vdsk.c revision 1.15 1 /* $NetBSD: vdsk.c,v 1.15 2024/06/19 20:00:10 palle Exp $ */
2 /* $OpenBSD: vdsk.c,v 1.46 2015/01/25 21:42:13 kettenis Exp $ */
3 /*
4 * Copyright (c) 2009, 2011 Mark Kettenis
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/kmem.h>
20 #include <sys/param.h>
21 #include <sys/buf.h>
22 #include <sys/device.h>
23 #include <sys/systm.h>
24
25 #include <machine/autoconf.h>
26 #include <machine/hypervisor.h>
27
28 #include <uvm/uvm_extern.h>
29
30 #include <dev/scsipi/scsi_all.h>
31 #include <dev/scsipi/scsipi_disk.h>
32 #include <dev/scsipi/scsipi_cd.h>
33 #include <dev/scsipi/scsiconf.h>
34
35 #include <dev/scsipi/scsi_disk.h>
36 #include <dev/scsipi/scsipi_all.h>
37 #include <dev/scsipi/scsiconf.h>
38 #include <dev/scsipi/scsi_message.h>
39
40 #include <sparc64/dev/cbusvar.h>
41 #include <sparc64/dev/ldcvar.h>
42 #include <sparc64/dev/viovar.h>
43
44 #ifdef VDSK_DEBUG
45 #define DPRINTF(x) printf x
46 #else
47 #define DPRINTF(x)
48 #endif
49
50 #define VDSK_TX_ENTRIES 32
51 #define VDSK_RX_ENTRIES 32
52
53 struct vd_attr_info {
54 struct vio_msg_tag tag;
55 uint8_t xfer_mode;
56 uint8_t vd_type;
57 uint8_t vd_mtype;
58 uint8_t _reserved1;
59 uint32_t vdisk_block_size;
60 uint64_t operations;
61 uint64_t vdisk_size;
62 uint64_t max_xfer_sz;
63 uint64_t _reserved2[2];
64 };
65
66 #define VD_DISK_TYPE_SLICE 0x01
67 #define VD_DISK_TYPE_DISK 0x02
68
69 #define VD_MEDIA_TYPE_FIXED 0x01
70 #define VD_MEDIA_TYPE_CD 0x02
71 #define VD_MEDIA_TYPE_DVD 0x03
72
73 /* vDisk version 1.0. */
74 #define VD_OP_BREAD 0x01
75 #define VD_OP_BWRITE 0x02
76 #define VD_OP_FLUSH 0x03
77 #define VD_OP_GET_WCE 0x04
78 #define VD_OP_SET_WCE 0x05
79 #define VD_OP_GET_VTOC 0x06
80 #define VD_OP_SET_VTOC 0x07
81 #define VD_OP_GET_DISKGEOM 0x08
82 #define VD_OP_SET_DISKGEOM 0x09
83 #define VD_OP_GET_DEVID 0x0b
84 #define VD_OP_GET_EFI 0x0c
85 #define VD_OP_SET_EFI 0x0d
86
87 /* vDisk version 1.1 */
88 #define VD_OP_SCSICMD 0x0a
89 #define VD_OP_RESET 0x0e
90 #define VD_OP_GET_ACCESS 0x0f
91 #define VD_OP_SET_ACCESS 0x10
92 #define VD_OP_GET_CAPACITY 0x11
93
94 struct vd_desc {
95 struct vio_dring_hdr hdr;
96 uint64_t req_id;
97 uint8_t operation;
98 uint8_t slice;
99 uint16_t _reserved1;
100 uint32_t status;
101 uint64_t offset;
102 uint64_t size;
103 uint32_t ncookies;
104 uint32_t _reserved2;
105 struct ldc_cookie cookie[MAXPHYS / PAGE_SIZE];
106 };
107
108 #define VD_SLICE_NONE 0xff
109
110 struct vdsk_dring {
111 bus_dmamap_t vd_map;
112 bus_dma_segment_t vd_seg;
113 struct vd_desc *vd_desc;
114 int vd_nentries;
115 };
116
117 #if OPENBSD_BUSDMA
118 struct vdsk_dring *vdsk_dring_alloc(bus_dma_tag_t, int);
119 void vdsk_dring_free(bus_dma_tag_t, struct vdsk_dring *);
120 #else
121 struct vdsk_dring *vdsk_dring_alloc(int);
122 void vdsk_dring_free(struct vdsk_dring *);
123 #endif
124
125 /*
126 * We support vDisk 1.0 and 1.1.
127 */
128 #define VDSK_MAJOR 1
129 #define VDSK_MINOR 1
130
131 struct vdsk_soft_desc {
132 int vsd_map_idx[MAXPHYS / PAGE_SIZE];
133 struct scsipi_xfer *vsd_xs;
134 int vsd_ncookies;
135 };
136
137 struct vdsk_softc {
138 device_t sc_dv;
139
140 struct scsipi_adapter sc_adapter;
141 struct scsipi_channel sc_channel;
142
143 bus_space_tag_t sc_bustag;
144 bus_dma_tag_t sc_dmatag;
145
146 void *sc_tx_ih;
147 void *sc_rx_ih;
148
149 struct ldc_conn sc_lc;
150
151 uint16_t sc_vio_state;
152 #define VIO_SND_VER_INFO 0x0001
153 #define VIO_ACK_VER_INFO 0x0002
154 #define VIO_SND_ATTR_INFO 0x0004
155 #define VIO_ACK_ATTR_INFO 0x0008
156 #define VIO_SND_DRING_REG 0x0010
157 #define VIO_ACK_DRING_REG 0x0020
158 #define VIO_SND_RDX 0x0040
159 #define VIO_ACK_RDX 0x0080
160 #define VIO_ESTABLISHED 0x00ff
161
162 uint16_t sc_major;
163 uint16_t sc_minor;
164
165 uint32_t sc_local_sid;
166 uint64_t sc_dring_ident;
167 uint64_t sc_seq_no;
168
169 int sc_tx_cnt;
170 int sc_tx_prod;
171 int sc_tx_cons;
172
173 struct ldc_map *sc_lm;
174 struct vdsk_dring *sc_vd;
175 struct vdsk_soft_desc *sc_vsd;
176
177 uint32_t sc_vdisk_block_size;
178 uint64_t sc_vdisk_size;
179 uint8_t sc_vd_mtype;
180 };
181
182 int vdsk_match(device_t, cfdata_t, void *);
183 void vdsk_attach(device_t, device_t, void *);
184 void vdsk_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
185 void *);
186
187 CFATTACH_DECL_NEW(vdsk, sizeof(struct vdsk_softc),
188 vdsk_match, vdsk_attach, NULL, NULL);
189
190 int vdsk_tx_intr(void *);
191 int vdsk_rx_intr(void *);
192
193 void vdsk_rx_data(struct ldc_conn *, struct ldc_pkt *);
194 void vdsk_rx_vio_ctrl(struct vdsk_softc *, struct vio_msg *);
195 void vdsk_rx_vio_ver_info(struct vdsk_softc *, struct vio_msg_tag *);
196 void vdsk_rx_vio_attr_info(struct vdsk_softc *, struct vio_msg_tag *);
197 void vdsk_rx_vio_dring_reg(struct vdsk_softc *, struct vio_msg_tag *);
198 void vdsk_rx_vio_rdx(struct vdsk_softc *sc, struct vio_msg_tag *);
199 void vdsk_rx_vio_data(struct vdsk_softc *sc, struct vio_msg *);
200 void vdsk_rx_vio_dring_data(struct vdsk_softc *sc, struct vio_msg_tag *);
201
202 void vdsk_ldc_reset(struct ldc_conn *);
203 void vdsk_ldc_start(struct ldc_conn *);
204
205 void vdsk_sendmsg(struct vdsk_softc *, void *, size_t);
206 void vdsk_send_ver_info(struct vdsk_softc *, uint16_t, uint16_t);
207 void vdsk_send_attr_info(struct vdsk_softc *);
208 void vdsk_send_dring_reg(struct vdsk_softc *);
209 void vdsk_send_rdx(struct vdsk_softc *);
210
211 void *vdsk_io_get(void *);
212 void vdsk_io_put(void *, void *);
213
214 void vdsk_scsi_cmd(struct vdsk_softc *sc, struct scsipi_xfer *);
215 int vdsk_submit_cmd(struct vdsk_softc *sc, struct scsipi_xfer *);
216 void vdsk_complete_cmd(struct vdsk_softc *sc, struct scsipi_xfer *, int);
217 void vdsk_scsi_inq(struct vdsk_softc *sc, struct scsipi_xfer *);
218 void vdsk_scsi_inquiry(struct vdsk_softc *sc, struct scsipi_xfer *);
219 void vdsk_scsi_capacity(struct vdsk_softc *sc, struct scsipi_xfer *);
220 void vdsk_scsi_capacity16(struct vdsk_softc *sc, struct scsipi_xfer *);
221 void vdsk_scsi_report_luns(struct vdsk_softc *sc, struct scsipi_xfer *);
222 void vdsk_scsi_read_discinfo(struct vdsk_softc *sc, struct scsipi_xfer *);
223 void vdsk_scsi_read_trackinfo(struct vdsk_softc *sc, struct scsipi_xfer *);
224 void vdsk_scsi_done(struct scsipi_xfer *, int);
225
226 int
227 vdsk_match(device_t parent, cfdata_t match, void *aux)
228 {
229 struct cbus_attach_args *ca = aux;
230
231 if (strcmp(ca->ca_name, "disk") == 0)
232 return (1);
233
234 return (0);
235 }
236
237 void
238 vdsk_attach(device_t parent, device_t self, void *aux)
239 {
240 struct vdsk_softc *sc = device_private(self);
241 struct cbus_attach_args *ca = aux;
242 struct ldc_conn *lc;
243 int err, s;
244 int timeout;
245 vaddr_t va;
246 paddr_t pa;
247
248 sc->sc_bustag = ca->ca_bustag;
249 sc->sc_dmatag = ca->ca_dmatag;
250
251 printf(": ivec 0x%llx, 0x%llx",
252 (long long unsigned int)ca->ca_tx_ino,
253 (long long unsigned int)ca->ca_rx_ino);
254
255 /*
256 * Un-configure queues before registering interrupt handlers,
257 * such that we don't get any stale LDC packets or events.
258 */
259 hv_ldc_tx_qconf(ca->ca_id, 0, 0);
260 hv_ldc_rx_qconf(ca->ca_id, 0, 0);
261
262 sc->sc_tx_ih = bus_intr_establish(ca->ca_bustag, ca->ca_tx_ino,
263 IPL_BIO, vdsk_tx_intr, sc);
264 sc->sc_rx_ih = bus_intr_establish(ca->ca_bustag, ca->ca_rx_ino,
265 IPL_BIO, vdsk_rx_intr, sc);
266 if (sc->sc_tx_ih == NULL || sc->sc_rx_ih == NULL) {
267 printf(", can't establish interrupt\n");
268 return;
269 }
270
271 lc = &sc->sc_lc;
272 lc->lc_id = ca->ca_id;
273 lc->lc_sc = sc;
274 lc->lc_reset = vdsk_ldc_reset;
275 lc->lc_start = vdsk_ldc_start;
276 lc->lc_rx_data = vdsk_rx_data;
277
278 #if OPENBSD_BUSDMA
279 lc->lc_txq = ldc_queue_alloc(sc->sc_dmatag, VDSK_TX_ENTRIES);
280 #else
281 lc->lc_txq = ldc_queue_alloc(VDSK_TX_ENTRIES);
282 #endif
283 #if OPENBSD_BUSDMA
284 lc->lc_rxq = ldc_queue_alloc(sc->sc_dmatag, VDSK_RX_ENTRIES);
285 #else
286 lc->lc_rxq = ldc_queue_alloc(VDSK_RX_ENTRIES);
287 #endif
288 #if OPENBSD_BUSDMA
289 sc->sc_lm = ldc_map_alloc(sc->sc_dmatag, 2048);
290 #else
291 sc->sc_lm = ldc_map_alloc(2048);
292 #endif
293 #if OPENBSD_BUSDMA
294 err = hv_ldc_set_map_table(lc->lc_id,
295 sc->sc_lm->lm_map->dm_segs[0].ds_addr, sc->sc_lm->lm_nentries);
296 #else
297 va = (vaddr_t)sc->sc_lm->lm_slot;
298 pa = 0;
299 if (pmap_extract(pmap_kernel(), va, &pa) == FALSE)
300 panic("pmap_extract failed %lx\n", va);
301 err = hv_ldc_set_map_table(lc->lc_id, pa, 2048);
302 #endif
303 if (err != H_EOK) {
304 printf("hv_ldc_set_map_table %d\n", err);
305 goto free_map;
306 }
307 #if OPENBSD_BUSDMA
308 sc->sc_vd = vdsk_dring_alloc(sc->sc_dmatag, 32);
309 #else
310 sc->sc_vd = vdsk_dring_alloc(32);
311 #endif
312 sc->sc_vsd = kmem_zalloc(32 * sizeof(*sc->sc_vsd), KM_SLEEP);
313
314 #if OPENBSD_BUSDMA
315 sc->sc_lm->lm_slot[0].entry = sc->sc_vd->vd_map->dm_segs[0].ds_addr;
316 #else
317 va = (vaddr_t)sc->sc_vd->vd_desc;
318 pa = 0;
319 if (pmap_extract(pmap_kernel(), va, &pa) == FALSE)
320 panic("pmap_extract failed %lx\n", va);
321
322 sc->sc_lm->lm_slot[0].entry = pa;
323 #endif
324 sc->sc_lm->lm_slot[0].entry &= LDC_MTE_RA_MASK;
325 sc->sc_lm->lm_slot[0].entry |= LDC_MTE_CPR | LDC_MTE_CPW;
326 sc->sc_lm->lm_slot[0].entry |= LDC_MTE_R | LDC_MTE_W;
327 sc->sc_lm->lm_next = 1;
328 sc->sc_lm->lm_count = 1;
329 va = lc->lc_txq->lq_va;
330 pa = 0;
331 if (pmap_extract(pmap_kernel(), va, &pa) == FALSE)
332 panic("pmap_extract failed %lx\n", va);
333 #if OPENBSD_BUSDMA
334 err = hv_ldc_tx_qconf(lc->lc_id,
335 lc->lc_txq->lq_map->dm_segs[0].ds_addr, lc->lc_txq->lq_nentries);
336 #else
337 err = hv_ldc_tx_qconf(lc->lc_id, pa, lc->lc_txq->lq_nentries);
338 #endif
339 if (err != H_EOK)
340 printf("hv_ldc_tx_qconf %d\n", err);
341 va = (vaddr_t)lc->lc_rxq->lq_va;
342 pa = 0;
343 if (pmap_extract(pmap_kernel(), va, &pa) == FALSE)
344 panic("pmap_extract failed %lx\n", va);
345 #if OPENBSD_BUSDMA
346 err = hv_ldc_rx_qconf(lc->lc_id,
347 lc->lc_rxq->lq_map->dm_segs[0].ds_addr, lc->lc_rxq->lq_nentries);
348 #else
349 err = hv_ldc_rx_qconf(lc->lc_id, pa, lc->lc_rxq->lq_nentries);
350 #endif
351 if (err != H_EOK)
352 printf("hv_ldc_rx_qconf %d\n", err);
353
354 cbus_intr_setenabled(sc->sc_bustag, ca->ca_tx_ino, INTR_ENABLED);
355 cbus_intr_setenabled(sc->sc_bustag, ca->ca_rx_ino, INTR_ENABLED);
356
357 ldc_send_vers(lc);
358
359 printf("\n");
360
361 /*
362 * Interrupts aren't enabled during autoconf, so poll for VIO
363 * peer-to-peer handshake completion.
364 */
365 s = splbio();
366 timeout = 10 * 1000;
367 do {
368 if (vdsk_rx_intr(sc) && sc->sc_vio_state == VIO_ESTABLISHED)
369 break;
370
371 delay(1000);
372 } while(--timeout > 0);
373 splx(s);
374
375 if (sc->sc_vio_state != VIO_ESTABLISHED) {
376 printf("vio not established: %d\n", sc->sc_vio_state);
377 return;
378 }
379
380 sc->sc_dv = self;
381
382 sc->sc_adapter.adapt_dev = sc->sc_dv;
383 sc->sc_adapter.adapt_nchannels = 1;
384 sc->sc_adapter.adapt_openings = sc->sc_vd->vd_nentries - 1;
385 sc->sc_adapter.adapt_max_periph = sc->sc_vd->vd_nentries - 1;
386
387 sc->sc_adapter.adapt_minphys = minphys;
388 sc->sc_adapter.adapt_request = vdsk_scsipi_request;
389
390 sc->sc_channel.chan_adapter = &sc->sc_adapter;
391 sc->sc_channel.chan_bustype = &scsi_bustype;
392 sc->sc_channel.chan_channel = 0;
393 sc->sc_channel.chan_ntargets = 2; /* XXX why not 1? */
394 sc->sc_channel.chan_nluns = 1; /* XXX slices should be presented as luns? */
395 sc->sc_channel.chan_id = 0;
396 sc->sc_channel.chan_flags = SCSIPI_CHAN_NOSETTLE;
397
398 config_found(self, &sc->sc_channel, scsiprint, CFARGS_NONE);
399
400 return;
401
402 free_map:
403 hv_ldc_set_map_table(lc->lc_id, 0, 0);
404 #if OPENBSD_BUSDMA
405 ldc_map_free(sc->sc_dmatag, sc->sc_lm);
406 #else
407 ldc_map_free(sc->sc_lm);
408 #endif
409 }
410
411 void
412 vdsk_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
413 void *arg)
414 {
415
416 struct vdsk_softc *sc;
417 struct scsipi_xfer *xs;
418
419 sc = device_private(chan->chan_adapter->adapt_dev);
420
421 xs = arg;
422
423 switch (req) {
424 case ADAPTER_REQ_RUN_XFER:
425 vdsk_scsi_cmd(sc, xs);
426 break;
427 case ADAPTER_REQ_GROW_RESOURCES:
428 case ADAPTER_REQ_SET_XFER_MODE:
429 /* Ignored */
430 break;
431 default:
432 panic("req unhandled: %x", req);
433 }
434
435 }
436
437 int
438 vdsk_tx_intr(void *arg)
439 {
440 panic("%s: not verified yet", __FUNCTION__);
441
442 struct vdsk_softc *sc = arg;
443 struct ldc_conn *lc = &sc->sc_lc;
444 uint64_t tx_head, tx_tail, tx_state;
445
446 hv_ldc_tx_get_state(lc->lc_id, &tx_head, &tx_tail, &tx_state);
447 if (tx_state != lc->lc_tx_state) {
448 switch (tx_state) {
449 case LDC_CHANNEL_DOWN:
450 DPRINTF(("Tx link down\n"));
451 break;
452 case LDC_CHANNEL_UP:
453 DPRINTF(("Tx link up\n"));
454 break;
455 case LDC_CHANNEL_RESET:
456 DPRINTF(("Tx link reset\n"));
457 break;
458 }
459 lc->lc_tx_state = tx_state;
460 }
461
462 return (1);
463 }
464
465 int
466 vdsk_rx_intr(void *arg)
467 {
468 struct vdsk_softc *sc = arg;
469 struct ldc_conn *lc = &sc->sc_lc;
470 uint64_t rx_head, rx_tail, rx_state;
471 struct ldc_pkt *lp;
472 int err;
473
474 err = hv_ldc_rx_get_state(lc->lc_id, &rx_head, &rx_tail, &rx_state);
475 if (err == H_EINVAL) {
476 printf("hv_ldc_rx_get_state H_EINVAL\n");
477 return (0);
478 }
479 if (err != H_EOK) {
480 printf("hv_ldc_rx_get_state %d\n", err);
481 return (0);
482 }
483
484 if (rx_state != lc->lc_rx_state) {
485 sc->sc_vio_state = 0;
486 lc->lc_tx_seqid = 0;
487 lc->lc_state = 0;
488 switch (rx_state) {
489 case LDC_CHANNEL_DOWN:
490 DPRINTF(("Rx link down\n"));
491 break;
492 case LDC_CHANNEL_UP:
493 DPRINTF(("Rx link up\n"));
494 ldc_send_vers(lc);
495 break;
496 case LDC_CHANNEL_RESET:
497 DPRINTF(("Rx link reset\n"));
498 ldc_send_vers(lc);
499 break;
500 }
501 lc->lc_rx_state = rx_state;
502 hv_ldc_rx_set_qhead(lc->lc_id, rx_tail);
503 return (1);
504 }
505
506 if (rx_head == rx_tail)
507 return (0);
508
509 lp = (struct ldc_pkt *)(uintptr_t)(lc->lc_rxq->lq_va + rx_head);
510 switch (lp->type) {
511 case LDC_CTRL:
512 ldc_rx_ctrl(lc, lp);
513 break;
514
515 case LDC_DATA:
516 ldc_rx_data(lc, lp);
517 break;
518
519 default:
520 DPRINTF(("%0x02/%0x02/%0x02\n", lp->type, lp->stype,
521 lp->ctrl));
522 ldc_reset(lc);
523 break;
524 }
525
526 if (lc->lc_state == 0)
527 return (1);
528
529 rx_head += sizeof(*lp);
530 rx_head &= ((lc->lc_rxq->lq_nentries * sizeof(*lp)) - 1);
531 err = hv_ldc_rx_set_qhead(lc->lc_id, rx_head);
532 if (err != H_EOK)
533 printf("%s: hv_ldc_rx_set_qhead %d\n", __func__, err);
534
535 return (1);
536 }
537
538 void
539 vdsk_rx_data(struct ldc_conn *lc, struct ldc_pkt *lp)
540 {
541 struct vio_msg *vm = (struct vio_msg *)lp;
542
543 switch (vm->type) {
544 case VIO_TYPE_CTRL:
545 if ((lp->env & LDC_FRAG_START) == 0 &&
546 (lp->env & LDC_FRAG_STOP) == 0)
547 return;
548 vdsk_rx_vio_ctrl(lc->lc_sc, vm);
549 break;
550
551 case VIO_TYPE_DATA:
552 if((lp->env & LDC_FRAG_START) == 0)
553 return;
554 vdsk_rx_vio_data(lc->lc_sc, vm);
555 break;
556
557 default:
558 DPRINTF(("Unhandled packet type 0x%02x\n", vm->type));
559 ldc_reset(lc);
560 break;
561 }
562 }
563
564 void
565 vdsk_rx_vio_ctrl(struct vdsk_softc *sc, struct vio_msg *vm)
566 {
567 struct vio_msg_tag *tag = (struct vio_msg_tag *)&vm->type;
568
569 switch (tag->stype_env) {
570 case VIO_VER_INFO:
571 vdsk_rx_vio_ver_info(sc, tag);
572 break;
573 case VIO_ATTR_INFO:
574 vdsk_rx_vio_attr_info(sc, tag);
575 break;
576 case VIO_DRING_REG:
577 vdsk_rx_vio_dring_reg(sc, tag);
578 break;
579 case VIO_RDX:
580 vdsk_rx_vio_rdx(sc, tag);
581 break;
582 default:
583 DPRINTF(("CTRL/0x%02x/0x%04x\n", tag->stype, tag->stype_env));
584 break;
585 }
586 }
587
588 void
589 vdsk_rx_vio_ver_info(struct vdsk_softc *sc, struct vio_msg_tag *tag)
590 {
591 struct vio_ver_info *vi = (struct vio_ver_info *)tag;
592
593 switch (vi->tag.stype) {
594 case VIO_SUBTYPE_INFO:
595 DPRINTF(("CTRL/INFO/VER_INFO\n"));
596 break;
597
598 case VIO_SUBTYPE_ACK:
599 DPRINTF(("CTRL/ACK/VER_INFO\n"));
600 if (!ISSET(sc->sc_vio_state, VIO_SND_VER_INFO)) {
601 ldc_reset(&sc->sc_lc);
602 break;
603 }
604 sc->sc_major = vi->major;
605 sc->sc_minor = vi->minor;
606 sc->sc_vio_state |= VIO_ACK_VER_INFO;
607 break;
608
609 default:
610 DPRINTF(("CTRL/0x%02x/VER_INFO\n", vi->tag.stype));
611 break;
612 }
613
614 if (ISSET(sc->sc_vio_state, VIO_ACK_VER_INFO))
615 vdsk_send_attr_info(sc);
616 }
617
618 void
619 vdsk_rx_vio_attr_info(struct vdsk_softc *sc, struct vio_msg_tag *tag)
620 {
621 struct vd_attr_info *ai = (struct vd_attr_info *)tag;
622
623 switch (ai->tag.stype) {
624 case VIO_SUBTYPE_INFO:
625 DPRINTF(("CTRL/INFO/ATTR_INFO\n"));
626 break;
627
628 case VIO_SUBTYPE_ACK:
629 DPRINTF(("CTRL/ACK/ATTR_INFO\n"));
630 if (!ISSET(sc->sc_vio_state, VIO_SND_ATTR_INFO)) {
631 ldc_reset(&sc->sc_lc);
632 break;
633 }
634
635 sc->sc_vdisk_block_size = ai->vdisk_block_size;
636 sc->sc_vdisk_size = ai->vdisk_size;
637 if (sc->sc_major > 1 || sc->sc_minor >= 1)
638 sc->sc_vd_mtype = ai->vd_mtype;
639 else
640 sc->sc_vd_mtype = VD_MEDIA_TYPE_FIXED;
641
642 sc->sc_vio_state |= VIO_ACK_ATTR_INFO;
643 break;
644
645 default:
646 DPRINTF(("CTRL/0x%02x/ATTR_INFO\n", ai->tag.stype));
647 break;
648 }
649
650 if (ISSET(sc->sc_vio_state, VIO_ACK_ATTR_INFO))
651 vdsk_send_dring_reg(sc);
652
653 }
654
655 void
656 vdsk_rx_vio_dring_reg(struct vdsk_softc *sc, struct vio_msg_tag *tag)
657 {
658 struct vio_dring_reg *dr = (struct vio_dring_reg *)tag;
659
660 switch (dr->tag.stype) {
661 case VIO_SUBTYPE_INFO:
662 DPRINTF(("CTRL/INFO/DRING_REG\n"));
663 break;
664
665 case VIO_SUBTYPE_ACK:
666 DPRINTF(("CTRL/ACK/DRING_REG\n"));
667 if (!ISSET(sc->sc_vio_state, VIO_SND_DRING_REG)) {
668 ldc_reset(&sc->sc_lc);
669 break;
670 }
671
672 sc->sc_dring_ident = dr->dring_ident;
673 sc->sc_seq_no = 1;
674
675 sc->sc_vio_state |= VIO_ACK_DRING_REG;
676 break;
677
678 default:
679 DPRINTF(("CTRL/0x%02x/DRING_REG\n", dr->tag.stype));
680 break;
681 }
682
683 if (ISSET(sc->sc_vio_state, VIO_ACK_DRING_REG))
684 vdsk_send_rdx(sc);
685 }
686
687 void
688 vdsk_rx_vio_rdx(struct vdsk_softc *sc, struct vio_msg_tag *tag)
689 {
690 switch(tag->stype) {
691 case VIO_SUBTYPE_INFO:
692 DPRINTF(("CTRL/INFO/RDX\n"));
693 break;
694
695 case VIO_SUBTYPE_ACK:
696 {
697 int prod;
698
699 DPRINTF(("CTRL/ACK/RDX\n"));
700 if (!ISSET(sc->sc_vio_state, VIO_SND_RDX)) {
701 ldc_reset(&sc->sc_lc);
702 break;
703 }
704 sc->sc_vio_state |= VIO_ACK_RDX;
705
706 /*
707 * If this ACK is the result of a reconnect, we may
708 * have pending I/O that we need to resubmit. We need
709 * to rebuild the ring descriptors though since the
710 * vDisk server on the other side may have touched
711 * them already. So we just clean up the ring and the
712 * LDC map and resubmit the SCSI commands based on our
713 * soft descriptors.
714 */
715 prod = sc->sc_tx_prod;
716 sc->sc_tx_prod = sc->sc_tx_cons;
717 sc->sc_tx_cnt = 0;
718 sc->sc_lm->lm_next = 1;
719 sc->sc_lm->lm_count = 1;
720 while (sc->sc_tx_prod != prod)
721 vdsk_submit_cmd(sc, sc->sc_vsd[sc->sc_tx_prod].vsd_xs);
722 break;
723 }
724
725 default:
726 DPRINTF(("CTRL/0x%02x/RDX (VIO)\n", tag->stype));
727 break;
728 }
729 }
730
731 void
732 vdsk_rx_vio_data(struct vdsk_softc *sc, struct vio_msg *vm)
733 {
734 struct vio_msg_tag *tag = (struct vio_msg_tag *)&vm->type;
735
736 if (sc->sc_vio_state != VIO_ESTABLISHED) {
737 DPRINTF(("Spurious DATA/0x%02x/0x%04x\n", tag->stype,
738 tag->stype_env));
739 return;
740 }
741
742 switch(tag->stype_env) {
743 case VIO_DRING_DATA:
744 vdsk_rx_vio_dring_data(sc, tag);
745 break;
746
747 default:
748 DPRINTF(("DATA/0x%02x/0x%04x\n", tag->stype, tag->stype_env));
749 break;
750 }
751 }
752
753 void
754 vdsk_rx_vio_dring_data(struct vdsk_softc *sc, struct vio_msg_tag *tag)
755 {
756 switch(tag->stype) {
757 case VIO_SUBTYPE_INFO:
758 DPRINTF(("DATA/INFO/DRING_DATA\n"));
759 break;
760
761 case VIO_SUBTYPE_ACK:
762 {
763 struct scsipi_xfer *xs;
764 int cons;
765
766 cons = sc->sc_tx_cons;
767 while (sc->sc_vd->vd_desc[cons].hdr.dstate == VIO_DESC_DONE) {
768 xs = sc->sc_vsd[cons].vsd_xs;
769 if (ISSET(xs->xs_control, XS_CTL_POLL) == 0)
770 vdsk_complete_cmd(sc, xs, cons);
771 cons++;
772 cons &= (sc->sc_vd->vd_nentries - 1);
773 }
774 sc->sc_tx_cons = cons;
775 break;
776 }
777
778 case VIO_SUBTYPE_NACK:
779 DPRINTF(("DATA/NACK/DRING_DATA\n"));
780 struct ldc_conn *lc = &sc->sc_lc;
781 ldc_send_vers(lc);
782 break;
783
784 default:
785 DPRINTF(("DATA/0x%02x/DRING_DATA\n", tag->stype));
786 break;
787 }
788 }
789
790 void
791 vdsk_ldc_reset(struct ldc_conn *lc)
792 {
793 struct vdsk_softc *sc = lc->lc_sc;
794
795 sc->sc_vio_state = 0;
796 }
797
798 void
799 vdsk_ldc_start(struct ldc_conn *lc)
800 {
801 struct vdsk_softc *sc = lc->lc_sc;
802
803 vdsk_send_ver_info(sc, VDSK_MAJOR, VDSK_MINOR);
804 }
805
806 void
807 vdsk_sendmsg(struct vdsk_softc *sc, void *msg, size_t len)
808 {
809 struct ldc_conn *lc = &sc->sc_lc;
810 int err;
811
812 err = ldc_send_unreliable(lc, msg, len);
813 if (err)
814 printf("%s: ldc_send_unreliable: %d\n", __func__, err);
815 }
816
817 void
818 vdsk_send_ver_info(struct vdsk_softc *sc, uint16_t major, uint16_t minor)
819 {
820 struct vio_ver_info vi;
821
822 /* Allocate new session ID. */
823 sc->sc_local_sid = gettick();
824
825 bzero(&vi, sizeof(vi));
826 vi.tag.type = VIO_TYPE_CTRL;
827 vi.tag.stype = VIO_SUBTYPE_INFO;
828 vi.tag.stype_env = VIO_VER_INFO;
829 vi.tag.sid = sc->sc_local_sid;
830 vi.major = major;
831 vi.minor = minor;
832 vi.dev_class = VDEV_DISK;
833 vdsk_sendmsg(sc, &vi, sizeof(vi));
834
835 sc->sc_vio_state |= VIO_SND_VER_INFO;
836 }
837
838 void
839 vdsk_send_attr_info(struct vdsk_softc *sc)
840 {
841 struct vd_attr_info ai;
842
843 bzero(&ai, sizeof(ai));
844 ai.tag.type = VIO_TYPE_CTRL;
845 ai.tag.stype = VIO_SUBTYPE_INFO;
846 ai.tag.stype_env = VIO_ATTR_INFO;
847 ai.tag.sid = sc->sc_local_sid;
848 ai.xfer_mode = VIO_DRING_MODE;
849 ai.vdisk_block_size = DEV_BSIZE;
850 ai.max_xfer_sz = MAXPHYS / DEV_BSIZE;
851 vdsk_sendmsg(sc, &ai, sizeof(ai));
852
853 sc->sc_vio_state |= VIO_SND_ATTR_INFO;
854 }
855
856 void
857 vdsk_send_dring_reg(struct vdsk_softc *sc)
858 {
859 struct vio_dring_reg dr;
860
861 bzero(&dr, sizeof(dr));
862 dr.tag.type = VIO_TYPE_CTRL;
863 dr.tag.stype = VIO_SUBTYPE_INFO;
864 dr.tag.stype_env = VIO_DRING_REG;
865 dr.tag.sid = sc->sc_local_sid;
866 dr.dring_ident = 0;
867 dr.num_descriptors = sc->sc_vd->vd_nentries;
868 dr.descriptor_size = sizeof(struct vd_desc);
869 dr.options = VIO_TX_RING | VIO_RX_RING;
870 dr.ncookies = 1;
871 dr.cookie[0].addr = 0;
872 dr.cookie[0].size = PAGE_SIZE;
873 vdsk_sendmsg(sc, &dr, sizeof(dr));
874
875 sc->sc_vio_state |= VIO_SND_DRING_REG;
876 };
877
878 void
879 vdsk_send_rdx(struct vdsk_softc *sc)
880 {
881 struct vio_rdx rdx;
882
883 bzero(&rdx, sizeof(rdx));
884 rdx.tag.type = VIO_TYPE_CTRL;
885 rdx.tag.stype = VIO_SUBTYPE_INFO;
886 rdx.tag.stype_env = VIO_RDX;
887 rdx.tag.sid = sc->sc_local_sid;
888 vdsk_sendmsg(sc, &rdx, sizeof(rdx));
889
890 sc->sc_vio_state |= VIO_SND_RDX;
891 }
892
893 #if OPENBSD_BUSDMA
894 struct vdsk_dring *
895 vdsk_dring_alloc(bus_dma_tag_t t, int nentries)
896 #else
897 struct vdsk_dring *
898 vdsk_dring_alloc(int nentries)
899 #endif
900 {
901
902 struct vdsk_dring *vd;
903 bus_size_t size;
904 vaddr_t va;
905 #if OPENBSD_BUSDMA
906 int nsegs;
907 #endif
908 int i;
909
910 vd = kmem_zalloc(sizeof(struct vdsk_dring), KM_SLEEP);
911
912 size = roundup(nentries * sizeof(struct vd_desc), PAGE_SIZE);
913
914 #if OPENBSD_BUSDMA
915 if (bus_dmamap_create(t, size, 1, size, 0,
916 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &vd->vd_map) != 0)
917 return (NULL);
918
919 if (bus_dmamem_alloc(t, size, PAGE_SIZE, 0, &vd->vd_seg, 1,
920 &nsegs, BUS_DMA_NOWAIT) != 0)
921 goto destroy;
922
923 if (bus_dmamem_map(t, &vd->vd_seg, 1, size, (void*)&va,
924 BUS_DMA_NOWAIT) != 0)
925 goto free;
926
927 if (bus_dmamap_load(t, vd->vd_map, (void*)va, size, NULL,
928 BUS_DMA_NOWAIT) != 0)
929 goto unmap;
930 #else
931 va = (vaddr_t)kmem_zalloc(size, KM_SLEEP);
932 #endif
933 vd->vd_desc = (struct vd_desc *)va;
934 vd->vd_nentries = nentries;
935 bzero(vd->vd_desc, nentries * sizeof(struct vd_desc));
936 for (i = 0; i < vd->vd_nentries; i++)
937 vd->vd_desc[i].hdr.dstate = VIO_DESC_FREE;
938 return (vd);
939
940 #if OPENBSD_BUSDMA
941 unmap:
942 bus_dmamem_unmap(t, (void*)va, size);
943 free:
944 bus_dmamem_free(t, &vd->vd_seg, 1);
945 destroy:
946 bus_dmamap_destroy(t, vd->vd_map);
947 #endif
948 return (NULL);
949 }
950
951 #if OPENBSD_BUSDMA
952 void
953 vdsk_dring_free(bus_dma_tag_t t, struct vdsk_dring *vd)
954 #else
955 void
956 vdsk_dring_free(struct vdsk_dring *vd)
957 #endif
958 {
959
960 bus_size_t size;
961
962 size = vd->vd_nentries * sizeof(struct vd_desc);
963 size = roundup(size, PAGE_SIZE);
964
965 #if OPENBSD_BUSDMA
966 bus_dmamap_unload(t, vd->vd_map);
967
968 bus_dmamem_unmap(t, (caddr_t)vd->vd_desc, size);
969 bus_dmamem_free(t, &vd->vd_seg, 1);
970 bus_dmamap_destroy(t, vd->vd_map);
971 #else
972 kmem_free(vd->vd_desc, size);
973 #endif
974 kmem_free(vd, size);
975 }
976
977 void *
978 vdsk_io_get(void *xsc)
979 {
980
981 panic("%s: not verified yet", __FUNCTION__);
982
983 struct vdsk_softc *sc = xsc;
984 void *rv = sc; /* just has to be !NULL */
985 int s;
986
987 s = splbio();
988 if (sc->sc_vio_state != VIO_ESTABLISHED ||
989 sc->sc_tx_cnt >= sc->sc_vd->vd_nentries)
990 rv = NULL;
991 else
992 sc->sc_tx_cnt++;
993 splx(s);
994
995 return (rv);
996 }
997
998 void
999 vdsk_io_put(void *xsc, void *io)
1000 {
1001
1002 panic("%s: not verified yet", __FUNCTION__);
1003
1004 struct vdsk_softc *sc = xsc;
1005 int s;
1006
1007 #ifdef DIAGNOSTIC
1008 if (sc != io)
1009 panic("vsdk_io_put: unexpected io");
1010 #endif
1011
1012 s = splbio();
1013 sc->sc_tx_cnt--;
1014 splx(s);
1015 }
1016
1017 void
1018 vdsk_scsi_cmd(struct vdsk_softc *sc, struct scsipi_xfer *xs)
1019 {
1020 int timeout, s;
1021 int desc;
1022
1023 DPRINTF(("vdsk_scsi_cmd() opcode %x\n", xs->cmd->opcode));
1024
1025 switch (xs->cmd->opcode) {
1026
1027 case SCSI_READ_6_COMMAND:
1028 case READ_10:
1029 case READ_12:
1030 case READ_16:
1031 case SCSI_WRITE_6_COMMAND:
1032 case WRITE_10:
1033 case WRITE_12:
1034 case WRITE_16:
1035 case SCSI_SYNCHRONIZE_CACHE_10:
1036 break;
1037
1038 case INQUIRY:
1039 vdsk_scsi_inq(sc, xs);
1040 return;
1041
1042 case READ_CAPACITY_10:
1043 vdsk_scsi_capacity(sc, xs);
1044 return;
1045
1046 case READ_CAPACITY_16:
1047 vdsk_scsi_capacity16(sc, xs);
1048 return;
1049
1050 case SCSI_REPORT_LUNS:
1051 vdsk_scsi_report_luns(sc, xs);
1052 return;
1053
1054 case READ_DISCINFO:
1055 vdsk_scsi_read_discinfo(sc, xs);
1056 return;
1057
1058 case READ_TRACKINFO:
1059 vdsk_scsi_read_trackinfo(sc, xs);
1060 return;
1061
1062 case SCSI_TEST_UNIT_READY:
1063 case START_STOP:
1064 case SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL:
1065 case SCSI_MODE_SENSE_6:
1066 case SCSI_MAINTENANCE_IN:
1067 vdsk_scsi_done(xs, XS_NOERROR);
1068 return;
1069
1070 case SCSI_MODE_SENSE_10:
1071 case READ_TOC:
1072 vdsk_scsi_done(xs, XS_DRIVER_STUFFUP);
1073 return;
1074
1075 default:
1076 panic("%s unhandled cmd 0x%02x\n",
1077 __func__, xs->cmd->opcode);
1078 }
1079
1080 s = splbio();
1081 desc = vdsk_submit_cmd(sc, xs);
1082
1083 if (!ISSET(xs->xs_control, XS_CTL_POLL)) {
1084 splx(s);
1085 return;
1086 }
1087 timeout = 1000;
1088 do {
1089 if (sc->sc_vd->vd_desc[desc].hdr.dstate == VIO_DESC_DONE)
1090 break;
1091
1092 delay(1000);
1093 } while(--timeout > 0);
1094 if (sc->sc_vd->vd_desc[desc].hdr.dstate == VIO_DESC_DONE) {
1095 vdsk_complete_cmd(sc, xs, desc);
1096 } else {
1097 ldc_reset(&sc->sc_lc);
1098 vdsk_scsi_done(xs, XS_TIMEOUT);
1099 }
1100 splx(s);
1101 }
1102
1103 int
1104 vdsk_submit_cmd(struct vdsk_softc *sc, struct scsipi_xfer *xs)
1105 {
1106 struct ldc_map *map = sc->sc_lm;
1107 struct vio_dring_msg dm;
1108 struct scsi_rw_6 *rw6;
1109 struct scsipi_rw_10 *rw10;
1110 struct scsipi_rw_12 *rw12;
1111 struct scsipi_rw_16 *rw16;
1112 u_int64_t lba = 0;
1113 uint8_t operation;
1114 vaddr_t va;
1115 paddr_t pa;
1116 psize_t nbytes;
1117 int len, ncookies;
1118 int desc;
1119
1120 switch (xs->cmd->opcode) {
1121
1122 case SCSI_READ_6_COMMAND:
1123 case READ_10:
1124 case READ_12:
1125 case READ_16:
1126 operation = VD_OP_BREAD;
1127 break;
1128
1129 case SCSI_WRITE_6_COMMAND:
1130 case WRITE_10:
1131 case WRITE_12:
1132 case WRITE_16:
1133 operation = VD_OP_BWRITE;
1134 break;
1135
1136 case SCSI_SYNCHRONIZE_CACHE_10:
1137 operation = VD_OP_FLUSH;
1138 break;
1139
1140 default:
1141 panic("%s unhandled cmd opcode 0x%x",
1142 __func__, xs->cmd->opcode);
1143 }
1144
1145 /*
1146 * READ/WRITE/SYNCHRONIZE commands. SYNCHRONIZE CACHE has same
1147 * layout as 10-byte READ/WRITE commands.
1148 */
1149 if (xs->cmdlen == 6) {
1150 rw6 = (struct scsi_rw_6 *)xs->cmd;
1151 lba = _3btol(rw6->addr) & (SRW_TOPADDR << 16 | 0xffff);
1152 } else if (xs->cmdlen == 10) {
1153 rw10 = (struct scsipi_rw_10 *)xs->cmd;
1154 lba = _4btol(rw10->addr);
1155 } else if (xs->cmdlen == 12) {
1156 rw12 = (struct scsipi_rw_12 *)xs->cmd;
1157 lba = _4btol(rw12->addr);
1158 } else if (xs->cmdlen == 16) {
1159 rw16 = (struct scsipi_rw_16 *)xs->cmd;
1160 lba = _8btol(rw16->addr);
1161 }
1162
1163 DPRINTF(("lba = %lu\n", lba));
1164
1165 desc = sc->sc_tx_prod;
1166 ncookies = 0;
1167 len = xs->datalen;
1168 va = (vaddr_t)xs->data;
1169 while (len > 0) {
1170 DPRINTF(("len = %u\n", len));
1171 KASSERT(ncookies < MAXPHYS / PAGE_SIZE);
1172 pa = 0;
1173 pmap_extract(pmap_kernel(), va, &pa);
1174 while (map->lm_slot[map->lm_next].entry != 0) {
1175 map->lm_next++;
1176 map->lm_next &= (map->lm_nentries - 1);
1177 }
1178 map->lm_slot[map->lm_next].entry = (pa & LDC_MTE_RA_MASK);
1179 map->lm_slot[map->lm_next].entry |= LDC_MTE_CPR | LDC_MTE_CPW;
1180 map->lm_slot[map->lm_next].entry |= LDC_MTE_IOR | LDC_MTE_IOW;
1181 map->lm_slot[map->lm_next].entry |= LDC_MTE_R | LDC_MTE_W;
1182 map->lm_count++;
1183
1184 nbytes = MIN(len, PAGE_SIZE - (pa & PAGE_MASK));
1185
1186 sc->sc_vd->vd_desc[desc].cookie[ncookies].addr =
1187 map->lm_next << PAGE_SHIFT | (pa & PAGE_MASK);
1188 sc->sc_vd->vd_desc[desc].cookie[ncookies].size = nbytes;
1189
1190 sc->sc_vsd[desc].vsd_map_idx[ncookies] = map->lm_next;
1191 va += nbytes;
1192 len -= nbytes;
1193 ncookies++;
1194 }
1195 if (ISSET(xs->xs_control, XS_CTL_POLL) == 0)
1196 sc->sc_vd->vd_desc[desc].hdr.ack = 1;
1197 else
1198 sc->sc_vd->vd_desc[desc].hdr.ack = 0;
1199 sc->sc_vd->vd_desc[desc].operation = operation;
1200 sc->sc_vd->vd_desc[desc].slice = VD_SLICE_NONE;
1201 sc->sc_vd->vd_desc[desc].status = 0xffffffff;
1202 sc->sc_vd->vd_desc[desc].offset = lba;
1203 sc->sc_vd->vd_desc[desc].size = xs->datalen;
1204 sc->sc_vd->vd_desc[desc].ncookies = ncookies;
1205
1206 membar_Sync();
1207
1208 sc->sc_vd->vd_desc[desc].hdr.dstate = VIO_DESC_READY;
1209
1210 sc->sc_vsd[desc].vsd_xs = xs;
1211 sc->sc_vsd[desc].vsd_ncookies = ncookies;
1212
1213 sc->sc_tx_prod++;
1214 sc->sc_tx_prod &= (sc->sc_vd->vd_nentries - 1);
1215
1216 bzero(&dm, sizeof(dm));
1217 dm.tag.type = VIO_TYPE_DATA;
1218 dm.tag.stype = VIO_SUBTYPE_INFO;
1219 dm.tag.stype_env = VIO_DRING_DATA;
1220 dm.tag.sid = sc->sc_local_sid;
1221 dm.seq_no = sc->sc_seq_no++;
1222 dm.dring_ident = sc->sc_dring_ident;
1223 dm.start_idx = dm.end_idx = desc;
1224 vdsk_sendmsg(sc, &dm, sizeof(dm));
1225
1226 return desc;
1227 }
1228
1229 void
1230 vdsk_complete_cmd(struct vdsk_softc *sc, struct scsipi_xfer *xs, int desc)
1231 {
1232 struct ldc_map *map = sc->sc_lm;
1233 int cookie, idx;
1234 int error;
1235
1236 cookie = 0;
1237 while (cookie < sc->sc_vsd[desc].vsd_ncookies) {
1238 idx = sc->sc_vsd[desc].vsd_map_idx[cookie++];
1239 map->lm_slot[idx].entry = 0;
1240 map->lm_count--;
1241 }
1242
1243 error = XS_NOERROR;
1244 if (sc->sc_vd->vd_desc[desc].status != 0)
1245 error = XS_DRIVER_STUFFUP;
1246 xs->resid = xs->datalen -
1247 sc->sc_vd->vd_desc[desc].size;
1248
1249 /*
1250 * scsi_done() called by vdsk_scsi_done() requires
1251 * the kernel to be locked
1252 */
1253 KERNEL_LOCK(1, curlwp);
1254 vdsk_scsi_done(xs, error);
1255 KERNEL_UNLOCK_ONE(curlwp);
1256
1257 sc->sc_vd->vd_desc[desc].hdr.dstate = VIO_DESC_FREE;
1258
1259 }
1260
1261 void
1262 vdsk_scsi_inq(struct vdsk_softc *sc, struct scsipi_xfer *xs)
1263 {
1264 vdsk_scsi_inquiry(sc, xs);
1265 }
1266
1267 void
1268 vdsk_scsi_inquiry(struct vdsk_softc *sc, struct scsipi_xfer *xs)
1269 {
1270 struct scsipi_inquiry_data inq;
1271 char buf[5];
1272
1273 bzero(&inq, sizeof(inq));
1274
1275 switch (sc->sc_vd_mtype) {
1276 case VD_MEDIA_TYPE_CD:
1277 case VD_MEDIA_TYPE_DVD:
1278 inq.device = T_CDROM;
1279 inq.dev_qual2 = SID_REMOVABLE;
1280 bcopy("Virtual CDROM ", inq.product, sizeof(inq.product));
1281 break;
1282 case VD_MEDIA_TYPE_FIXED:
1283 inq.device = T_DIRECT;
1284 bcopy("Virtual Disk ", inq.product, sizeof(inq.product));
1285 break;
1286 default:
1287 panic("Unhandled media type %d\n", sc->sc_vd_mtype);
1288 }
1289 inq.version = 0x05; /* SPC-3 */
1290 inq.response_format = 2;
1291 inq.additional_length = 32;
1292 inq.flags3 |= SID_CmdQue;
1293 bcopy("SUN ", inq.vendor, sizeof(inq.vendor));
1294 snprintf(buf, sizeof(buf), "%u.%u ", sc->sc_major, sc->sc_minor);
1295 bcopy(buf, inq.revision, sizeof(inq.revision));
1296
1297 bcopy(&inq, xs->data, MIN(sizeof(inq), xs->datalen));
1298
1299 vdsk_scsi_done(xs, XS_NOERROR);
1300 }
1301
1302 void
1303 vdsk_scsi_capacity(struct vdsk_softc *sc, struct scsipi_xfer *xs)
1304 {
1305 struct scsipi_read_capacity_10_data rcd;
1306 uint64_t capacity;
1307
1308 bzero(&rcd, sizeof(rcd));
1309
1310 capacity = sc->sc_vdisk_size - 1;
1311 if (capacity > 0xffffffff)
1312 capacity = 0xffffffff;
1313
1314 _lto4b(capacity, rcd.addr);
1315 _lto4b(sc->sc_vdisk_block_size, rcd.length);
1316
1317 DPRINTF(("%s() capacity %lu block size %u\n",
1318 __FUNCTION__, capacity, sc->sc_vdisk_block_size));
1319
1320 bcopy(&rcd, xs->data, MIN(sizeof(rcd), xs->datalen));
1321
1322 vdsk_scsi_done(xs, XS_NOERROR);
1323 }
1324
1325 void
1326 vdsk_scsi_capacity16(struct vdsk_softc *sc, struct scsipi_xfer *xs)
1327 {
1328 struct scsipi_read_capacity_16_data rcd;
1329 uint64_t capacity;
1330
1331 bzero(&rcd, sizeof(rcd));
1332
1333 capacity = sc->sc_vdisk_size - 1;
1334
1335 _lto8b(capacity, rcd.addr);
1336 _lto4b(sc->sc_vdisk_block_size, rcd.length);
1337
1338 DPRINTF(("%s() capacity %lu block size %u\n",
1339 __FUNCTION__, capacity, sc->sc_vdisk_block_size));
1340
1341 bcopy(&rcd, xs->data, MIN(sizeof(rcd), xs->datalen));
1342
1343 vdsk_scsi_done(xs, XS_NOERROR);
1344 }
1345
1346 void
1347 vdsk_scsi_report_luns(struct vdsk_softc *sc, struct scsipi_xfer *xs)
1348 {
1349 vdsk_scsi_done(xs, XS_NOERROR);
1350 }
1351
1352 void
1353 vdsk_scsi_read_discinfo(struct vdsk_softc *sc, struct scsipi_xfer *xs)
1354 {
1355 DPRINTF(("%s()\n", __FUNCTION__));
1356
1357 struct scsipi_read_discinfo_data read_discinfo_data;
1358 bzero(&read_discinfo_data, sizeof(read_discinfo_data));
1359
1360 bcopy(&read_discinfo_data, xs->data, MIN(sizeof(read_discinfo_data), xs->datalen));
1361
1362 vdsk_scsi_done(xs, XS_NOERROR);
1363 }
1364
1365 void
1366 vdsk_scsi_read_trackinfo(struct vdsk_softc *sc, struct scsipi_xfer *xs)
1367 {
1368 DPRINTF(("%s()\n", __FUNCTION__));
1369
1370 struct scsipi_read_trackinfo_data read_trackinfo_data;
1371 bzero(&read_trackinfo_data, sizeof(read_trackinfo_data));
1372
1373 bcopy(&read_trackinfo_data, xs->data, MIN(sizeof(read_trackinfo_data), xs->datalen));
1374
1375 vdsk_scsi_done(xs, XS_NOERROR);
1376 }
1377
1378 void
1379 vdsk_scsi_done(struct scsipi_xfer *xs, int error)
1380 {
1381 xs->error = error;
1382
1383 scsipi_done(xs);
1384 }
1385