blob: b92ea94be98ffa7aae10733314a8010ea141a234 [file] [log] [blame]
Alok Kataria851b1642009-10-13 14:51:05 -07001/*
2 * Linux driver for VMware's para-virtualized SCSI HBA.
3 *
Arvind Kumara2713cc2014-03-08 12:51:12 -08004 * Copyright (C) 2008-2014, VMware, Inc. All Rights Reserved.
Alok Kataria851b1642009-10-13 14:51:05 -07005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 of the License and no later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
14 * details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
Arvind Kumara9310732012-03-08 15:48:53 +053020 * Maintained by: Arvind Kumar <arvindkumar@vmware.com>
Alok Kataria851b1642009-10-13 14:51:05 -070021 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Alok Kataria851b1642009-10-13 14:51:05 -070028#include <linux/workqueue.h>
29#include <linux/pci.h>
30
31#include <scsi/scsi.h>
32#include <scsi/scsi_host.h>
33#include <scsi/scsi_cmnd.h>
34#include <scsi/scsi_device.h>
35
36#include "vmw_pvscsi.h"
37
38#define PVSCSI_LINUX_DRIVER_DESC "VMware PVSCSI driver"
39
40MODULE_DESCRIPTION(PVSCSI_LINUX_DRIVER_DESC);
41MODULE_AUTHOR("VMware, Inc.");
42MODULE_LICENSE("GPL");
43MODULE_VERSION(PVSCSI_DRIVER_VERSION_STRING);
44
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
49
50struct pvscsi_sg_list {
51 struct PVSCSISGElement sge[PVSCSI_MAX_NUM_SG_ENTRIES_PER_SEGMENT];
52};
53
54struct pvscsi_ctx {
55 /*
56 * The index of the context in cmd_map serves as the context ID for a
57 * 1-to-1 mapping completions back to requests.
58 */
59 struct scsi_cmnd *cmd;
60 struct pvscsi_sg_list *sgl;
61 struct list_head list;
62 dma_addr_t dataPA;
63 dma_addr_t sensePA;
64 dma_addr_t sglPA;
Arvind Kumara2713cc2014-03-08 12:51:12 -080065 struct completion *abort_cmp;
Alok Kataria851b1642009-10-13 14:51:05 -070066};
67
68struct pvscsi_adapter {
69 char *mmioBase;
70 unsigned int irq;
71 u8 rev;
72 bool use_msi;
73 bool use_msix;
74 bool use_msg;
Rishi Mehta2a815b52014-03-11 13:51:33 -070075 bool use_req_threshold;
Alok Kataria851b1642009-10-13 14:51:05 -070076
77 spinlock_t hw_lock;
78
79 struct workqueue_struct *workqueue;
80 struct work_struct work;
81
82 struct PVSCSIRingReqDesc *req_ring;
83 unsigned req_pages;
84 unsigned req_depth;
85 dma_addr_t reqRingPA;
86
87 struct PVSCSIRingCmpDesc *cmp_ring;
88 unsigned cmp_pages;
89 dma_addr_t cmpRingPA;
90
91 struct PVSCSIRingMsgDesc *msg_ring;
92 unsigned msg_pages;
93 dma_addr_t msgRingPA;
94
95 struct PVSCSIRingsState *rings_state;
96 dma_addr_t ringStatePA;
97
98 struct pci_dev *dev;
99 struct Scsi_Host *host;
100
101 struct list_head cmd_pool;
102 struct pvscsi_ctx *cmd_map;
103};
104
105
106/* Command line parameters */
107static int pvscsi_ring_pages = PVSCSI_DEFAULT_NUM_PAGES_PER_RING;
108static int pvscsi_msg_ring_pages = PVSCSI_DEFAULT_NUM_PAGES_MSG_RING;
109static int pvscsi_cmd_per_lun = PVSCSI_DEFAULT_QUEUE_DEPTH;
110static bool pvscsi_disable_msi;
111static bool pvscsi_disable_msix;
112static bool pvscsi_use_msg = true;
Rishi Mehta2a815b52014-03-11 13:51:33 -0700113static bool pvscsi_use_req_threshold = true;
Alok Kataria851b1642009-10-13 14:51:05 -0700114
115#define PVSCSI_RW (S_IRUSR | S_IWUSR)
116
117module_param_named(ring_pages, pvscsi_ring_pages, int, PVSCSI_RW);
118MODULE_PARM_DESC(ring_pages, "Number of pages per req/cmp ring - (default="
119 __stringify(PVSCSI_DEFAULT_NUM_PAGES_PER_RING) ")");
120
121module_param_named(msg_ring_pages, pvscsi_msg_ring_pages, int, PVSCSI_RW);
122MODULE_PARM_DESC(msg_ring_pages, "Number of pages for the msg ring - (default="
123 __stringify(PVSCSI_DEFAULT_NUM_PAGES_MSG_RING) ")");
124
125module_param_named(cmd_per_lun, pvscsi_cmd_per_lun, int, PVSCSI_RW);
126MODULE_PARM_DESC(cmd_per_lun, "Maximum commands per lun - (default="
127 __stringify(PVSCSI_MAX_REQ_QUEUE_DEPTH) ")");
128
129module_param_named(disable_msi, pvscsi_disable_msi, bool, PVSCSI_RW);
130MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default=0)");
131
132module_param_named(disable_msix, pvscsi_disable_msix, bool, PVSCSI_RW);
133MODULE_PARM_DESC(disable_msix, "Disable MSI-X use in driver - (default=0)");
134
135module_param_named(use_msg, pvscsi_use_msg, bool, PVSCSI_RW);
136MODULE_PARM_DESC(use_msg, "Use msg ring when available - (default=1)");
137
Rishi Mehta2a815b52014-03-11 13:51:33 -0700138module_param_named(use_req_threshold, pvscsi_use_req_threshold,
139 bool, PVSCSI_RW);
140MODULE_PARM_DESC(use_req_threshold, "Use driver-based request coalescing if configured - (default=1)");
141
Alok Kataria851b1642009-10-13 14:51:05 -0700142static const struct pci_device_id pvscsi_pci_tbl[] = {
143 { PCI_VDEVICE(VMWARE, PCI_DEVICE_ID_VMWARE_PVSCSI) },
144 { 0 }
145};
146
147MODULE_DEVICE_TABLE(pci, pvscsi_pci_tbl);
148
149static struct device *
150pvscsi_dev(const struct pvscsi_adapter *adapter)
151{
152 return &(adapter->dev->dev);
153}
154
155static struct pvscsi_ctx *
156pvscsi_find_context(const struct pvscsi_adapter *adapter, struct scsi_cmnd *cmd)
157{
158 struct pvscsi_ctx *ctx, *end;
159
160 end = &adapter->cmd_map[adapter->req_depth];
161 for (ctx = adapter->cmd_map; ctx < end; ctx++)
162 if (ctx->cmd == cmd)
163 return ctx;
164
165 return NULL;
166}
167
168static struct pvscsi_ctx *
169pvscsi_acquire_context(struct pvscsi_adapter *adapter, struct scsi_cmnd *cmd)
170{
171 struct pvscsi_ctx *ctx;
172
173 if (list_empty(&adapter->cmd_pool))
174 return NULL;
175
176 ctx = list_first_entry(&adapter->cmd_pool, struct pvscsi_ctx, list);
177 ctx->cmd = cmd;
178 list_del(&ctx->list);
179
180 return ctx;
181}
182
183static void pvscsi_release_context(struct pvscsi_adapter *adapter,
184 struct pvscsi_ctx *ctx)
185{
186 ctx->cmd = NULL;
Arvind Kumara2713cc2014-03-08 12:51:12 -0800187 ctx->abort_cmp = NULL;
Alok Kataria851b1642009-10-13 14:51:05 -0700188 list_add(&ctx->list, &adapter->cmd_pool);
189}
190
191/*
192 * Map a pvscsi_ctx struct to a context ID field value; we map to a simple
193 * non-zero integer. ctx always points to an entry in cmd_map array, hence
194 * the return value is always >=1.
195 */
196static u64 pvscsi_map_context(const struct pvscsi_adapter *adapter,
197 const struct pvscsi_ctx *ctx)
198{
199 return ctx - adapter->cmd_map + 1;
200}
201
202static struct pvscsi_ctx *
203pvscsi_get_context(const struct pvscsi_adapter *adapter, u64 context)
204{
205 return &adapter->cmd_map[context - 1];
206}
207
208static void pvscsi_reg_write(const struct pvscsi_adapter *adapter,
209 u32 offset, u32 val)
210{
211 writel(val, adapter->mmioBase + offset);
212}
213
214static u32 pvscsi_reg_read(const struct pvscsi_adapter *adapter, u32 offset)
215{
216 return readl(adapter->mmioBase + offset);
217}
218
219static u32 pvscsi_read_intr_status(const struct pvscsi_adapter *adapter)
220{
221 return pvscsi_reg_read(adapter, PVSCSI_REG_OFFSET_INTR_STATUS);
222}
223
224static void pvscsi_write_intr_status(const struct pvscsi_adapter *adapter,
225 u32 val)
226{
227 pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_INTR_STATUS, val);
228}
229
230static void pvscsi_unmask_intr(const struct pvscsi_adapter *adapter)
231{
232 u32 intr_bits;
233
234 intr_bits = PVSCSI_INTR_CMPL_MASK;
235 if (adapter->use_msg)
236 intr_bits |= PVSCSI_INTR_MSG_MASK;
237
238 pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_INTR_MASK, intr_bits);
239}
240
241static void pvscsi_mask_intr(const struct pvscsi_adapter *adapter)
242{
243 pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_INTR_MASK, 0);
244}
245
246static void pvscsi_write_cmd_desc(const struct pvscsi_adapter *adapter,
247 u32 cmd, const void *desc, size_t len)
248{
249 const u32 *ptr = desc;
250 size_t i;
251
252 len /= sizeof(*ptr);
253 pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_COMMAND, cmd);
254 for (i = 0; i < len; i++)
255 pvscsi_reg_write(adapter,
256 PVSCSI_REG_OFFSET_COMMAND_DATA, ptr[i]);
257}
258
259static void pvscsi_abort_cmd(const struct pvscsi_adapter *adapter,
260 const struct pvscsi_ctx *ctx)
261{
262 struct PVSCSICmdDescAbortCmd cmd = { 0 };
263
264 cmd.target = ctx->cmd->device->id;
265 cmd.context = pvscsi_map_context(adapter, ctx);
266
267 pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_ABORT_CMD, &cmd, sizeof(cmd));
268}
269
270static void pvscsi_kick_rw_io(const struct pvscsi_adapter *adapter)
271{
272 pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_KICK_RW_IO, 0);
273}
274
275static void pvscsi_process_request_ring(const struct pvscsi_adapter *adapter)
276{
277 pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_KICK_NON_RW_IO, 0);
278}
279
280static int scsi_is_rw(unsigned char op)
281{
282 return op == READ_6 || op == WRITE_6 ||
283 op == READ_10 || op == WRITE_10 ||
284 op == READ_12 || op == WRITE_12 ||
285 op == READ_16 || op == WRITE_16;
286}
287
288static void pvscsi_kick_io(const struct pvscsi_adapter *adapter,
289 unsigned char op)
290{
Rishi Mehta2a815b52014-03-11 13:51:33 -0700291 if (scsi_is_rw(op)) {
292 struct PVSCSIRingsState *s = adapter->rings_state;
293
294 if (!adapter->use_req_threshold ||
295 s->reqProdIdx - s->reqConsIdx >= s->reqCallThreshold)
296 pvscsi_kick_rw_io(adapter);
297 } else {
Alok Kataria851b1642009-10-13 14:51:05 -0700298 pvscsi_process_request_ring(adapter);
Rishi Mehta2a815b52014-03-11 13:51:33 -0700299 }
Alok Kataria851b1642009-10-13 14:51:05 -0700300}
301
302static void ll_adapter_reset(const struct pvscsi_adapter *adapter)
303{
304 dev_dbg(pvscsi_dev(adapter), "Adapter Reset on %p\n", adapter);
305
306 pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_ADAPTER_RESET, NULL, 0);
307}
308
309static void ll_bus_reset(const struct pvscsi_adapter *adapter)
310{
Masanari Iida59e13d42012-04-25 00:24:16 +0900311 dev_dbg(pvscsi_dev(adapter), "Resetting bus on %p\n", adapter);
Alok Kataria851b1642009-10-13 14:51:05 -0700312
313 pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_RESET_BUS, NULL, 0);
314}
315
316static void ll_device_reset(const struct pvscsi_adapter *adapter, u32 target)
317{
318 struct PVSCSICmdDescResetDevice cmd = { 0 };
319
Masanari Iida59e13d42012-04-25 00:24:16 +0900320 dev_dbg(pvscsi_dev(adapter), "Resetting device: target=%u\n", target);
Alok Kataria851b1642009-10-13 14:51:05 -0700321
322 cmd.target = target;
323
324 pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_RESET_DEVICE,
325 &cmd, sizeof(cmd));
326}
327
328static void pvscsi_create_sg(struct pvscsi_ctx *ctx,
329 struct scatterlist *sg, unsigned count)
330{
331 unsigned i;
332 struct PVSCSISGElement *sge;
333
334 BUG_ON(count > PVSCSI_MAX_NUM_SG_ENTRIES_PER_SEGMENT);
335
336 sge = &ctx->sgl->sge[0];
337 for (i = 0; i < count; i++, sg++) {
338 sge[i].addr = sg_dma_address(sg);
339 sge[i].length = sg_dma_len(sg);
340 sge[i].flags = 0;
341 }
342}
343
344/*
345 * Map all data buffers for a command into PCI space and
346 * setup the scatter/gather list if needed.
347 */
348static void pvscsi_map_buffers(struct pvscsi_adapter *adapter,
349 struct pvscsi_ctx *ctx, struct scsi_cmnd *cmd,
350 struct PVSCSIRingReqDesc *e)
351{
352 unsigned count;
353 unsigned bufflen = scsi_bufflen(cmd);
354 struct scatterlist *sg;
355
356 e->dataLen = bufflen;
357 e->dataAddr = 0;
358 if (bufflen == 0)
359 return;
360
361 sg = scsi_sglist(cmd);
362 count = scsi_sg_count(cmd);
363 if (count != 0) {
364 int segs = scsi_dma_map(cmd);
365 if (segs > 1) {
366 pvscsi_create_sg(ctx, sg, segs);
367
368 e->flags |= PVSCSI_FLAG_CMD_WITH_SG_LIST;
369 ctx->sglPA = pci_map_single(adapter->dev, ctx->sgl,
370 SGL_SIZE, PCI_DMA_TODEVICE);
371 e->dataAddr = ctx->sglPA;
372 } else
373 e->dataAddr = sg_dma_address(sg);
374 } else {
375 /*
376 * In case there is no S/G list, scsi_sglist points
377 * directly to the buffer.
378 */
379 ctx->dataPA = pci_map_single(adapter->dev, sg, bufflen,
380 cmd->sc_data_direction);
381 e->dataAddr = ctx->dataPA;
382 }
383}
384
385static void pvscsi_unmap_buffers(const struct pvscsi_adapter *adapter,
386 struct pvscsi_ctx *ctx)
387{
388 struct scsi_cmnd *cmd;
389 unsigned bufflen;
390
391 cmd = ctx->cmd;
392 bufflen = scsi_bufflen(cmd);
393
394 if (bufflen != 0) {
395 unsigned count = scsi_sg_count(cmd);
396
397 if (count != 0) {
398 scsi_dma_unmap(cmd);
399 if (ctx->sglPA) {
400 pci_unmap_single(adapter->dev, ctx->sglPA,
401 SGL_SIZE, PCI_DMA_TODEVICE);
402 ctx->sglPA = 0;
403 }
404 } else
405 pci_unmap_single(adapter->dev, ctx->dataPA, bufflen,
406 cmd->sc_data_direction);
407 }
408 if (cmd->sense_buffer)
409 pci_unmap_single(adapter->dev, ctx->sensePA,
410 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
411}
412
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800413static int pvscsi_allocate_rings(struct pvscsi_adapter *adapter)
Alok Kataria851b1642009-10-13 14:51:05 -0700414{
415 adapter->rings_state = pci_alloc_consistent(adapter->dev, PAGE_SIZE,
416 &adapter->ringStatePA);
417 if (!adapter->rings_state)
418 return -ENOMEM;
419
420 adapter->req_pages = min(PVSCSI_MAX_NUM_PAGES_REQ_RING,
421 pvscsi_ring_pages);
422 adapter->req_depth = adapter->req_pages
423 * PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE;
424 adapter->req_ring = pci_alloc_consistent(adapter->dev,
425 adapter->req_pages * PAGE_SIZE,
426 &adapter->reqRingPA);
427 if (!adapter->req_ring)
428 return -ENOMEM;
429
430 adapter->cmp_pages = min(PVSCSI_MAX_NUM_PAGES_CMP_RING,
431 pvscsi_ring_pages);
432 adapter->cmp_ring = pci_alloc_consistent(adapter->dev,
433 adapter->cmp_pages * PAGE_SIZE,
434 &adapter->cmpRingPA);
435 if (!adapter->cmp_ring)
436 return -ENOMEM;
437
438 BUG_ON(!IS_ALIGNED(adapter->ringStatePA, PAGE_SIZE));
439 BUG_ON(!IS_ALIGNED(adapter->reqRingPA, PAGE_SIZE));
440 BUG_ON(!IS_ALIGNED(adapter->cmpRingPA, PAGE_SIZE));
441
442 if (!adapter->use_msg)
443 return 0;
444
445 adapter->msg_pages = min(PVSCSI_MAX_NUM_PAGES_MSG_RING,
446 pvscsi_msg_ring_pages);
447 adapter->msg_ring = pci_alloc_consistent(adapter->dev,
448 adapter->msg_pages * PAGE_SIZE,
449 &adapter->msgRingPA);
450 if (!adapter->msg_ring)
451 return -ENOMEM;
452 BUG_ON(!IS_ALIGNED(adapter->msgRingPA, PAGE_SIZE));
453
454 return 0;
455}
456
457static void pvscsi_setup_all_rings(const struct pvscsi_adapter *adapter)
458{
459 struct PVSCSICmdDescSetupRings cmd = { 0 };
460 dma_addr_t base;
461 unsigned i;
462
463 cmd.ringsStatePPN = adapter->ringStatePA >> PAGE_SHIFT;
464 cmd.reqRingNumPages = adapter->req_pages;
465 cmd.cmpRingNumPages = adapter->cmp_pages;
466
467 base = adapter->reqRingPA;
468 for (i = 0; i < adapter->req_pages; i++) {
469 cmd.reqRingPPNs[i] = base >> PAGE_SHIFT;
470 base += PAGE_SIZE;
471 }
472
473 base = adapter->cmpRingPA;
474 for (i = 0; i < adapter->cmp_pages; i++) {
475 cmd.cmpRingPPNs[i] = base >> PAGE_SHIFT;
476 base += PAGE_SIZE;
477 }
478
479 memset(adapter->rings_state, 0, PAGE_SIZE);
480 memset(adapter->req_ring, 0, adapter->req_pages * PAGE_SIZE);
481 memset(adapter->cmp_ring, 0, adapter->cmp_pages * PAGE_SIZE);
482
483 pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_SETUP_RINGS,
484 &cmd, sizeof(cmd));
485
486 if (adapter->use_msg) {
487 struct PVSCSICmdDescSetupMsgRing cmd_msg = { 0 };
488
489 cmd_msg.numPages = adapter->msg_pages;
490
491 base = adapter->msgRingPA;
492 for (i = 0; i < adapter->msg_pages; i++) {
493 cmd_msg.ringPPNs[i] = base >> PAGE_SHIFT;
494 base += PAGE_SIZE;
495 }
496 memset(adapter->msg_ring, 0, adapter->msg_pages * PAGE_SIZE);
497
498 pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_SETUP_MSG_RING,
499 &cmd_msg, sizeof(cmd_msg));
500 }
501}
502
503/*
504 * Pull a completion descriptor off and pass the completion back
505 * to the SCSI mid layer.
506 */
507static void pvscsi_complete_request(struct pvscsi_adapter *adapter,
508 const struct PVSCSIRingCmpDesc *e)
509{
510 struct pvscsi_ctx *ctx;
511 struct scsi_cmnd *cmd;
Arvind Kumara2713cc2014-03-08 12:51:12 -0800512 struct completion *abort_cmp;
Alok Kataria851b1642009-10-13 14:51:05 -0700513 u32 btstat = e->hostStatus;
514 u32 sdstat = e->scsiStatus;
515
516 ctx = pvscsi_get_context(adapter, e->context);
517 cmd = ctx->cmd;
Arvind Kumara2713cc2014-03-08 12:51:12 -0800518 abort_cmp = ctx->abort_cmp;
Alok Kataria851b1642009-10-13 14:51:05 -0700519 pvscsi_unmap_buffers(adapter, ctx);
520 pvscsi_release_context(adapter, ctx);
Arvind Kumara2713cc2014-03-08 12:51:12 -0800521 if (abort_cmp) {
522 /*
523 * The command was requested to be aborted. Just signal that
524 * the request completed and swallow the actual cmd completion
525 * here. The abort handler will post a completion for this
526 * command indicating that it got successfully aborted.
527 */
528 complete(abort_cmp);
529 return;
530 }
Alok Kataria851b1642009-10-13 14:51:05 -0700531
Arvind Kumara2713cc2014-03-08 12:51:12 -0800532 cmd->result = 0;
Alok Kataria851b1642009-10-13 14:51:05 -0700533 if (sdstat != SAM_STAT_GOOD &&
534 (btstat == BTSTAT_SUCCESS ||
535 btstat == BTSTAT_LINKED_COMMAND_COMPLETED ||
536 btstat == BTSTAT_LINKED_COMMAND_COMPLETED_WITH_FLAG)) {
537 cmd->result = (DID_OK << 16) | sdstat;
538 if (sdstat == SAM_STAT_CHECK_CONDITION && cmd->sense_buffer)
539 cmd->result |= (DRIVER_SENSE << 24);
540 } else
541 switch (btstat) {
542 case BTSTAT_SUCCESS:
543 case BTSTAT_LINKED_COMMAND_COMPLETED:
544 case BTSTAT_LINKED_COMMAND_COMPLETED_WITH_FLAG:
545 /* If everything went fine, let's move on.. */
546 cmd->result = (DID_OK << 16);
547 break;
548
549 case BTSTAT_DATARUN:
550 case BTSTAT_DATA_UNDERRUN:
551 /* Report residual data in underruns */
552 scsi_set_resid(cmd, scsi_bufflen(cmd) - e->dataLen);
553 cmd->result = (DID_ERROR << 16);
554 break;
555
556 case BTSTAT_SELTIMEO:
557 /* Our emulation returns this for non-connected devs */
558 cmd->result = (DID_BAD_TARGET << 16);
559 break;
560
561 case BTSTAT_LUNMISMATCH:
562 case BTSTAT_TAGREJECT:
563 case BTSTAT_BADMSG:
564 cmd->result = (DRIVER_INVALID << 24);
565 /* fall through */
566
567 case BTSTAT_HAHARDWARE:
568 case BTSTAT_INVPHASE:
569 case BTSTAT_HATIMEOUT:
570 case BTSTAT_NORESPONSE:
571 case BTSTAT_DISCONNECT:
572 case BTSTAT_HASOFTWARE:
573 case BTSTAT_BUSFREE:
574 case BTSTAT_SENSFAILED:
575 cmd->result |= (DID_ERROR << 16);
576 break;
577
578 case BTSTAT_SENTRST:
579 case BTSTAT_RECVRST:
580 case BTSTAT_BUSRESET:
581 cmd->result = (DID_RESET << 16);
582 break;
583
584 case BTSTAT_ABORTQUEUE:
585 cmd->result = (DID_ABORT << 16);
586 break;
587
588 case BTSTAT_SCSIPARITY:
589 cmd->result = (DID_PARITY << 16);
590 break;
591
592 default:
593 cmd->result = (DID_ERROR << 16);
594 scmd_printk(KERN_DEBUG, cmd,
595 "Unknown completion status: 0x%x\n",
596 btstat);
597 }
598
599 dev_dbg(&cmd->device->sdev_gendev,
600 "cmd=%p %x ctx=%p result=0x%x status=0x%x,%x\n",
601 cmd, cmd->cmnd[0], ctx, cmd->result, btstat, sdstat);
602
603 cmd->scsi_done(cmd);
604}
605
606/*
607 * barrier usage : Since the PVSCSI device is emulated, there could be cases
608 * where we may want to serialize some accesses between the driver and the
609 * emulation layer. We use compiler barriers instead of the more expensive
610 * memory barriers because PVSCSI is only supported on X86 which has strong
611 * memory access ordering.
612 */
613static void pvscsi_process_completion_ring(struct pvscsi_adapter *adapter)
614{
615 struct PVSCSIRingsState *s = adapter->rings_state;
616 struct PVSCSIRingCmpDesc *ring = adapter->cmp_ring;
617 u32 cmp_entries = s->cmpNumEntriesLog2;
618
619 while (s->cmpConsIdx != s->cmpProdIdx) {
620 struct PVSCSIRingCmpDesc *e = ring + (s->cmpConsIdx &
621 MASK(cmp_entries));
622 /*
623 * This barrier() ensures that *e is not dereferenced while
624 * the device emulation still writes data into the slot.
625 * Since the device emulation advances s->cmpProdIdx only after
626 * updating the slot we want to check it first.
627 */
628 barrier();
629 pvscsi_complete_request(adapter, e);
630 /*
631 * This barrier() ensures that compiler doesn't reorder write
632 * to s->cmpConsIdx before the read of (*e) inside
633 * pvscsi_complete_request. Otherwise, device emulation may
634 * overwrite *e before we had a chance to read it.
635 */
636 barrier();
637 s->cmpConsIdx++;
638 }
639}
640
641/*
642 * Translate a Linux SCSI request into a request ring entry.
643 */
644static int pvscsi_queue_ring(struct pvscsi_adapter *adapter,
645 struct pvscsi_ctx *ctx, struct scsi_cmnd *cmd)
646{
647 struct PVSCSIRingsState *s;
648 struct PVSCSIRingReqDesc *e;
649 struct scsi_device *sdev;
650 u32 req_entries;
651
652 s = adapter->rings_state;
653 sdev = cmd->device;
654 req_entries = s->reqNumEntriesLog2;
655
656 /*
657 * If this condition holds, we might have room on the request ring, but
658 * we might not have room on the completion ring for the response.
659 * However, we have already ruled out this possibility - we would not
660 * have successfully allocated a context if it were true, since we only
661 * have one context per request entry. Check for it anyway, since it
662 * would be a serious bug.
663 */
664 if (s->reqProdIdx - s->cmpConsIdx >= 1 << req_entries) {
665 scmd_printk(KERN_ERR, cmd, "vmw_pvscsi: "
666 "ring full: reqProdIdx=%d cmpConsIdx=%d\n",
667 s->reqProdIdx, s->cmpConsIdx);
668 return -1;
669 }
670
671 e = adapter->req_ring + (s->reqProdIdx & MASK(req_entries));
672
673 e->bus = sdev->channel;
674 e->target = sdev->id;
675 memset(e->lun, 0, sizeof(e->lun));
676 e->lun[1] = sdev->lun;
677
678 if (cmd->sense_buffer) {
679 ctx->sensePA = pci_map_single(adapter->dev, cmd->sense_buffer,
680 SCSI_SENSE_BUFFERSIZE,
681 PCI_DMA_FROMDEVICE);
682 e->senseAddr = ctx->sensePA;
683 e->senseLen = SCSI_SENSE_BUFFERSIZE;
684 } else {
685 e->senseLen = 0;
686 e->senseAddr = 0;
687 }
688 e->cdbLen = cmd->cmd_len;
689 e->vcpuHint = smp_processor_id();
690 memcpy(e->cdb, cmd->cmnd, e->cdbLen);
691
692 e->tag = SIMPLE_QUEUE_TAG;
693 if (sdev->tagged_supported &&
694 (cmd->tag == HEAD_OF_QUEUE_TAG ||
695 cmd->tag == ORDERED_QUEUE_TAG))
696 e->tag = cmd->tag;
697
698 if (cmd->sc_data_direction == DMA_FROM_DEVICE)
699 e->flags = PVSCSI_FLAG_CMD_DIR_TOHOST;
700 else if (cmd->sc_data_direction == DMA_TO_DEVICE)
701 e->flags = PVSCSI_FLAG_CMD_DIR_TODEVICE;
702 else if (cmd->sc_data_direction == DMA_NONE)
703 e->flags = PVSCSI_FLAG_CMD_DIR_NONE;
704 else
705 e->flags = 0;
706
707 pvscsi_map_buffers(adapter, ctx, cmd, e);
708
709 e->context = pvscsi_map_context(adapter, ctx);
710
711 barrier();
712
713 s->reqProdIdx++;
714
715 return 0;
716}
717
Jeff Garzikf2812332010-11-16 02:10:29 -0500718static int pvscsi_queue_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
Alok Kataria851b1642009-10-13 14:51:05 -0700719{
720 struct Scsi_Host *host = cmd->device->host;
721 struct pvscsi_adapter *adapter = shost_priv(host);
722 struct pvscsi_ctx *ctx;
723 unsigned long flags;
724
725 spin_lock_irqsave(&adapter->hw_lock, flags);
726
727 ctx = pvscsi_acquire_context(adapter, cmd);
728 if (!ctx || pvscsi_queue_ring(adapter, ctx, cmd) != 0) {
729 if (ctx)
730 pvscsi_release_context(adapter, ctx);
731 spin_unlock_irqrestore(&adapter->hw_lock, flags);
732 return SCSI_MLQUEUE_HOST_BUSY;
733 }
734
735 cmd->scsi_done = done;
736
737 dev_dbg(&cmd->device->sdev_gendev,
738 "queued cmd %p, ctx %p, op=%x\n", cmd, ctx, cmd->cmnd[0]);
739
740 spin_unlock_irqrestore(&adapter->hw_lock, flags);
741
742 pvscsi_kick_io(adapter, cmd->cmnd[0]);
743
744 return 0;
745}
746
Jeff Garzikf2812332010-11-16 02:10:29 -0500747static DEF_SCSI_QCMD(pvscsi_queue)
748
Alok Kataria851b1642009-10-13 14:51:05 -0700749static int pvscsi_abort(struct scsi_cmnd *cmd)
750{
751 struct pvscsi_adapter *adapter = shost_priv(cmd->device->host);
752 struct pvscsi_ctx *ctx;
753 unsigned long flags;
Arvind Kumara2713cc2014-03-08 12:51:12 -0800754 int result = SUCCESS;
755 DECLARE_COMPLETION_ONSTACK(abort_cmp);
Alok Kataria851b1642009-10-13 14:51:05 -0700756
757 scmd_printk(KERN_DEBUG, cmd, "task abort on host %u, %p\n",
758 adapter->host->host_no, cmd);
759
760 spin_lock_irqsave(&adapter->hw_lock, flags);
761
762 /*
763 * Poll the completion ring first - we might be trying to abort
764 * a command that is waiting to be dispatched in the completion ring.
765 */
766 pvscsi_process_completion_ring(adapter);
767
768 /*
769 * If there is no context for the command, it either already succeeded
770 * or else was never properly issued. Not our problem.
771 */
772 ctx = pvscsi_find_context(adapter, cmd);
773 if (!ctx) {
774 scmd_printk(KERN_DEBUG, cmd, "Failed to abort cmd %p\n", cmd);
775 goto out;
776 }
777
Arvind Kumara2713cc2014-03-08 12:51:12 -0800778 /*
779 * Mark that the command has been requested to be aborted and issue
780 * the abort.
781 */
782 ctx->abort_cmp = &abort_cmp;
Alok Kataria851b1642009-10-13 14:51:05 -0700783
Arvind Kumara2713cc2014-03-08 12:51:12 -0800784 pvscsi_abort_cmd(adapter, ctx);
785 spin_unlock_irqrestore(&adapter->hw_lock, flags);
786 /* Wait for 2 secs for the completion. */
787 wait_for_completion_timeout(&abort_cmp, msecs_to_jiffies(2000));
788 spin_lock_irqsave(&adapter->hw_lock, flags);
789
790 if (!completion_done(&abort_cmp)) {
791 /*
792 * Failed to abort the command, unmark the fact that it
793 * was requested to be aborted.
794 */
795 ctx->abort_cmp = NULL;
796 result = FAILED;
797 scmd_printk(KERN_DEBUG, cmd,
798 "Failed to get completion for aborted cmd %p\n",
799 cmd);
800 goto out;
801 }
802
803 /*
804 * Successfully aborted the command.
805 */
806 cmd->result = (DID_ABORT << 16);
807 cmd->scsi_done(cmd);
Alok Kataria851b1642009-10-13 14:51:05 -0700808
809out:
810 spin_unlock_irqrestore(&adapter->hw_lock, flags);
Arvind Kumara2713cc2014-03-08 12:51:12 -0800811 return result;
Alok Kataria851b1642009-10-13 14:51:05 -0700812}
813
814/*
815 * Abort all outstanding requests. This is only safe to use if the completion
816 * ring will never be walked again or the device has been reset, because it
817 * destroys the 1-1 mapping between context field passed to emulation and our
818 * request structure.
819 */
820static void pvscsi_reset_all(struct pvscsi_adapter *adapter)
821{
822 unsigned i;
823
824 for (i = 0; i < adapter->req_depth; i++) {
825 struct pvscsi_ctx *ctx = &adapter->cmd_map[i];
826 struct scsi_cmnd *cmd = ctx->cmd;
827 if (cmd) {
828 scmd_printk(KERN_ERR, cmd,
829 "Forced reset on cmd %p\n", cmd);
830 pvscsi_unmap_buffers(adapter, ctx);
831 pvscsi_release_context(adapter, ctx);
832 cmd->result = (DID_RESET << 16);
833 cmd->scsi_done(cmd);
834 }
835 }
836}
837
838static int pvscsi_host_reset(struct scsi_cmnd *cmd)
839{
840 struct Scsi_Host *host = cmd->device->host;
841 struct pvscsi_adapter *adapter = shost_priv(host);
842 unsigned long flags;
843 bool use_msg;
844
845 scmd_printk(KERN_INFO, cmd, "SCSI Host reset\n");
846
847 spin_lock_irqsave(&adapter->hw_lock, flags);
848
849 use_msg = adapter->use_msg;
850
851 if (use_msg) {
852 adapter->use_msg = 0;
853 spin_unlock_irqrestore(&adapter->hw_lock, flags);
854
855 /*
856 * Now that we know that the ISR won't add more work on the
857 * workqueue we can safely flush any outstanding work.
858 */
859 flush_workqueue(adapter->workqueue);
860 spin_lock_irqsave(&adapter->hw_lock, flags);
861 }
862
863 /*
864 * We're going to tear down the entire ring structure and set it back
865 * up, so stalling new requests until all completions are flushed and
866 * the rings are back in place.
867 */
868
869 pvscsi_process_request_ring(adapter);
870
871 ll_adapter_reset(adapter);
872
873 /*
874 * Now process any completions. Note we do this AFTER adapter reset,
875 * which is strange, but stops races where completions get posted
876 * between processing the ring and issuing the reset. The backend will
877 * not touch the ring memory after reset, so the immediately pre-reset
878 * completion ring state is still valid.
879 */
880 pvscsi_process_completion_ring(adapter);
881
882 pvscsi_reset_all(adapter);
883 adapter->use_msg = use_msg;
884 pvscsi_setup_all_rings(adapter);
885 pvscsi_unmask_intr(adapter);
886
887 spin_unlock_irqrestore(&adapter->hw_lock, flags);
888
889 return SUCCESS;
890}
891
892static int pvscsi_bus_reset(struct scsi_cmnd *cmd)
893{
894 struct Scsi_Host *host = cmd->device->host;
895 struct pvscsi_adapter *adapter = shost_priv(host);
896 unsigned long flags;
897
898 scmd_printk(KERN_INFO, cmd, "SCSI Bus reset\n");
899
900 /*
901 * We don't want to queue new requests for this bus after
902 * flushing all pending requests to emulation, since new
903 * requests could then sneak in during this bus reset phase,
904 * so take the lock now.
905 */
906 spin_lock_irqsave(&adapter->hw_lock, flags);
907
908 pvscsi_process_request_ring(adapter);
909 ll_bus_reset(adapter);
910 pvscsi_process_completion_ring(adapter);
911
912 spin_unlock_irqrestore(&adapter->hw_lock, flags);
913
914 return SUCCESS;
915}
916
917static int pvscsi_device_reset(struct scsi_cmnd *cmd)
918{
919 struct Scsi_Host *host = cmd->device->host;
920 struct pvscsi_adapter *adapter = shost_priv(host);
921 unsigned long flags;
922
923 scmd_printk(KERN_INFO, cmd, "SCSI device reset on scsi%u:%u\n",
924 host->host_no, cmd->device->id);
925
926 /*
927 * We don't want to queue new requests for this device after flushing
928 * all pending requests to emulation, since new requests could then
929 * sneak in during this device reset phase, so take the lock now.
930 */
931 spin_lock_irqsave(&adapter->hw_lock, flags);
932
933 pvscsi_process_request_ring(adapter);
934 ll_device_reset(adapter, cmd->device->id);
935 pvscsi_process_completion_ring(adapter);
936
937 spin_unlock_irqrestore(&adapter->hw_lock, flags);
938
939 return SUCCESS;
940}
941
942static struct scsi_host_template pvscsi_template;
943
944static const char *pvscsi_info(struct Scsi_Host *host)
945{
946 struct pvscsi_adapter *adapter = shost_priv(host);
947 static char buf[256];
948
949 sprintf(buf, "VMware PVSCSI storage adapter rev %d, req/cmp/msg rings: "
950 "%u/%u/%u pages, cmd_per_lun=%u", adapter->rev,
951 adapter->req_pages, adapter->cmp_pages, adapter->msg_pages,
952 pvscsi_template.cmd_per_lun);
953
954 return buf;
955}
956
957static struct scsi_host_template pvscsi_template = {
958 .module = THIS_MODULE,
959 .name = "VMware PVSCSI Host Adapter",
960 .proc_name = "vmw_pvscsi",
961 .info = pvscsi_info,
962 .queuecommand = pvscsi_queue,
963 .this_id = -1,
964 .sg_tablesize = PVSCSI_MAX_NUM_SG_ENTRIES_PER_SEGMENT,
965 .dma_boundary = UINT_MAX,
966 .max_sectors = 0xffff,
967 .use_clustering = ENABLE_CLUSTERING,
968 .eh_abort_handler = pvscsi_abort,
969 .eh_device_reset_handler = pvscsi_device_reset,
970 .eh_bus_reset_handler = pvscsi_bus_reset,
971 .eh_host_reset_handler = pvscsi_host_reset,
972};
973
974static void pvscsi_process_msg(const struct pvscsi_adapter *adapter,
975 const struct PVSCSIRingMsgDesc *e)
976{
977 struct PVSCSIRingsState *s = adapter->rings_state;
978 struct Scsi_Host *host = adapter->host;
979 struct scsi_device *sdev;
980
981 printk(KERN_INFO "vmw_pvscsi: msg type: 0x%x - MSG RING: %u/%u (%u) \n",
982 e->type, s->msgProdIdx, s->msgConsIdx, s->msgNumEntriesLog2);
983
984 BUILD_BUG_ON(PVSCSI_MSG_LAST != 2);
985
986 if (e->type == PVSCSI_MSG_DEV_ADDED) {
987 struct PVSCSIMsgDescDevStatusChanged *desc;
988 desc = (struct PVSCSIMsgDescDevStatusChanged *)e;
989
990 printk(KERN_INFO
991 "vmw_pvscsi: msg: device added at scsi%u:%u:%u\n",
992 desc->bus, desc->target, desc->lun[1]);
993
994 if (!scsi_host_get(host))
995 return;
996
997 sdev = scsi_device_lookup(host, desc->bus, desc->target,
998 desc->lun[1]);
999 if (sdev) {
1000 printk(KERN_INFO "vmw_pvscsi: device already exists\n");
1001 scsi_device_put(sdev);
1002 } else
1003 scsi_add_device(adapter->host, desc->bus,
1004 desc->target, desc->lun[1]);
1005
1006 scsi_host_put(host);
1007 } else if (e->type == PVSCSI_MSG_DEV_REMOVED) {
1008 struct PVSCSIMsgDescDevStatusChanged *desc;
1009 desc = (struct PVSCSIMsgDescDevStatusChanged *)e;
1010
1011 printk(KERN_INFO
1012 "vmw_pvscsi: msg: device removed at scsi%u:%u:%u\n",
1013 desc->bus, desc->target, desc->lun[1]);
1014
1015 if (!scsi_host_get(host))
1016 return;
1017
1018 sdev = scsi_device_lookup(host, desc->bus, desc->target,
1019 desc->lun[1]);
1020 if (sdev) {
1021 scsi_remove_device(sdev);
1022 scsi_device_put(sdev);
1023 } else
1024 printk(KERN_INFO
1025 "vmw_pvscsi: failed to lookup scsi%u:%u:%u\n",
1026 desc->bus, desc->target, desc->lun[1]);
1027
1028 scsi_host_put(host);
1029 }
1030}
1031
1032static int pvscsi_msg_pending(const struct pvscsi_adapter *adapter)
1033{
1034 struct PVSCSIRingsState *s = adapter->rings_state;
1035
1036 return s->msgProdIdx != s->msgConsIdx;
1037}
1038
1039static void pvscsi_process_msg_ring(const struct pvscsi_adapter *adapter)
1040{
1041 struct PVSCSIRingsState *s = adapter->rings_state;
1042 struct PVSCSIRingMsgDesc *ring = adapter->msg_ring;
1043 u32 msg_entries = s->msgNumEntriesLog2;
1044
1045 while (pvscsi_msg_pending(adapter)) {
1046 struct PVSCSIRingMsgDesc *e = ring + (s->msgConsIdx &
1047 MASK(msg_entries));
1048
1049 barrier();
1050 pvscsi_process_msg(adapter, e);
1051 barrier();
1052 s->msgConsIdx++;
1053 }
1054}
1055
1056static void pvscsi_msg_workqueue_handler(struct work_struct *data)
1057{
1058 struct pvscsi_adapter *adapter;
1059
1060 adapter = container_of(data, struct pvscsi_adapter, work);
1061
1062 pvscsi_process_msg_ring(adapter);
1063}
1064
1065static int pvscsi_setup_msg_workqueue(struct pvscsi_adapter *adapter)
1066{
1067 char name[32];
1068
1069 if (!pvscsi_use_msg)
1070 return 0;
1071
1072 pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_COMMAND,
1073 PVSCSI_CMD_SETUP_MSG_RING);
1074
1075 if (pvscsi_reg_read(adapter, PVSCSI_REG_OFFSET_COMMAND_STATUS) == -1)
1076 return 0;
1077
1078 snprintf(name, sizeof(name),
1079 "vmw_pvscsi_wq_%u", adapter->host->host_no);
1080
1081 adapter->workqueue = create_singlethread_workqueue(name);
1082 if (!adapter->workqueue) {
1083 printk(KERN_ERR "vmw_pvscsi: failed to create work queue\n");
1084 return 0;
1085 }
1086 INIT_WORK(&adapter->work, pvscsi_msg_workqueue_handler);
1087
1088 return 1;
1089}
1090
Rishi Mehta2a815b52014-03-11 13:51:33 -07001091static bool pvscsi_setup_req_threshold(struct pvscsi_adapter *adapter,
1092 bool enable)
1093{
1094 u32 val;
1095
1096 if (!pvscsi_use_req_threshold)
1097 return false;
1098
1099 pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_COMMAND,
1100 PVSCSI_CMD_SETUP_REQCALLTHRESHOLD);
1101 val = pvscsi_reg_read(adapter, PVSCSI_REG_OFFSET_COMMAND_STATUS);
1102 if (val == -1) {
1103 printk(KERN_INFO "vmw_pvscsi: device does not support req_threshold\n");
1104 return false;
1105 } else {
1106 struct PVSCSICmdDescSetupReqCall cmd_msg = { 0 };
1107 cmd_msg.enable = enable;
1108 printk(KERN_INFO
1109 "vmw_pvscsi: %sabling reqCallThreshold\n",
1110 enable ? "en" : "dis");
1111 pvscsi_write_cmd_desc(adapter,
1112 PVSCSI_CMD_SETUP_REQCALLTHRESHOLD,
1113 &cmd_msg, sizeof(cmd_msg));
1114 return pvscsi_reg_read(adapter,
1115 PVSCSI_REG_OFFSET_COMMAND_STATUS) != 0;
1116 }
1117}
1118
Alok Kataria851b1642009-10-13 14:51:05 -07001119static irqreturn_t pvscsi_isr(int irq, void *devp)
1120{
1121 struct pvscsi_adapter *adapter = devp;
1122 int handled;
1123
1124 if (adapter->use_msi || adapter->use_msix)
1125 handled = true;
1126 else {
1127 u32 val = pvscsi_read_intr_status(adapter);
1128 handled = (val & PVSCSI_INTR_ALL_SUPPORTED) != 0;
1129 if (handled)
1130 pvscsi_write_intr_status(devp, val);
1131 }
1132
1133 if (handled) {
1134 unsigned long flags;
1135
1136 spin_lock_irqsave(&adapter->hw_lock, flags);
1137
1138 pvscsi_process_completion_ring(adapter);
1139 if (adapter->use_msg && pvscsi_msg_pending(adapter))
1140 queue_work(adapter->workqueue, &adapter->work);
1141
1142 spin_unlock_irqrestore(&adapter->hw_lock, flags);
1143 }
1144
1145 return IRQ_RETVAL(handled);
1146}
1147
1148static void pvscsi_free_sgls(const struct pvscsi_adapter *adapter)
1149{
1150 struct pvscsi_ctx *ctx = adapter->cmd_map;
1151 unsigned i;
1152
1153 for (i = 0; i < adapter->req_depth; ++i, ++ctx)
1154 free_pages((unsigned long)ctx->sgl, get_order(SGL_SIZE));
1155}
1156
Dmitry Torokhovd0e2ddf2010-01-19 10:24:40 -08001157static int pvscsi_setup_msix(const struct pvscsi_adapter *adapter,
1158 unsigned int *irq)
Alok Kataria851b1642009-10-13 14:51:05 -07001159{
1160 struct msix_entry entry = { 0, PVSCSI_VECTOR_COMPLETION };
1161 int ret;
1162
1163 ret = pci_enable_msix(adapter->dev, &entry, 1);
1164 if (ret)
1165 return ret;
1166
1167 *irq = entry.vector;
1168
1169 return 0;
1170}
1171
1172static void pvscsi_shutdown_intr(struct pvscsi_adapter *adapter)
1173{
1174 if (adapter->irq) {
1175 free_irq(adapter->irq, adapter);
1176 adapter->irq = 0;
1177 }
1178 if (adapter->use_msi) {
1179 pci_disable_msi(adapter->dev);
1180 adapter->use_msi = 0;
1181 } else if (adapter->use_msix) {
1182 pci_disable_msix(adapter->dev);
1183 adapter->use_msix = 0;
1184 }
1185}
1186
1187static void pvscsi_release_resources(struct pvscsi_adapter *adapter)
1188{
1189 pvscsi_shutdown_intr(adapter);
1190
1191 if (adapter->workqueue)
1192 destroy_workqueue(adapter->workqueue);
1193
1194 if (adapter->mmioBase)
1195 pci_iounmap(adapter->dev, adapter->mmioBase);
1196
1197 pci_release_regions(adapter->dev);
1198
1199 if (adapter->cmd_map) {
1200 pvscsi_free_sgls(adapter);
1201 kfree(adapter->cmd_map);
1202 }
1203
1204 if (adapter->rings_state)
1205 pci_free_consistent(adapter->dev, PAGE_SIZE,
1206 adapter->rings_state, adapter->ringStatePA);
1207
1208 if (adapter->req_ring)
1209 pci_free_consistent(adapter->dev,
1210 adapter->req_pages * PAGE_SIZE,
1211 adapter->req_ring, adapter->reqRingPA);
1212
1213 if (adapter->cmp_ring)
1214 pci_free_consistent(adapter->dev,
1215 adapter->cmp_pages * PAGE_SIZE,
1216 adapter->cmp_ring, adapter->cmpRingPA);
1217
1218 if (adapter->msg_ring)
1219 pci_free_consistent(adapter->dev,
1220 adapter->msg_pages * PAGE_SIZE,
1221 adapter->msg_ring, adapter->msgRingPA);
1222}
1223
1224/*
1225 * Allocate scatter gather lists.
1226 *
1227 * These are statically allocated. Trying to be clever was not worth it.
1228 *
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08001229 * Dynamic allocation can fail, and we can't go deep into the memory
Alok Kataria851b1642009-10-13 14:51:05 -07001230 * allocator, since we're a SCSI driver, and trying too hard to allocate
1231 * memory might generate disk I/O. We also don't want to fail disk I/O
1232 * in that case because we can't get an allocation - the I/O could be
1233 * trying to swap out data to free memory. Since that is pathological,
1234 * just use a statically allocated scatter list.
1235 *
1236 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001237static int pvscsi_allocate_sg(struct pvscsi_adapter *adapter)
Alok Kataria851b1642009-10-13 14:51:05 -07001238{
1239 struct pvscsi_ctx *ctx;
1240 int i;
1241
1242 ctx = adapter->cmd_map;
1243 BUILD_BUG_ON(sizeof(struct pvscsi_sg_list) > SGL_SIZE);
1244
1245 for (i = 0; i < adapter->req_depth; ++i, ++ctx) {
1246 ctx->sgl = (void *)__get_free_pages(GFP_KERNEL,
1247 get_order(SGL_SIZE));
1248 ctx->sglPA = 0;
1249 BUG_ON(!IS_ALIGNED(((unsigned long)ctx->sgl), PAGE_SIZE));
1250 if (!ctx->sgl) {
1251 for (; i >= 0; --i, --ctx) {
1252 free_pages((unsigned long)ctx->sgl,
1253 get_order(SGL_SIZE));
1254 ctx->sgl = NULL;
1255 }
1256 return -ENOMEM;
1257 }
1258 }
1259
1260 return 0;
1261}
1262
Arvind Kumara9310732012-03-08 15:48:53 +05301263/*
1264 * Query the device, fetch the config info and return the
1265 * maximum number of targets on the adapter. In case of
1266 * failure due to any reason return default i.e. 16.
1267 */
1268static u32 pvscsi_get_max_targets(struct pvscsi_adapter *adapter)
1269{
1270 struct PVSCSICmdDescConfigCmd cmd;
1271 struct PVSCSIConfigPageHeader *header;
1272 struct device *dev;
1273 dma_addr_t configPagePA;
1274 void *config_page;
1275 u32 numPhys = 16;
1276
1277 dev = pvscsi_dev(adapter);
1278 config_page = pci_alloc_consistent(adapter->dev, PAGE_SIZE,
1279 &configPagePA);
1280 if (!config_page) {
1281 dev_warn(dev, "vmw_pvscsi: failed to allocate memory for config page\n");
1282 goto exit;
1283 }
1284 BUG_ON(configPagePA & ~PAGE_MASK);
1285
1286 /* Fetch config info from the device. */
1287 cmd.configPageAddress = ((u64)PVSCSI_CONFIG_CONTROLLER_ADDRESS) << 32;
1288 cmd.configPageNum = PVSCSI_CONFIG_PAGE_CONTROLLER;
1289 cmd.cmpAddr = configPagePA;
1290 cmd._pad = 0;
1291
1292 /*
1293 * Mark the completion page header with error values. If the device
1294 * completes the command successfully, it sets the status values to
1295 * indicate success.
1296 */
1297 header = config_page;
1298 memset(header, 0, sizeof *header);
1299 header->hostStatus = BTSTAT_INVPARAM;
1300 header->scsiStatus = SDSTAT_CHECK;
1301
1302 pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_CONFIG, &cmd, sizeof cmd);
1303
1304 if (header->hostStatus == BTSTAT_SUCCESS &&
1305 header->scsiStatus == SDSTAT_GOOD) {
1306 struct PVSCSIConfigPageController *config;
1307
1308 config = config_page;
1309 numPhys = config->numPhys;
1310 } else
1311 dev_warn(dev, "vmw_pvscsi: PVSCSI_CMD_CONFIG failed. hostStatus = 0x%x, scsiStatus = 0x%x\n",
1312 header->hostStatus, header->scsiStatus);
1313 pci_free_consistent(adapter->dev, PAGE_SIZE, config_page, configPagePA);
1314exit:
1315 return numPhys;
1316}
1317
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001318static int pvscsi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Alok Kataria851b1642009-10-13 14:51:05 -07001319{
1320 struct pvscsi_adapter *adapter;
1321 struct Scsi_Host *host;
Arvind Kumara9310732012-03-08 15:48:53 +05301322 struct device *dev;
Alok Kataria851b1642009-10-13 14:51:05 -07001323 unsigned int i;
1324 unsigned long flags = 0;
1325 int error;
1326
1327 error = -ENODEV;
1328
1329 if (pci_enable_device(pdev))
1330 return error;
1331
1332 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0 &&
1333 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
1334 printk(KERN_INFO "vmw_pvscsi: using 64bit dma\n");
1335 } else if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) == 0 &&
1336 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) == 0) {
1337 printk(KERN_INFO "vmw_pvscsi: using 32bit dma\n");
1338 } else {
1339 printk(KERN_ERR "vmw_pvscsi: failed to set DMA mask\n");
1340 goto out_disable_device;
1341 }
1342
1343 pvscsi_template.can_queue =
1344 min(PVSCSI_MAX_NUM_PAGES_REQ_RING, pvscsi_ring_pages) *
1345 PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE;
1346 pvscsi_template.cmd_per_lun =
1347 min(pvscsi_template.can_queue, pvscsi_cmd_per_lun);
1348 host = scsi_host_alloc(&pvscsi_template, sizeof(struct pvscsi_adapter));
1349 if (!host) {
1350 printk(KERN_ERR "vmw_pvscsi: failed to allocate host\n");
1351 goto out_disable_device;
1352 }
1353
1354 adapter = shost_priv(host);
1355 memset(adapter, 0, sizeof(*adapter));
1356 adapter->dev = pdev;
1357 adapter->host = host;
1358
1359 spin_lock_init(&adapter->hw_lock);
1360
1361 host->max_channel = 0;
1362 host->max_id = 16;
1363 host->max_lun = 1;
1364 host->max_cmd_len = 16;
1365
1366 adapter->rev = pdev->revision;
1367
1368 if (pci_request_regions(pdev, "vmw_pvscsi")) {
1369 printk(KERN_ERR "vmw_pvscsi: pci memory selection failed\n");
1370 goto out_free_host;
1371 }
1372
1373 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1374 if ((pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE_IO))
1375 continue;
1376
1377 if (pci_resource_len(pdev, i) < PVSCSI_MEM_SPACE_SIZE)
1378 continue;
1379
1380 break;
1381 }
1382
1383 if (i == DEVICE_COUNT_RESOURCE) {
1384 printk(KERN_ERR
1385 "vmw_pvscsi: adapter has no suitable MMIO region\n");
1386 goto out_release_resources;
1387 }
1388
1389 adapter->mmioBase = pci_iomap(pdev, i, PVSCSI_MEM_SPACE_SIZE);
1390
1391 if (!adapter->mmioBase) {
1392 printk(KERN_ERR
1393 "vmw_pvscsi: can't iomap for BAR %d memsize %lu\n",
1394 i, PVSCSI_MEM_SPACE_SIZE);
1395 goto out_release_resources;
1396 }
1397
1398 pci_set_master(pdev);
1399 pci_set_drvdata(pdev, host);
1400
1401 ll_adapter_reset(adapter);
1402
1403 adapter->use_msg = pvscsi_setup_msg_workqueue(adapter);
1404
1405 error = pvscsi_allocate_rings(adapter);
1406 if (error) {
1407 printk(KERN_ERR "vmw_pvscsi: unable to allocate ring memory\n");
1408 goto out_release_resources;
1409 }
1410
1411 /*
Arvind Kumara9310732012-03-08 15:48:53 +05301412 * Ask the device for max number of targets.
1413 */
1414 host->max_id = pvscsi_get_max_targets(adapter);
1415 dev = pvscsi_dev(adapter);
1416 dev_info(dev, "vmw_pvscsi: host->max_id: %u\n", host->max_id);
1417
1418 /*
Alok Kataria851b1642009-10-13 14:51:05 -07001419 * From this point on we should reset the adapter if anything goes
1420 * wrong.
1421 */
1422 pvscsi_setup_all_rings(adapter);
1423
1424 adapter->cmd_map = kcalloc(adapter->req_depth,
1425 sizeof(struct pvscsi_ctx), GFP_KERNEL);
1426 if (!adapter->cmd_map) {
1427 printk(KERN_ERR "vmw_pvscsi: failed to allocate memory.\n");
1428 error = -ENOMEM;
1429 goto out_reset_adapter;
1430 }
1431
1432 INIT_LIST_HEAD(&adapter->cmd_pool);
1433 for (i = 0; i < adapter->req_depth; i++) {
1434 struct pvscsi_ctx *ctx = adapter->cmd_map + i;
1435 list_add(&ctx->list, &adapter->cmd_pool);
1436 }
1437
1438 error = pvscsi_allocate_sg(adapter);
1439 if (error) {
1440 printk(KERN_ERR "vmw_pvscsi: unable to allocate s/g table\n");
1441 goto out_reset_adapter;
1442 }
1443
1444 if (!pvscsi_disable_msix &&
1445 pvscsi_setup_msix(adapter, &adapter->irq) == 0) {
1446 printk(KERN_INFO "vmw_pvscsi: using MSI-X\n");
1447 adapter->use_msix = 1;
1448 } else if (!pvscsi_disable_msi && pci_enable_msi(pdev) == 0) {
1449 printk(KERN_INFO "vmw_pvscsi: using MSI\n");
1450 adapter->use_msi = 1;
1451 adapter->irq = pdev->irq;
1452 } else {
1453 printk(KERN_INFO "vmw_pvscsi: using INTx\n");
1454 adapter->irq = pdev->irq;
1455 flags = IRQF_SHARED;
1456 }
1457
Rishi Mehta2a815b52014-03-11 13:51:33 -07001458 adapter->use_req_threshold = pvscsi_setup_req_threshold(adapter, true);
1459 printk(KERN_DEBUG "vmw_pvscsi: driver-based request coalescing %sabled\n",
1460 adapter->use_req_threshold ? "en" : "dis");
1461
Alok Kataria851b1642009-10-13 14:51:05 -07001462 error = request_irq(adapter->irq, pvscsi_isr, flags,
1463 "vmw_pvscsi", adapter);
1464 if (error) {
1465 printk(KERN_ERR
1466 "vmw_pvscsi: unable to request IRQ: %d\n", error);
1467 adapter->irq = 0;
1468 goto out_reset_adapter;
1469 }
1470
1471 error = scsi_add_host(host, &pdev->dev);
1472 if (error) {
1473 printk(KERN_ERR
1474 "vmw_pvscsi: scsi_add_host failed: %d\n", error);
1475 goto out_reset_adapter;
1476 }
1477
1478 dev_info(&pdev->dev, "VMware PVSCSI rev %d host #%u\n",
1479 adapter->rev, host->host_no);
1480
1481 pvscsi_unmask_intr(adapter);
1482
1483 scsi_scan_host(host);
1484
1485 return 0;
1486
1487out_reset_adapter:
1488 ll_adapter_reset(adapter);
1489out_release_resources:
1490 pvscsi_release_resources(adapter);
1491out_free_host:
1492 scsi_host_put(host);
1493out_disable_device:
Alok Kataria851b1642009-10-13 14:51:05 -07001494 pci_disable_device(pdev);
1495
1496 return error;
1497}
1498
1499static void __pvscsi_shutdown(struct pvscsi_adapter *adapter)
1500{
1501 pvscsi_mask_intr(adapter);
1502
1503 if (adapter->workqueue)
1504 flush_workqueue(adapter->workqueue);
1505
1506 pvscsi_shutdown_intr(adapter);
1507
1508 pvscsi_process_request_ring(adapter);
1509 pvscsi_process_completion_ring(adapter);
1510 ll_adapter_reset(adapter);
1511}
1512
1513static void pvscsi_shutdown(struct pci_dev *dev)
1514{
1515 struct Scsi_Host *host = pci_get_drvdata(dev);
1516 struct pvscsi_adapter *adapter = shost_priv(host);
1517
1518 __pvscsi_shutdown(adapter);
1519}
1520
1521static void pvscsi_remove(struct pci_dev *pdev)
1522{
1523 struct Scsi_Host *host = pci_get_drvdata(pdev);
1524 struct pvscsi_adapter *adapter = shost_priv(host);
1525
1526 scsi_remove_host(host);
1527
1528 __pvscsi_shutdown(adapter);
1529 pvscsi_release_resources(adapter);
1530
1531 scsi_host_put(host);
1532
Alok Kataria851b1642009-10-13 14:51:05 -07001533 pci_disable_device(pdev);
1534}
1535
1536static struct pci_driver pvscsi_pci_driver = {
1537 .name = "vmw_pvscsi",
1538 .id_table = pvscsi_pci_tbl,
1539 .probe = pvscsi_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001540 .remove = pvscsi_remove,
Alok Kataria851b1642009-10-13 14:51:05 -07001541 .shutdown = pvscsi_shutdown,
1542};
1543
1544static int __init pvscsi_init(void)
1545{
1546 pr_info("%s - version %s\n",
1547 PVSCSI_LINUX_DRIVER_DESC, PVSCSI_DRIVER_VERSION_STRING);
1548 return pci_register_driver(&pvscsi_pci_driver);
1549}
1550
1551static void __exit pvscsi_exit(void)
1552{
1553 pci_unregister_driver(&pvscsi_pci_driver);
1554}
1555
1556module_init(pvscsi_init);
1557module_exit(pvscsi_exit);