blob: bfaba069937f46ceb242e1bfb94c5997f08fcaa8 [file] [log] [blame]
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001/*
2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
4 *
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16 * SOFTWARE.
17 */
18#include <linux/mempool.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/workqueue.h>
22#include <linux/pci.h>
23#include <linux/scatterlist.h>
24#include <linux/skbuff.h>
25#include <linux/spinlock.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
28#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/gfp.h>
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070030#include <scsi/scsi.h>
31#include <scsi/scsi_host.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_cmnd.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/fc/fc_els.h>
36#include <scsi/fc/fc_fcoe.h>
37#include <scsi/libfc.h>
38#include <scsi/fc_frame.h>
39#include "fnic_io.h"
40#include "fnic.h"
41
42const char *fnic_state_str[] = {
43 [FNIC_IN_FC_MODE] = "FNIC_IN_FC_MODE",
44 [FNIC_IN_FC_TRANS_ETH_MODE] = "FNIC_IN_FC_TRANS_ETH_MODE",
45 [FNIC_IN_ETH_MODE] = "FNIC_IN_ETH_MODE",
46 [FNIC_IN_ETH_TRANS_FC_MODE] = "FNIC_IN_ETH_TRANS_FC_MODE",
47};
48
49static const char *fnic_ioreq_state_str[] = {
Hiral Patel14eb5d92013-02-12 17:01:01 -080050 [FNIC_IOREQ_NOT_INITED] = "FNIC_IOREQ_NOT_INITED",
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070051 [FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
52 [FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
53 [FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
54 [FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
55};
56
57static const char *fcpio_status_str[] = {
58 [FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
59 [FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
60 [FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
61 [FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
62 [FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
63 [FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
64 [FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
65 [FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
66 [FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
67 [FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
68 [FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
69 [FCPIO_FW_ERR] = "FCPIO_FW_ERR",
70 [FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
71 [FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
72 [FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
73 [FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
74 [FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
75 [FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
76 [FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
77};
78
79const char *fnic_state_to_str(unsigned int state)
80{
81 if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
82 return "unknown";
83
84 return fnic_state_str[state];
85}
86
87static const char *fnic_ioreq_state_to_str(unsigned int state)
88{
89 if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
90 !fnic_ioreq_state_str[state])
91 return "unknown";
92
93 return fnic_ioreq_state_str[state];
94}
95
96static const char *fnic_fcpio_status_to_str(unsigned int status)
97{
98 if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
99 return "unknown";
100
101 return fcpio_status_str[status];
102}
103
104static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
105
106static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
107 struct scsi_cmnd *sc)
108{
109 u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
110
111 return &fnic->io_req_lock[hash];
112}
113
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -0700114static inline spinlock_t *fnic_io_lock_tag(struct fnic *fnic,
115 int tag)
116{
117 return &fnic->io_req_lock[tag & (FNIC_IO_LOCKS - 1)];
118}
119
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700120/*
121 * Unmap the data buffer and sense buffer for an io_req,
122 * also unmap and free the device-private scatter/gather list.
123 */
124static void fnic_release_ioreq_buf(struct fnic *fnic,
125 struct fnic_io_req *io_req,
126 struct scsi_cmnd *sc)
127{
128 if (io_req->sgl_list_pa)
129 pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
130 sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
131 PCI_DMA_TODEVICE);
132 scsi_dma_unmap(sc);
133
134 if (io_req->sgl_cnt)
135 mempool_free(io_req->sgl_list_alloc,
136 fnic->io_sgl_pool[io_req->sgl_type]);
137 if (io_req->sense_buf_pa)
138 pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
139 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
140}
141
142/* Free up Copy Wq descriptors. Called with copy_wq lock held */
143static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
144{
145 /* if no Ack received from firmware, then nothing to clean */
146 if (!fnic->fw_ack_recd[0])
147 return 1;
148
149 /*
150 * Update desc_available count based on number of freed descriptors
151 * Account for wraparound
152 */
153 if (wq->to_clean_index <= fnic->fw_ack_index[0])
154 wq->ring.desc_avail += (fnic->fw_ack_index[0]
155 - wq->to_clean_index + 1);
156 else
157 wq->ring.desc_avail += (wq->ring.desc_count
158 - wq->to_clean_index
159 + fnic->fw_ack_index[0] + 1);
160
161 /*
162 * just bump clean index to ack_index+1 accounting for wraparound
163 * this will essentially free up all descriptors between
164 * to_clean_index and fw_ack_index, both inclusive
165 */
166 wq->to_clean_index =
167 (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
168
169 /* we have processed the acks received so far */
170 fnic->fw_ack_recd[0] = 0;
171 return 0;
172}
173
174
Hiral Patel03298552013-02-12 17:00:58 -0800175/**
176 * __fnic_set_state_flags
177 * Sets/Clears bits in fnic's state_flags
178 **/
179void
180__fnic_set_state_flags(struct fnic *fnic, unsigned long st_flags,
181 unsigned long clearbits)
182{
183 struct Scsi_Host *host = fnic->lport->host;
184 int sh_locked = spin_is_locked(host->host_lock);
185 unsigned long flags = 0;
186
187 if (!sh_locked)
188 spin_lock_irqsave(host->host_lock, flags);
189
190 if (clearbits)
191 fnic->state_flags &= ~st_flags;
192 else
193 fnic->state_flags |= st_flags;
194
195 if (!sh_locked)
196 spin_unlock_irqrestore(host->host_lock, flags);
197
198 return;
199}
200
201
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700202/*
203 * fnic_fw_reset_handler
204 * Routine to send reset msg to fw
205 */
206int fnic_fw_reset_handler(struct fnic *fnic)
207{
208 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
209 int ret = 0;
210 unsigned long flags;
211
Hiral Patel03298552013-02-12 17:00:58 -0800212 /* indicate fwreset to io path */
213 fnic_set_state_flags(fnic, FNIC_FLAGS_FWRESET);
214
Joe Eykholt78112e52009-11-03 11:49:22 -0800215 skb_queue_purge(&fnic->frame_queue);
216 skb_queue_purge(&fnic->tx_queue);
217
Hiral Patel03298552013-02-12 17:00:58 -0800218 /* wait for io cmpl */
219 while (atomic_read(&fnic->in_flight))
220 schedule_timeout(msecs_to_jiffies(1));
221
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700222 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
223
224 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
225 free_wq_copy_descs(fnic, wq);
226
227 if (!vnic_wq_copy_desc_avail(wq))
228 ret = -EAGAIN;
Hiral Patel67125b02013-09-12 17:45:42 -0700229 else {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700230 fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
Hiral Patel67125b02013-09-12 17:45:42 -0700231 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
232 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
233 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
234 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
235 atomic64_read(
236 &fnic->fnic_stats.fw_stats.active_fw_reqs));
237 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700238
239 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
240
Hiral Patel67125b02013-09-12 17:45:42 -0700241 if (!ret) {
242 atomic64_inc(&fnic->fnic_stats.reset_stats.fw_resets);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700243 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
244 "Issued fw reset\n");
Hiral Patel67125b02013-09-12 17:45:42 -0700245 } else {
Hiral Patel03298552013-02-12 17:00:58 -0800246 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700247 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
248 "Failed to issue fw reset\n");
Hiral Patel03298552013-02-12 17:00:58 -0800249 }
250
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700251 return ret;
252}
253
254
255/*
256 * fnic_flogi_reg_handler
257 * Routine to send flogi register msg to fw
258 */
Joe Eykholt78112e52009-11-03 11:49:22 -0800259int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700260{
261 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Joe Eykholt78112e52009-11-03 11:49:22 -0800262 enum fcpio_flogi_reg_format_type format;
263 struct fc_lport *lp = fnic->lport;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700264 u8 gw_mac[ETH_ALEN];
265 int ret = 0;
266 unsigned long flags;
267
268 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
269
270 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
271 free_wq_copy_descs(fnic, wq);
272
273 if (!vnic_wq_copy_desc_avail(wq)) {
274 ret = -EAGAIN;
275 goto flogi_reg_ioreq_end;
276 }
277
Joe Eykholt78112e52009-11-03 11:49:22 -0800278 if (fnic->ctlr.map_dest) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700279 memset(gw_mac, 0xff, ETH_ALEN);
Joe Eykholt78112e52009-11-03 11:49:22 -0800280 format = FCPIO_FLOGI_REG_DEF_DEST;
281 } else {
282 memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
283 format = FCPIO_FLOGI_REG_GW_DEST;
284 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700285
Joe Eykholt78112e52009-11-03 11:49:22 -0800286 if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
287 fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
288 fc_id, gw_mac,
289 fnic->data_src_addr,
290 lp->r_a_tov, lp->e_d_tov);
291 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
292 "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
293 fc_id, fnic->data_src_addr, gw_mac);
294 } else {
295 fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
296 format, fc_id, gw_mac);
297 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
298 "FLOGI reg issued fcid %x map %d dest %pM\n",
299 fc_id, fnic->ctlr.map_dest, gw_mac);
300 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700301
Hiral Patel67125b02013-09-12 17:45:42 -0700302 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
303 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
304 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
305 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
306 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
307
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700308flogi_reg_ioreq_end:
309 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700310 return ret;
311}
312
313/*
314 * fnic_queue_wq_copy_desc
315 * Routine to enqueue a wq copy desc
316 */
317static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
318 struct vnic_wq_copy *wq,
319 struct fnic_io_req *io_req,
320 struct scsi_cmnd *sc,
Roel Kluin87a2d342009-06-23 01:06:40 +0200321 int sg_count)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700322{
323 struct scatterlist *sg;
324 struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
325 struct fc_rport_libfc_priv *rp = rport->dd_data;
326 struct host_sg_desc *desc;
Hiral Patel67125b02013-09-12 17:45:42 -0700327 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700328 unsigned int i;
329 unsigned long intr_flags;
330 int flags;
331 u8 exch_flags;
332 struct scsi_lun fc_lun;
Maurizio Lombardifd6ddfa2015-08-12 17:00:23 +0200333 int r;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700334
335 if (sg_count) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700336 /* For each SGE, create a device desc entry */
337 desc = io_req->sgl_list;
338 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
339 desc->addr = cpu_to_le64(sg_dma_address(sg));
340 desc->len = cpu_to_le32(sg_dma_len(sg));
341 desc->_resvd = 0;
342 desc++;
343 }
344
345 io_req->sgl_list_pa = pci_map_single
346 (fnic->pdev,
347 io_req->sgl_list,
348 sizeof(io_req->sgl_list[0]) * sg_count,
349 PCI_DMA_TODEVICE);
Maurizio Lombardifd6ddfa2015-08-12 17:00:23 +0200350
351 r = pci_dma_mapping_error(fnic->pdev, io_req->sgl_list_pa);
352 if (r) {
353 printk(KERN_ERR "PCI mapping failed with error %d\n", r);
354 return SCSI_MLQUEUE_HOST_BUSY;
355 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700356 }
357
358 io_req->sense_buf_pa = pci_map_single(fnic->pdev,
359 sc->sense_buffer,
360 SCSI_SENSE_BUFFERSIZE,
361 PCI_DMA_FROMDEVICE);
362
Maurizio Lombardifd6ddfa2015-08-12 17:00:23 +0200363 r = pci_dma_mapping_error(fnic->pdev, io_req->sense_buf_pa);
364 if (r) {
365 pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
366 sizeof(io_req->sgl_list[0]) * sg_count,
367 PCI_DMA_TODEVICE);
368 printk(KERN_ERR "PCI mapping failed with error %d\n", r);
369 return SCSI_MLQUEUE_HOST_BUSY;
370 }
371
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700372 int_to_scsilun(sc->device->lun, &fc_lun);
373
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700374 /* Enqueue the descriptor in the Copy WQ */
375 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
376
377 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
378 free_wq_copy_descs(fnic, wq);
379
380 if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
381 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800382 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
383 "fnic_queue_wq_copy_desc failure - no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -0700384 atomic64_inc(&misc_stats->io_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700385 return SCSI_MLQUEUE_HOST_BUSY;
386 }
387
388 flags = 0;
389 if (sc->sc_data_direction == DMA_FROM_DEVICE)
390 flags = FCPIO_ICMND_RDDATA;
391 else if (sc->sc_data_direction == DMA_TO_DEVICE)
392 flags = FCPIO_ICMND_WRDATA;
393
394 exch_flags = 0;
395 if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
396 (rp->flags & FC_RP_FLAGS_RETRY))
397 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
398
399 fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
400 0, exch_flags, io_req->sgl_cnt,
401 SCSI_SENSE_BUFFERSIZE,
402 io_req->sgl_list_pa,
403 io_req->sense_buf_pa,
404 0, /* scsi cmd ref, always 0 */
Christoph Hellwig50668632014-10-30 14:30:06 +0100405 FCPIO_ICMND_PTA_SIMPLE,
406 /* scsi pri and tag */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700407 flags, /* command flags */
Abhijeet Joglekar4b536622009-10-21 16:28:25 -0700408 sc->cmnd, sc->cmd_len,
409 scsi_bufflen(sc),
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700410 fc_lun.scsi_lun, io_req->port_id,
411 rport->maxframe_size, rp->r_a_tov,
412 rp->e_d_tov);
413
Hiral Patel67125b02013-09-12 17:45:42 -0700414 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
415 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
416 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
417 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
418 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
419
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700420 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
421 return 0;
422}
423
424/*
425 * fnic_queuecommand
426 * Routine to send a scsi cdb
427 * Called with host_lock held and interrupts disabled.
428 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500429static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700430{
Hiral Patel03298552013-02-12 17:00:58 -0800431 struct fc_lport *lp = shost_priv(sc->device->host);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700432 struct fc_rport *rport;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800433 struct fnic_io_req *io_req = NULL;
Hiral Patel03298552013-02-12 17:00:58 -0800434 struct fnic *fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -0700435 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700436 struct vnic_wq_copy *wq;
437 int ret;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800438 u64 cmd_trace;
439 int sg_count = 0;
Hiral Shah41df7b02014-11-10 12:54:36 -0800440 unsigned long flags = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700441 unsigned long ptr;
Hiral Shah41df7b02014-11-10 12:54:36 -0800442 spinlock_t *io_lock = NULL;
Hiral Shahdb196932015-07-14 07:08:57 -0700443 int io_lock_acquired = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700444
Hiral Patel03298552013-02-12 17:00:58 -0800445 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
446 return SCSI_MLQUEUE_HOST_BUSY;
447
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700448 rport = starget_to_rport(scsi_target(sc->device));
449 ret = fc_remote_port_chkready(rport);
450 if (ret) {
Hiral Patel67125b02013-09-12 17:45:42 -0700451 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700452 sc->result = ret;
453 done(sc);
454 return 0;
455 }
456
Satish Kharat1b6ac5e2016-03-18 11:22:50 -0700457 if (rport) {
458 struct fc_rport_libfc_priv *rp = rport->dd_data;
459
460 if (!rp || rp->rp_state != RPORT_ST_READY) {
461 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
462 "returning DID_NO_CONNECT for IO as rport is removed\n");
463 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
464 sc->result = DID_NO_CONNECT<<16;
465 done(sc);
466 return 0;
467 }
Hiral Shah35061e22014-11-10 12:54:34 -0800468 }
469
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700470 if (lp->state != LPORT_ST_READY || !(lp->link_up))
471 return SCSI_MLQUEUE_HOST_BUSY;
472
Hiral Patel03298552013-02-12 17:00:58 -0800473 atomic_inc(&fnic->in_flight);
474
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700475 /*
476 * Release host lock, use driver resource specific locks from here.
477 * Don't re-enable interrupts in case they were disabled prior to the
478 * caller disabling them.
479 */
480 spin_unlock(lp->host->host_lock);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800481 CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
482 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700483
484 /* Get a new io_req for this SCSI IO */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700485 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
486 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -0700487 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700488 ret = SCSI_MLQUEUE_HOST_BUSY;
489 goto out;
490 }
491 memset(io_req, 0, sizeof(*io_req));
492
493 /* Map the data buffer */
494 sg_count = scsi_dma_map(sc);
495 if (sg_count < 0) {
Hiral Patel4d7007b2013-02-12 17:01:02 -0800496 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
497 sc->request->tag, sc, 0, sc->cmnd[0],
498 sg_count, CMD_STATE(sc));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700499 mempool_free(io_req, fnic->io_req_pool);
500 goto out;
501 }
502
503 /* Determine the type of scatter/gather list we need */
504 io_req->sgl_cnt = sg_count;
505 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
506 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
507 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
508
509 if (sg_count) {
510 io_req->sgl_list =
511 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
Abhijeet Joglekar0c79c742011-06-13 21:21:01 -0700512 GFP_ATOMIC);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700513 if (!io_req->sgl_list) {
Hiral Patel67125b02013-09-12 17:45:42 -0700514 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700515 ret = SCSI_MLQUEUE_HOST_BUSY;
516 scsi_dma_unmap(sc);
517 mempool_free(io_req, fnic->io_req_pool);
518 goto out;
519 }
520
521 /* Cache sgl list allocated address before alignment */
522 io_req->sgl_list_alloc = io_req->sgl_list;
523 ptr = (unsigned long) io_req->sgl_list;
524 if (ptr % FNIC_SG_DESC_ALIGN) {
525 io_req->sgl_list = (struct host_sg_desc *)
526 (((unsigned long) ptr
527 + FNIC_SG_DESC_ALIGN - 1)
528 & ~(FNIC_SG_DESC_ALIGN - 1));
529 }
530 }
531
Hiral Shah41df7b02014-11-10 12:54:36 -0800532 /*
533 * Will acquire lock defore setting to IO initialized.
534 */
535
536 io_lock = fnic_io_lock_hash(fnic, sc);
537 spin_lock_irqsave(io_lock, flags);
538
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700539 /* initialize rest of io_req */
Hiral Shahdb196932015-07-14 07:08:57 -0700540 io_lock_acquired = 1;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700541 io_req->port_id = rport->port_id;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800542 io_req->start_time = jiffies;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700543 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
544 CMD_SP(sc) = (char *)io_req;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800545 CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700546 sc->scsi_done = done;
547
548 /* create copy wq desc and enqueue it */
549 wq = &fnic->wq_copy[0];
550 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
551 if (ret) {
552 /*
553 * In case another thread cancelled the request,
554 * refetch the pointer under the lock.
555 */
Hiral Patel4d7007b2013-02-12 17:01:02 -0800556 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
557 sc->request->tag, sc, 0, 0, 0,
558 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700559 io_req = (struct fnic_io_req *)CMD_SP(sc);
560 CMD_SP(sc) = NULL;
561 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
562 spin_unlock_irqrestore(io_lock, flags);
563 if (io_req) {
564 fnic_release_ioreq_buf(fnic, io_req, sc);
565 mempool_free(io_req, fnic->io_req_pool);
566 }
Hiral Shah41df7b02014-11-10 12:54:36 -0800567 atomic_dec(&fnic->in_flight);
568 /* acquire host lock before returning to SCSI */
569 spin_lock(lp->host->host_lock);
570 return ret;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800571 } else {
Hiral Patel67125b02013-09-12 17:45:42 -0700572 atomic64_inc(&fnic_stats->io_stats.active_ios);
573 atomic64_inc(&fnic_stats->io_stats.num_ios);
574 if (atomic64_read(&fnic_stats->io_stats.active_ios) >
575 atomic64_read(&fnic_stats->io_stats.max_active_ios))
576 atomic64_set(&fnic_stats->io_stats.max_active_ios,
577 atomic64_read(&fnic_stats->io_stats.active_ios));
578
Hiral Patel14eb5d92013-02-12 17:01:01 -0800579 /* REVISIT: Use per IO lock in the final code */
580 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700581 }
582out:
Hiral Patel4d7007b2013-02-12 17:01:02 -0800583 cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
584 (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
585 (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
586 sc->cmnd[5]);
587
588 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
589 sc->request->tag, sc, io_req,
590 sg_count, cmd_trace,
591 (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
Hiral Shah41df7b02014-11-10 12:54:36 -0800592
593 /* if only we issued IO, will we have the io lock */
Hiral Shahdb196932015-07-14 07:08:57 -0700594 if (io_lock_acquired)
Hiral Shah41df7b02014-11-10 12:54:36 -0800595 spin_unlock_irqrestore(io_lock, flags);
596
Hiral Patel03298552013-02-12 17:00:58 -0800597 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700598 /* acquire host lock before returning to SCSI */
599 spin_lock(lp->host->host_lock);
600 return ret;
601}
602
Jeff Garzikf2812332010-11-16 02:10:29 -0500603DEF_SCSI_QCMD(fnic_queuecommand)
604
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700605/*
606 * fnic_fcpio_fw_reset_cmpl_handler
607 * Routine to handle fw reset completion
608 */
609static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
610 struct fcpio_fw_req *desc)
611{
612 u8 type;
613 u8 hdr_status;
614 struct fcpio_tag tag;
615 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700616 unsigned long flags;
Hiral Patel67125b02013-09-12 17:45:42 -0700617 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700618
619 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
620
Hiral Patel67125b02013-09-12 17:45:42 -0700621 atomic64_inc(&reset_stats->fw_reset_completions);
622
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700623 /* Clean up all outstanding io requests */
624 fnic_cleanup_io(fnic, SCSI_NO_TAG);
625
Hiral Patel67125b02013-09-12 17:45:42 -0700626 atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
627 atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
628
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700629 spin_lock_irqsave(&fnic->fnic_lock, flags);
630
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700631 /* fnic should be in FC_TRANS_ETH_MODE */
632 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
633 /* Check status of reset completion */
634 if (!hdr_status) {
635 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
636 "reset cmpl success\n");
637 /* Ready to send flogi out */
638 fnic->state = FNIC_IN_ETH_MODE;
639 } else {
640 FNIC_SCSI_DBG(KERN_DEBUG,
641 fnic->lport->host,
642 "fnic fw_reset : failed %s\n",
643 fnic_fcpio_status_to_str(hdr_status));
644
645 /*
646 * Unable to change to eth mode, cannot send out flogi
647 * Change state to fc mode, so that subsequent Flogi
648 * requests from libFC will cause more attempts to
649 * reset the firmware. Free the cached flogi
650 */
651 fnic->state = FNIC_IN_FC_MODE;
Hiral Patel67125b02013-09-12 17:45:42 -0700652 atomic64_inc(&reset_stats->fw_reset_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700653 ret = -1;
654 }
655 } else {
656 FNIC_SCSI_DBG(KERN_DEBUG,
657 fnic->lport->host,
658 "Unexpected state %s while processing"
659 " reset cmpl\n", fnic_state_to_str(fnic->state));
Hiral Patel67125b02013-09-12 17:45:42 -0700660 atomic64_inc(&reset_stats->fw_reset_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700661 ret = -1;
662 }
663
664 /* Thread removing device blocks till firmware reset is complete */
665 if (fnic->remove_wait)
666 complete(fnic->remove_wait);
667
668 /*
669 * If fnic is being removed, or fw reset failed
670 * free the flogi frame. Else, send it out
671 */
672 if (fnic->remove_wait || ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700673 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Joe Eykholt78112e52009-11-03 11:49:22 -0800674 skb_queue_purge(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700675 goto reset_cmpl_handler_end;
676 }
677
678 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
679
Joe Eykholt78112e52009-11-03 11:49:22 -0800680 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700681
682 reset_cmpl_handler_end:
Hiral Patel03298552013-02-12 17:00:58 -0800683 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
684
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700685 return ret;
686}
687
688/*
689 * fnic_fcpio_flogi_reg_cmpl_handler
690 * Routine to handle flogi register completion
691 */
692static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
693 struct fcpio_fw_req *desc)
694{
695 u8 type;
696 u8 hdr_status;
697 struct fcpio_tag tag;
698 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700699 unsigned long flags;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700700
701 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
702
703 /* Update fnic state based on status of flogi reg completion */
704 spin_lock_irqsave(&fnic->fnic_lock, flags);
705
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700706 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
707
708 /* Check flogi registration completion status */
709 if (!hdr_status) {
710 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
711 "flog reg succeeded\n");
712 fnic->state = FNIC_IN_FC_MODE;
713 } else {
714 FNIC_SCSI_DBG(KERN_DEBUG,
715 fnic->lport->host,
716 "fnic flogi reg :failed %s\n",
717 fnic_fcpio_status_to_str(hdr_status));
718 fnic->state = FNIC_IN_ETH_MODE;
719 ret = -1;
720 }
721 } else {
722 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
723 "Unexpected fnic state %s while"
724 " processing flogi reg completion\n",
725 fnic_state_to_str(fnic->state));
726 ret = -1;
727 }
728
Joe Eykholt78112e52009-11-03 11:49:22 -0800729 if (!ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700730 if (fnic->stop_rx_link_events) {
731 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
732 goto reg_cmpl_handler_end;
733 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700734 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
735
Joe Eykholt78112e52009-11-03 11:49:22 -0800736 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700737 queue_work(fnic_event_queue, &fnic->frame_work);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700738 } else {
739 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700740 }
741
742reg_cmpl_handler_end:
743 return ret;
744}
745
746static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
747 u16 request_out)
748{
749 if (wq->to_clean_index <= wq->to_use_index) {
750 /* out of range, stale request_out index */
751 if (request_out < wq->to_clean_index ||
752 request_out >= wq->to_use_index)
753 return 0;
754 } else {
755 /* out of range, stale request_out index */
756 if (request_out < wq->to_clean_index &&
757 request_out >= wq->to_use_index)
758 return 0;
759 }
760 /* request_out index is in range */
761 return 1;
762}
763
764
765/*
766 * Mark that ack received and store the Ack index. If there are multiple
767 * acks received before Tx thread cleans it up, the latest value will be
768 * used which is correct behavior. This state should be in the copy Wq
769 * instead of in the fnic
770 */
771static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
772 unsigned int cq_index,
773 struct fcpio_fw_req *desc)
774{
775 struct vnic_wq_copy *wq;
776 u16 request_out = desc->u.ack.request_out;
777 unsigned long flags;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800778 u64 *ox_id_tag = (u64 *)(void *)desc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700779
780 /* mark the ack state */
781 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
782 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
783
Hiral Patel67125b02013-09-12 17:45:42 -0700784 fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700785 if (is_ack_index_in_range(wq, request_out)) {
786 fnic->fw_ack_index[0] = request_out;
787 fnic->fw_ack_recd[0] = 1;
Hiral Patel67125b02013-09-12 17:45:42 -0700788 } else
789 atomic64_inc(
790 &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
791
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700792 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800793 FNIC_TRACE(fnic_fcpio_ack_handler,
794 fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
795 ox_id_tag[4], ox_id_tag[5]);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700796}
797
798/*
799 * fnic_fcpio_icmnd_cmpl_handler
800 * Routine to handle icmnd completions
801 */
802static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
803 struct fcpio_fw_req *desc)
804{
805 u8 type;
806 u8 hdr_status;
807 struct fcpio_tag tag;
808 u32 id;
809 u64 xfer_len = 0;
810 struct fcpio_icmnd_cmpl *icmnd_cmpl;
811 struct fnic_io_req *io_req;
812 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -0700813 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700814 unsigned long flags;
815 spinlock_t *io_lock;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800816 u64 cmd_trace;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800817 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700818
819 /* Decode the cmpl description to get the io_req id */
820 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
821 fcpio_tag_id_dec(&tag, &id);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800822 icmnd_cmpl = &desc->u.icmnd_cmpl;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700823
Hiral Patelfc857992013-09-09 13:31:51 -0700824 if (id >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -0800825 shost_printk(KERN_ERR, fnic->lport->host,
826 "Tag out of range tag %x hdr status = %s\n",
827 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700828 return;
Hiral Patel03298552013-02-12 17:00:58 -0800829 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700830
831 sc = scsi_host_find_tag(fnic->lport->host, id);
832 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800833 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -0700834 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800835 shost_printk(KERN_ERR, fnic->lport->host,
836 "icmnd_cmpl sc is null - "
837 "hdr status = %s tag = 0x%x desc = 0x%p\n",
838 fnic_fcpio_status_to_str(hdr_status), id, desc);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800839 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
840 fnic->lport->host->host_no, id,
841 ((u64)icmnd_cmpl->_resvd0[1] << 16 |
842 (u64)icmnd_cmpl->_resvd0[0]),
843 ((u64)hdr_status << 16 |
844 (u64)icmnd_cmpl->scsi_status << 8 |
845 (u64)icmnd_cmpl->flags), desc,
846 (u64)icmnd_cmpl->residual, 0);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700847 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800848 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700849
850 io_lock = fnic_io_lock_hash(fnic, sc);
851 spin_lock_irqsave(io_lock, flags);
852 io_req = (struct fnic_io_req *)CMD_SP(sc);
853 WARN_ON_ONCE(!io_req);
854 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -0700855 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800856 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700857 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800858 shost_printk(KERN_ERR, fnic->lport->host,
859 "icmnd_cmpl io_req is null - "
860 "hdr status = %s tag = 0x%x sc 0x%p\n",
861 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700862 return;
863 }
Hiral Patel14eb5d92013-02-12 17:01:01 -0800864 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700865
866 /* firmware completed the io */
867 io_req->io_completed = 1;
868
869 /*
870 * if SCSI-ML has already issued abort on this command,
871 * ignore completion of the IO. The abts path will clean it up
872 */
873 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
874 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800875 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
876 switch (hdr_status) {
877 case FCPIO_SUCCESS:
878 CMD_FLAGS(sc) |= FNIC_IO_DONE;
879 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
880 "icmnd_cmpl ABTS pending hdr status = %s "
881 "sc 0x%p scsi_status %x residual %d\n",
882 fnic_fcpio_status_to_str(hdr_status), sc,
883 icmnd_cmpl->scsi_status,
884 icmnd_cmpl->residual);
885 break;
886 case FCPIO_ABORTED:
887 CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
888 break;
889 default:
890 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
891 "icmnd_cmpl abts pending "
892 "hdr status = %s tag = 0x%x sc = 0x%p\n",
893 fnic_fcpio_status_to_str(hdr_status),
894 id, sc);
895 break;
896 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700897 return;
898 }
899
900 /* Mark the IO as complete */
901 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
902
903 icmnd_cmpl = &desc->u.icmnd_cmpl;
904
905 switch (hdr_status) {
906 case FCPIO_SUCCESS:
907 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
908 xfer_len = scsi_bufflen(sc);
909 scsi_set_resid(sc, icmnd_cmpl->residual);
910
911 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
912 xfer_len -= icmnd_cmpl->residual;
913
Hiral Patel67125b02013-09-12 17:45:42 -0700914 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
915 atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700916 break;
917
918 case FCPIO_TIMEOUT: /* request was timed out */
Hiral Patel67125b02013-09-12 17:45:42 -0700919 atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700920 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
921 break;
922
923 case FCPIO_ABORTED: /* request was aborted */
Hiral Patel67125b02013-09-12 17:45:42 -0700924 atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700925 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
926 break;
927
928 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
Hiral Patel67125b02013-09-12 17:45:42 -0700929 atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700930 scsi_set_resid(sc, icmnd_cmpl->residual);
931 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
932 break;
933
934 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
Hiral Patel67125b02013-09-12 17:45:42 -0700935 atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700936 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
937 break;
Hiral Patel67125b02013-09-12 17:45:42 -0700938
939 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
940 atomic64_inc(&fnic_stats->io_stats.io_not_found);
941 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
942 break;
943
944 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
945 atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
946 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
947 break;
948
949 case FCPIO_FW_ERR: /* request was terminated due fw error */
950 atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
951 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
952 break;
953
954 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
955 atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
956 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
957 break;
958
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700959 case FCPIO_INVALID_HEADER: /* header contains invalid data */
960 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
961 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700962 default:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700963 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
964 break;
965 }
966
Maurizio Lombardi14cee5b2016-03-16 14:44:08 +0100967 /* Break link with the SCSI command */
968 CMD_SP(sc) = NULL;
969 CMD_FLAGS(sc) |= FNIC_IO_DONE;
970
971 spin_unlock_irqrestore(io_lock, flags);
972
Hiral Patel67125b02013-09-12 17:45:42 -0700973 if (hdr_status != FCPIO_SUCCESS) {
974 atomic64_inc(&fnic_stats->io_stats.io_failures);
975 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
976 fnic_fcpio_status_to_str(hdr_status));
977 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700978
979 fnic_release_ioreq_buf(fnic, io_req, sc);
980
981 mempool_free(io_req, fnic->io_req_pool);
982
Hiral Patel4d7007b2013-02-12 17:01:02 -0800983 cmd_trace = ((u64)hdr_status << 56) |
984 (u64)icmnd_cmpl->scsi_status << 48 |
985 (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
986 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
987 (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
988
989 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
990 sc->device->host->host_no, id, sc,
991 ((u64)icmnd_cmpl->_resvd0[1] << 56 |
992 (u64)icmnd_cmpl->_resvd0[0] << 48 |
993 jiffies_to_msecs(jiffies - start_time)),
994 desc, cmd_trace,
995 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
996
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700997 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
998 fnic->lport->host_stats.fcp_input_requests++;
999 fnic->fcp_input_bytes += xfer_len;
1000 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
1001 fnic->lport->host_stats.fcp_output_requests++;
1002 fnic->fcp_output_bytes += xfer_len;
1003 } else
1004 fnic->lport->host_stats.fcp_control_requests++;
1005
Hiral Patel67125b02013-09-12 17:45:42 -07001006 atomic64_dec(&fnic_stats->io_stats.active_ios);
1007 if (atomic64_read(&fnic->io_cmpl_skip))
1008 atomic64_dec(&fnic->io_cmpl_skip);
1009 else
1010 atomic64_inc(&fnic_stats->io_stats.io_completions);
1011
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001012 /* Call SCSI completion function to complete the IO */
1013 if (sc->scsi_done)
1014 sc->scsi_done(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001015}
1016
1017/* fnic_fcpio_itmf_cmpl_handler
1018 * Routine to handle itmf completions
1019 */
1020static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1021 struct fcpio_fw_req *desc)
1022{
1023 u8 type;
1024 u8 hdr_status;
1025 struct fcpio_tag tag;
1026 u32 id;
1027 struct scsi_cmnd *sc;
1028 struct fnic_io_req *io_req;
Hiral Patel67125b02013-09-12 17:45:42 -07001029 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1030 struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1031 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1032 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001033 unsigned long flags;
1034 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001035 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001036
1037 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1038 fcpio_tag_id_dec(&tag, &id);
1039
Hiral Patelfc857992013-09-09 13:31:51 -07001040 if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -08001041 shost_printk(KERN_ERR, fnic->lport->host,
1042 "Tag out of range tag %x hdr status = %s\n",
1043 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001044 return;
Hiral Patel03298552013-02-12 17:00:58 -08001045 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001046
1047 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1048 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001049 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -07001050 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001051 shost_printk(KERN_ERR, fnic->lport->host,
1052 "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1053 fnic_fcpio_status_to_str(hdr_status), id);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001054 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001055 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001056 io_lock = fnic_io_lock_hash(fnic, sc);
1057 spin_lock_irqsave(io_lock, flags);
1058 io_req = (struct fnic_io_req *)CMD_SP(sc);
1059 WARN_ON_ONCE(!io_req);
1060 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001061 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001062 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001063 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1064 shost_printk(KERN_ERR, fnic->lport->host,
1065 "itmf_cmpl io_req is null - "
1066 "hdr status = %s tag = 0x%x sc 0x%p\n",
1067 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001068 return;
1069 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001070 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001071
Hiral Patel03298552013-02-12 17:00:58 -08001072 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1073 /* Abort and terminate completion of device reset req */
1074 /* REVISIT : Add asserts about various flags */
1075 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1076 "dev reset abts cmpl recd. id %x status %s\n",
1077 id, fnic_fcpio_status_to_str(hdr_status));
1078 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1079 CMD_ABTS_STATUS(sc) = hdr_status;
1080 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1081 if (io_req->abts_done)
1082 complete(io_req->abts_done);
1083 spin_unlock_irqrestore(io_lock, flags);
1084 } else if (id & FNIC_TAG_ABORT) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001085 /* Completion of abort cmd */
Hiral Patel67125b02013-09-12 17:45:42 -07001086 switch (hdr_status) {
1087 case FCPIO_SUCCESS:
1088 break;
1089 case FCPIO_TIMEOUT:
1090 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1091 atomic64_inc(&abts_stats->abort_fw_timeouts);
1092 else
1093 atomic64_inc(
1094 &term_stats->terminate_fw_timeouts);
1095 break;
Satish Kharat691a8372016-03-18 11:22:48 -07001096 case FCPIO_ITMF_REJECTED:
1097 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1098 "abort reject recd. id %d\n",
1099 (int)(id & FNIC_TAG_MASK));
1100 break;
Hiral Patel67125b02013-09-12 17:45:42 -07001101 case FCPIO_IO_NOT_FOUND:
1102 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1103 atomic64_inc(&abts_stats->abort_io_not_found);
1104 else
1105 atomic64_inc(
1106 &term_stats->terminate_io_not_found);
1107 break;
1108 default:
1109 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1110 atomic64_inc(&abts_stats->abort_failures);
1111 else
1112 atomic64_inc(
1113 &term_stats->terminate_failures);
1114 break;
1115 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001116 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1117 /* This is a late completion. Ignore it */
1118 spin_unlock_irqrestore(io_lock, flags);
1119 return;
1120 }
Satish Kharat691a8372016-03-18 11:22:48 -07001121
Hiral Patel14eb5d92013-02-12 17:01:01 -08001122 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Hiral Patel67125b02013-09-12 17:45:42 -07001123
Satish Kharat691a8372016-03-18 11:22:48 -07001124 /* If the status is IO not found consider it as success */
1125 if (hdr_status == FCPIO_IO_NOT_FOUND)
1126 CMD_ABTS_STATUS(sc) = FCPIO_SUCCESS;
1127 else
1128 CMD_ABTS_STATUS(sc) = hdr_status;
1129
Hiral Patel67125b02013-09-12 17:45:42 -07001130 atomic64_dec(&fnic_stats->io_stats.active_ios);
1131 if (atomic64_read(&fnic->io_cmpl_skip))
1132 atomic64_dec(&fnic->io_cmpl_skip);
1133 else
1134 atomic64_inc(&fnic_stats->io_stats.io_completions);
1135
1136 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1137 atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1138
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001139 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1140 "abts cmpl recd. id %d status %s\n",
1141 (int)(id & FNIC_TAG_MASK),
1142 fnic_fcpio_status_to_str(hdr_status));
1143
1144 /*
1145 * If scsi_eh thread is blocked waiting for abts to complete,
1146 * signal completion to it. IO will be cleaned in the thread
1147 * else clean it in this context
1148 */
1149 if (io_req->abts_done) {
1150 complete(io_req->abts_done);
1151 spin_unlock_irqrestore(io_lock, flags);
1152 } else {
1153 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1154 "abts cmpl, completing IO\n");
1155 CMD_SP(sc) = NULL;
1156 sc->result = (DID_ERROR << 16);
1157
1158 spin_unlock_irqrestore(io_lock, flags);
1159
1160 fnic_release_ioreq_buf(fnic, io_req, sc);
1161 mempool_free(io_req, fnic->io_req_pool);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001162 if (sc->scsi_done) {
1163 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1164 sc->device->host->host_no, id,
1165 sc,
1166 jiffies_to_msecs(jiffies - start_time),
1167 desc,
1168 (((u64)hdr_status << 40) |
1169 (u64)sc->cmnd[0] << 32 |
1170 (u64)sc->cmnd[2] << 24 |
1171 (u64)sc->cmnd[3] << 16 |
1172 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1173 (((u64)CMD_FLAGS(sc) << 32) |
1174 CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001175 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001176 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001177 }
1178
1179 } else if (id & FNIC_TAG_DEV_RST) {
1180 /* Completion of device reset */
1181 CMD_LR_STATUS(sc) = hdr_status;
Hiral Patel03298552013-02-12 17:00:58 -08001182 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1183 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001184 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001185 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1186 sc->device->host->host_no, id, sc,
1187 jiffies_to_msecs(jiffies - start_time),
1188 desc, 0,
1189 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001190 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1191 "Terminate pending "
1192 "dev reset cmpl recd. id %d status %s\n",
1193 (int)(id & FNIC_TAG_MASK),
1194 fnic_fcpio_status_to_str(hdr_status));
1195 return;
1196 }
1197 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1198 /* Need to wait for terminate completion */
1199 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001200 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1201 sc->device->host->host_no, id, sc,
1202 jiffies_to_msecs(jiffies - start_time),
1203 desc, 0,
1204 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001205 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1206 "dev reset cmpl recd after time out. "
1207 "id %d status %s\n",
1208 (int)(id & FNIC_TAG_MASK),
1209 fnic_fcpio_status_to_str(hdr_status));
1210 return;
1211 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001212 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
Hiral Patel03298552013-02-12 17:00:58 -08001213 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001214 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1215 "dev reset cmpl recd. id %d status %s\n",
1216 (int)(id & FNIC_TAG_MASK),
1217 fnic_fcpio_status_to_str(hdr_status));
1218 if (io_req->dr_done)
1219 complete(io_req->dr_done);
1220 spin_unlock_irqrestore(io_lock, flags);
1221
1222 } else {
1223 shost_printk(KERN_ERR, fnic->lport->host,
1224 "Unexpected itmf io state %s tag %x\n",
1225 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1226 spin_unlock_irqrestore(io_lock, flags);
1227 }
1228
1229}
1230
1231/*
1232 * fnic_fcpio_cmpl_handler
1233 * Routine to service the cq for wq_copy
1234 */
1235static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1236 unsigned int cq_index,
1237 struct fcpio_fw_req *desc)
1238{
1239 struct fnic *fnic = vnic_dev_priv(vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001240
1241 switch (desc->hdr.type) {
Hiral Patel67125b02013-09-12 17:45:42 -07001242 case FCPIO_ICMND_CMPL: /* fw completed a command */
1243 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1244 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1245 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1246 case FCPIO_RESET_CMPL: /* fw completed reset */
1247 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1248 break;
1249 default:
1250 break;
1251 }
1252
1253 switch (desc->hdr.type) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001254 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1255 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1256 break;
1257
1258 case FCPIO_ICMND_CMPL: /* fw completed a command */
1259 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1260 break;
1261
1262 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1263 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1264 break;
1265
1266 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
Joe Eykholt78112e52009-11-03 11:49:22 -08001267 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
Hiral Patel03298552013-02-12 17:00:58 -08001268 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001269 break;
1270
1271 case FCPIO_RESET_CMPL: /* fw completed reset */
Hiral Patel03298552013-02-12 17:00:58 -08001272 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001273 break;
1274
1275 default:
1276 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1277 "firmware completion type %d\n",
1278 desc->hdr.type);
1279 break;
1280 }
1281
Hiral Patel03298552013-02-12 17:00:58 -08001282 return 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001283}
1284
1285/*
1286 * fnic_wq_copy_cmpl_handler
1287 * Routine to process wq copy
1288 */
1289int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1290{
1291 unsigned int wq_work_done = 0;
1292 unsigned int i, cq_index;
1293 unsigned int cur_work_done;
1294
1295 for (i = 0; i < fnic->wq_copy_count; i++) {
1296 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1297 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1298 fnic_fcpio_cmpl_handler,
1299 copy_work_to_do);
1300 wq_work_done += cur_work_done;
1301 }
1302 return wq_work_done;
1303}
1304
1305static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1306{
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001307 int i;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001308 struct fnic_io_req *io_req;
1309 unsigned long flags = 0;
1310 struct scsi_cmnd *sc;
1311 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001312 unsigned long start_time = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07001313 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001314
Hiral Patelfc857992013-09-09 13:31:51 -07001315 for (i = 0; i < fnic->fnic_max_tag_id; i++) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001316 if (i == exclude_id)
1317 continue;
1318
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001319 io_lock = fnic_io_lock_tag(fnic, i);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001320 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001321 sc = scsi_host_find_tag(fnic->lport->host, i);
1322 if (!sc) {
1323 spin_unlock_irqrestore(io_lock, flags);
1324 continue;
1325 }
1326
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001327 io_req = (struct fnic_io_req *)CMD_SP(sc);
Hiral Patel03298552013-02-12 17:00:58 -08001328 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1329 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1330 /*
1331 * We will be here only when FW completes reset
1332 * without sending completions for outstanding ios.
1333 */
1334 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1335 if (io_req && io_req->dr_done)
1336 complete(io_req->dr_done);
1337 else if (io_req && io_req->abts_done)
1338 complete(io_req->abts_done);
1339 spin_unlock_irqrestore(io_lock, flags);
1340 continue;
1341 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1342 spin_unlock_irqrestore(io_lock, flags);
1343 continue;
1344 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001345 if (!io_req) {
1346 spin_unlock_irqrestore(io_lock, flags);
1347 goto cleanup_scsi_cmd;
1348 }
1349
1350 CMD_SP(sc) = NULL;
1351
1352 spin_unlock_irqrestore(io_lock, flags);
1353
1354 /*
1355 * If there is a scsi_cmnd associated with this io_req, then
1356 * free the corresponding state
1357 */
Hiral Patel14eb5d92013-02-12 17:01:01 -08001358 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001359 fnic_release_ioreq_buf(fnic, io_req, sc);
1360 mempool_free(io_req, fnic->io_req_pool);
1361
1362cleanup_scsi_cmd:
1363 sc->result = DID_TRANSPORT_DISRUPTED << 16;
Hiral Shah66818662014-04-18 12:28:18 -07001364 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1365 "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1366 __func__, (jiffies - start_time));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001367
Hiral Patel67125b02013-09-12 17:45:42 -07001368 if (atomic64_read(&fnic->io_cmpl_skip))
1369 atomic64_dec(&fnic->io_cmpl_skip);
1370 else
1371 atomic64_inc(&fnic_stats->io_stats.io_completions);
1372
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001373 /* Complete the command to SCSI */
Hiral Patel4d7007b2013-02-12 17:01:02 -08001374 if (sc->scsi_done) {
1375 FNIC_TRACE(fnic_cleanup_io,
1376 sc->device->host->host_no, i, sc,
1377 jiffies_to_msecs(jiffies - start_time),
1378 0, ((u64)sc->cmnd[0] << 32 |
1379 (u64)sc->cmnd[2] << 24 |
1380 (u64)sc->cmnd[3] << 16 |
1381 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1382 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1383
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001384 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001385 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001386 }
1387}
1388
1389void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1390 struct fcpio_host_req *desc)
1391{
1392 u32 id;
1393 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1394 struct fnic_io_req *io_req;
1395 struct scsi_cmnd *sc;
1396 unsigned long flags;
1397 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001398 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001399
1400 /* get the tag reference */
1401 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1402 id &= FNIC_TAG_MASK;
1403
Hiral Patelfc857992013-09-09 13:31:51 -07001404 if (id >= fnic->fnic_max_tag_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001405 return;
1406
1407 sc = scsi_host_find_tag(fnic->lport->host, id);
1408 if (!sc)
1409 return;
1410
1411 io_lock = fnic_io_lock_hash(fnic, sc);
1412 spin_lock_irqsave(io_lock, flags);
1413
1414 /* Get the IO context which this desc refers to */
1415 io_req = (struct fnic_io_req *)CMD_SP(sc);
1416
1417 /* fnic interrupts are turned off by now */
1418
1419 if (!io_req) {
1420 spin_unlock_irqrestore(io_lock, flags);
1421 goto wq_copy_cleanup_scsi_cmd;
1422 }
1423
1424 CMD_SP(sc) = NULL;
1425
1426 spin_unlock_irqrestore(io_lock, flags);
1427
Hiral Patel14eb5d92013-02-12 17:01:01 -08001428 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001429 fnic_release_ioreq_buf(fnic, io_req, sc);
1430 mempool_free(io_req, fnic->io_req_pool);
1431
1432wq_copy_cleanup_scsi_cmd:
1433 sc->result = DID_NO_CONNECT << 16;
1434 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1435 " DID_NO_CONNECT\n");
1436
Hiral Patel4d7007b2013-02-12 17:01:02 -08001437 if (sc->scsi_done) {
1438 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1439 sc->device->host->host_no, id, sc,
1440 jiffies_to_msecs(jiffies - start_time),
1441 0, ((u64)sc->cmnd[0] << 32 |
1442 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1443 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1444 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1445
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001446 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001447 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001448}
1449
1450static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1451 u32 task_req, u8 *fc_lun,
1452 struct fnic_io_req *io_req)
1453{
1454 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001455 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001456 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001457 unsigned long flags;
1458
Hiral Patel03298552013-02-12 17:00:58 -08001459 spin_lock_irqsave(host->host_lock, flags);
1460 if (unlikely(fnic_chk_state_flags_locked(fnic,
1461 FNIC_FLAGS_IO_BLOCKED))) {
1462 spin_unlock_irqrestore(host->host_lock, flags);
1463 return 1;
1464 } else
1465 atomic_inc(&fnic->in_flight);
1466 spin_unlock_irqrestore(host->host_lock, flags);
1467
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001468 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1469
1470 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1471 free_wq_copy_descs(fnic, wq);
1472
1473 if (!vnic_wq_copy_desc_avail(wq)) {
1474 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001475 atomic_dec(&fnic->in_flight);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001476 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001477 "fnic_queue_abort_io_req: failure: no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07001478 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001479 return 1;
1480 }
1481 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1482 0, task_req, tag, fc_lun, io_req->port_id,
1483 fnic->config.ra_tov, fnic->config.ed_tov);
1484
Hiral Patel67125b02013-09-12 17:45:42 -07001485 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1486 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1487 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1488 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1489 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1490
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001491 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001492 atomic_dec(&fnic->in_flight);
1493
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001494 return 0;
1495}
1496
Hiral Patel03298552013-02-12 17:00:58 -08001497static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001498{
1499 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001500 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001501 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001502 struct fnic_io_req *io_req;
1503 spinlock_t *io_lock;
1504 unsigned long flags;
1505 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -07001506 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1507 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001508 struct scsi_lun fc_lun;
1509 enum fnic_ioreq_state old_ioreq_state;
1510
1511 FNIC_SCSI_DBG(KERN_DEBUG,
1512 fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001513 "fnic_rport_exch_reset called portid 0x%06x\n",
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001514 port_id);
1515
1516 if (fnic->in_remove)
1517 return;
1518
Hiral Patelfc857992013-09-09 13:31:51 -07001519 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001520 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001521 io_lock = fnic_io_lock_tag(fnic, tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001522 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001523 sc = scsi_host_find_tag(fnic->lport->host, tag);
1524 if (!sc) {
1525 spin_unlock_irqrestore(io_lock, flags);
1526 continue;
1527 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001528
1529 io_req = (struct fnic_io_req *)CMD_SP(sc);
1530
1531 if (!io_req || io_req->port_id != port_id) {
1532 spin_unlock_irqrestore(io_lock, flags);
1533 continue;
1534 }
1535
Hiral Patel03298552013-02-12 17:00:58 -08001536 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001537 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001538 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1539 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1540 sc);
1541 spin_unlock_irqrestore(io_lock, flags);
1542 continue;
1543 }
1544
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001545 /*
1546 * Found IO that is still pending with firmware and
1547 * belongs to rport that went away
1548 */
1549 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1550 spin_unlock_irqrestore(io_lock, flags);
1551 continue;
1552 }
Hiral Patel03298552013-02-12 17:00:58 -08001553 if (io_req->abts_done) {
1554 shost_printk(KERN_ERR, fnic->lport->host,
1555 "fnic_rport_exch_reset: io_req->abts_done is set "
1556 "state is %s\n",
1557 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1558 }
1559
Hiral Patel14eb5d92013-02-12 17:01:01 -08001560 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1561 shost_printk(KERN_ERR, fnic->lport->host,
1562 "rport_exch_reset "
1563 "IO not yet issued %p tag 0x%x flags "
1564 "%x state %d\n",
1565 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1566 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001567 old_ioreq_state = CMD_STATE(sc);
1568 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1569 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001570 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001571 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001572 abt_tag = (tag | FNIC_TAG_DEV_RST);
1573 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1574 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1575 sc);
1576 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001577
1578 BUG_ON(io_req->abts_done);
1579
1580 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1581 "fnic_rport_reset_exch: Issuing abts\n");
1582
1583 spin_unlock_irqrestore(io_lock, flags);
1584
1585 /* Now queue the abort command to firmware */
1586 int_to_scsilun(sc->device->lun, &fc_lun);
1587
Hiral Patel03298552013-02-12 17:00:58 -08001588 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001589 FCPIO_ITMF_ABT_TASK_TERM,
1590 fc_lun.scsi_lun, io_req)) {
1591 /*
1592 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001593 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001594 * aborted later by scsi_eh, or cleaned up during
1595 * lun reset
1596 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001597 spin_lock_irqsave(io_lock, flags);
1598 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1599 CMD_STATE(sc) = old_ioreq_state;
1600 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001601 } else {
1602 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001603 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1604 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001605 else
1606 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001607 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001608 atomic64_inc(&term_stats->terminates);
1609 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001610 }
1611 }
Hiral Patel67125b02013-09-12 17:45:42 -07001612 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1613 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001614
1615}
1616
1617void fnic_terminate_rport_io(struct fc_rport *rport)
1618{
1619 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001620 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001621 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001622 struct fnic_io_req *io_req;
1623 spinlock_t *io_lock;
1624 unsigned long flags;
1625 struct scsi_cmnd *sc;
1626 struct scsi_lun fc_lun;
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001627 struct fc_rport_libfc_priv *rdata;
1628 struct fc_lport *lport;
1629 struct fnic *fnic;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001630 struct fc_rport *cmd_rport;
Hiral Patel67125b02013-09-12 17:45:42 -07001631 struct reset_stats *reset_stats;
1632 struct terminate_stats *term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001633 enum fnic_ioreq_state old_ioreq_state;
1634
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001635 if (!rport) {
1636 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1637 return;
1638 }
1639 rdata = rport->dd_data;
1640
1641 if (!rdata) {
1642 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1643 return;
1644 }
1645 lport = rdata->local_port;
1646
1647 if (!lport) {
1648 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1649 return;
1650 }
1651 fnic = lport_priv(lport);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001652 FNIC_SCSI_DBG(KERN_DEBUG,
1653 fnic->lport->host, "fnic_terminate_rport_io called"
Hiral Patel03298552013-02-12 17:00:58 -08001654 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1655 rport->port_name, rport->node_name, rport,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001656 rport->port_id);
1657
1658 if (fnic->in_remove)
1659 return;
1660
Hiral Patel67125b02013-09-12 17:45:42 -07001661 reset_stats = &fnic->fnic_stats.reset_stats;
1662 term_stats = &fnic->fnic_stats.term_stats;
1663
Hiral Patelfc857992013-09-09 13:31:51 -07001664 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001665 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001666 io_lock = fnic_io_lock_tag(fnic, tag);
1667 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001668 sc = scsi_host_find_tag(fnic->lport->host, tag);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001669 if (!sc) {
1670 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001671 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001672 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001673
1674 cmd_rport = starget_to_rport(scsi_target(sc->device));
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001675 if (rport != cmd_rport) {
1676 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001677 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001678 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001679
1680 io_req = (struct fnic_io_req *)CMD_SP(sc);
1681
1682 if (!io_req || rport != cmd_rport) {
1683 spin_unlock_irqrestore(io_lock, flags);
1684 continue;
1685 }
1686
Hiral Patel03298552013-02-12 17:00:58 -08001687 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001688 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001689 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1690 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1691 sc);
1692 spin_unlock_irqrestore(io_lock, flags);
1693 continue;
1694 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001695 /*
1696 * Found IO that is still pending with firmware and
1697 * belongs to rport that went away
1698 */
1699 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1700 spin_unlock_irqrestore(io_lock, flags);
1701 continue;
1702 }
Hiral Patel03298552013-02-12 17:00:58 -08001703 if (io_req->abts_done) {
1704 shost_printk(KERN_ERR, fnic->lport->host,
1705 "fnic_terminate_rport_io: io_req->abts_done is set "
1706 "state is %s\n",
1707 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1708 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001709 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1710 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1711 "fnic_terminate_rport_io "
1712 "IO not yet issued %p tag 0x%x flags "
1713 "%x state %d\n",
1714 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1715 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001716 old_ioreq_state = CMD_STATE(sc);
1717 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1718 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001719 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001720 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001721 abt_tag = (tag | FNIC_TAG_DEV_RST);
1722 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1723 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1724 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001725
1726 BUG_ON(io_req->abts_done);
1727
1728 FNIC_SCSI_DBG(KERN_DEBUG,
1729 fnic->lport->host,
1730 "fnic_terminate_rport_io: Issuing abts\n");
1731
1732 spin_unlock_irqrestore(io_lock, flags);
1733
1734 /* Now queue the abort command to firmware */
1735 int_to_scsilun(sc->device->lun, &fc_lun);
1736
Hiral Patel03298552013-02-12 17:00:58 -08001737 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001738 FCPIO_ITMF_ABT_TASK_TERM,
1739 fc_lun.scsi_lun, io_req)) {
1740 /*
1741 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001742 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001743 * aborted later by scsi_eh, or cleaned up during
1744 * lun reset
1745 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001746 spin_lock_irqsave(io_lock, flags);
1747 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1748 CMD_STATE(sc) = old_ioreq_state;
1749 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001750 } else {
1751 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001752 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1753 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001754 else
1755 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001756 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001757 atomic64_inc(&term_stats->terminates);
1758 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001759 }
1760 }
Hiral Patel67125b02013-09-12 17:45:42 -07001761 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1762 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001763
1764}
1765
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001766/*
1767 * This function is exported to SCSI for sending abort cmnds.
1768 * A SCSI IO is represented by a io_req in the driver.
1769 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1770 */
1771int fnic_abort_cmd(struct scsi_cmnd *sc)
1772{
1773 struct fc_lport *lp;
1774 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001775 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001776 struct fc_rport *rport;
1777 spinlock_t *io_lock;
1778 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001779 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001780 int ret = SUCCESS;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001781 u32 task_req = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001782 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07001783 struct fnic_stats *fnic_stats;
1784 struct abort_stats *abts_stats;
1785 struct terminate_stats *term_stats;
Hiral Shah66818662014-04-18 12:28:18 -07001786 enum fnic_ioreq_state old_ioreq_state;
Hiral Patel03298552013-02-12 17:00:58 -08001787 int tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001788 DECLARE_COMPLETION_ONSTACK(tm_done);
1789
1790 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001791 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001792
1793 /* Get local-port, check ready and link up */
1794 lp = shost_priv(sc->device->host);
1795
1796 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07001797 fnic_stats = &fnic->fnic_stats;
1798 abts_stats = &fnic->fnic_stats.abts_stats;
1799 term_stats = &fnic->fnic_stats.term_stats;
1800
Roel Kluin0db6f432010-06-11 16:44:46 -07001801 rport = starget_to_rport(scsi_target(sc->device));
Hiral Patel03298552013-02-12 17:00:58 -08001802 tag = sc->request->tag;
1803 FNIC_SCSI_DBG(KERN_DEBUG,
1804 fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001805 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
Hiral Patel03298552013-02-12 17:00:58 -08001806 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1807
1808 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1809
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001810 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1811 ret = FAILED;
1812 goto fnic_abort_cmd_end;
1813 }
1814
1815 /*
1816 * Avoid a race between SCSI issuing the abort and the device
1817 * completing the command.
1818 *
1819 * If the command is already completed by the fw cmpl code,
1820 * we just return SUCCESS from here. This means that the abort
1821 * succeeded. In the SCSI ML, since the timeout for command has
1822 * happened, the completion wont actually complete the command
1823 * and it will be considered as an aborted command
1824 *
1825 * The CMD_SP will not be cleared except while holding io_req_lock.
1826 */
1827 io_lock = fnic_io_lock_hash(fnic, sc);
1828 spin_lock_irqsave(io_lock, flags);
1829 io_req = (struct fnic_io_req *)CMD_SP(sc);
1830 if (!io_req) {
1831 spin_unlock_irqrestore(io_lock, flags);
1832 goto fnic_abort_cmd_end;
1833 }
1834
1835 io_req->abts_done = &tm_done;
1836
1837 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1838 spin_unlock_irqrestore(io_lock, flags);
1839 goto wait_pending;
1840 }
1841 /*
1842 * Command is still pending, need to abort it
1843 * If the firmware completes the command after this point,
1844 * the completion wont be done till mid-layer, since abort
1845 * has already started.
1846 */
Hiral Shah66818662014-04-18 12:28:18 -07001847 old_ioreq_state = CMD_STATE(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001848 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1849 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1850
1851 spin_unlock_irqrestore(io_lock, flags);
1852
1853 /*
1854 * Check readiness of the remote port. If the path to remote
1855 * port is up, then send abts to the remote port to terminate
1856 * the IO. Else, just locally terminate the IO in the firmware
1857 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001858 if (fc_remote_port_chkready(rport) == 0)
1859 task_req = FCPIO_ITMF_ABT_TASK;
Hiral Patel67125b02013-09-12 17:45:42 -07001860 else {
1861 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001862 task_req = FCPIO_ITMF_ABT_TASK_TERM;
Hiral Patel67125b02013-09-12 17:45:42 -07001863 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001864
1865 /* Now queue the abort command to firmware */
1866 int_to_scsilun(sc->device->lun, &fc_lun);
1867
1868 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1869 fc_lun.scsi_lun, io_req)) {
1870 spin_lock_irqsave(io_lock, flags);
Hiral Shah66818662014-04-18 12:28:18 -07001871 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1872 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001873 io_req = (struct fnic_io_req *)CMD_SP(sc);
1874 if (io_req)
1875 io_req->abts_done = NULL;
1876 spin_unlock_irqrestore(io_lock, flags);
1877 ret = FAILED;
1878 goto fnic_abort_cmd_end;
1879 }
Hiral Patel67125b02013-09-12 17:45:42 -07001880 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001881 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001882 atomic64_inc(&fnic_stats->abts_stats.aborts);
1883 } else {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001884 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001885 atomic64_inc(&fnic_stats->term_stats.terminates);
1886 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001887
1888 /*
1889 * We queued an abort IO, wait for its completion.
1890 * Once the firmware completes the abort command, it will
1891 * wake up this thread.
1892 */
1893 wait_pending:
1894 wait_for_completion_timeout(&tm_done,
1895 msecs_to_jiffies
1896 (2 * fnic->config.ra_tov +
1897 fnic->config.ed_tov));
1898
1899 /* Check the abort status */
1900 spin_lock_irqsave(io_lock, flags);
1901
1902 io_req = (struct fnic_io_req *)CMD_SP(sc);
1903 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001904 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001905 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001906 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001907 ret = FAILED;
1908 goto fnic_abort_cmd_end;
1909 }
1910 io_req->abts_done = NULL;
1911
1912 /* fw did not complete abort, timed out */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001913 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001914 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001915 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel67125b02013-09-12 17:45:42 -07001916 atomic64_inc(&abts_stats->abort_drv_timeouts);
1917 } else {
Hiral Patel67125b02013-09-12 17:45:42 -07001918 atomic64_inc(&term_stats->terminate_drv_timeouts);
1919 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001920 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001921 ret = FAILED;
1922 goto fnic_abort_cmd_end;
1923 }
1924
Anil Chintalapati (achintal)efc7a282014-12-23 19:40:00 +00001925 /* IO out of order */
1926
1927 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1928 spin_unlock_irqrestore(io_lock, flags);
1929 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1930 "Issuing Host reset due to out of order IO\n");
1931
1932 if (fnic_host_reset(sc) == FAILED) {
1933 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1934 "fnic_host_reset failed.\n");
1935 }
1936 ret = FAILED;
1937 goto fnic_abort_cmd_end;
1938 }
1939
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001940 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1941
Satish Kharat691a8372016-03-18 11:22:48 -07001942 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001943 /*
1944 * firmware completed the abort, check the status,
Satish Kharat691a8372016-03-18 11:22:48 -07001945 * free the io_req if successful. If abort fails,
1946 * Device reset will clean the I/O.
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001947 */
Satish Kharat691a8372016-03-18 11:22:48 -07001948 if (CMD_ABTS_STATUS(sc) == FCPIO_SUCCESS)
1949 CMD_SP(sc) = NULL;
1950 else {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001951 ret = FAILED;
Satish Kharat691a8372016-03-18 11:22:48 -07001952 spin_unlock_irqrestore(io_lock, flags);
1953 goto fnic_abort_cmd_end;
1954 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001955
1956 spin_unlock_irqrestore(io_lock, flags);
1957
1958 fnic_release_ioreq_buf(fnic, io_req, sc);
1959 mempool_free(io_req, fnic->io_req_pool);
1960
Satish Kharat691a8372016-03-18 11:22:48 -07001961 if (sc->scsi_done) {
1962 /* Call SCSI completion function to complete the IO */
1963 sc->result = (DID_ABORT << 16);
1964 sc->scsi_done(sc);
1965 }
1966
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001967fnic_abort_cmd_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08001968 FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1969 sc->request->tag, sc,
1970 jiffies_to_msecs(jiffies - start_time),
1971 0, ((u64)sc->cmnd[0] << 32 |
1972 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1973 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1974 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1975
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001976 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel14eb5d92013-02-12 17:01:01 -08001977 "Returning from abort cmd type %x %s\n", task_req,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001978 (ret == SUCCESS) ?
1979 "SUCCESS" : "FAILED");
1980 return ret;
1981}
1982
1983static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1984 struct scsi_cmnd *sc,
1985 struct fnic_io_req *io_req)
1986{
1987 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001988 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001989 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001990 struct scsi_lun fc_lun;
1991 int ret = 0;
1992 unsigned long intr_flags;
1993
Hiral Patel03298552013-02-12 17:00:58 -08001994 spin_lock_irqsave(host->host_lock, intr_flags);
1995 if (unlikely(fnic_chk_state_flags_locked(fnic,
1996 FNIC_FLAGS_IO_BLOCKED))) {
1997 spin_unlock_irqrestore(host->host_lock, intr_flags);
1998 return FAILED;
1999 } else
2000 atomic_inc(&fnic->in_flight);
2001 spin_unlock_irqrestore(host->host_lock, intr_flags);
2002
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002003 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
2004
2005 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
2006 free_wq_copy_descs(fnic, wq);
2007
2008 if (!vnic_wq_copy_desc_avail(wq)) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002009 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2010 "queue_dr_io_req failure - no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07002011 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002012 ret = -EAGAIN;
2013 goto lr_io_req_end;
2014 }
2015
2016 /* fill in the lun info */
2017 int_to_scsilun(sc->device->lun, &fc_lun);
2018
2019 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
2020 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
2021 fc_lun.scsi_lun, io_req->port_id,
2022 fnic->config.ra_tov, fnic->config.ed_tov);
2023
Hiral Patel67125b02013-09-12 17:45:42 -07002024 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
2025 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
2026 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
2027 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
2028 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
2029
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002030lr_io_req_end:
2031 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel03298552013-02-12 17:00:58 -08002032 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002033
2034 return ret;
2035}
2036
2037/*
2038 * Clean up any pending aborts on the lun
2039 * For each outstanding IO on this lun, whose abort is not completed by fw,
2040 * issue a local abort. Wait for abort to complete. Return 0 if all commands
2041 * successfully aborted, 1 otherwise
2042 */
2043static int fnic_clean_pending_aborts(struct fnic *fnic,
Satish Kharata36f5dd2016-03-18 11:22:49 -07002044 struct scsi_cmnd *lr_sc,
2045 bool new_sc)
2046
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002047{
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002048 int tag, abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002049 struct fnic_io_req *io_req;
2050 spinlock_t *io_lock;
2051 unsigned long flags;
2052 int ret = 0;
2053 struct scsi_cmnd *sc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002054 struct scsi_lun fc_lun;
2055 struct scsi_device *lun_dev = lr_sc->device;
2056 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002057 enum fnic_ioreq_state old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002058
Hiral Patelfc857992013-09-09 13:31:51 -07002059 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002060 io_lock = fnic_io_lock_tag(fnic, tag);
2061 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002062 sc = scsi_host_find_tag(fnic->lport->host, tag);
2063 /*
Satish Kharata36f5dd2016-03-18 11:22:49 -07002064 * ignore this lun reset cmd if issued using new SC
2065 * or cmds that do not belong to this lun
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002066 */
Satish Kharata36f5dd2016-03-18 11:22:49 -07002067 if (!sc || ((sc == lr_sc) && new_sc) || sc->device != lun_dev) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002068 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002069 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002070 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002071
2072 io_req = (struct fnic_io_req *)CMD_SP(sc);
2073
2074 if (!io_req || sc->device != lun_dev) {
2075 spin_unlock_irqrestore(io_lock, flags);
2076 continue;
2077 }
2078
2079 /*
2080 * Found IO that is still pending with firmware and
2081 * belongs to the LUN that we are resetting
2082 */
2083 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2084 "Found IO in %s on lun\n",
2085 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2086
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002087 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2088 spin_unlock_irqrestore(io_lock, flags);
2089 continue;
2090 }
2091 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08002092 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002093 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2094 "%s dev rst not pending sc 0x%p\n", __func__,
2095 sc);
2096 spin_unlock_irqrestore(io_lock, flags);
2097 continue;
2098 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002099
2100 if (io_req->abts_done)
2101 shost_printk(KERN_ERR, fnic->lport->host,
2102 "%s: io_req->abts_done is set state is %s\n",
2103 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002104 old_ioreq_state = CMD_STATE(sc);
2105 /*
2106 * Any pending IO issued prior to reset is expected to be
2107 * in abts pending state, if not we need to set
2108 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2109 * When IO is completed, the IO will be handed over and
2110 * handled in this function.
2111 */
2112 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2113
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002114 BUG_ON(io_req->abts_done);
2115
2116 abt_tag = tag;
2117 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2118 abt_tag |= FNIC_TAG_DEV_RST;
2119 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2120 "%s: dev rst sc 0x%p\n", __func__, sc);
2121 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002122
2123 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2124 io_req->abts_done = &tm_done;
2125 spin_unlock_irqrestore(io_lock, flags);
2126
2127 /* Now queue the abort command to firmware */
2128 int_to_scsilun(sc->device->lun, &fc_lun);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002129
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002130 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002131 FCPIO_ITMF_ABT_TASK_TERM,
2132 fc_lun.scsi_lun, io_req)) {
2133 spin_lock_irqsave(io_lock, flags);
2134 io_req = (struct fnic_io_req *)CMD_SP(sc);
2135 if (io_req)
2136 io_req->abts_done = NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002137 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2138 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002139 spin_unlock_irqrestore(io_lock, flags);
2140 ret = 1;
2141 goto clean_pending_aborts_end;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002142 } else {
2143 spin_lock_irqsave(io_lock, flags);
2144 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2145 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2146 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002147 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08002148 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002149
2150 wait_for_completion_timeout(&tm_done,
2151 msecs_to_jiffies
2152 (fnic->config.ed_tov));
2153
2154 /* Recheck cmd state to check if it is now aborted */
2155 spin_lock_irqsave(io_lock, flags);
2156 io_req = (struct fnic_io_req *)CMD_SP(sc);
2157 if (!io_req) {
2158 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002159 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002160 continue;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002161 }
2162
2163 io_req->abts_done = NULL;
2164
2165 /* if abort is still pending with fw, fail */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002166 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002167 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002168 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002169 ret = 1;
2170 goto clean_pending_aborts_end;
2171 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002172 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
Satish Kharata36f5dd2016-03-18 11:22:49 -07002173
2174 /* original sc used for lr is handled by dev reset code */
2175 if (sc != lr_sc)
2176 CMD_SP(sc) = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002177 spin_unlock_irqrestore(io_lock, flags);
2178
Satish Kharata36f5dd2016-03-18 11:22:49 -07002179 /* original sc used for lr is handled by dev reset code */
2180 if (sc != lr_sc) {
2181 fnic_release_ioreq_buf(fnic, io_req, sc);
2182 mempool_free(io_req, fnic->io_req_pool);
2183 }
2184
2185 /*
2186 * Any IO is returned during reset, it needs to call scsi_done
2187 * to return the scsi_cmnd to upper layer.
2188 */
2189 if (sc->scsi_done) {
2190 /* Set result to let upper SCSI layer retry */
2191 sc->result = DID_RESET << 16;
2192 sc->scsi_done(sc);
2193 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002194 }
2195
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002196 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2197
2198 /* walk again to check, if IOs are still pending in fw */
2199 if (fnic_is_abts_pending(fnic, lr_sc))
2200 ret = FAILED;
2201
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002202clean_pending_aborts_end:
2203 return ret;
2204}
2205
Hiral Patel03298552013-02-12 17:00:58 -08002206/**
2207 * fnic_scsi_host_start_tag
2208 * Allocates tagid from host's tag list
2209 **/
2210static inline int
2211fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2212{
2213 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2214 int tag, ret = SCSI_NO_TAG;
2215
2216 BUG_ON(!bqt);
2217 if (!bqt) {
2218 pr_err("Tags are not supported\n");
2219 goto end;
2220 }
2221
2222 do {
2223 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2224 if (tag >= bqt->max_depth) {
2225 pr_err("Tag allocation failure\n");
2226 goto end;
2227 }
2228 } while (test_and_set_bit(tag, bqt->tag_map));
2229
2230 bqt->tag_index[tag] = sc->request;
2231 sc->request->tag = tag;
2232 sc->tag = tag;
2233 if (!sc->request->special)
2234 sc->request->special = sc;
2235
2236 ret = tag;
2237
2238end:
2239 return ret;
2240}
2241
2242/**
2243 * fnic_scsi_host_end_tag
2244 * frees tag allocated by fnic_scsi_host_start_tag.
2245 **/
2246static inline void
2247fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2248{
2249 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2250 int tag = sc->request->tag;
2251
2252 if (tag == SCSI_NO_TAG)
2253 return;
2254
2255 BUG_ON(!bqt || !bqt->tag_index[tag]);
2256 if (!bqt)
2257 return;
2258
2259 bqt->tag_index[tag] = NULL;
2260 clear_bit(tag, bqt->tag_map);
2261
2262 return;
2263}
2264
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002265/*
2266 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2267 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2268 * on the LUN.
2269 */
2270int fnic_device_reset(struct scsi_cmnd *sc)
2271{
2272 struct fc_lport *lp;
2273 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002274 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002275 struct fc_rport *rport;
2276 int status;
2277 int ret = FAILED;
2278 spinlock_t *io_lock;
2279 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08002280 unsigned long start_time = 0;
Hiral Patel03298552013-02-12 17:00:58 -08002281 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07002282 struct fnic_stats *fnic_stats;
2283 struct reset_stats *reset_stats;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002284 int tag = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002285 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patel03298552013-02-12 17:00:58 -08002286 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
Satish Kharata36f5dd2016-03-18 11:22:49 -07002287 bool new_sc = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002288
2289 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01002290 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002291
2292 /* Get local-port, check ready and link up */
2293 lp = shost_priv(sc->device->host);
2294
2295 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002296 fnic_stats = &fnic->fnic_stats;
2297 reset_stats = &fnic->fnic_stats.reset_stats;
2298
2299 atomic64_inc(&reset_stats->device_resets);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002300
Roel Kluin0db6f432010-06-11 16:44:46 -07002301 rport = starget_to_rport(scsi_target(sc->device));
2302 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002303 "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
Hiral Patel03298552013-02-12 17:00:58 -08002304 rport->port_id, sc->device->lun, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002305
2306 if (lp->state != LPORT_ST_READY || !(lp->link_up))
2307 goto fnic_device_reset_end;
2308
2309 /* Check if remote port up */
Hiral Patel67125b02013-09-12 17:45:42 -07002310 if (fc_remote_port_chkready(rport)) {
2311 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002312 goto fnic_device_reset_end;
Hiral Patel67125b02013-09-12 17:45:42 -07002313 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002314
Hiral Patel14eb5d92013-02-12 17:01:01 -08002315 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
Hiral Patel03298552013-02-12 17:00:58 -08002316 /* Allocate tag if not present */
2317
2318 tag = sc->request->tag;
2319 if (unlikely(tag < 0)) {
Christoph Hellwig4d637162014-06-12 14:53:23 +02002320 /*
2321 * XXX(hch): current the midlayer fakes up a struct
2322 * request for the explicit reset ioctls, and those
2323 * don't have a tag allocated to them. The below
2324 * code pokes into midlayer structures to paper over
2325 * this design issue, but that won't work for blk-mq.
2326 *
2327 * Either someone who can actually test the hardware
2328 * will have to come up with a similar hack for the
2329 * blk-mq case, or we'll have to bite the bullet and
2330 * fix the way the EH ioctls work for real, but until
2331 * that happens we fail these explicit requests here.
2332 */
Christoph Hellwig4d637162014-06-12 14:53:23 +02002333
Hiral Patel03298552013-02-12 17:00:58 -08002334 tag = fnic_scsi_host_start_tag(fnic, sc);
2335 if (unlikely(tag == SCSI_NO_TAG))
2336 goto fnic_device_reset_end;
2337 tag_gen_flag = 1;
Satish Kharata36f5dd2016-03-18 11:22:49 -07002338 new_sc = 1;
Hiral Patel03298552013-02-12 17:00:58 -08002339 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002340 io_lock = fnic_io_lock_hash(fnic, sc);
2341 spin_lock_irqsave(io_lock, flags);
2342 io_req = (struct fnic_io_req *)CMD_SP(sc);
2343
2344 /*
2345 * If there is a io_req attached to this command, then use it,
2346 * else allocate a new one.
2347 */
2348 if (!io_req) {
2349 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2350 if (!io_req) {
2351 spin_unlock_irqrestore(io_lock, flags);
2352 goto fnic_device_reset_end;
2353 }
2354 memset(io_req, 0, sizeof(*io_req));
2355 io_req->port_id = rport->port_id;
2356 CMD_SP(sc) = (char *)io_req;
2357 }
2358 io_req->dr_done = &tm_done;
2359 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2360 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2361 spin_unlock_irqrestore(io_lock, flags);
2362
Hiral Patel03298552013-02-12 17:00:58 -08002363 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002364
2365 /*
2366 * issue the device reset, if enqueue failed, clean up the ioreq
2367 * and break assoc with scsi cmd
2368 */
2369 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2370 spin_lock_irqsave(io_lock, flags);
2371 io_req = (struct fnic_io_req *)CMD_SP(sc);
2372 if (io_req)
2373 io_req->dr_done = NULL;
2374 goto fnic_device_reset_clean;
2375 }
Hiral Patel03298552013-02-12 17:00:58 -08002376 spin_lock_irqsave(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002377 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08002378 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002379
2380 /*
2381 * Wait on the local completion for LUN reset. The io_req may be
2382 * freed while we wait since we hold no lock.
2383 */
2384 wait_for_completion_timeout(&tm_done,
2385 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2386
2387 spin_lock_irqsave(io_lock, flags);
2388 io_req = (struct fnic_io_req *)CMD_SP(sc);
2389 if (!io_req) {
2390 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002391 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2392 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002393 goto fnic_device_reset_end;
2394 }
2395 io_req->dr_done = NULL;
2396
2397 status = CMD_LR_STATUS(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002398
2399 /*
2400 * If lun reset not completed, bail out with failed. io_req
2401 * gets cleaned up during higher levels of EH
2402 */
2403 if (status == FCPIO_INVALID_CODE) {
Hiral Patel67125b02013-09-12 17:45:42 -07002404 atomic64_inc(&reset_stats->device_reset_timeouts);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002405 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2406 "Device reset timed out\n");
Hiral Patel03298552013-02-12 17:00:58 -08002407 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2408 spin_unlock_irqrestore(io_lock, flags);
2409 int_to_scsilun(sc->device->lun, &fc_lun);
2410 /*
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002411 * Issue abort and terminate on device reset request.
2412 * If q'ing of terminate fails, retry it after a delay.
Hiral Patel03298552013-02-12 17:00:58 -08002413 */
2414 while (1) {
2415 spin_lock_irqsave(io_lock, flags);
2416 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2417 spin_unlock_irqrestore(io_lock, flags);
2418 break;
2419 }
2420 spin_unlock_irqrestore(io_lock, flags);
2421 if (fnic_queue_abort_io_req(fnic,
2422 tag | FNIC_TAG_DEV_RST,
2423 FCPIO_ITMF_ABT_TASK_TERM,
2424 fc_lun.scsi_lun, io_req)) {
2425 wait_for_completion_timeout(&tm_done,
2426 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2427 } else {
2428 spin_lock_irqsave(io_lock, flags);
2429 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2430 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2431 io_req->abts_done = &tm_done;
2432 spin_unlock_irqrestore(io_lock, flags);
2433 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2434 "Abort and terminate issued on Device reset "
2435 "tag 0x%x sc 0x%p\n", tag, sc);
2436 break;
2437 }
2438 }
2439 while (1) {
2440 spin_lock_irqsave(io_lock, flags);
2441 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2442 spin_unlock_irqrestore(io_lock, flags);
2443 wait_for_completion_timeout(&tm_done,
2444 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2445 break;
2446 } else {
2447 io_req = (struct fnic_io_req *)CMD_SP(sc);
2448 io_req->abts_done = NULL;
2449 goto fnic_device_reset_clean;
2450 }
2451 }
2452 } else {
2453 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002454 }
2455
2456 /* Completed, but not successful, clean up the io_req, return fail */
2457 if (status != FCPIO_SUCCESS) {
2458 spin_lock_irqsave(io_lock, flags);
2459 FNIC_SCSI_DBG(KERN_DEBUG,
2460 fnic->lport->host,
2461 "Device reset completed - failed\n");
2462 io_req = (struct fnic_io_req *)CMD_SP(sc);
2463 goto fnic_device_reset_clean;
2464 }
2465
2466 /*
2467 * Clean up any aborts on this lun that have still not
2468 * completed. If any of these fail, then LUN reset fails.
2469 * clean_pending_aborts cleans all cmds on this lun except
2470 * the lun reset cmd. If all cmds get cleaned, the lun reset
2471 * succeeds
2472 */
Satish Kharata36f5dd2016-03-18 11:22:49 -07002473 if (fnic_clean_pending_aborts(fnic, sc, new_sc)) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002474 spin_lock_irqsave(io_lock, flags);
2475 io_req = (struct fnic_io_req *)CMD_SP(sc);
2476 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2477 "Device reset failed"
2478 " since could not abort all IOs\n");
2479 goto fnic_device_reset_clean;
2480 }
2481
2482 /* Clean lun reset command */
2483 spin_lock_irqsave(io_lock, flags);
2484 io_req = (struct fnic_io_req *)CMD_SP(sc);
2485 if (io_req)
2486 /* Completed, and successful */
2487 ret = SUCCESS;
2488
2489fnic_device_reset_clean:
2490 if (io_req)
2491 CMD_SP(sc) = NULL;
2492
2493 spin_unlock_irqrestore(io_lock, flags);
2494
2495 if (io_req) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002496 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002497 fnic_release_ioreq_buf(fnic, io_req, sc);
2498 mempool_free(io_req, fnic->io_req_pool);
2499 }
2500
2501fnic_device_reset_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08002502 FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2503 sc->request->tag, sc,
2504 jiffies_to_msecs(jiffies - start_time),
2505 0, ((u64)sc->cmnd[0] << 32 |
2506 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2507 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2508 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2509
Hiral Patel03298552013-02-12 17:00:58 -08002510 /* free tag if it is allocated */
2511 if (unlikely(tag_gen_flag))
2512 fnic_scsi_host_end_tag(fnic, sc);
2513
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002514 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2515 "Returning from device reset %s\n",
2516 (ret == SUCCESS) ?
2517 "SUCCESS" : "FAILED");
Hiral Patel67125b02013-09-12 17:45:42 -07002518
2519 if (ret == FAILED)
2520 atomic64_inc(&reset_stats->device_reset_failures);
2521
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002522 return ret;
2523}
2524
2525/* Clean up all IOs, clean up libFC local port */
2526int fnic_reset(struct Scsi_Host *shost)
2527{
2528 struct fc_lport *lp;
2529 struct fnic *fnic;
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002530 int ret = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07002531 struct reset_stats *reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002532
2533 lp = shost_priv(shost);
2534 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002535 reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002536
2537 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2538 "fnic_reset called\n");
2539
Hiral Patel67125b02013-09-12 17:45:42 -07002540 atomic64_inc(&reset_stats->fnic_resets);
2541
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002542 /*
2543 * Reset local port, this will clean up libFC exchanges,
2544 * reset remote port sessions, and if link is up, begin flogi
2545 */
Hannes Reinecke31c0a632016-10-18 10:01:36 +02002546 ret = fc_lport_reset(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002547
2548 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2549 "Returning from fnic reset %s\n",
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002550 (ret == 0) ?
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002551 "SUCCESS" : "FAILED");
2552
Hiral Patel67125b02013-09-12 17:45:42 -07002553 if (ret == 0)
2554 atomic64_inc(&reset_stats->fnic_reset_completions);
2555 else
2556 atomic64_inc(&reset_stats->fnic_reset_failures);
2557
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002558 return ret;
2559}
2560
2561/*
2562 * SCSI Error handling calls driver's eh_host_reset if all prior
2563 * error handling levels return FAILED. If host reset completes
2564 * successfully, and if link is up, then Fabric login begins.
2565 *
2566 * Host Reset is the highest level of error recovery. If this fails, then
2567 * host is offlined by SCSI.
2568 *
2569 */
2570int fnic_host_reset(struct scsi_cmnd *sc)
2571{
2572 int ret;
2573 unsigned long wait_host_tmo;
2574 struct Scsi_Host *shost = sc->device->host;
2575 struct fc_lport *lp = shost_priv(shost);
2576
2577 /*
2578 * If fnic_reset is successful, wait for fabric login to complete
2579 * scsi-ml tries to send a TUR to every device if host reset is
2580 * successful, so before returning to scsi, fabric should be up
2581 */
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002582 ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002583 if (ret == SUCCESS) {
2584 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2585 ret = FAILED;
2586 while (time_before(jiffies, wait_host_tmo)) {
2587 if ((lp->state == LPORT_ST_READY) &&
2588 (lp->link_up)) {
2589 ret = SUCCESS;
2590 break;
2591 }
2592 ssleep(1);
2593 }
2594 }
2595
2596 return ret;
2597}
2598
2599/*
2600 * This fxn is called from libFC when host is removed
2601 */
2602void fnic_scsi_abort_io(struct fc_lport *lp)
2603{
2604 int err = 0;
2605 unsigned long flags;
2606 enum fnic_state old_state;
2607 struct fnic *fnic = lport_priv(lp);
2608 DECLARE_COMPLETION_ONSTACK(remove_wait);
2609
2610 /* Issue firmware reset for fnic, wait for reset to complete */
Hiral Patel03298552013-02-12 17:00:58 -08002611retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002612 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002613 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2614 /* fw reset is in progress, poll for its completion */
2615 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2616 schedule_timeout(msecs_to_jiffies(100));
2617 goto retry_fw_reset;
2618 }
2619
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002620 fnic->remove_wait = &remove_wait;
2621 old_state = fnic->state;
2622 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002623 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002624 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2625
2626 err = fnic_fw_reset_handler(fnic);
2627 if (err) {
2628 spin_lock_irqsave(&fnic->fnic_lock, flags);
2629 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2630 fnic->state = old_state;
2631 fnic->remove_wait = NULL;
2632 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2633 return;
2634 }
2635
2636 /* Wait for firmware reset to complete */
2637 wait_for_completion_timeout(&remove_wait,
2638 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2639
2640 spin_lock_irqsave(&fnic->fnic_lock, flags);
2641 fnic->remove_wait = NULL;
2642 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2643 "fnic_scsi_abort_io %s\n",
2644 (fnic->state == FNIC_IN_ETH_MODE) ?
2645 "SUCCESS" : "FAILED");
2646 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2647
2648}
2649
2650/*
2651 * This fxn called from libFC to clean up driver IO state on link down
2652 */
2653void fnic_scsi_cleanup(struct fc_lport *lp)
2654{
2655 unsigned long flags;
2656 enum fnic_state old_state;
2657 struct fnic *fnic = lport_priv(lp);
2658
2659 /* issue fw reset */
Hiral Patel03298552013-02-12 17:00:58 -08002660retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002661 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002662 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2663 /* fw reset is in progress, poll for its completion */
2664 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2665 schedule_timeout(msecs_to_jiffies(100));
2666 goto retry_fw_reset;
2667 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002668 old_state = fnic->state;
2669 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002670 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002671 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2672
2673 if (fnic_fw_reset_handler(fnic)) {
2674 spin_lock_irqsave(&fnic->fnic_lock, flags);
2675 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2676 fnic->state = old_state;
2677 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2678 }
2679
2680}
2681
2682void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2683{
2684}
2685
2686void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2687{
2688 struct fnic *fnic = lport_priv(lp);
2689
2690 /* Non-zero sid, nothing to do */
2691 if (sid)
2692 goto call_fc_exch_mgr_reset;
2693
2694 if (did) {
2695 fnic_rport_exch_reset(fnic, did);
2696 goto call_fc_exch_mgr_reset;
2697 }
2698
2699 /*
2700 * sid = 0, did = 0
2701 * link down or device being removed
2702 */
2703 if (!fnic->in_remove)
2704 fnic_scsi_cleanup(lp);
2705 else
2706 fnic_scsi_abort_io(lp);
2707
2708 /* call libFC exch mgr reset to reset its exchanges */
2709call_fc_exch_mgr_reset:
2710 fc_exch_mgr_reset(lp, sid, did);
2711
2712}
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002713
2714/*
2715 * fnic_is_abts_pending() is a helper function that
2716 * walks through tag map to check if there is any IOs pending,if there is one,
2717 * then it returns 1 (true), otherwise 0 (false)
2718 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2719 * otherwise, it checks for all IOs.
2720 */
2721int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2722{
2723 int tag;
2724 struct fnic_io_req *io_req;
2725 spinlock_t *io_lock;
2726 unsigned long flags;
2727 int ret = 0;
2728 struct scsi_cmnd *sc;
2729 struct scsi_device *lun_dev = NULL;
2730
2731 if (lr_sc)
2732 lun_dev = lr_sc->device;
2733
2734 /* walk again to check, if IOs are still pending in fw */
Hiral Patelfc857992013-09-09 13:31:51 -07002735 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002736 sc = scsi_host_find_tag(fnic->lport->host, tag);
2737 /*
2738 * ignore this lun reset cmd or cmds that do not belong to
2739 * this lun
2740 */
2741 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2742 continue;
2743
2744 io_lock = fnic_io_lock_hash(fnic, sc);
2745 spin_lock_irqsave(io_lock, flags);
2746
2747 io_req = (struct fnic_io_req *)CMD_SP(sc);
2748
2749 if (!io_req || sc->device != lun_dev) {
2750 spin_unlock_irqrestore(io_lock, flags);
2751 continue;
2752 }
2753
2754 /*
2755 * Found IO that is still pending with firmware and
2756 * belongs to the LUN that we are resetting
2757 */
2758 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2759 "Found IO in %s on lun\n",
2760 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2761
Dan Carpenter5d65f912013-05-30 10:50:46 +03002762 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002763 ret = 1;
Dan Carpenter5d65f912013-05-30 10:50:46 +03002764 spin_unlock_irqrestore(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002765 }
2766
2767 return ret;
2768}