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