blob: 7bf6102b4c3d52396014391cc5929f54b1f2c155 [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
Hannes Reinecke43c70e22020-01-16 11:20:53 +0100448 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_FWRESET)))
449 return SCSI_MLQUEUE_HOST_BUSY;
450
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700451 rport = starget_to_rport(scsi_target(sc->device));
452 ret = fc_remote_port_chkready(rport);
453 if (ret) {
Hiral Patel67125b02013-09-12 17:45:42 -0700454 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700455 sc->result = ret;
456 done(sc);
457 return 0;
458 }
459
Satish Kharat1b6ac5e2016-03-18 11:22:50 -0700460 if (rport) {
461 struct fc_rport_libfc_priv *rp = rport->dd_data;
462
463 if (!rp || rp->rp_state != RPORT_ST_READY) {
464 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
465 "returning DID_NO_CONNECT for IO as rport is removed\n");
466 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
467 sc->result = DID_NO_CONNECT<<16;
468 done(sc);
469 return 0;
470 }
Hiral Shah35061e22014-11-10 12:54:34 -0800471 }
472
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700473 if (lp->state != LPORT_ST_READY || !(lp->link_up))
474 return SCSI_MLQUEUE_HOST_BUSY;
475
Hiral Patel03298552013-02-12 17:00:58 -0800476 atomic_inc(&fnic->in_flight);
477
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700478 /*
479 * Release host lock, use driver resource specific locks from here.
480 * Don't re-enable interrupts in case they were disabled prior to the
481 * caller disabling them.
482 */
483 spin_unlock(lp->host->host_lock);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800484 CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
485 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700486
487 /* Get a new io_req for this SCSI IO */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700488 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
489 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -0700490 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700491 ret = SCSI_MLQUEUE_HOST_BUSY;
492 goto out;
493 }
494 memset(io_req, 0, sizeof(*io_req));
495
496 /* Map the data buffer */
497 sg_count = scsi_dma_map(sc);
498 if (sg_count < 0) {
Hiral Patel4d7007b2013-02-12 17:01:02 -0800499 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
500 sc->request->tag, sc, 0, sc->cmnd[0],
501 sg_count, CMD_STATE(sc));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700502 mempool_free(io_req, fnic->io_req_pool);
503 goto out;
504 }
505
506 /* Determine the type of scatter/gather list we need */
507 io_req->sgl_cnt = sg_count;
508 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
509 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
510 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
511
512 if (sg_count) {
513 io_req->sgl_list =
514 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
Abhijeet Joglekar0c79c742011-06-13 21:21:01 -0700515 GFP_ATOMIC);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700516 if (!io_req->sgl_list) {
Hiral Patel67125b02013-09-12 17:45:42 -0700517 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700518 ret = SCSI_MLQUEUE_HOST_BUSY;
519 scsi_dma_unmap(sc);
520 mempool_free(io_req, fnic->io_req_pool);
521 goto out;
522 }
523
524 /* Cache sgl list allocated address before alignment */
525 io_req->sgl_list_alloc = io_req->sgl_list;
526 ptr = (unsigned long) io_req->sgl_list;
527 if (ptr % FNIC_SG_DESC_ALIGN) {
528 io_req->sgl_list = (struct host_sg_desc *)
529 (((unsigned long) ptr
530 + FNIC_SG_DESC_ALIGN - 1)
531 & ~(FNIC_SG_DESC_ALIGN - 1));
532 }
533 }
534
Hiral Shah41df7b02014-11-10 12:54:36 -0800535 /*
536 * Will acquire lock defore setting to IO initialized.
537 */
538
539 io_lock = fnic_io_lock_hash(fnic, sc);
540 spin_lock_irqsave(io_lock, flags);
541
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700542 /* initialize rest of io_req */
Hiral Shahdb196932015-07-14 07:08:57 -0700543 io_lock_acquired = 1;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700544 io_req->port_id = rport->port_id;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800545 io_req->start_time = jiffies;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700546 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
547 CMD_SP(sc) = (char *)io_req;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800548 CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700549 sc->scsi_done = done;
550
551 /* create copy wq desc and enqueue it */
552 wq = &fnic->wq_copy[0];
553 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
554 if (ret) {
555 /*
556 * In case another thread cancelled the request,
557 * refetch the pointer under the lock.
558 */
Hiral Patel4d7007b2013-02-12 17:01:02 -0800559 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
560 sc->request->tag, sc, 0, 0, 0,
561 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700562 io_req = (struct fnic_io_req *)CMD_SP(sc);
563 CMD_SP(sc) = NULL;
564 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
565 spin_unlock_irqrestore(io_lock, flags);
566 if (io_req) {
567 fnic_release_ioreq_buf(fnic, io_req, sc);
568 mempool_free(io_req, fnic->io_req_pool);
569 }
Hiral Shah41df7b02014-11-10 12:54:36 -0800570 atomic_dec(&fnic->in_flight);
571 /* acquire host lock before returning to SCSI */
572 spin_lock(lp->host->host_lock);
573 return ret;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800574 } else {
Hiral Patel67125b02013-09-12 17:45:42 -0700575 atomic64_inc(&fnic_stats->io_stats.active_ios);
576 atomic64_inc(&fnic_stats->io_stats.num_ios);
577 if (atomic64_read(&fnic_stats->io_stats.active_ios) >
578 atomic64_read(&fnic_stats->io_stats.max_active_ios))
579 atomic64_set(&fnic_stats->io_stats.max_active_ios,
580 atomic64_read(&fnic_stats->io_stats.active_ios));
581
Hiral Patel14eb5d92013-02-12 17:01:01 -0800582 /* REVISIT: Use per IO lock in the final code */
583 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700584 }
585out:
Hiral Patel4d7007b2013-02-12 17:01:02 -0800586 cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
587 (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
588 (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
589 sc->cmnd[5]);
590
591 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
592 sc->request->tag, sc, io_req,
593 sg_count, cmd_trace,
594 (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
Hiral Shah41df7b02014-11-10 12:54:36 -0800595
596 /* if only we issued IO, will we have the io lock */
Hiral Shahdb196932015-07-14 07:08:57 -0700597 if (io_lock_acquired)
Hiral Shah41df7b02014-11-10 12:54:36 -0800598 spin_unlock_irqrestore(io_lock, flags);
599
Hiral Patel03298552013-02-12 17:00:58 -0800600 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700601 /* acquire host lock before returning to SCSI */
602 spin_lock(lp->host->host_lock);
603 return ret;
604}
605
Jeff Garzikf2812332010-11-16 02:10:29 -0500606DEF_SCSI_QCMD(fnic_queuecommand)
607
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700608/*
609 * fnic_fcpio_fw_reset_cmpl_handler
610 * Routine to handle fw reset completion
611 */
612static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
613 struct fcpio_fw_req *desc)
614{
615 u8 type;
616 u8 hdr_status;
617 struct fcpio_tag tag;
618 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700619 unsigned long flags;
Hiral Patel67125b02013-09-12 17:45:42 -0700620 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700621
622 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
623
Hiral Patel67125b02013-09-12 17:45:42 -0700624 atomic64_inc(&reset_stats->fw_reset_completions);
625
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700626 /* Clean up all outstanding io requests */
627 fnic_cleanup_io(fnic, SCSI_NO_TAG);
628
Hiral Patel67125b02013-09-12 17:45:42 -0700629 atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
630 atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
631
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700632 spin_lock_irqsave(&fnic->fnic_lock, flags);
633
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700634 /* fnic should be in FC_TRANS_ETH_MODE */
635 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
636 /* Check status of reset completion */
637 if (!hdr_status) {
638 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
639 "reset cmpl success\n");
640 /* Ready to send flogi out */
641 fnic->state = FNIC_IN_ETH_MODE;
642 } else {
643 FNIC_SCSI_DBG(KERN_DEBUG,
644 fnic->lport->host,
645 "fnic fw_reset : failed %s\n",
646 fnic_fcpio_status_to_str(hdr_status));
647
648 /*
649 * Unable to change to eth mode, cannot send out flogi
650 * Change state to fc mode, so that subsequent Flogi
651 * requests from libFC will cause more attempts to
652 * reset the firmware. Free the cached flogi
653 */
654 fnic->state = FNIC_IN_FC_MODE;
Hiral Patel67125b02013-09-12 17:45:42 -0700655 atomic64_inc(&reset_stats->fw_reset_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700656 ret = -1;
657 }
658 } else {
659 FNIC_SCSI_DBG(KERN_DEBUG,
660 fnic->lport->host,
661 "Unexpected state %s while processing"
662 " reset cmpl\n", fnic_state_to_str(fnic->state));
Hiral Patel67125b02013-09-12 17:45:42 -0700663 atomic64_inc(&reset_stats->fw_reset_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700664 ret = -1;
665 }
666
667 /* Thread removing device blocks till firmware reset is complete */
668 if (fnic->remove_wait)
669 complete(fnic->remove_wait);
670
671 /*
672 * If fnic is being removed, or fw reset failed
673 * free the flogi frame. Else, send it out
674 */
675 if (fnic->remove_wait || ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700676 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Joe Eykholt78112e52009-11-03 11:49:22 -0800677 skb_queue_purge(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700678 goto reset_cmpl_handler_end;
679 }
680
681 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
682
Joe Eykholt78112e52009-11-03 11:49:22 -0800683 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700684
685 reset_cmpl_handler_end:
Hiral Patel03298552013-02-12 17:00:58 -0800686 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
687
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700688 return ret;
689}
690
691/*
692 * fnic_fcpio_flogi_reg_cmpl_handler
693 * Routine to handle flogi register completion
694 */
695static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
696 struct fcpio_fw_req *desc)
697{
698 u8 type;
699 u8 hdr_status;
700 struct fcpio_tag tag;
701 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700702 unsigned long flags;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700703
704 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
705
706 /* Update fnic state based on status of flogi reg completion */
707 spin_lock_irqsave(&fnic->fnic_lock, flags);
708
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700709 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
710
711 /* Check flogi registration completion status */
712 if (!hdr_status) {
713 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
714 "flog reg succeeded\n");
715 fnic->state = FNIC_IN_FC_MODE;
716 } else {
717 FNIC_SCSI_DBG(KERN_DEBUG,
718 fnic->lport->host,
719 "fnic flogi reg :failed %s\n",
720 fnic_fcpio_status_to_str(hdr_status));
721 fnic->state = FNIC_IN_ETH_MODE;
722 ret = -1;
723 }
724 } else {
725 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
726 "Unexpected fnic state %s while"
727 " processing flogi reg completion\n",
728 fnic_state_to_str(fnic->state));
729 ret = -1;
730 }
731
Joe Eykholt78112e52009-11-03 11:49:22 -0800732 if (!ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700733 if (fnic->stop_rx_link_events) {
734 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
735 goto reg_cmpl_handler_end;
736 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700737 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
738
Joe Eykholt78112e52009-11-03 11:49:22 -0800739 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700740 queue_work(fnic_event_queue, &fnic->frame_work);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700741 } else {
742 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700743 }
744
745reg_cmpl_handler_end:
746 return ret;
747}
748
749static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
750 u16 request_out)
751{
752 if (wq->to_clean_index <= wq->to_use_index) {
753 /* out of range, stale request_out index */
754 if (request_out < wq->to_clean_index ||
755 request_out >= wq->to_use_index)
756 return 0;
757 } else {
758 /* out of range, stale request_out index */
759 if (request_out < wq->to_clean_index &&
760 request_out >= wq->to_use_index)
761 return 0;
762 }
763 /* request_out index is in range */
764 return 1;
765}
766
767
768/*
769 * Mark that ack received and store the Ack index. If there are multiple
770 * acks received before Tx thread cleans it up, the latest value will be
771 * used which is correct behavior. This state should be in the copy Wq
772 * instead of in the fnic
773 */
774static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
775 unsigned int cq_index,
776 struct fcpio_fw_req *desc)
777{
778 struct vnic_wq_copy *wq;
779 u16 request_out = desc->u.ack.request_out;
780 unsigned long flags;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800781 u64 *ox_id_tag = (u64 *)(void *)desc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700782
783 /* mark the ack state */
784 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
785 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
786
Hiral Patel67125b02013-09-12 17:45:42 -0700787 fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700788 if (is_ack_index_in_range(wq, request_out)) {
789 fnic->fw_ack_index[0] = request_out;
790 fnic->fw_ack_recd[0] = 1;
Hiral Patel67125b02013-09-12 17:45:42 -0700791 } else
792 atomic64_inc(
793 &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
794
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700795 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800796 FNIC_TRACE(fnic_fcpio_ack_handler,
797 fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
798 ox_id_tag[4], ox_id_tag[5]);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700799}
800
801/*
802 * fnic_fcpio_icmnd_cmpl_handler
803 * Routine to handle icmnd completions
804 */
805static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
806 struct fcpio_fw_req *desc)
807{
808 u8 type;
809 u8 hdr_status;
810 struct fcpio_tag tag;
811 u32 id;
812 u64 xfer_len = 0;
813 struct fcpio_icmnd_cmpl *icmnd_cmpl;
814 struct fnic_io_req *io_req;
815 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -0700816 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700817 unsigned long flags;
818 spinlock_t *io_lock;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800819 u64 cmd_trace;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800820 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700821
822 /* Decode the cmpl description to get the io_req id */
823 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
824 fcpio_tag_id_dec(&tag, &id);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800825 icmnd_cmpl = &desc->u.icmnd_cmpl;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700826
Hiral Patelfc857992013-09-09 13:31:51 -0700827 if (id >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -0800828 shost_printk(KERN_ERR, fnic->lport->host,
829 "Tag out of range tag %x hdr status = %s\n",
830 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700831 return;
Hiral Patel03298552013-02-12 17:00:58 -0800832 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700833
834 sc = scsi_host_find_tag(fnic->lport->host, id);
835 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800836 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -0700837 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800838 shost_printk(KERN_ERR, fnic->lport->host,
839 "icmnd_cmpl sc is null - "
840 "hdr status = %s tag = 0x%x desc = 0x%p\n",
841 fnic_fcpio_status_to_str(hdr_status), id, desc);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800842 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
843 fnic->lport->host->host_no, id,
844 ((u64)icmnd_cmpl->_resvd0[1] << 16 |
845 (u64)icmnd_cmpl->_resvd0[0]),
846 ((u64)hdr_status << 16 |
847 (u64)icmnd_cmpl->scsi_status << 8 |
848 (u64)icmnd_cmpl->flags), desc,
849 (u64)icmnd_cmpl->residual, 0);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700850 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800851 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700852
853 io_lock = fnic_io_lock_hash(fnic, sc);
854 spin_lock_irqsave(io_lock, flags);
855 io_req = (struct fnic_io_req *)CMD_SP(sc);
856 WARN_ON_ONCE(!io_req);
857 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -0700858 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800859 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700860 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800861 shost_printk(KERN_ERR, fnic->lport->host,
862 "icmnd_cmpl io_req is null - "
863 "hdr status = %s tag = 0x%x sc 0x%p\n",
864 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700865 return;
866 }
Hiral Patel14eb5d92013-02-12 17:01:01 -0800867 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700868
869 /* firmware completed the io */
870 io_req->io_completed = 1;
871
872 /*
873 * if SCSI-ML has already issued abort on this command,
874 * ignore completion of the IO. The abts path will clean it up
875 */
876 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
877 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800878 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
879 switch (hdr_status) {
880 case FCPIO_SUCCESS:
881 CMD_FLAGS(sc) |= FNIC_IO_DONE;
882 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
883 "icmnd_cmpl ABTS pending hdr status = %s "
884 "sc 0x%p scsi_status %x residual %d\n",
885 fnic_fcpio_status_to_str(hdr_status), sc,
886 icmnd_cmpl->scsi_status,
887 icmnd_cmpl->residual);
888 break;
889 case FCPIO_ABORTED:
890 CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
891 break;
892 default:
893 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
894 "icmnd_cmpl abts pending "
895 "hdr status = %s tag = 0x%x sc = 0x%p\n",
896 fnic_fcpio_status_to_str(hdr_status),
897 id, sc);
898 break;
899 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700900 return;
901 }
902
903 /* Mark the IO as complete */
904 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
905
906 icmnd_cmpl = &desc->u.icmnd_cmpl;
907
908 switch (hdr_status) {
909 case FCPIO_SUCCESS:
910 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
911 xfer_len = scsi_bufflen(sc);
912 scsi_set_resid(sc, icmnd_cmpl->residual);
913
914 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
915 xfer_len -= icmnd_cmpl->residual;
916
Hiral Patel67125b02013-09-12 17:45:42 -0700917 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
918 atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700919 break;
920
921 case FCPIO_TIMEOUT: /* request was timed out */
Hiral Patel67125b02013-09-12 17:45:42 -0700922 atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700923 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
924 break;
925
926 case FCPIO_ABORTED: /* request was aborted */
Hiral Patel67125b02013-09-12 17:45:42 -0700927 atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700928 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
929 break;
930
931 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
Hiral Patel67125b02013-09-12 17:45:42 -0700932 atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700933 scsi_set_resid(sc, icmnd_cmpl->residual);
934 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
935 break;
936
937 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
Hiral Patel67125b02013-09-12 17:45:42 -0700938 atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700939 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
940 break;
Hiral Patel67125b02013-09-12 17:45:42 -0700941
942 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
943 atomic64_inc(&fnic_stats->io_stats.io_not_found);
944 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
945 break;
946
947 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
948 atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
949 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
950 break;
951
952 case FCPIO_FW_ERR: /* request was terminated due fw error */
953 atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
954 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
955 break;
956
957 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
958 atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
959 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
960 break;
961
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700962 case FCPIO_INVALID_HEADER: /* header contains invalid data */
963 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
964 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700965 default:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700966 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
967 break;
968 }
969
Maurizio Lombardi14cee5b2016-03-16 14:44:08 +0100970 /* Break link with the SCSI command */
971 CMD_SP(sc) = NULL;
972 CMD_FLAGS(sc) |= FNIC_IO_DONE;
973
974 spin_unlock_irqrestore(io_lock, flags);
975
Hiral Patel67125b02013-09-12 17:45:42 -0700976 if (hdr_status != FCPIO_SUCCESS) {
977 atomic64_inc(&fnic_stats->io_stats.io_failures);
978 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
979 fnic_fcpio_status_to_str(hdr_status));
980 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700981
982 fnic_release_ioreq_buf(fnic, io_req, sc);
983
984 mempool_free(io_req, fnic->io_req_pool);
985
Hiral Patel4d7007b2013-02-12 17:01:02 -0800986 cmd_trace = ((u64)hdr_status << 56) |
987 (u64)icmnd_cmpl->scsi_status << 48 |
988 (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
989 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
990 (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
991
992 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
993 sc->device->host->host_no, id, sc,
994 ((u64)icmnd_cmpl->_resvd0[1] << 56 |
995 (u64)icmnd_cmpl->_resvd0[0] << 48 |
996 jiffies_to_msecs(jiffies - start_time)),
997 desc, cmd_trace,
998 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
999
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001000 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
1001 fnic->lport->host_stats.fcp_input_requests++;
1002 fnic->fcp_input_bytes += xfer_len;
1003 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
1004 fnic->lport->host_stats.fcp_output_requests++;
1005 fnic->fcp_output_bytes += xfer_len;
1006 } else
1007 fnic->lport->host_stats.fcp_control_requests++;
1008
Hiral Patel67125b02013-09-12 17:45:42 -07001009 atomic64_dec(&fnic_stats->io_stats.active_ios);
1010 if (atomic64_read(&fnic->io_cmpl_skip))
1011 atomic64_dec(&fnic->io_cmpl_skip);
1012 else
1013 atomic64_inc(&fnic_stats->io_stats.io_completions);
1014
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001015 /* Call SCSI completion function to complete the IO */
1016 if (sc->scsi_done)
1017 sc->scsi_done(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001018}
1019
1020/* fnic_fcpio_itmf_cmpl_handler
1021 * Routine to handle itmf completions
1022 */
1023static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1024 struct fcpio_fw_req *desc)
1025{
1026 u8 type;
1027 u8 hdr_status;
1028 struct fcpio_tag tag;
1029 u32 id;
1030 struct scsi_cmnd *sc;
1031 struct fnic_io_req *io_req;
Hiral Patel67125b02013-09-12 17:45:42 -07001032 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1033 struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1034 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1035 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001036 unsigned long flags;
1037 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001038 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001039
1040 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1041 fcpio_tag_id_dec(&tag, &id);
1042
Hiral Patelfc857992013-09-09 13:31:51 -07001043 if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -08001044 shost_printk(KERN_ERR, fnic->lport->host,
1045 "Tag out of range tag %x hdr status = %s\n",
1046 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001047 return;
Hiral Patel03298552013-02-12 17:00:58 -08001048 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001049
1050 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1051 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001052 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -07001053 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001054 shost_printk(KERN_ERR, fnic->lport->host,
1055 "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1056 fnic_fcpio_status_to_str(hdr_status), id);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001057 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001058 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001059 io_lock = fnic_io_lock_hash(fnic, sc);
1060 spin_lock_irqsave(io_lock, flags);
1061 io_req = (struct fnic_io_req *)CMD_SP(sc);
1062 WARN_ON_ONCE(!io_req);
1063 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001064 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001065 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001066 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1067 shost_printk(KERN_ERR, fnic->lport->host,
1068 "itmf_cmpl io_req is null - "
1069 "hdr status = %s tag = 0x%x sc 0x%p\n",
1070 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001071 return;
1072 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001073 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001074
Hiral Patel03298552013-02-12 17:00:58 -08001075 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1076 /* Abort and terminate completion of device reset req */
1077 /* REVISIT : Add asserts about various flags */
1078 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1079 "dev reset abts cmpl recd. id %x status %s\n",
1080 id, fnic_fcpio_status_to_str(hdr_status));
1081 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1082 CMD_ABTS_STATUS(sc) = hdr_status;
1083 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1084 if (io_req->abts_done)
1085 complete(io_req->abts_done);
1086 spin_unlock_irqrestore(io_lock, flags);
1087 } else if (id & FNIC_TAG_ABORT) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001088 /* Completion of abort cmd */
Hiral Patel67125b02013-09-12 17:45:42 -07001089 switch (hdr_status) {
1090 case FCPIO_SUCCESS:
1091 break;
1092 case FCPIO_TIMEOUT:
1093 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1094 atomic64_inc(&abts_stats->abort_fw_timeouts);
1095 else
1096 atomic64_inc(
1097 &term_stats->terminate_fw_timeouts);
1098 break;
Satish Kharat691a8372016-03-18 11:22:48 -07001099 case FCPIO_ITMF_REJECTED:
1100 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1101 "abort reject recd. id %d\n",
1102 (int)(id & FNIC_TAG_MASK));
1103 break;
Hiral Patel67125b02013-09-12 17:45:42 -07001104 case FCPIO_IO_NOT_FOUND:
1105 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1106 atomic64_inc(&abts_stats->abort_io_not_found);
1107 else
1108 atomic64_inc(
1109 &term_stats->terminate_io_not_found);
1110 break;
1111 default:
1112 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1113 atomic64_inc(&abts_stats->abort_failures);
1114 else
1115 atomic64_inc(
1116 &term_stats->terminate_failures);
1117 break;
1118 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001119 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1120 /* This is a late completion. Ignore it */
1121 spin_unlock_irqrestore(io_lock, flags);
1122 return;
1123 }
Satish Kharat691a8372016-03-18 11:22:48 -07001124
Hiral Patel14eb5d92013-02-12 17:01:01 -08001125 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Hiral Patel67125b02013-09-12 17:45:42 -07001126
Satish Kharat691a8372016-03-18 11:22:48 -07001127 /* If the status is IO not found consider it as success */
1128 if (hdr_status == FCPIO_IO_NOT_FOUND)
1129 CMD_ABTS_STATUS(sc) = FCPIO_SUCCESS;
1130 else
1131 CMD_ABTS_STATUS(sc) = hdr_status;
1132
Hiral Patel67125b02013-09-12 17:45:42 -07001133 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1134 atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1135
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001136 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1137 "abts cmpl recd. id %d status %s\n",
1138 (int)(id & FNIC_TAG_MASK),
1139 fnic_fcpio_status_to_str(hdr_status));
1140
1141 /*
1142 * If scsi_eh thread is blocked waiting for abts to complete,
1143 * signal completion to it. IO will be cleaned in the thread
1144 * else clean it in this context
1145 */
1146 if (io_req->abts_done) {
1147 complete(io_req->abts_done);
1148 spin_unlock_irqrestore(io_lock, flags);
1149 } else {
1150 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1151 "abts cmpl, completing IO\n");
1152 CMD_SP(sc) = NULL;
1153 sc->result = (DID_ERROR << 16);
1154
1155 spin_unlock_irqrestore(io_lock, flags);
1156
1157 fnic_release_ioreq_buf(fnic, io_req, sc);
1158 mempool_free(io_req, fnic->io_req_pool);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001159 if (sc->scsi_done) {
1160 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1161 sc->device->host->host_no, id,
1162 sc,
1163 jiffies_to_msecs(jiffies - start_time),
1164 desc,
1165 (((u64)hdr_status << 40) |
1166 (u64)sc->cmnd[0] << 32 |
1167 (u64)sc->cmnd[2] << 24 |
1168 (u64)sc->cmnd[3] << 16 |
1169 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1170 (((u64)CMD_FLAGS(sc) << 32) |
1171 CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001172 sc->scsi_done(sc);
Satish Kharat3dda4b42017-02-28 16:14:56 -08001173 atomic64_dec(&fnic_stats->io_stats.active_ios);
1174 if (atomic64_read(&fnic->io_cmpl_skip))
1175 atomic64_dec(&fnic->io_cmpl_skip);
1176 else
1177 atomic64_inc(&fnic_stats->io_stats.io_completions);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001178 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001179 }
1180
1181 } else if (id & FNIC_TAG_DEV_RST) {
1182 /* Completion of device reset */
1183 CMD_LR_STATUS(sc) = hdr_status;
Hiral Patel03298552013-02-12 17:00:58 -08001184 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1185 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001186 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001187 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1188 sc->device->host->host_no, id, sc,
1189 jiffies_to_msecs(jiffies - start_time),
1190 desc, 0,
1191 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001192 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1193 "Terminate pending "
1194 "dev reset cmpl recd. id %d status %s\n",
1195 (int)(id & FNIC_TAG_MASK),
1196 fnic_fcpio_status_to_str(hdr_status));
1197 return;
1198 }
1199 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1200 /* Need to wait for terminate completion */
1201 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001202 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1203 sc->device->host->host_no, id, sc,
1204 jiffies_to_msecs(jiffies - start_time),
1205 desc, 0,
1206 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001207 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1208 "dev reset cmpl recd after time out. "
1209 "id %d status %s\n",
1210 (int)(id & FNIC_TAG_MASK),
1211 fnic_fcpio_status_to_str(hdr_status));
1212 return;
1213 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001214 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
Hiral Patel03298552013-02-12 17:00:58 -08001215 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001216 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1217 "dev reset cmpl recd. id %d status %s\n",
1218 (int)(id & FNIC_TAG_MASK),
1219 fnic_fcpio_status_to_str(hdr_status));
1220 if (io_req->dr_done)
1221 complete(io_req->dr_done);
1222 spin_unlock_irqrestore(io_lock, flags);
1223
1224 } else {
1225 shost_printk(KERN_ERR, fnic->lport->host,
1226 "Unexpected itmf io state %s tag %x\n",
1227 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1228 spin_unlock_irqrestore(io_lock, flags);
1229 }
1230
1231}
1232
1233/*
1234 * fnic_fcpio_cmpl_handler
1235 * Routine to service the cq for wq_copy
1236 */
1237static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1238 unsigned int cq_index,
1239 struct fcpio_fw_req *desc)
1240{
1241 struct fnic *fnic = vnic_dev_priv(vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001242
1243 switch (desc->hdr.type) {
Hiral Patel67125b02013-09-12 17:45:42 -07001244 case FCPIO_ICMND_CMPL: /* fw completed a command */
1245 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1246 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1247 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1248 case FCPIO_RESET_CMPL: /* fw completed reset */
1249 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1250 break;
1251 default:
1252 break;
1253 }
1254
1255 switch (desc->hdr.type) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001256 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1257 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1258 break;
1259
1260 case FCPIO_ICMND_CMPL: /* fw completed a command */
1261 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1262 break;
1263
1264 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1265 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1266 break;
1267
1268 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
Joe Eykholt78112e52009-11-03 11:49:22 -08001269 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
Hiral Patel03298552013-02-12 17:00:58 -08001270 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001271 break;
1272
1273 case FCPIO_RESET_CMPL: /* fw completed reset */
Hiral Patel03298552013-02-12 17:00:58 -08001274 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001275 break;
1276
1277 default:
1278 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1279 "firmware completion type %d\n",
1280 desc->hdr.type);
1281 break;
1282 }
1283
Hiral Patel03298552013-02-12 17:00:58 -08001284 return 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001285}
1286
1287/*
1288 * fnic_wq_copy_cmpl_handler
1289 * Routine to process wq copy
1290 */
1291int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1292{
1293 unsigned int wq_work_done = 0;
1294 unsigned int i, cq_index;
1295 unsigned int cur_work_done;
1296
1297 for (i = 0; i < fnic->wq_copy_count; i++) {
1298 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1299 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1300 fnic_fcpio_cmpl_handler,
1301 copy_work_to_do);
1302 wq_work_done += cur_work_done;
1303 }
1304 return wq_work_done;
1305}
1306
1307static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1308{
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001309 int i;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001310 struct fnic_io_req *io_req;
1311 unsigned long flags = 0;
1312 struct scsi_cmnd *sc;
1313 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001314 unsigned long start_time = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07001315 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001316
Hiral Patelfc857992013-09-09 13:31:51 -07001317 for (i = 0; i < fnic->fnic_max_tag_id; i++) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001318 if (i == exclude_id)
1319 continue;
1320
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001321 io_lock = fnic_io_lock_tag(fnic, i);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001322 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001323 sc = scsi_host_find_tag(fnic->lport->host, i);
1324 if (!sc) {
1325 spin_unlock_irqrestore(io_lock, flags);
1326 continue;
1327 }
1328
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001329 io_req = (struct fnic_io_req *)CMD_SP(sc);
Hiral Patel03298552013-02-12 17:00:58 -08001330 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1331 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1332 /*
1333 * We will be here only when FW completes reset
1334 * without sending completions for outstanding ios.
1335 */
1336 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1337 if (io_req && io_req->dr_done)
1338 complete(io_req->dr_done);
1339 else if (io_req && io_req->abts_done)
1340 complete(io_req->abts_done);
1341 spin_unlock_irqrestore(io_lock, flags);
1342 continue;
1343 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1344 spin_unlock_irqrestore(io_lock, flags);
1345 continue;
1346 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001347 if (!io_req) {
1348 spin_unlock_irqrestore(io_lock, flags);
1349 goto cleanup_scsi_cmd;
1350 }
1351
1352 CMD_SP(sc) = NULL;
1353
1354 spin_unlock_irqrestore(io_lock, flags);
1355
1356 /*
1357 * If there is a scsi_cmnd associated with this io_req, then
1358 * free the corresponding state
1359 */
Hiral Patel14eb5d92013-02-12 17:01:01 -08001360 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001361 fnic_release_ioreq_buf(fnic, io_req, sc);
1362 mempool_free(io_req, fnic->io_req_pool);
1363
1364cleanup_scsi_cmd:
1365 sc->result = DID_TRANSPORT_DISRUPTED << 16;
Hiral Shah66818662014-04-18 12:28:18 -07001366 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1367 "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1368 __func__, (jiffies - start_time));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001369
Hiral Patel67125b02013-09-12 17:45:42 -07001370 if (atomic64_read(&fnic->io_cmpl_skip))
1371 atomic64_dec(&fnic->io_cmpl_skip);
1372 else
1373 atomic64_inc(&fnic_stats->io_stats.io_completions);
1374
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001375 /* Complete the command to SCSI */
Hiral Patel4d7007b2013-02-12 17:01:02 -08001376 if (sc->scsi_done) {
1377 FNIC_TRACE(fnic_cleanup_io,
1378 sc->device->host->host_no, i, sc,
1379 jiffies_to_msecs(jiffies - start_time),
1380 0, ((u64)sc->cmnd[0] << 32 |
1381 (u64)sc->cmnd[2] << 24 |
1382 (u64)sc->cmnd[3] << 16 |
1383 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1384 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1385
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001386 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001387 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001388 }
1389}
1390
1391void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1392 struct fcpio_host_req *desc)
1393{
1394 u32 id;
1395 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1396 struct fnic_io_req *io_req;
1397 struct scsi_cmnd *sc;
1398 unsigned long flags;
1399 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001400 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001401
1402 /* get the tag reference */
1403 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1404 id &= FNIC_TAG_MASK;
1405
Hiral Patelfc857992013-09-09 13:31:51 -07001406 if (id >= fnic->fnic_max_tag_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001407 return;
1408
1409 sc = scsi_host_find_tag(fnic->lport->host, id);
1410 if (!sc)
1411 return;
1412
1413 io_lock = fnic_io_lock_hash(fnic, sc);
1414 spin_lock_irqsave(io_lock, flags);
1415
1416 /* Get the IO context which this desc refers to */
1417 io_req = (struct fnic_io_req *)CMD_SP(sc);
1418
1419 /* fnic interrupts are turned off by now */
1420
1421 if (!io_req) {
1422 spin_unlock_irqrestore(io_lock, flags);
1423 goto wq_copy_cleanup_scsi_cmd;
1424 }
1425
1426 CMD_SP(sc) = NULL;
1427
1428 spin_unlock_irqrestore(io_lock, flags);
1429
Hiral Patel14eb5d92013-02-12 17:01:01 -08001430 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001431 fnic_release_ioreq_buf(fnic, io_req, sc);
1432 mempool_free(io_req, fnic->io_req_pool);
1433
1434wq_copy_cleanup_scsi_cmd:
1435 sc->result = DID_NO_CONNECT << 16;
1436 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1437 " DID_NO_CONNECT\n");
1438
Hiral Patel4d7007b2013-02-12 17:01:02 -08001439 if (sc->scsi_done) {
1440 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1441 sc->device->host->host_no, id, sc,
1442 jiffies_to_msecs(jiffies - start_time),
1443 0, ((u64)sc->cmnd[0] << 32 |
1444 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1445 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1446 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1447
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001448 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001449 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001450}
1451
1452static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1453 u32 task_req, u8 *fc_lun,
1454 struct fnic_io_req *io_req)
1455{
1456 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001457 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001458 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001459 unsigned long flags;
1460
Hiral Patel03298552013-02-12 17:00:58 -08001461 spin_lock_irqsave(host->host_lock, flags);
1462 if (unlikely(fnic_chk_state_flags_locked(fnic,
1463 FNIC_FLAGS_IO_BLOCKED))) {
1464 spin_unlock_irqrestore(host->host_lock, flags);
1465 return 1;
1466 } else
1467 atomic_inc(&fnic->in_flight);
1468 spin_unlock_irqrestore(host->host_lock, flags);
1469
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001470 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1471
1472 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1473 free_wq_copy_descs(fnic, wq);
1474
1475 if (!vnic_wq_copy_desc_avail(wq)) {
1476 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001477 atomic_dec(&fnic->in_flight);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001478 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001479 "fnic_queue_abort_io_req: failure: no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07001480 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001481 return 1;
1482 }
1483 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1484 0, task_req, tag, fc_lun, io_req->port_id,
1485 fnic->config.ra_tov, fnic->config.ed_tov);
1486
Hiral Patel67125b02013-09-12 17:45:42 -07001487 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1488 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1489 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1490 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1491 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1492
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001493 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001494 atomic_dec(&fnic->in_flight);
1495
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001496 return 0;
1497}
1498
Hiral Patel03298552013-02-12 17:00:58 -08001499static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001500{
1501 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001502 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001503 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001504 struct fnic_io_req *io_req;
1505 spinlock_t *io_lock;
1506 unsigned long flags;
1507 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -07001508 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1509 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001510 struct scsi_lun fc_lun;
1511 enum fnic_ioreq_state old_ioreq_state;
1512
1513 FNIC_SCSI_DBG(KERN_DEBUG,
1514 fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001515 "fnic_rport_exch_reset called portid 0x%06x\n",
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001516 port_id);
1517
1518 if (fnic->in_remove)
1519 return;
1520
Hiral Patelfc857992013-09-09 13:31:51 -07001521 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001522 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001523 io_lock = fnic_io_lock_tag(fnic, tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001524 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001525 sc = scsi_host_find_tag(fnic->lport->host, tag);
1526 if (!sc) {
1527 spin_unlock_irqrestore(io_lock, flags);
1528 continue;
1529 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001530
1531 io_req = (struct fnic_io_req *)CMD_SP(sc);
1532
1533 if (!io_req || io_req->port_id != port_id) {
1534 spin_unlock_irqrestore(io_lock, flags);
1535 continue;
1536 }
1537
Hiral Patel03298552013-02-12 17:00:58 -08001538 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001539 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001540 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1541 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1542 sc);
1543 spin_unlock_irqrestore(io_lock, flags);
1544 continue;
1545 }
1546
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001547 /*
1548 * Found IO that is still pending with firmware and
1549 * belongs to rport that went away
1550 */
1551 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1552 spin_unlock_irqrestore(io_lock, flags);
1553 continue;
1554 }
Hiral Patel03298552013-02-12 17:00:58 -08001555 if (io_req->abts_done) {
1556 shost_printk(KERN_ERR, fnic->lport->host,
1557 "fnic_rport_exch_reset: io_req->abts_done is set "
1558 "state is %s\n",
1559 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1560 }
1561
Hiral Patel14eb5d92013-02-12 17:01:01 -08001562 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1563 shost_printk(KERN_ERR, fnic->lport->host,
1564 "rport_exch_reset "
1565 "IO not yet issued %p tag 0x%x flags "
1566 "%x state %d\n",
1567 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1568 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001569 old_ioreq_state = CMD_STATE(sc);
1570 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1571 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001572 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001573 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001574 abt_tag = (tag | FNIC_TAG_DEV_RST);
1575 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1576 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1577 sc);
1578 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001579
1580 BUG_ON(io_req->abts_done);
1581
1582 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1583 "fnic_rport_reset_exch: Issuing abts\n");
1584
1585 spin_unlock_irqrestore(io_lock, flags);
1586
1587 /* Now queue the abort command to firmware */
1588 int_to_scsilun(sc->device->lun, &fc_lun);
1589
Hiral Patel03298552013-02-12 17:00:58 -08001590 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001591 FCPIO_ITMF_ABT_TASK_TERM,
1592 fc_lun.scsi_lun, io_req)) {
1593 /*
1594 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001595 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001596 * aborted later by scsi_eh, or cleaned up during
1597 * lun reset
1598 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001599 spin_lock_irqsave(io_lock, flags);
1600 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1601 CMD_STATE(sc) = old_ioreq_state;
1602 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001603 } else {
1604 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001605 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1606 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001607 else
1608 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001609 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001610 atomic64_inc(&term_stats->terminates);
1611 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001612 }
1613 }
Hiral Patel67125b02013-09-12 17:45:42 -07001614 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1615 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001616
1617}
1618
1619void fnic_terminate_rport_io(struct fc_rport *rport)
1620{
1621 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001622 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001623 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001624 struct fnic_io_req *io_req;
1625 spinlock_t *io_lock;
1626 unsigned long flags;
1627 struct scsi_cmnd *sc;
1628 struct scsi_lun fc_lun;
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001629 struct fc_rport_libfc_priv *rdata;
1630 struct fc_lport *lport;
1631 struct fnic *fnic;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001632 struct fc_rport *cmd_rport;
Hiral Patel67125b02013-09-12 17:45:42 -07001633 struct reset_stats *reset_stats;
1634 struct terminate_stats *term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001635 enum fnic_ioreq_state old_ioreq_state;
1636
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001637 if (!rport) {
1638 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1639 return;
1640 }
1641 rdata = rport->dd_data;
1642
1643 if (!rdata) {
1644 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1645 return;
1646 }
1647 lport = rdata->local_port;
1648
1649 if (!lport) {
1650 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1651 return;
1652 }
1653 fnic = lport_priv(lport);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001654 FNIC_SCSI_DBG(KERN_DEBUG,
1655 fnic->lport->host, "fnic_terminate_rport_io called"
Hiral Patel03298552013-02-12 17:00:58 -08001656 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1657 rport->port_name, rport->node_name, rport,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001658 rport->port_id);
1659
1660 if (fnic->in_remove)
1661 return;
1662
Hiral Patel67125b02013-09-12 17:45:42 -07001663 reset_stats = &fnic->fnic_stats.reset_stats;
1664 term_stats = &fnic->fnic_stats.term_stats;
1665
Hiral Patelfc857992013-09-09 13:31:51 -07001666 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001667 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001668 io_lock = fnic_io_lock_tag(fnic, tag);
1669 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001670 sc = scsi_host_find_tag(fnic->lport->host, tag);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001671 if (!sc) {
1672 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001673 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001674 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001675
1676 cmd_rport = starget_to_rport(scsi_target(sc->device));
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001677 if (rport != cmd_rport) {
1678 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001679 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001680 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001681
1682 io_req = (struct fnic_io_req *)CMD_SP(sc);
1683
1684 if (!io_req || rport != cmd_rport) {
1685 spin_unlock_irqrestore(io_lock, flags);
1686 continue;
1687 }
1688
Hiral Patel03298552013-02-12 17:00:58 -08001689 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001690 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001691 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1692 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1693 sc);
1694 spin_unlock_irqrestore(io_lock, flags);
1695 continue;
1696 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001697 /*
1698 * Found IO that is still pending with firmware and
1699 * belongs to rport that went away
1700 */
1701 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1702 spin_unlock_irqrestore(io_lock, flags);
1703 continue;
1704 }
Hiral Patel03298552013-02-12 17:00:58 -08001705 if (io_req->abts_done) {
1706 shost_printk(KERN_ERR, fnic->lport->host,
1707 "fnic_terminate_rport_io: io_req->abts_done is set "
1708 "state is %s\n",
1709 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1710 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001711 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1712 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1713 "fnic_terminate_rport_io "
1714 "IO not yet issued %p tag 0x%x flags "
1715 "%x state %d\n",
1716 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1717 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001718 old_ioreq_state = CMD_STATE(sc);
1719 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1720 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001721 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001722 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001723 abt_tag = (tag | FNIC_TAG_DEV_RST);
1724 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1725 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1726 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001727
1728 BUG_ON(io_req->abts_done);
1729
1730 FNIC_SCSI_DBG(KERN_DEBUG,
1731 fnic->lport->host,
1732 "fnic_terminate_rport_io: Issuing abts\n");
1733
1734 spin_unlock_irqrestore(io_lock, flags);
1735
1736 /* Now queue the abort command to firmware */
1737 int_to_scsilun(sc->device->lun, &fc_lun);
1738
Hiral Patel03298552013-02-12 17:00:58 -08001739 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001740 FCPIO_ITMF_ABT_TASK_TERM,
1741 fc_lun.scsi_lun, io_req)) {
1742 /*
1743 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001744 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001745 * aborted later by scsi_eh, or cleaned up during
1746 * lun reset
1747 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001748 spin_lock_irqsave(io_lock, flags);
1749 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1750 CMD_STATE(sc) = old_ioreq_state;
1751 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001752 } else {
1753 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001754 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1755 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001756 else
1757 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001758 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001759 atomic64_inc(&term_stats->terminates);
1760 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001761 }
1762 }
Hiral Patel67125b02013-09-12 17:45:42 -07001763 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1764 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001765
1766}
1767
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001768/*
1769 * This function is exported to SCSI for sending abort cmnds.
1770 * A SCSI IO is represented by a io_req in the driver.
1771 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1772 */
1773int fnic_abort_cmd(struct scsi_cmnd *sc)
1774{
1775 struct fc_lport *lp;
1776 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001777 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001778 struct fc_rport *rport;
1779 spinlock_t *io_lock;
1780 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001781 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001782 int ret = SUCCESS;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001783 u32 task_req = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001784 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07001785 struct fnic_stats *fnic_stats;
1786 struct abort_stats *abts_stats;
1787 struct terminate_stats *term_stats;
Hiral Shah66818662014-04-18 12:28:18 -07001788 enum fnic_ioreq_state old_ioreq_state;
Hiral Patel03298552013-02-12 17:00:58 -08001789 int tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001790 DECLARE_COMPLETION_ONSTACK(tm_done);
1791
1792 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001793 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001794
1795 /* Get local-port, check ready and link up */
1796 lp = shost_priv(sc->device->host);
1797
1798 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07001799 fnic_stats = &fnic->fnic_stats;
1800 abts_stats = &fnic->fnic_stats.abts_stats;
1801 term_stats = &fnic->fnic_stats.term_stats;
1802
Roel Kluin0db6f432010-06-11 16:44:46 -07001803 rport = starget_to_rport(scsi_target(sc->device));
Hiral Patel03298552013-02-12 17:00:58 -08001804 tag = sc->request->tag;
1805 FNIC_SCSI_DBG(KERN_DEBUG,
1806 fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001807 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
Hiral Patel03298552013-02-12 17:00:58 -08001808 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1809
1810 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1811
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001812 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1813 ret = FAILED;
1814 goto fnic_abort_cmd_end;
1815 }
1816
1817 /*
1818 * Avoid a race between SCSI issuing the abort and the device
1819 * completing the command.
1820 *
1821 * If the command is already completed by the fw cmpl code,
1822 * we just return SUCCESS from here. This means that the abort
1823 * succeeded. In the SCSI ML, since the timeout for command has
1824 * happened, the completion wont actually complete the command
1825 * and it will be considered as an aborted command
1826 *
1827 * The CMD_SP will not be cleared except while holding io_req_lock.
1828 */
1829 io_lock = fnic_io_lock_hash(fnic, sc);
1830 spin_lock_irqsave(io_lock, flags);
1831 io_req = (struct fnic_io_req *)CMD_SP(sc);
1832 if (!io_req) {
1833 spin_unlock_irqrestore(io_lock, flags);
1834 goto fnic_abort_cmd_end;
1835 }
1836
1837 io_req->abts_done = &tm_done;
1838
1839 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1840 spin_unlock_irqrestore(io_lock, flags);
1841 goto wait_pending;
1842 }
1843 /*
1844 * Command is still pending, need to abort it
1845 * If the firmware completes the command after this point,
1846 * the completion wont be done till mid-layer, since abort
1847 * has already started.
1848 */
Hiral Shah66818662014-04-18 12:28:18 -07001849 old_ioreq_state = CMD_STATE(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001850 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1851 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1852
1853 spin_unlock_irqrestore(io_lock, flags);
1854
1855 /*
1856 * Check readiness of the remote port. If the path to remote
1857 * port is up, then send abts to the remote port to terminate
1858 * the IO. Else, just locally terminate the IO in the firmware
1859 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001860 if (fc_remote_port_chkready(rport) == 0)
1861 task_req = FCPIO_ITMF_ABT_TASK;
Hiral Patel67125b02013-09-12 17:45:42 -07001862 else {
1863 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001864 task_req = FCPIO_ITMF_ABT_TASK_TERM;
Hiral Patel67125b02013-09-12 17:45:42 -07001865 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001866
1867 /* Now queue the abort command to firmware */
1868 int_to_scsilun(sc->device->lun, &fc_lun);
1869
1870 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1871 fc_lun.scsi_lun, io_req)) {
1872 spin_lock_irqsave(io_lock, flags);
Hiral Shah66818662014-04-18 12:28:18 -07001873 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1874 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001875 io_req = (struct fnic_io_req *)CMD_SP(sc);
1876 if (io_req)
1877 io_req->abts_done = NULL;
1878 spin_unlock_irqrestore(io_lock, flags);
1879 ret = FAILED;
1880 goto fnic_abort_cmd_end;
1881 }
Hiral Patel67125b02013-09-12 17:45:42 -07001882 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001883 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001884 atomic64_inc(&fnic_stats->abts_stats.aborts);
1885 } else {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001886 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001887 atomic64_inc(&fnic_stats->term_stats.terminates);
1888 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001889
1890 /*
1891 * We queued an abort IO, wait for its completion.
1892 * Once the firmware completes the abort command, it will
1893 * wake up this thread.
1894 */
1895 wait_pending:
1896 wait_for_completion_timeout(&tm_done,
1897 msecs_to_jiffies
1898 (2 * fnic->config.ra_tov +
1899 fnic->config.ed_tov));
1900
1901 /* Check the abort status */
1902 spin_lock_irqsave(io_lock, flags);
1903
1904 io_req = (struct fnic_io_req *)CMD_SP(sc);
1905 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001906 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001907 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001908 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001909 ret = FAILED;
1910 goto fnic_abort_cmd_end;
1911 }
1912 io_req->abts_done = NULL;
1913
1914 /* fw did not complete abort, timed out */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001915 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001916 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001917 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel67125b02013-09-12 17:45:42 -07001918 atomic64_inc(&abts_stats->abort_drv_timeouts);
1919 } else {
Hiral Patel67125b02013-09-12 17:45:42 -07001920 atomic64_inc(&term_stats->terminate_drv_timeouts);
1921 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001922 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001923 ret = FAILED;
1924 goto fnic_abort_cmd_end;
1925 }
1926
Anil Chintalapati (achintal)efc7a282014-12-23 19:40:00 +00001927 /* IO out of order */
1928
1929 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1930 spin_unlock_irqrestore(io_lock, flags);
1931 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1932 "Issuing Host reset due to out of order IO\n");
1933
1934 if (fnic_host_reset(sc) == FAILED) {
1935 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1936 "fnic_host_reset failed.\n");
1937 }
1938 ret = FAILED;
1939 goto fnic_abort_cmd_end;
1940 }
1941
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001942 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1943
Satish Kharat691a8372016-03-18 11:22:48 -07001944 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001945 /*
1946 * firmware completed the abort, check the status,
Satish Kharat691a8372016-03-18 11:22:48 -07001947 * free the io_req if successful. If abort fails,
1948 * Device reset will clean the I/O.
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001949 */
Satish Kharat691a8372016-03-18 11:22:48 -07001950 if (CMD_ABTS_STATUS(sc) == FCPIO_SUCCESS)
1951 CMD_SP(sc) = NULL;
1952 else {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001953 ret = FAILED;
Satish Kharat691a8372016-03-18 11:22:48 -07001954 spin_unlock_irqrestore(io_lock, flags);
1955 goto fnic_abort_cmd_end;
1956 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001957
1958 spin_unlock_irqrestore(io_lock, flags);
1959
1960 fnic_release_ioreq_buf(fnic, io_req, sc);
1961 mempool_free(io_req, fnic->io_req_pool);
1962
Satish Kharat691a8372016-03-18 11:22:48 -07001963 if (sc->scsi_done) {
1964 /* Call SCSI completion function to complete the IO */
1965 sc->result = (DID_ABORT << 16);
1966 sc->scsi_done(sc);
Satish Kharat3dda4b42017-02-28 16:14:56 -08001967 atomic64_dec(&fnic_stats->io_stats.active_ios);
1968 if (atomic64_read(&fnic->io_cmpl_skip))
1969 atomic64_dec(&fnic->io_cmpl_skip);
1970 else
1971 atomic64_inc(&fnic_stats->io_stats.io_completions);
Satish Kharat691a8372016-03-18 11:22:48 -07001972 }
1973
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001974fnic_abort_cmd_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08001975 FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1976 sc->request->tag, sc,
1977 jiffies_to_msecs(jiffies - start_time),
1978 0, ((u64)sc->cmnd[0] << 32 |
1979 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1980 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1981 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1982
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001983 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel14eb5d92013-02-12 17:01:01 -08001984 "Returning from abort cmd type %x %s\n", task_req,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001985 (ret == SUCCESS) ?
1986 "SUCCESS" : "FAILED");
1987 return ret;
1988}
1989
1990static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1991 struct scsi_cmnd *sc,
1992 struct fnic_io_req *io_req)
1993{
1994 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001995 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001996 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001997 struct scsi_lun fc_lun;
1998 int ret = 0;
1999 unsigned long intr_flags;
2000
Hiral Patel03298552013-02-12 17:00:58 -08002001 spin_lock_irqsave(host->host_lock, intr_flags);
2002 if (unlikely(fnic_chk_state_flags_locked(fnic,
2003 FNIC_FLAGS_IO_BLOCKED))) {
2004 spin_unlock_irqrestore(host->host_lock, intr_flags);
2005 return FAILED;
2006 } else
2007 atomic_inc(&fnic->in_flight);
2008 spin_unlock_irqrestore(host->host_lock, intr_flags);
2009
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002010 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
2011
2012 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
2013 free_wq_copy_descs(fnic, wq);
2014
2015 if (!vnic_wq_copy_desc_avail(wq)) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002016 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2017 "queue_dr_io_req failure - no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07002018 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002019 ret = -EAGAIN;
2020 goto lr_io_req_end;
2021 }
2022
2023 /* fill in the lun info */
2024 int_to_scsilun(sc->device->lun, &fc_lun);
2025
2026 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
2027 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
2028 fc_lun.scsi_lun, io_req->port_id,
2029 fnic->config.ra_tov, fnic->config.ed_tov);
2030
Hiral Patel67125b02013-09-12 17:45:42 -07002031 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
2032 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
2033 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
2034 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
2035 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
2036
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002037lr_io_req_end:
2038 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel03298552013-02-12 17:00:58 -08002039 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002040
2041 return ret;
2042}
2043
2044/*
2045 * Clean up any pending aborts on the lun
2046 * For each outstanding IO on this lun, whose abort is not completed by fw,
2047 * issue a local abort. Wait for abort to complete. Return 0 if all commands
2048 * successfully aborted, 1 otherwise
2049 */
2050static int fnic_clean_pending_aborts(struct fnic *fnic,
Satish Kharata36f5dd2016-03-18 11:22:49 -07002051 struct scsi_cmnd *lr_sc,
2052 bool new_sc)
2053
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002054{
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002055 int tag, abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002056 struct fnic_io_req *io_req;
2057 spinlock_t *io_lock;
2058 unsigned long flags;
2059 int ret = 0;
2060 struct scsi_cmnd *sc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002061 struct scsi_lun fc_lun;
2062 struct scsi_device *lun_dev = lr_sc->device;
2063 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002064 enum fnic_ioreq_state old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002065
Hiral Patelfc857992013-09-09 13:31:51 -07002066 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002067 io_lock = fnic_io_lock_tag(fnic, tag);
2068 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002069 sc = scsi_host_find_tag(fnic->lport->host, tag);
2070 /*
Satish Kharata36f5dd2016-03-18 11:22:49 -07002071 * ignore this lun reset cmd if issued using new SC
2072 * or cmds that do not belong to this lun
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002073 */
Satish Kharata36f5dd2016-03-18 11:22:49 -07002074 if (!sc || ((sc == lr_sc) && new_sc) || sc->device != lun_dev) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002075 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002076 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002077 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002078
2079 io_req = (struct fnic_io_req *)CMD_SP(sc);
2080
2081 if (!io_req || sc->device != lun_dev) {
2082 spin_unlock_irqrestore(io_lock, flags);
2083 continue;
2084 }
2085
2086 /*
2087 * Found IO that is still pending with firmware and
2088 * belongs to the LUN that we are resetting
2089 */
2090 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2091 "Found IO in %s on lun\n",
2092 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2093
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002094 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2095 spin_unlock_irqrestore(io_lock, flags);
2096 continue;
2097 }
2098 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08002099 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002100 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2101 "%s dev rst not pending sc 0x%p\n", __func__,
2102 sc);
2103 spin_unlock_irqrestore(io_lock, flags);
2104 continue;
2105 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002106
2107 if (io_req->abts_done)
2108 shost_printk(KERN_ERR, fnic->lport->host,
2109 "%s: io_req->abts_done is set state is %s\n",
2110 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002111 old_ioreq_state = CMD_STATE(sc);
2112 /*
2113 * Any pending IO issued prior to reset is expected to be
2114 * in abts pending state, if not we need to set
2115 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2116 * When IO is completed, the IO will be handed over and
2117 * handled in this function.
2118 */
2119 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2120
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002121 BUG_ON(io_req->abts_done);
2122
2123 abt_tag = tag;
2124 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2125 abt_tag |= FNIC_TAG_DEV_RST;
2126 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2127 "%s: dev rst sc 0x%p\n", __func__, sc);
2128 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002129
2130 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2131 io_req->abts_done = &tm_done;
2132 spin_unlock_irqrestore(io_lock, flags);
2133
2134 /* Now queue the abort command to firmware */
2135 int_to_scsilun(sc->device->lun, &fc_lun);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002136
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002137 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002138 FCPIO_ITMF_ABT_TASK_TERM,
2139 fc_lun.scsi_lun, io_req)) {
2140 spin_lock_irqsave(io_lock, flags);
2141 io_req = (struct fnic_io_req *)CMD_SP(sc);
2142 if (io_req)
2143 io_req->abts_done = NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002144 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2145 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002146 spin_unlock_irqrestore(io_lock, flags);
2147 ret = 1;
2148 goto clean_pending_aborts_end;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002149 } else {
2150 spin_lock_irqsave(io_lock, flags);
2151 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2152 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2153 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002154 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08002155 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002156
2157 wait_for_completion_timeout(&tm_done,
2158 msecs_to_jiffies
2159 (fnic->config.ed_tov));
2160
2161 /* Recheck cmd state to check if it is now aborted */
2162 spin_lock_irqsave(io_lock, flags);
2163 io_req = (struct fnic_io_req *)CMD_SP(sc);
2164 if (!io_req) {
2165 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002166 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002167 continue;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002168 }
2169
2170 io_req->abts_done = NULL;
2171
2172 /* if abort is still pending with fw, fail */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002173 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002174 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002175 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002176 ret = 1;
2177 goto clean_pending_aborts_end;
2178 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002179 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
Satish Kharata36f5dd2016-03-18 11:22:49 -07002180
2181 /* original sc used for lr is handled by dev reset code */
2182 if (sc != lr_sc)
2183 CMD_SP(sc) = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002184 spin_unlock_irqrestore(io_lock, flags);
2185
Satish Kharata36f5dd2016-03-18 11:22:49 -07002186 /* original sc used for lr is handled by dev reset code */
2187 if (sc != lr_sc) {
2188 fnic_release_ioreq_buf(fnic, io_req, sc);
2189 mempool_free(io_req, fnic->io_req_pool);
2190 }
2191
2192 /*
2193 * Any IO is returned during reset, it needs to call scsi_done
2194 * to return the scsi_cmnd to upper layer.
2195 */
2196 if (sc->scsi_done) {
2197 /* Set result to let upper SCSI layer retry */
2198 sc->result = DID_RESET << 16;
2199 sc->scsi_done(sc);
2200 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002201 }
2202
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002203 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2204
2205 /* walk again to check, if IOs are still pending in fw */
2206 if (fnic_is_abts_pending(fnic, lr_sc))
2207 ret = FAILED;
2208
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002209clean_pending_aborts_end:
2210 return ret;
2211}
2212
Hiral Patel03298552013-02-12 17:00:58 -08002213/**
2214 * fnic_scsi_host_start_tag
2215 * Allocates tagid from host's tag list
2216 **/
2217static inline int
2218fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2219{
2220 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2221 int tag, ret = SCSI_NO_TAG;
2222
2223 BUG_ON(!bqt);
2224 if (!bqt) {
2225 pr_err("Tags are not supported\n");
2226 goto end;
2227 }
2228
2229 do {
2230 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2231 if (tag >= bqt->max_depth) {
2232 pr_err("Tag allocation failure\n");
2233 goto end;
2234 }
2235 } while (test_and_set_bit(tag, bqt->tag_map));
2236
2237 bqt->tag_index[tag] = sc->request;
2238 sc->request->tag = tag;
2239 sc->tag = tag;
2240 if (!sc->request->special)
2241 sc->request->special = sc;
2242
2243 ret = tag;
2244
2245end:
2246 return ret;
2247}
2248
2249/**
2250 * fnic_scsi_host_end_tag
2251 * frees tag allocated by fnic_scsi_host_start_tag.
2252 **/
2253static inline void
2254fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2255{
2256 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2257 int tag = sc->request->tag;
2258
2259 if (tag == SCSI_NO_TAG)
2260 return;
2261
2262 BUG_ON(!bqt || !bqt->tag_index[tag]);
2263 if (!bqt)
2264 return;
2265
2266 bqt->tag_index[tag] = NULL;
2267 clear_bit(tag, bqt->tag_map);
2268
2269 return;
2270}
2271
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002272/*
2273 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2274 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2275 * on the LUN.
2276 */
2277int fnic_device_reset(struct scsi_cmnd *sc)
2278{
2279 struct fc_lport *lp;
2280 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002281 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002282 struct fc_rport *rport;
2283 int status;
2284 int ret = FAILED;
2285 spinlock_t *io_lock;
2286 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08002287 unsigned long start_time = 0;
Hiral Patel03298552013-02-12 17:00:58 -08002288 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07002289 struct fnic_stats *fnic_stats;
2290 struct reset_stats *reset_stats;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002291 int tag = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002292 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patel03298552013-02-12 17:00:58 -08002293 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
Satish Kharata36f5dd2016-03-18 11:22:49 -07002294 bool new_sc = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002295
2296 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01002297 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002298
2299 /* Get local-port, check ready and link up */
2300 lp = shost_priv(sc->device->host);
2301
2302 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002303 fnic_stats = &fnic->fnic_stats;
2304 reset_stats = &fnic->fnic_stats.reset_stats;
2305
2306 atomic64_inc(&reset_stats->device_resets);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002307
Roel Kluin0db6f432010-06-11 16:44:46 -07002308 rport = starget_to_rport(scsi_target(sc->device));
2309 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002310 "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
Hiral Patel03298552013-02-12 17:00:58 -08002311 rport->port_id, sc->device->lun, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002312
2313 if (lp->state != LPORT_ST_READY || !(lp->link_up))
2314 goto fnic_device_reset_end;
2315
2316 /* Check if remote port up */
Hiral Patel67125b02013-09-12 17:45:42 -07002317 if (fc_remote_port_chkready(rport)) {
2318 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002319 goto fnic_device_reset_end;
Hiral Patel67125b02013-09-12 17:45:42 -07002320 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002321
Hiral Patel14eb5d92013-02-12 17:01:01 -08002322 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
Hiral Patel03298552013-02-12 17:00:58 -08002323 /* Allocate tag if not present */
2324
2325 tag = sc->request->tag;
2326 if (unlikely(tag < 0)) {
Christoph Hellwig4d637162014-06-12 14:53:23 +02002327 /*
2328 * XXX(hch): current the midlayer fakes up a struct
2329 * request for the explicit reset ioctls, and those
2330 * don't have a tag allocated to them. The below
2331 * code pokes into midlayer structures to paper over
2332 * this design issue, but that won't work for blk-mq.
2333 *
2334 * Either someone who can actually test the hardware
2335 * will have to come up with a similar hack for the
2336 * blk-mq case, or we'll have to bite the bullet and
2337 * fix the way the EH ioctls work for real, but until
2338 * that happens we fail these explicit requests here.
2339 */
Christoph Hellwig4d637162014-06-12 14:53:23 +02002340
Hiral Patel03298552013-02-12 17:00:58 -08002341 tag = fnic_scsi_host_start_tag(fnic, sc);
2342 if (unlikely(tag == SCSI_NO_TAG))
2343 goto fnic_device_reset_end;
2344 tag_gen_flag = 1;
Satish Kharata36f5dd2016-03-18 11:22:49 -07002345 new_sc = 1;
Hiral Patel03298552013-02-12 17:00:58 -08002346 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002347 io_lock = fnic_io_lock_hash(fnic, sc);
2348 spin_lock_irqsave(io_lock, flags);
2349 io_req = (struct fnic_io_req *)CMD_SP(sc);
2350
2351 /*
2352 * If there is a io_req attached to this command, then use it,
2353 * else allocate a new one.
2354 */
2355 if (!io_req) {
2356 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2357 if (!io_req) {
2358 spin_unlock_irqrestore(io_lock, flags);
2359 goto fnic_device_reset_end;
2360 }
2361 memset(io_req, 0, sizeof(*io_req));
2362 io_req->port_id = rport->port_id;
2363 CMD_SP(sc) = (char *)io_req;
2364 }
2365 io_req->dr_done = &tm_done;
2366 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2367 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2368 spin_unlock_irqrestore(io_lock, flags);
2369
Hiral Patel03298552013-02-12 17:00:58 -08002370 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002371
2372 /*
2373 * issue the device reset, if enqueue failed, clean up the ioreq
2374 * and break assoc with scsi cmd
2375 */
2376 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2377 spin_lock_irqsave(io_lock, flags);
2378 io_req = (struct fnic_io_req *)CMD_SP(sc);
2379 if (io_req)
2380 io_req->dr_done = NULL;
2381 goto fnic_device_reset_clean;
2382 }
Hiral Patel03298552013-02-12 17:00:58 -08002383 spin_lock_irqsave(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002384 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08002385 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002386
2387 /*
2388 * Wait on the local completion for LUN reset. The io_req may be
2389 * freed while we wait since we hold no lock.
2390 */
2391 wait_for_completion_timeout(&tm_done,
2392 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2393
2394 spin_lock_irqsave(io_lock, flags);
2395 io_req = (struct fnic_io_req *)CMD_SP(sc);
2396 if (!io_req) {
2397 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002398 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2399 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002400 goto fnic_device_reset_end;
2401 }
2402 io_req->dr_done = NULL;
2403
2404 status = CMD_LR_STATUS(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002405
2406 /*
2407 * If lun reset not completed, bail out with failed. io_req
2408 * gets cleaned up during higher levels of EH
2409 */
2410 if (status == FCPIO_INVALID_CODE) {
Hiral Patel67125b02013-09-12 17:45:42 -07002411 atomic64_inc(&reset_stats->device_reset_timeouts);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002412 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2413 "Device reset timed out\n");
Hiral Patel03298552013-02-12 17:00:58 -08002414 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2415 spin_unlock_irqrestore(io_lock, flags);
2416 int_to_scsilun(sc->device->lun, &fc_lun);
2417 /*
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002418 * Issue abort and terminate on device reset request.
2419 * If q'ing of terminate fails, retry it after a delay.
Hiral Patel03298552013-02-12 17:00:58 -08002420 */
2421 while (1) {
2422 spin_lock_irqsave(io_lock, flags);
2423 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2424 spin_unlock_irqrestore(io_lock, flags);
2425 break;
2426 }
2427 spin_unlock_irqrestore(io_lock, flags);
2428 if (fnic_queue_abort_io_req(fnic,
2429 tag | FNIC_TAG_DEV_RST,
2430 FCPIO_ITMF_ABT_TASK_TERM,
2431 fc_lun.scsi_lun, io_req)) {
2432 wait_for_completion_timeout(&tm_done,
2433 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2434 } else {
2435 spin_lock_irqsave(io_lock, flags);
2436 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2437 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2438 io_req->abts_done = &tm_done;
2439 spin_unlock_irqrestore(io_lock, flags);
2440 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2441 "Abort and terminate issued on Device reset "
2442 "tag 0x%x sc 0x%p\n", tag, sc);
2443 break;
2444 }
2445 }
2446 while (1) {
2447 spin_lock_irqsave(io_lock, flags);
2448 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2449 spin_unlock_irqrestore(io_lock, flags);
2450 wait_for_completion_timeout(&tm_done,
2451 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2452 break;
2453 } else {
2454 io_req = (struct fnic_io_req *)CMD_SP(sc);
2455 io_req->abts_done = NULL;
2456 goto fnic_device_reset_clean;
2457 }
2458 }
2459 } else {
2460 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002461 }
2462
2463 /* Completed, but not successful, clean up the io_req, return fail */
2464 if (status != FCPIO_SUCCESS) {
2465 spin_lock_irqsave(io_lock, flags);
2466 FNIC_SCSI_DBG(KERN_DEBUG,
2467 fnic->lport->host,
2468 "Device reset completed - failed\n");
2469 io_req = (struct fnic_io_req *)CMD_SP(sc);
2470 goto fnic_device_reset_clean;
2471 }
2472
2473 /*
2474 * Clean up any aborts on this lun that have still not
2475 * completed. If any of these fail, then LUN reset fails.
2476 * clean_pending_aborts cleans all cmds on this lun except
2477 * the lun reset cmd. If all cmds get cleaned, the lun reset
2478 * succeeds
2479 */
Satish Kharata36f5dd2016-03-18 11:22:49 -07002480 if (fnic_clean_pending_aborts(fnic, sc, new_sc)) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002481 spin_lock_irqsave(io_lock, flags);
2482 io_req = (struct fnic_io_req *)CMD_SP(sc);
2483 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2484 "Device reset failed"
2485 " since could not abort all IOs\n");
2486 goto fnic_device_reset_clean;
2487 }
2488
2489 /* Clean lun reset command */
2490 spin_lock_irqsave(io_lock, flags);
2491 io_req = (struct fnic_io_req *)CMD_SP(sc);
2492 if (io_req)
2493 /* Completed, and successful */
2494 ret = SUCCESS;
2495
2496fnic_device_reset_clean:
2497 if (io_req)
2498 CMD_SP(sc) = NULL;
2499
2500 spin_unlock_irqrestore(io_lock, flags);
2501
2502 if (io_req) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002503 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002504 fnic_release_ioreq_buf(fnic, io_req, sc);
2505 mempool_free(io_req, fnic->io_req_pool);
2506 }
2507
2508fnic_device_reset_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08002509 FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2510 sc->request->tag, sc,
2511 jiffies_to_msecs(jiffies - start_time),
2512 0, ((u64)sc->cmnd[0] << 32 |
2513 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2514 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2515 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2516
Hiral Patel03298552013-02-12 17:00:58 -08002517 /* free tag if it is allocated */
2518 if (unlikely(tag_gen_flag))
2519 fnic_scsi_host_end_tag(fnic, sc);
2520
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002521 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2522 "Returning from device reset %s\n",
2523 (ret == SUCCESS) ?
2524 "SUCCESS" : "FAILED");
Hiral Patel67125b02013-09-12 17:45:42 -07002525
2526 if (ret == FAILED)
2527 atomic64_inc(&reset_stats->device_reset_failures);
2528
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002529 return ret;
2530}
2531
2532/* Clean up all IOs, clean up libFC local port */
2533int fnic_reset(struct Scsi_Host *shost)
2534{
2535 struct fc_lport *lp;
2536 struct fnic *fnic;
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002537 int ret = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07002538 struct reset_stats *reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002539
2540 lp = shost_priv(shost);
2541 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002542 reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002543
2544 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2545 "fnic_reset called\n");
2546
Hiral Patel67125b02013-09-12 17:45:42 -07002547 atomic64_inc(&reset_stats->fnic_resets);
2548
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002549 /*
2550 * Reset local port, this will clean up libFC exchanges,
2551 * reset remote port sessions, and if link is up, begin flogi
2552 */
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002553 ret = lp->tt.lport_reset(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002554
2555 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2556 "Returning from fnic reset %s\n",
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002557 (ret == 0) ?
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002558 "SUCCESS" : "FAILED");
2559
Hiral Patel67125b02013-09-12 17:45:42 -07002560 if (ret == 0)
2561 atomic64_inc(&reset_stats->fnic_reset_completions);
2562 else
2563 atomic64_inc(&reset_stats->fnic_reset_failures);
2564
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002565 return ret;
2566}
2567
2568/*
2569 * SCSI Error handling calls driver's eh_host_reset if all prior
2570 * error handling levels return FAILED. If host reset completes
2571 * successfully, and if link is up, then Fabric login begins.
2572 *
2573 * Host Reset is the highest level of error recovery. If this fails, then
2574 * host is offlined by SCSI.
2575 *
2576 */
2577int fnic_host_reset(struct scsi_cmnd *sc)
2578{
2579 int ret;
2580 unsigned long wait_host_tmo;
2581 struct Scsi_Host *shost = sc->device->host;
2582 struct fc_lport *lp = shost_priv(shost);
Satish Kharat2803ea72016-12-14 13:20:41 -08002583 struct fnic *fnic = lport_priv(lp);
2584 unsigned long flags;
2585
2586 spin_lock_irqsave(&fnic->fnic_lock, flags);
2587 if (fnic->internal_reset_inprogress == 0) {
2588 fnic->internal_reset_inprogress = 1;
2589 } else {
2590 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2591 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2592 "host reset in progress skipping another host reset\n");
2593 return SUCCESS;
2594 }
2595 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002596
2597 /*
2598 * If fnic_reset is successful, wait for fabric login to complete
2599 * scsi-ml tries to send a TUR to every device if host reset is
2600 * successful, so before returning to scsi, fabric should be up
2601 */
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002602 ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002603 if (ret == SUCCESS) {
2604 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2605 ret = FAILED;
2606 while (time_before(jiffies, wait_host_tmo)) {
2607 if ((lp->state == LPORT_ST_READY) &&
2608 (lp->link_up)) {
2609 ret = SUCCESS;
2610 break;
2611 }
2612 ssleep(1);
2613 }
2614 }
2615
Satish Kharat2803ea72016-12-14 13:20:41 -08002616 spin_lock_irqsave(&fnic->fnic_lock, flags);
2617 fnic->internal_reset_inprogress = 0;
2618 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002619 return ret;
2620}
2621
2622/*
2623 * This fxn is called from libFC when host is removed
2624 */
2625void fnic_scsi_abort_io(struct fc_lport *lp)
2626{
2627 int err = 0;
2628 unsigned long flags;
2629 enum fnic_state old_state;
2630 struct fnic *fnic = lport_priv(lp);
2631 DECLARE_COMPLETION_ONSTACK(remove_wait);
2632
2633 /* Issue firmware reset for fnic, wait for reset to complete */
Hiral Patel03298552013-02-12 17:00:58 -08002634retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002635 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002636 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2637 /* fw reset is in progress, poll for its completion */
2638 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2639 schedule_timeout(msecs_to_jiffies(100));
2640 goto retry_fw_reset;
2641 }
2642
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002643 fnic->remove_wait = &remove_wait;
2644 old_state = fnic->state;
2645 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002646 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002647 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2648
2649 err = fnic_fw_reset_handler(fnic);
2650 if (err) {
2651 spin_lock_irqsave(&fnic->fnic_lock, flags);
2652 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2653 fnic->state = old_state;
2654 fnic->remove_wait = NULL;
2655 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2656 return;
2657 }
2658
2659 /* Wait for firmware reset to complete */
2660 wait_for_completion_timeout(&remove_wait,
2661 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2662
2663 spin_lock_irqsave(&fnic->fnic_lock, flags);
2664 fnic->remove_wait = NULL;
2665 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2666 "fnic_scsi_abort_io %s\n",
2667 (fnic->state == FNIC_IN_ETH_MODE) ?
2668 "SUCCESS" : "FAILED");
2669 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2670
2671}
2672
2673/*
2674 * This fxn called from libFC to clean up driver IO state on link down
2675 */
2676void fnic_scsi_cleanup(struct fc_lport *lp)
2677{
2678 unsigned long flags;
2679 enum fnic_state old_state;
2680 struct fnic *fnic = lport_priv(lp);
2681
2682 /* issue fw reset */
Hiral Patel03298552013-02-12 17:00:58 -08002683retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002684 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002685 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2686 /* fw reset is in progress, poll for its completion */
2687 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2688 schedule_timeout(msecs_to_jiffies(100));
2689 goto retry_fw_reset;
2690 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002691 old_state = fnic->state;
2692 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002693 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002694 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2695
2696 if (fnic_fw_reset_handler(fnic)) {
2697 spin_lock_irqsave(&fnic->fnic_lock, flags);
2698 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2699 fnic->state = old_state;
2700 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2701 }
2702
2703}
2704
2705void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2706{
2707}
2708
2709void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2710{
2711 struct fnic *fnic = lport_priv(lp);
2712
2713 /* Non-zero sid, nothing to do */
2714 if (sid)
2715 goto call_fc_exch_mgr_reset;
2716
2717 if (did) {
2718 fnic_rport_exch_reset(fnic, did);
2719 goto call_fc_exch_mgr_reset;
2720 }
2721
2722 /*
2723 * sid = 0, did = 0
2724 * link down or device being removed
2725 */
2726 if (!fnic->in_remove)
2727 fnic_scsi_cleanup(lp);
2728 else
2729 fnic_scsi_abort_io(lp);
2730
2731 /* call libFC exch mgr reset to reset its exchanges */
2732call_fc_exch_mgr_reset:
2733 fc_exch_mgr_reset(lp, sid, did);
2734
2735}
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002736
2737/*
2738 * fnic_is_abts_pending() is a helper function that
2739 * walks through tag map to check if there is any IOs pending,if there is one,
2740 * then it returns 1 (true), otherwise 0 (false)
2741 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2742 * otherwise, it checks for all IOs.
2743 */
2744int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2745{
2746 int tag;
2747 struct fnic_io_req *io_req;
2748 spinlock_t *io_lock;
2749 unsigned long flags;
2750 int ret = 0;
2751 struct scsi_cmnd *sc;
2752 struct scsi_device *lun_dev = NULL;
2753
2754 if (lr_sc)
2755 lun_dev = lr_sc->device;
2756
2757 /* walk again to check, if IOs are still pending in fw */
Hiral Patelfc857992013-09-09 13:31:51 -07002758 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002759 sc = scsi_host_find_tag(fnic->lport->host, tag);
2760 /*
2761 * ignore this lun reset cmd or cmds that do not belong to
2762 * this lun
2763 */
2764 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2765 continue;
2766
2767 io_lock = fnic_io_lock_hash(fnic, sc);
2768 spin_lock_irqsave(io_lock, flags);
2769
2770 io_req = (struct fnic_io_req *)CMD_SP(sc);
2771
2772 if (!io_req || sc->device != lun_dev) {
2773 spin_unlock_irqrestore(io_lock, flags);
2774 continue;
2775 }
2776
2777 /*
2778 * Found IO that is still pending with firmware and
2779 * belongs to the LUN that we are resetting
2780 */
2781 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2782 "Found IO in %s on lun\n",
2783 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2784
Dan Carpenter5d65f912013-05-30 10:50:46 +03002785 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002786 ret = 1;
Dan Carpenter5d65f912013-05-30 10:50:46 +03002787 spin_unlock_irqrestore(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002788 }
2789
2790 return ret;
2791}