2 * Copyright (c) 2018 VMware, Inc.
4 * SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0)
10 #include <sys/param.h>
12 #include <sys/errno.h>
13 #include <sys/kernel.h>
14 #include <sys/malloc.h>
15 #include <sys/module.h>
16 #include <sys/queue.h>
18 #include <sys/sysctl.h>
19 #include <sys/systm.h>
21 #include <machine/bus.h>
22 #include <machine/resource.h>
24 #include <dev/pci/pcireg.h>
25 #include <dev/pci/pcivar.h>
28 #include <cam/cam_ccb.h>
29 #include <cam/cam_debug.h>
30 #include <cam/cam_sim.h>
31 #include <cam/cam_xpt_sim.h>
32 #include <cam/scsi/scsi_message.h>
36 #define PVSCSI_DEFAULT_NUM_PAGES_REQ_RING 8
37 #define PVSCSI_SENSE_LENGTH 256
39 MALLOC_DECLARE(M_PVSCSI);
40 MALLOC_DEFINE(M_PVSCSI, "pvscsi", "PVSCSI memory");
42 #ifdef PVSCSI_DEBUG_LOGGING
43 #define DEBUG_PRINTF(level, dev, fmt, ...) \
45 if (pvscsi_log_level >= (level)) { \
46 device_printf((dev), (fmt), ##__VA_ARGS__); \
50 #define DEBUG_PRINTF(level, dev, fmt, ...)
51 #endif /* PVSCSI_DEBUG_LOGGING */
53 #define ccb_pvscsi_hcb spriv_ptr0
54 #define ccb_pvscsi_sc spriv_ptr1
60 static inline uint32_t pvscsi_reg_read(struct pvscsi_softc *sc,
62 static inline void pvscsi_reg_write(struct pvscsi_softc *sc, uint32_t offset,
64 static inline uint32_t pvscsi_read_intr_status(struct pvscsi_softc *sc);
65 static inline void pvscsi_write_intr_status(struct pvscsi_softc *sc,
67 static inline void pvscsi_intr_enable(struct pvscsi_softc *sc);
68 static inline void pvscsi_intr_disable(struct pvscsi_softc *sc);
69 static void pvscsi_kick_io(struct pvscsi_softc *sc, uint8_t cdb0);
70 static void pvscsi_write_cmd(struct pvscsi_softc *sc, uint32_t cmd, void *data,
72 static uint32_t pvscsi_get_max_targets(struct pvscsi_softc *sc);
73 static int pvscsi_setup_req_call(struct pvscsi_softc *sc, uint32_t enable);
74 static void pvscsi_setup_rings(struct pvscsi_softc *sc);
75 static void pvscsi_setup_msg_ring(struct pvscsi_softc *sc);
76 static int pvscsi_hw_supports_msg(struct pvscsi_softc *sc);
78 static void pvscsi_timeout(void *arg);
79 static void pvscsi_freeze(struct pvscsi_softc *sc);
80 static void pvscsi_adapter_reset(struct pvscsi_softc *sc);
81 static void pvscsi_bus_reset(struct pvscsi_softc *sc);
82 static void pvscsi_device_reset(struct pvscsi_softc *sc, uint32_t target);
83 static void pvscsi_abort(struct pvscsi_softc *sc, uint32_t target,
86 static void pvscsi_process_completion(struct pvscsi_softc *sc,
87 struct pvscsi_ring_cmp_desc *e);
88 static void pvscsi_process_cmp_ring(struct pvscsi_softc *sc);
89 static void pvscsi_process_msg(struct pvscsi_softc *sc,
90 struct pvscsi_ring_msg_desc *e);
91 static void pvscsi_process_msg_ring(struct pvscsi_softc *sc);
93 static void pvscsi_intr_locked(struct pvscsi_softc *sc);
94 static void pvscsi_intr(void *xsc);
95 static void pvscsi_poll(struct cam_sim *sim);
97 static void pvscsi_execute_ccb(void *arg, bus_dma_segment_t *segs, int nseg,
99 static void pvscsi_action(struct cam_sim *sim, union ccb *ccb);
101 static inline uint64_t pvscsi_hcb_to_context(struct pvscsi_softc *sc,
102 struct pvscsi_hcb *hcb);
103 static inline struct pvscsi_hcb* pvscsi_context_to_hcb(struct pvscsi_softc *sc,
105 static struct pvscsi_hcb * pvscsi_hcb_get(struct pvscsi_softc *sc);
106 static void pvscsi_hcb_put(struct pvscsi_softc *sc, struct pvscsi_hcb *hcb);
108 static void pvscsi_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg,
110 static void pvscsi_dma_free(struct pvscsi_softc *sc, struct pvscsi_dma *dma);
111 static int pvscsi_dma_alloc(struct pvscsi_softc *sc, struct pvscsi_dma *dma,
112 bus_size_t size, bus_size_t alignment);
113 static int pvscsi_dma_alloc_ppns(struct pvscsi_softc *sc,
114 struct pvscsi_dma *dma, uint64_t *ppn_list, uint32_t num_pages);
115 static void pvscsi_dma_free_per_hcb(struct pvscsi_softc *sc,
116 uint32_t hcbs_allocated);
117 static int pvscsi_dma_alloc_per_hcb(struct pvscsi_softc *sc);
118 static void pvscsi_free_rings(struct pvscsi_softc *sc);
119 static int pvscsi_allocate_rings(struct pvscsi_softc *sc);
120 static void pvscsi_free_interrupts(struct pvscsi_softc *sc);
121 static int pvscsi_setup_interrupts(struct pvscsi_softc *sc);
122 static void pvscsi_free_all(struct pvscsi_softc *sc);
124 static int pvscsi_attach(device_t dev);
125 static int pvscsi_detach(device_t dev);
126 static int pvscsi_probe(device_t dev);
127 static int pvscsi_shutdown(device_t dev);
128 static int pvscsi_get_tunable(struct pvscsi_softc *sc, char *name, int value);
131 #ifdef PVSCSI_DEBUG_LOGGING
132 static int pvscsi_log_level = 0;
133 static SYSCTL_NODE(_hw, OID_AUTO, pvscsi, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
134 "PVSCSI driver parameters");
135 SYSCTL_INT(_hw_pvscsi, OID_AUTO, log_level, CTLFLAG_RWTUN, &pvscsi_log_level,
136 0, "PVSCSI debug log level");
139 static int pvscsi_request_ring_pages = 0;
140 TUNABLE_INT("hw.pvscsi.request_ring_pages", &pvscsi_request_ring_pages);
142 static int pvscsi_use_msg = 1;
143 TUNABLE_INT("hw.pvscsi.use_msg", &pvscsi_use_msg);
145 static int pvscsi_use_msi = 1;
146 TUNABLE_INT("hw.pvscsi.use_msi", &pvscsi_use_msi);
148 static int pvscsi_use_msix = 1;
149 TUNABLE_INT("hw.pvscsi.use_msix", &pvscsi_use_msix);
151 static int pvscsi_use_req_call_threshold = 1;
152 TUNABLE_INT("hw.pvscsi.use_req_call_threshold", &pvscsi_use_req_call_threshold);
154 static int pvscsi_max_queue_depth = 0;
155 TUNABLE_INT("hw.pvscsi.max_queue_depth", &pvscsi_max_queue_depth);
158 struct pvscsi_sg_list {
159 struct pvscsi_sg_element sge[PVSCSI_MAX_SG_ENTRIES_PER_SEGMENT];
163 #define PVSCSI_ABORT_TIMEOUT 2
164 #define PVSCSI_RESET_TIMEOUT 10
166 #define PVSCSI_HCB_NONE 0
167 #define PVSCSI_HCB_ABORT 1
168 #define PVSCSI_HCB_DEVICE_RESET 2
169 #define PVSCSI_HCB_BUS_RESET 3
173 struct pvscsi_ring_req_desc *e;
175 SLIST_ENTRY(pvscsi_hcb) links;
177 struct callout callout;
178 bus_dmamap_t dma_map;
180 bus_addr_t sense_buffer_paddr;
181 struct pvscsi_sg_list *sg_list;
182 bus_addr_t sg_list_paddr;
194 struct pvscsi_softc {
198 struct cam_path *bus_path;
200 struct pvscsi_rings_state *rings_state;
201 struct pvscsi_ring_req_desc *req_ring;
202 struct pvscsi_ring_cmp_desc *cmp_ring;
203 struct pvscsi_ring_msg_desc *msg_ring;
205 struct pvscsi_hcb *hcbs;
206 SLIST_HEAD(, pvscsi_hcb) free_list;
207 bus_dma_tag_t parent_dmat;
208 bus_dma_tag_t buffer_dmat;
211 uint32_t max_targets;
213 struct resource *mm_res;
215 struct resource *irq_res;
217 int use_req_call_threshold;
220 uint64_t rings_state_ppn;
221 uint32_t req_ring_num_pages;
222 uint64_t req_ring_ppn[PVSCSI_MAX_NUM_PAGES_REQ_RING];
223 uint32_t cmp_ring_num_pages;
224 uint64_t cmp_ring_ppn[PVSCSI_MAX_NUM_PAGES_CMP_RING];
225 uint32_t msg_ring_num_pages;
226 uint64_t msg_ring_ppn[PVSCSI_MAX_NUM_PAGES_MSG_RING];
228 struct pvscsi_dma rings_state_dma;
229 struct pvscsi_dma req_ring_dma;
230 struct pvscsi_dma cmp_ring_dma;
231 struct pvscsi_dma msg_ring_dma;
233 struct pvscsi_dma sg_list_dma;
234 struct pvscsi_dma sense_buffer_dma;
237 static int pvscsi_get_tunable(struct pvscsi_softc *sc, char *name, int value)
241 snprintf(cfg, sizeof(cfg), "hw.pvscsi.%d.%s", device_get_unit(sc->dev),
243 TUNABLE_INT_FETCH(cfg, &value);
249 pvscsi_freeze(struct pvscsi_softc *sc)
253 xpt_freeze_simq(sc->sim, 1);
258 static inline uint32_t
259 pvscsi_reg_read(struct pvscsi_softc *sc, uint32_t offset)
262 return (bus_read_4(sc->mm_res, offset));
266 pvscsi_reg_write(struct pvscsi_softc *sc, uint32_t offset, uint32_t val)
269 bus_write_4(sc->mm_res, offset, val);
272 static inline uint32_t
273 pvscsi_read_intr_status(struct pvscsi_softc *sc)
276 return (pvscsi_reg_read(sc, PVSCSI_REG_OFFSET_INTR_STATUS));
280 pvscsi_write_intr_status(struct pvscsi_softc *sc, uint32_t val)
283 pvscsi_reg_write(sc, PVSCSI_REG_OFFSET_INTR_STATUS, val);
287 pvscsi_intr_enable(struct pvscsi_softc *sc)
291 mask = PVSCSI_INTR_CMPL_MASK;
293 mask |= PVSCSI_INTR_MSG_MASK;
296 pvscsi_reg_write(sc, PVSCSI_REG_OFFSET_INTR_MASK, mask);
300 pvscsi_intr_disable(struct pvscsi_softc *sc)
303 pvscsi_reg_write(sc, PVSCSI_REG_OFFSET_INTR_MASK, 0);
307 pvscsi_kick_io(struct pvscsi_softc *sc, uint8_t cdb0)
309 struct pvscsi_rings_state *s;
311 if (cdb0 == READ_6 || cdb0 == READ_10 ||
312 cdb0 == READ_12 || cdb0 == READ_16 ||
313 cdb0 == WRITE_6 || cdb0 == WRITE_10 ||
314 cdb0 == WRITE_12 || cdb0 == WRITE_16) {
317 if (!sc->use_req_call_threshold ||
318 (s->req_prod_idx - s->req_cons_idx) >=
319 s->req_call_threshold) {
320 pvscsi_reg_write(sc, PVSCSI_REG_OFFSET_KICK_RW_IO, 0);
323 pvscsi_reg_write(sc, PVSCSI_REG_OFFSET_KICK_NON_RW_IO, 0);
328 pvscsi_write_cmd(struct pvscsi_softc *sc, uint32_t cmd, void *data,
334 KASSERT(len % sizeof(uint32_t) == 0,
335 ("command size not a multiple of 4"));
338 len /= sizeof(uint32_t);
340 pvscsi_reg_write(sc, PVSCSI_REG_OFFSET_COMMAND, cmd);
341 for (i = 0; i < len; ++i) {
342 pvscsi_reg_write(sc, PVSCSI_REG_OFFSET_COMMAND_DATA,
347 static inline uint64_t pvscsi_hcb_to_context(struct pvscsi_softc *sc,
348 struct pvscsi_hcb *hcb)
351 /* Offset by 1 because context must not be 0 */
352 return (hcb - sc->hcbs + 1);
355 static inline struct pvscsi_hcb* pvscsi_context_to_hcb(struct pvscsi_softc *sc,
359 return (sc->hcbs + (context - 1));
362 static struct pvscsi_hcb *
363 pvscsi_hcb_get(struct pvscsi_softc *sc)
365 struct pvscsi_hcb *hcb;
367 mtx_assert(&sc->lock, MA_OWNED);
369 hcb = SLIST_FIRST(&sc->free_list);
371 SLIST_REMOVE_HEAD(&sc->free_list, links);
378 pvscsi_hcb_put(struct pvscsi_softc *sc, struct pvscsi_hcb *hcb)
381 mtx_assert(&sc->lock, MA_OWNED);
384 hcb->recovery = PVSCSI_HCB_NONE;
385 SLIST_INSERT_HEAD(&sc->free_list, hcb, links);
389 pvscsi_get_max_targets(struct pvscsi_softc *sc)
391 uint32_t max_targets;
393 pvscsi_write_cmd(sc, PVSCSI_CMD_GET_MAX_TARGETS, NULL, 0);
395 max_targets = pvscsi_reg_read(sc, PVSCSI_REG_OFFSET_COMMAND_STATUS);
397 if (max_targets == ~0) {
401 return (max_targets);
404 static int pvscsi_setup_req_call(struct pvscsi_softc *sc, uint32_t enable)
407 struct pvscsi_cmd_desc_setup_req_call cmd;
409 if (!pvscsi_get_tunable(sc, "pvscsi_use_req_call_threshold",
410 pvscsi_use_req_call_threshold)) {
414 pvscsi_reg_write(sc, PVSCSI_REG_OFFSET_COMMAND,
415 PVSCSI_CMD_SETUP_REQCALLTHRESHOLD);
416 status = pvscsi_reg_read(sc, PVSCSI_REG_OFFSET_COMMAND_STATUS);
419 bzero(&cmd, sizeof(cmd));
421 pvscsi_write_cmd(sc, PVSCSI_CMD_SETUP_REQCALLTHRESHOLD,
423 status = pvscsi_reg_read(sc, PVSCSI_REG_OFFSET_COMMAND_STATUS);
425 return (status != 0);
432 pvscsi_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
436 KASSERT(nseg == 1, ("more than one segment"));
441 *dest = segs->ds_addr;
446 pvscsi_dma_free(struct pvscsi_softc *sc, struct pvscsi_dma *dma)
449 if (dma->tag != NULL) {
450 if (dma->paddr != 0) {
451 bus_dmamap_unload(dma->tag, dma->map);
454 if (dma->vaddr != NULL) {
455 bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
458 bus_dma_tag_destroy(dma->tag);
461 bzero(dma, sizeof(*dma));
465 pvscsi_dma_alloc(struct pvscsi_softc *sc, struct pvscsi_dma *dma,
466 bus_size_t size, bus_size_t alignment)
470 bzero(dma, sizeof(*dma));
472 error = bus_dma_tag_create(sc->parent_dmat, alignment, 0,
473 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, size, 1, size,
474 BUS_DMA_ALLOCNOW, NULL, NULL, &dma->tag);
476 device_printf(sc->dev, "error creating dma tag, error %d\n",
481 error = bus_dmamem_alloc(dma->tag, &dma->vaddr,
482 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dma->map);
484 device_printf(sc->dev, "error allocating dma mem, error %d\n",
489 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size,
490 pvscsi_dma_cb, &dma->paddr, BUS_DMA_NOWAIT);
492 device_printf(sc->dev, "error mapping dma mam, error %d\n",
501 pvscsi_dma_free(sc, dma);
507 pvscsi_dma_alloc_ppns(struct pvscsi_softc *sc, struct pvscsi_dma *dma,
508 uint64_t *ppn_list, uint32_t num_pages)
514 error = pvscsi_dma_alloc(sc, dma, num_pages * PAGE_SIZE, PAGE_SIZE);
516 device_printf(sc->dev, "Error allocating pages, error %d\n",
521 ppn = dma->paddr >> PAGE_SHIFT;
522 for (i = 0; i < num_pages; i++) {
523 ppn_list[i] = ppn + i;
530 pvscsi_dma_free_per_hcb(struct pvscsi_softc *sc, uint32_t hcbs_allocated)
534 struct pvscsi_hcb *hcb;
536 lock_owned = mtx_owned(&sc->lock);
539 mtx_unlock(&sc->lock);
541 for (i = 0; i < hcbs_allocated; ++i) {
543 callout_drain(&hcb->callout);
549 for (i = 0; i < hcbs_allocated; ++i) {
551 bus_dmamap_destroy(sc->buffer_dmat, hcb->dma_map);
554 pvscsi_dma_free(sc, &sc->sense_buffer_dma);
555 pvscsi_dma_free(sc, &sc->sg_list_dma);
559 pvscsi_dma_alloc_per_hcb(struct pvscsi_softc *sc)
563 struct pvscsi_hcb *hcb;
567 error = pvscsi_dma_alloc(sc, &sc->sg_list_dma,
568 sizeof(struct pvscsi_sg_list) * sc->hcb_cnt, 1);
570 device_printf(sc->dev,
571 "Error allocation sg list DMA memory, error %d\n", error);
575 error = pvscsi_dma_alloc(sc, &sc->sense_buffer_dma,
576 PVSCSI_SENSE_LENGTH * sc->hcb_cnt, 1);
578 device_printf(sc->dev,
579 "Error allocation sg list DMA memory, error %d\n", error);
583 for (i = 0; i < sc->hcb_cnt; ++i) {
586 error = bus_dmamap_create(sc->buffer_dmat, 0, &hcb->dma_map);
588 device_printf(sc->dev,
589 "Error creating dma map for hcb %d, error %d\n",
595 (void *)((caddr_t)sc->sense_buffer_dma.vaddr +
596 PVSCSI_SENSE_LENGTH * i);
597 hcb->sense_buffer_paddr =
598 sc->sense_buffer_dma.paddr + PVSCSI_SENSE_LENGTH * i;
601 (struct pvscsi_sg_list *)((caddr_t)sc->sg_list_dma.vaddr +
602 sizeof(struct pvscsi_sg_list) * i);
604 sc->sg_list_dma.paddr + sizeof(struct pvscsi_sg_list) * i;
606 callout_init_mtx(&hcb->callout, &sc->lock, 0);
609 SLIST_INIT(&sc->free_list);
610 for (i = (sc->hcb_cnt - 1); i >= 0; --i) {
612 SLIST_INSERT_HEAD(&sc->free_list, hcb, links);
617 pvscsi_dma_free_per_hcb(sc, i);
624 pvscsi_free_rings(struct pvscsi_softc *sc)
627 pvscsi_dma_free(sc, &sc->rings_state_dma);
628 pvscsi_dma_free(sc, &sc->req_ring_dma);
629 pvscsi_dma_free(sc, &sc->cmp_ring_dma);
631 pvscsi_dma_free(sc, &sc->msg_ring_dma);
636 pvscsi_allocate_rings(struct pvscsi_softc *sc)
640 error = pvscsi_dma_alloc_ppns(sc, &sc->rings_state_dma,
641 &sc->rings_state_ppn, 1);
643 device_printf(sc->dev,
644 "Error allocating rings state, error = %d\n", error);
647 sc->rings_state = sc->rings_state_dma.vaddr;
649 error = pvscsi_dma_alloc_ppns(sc, &sc->req_ring_dma, sc->req_ring_ppn,
650 sc->req_ring_num_pages);
652 device_printf(sc->dev,
653 "Error allocating req ring pages, error = %d\n", error);
656 sc->req_ring = sc->req_ring_dma.vaddr;
658 error = pvscsi_dma_alloc_ppns(sc, &sc->cmp_ring_dma, sc->cmp_ring_ppn,
659 sc->cmp_ring_num_pages);
661 device_printf(sc->dev,
662 "Error allocating cmp ring pages, error = %d\n", error);
665 sc->cmp_ring = sc->cmp_ring_dma.vaddr;
669 error = pvscsi_dma_alloc_ppns(sc, &sc->msg_ring_dma,
670 sc->msg_ring_ppn, sc->msg_ring_num_pages);
672 device_printf(sc->dev,
673 "Error allocating cmp ring pages, error = %d\n",
677 sc->msg_ring = sc->msg_ring_dma.vaddr;
680 DEBUG_PRINTF(1, sc->dev, "rings_state: %p\n", sc->rings_state);
681 DEBUG_PRINTF(1, sc->dev, "req_ring: %p - %u pages\n", sc->req_ring,
682 sc->req_ring_num_pages);
683 DEBUG_PRINTF(1, sc->dev, "cmp_ring: %p - %u pages\n", sc->cmp_ring,
684 sc->cmp_ring_num_pages);
685 DEBUG_PRINTF(1, sc->dev, "msg_ring: %p - %u pages\n", sc->msg_ring,
686 sc->msg_ring_num_pages);
690 pvscsi_free_rings(sc);
696 pvscsi_setup_rings(struct pvscsi_softc *sc)
698 struct pvscsi_cmd_desc_setup_rings cmd;
701 bzero(&cmd, sizeof(cmd));
703 cmd.rings_state_ppn = sc->rings_state_ppn;
705 cmd.req_ring_num_pages = sc->req_ring_num_pages;
706 for (i = 0; i < sc->req_ring_num_pages; ++i) {
707 cmd.req_ring_ppns[i] = sc->req_ring_ppn[i];
710 cmd.cmp_ring_num_pages = sc->cmp_ring_num_pages;
711 for (i = 0; i < sc->cmp_ring_num_pages; ++i) {
712 cmd.cmp_ring_ppns[i] = sc->cmp_ring_ppn[i];
715 pvscsi_write_cmd(sc, PVSCSI_CMD_SETUP_RINGS, &cmd, sizeof(cmd));
719 pvscsi_hw_supports_msg(struct pvscsi_softc *sc)
723 pvscsi_reg_write(sc, PVSCSI_REG_OFFSET_COMMAND,
724 PVSCSI_CMD_SETUP_MSG_RING);
725 status = pvscsi_reg_read(sc, PVSCSI_REG_OFFSET_COMMAND_STATUS);
727 return (status != -1);
731 pvscsi_setup_msg_ring(struct pvscsi_softc *sc)
733 struct pvscsi_cmd_desc_setup_msg_ring cmd;
736 KASSERT(sc->use_msg, ("msg is not being used"));
738 bzero(&cmd, sizeof(cmd));
740 cmd.num_pages = sc->msg_ring_num_pages;
741 for (i = 0; i < sc->msg_ring_num_pages; ++i) {
742 cmd.ring_ppns[i] = sc->msg_ring_ppn[i];
745 pvscsi_write_cmd(sc, PVSCSI_CMD_SETUP_MSG_RING, &cmd, sizeof(cmd));
749 pvscsi_adapter_reset(struct pvscsi_softc *sc)
753 device_printf(sc->dev, "Adapter Reset\n");
755 pvscsi_write_cmd(sc, PVSCSI_CMD_ADAPTER_RESET, NULL, 0);
756 val = pvscsi_read_intr_status(sc);
758 DEBUG_PRINTF(2, sc->dev, "adapter reset done: %u\n", val);
762 pvscsi_bus_reset(struct pvscsi_softc *sc)
765 device_printf(sc->dev, "Bus Reset\n");
767 pvscsi_write_cmd(sc, PVSCSI_CMD_RESET_BUS, NULL, 0);
768 pvscsi_process_cmp_ring(sc);
770 DEBUG_PRINTF(2, sc->dev, "bus reset done\n");
774 pvscsi_device_reset(struct pvscsi_softc *sc, uint32_t target)
776 struct pvscsi_cmd_desc_reset_device cmd;
778 memset(&cmd, 0, sizeof(cmd));
782 device_printf(sc->dev, "Device reset for target %u\n", target);
784 pvscsi_write_cmd(sc, PVSCSI_CMD_RESET_DEVICE, &cmd, sizeof cmd);
785 pvscsi_process_cmp_ring(sc);
787 DEBUG_PRINTF(2, sc->dev, "device reset done\n");
791 pvscsi_abort(struct pvscsi_softc *sc, uint32_t target, union ccb *ccb)
793 struct pvscsi_cmd_desc_abort_cmd cmd;
794 struct pvscsi_hcb *hcb;
797 pvscsi_process_cmp_ring(sc);
799 hcb = ccb->ccb_h.ccb_pvscsi_hcb;
802 context = pvscsi_hcb_to_context(sc, hcb);
804 memset(&cmd, 0, sizeof cmd);
806 cmd.context = context;
808 device_printf(sc->dev, "Abort for target %u context %llx\n",
809 target, (unsigned long long)context);
811 pvscsi_write_cmd(sc, PVSCSI_CMD_ABORT_CMD, &cmd, sizeof(cmd));
812 pvscsi_process_cmp_ring(sc);
814 DEBUG_PRINTF(2, sc->dev, "abort done\n");
816 DEBUG_PRINTF(1, sc->dev,
817 "Target %u ccb %p not found for abort\n", target, ccb);
822 pvscsi_probe(device_t dev)
825 if (pci_get_vendor(dev) == PCI_VENDOR_ID_VMWARE &&
826 pci_get_device(dev) == PCI_DEVICE_ID_VMWARE_PVSCSI) {
827 device_set_desc(dev, "VMware Paravirtual SCSI Controller");
828 return (BUS_PROBE_DEFAULT);
834 pvscsi_shutdown(device_t dev)
841 pvscsi_timeout(void *arg)
843 struct pvscsi_hcb *hcb;
844 struct pvscsi_softc *sc;
851 /* Already completed */
855 sc = ccb->ccb_h.ccb_pvscsi_sc;
856 mtx_assert(&sc->lock, MA_OWNED);
858 device_printf(sc->dev, "Command timed out hcb=%p ccb=%p.\n", hcb, ccb);
860 switch (hcb->recovery) {
861 case PVSCSI_HCB_NONE:
862 hcb->recovery = PVSCSI_HCB_ABORT;
863 pvscsi_abort(sc, ccb->ccb_h.target_id, ccb);
864 callout_reset_sbt(&hcb->callout, PVSCSI_ABORT_TIMEOUT * SBT_1S,
865 0, pvscsi_timeout, hcb, 0);
867 case PVSCSI_HCB_ABORT:
868 hcb->recovery = PVSCSI_HCB_DEVICE_RESET;
870 pvscsi_device_reset(sc, ccb->ccb_h.target_id);
871 callout_reset_sbt(&hcb->callout, PVSCSI_RESET_TIMEOUT * SBT_1S,
872 0, pvscsi_timeout, hcb, 0);
874 case PVSCSI_HCB_DEVICE_RESET:
875 hcb->recovery = PVSCSI_HCB_BUS_RESET;
877 pvscsi_bus_reset(sc);
878 callout_reset_sbt(&hcb->callout, PVSCSI_RESET_TIMEOUT * SBT_1S,
879 0, pvscsi_timeout, hcb, 0);
881 case PVSCSI_HCB_BUS_RESET:
883 pvscsi_adapter_reset(sc);
889 pvscsi_process_completion(struct pvscsi_softc *sc,
890 struct pvscsi_ring_cmp_desc *e)
892 struct pvscsi_hcb *hcb;
899 hcb = pvscsi_context_to_hcb(sc, e->context);
901 callout_stop(&hcb->callout);
905 btstat = e->host_status;
906 sdstat = e->scsi_status;
908 ccb->csio.scsi_status = sdstat;
909 ccb->csio.resid = ccb->csio.dxfer_len - e->data_len;
911 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
912 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
913 op = BUS_DMASYNC_POSTREAD;
915 op = BUS_DMASYNC_POSTWRITE;
917 bus_dmamap_sync(sc->buffer_dmat, hcb->dma_map, op);
918 bus_dmamap_unload(sc->buffer_dmat, hcb->dma_map);
921 if (btstat == BTSTAT_SUCCESS && sdstat == SCSI_STATUS_OK) {
922 DEBUG_PRINTF(3, sc->dev,
923 "completing command context %llx success\n",
924 (unsigned long long)e->context);
926 status = CAM_REQ_CMP;
930 case BTSTAT_LINKED_COMMAND_COMPLETED:
931 case BTSTAT_LINKED_COMMAND_COMPLETED_WITH_FLAG:
935 status = CAM_REQ_CMP;
937 case SCSI_STATUS_CHECK_COND:
938 status = CAM_SCSI_STATUS_ERROR;
940 if (ccb->csio.sense_len != 0) {
941 status |= CAM_AUTOSNS_VALID;
943 memset(&ccb->csio.sense_data, 0,
944 sizeof(ccb->csio.sense_data));
945 memcpy(&ccb->csio.sense_data,
947 MIN(ccb->csio.sense_len,
951 case SCSI_STATUS_BUSY:
952 case SCSI_STATUS_QUEUE_FULL:
953 status = CAM_REQUEUE_REQ;
955 case SCSI_STATUS_CMD_TERMINATED:
956 case SCSI_STATUS_TASK_ABORTED:
957 status = CAM_REQ_ABORTED;
960 DEBUG_PRINTF(1, sc->dev,
961 "ccb: %p sdstat=0x%x\n", ccb, sdstat);
962 status = CAM_SCSI_STATUS_ERROR;
966 case BTSTAT_SELTIMEO:
967 status = CAM_SEL_TIMEOUT;
970 case BTSTAT_DATA_UNDERRUN:
971 status = CAM_DATA_RUN_ERR;
973 case BTSTAT_ABORTQUEUE:
974 case BTSTAT_HATIMEOUT:
975 status = CAM_REQUEUE_REQ;
977 case BTSTAT_NORESPONSE:
980 case BTSTAT_BUSRESET:
981 status = CAM_SCSI_BUS_RESET;
983 case BTSTAT_SCSIPARITY:
984 status = CAM_UNCOR_PARITY;
987 status = CAM_UNEXP_BUSFREE;
989 case BTSTAT_INVPHASE:
990 status = CAM_SEQUENCE_FAIL;
992 case BTSTAT_SENSFAILED:
993 status = CAM_AUTOSENSE_FAIL;
995 case BTSTAT_LUNMISMATCH:
996 case BTSTAT_TAGREJECT:
997 case BTSTAT_DISCONNECT:
999 case BTSTAT_INVPARAM:
1000 status = CAM_REQ_CMP_ERR;
1002 case BTSTAT_HASOFTWARE:
1003 case BTSTAT_HAHARDWARE:
1004 status = CAM_NO_HBA;
1007 device_printf(sc->dev, "unknown hba status: 0x%x\n",
1009 status = CAM_NO_HBA;
1013 DEBUG_PRINTF(3, sc->dev,
1014 "completing command context %llx btstat %x sdstat %x - status %x\n",
1015 (unsigned long long)e->context, btstat, sdstat, status);
1018 ccb->ccb_h.ccb_pvscsi_hcb = NULL;
1019 ccb->ccb_h.ccb_pvscsi_sc = NULL;
1020 pvscsi_hcb_put(sc, hcb);
1023 status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
1026 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1030 if (status != CAM_REQ_CMP) {
1031 ccb->ccb_h.status |= CAM_DEV_QFRZN;
1032 xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
1038 pvscsi_process_cmp_ring(struct pvscsi_softc *sc)
1040 struct pvscsi_ring_cmp_desc *ring;
1041 struct pvscsi_rings_state *s;
1042 struct pvscsi_ring_cmp_desc *e;
1045 mtx_assert(&sc->lock, MA_OWNED);
1047 s = sc->rings_state;
1048 ring = sc->cmp_ring;
1049 mask = MASK(s->cmp_num_entries_log2);
1051 while (s->cmp_cons_idx != s->cmp_prod_idx) {
1052 e = ring + (s->cmp_cons_idx & mask);
1054 pvscsi_process_completion(sc, e);
1062 pvscsi_process_msg(struct pvscsi_softc *sc, struct pvscsi_ring_msg_desc *e)
1064 struct pvscsi_ring_msg_dev_status_changed *desc;
1068 case PVSCSI_MSG_DEV_ADDED:
1069 case PVSCSI_MSG_DEV_REMOVED: {
1070 desc = (struct pvscsi_ring_msg_dev_status_changed *)e;
1072 device_printf(sc->dev, "MSG: device %s at scsi%u:%u:%u\n",
1073 desc->type == PVSCSI_MSG_DEV_ADDED ? "addition" : "removal",
1074 desc->bus, desc->target, desc->lun[1]);
1076 ccb = xpt_alloc_ccb_nowait();
1078 device_printf(sc->dev,
1079 "Error allocating CCB for dev change.\n");
1083 if (xpt_create_path(&ccb->ccb_h.path, NULL,
1084 cam_sim_path(sc->sim), desc->target, desc->lun[1])
1086 device_printf(sc->dev,
1087 "Error creating path for dev change.\n");
1095 device_printf(sc->dev, "Unknown msg type 0x%x\n", e->type);
1100 pvscsi_process_msg_ring(struct pvscsi_softc *sc)
1102 struct pvscsi_ring_msg_desc *ring;
1103 struct pvscsi_rings_state *s;
1104 struct pvscsi_ring_msg_desc *e;
1107 mtx_assert(&sc->lock, MA_OWNED);
1109 s = sc->rings_state;
1110 ring = sc->msg_ring;
1111 mask = MASK(s->msg_num_entries_log2);
1113 while (s->msg_cons_idx != s->msg_prod_idx) {
1114 e = ring + (s->msg_cons_idx & mask);
1116 pvscsi_process_msg(sc, e);
1124 pvscsi_intr_locked(struct pvscsi_softc *sc)
1128 mtx_assert(&sc->lock, MA_OWNED);
1130 val = pvscsi_read_intr_status(sc);
1132 if ((val & PVSCSI_INTR_ALL_SUPPORTED) != 0) {
1133 pvscsi_write_intr_status(sc, val & PVSCSI_INTR_ALL_SUPPORTED);
1134 pvscsi_process_cmp_ring(sc);
1136 pvscsi_process_msg_ring(sc);
1142 pvscsi_intr(void *xsc)
1144 struct pvscsi_softc *sc;
1148 mtx_assert(&sc->lock, MA_NOTOWNED);
1150 mtx_lock(&sc->lock);
1151 pvscsi_intr_locked(xsc);
1152 mtx_unlock(&sc->lock);
1156 pvscsi_poll(struct cam_sim *sim)
1158 struct pvscsi_softc *sc;
1160 sc = cam_sim_softc(sim);
1162 mtx_assert(&sc->lock, MA_OWNED);
1163 pvscsi_intr_locked(sc);
1167 pvscsi_execute_ccb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1169 struct pvscsi_hcb *hcb;
1170 struct pvscsi_ring_req_desc *e;
1172 struct pvscsi_softc *sc;
1173 struct pvscsi_rings_state *s;
1175 bus_dmasync_op_t op;
1180 sc = ccb->ccb_h.ccb_pvscsi_sc;
1181 s = sc->rings_state;
1183 mtx_assert(&sc->lock, MA_OWNED);
1186 device_printf(sc->dev, "pvscsi_execute_ccb error %d\n", error);
1188 if (error == EFBIG) {
1189 ccb->ccb_h.status = CAM_REQ_TOO_BIG;
1191 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1194 pvscsi_hcb_put(sc, hcb);
1201 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
1203 e->flags |= PVSCSI_FLAG_CMD_DIR_NONE;
1206 e->flags |= PVSCSI_FLAG_CMD_DIR_TOHOST;
1207 op = BUS_DMASYNC_PREREAD;
1210 e->flags |= PVSCSI_FLAG_CMD_DIR_TODEVICE;
1211 op = BUS_DMASYNC_PREWRITE;
1214 /* TODO: does this need handling? */
1221 struct pvscsi_sg_element *sge;
1223 KASSERT(nseg <= PVSCSI_MAX_SG_ENTRIES_PER_SEGMENT,
1224 ("too many sg segments"));
1226 sge = hcb->sg_list->sge;
1227 e->flags |= PVSCSI_FLAG_CMD_WITH_SG_LIST;
1229 for (i = 0; i < nseg; ++i) {
1230 sge[i].addr = segs[i].ds_addr;
1231 sge[i].length = segs[i].ds_len;
1235 e->data_addr = hcb->sg_list_paddr;
1237 e->data_addr = segs->ds_addr;
1240 bus_dmamap_sync(sc->buffer_dmat, hcb->dma_map, op);
1246 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1248 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1249 callout_reset_sbt(&hcb->callout, ccb->ccb_h.timeout * SBT_1MS,
1250 0, pvscsi_timeout, hcb, 0);
1255 pvscsi_kick_io(sc, cdb0);
1259 pvscsi_action(struct cam_sim *sim, union ccb *ccb)
1261 struct pvscsi_softc *sc;
1262 struct ccb_hdr *ccb_h;
1264 sc = cam_sim_softc(sim);
1265 ccb_h = &ccb->ccb_h;
1267 mtx_assert(&sc->lock, MA_OWNED);
1269 switch (ccb_h->func_code) {
1272 struct ccb_scsiio *csio;
1273 uint32_t req_num_entries_log2;
1274 struct pvscsi_ring_req_desc *ring;
1275 struct pvscsi_ring_req_desc *e;
1276 struct pvscsi_rings_state *s;
1277 struct pvscsi_hcb *hcb;
1280 ring = sc->req_ring;
1281 s = sc->rings_state;
1286 * Check if it was completed already (such as aborted
1289 if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1294 req_num_entries_log2 = s->req_num_entries_log2;
1296 if (s->req_prod_idx - s->cmp_cons_idx >=
1297 (1 << req_num_entries_log2)) {
1298 device_printf(sc->dev,
1299 "Not enough room on completion ring.\n");
1301 ccb_h->status = CAM_REQUEUE_REQ;
1305 hcb = pvscsi_hcb_get(sc);
1307 device_printf(sc->dev, "No free hcbs.\n");
1309 ccb_h->status = CAM_REQUEUE_REQ;
1314 ccb_h->ccb_pvscsi_hcb = hcb;
1315 ccb_h->ccb_pvscsi_sc = sc;
1317 if (csio->cdb_len > sizeof(e->cdb)) {
1318 DEBUG_PRINTF(2, sc->dev, "cdb length %u too large\n",
1320 ccb_h->status = CAM_REQ_INVALID;
1324 if (ccb_h->flags & CAM_CDB_PHYS) {
1325 DEBUG_PRINTF(2, sc->dev,
1326 "CAM_CDB_PHYS not implemented\n");
1327 ccb_h->status = CAM_REQ_INVALID;
1331 e = ring + (s->req_prod_idx & MASK(req_num_entries_log2));
1333 e->bus = cam_sim_bus(sim);
1334 e->target = ccb_h->target_id;
1335 memset(e->lun, 0, sizeof(e->lun));
1336 e->lun[1] = ccb_h->target_lun;
1338 e->data_len = csio->dxfer_len;
1339 e->vcpu_hint = curcpu;
1341 e->cdb_len = csio->cdb_len;
1342 memcpy(e->cdb, scsiio_cdb_ptr(csio), csio->cdb_len);
1345 e->sense_len = csio->sense_len;
1346 if (e->sense_len > 0) {
1347 e->sense_addr = hcb->sense_buffer_paddr;
1350 e->tag = MSG_SIMPLE_Q_TAG;
1351 if (ccb_h->flags & CAM_TAG_ACTION_VALID) {
1352 e->tag = csio->tag_action;
1355 e->context = pvscsi_hcb_to_context(sc, hcb);
1358 DEBUG_PRINTF(3, sc->dev,
1359 " queuing command %02x context %llx\n", e->cdb[0],
1360 (unsigned long long)e->context);
1361 bus_dmamap_load_ccb(sc->buffer_dmat, hcb->dma_map, ccb,
1362 pvscsi_execute_ccb, hcb, 0);
1367 pvscsi_hcb_put(sc, hcb);
1373 struct pvscsi_hcb *abort_hcb;
1374 union ccb *abort_ccb;
1376 abort_ccb = ccb->cab.abort_ccb;
1377 abort_hcb = abort_ccb->ccb_h.ccb_pvscsi_hcb;
1379 if (abort_hcb->ccb != NULL && abort_hcb->ccb == abort_ccb) {
1380 if (abort_ccb->ccb_h.func_code == XPT_SCSI_IO) {
1381 pvscsi_abort(sc, ccb_h->target_id, abort_ccb);
1382 ccb_h->status = CAM_REQ_CMP;
1384 ccb_h->status = CAM_UA_ABORT;
1387 device_printf(sc->dev,
1388 "Could not find hcb for ccb %p (tgt %u)\n",
1389 ccb, ccb_h->target_id);
1390 ccb_h->status = CAM_REQ_CMP;
1396 pvscsi_device_reset(sc, ccb_h->target_id);
1397 ccb_h->status = CAM_REQ_CMP;
1402 pvscsi_bus_reset(sc);
1403 ccb_h->status = CAM_REQ_CMP;
1408 struct ccb_pathinq *cpi;
1412 cpi->version_num = 1;
1413 cpi->hba_inquiry = PI_TAG_ABLE;
1414 cpi->target_sprt = 0;
1415 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
1416 cpi->hba_eng_cnt = 0;
1417 /* cpi->vuhba_flags = 0; */
1418 cpi->max_target = sc->max_targets;
1420 cpi->async_flags = 0;
1422 cpi->unit_number = cam_sim_unit(sim);
1423 cpi->bus_id = cam_sim_bus(sim);
1424 cpi->initiator_id = 7;
1425 cpi->base_transfer_speed = 750000;
1426 strlcpy(cpi->sim_vid, "VMware", SIM_IDLEN);
1427 strlcpy(cpi->hba_vid, "VMware", HBA_IDLEN);
1428 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1429 cpi->maxio = PVSCSI_MAX_SG_ENTRIES_PER_SEGMENT * PAGE_SIZE;
1430 cpi->protocol = PROTO_SCSI;
1431 cpi->protocol_version = SCSI_REV_SPC2;
1432 cpi->transport = XPORT_SAS;
1433 cpi->transport_version = 0;
1435 ccb_h->status = CAM_REQ_CMP;
1438 case XPT_GET_TRAN_SETTINGS:
1440 struct ccb_trans_settings *cts;
1444 cts->protocol = PROTO_SCSI;
1445 cts->protocol_version = SCSI_REV_SPC2;
1446 cts->transport = XPORT_SAS;
1447 cts->transport_version = 0;
1449 cts->proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
1450 cts->proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
1452 ccb_h->status = CAM_REQ_CMP;
1455 case XPT_CALC_GEOMETRY:
1457 cam_calc_geometry(&ccb->ccg, 1);
1461 ccb_h->status = CAM_REQ_INVALID;
1468 pvscsi_free_interrupts(struct pvscsi_softc *sc)
1471 if (sc->irq_handler != NULL) {
1472 bus_teardown_intr(sc->dev, sc->irq_res, sc->irq_handler);
1474 if (sc->irq_res != NULL) {
1475 bus_release_resource(sc->dev, SYS_RES_IRQ, sc->irq_id,
1478 if (sc->use_msi_or_msix) {
1479 pci_release_msi(sc->dev);
1484 pvscsi_setup_interrupts(struct pvscsi_softc *sc)
1492 sc->use_msi_or_msix = 0;
1494 use_msix = pvscsi_get_tunable(sc, "use_msix", pvscsi_use_msix);
1495 use_msi = pvscsi_get_tunable(sc, "use_msi", pvscsi_use_msi);
1497 if (use_msix && pci_msix_count(sc->dev) > 0) {
1499 if (pci_alloc_msix(sc->dev, &count) == 0 && count == 1) {
1500 sc->use_msi_or_msix = 1;
1501 device_printf(sc->dev, "Interrupt: MSI-X\n");
1503 pci_release_msi(sc->dev);
1507 if (sc->use_msi_or_msix == 0 && use_msi && pci_msi_count(sc->dev) > 0) {
1509 if (pci_alloc_msi(sc->dev, &count) == 0 && count == 1) {
1510 sc->use_msi_or_msix = 1;
1511 device_printf(sc->dev, "Interrupt: MSI\n");
1513 pci_release_msi(sc->dev);
1518 if (sc->use_msi_or_msix) {
1521 device_printf(sc->dev, "Interrupt: INT\n");
1523 flags |= RF_SHAREABLE;
1526 sc->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &sc->irq_id,
1528 if (sc->irq_res == NULL) {
1529 device_printf(sc->dev, "IRQ allocation failed\n");
1530 if (sc->use_msi_or_msix) {
1531 pci_release_msi(sc->dev);
1536 error = bus_setup_intr(sc->dev, sc->irq_res,
1537 INTR_TYPE_CAM | INTR_MPSAFE, NULL, pvscsi_intr, sc,
1540 device_printf(sc->dev, "IRQ handler setup failed\n");
1541 pvscsi_free_interrupts(sc);
1549 pvscsi_free_all(struct pvscsi_softc *sc)
1556 xpt_free_path(sc->bus_path);
1559 status = xpt_bus_deregister(cam_sim_path(sc->sim));
1560 if (status != CAM_REQ_CMP) {
1561 device_printf(sc->dev,
1562 "Error deregistering bus, status=%d\n", status);
1565 cam_sim_free(sc->sim, TRUE);
1568 pvscsi_dma_free_per_hcb(sc, sc->hcb_cnt);
1571 free(sc->hcbs, M_PVSCSI);
1574 pvscsi_free_rings(sc);
1576 pvscsi_free_interrupts(sc);
1578 if (sc->buffer_dmat != NULL) {
1579 bus_dma_tag_destroy(sc->buffer_dmat);
1582 if (sc->parent_dmat != NULL) {
1583 bus_dma_tag_destroy(sc->parent_dmat);
1586 if (sc->mm_res != NULL) {
1587 bus_release_resource(sc->dev, SYS_RES_MEMORY, sc->mm_rid,
1593 pvscsi_attach(device_t dev)
1595 struct pvscsi_softc *sc;
1599 int max_queue_depth;
1600 int adapter_queue_size;
1601 struct cam_devq *devq;
1603 sc = device_get_softc(dev);
1606 mtx_init(&sc->lock, "pvscsi", NULL, MTX_DEF);
1608 pci_enable_busmaster(dev);
1611 for (barid = 0; barid <= PCIR_MAX_BAR_0; ++barid) {
1612 rid = PCIR_BAR(barid);
1614 sc->mm_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1616 if (sc->mm_res != NULL) {
1622 if (sc->mm_res == NULL) {
1623 device_printf(dev, "could not map device memory\n");
1627 error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
1628 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE,
1629 BUS_SPACE_UNRESTRICTED, BUS_SPACE_MAXSIZE, 0, NULL, NULL,
1632 device_printf(dev, "parent dma tag create failure, error %d\n",
1634 pvscsi_free_all(sc);
1638 error = bus_dma_tag_create(sc->parent_dmat, 1, 0,
1639 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1640 PVSCSI_MAX_SG_ENTRIES_PER_SEGMENT * PAGE_SIZE,
1641 PVSCSI_MAX_SG_ENTRIES_PER_SEGMENT, PAGE_SIZE, BUS_DMA_ALLOCNOW,
1642 NULL, NULL, &sc->buffer_dmat);
1644 device_printf(dev, "parent dma tag create failure, error %d\n",
1646 pvscsi_free_all(sc);
1650 error = pvscsi_setup_interrupts(sc);
1652 device_printf(dev, "Interrupt setup failed\n");
1653 pvscsi_free_all(sc);
1657 sc->max_targets = pvscsi_get_max_targets(sc);
1659 sc->use_msg = pvscsi_get_tunable(sc, "use_msg", pvscsi_use_msg) &&
1660 pvscsi_hw_supports_msg(sc);
1661 sc->msg_ring_num_pages = sc->use_msg ? 1 : 0;
1663 sc->req_ring_num_pages = pvscsi_get_tunable(sc, "request_ring_pages",
1664 pvscsi_request_ring_pages);
1665 if (sc->req_ring_num_pages <= 0) {
1666 if (sc->max_targets <= 16) {
1667 sc->req_ring_num_pages =
1668 PVSCSI_DEFAULT_NUM_PAGES_REQ_RING;
1670 sc->req_ring_num_pages = PVSCSI_MAX_NUM_PAGES_REQ_RING;
1672 } else if (sc->req_ring_num_pages > PVSCSI_MAX_NUM_PAGES_REQ_RING) {
1673 sc->req_ring_num_pages = PVSCSI_MAX_NUM_PAGES_REQ_RING;
1675 sc->cmp_ring_num_pages = sc->req_ring_num_pages;
1677 max_queue_depth = pvscsi_get_tunable(sc, "max_queue_depth",
1678 pvscsi_max_queue_depth);
1680 adapter_queue_size = (sc->req_ring_num_pages * PAGE_SIZE) /
1681 sizeof(struct pvscsi_ring_req_desc);
1682 if (max_queue_depth > 0) {
1683 adapter_queue_size = MIN(adapter_queue_size, max_queue_depth);
1685 adapter_queue_size = MIN(adapter_queue_size,
1686 PVSCSI_MAX_REQ_QUEUE_DEPTH);
1688 device_printf(sc->dev, "Use Msg: %d\n", sc->use_msg);
1689 device_printf(sc->dev, "REQ num pages: %d\n", sc->req_ring_num_pages);
1690 device_printf(sc->dev, "CMP num pages: %d\n", sc->cmp_ring_num_pages);
1691 device_printf(sc->dev, "MSG num pages: %d\n", sc->msg_ring_num_pages);
1692 device_printf(sc->dev, "Queue size: %d\n", adapter_queue_size);
1694 if (pvscsi_allocate_rings(sc)) {
1695 device_printf(dev, "ring allocation failed\n");
1696 pvscsi_free_all(sc);
1700 sc->hcb_cnt = adapter_queue_size;
1701 sc->hcbs = malloc(sc->hcb_cnt * sizeof(*sc->hcbs), M_PVSCSI,
1703 if (sc->hcbs == NULL) {
1704 device_printf(dev, "error allocating hcb array\n");
1705 pvscsi_free_all(sc);
1709 if (pvscsi_dma_alloc_per_hcb(sc)) {
1710 device_printf(dev, "error allocating per hcb dma memory\n");
1711 pvscsi_free_all(sc);
1715 pvscsi_adapter_reset(sc);
1717 devq = cam_simq_alloc(adapter_queue_size);
1719 device_printf(dev, "cam devq alloc failed\n");
1720 pvscsi_free_all(sc);
1724 sc->sim = cam_sim_alloc(pvscsi_action, pvscsi_poll, "pvscsi", sc,
1725 device_get_unit(dev), &sc->lock, 1, adapter_queue_size, devq);
1726 if (sc->sim == NULL) {
1727 device_printf(dev, "cam sim alloc failed\n");
1728 cam_simq_free(devq);
1729 pvscsi_free_all(sc);
1733 mtx_lock(&sc->lock);
1735 if (xpt_bus_register(sc->sim, dev, 0) != CAM_SUCCESS) {
1736 device_printf(dev, "xpt bus register failed\n");
1737 pvscsi_free_all(sc);
1738 mtx_unlock(&sc->lock);
1742 if (xpt_create_path(&sc->bus_path, NULL, cam_sim_path(sc->sim),
1743 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1744 device_printf(dev, "xpt create path failed\n");
1745 pvscsi_free_all(sc);
1746 mtx_unlock(&sc->lock);
1750 pvscsi_setup_rings(sc);
1752 pvscsi_setup_msg_ring(sc);
1755 sc->use_req_call_threshold = pvscsi_setup_req_call(sc, 1);
1757 pvscsi_intr_enable(sc);
1759 mtx_unlock(&sc->lock);
1765 pvscsi_detach(device_t dev)
1767 struct pvscsi_softc *sc;
1769 sc = device_get_softc(dev);
1771 pvscsi_intr_disable(sc);
1772 pvscsi_adapter_reset(sc);
1774 if (sc->irq_handler != NULL) {
1775 bus_teardown_intr(dev, sc->irq_res, sc->irq_handler);
1778 mtx_lock(&sc->lock);
1779 pvscsi_free_all(sc);
1780 mtx_unlock(&sc->lock);
1782 mtx_destroy(&sc->lock);
1787 static device_method_t pvscsi_methods[] = {
1788 DEVMETHOD(device_probe, pvscsi_probe),
1789 DEVMETHOD(device_shutdown, pvscsi_shutdown),
1790 DEVMETHOD(device_attach, pvscsi_attach),
1791 DEVMETHOD(device_detach, pvscsi_detach),
1795 static driver_t pvscsi_driver = {
1796 "pvscsi", pvscsi_methods, sizeof(struct pvscsi_softc)
1799 static devclass_t pvscsi_devclass;
1800 DRIVER_MODULE(pvscsi, pci, pvscsi_driver, pvscsi_devclass, 0, 0);
1802 MODULE_DEPEND(pvscsi, pci, 1, 1, 1);
1803 MODULE_DEPEND(pvscsi, cam, 1, 1, 1);