2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2013 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/selinfo.h>
40 #include <sys/eventhandler.h>
42 #include <sys/bus_dma.h>
44 #include <sys/ioccom.h>
47 #include <sys/signalvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/endian.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_debug.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
62 #include <dev/ofw/openfirm.h>
63 #include <dev/ofw/ofw_bus.h>
64 #include <dev/ofw/ofw_bus_subr.h>
66 #include <machine/bus.h>
67 #include <machine/resource.h>
69 #include <powerpc/pseries/phyp-hvcall.h>
73 /* VSCSI CRQ format from table 260 of PAPR spec 2.4 (page 760) */
85 TAILQ_ENTRY(vscsi_xfer) queue;
86 struct vscsi_softc *sc;
91 vmem_addr_t srp_iu_offset;
92 vmem_size_t srp_iu_size;
95 TAILQ_HEAD(vscsi_xferq, vscsi_xfer);
99 struct cam_devq *devq;
101 struct cam_path *path;
107 int max_transactions;
110 struct resource *irq;
113 bus_dma_tag_t crq_tag;
114 struct vscsi_crq *crq_queue;
116 bus_dmamap_t crq_map;
119 vmem_t *srp_iu_arena;
121 bus_addr_t srp_iu_phys;
123 bus_dma_tag_t data_tag;
125 struct vscsi_xfer loginxp;
126 struct vscsi_xfer *xfer;
127 struct vscsi_xferq active_xferq;
128 struct vscsi_xferq free_xferq;
135 uint64_t max_cmd_length;
137 uint16_t buffer_formats;
139 uint8_t reserved3[5];
140 uint8_t initiator_port_id[16];
141 uint8_t target_port_id[16];
144 struct srp_login_rsp {
147 uint32_t request_limit_delta;
149 uint32_t max_i_to_t_len;
150 uint32_t max_t_to_i_len;
151 uint16_t buffer_formats;
153 /* Some reserved bits follow */
161 uint8_t out_buffer_count;
162 uint8_t in_buffer_count;
166 uint8_t reserved3[3];
167 uint8_t additional_cdb;
169 uint8_t data_payload[0];
175 uint32_t request_limit_delta;
180 uint32_t data_out_resid;
181 uint32_t data_in_resid;
182 uint32_t sense_data_len;
183 uint32_t response_data_len;
184 uint8_t data_payload[0];
187 struct srp_tsk_mgmt {
193 uint8_t reserved3[2];
200 /* Message code type */
201 #define SRP_LOGIN_REQ 0x00
202 #define SRP_TSK_MGMT 0x01
204 #define SRP_I_LOGOUT 0x03
206 #define SRP_LOGIN_RSP 0xC0
208 #define SRP_LOGIN_REJ 0xC2
210 #define SRP_T_LOGOUT 0x80
211 #define SRP_CRED_REQ 0x81
212 #define SRP_AER_REQ 0x82
214 #define SRP_CRED_RSP 0x41
215 #define SRP_AER_RSP 0x41
217 /* Flags for srp_rsp flags field */
218 #define SRP_RSPVALID 0x01
219 #define SRP_SNSVALID 0x02
220 #define SRP_DOOVER 0x04
221 #define SRP_DOUNDER 0x08
222 #define SRP_DIOVER 0x10
223 #define SRP_DIUNDER 0x20
225 #define MAD_SUCESS 0x00
226 #define MAD_NOT_SUPPORTED 0xf1
227 #define MAD_FAILED 0xf7
229 #define MAD_EMPTY_IU 0x01
230 #define MAD_ERROR_LOGGING_REQUEST 0x02
231 #define MAD_ADAPTER_INFO_REQUEST 0x03
232 #define MAD_CAPABILITIES_EXCHANGE 0x05
233 #define MAD_PHYS_ADAP_INFO_REQUEST 0x06
234 #define MAD_TAPE_PASSTHROUGH_REQUEST 0x07
235 #define MAD_ENABLE_FAST_FAIL 0x08
237 static int vscsi_probe(device_t);
238 static int vscsi_attach(device_t);
239 static int vscsi_detach(device_t);
240 static void vscsi_cam_action(struct cam_sim *, union ccb *);
241 static void vscsi_cam_poll(struct cam_sim *);
242 static void vscsi_intr(void *arg);
243 static void vscsi_check_response_queue(struct vscsi_softc *sc);
244 static void vscsi_setup_bus(struct vscsi_softc *sc);
246 static void vscsi_srp_login(struct vscsi_softc *sc);
247 static void vscsi_crq_load_cb(void *, bus_dma_segment_t *, int, int);
248 static void vscsi_scsi_command(void *xxp, bus_dma_segment_t *segs,
250 static void vscsi_task_management(struct vscsi_softc *sc, union ccb *ccb);
251 static void vscsi_srp_response(struct vscsi_xfer *, struct vscsi_crq *);
253 static devclass_t vscsi_devclass;
254 static device_method_t vscsi_methods[] = {
255 DEVMETHOD(device_probe, vscsi_probe),
256 DEVMETHOD(device_attach, vscsi_attach),
257 DEVMETHOD(device_detach, vscsi_detach),
261 static driver_t vscsi_driver = {
264 sizeof(struct vscsi_softc)
266 DRIVER_MODULE(vscsi, vdevice, vscsi_driver, vscsi_devclass, 0, 0);
267 MALLOC_DEFINE(M_VSCSI, "vscsi", "CAM device queue for VSCSI");
270 vscsi_probe(device_t dev)
273 if (!ofw_bus_is_compatible(dev, "IBM,v-scsi"))
276 device_set_desc(dev, "POWER Hypervisor Virtual SCSI Bus");
281 vscsi_attach(device_t dev)
283 struct vscsi_softc *sc;
284 struct vscsi_xfer *xp;
287 sc = device_get_softc(dev);
292 mtx_init(&sc->io_lock, "vscsi", NULL, MTX_DEF);
295 OF_getencprop(ofw_bus_get_node(dev), "reg", &sc->unit,
298 /* Setup interrupt */
300 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqid,
304 device_printf(dev, "Could not allocate IRQ\n");
305 mtx_destroy(&sc->io_lock);
309 bus_setup_intr(dev, sc->irq, INTR_TYPE_CAM | INTR_MPSAFE |
310 INTR_ENTROPY, NULL, vscsi_intr, sc, &sc->irq_cookie);
313 error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
314 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE,
315 256, BUS_SPACE_MAXSIZE_32BIT, 0, busdma_lock_mutex, &sc->io_lock,
318 TAILQ_INIT(&sc->active_xferq);
319 TAILQ_INIT(&sc->free_xferq);
321 /* First XFER for login data */
323 bus_dmamap_create(sc->data_tag, 0, &sc->loginxp.dmamap);
324 TAILQ_INSERT_TAIL(&sc->free_xferq, &sc->loginxp, queue);
327 error = bus_dma_tag_create(bus_get_dma_tag(dev), PAGE_SIZE, 0,
328 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 8*PAGE_SIZE,
329 1, BUS_SPACE_MAXSIZE, 0, NULL, NULL, &sc->crq_tag);
330 error = bus_dmamem_alloc(sc->crq_tag, (void **)&sc->crq_queue,
331 BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->crq_map);
334 error = bus_dmamap_load(sc->crq_tag, sc->crq_map, sc->crq_queue,
335 8*PAGE_SIZE, vscsi_crq_load_cb, sc, 0);
337 mtx_lock(&sc->io_lock);
339 sc->xfer = mallocarray(sc->max_transactions, sizeof(sc->xfer[0]),
341 for (i = 0; i < sc->max_transactions; i++) {
345 error = bus_dmamap_create(sc->data_tag, 0, &xp->dmamap);
347 device_printf(dev, "Could not create DMA map (%d)\n",
352 TAILQ_INSERT_TAIL(&sc->free_xferq, xp, queue);
354 mtx_unlock(&sc->io_lock);
356 /* Allocate CAM bits */
357 if ((sc->devq = cam_simq_alloc(sc->max_transactions)) == NULL)
360 sc->sim = cam_sim_alloc(vscsi_cam_action, vscsi_cam_poll, "vscsi", sc,
361 device_get_unit(dev), &sc->io_lock,
362 sc->max_transactions, sc->max_transactions,
364 if (sc->sim == NULL) {
365 cam_simq_free(sc->devq);
367 device_printf(dev, "CAM SIM attach failed\n");
372 mtx_lock(&sc->io_lock);
373 if (xpt_bus_register(sc->sim, dev, 0) != 0) {
374 device_printf(dev, "XPT bus registration failed\n");
375 cam_sim_free(sc->sim, FALSE);
377 cam_simq_free(sc->devq);
379 mtx_unlock(&sc->io_lock);
382 mtx_unlock(&sc->io_lock);
388 vscsi_detach(device_t dev)
390 struct vscsi_softc *sc;
392 sc = device_get_softc(dev);
396 if (sc->sim != NULL) {
397 mtx_lock(&sc->io_lock);
398 xpt_bus_deregister(cam_sim_path(sc->sim));
399 cam_sim_free(sc->sim, FALSE);
401 mtx_unlock(&sc->io_lock);
404 if (sc->devq != NULL) {
405 cam_simq_free(sc->devq);
409 mtx_destroy(&sc->io_lock);
415 vscsi_cam_action(struct cam_sim *sim, union ccb *ccb)
417 struct vscsi_softc *sc = cam_sim_softc(sim);
419 mtx_assert(&sc->io_lock, MA_OWNED);
421 switch (ccb->ccb_h.func_code) {
424 struct ccb_pathinq *cpi = &ccb->cpi;
426 cpi->version_num = 1;
427 cpi->hba_inquiry = PI_TAG_ABLE;
428 cpi->hba_misc = PIM_EXTLUNS;
429 cpi->target_sprt = 0;
430 cpi->hba_eng_cnt = 0;
433 cpi->initiator_id = ~0;
434 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
435 strlcpy(cpi->hba_vid, "IBM", HBA_IDLEN);
436 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
437 cpi->unit_number = cam_sim_unit(sim);
438 cpi->bus_id = cam_sim_bus(sim);
439 cpi->base_transfer_speed = 150000;
440 cpi->transport = XPORT_SRP;
441 cpi->transport_version = 0;
442 cpi->protocol = PROTO_SCSI;
443 cpi->protocol_version = SCSI_REV_SPC4;
444 cpi->ccb_h.status = CAM_REQ_CMP;
448 ccb->ccb_h.status = CAM_REQ_CMP;
451 ccb->ccb_h.status = CAM_REQ_INPROG;
452 vscsi_task_management(sc, ccb);
454 case XPT_GET_TRAN_SETTINGS:
455 ccb->cts.protocol = PROTO_SCSI;
456 ccb->cts.protocol_version = SCSI_REV_SPC4;
457 ccb->cts.transport = XPORT_SRP;
458 ccb->cts.transport_version = 0;
459 ccb->cts.proto_specific.valid = 0;
460 ccb->cts.xport_specific.valid = 0;
461 ccb->ccb_h.status = CAM_REQ_CMP;
463 case XPT_SET_TRAN_SETTINGS:
464 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
468 struct vscsi_xfer *xp;
470 ccb->ccb_h.status = CAM_REQ_INPROG;
472 xp = TAILQ_FIRST(&sc->free_xferq);
474 panic("SCSI queue flooded");
476 TAILQ_REMOVE(&sc->free_xferq, xp, queue);
477 TAILQ_INSERT_TAIL(&sc->active_xferq, xp, queue);
478 bus_dmamap_load_ccb(sc->data_tag, xp->dmamap,
479 ccb, vscsi_scsi_command, xp, 0);
484 ccb->ccb_h.status = CAM_REQ_INVALID;
493 vscsi_srp_login(struct vscsi_softc *sc)
495 struct vscsi_xfer *xp;
496 struct srp_login *login;
497 struct vscsi_crq crq;
500 mtx_assert(&sc->io_lock, MA_OWNED);
502 xp = TAILQ_FIRST(&sc->free_xferq);
504 panic("SCSI queue flooded");
506 TAILQ_REMOVE(&sc->free_xferq, xp, queue);
507 TAILQ_INSERT_TAIL(&sc->active_xferq, xp, queue);
510 xp->srp_iu_size = crq.iu_length = 64;
511 err = vmem_alloc(xp->sc->srp_iu_arena, xp->srp_iu_size,
512 M_BESTFIT | M_NOWAIT, &xp->srp_iu_offset);
514 panic("Error during VMEM allocation (%d)", err);
516 login = (struct srp_login *)((uint8_t *)xp->sc->srp_iu_queue +
517 (uintptr_t)xp->srp_iu_offset);
518 bzero(login, xp->srp_iu_size);
519 login->type = SRP_LOGIN_REQ;
520 login->tag = (uint64_t)(xp);
521 login->max_cmd_length = htobe64(256);
522 login->buffer_formats = htobe16(0x1 | 0x2); /* Direct and indirect */
525 /* Create CRQ entry */
528 crq.iu_data = xp->sc->srp_iu_phys + xp->srp_iu_offset;
529 bus_dmamap_sync(sc->crq_tag, sc->crq_map, BUS_DMASYNC_PREWRITE);
531 err = phyp_hcall(H_SEND_CRQ, xp->sc->unit, ((uint64_t *)(&crq))[0],
532 ((uint64_t *)(&crq))[1]);
534 panic("CRQ send failure (%d)", err);
538 vscsi_task_management(struct vscsi_softc *sc, union ccb *ccb)
540 struct srp_tsk_mgmt *cmd;
541 struct vscsi_xfer *xp;
542 struct vscsi_crq crq;
545 mtx_assert(&sc->io_lock, MA_OWNED);
547 xp = TAILQ_FIRST(&sc->free_xferq);
549 panic("SCSI queue flooded");
551 TAILQ_REMOVE(&sc->free_xferq, xp, queue);
552 TAILQ_INSERT_TAIL(&sc->active_xferq, xp, queue);
554 xp->srp_iu_size = crq.iu_length = sizeof(*cmd);
555 err = vmem_alloc(xp->sc->srp_iu_arena, xp->srp_iu_size,
556 M_BESTFIT | M_NOWAIT, &xp->srp_iu_offset);
558 panic("Error during VMEM allocation (%d)", err);
560 cmd = (struct srp_tsk_mgmt *)((uint8_t *)xp->sc->srp_iu_queue +
561 (uintptr_t)xp->srp_iu_offset);
562 bzero(cmd, xp->srp_iu_size);
563 cmd->type = SRP_TSK_MGMT;
564 cmd->tag = (uint64_t)xp;
565 cmd->lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
567 switch (ccb->ccb_h.func_code) {
569 cmd->function = 0x08;
572 panic("Unimplemented code %d", ccb->ccb_h.func_code);
576 bus_dmamap_sync(xp->sc->crq_tag, xp->sc->crq_map, BUS_DMASYNC_PREWRITE);
578 /* Create CRQ entry */
581 crq.iu_data = xp->sc->srp_iu_phys + xp->srp_iu_offset;
583 err = phyp_hcall(H_SEND_CRQ, xp->sc->unit, ((uint64_t *)(&crq))[0],
584 ((uint64_t *)(&crq))[1]);
586 panic("CRQ send failure (%d)", err);
590 vscsi_scsi_command(void *xxp, bus_dma_segment_t *segs, int nsegs, int err)
592 struct vscsi_xfer *xp = xxp;
594 union ccb *ccb = xp->ccb;
599 struct vscsi_crq crq;
601 KASSERT(err == 0, ("DMA error %d\n", err));
603 mtx_assert(&xp->sc->io_lock, MA_OWNED);
605 cdb = (ccb->ccb_h.flags & CAM_CDB_POINTER) ?
606 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes;
608 /* Command format from Table 20, page 37 of SRP spec */
609 crq.iu_length = 48 + ((nsegs > 1) ? 20 : 16) +
610 ((ccb->csio.cdb_len > 16) ? (ccb->csio.cdb_len - 16) : 0);
611 xp->srp_iu_size = crq.iu_length;
613 xp->srp_iu_size += nsegs*16;
614 xp->srp_iu_size = roundup(xp->srp_iu_size, 16);
615 err = vmem_alloc(xp->sc->srp_iu_arena, xp->srp_iu_size,
616 M_BESTFIT | M_NOWAIT, &xp->srp_iu_offset);
618 panic("Error during VMEM allocation (%d)", err);
620 cmd = (struct srp_cmd *)((uint8_t *)xp->sc->srp_iu_queue +
621 (uintptr_t)xp->srp_iu_offset);
622 bzero(cmd, xp->srp_iu_size);
624 if (ccb->csio.cdb_len > 16)
625 cmd->additional_cdb = (ccb->csio.cdb_len - 16) << 2;
626 memcpy(cmd->cdb, cdb, ccb->csio.cdb_len);
628 cmd->tag = (uint64_t)(xp); /* Let the responder find this again */
629 cmd->lun = htobe64(CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
632 /* Use indirect descriptors */
633 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
635 cmd->formats = (2 << 4);
641 panic("Does not support bidirectional commands (%d)",
642 ccb->ccb_h.flags & CAM_DIR_MASK);
646 desc_start = ((ccb->csio.cdb_len > 16) ?
647 ccb->csio.cdb_len - 16 : 0);
648 chunk_addr = xp->sc->srp_iu_phys + xp->srp_iu_offset + 20 +
649 desc_start + sizeof(*cmd);
650 chunk_size = 16*nsegs;
651 memcpy(&cmd->data_payload[desc_start], &chunk_addr, 8);
652 memcpy(&cmd->data_payload[desc_start+12], &chunk_size, 4);
654 for (i = 0; i < nsegs; i++)
655 chunk_size += segs[i].ds_len;
656 memcpy(&cmd->data_payload[desc_start+16], &chunk_size, 4);
658 for (i = 0; i < nsegs; i++) {
659 chunk_addr = segs[i].ds_addr;
660 chunk_size = segs[i].ds_len;
662 memcpy(&cmd->data_payload[desc_start + 16*i],
664 /* Set handle tag to 0 */
665 memcpy(&cmd->data_payload[desc_start + 16*i + 12],
668 } else if (nsegs == 1) {
669 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
671 cmd->formats = (1 << 4);
677 panic("Does not support bidirectional commands (%d)",
678 ccb->ccb_h.flags & CAM_DIR_MASK);
689 chunk_addr = segs[0].ds_addr;
690 chunk_size = segs[0].ds_len;
691 desc_start = ((ccb->csio.cdb_len > 16) ?
692 ccb->csio.cdb_len - 16 : 0);
694 memcpy(&cmd->data_payload[desc_start], &chunk_addr, 8);
695 /* Set handle tag to 0 */
696 memcpy(&cmd->data_payload[desc_start+12], &chunk_size, 4);
697 KASSERT(xp->srp_iu_size >= 48 + ((ccb->csio.cdb_len > 16) ?
698 ccb->csio.cdb_len : 16), ("SRP IU command length"));
702 bus_dmamap_sync(xp->sc->crq_tag, xp->sc->crq_map, BUS_DMASYNC_PREWRITE);
704 /* Create CRQ entry */
707 crq.iu_data = xp->sc->srp_iu_phys + xp->srp_iu_offset;
709 err = phyp_hcall(H_SEND_CRQ, xp->sc->unit, ((uint64_t *)(&crq))[0],
710 ((uint64_t *)(&crq))[1]);
712 panic("CRQ send failure (%d)", err);
716 vscsi_crq_load_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int err)
718 struct vscsi_softc *sc = xsc;
720 sc->crq_phys = segs[0].ds_addr;
721 sc->n_crqs = PAGE_SIZE/sizeof(struct vscsi_crq);
723 sc->srp_iu_queue = (uint8_t *)(sc->crq_queue);
724 sc->srp_iu_phys = segs[0].ds_addr;
725 sc->srp_iu_arena = vmem_create("VSCSI SRP IU", PAGE_SIZE,
726 segs[0].ds_len - PAGE_SIZE, 16, 0, M_BESTFIT | M_NOWAIT);
730 vscsi_setup_bus(struct vscsi_softc *sc)
732 struct vscsi_crq crq;
733 struct vscsi_xfer *xp;
744 char partition_name[96];
745 uint32_t partition_number;
746 uint32_t mad_version;
748 uint32_t port_max_txu[8];
752 bzero(&crq, sizeof(crq));
759 error = phyp_hcall(H_FREE_CRQ, sc->unit);
760 } while (error == H_BUSY);
762 /* See initialization sequence page 757 */
763 bzero(sc->crq_queue, sc->n_crqs*sizeof(sc->crq_queue[0]));
765 sc->bus_initialized = 0;
766 sc->bus_logged_in = 0;
767 bus_dmamap_sync(sc->crq_tag, sc->crq_map, BUS_DMASYNC_PREWRITE);
768 error = phyp_hcall(H_REG_CRQ, sc->unit, sc->crq_phys,
769 sc->n_crqs*sizeof(sc->crq_queue[0]));
770 KASSERT(error == 0, ("CRQ registration success"));
772 error = phyp_hcall(H_SEND_CRQ, sc->unit, ((uint64_t *)(&crq))[0],
773 ((uint64_t *)(&crq))[1]);
775 panic("CRQ setup failure (%d)", error);
777 while (sc->bus_initialized == 0)
778 vscsi_check_response_queue(sc);
780 /* Send MAD adapter info */
781 mad_adapter_info.type = MAD_ADAPTER_INFO_REQUEST;
782 mad_adapter_info.status = 0;
783 mad_adapter_info.length = sizeof(mad_adapter_info.payload);
785 strcpy(mad_adapter_info.payload.srp_version, "16.a");
786 strcpy(mad_adapter_info.payload.partition_name, "UNKNOWN");
787 mad_adapter_info.payload.partition_number = -1;
788 mad_adapter_info.payload.mad_version = 1;
789 mad_adapter_info.payload.os_type = 2; /* Claim we are Linux */
790 mad_adapter_info.payload.port_max_txu[0] = 0;
791 /* If this fails, we get the defaults above */
792 OF_getprop(OF_finddevice("/"), "ibm,partition-name",
793 mad_adapter_info.payload.partition_name,
794 sizeof(mad_adapter_info.payload.partition_name));
795 OF_getprop(OF_finddevice("/"), "ibm,partition-no",
796 &mad_adapter_info.payload.partition_number,
797 sizeof(mad_adapter_info.payload.partition_number));
799 xp = TAILQ_FIRST(&sc->free_xferq);
801 TAILQ_REMOVE(&sc->free_xferq, xp, queue);
802 TAILQ_INSERT_TAIL(&sc->active_xferq, xp, queue);
803 xp->srp_iu_size = crq.iu_length = sizeof(mad_adapter_info);
804 vmem_alloc(xp->sc->srp_iu_arena, xp->srp_iu_size,
805 M_BESTFIT | M_NOWAIT, &xp->srp_iu_offset);
806 mad_adapter_info.buffer = xp->sc->srp_iu_phys + xp->srp_iu_offset + 24;
807 mad_adapter_info.tag = (uint64_t)xp;
808 memcpy((uint8_t *)xp->sc->srp_iu_queue + (uintptr_t)xp->srp_iu_offset,
809 &mad_adapter_info, sizeof(mad_adapter_info));
812 crq.iu_data = xp->sc->srp_iu_phys + xp->srp_iu_offset;
813 bus_dmamap_sync(sc->crq_tag, sc->crq_map, BUS_DMASYNC_PREWRITE);
814 phyp_hcall(H_SEND_CRQ, xp->sc->unit, ((uint64_t *)(&crq))[0],
815 ((uint64_t *)(&crq))[1]);
817 while (TAILQ_EMPTY(&sc->free_xferq))
818 vscsi_check_response_queue(sc);
822 while (sc->bus_logged_in == 0)
823 vscsi_check_response_queue(sc);
825 error = phyp_hcall(H_VIO_SIGNAL, sc->unit, 1); /* Enable interrupts */
830 vscsi_intr(void *xsc)
832 struct vscsi_softc *sc = xsc;
834 mtx_lock(&sc->io_lock);
835 vscsi_check_response_queue(sc);
836 mtx_unlock(&sc->io_lock);
840 vscsi_srp_response(struct vscsi_xfer *xp, struct vscsi_crq *crq)
842 union ccb *ccb = xp->ccb;
843 struct vscsi_softc *sc = xp->sc;
847 /* SRP response packet in original request */
848 rsp = (struct srp_rsp *)((uint8_t *)sc->srp_iu_queue +
849 (uintptr_t)xp->srp_iu_offset);
850 ccb->csio.scsi_status = rsp->status;
851 if (ccb->csio.scsi_status == SCSI_STATUS_OK)
852 ccb->ccb_h.status = CAM_REQ_CMP;
854 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
856 /* Collect fast fail codes */
857 if (crq->status != 0)
858 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
861 if (ccb->ccb_h.status != CAM_REQ_CMP) {
862 ccb->ccb_h.status |= CAM_DEV_QFRZN;
863 xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
866 if (!(rsp->flags & SRP_RSPVALID))
867 rsp->response_data_len = 0;
868 if (!(rsp->flags & SRP_SNSVALID))
869 rsp->sense_data_len = 0;
870 if (!(rsp->flags & (SRP_DOOVER | SRP_DOUNDER)))
871 rsp->data_out_resid = 0;
872 if (!(rsp->flags & (SRP_DIOVER | SRP_DIUNDER)))
873 rsp->data_in_resid = 0;
875 if (rsp->flags & SRP_SNSVALID) {
876 bzero(&ccb->csio.sense_data, sizeof(struct scsi_sense_data));
877 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
878 sense_len = min(be32toh(rsp->sense_data_len),
879 ccb->csio.sense_len);
880 memcpy(&ccb->csio.sense_data,
881 &rsp->data_payload[be32toh(rsp->response_data_len)],
883 ccb->csio.sense_resid = ccb->csio.sense_len -
884 be32toh(rsp->sense_data_len);
887 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
889 ccb->csio.resid = rsp->data_out_resid;
892 ccb->csio.resid = rsp->data_in_resid;
896 bus_dmamap_sync(sc->data_tag, xp->dmamap, BUS_DMASYNC_POSTREAD);
897 bus_dmamap_unload(sc->data_tag, xp->dmamap);
903 vscsi_login_response(struct vscsi_xfer *xp, struct vscsi_crq *crq)
905 struct vscsi_softc *sc = xp->sc;
906 struct srp_login_rsp *rsp;
908 /* SRP response packet in original request */
909 rsp = (struct srp_login_rsp *)((uint8_t *)sc->srp_iu_queue +
910 (uintptr_t)xp->srp_iu_offset);
911 KASSERT(be16toh(rsp->buffer_formats) & 0x3, ("Both direct and indirect "
912 "buffers supported"));
914 sc->max_transactions = be32toh(rsp->request_limit_delta);
915 device_printf(sc->dev, "Queue depth %d commands\n",
916 sc->max_transactions);
917 sc->bus_logged_in = 1;
921 vscsi_cam_poll(struct cam_sim *sim)
923 struct vscsi_softc *sc = cam_sim_softc(sim);
925 vscsi_check_response_queue(sc);
929 vscsi_check_response_queue(struct vscsi_softc *sc)
931 struct vscsi_crq *crq;
932 struct vscsi_xfer *xp;
935 mtx_assert(&sc->io_lock, MA_OWNED);
937 while (sc->crq_queue[sc->cur_crq].valid != 0) {
938 /* The hypercalls at both ends of this are not optimal */
939 phyp_hcall(H_VIO_SIGNAL, sc->unit, 0);
940 bus_dmamap_sync(sc->crq_tag, sc->crq_map, BUS_DMASYNC_POSTREAD);
942 crq = &sc->crq_queue[sc->cur_crq];
944 switch (crq->valid) {
946 if (crq->format == 0x02)
947 sc->bus_initialized = 1;
950 /* IU data is set to tag pointer (the XP) */
951 xp = (struct vscsi_xfer *)crq->iu_data;
953 switch (crq->format) {
955 code = *((uint8_t *)sc->srp_iu_queue +
956 (uintptr_t)xp->srp_iu_offset);
959 vscsi_srp_response(xp, crq);
962 vscsi_login_response(xp, crq);
965 device_printf(sc->dev, "Unknown SRP "
966 "response code %d\n", code);
971 /* Ignore management datagrams */
974 panic("Unknown CRQ format %d\n", crq->format);
977 vmem_free(sc->srp_iu_arena, xp->srp_iu_offset,
979 TAILQ_REMOVE(&sc->active_xferq, xp, queue);
980 TAILQ_INSERT_TAIL(&sc->free_xferq, xp, queue);
983 device_printf(sc->dev,
984 "Unknown CRQ message type %d\n", crq->valid);
989 sc->cur_crq = (sc->cur_crq + 1) % sc->n_crqs;
991 bus_dmamap_sync(sc->crq_tag, sc->crq_map, BUS_DMASYNC_PREWRITE);
992 phyp_hcall(H_VIO_SIGNAL, sc->unit, 1);