blob: adb3d5871e743442e04014cd2f86eae15ab27912 [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;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700826
827 /* Decode the cmpl description to get the io_req id */
828 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
829 fcpio_tag_id_dec(&tag, &id);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800830 icmnd_cmpl = &desc->u.icmnd_cmpl;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700831
Hiral Patelfc857992013-09-09 13:31:51 -0700832 if (id >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -0800833 shost_printk(KERN_ERR, fnic->lport->host,
834 "Tag out of range tag %x hdr status = %s\n",
835 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700836 return;
Hiral Patel03298552013-02-12 17:00:58 -0800837 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700838
839 sc = scsi_host_find_tag(fnic->lport->host, id);
840 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800841 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -0700842 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800843 shost_printk(KERN_ERR, fnic->lport->host,
844 "icmnd_cmpl sc is null - "
845 "hdr status = %s tag = 0x%x desc = 0x%p\n",
846 fnic_fcpio_status_to_str(hdr_status), id, desc);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800847 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
848 fnic->lport->host->host_no, id,
849 ((u64)icmnd_cmpl->_resvd0[1] << 16 |
850 (u64)icmnd_cmpl->_resvd0[0]),
851 ((u64)hdr_status << 16 |
852 (u64)icmnd_cmpl->scsi_status << 8 |
853 (u64)icmnd_cmpl->flags), desc,
854 (u64)icmnd_cmpl->residual, 0);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700855 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800856 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700857
858 io_lock = fnic_io_lock_hash(fnic, sc);
859 spin_lock_irqsave(io_lock, flags);
860 io_req = (struct fnic_io_req *)CMD_SP(sc);
861 WARN_ON_ONCE(!io_req);
862 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -0700863 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800864 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700865 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800866 shost_printk(KERN_ERR, fnic->lport->host,
867 "icmnd_cmpl io_req is null - "
868 "hdr status = %s tag = 0x%x sc 0x%p\n",
869 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700870 return;
871 }
Hiral Patel14eb5d92013-02-12 17:01:01 -0800872 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700873
874 /* firmware completed the io */
875 io_req->io_completed = 1;
876
877 /*
878 * if SCSI-ML has already issued abort on this command,
879 * ignore completion of the IO. The abts path will clean it up
880 */
881 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
882 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800883 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
884 switch (hdr_status) {
885 case FCPIO_SUCCESS:
886 CMD_FLAGS(sc) |= FNIC_IO_DONE;
887 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
888 "icmnd_cmpl ABTS pending hdr status = %s "
889 "sc 0x%p scsi_status %x residual %d\n",
890 fnic_fcpio_status_to_str(hdr_status), sc,
891 icmnd_cmpl->scsi_status,
892 icmnd_cmpl->residual);
893 break;
894 case FCPIO_ABORTED:
895 CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
896 break;
897 default:
898 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
899 "icmnd_cmpl abts pending "
900 "hdr status = %s tag = 0x%x sc = 0x%p\n",
901 fnic_fcpio_status_to_str(hdr_status),
902 id, sc);
903 break;
904 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700905 return;
906 }
907
908 /* Mark the IO as complete */
909 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
910
911 icmnd_cmpl = &desc->u.icmnd_cmpl;
912
913 switch (hdr_status) {
914 case FCPIO_SUCCESS:
915 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
916 xfer_len = scsi_bufflen(sc);
917 scsi_set_resid(sc, icmnd_cmpl->residual);
918
919 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
920 xfer_len -= icmnd_cmpl->residual;
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
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001020 /* Call SCSI completion function to complete the IO */
1021 if (sc->scsi_done)
1022 sc->scsi_done(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001023}
1024
1025/* fnic_fcpio_itmf_cmpl_handler
1026 * Routine to handle itmf completions
1027 */
1028static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1029 struct fcpio_fw_req *desc)
1030{
1031 u8 type;
1032 u8 hdr_status;
1033 struct fcpio_tag tag;
1034 u32 id;
1035 struct scsi_cmnd *sc;
1036 struct fnic_io_req *io_req;
Hiral Patel67125b02013-09-12 17:45:42 -07001037 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1038 struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1039 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1040 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001041 unsigned long flags;
1042 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001043 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001044
1045 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1046 fcpio_tag_id_dec(&tag, &id);
1047
Hiral Patelfc857992013-09-09 13:31:51 -07001048 if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -08001049 shost_printk(KERN_ERR, fnic->lport->host,
1050 "Tag out of range tag %x hdr status = %s\n",
1051 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001052 return;
Hiral Patel03298552013-02-12 17:00:58 -08001053 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001054
1055 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1056 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001057 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -07001058 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001059 shost_printk(KERN_ERR, fnic->lport->host,
1060 "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1061 fnic_fcpio_status_to_str(hdr_status), id);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001062 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001063 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001064 io_lock = fnic_io_lock_hash(fnic, sc);
1065 spin_lock_irqsave(io_lock, flags);
1066 io_req = (struct fnic_io_req *)CMD_SP(sc);
1067 WARN_ON_ONCE(!io_req);
1068 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001069 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001070 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001071 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1072 shost_printk(KERN_ERR, fnic->lport->host,
1073 "itmf_cmpl io_req is null - "
1074 "hdr status = %s tag = 0x%x sc 0x%p\n",
1075 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001076 return;
1077 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001078 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001079
Hiral Patel03298552013-02-12 17:00:58 -08001080 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1081 /* Abort and terminate completion of device reset req */
1082 /* REVISIT : Add asserts about various flags */
1083 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1084 "dev reset abts cmpl recd. id %x status %s\n",
1085 id, fnic_fcpio_status_to_str(hdr_status));
1086 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1087 CMD_ABTS_STATUS(sc) = hdr_status;
1088 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1089 if (io_req->abts_done)
1090 complete(io_req->abts_done);
1091 spin_unlock_irqrestore(io_lock, flags);
1092 } else if (id & FNIC_TAG_ABORT) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001093 /* Completion of abort cmd */
Hiral Patel67125b02013-09-12 17:45:42 -07001094 switch (hdr_status) {
1095 case FCPIO_SUCCESS:
1096 break;
1097 case FCPIO_TIMEOUT:
1098 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1099 atomic64_inc(&abts_stats->abort_fw_timeouts);
1100 else
1101 atomic64_inc(
1102 &term_stats->terminate_fw_timeouts);
1103 break;
Satish Kharat691a8372016-03-18 11:22:48 -07001104 case FCPIO_ITMF_REJECTED:
1105 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1106 "abort reject recd. id %d\n",
1107 (int)(id & FNIC_TAG_MASK));
1108 break;
Hiral Patel67125b02013-09-12 17:45:42 -07001109 case FCPIO_IO_NOT_FOUND:
1110 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1111 atomic64_inc(&abts_stats->abort_io_not_found);
1112 else
1113 atomic64_inc(
1114 &term_stats->terminate_io_not_found);
1115 break;
1116 default:
1117 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1118 atomic64_inc(&abts_stats->abort_failures);
1119 else
1120 atomic64_inc(
1121 &term_stats->terminate_failures);
1122 break;
1123 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001124 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1125 /* This is a late completion. Ignore it */
1126 spin_unlock_irqrestore(io_lock, flags);
1127 return;
1128 }
Satish Kharat691a8372016-03-18 11:22:48 -07001129
Hiral Patel14eb5d92013-02-12 17:01:01 -08001130 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Hiral Patel67125b02013-09-12 17:45:42 -07001131
Satish Kharat691a8372016-03-18 11:22:48 -07001132 /* If the status is IO not found consider it as success */
1133 if (hdr_status == FCPIO_IO_NOT_FOUND)
1134 CMD_ABTS_STATUS(sc) = FCPIO_SUCCESS;
1135 else
1136 CMD_ABTS_STATUS(sc) = hdr_status;
1137
Hiral Patel67125b02013-09-12 17:45:42 -07001138 atomic64_dec(&fnic_stats->io_stats.active_ios);
1139 if (atomic64_read(&fnic->io_cmpl_skip))
1140 atomic64_dec(&fnic->io_cmpl_skip);
1141 else
1142 atomic64_inc(&fnic_stats->io_stats.io_completions);
1143
1144 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1145 atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1146
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001147 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1148 "abts cmpl recd. id %d status %s\n",
1149 (int)(id & FNIC_TAG_MASK),
1150 fnic_fcpio_status_to_str(hdr_status));
1151
1152 /*
1153 * If scsi_eh thread is blocked waiting for abts to complete,
1154 * signal completion to it. IO will be cleaned in the thread
1155 * else clean it in this context
1156 */
1157 if (io_req->abts_done) {
1158 complete(io_req->abts_done);
1159 spin_unlock_irqrestore(io_lock, flags);
1160 } else {
1161 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1162 "abts cmpl, completing IO\n");
1163 CMD_SP(sc) = NULL;
1164 sc->result = (DID_ERROR << 16);
1165
1166 spin_unlock_irqrestore(io_lock, flags);
1167
1168 fnic_release_ioreq_buf(fnic, io_req, sc);
1169 mempool_free(io_req, fnic->io_req_pool);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001170 if (sc->scsi_done) {
1171 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1172 sc->device->host->host_no, id,
1173 sc,
1174 jiffies_to_msecs(jiffies - start_time),
1175 desc,
1176 (((u64)hdr_status << 40) |
1177 (u64)sc->cmnd[0] << 32 |
1178 (u64)sc->cmnd[2] << 24 |
1179 (u64)sc->cmnd[3] << 16 |
1180 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1181 (((u64)CMD_FLAGS(sc) << 32) |
1182 CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001183 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001184 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001185 }
1186
1187 } else if (id & FNIC_TAG_DEV_RST) {
1188 /* Completion of device reset */
1189 CMD_LR_STATUS(sc) = hdr_status;
Hiral Patel03298552013-02-12 17:00:58 -08001190 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1191 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001192 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001193 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1194 sc->device->host->host_no, id, sc,
1195 jiffies_to_msecs(jiffies - start_time),
1196 desc, 0,
1197 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001198 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1199 "Terminate pending "
1200 "dev reset cmpl recd. id %d status %s\n",
1201 (int)(id & FNIC_TAG_MASK),
1202 fnic_fcpio_status_to_str(hdr_status));
1203 return;
1204 }
1205 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1206 /* Need to wait for terminate completion */
1207 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001208 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1209 sc->device->host->host_no, id, sc,
1210 jiffies_to_msecs(jiffies - start_time),
1211 desc, 0,
1212 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001213 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1214 "dev reset cmpl recd after time out. "
1215 "id %d status %s\n",
1216 (int)(id & FNIC_TAG_MASK),
1217 fnic_fcpio_status_to_str(hdr_status));
1218 return;
1219 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001220 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
Hiral Patel03298552013-02-12 17:00:58 -08001221 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001222 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1223 "dev reset cmpl recd. id %d status %s\n",
1224 (int)(id & FNIC_TAG_MASK),
1225 fnic_fcpio_status_to_str(hdr_status));
1226 if (io_req->dr_done)
1227 complete(io_req->dr_done);
1228 spin_unlock_irqrestore(io_lock, flags);
1229
1230 } else {
1231 shost_printk(KERN_ERR, fnic->lport->host,
1232 "Unexpected itmf io state %s tag %x\n",
1233 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1234 spin_unlock_irqrestore(io_lock, flags);
1235 }
1236
1237}
1238
1239/*
1240 * fnic_fcpio_cmpl_handler
1241 * Routine to service the cq for wq_copy
1242 */
1243static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1244 unsigned int cq_index,
1245 struct fcpio_fw_req *desc)
1246{
1247 struct fnic *fnic = vnic_dev_priv(vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001248
1249 switch (desc->hdr.type) {
Hiral Patel67125b02013-09-12 17:45:42 -07001250 case FCPIO_ICMND_CMPL: /* fw completed a command */
1251 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1252 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1253 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1254 case FCPIO_RESET_CMPL: /* fw completed reset */
1255 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1256 break;
1257 default:
1258 break;
1259 }
1260
1261 switch (desc->hdr.type) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001262 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1263 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1264 break;
1265
1266 case FCPIO_ICMND_CMPL: /* fw completed a command */
1267 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1268 break;
1269
1270 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1271 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1272 break;
1273
1274 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
Joe Eykholt78112e52009-11-03 11:49:22 -08001275 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
Hiral Patel03298552013-02-12 17:00:58 -08001276 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001277 break;
1278
1279 case FCPIO_RESET_CMPL: /* fw completed reset */
Hiral Patel03298552013-02-12 17:00:58 -08001280 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001281 break;
1282
1283 default:
1284 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1285 "firmware completion type %d\n",
1286 desc->hdr.type);
1287 break;
1288 }
1289
Hiral Patel03298552013-02-12 17:00:58 -08001290 return 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001291}
1292
1293/*
1294 * fnic_wq_copy_cmpl_handler
1295 * Routine to process wq copy
1296 */
1297int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1298{
1299 unsigned int wq_work_done = 0;
1300 unsigned int i, cq_index;
1301 unsigned int cur_work_done;
1302
1303 for (i = 0; i < fnic->wq_copy_count; i++) {
1304 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1305 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1306 fnic_fcpio_cmpl_handler,
1307 copy_work_to_do);
1308 wq_work_done += cur_work_done;
1309 }
1310 return wq_work_done;
1311}
1312
1313static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1314{
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001315 int i;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001316 struct fnic_io_req *io_req;
1317 unsigned long flags = 0;
1318 struct scsi_cmnd *sc;
1319 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001320 unsigned long start_time = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07001321 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001322
Hiral Patelfc857992013-09-09 13:31:51 -07001323 for (i = 0; i < fnic->fnic_max_tag_id; i++) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001324 if (i == exclude_id)
1325 continue;
1326
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001327 io_lock = fnic_io_lock_tag(fnic, i);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001328 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001329 sc = scsi_host_find_tag(fnic->lport->host, i);
1330 if (!sc) {
1331 spin_unlock_irqrestore(io_lock, flags);
1332 continue;
1333 }
1334
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001335 io_req = (struct fnic_io_req *)CMD_SP(sc);
Hiral Patel03298552013-02-12 17:00:58 -08001336 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1337 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1338 /*
1339 * We will be here only when FW completes reset
1340 * without sending completions for outstanding ios.
1341 */
1342 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1343 if (io_req && io_req->dr_done)
1344 complete(io_req->dr_done);
1345 else if (io_req && io_req->abts_done)
1346 complete(io_req->abts_done);
1347 spin_unlock_irqrestore(io_lock, flags);
1348 continue;
1349 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1350 spin_unlock_irqrestore(io_lock, flags);
1351 continue;
1352 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001353 if (!io_req) {
1354 spin_unlock_irqrestore(io_lock, flags);
1355 goto cleanup_scsi_cmd;
1356 }
1357
1358 CMD_SP(sc) = NULL;
1359
1360 spin_unlock_irqrestore(io_lock, flags);
1361
1362 /*
1363 * If there is a scsi_cmnd associated with this io_req, then
1364 * free the corresponding state
1365 */
Hiral Patel14eb5d92013-02-12 17:01:01 -08001366 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001367 fnic_release_ioreq_buf(fnic, io_req, sc);
1368 mempool_free(io_req, fnic->io_req_pool);
1369
1370cleanup_scsi_cmd:
1371 sc->result = DID_TRANSPORT_DISRUPTED << 16;
Hiral Shah66818662014-04-18 12:28:18 -07001372 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1373 "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1374 __func__, (jiffies - start_time));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001375
Hiral Patel67125b02013-09-12 17:45:42 -07001376 if (atomic64_read(&fnic->io_cmpl_skip))
1377 atomic64_dec(&fnic->io_cmpl_skip);
1378 else
1379 atomic64_inc(&fnic_stats->io_stats.io_completions);
1380
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001381 /* Complete the command to SCSI */
Hiral Patel4d7007b2013-02-12 17:01:02 -08001382 if (sc->scsi_done) {
1383 FNIC_TRACE(fnic_cleanup_io,
1384 sc->device->host->host_no, i, sc,
1385 jiffies_to_msecs(jiffies - start_time),
1386 0, ((u64)sc->cmnd[0] << 32 |
1387 (u64)sc->cmnd[2] << 24 |
1388 (u64)sc->cmnd[3] << 16 |
1389 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1390 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1391
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001392 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001393 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001394 }
1395}
1396
1397void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1398 struct fcpio_host_req *desc)
1399{
1400 u32 id;
1401 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1402 struct fnic_io_req *io_req;
1403 struct scsi_cmnd *sc;
1404 unsigned long flags;
1405 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001406 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001407
1408 /* get the tag reference */
1409 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1410 id &= FNIC_TAG_MASK;
1411
Hiral Patelfc857992013-09-09 13:31:51 -07001412 if (id >= fnic->fnic_max_tag_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001413 return;
1414
1415 sc = scsi_host_find_tag(fnic->lport->host, id);
1416 if (!sc)
1417 return;
1418
1419 io_lock = fnic_io_lock_hash(fnic, sc);
1420 spin_lock_irqsave(io_lock, flags);
1421
1422 /* Get the IO context which this desc refers to */
1423 io_req = (struct fnic_io_req *)CMD_SP(sc);
1424
1425 /* fnic interrupts are turned off by now */
1426
1427 if (!io_req) {
1428 spin_unlock_irqrestore(io_lock, flags);
1429 goto wq_copy_cleanup_scsi_cmd;
1430 }
1431
1432 CMD_SP(sc) = NULL;
1433
1434 spin_unlock_irqrestore(io_lock, flags);
1435
Hiral Patel14eb5d92013-02-12 17:01:01 -08001436 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001437 fnic_release_ioreq_buf(fnic, io_req, sc);
1438 mempool_free(io_req, fnic->io_req_pool);
1439
1440wq_copy_cleanup_scsi_cmd:
1441 sc->result = DID_NO_CONNECT << 16;
1442 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1443 " DID_NO_CONNECT\n");
1444
Hiral Patel4d7007b2013-02-12 17:01:02 -08001445 if (sc->scsi_done) {
1446 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1447 sc->device->host->host_no, id, sc,
1448 jiffies_to_msecs(jiffies - start_time),
1449 0, ((u64)sc->cmnd[0] << 32 |
1450 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1451 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1452 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1453
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001454 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001455 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001456}
1457
1458static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1459 u32 task_req, u8 *fc_lun,
1460 struct fnic_io_req *io_req)
1461{
1462 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001463 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001464 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001465 unsigned long flags;
1466
Hiral Patel03298552013-02-12 17:00:58 -08001467 spin_lock_irqsave(host->host_lock, flags);
1468 if (unlikely(fnic_chk_state_flags_locked(fnic,
1469 FNIC_FLAGS_IO_BLOCKED))) {
1470 spin_unlock_irqrestore(host->host_lock, flags);
1471 return 1;
1472 } else
1473 atomic_inc(&fnic->in_flight);
1474 spin_unlock_irqrestore(host->host_lock, flags);
1475
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001476 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1477
1478 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1479 free_wq_copy_descs(fnic, wq);
1480
1481 if (!vnic_wq_copy_desc_avail(wq)) {
1482 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001483 atomic_dec(&fnic->in_flight);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001484 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001485 "fnic_queue_abort_io_req: failure: no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07001486 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001487 return 1;
1488 }
1489 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1490 0, task_req, tag, fc_lun, io_req->port_id,
1491 fnic->config.ra_tov, fnic->config.ed_tov);
1492
Hiral Patel67125b02013-09-12 17:45:42 -07001493 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1494 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1495 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1496 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1497 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1498
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001499 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001500 atomic_dec(&fnic->in_flight);
1501
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001502 return 0;
1503}
1504
Hiral Patel03298552013-02-12 17:00:58 -08001505static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001506{
1507 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001508 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001509 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001510 struct fnic_io_req *io_req;
1511 spinlock_t *io_lock;
1512 unsigned long flags;
1513 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -07001514 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1515 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001516 struct scsi_lun fc_lun;
1517 enum fnic_ioreq_state old_ioreq_state;
1518
1519 FNIC_SCSI_DBG(KERN_DEBUG,
1520 fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001521 "fnic_rport_exch_reset called portid 0x%06x\n",
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001522 port_id);
1523
1524 if (fnic->in_remove)
1525 return;
1526
Hiral Patelfc857992013-09-09 13:31:51 -07001527 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001528 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001529 io_lock = fnic_io_lock_tag(fnic, tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001530 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001531 sc = scsi_host_find_tag(fnic->lport->host, tag);
1532 if (!sc) {
1533 spin_unlock_irqrestore(io_lock, flags);
1534 continue;
1535 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001536
1537 io_req = (struct fnic_io_req *)CMD_SP(sc);
1538
1539 if (!io_req || io_req->port_id != port_id) {
1540 spin_unlock_irqrestore(io_lock, flags);
1541 continue;
1542 }
1543
Hiral Patel03298552013-02-12 17:00:58 -08001544 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001545 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001546 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1547 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1548 sc);
1549 spin_unlock_irqrestore(io_lock, flags);
1550 continue;
1551 }
1552
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001553 /*
1554 * Found IO that is still pending with firmware and
1555 * belongs to rport that went away
1556 */
1557 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1558 spin_unlock_irqrestore(io_lock, flags);
1559 continue;
1560 }
Hiral Patel03298552013-02-12 17:00:58 -08001561 if (io_req->abts_done) {
1562 shost_printk(KERN_ERR, fnic->lport->host,
1563 "fnic_rport_exch_reset: io_req->abts_done is set "
1564 "state is %s\n",
1565 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1566 }
1567
Hiral Patel14eb5d92013-02-12 17:01:01 -08001568 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1569 shost_printk(KERN_ERR, fnic->lport->host,
1570 "rport_exch_reset "
1571 "IO not yet issued %p tag 0x%x flags "
1572 "%x state %d\n",
1573 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1574 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001575 old_ioreq_state = CMD_STATE(sc);
1576 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1577 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001578 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001579 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001580 abt_tag = (tag | FNIC_TAG_DEV_RST);
1581 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1582 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1583 sc);
1584 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001585
1586 BUG_ON(io_req->abts_done);
1587
1588 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1589 "fnic_rport_reset_exch: Issuing abts\n");
1590
1591 spin_unlock_irqrestore(io_lock, flags);
1592
1593 /* Now queue the abort command to firmware */
1594 int_to_scsilun(sc->device->lun, &fc_lun);
1595
Hiral Patel03298552013-02-12 17:00:58 -08001596 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001597 FCPIO_ITMF_ABT_TASK_TERM,
1598 fc_lun.scsi_lun, io_req)) {
1599 /*
1600 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001601 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001602 * aborted later by scsi_eh, or cleaned up during
1603 * lun reset
1604 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001605 spin_lock_irqsave(io_lock, flags);
1606 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1607 CMD_STATE(sc) = old_ioreq_state;
1608 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001609 } else {
1610 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001611 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1612 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001613 else
1614 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001615 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001616 atomic64_inc(&term_stats->terminates);
1617 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001618 }
1619 }
Hiral Patel67125b02013-09-12 17:45:42 -07001620 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1621 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001622
1623}
1624
1625void fnic_terminate_rport_io(struct fc_rport *rport)
1626{
1627 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001628 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001629 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001630 struct fnic_io_req *io_req;
1631 spinlock_t *io_lock;
1632 unsigned long flags;
1633 struct scsi_cmnd *sc;
1634 struct scsi_lun fc_lun;
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001635 struct fc_rport_libfc_priv *rdata;
1636 struct fc_lport *lport;
1637 struct fnic *fnic;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001638 struct fc_rport *cmd_rport;
Hiral Patel67125b02013-09-12 17:45:42 -07001639 struct reset_stats *reset_stats;
1640 struct terminate_stats *term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001641 enum fnic_ioreq_state old_ioreq_state;
1642
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001643 if (!rport) {
1644 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1645 return;
1646 }
1647 rdata = rport->dd_data;
1648
1649 if (!rdata) {
1650 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1651 return;
1652 }
1653 lport = rdata->local_port;
1654
1655 if (!lport) {
1656 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1657 return;
1658 }
1659 fnic = lport_priv(lport);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001660 FNIC_SCSI_DBG(KERN_DEBUG,
1661 fnic->lport->host, "fnic_terminate_rport_io called"
Hiral Patel03298552013-02-12 17:00:58 -08001662 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1663 rport->port_name, rport->node_name, rport,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001664 rport->port_id);
1665
1666 if (fnic->in_remove)
1667 return;
1668
Hiral Patel67125b02013-09-12 17:45:42 -07001669 reset_stats = &fnic->fnic_stats.reset_stats;
1670 term_stats = &fnic->fnic_stats.term_stats;
1671
Hiral Patelfc857992013-09-09 13:31:51 -07001672 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001673 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001674 io_lock = fnic_io_lock_tag(fnic, tag);
1675 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001676 sc = scsi_host_find_tag(fnic->lport->host, tag);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001677 if (!sc) {
1678 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001679 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001680 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001681
1682 cmd_rport = starget_to_rport(scsi_target(sc->device));
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001683 if (rport != cmd_rport) {
1684 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001685 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001686 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001687
1688 io_req = (struct fnic_io_req *)CMD_SP(sc);
1689
1690 if (!io_req || rport != cmd_rport) {
1691 spin_unlock_irqrestore(io_lock, flags);
1692 continue;
1693 }
1694
Hiral Patel03298552013-02-12 17:00:58 -08001695 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001696 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001697 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1698 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1699 sc);
1700 spin_unlock_irqrestore(io_lock, flags);
1701 continue;
1702 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001703 /*
1704 * Found IO that is still pending with firmware and
1705 * belongs to rport that went away
1706 */
1707 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1708 spin_unlock_irqrestore(io_lock, flags);
1709 continue;
1710 }
Hiral Patel03298552013-02-12 17:00:58 -08001711 if (io_req->abts_done) {
1712 shost_printk(KERN_ERR, fnic->lport->host,
1713 "fnic_terminate_rport_io: io_req->abts_done is set "
1714 "state is %s\n",
1715 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1716 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001717 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1718 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1719 "fnic_terminate_rport_io "
1720 "IO not yet issued %p tag 0x%x flags "
1721 "%x state %d\n",
1722 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1723 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001724 old_ioreq_state = CMD_STATE(sc);
1725 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1726 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001727 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001728 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001729 abt_tag = (tag | FNIC_TAG_DEV_RST);
1730 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1731 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1732 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001733
1734 BUG_ON(io_req->abts_done);
1735
1736 FNIC_SCSI_DBG(KERN_DEBUG,
1737 fnic->lport->host,
1738 "fnic_terminate_rport_io: Issuing abts\n");
1739
1740 spin_unlock_irqrestore(io_lock, flags);
1741
1742 /* Now queue the abort command to firmware */
1743 int_to_scsilun(sc->device->lun, &fc_lun);
1744
Hiral Patel03298552013-02-12 17:00:58 -08001745 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001746 FCPIO_ITMF_ABT_TASK_TERM,
1747 fc_lun.scsi_lun, io_req)) {
1748 /*
1749 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001750 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001751 * aborted later by scsi_eh, or cleaned up during
1752 * lun reset
1753 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001754 spin_lock_irqsave(io_lock, flags);
1755 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1756 CMD_STATE(sc) = old_ioreq_state;
1757 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001758 } else {
1759 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001760 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1761 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001762 else
1763 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001764 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001765 atomic64_inc(&term_stats->terminates);
1766 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001767 }
1768 }
Hiral Patel67125b02013-09-12 17:45:42 -07001769 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1770 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001771
1772}
1773
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001774/*
1775 * This function is exported to SCSI for sending abort cmnds.
1776 * A SCSI IO is represented by a io_req in the driver.
1777 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1778 */
1779int fnic_abort_cmd(struct scsi_cmnd *sc)
1780{
1781 struct fc_lport *lp;
1782 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001783 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001784 struct fc_rport *rport;
1785 spinlock_t *io_lock;
1786 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001787 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001788 int ret = SUCCESS;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001789 u32 task_req = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001790 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07001791 struct fnic_stats *fnic_stats;
1792 struct abort_stats *abts_stats;
1793 struct terminate_stats *term_stats;
Hiral Shah66818662014-04-18 12:28:18 -07001794 enum fnic_ioreq_state old_ioreq_state;
Hiral Patel03298552013-02-12 17:00:58 -08001795 int tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001796 DECLARE_COMPLETION_ONSTACK(tm_done);
1797
1798 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001799 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001800
1801 /* Get local-port, check ready and link up */
1802 lp = shost_priv(sc->device->host);
1803
1804 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07001805 fnic_stats = &fnic->fnic_stats;
1806 abts_stats = &fnic->fnic_stats.abts_stats;
1807 term_stats = &fnic->fnic_stats.term_stats;
1808
Roel Kluin0db6f432010-06-11 16:44:46 -07001809 rport = starget_to_rport(scsi_target(sc->device));
Hiral Patel03298552013-02-12 17:00:58 -08001810 tag = sc->request->tag;
1811 FNIC_SCSI_DBG(KERN_DEBUG,
1812 fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001813 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
Hiral Patel03298552013-02-12 17:00:58 -08001814 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1815
1816 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1817
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001818 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1819 ret = FAILED;
1820 goto fnic_abort_cmd_end;
1821 }
1822
1823 /*
1824 * Avoid a race between SCSI issuing the abort and the device
1825 * completing the command.
1826 *
1827 * If the command is already completed by the fw cmpl code,
1828 * we just return SUCCESS from here. This means that the abort
1829 * succeeded. In the SCSI ML, since the timeout for command has
1830 * happened, the completion wont actually complete the command
1831 * and it will be considered as an aborted command
1832 *
1833 * The CMD_SP will not be cleared except while holding io_req_lock.
1834 */
1835 io_lock = fnic_io_lock_hash(fnic, sc);
1836 spin_lock_irqsave(io_lock, flags);
1837 io_req = (struct fnic_io_req *)CMD_SP(sc);
1838 if (!io_req) {
1839 spin_unlock_irqrestore(io_lock, flags);
1840 goto fnic_abort_cmd_end;
1841 }
1842
1843 io_req->abts_done = &tm_done;
1844
1845 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1846 spin_unlock_irqrestore(io_lock, flags);
1847 goto wait_pending;
1848 }
1849 /*
1850 * Command is still pending, need to abort it
1851 * If the firmware completes the command after this point,
1852 * the completion wont be done till mid-layer, since abort
1853 * has already started.
1854 */
Hiral Shah66818662014-04-18 12:28:18 -07001855 old_ioreq_state = CMD_STATE(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001856 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1857 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1858
1859 spin_unlock_irqrestore(io_lock, flags);
1860
1861 /*
1862 * Check readiness of the remote port. If the path to remote
1863 * port is up, then send abts to the remote port to terminate
1864 * the IO. Else, just locally terminate the IO in the firmware
1865 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001866 if (fc_remote_port_chkready(rport) == 0)
1867 task_req = FCPIO_ITMF_ABT_TASK;
Hiral Patel67125b02013-09-12 17:45:42 -07001868 else {
1869 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001870 task_req = FCPIO_ITMF_ABT_TASK_TERM;
Hiral Patel67125b02013-09-12 17:45:42 -07001871 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001872
1873 /* Now queue the abort command to firmware */
1874 int_to_scsilun(sc->device->lun, &fc_lun);
1875
1876 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1877 fc_lun.scsi_lun, io_req)) {
1878 spin_lock_irqsave(io_lock, flags);
Hiral Shah66818662014-04-18 12:28:18 -07001879 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1880 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001881 io_req = (struct fnic_io_req *)CMD_SP(sc);
1882 if (io_req)
1883 io_req->abts_done = NULL;
1884 spin_unlock_irqrestore(io_lock, flags);
1885 ret = FAILED;
1886 goto fnic_abort_cmd_end;
1887 }
Hiral Patel67125b02013-09-12 17:45:42 -07001888 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001889 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001890 atomic64_inc(&fnic_stats->abts_stats.aborts);
1891 } else {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001892 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001893 atomic64_inc(&fnic_stats->term_stats.terminates);
1894 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001895
1896 /*
1897 * We queued an abort IO, wait for its completion.
1898 * Once the firmware completes the abort command, it will
1899 * wake up this thread.
1900 */
1901 wait_pending:
1902 wait_for_completion_timeout(&tm_done,
1903 msecs_to_jiffies
1904 (2 * fnic->config.ra_tov +
1905 fnic->config.ed_tov));
1906
1907 /* Check the abort status */
1908 spin_lock_irqsave(io_lock, flags);
1909
1910 io_req = (struct fnic_io_req *)CMD_SP(sc);
1911 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001912 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001913 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001914 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001915 ret = FAILED;
1916 goto fnic_abort_cmd_end;
1917 }
1918 io_req->abts_done = NULL;
1919
1920 /* fw did not complete abort, timed out */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001921 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001922 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001923 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel67125b02013-09-12 17:45:42 -07001924 atomic64_inc(&abts_stats->abort_drv_timeouts);
1925 } else {
Hiral Patel67125b02013-09-12 17:45:42 -07001926 atomic64_inc(&term_stats->terminate_drv_timeouts);
1927 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001928 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001929 ret = FAILED;
1930 goto fnic_abort_cmd_end;
1931 }
1932
Anil Chintalapati (achintal)efc7a282014-12-23 19:40:00 +00001933 /* IO out of order */
1934
1935 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1936 spin_unlock_irqrestore(io_lock, flags);
1937 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1938 "Issuing Host reset due to out of order IO\n");
1939
1940 if (fnic_host_reset(sc) == FAILED) {
1941 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1942 "fnic_host_reset failed.\n");
1943 }
1944 ret = FAILED;
1945 goto fnic_abort_cmd_end;
1946 }
1947
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001948 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1949
Satish Kharat691a8372016-03-18 11:22:48 -07001950 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001951 /*
1952 * firmware completed the abort, check the status,
Satish Kharat691a8372016-03-18 11:22:48 -07001953 * free the io_req if successful. If abort fails,
1954 * Device reset will clean the I/O.
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001955 */
Satish Kharat691a8372016-03-18 11:22:48 -07001956 if (CMD_ABTS_STATUS(sc) == FCPIO_SUCCESS)
1957 CMD_SP(sc) = NULL;
1958 else {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001959 ret = FAILED;
Satish Kharat691a8372016-03-18 11:22:48 -07001960 spin_unlock_irqrestore(io_lock, flags);
1961 goto fnic_abort_cmd_end;
1962 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001963
1964 spin_unlock_irqrestore(io_lock, flags);
1965
1966 fnic_release_ioreq_buf(fnic, io_req, sc);
1967 mempool_free(io_req, fnic->io_req_pool);
1968
Satish Kharat691a8372016-03-18 11:22:48 -07001969 if (sc->scsi_done) {
1970 /* Call SCSI completion function to complete the IO */
1971 sc->result = (DID_ABORT << 16);
1972 sc->scsi_done(sc);
1973 }
1974
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001975fnic_abort_cmd_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08001976 FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1977 sc->request->tag, sc,
1978 jiffies_to_msecs(jiffies - start_time),
1979 0, ((u64)sc->cmnd[0] << 32 |
1980 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1981 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1982 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1983
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001984 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel14eb5d92013-02-12 17:01:01 -08001985 "Returning from abort cmd type %x %s\n", task_req,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001986 (ret == SUCCESS) ?
1987 "SUCCESS" : "FAILED");
1988 return ret;
1989}
1990
1991static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1992 struct scsi_cmnd *sc,
1993 struct fnic_io_req *io_req)
1994{
1995 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001996 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001997 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001998 struct scsi_lun fc_lun;
1999 int ret = 0;
2000 unsigned long intr_flags;
2001
Hiral Patel03298552013-02-12 17:00:58 -08002002 spin_lock_irqsave(host->host_lock, intr_flags);
2003 if (unlikely(fnic_chk_state_flags_locked(fnic,
2004 FNIC_FLAGS_IO_BLOCKED))) {
2005 spin_unlock_irqrestore(host->host_lock, intr_flags);
2006 return FAILED;
2007 } else
2008 atomic_inc(&fnic->in_flight);
2009 spin_unlock_irqrestore(host->host_lock, intr_flags);
2010
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002011 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
2012
2013 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
2014 free_wq_copy_descs(fnic, wq);
2015
2016 if (!vnic_wq_copy_desc_avail(wq)) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002017 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2018 "queue_dr_io_req failure - no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07002019 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002020 ret = -EAGAIN;
2021 goto lr_io_req_end;
2022 }
2023
2024 /* fill in the lun info */
2025 int_to_scsilun(sc->device->lun, &fc_lun);
2026
2027 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
2028 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
2029 fc_lun.scsi_lun, io_req->port_id,
2030 fnic->config.ra_tov, fnic->config.ed_tov);
2031
Hiral Patel67125b02013-09-12 17:45:42 -07002032 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
2033 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
2034 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
2035 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
2036 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
2037
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002038lr_io_req_end:
2039 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel03298552013-02-12 17:00:58 -08002040 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002041
2042 return ret;
2043}
2044
2045/*
2046 * Clean up any pending aborts on the lun
2047 * For each outstanding IO on this lun, whose abort is not completed by fw,
2048 * issue a local abort. Wait for abort to complete. Return 0 if all commands
2049 * successfully aborted, 1 otherwise
2050 */
2051static int fnic_clean_pending_aborts(struct fnic *fnic,
Satish Kharata36f5dd2016-03-18 11:22:49 -07002052 struct scsi_cmnd *lr_sc,
2053 bool new_sc)
2054
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002055{
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002056 int tag, abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002057 struct fnic_io_req *io_req;
2058 spinlock_t *io_lock;
2059 unsigned long flags;
2060 int ret = 0;
2061 struct scsi_cmnd *sc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002062 struct scsi_lun fc_lun;
2063 struct scsi_device *lun_dev = lr_sc->device;
2064 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002065 enum fnic_ioreq_state old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002066
Hiral Patelfc857992013-09-09 13:31:51 -07002067 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002068 io_lock = fnic_io_lock_tag(fnic, tag);
2069 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002070 sc = scsi_host_find_tag(fnic->lport->host, tag);
2071 /*
Satish Kharata36f5dd2016-03-18 11:22:49 -07002072 * ignore this lun reset cmd if issued using new SC
2073 * or cmds that do not belong to this lun
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002074 */
Satish Kharata36f5dd2016-03-18 11:22:49 -07002075 if (!sc || ((sc == lr_sc) && new_sc) || sc->device != lun_dev) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002076 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002077 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002078 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002079
2080 io_req = (struct fnic_io_req *)CMD_SP(sc);
2081
2082 if (!io_req || sc->device != lun_dev) {
2083 spin_unlock_irqrestore(io_lock, flags);
2084 continue;
2085 }
2086
2087 /*
2088 * Found IO that is still pending with firmware and
2089 * belongs to the LUN that we are resetting
2090 */
2091 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2092 "Found IO in %s on lun\n",
2093 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2094
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002095 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2096 spin_unlock_irqrestore(io_lock, flags);
2097 continue;
2098 }
2099 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08002100 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002101 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2102 "%s dev rst not pending sc 0x%p\n", __func__,
2103 sc);
2104 spin_unlock_irqrestore(io_lock, flags);
2105 continue;
2106 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002107
2108 if (io_req->abts_done)
2109 shost_printk(KERN_ERR, fnic->lport->host,
2110 "%s: io_req->abts_done is set state is %s\n",
2111 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002112 old_ioreq_state = CMD_STATE(sc);
2113 /*
2114 * Any pending IO issued prior to reset is expected to be
2115 * in abts pending state, if not we need to set
2116 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2117 * When IO is completed, the IO will be handed over and
2118 * handled in this function.
2119 */
2120 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2121
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002122 BUG_ON(io_req->abts_done);
2123
2124 abt_tag = tag;
2125 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2126 abt_tag |= FNIC_TAG_DEV_RST;
2127 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2128 "%s: dev rst sc 0x%p\n", __func__, sc);
2129 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002130
2131 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2132 io_req->abts_done = &tm_done;
2133 spin_unlock_irqrestore(io_lock, flags);
2134
2135 /* Now queue the abort command to firmware */
2136 int_to_scsilun(sc->device->lun, &fc_lun);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002137
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002138 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002139 FCPIO_ITMF_ABT_TASK_TERM,
2140 fc_lun.scsi_lun, io_req)) {
2141 spin_lock_irqsave(io_lock, flags);
2142 io_req = (struct fnic_io_req *)CMD_SP(sc);
2143 if (io_req)
2144 io_req->abts_done = NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002145 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2146 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002147 spin_unlock_irqrestore(io_lock, flags);
2148 ret = 1;
2149 goto clean_pending_aborts_end;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002150 } else {
2151 spin_lock_irqsave(io_lock, flags);
2152 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2153 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2154 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002155 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08002156 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002157
2158 wait_for_completion_timeout(&tm_done,
2159 msecs_to_jiffies
2160 (fnic->config.ed_tov));
2161
2162 /* Recheck cmd state to check if it is now aborted */
2163 spin_lock_irqsave(io_lock, flags);
2164 io_req = (struct fnic_io_req *)CMD_SP(sc);
2165 if (!io_req) {
2166 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002167 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002168 continue;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002169 }
2170
2171 io_req->abts_done = NULL;
2172
2173 /* if abort is still pending with fw, fail */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002174 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002175 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002176 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002177 ret = 1;
2178 goto clean_pending_aborts_end;
2179 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002180 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
Satish Kharata36f5dd2016-03-18 11:22:49 -07002181
2182 /* original sc used for lr is handled by dev reset code */
2183 if (sc != lr_sc)
2184 CMD_SP(sc) = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002185 spin_unlock_irqrestore(io_lock, flags);
2186
Satish Kharata36f5dd2016-03-18 11:22:49 -07002187 /* original sc used for lr is handled by dev reset code */
2188 if (sc != lr_sc) {
2189 fnic_release_ioreq_buf(fnic, io_req, sc);
2190 mempool_free(io_req, fnic->io_req_pool);
2191 }
2192
2193 /*
2194 * Any IO is returned during reset, it needs to call scsi_done
2195 * to return the scsi_cmnd to upper layer.
2196 */
2197 if (sc->scsi_done) {
2198 /* Set result to let upper SCSI layer retry */
2199 sc->result = DID_RESET << 16;
2200 sc->scsi_done(sc);
2201 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002202 }
2203
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002204 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2205
2206 /* walk again to check, if IOs are still pending in fw */
2207 if (fnic_is_abts_pending(fnic, lr_sc))
2208 ret = FAILED;
2209
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002210clean_pending_aborts_end:
2211 return ret;
2212}
2213
Hiral Patel03298552013-02-12 17:00:58 -08002214/**
2215 * fnic_scsi_host_start_tag
2216 * Allocates tagid from host's tag list
2217 **/
2218static inline int
2219fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2220{
2221 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2222 int tag, ret = SCSI_NO_TAG;
2223
2224 BUG_ON(!bqt);
2225 if (!bqt) {
2226 pr_err("Tags are not supported\n");
2227 goto end;
2228 }
2229
2230 do {
2231 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2232 if (tag >= bqt->max_depth) {
2233 pr_err("Tag allocation failure\n");
2234 goto end;
2235 }
2236 } while (test_and_set_bit(tag, bqt->tag_map));
2237
2238 bqt->tag_index[tag] = sc->request;
2239 sc->request->tag = tag;
2240 sc->tag = tag;
2241 if (!sc->request->special)
2242 sc->request->special = sc;
2243
2244 ret = tag;
2245
2246end:
2247 return ret;
2248}
2249
2250/**
2251 * fnic_scsi_host_end_tag
2252 * frees tag allocated by fnic_scsi_host_start_tag.
2253 **/
2254static inline void
2255fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2256{
2257 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2258 int tag = sc->request->tag;
2259
2260 if (tag == SCSI_NO_TAG)
2261 return;
2262
2263 BUG_ON(!bqt || !bqt->tag_index[tag]);
2264 if (!bqt)
2265 return;
2266
2267 bqt->tag_index[tag] = NULL;
2268 clear_bit(tag, bqt->tag_map);
2269
2270 return;
2271}
2272
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002273/*
2274 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2275 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2276 * on the LUN.
2277 */
2278int fnic_device_reset(struct scsi_cmnd *sc)
2279{
2280 struct fc_lport *lp;
2281 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002282 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002283 struct fc_rport *rport;
2284 int status;
2285 int ret = FAILED;
2286 spinlock_t *io_lock;
2287 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08002288 unsigned long start_time = 0;
Hiral Patel03298552013-02-12 17:00:58 -08002289 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07002290 struct fnic_stats *fnic_stats;
2291 struct reset_stats *reset_stats;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002292 int tag = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002293 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patel03298552013-02-12 17:00:58 -08002294 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
Satish Kharata36f5dd2016-03-18 11:22:49 -07002295 bool new_sc = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002296
2297 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01002298 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002299
2300 /* Get local-port, check ready and link up */
2301 lp = shost_priv(sc->device->host);
2302
2303 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002304 fnic_stats = &fnic->fnic_stats;
2305 reset_stats = &fnic->fnic_stats.reset_stats;
2306
2307 atomic64_inc(&reset_stats->device_resets);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002308
Roel Kluin0db6f432010-06-11 16:44:46 -07002309 rport = starget_to_rport(scsi_target(sc->device));
2310 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002311 "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
Hiral Patel03298552013-02-12 17:00:58 -08002312 rport->port_id, sc->device->lun, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002313
2314 if (lp->state != LPORT_ST_READY || !(lp->link_up))
2315 goto fnic_device_reset_end;
2316
2317 /* Check if remote port up */
Hiral Patel67125b02013-09-12 17:45:42 -07002318 if (fc_remote_port_chkready(rport)) {
2319 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002320 goto fnic_device_reset_end;
Hiral Patel67125b02013-09-12 17:45:42 -07002321 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002322
Hiral Patel14eb5d92013-02-12 17:01:01 -08002323 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
Hiral Patel03298552013-02-12 17:00:58 -08002324 /* Allocate tag if not present */
2325
2326 tag = sc->request->tag;
2327 if (unlikely(tag < 0)) {
Christoph Hellwig4d637162014-06-12 14:53:23 +02002328 /*
2329 * XXX(hch): current the midlayer fakes up a struct
2330 * request for the explicit reset ioctls, and those
2331 * don't have a tag allocated to them. The below
2332 * code pokes into midlayer structures to paper over
2333 * this design issue, but that won't work for blk-mq.
2334 *
2335 * Either someone who can actually test the hardware
2336 * will have to come up with a similar hack for the
2337 * blk-mq case, or we'll have to bite the bullet and
2338 * fix the way the EH ioctls work for real, but until
2339 * that happens we fail these explicit requests here.
2340 */
Christoph Hellwig4d637162014-06-12 14:53:23 +02002341
Hiral Patel03298552013-02-12 17:00:58 -08002342 tag = fnic_scsi_host_start_tag(fnic, sc);
2343 if (unlikely(tag == SCSI_NO_TAG))
2344 goto fnic_device_reset_end;
2345 tag_gen_flag = 1;
Satish Kharata36f5dd2016-03-18 11:22:49 -07002346 new_sc = 1;
Hiral Patel03298552013-02-12 17:00:58 -08002347 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002348 io_lock = fnic_io_lock_hash(fnic, sc);
2349 spin_lock_irqsave(io_lock, flags);
2350 io_req = (struct fnic_io_req *)CMD_SP(sc);
2351
2352 /*
2353 * If there is a io_req attached to this command, then use it,
2354 * else allocate a new one.
2355 */
2356 if (!io_req) {
2357 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2358 if (!io_req) {
2359 spin_unlock_irqrestore(io_lock, flags);
2360 goto fnic_device_reset_end;
2361 }
2362 memset(io_req, 0, sizeof(*io_req));
2363 io_req->port_id = rport->port_id;
2364 CMD_SP(sc) = (char *)io_req;
2365 }
2366 io_req->dr_done = &tm_done;
2367 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2368 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2369 spin_unlock_irqrestore(io_lock, flags);
2370
Hiral Patel03298552013-02-12 17:00:58 -08002371 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002372
2373 /*
2374 * issue the device reset, if enqueue failed, clean up the ioreq
2375 * and break assoc with scsi cmd
2376 */
2377 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2378 spin_lock_irqsave(io_lock, flags);
2379 io_req = (struct fnic_io_req *)CMD_SP(sc);
2380 if (io_req)
2381 io_req->dr_done = NULL;
2382 goto fnic_device_reset_clean;
2383 }
Hiral Patel03298552013-02-12 17:00:58 -08002384 spin_lock_irqsave(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002385 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08002386 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002387
2388 /*
2389 * Wait on the local completion for LUN reset. The io_req may be
2390 * freed while we wait since we hold no lock.
2391 */
2392 wait_for_completion_timeout(&tm_done,
2393 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2394
2395 spin_lock_irqsave(io_lock, flags);
2396 io_req = (struct fnic_io_req *)CMD_SP(sc);
2397 if (!io_req) {
2398 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002399 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2400 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002401 goto fnic_device_reset_end;
2402 }
2403 io_req->dr_done = NULL;
2404
2405 status = CMD_LR_STATUS(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002406
2407 /*
2408 * If lun reset not completed, bail out with failed. io_req
2409 * gets cleaned up during higher levels of EH
2410 */
2411 if (status == FCPIO_INVALID_CODE) {
Hiral Patel67125b02013-09-12 17:45:42 -07002412 atomic64_inc(&reset_stats->device_reset_timeouts);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002413 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2414 "Device reset timed out\n");
Hiral Patel03298552013-02-12 17:00:58 -08002415 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2416 spin_unlock_irqrestore(io_lock, flags);
2417 int_to_scsilun(sc->device->lun, &fc_lun);
2418 /*
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002419 * Issue abort and terminate on device reset request.
2420 * If q'ing of terminate fails, retry it after a delay.
Hiral Patel03298552013-02-12 17:00:58 -08002421 */
2422 while (1) {
2423 spin_lock_irqsave(io_lock, flags);
2424 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2425 spin_unlock_irqrestore(io_lock, flags);
2426 break;
2427 }
2428 spin_unlock_irqrestore(io_lock, flags);
2429 if (fnic_queue_abort_io_req(fnic,
2430 tag | FNIC_TAG_DEV_RST,
2431 FCPIO_ITMF_ABT_TASK_TERM,
2432 fc_lun.scsi_lun, io_req)) {
2433 wait_for_completion_timeout(&tm_done,
2434 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2435 } else {
2436 spin_lock_irqsave(io_lock, flags);
2437 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2438 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2439 io_req->abts_done = &tm_done;
2440 spin_unlock_irqrestore(io_lock, flags);
2441 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2442 "Abort and terminate issued on Device reset "
2443 "tag 0x%x sc 0x%p\n", tag, sc);
2444 break;
2445 }
2446 }
2447 while (1) {
2448 spin_lock_irqsave(io_lock, flags);
2449 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2450 spin_unlock_irqrestore(io_lock, flags);
2451 wait_for_completion_timeout(&tm_done,
2452 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2453 break;
2454 } else {
2455 io_req = (struct fnic_io_req *)CMD_SP(sc);
2456 io_req->abts_done = NULL;
2457 goto fnic_device_reset_clean;
2458 }
2459 }
2460 } else {
2461 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002462 }
2463
2464 /* Completed, but not successful, clean up the io_req, return fail */
2465 if (status != FCPIO_SUCCESS) {
2466 spin_lock_irqsave(io_lock, flags);
2467 FNIC_SCSI_DBG(KERN_DEBUG,
2468 fnic->lport->host,
2469 "Device reset completed - failed\n");
2470 io_req = (struct fnic_io_req *)CMD_SP(sc);
2471 goto fnic_device_reset_clean;
2472 }
2473
2474 /*
2475 * Clean up any aborts on this lun that have still not
2476 * completed. If any of these fail, then LUN reset fails.
2477 * clean_pending_aborts cleans all cmds on this lun except
2478 * the lun reset cmd. If all cmds get cleaned, the lun reset
2479 * succeeds
2480 */
Satish Kharata36f5dd2016-03-18 11:22:49 -07002481 if (fnic_clean_pending_aborts(fnic, sc, new_sc)) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002482 spin_lock_irqsave(io_lock, flags);
2483 io_req = (struct fnic_io_req *)CMD_SP(sc);
2484 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2485 "Device reset failed"
2486 " since could not abort all IOs\n");
2487 goto fnic_device_reset_clean;
2488 }
2489
2490 /* Clean lun reset command */
2491 spin_lock_irqsave(io_lock, flags);
2492 io_req = (struct fnic_io_req *)CMD_SP(sc);
2493 if (io_req)
2494 /* Completed, and successful */
2495 ret = SUCCESS;
2496
2497fnic_device_reset_clean:
2498 if (io_req)
2499 CMD_SP(sc) = NULL;
2500
2501 spin_unlock_irqrestore(io_lock, flags);
2502
2503 if (io_req) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002504 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002505 fnic_release_ioreq_buf(fnic, io_req, sc);
2506 mempool_free(io_req, fnic->io_req_pool);
2507 }
2508
2509fnic_device_reset_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08002510 FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2511 sc->request->tag, sc,
2512 jiffies_to_msecs(jiffies - start_time),
2513 0, ((u64)sc->cmnd[0] << 32 |
2514 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2515 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2516 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2517
Hiral Patel03298552013-02-12 17:00:58 -08002518 /* free tag if it is allocated */
2519 if (unlikely(tag_gen_flag))
2520 fnic_scsi_host_end_tag(fnic, sc);
2521
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002522 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2523 "Returning from device reset %s\n",
2524 (ret == SUCCESS) ?
2525 "SUCCESS" : "FAILED");
Hiral Patel67125b02013-09-12 17:45:42 -07002526
2527 if (ret == FAILED)
2528 atomic64_inc(&reset_stats->device_reset_failures);
2529
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002530 return ret;
2531}
2532
2533/* Clean up all IOs, clean up libFC local port */
2534int fnic_reset(struct Scsi_Host *shost)
2535{
2536 struct fc_lport *lp;
2537 struct fnic *fnic;
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002538 int ret = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07002539 struct reset_stats *reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002540
2541 lp = shost_priv(shost);
2542 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002543 reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002544
2545 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2546 "fnic_reset called\n");
2547
Hiral Patel67125b02013-09-12 17:45:42 -07002548 atomic64_inc(&reset_stats->fnic_resets);
2549
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002550 /*
2551 * Reset local port, this will clean up libFC exchanges,
2552 * reset remote port sessions, and if link is up, begin flogi
2553 */
Hannes Reinecke31c0a632016-10-18 10:01:36 +02002554 ret = fc_lport_reset(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002555
2556 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2557 "Returning from fnic reset %s\n",
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002558 (ret == 0) ?
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002559 "SUCCESS" : "FAILED");
2560
Hiral Patel67125b02013-09-12 17:45:42 -07002561 if (ret == 0)
2562 atomic64_inc(&reset_stats->fnic_reset_completions);
2563 else
2564 atomic64_inc(&reset_stats->fnic_reset_failures);
2565
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002566 return ret;
2567}
2568
2569/*
2570 * SCSI Error handling calls driver's eh_host_reset if all prior
2571 * error handling levels return FAILED. If host reset completes
2572 * successfully, and if link is up, then Fabric login begins.
2573 *
2574 * Host Reset is the highest level of error recovery. If this fails, then
2575 * host is offlined by SCSI.
2576 *
2577 */
2578int fnic_host_reset(struct scsi_cmnd *sc)
2579{
2580 int ret;
2581 unsigned long wait_host_tmo;
2582 struct Scsi_Host *shost = sc->device->host;
2583 struct fc_lport *lp = shost_priv(shost);
Satish Kharat9698b6f2016-12-14 13:20:41 -08002584 struct fnic *fnic = lport_priv(lp);
2585 unsigned long flags;
2586
2587 spin_lock_irqsave(&fnic->fnic_lock, flags);
2588 if (fnic->internal_reset_inprogress == 0) {
2589 fnic->internal_reset_inprogress = 1;
2590 } else {
2591 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2592 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2593 "host reset in progress skipping another host reset\n");
2594 return SUCCESS;
2595 }
2596 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002597
2598 /*
2599 * If fnic_reset is successful, wait for fabric login to complete
2600 * scsi-ml tries to send a TUR to every device if host reset is
2601 * successful, so before returning to scsi, fabric should be up
2602 */
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002603 ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002604 if (ret == SUCCESS) {
2605 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2606 ret = FAILED;
2607 while (time_before(jiffies, wait_host_tmo)) {
2608 if ((lp->state == LPORT_ST_READY) &&
2609 (lp->link_up)) {
2610 ret = SUCCESS;
2611 break;
2612 }
2613 ssleep(1);
2614 }
2615 }
2616
Satish Kharat9698b6f2016-12-14 13:20:41 -08002617 spin_lock_irqsave(&fnic->fnic_lock, flags);
2618 fnic->internal_reset_inprogress = 0;
2619 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002620 return ret;
2621}
2622
2623/*
2624 * This fxn is called from libFC when host is removed
2625 */
2626void fnic_scsi_abort_io(struct fc_lport *lp)
2627{
2628 int err = 0;
2629 unsigned long flags;
2630 enum fnic_state old_state;
2631 struct fnic *fnic = lport_priv(lp);
2632 DECLARE_COMPLETION_ONSTACK(remove_wait);
2633
2634 /* Issue firmware reset for fnic, wait for reset to complete */
Hiral Patel03298552013-02-12 17:00:58 -08002635retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002636 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002637 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2638 /* fw reset is in progress, poll for its completion */
2639 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2640 schedule_timeout(msecs_to_jiffies(100));
2641 goto retry_fw_reset;
2642 }
2643
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002644 fnic->remove_wait = &remove_wait;
2645 old_state = fnic->state;
2646 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002647 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002648 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2649
2650 err = fnic_fw_reset_handler(fnic);
2651 if (err) {
2652 spin_lock_irqsave(&fnic->fnic_lock, flags);
2653 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2654 fnic->state = old_state;
2655 fnic->remove_wait = NULL;
2656 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2657 return;
2658 }
2659
2660 /* Wait for firmware reset to complete */
2661 wait_for_completion_timeout(&remove_wait,
2662 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2663
2664 spin_lock_irqsave(&fnic->fnic_lock, flags);
2665 fnic->remove_wait = NULL;
2666 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2667 "fnic_scsi_abort_io %s\n",
2668 (fnic->state == FNIC_IN_ETH_MODE) ?
2669 "SUCCESS" : "FAILED");
2670 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2671
2672}
2673
2674/*
2675 * This fxn called from libFC to clean up driver IO state on link down
2676 */
2677void fnic_scsi_cleanup(struct fc_lport *lp)
2678{
2679 unsigned long flags;
2680 enum fnic_state old_state;
2681 struct fnic *fnic = lport_priv(lp);
2682
2683 /* issue fw reset */
Hiral Patel03298552013-02-12 17:00:58 -08002684retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002685 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002686 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2687 /* fw reset is in progress, poll for its completion */
2688 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2689 schedule_timeout(msecs_to_jiffies(100));
2690 goto retry_fw_reset;
2691 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002692 old_state = fnic->state;
2693 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002694 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002695 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2696
2697 if (fnic_fw_reset_handler(fnic)) {
2698 spin_lock_irqsave(&fnic->fnic_lock, flags);
2699 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2700 fnic->state = old_state;
2701 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2702 }
2703
2704}
2705
2706void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2707{
2708}
2709
2710void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2711{
2712 struct fnic *fnic = lport_priv(lp);
2713
2714 /* Non-zero sid, nothing to do */
2715 if (sid)
2716 goto call_fc_exch_mgr_reset;
2717
2718 if (did) {
2719 fnic_rport_exch_reset(fnic, did);
2720 goto call_fc_exch_mgr_reset;
2721 }
2722
2723 /*
2724 * sid = 0, did = 0
2725 * link down or device being removed
2726 */
2727 if (!fnic->in_remove)
2728 fnic_scsi_cleanup(lp);
2729 else
2730 fnic_scsi_abort_io(lp);
2731
2732 /* call libFC exch mgr reset to reset its exchanges */
2733call_fc_exch_mgr_reset:
2734 fc_exch_mgr_reset(lp, sid, did);
2735
2736}
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002737
2738/*
2739 * fnic_is_abts_pending() is a helper function that
2740 * walks through tag map to check if there is any IOs pending,if there is one,
2741 * then it returns 1 (true), otherwise 0 (false)
2742 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2743 * otherwise, it checks for all IOs.
2744 */
2745int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2746{
2747 int tag;
2748 struct fnic_io_req *io_req;
2749 spinlock_t *io_lock;
2750 unsigned long flags;
2751 int ret = 0;
2752 struct scsi_cmnd *sc;
2753 struct scsi_device *lun_dev = NULL;
2754
2755 if (lr_sc)
2756 lun_dev = lr_sc->device;
2757
2758 /* walk again to check, if IOs are still pending in fw */
Hiral Patelfc857992013-09-09 13:31:51 -07002759 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002760 sc = scsi_host_find_tag(fnic->lport->host, tag);
2761 /*
2762 * ignore this lun reset cmd or cmds that do not belong to
2763 * this lun
2764 */
2765 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2766 continue;
2767
2768 io_lock = fnic_io_lock_hash(fnic, sc);
2769 spin_lock_irqsave(io_lock, flags);
2770
2771 io_req = (struct fnic_io_req *)CMD_SP(sc);
2772
2773 if (!io_req || sc->device != lun_dev) {
2774 spin_unlock_irqrestore(io_lock, flags);
2775 continue;
2776 }
2777
2778 /*
2779 * Found IO that is still pending with firmware and
2780 * belongs to the LUN that we are resetting
2781 */
2782 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2783 "Found IO in %s on lun\n",
2784 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2785
Dan Carpenter5d65f912013-05-30 10:50:46 +03002786 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002787 ret = 1;
Dan Carpenter5d65f912013-05-30 10:50:46 +03002788 spin_unlock_irqrestore(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002789 }
2790
2791 return ret;
2792}