blob: d048f3b5006f4bccc305087238be4ed7bf49fd33 [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;
Satish Kharat6008e962016-11-16 00:54:01 -0800444 struct fc_rport_libfc_priv *rp;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700445
Hiral Patel03298552013-02-12 17:00:58 -0800446 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
447 return SCSI_MLQUEUE_HOST_BUSY;
448
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700449 rport = starget_to_rport(scsi_target(sc->device));
Satish Kharat6008e962016-11-16 00:54:01 -0800450 if (!rport) {
451 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
452 "returning DID_NO_CONNECT for IO as rport is NULL\n");
453 sc->result = DID_NO_CONNECT << 16;
454 done(sc);
455 return 0;
456 }
457
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700458 ret = fc_remote_port_chkready(rport);
459 if (ret) {
Satish Kharat6008e962016-11-16 00:54:01 -0800460 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
461 "rport is not ready\n");
Hiral Patel67125b02013-09-12 17:45:42 -0700462 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700463 sc->result = ret;
464 done(sc);
465 return 0;
466 }
467
Satish Kharat6008e962016-11-16 00:54:01 -0800468 rp = rport->dd_data;
469 if (!rp || rp->rp_state != RPORT_ST_READY) {
470 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Satish Kharat1b6ac5e2016-03-18 11:22:50 -0700471 "returning DID_NO_CONNECT for IO as rport is removed\n");
Satish Kharat6008e962016-11-16 00:54:01 -0800472 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
473 sc->result = DID_NO_CONNECT<<16;
474 done(sc);
475 return 0;
Hiral Shah35061e22014-11-10 12:54:34 -0800476 }
477
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700478 if (lp->state != LPORT_ST_READY || !(lp->link_up))
479 return SCSI_MLQUEUE_HOST_BUSY;
480
Hiral Patel03298552013-02-12 17:00:58 -0800481 atomic_inc(&fnic->in_flight);
482
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700483 /*
484 * Release host lock, use driver resource specific locks from here.
485 * Don't re-enable interrupts in case they were disabled prior to the
486 * caller disabling them.
487 */
488 spin_unlock(lp->host->host_lock);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800489 CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
490 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700491
492 /* Get a new io_req for this SCSI IO */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700493 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
494 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -0700495 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700496 ret = SCSI_MLQUEUE_HOST_BUSY;
497 goto out;
498 }
499 memset(io_req, 0, sizeof(*io_req));
500
501 /* Map the data buffer */
502 sg_count = scsi_dma_map(sc);
503 if (sg_count < 0) {
Hiral Patel4d7007b2013-02-12 17:01:02 -0800504 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
505 sc->request->tag, sc, 0, sc->cmnd[0],
506 sg_count, CMD_STATE(sc));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700507 mempool_free(io_req, fnic->io_req_pool);
508 goto out;
509 }
510
511 /* Determine the type of scatter/gather list we need */
512 io_req->sgl_cnt = sg_count;
513 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
514 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
515 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
516
517 if (sg_count) {
518 io_req->sgl_list =
519 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
Abhijeet Joglekar0c79c742011-06-13 21:21:01 -0700520 GFP_ATOMIC);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700521 if (!io_req->sgl_list) {
Hiral Patel67125b02013-09-12 17:45:42 -0700522 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700523 ret = SCSI_MLQUEUE_HOST_BUSY;
524 scsi_dma_unmap(sc);
525 mempool_free(io_req, fnic->io_req_pool);
526 goto out;
527 }
528
529 /* Cache sgl list allocated address before alignment */
530 io_req->sgl_list_alloc = io_req->sgl_list;
531 ptr = (unsigned long) io_req->sgl_list;
532 if (ptr % FNIC_SG_DESC_ALIGN) {
533 io_req->sgl_list = (struct host_sg_desc *)
534 (((unsigned long) ptr
535 + FNIC_SG_DESC_ALIGN - 1)
536 & ~(FNIC_SG_DESC_ALIGN - 1));
537 }
538 }
539
Hiral Shah41df7b02014-11-10 12:54:36 -0800540 /*
541 * Will acquire lock defore setting to IO initialized.
542 */
543
544 io_lock = fnic_io_lock_hash(fnic, sc);
545 spin_lock_irqsave(io_lock, flags);
546
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700547 /* initialize rest of io_req */
Hiral Shahdb196932015-07-14 07:08:57 -0700548 io_lock_acquired = 1;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700549 io_req->port_id = rport->port_id;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800550 io_req->start_time = jiffies;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700551 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
552 CMD_SP(sc) = (char *)io_req;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800553 CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700554 sc->scsi_done = done;
555
556 /* create copy wq desc and enqueue it */
557 wq = &fnic->wq_copy[0];
558 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
559 if (ret) {
560 /*
561 * In case another thread cancelled the request,
562 * refetch the pointer under the lock.
563 */
Hiral Patel4d7007b2013-02-12 17:01:02 -0800564 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
565 sc->request->tag, sc, 0, 0, 0,
566 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700567 io_req = (struct fnic_io_req *)CMD_SP(sc);
568 CMD_SP(sc) = NULL;
569 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
570 spin_unlock_irqrestore(io_lock, flags);
571 if (io_req) {
572 fnic_release_ioreq_buf(fnic, io_req, sc);
573 mempool_free(io_req, fnic->io_req_pool);
574 }
Hiral Shah41df7b02014-11-10 12:54:36 -0800575 atomic_dec(&fnic->in_flight);
576 /* acquire host lock before returning to SCSI */
577 spin_lock(lp->host->host_lock);
578 return ret;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800579 } else {
Hiral Patel67125b02013-09-12 17:45:42 -0700580 atomic64_inc(&fnic_stats->io_stats.active_ios);
581 atomic64_inc(&fnic_stats->io_stats.num_ios);
582 if (atomic64_read(&fnic_stats->io_stats.active_ios) >
583 atomic64_read(&fnic_stats->io_stats.max_active_ios))
584 atomic64_set(&fnic_stats->io_stats.max_active_ios,
585 atomic64_read(&fnic_stats->io_stats.active_ios));
586
Hiral Patel14eb5d92013-02-12 17:01:01 -0800587 /* REVISIT: Use per IO lock in the final code */
588 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700589 }
590out:
Hiral Patel4d7007b2013-02-12 17:01:02 -0800591 cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
592 (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
593 (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
594 sc->cmnd[5]);
595
596 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
597 sc->request->tag, sc, io_req,
598 sg_count, cmd_trace,
599 (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
Hiral Shah41df7b02014-11-10 12:54:36 -0800600
601 /* if only we issued IO, will we have the io lock */
Hiral Shahdb196932015-07-14 07:08:57 -0700602 if (io_lock_acquired)
Hiral Shah41df7b02014-11-10 12:54:36 -0800603 spin_unlock_irqrestore(io_lock, flags);
604
Hiral Patel03298552013-02-12 17:00:58 -0800605 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700606 /* acquire host lock before returning to SCSI */
607 spin_lock(lp->host->host_lock);
608 return ret;
609}
610
Jeff Garzikf2812332010-11-16 02:10:29 -0500611DEF_SCSI_QCMD(fnic_queuecommand)
612
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700613/*
614 * fnic_fcpio_fw_reset_cmpl_handler
615 * Routine to handle fw reset completion
616 */
617static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
618 struct fcpio_fw_req *desc)
619{
620 u8 type;
621 u8 hdr_status;
622 struct fcpio_tag tag;
623 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700624 unsigned long flags;
Hiral Patel67125b02013-09-12 17:45:42 -0700625 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700626
627 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
628
Hiral Patel67125b02013-09-12 17:45:42 -0700629 atomic64_inc(&reset_stats->fw_reset_completions);
630
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700631 /* Clean up all outstanding io requests */
632 fnic_cleanup_io(fnic, SCSI_NO_TAG);
633
Hiral Patel67125b02013-09-12 17:45:42 -0700634 atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
635 atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
636
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700637 spin_lock_irqsave(&fnic->fnic_lock, flags);
638
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700639 /* fnic should be in FC_TRANS_ETH_MODE */
640 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
641 /* Check status of reset completion */
642 if (!hdr_status) {
643 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
644 "reset cmpl success\n");
645 /* Ready to send flogi out */
646 fnic->state = FNIC_IN_ETH_MODE;
647 } else {
648 FNIC_SCSI_DBG(KERN_DEBUG,
649 fnic->lport->host,
650 "fnic fw_reset : failed %s\n",
651 fnic_fcpio_status_to_str(hdr_status));
652
653 /*
654 * Unable to change to eth mode, cannot send out flogi
655 * Change state to fc mode, so that subsequent Flogi
656 * requests from libFC will cause more attempts to
657 * reset the firmware. Free the cached flogi
658 */
659 fnic->state = FNIC_IN_FC_MODE;
Hiral Patel67125b02013-09-12 17:45:42 -0700660 atomic64_inc(&reset_stats->fw_reset_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700661 ret = -1;
662 }
663 } else {
664 FNIC_SCSI_DBG(KERN_DEBUG,
665 fnic->lport->host,
666 "Unexpected state %s while processing"
667 " reset cmpl\n", fnic_state_to_str(fnic->state));
Hiral Patel67125b02013-09-12 17:45:42 -0700668 atomic64_inc(&reset_stats->fw_reset_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700669 ret = -1;
670 }
671
672 /* Thread removing device blocks till firmware reset is complete */
673 if (fnic->remove_wait)
674 complete(fnic->remove_wait);
675
676 /*
677 * If fnic is being removed, or fw reset failed
678 * free the flogi frame. Else, send it out
679 */
680 if (fnic->remove_wait || ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700681 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Joe Eykholt78112e52009-11-03 11:49:22 -0800682 skb_queue_purge(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700683 goto reset_cmpl_handler_end;
684 }
685
686 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
687
Joe Eykholt78112e52009-11-03 11:49:22 -0800688 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700689
690 reset_cmpl_handler_end:
Hiral Patel03298552013-02-12 17:00:58 -0800691 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
692
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700693 return ret;
694}
695
696/*
697 * fnic_fcpio_flogi_reg_cmpl_handler
698 * Routine to handle flogi register completion
699 */
700static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
701 struct fcpio_fw_req *desc)
702{
703 u8 type;
704 u8 hdr_status;
705 struct fcpio_tag tag;
706 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700707 unsigned long flags;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700708
709 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
710
711 /* Update fnic state based on status of flogi reg completion */
712 spin_lock_irqsave(&fnic->fnic_lock, flags);
713
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700714 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
715
716 /* Check flogi registration completion status */
717 if (!hdr_status) {
718 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
719 "flog reg succeeded\n");
720 fnic->state = FNIC_IN_FC_MODE;
721 } else {
722 FNIC_SCSI_DBG(KERN_DEBUG,
723 fnic->lport->host,
724 "fnic flogi reg :failed %s\n",
725 fnic_fcpio_status_to_str(hdr_status));
726 fnic->state = FNIC_IN_ETH_MODE;
727 ret = -1;
728 }
729 } else {
730 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
731 "Unexpected fnic state %s while"
732 " processing flogi reg completion\n",
733 fnic_state_to_str(fnic->state));
734 ret = -1;
735 }
736
Joe Eykholt78112e52009-11-03 11:49:22 -0800737 if (!ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700738 if (fnic->stop_rx_link_events) {
739 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
740 goto reg_cmpl_handler_end;
741 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700742 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
743
Joe Eykholt78112e52009-11-03 11:49:22 -0800744 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700745 queue_work(fnic_event_queue, &fnic->frame_work);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700746 } else {
747 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700748 }
749
750reg_cmpl_handler_end:
751 return ret;
752}
753
754static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
755 u16 request_out)
756{
757 if (wq->to_clean_index <= wq->to_use_index) {
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 } else {
763 /* out of range, stale request_out index */
764 if (request_out < wq->to_clean_index &&
765 request_out >= wq->to_use_index)
766 return 0;
767 }
768 /* request_out index is in range */
769 return 1;
770}
771
772
773/*
774 * Mark that ack received and store the Ack index. If there are multiple
775 * acks received before Tx thread cleans it up, the latest value will be
776 * used which is correct behavior. This state should be in the copy Wq
777 * instead of in the fnic
778 */
779static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
780 unsigned int cq_index,
781 struct fcpio_fw_req *desc)
782{
783 struct vnic_wq_copy *wq;
784 u16 request_out = desc->u.ack.request_out;
785 unsigned long flags;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800786 u64 *ox_id_tag = (u64 *)(void *)desc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700787
788 /* mark the ack state */
789 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
790 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
791
Hiral Patel67125b02013-09-12 17:45:42 -0700792 fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700793 if (is_ack_index_in_range(wq, request_out)) {
794 fnic->fw_ack_index[0] = request_out;
795 fnic->fw_ack_recd[0] = 1;
Hiral Patel67125b02013-09-12 17:45:42 -0700796 } else
797 atomic64_inc(
798 &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
799
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700800 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800801 FNIC_TRACE(fnic_fcpio_ack_handler,
802 fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
803 ox_id_tag[4], ox_id_tag[5]);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700804}
805
806/*
807 * fnic_fcpio_icmnd_cmpl_handler
808 * Routine to handle icmnd completions
809 */
810static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
811 struct fcpio_fw_req *desc)
812{
813 u8 type;
814 u8 hdr_status;
815 struct fcpio_tag tag;
816 u32 id;
817 u64 xfer_len = 0;
818 struct fcpio_icmnd_cmpl *icmnd_cmpl;
819 struct fnic_io_req *io_req;
820 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -0700821 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700822 unsigned long flags;
823 spinlock_t *io_lock;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800824 u64 cmd_trace;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800825 unsigned long start_time;
Satish Kharat445d2962017-02-28 16:15:59 -0800826 unsigned long io_duration_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700827
828 /* Decode the cmpl description to get the io_req id */
829 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
830 fcpio_tag_id_dec(&tag, &id);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800831 icmnd_cmpl = &desc->u.icmnd_cmpl;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700832
Hiral Patelfc857992013-09-09 13:31:51 -0700833 if (id >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -0800834 shost_printk(KERN_ERR, fnic->lport->host,
835 "Tag out of range tag %x hdr status = %s\n",
836 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700837 return;
Hiral Patel03298552013-02-12 17:00:58 -0800838 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700839
840 sc = scsi_host_find_tag(fnic->lport->host, id);
841 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800842 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -0700843 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800844 shost_printk(KERN_ERR, fnic->lport->host,
845 "icmnd_cmpl sc is null - "
846 "hdr status = %s tag = 0x%x desc = 0x%p\n",
847 fnic_fcpio_status_to_str(hdr_status), id, desc);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800848 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
849 fnic->lport->host->host_no, id,
850 ((u64)icmnd_cmpl->_resvd0[1] << 16 |
851 (u64)icmnd_cmpl->_resvd0[0]),
852 ((u64)hdr_status << 16 |
853 (u64)icmnd_cmpl->scsi_status << 8 |
854 (u64)icmnd_cmpl->flags), desc,
855 (u64)icmnd_cmpl->residual, 0);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700856 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800857 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700858
859 io_lock = fnic_io_lock_hash(fnic, sc);
860 spin_lock_irqsave(io_lock, flags);
861 io_req = (struct fnic_io_req *)CMD_SP(sc);
862 WARN_ON_ONCE(!io_req);
863 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -0700864 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800865 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700866 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800867 shost_printk(KERN_ERR, fnic->lport->host,
868 "icmnd_cmpl io_req is null - "
869 "hdr status = %s tag = 0x%x sc 0x%p\n",
870 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700871 return;
872 }
Hiral Patel14eb5d92013-02-12 17:01:01 -0800873 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700874
875 /* firmware completed the io */
876 io_req->io_completed = 1;
877
878 /*
879 * if SCSI-ML has already issued abort on this command,
Satish Kharatb9202b42017-02-28 16:15:12 -0800880 * set completion of the IO. The abts path will clean it up
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700881 */
882 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
Satish Kharatb9202b42017-02-28 16:15:12 -0800883
884 /*
885 * set the FNIC_IO_DONE so that this doesn't get
886 * flagged as 'out of order' if it was not aborted
887 */
888 CMD_FLAGS(sc) |= FNIC_IO_DONE;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800889 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
Satish Kharatb9202b42017-02-28 16:15:12 -0800890 spin_unlock_irqrestore(io_lock, flags);
891 if(FCPIO_ABORTED == hdr_status)
Hiral Patel14eb5d92013-02-12 17:01:01 -0800892 CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
Satish Kharatb9202b42017-02-28 16:15:12 -0800893
894 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
895 "icmnd_cmpl abts pending "
896 "hdr status = %s tag = 0x%x sc = 0x%p"
897 "scsi_status = %x residual = %d\n",
898 fnic_fcpio_status_to_str(hdr_status),
899 id, sc,
900 icmnd_cmpl->scsi_status,
901 icmnd_cmpl->residual);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700902 return;
903 }
904
905 /* Mark the IO as complete */
906 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
907
908 icmnd_cmpl = &desc->u.icmnd_cmpl;
909
910 switch (hdr_status) {
911 case FCPIO_SUCCESS:
912 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
913 xfer_len = scsi_bufflen(sc);
914 scsi_set_resid(sc, icmnd_cmpl->residual);
915
916 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
917 xfer_len -= icmnd_cmpl->residual;
918
Satish Kharat39fcbbc2017-02-28 16:15:44 -0800919 if (icmnd_cmpl->scsi_status == SAM_STAT_CHECK_CONDITION)
920 atomic64_inc(&fnic_stats->misc_stats.check_condition);
921
Hiral Patel67125b02013-09-12 17:45:42 -0700922 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
923 atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700924 break;
925
926 case FCPIO_TIMEOUT: /* request was timed out */
Hiral Patel67125b02013-09-12 17:45:42 -0700927 atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700928 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
929 break;
930
931 case FCPIO_ABORTED: /* request was aborted */
Hiral Patel67125b02013-09-12 17:45:42 -0700932 atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700933 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
934 break;
935
936 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
Hiral Patel67125b02013-09-12 17:45:42 -0700937 atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700938 scsi_set_resid(sc, icmnd_cmpl->residual);
939 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
940 break;
941
942 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
Hiral Patel67125b02013-09-12 17:45:42 -0700943 atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700944 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
945 break;
Hiral Patel67125b02013-09-12 17:45:42 -0700946
947 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
948 atomic64_inc(&fnic_stats->io_stats.io_not_found);
949 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
950 break;
951
952 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
953 atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
954 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
955 break;
956
957 case FCPIO_FW_ERR: /* request was terminated due fw error */
958 atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
959 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
960 break;
961
962 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
963 atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
964 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
965 break;
966
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700967 case FCPIO_INVALID_HEADER: /* header contains invalid data */
968 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
969 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700970 default:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700971 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
972 break;
973 }
974
Maurizio Lombardi14cee5b2016-03-16 14:44:08 +0100975 /* Break link with the SCSI command */
976 CMD_SP(sc) = NULL;
977 CMD_FLAGS(sc) |= FNIC_IO_DONE;
978
979 spin_unlock_irqrestore(io_lock, flags);
980
Hiral Patel67125b02013-09-12 17:45:42 -0700981 if (hdr_status != FCPIO_SUCCESS) {
982 atomic64_inc(&fnic_stats->io_stats.io_failures);
983 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
984 fnic_fcpio_status_to_str(hdr_status));
985 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700986
987 fnic_release_ioreq_buf(fnic, io_req, sc);
988
989 mempool_free(io_req, fnic->io_req_pool);
990
Hiral Patel4d7007b2013-02-12 17:01:02 -0800991 cmd_trace = ((u64)hdr_status << 56) |
992 (u64)icmnd_cmpl->scsi_status << 48 |
993 (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
994 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
995 (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
996
997 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
998 sc->device->host->host_no, id, sc,
999 ((u64)icmnd_cmpl->_resvd0[1] << 56 |
1000 (u64)icmnd_cmpl->_resvd0[0] << 48 |
1001 jiffies_to_msecs(jiffies - start_time)),
1002 desc, cmd_trace,
1003 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1004
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001005 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
1006 fnic->lport->host_stats.fcp_input_requests++;
1007 fnic->fcp_input_bytes += xfer_len;
1008 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
1009 fnic->lport->host_stats.fcp_output_requests++;
1010 fnic->fcp_output_bytes += xfer_len;
1011 } else
1012 fnic->lport->host_stats.fcp_control_requests++;
1013
Hiral Patel67125b02013-09-12 17:45:42 -07001014 atomic64_dec(&fnic_stats->io_stats.active_ios);
1015 if (atomic64_read(&fnic->io_cmpl_skip))
1016 atomic64_dec(&fnic->io_cmpl_skip);
1017 else
1018 atomic64_inc(&fnic_stats->io_stats.io_completions);
1019
Satish Kharat445d2962017-02-28 16:15:59 -08001020
1021 io_duration_time = jiffies_to_msecs(jiffies) - jiffies_to_msecs(io_req->start_time);
1022
1023 if(io_duration_time <= 10)
1024 atomic64_inc(&fnic_stats->io_stats.io_btw_0_to_10_msec);
1025 else if(io_duration_time <= 100)
1026 atomic64_inc(&fnic_stats->io_stats.io_btw_10_to_100_msec);
1027 else if(io_duration_time <= 500)
1028 atomic64_inc(&fnic_stats->io_stats.io_btw_100_to_500_msec);
1029 else if(io_duration_time <= 5000)
1030 atomic64_inc(&fnic_stats->io_stats.io_btw_500_to_5000_msec);
1031 else if(io_duration_time <= 10000)
1032 atomic64_inc(&fnic_stats->io_stats.io_btw_5000_to_10000_msec);
1033 else if(io_duration_time <= 30000)
1034 atomic64_inc(&fnic_stats->io_stats.io_btw_10000_to_30000_msec);
1035 else {
1036 atomic64_inc(&fnic_stats->io_stats.io_greater_than_30000_msec);
1037
1038 if(io_duration_time > atomic64_read(&fnic_stats->io_stats.current_max_io_time))
1039 atomic64_set(&fnic_stats->io_stats.current_max_io_time, io_duration_time);
1040 }
1041
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001042 /* Call SCSI completion function to complete the IO */
1043 if (sc->scsi_done)
1044 sc->scsi_done(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001045}
1046
1047/* fnic_fcpio_itmf_cmpl_handler
1048 * Routine to handle itmf completions
1049 */
1050static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1051 struct fcpio_fw_req *desc)
1052{
1053 u8 type;
1054 u8 hdr_status;
1055 struct fcpio_tag tag;
1056 u32 id;
1057 struct scsi_cmnd *sc;
1058 struct fnic_io_req *io_req;
Hiral Patel67125b02013-09-12 17:45:42 -07001059 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1060 struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1061 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1062 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001063 unsigned long flags;
1064 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001065 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001066
1067 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1068 fcpio_tag_id_dec(&tag, &id);
1069
Hiral Patelfc857992013-09-09 13:31:51 -07001070 if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -08001071 shost_printk(KERN_ERR, fnic->lport->host,
1072 "Tag out of range tag %x hdr status = %s\n",
1073 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001074 return;
Hiral Patel03298552013-02-12 17:00:58 -08001075 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001076
1077 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1078 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001079 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -07001080 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001081 shost_printk(KERN_ERR, fnic->lport->host,
1082 "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1083 fnic_fcpio_status_to_str(hdr_status), id);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001084 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001085 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001086 io_lock = fnic_io_lock_hash(fnic, sc);
1087 spin_lock_irqsave(io_lock, flags);
1088 io_req = (struct fnic_io_req *)CMD_SP(sc);
1089 WARN_ON_ONCE(!io_req);
1090 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001091 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001092 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001093 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1094 shost_printk(KERN_ERR, fnic->lport->host,
1095 "itmf_cmpl io_req is null - "
1096 "hdr status = %s tag = 0x%x sc 0x%p\n",
1097 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001098 return;
1099 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001100 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001101
Hiral Patel03298552013-02-12 17:00:58 -08001102 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1103 /* Abort and terminate completion of device reset req */
1104 /* REVISIT : Add asserts about various flags */
1105 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1106 "dev reset abts cmpl recd. id %x status %s\n",
1107 id, fnic_fcpio_status_to_str(hdr_status));
1108 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1109 CMD_ABTS_STATUS(sc) = hdr_status;
1110 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1111 if (io_req->abts_done)
1112 complete(io_req->abts_done);
1113 spin_unlock_irqrestore(io_lock, flags);
1114 } else if (id & FNIC_TAG_ABORT) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001115 /* Completion of abort cmd */
Hiral Patel67125b02013-09-12 17:45:42 -07001116 switch (hdr_status) {
1117 case FCPIO_SUCCESS:
1118 break;
1119 case FCPIO_TIMEOUT:
1120 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1121 atomic64_inc(&abts_stats->abort_fw_timeouts);
1122 else
1123 atomic64_inc(
1124 &term_stats->terminate_fw_timeouts);
1125 break;
Satish Kharat691a8372016-03-18 11:22:48 -07001126 case FCPIO_ITMF_REJECTED:
1127 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1128 "abort reject recd. id %d\n",
1129 (int)(id & FNIC_TAG_MASK));
1130 break;
Hiral Patel67125b02013-09-12 17:45:42 -07001131 case FCPIO_IO_NOT_FOUND:
1132 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1133 atomic64_inc(&abts_stats->abort_io_not_found);
1134 else
1135 atomic64_inc(
1136 &term_stats->terminate_io_not_found);
1137 break;
1138 default:
1139 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1140 atomic64_inc(&abts_stats->abort_failures);
1141 else
1142 atomic64_inc(
1143 &term_stats->terminate_failures);
1144 break;
1145 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001146 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1147 /* This is a late completion. Ignore it */
1148 spin_unlock_irqrestore(io_lock, flags);
1149 return;
1150 }
Satish Kharat691a8372016-03-18 11:22:48 -07001151
Hiral Patel14eb5d92013-02-12 17:01:01 -08001152 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Satish Kharatccc6d702017-02-28 16:14:33 -08001153 CMD_ABTS_STATUS(sc) = hdr_status;
Hiral Patel67125b02013-09-12 17:45:42 -07001154
Satish Kharat691a8372016-03-18 11:22:48 -07001155 /* If the status is IO not found consider it as success */
1156 if (hdr_status == FCPIO_IO_NOT_FOUND)
1157 CMD_ABTS_STATUS(sc) = FCPIO_SUCCESS;
Satish Kharat691a8372016-03-18 11:22:48 -07001158
Hiral Patel67125b02013-09-12 17:45:42 -07001159 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1160 atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1161
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001162 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1163 "abts cmpl recd. id %d status %s\n",
1164 (int)(id & FNIC_TAG_MASK),
1165 fnic_fcpio_status_to_str(hdr_status));
1166
1167 /*
1168 * If scsi_eh thread is blocked waiting for abts to complete,
1169 * signal completion to it. IO will be cleaned in the thread
1170 * else clean it in this context
1171 */
1172 if (io_req->abts_done) {
1173 complete(io_req->abts_done);
1174 spin_unlock_irqrestore(io_lock, flags);
1175 } else {
1176 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1177 "abts cmpl, completing IO\n");
1178 CMD_SP(sc) = NULL;
1179 sc->result = (DID_ERROR << 16);
1180
1181 spin_unlock_irqrestore(io_lock, flags);
1182
1183 fnic_release_ioreq_buf(fnic, io_req, sc);
1184 mempool_free(io_req, fnic->io_req_pool);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001185 if (sc->scsi_done) {
1186 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1187 sc->device->host->host_no, id,
1188 sc,
1189 jiffies_to_msecs(jiffies - start_time),
1190 desc,
1191 (((u64)hdr_status << 40) |
1192 (u64)sc->cmnd[0] << 32 |
1193 (u64)sc->cmnd[2] << 24 |
1194 (u64)sc->cmnd[3] << 16 |
1195 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1196 (((u64)CMD_FLAGS(sc) << 32) |
1197 CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001198 sc->scsi_done(sc);
Satish Kharat7ef539c2017-02-28 16:14:56 -08001199 atomic64_dec(&fnic_stats->io_stats.active_ios);
1200 if (atomic64_read(&fnic->io_cmpl_skip))
1201 atomic64_dec(&fnic->io_cmpl_skip);
1202 else
1203 atomic64_inc(&fnic_stats->io_stats.io_completions);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001204 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001205 }
1206
1207 } else if (id & FNIC_TAG_DEV_RST) {
1208 /* Completion of device reset */
1209 CMD_LR_STATUS(sc) = hdr_status;
Hiral Patel03298552013-02-12 17:00:58 -08001210 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1211 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001212 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001213 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1214 sc->device->host->host_no, id, sc,
1215 jiffies_to_msecs(jiffies - start_time),
1216 desc, 0,
1217 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001218 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1219 "Terminate pending "
1220 "dev reset cmpl recd. id %d status %s\n",
1221 (int)(id & FNIC_TAG_MASK),
1222 fnic_fcpio_status_to_str(hdr_status));
1223 return;
1224 }
1225 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1226 /* Need to wait for terminate completion */
1227 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001228 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1229 sc->device->host->host_no, id, sc,
1230 jiffies_to_msecs(jiffies - start_time),
1231 desc, 0,
1232 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001233 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1234 "dev reset cmpl recd after time out. "
1235 "id %d status %s\n",
1236 (int)(id & FNIC_TAG_MASK),
1237 fnic_fcpio_status_to_str(hdr_status));
1238 return;
1239 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001240 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
Hiral Patel03298552013-02-12 17:00:58 -08001241 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001242 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1243 "dev reset cmpl recd. id %d status %s\n",
1244 (int)(id & FNIC_TAG_MASK),
1245 fnic_fcpio_status_to_str(hdr_status));
1246 if (io_req->dr_done)
1247 complete(io_req->dr_done);
1248 spin_unlock_irqrestore(io_lock, flags);
1249
1250 } else {
1251 shost_printk(KERN_ERR, fnic->lport->host,
1252 "Unexpected itmf io state %s tag %x\n",
1253 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1254 spin_unlock_irqrestore(io_lock, flags);
1255 }
1256
1257}
1258
1259/*
1260 * fnic_fcpio_cmpl_handler
1261 * Routine to service the cq for wq_copy
1262 */
1263static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1264 unsigned int cq_index,
1265 struct fcpio_fw_req *desc)
1266{
1267 struct fnic *fnic = vnic_dev_priv(vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001268
1269 switch (desc->hdr.type) {
Hiral Patel67125b02013-09-12 17:45:42 -07001270 case FCPIO_ICMND_CMPL: /* fw completed a command */
1271 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1272 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1273 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1274 case FCPIO_RESET_CMPL: /* fw completed reset */
1275 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1276 break;
1277 default:
1278 break;
1279 }
1280
1281 switch (desc->hdr.type) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001282 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1283 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1284 break;
1285
1286 case FCPIO_ICMND_CMPL: /* fw completed a command */
1287 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1288 break;
1289
1290 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1291 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1292 break;
1293
1294 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
Joe Eykholt78112e52009-11-03 11:49:22 -08001295 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
Hiral Patel03298552013-02-12 17:00:58 -08001296 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001297 break;
1298
1299 case FCPIO_RESET_CMPL: /* fw completed reset */
Hiral Patel03298552013-02-12 17:00:58 -08001300 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001301 break;
1302
1303 default:
1304 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1305 "firmware completion type %d\n",
1306 desc->hdr.type);
1307 break;
1308 }
1309
Hiral Patel03298552013-02-12 17:00:58 -08001310 return 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001311}
1312
1313/*
1314 * fnic_wq_copy_cmpl_handler
1315 * Routine to process wq copy
1316 */
1317int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1318{
1319 unsigned int wq_work_done = 0;
1320 unsigned int i, cq_index;
1321 unsigned int cur_work_done;
1322
1323 for (i = 0; i < fnic->wq_copy_count; i++) {
1324 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1325 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1326 fnic_fcpio_cmpl_handler,
1327 copy_work_to_do);
1328 wq_work_done += cur_work_done;
1329 }
1330 return wq_work_done;
1331}
1332
1333static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1334{
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001335 int i;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001336 struct fnic_io_req *io_req;
1337 unsigned long flags = 0;
1338 struct scsi_cmnd *sc;
1339 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001340 unsigned long start_time = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07001341 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001342
Hiral Patelfc857992013-09-09 13:31:51 -07001343 for (i = 0; i < fnic->fnic_max_tag_id; i++) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001344 if (i == exclude_id)
1345 continue;
1346
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001347 io_lock = fnic_io_lock_tag(fnic, i);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001348 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001349 sc = scsi_host_find_tag(fnic->lport->host, i);
1350 if (!sc) {
1351 spin_unlock_irqrestore(io_lock, flags);
1352 continue;
1353 }
1354
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001355 io_req = (struct fnic_io_req *)CMD_SP(sc);
Hiral Patel03298552013-02-12 17:00:58 -08001356 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1357 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1358 /*
1359 * We will be here only when FW completes reset
1360 * without sending completions for outstanding ios.
1361 */
1362 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1363 if (io_req && io_req->dr_done)
1364 complete(io_req->dr_done);
1365 else if (io_req && io_req->abts_done)
1366 complete(io_req->abts_done);
1367 spin_unlock_irqrestore(io_lock, flags);
1368 continue;
1369 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1370 spin_unlock_irqrestore(io_lock, flags);
1371 continue;
1372 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001373 if (!io_req) {
1374 spin_unlock_irqrestore(io_lock, flags);
1375 goto cleanup_scsi_cmd;
1376 }
1377
1378 CMD_SP(sc) = NULL;
1379
1380 spin_unlock_irqrestore(io_lock, flags);
1381
1382 /*
1383 * If there is a scsi_cmnd associated with this io_req, then
1384 * free the corresponding state
1385 */
Hiral Patel14eb5d92013-02-12 17:01:01 -08001386 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001387 fnic_release_ioreq_buf(fnic, io_req, sc);
1388 mempool_free(io_req, fnic->io_req_pool);
1389
1390cleanup_scsi_cmd:
1391 sc->result = DID_TRANSPORT_DISRUPTED << 16;
Hiral Shah66818662014-04-18 12:28:18 -07001392 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1393 "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1394 __func__, (jiffies - start_time));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001395
Hiral Patel67125b02013-09-12 17:45:42 -07001396 if (atomic64_read(&fnic->io_cmpl_skip))
1397 atomic64_dec(&fnic->io_cmpl_skip);
1398 else
1399 atomic64_inc(&fnic_stats->io_stats.io_completions);
1400
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001401 /* Complete the command to SCSI */
Hiral Patel4d7007b2013-02-12 17:01:02 -08001402 if (sc->scsi_done) {
1403 FNIC_TRACE(fnic_cleanup_io,
1404 sc->device->host->host_no, i, sc,
1405 jiffies_to_msecs(jiffies - start_time),
1406 0, ((u64)sc->cmnd[0] << 32 |
1407 (u64)sc->cmnd[2] << 24 |
1408 (u64)sc->cmnd[3] << 16 |
1409 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1410 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1411
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001412 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001413 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001414 }
1415}
1416
1417void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1418 struct fcpio_host_req *desc)
1419{
1420 u32 id;
1421 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1422 struct fnic_io_req *io_req;
1423 struct scsi_cmnd *sc;
1424 unsigned long flags;
1425 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001426 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001427
1428 /* get the tag reference */
1429 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1430 id &= FNIC_TAG_MASK;
1431
Hiral Patelfc857992013-09-09 13:31:51 -07001432 if (id >= fnic->fnic_max_tag_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001433 return;
1434
1435 sc = scsi_host_find_tag(fnic->lport->host, id);
1436 if (!sc)
1437 return;
1438
1439 io_lock = fnic_io_lock_hash(fnic, sc);
1440 spin_lock_irqsave(io_lock, flags);
1441
1442 /* Get the IO context which this desc refers to */
1443 io_req = (struct fnic_io_req *)CMD_SP(sc);
1444
1445 /* fnic interrupts are turned off by now */
1446
1447 if (!io_req) {
1448 spin_unlock_irqrestore(io_lock, flags);
1449 goto wq_copy_cleanup_scsi_cmd;
1450 }
1451
1452 CMD_SP(sc) = NULL;
1453
1454 spin_unlock_irqrestore(io_lock, flags);
1455
Hiral Patel14eb5d92013-02-12 17:01:01 -08001456 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001457 fnic_release_ioreq_buf(fnic, io_req, sc);
1458 mempool_free(io_req, fnic->io_req_pool);
1459
1460wq_copy_cleanup_scsi_cmd:
1461 sc->result = DID_NO_CONNECT << 16;
1462 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1463 " DID_NO_CONNECT\n");
1464
Hiral Patel4d7007b2013-02-12 17:01:02 -08001465 if (sc->scsi_done) {
1466 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1467 sc->device->host->host_no, id, sc,
1468 jiffies_to_msecs(jiffies - start_time),
1469 0, ((u64)sc->cmnd[0] << 32 |
1470 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1471 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1472 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1473
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001474 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001475 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001476}
1477
1478static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1479 u32 task_req, u8 *fc_lun,
1480 struct fnic_io_req *io_req)
1481{
1482 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001483 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001484 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001485 unsigned long flags;
1486
Hiral Patel03298552013-02-12 17:00:58 -08001487 spin_lock_irqsave(host->host_lock, flags);
1488 if (unlikely(fnic_chk_state_flags_locked(fnic,
1489 FNIC_FLAGS_IO_BLOCKED))) {
1490 spin_unlock_irqrestore(host->host_lock, flags);
1491 return 1;
1492 } else
1493 atomic_inc(&fnic->in_flight);
1494 spin_unlock_irqrestore(host->host_lock, flags);
1495
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001496 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1497
1498 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1499 free_wq_copy_descs(fnic, wq);
1500
1501 if (!vnic_wq_copy_desc_avail(wq)) {
1502 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001503 atomic_dec(&fnic->in_flight);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001504 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001505 "fnic_queue_abort_io_req: failure: no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07001506 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001507 return 1;
1508 }
1509 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1510 0, task_req, tag, fc_lun, io_req->port_id,
1511 fnic->config.ra_tov, fnic->config.ed_tov);
1512
Hiral Patel67125b02013-09-12 17:45:42 -07001513 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1514 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1515 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1516 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1517 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1518
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001519 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001520 atomic_dec(&fnic->in_flight);
1521
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001522 return 0;
1523}
1524
Hiral Patel03298552013-02-12 17:00:58 -08001525static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001526{
1527 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001528 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001529 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001530 struct fnic_io_req *io_req;
1531 spinlock_t *io_lock;
1532 unsigned long flags;
1533 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -07001534 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1535 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001536 struct scsi_lun fc_lun;
1537 enum fnic_ioreq_state old_ioreq_state;
1538
1539 FNIC_SCSI_DBG(KERN_DEBUG,
1540 fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001541 "fnic_rport_exch_reset called portid 0x%06x\n",
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001542 port_id);
1543
1544 if (fnic->in_remove)
1545 return;
1546
Hiral Patelfc857992013-09-09 13:31:51 -07001547 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001548 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001549 io_lock = fnic_io_lock_tag(fnic, tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001550 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001551 sc = scsi_host_find_tag(fnic->lport->host, tag);
1552 if (!sc) {
1553 spin_unlock_irqrestore(io_lock, flags);
1554 continue;
1555 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001556
1557 io_req = (struct fnic_io_req *)CMD_SP(sc);
1558
1559 if (!io_req || io_req->port_id != port_id) {
1560 spin_unlock_irqrestore(io_lock, flags);
1561 continue;
1562 }
1563
Hiral Patel03298552013-02-12 17:00:58 -08001564 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001565 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001566 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1567 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1568 sc);
1569 spin_unlock_irqrestore(io_lock, flags);
1570 continue;
1571 }
1572
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001573 /*
1574 * Found IO that is still pending with firmware and
1575 * belongs to rport that went away
1576 */
1577 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1578 spin_unlock_irqrestore(io_lock, flags);
1579 continue;
1580 }
Hiral Patel03298552013-02-12 17:00:58 -08001581 if (io_req->abts_done) {
1582 shost_printk(KERN_ERR, fnic->lport->host,
1583 "fnic_rport_exch_reset: io_req->abts_done is set "
1584 "state is %s\n",
1585 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1586 }
1587
Hiral Patel14eb5d92013-02-12 17:01:01 -08001588 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1589 shost_printk(KERN_ERR, fnic->lport->host,
1590 "rport_exch_reset "
1591 "IO not yet issued %p tag 0x%x flags "
1592 "%x state %d\n",
1593 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1594 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001595 old_ioreq_state = CMD_STATE(sc);
1596 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1597 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001598 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001599 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001600 abt_tag = (tag | FNIC_TAG_DEV_RST);
1601 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1602 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1603 sc);
1604 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001605
1606 BUG_ON(io_req->abts_done);
1607
1608 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1609 "fnic_rport_reset_exch: Issuing abts\n");
1610
1611 spin_unlock_irqrestore(io_lock, flags);
1612
1613 /* Now queue the abort command to firmware */
1614 int_to_scsilun(sc->device->lun, &fc_lun);
1615
Hiral Patel03298552013-02-12 17:00:58 -08001616 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001617 FCPIO_ITMF_ABT_TASK_TERM,
1618 fc_lun.scsi_lun, io_req)) {
1619 /*
1620 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001621 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001622 * aborted later by scsi_eh, or cleaned up during
1623 * lun reset
1624 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001625 spin_lock_irqsave(io_lock, flags);
1626 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1627 CMD_STATE(sc) = old_ioreq_state;
1628 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001629 } else {
1630 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001631 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1632 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001633 else
1634 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001635 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001636 atomic64_inc(&term_stats->terminates);
1637 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001638 }
1639 }
Hiral Patel67125b02013-09-12 17:45:42 -07001640 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1641 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001642
1643}
1644
1645void fnic_terminate_rport_io(struct fc_rport *rport)
1646{
1647 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001648 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001649 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001650 struct fnic_io_req *io_req;
1651 spinlock_t *io_lock;
1652 unsigned long flags;
1653 struct scsi_cmnd *sc;
1654 struct scsi_lun fc_lun;
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001655 struct fc_rport_libfc_priv *rdata;
1656 struct fc_lport *lport;
1657 struct fnic *fnic;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001658 struct fc_rport *cmd_rport;
Hiral Patel67125b02013-09-12 17:45:42 -07001659 struct reset_stats *reset_stats;
1660 struct terminate_stats *term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001661 enum fnic_ioreq_state old_ioreq_state;
1662
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001663 if (!rport) {
1664 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1665 return;
1666 }
1667 rdata = rport->dd_data;
1668
1669 if (!rdata) {
1670 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1671 return;
1672 }
1673 lport = rdata->local_port;
1674
1675 if (!lport) {
1676 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1677 return;
1678 }
1679 fnic = lport_priv(lport);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001680 FNIC_SCSI_DBG(KERN_DEBUG,
1681 fnic->lport->host, "fnic_terminate_rport_io called"
Hiral Patel03298552013-02-12 17:00:58 -08001682 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1683 rport->port_name, rport->node_name, rport,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001684 rport->port_id);
1685
1686 if (fnic->in_remove)
1687 return;
1688
Hiral Patel67125b02013-09-12 17:45:42 -07001689 reset_stats = &fnic->fnic_stats.reset_stats;
1690 term_stats = &fnic->fnic_stats.term_stats;
1691
Hiral Patelfc857992013-09-09 13:31:51 -07001692 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001693 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001694 io_lock = fnic_io_lock_tag(fnic, tag);
1695 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001696 sc = scsi_host_find_tag(fnic->lport->host, tag);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001697 if (!sc) {
1698 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001699 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001700 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001701
1702 cmd_rport = starget_to_rport(scsi_target(sc->device));
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001703 if (rport != cmd_rport) {
1704 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001705 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001706 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001707
1708 io_req = (struct fnic_io_req *)CMD_SP(sc);
1709
1710 if (!io_req || rport != cmd_rport) {
1711 spin_unlock_irqrestore(io_lock, flags);
1712 continue;
1713 }
1714
Hiral Patel03298552013-02-12 17:00:58 -08001715 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001716 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001717 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1718 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1719 sc);
1720 spin_unlock_irqrestore(io_lock, flags);
1721 continue;
1722 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001723 /*
1724 * Found IO that is still pending with firmware and
1725 * belongs to rport that went away
1726 */
1727 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1728 spin_unlock_irqrestore(io_lock, flags);
1729 continue;
1730 }
Hiral Patel03298552013-02-12 17:00:58 -08001731 if (io_req->abts_done) {
1732 shost_printk(KERN_ERR, fnic->lport->host,
1733 "fnic_terminate_rport_io: io_req->abts_done is set "
1734 "state is %s\n",
1735 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1736 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001737 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1738 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1739 "fnic_terminate_rport_io "
1740 "IO not yet issued %p tag 0x%x flags "
1741 "%x state %d\n",
1742 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1743 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001744 old_ioreq_state = CMD_STATE(sc);
1745 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1746 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001747 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001748 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001749 abt_tag = (tag | FNIC_TAG_DEV_RST);
1750 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1751 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1752 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001753
1754 BUG_ON(io_req->abts_done);
1755
1756 FNIC_SCSI_DBG(KERN_DEBUG,
1757 fnic->lport->host,
1758 "fnic_terminate_rport_io: Issuing abts\n");
1759
1760 spin_unlock_irqrestore(io_lock, flags);
1761
1762 /* Now queue the abort command to firmware */
1763 int_to_scsilun(sc->device->lun, &fc_lun);
1764
Hiral Patel03298552013-02-12 17:00:58 -08001765 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001766 FCPIO_ITMF_ABT_TASK_TERM,
1767 fc_lun.scsi_lun, io_req)) {
1768 /*
1769 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001770 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001771 * aborted later by scsi_eh, or cleaned up during
1772 * lun reset
1773 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001774 spin_lock_irqsave(io_lock, flags);
1775 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1776 CMD_STATE(sc) = old_ioreq_state;
1777 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001778 } else {
1779 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001780 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1781 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001782 else
1783 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001784 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001785 atomic64_inc(&term_stats->terminates);
1786 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001787 }
1788 }
Hiral Patel67125b02013-09-12 17:45:42 -07001789 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1790 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001791
1792}
1793
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001794/*
1795 * This function is exported to SCSI for sending abort cmnds.
1796 * A SCSI IO is represented by a io_req in the driver.
1797 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1798 */
1799int fnic_abort_cmd(struct scsi_cmnd *sc)
1800{
1801 struct fc_lport *lp;
1802 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001803 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001804 struct fc_rport *rport;
1805 spinlock_t *io_lock;
1806 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001807 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001808 int ret = SUCCESS;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001809 u32 task_req = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001810 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07001811 struct fnic_stats *fnic_stats;
1812 struct abort_stats *abts_stats;
1813 struct terminate_stats *term_stats;
Hiral Shah66818662014-04-18 12:28:18 -07001814 enum fnic_ioreq_state old_ioreq_state;
Hiral Patel03298552013-02-12 17:00:58 -08001815 int tag;
Satish Kharat445d2962017-02-28 16:15:59 -08001816 unsigned long abt_issued_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001817 DECLARE_COMPLETION_ONSTACK(tm_done);
1818
1819 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001820 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001821
1822 /* Get local-port, check ready and link up */
1823 lp = shost_priv(sc->device->host);
1824
1825 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07001826 fnic_stats = &fnic->fnic_stats;
1827 abts_stats = &fnic->fnic_stats.abts_stats;
1828 term_stats = &fnic->fnic_stats.term_stats;
1829
Roel Kluin0db6f432010-06-11 16:44:46 -07001830 rport = starget_to_rport(scsi_target(sc->device));
Hiral Patel03298552013-02-12 17:00:58 -08001831 tag = sc->request->tag;
1832 FNIC_SCSI_DBG(KERN_DEBUG,
1833 fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001834 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
Hiral Patel03298552013-02-12 17:00:58 -08001835 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1836
1837 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1838
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001839 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1840 ret = FAILED;
1841 goto fnic_abort_cmd_end;
1842 }
1843
1844 /*
1845 * Avoid a race between SCSI issuing the abort and the device
1846 * completing the command.
1847 *
1848 * If the command is already completed by the fw cmpl code,
1849 * we just return SUCCESS from here. This means that the abort
1850 * succeeded. In the SCSI ML, since the timeout for command has
1851 * happened, the completion wont actually complete the command
1852 * and it will be considered as an aborted command
1853 *
1854 * The CMD_SP will not be cleared except while holding io_req_lock.
1855 */
1856 io_lock = fnic_io_lock_hash(fnic, sc);
1857 spin_lock_irqsave(io_lock, flags);
1858 io_req = (struct fnic_io_req *)CMD_SP(sc);
1859 if (!io_req) {
1860 spin_unlock_irqrestore(io_lock, flags);
1861 goto fnic_abort_cmd_end;
1862 }
1863
1864 io_req->abts_done = &tm_done;
1865
1866 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1867 spin_unlock_irqrestore(io_lock, flags);
1868 goto wait_pending;
1869 }
Satish Kharat445d2962017-02-28 16:15:59 -08001870
1871 abt_issued_time = jiffies_to_msecs(jiffies) - jiffies_to_msecs(io_req->start_time);
1872 if (abt_issued_time <= 6000)
1873 atomic64_inc(&abts_stats->abort_issued_btw_0_to_6_sec);
1874 else if (abt_issued_time > 6000 && abt_issued_time <= 20000)
1875 atomic64_inc(&abts_stats->abort_issued_btw_6_to_20_sec);
1876 else if (abt_issued_time > 20000 && abt_issued_time <= 30000)
1877 atomic64_inc(&abts_stats->abort_issued_btw_20_to_30_sec);
1878 else if (abt_issued_time > 30000 && abt_issued_time <= 40000)
1879 atomic64_inc(&abts_stats->abort_issued_btw_30_to_40_sec);
1880 else if (abt_issued_time > 40000 && abt_issued_time <= 50000)
1881 atomic64_inc(&abts_stats->abort_issued_btw_40_to_50_sec);
1882 else if (abt_issued_time > 50000 && abt_issued_time <= 60000)
1883 atomic64_inc(&abts_stats->abort_issued_btw_50_to_60_sec);
1884 else
1885 atomic64_inc(&abts_stats->abort_issued_greater_than_60_sec);
1886
1887 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1888 "CBD Opcode: %02x Abort issued time: %lu msec\n", sc->cmnd[0], abt_issued_time);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001889 /*
1890 * Command is still pending, need to abort it
1891 * If the firmware completes the command after this point,
1892 * the completion wont be done till mid-layer, since abort
1893 * has already started.
1894 */
Hiral Shah66818662014-04-18 12:28:18 -07001895 old_ioreq_state = CMD_STATE(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001896 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1897 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1898
1899 spin_unlock_irqrestore(io_lock, flags);
1900
1901 /*
1902 * Check readiness of the remote port. If the path to remote
1903 * port is up, then send abts to the remote port to terminate
1904 * the IO. Else, just locally terminate the IO in the firmware
1905 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001906 if (fc_remote_port_chkready(rport) == 0)
1907 task_req = FCPIO_ITMF_ABT_TASK;
Hiral Patel67125b02013-09-12 17:45:42 -07001908 else {
1909 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001910 task_req = FCPIO_ITMF_ABT_TASK_TERM;
Hiral Patel67125b02013-09-12 17:45:42 -07001911 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001912
1913 /* Now queue the abort command to firmware */
1914 int_to_scsilun(sc->device->lun, &fc_lun);
1915
1916 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1917 fc_lun.scsi_lun, io_req)) {
1918 spin_lock_irqsave(io_lock, flags);
Hiral Shah66818662014-04-18 12:28:18 -07001919 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1920 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001921 io_req = (struct fnic_io_req *)CMD_SP(sc);
1922 if (io_req)
1923 io_req->abts_done = NULL;
1924 spin_unlock_irqrestore(io_lock, flags);
1925 ret = FAILED;
1926 goto fnic_abort_cmd_end;
1927 }
Hiral Patel67125b02013-09-12 17:45:42 -07001928 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001929 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001930 atomic64_inc(&fnic_stats->abts_stats.aborts);
1931 } else {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001932 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001933 atomic64_inc(&fnic_stats->term_stats.terminates);
1934 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001935
1936 /*
1937 * We queued an abort IO, wait for its completion.
1938 * Once the firmware completes the abort command, it will
1939 * wake up this thread.
1940 */
1941 wait_pending:
1942 wait_for_completion_timeout(&tm_done,
1943 msecs_to_jiffies
1944 (2 * fnic->config.ra_tov +
1945 fnic->config.ed_tov));
1946
1947 /* Check the abort status */
1948 spin_lock_irqsave(io_lock, flags);
1949
1950 io_req = (struct fnic_io_req *)CMD_SP(sc);
1951 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001952 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001953 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001954 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001955 ret = FAILED;
1956 goto fnic_abort_cmd_end;
1957 }
1958 io_req->abts_done = NULL;
1959
1960 /* fw did not complete abort, timed out */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001961 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001962 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001963 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel67125b02013-09-12 17:45:42 -07001964 atomic64_inc(&abts_stats->abort_drv_timeouts);
1965 } else {
Hiral Patel67125b02013-09-12 17:45:42 -07001966 atomic64_inc(&term_stats->terminate_drv_timeouts);
1967 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001968 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001969 ret = FAILED;
1970 goto fnic_abort_cmd_end;
1971 }
1972
Anil Chintalapati (achintal)efc7a282014-12-23 19:40:00 +00001973 /* IO out of order */
1974
1975 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1976 spin_unlock_irqrestore(io_lock, flags);
1977 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1978 "Issuing Host reset due to out of order IO\n");
1979
1980 if (fnic_host_reset(sc) == FAILED) {
1981 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1982 "fnic_host_reset failed.\n");
1983 }
1984 ret = FAILED;
1985 goto fnic_abort_cmd_end;
1986 }
1987
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001988 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1989
Satish Kharat691a8372016-03-18 11:22:48 -07001990 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001991 /*
1992 * firmware completed the abort, check the status,
Satish Kharat691a8372016-03-18 11:22:48 -07001993 * free the io_req if successful. If abort fails,
1994 * Device reset will clean the I/O.
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001995 */
Satish Kharat691a8372016-03-18 11:22:48 -07001996 if (CMD_ABTS_STATUS(sc) == FCPIO_SUCCESS)
1997 CMD_SP(sc) = NULL;
1998 else {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001999 ret = FAILED;
Satish Kharat691a8372016-03-18 11:22:48 -07002000 spin_unlock_irqrestore(io_lock, flags);
2001 goto fnic_abort_cmd_end;
2002 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002003
2004 spin_unlock_irqrestore(io_lock, flags);
2005
2006 fnic_release_ioreq_buf(fnic, io_req, sc);
2007 mempool_free(io_req, fnic->io_req_pool);
2008
Satish Kharat691a8372016-03-18 11:22:48 -07002009 if (sc->scsi_done) {
2010 /* Call SCSI completion function to complete the IO */
2011 sc->result = (DID_ABORT << 16);
2012 sc->scsi_done(sc);
Satish Kharat7ef539c2017-02-28 16:14:56 -08002013 atomic64_dec(&fnic_stats->io_stats.active_ios);
2014 if (atomic64_read(&fnic->io_cmpl_skip))
2015 atomic64_dec(&fnic->io_cmpl_skip);
2016 else
2017 atomic64_inc(&fnic_stats->io_stats.io_completions);
Satish Kharat691a8372016-03-18 11:22:48 -07002018 }
2019
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002020fnic_abort_cmd_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08002021 FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
2022 sc->request->tag, sc,
2023 jiffies_to_msecs(jiffies - start_time),
2024 0, ((u64)sc->cmnd[0] << 32 |
2025 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2026 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2027 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2028
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002029 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel14eb5d92013-02-12 17:01:01 -08002030 "Returning from abort cmd type %x %s\n", task_req,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002031 (ret == SUCCESS) ?
2032 "SUCCESS" : "FAILED");
2033 return ret;
2034}
2035
2036static inline int fnic_queue_dr_io_req(struct fnic *fnic,
2037 struct scsi_cmnd *sc,
2038 struct fnic_io_req *io_req)
2039{
2040 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08002041 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07002042 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002043 struct scsi_lun fc_lun;
2044 int ret = 0;
2045 unsigned long intr_flags;
2046
Hiral Patel03298552013-02-12 17:00:58 -08002047 spin_lock_irqsave(host->host_lock, intr_flags);
2048 if (unlikely(fnic_chk_state_flags_locked(fnic,
2049 FNIC_FLAGS_IO_BLOCKED))) {
2050 spin_unlock_irqrestore(host->host_lock, intr_flags);
2051 return FAILED;
2052 } else
2053 atomic_inc(&fnic->in_flight);
2054 spin_unlock_irqrestore(host->host_lock, intr_flags);
2055
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002056 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
2057
2058 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
2059 free_wq_copy_descs(fnic, wq);
2060
2061 if (!vnic_wq_copy_desc_avail(wq)) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002062 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2063 "queue_dr_io_req failure - no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07002064 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002065 ret = -EAGAIN;
2066 goto lr_io_req_end;
2067 }
2068
2069 /* fill in the lun info */
2070 int_to_scsilun(sc->device->lun, &fc_lun);
2071
2072 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
2073 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
2074 fc_lun.scsi_lun, io_req->port_id,
2075 fnic->config.ra_tov, fnic->config.ed_tov);
2076
Hiral Patel67125b02013-09-12 17:45:42 -07002077 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
2078 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
2079 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
2080 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
2081 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
2082
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002083lr_io_req_end:
2084 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel03298552013-02-12 17:00:58 -08002085 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002086
2087 return ret;
2088}
2089
2090/*
2091 * Clean up any pending aborts on the lun
2092 * For each outstanding IO on this lun, whose abort is not completed by fw,
2093 * issue a local abort. Wait for abort to complete. Return 0 if all commands
2094 * successfully aborted, 1 otherwise
2095 */
2096static int fnic_clean_pending_aborts(struct fnic *fnic,
Satish Kharata36f5dd2016-03-18 11:22:49 -07002097 struct scsi_cmnd *lr_sc,
2098 bool new_sc)
2099
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002100{
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002101 int tag, abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002102 struct fnic_io_req *io_req;
2103 spinlock_t *io_lock;
2104 unsigned long flags;
2105 int ret = 0;
2106 struct scsi_cmnd *sc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002107 struct scsi_lun fc_lun;
2108 struct scsi_device *lun_dev = lr_sc->device;
2109 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002110 enum fnic_ioreq_state old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002111
Hiral Patelfc857992013-09-09 13:31:51 -07002112 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002113 io_lock = fnic_io_lock_tag(fnic, tag);
2114 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002115 sc = scsi_host_find_tag(fnic->lport->host, tag);
2116 /*
Satish Kharata36f5dd2016-03-18 11:22:49 -07002117 * ignore this lun reset cmd if issued using new SC
2118 * or cmds that do not belong to this lun
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002119 */
Satish Kharata36f5dd2016-03-18 11:22:49 -07002120 if (!sc || ((sc == lr_sc) && new_sc) || sc->device != lun_dev) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002121 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002122 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002123 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002124
2125 io_req = (struct fnic_io_req *)CMD_SP(sc);
2126
2127 if (!io_req || sc->device != lun_dev) {
2128 spin_unlock_irqrestore(io_lock, flags);
2129 continue;
2130 }
2131
2132 /*
2133 * Found IO that is still pending with firmware and
2134 * belongs to the LUN that we are resetting
2135 */
2136 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2137 "Found IO in %s on lun\n",
2138 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2139
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002140 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2141 spin_unlock_irqrestore(io_lock, flags);
2142 continue;
2143 }
2144 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08002145 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002146 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2147 "%s dev rst not pending sc 0x%p\n", __func__,
2148 sc);
2149 spin_unlock_irqrestore(io_lock, flags);
2150 continue;
2151 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002152
2153 if (io_req->abts_done)
2154 shost_printk(KERN_ERR, fnic->lport->host,
2155 "%s: io_req->abts_done is set state is %s\n",
2156 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002157 old_ioreq_state = CMD_STATE(sc);
2158 /*
2159 * Any pending IO issued prior to reset is expected to be
2160 * in abts pending state, if not we need to set
2161 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2162 * When IO is completed, the IO will be handed over and
2163 * handled in this function.
2164 */
2165 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2166
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002167 BUG_ON(io_req->abts_done);
2168
2169 abt_tag = tag;
2170 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2171 abt_tag |= FNIC_TAG_DEV_RST;
2172 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2173 "%s: dev rst sc 0x%p\n", __func__, sc);
2174 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002175
2176 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2177 io_req->abts_done = &tm_done;
2178 spin_unlock_irqrestore(io_lock, flags);
2179
2180 /* Now queue the abort command to firmware */
2181 int_to_scsilun(sc->device->lun, &fc_lun);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002182
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002183 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002184 FCPIO_ITMF_ABT_TASK_TERM,
2185 fc_lun.scsi_lun, io_req)) {
2186 spin_lock_irqsave(io_lock, flags);
2187 io_req = (struct fnic_io_req *)CMD_SP(sc);
2188 if (io_req)
2189 io_req->abts_done = NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002190 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2191 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002192 spin_unlock_irqrestore(io_lock, flags);
2193 ret = 1;
2194 goto clean_pending_aborts_end;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002195 } else {
2196 spin_lock_irqsave(io_lock, flags);
2197 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2198 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2199 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002200 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08002201 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002202
2203 wait_for_completion_timeout(&tm_done,
2204 msecs_to_jiffies
2205 (fnic->config.ed_tov));
2206
2207 /* Recheck cmd state to check if it is now aborted */
2208 spin_lock_irqsave(io_lock, flags);
2209 io_req = (struct fnic_io_req *)CMD_SP(sc);
2210 if (!io_req) {
2211 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002212 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002213 continue;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002214 }
2215
2216 io_req->abts_done = NULL;
2217
2218 /* if abort is still pending with fw, fail */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002219 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002220 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002221 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002222 ret = 1;
2223 goto clean_pending_aborts_end;
2224 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002225 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
Satish Kharata36f5dd2016-03-18 11:22:49 -07002226
2227 /* original sc used for lr is handled by dev reset code */
2228 if (sc != lr_sc)
2229 CMD_SP(sc) = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002230 spin_unlock_irqrestore(io_lock, flags);
2231
Satish Kharata36f5dd2016-03-18 11:22:49 -07002232 /* original sc used for lr is handled by dev reset code */
2233 if (sc != lr_sc) {
2234 fnic_release_ioreq_buf(fnic, io_req, sc);
2235 mempool_free(io_req, fnic->io_req_pool);
2236 }
2237
2238 /*
2239 * Any IO is returned during reset, it needs to call scsi_done
2240 * to return the scsi_cmnd to upper layer.
2241 */
2242 if (sc->scsi_done) {
2243 /* Set result to let upper SCSI layer retry */
2244 sc->result = DID_RESET << 16;
2245 sc->scsi_done(sc);
2246 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002247 }
2248
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002249 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2250
2251 /* walk again to check, if IOs are still pending in fw */
2252 if (fnic_is_abts_pending(fnic, lr_sc))
2253 ret = FAILED;
2254
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002255clean_pending_aborts_end:
2256 return ret;
2257}
2258
Hiral Patel03298552013-02-12 17:00:58 -08002259/**
2260 * fnic_scsi_host_start_tag
2261 * Allocates tagid from host's tag list
2262 **/
2263static inline int
2264fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2265{
2266 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2267 int tag, ret = SCSI_NO_TAG;
2268
2269 BUG_ON(!bqt);
2270 if (!bqt) {
2271 pr_err("Tags are not supported\n");
2272 goto end;
2273 }
2274
2275 do {
2276 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2277 if (tag >= bqt->max_depth) {
2278 pr_err("Tag allocation failure\n");
2279 goto end;
2280 }
2281 } while (test_and_set_bit(tag, bqt->tag_map));
2282
2283 bqt->tag_index[tag] = sc->request;
2284 sc->request->tag = tag;
2285 sc->tag = tag;
2286 if (!sc->request->special)
2287 sc->request->special = sc;
2288
2289 ret = tag;
2290
2291end:
2292 return ret;
2293}
2294
2295/**
2296 * fnic_scsi_host_end_tag
2297 * frees tag allocated by fnic_scsi_host_start_tag.
2298 **/
2299static inline void
2300fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2301{
2302 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2303 int tag = sc->request->tag;
2304
2305 if (tag == SCSI_NO_TAG)
2306 return;
2307
2308 BUG_ON(!bqt || !bqt->tag_index[tag]);
2309 if (!bqt)
2310 return;
2311
2312 bqt->tag_index[tag] = NULL;
2313 clear_bit(tag, bqt->tag_map);
2314
2315 return;
2316}
2317
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002318/*
2319 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2320 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2321 * on the LUN.
2322 */
2323int fnic_device_reset(struct scsi_cmnd *sc)
2324{
2325 struct fc_lport *lp;
2326 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002327 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002328 struct fc_rport *rport;
2329 int status;
2330 int ret = FAILED;
2331 spinlock_t *io_lock;
2332 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08002333 unsigned long start_time = 0;
Hiral Patel03298552013-02-12 17:00:58 -08002334 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07002335 struct fnic_stats *fnic_stats;
2336 struct reset_stats *reset_stats;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002337 int tag = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002338 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patel03298552013-02-12 17:00:58 -08002339 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
Satish Kharata36f5dd2016-03-18 11:22:49 -07002340 bool new_sc = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002341
2342 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01002343 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002344
2345 /* Get local-port, check ready and link up */
2346 lp = shost_priv(sc->device->host);
2347
2348 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002349 fnic_stats = &fnic->fnic_stats;
2350 reset_stats = &fnic->fnic_stats.reset_stats;
2351
2352 atomic64_inc(&reset_stats->device_resets);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002353
Roel Kluin0db6f432010-06-11 16:44:46 -07002354 rport = starget_to_rport(scsi_target(sc->device));
2355 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002356 "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
Hiral Patel03298552013-02-12 17:00:58 -08002357 rport->port_id, sc->device->lun, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002358
2359 if (lp->state != LPORT_ST_READY || !(lp->link_up))
2360 goto fnic_device_reset_end;
2361
2362 /* Check if remote port up */
Hiral Patel67125b02013-09-12 17:45:42 -07002363 if (fc_remote_port_chkready(rport)) {
2364 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002365 goto fnic_device_reset_end;
Hiral Patel67125b02013-09-12 17:45:42 -07002366 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002367
Hiral Patel14eb5d92013-02-12 17:01:01 -08002368 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
Hiral Patel03298552013-02-12 17:00:58 -08002369 /* Allocate tag if not present */
2370
2371 tag = sc->request->tag;
2372 if (unlikely(tag < 0)) {
Christoph Hellwig4d637162014-06-12 14:53:23 +02002373 /*
2374 * XXX(hch): current the midlayer fakes up a struct
2375 * request for the explicit reset ioctls, and those
2376 * don't have a tag allocated to them. The below
2377 * code pokes into midlayer structures to paper over
2378 * this design issue, but that won't work for blk-mq.
2379 *
2380 * Either someone who can actually test the hardware
2381 * will have to come up with a similar hack for the
2382 * blk-mq case, or we'll have to bite the bullet and
2383 * fix the way the EH ioctls work for real, but until
2384 * that happens we fail these explicit requests here.
2385 */
Christoph Hellwig4d637162014-06-12 14:53:23 +02002386
Hiral Patel03298552013-02-12 17:00:58 -08002387 tag = fnic_scsi_host_start_tag(fnic, sc);
2388 if (unlikely(tag == SCSI_NO_TAG))
2389 goto fnic_device_reset_end;
2390 tag_gen_flag = 1;
Satish Kharata36f5dd2016-03-18 11:22:49 -07002391 new_sc = 1;
Hiral Patel03298552013-02-12 17:00:58 -08002392 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002393 io_lock = fnic_io_lock_hash(fnic, sc);
2394 spin_lock_irqsave(io_lock, flags);
2395 io_req = (struct fnic_io_req *)CMD_SP(sc);
2396
2397 /*
2398 * If there is a io_req attached to this command, then use it,
2399 * else allocate a new one.
2400 */
2401 if (!io_req) {
2402 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2403 if (!io_req) {
2404 spin_unlock_irqrestore(io_lock, flags);
2405 goto fnic_device_reset_end;
2406 }
2407 memset(io_req, 0, sizeof(*io_req));
2408 io_req->port_id = rport->port_id;
2409 CMD_SP(sc) = (char *)io_req;
2410 }
2411 io_req->dr_done = &tm_done;
2412 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2413 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2414 spin_unlock_irqrestore(io_lock, flags);
2415
Hiral Patel03298552013-02-12 17:00:58 -08002416 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002417
2418 /*
2419 * issue the device reset, if enqueue failed, clean up the ioreq
2420 * and break assoc with scsi cmd
2421 */
2422 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2423 spin_lock_irqsave(io_lock, flags);
2424 io_req = (struct fnic_io_req *)CMD_SP(sc);
2425 if (io_req)
2426 io_req->dr_done = NULL;
2427 goto fnic_device_reset_clean;
2428 }
Hiral Patel03298552013-02-12 17:00:58 -08002429 spin_lock_irqsave(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002430 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08002431 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002432
2433 /*
2434 * Wait on the local completion for LUN reset. The io_req may be
2435 * freed while we wait since we hold no lock.
2436 */
2437 wait_for_completion_timeout(&tm_done,
2438 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2439
2440 spin_lock_irqsave(io_lock, flags);
2441 io_req = (struct fnic_io_req *)CMD_SP(sc);
2442 if (!io_req) {
2443 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002444 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2445 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002446 goto fnic_device_reset_end;
2447 }
2448 io_req->dr_done = NULL;
2449
2450 status = CMD_LR_STATUS(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002451
2452 /*
2453 * If lun reset not completed, bail out with failed. io_req
2454 * gets cleaned up during higher levels of EH
2455 */
2456 if (status == FCPIO_INVALID_CODE) {
Hiral Patel67125b02013-09-12 17:45:42 -07002457 atomic64_inc(&reset_stats->device_reset_timeouts);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002458 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2459 "Device reset timed out\n");
Hiral Patel03298552013-02-12 17:00:58 -08002460 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2461 spin_unlock_irqrestore(io_lock, flags);
2462 int_to_scsilun(sc->device->lun, &fc_lun);
2463 /*
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002464 * Issue abort and terminate on device reset request.
2465 * If q'ing of terminate fails, retry it after a delay.
Hiral Patel03298552013-02-12 17:00:58 -08002466 */
2467 while (1) {
2468 spin_lock_irqsave(io_lock, flags);
2469 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2470 spin_unlock_irqrestore(io_lock, flags);
2471 break;
2472 }
2473 spin_unlock_irqrestore(io_lock, flags);
2474 if (fnic_queue_abort_io_req(fnic,
2475 tag | FNIC_TAG_DEV_RST,
2476 FCPIO_ITMF_ABT_TASK_TERM,
2477 fc_lun.scsi_lun, io_req)) {
2478 wait_for_completion_timeout(&tm_done,
2479 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2480 } else {
2481 spin_lock_irqsave(io_lock, flags);
2482 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2483 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2484 io_req->abts_done = &tm_done;
2485 spin_unlock_irqrestore(io_lock, flags);
2486 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2487 "Abort and terminate issued on Device reset "
2488 "tag 0x%x sc 0x%p\n", tag, sc);
2489 break;
2490 }
2491 }
2492 while (1) {
2493 spin_lock_irqsave(io_lock, flags);
2494 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2495 spin_unlock_irqrestore(io_lock, flags);
2496 wait_for_completion_timeout(&tm_done,
2497 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2498 break;
2499 } else {
2500 io_req = (struct fnic_io_req *)CMD_SP(sc);
2501 io_req->abts_done = NULL;
2502 goto fnic_device_reset_clean;
2503 }
2504 }
2505 } else {
2506 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002507 }
2508
2509 /* Completed, but not successful, clean up the io_req, return fail */
2510 if (status != FCPIO_SUCCESS) {
2511 spin_lock_irqsave(io_lock, flags);
2512 FNIC_SCSI_DBG(KERN_DEBUG,
2513 fnic->lport->host,
2514 "Device reset completed - failed\n");
2515 io_req = (struct fnic_io_req *)CMD_SP(sc);
2516 goto fnic_device_reset_clean;
2517 }
2518
2519 /*
2520 * Clean up any aborts on this lun that have still not
2521 * completed. If any of these fail, then LUN reset fails.
2522 * clean_pending_aborts cleans all cmds on this lun except
2523 * the lun reset cmd. If all cmds get cleaned, the lun reset
2524 * succeeds
2525 */
Satish Kharata36f5dd2016-03-18 11:22:49 -07002526 if (fnic_clean_pending_aborts(fnic, sc, new_sc)) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002527 spin_lock_irqsave(io_lock, flags);
2528 io_req = (struct fnic_io_req *)CMD_SP(sc);
2529 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2530 "Device reset failed"
2531 " since could not abort all IOs\n");
2532 goto fnic_device_reset_clean;
2533 }
2534
2535 /* Clean lun reset command */
2536 spin_lock_irqsave(io_lock, flags);
2537 io_req = (struct fnic_io_req *)CMD_SP(sc);
2538 if (io_req)
2539 /* Completed, and successful */
2540 ret = SUCCESS;
2541
2542fnic_device_reset_clean:
2543 if (io_req)
2544 CMD_SP(sc) = NULL;
2545
2546 spin_unlock_irqrestore(io_lock, flags);
2547
2548 if (io_req) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002549 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002550 fnic_release_ioreq_buf(fnic, io_req, sc);
2551 mempool_free(io_req, fnic->io_req_pool);
2552 }
2553
2554fnic_device_reset_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08002555 FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2556 sc->request->tag, sc,
2557 jiffies_to_msecs(jiffies - start_time),
2558 0, ((u64)sc->cmnd[0] << 32 |
2559 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2560 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2561 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2562
Hiral Patel03298552013-02-12 17:00:58 -08002563 /* free tag if it is allocated */
2564 if (unlikely(tag_gen_flag))
2565 fnic_scsi_host_end_tag(fnic, sc);
2566
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002567 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2568 "Returning from device reset %s\n",
2569 (ret == SUCCESS) ?
2570 "SUCCESS" : "FAILED");
Hiral Patel67125b02013-09-12 17:45:42 -07002571
2572 if (ret == FAILED)
2573 atomic64_inc(&reset_stats->device_reset_failures);
2574
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002575 return ret;
2576}
2577
2578/* Clean up all IOs, clean up libFC local port */
2579int fnic_reset(struct Scsi_Host *shost)
2580{
2581 struct fc_lport *lp;
2582 struct fnic *fnic;
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002583 int ret = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07002584 struct reset_stats *reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002585
2586 lp = shost_priv(shost);
2587 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002588 reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002589
2590 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2591 "fnic_reset called\n");
2592
Hiral Patel67125b02013-09-12 17:45:42 -07002593 atomic64_inc(&reset_stats->fnic_resets);
2594
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002595 /*
2596 * Reset local port, this will clean up libFC exchanges,
2597 * reset remote port sessions, and if link is up, begin flogi
2598 */
Hannes Reinecke31c0a632016-10-18 10:01:36 +02002599 ret = fc_lport_reset(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002600
2601 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2602 "Returning from fnic reset %s\n",
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002603 (ret == 0) ?
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002604 "SUCCESS" : "FAILED");
2605
Hiral Patel67125b02013-09-12 17:45:42 -07002606 if (ret == 0)
2607 atomic64_inc(&reset_stats->fnic_reset_completions);
2608 else
2609 atomic64_inc(&reset_stats->fnic_reset_failures);
2610
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002611 return ret;
2612}
2613
2614/*
2615 * SCSI Error handling calls driver's eh_host_reset if all prior
2616 * error handling levels return FAILED. If host reset completes
2617 * successfully, and if link is up, then Fabric login begins.
2618 *
2619 * Host Reset is the highest level of error recovery. If this fails, then
2620 * host is offlined by SCSI.
2621 *
2622 */
2623int fnic_host_reset(struct scsi_cmnd *sc)
2624{
2625 int ret;
2626 unsigned long wait_host_tmo;
2627 struct Scsi_Host *shost = sc->device->host;
2628 struct fc_lport *lp = shost_priv(shost);
Satish Kharat9698b6f2016-12-14 13:20:41 -08002629 struct fnic *fnic = lport_priv(lp);
2630 unsigned long flags;
2631
2632 spin_lock_irqsave(&fnic->fnic_lock, flags);
2633 if (fnic->internal_reset_inprogress == 0) {
2634 fnic->internal_reset_inprogress = 1;
2635 } else {
2636 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2637 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2638 "host reset in progress skipping another host reset\n");
2639 return SUCCESS;
2640 }
2641 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002642
2643 /*
2644 * If fnic_reset is successful, wait for fabric login to complete
2645 * scsi-ml tries to send a TUR to every device if host reset is
2646 * successful, so before returning to scsi, fabric should be up
2647 */
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002648 ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002649 if (ret == SUCCESS) {
2650 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2651 ret = FAILED;
2652 while (time_before(jiffies, wait_host_tmo)) {
2653 if ((lp->state == LPORT_ST_READY) &&
2654 (lp->link_up)) {
2655 ret = SUCCESS;
2656 break;
2657 }
2658 ssleep(1);
2659 }
2660 }
2661
Satish Kharat9698b6f2016-12-14 13:20:41 -08002662 spin_lock_irqsave(&fnic->fnic_lock, flags);
2663 fnic->internal_reset_inprogress = 0;
2664 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002665 return ret;
2666}
2667
2668/*
2669 * This fxn is called from libFC when host is removed
2670 */
2671void fnic_scsi_abort_io(struct fc_lport *lp)
2672{
2673 int err = 0;
2674 unsigned long flags;
2675 enum fnic_state old_state;
2676 struct fnic *fnic = lport_priv(lp);
2677 DECLARE_COMPLETION_ONSTACK(remove_wait);
2678
2679 /* Issue firmware reset for fnic, wait for reset to complete */
Hiral Patel03298552013-02-12 17:00:58 -08002680retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002681 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002682 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2683 /* fw reset is in progress, poll for its completion */
2684 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2685 schedule_timeout(msecs_to_jiffies(100));
2686 goto retry_fw_reset;
2687 }
2688
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002689 fnic->remove_wait = &remove_wait;
2690 old_state = fnic->state;
2691 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002692 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002693 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2694
2695 err = fnic_fw_reset_handler(fnic);
2696 if (err) {
2697 spin_lock_irqsave(&fnic->fnic_lock, flags);
2698 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2699 fnic->state = old_state;
2700 fnic->remove_wait = NULL;
2701 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2702 return;
2703 }
2704
2705 /* Wait for firmware reset to complete */
2706 wait_for_completion_timeout(&remove_wait,
2707 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2708
2709 spin_lock_irqsave(&fnic->fnic_lock, flags);
2710 fnic->remove_wait = NULL;
2711 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2712 "fnic_scsi_abort_io %s\n",
2713 (fnic->state == FNIC_IN_ETH_MODE) ?
2714 "SUCCESS" : "FAILED");
2715 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2716
2717}
2718
2719/*
2720 * This fxn called from libFC to clean up driver IO state on link down
2721 */
2722void fnic_scsi_cleanup(struct fc_lport *lp)
2723{
2724 unsigned long flags;
2725 enum fnic_state old_state;
2726 struct fnic *fnic = lport_priv(lp);
2727
2728 /* issue fw reset */
Hiral Patel03298552013-02-12 17:00:58 -08002729retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002730 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002731 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2732 /* fw reset is in progress, poll for its completion */
2733 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2734 schedule_timeout(msecs_to_jiffies(100));
2735 goto retry_fw_reset;
2736 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002737 old_state = fnic->state;
2738 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002739 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002740 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2741
2742 if (fnic_fw_reset_handler(fnic)) {
2743 spin_lock_irqsave(&fnic->fnic_lock, flags);
2744 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2745 fnic->state = old_state;
2746 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2747 }
2748
2749}
2750
2751void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2752{
2753}
2754
2755void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2756{
2757 struct fnic *fnic = lport_priv(lp);
2758
2759 /* Non-zero sid, nothing to do */
2760 if (sid)
2761 goto call_fc_exch_mgr_reset;
2762
2763 if (did) {
2764 fnic_rport_exch_reset(fnic, did);
2765 goto call_fc_exch_mgr_reset;
2766 }
2767
2768 /*
2769 * sid = 0, did = 0
2770 * link down or device being removed
2771 */
2772 if (!fnic->in_remove)
2773 fnic_scsi_cleanup(lp);
2774 else
2775 fnic_scsi_abort_io(lp);
2776
2777 /* call libFC exch mgr reset to reset its exchanges */
2778call_fc_exch_mgr_reset:
2779 fc_exch_mgr_reset(lp, sid, did);
2780
2781}
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002782
2783/*
2784 * fnic_is_abts_pending() is a helper function that
2785 * walks through tag map to check if there is any IOs pending,if there is one,
2786 * then it returns 1 (true), otherwise 0 (false)
2787 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2788 * otherwise, it checks for all IOs.
2789 */
2790int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2791{
2792 int tag;
2793 struct fnic_io_req *io_req;
2794 spinlock_t *io_lock;
2795 unsigned long flags;
2796 int ret = 0;
2797 struct scsi_cmnd *sc;
2798 struct scsi_device *lun_dev = NULL;
2799
2800 if (lr_sc)
2801 lun_dev = lr_sc->device;
2802
2803 /* walk again to check, if IOs are still pending in fw */
Hiral Patelfc857992013-09-09 13:31:51 -07002804 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002805 sc = scsi_host_find_tag(fnic->lport->host, tag);
2806 /*
2807 * ignore this lun reset cmd or cmds that do not belong to
2808 * this lun
2809 */
2810 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2811 continue;
2812
2813 io_lock = fnic_io_lock_hash(fnic, sc);
2814 spin_lock_irqsave(io_lock, flags);
2815
2816 io_req = (struct fnic_io_req *)CMD_SP(sc);
2817
2818 if (!io_req || sc->device != lun_dev) {
2819 spin_unlock_irqrestore(io_lock, flags);
2820 continue;
2821 }
2822
2823 /*
2824 * Found IO that is still pending with firmware and
2825 * belongs to the LUN that we are resetting
2826 */
2827 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2828 "Found IO in %s on lun\n",
2829 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2830
Dan Carpenter5d65f912013-05-30 10:50:46 +03002831 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002832 ret = 1;
Dan Carpenter5d65f912013-05-30 10:50:46 +03002833 spin_unlock_irqrestore(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002834 }
2835
2836 return ret;
2837}