1851b1642SAlok Kataria /*
2851b1642SAlok Kataria * Linux driver for VMware's para-virtualized SCSI HBA.
3851b1642SAlok Kataria *
4a2713cceSArvind Kumar * Copyright (C) 2008-2014, VMware, Inc. All Rights Reserved.
5851b1642SAlok Kataria *
6851b1642SAlok Kataria * This program is free software; you can redistribute it and/or modify it
7851b1642SAlok Kataria * under the terms of the GNU General Public License as published by the
8851b1642SAlok Kataria * Free Software Foundation; version 2 of the License and no later version.
9851b1642SAlok Kataria *
10851b1642SAlok Kataria * This program is distributed in the hope that it will be useful, but
11851b1642SAlok Kataria * WITHOUT ANY WARRANTY; without even the implied warranty of
12851b1642SAlok Kataria * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13851b1642SAlok Kataria * NON INFRINGEMENT. See the GNU General Public License for more
14851b1642SAlok Kataria * details.
15851b1642SAlok Kataria *
16851b1642SAlok Kataria * You should have received a copy of the GNU General Public License
17851b1642SAlok Kataria * along with this program; if not, write to the Free Software
18851b1642SAlok Kataria * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19851b1642SAlok Kataria *
20851b1642SAlok Kataria */
21851b1642SAlok Kataria
22851b1642SAlok Kataria #include <linux/kernel.h>
23851b1642SAlok Kataria #include <linux/module.h>
24851b1642SAlok Kataria #include <linux/interrupt.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
26851b1642SAlok Kataria #include <linux/workqueue.h>
27851b1642SAlok Kataria #include <linux/pci.h>
28851b1642SAlok Kataria
29851b1642SAlok Kataria #include <scsi/scsi.h>
30851b1642SAlok Kataria #include <scsi/scsi_host.h>
31851b1642SAlok Kataria #include <scsi/scsi_cmnd.h>
32851b1642SAlok Kataria #include <scsi/scsi_device.h>
3302845560SArvind Kumar #include <scsi/scsi_tcq.h>
34851b1642SAlok Kataria
35851b1642SAlok Kataria #include "vmw_pvscsi.h"
36851b1642SAlok Kataria
37851b1642SAlok Kataria #define PVSCSI_LINUX_DRIVER_DESC "VMware PVSCSI driver"
38851b1642SAlok Kataria
39851b1642SAlok Kataria MODULE_DESCRIPTION(PVSCSI_LINUX_DRIVER_DESC);
40851b1642SAlok Kataria MODULE_AUTHOR("VMware, Inc.");
41851b1642SAlok Kataria MODULE_LICENSE("GPL");
42851b1642SAlok Kataria MODULE_VERSION(PVSCSI_DRIVER_VERSION_STRING);
43851b1642SAlok Kataria
44851b1642SAlok Kataria #define PVSCSI_DEFAULT_NUM_PAGES_PER_RING 8
45851b1642SAlok Kataria #define PVSCSI_DEFAULT_NUM_PAGES_MSG_RING 1
4602845560SArvind Kumar #define PVSCSI_DEFAULT_QUEUE_DEPTH 254
47851b1642SAlok Kataria #define SGL_SIZE PAGE_SIZE
48851b1642SAlok Kataria
49851b1642SAlok Kataria struct pvscsi_sg_list {
50851b1642SAlok Kataria struct PVSCSISGElement sge[PVSCSI_MAX_NUM_SG_ENTRIES_PER_SEGMENT];
51851b1642SAlok Kataria };
52851b1642SAlok Kataria
53851b1642SAlok Kataria struct pvscsi_ctx {
54851b1642SAlok Kataria /*
55851b1642SAlok Kataria * The index of the context in cmd_map serves as the context ID for a
56851b1642SAlok Kataria * 1-to-1 mapping completions back to requests.
57851b1642SAlok Kataria */
58851b1642SAlok Kataria struct scsi_cmnd *cmd;
59851b1642SAlok Kataria struct pvscsi_sg_list *sgl;
60851b1642SAlok Kataria struct list_head list;
61851b1642SAlok Kataria dma_addr_t dataPA;
62851b1642SAlok Kataria dma_addr_t sensePA;
63851b1642SAlok Kataria dma_addr_t sglPA;
64a2713cceSArvind Kumar struct completion *abort_cmp;
65851b1642SAlok Kataria };
66851b1642SAlok Kataria
67851b1642SAlok Kataria struct pvscsi_adapter {
68851b1642SAlok Kataria char *mmioBase;
69851b1642SAlok Kataria u8 rev;
70851b1642SAlok Kataria bool use_msg;
712a815b5aSRishi Mehta bool use_req_threshold;
72851b1642SAlok Kataria
73851b1642SAlok Kataria spinlock_t hw_lock;
74851b1642SAlok Kataria
75851b1642SAlok Kataria struct workqueue_struct *workqueue;
76851b1642SAlok Kataria struct work_struct work;
77851b1642SAlok Kataria
78851b1642SAlok Kataria struct PVSCSIRingReqDesc *req_ring;
79851b1642SAlok Kataria unsigned req_pages;
80851b1642SAlok Kataria unsigned req_depth;
81851b1642SAlok Kataria dma_addr_t reqRingPA;
82851b1642SAlok Kataria
83851b1642SAlok Kataria struct PVSCSIRingCmpDesc *cmp_ring;
84851b1642SAlok Kataria unsigned cmp_pages;
85851b1642SAlok Kataria dma_addr_t cmpRingPA;
86851b1642SAlok Kataria
87851b1642SAlok Kataria struct PVSCSIRingMsgDesc *msg_ring;
88851b1642SAlok Kataria unsigned msg_pages;
89851b1642SAlok Kataria dma_addr_t msgRingPA;
90851b1642SAlok Kataria
91851b1642SAlok Kataria struct PVSCSIRingsState *rings_state;
92851b1642SAlok Kataria dma_addr_t ringStatePA;
93851b1642SAlok Kataria
94851b1642SAlok Kataria struct pci_dev *dev;
95851b1642SAlok Kataria struct Scsi_Host *host;
96851b1642SAlok Kataria
97851b1642SAlok Kataria struct list_head cmd_pool;
98851b1642SAlok Kataria struct pvscsi_ctx *cmd_map;
99851b1642SAlok Kataria };
100851b1642SAlok Kataria
101851b1642SAlok Kataria
102851b1642SAlok Kataria /* Command line parameters */
10302845560SArvind Kumar static int pvscsi_ring_pages;
104851b1642SAlok Kataria static int pvscsi_msg_ring_pages = PVSCSI_DEFAULT_NUM_PAGES_MSG_RING;
105851b1642SAlok Kataria static int pvscsi_cmd_per_lun = PVSCSI_DEFAULT_QUEUE_DEPTH;
106851b1642SAlok Kataria static bool pvscsi_disable_msi;
107851b1642SAlok Kataria static bool pvscsi_disable_msix;
108851b1642SAlok Kataria static bool pvscsi_use_msg = true;
1092a815b5aSRishi Mehta static bool pvscsi_use_req_threshold = true;
110851b1642SAlok Kataria
111851b1642SAlok Kataria #define PVSCSI_RW (S_IRUSR | S_IWUSR)
112851b1642SAlok Kataria
113851b1642SAlok Kataria module_param_named(ring_pages, pvscsi_ring_pages, int, PVSCSI_RW);
114851b1642SAlok Kataria MODULE_PARM_DESC(ring_pages, "Number of pages per req/cmp ring - (default="
11502845560SArvind Kumar __stringify(PVSCSI_DEFAULT_NUM_PAGES_PER_RING)
11602845560SArvind Kumar "[up to 16 targets],"
11702845560SArvind Kumar __stringify(PVSCSI_SETUP_RINGS_MAX_NUM_PAGES)
11802845560SArvind Kumar "[for 16+ targets])");
119851b1642SAlok Kataria
120851b1642SAlok Kataria module_param_named(msg_ring_pages, pvscsi_msg_ring_pages, int, PVSCSI_RW);
121851b1642SAlok Kataria MODULE_PARM_DESC(msg_ring_pages, "Number of pages for the msg ring - (default="
122851b1642SAlok Kataria __stringify(PVSCSI_DEFAULT_NUM_PAGES_MSG_RING) ")");
123851b1642SAlok Kataria
124851b1642SAlok Kataria module_param_named(cmd_per_lun, pvscsi_cmd_per_lun, int, PVSCSI_RW);
125851b1642SAlok Kataria MODULE_PARM_DESC(cmd_per_lun, "Maximum commands per lun - (default="
12602845560SArvind Kumar __stringify(PVSCSI_DEFAULT_QUEUE_DEPTH) ")");
127851b1642SAlok Kataria
128851b1642SAlok Kataria module_param_named(disable_msi, pvscsi_disable_msi, bool, PVSCSI_RW);
129851b1642SAlok Kataria MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default=0)");
130851b1642SAlok Kataria
131851b1642SAlok Kataria module_param_named(disable_msix, pvscsi_disable_msix, bool, PVSCSI_RW);
132851b1642SAlok Kataria MODULE_PARM_DESC(disable_msix, "Disable MSI-X use in driver - (default=0)");
133851b1642SAlok Kataria
134851b1642SAlok Kataria module_param_named(use_msg, pvscsi_use_msg, bool, PVSCSI_RW);
135851b1642SAlok Kataria MODULE_PARM_DESC(use_msg, "Use msg ring when available - (default=1)");
136851b1642SAlok Kataria
1372a815b5aSRishi Mehta module_param_named(use_req_threshold, pvscsi_use_req_threshold,
1382a815b5aSRishi Mehta bool, PVSCSI_RW);
1392a815b5aSRishi Mehta MODULE_PARM_DESC(use_req_threshold, "Use driver-based request coalescing if configured - (default=1)");
1402a815b5aSRishi Mehta
141851b1642SAlok Kataria static const struct pci_device_id pvscsi_pci_tbl[] = {
142851b1642SAlok Kataria { PCI_VDEVICE(VMWARE, PCI_DEVICE_ID_VMWARE_PVSCSI) },
143851b1642SAlok Kataria { 0 }
144851b1642SAlok Kataria };
145851b1642SAlok Kataria
146851b1642SAlok Kataria MODULE_DEVICE_TABLE(pci, pvscsi_pci_tbl);
147851b1642SAlok Kataria
148851b1642SAlok Kataria static struct device *
pvscsi_dev(const struct pvscsi_adapter * adapter)149851b1642SAlok Kataria pvscsi_dev(const struct pvscsi_adapter *adapter)
150851b1642SAlok Kataria {
151851b1642SAlok Kataria return &(adapter->dev->dev);
152851b1642SAlok Kataria }
153851b1642SAlok Kataria
154851b1642SAlok Kataria static struct pvscsi_ctx *
pvscsi_find_context(const struct pvscsi_adapter * adapter,struct scsi_cmnd * cmd)155851b1642SAlok Kataria pvscsi_find_context(const struct pvscsi_adapter *adapter, struct scsi_cmnd *cmd)
156851b1642SAlok Kataria {
157851b1642SAlok Kataria struct pvscsi_ctx *ctx, *end;
158851b1642SAlok Kataria
159851b1642SAlok Kataria end = &adapter->cmd_map[adapter->req_depth];
160851b1642SAlok Kataria for (ctx = adapter->cmd_map; ctx < end; ctx++)
161851b1642SAlok Kataria if (ctx->cmd == cmd)
162851b1642SAlok Kataria return ctx;
163851b1642SAlok Kataria
164851b1642SAlok Kataria return NULL;
165851b1642SAlok Kataria }
166851b1642SAlok Kataria
167851b1642SAlok Kataria static struct pvscsi_ctx *
pvscsi_acquire_context(struct pvscsi_adapter * adapter,struct scsi_cmnd * cmd)168851b1642SAlok Kataria pvscsi_acquire_context(struct pvscsi_adapter *adapter, struct scsi_cmnd *cmd)
169851b1642SAlok Kataria {
170851b1642SAlok Kataria struct pvscsi_ctx *ctx;
171851b1642SAlok Kataria
172851b1642SAlok Kataria if (list_empty(&adapter->cmd_pool))
173851b1642SAlok Kataria return NULL;
174851b1642SAlok Kataria
175851b1642SAlok Kataria ctx = list_first_entry(&adapter->cmd_pool, struct pvscsi_ctx, list);
176851b1642SAlok Kataria ctx->cmd = cmd;
177851b1642SAlok Kataria list_del(&ctx->list);
178851b1642SAlok Kataria
179851b1642SAlok Kataria return ctx;
180851b1642SAlok Kataria }
181851b1642SAlok Kataria
pvscsi_release_context(struct pvscsi_adapter * adapter,struct pvscsi_ctx * ctx)182851b1642SAlok Kataria static void pvscsi_release_context(struct pvscsi_adapter *adapter,
183851b1642SAlok Kataria struct pvscsi_ctx *ctx)
184851b1642SAlok Kataria {
185851b1642SAlok Kataria ctx->cmd = NULL;
186a2713cceSArvind Kumar ctx->abort_cmp = NULL;
187851b1642SAlok Kataria list_add(&ctx->list, &adapter->cmd_pool);
188851b1642SAlok Kataria }
189851b1642SAlok Kataria
190851b1642SAlok Kataria /*
191851b1642SAlok Kataria * Map a pvscsi_ctx struct to a context ID field value; we map to a simple
192851b1642SAlok Kataria * non-zero integer. ctx always points to an entry in cmd_map array, hence
193851b1642SAlok Kataria * the return value is always >=1.
194851b1642SAlok Kataria */
pvscsi_map_context(const struct pvscsi_adapter * adapter,const struct pvscsi_ctx * ctx)195851b1642SAlok Kataria static u64 pvscsi_map_context(const struct pvscsi_adapter *adapter,
196851b1642SAlok Kataria const struct pvscsi_ctx *ctx)
197851b1642SAlok Kataria {
198851b1642SAlok Kataria return ctx - adapter->cmd_map + 1;
199851b1642SAlok Kataria }
200851b1642SAlok Kataria
201851b1642SAlok Kataria static struct pvscsi_ctx *
pvscsi_get_context(const struct pvscsi_adapter * adapter,u64 context)202851b1642SAlok Kataria pvscsi_get_context(const struct pvscsi_adapter *adapter, u64 context)
203851b1642SAlok Kataria {
204851b1642SAlok Kataria return &adapter->cmd_map[context - 1];
205851b1642SAlok Kataria }
206851b1642SAlok Kataria
pvscsi_reg_write(const struct pvscsi_adapter * adapter,u32 offset,u32 val)207851b1642SAlok Kataria static void pvscsi_reg_write(const struct pvscsi_adapter *adapter,
208851b1642SAlok Kataria u32 offset, u32 val)
209851b1642SAlok Kataria {
210851b1642SAlok Kataria writel(val, adapter->mmioBase + offset);
211851b1642SAlok Kataria }
212851b1642SAlok Kataria
pvscsi_reg_read(const struct pvscsi_adapter * adapter,u32 offset)213851b1642SAlok Kataria static u32 pvscsi_reg_read(const struct pvscsi_adapter *adapter, u32 offset)
214851b1642SAlok Kataria {
215851b1642SAlok Kataria return readl(adapter->mmioBase + offset);
216851b1642SAlok Kataria }
217851b1642SAlok Kataria
pvscsi_read_intr_status(const struct pvscsi_adapter * adapter)218851b1642SAlok Kataria static u32 pvscsi_read_intr_status(const struct pvscsi_adapter *adapter)
219851b1642SAlok Kataria {
220851b1642SAlok Kataria return pvscsi_reg_read(adapter, PVSCSI_REG_OFFSET_INTR_STATUS);
221851b1642SAlok Kataria }
222851b1642SAlok Kataria
pvscsi_write_intr_status(const struct pvscsi_adapter * adapter,u32 val)223851b1642SAlok Kataria static void pvscsi_write_intr_status(const struct pvscsi_adapter *adapter,
224851b1642SAlok Kataria u32 val)
225851b1642SAlok Kataria {
226851b1642SAlok Kataria pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_INTR_STATUS, val);
227851b1642SAlok Kataria }
228851b1642SAlok Kataria
pvscsi_unmask_intr(const struct pvscsi_adapter * adapter)229851b1642SAlok Kataria static void pvscsi_unmask_intr(const struct pvscsi_adapter *adapter)
230851b1642SAlok Kataria {
231851b1642SAlok Kataria u32 intr_bits;
232851b1642SAlok Kataria
233851b1642SAlok Kataria intr_bits = PVSCSI_INTR_CMPL_MASK;
234851b1642SAlok Kataria if (adapter->use_msg)
235851b1642SAlok Kataria intr_bits |= PVSCSI_INTR_MSG_MASK;
236851b1642SAlok Kataria
237851b1642SAlok Kataria pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_INTR_MASK, intr_bits);
238851b1642SAlok Kataria }
239851b1642SAlok Kataria
pvscsi_mask_intr(const struct pvscsi_adapter * adapter)240851b1642SAlok Kataria static void pvscsi_mask_intr(const struct pvscsi_adapter *adapter)
241851b1642SAlok Kataria {
242851b1642SAlok Kataria pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_INTR_MASK, 0);
243851b1642SAlok Kataria }
244851b1642SAlok Kataria
pvscsi_write_cmd_desc(const struct pvscsi_adapter * adapter,u32 cmd,const void * desc,size_t len)245851b1642SAlok Kataria static void pvscsi_write_cmd_desc(const struct pvscsi_adapter *adapter,
246851b1642SAlok Kataria u32 cmd, const void *desc, size_t len)
247851b1642SAlok Kataria {
248851b1642SAlok Kataria const u32 *ptr = desc;
249851b1642SAlok Kataria size_t i;
250851b1642SAlok Kataria
251851b1642SAlok Kataria len /= sizeof(*ptr);
252851b1642SAlok Kataria pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_COMMAND, cmd);
253851b1642SAlok Kataria for (i = 0; i < len; i++)
254851b1642SAlok Kataria pvscsi_reg_write(adapter,
255851b1642SAlok Kataria PVSCSI_REG_OFFSET_COMMAND_DATA, ptr[i]);
256851b1642SAlok Kataria }
257851b1642SAlok Kataria
pvscsi_abort_cmd(const struct pvscsi_adapter * adapter,const struct pvscsi_ctx * ctx)258851b1642SAlok Kataria static void pvscsi_abort_cmd(const struct pvscsi_adapter *adapter,
259851b1642SAlok Kataria const struct pvscsi_ctx *ctx)
260851b1642SAlok Kataria {
261851b1642SAlok Kataria struct PVSCSICmdDescAbortCmd cmd = { 0 };
262851b1642SAlok Kataria
263851b1642SAlok Kataria cmd.target = ctx->cmd->device->id;
264851b1642SAlok Kataria cmd.context = pvscsi_map_context(adapter, ctx);
265851b1642SAlok Kataria
266851b1642SAlok Kataria pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_ABORT_CMD, &cmd, sizeof(cmd));
267851b1642SAlok Kataria }
268851b1642SAlok Kataria
pvscsi_kick_rw_io(const struct pvscsi_adapter * adapter)269851b1642SAlok Kataria static void pvscsi_kick_rw_io(const struct pvscsi_adapter *adapter)
270851b1642SAlok Kataria {
271851b1642SAlok Kataria pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_KICK_RW_IO, 0);
272851b1642SAlok Kataria }
273851b1642SAlok Kataria
pvscsi_process_request_ring(const struct pvscsi_adapter * adapter)274851b1642SAlok Kataria static void pvscsi_process_request_ring(const struct pvscsi_adapter *adapter)
275851b1642SAlok Kataria {
276851b1642SAlok Kataria pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_KICK_NON_RW_IO, 0);
277851b1642SAlok Kataria }
278851b1642SAlok Kataria
scsi_is_rw(unsigned char op)279851b1642SAlok Kataria static int scsi_is_rw(unsigned char op)
280851b1642SAlok Kataria {
281851b1642SAlok Kataria return op == READ_6 || op == WRITE_6 ||
282851b1642SAlok Kataria op == READ_10 || op == WRITE_10 ||
283851b1642SAlok Kataria op == READ_12 || op == WRITE_12 ||
284851b1642SAlok Kataria op == READ_16 || op == WRITE_16;
285851b1642SAlok Kataria }
286851b1642SAlok Kataria
pvscsi_kick_io(const struct pvscsi_adapter * adapter,unsigned char op)287851b1642SAlok Kataria static void pvscsi_kick_io(const struct pvscsi_adapter *adapter,
288851b1642SAlok Kataria unsigned char op)
289851b1642SAlok Kataria {
2902a815b5aSRishi Mehta if (scsi_is_rw(op)) {
2912a815b5aSRishi Mehta struct PVSCSIRingsState *s = adapter->rings_state;
2922a815b5aSRishi Mehta
2932a815b5aSRishi Mehta if (!adapter->use_req_threshold ||
2942a815b5aSRishi Mehta s->reqProdIdx - s->reqConsIdx >= s->reqCallThreshold)
295851b1642SAlok Kataria pvscsi_kick_rw_io(adapter);
2962a815b5aSRishi Mehta } else {
297851b1642SAlok Kataria pvscsi_process_request_ring(adapter);
298851b1642SAlok Kataria }
2992a815b5aSRishi Mehta }
300851b1642SAlok Kataria
ll_adapter_reset(const struct pvscsi_adapter * adapter)301851b1642SAlok Kataria static void ll_adapter_reset(const struct pvscsi_adapter *adapter)
302851b1642SAlok Kataria {
303851b1642SAlok Kataria dev_dbg(pvscsi_dev(adapter), "Adapter Reset on %p\n", adapter);
304851b1642SAlok Kataria
305851b1642SAlok Kataria pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_ADAPTER_RESET, NULL, 0);
306851b1642SAlok Kataria }
307851b1642SAlok Kataria
ll_bus_reset(const struct pvscsi_adapter * adapter)308851b1642SAlok Kataria static void ll_bus_reset(const struct pvscsi_adapter *adapter)
309851b1642SAlok Kataria {
31059e13d48SMasanari Iida dev_dbg(pvscsi_dev(adapter), "Resetting bus on %p\n", adapter);
311851b1642SAlok Kataria
312851b1642SAlok Kataria pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_RESET_BUS, NULL, 0);
313851b1642SAlok Kataria }
314851b1642SAlok Kataria
ll_device_reset(const struct pvscsi_adapter * adapter,u32 target)315851b1642SAlok Kataria static void ll_device_reset(const struct pvscsi_adapter *adapter, u32 target)
316851b1642SAlok Kataria {
317851b1642SAlok Kataria struct PVSCSICmdDescResetDevice cmd = { 0 };
318851b1642SAlok Kataria
31959e13d48SMasanari Iida dev_dbg(pvscsi_dev(adapter), "Resetting device: target=%u\n", target);
320851b1642SAlok Kataria
321851b1642SAlok Kataria cmd.target = target;
322851b1642SAlok Kataria
323851b1642SAlok Kataria pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_RESET_DEVICE,
324851b1642SAlok Kataria &cmd, sizeof(cmd));
325851b1642SAlok Kataria }
326851b1642SAlok Kataria
pvscsi_create_sg(struct pvscsi_ctx * ctx,struct scatterlist * sg,unsigned count)327851b1642SAlok Kataria static void pvscsi_create_sg(struct pvscsi_ctx *ctx,
328851b1642SAlok Kataria struct scatterlist *sg, unsigned count)
329851b1642SAlok Kataria {
330851b1642SAlok Kataria unsigned i;
331851b1642SAlok Kataria struct PVSCSISGElement *sge;
332851b1642SAlok Kataria
333851b1642SAlok Kataria BUG_ON(count > PVSCSI_MAX_NUM_SG_ENTRIES_PER_SEGMENT);
334851b1642SAlok Kataria
335851b1642SAlok Kataria sge = &ctx->sgl->sge[0];
336cf9648cbSMing Lei for (i = 0; i < count; i++, sg = sg_next(sg)) {
337851b1642SAlok Kataria sge[i].addr = sg_dma_address(sg);
338851b1642SAlok Kataria sge[i].length = sg_dma_len(sg);
339851b1642SAlok Kataria sge[i].flags = 0;
340851b1642SAlok Kataria }
341851b1642SAlok Kataria }
342851b1642SAlok Kataria
343851b1642SAlok Kataria /*
344851b1642SAlok Kataria * Map all data buffers for a command into PCI space and
345851b1642SAlok Kataria * setup the scatter/gather list if needed.
346851b1642SAlok Kataria */
pvscsi_map_buffers(struct pvscsi_adapter * adapter,struct pvscsi_ctx * ctx,struct scsi_cmnd * cmd,struct PVSCSIRingReqDesc * e)347c965853aSJosh Boyer static int pvscsi_map_buffers(struct pvscsi_adapter *adapter,
348851b1642SAlok Kataria struct pvscsi_ctx *ctx, struct scsi_cmnd *cmd,
349851b1642SAlok Kataria struct PVSCSIRingReqDesc *e)
350851b1642SAlok Kataria {
351851b1642SAlok Kataria unsigned count;
352851b1642SAlok Kataria unsigned bufflen = scsi_bufflen(cmd);
353851b1642SAlok Kataria struct scatterlist *sg;
354851b1642SAlok Kataria
355851b1642SAlok Kataria e->dataLen = bufflen;
356851b1642SAlok Kataria e->dataAddr = 0;
357851b1642SAlok Kataria if (bufflen == 0)
358c965853aSJosh Boyer return 0;
359851b1642SAlok Kataria
360851b1642SAlok Kataria sg = scsi_sglist(cmd);
361851b1642SAlok Kataria count = scsi_sg_count(cmd);
362851b1642SAlok Kataria if (count != 0) {
363851b1642SAlok Kataria int segs = scsi_dma_map(cmd);
364c965853aSJosh Boyer
365c965853aSJosh Boyer if (segs == -ENOMEM) {
366d8dd7d76SThomas Hellstrom scmd_printk(KERN_DEBUG, cmd,
367c965853aSJosh Boyer "vmw_pvscsi: Failed to map cmd sglist for DMA.\n");
368c965853aSJosh Boyer return -ENOMEM;
369c965853aSJosh Boyer } else if (segs > 1) {
370851b1642SAlok Kataria pvscsi_create_sg(ctx, sg, segs);
371851b1642SAlok Kataria
372851b1642SAlok Kataria e->flags |= PVSCSI_FLAG_CMD_WITH_SG_LIST;
3739b7ca6c2SChristoph Hellwig ctx->sglPA = dma_map_single(&adapter->dev->dev,
3749b7ca6c2SChristoph Hellwig ctx->sgl, SGL_SIZE, DMA_TO_DEVICE);
3759b7ca6c2SChristoph Hellwig if (dma_mapping_error(&adapter->dev->dev, ctx->sglPA)) {
376c965853aSJosh Boyer scmd_printk(KERN_ERR, cmd,
377c965853aSJosh Boyer "vmw_pvscsi: Failed to map ctx sglist for DMA.\n");
378c965853aSJosh Boyer scsi_dma_unmap(cmd);
379c965853aSJosh Boyer ctx->sglPA = 0;
380c965853aSJosh Boyer return -ENOMEM;
381c965853aSJosh Boyer }
382851b1642SAlok Kataria e->dataAddr = ctx->sglPA;
383851b1642SAlok Kataria } else
384851b1642SAlok Kataria e->dataAddr = sg_dma_address(sg);
385851b1642SAlok Kataria } else {
386851b1642SAlok Kataria /*
387851b1642SAlok Kataria * In case there is no S/G list, scsi_sglist points
388851b1642SAlok Kataria * directly to the buffer.
389851b1642SAlok Kataria */
3909b7ca6c2SChristoph Hellwig ctx->dataPA = dma_map_single(&adapter->dev->dev, sg, bufflen,
391851b1642SAlok Kataria cmd->sc_data_direction);
3929b7ca6c2SChristoph Hellwig if (dma_mapping_error(&adapter->dev->dev, ctx->dataPA)) {
393d8dd7d76SThomas Hellstrom scmd_printk(KERN_DEBUG, cmd,
394c965853aSJosh Boyer "vmw_pvscsi: Failed to map direct data buffer for DMA.\n");
395c965853aSJosh Boyer return -ENOMEM;
396c965853aSJosh Boyer }
397851b1642SAlok Kataria e->dataAddr = ctx->dataPA;
398851b1642SAlok Kataria }
399c965853aSJosh Boyer
400c965853aSJosh Boyer return 0;
401851b1642SAlok Kataria }
402851b1642SAlok Kataria
403f4652752SThomas Hellstrom /*
404f4652752SThomas Hellstrom * The device incorrectly doesn't clear the first byte of the sense
405f4652752SThomas Hellstrom * buffer in some cases. We have to do it ourselves.
406f4652752SThomas Hellstrom * Otherwise we run into trouble when SWIOTLB is forced.
407f4652752SThomas Hellstrom */
pvscsi_patch_sense(struct scsi_cmnd * cmd)408f4652752SThomas Hellstrom static void pvscsi_patch_sense(struct scsi_cmnd *cmd)
409f4652752SThomas Hellstrom {
410f4652752SThomas Hellstrom if (cmd->sense_buffer)
411f4652752SThomas Hellstrom cmd->sense_buffer[0] = 0;
412f4652752SThomas Hellstrom }
413f4652752SThomas Hellstrom
pvscsi_unmap_buffers(const struct pvscsi_adapter * adapter,struct pvscsi_ctx * ctx)414851b1642SAlok Kataria static void pvscsi_unmap_buffers(const struct pvscsi_adapter *adapter,
415851b1642SAlok Kataria struct pvscsi_ctx *ctx)
416851b1642SAlok Kataria {
417851b1642SAlok Kataria struct scsi_cmnd *cmd;
418851b1642SAlok Kataria unsigned bufflen;
419851b1642SAlok Kataria
420851b1642SAlok Kataria cmd = ctx->cmd;
421851b1642SAlok Kataria bufflen = scsi_bufflen(cmd);
422851b1642SAlok Kataria
423851b1642SAlok Kataria if (bufflen != 0) {
424851b1642SAlok Kataria unsigned count = scsi_sg_count(cmd);
425851b1642SAlok Kataria
426851b1642SAlok Kataria if (count != 0) {
427851b1642SAlok Kataria scsi_dma_unmap(cmd);
428851b1642SAlok Kataria if (ctx->sglPA) {
4299b7ca6c2SChristoph Hellwig dma_unmap_single(&adapter->dev->dev, ctx->sglPA,
4309b7ca6c2SChristoph Hellwig SGL_SIZE, DMA_TO_DEVICE);
431851b1642SAlok Kataria ctx->sglPA = 0;
432851b1642SAlok Kataria }
433851b1642SAlok Kataria } else
4349b7ca6c2SChristoph Hellwig dma_unmap_single(&adapter->dev->dev, ctx->dataPA,
4359b7ca6c2SChristoph Hellwig bufflen, cmd->sc_data_direction);
436851b1642SAlok Kataria }
437851b1642SAlok Kataria if (cmd->sense_buffer)
4389b7ca6c2SChristoph Hellwig dma_unmap_single(&adapter->dev->dev, ctx->sensePA,
4399b7ca6c2SChristoph Hellwig SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
440851b1642SAlok Kataria }
441851b1642SAlok Kataria
pvscsi_allocate_rings(struct pvscsi_adapter * adapter)4426f039790SGreg Kroah-Hartman static int pvscsi_allocate_rings(struct pvscsi_adapter *adapter)
443851b1642SAlok Kataria {
4449b7ca6c2SChristoph Hellwig adapter->rings_state = dma_alloc_coherent(&adapter->dev->dev, PAGE_SIZE,
4459b7ca6c2SChristoph Hellwig &adapter->ringStatePA, GFP_KERNEL);
446851b1642SAlok Kataria if (!adapter->rings_state)
447851b1642SAlok Kataria return -ENOMEM;
448851b1642SAlok Kataria
449851b1642SAlok Kataria adapter->req_pages = min(PVSCSI_MAX_NUM_PAGES_REQ_RING,
450851b1642SAlok Kataria pvscsi_ring_pages);
451851b1642SAlok Kataria adapter->req_depth = adapter->req_pages
452851b1642SAlok Kataria * PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE;
4539b7ca6c2SChristoph Hellwig adapter->req_ring = dma_alloc_coherent(&adapter->dev->dev,
4549b7ca6c2SChristoph Hellwig adapter->req_pages * PAGE_SIZE, &adapter->reqRingPA,
4559b7ca6c2SChristoph Hellwig GFP_KERNEL);
456851b1642SAlok Kataria if (!adapter->req_ring)
457851b1642SAlok Kataria return -ENOMEM;
458851b1642SAlok Kataria
459851b1642SAlok Kataria adapter->cmp_pages = min(PVSCSI_MAX_NUM_PAGES_CMP_RING,
460851b1642SAlok Kataria pvscsi_ring_pages);
4619b7ca6c2SChristoph Hellwig adapter->cmp_ring = dma_alloc_coherent(&adapter->dev->dev,
4629b7ca6c2SChristoph Hellwig adapter->cmp_pages * PAGE_SIZE, &adapter->cmpRingPA,
4639b7ca6c2SChristoph Hellwig GFP_KERNEL);
464851b1642SAlok Kataria if (!adapter->cmp_ring)
465851b1642SAlok Kataria return -ENOMEM;
466851b1642SAlok Kataria
467851b1642SAlok Kataria BUG_ON(!IS_ALIGNED(adapter->ringStatePA, PAGE_SIZE));
468851b1642SAlok Kataria BUG_ON(!IS_ALIGNED(adapter->reqRingPA, PAGE_SIZE));
469851b1642SAlok Kataria BUG_ON(!IS_ALIGNED(adapter->cmpRingPA, PAGE_SIZE));
470851b1642SAlok Kataria
471851b1642SAlok Kataria if (!adapter->use_msg)
472851b1642SAlok Kataria return 0;
473851b1642SAlok Kataria
474851b1642SAlok Kataria adapter->msg_pages = min(PVSCSI_MAX_NUM_PAGES_MSG_RING,
475851b1642SAlok Kataria pvscsi_msg_ring_pages);
4769b7ca6c2SChristoph Hellwig adapter->msg_ring = dma_alloc_coherent(&adapter->dev->dev,
4779b7ca6c2SChristoph Hellwig adapter->msg_pages * PAGE_SIZE, &adapter->msgRingPA,
4789b7ca6c2SChristoph Hellwig GFP_KERNEL);
479851b1642SAlok Kataria if (!adapter->msg_ring)
480851b1642SAlok Kataria return -ENOMEM;
481851b1642SAlok Kataria BUG_ON(!IS_ALIGNED(adapter->msgRingPA, PAGE_SIZE));
482851b1642SAlok Kataria
483851b1642SAlok Kataria return 0;
484851b1642SAlok Kataria }
485851b1642SAlok Kataria
pvscsi_setup_all_rings(const struct pvscsi_adapter * adapter)486851b1642SAlok Kataria static void pvscsi_setup_all_rings(const struct pvscsi_adapter *adapter)
487851b1642SAlok Kataria {
488851b1642SAlok Kataria struct PVSCSICmdDescSetupRings cmd = { 0 };
489851b1642SAlok Kataria dma_addr_t base;
490851b1642SAlok Kataria unsigned i;
491851b1642SAlok Kataria
492851b1642SAlok Kataria cmd.ringsStatePPN = adapter->ringStatePA >> PAGE_SHIFT;
493851b1642SAlok Kataria cmd.reqRingNumPages = adapter->req_pages;
494851b1642SAlok Kataria cmd.cmpRingNumPages = adapter->cmp_pages;
495851b1642SAlok Kataria
496851b1642SAlok Kataria base = adapter->reqRingPA;
497851b1642SAlok Kataria for (i = 0; i < adapter->req_pages; i++) {
498851b1642SAlok Kataria cmd.reqRingPPNs[i] = base >> PAGE_SHIFT;
499851b1642SAlok Kataria base += PAGE_SIZE;
500851b1642SAlok Kataria }
501851b1642SAlok Kataria
502851b1642SAlok Kataria base = adapter->cmpRingPA;
503851b1642SAlok Kataria for (i = 0; i < adapter->cmp_pages; i++) {
504851b1642SAlok Kataria cmd.cmpRingPPNs[i] = base >> PAGE_SHIFT;
505851b1642SAlok Kataria base += PAGE_SIZE;
506851b1642SAlok Kataria }
507851b1642SAlok Kataria
508851b1642SAlok Kataria memset(adapter->rings_state, 0, PAGE_SIZE);
509851b1642SAlok Kataria memset(adapter->req_ring, 0, adapter->req_pages * PAGE_SIZE);
510851b1642SAlok Kataria memset(adapter->cmp_ring, 0, adapter->cmp_pages * PAGE_SIZE);
511851b1642SAlok Kataria
512851b1642SAlok Kataria pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_SETUP_RINGS,
513851b1642SAlok Kataria &cmd, sizeof(cmd));
514851b1642SAlok Kataria
515851b1642SAlok Kataria if (adapter->use_msg) {
516851b1642SAlok Kataria struct PVSCSICmdDescSetupMsgRing cmd_msg = { 0 };
517851b1642SAlok Kataria
518851b1642SAlok Kataria cmd_msg.numPages = adapter->msg_pages;
519851b1642SAlok Kataria
520851b1642SAlok Kataria base = adapter->msgRingPA;
521851b1642SAlok Kataria for (i = 0; i < adapter->msg_pages; i++) {
522851b1642SAlok Kataria cmd_msg.ringPPNs[i] = base >> PAGE_SHIFT;
523851b1642SAlok Kataria base += PAGE_SIZE;
524851b1642SAlok Kataria }
525851b1642SAlok Kataria memset(adapter->msg_ring, 0, adapter->msg_pages * PAGE_SIZE);
526851b1642SAlok Kataria
527851b1642SAlok Kataria pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_SETUP_MSG_RING,
528851b1642SAlok Kataria &cmd_msg, sizeof(cmd_msg));
529851b1642SAlok Kataria }
530851b1642SAlok Kataria }
531851b1642SAlok Kataria
pvscsi_change_queue_depth(struct scsi_device * sdev,int qdepth)532db5ed4dfSChristoph Hellwig static int pvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
53302845560SArvind Kumar {
53402845560SArvind Kumar if (!sdev->tagged_supported)
5351e6f2416SChristoph Hellwig qdepth = 1;
53639f79500SChristoph Hellwig return scsi_change_queue_depth(sdev, qdepth);
53702845560SArvind Kumar }
53802845560SArvind Kumar
539851b1642SAlok Kataria /*
540851b1642SAlok Kataria * Pull a completion descriptor off and pass the completion back
541851b1642SAlok Kataria * to the SCSI mid layer.
542851b1642SAlok Kataria */
pvscsi_complete_request(struct pvscsi_adapter * adapter,const struct PVSCSIRingCmpDesc * e)543851b1642SAlok Kataria static void pvscsi_complete_request(struct pvscsi_adapter *adapter,
544851b1642SAlok Kataria const struct PVSCSIRingCmpDesc *e)
545851b1642SAlok Kataria {
546851b1642SAlok Kataria struct pvscsi_ctx *ctx;
547851b1642SAlok Kataria struct scsi_cmnd *cmd;
548a2713cceSArvind Kumar struct completion *abort_cmp;
549851b1642SAlok Kataria u32 btstat = e->hostStatus;
550851b1642SAlok Kataria u32 sdstat = e->scsiStatus;
551851b1642SAlok Kataria
552851b1642SAlok Kataria ctx = pvscsi_get_context(adapter, e->context);
553851b1642SAlok Kataria cmd = ctx->cmd;
554a2713cceSArvind Kumar abort_cmp = ctx->abort_cmp;
555851b1642SAlok Kataria pvscsi_unmap_buffers(adapter, ctx);
556f4652752SThomas Hellstrom if (sdstat != SAM_STAT_CHECK_CONDITION)
557f4652752SThomas Hellstrom pvscsi_patch_sense(cmd);
558851b1642SAlok Kataria pvscsi_release_context(adapter, ctx);
559a2713cceSArvind Kumar if (abort_cmp) {
560a2713cceSArvind Kumar /*
561a2713cceSArvind Kumar * The command was requested to be aborted. Just signal that
562a2713cceSArvind Kumar * the request completed and swallow the actual cmd completion
563a2713cceSArvind Kumar * here. The abort handler will post a completion for this
564a2713cceSArvind Kumar * command indicating that it got successfully aborted.
565a2713cceSArvind Kumar */
566a2713cceSArvind Kumar complete(abort_cmp);
567a2713cceSArvind Kumar return;
568a2713cceSArvind Kumar }
569851b1642SAlok Kataria
570a2713cceSArvind Kumar cmd->result = 0;
571851b1642SAlok Kataria if (sdstat != SAM_STAT_GOOD &&
572851b1642SAlok Kataria (btstat == BTSTAT_SUCCESS ||
573851b1642SAlok Kataria btstat == BTSTAT_LINKED_COMMAND_COMPLETED ||
574851b1642SAlok Kataria btstat == BTSTAT_LINKED_COMMAND_COMPLETED_WITH_FLAG)) {
575e95153b6SJim Gill if (sdstat == SAM_STAT_COMMAND_TERMINATED) {
576e95153b6SJim Gill cmd->result = (DID_RESET << 16);
577e95153b6SJim Gill } else {
578851b1642SAlok Kataria cmd->result = (DID_OK << 16) | sdstat;
579e95153b6SJim Gill }
580851b1642SAlok Kataria } else
581851b1642SAlok Kataria switch (btstat) {
582851b1642SAlok Kataria case BTSTAT_SUCCESS:
583851b1642SAlok Kataria case BTSTAT_LINKED_COMMAND_COMPLETED:
584851b1642SAlok Kataria case BTSTAT_LINKED_COMMAND_COMPLETED_WITH_FLAG:
585e662502bSMatt Wang /*
586e662502bSMatt Wang * Commands like INQUIRY may transfer less data than
587e662502bSMatt Wang * requested by the initiator via bufflen. Set residual
588e662502bSMatt Wang * count to make upper layer aware of the actual amount
589*142c779dSAlexey Makhalov * of data returned. There are cases when controller
590*142c779dSAlexey Makhalov * returns zero dataLen with non zero data - do not set
591*142c779dSAlexey Makhalov * residual count in that case.
592e662502bSMatt Wang */
593*142c779dSAlexey Makhalov if (e->dataLen && (e->dataLen < scsi_bufflen(cmd)))
594e662502bSMatt Wang scsi_set_resid(cmd, scsi_bufflen(cmd) - e->dataLen);
595851b1642SAlok Kataria cmd->result = (DID_OK << 16);
596851b1642SAlok Kataria break;
597851b1642SAlok Kataria
598851b1642SAlok Kataria case BTSTAT_DATARUN:
599851b1642SAlok Kataria case BTSTAT_DATA_UNDERRUN:
600851b1642SAlok Kataria /* Report residual data in underruns */
601851b1642SAlok Kataria scsi_set_resid(cmd, scsi_bufflen(cmd) - e->dataLen);
602851b1642SAlok Kataria cmd->result = (DID_ERROR << 16);
603851b1642SAlok Kataria break;
604851b1642SAlok Kataria
605851b1642SAlok Kataria case BTSTAT_SELTIMEO:
606851b1642SAlok Kataria /* Our emulation returns this for non-connected devs */
607851b1642SAlok Kataria cmd->result = (DID_BAD_TARGET << 16);
608851b1642SAlok Kataria break;
609851b1642SAlok Kataria
610851b1642SAlok Kataria case BTSTAT_LUNMISMATCH:
611851b1642SAlok Kataria case BTSTAT_TAGREJECT:
612851b1642SAlok Kataria case BTSTAT_BADMSG:
613851b1642SAlok Kataria case BTSTAT_HAHARDWARE:
614851b1642SAlok Kataria case BTSTAT_INVPHASE:
615851b1642SAlok Kataria case BTSTAT_HATIMEOUT:
616851b1642SAlok Kataria case BTSTAT_NORESPONSE:
617851b1642SAlok Kataria case BTSTAT_DISCONNECT:
618851b1642SAlok Kataria case BTSTAT_HASOFTWARE:
619851b1642SAlok Kataria case BTSTAT_BUSFREE:
620851b1642SAlok Kataria case BTSTAT_SENSFAILED:
621851b1642SAlok Kataria cmd->result |= (DID_ERROR << 16);
622851b1642SAlok Kataria break;
623851b1642SAlok Kataria
624851b1642SAlok Kataria case BTSTAT_SENTRST:
625851b1642SAlok Kataria case BTSTAT_RECVRST:
626851b1642SAlok Kataria case BTSTAT_BUSRESET:
627851b1642SAlok Kataria cmd->result = (DID_RESET << 16);
628851b1642SAlok Kataria break;
629851b1642SAlok Kataria
630851b1642SAlok Kataria case BTSTAT_ABORTQUEUE:
631f4b02427SJim Gill cmd->result = (DID_BUS_BUSY << 16);
632851b1642SAlok Kataria break;
633851b1642SAlok Kataria
634851b1642SAlok Kataria case BTSTAT_SCSIPARITY:
635851b1642SAlok Kataria cmd->result = (DID_PARITY << 16);
636851b1642SAlok Kataria break;
637851b1642SAlok Kataria
638851b1642SAlok Kataria default:
639851b1642SAlok Kataria cmd->result = (DID_ERROR << 16);
640851b1642SAlok Kataria scmd_printk(KERN_DEBUG, cmd,
641851b1642SAlok Kataria "Unknown completion status: 0x%x\n",
642851b1642SAlok Kataria btstat);
643851b1642SAlok Kataria }
644851b1642SAlok Kataria
645851b1642SAlok Kataria dev_dbg(&cmd->device->sdev_gendev,
646851b1642SAlok Kataria "cmd=%p %x ctx=%p result=0x%x status=0x%x,%x\n",
647851b1642SAlok Kataria cmd, cmd->cmnd[0], ctx, cmd->result, btstat, sdstat);
648851b1642SAlok Kataria
649aeb2627dSBart Van Assche scsi_done(cmd);
650851b1642SAlok Kataria }
651851b1642SAlok Kataria
652851b1642SAlok Kataria /*
653851b1642SAlok Kataria * barrier usage : Since the PVSCSI device is emulated, there could be cases
654851b1642SAlok Kataria * where we may want to serialize some accesses between the driver and the
655851b1642SAlok Kataria * emulation layer. We use compiler barriers instead of the more expensive
656851b1642SAlok Kataria * memory barriers because PVSCSI is only supported on X86 which has strong
657851b1642SAlok Kataria * memory access ordering.
658851b1642SAlok Kataria */
pvscsi_process_completion_ring(struct pvscsi_adapter * adapter)659851b1642SAlok Kataria static void pvscsi_process_completion_ring(struct pvscsi_adapter *adapter)
660851b1642SAlok Kataria {
661851b1642SAlok Kataria struct PVSCSIRingsState *s = adapter->rings_state;
662851b1642SAlok Kataria struct PVSCSIRingCmpDesc *ring = adapter->cmp_ring;
663851b1642SAlok Kataria u32 cmp_entries = s->cmpNumEntriesLog2;
664851b1642SAlok Kataria
665851b1642SAlok Kataria while (s->cmpConsIdx != s->cmpProdIdx) {
666851b1642SAlok Kataria struct PVSCSIRingCmpDesc *e = ring + (s->cmpConsIdx &
667851b1642SAlok Kataria MASK(cmp_entries));
668851b1642SAlok Kataria /*
669851b1642SAlok Kataria * This barrier() ensures that *e is not dereferenced while
670851b1642SAlok Kataria * the device emulation still writes data into the slot.
671851b1642SAlok Kataria * Since the device emulation advances s->cmpProdIdx only after
672851b1642SAlok Kataria * updating the slot we want to check it first.
673851b1642SAlok Kataria */
674851b1642SAlok Kataria barrier();
675851b1642SAlok Kataria pvscsi_complete_request(adapter, e);
676851b1642SAlok Kataria /*
677851b1642SAlok Kataria * This barrier() ensures that compiler doesn't reorder write
678851b1642SAlok Kataria * to s->cmpConsIdx before the read of (*e) inside
679851b1642SAlok Kataria * pvscsi_complete_request. Otherwise, device emulation may
680851b1642SAlok Kataria * overwrite *e before we had a chance to read it.
681851b1642SAlok Kataria */
682851b1642SAlok Kataria barrier();
683851b1642SAlok Kataria s->cmpConsIdx++;
684851b1642SAlok Kataria }
685851b1642SAlok Kataria }
686851b1642SAlok Kataria
687851b1642SAlok Kataria /*
688851b1642SAlok Kataria * Translate a Linux SCSI request into a request ring entry.
689851b1642SAlok Kataria */
pvscsi_queue_ring(struct pvscsi_adapter * adapter,struct pvscsi_ctx * ctx,struct scsi_cmnd * cmd)690851b1642SAlok Kataria static int pvscsi_queue_ring(struct pvscsi_adapter *adapter,
691851b1642SAlok Kataria struct pvscsi_ctx *ctx, struct scsi_cmnd *cmd)
692851b1642SAlok Kataria {
693851b1642SAlok Kataria struct PVSCSIRingsState *s;
694851b1642SAlok Kataria struct PVSCSIRingReqDesc *e;
695851b1642SAlok Kataria struct scsi_device *sdev;
696851b1642SAlok Kataria u32 req_entries;
697851b1642SAlok Kataria
698851b1642SAlok Kataria s = adapter->rings_state;
699851b1642SAlok Kataria sdev = cmd->device;
700851b1642SAlok Kataria req_entries = s->reqNumEntriesLog2;
701851b1642SAlok Kataria
702851b1642SAlok Kataria /*
703851b1642SAlok Kataria * If this condition holds, we might have room on the request ring, but
704851b1642SAlok Kataria * we might not have room on the completion ring for the response.
705851b1642SAlok Kataria * However, we have already ruled out this possibility - we would not
706851b1642SAlok Kataria * have successfully allocated a context if it were true, since we only
707851b1642SAlok Kataria * have one context per request entry. Check for it anyway, since it
708851b1642SAlok Kataria * would be a serious bug.
709851b1642SAlok Kataria */
710851b1642SAlok Kataria if (s->reqProdIdx - s->cmpConsIdx >= 1 << req_entries) {
711851b1642SAlok Kataria scmd_printk(KERN_ERR, cmd, "vmw_pvscsi: "
712851b1642SAlok Kataria "ring full: reqProdIdx=%d cmpConsIdx=%d\n",
713851b1642SAlok Kataria s->reqProdIdx, s->cmpConsIdx);
714851b1642SAlok Kataria return -1;
715851b1642SAlok Kataria }
716851b1642SAlok Kataria
717851b1642SAlok Kataria e = adapter->req_ring + (s->reqProdIdx & MASK(req_entries));
718851b1642SAlok Kataria
719851b1642SAlok Kataria e->bus = sdev->channel;
720851b1642SAlok Kataria e->target = sdev->id;
721851b1642SAlok Kataria memset(e->lun, 0, sizeof(e->lun));
722851b1642SAlok Kataria e->lun[1] = sdev->lun;
723851b1642SAlok Kataria
724851b1642SAlok Kataria if (cmd->sense_buffer) {
7259b7ca6c2SChristoph Hellwig ctx->sensePA = dma_map_single(&adapter->dev->dev,
7269b7ca6c2SChristoph Hellwig cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
7279b7ca6c2SChristoph Hellwig DMA_FROM_DEVICE);
7289b7ca6c2SChristoph Hellwig if (dma_mapping_error(&adapter->dev->dev, ctx->sensePA)) {
729d8dd7d76SThomas Hellstrom scmd_printk(KERN_DEBUG, cmd,
730c965853aSJosh Boyer "vmw_pvscsi: Failed to map sense buffer for DMA.\n");
731c965853aSJosh Boyer ctx->sensePA = 0;
732c965853aSJosh Boyer return -ENOMEM;
733c965853aSJosh Boyer }
734851b1642SAlok Kataria e->senseAddr = ctx->sensePA;
735851b1642SAlok Kataria e->senseLen = SCSI_SENSE_BUFFERSIZE;
736851b1642SAlok Kataria } else {
737851b1642SAlok Kataria e->senseLen = 0;
738851b1642SAlok Kataria e->senseAddr = 0;
739851b1642SAlok Kataria }
740851b1642SAlok Kataria e->cdbLen = cmd->cmd_len;
741851b1642SAlok Kataria e->vcpuHint = smp_processor_id();
742851b1642SAlok Kataria memcpy(e->cdb, cmd->cmnd, e->cdbLen);
743851b1642SAlok Kataria
744851b1642SAlok Kataria e->tag = SIMPLE_QUEUE_TAG;
745851b1642SAlok Kataria
746851b1642SAlok Kataria if (cmd->sc_data_direction == DMA_FROM_DEVICE)
747851b1642SAlok Kataria e->flags = PVSCSI_FLAG_CMD_DIR_TOHOST;
748851b1642SAlok Kataria else if (cmd->sc_data_direction == DMA_TO_DEVICE)
749851b1642SAlok Kataria e->flags = PVSCSI_FLAG_CMD_DIR_TODEVICE;
750851b1642SAlok Kataria else if (cmd->sc_data_direction == DMA_NONE)
751851b1642SAlok Kataria e->flags = PVSCSI_FLAG_CMD_DIR_NONE;
752851b1642SAlok Kataria else
753851b1642SAlok Kataria e->flags = 0;
754851b1642SAlok Kataria
755c965853aSJosh Boyer if (pvscsi_map_buffers(adapter, ctx, cmd, e) != 0) {
756c965853aSJosh Boyer if (cmd->sense_buffer) {
7579b7ca6c2SChristoph Hellwig dma_unmap_single(&adapter->dev->dev, ctx->sensePA,
758c965853aSJosh Boyer SCSI_SENSE_BUFFERSIZE,
7599b7ca6c2SChristoph Hellwig DMA_FROM_DEVICE);
760c965853aSJosh Boyer ctx->sensePA = 0;
761c965853aSJosh Boyer }
762c965853aSJosh Boyer return -ENOMEM;
763c965853aSJosh Boyer }
764851b1642SAlok Kataria
765851b1642SAlok Kataria e->context = pvscsi_map_context(adapter, ctx);
766851b1642SAlok Kataria
767851b1642SAlok Kataria barrier();
768851b1642SAlok Kataria
769851b1642SAlok Kataria s->reqProdIdx++;
770851b1642SAlok Kataria
771851b1642SAlok Kataria return 0;
772851b1642SAlok Kataria }
773851b1642SAlok Kataria
pvscsi_queue_lck(struct scsi_cmnd * cmd)774af049dfdSBart Van Assche static int pvscsi_queue_lck(struct scsi_cmnd *cmd)
775851b1642SAlok Kataria {
776851b1642SAlok Kataria struct Scsi_Host *host = cmd->device->host;
777851b1642SAlok Kataria struct pvscsi_adapter *adapter = shost_priv(host);
778851b1642SAlok Kataria struct pvscsi_ctx *ctx;
779851b1642SAlok Kataria unsigned long flags;
780240b4cc8SJan Kara unsigned char op;
781851b1642SAlok Kataria
782851b1642SAlok Kataria spin_lock_irqsave(&adapter->hw_lock, flags);
783851b1642SAlok Kataria
784851b1642SAlok Kataria ctx = pvscsi_acquire_context(adapter, cmd);
785851b1642SAlok Kataria if (!ctx || pvscsi_queue_ring(adapter, ctx, cmd) != 0) {
786851b1642SAlok Kataria if (ctx)
787851b1642SAlok Kataria pvscsi_release_context(adapter, ctx);
788851b1642SAlok Kataria spin_unlock_irqrestore(&adapter->hw_lock, flags);
789851b1642SAlok Kataria return SCSI_MLQUEUE_HOST_BUSY;
790851b1642SAlok Kataria }
791851b1642SAlok Kataria
792240b4cc8SJan Kara op = cmd->cmnd[0];
793851b1642SAlok Kataria
794851b1642SAlok Kataria dev_dbg(&cmd->device->sdev_gendev,
795240b4cc8SJan Kara "queued cmd %p, ctx %p, op=%x\n", cmd, ctx, op);
796851b1642SAlok Kataria
797851b1642SAlok Kataria spin_unlock_irqrestore(&adapter->hw_lock, flags);
798851b1642SAlok Kataria
799240b4cc8SJan Kara pvscsi_kick_io(adapter, op);
800851b1642SAlok Kataria
801851b1642SAlok Kataria return 0;
802851b1642SAlok Kataria }
803851b1642SAlok Kataria
DEF_SCSI_QCMD(pvscsi_queue)804f281233dSJeff Garzik static DEF_SCSI_QCMD(pvscsi_queue)
805f281233dSJeff Garzik
806851b1642SAlok Kataria static int pvscsi_abort(struct scsi_cmnd *cmd)
807851b1642SAlok Kataria {
808851b1642SAlok Kataria struct pvscsi_adapter *adapter = shost_priv(cmd->device->host);
809851b1642SAlok Kataria struct pvscsi_ctx *ctx;
810851b1642SAlok Kataria unsigned long flags;
811a2713cceSArvind Kumar int result = SUCCESS;
812a2713cceSArvind Kumar DECLARE_COMPLETION_ONSTACK(abort_cmp);
813aac173e9SDavid Jeffery int done;
814851b1642SAlok Kataria
815851b1642SAlok Kataria scmd_printk(KERN_DEBUG, cmd, "task abort on host %u, %p\n",
816851b1642SAlok Kataria adapter->host->host_no, cmd);
817851b1642SAlok Kataria
818851b1642SAlok Kataria spin_lock_irqsave(&adapter->hw_lock, flags);
819851b1642SAlok Kataria
820851b1642SAlok Kataria /*
821851b1642SAlok Kataria * Poll the completion ring first - we might be trying to abort
822851b1642SAlok Kataria * a command that is waiting to be dispatched in the completion ring.
823851b1642SAlok Kataria */
824851b1642SAlok Kataria pvscsi_process_completion_ring(adapter);
825851b1642SAlok Kataria
826851b1642SAlok Kataria /*
827851b1642SAlok Kataria * If there is no context for the command, it either already succeeded
828851b1642SAlok Kataria * or else was never properly issued. Not our problem.
829851b1642SAlok Kataria */
830851b1642SAlok Kataria ctx = pvscsi_find_context(adapter, cmd);
831851b1642SAlok Kataria if (!ctx) {
832851b1642SAlok Kataria scmd_printk(KERN_DEBUG, cmd, "Failed to abort cmd %p\n", cmd);
833851b1642SAlok Kataria goto out;
834851b1642SAlok Kataria }
835851b1642SAlok Kataria
836a2713cceSArvind Kumar /*
837a2713cceSArvind Kumar * Mark that the command has been requested to be aborted and issue
838a2713cceSArvind Kumar * the abort.
839a2713cceSArvind Kumar */
840a2713cceSArvind Kumar ctx->abort_cmp = &abort_cmp;
841851b1642SAlok Kataria
842a2713cceSArvind Kumar pvscsi_abort_cmd(adapter, ctx);
843a2713cceSArvind Kumar spin_unlock_irqrestore(&adapter->hw_lock, flags);
844a2713cceSArvind Kumar /* Wait for 2 secs for the completion. */
845aac173e9SDavid Jeffery done = wait_for_completion_timeout(&abort_cmp, msecs_to_jiffies(2000));
846a2713cceSArvind Kumar spin_lock_irqsave(&adapter->hw_lock, flags);
847a2713cceSArvind Kumar
848aac173e9SDavid Jeffery if (!done) {
849a2713cceSArvind Kumar /*
850a2713cceSArvind Kumar * Failed to abort the command, unmark the fact that it
851a2713cceSArvind Kumar * was requested to be aborted.
852a2713cceSArvind Kumar */
853a2713cceSArvind Kumar ctx->abort_cmp = NULL;
854a2713cceSArvind Kumar result = FAILED;
855a2713cceSArvind Kumar scmd_printk(KERN_DEBUG, cmd,
856a2713cceSArvind Kumar "Failed to get completion for aborted cmd %p\n",
857a2713cceSArvind Kumar cmd);
858a2713cceSArvind Kumar goto out;
859a2713cceSArvind Kumar }
860a2713cceSArvind Kumar
861a2713cceSArvind Kumar /*
862a2713cceSArvind Kumar * Successfully aborted the command.
863a2713cceSArvind Kumar */
864a2713cceSArvind Kumar cmd->result = (DID_ABORT << 16);
865aeb2627dSBart Van Assche scsi_done(cmd);
866851b1642SAlok Kataria
867851b1642SAlok Kataria out:
868851b1642SAlok Kataria spin_unlock_irqrestore(&adapter->hw_lock, flags);
869a2713cceSArvind Kumar return result;
870851b1642SAlok Kataria }
871851b1642SAlok Kataria
872851b1642SAlok Kataria /*
873851b1642SAlok Kataria * Abort all outstanding requests. This is only safe to use if the completion
874851b1642SAlok Kataria * ring will never be walked again or the device has been reset, because it
875851b1642SAlok Kataria * destroys the 1-1 mapping between context field passed to emulation and our
876851b1642SAlok Kataria * request structure.
877851b1642SAlok Kataria */
pvscsi_reset_all(struct pvscsi_adapter * adapter)878851b1642SAlok Kataria static void pvscsi_reset_all(struct pvscsi_adapter *adapter)
879851b1642SAlok Kataria {
880851b1642SAlok Kataria unsigned i;
881851b1642SAlok Kataria
882851b1642SAlok Kataria for (i = 0; i < adapter->req_depth; i++) {
883851b1642SAlok Kataria struct pvscsi_ctx *ctx = &adapter->cmd_map[i];
884851b1642SAlok Kataria struct scsi_cmnd *cmd = ctx->cmd;
885851b1642SAlok Kataria if (cmd) {
886851b1642SAlok Kataria scmd_printk(KERN_ERR, cmd,
887851b1642SAlok Kataria "Forced reset on cmd %p\n", cmd);
888851b1642SAlok Kataria pvscsi_unmap_buffers(adapter, ctx);
889f4652752SThomas Hellstrom pvscsi_patch_sense(cmd);
890851b1642SAlok Kataria pvscsi_release_context(adapter, ctx);
891851b1642SAlok Kataria cmd->result = (DID_RESET << 16);
892aeb2627dSBart Van Assche scsi_done(cmd);
893851b1642SAlok Kataria }
894851b1642SAlok Kataria }
895851b1642SAlok Kataria }
896851b1642SAlok Kataria
pvscsi_host_reset(struct scsi_cmnd * cmd)897851b1642SAlok Kataria static int pvscsi_host_reset(struct scsi_cmnd *cmd)
898851b1642SAlok Kataria {
899851b1642SAlok Kataria struct Scsi_Host *host = cmd->device->host;
900851b1642SAlok Kataria struct pvscsi_adapter *adapter = shost_priv(host);
901851b1642SAlok Kataria unsigned long flags;
902851b1642SAlok Kataria bool use_msg;
903851b1642SAlok Kataria
904851b1642SAlok Kataria scmd_printk(KERN_INFO, cmd, "SCSI Host reset\n");
905851b1642SAlok Kataria
906851b1642SAlok Kataria spin_lock_irqsave(&adapter->hw_lock, flags);
907851b1642SAlok Kataria
908851b1642SAlok Kataria use_msg = adapter->use_msg;
909851b1642SAlok Kataria
910851b1642SAlok Kataria if (use_msg) {
911013f69a9SJason Yan adapter->use_msg = false;
912851b1642SAlok Kataria spin_unlock_irqrestore(&adapter->hw_lock, flags);
913851b1642SAlok Kataria
914851b1642SAlok Kataria /*
915851b1642SAlok Kataria * Now that we know that the ISR won't add more work on the
916851b1642SAlok Kataria * workqueue we can safely flush any outstanding work.
917851b1642SAlok Kataria */
918851b1642SAlok Kataria flush_workqueue(adapter->workqueue);
919851b1642SAlok Kataria spin_lock_irqsave(&adapter->hw_lock, flags);
920851b1642SAlok Kataria }
921851b1642SAlok Kataria
922851b1642SAlok Kataria /*
923851b1642SAlok Kataria * We're going to tear down the entire ring structure and set it back
924851b1642SAlok Kataria * up, so stalling new requests until all completions are flushed and
925851b1642SAlok Kataria * the rings are back in place.
926851b1642SAlok Kataria */
927851b1642SAlok Kataria
928851b1642SAlok Kataria pvscsi_process_request_ring(adapter);
929851b1642SAlok Kataria
930851b1642SAlok Kataria ll_adapter_reset(adapter);
931851b1642SAlok Kataria
932851b1642SAlok Kataria /*
933851b1642SAlok Kataria * Now process any completions. Note we do this AFTER adapter reset,
934851b1642SAlok Kataria * which is strange, but stops races where completions get posted
935851b1642SAlok Kataria * between processing the ring and issuing the reset. The backend will
936851b1642SAlok Kataria * not touch the ring memory after reset, so the immediately pre-reset
937851b1642SAlok Kataria * completion ring state is still valid.
938851b1642SAlok Kataria */
939851b1642SAlok Kataria pvscsi_process_completion_ring(adapter);
940851b1642SAlok Kataria
941851b1642SAlok Kataria pvscsi_reset_all(adapter);
942851b1642SAlok Kataria adapter->use_msg = use_msg;
943851b1642SAlok Kataria pvscsi_setup_all_rings(adapter);
944851b1642SAlok Kataria pvscsi_unmask_intr(adapter);
945851b1642SAlok Kataria
946851b1642SAlok Kataria spin_unlock_irqrestore(&adapter->hw_lock, flags);
947851b1642SAlok Kataria
948851b1642SAlok Kataria return SUCCESS;
949851b1642SAlok Kataria }
950851b1642SAlok Kataria
pvscsi_bus_reset(struct scsi_cmnd * cmd)951851b1642SAlok Kataria static int pvscsi_bus_reset(struct scsi_cmnd *cmd)
952851b1642SAlok Kataria {
953851b1642SAlok Kataria struct Scsi_Host *host = cmd->device->host;
954851b1642SAlok Kataria struct pvscsi_adapter *adapter = shost_priv(host);
955851b1642SAlok Kataria unsigned long flags;
956851b1642SAlok Kataria
957851b1642SAlok Kataria scmd_printk(KERN_INFO, cmd, "SCSI Bus reset\n");
958851b1642SAlok Kataria
959851b1642SAlok Kataria /*
960851b1642SAlok Kataria * We don't want to queue new requests for this bus after
961851b1642SAlok Kataria * flushing all pending requests to emulation, since new
962851b1642SAlok Kataria * requests could then sneak in during this bus reset phase,
963851b1642SAlok Kataria * so take the lock now.
964851b1642SAlok Kataria */
965851b1642SAlok Kataria spin_lock_irqsave(&adapter->hw_lock, flags);
966851b1642SAlok Kataria
967851b1642SAlok Kataria pvscsi_process_request_ring(adapter);
968851b1642SAlok Kataria ll_bus_reset(adapter);
969851b1642SAlok Kataria pvscsi_process_completion_ring(adapter);
970851b1642SAlok Kataria
971851b1642SAlok Kataria spin_unlock_irqrestore(&adapter->hw_lock, flags);
972851b1642SAlok Kataria
973851b1642SAlok Kataria return SUCCESS;
974851b1642SAlok Kataria }
975851b1642SAlok Kataria
pvscsi_device_reset(struct scsi_cmnd * cmd)976851b1642SAlok Kataria static int pvscsi_device_reset(struct scsi_cmnd *cmd)
977851b1642SAlok Kataria {
978851b1642SAlok Kataria struct Scsi_Host *host = cmd->device->host;
979851b1642SAlok Kataria struct pvscsi_adapter *adapter = shost_priv(host);
980851b1642SAlok Kataria unsigned long flags;
981851b1642SAlok Kataria
982851b1642SAlok Kataria scmd_printk(KERN_INFO, cmd, "SCSI device reset on scsi%u:%u\n",
983851b1642SAlok Kataria host->host_no, cmd->device->id);
984851b1642SAlok Kataria
985851b1642SAlok Kataria /*
986851b1642SAlok Kataria * We don't want to queue new requests for this device after flushing
987851b1642SAlok Kataria * all pending requests to emulation, since new requests could then
988851b1642SAlok Kataria * sneak in during this device reset phase, so take the lock now.
989851b1642SAlok Kataria */
990851b1642SAlok Kataria spin_lock_irqsave(&adapter->hw_lock, flags);
991851b1642SAlok Kataria
992851b1642SAlok Kataria pvscsi_process_request_ring(adapter);
993851b1642SAlok Kataria ll_device_reset(adapter, cmd->device->id);
994851b1642SAlok Kataria pvscsi_process_completion_ring(adapter);
995851b1642SAlok Kataria
996851b1642SAlok Kataria spin_unlock_irqrestore(&adapter->hw_lock, flags);
997851b1642SAlok Kataria
998851b1642SAlok Kataria return SUCCESS;
999851b1642SAlok Kataria }
1000851b1642SAlok Kataria
1001851b1642SAlok Kataria static struct scsi_host_template pvscsi_template;
1002851b1642SAlok Kataria
pvscsi_info(struct Scsi_Host * host)1003851b1642SAlok Kataria static const char *pvscsi_info(struct Scsi_Host *host)
1004851b1642SAlok Kataria {
1005851b1642SAlok Kataria struct pvscsi_adapter *adapter = shost_priv(host);
1006851b1642SAlok Kataria static char buf[256];
1007851b1642SAlok Kataria
1008851b1642SAlok Kataria sprintf(buf, "VMware PVSCSI storage adapter rev %d, req/cmp/msg rings: "
1009851b1642SAlok Kataria "%u/%u/%u pages, cmd_per_lun=%u", adapter->rev,
1010851b1642SAlok Kataria adapter->req_pages, adapter->cmp_pages, adapter->msg_pages,
1011851b1642SAlok Kataria pvscsi_template.cmd_per_lun);
1012851b1642SAlok Kataria
1013851b1642SAlok Kataria return buf;
1014851b1642SAlok Kataria }
1015851b1642SAlok Kataria
1016851b1642SAlok Kataria static struct scsi_host_template pvscsi_template = {
1017851b1642SAlok Kataria .module = THIS_MODULE,
1018851b1642SAlok Kataria .name = "VMware PVSCSI Host Adapter",
1019851b1642SAlok Kataria .proc_name = "vmw_pvscsi",
1020851b1642SAlok Kataria .info = pvscsi_info,
1021851b1642SAlok Kataria .queuecommand = pvscsi_queue,
1022851b1642SAlok Kataria .this_id = -1,
1023851b1642SAlok Kataria .sg_tablesize = PVSCSI_MAX_NUM_SG_ENTRIES_PER_SEGMENT,
1024851b1642SAlok Kataria .dma_boundary = UINT_MAX,
1025851b1642SAlok Kataria .max_sectors = 0xffff,
102602845560SArvind Kumar .change_queue_depth = pvscsi_change_queue_depth,
1027851b1642SAlok Kataria .eh_abort_handler = pvscsi_abort,
1028851b1642SAlok Kataria .eh_device_reset_handler = pvscsi_device_reset,
1029851b1642SAlok Kataria .eh_bus_reset_handler = pvscsi_bus_reset,
1030851b1642SAlok Kataria .eh_host_reset_handler = pvscsi_host_reset,
1031851b1642SAlok Kataria };
1032851b1642SAlok Kataria
pvscsi_process_msg(const struct pvscsi_adapter * adapter,const struct PVSCSIRingMsgDesc * e)1033851b1642SAlok Kataria static void pvscsi_process_msg(const struct pvscsi_adapter *adapter,
1034851b1642SAlok Kataria const struct PVSCSIRingMsgDesc *e)
1035851b1642SAlok Kataria {
1036851b1642SAlok Kataria struct PVSCSIRingsState *s = adapter->rings_state;
1037851b1642SAlok Kataria struct Scsi_Host *host = adapter->host;
1038851b1642SAlok Kataria struct scsi_device *sdev;
1039851b1642SAlok Kataria
1040851b1642SAlok Kataria printk(KERN_INFO "vmw_pvscsi: msg type: 0x%x - MSG RING: %u/%u (%u) \n",
1041851b1642SAlok Kataria e->type, s->msgProdIdx, s->msgConsIdx, s->msgNumEntriesLog2);
1042851b1642SAlok Kataria
1043851b1642SAlok Kataria BUILD_BUG_ON(PVSCSI_MSG_LAST != 2);
1044851b1642SAlok Kataria
1045851b1642SAlok Kataria if (e->type == PVSCSI_MSG_DEV_ADDED) {
1046851b1642SAlok Kataria struct PVSCSIMsgDescDevStatusChanged *desc;
1047851b1642SAlok Kataria desc = (struct PVSCSIMsgDescDevStatusChanged *)e;
1048851b1642SAlok Kataria
1049851b1642SAlok Kataria printk(KERN_INFO
1050851b1642SAlok Kataria "vmw_pvscsi: msg: device added at scsi%u:%u:%u\n",
1051851b1642SAlok Kataria desc->bus, desc->target, desc->lun[1]);
1052851b1642SAlok Kataria
1053851b1642SAlok Kataria if (!scsi_host_get(host))
1054851b1642SAlok Kataria return;
1055851b1642SAlok Kataria
1056851b1642SAlok Kataria sdev = scsi_device_lookup(host, desc->bus, desc->target,
1057851b1642SAlok Kataria desc->lun[1]);
1058851b1642SAlok Kataria if (sdev) {
1059851b1642SAlok Kataria printk(KERN_INFO "vmw_pvscsi: device already exists\n");
1060851b1642SAlok Kataria scsi_device_put(sdev);
1061851b1642SAlok Kataria } else
1062851b1642SAlok Kataria scsi_add_device(adapter->host, desc->bus,
1063851b1642SAlok Kataria desc->target, desc->lun[1]);
1064851b1642SAlok Kataria
1065851b1642SAlok Kataria scsi_host_put(host);
1066851b1642SAlok Kataria } else if (e->type == PVSCSI_MSG_DEV_REMOVED) {
1067851b1642SAlok Kataria struct PVSCSIMsgDescDevStatusChanged *desc;
1068851b1642SAlok Kataria desc = (struct PVSCSIMsgDescDevStatusChanged *)e;
1069851b1642SAlok Kataria
1070851b1642SAlok Kataria printk(KERN_INFO
1071851b1642SAlok Kataria "vmw_pvscsi: msg: device removed at scsi%u:%u:%u\n",
1072851b1642SAlok Kataria desc->bus, desc->target, desc->lun[1]);
1073851b1642SAlok Kataria
1074851b1642SAlok Kataria if (!scsi_host_get(host))
1075851b1642SAlok Kataria return;
1076851b1642SAlok Kataria
1077851b1642SAlok Kataria sdev = scsi_device_lookup(host, desc->bus, desc->target,
1078851b1642SAlok Kataria desc->lun[1]);
1079851b1642SAlok Kataria if (sdev) {
1080851b1642SAlok Kataria scsi_remove_device(sdev);
1081851b1642SAlok Kataria scsi_device_put(sdev);
1082851b1642SAlok Kataria } else
1083851b1642SAlok Kataria printk(KERN_INFO
1084851b1642SAlok Kataria "vmw_pvscsi: failed to lookup scsi%u:%u:%u\n",
1085851b1642SAlok Kataria desc->bus, desc->target, desc->lun[1]);
1086851b1642SAlok Kataria
1087851b1642SAlok Kataria scsi_host_put(host);
1088851b1642SAlok Kataria }
1089851b1642SAlok Kataria }
1090851b1642SAlok Kataria
pvscsi_msg_pending(const struct pvscsi_adapter * adapter)1091851b1642SAlok Kataria static int pvscsi_msg_pending(const struct pvscsi_adapter *adapter)
1092851b1642SAlok Kataria {
1093851b1642SAlok Kataria struct PVSCSIRingsState *s = adapter->rings_state;
1094851b1642SAlok Kataria
1095851b1642SAlok Kataria return s->msgProdIdx != s->msgConsIdx;
1096851b1642SAlok Kataria }
1097851b1642SAlok Kataria
pvscsi_process_msg_ring(const struct pvscsi_adapter * adapter)1098851b1642SAlok Kataria static void pvscsi_process_msg_ring(const struct pvscsi_adapter *adapter)
1099851b1642SAlok Kataria {
1100851b1642SAlok Kataria struct PVSCSIRingsState *s = adapter->rings_state;
1101851b1642SAlok Kataria struct PVSCSIRingMsgDesc *ring = adapter->msg_ring;
1102851b1642SAlok Kataria u32 msg_entries = s->msgNumEntriesLog2;
1103851b1642SAlok Kataria
1104851b1642SAlok Kataria while (pvscsi_msg_pending(adapter)) {
1105851b1642SAlok Kataria struct PVSCSIRingMsgDesc *e = ring + (s->msgConsIdx &
1106851b1642SAlok Kataria MASK(msg_entries));
1107851b1642SAlok Kataria
1108851b1642SAlok Kataria barrier();
1109851b1642SAlok Kataria pvscsi_process_msg(adapter, e);
1110851b1642SAlok Kataria barrier();
1111851b1642SAlok Kataria s->msgConsIdx++;
1112851b1642SAlok Kataria }
1113851b1642SAlok Kataria }
1114851b1642SAlok Kataria
pvscsi_msg_workqueue_handler(struct work_struct * data)1115851b1642SAlok Kataria static void pvscsi_msg_workqueue_handler(struct work_struct *data)
1116851b1642SAlok Kataria {
1117851b1642SAlok Kataria struct pvscsi_adapter *adapter;
1118851b1642SAlok Kataria
1119851b1642SAlok Kataria adapter = container_of(data, struct pvscsi_adapter, work);
1120851b1642SAlok Kataria
1121851b1642SAlok Kataria pvscsi_process_msg_ring(adapter);
1122851b1642SAlok Kataria }
1123851b1642SAlok Kataria
pvscsi_setup_msg_workqueue(struct pvscsi_adapter * adapter)1124851b1642SAlok Kataria static int pvscsi_setup_msg_workqueue(struct pvscsi_adapter *adapter)
1125851b1642SAlok Kataria {
1126851b1642SAlok Kataria char name[32];
1127851b1642SAlok Kataria
1128851b1642SAlok Kataria if (!pvscsi_use_msg)
1129851b1642SAlok Kataria return 0;
1130851b1642SAlok Kataria
1131851b1642SAlok Kataria pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_COMMAND,
1132851b1642SAlok Kataria PVSCSI_CMD_SETUP_MSG_RING);
1133851b1642SAlok Kataria
1134851b1642SAlok Kataria if (pvscsi_reg_read(adapter, PVSCSI_REG_OFFSET_COMMAND_STATUS) == -1)
1135851b1642SAlok Kataria return 0;
1136851b1642SAlok Kataria
1137851b1642SAlok Kataria snprintf(name, sizeof(name),
1138851b1642SAlok Kataria "vmw_pvscsi_wq_%u", adapter->host->host_no);
1139851b1642SAlok Kataria
1140851b1642SAlok Kataria adapter->workqueue = create_singlethread_workqueue(name);
1141851b1642SAlok Kataria if (!adapter->workqueue) {
1142851b1642SAlok Kataria printk(KERN_ERR "vmw_pvscsi: failed to create work queue\n");
1143851b1642SAlok Kataria return 0;
1144851b1642SAlok Kataria }
1145851b1642SAlok Kataria INIT_WORK(&adapter->work, pvscsi_msg_workqueue_handler);
1146851b1642SAlok Kataria
1147851b1642SAlok Kataria return 1;
1148851b1642SAlok Kataria }
1149851b1642SAlok Kataria
pvscsi_setup_req_threshold(struct pvscsi_adapter * adapter,bool enable)11502a815b5aSRishi Mehta static bool pvscsi_setup_req_threshold(struct pvscsi_adapter *adapter,
11512a815b5aSRishi Mehta bool enable)
11522a815b5aSRishi Mehta {
11532a815b5aSRishi Mehta u32 val;
11542a815b5aSRishi Mehta
11552a815b5aSRishi Mehta if (!pvscsi_use_req_threshold)
11562a815b5aSRishi Mehta return false;
11572a815b5aSRishi Mehta
11582a815b5aSRishi Mehta pvscsi_reg_write(adapter, PVSCSI_REG_OFFSET_COMMAND,
11592a815b5aSRishi Mehta PVSCSI_CMD_SETUP_REQCALLTHRESHOLD);
11602a815b5aSRishi Mehta val = pvscsi_reg_read(adapter, PVSCSI_REG_OFFSET_COMMAND_STATUS);
11612a815b5aSRishi Mehta if (val == -1) {
11622a815b5aSRishi Mehta printk(KERN_INFO "vmw_pvscsi: device does not support req_threshold\n");
11632a815b5aSRishi Mehta return false;
11642a815b5aSRishi Mehta } else {
11652a815b5aSRishi Mehta struct PVSCSICmdDescSetupReqCall cmd_msg = { 0 };
11662a815b5aSRishi Mehta cmd_msg.enable = enable;
11672a815b5aSRishi Mehta printk(KERN_INFO
11682a815b5aSRishi Mehta "vmw_pvscsi: %sabling reqCallThreshold\n",
11692a815b5aSRishi Mehta enable ? "en" : "dis");
11702a815b5aSRishi Mehta pvscsi_write_cmd_desc(adapter,
11712a815b5aSRishi Mehta PVSCSI_CMD_SETUP_REQCALLTHRESHOLD,
11722a815b5aSRishi Mehta &cmd_msg, sizeof(cmd_msg));
11732a815b5aSRishi Mehta return pvscsi_reg_read(adapter,
11742a815b5aSRishi Mehta PVSCSI_REG_OFFSET_COMMAND_STATUS) != 0;
11752a815b5aSRishi Mehta }
11762a815b5aSRishi Mehta }
11772a815b5aSRishi Mehta
pvscsi_isr(int irq,void * devp)1178851b1642SAlok Kataria static irqreturn_t pvscsi_isr(int irq, void *devp)
1179851b1642SAlok Kataria {
1180851b1642SAlok Kataria struct pvscsi_adapter *adapter = devp;
1181851b1642SAlok Kataria unsigned long flags;
1182851b1642SAlok Kataria
1183851b1642SAlok Kataria spin_lock_irqsave(&adapter->hw_lock, flags);
1184851b1642SAlok Kataria pvscsi_process_completion_ring(adapter);
1185851b1642SAlok Kataria if (adapter->use_msg && pvscsi_msg_pending(adapter))
1186851b1642SAlok Kataria queue_work(adapter->workqueue, &adapter->work);
1187851b1642SAlok Kataria spin_unlock_irqrestore(&adapter->hw_lock, flags);
11882e48e349SChristoph Hellwig
11892e48e349SChristoph Hellwig return IRQ_HANDLED;
1190851b1642SAlok Kataria }
1191851b1642SAlok Kataria
pvscsi_shared_isr(int irq,void * devp)11922e48e349SChristoph Hellwig static irqreturn_t pvscsi_shared_isr(int irq, void *devp)
11932e48e349SChristoph Hellwig {
11942e48e349SChristoph Hellwig struct pvscsi_adapter *adapter = devp;
11952e48e349SChristoph Hellwig u32 val = pvscsi_read_intr_status(adapter);
11962e48e349SChristoph Hellwig
11972e48e349SChristoph Hellwig if (!(val & PVSCSI_INTR_ALL_SUPPORTED))
11982e48e349SChristoph Hellwig return IRQ_NONE;
11992e48e349SChristoph Hellwig pvscsi_write_intr_status(devp, val);
12002e48e349SChristoph Hellwig return pvscsi_isr(irq, devp);
1201851b1642SAlok Kataria }
1202851b1642SAlok Kataria
pvscsi_free_sgls(const struct pvscsi_adapter * adapter)1203851b1642SAlok Kataria static void pvscsi_free_sgls(const struct pvscsi_adapter *adapter)
1204851b1642SAlok Kataria {
1205851b1642SAlok Kataria struct pvscsi_ctx *ctx = adapter->cmd_map;
1206851b1642SAlok Kataria unsigned i;
1207851b1642SAlok Kataria
1208851b1642SAlok Kataria for (i = 0; i < adapter->req_depth; ++i, ++ctx)
1209851b1642SAlok Kataria free_pages((unsigned long)ctx->sgl, get_order(SGL_SIZE));
1210851b1642SAlok Kataria }
1211851b1642SAlok Kataria
pvscsi_shutdown_intr(struct pvscsi_adapter * adapter)1212851b1642SAlok Kataria static void pvscsi_shutdown_intr(struct pvscsi_adapter *adapter)
1213851b1642SAlok Kataria {
12142e48e349SChristoph Hellwig free_irq(pci_irq_vector(adapter->dev, 0), adapter);
12152e48e349SChristoph Hellwig pci_free_irq_vectors(adapter->dev);
1216851b1642SAlok Kataria }
1217851b1642SAlok Kataria
pvscsi_release_resources(struct pvscsi_adapter * adapter)1218851b1642SAlok Kataria static void pvscsi_release_resources(struct pvscsi_adapter *adapter)
1219851b1642SAlok Kataria {
1220851b1642SAlok Kataria if (adapter->workqueue)
1221851b1642SAlok Kataria destroy_workqueue(adapter->workqueue);
1222851b1642SAlok Kataria
1223851b1642SAlok Kataria if (adapter->mmioBase)
1224851b1642SAlok Kataria pci_iounmap(adapter->dev, adapter->mmioBase);
1225851b1642SAlok Kataria
1226851b1642SAlok Kataria pci_release_regions(adapter->dev);
1227851b1642SAlok Kataria
1228851b1642SAlok Kataria if (adapter->cmd_map) {
1229851b1642SAlok Kataria pvscsi_free_sgls(adapter);
1230851b1642SAlok Kataria kfree(adapter->cmd_map);
1231851b1642SAlok Kataria }
1232851b1642SAlok Kataria
1233851b1642SAlok Kataria if (adapter->rings_state)
12349b7ca6c2SChristoph Hellwig dma_free_coherent(&adapter->dev->dev, PAGE_SIZE,
1235851b1642SAlok Kataria adapter->rings_state, adapter->ringStatePA);
1236851b1642SAlok Kataria
1237851b1642SAlok Kataria if (adapter->req_ring)
12389b7ca6c2SChristoph Hellwig dma_free_coherent(&adapter->dev->dev,
1239851b1642SAlok Kataria adapter->req_pages * PAGE_SIZE,
1240851b1642SAlok Kataria adapter->req_ring, adapter->reqRingPA);
1241851b1642SAlok Kataria
1242851b1642SAlok Kataria if (adapter->cmp_ring)
12439b7ca6c2SChristoph Hellwig dma_free_coherent(&adapter->dev->dev,
1244851b1642SAlok Kataria adapter->cmp_pages * PAGE_SIZE,
1245851b1642SAlok Kataria adapter->cmp_ring, adapter->cmpRingPA);
1246851b1642SAlok Kataria
1247851b1642SAlok Kataria if (adapter->msg_ring)
12489b7ca6c2SChristoph Hellwig dma_free_coherent(&adapter->dev->dev,
1249851b1642SAlok Kataria adapter->msg_pages * PAGE_SIZE,
1250851b1642SAlok Kataria adapter->msg_ring, adapter->msgRingPA);
1251851b1642SAlok Kataria }
1252851b1642SAlok Kataria
1253851b1642SAlok Kataria /*
1254851b1642SAlok Kataria * Allocate scatter gather lists.
1255851b1642SAlok Kataria *
1256851b1642SAlok Kataria * These are statically allocated. Trying to be clever was not worth it.
1257851b1642SAlok Kataria *
125842b2aa86SJustin P. Mattock * Dynamic allocation can fail, and we can't go deep into the memory
1259851b1642SAlok Kataria * allocator, since we're a SCSI driver, and trying too hard to allocate
1260851b1642SAlok Kataria * memory might generate disk I/O. We also don't want to fail disk I/O
1261851b1642SAlok Kataria * in that case because we can't get an allocation - the I/O could be
1262851b1642SAlok Kataria * trying to swap out data to free memory. Since that is pathological,
1263851b1642SAlok Kataria * just use a statically allocated scatter list.
1264851b1642SAlok Kataria *
1265851b1642SAlok Kataria */
pvscsi_allocate_sg(struct pvscsi_adapter * adapter)12666f039790SGreg Kroah-Hartman static int pvscsi_allocate_sg(struct pvscsi_adapter *adapter)
1267851b1642SAlok Kataria {
1268851b1642SAlok Kataria struct pvscsi_ctx *ctx;
1269851b1642SAlok Kataria int i;
1270851b1642SAlok Kataria
1271851b1642SAlok Kataria ctx = adapter->cmd_map;
1272851b1642SAlok Kataria BUILD_BUG_ON(sizeof(struct pvscsi_sg_list) > SGL_SIZE);
1273851b1642SAlok Kataria
1274851b1642SAlok Kataria for (i = 0; i < adapter->req_depth; ++i, ++ctx) {
1275851b1642SAlok Kataria ctx->sgl = (void *)__get_free_pages(GFP_KERNEL,
1276851b1642SAlok Kataria get_order(SGL_SIZE));
1277851b1642SAlok Kataria ctx->sglPA = 0;
1278851b1642SAlok Kataria BUG_ON(!IS_ALIGNED(((unsigned long)ctx->sgl), PAGE_SIZE));
1279851b1642SAlok Kataria if (!ctx->sgl) {
1280851b1642SAlok Kataria for (; i >= 0; --i, --ctx) {
1281851b1642SAlok Kataria free_pages((unsigned long)ctx->sgl,
1282851b1642SAlok Kataria get_order(SGL_SIZE));
1283851b1642SAlok Kataria ctx->sgl = NULL;
1284851b1642SAlok Kataria }
1285851b1642SAlok Kataria return -ENOMEM;
1286851b1642SAlok Kataria }
1287851b1642SAlok Kataria }
1288851b1642SAlok Kataria
1289851b1642SAlok Kataria return 0;
1290851b1642SAlok Kataria }
1291851b1642SAlok Kataria
1292a9310735SArvind Kumar /*
1293a9310735SArvind Kumar * Query the device, fetch the config info and return the
1294a9310735SArvind Kumar * maximum number of targets on the adapter. In case of
1295a9310735SArvind Kumar * failure due to any reason return default i.e. 16.
1296a9310735SArvind Kumar */
pvscsi_get_max_targets(struct pvscsi_adapter * adapter)1297a9310735SArvind Kumar static u32 pvscsi_get_max_targets(struct pvscsi_adapter *adapter)
1298a9310735SArvind Kumar {
1299a9310735SArvind Kumar struct PVSCSICmdDescConfigCmd cmd;
1300a9310735SArvind Kumar struct PVSCSIConfigPageHeader *header;
1301a9310735SArvind Kumar struct device *dev;
1302a9310735SArvind Kumar dma_addr_t configPagePA;
1303a9310735SArvind Kumar void *config_page;
1304a9310735SArvind Kumar u32 numPhys = 16;
1305a9310735SArvind Kumar
1306a9310735SArvind Kumar dev = pvscsi_dev(adapter);
13079b7ca6c2SChristoph Hellwig config_page = dma_alloc_coherent(&adapter->dev->dev, PAGE_SIZE,
13089b7ca6c2SChristoph Hellwig &configPagePA, GFP_KERNEL);
1309a9310735SArvind Kumar if (!config_page) {
1310a9310735SArvind Kumar dev_warn(dev, "vmw_pvscsi: failed to allocate memory for config page\n");
1311a9310735SArvind Kumar goto exit;
1312a9310735SArvind Kumar }
1313a9310735SArvind Kumar BUG_ON(configPagePA & ~PAGE_MASK);
1314a9310735SArvind Kumar
1315a9310735SArvind Kumar /* Fetch config info from the device. */
1316a9310735SArvind Kumar cmd.configPageAddress = ((u64)PVSCSI_CONFIG_CONTROLLER_ADDRESS) << 32;
1317a9310735SArvind Kumar cmd.configPageNum = PVSCSI_CONFIG_PAGE_CONTROLLER;
1318a9310735SArvind Kumar cmd.cmpAddr = configPagePA;
1319a9310735SArvind Kumar cmd._pad = 0;
1320a9310735SArvind Kumar
1321a9310735SArvind Kumar /*
1322a9310735SArvind Kumar * Mark the completion page header with error values. If the device
1323a9310735SArvind Kumar * completes the command successfully, it sets the status values to
1324a9310735SArvind Kumar * indicate success.
1325a9310735SArvind Kumar */
1326a9310735SArvind Kumar header = config_page;
1327a9310735SArvind Kumar header->hostStatus = BTSTAT_INVPARAM;
1328a9310735SArvind Kumar header->scsiStatus = SDSTAT_CHECK;
1329a9310735SArvind Kumar
1330a9310735SArvind Kumar pvscsi_write_cmd_desc(adapter, PVSCSI_CMD_CONFIG, &cmd, sizeof cmd);
1331a9310735SArvind Kumar
1332a9310735SArvind Kumar if (header->hostStatus == BTSTAT_SUCCESS &&
1333a9310735SArvind Kumar header->scsiStatus == SDSTAT_GOOD) {
1334a9310735SArvind Kumar struct PVSCSIConfigPageController *config;
1335a9310735SArvind Kumar
1336a9310735SArvind Kumar config = config_page;
1337a9310735SArvind Kumar numPhys = config->numPhys;
1338a9310735SArvind Kumar } else
1339a9310735SArvind Kumar dev_warn(dev, "vmw_pvscsi: PVSCSI_CMD_CONFIG failed. hostStatus = 0x%x, scsiStatus = 0x%x\n",
1340a9310735SArvind Kumar header->hostStatus, header->scsiStatus);
13419b7ca6c2SChristoph Hellwig dma_free_coherent(&adapter->dev->dev, PAGE_SIZE, config_page,
13429b7ca6c2SChristoph Hellwig configPagePA);
1343a9310735SArvind Kumar exit:
1344a9310735SArvind Kumar return numPhys;
1345a9310735SArvind Kumar }
1346a9310735SArvind Kumar
pvscsi_probe(struct pci_dev * pdev,const struct pci_device_id * id)13476f039790SGreg Kroah-Hartman static int pvscsi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1348851b1642SAlok Kataria {
13492e48e349SChristoph Hellwig unsigned int irq_flag = PCI_IRQ_MSIX | PCI_IRQ_MSI | PCI_IRQ_LEGACY;
1350851b1642SAlok Kataria struct pvscsi_adapter *adapter;
135102845560SArvind Kumar struct pvscsi_adapter adapter_temp;
135202845560SArvind Kumar struct Scsi_Host *host = NULL;
1353851b1642SAlok Kataria unsigned int i;
1354851b1642SAlok Kataria int error;
135502845560SArvind Kumar u32 max_id;
1356851b1642SAlok Kataria
1357851b1642SAlok Kataria error = -ENODEV;
1358851b1642SAlok Kataria
1359851b1642SAlok Kataria if (pci_enable_device(pdev))
1360851b1642SAlok Kataria return error;
1361851b1642SAlok Kataria
13629b7ca6c2SChristoph Hellwig if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
1363851b1642SAlok Kataria printk(KERN_INFO "vmw_pvscsi: using 64bit dma\n");
13649b7ca6c2SChristoph Hellwig } else if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
1365851b1642SAlok Kataria printk(KERN_INFO "vmw_pvscsi: using 32bit dma\n");
1366851b1642SAlok Kataria } else {
1367851b1642SAlok Kataria printk(KERN_ERR "vmw_pvscsi: failed to set DMA mask\n");
1368851b1642SAlok Kataria goto out_disable_device;
1369851b1642SAlok Kataria }
1370851b1642SAlok Kataria
137102845560SArvind Kumar /*
137202845560SArvind Kumar * Let's use a temp pvscsi_adapter struct until we find the number of
137302845560SArvind Kumar * targets on the adapter, after that we will switch to the real
137402845560SArvind Kumar * allocated struct.
137502845560SArvind Kumar */
137602845560SArvind Kumar adapter = &adapter_temp;
1377851b1642SAlok Kataria memset(adapter, 0, sizeof(*adapter));
1378851b1642SAlok Kataria adapter->dev = pdev;
1379851b1642SAlok Kataria adapter->rev = pdev->revision;
1380851b1642SAlok Kataria
1381851b1642SAlok Kataria if (pci_request_regions(pdev, "vmw_pvscsi")) {
1382851b1642SAlok Kataria printk(KERN_ERR "vmw_pvscsi: pci memory selection failed\n");
138302845560SArvind Kumar goto out_disable_device;
1384851b1642SAlok Kataria }
1385851b1642SAlok Kataria
1386851b1642SAlok Kataria for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1387851b1642SAlok Kataria if ((pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE_IO))
1388851b1642SAlok Kataria continue;
1389851b1642SAlok Kataria
1390851b1642SAlok Kataria if (pci_resource_len(pdev, i) < PVSCSI_MEM_SPACE_SIZE)
1391851b1642SAlok Kataria continue;
1392851b1642SAlok Kataria
1393851b1642SAlok Kataria break;
1394851b1642SAlok Kataria }
1395851b1642SAlok Kataria
1396851b1642SAlok Kataria if (i == DEVICE_COUNT_RESOURCE) {
1397851b1642SAlok Kataria printk(KERN_ERR
1398851b1642SAlok Kataria "vmw_pvscsi: adapter has no suitable MMIO region\n");
139902845560SArvind Kumar goto out_release_resources_and_disable;
1400851b1642SAlok Kataria }
1401851b1642SAlok Kataria
1402851b1642SAlok Kataria adapter->mmioBase = pci_iomap(pdev, i, PVSCSI_MEM_SPACE_SIZE);
1403851b1642SAlok Kataria
1404851b1642SAlok Kataria if (!adapter->mmioBase) {
1405851b1642SAlok Kataria printk(KERN_ERR
1406851b1642SAlok Kataria "vmw_pvscsi: can't iomap for BAR %d memsize %lu\n",
1407851b1642SAlok Kataria i, PVSCSI_MEM_SPACE_SIZE);
140802845560SArvind Kumar goto out_release_resources_and_disable;
1409851b1642SAlok Kataria }
1410851b1642SAlok Kataria
1411851b1642SAlok Kataria pci_set_master(pdev);
141202845560SArvind Kumar
141302845560SArvind Kumar /*
141402845560SArvind Kumar * Ask the device for max number of targets before deciding the
141502845560SArvind Kumar * default pvscsi_ring_pages value.
141602845560SArvind Kumar */
141702845560SArvind Kumar max_id = pvscsi_get_max_targets(adapter);
141802845560SArvind Kumar printk(KERN_INFO "vmw_pvscsi: max_id: %u\n", max_id);
141902845560SArvind Kumar
142002845560SArvind Kumar if (pvscsi_ring_pages == 0)
142102845560SArvind Kumar /*
142202845560SArvind Kumar * Set the right default value. Up to 16 it is 8, above it is
142302845560SArvind Kumar * max.
142402845560SArvind Kumar */
142502845560SArvind Kumar pvscsi_ring_pages = (max_id > 16) ?
142602845560SArvind Kumar PVSCSI_SETUP_RINGS_MAX_NUM_PAGES :
142702845560SArvind Kumar PVSCSI_DEFAULT_NUM_PAGES_PER_RING;
142802845560SArvind Kumar printk(KERN_INFO
142902845560SArvind Kumar "vmw_pvscsi: setting ring_pages to %d\n",
143002845560SArvind Kumar pvscsi_ring_pages);
143102845560SArvind Kumar
143202845560SArvind Kumar pvscsi_template.can_queue =
143302845560SArvind Kumar min(PVSCSI_MAX_NUM_PAGES_REQ_RING, pvscsi_ring_pages) *
143402845560SArvind Kumar PVSCSI_MAX_NUM_REQ_ENTRIES_PER_PAGE;
143502845560SArvind Kumar pvscsi_template.cmd_per_lun =
143602845560SArvind Kumar min(pvscsi_template.can_queue, pvscsi_cmd_per_lun);
143702845560SArvind Kumar host = scsi_host_alloc(&pvscsi_template, sizeof(struct pvscsi_adapter));
143802845560SArvind Kumar if (!host) {
143902845560SArvind Kumar printk(KERN_ERR "vmw_pvscsi: failed to allocate host\n");
144002845560SArvind Kumar goto out_release_resources_and_disable;
144102845560SArvind Kumar }
144202845560SArvind Kumar
144302845560SArvind Kumar /*
144402845560SArvind Kumar * Let's use the real pvscsi_adapter struct here onwards.
144502845560SArvind Kumar */
144602845560SArvind Kumar adapter = shost_priv(host);
144702845560SArvind Kumar memset(adapter, 0, sizeof(*adapter));
144802845560SArvind Kumar adapter->dev = pdev;
144902845560SArvind Kumar adapter->host = host;
145002845560SArvind Kumar /*
145102845560SArvind Kumar * Copy back what we already have to the allocated adapter struct.
145202845560SArvind Kumar */
145302845560SArvind Kumar adapter->rev = adapter_temp.rev;
145402845560SArvind Kumar adapter->mmioBase = adapter_temp.mmioBase;
145502845560SArvind Kumar
145602845560SArvind Kumar spin_lock_init(&adapter->hw_lock);
145702845560SArvind Kumar host->max_channel = 0;
145802845560SArvind Kumar host->max_lun = 1;
145902845560SArvind Kumar host->max_cmd_len = 16;
146002845560SArvind Kumar host->max_id = max_id;
146102845560SArvind Kumar
1462851b1642SAlok Kataria pci_set_drvdata(pdev, host);
1463851b1642SAlok Kataria
1464851b1642SAlok Kataria ll_adapter_reset(adapter);
1465851b1642SAlok Kataria
1466851b1642SAlok Kataria adapter->use_msg = pvscsi_setup_msg_workqueue(adapter);
1467851b1642SAlok Kataria
1468851b1642SAlok Kataria error = pvscsi_allocate_rings(adapter);
1469851b1642SAlok Kataria if (error) {
1470851b1642SAlok Kataria printk(KERN_ERR "vmw_pvscsi: unable to allocate ring memory\n");
1471851b1642SAlok Kataria goto out_release_resources;
1472851b1642SAlok Kataria }
1473851b1642SAlok Kataria
1474851b1642SAlok Kataria /*
1475851b1642SAlok Kataria * From this point on we should reset the adapter if anything goes
1476851b1642SAlok Kataria * wrong.
1477851b1642SAlok Kataria */
1478851b1642SAlok Kataria pvscsi_setup_all_rings(adapter);
1479851b1642SAlok Kataria
1480851b1642SAlok Kataria adapter->cmd_map = kcalloc(adapter->req_depth,
1481851b1642SAlok Kataria sizeof(struct pvscsi_ctx), GFP_KERNEL);
1482851b1642SAlok Kataria if (!adapter->cmd_map) {
1483851b1642SAlok Kataria printk(KERN_ERR "vmw_pvscsi: failed to allocate memory.\n");
1484851b1642SAlok Kataria error = -ENOMEM;
1485851b1642SAlok Kataria goto out_reset_adapter;
1486851b1642SAlok Kataria }
1487851b1642SAlok Kataria
1488851b1642SAlok Kataria INIT_LIST_HEAD(&adapter->cmd_pool);
1489851b1642SAlok Kataria for (i = 0; i < adapter->req_depth; i++) {
1490851b1642SAlok Kataria struct pvscsi_ctx *ctx = adapter->cmd_map + i;
1491851b1642SAlok Kataria list_add(&ctx->list, &adapter->cmd_pool);
1492851b1642SAlok Kataria }
1493851b1642SAlok Kataria
1494851b1642SAlok Kataria error = pvscsi_allocate_sg(adapter);
1495851b1642SAlok Kataria if (error) {
1496851b1642SAlok Kataria printk(KERN_ERR "vmw_pvscsi: unable to allocate s/g table\n");
1497851b1642SAlok Kataria goto out_reset_adapter;
1498851b1642SAlok Kataria }
1499851b1642SAlok Kataria
15002e48e349SChristoph Hellwig if (pvscsi_disable_msix)
15012e48e349SChristoph Hellwig irq_flag &= ~PCI_IRQ_MSIX;
15022e48e349SChristoph Hellwig if (pvscsi_disable_msi)
15032e48e349SChristoph Hellwig irq_flag &= ~PCI_IRQ_MSI;
15042e48e349SChristoph Hellwig
15052e48e349SChristoph Hellwig error = pci_alloc_irq_vectors(adapter->dev, 1, 1, irq_flag);
1506c527de41SChristoph Hellwig if (error < 0)
15072e48e349SChristoph Hellwig goto out_reset_adapter;
1508851b1642SAlok Kataria
15092a815b5aSRishi Mehta adapter->use_req_threshold = pvscsi_setup_req_threshold(adapter, true);
15102a815b5aSRishi Mehta printk(KERN_DEBUG "vmw_pvscsi: driver-based request coalescing %sabled\n",
15112a815b5aSRishi Mehta adapter->use_req_threshold ? "en" : "dis");
15122a815b5aSRishi Mehta
15132e48e349SChristoph Hellwig if (adapter->dev->msix_enabled || adapter->dev->msi_enabled) {
15142e48e349SChristoph Hellwig printk(KERN_INFO "vmw_pvscsi: using MSI%s\n",
15152e48e349SChristoph Hellwig adapter->dev->msix_enabled ? "-X" : "");
15162e48e349SChristoph Hellwig error = request_irq(pci_irq_vector(pdev, 0), pvscsi_isr,
15172e48e349SChristoph Hellwig 0, "vmw_pvscsi", adapter);
15182e48e349SChristoph Hellwig } else {
15192e48e349SChristoph Hellwig printk(KERN_INFO "vmw_pvscsi: using INTx\n");
15202e48e349SChristoph Hellwig error = request_irq(pci_irq_vector(pdev, 0), pvscsi_shared_isr,
15212e48e349SChristoph Hellwig IRQF_SHARED, "vmw_pvscsi", adapter);
15222e48e349SChristoph Hellwig }
15232e48e349SChristoph Hellwig
1524851b1642SAlok Kataria if (error) {
1525851b1642SAlok Kataria printk(KERN_ERR
1526851b1642SAlok Kataria "vmw_pvscsi: unable to request IRQ: %d\n", error);
1527851b1642SAlok Kataria goto out_reset_adapter;
1528851b1642SAlok Kataria }
1529851b1642SAlok Kataria
1530851b1642SAlok Kataria error = scsi_add_host(host, &pdev->dev);
1531851b1642SAlok Kataria if (error) {
1532851b1642SAlok Kataria printk(KERN_ERR
1533851b1642SAlok Kataria "vmw_pvscsi: scsi_add_host failed: %d\n", error);
1534851b1642SAlok Kataria goto out_reset_adapter;
1535851b1642SAlok Kataria }
1536851b1642SAlok Kataria
1537851b1642SAlok Kataria dev_info(&pdev->dev, "VMware PVSCSI rev %d host #%u\n",
1538851b1642SAlok Kataria adapter->rev, host->host_no);
1539851b1642SAlok Kataria
1540851b1642SAlok Kataria pvscsi_unmask_intr(adapter);
1541851b1642SAlok Kataria
1542851b1642SAlok Kataria scsi_scan_host(host);
1543851b1642SAlok Kataria
1544851b1642SAlok Kataria return 0;
1545851b1642SAlok Kataria
1546851b1642SAlok Kataria out_reset_adapter:
1547851b1642SAlok Kataria ll_adapter_reset(adapter);
1548851b1642SAlok Kataria out_release_resources:
154902f425f8SCathy Avery pvscsi_shutdown_intr(adapter);
1550851b1642SAlok Kataria pvscsi_release_resources(adapter);
1551851b1642SAlok Kataria scsi_host_put(host);
1552851b1642SAlok Kataria out_disable_device:
1553851b1642SAlok Kataria pci_disable_device(pdev);
1554851b1642SAlok Kataria
1555851b1642SAlok Kataria return error;
155602845560SArvind Kumar
155702845560SArvind Kumar out_release_resources_and_disable:
155802f425f8SCathy Avery pvscsi_shutdown_intr(adapter);
155902845560SArvind Kumar pvscsi_release_resources(adapter);
156002845560SArvind Kumar goto out_disable_device;
1561851b1642SAlok Kataria }
1562851b1642SAlok Kataria
__pvscsi_shutdown(struct pvscsi_adapter * adapter)1563851b1642SAlok Kataria static void __pvscsi_shutdown(struct pvscsi_adapter *adapter)
1564851b1642SAlok Kataria {
1565851b1642SAlok Kataria pvscsi_mask_intr(adapter);
1566851b1642SAlok Kataria
1567851b1642SAlok Kataria if (adapter->workqueue)
1568851b1642SAlok Kataria flush_workqueue(adapter->workqueue);
1569851b1642SAlok Kataria
1570851b1642SAlok Kataria pvscsi_shutdown_intr(adapter);
1571851b1642SAlok Kataria
1572851b1642SAlok Kataria pvscsi_process_request_ring(adapter);
1573851b1642SAlok Kataria pvscsi_process_completion_ring(adapter);
1574851b1642SAlok Kataria ll_adapter_reset(adapter);
1575851b1642SAlok Kataria }
1576851b1642SAlok Kataria
pvscsi_shutdown(struct pci_dev * dev)1577851b1642SAlok Kataria static void pvscsi_shutdown(struct pci_dev *dev)
1578851b1642SAlok Kataria {
1579851b1642SAlok Kataria struct Scsi_Host *host = pci_get_drvdata(dev);
1580851b1642SAlok Kataria struct pvscsi_adapter *adapter = shost_priv(host);
1581851b1642SAlok Kataria
1582851b1642SAlok Kataria __pvscsi_shutdown(adapter);
1583851b1642SAlok Kataria }
1584851b1642SAlok Kataria
pvscsi_remove(struct pci_dev * pdev)1585851b1642SAlok Kataria static void pvscsi_remove(struct pci_dev *pdev)
1586851b1642SAlok Kataria {
1587851b1642SAlok Kataria struct Scsi_Host *host = pci_get_drvdata(pdev);
1588851b1642SAlok Kataria struct pvscsi_adapter *adapter = shost_priv(host);
1589851b1642SAlok Kataria
1590851b1642SAlok Kataria scsi_remove_host(host);
1591851b1642SAlok Kataria
1592851b1642SAlok Kataria __pvscsi_shutdown(adapter);
1593851b1642SAlok Kataria pvscsi_release_resources(adapter);
1594851b1642SAlok Kataria
1595851b1642SAlok Kataria scsi_host_put(host);
1596851b1642SAlok Kataria
1597851b1642SAlok Kataria pci_disable_device(pdev);
1598851b1642SAlok Kataria }
1599851b1642SAlok Kataria
1600851b1642SAlok Kataria static struct pci_driver pvscsi_pci_driver = {
1601851b1642SAlok Kataria .name = "vmw_pvscsi",
1602851b1642SAlok Kataria .id_table = pvscsi_pci_tbl,
1603851b1642SAlok Kataria .probe = pvscsi_probe,
16046f039790SGreg Kroah-Hartman .remove = pvscsi_remove,
1605851b1642SAlok Kataria .shutdown = pvscsi_shutdown,
1606851b1642SAlok Kataria };
1607851b1642SAlok Kataria
pvscsi_init(void)1608851b1642SAlok Kataria static int __init pvscsi_init(void)
1609851b1642SAlok Kataria {
1610851b1642SAlok Kataria pr_info("%s - version %s\n",
1611851b1642SAlok Kataria PVSCSI_LINUX_DRIVER_DESC, PVSCSI_DRIVER_VERSION_STRING);
1612851b1642SAlok Kataria return pci_register_driver(&pvscsi_pci_driver);
1613851b1642SAlok Kataria }
1614851b1642SAlok Kataria
pvscsi_exit(void)1615851b1642SAlok Kataria static void __exit pvscsi_exit(void)
1616851b1642SAlok Kataria {
1617851b1642SAlok Kataria pci_unregister_driver(&pvscsi_pci_driver);
1618851b1642SAlok Kataria }
1619851b1642SAlok Kataria
1620851b1642SAlok Kataria module_init(pvscsi_init);
1621851b1642SAlok Kataria module_exit(pvscsi_exit);
1622