24 #include <linux/kernel.h>
25 #include <linux/module.h>
27 #include <linux/slab.h>
29 #include <linux/pci.h>
31 #include <scsi/scsi.h>
38 #define PVSCSI_LINUX_DRIVER_DESC "VMware PVSCSI driver"
45 #define PVSCSI_DEFAULT_NUM_PAGES_PER_RING 8
46 #define PVSCSI_DEFAULT_NUM_PAGES_MSG_RING 1
47 #define PVSCSI_DEFAULT_QUEUE_DEPTH 64
48 #define SGL_SIZE PAGE_SIZE
108 static bool pvscsi_disable_msi;
109 static bool pvscsi_disable_msix;
110 static bool pvscsi_use_msg =
true;
112 #define PVSCSI_RW (S_IRUSR | S_IWUSR)
119 MODULE_PARM_DESC(msg_ring_pages,
"Number of pages for the msg ring - (default="
130 MODULE_PARM_DESC(disable_msix,
"Disable MSI-X use in driver - (default=0)");
145 return &(adapter->
dev->dev);
154 for (ctx = adapter->
cmd_map; ctx < end; ctx++)
176 static void pvscsi_release_context(
struct pvscsi_adapter *adapter,
191 return ctx - adapter->
cmd_map + 1;
197 return &adapter->
cmd_map[context - 1];
200 static void pvscsi_reg_write(
const struct pvscsi_adapter *adapter,
216 static void pvscsi_write_intr_status(
const struct pvscsi_adapter *adapter,
222 static void pvscsi_unmask_intr(
const struct pvscsi_adapter *adapter)
233 static void pvscsi_mask_intr(
const struct pvscsi_adapter *adapter)
238 static void pvscsi_write_cmd_desc(
const struct pvscsi_adapter *adapter,
239 u32 cmd,
const void *
desc,
size_t len)
246 for (i = 0; i < len; i++)
247 pvscsi_reg_write(adapter,
251 static void pvscsi_abort_cmd(
const struct pvscsi_adapter *adapter,
257 cmd.
context = pvscsi_map_context(adapter, ctx);
262 static void pvscsi_kick_rw_io(
const struct pvscsi_adapter *adapter)
267 static void pvscsi_process_request_ring(
const struct pvscsi_adapter *adapter)
272 static int scsi_is_rw(
unsigned char op)
284 pvscsi_kick_rw_io(adapter);
286 pvscsi_process_request_ring(adapter);
289 static void ll_adapter_reset(
const struct pvscsi_adapter *adapter)
291 dev_dbg(pvscsi_dev(adapter),
"Adapter Reset on %p\n", adapter);
298 dev_dbg(pvscsi_dev(adapter),
"Resetting bus on %p\n", adapter);
307 dev_dbg(pvscsi_dev(adapter),
"Resetting device: target=%u\n", target);
315 static void pvscsi_create_sg(
struct pvscsi_ctx *ctx,
323 sge = &ctx->
sgl->sge[0];
324 for (i = 0; i <
count; i++, sg++) {
340 unsigned bufflen = scsi_bufflen(cmd);
348 sg = scsi_sglist(cmd);
349 count = scsi_sg_count(cmd);
353 pvscsi_create_sg(ctx, sg, segs);
356 ctx->
sglPA = pci_map_single(adapter->
dev, ctx->
sgl,
366 ctx->
dataPA = pci_map_single(adapter->
dev, sg, bufflen,
372 static void pvscsi_unmap_buffers(
const struct pvscsi_adapter *adapter,
379 bufflen = scsi_bufflen(cmd);
382 unsigned count = scsi_sg_count(cmd);
387 pci_unmap_single(adapter->
dev, ctx->
sglPA,
392 pci_unmap_single(adapter->
dev, ctx->
dataPA, bufflen,
433 pvscsi_msg_ring_pages);
444 static void pvscsi_setup_all_rings(
const struct pvscsi_adapter *adapter)
455 for (i = 0; i < adapter->
req_pages; i++) {
461 for (i = 0; i < adapter->
cmp_pages; i++) {
479 for (i = 0; i < adapter->
msg_pages; i++) {
486 &cmd_msg,
sizeof(cmd_msg));
494 static void pvscsi_complete_request(
struct pvscsi_adapter *adapter,
502 ctx = pvscsi_get_context(adapter, e->
context);
504 pvscsi_unmap_buffers(adapter, ctx);
505 pvscsi_release_context(adapter, ctx);
527 scsi_set_resid(cmd, scsi_bufflen(cmd) - e->
dataLen);
570 "Unknown completion status: 0x%x\n",
575 "cmd=%p %x ctx=%p result=0x%x status=0x%x,%x\n",
576 cmd, cmd->
cmnd[0], ctx, cmd->
result, btstat, sdstat);
588 static void pvscsi_process_completion_ring(
struct pvscsi_adapter *adapter)
604 pvscsi_complete_request(adapter, e);
641 "ring full: reqProdIdx=%d cmpConsIdx=%d\n",
682 pvscsi_map_buffers(adapter, ctx, cmd, e);
684 e->
context = pvscsi_map_context(adapter, ctx);
702 ctx = pvscsi_acquire_context(adapter, cmd);
703 if (!ctx || pvscsi_queue_ring(adapter, ctx, cmd) != 0) {
705 pvscsi_release_context(adapter, ctx);
706 spin_unlock_irqrestore(&adapter->
hw_lock, flags);
713 "queued cmd %p, ctx %p, op=%x\n", cmd, ctx, cmd->
cmnd[0]);
715 spin_unlock_irqrestore(&adapter->
hw_lock, flags);
717 pvscsi_kick_io(adapter, cmd->
cmnd[0]);
731 adapter->
host->host_no, cmd);
739 pvscsi_process_completion_ring(adapter);
745 ctx = pvscsi_find_context(adapter, cmd);
751 pvscsi_abort_cmd(adapter, ctx);
753 pvscsi_process_completion_ring(adapter);
756 spin_unlock_irqrestore(&adapter->
hw_lock, flags);
770 for (i = 0; i < adapter->
req_depth; i++) {
775 "Forced reset on cmd %p\n", cmd);
776 pvscsi_unmap_buffers(adapter, ctx);
777 pvscsi_release_context(adapter, ctx);
784 static int pvscsi_host_reset(
struct scsi_cmnd *cmd)
799 spin_unlock_irqrestore(&adapter->
hw_lock, flags);
815 pvscsi_process_request_ring(adapter);
817 ll_adapter_reset(adapter);
826 pvscsi_process_completion_ring(adapter);
828 pvscsi_reset_all(adapter);
830 pvscsi_setup_all_rings(adapter);
831 pvscsi_unmask_intr(adapter);
833 spin_unlock_irqrestore(&adapter->
hw_lock, flags);
838 static int pvscsi_bus_reset(
struct scsi_cmnd *cmd)
854 pvscsi_process_request_ring(adapter);
855 ll_bus_reset(adapter);
856 pvscsi_process_completion_ring(adapter);
858 spin_unlock_irqrestore(&adapter->
hw_lock, flags);
863 static int pvscsi_device_reset(
struct scsi_cmnd *cmd)
879 pvscsi_process_request_ring(adapter);
880 ll_device_reset(adapter, cmd->
device->id);
881 pvscsi_process_completion_ring(adapter);
883 spin_unlock_irqrestore(&adapter->
hw_lock, flags);
890 static const char *pvscsi_info(
struct Scsi_Host *host)
893 static char buf[256];
895 sprintf(buf,
"VMware PVSCSI storage adapter rev %d, req/cmp/msg rings: "
896 "%u/%u/%u pages, cmd_per_lun=%u", adapter->
rev,
898 pvscsi_template.cmd_per_lun);
905 .name =
"VMware PVSCSI Host Adapter",
906 .proc_name =
"vmw_pvscsi",
908 .queuecommand = pvscsi_queue,
912 .max_sectors = 0xffff,
914 .eh_abort_handler = pvscsi_abort,
915 .eh_device_reset_handler = pvscsi_device_reset,
916 .eh_bus_reset_handler = pvscsi_bus_reset,
917 .eh_host_reset_handler = pvscsi_host_reset,
920 static void pvscsi_process_msg(
const struct pvscsi_adapter *adapter,
927 printk(
KERN_INFO "vmw_pvscsi: msg type: 0x%x - MSG RING: %u/%u (%u) \n",
937 "vmw_pvscsi: msg: device added at scsi%u:%u:%u\n",
958 "vmw_pvscsi: msg: device removed at scsi%u:%u:%u\n",
971 "vmw_pvscsi: failed to lookup scsi%u:%u:%u\n",
978 static int pvscsi_msg_pending(
const struct pvscsi_adapter *adapter)
985 static void pvscsi_process_msg_ring(
const struct pvscsi_adapter *adapter)
991 while (pvscsi_msg_pending(adapter)) {
996 pvscsi_process_msg(adapter, e);
1008 pvscsi_process_msg_ring(adapter);
1011 static int pvscsi_setup_msg_workqueue(
struct pvscsi_adapter *adapter)
1015 if (!pvscsi_use_msg)
1025 "vmw_pvscsi_wq_%u", adapter->
host->host_no);
1045 u32 val = pvscsi_read_intr_status(adapter);
1048 pvscsi_write_intr_status(devp, val);
1052 unsigned long flags;
1056 pvscsi_process_completion_ring(adapter);
1057 if (adapter->
use_msg && pvscsi_msg_pending(adapter))
1060 spin_unlock_irqrestore(&adapter->
hw_lock, flags);
1066 static void pvscsi_free_sgls(
const struct pvscsi_adapter *adapter)
1075 static int pvscsi_setup_msix(
const struct pvscsi_adapter *adapter,
1085 *irq = entry.vector;
1105 static void pvscsi_release_resources(
struct pvscsi_adapter *adapter)
1107 pvscsi_shutdown_intr(adapter);
1118 pvscsi_free_sgls(adapter);
1169 for (; i >= 0; --
i, --
ctx) {
1195 dev = pvscsi_dev(adapter);
1199 dev_warn(dev,
"vmw_pvscsi: failed to allocate memory for config page\n");
1207 cmd.cmpAddr = configPagePA;
1215 header = config_page;
1216 memset(header, 0,
sizeof *header);
1226 config = config_page;
1229 dev_warn(dev,
"vmw_pvscsi: PVSCSI_CMD_CONFIG failed. hostStatus = 0x%x, scsiStatus = 0x%x\n",
1243 unsigned long flags = 0;
1252 pci_set_consistent_dma_mask(pdev,
DMA_BIT_MASK(64)) == 0) {
1254 }
else if (pci_set_dma_mask(pdev,
DMA_BIT_MASK(32)) == 0 &&
1255 pci_set_consistent_dma_mask(pdev,
DMA_BIT_MASK(32)) == 0) {
1259 goto out_disable_device;
1270 goto out_disable_device;
1273 adapter = shost_priv(host);
1274 memset(adapter, 0,
sizeof(*adapter));
1275 adapter->
dev = pdev;
1276 adapter->
host = host;
1302 if (i == DEVICE_COUNT_RESOURCE) {
1304 "vmw_pvscsi: adapter has no suitable MMIO region\n");
1305 goto out_release_resources;
1312 "vmw_pvscsi: can't iomap for BAR %d memsize %lu\n",
1314 goto out_release_resources;
1318 pci_set_drvdata(pdev, host);
1320 ll_adapter_reset(adapter);
1322 adapter->
use_msg = pvscsi_setup_msg_workqueue(adapter);
1324 error = pvscsi_allocate_rings(adapter);
1327 goto out_release_resources;
1333 host->
max_id = pvscsi_get_max_targets(adapter);
1334 dev = pvscsi_dev(adapter);
1341 pvscsi_setup_all_rings(adapter);
1348 goto out_reset_adapter;
1351 INIT_LIST_HEAD(&adapter->
cmd_pool);
1352 for (i = 0; i < adapter->
req_depth; i++) {
1357 error = pvscsi_allocate_sg(adapter);
1360 goto out_reset_adapter;
1363 if (!pvscsi_disable_msix &&
1364 pvscsi_setup_msix(adapter, &adapter->
irq) == 0) {
1367 }
else if (!pvscsi_disable_msi && pci_enable_msi(pdev) == 0) {
1370 adapter->
irq = pdev->
irq;
1373 adapter->
irq = pdev->
irq;
1378 "vmw_pvscsi", adapter);
1381 "vmw_pvscsi: unable to request IRQ: %d\n", error);
1383 goto out_reset_adapter;
1386 error = scsi_add_host(host, &pdev->
dev);
1389 "vmw_pvscsi: scsi_add_host failed: %d\n", error);
1390 goto out_reset_adapter;
1393 dev_info(&pdev->
dev,
"VMware PVSCSI rev %d host #%u\n",
1396 pvscsi_unmask_intr(adapter);
1403 ll_adapter_reset(adapter);
1404 out_release_resources:
1405 pvscsi_release_resources(adapter);
1409 pci_set_drvdata(pdev,
NULL);
1417 pvscsi_mask_intr(adapter);
1422 pvscsi_shutdown_intr(adapter);
1424 pvscsi_process_request_ring(adapter);
1425 pvscsi_process_completion_ring(adapter);
1426 ll_adapter_reset(adapter);
1429 static void pvscsi_shutdown(
struct pci_dev *dev)
1431 struct Scsi_Host *host = pci_get_drvdata(dev);
1434 __pvscsi_shutdown(adapter);
1437 static void pvscsi_remove(
struct pci_dev *pdev)
1439 struct Scsi_Host *host = pci_get_drvdata(pdev);
1444 __pvscsi_shutdown(adapter);
1445 pvscsi_release_resources(adapter);
1449 pci_set_drvdata(pdev,
NULL);
1453 static struct pci_driver pvscsi_pci_driver = {
1454 .name =
"vmw_pvscsi",
1455 .id_table = pvscsi_pci_tbl,
1456 .probe = pvscsi_probe,
1458 .shutdown = pvscsi_shutdown,
1461 static int __init pvscsi_init(
void)
1465 return pci_register_driver(&pvscsi_pci_driver);
1468 static void __exit pvscsi_exit(
void)