blob: 5e694a591c9d2056fbd20fa3e42e63b96e7fc896 [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;
Satish Kharatccc6d702017-02-28 16:14:33 -08001131 CMD_ABTS_STATUS(sc) = hdr_status;
Hiral Patel67125b02013-09-12 17:45:42 -07001132
Satish Kharat691a8372016-03-18 11:22:48 -07001133 /* If the status is IO not found consider it as success */
1134 if (hdr_status == FCPIO_IO_NOT_FOUND)
1135 CMD_ABTS_STATUS(sc) = FCPIO_SUCCESS;
Satish Kharat691a8372016-03-18 11:22:48 -07001136
Hiral Patel67125b02013-09-12 17:45:42 -07001137 atomic64_dec(&fnic_stats->io_stats.active_ios);
1138 if (atomic64_read(&fnic->io_cmpl_skip))
1139 atomic64_dec(&fnic->io_cmpl_skip);
1140 else
1141 atomic64_inc(&fnic_stats->io_stats.io_completions);
1142
1143 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1144 atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1145
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001146 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1147 "abts cmpl recd. id %d status %s\n",
1148 (int)(id & FNIC_TAG_MASK),
1149 fnic_fcpio_status_to_str(hdr_status));
1150
1151 /*
1152 * If scsi_eh thread is blocked waiting for abts to complete,
1153 * signal completion to it. IO will be cleaned in the thread
1154 * else clean it in this context
1155 */
1156 if (io_req->abts_done) {
1157 complete(io_req->abts_done);
1158 spin_unlock_irqrestore(io_lock, flags);
1159 } else {
1160 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1161 "abts cmpl, completing IO\n");
1162 CMD_SP(sc) = NULL;
1163 sc->result = (DID_ERROR << 16);
1164
1165 spin_unlock_irqrestore(io_lock, flags);
1166
1167 fnic_release_ioreq_buf(fnic, io_req, sc);
1168 mempool_free(io_req, fnic->io_req_pool);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001169 if (sc->scsi_done) {
1170 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1171 sc->device->host->host_no, id,
1172 sc,
1173 jiffies_to_msecs(jiffies - start_time),
1174 desc,
1175 (((u64)hdr_status << 40) |
1176 (u64)sc->cmnd[0] << 32 |
1177 (u64)sc->cmnd[2] << 24 |
1178 (u64)sc->cmnd[3] << 16 |
1179 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1180 (((u64)CMD_FLAGS(sc) << 32) |
1181 CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001182 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001183 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001184 }
1185
1186 } else if (id & FNIC_TAG_DEV_RST) {
1187 /* Completion of device reset */
1188 CMD_LR_STATUS(sc) = hdr_status;
Hiral Patel03298552013-02-12 17:00:58 -08001189 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1190 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001191 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001192 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1193 sc->device->host->host_no, id, sc,
1194 jiffies_to_msecs(jiffies - start_time),
1195 desc, 0,
1196 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001197 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1198 "Terminate pending "
1199 "dev reset cmpl recd. id %d status %s\n",
1200 (int)(id & FNIC_TAG_MASK),
1201 fnic_fcpio_status_to_str(hdr_status));
1202 return;
1203 }
1204 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1205 /* Need to wait for terminate completion */
1206 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001207 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1208 sc->device->host->host_no, id, sc,
1209 jiffies_to_msecs(jiffies - start_time),
1210 desc, 0,
1211 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001212 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1213 "dev reset cmpl recd after time out. "
1214 "id %d status %s\n",
1215 (int)(id & FNIC_TAG_MASK),
1216 fnic_fcpio_status_to_str(hdr_status));
1217 return;
1218 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001219 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
Hiral Patel03298552013-02-12 17:00:58 -08001220 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001221 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1222 "dev reset cmpl recd. id %d status %s\n",
1223 (int)(id & FNIC_TAG_MASK),
1224 fnic_fcpio_status_to_str(hdr_status));
1225 if (io_req->dr_done)
1226 complete(io_req->dr_done);
1227 spin_unlock_irqrestore(io_lock, flags);
1228
1229 } else {
1230 shost_printk(KERN_ERR, fnic->lport->host,
1231 "Unexpected itmf io state %s tag %x\n",
1232 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1233 spin_unlock_irqrestore(io_lock, flags);
1234 }
1235
1236}
1237
1238/*
1239 * fnic_fcpio_cmpl_handler
1240 * Routine to service the cq for wq_copy
1241 */
1242static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1243 unsigned int cq_index,
1244 struct fcpio_fw_req *desc)
1245{
1246 struct fnic *fnic = vnic_dev_priv(vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001247
1248 switch (desc->hdr.type) {
Hiral Patel67125b02013-09-12 17:45:42 -07001249 case FCPIO_ICMND_CMPL: /* fw completed a command */
1250 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1251 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1252 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1253 case FCPIO_RESET_CMPL: /* fw completed reset */
1254 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1255 break;
1256 default:
1257 break;
1258 }
1259
1260 switch (desc->hdr.type) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001261 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1262 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1263 break;
1264
1265 case FCPIO_ICMND_CMPL: /* fw completed a command */
1266 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1267 break;
1268
1269 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1270 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1271 break;
1272
1273 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
Joe Eykholt78112e52009-11-03 11:49:22 -08001274 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
Hiral Patel03298552013-02-12 17:00:58 -08001275 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001276 break;
1277
1278 case FCPIO_RESET_CMPL: /* fw completed reset */
Hiral Patel03298552013-02-12 17:00:58 -08001279 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001280 break;
1281
1282 default:
1283 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1284 "firmware completion type %d\n",
1285 desc->hdr.type);
1286 break;
1287 }
1288
Hiral Patel03298552013-02-12 17:00:58 -08001289 return 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001290}
1291
1292/*
1293 * fnic_wq_copy_cmpl_handler
1294 * Routine to process wq copy
1295 */
1296int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1297{
1298 unsigned int wq_work_done = 0;
1299 unsigned int i, cq_index;
1300 unsigned int cur_work_done;
1301
1302 for (i = 0; i < fnic->wq_copy_count; i++) {
1303 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1304 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1305 fnic_fcpio_cmpl_handler,
1306 copy_work_to_do);
1307 wq_work_done += cur_work_done;
1308 }
1309 return wq_work_done;
1310}
1311
1312static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1313{
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001314 int i;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001315 struct fnic_io_req *io_req;
1316 unsigned long flags = 0;
1317 struct scsi_cmnd *sc;
1318 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001319 unsigned long start_time = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07001320 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001321
Hiral Patelfc857992013-09-09 13:31:51 -07001322 for (i = 0; i < fnic->fnic_max_tag_id; i++) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001323 if (i == exclude_id)
1324 continue;
1325
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001326 io_lock = fnic_io_lock_tag(fnic, i);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001327 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001328 sc = scsi_host_find_tag(fnic->lport->host, i);
1329 if (!sc) {
1330 spin_unlock_irqrestore(io_lock, flags);
1331 continue;
1332 }
1333
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001334 io_req = (struct fnic_io_req *)CMD_SP(sc);
Hiral Patel03298552013-02-12 17:00:58 -08001335 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1336 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1337 /*
1338 * We will be here only when FW completes reset
1339 * without sending completions for outstanding ios.
1340 */
1341 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1342 if (io_req && io_req->dr_done)
1343 complete(io_req->dr_done);
1344 else if (io_req && io_req->abts_done)
1345 complete(io_req->abts_done);
1346 spin_unlock_irqrestore(io_lock, flags);
1347 continue;
1348 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1349 spin_unlock_irqrestore(io_lock, flags);
1350 continue;
1351 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001352 if (!io_req) {
1353 spin_unlock_irqrestore(io_lock, flags);
1354 goto cleanup_scsi_cmd;
1355 }
1356
1357 CMD_SP(sc) = NULL;
1358
1359 spin_unlock_irqrestore(io_lock, flags);
1360
1361 /*
1362 * If there is a scsi_cmnd associated with this io_req, then
1363 * free the corresponding state
1364 */
Hiral Patel14eb5d92013-02-12 17:01:01 -08001365 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001366 fnic_release_ioreq_buf(fnic, io_req, sc);
1367 mempool_free(io_req, fnic->io_req_pool);
1368
1369cleanup_scsi_cmd:
1370 sc->result = DID_TRANSPORT_DISRUPTED << 16;
Hiral Shah66818662014-04-18 12:28:18 -07001371 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1372 "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1373 __func__, (jiffies - start_time));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001374
Hiral Patel67125b02013-09-12 17:45:42 -07001375 if (atomic64_read(&fnic->io_cmpl_skip))
1376 atomic64_dec(&fnic->io_cmpl_skip);
1377 else
1378 atomic64_inc(&fnic_stats->io_stats.io_completions);
1379
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001380 /* Complete the command to SCSI */
Hiral Patel4d7007b2013-02-12 17:01:02 -08001381 if (sc->scsi_done) {
1382 FNIC_TRACE(fnic_cleanup_io,
1383 sc->device->host->host_no, i, sc,
1384 jiffies_to_msecs(jiffies - start_time),
1385 0, ((u64)sc->cmnd[0] << 32 |
1386 (u64)sc->cmnd[2] << 24 |
1387 (u64)sc->cmnd[3] << 16 |
1388 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1389 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1390
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001391 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001392 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001393 }
1394}
1395
1396void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1397 struct fcpio_host_req *desc)
1398{
1399 u32 id;
1400 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1401 struct fnic_io_req *io_req;
1402 struct scsi_cmnd *sc;
1403 unsigned long flags;
1404 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001405 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001406
1407 /* get the tag reference */
1408 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1409 id &= FNIC_TAG_MASK;
1410
Hiral Patelfc857992013-09-09 13:31:51 -07001411 if (id >= fnic->fnic_max_tag_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001412 return;
1413
1414 sc = scsi_host_find_tag(fnic->lport->host, id);
1415 if (!sc)
1416 return;
1417
1418 io_lock = fnic_io_lock_hash(fnic, sc);
1419 spin_lock_irqsave(io_lock, flags);
1420
1421 /* Get the IO context which this desc refers to */
1422 io_req = (struct fnic_io_req *)CMD_SP(sc);
1423
1424 /* fnic interrupts are turned off by now */
1425
1426 if (!io_req) {
1427 spin_unlock_irqrestore(io_lock, flags);
1428 goto wq_copy_cleanup_scsi_cmd;
1429 }
1430
1431 CMD_SP(sc) = NULL;
1432
1433 spin_unlock_irqrestore(io_lock, flags);
1434
Hiral Patel14eb5d92013-02-12 17:01:01 -08001435 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001436 fnic_release_ioreq_buf(fnic, io_req, sc);
1437 mempool_free(io_req, fnic->io_req_pool);
1438
1439wq_copy_cleanup_scsi_cmd:
1440 sc->result = DID_NO_CONNECT << 16;
1441 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1442 " DID_NO_CONNECT\n");
1443
Hiral Patel4d7007b2013-02-12 17:01:02 -08001444 if (sc->scsi_done) {
1445 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1446 sc->device->host->host_no, id, sc,
1447 jiffies_to_msecs(jiffies - start_time),
1448 0, ((u64)sc->cmnd[0] << 32 |
1449 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1450 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1451 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1452
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001453 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001454 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001455}
1456
1457static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1458 u32 task_req, u8 *fc_lun,
1459 struct fnic_io_req *io_req)
1460{
1461 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001462 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001463 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001464 unsigned long flags;
1465
Hiral Patel03298552013-02-12 17:00:58 -08001466 spin_lock_irqsave(host->host_lock, flags);
1467 if (unlikely(fnic_chk_state_flags_locked(fnic,
1468 FNIC_FLAGS_IO_BLOCKED))) {
1469 spin_unlock_irqrestore(host->host_lock, flags);
1470 return 1;
1471 } else
1472 atomic_inc(&fnic->in_flight);
1473 spin_unlock_irqrestore(host->host_lock, flags);
1474
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001475 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1476
1477 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1478 free_wq_copy_descs(fnic, wq);
1479
1480 if (!vnic_wq_copy_desc_avail(wq)) {
1481 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001482 atomic_dec(&fnic->in_flight);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001483 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001484 "fnic_queue_abort_io_req: failure: no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07001485 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001486 return 1;
1487 }
1488 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1489 0, task_req, tag, fc_lun, io_req->port_id,
1490 fnic->config.ra_tov, fnic->config.ed_tov);
1491
Hiral Patel67125b02013-09-12 17:45:42 -07001492 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1493 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1494 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1495 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1496 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1497
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001498 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001499 atomic_dec(&fnic->in_flight);
1500
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001501 return 0;
1502}
1503
Hiral Patel03298552013-02-12 17:00:58 -08001504static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001505{
1506 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001507 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001508 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001509 struct fnic_io_req *io_req;
1510 spinlock_t *io_lock;
1511 unsigned long flags;
1512 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -07001513 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1514 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001515 struct scsi_lun fc_lun;
1516 enum fnic_ioreq_state old_ioreq_state;
1517
1518 FNIC_SCSI_DBG(KERN_DEBUG,
1519 fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001520 "fnic_rport_exch_reset called portid 0x%06x\n",
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001521 port_id);
1522
1523 if (fnic->in_remove)
1524 return;
1525
Hiral Patelfc857992013-09-09 13:31:51 -07001526 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001527 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001528 io_lock = fnic_io_lock_tag(fnic, tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001529 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001530 sc = scsi_host_find_tag(fnic->lport->host, tag);
1531 if (!sc) {
1532 spin_unlock_irqrestore(io_lock, flags);
1533 continue;
1534 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001535
1536 io_req = (struct fnic_io_req *)CMD_SP(sc);
1537
1538 if (!io_req || io_req->port_id != port_id) {
1539 spin_unlock_irqrestore(io_lock, flags);
1540 continue;
1541 }
1542
Hiral Patel03298552013-02-12 17:00:58 -08001543 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001544 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001545 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1546 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1547 sc);
1548 spin_unlock_irqrestore(io_lock, flags);
1549 continue;
1550 }
1551
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001552 /*
1553 * Found IO that is still pending with firmware and
1554 * belongs to rport that went away
1555 */
1556 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1557 spin_unlock_irqrestore(io_lock, flags);
1558 continue;
1559 }
Hiral Patel03298552013-02-12 17:00:58 -08001560 if (io_req->abts_done) {
1561 shost_printk(KERN_ERR, fnic->lport->host,
1562 "fnic_rport_exch_reset: io_req->abts_done is set "
1563 "state is %s\n",
1564 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1565 }
1566
Hiral Patel14eb5d92013-02-12 17:01:01 -08001567 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1568 shost_printk(KERN_ERR, fnic->lport->host,
1569 "rport_exch_reset "
1570 "IO not yet issued %p tag 0x%x flags "
1571 "%x state %d\n",
1572 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1573 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001574 old_ioreq_state = CMD_STATE(sc);
1575 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1576 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001577 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001578 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001579 abt_tag = (tag | FNIC_TAG_DEV_RST);
1580 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1581 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1582 sc);
1583 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001584
1585 BUG_ON(io_req->abts_done);
1586
1587 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1588 "fnic_rport_reset_exch: Issuing abts\n");
1589
1590 spin_unlock_irqrestore(io_lock, flags);
1591
1592 /* Now queue the abort command to firmware */
1593 int_to_scsilun(sc->device->lun, &fc_lun);
1594
Hiral Patel03298552013-02-12 17:00:58 -08001595 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001596 FCPIO_ITMF_ABT_TASK_TERM,
1597 fc_lun.scsi_lun, io_req)) {
1598 /*
1599 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001600 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001601 * aborted later by scsi_eh, or cleaned up during
1602 * lun reset
1603 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001604 spin_lock_irqsave(io_lock, flags);
1605 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1606 CMD_STATE(sc) = old_ioreq_state;
1607 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001608 } else {
1609 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001610 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1611 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001612 else
1613 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001614 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001615 atomic64_inc(&term_stats->terminates);
1616 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001617 }
1618 }
Hiral Patel67125b02013-09-12 17:45:42 -07001619 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1620 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001621
1622}
1623
1624void fnic_terminate_rport_io(struct fc_rport *rport)
1625{
1626 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001627 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001628 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001629 struct fnic_io_req *io_req;
1630 spinlock_t *io_lock;
1631 unsigned long flags;
1632 struct scsi_cmnd *sc;
1633 struct scsi_lun fc_lun;
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001634 struct fc_rport_libfc_priv *rdata;
1635 struct fc_lport *lport;
1636 struct fnic *fnic;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001637 struct fc_rport *cmd_rport;
Hiral Patel67125b02013-09-12 17:45:42 -07001638 struct reset_stats *reset_stats;
1639 struct terminate_stats *term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001640 enum fnic_ioreq_state old_ioreq_state;
1641
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001642 if (!rport) {
1643 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1644 return;
1645 }
1646 rdata = rport->dd_data;
1647
1648 if (!rdata) {
1649 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1650 return;
1651 }
1652 lport = rdata->local_port;
1653
1654 if (!lport) {
1655 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1656 return;
1657 }
1658 fnic = lport_priv(lport);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001659 FNIC_SCSI_DBG(KERN_DEBUG,
1660 fnic->lport->host, "fnic_terminate_rport_io called"
Hiral Patel03298552013-02-12 17:00:58 -08001661 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1662 rport->port_name, rport->node_name, rport,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001663 rport->port_id);
1664
1665 if (fnic->in_remove)
1666 return;
1667
Hiral Patel67125b02013-09-12 17:45:42 -07001668 reset_stats = &fnic->fnic_stats.reset_stats;
1669 term_stats = &fnic->fnic_stats.term_stats;
1670
Hiral Patelfc857992013-09-09 13:31:51 -07001671 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001672 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001673 io_lock = fnic_io_lock_tag(fnic, tag);
1674 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001675 sc = scsi_host_find_tag(fnic->lport->host, tag);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001676 if (!sc) {
1677 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001678 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001679 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001680
1681 cmd_rport = starget_to_rport(scsi_target(sc->device));
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001682 if (rport != cmd_rport) {
1683 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001684 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001685 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001686
1687 io_req = (struct fnic_io_req *)CMD_SP(sc);
1688
1689 if (!io_req || rport != cmd_rport) {
1690 spin_unlock_irqrestore(io_lock, flags);
1691 continue;
1692 }
1693
Hiral Patel03298552013-02-12 17:00:58 -08001694 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001695 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001696 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1697 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1698 sc);
1699 spin_unlock_irqrestore(io_lock, flags);
1700 continue;
1701 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001702 /*
1703 * Found IO that is still pending with firmware and
1704 * belongs to rport that went away
1705 */
1706 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1707 spin_unlock_irqrestore(io_lock, flags);
1708 continue;
1709 }
Hiral Patel03298552013-02-12 17:00:58 -08001710 if (io_req->abts_done) {
1711 shost_printk(KERN_ERR, fnic->lport->host,
1712 "fnic_terminate_rport_io: io_req->abts_done is set "
1713 "state is %s\n",
1714 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1715 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001716 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1717 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1718 "fnic_terminate_rport_io "
1719 "IO not yet issued %p tag 0x%x flags "
1720 "%x state %d\n",
1721 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1722 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001723 old_ioreq_state = CMD_STATE(sc);
1724 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1725 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001726 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001727 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001728 abt_tag = (tag | FNIC_TAG_DEV_RST);
1729 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1730 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1731 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001732
1733 BUG_ON(io_req->abts_done);
1734
1735 FNIC_SCSI_DBG(KERN_DEBUG,
1736 fnic->lport->host,
1737 "fnic_terminate_rport_io: Issuing abts\n");
1738
1739 spin_unlock_irqrestore(io_lock, flags);
1740
1741 /* Now queue the abort command to firmware */
1742 int_to_scsilun(sc->device->lun, &fc_lun);
1743
Hiral Patel03298552013-02-12 17:00:58 -08001744 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001745 FCPIO_ITMF_ABT_TASK_TERM,
1746 fc_lun.scsi_lun, io_req)) {
1747 /*
1748 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001749 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001750 * aborted later by scsi_eh, or cleaned up during
1751 * lun reset
1752 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001753 spin_lock_irqsave(io_lock, flags);
1754 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1755 CMD_STATE(sc) = old_ioreq_state;
1756 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001757 } else {
1758 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001759 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1760 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001761 else
1762 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001763 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001764 atomic64_inc(&term_stats->terminates);
1765 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001766 }
1767 }
Hiral Patel67125b02013-09-12 17:45:42 -07001768 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1769 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001770
1771}
1772
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001773/*
1774 * This function is exported to SCSI for sending abort cmnds.
1775 * A SCSI IO is represented by a io_req in the driver.
1776 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1777 */
1778int fnic_abort_cmd(struct scsi_cmnd *sc)
1779{
1780 struct fc_lport *lp;
1781 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001782 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001783 struct fc_rport *rport;
1784 spinlock_t *io_lock;
1785 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001786 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001787 int ret = SUCCESS;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001788 u32 task_req = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001789 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07001790 struct fnic_stats *fnic_stats;
1791 struct abort_stats *abts_stats;
1792 struct terminate_stats *term_stats;
Hiral Shah66818662014-04-18 12:28:18 -07001793 enum fnic_ioreq_state old_ioreq_state;
Hiral Patel03298552013-02-12 17:00:58 -08001794 int tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001795 DECLARE_COMPLETION_ONSTACK(tm_done);
1796
1797 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001798 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001799
1800 /* Get local-port, check ready and link up */
1801 lp = shost_priv(sc->device->host);
1802
1803 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07001804 fnic_stats = &fnic->fnic_stats;
1805 abts_stats = &fnic->fnic_stats.abts_stats;
1806 term_stats = &fnic->fnic_stats.term_stats;
1807
Roel Kluin0db6f432010-06-11 16:44:46 -07001808 rport = starget_to_rport(scsi_target(sc->device));
Hiral Patel03298552013-02-12 17:00:58 -08001809 tag = sc->request->tag;
1810 FNIC_SCSI_DBG(KERN_DEBUG,
1811 fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001812 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
Hiral Patel03298552013-02-12 17:00:58 -08001813 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1814
1815 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1816
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001817 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1818 ret = FAILED;
1819 goto fnic_abort_cmd_end;
1820 }
1821
1822 /*
1823 * Avoid a race between SCSI issuing the abort and the device
1824 * completing the command.
1825 *
1826 * If the command is already completed by the fw cmpl code,
1827 * we just return SUCCESS from here. This means that the abort
1828 * succeeded. In the SCSI ML, since the timeout for command has
1829 * happened, the completion wont actually complete the command
1830 * and it will be considered as an aborted command
1831 *
1832 * The CMD_SP will not be cleared except while holding io_req_lock.
1833 */
1834 io_lock = fnic_io_lock_hash(fnic, sc);
1835 spin_lock_irqsave(io_lock, flags);
1836 io_req = (struct fnic_io_req *)CMD_SP(sc);
1837 if (!io_req) {
1838 spin_unlock_irqrestore(io_lock, flags);
1839 goto fnic_abort_cmd_end;
1840 }
1841
1842 io_req->abts_done = &tm_done;
1843
1844 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1845 spin_unlock_irqrestore(io_lock, flags);
1846 goto wait_pending;
1847 }
1848 /*
1849 * Command is still pending, need to abort it
1850 * If the firmware completes the command after this point,
1851 * the completion wont be done till mid-layer, since abort
1852 * has already started.
1853 */
Hiral Shah66818662014-04-18 12:28:18 -07001854 old_ioreq_state = CMD_STATE(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001855 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1856 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1857
1858 spin_unlock_irqrestore(io_lock, flags);
1859
1860 /*
1861 * Check readiness of the remote port. If the path to remote
1862 * port is up, then send abts to the remote port to terminate
1863 * the IO. Else, just locally terminate the IO in the firmware
1864 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001865 if (fc_remote_port_chkready(rport) == 0)
1866 task_req = FCPIO_ITMF_ABT_TASK;
Hiral Patel67125b02013-09-12 17:45:42 -07001867 else {
1868 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001869 task_req = FCPIO_ITMF_ABT_TASK_TERM;
Hiral Patel67125b02013-09-12 17:45:42 -07001870 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001871
1872 /* Now queue the abort command to firmware */
1873 int_to_scsilun(sc->device->lun, &fc_lun);
1874
1875 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1876 fc_lun.scsi_lun, io_req)) {
1877 spin_lock_irqsave(io_lock, flags);
Hiral Shah66818662014-04-18 12:28:18 -07001878 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1879 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001880 io_req = (struct fnic_io_req *)CMD_SP(sc);
1881 if (io_req)
1882 io_req->abts_done = NULL;
1883 spin_unlock_irqrestore(io_lock, flags);
1884 ret = FAILED;
1885 goto fnic_abort_cmd_end;
1886 }
Hiral Patel67125b02013-09-12 17:45:42 -07001887 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001888 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001889 atomic64_inc(&fnic_stats->abts_stats.aborts);
1890 } else {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001891 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001892 atomic64_inc(&fnic_stats->term_stats.terminates);
1893 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001894
1895 /*
1896 * We queued an abort IO, wait for its completion.
1897 * Once the firmware completes the abort command, it will
1898 * wake up this thread.
1899 */
1900 wait_pending:
1901 wait_for_completion_timeout(&tm_done,
1902 msecs_to_jiffies
1903 (2 * fnic->config.ra_tov +
1904 fnic->config.ed_tov));
1905
1906 /* Check the abort status */
1907 spin_lock_irqsave(io_lock, flags);
1908
1909 io_req = (struct fnic_io_req *)CMD_SP(sc);
1910 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001911 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001912 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001913 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001914 ret = FAILED;
1915 goto fnic_abort_cmd_end;
1916 }
1917 io_req->abts_done = NULL;
1918
1919 /* fw did not complete abort, timed out */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001920 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001921 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001922 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel67125b02013-09-12 17:45:42 -07001923 atomic64_inc(&abts_stats->abort_drv_timeouts);
1924 } else {
Hiral Patel67125b02013-09-12 17:45:42 -07001925 atomic64_inc(&term_stats->terminate_drv_timeouts);
1926 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001927 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001928 ret = FAILED;
1929 goto fnic_abort_cmd_end;
1930 }
1931
Anil Chintalapati (achintal)efc7a282014-12-23 19:40:00 +00001932 /* IO out of order */
1933
1934 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1935 spin_unlock_irqrestore(io_lock, flags);
1936 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1937 "Issuing Host reset due to out of order IO\n");
1938
1939 if (fnic_host_reset(sc) == FAILED) {
1940 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1941 "fnic_host_reset failed.\n");
1942 }
1943 ret = FAILED;
1944 goto fnic_abort_cmd_end;
1945 }
1946
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001947 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1948
Satish Kharat691a8372016-03-18 11:22:48 -07001949 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001950 /*
1951 * firmware completed the abort, check the status,
Satish Kharat691a8372016-03-18 11:22:48 -07001952 * free the io_req if successful. If abort fails,
1953 * Device reset will clean the I/O.
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001954 */
Satish Kharat691a8372016-03-18 11:22:48 -07001955 if (CMD_ABTS_STATUS(sc) == FCPIO_SUCCESS)
1956 CMD_SP(sc) = NULL;
1957 else {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001958 ret = FAILED;
Satish Kharat691a8372016-03-18 11:22:48 -07001959 spin_unlock_irqrestore(io_lock, flags);
1960 goto fnic_abort_cmd_end;
1961 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001962
1963 spin_unlock_irqrestore(io_lock, flags);
1964
1965 fnic_release_ioreq_buf(fnic, io_req, sc);
1966 mempool_free(io_req, fnic->io_req_pool);
1967
Satish Kharat691a8372016-03-18 11:22:48 -07001968 if (sc->scsi_done) {
1969 /* Call SCSI completion function to complete the IO */
1970 sc->result = (DID_ABORT << 16);
1971 sc->scsi_done(sc);
1972 }
1973
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001974fnic_abort_cmd_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08001975 FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1976 sc->request->tag, sc,
1977 jiffies_to_msecs(jiffies - start_time),
1978 0, ((u64)sc->cmnd[0] << 32 |
1979 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1980 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1981 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1982
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001983 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel14eb5d92013-02-12 17:01:01 -08001984 "Returning from abort cmd type %x %s\n", task_req,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001985 (ret == SUCCESS) ?
1986 "SUCCESS" : "FAILED");
1987 return ret;
1988}
1989
1990static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1991 struct scsi_cmnd *sc,
1992 struct fnic_io_req *io_req)
1993{
1994 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001995 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001996 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001997 struct scsi_lun fc_lun;
1998 int ret = 0;
1999 unsigned long intr_flags;
2000
Hiral Patel03298552013-02-12 17:00:58 -08002001 spin_lock_irqsave(host->host_lock, intr_flags);
2002 if (unlikely(fnic_chk_state_flags_locked(fnic,
2003 FNIC_FLAGS_IO_BLOCKED))) {
2004 spin_unlock_irqrestore(host->host_lock, intr_flags);
2005 return FAILED;
2006 } else
2007 atomic_inc(&fnic->in_flight);
2008 spin_unlock_irqrestore(host->host_lock, intr_flags);
2009
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002010 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
2011
2012 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
2013 free_wq_copy_descs(fnic, wq);
2014
2015 if (!vnic_wq_copy_desc_avail(wq)) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002016 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2017 "queue_dr_io_req failure - no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07002018 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002019 ret = -EAGAIN;
2020 goto lr_io_req_end;
2021 }
2022
2023 /* fill in the lun info */
2024 int_to_scsilun(sc->device->lun, &fc_lun);
2025
2026 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
2027 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
2028 fc_lun.scsi_lun, io_req->port_id,
2029 fnic->config.ra_tov, fnic->config.ed_tov);
2030
Hiral Patel67125b02013-09-12 17:45:42 -07002031 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
2032 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
2033 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
2034 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
2035 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
2036
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002037lr_io_req_end:
2038 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel03298552013-02-12 17:00:58 -08002039 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002040
2041 return ret;
2042}
2043
2044/*
2045 * Clean up any pending aborts on the lun
2046 * For each outstanding IO on this lun, whose abort is not completed by fw,
2047 * issue a local abort. Wait for abort to complete. Return 0 if all commands
2048 * successfully aborted, 1 otherwise
2049 */
2050static int fnic_clean_pending_aborts(struct fnic *fnic,
Satish Kharata36f5dd2016-03-18 11:22:49 -07002051 struct scsi_cmnd *lr_sc,
2052 bool new_sc)
2053
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002054{
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002055 int tag, abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002056 struct fnic_io_req *io_req;
2057 spinlock_t *io_lock;
2058 unsigned long flags;
2059 int ret = 0;
2060 struct scsi_cmnd *sc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002061 struct scsi_lun fc_lun;
2062 struct scsi_device *lun_dev = lr_sc->device;
2063 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002064 enum fnic_ioreq_state old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002065
Hiral Patelfc857992013-09-09 13:31:51 -07002066 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002067 io_lock = fnic_io_lock_tag(fnic, tag);
2068 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002069 sc = scsi_host_find_tag(fnic->lport->host, tag);
2070 /*
Satish Kharata36f5dd2016-03-18 11:22:49 -07002071 * ignore this lun reset cmd if issued using new SC
2072 * or cmds that do not belong to this lun
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002073 */
Satish Kharata36f5dd2016-03-18 11:22:49 -07002074 if (!sc || ((sc == lr_sc) && new_sc) || sc->device != lun_dev) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002075 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002076 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002077 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002078
2079 io_req = (struct fnic_io_req *)CMD_SP(sc);
2080
2081 if (!io_req || sc->device != lun_dev) {
2082 spin_unlock_irqrestore(io_lock, flags);
2083 continue;
2084 }
2085
2086 /*
2087 * Found IO that is still pending with firmware and
2088 * belongs to the LUN that we are resetting
2089 */
2090 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2091 "Found IO in %s on lun\n",
2092 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2093
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002094 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2095 spin_unlock_irqrestore(io_lock, flags);
2096 continue;
2097 }
2098 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08002099 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002100 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2101 "%s dev rst not pending sc 0x%p\n", __func__,
2102 sc);
2103 spin_unlock_irqrestore(io_lock, flags);
2104 continue;
2105 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002106
2107 if (io_req->abts_done)
2108 shost_printk(KERN_ERR, fnic->lport->host,
2109 "%s: io_req->abts_done is set state is %s\n",
2110 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002111 old_ioreq_state = CMD_STATE(sc);
2112 /*
2113 * Any pending IO issued prior to reset is expected to be
2114 * in abts pending state, if not we need to set
2115 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2116 * When IO is completed, the IO will be handed over and
2117 * handled in this function.
2118 */
2119 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2120
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002121 BUG_ON(io_req->abts_done);
2122
2123 abt_tag = tag;
2124 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2125 abt_tag |= FNIC_TAG_DEV_RST;
2126 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2127 "%s: dev rst sc 0x%p\n", __func__, sc);
2128 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002129
2130 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2131 io_req->abts_done = &tm_done;
2132 spin_unlock_irqrestore(io_lock, flags);
2133
2134 /* Now queue the abort command to firmware */
2135 int_to_scsilun(sc->device->lun, &fc_lun);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002136
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002137 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002138 FCPIO_ITMF_ABT_TASK_TERM,
2139 fc_lun.scsi_lun, io_req)) {
2140 spin_lock_irqsave(io_lock, flags);
2141 io_req = (struct fnic_io_req *)CMD_SP(sc);
2142 if (io_req)
2143 io_req->abts_done = NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002144 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2145 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002146 spin_unlock_irqrestore(io_lock, flags);
2147 ret = 1;
2148 goto clean_pending_aborts_end;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002149 } else {
2150 spin_lock_irqsave(io_lock, flags);
2151 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2152 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2153 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002154 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08002155 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002156
2157 wait_for_completion_timeout(&tm_done,
2158 msecs_to_jiffies
2159 (fnic->config.ed_tov));
2160
2161 /* Recheck cmd state to check if it is now aborted */
2162 spin_lock_irqsave(io_lock, flags);
2163 io_req = (struct fnic_io_req *)CMD_SP(sc);
2164 if (!io_req) {
2165 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002166 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002167 continue;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002168 }
2169
2170 io_req->abts_done = NULL;
2171
2172 /* if abort is still pending with fw, fail */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002173 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002174 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002175 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002176 ret = 1;
2177 goto clean_pending_aborts_end;
2178 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002179 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
Satish Kharata36f5dd2016-03-18 11:22:49 -07002180
2181 /* original sc used for lr is handled by dev reset code */
2182 if (sc != lr_sc)
2183 CMD_SP(sc) = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002184 spin_unlock_irqrestore(io_lock, flags);
2185
Satish Kharata36f5dd2016-03-18 11:22:49 -07002186 /* original sc used for lr is handled by dev reset code */
2187 if (sc != lr_sc) {
2188 fnic_release_ioreq_buf(fnic, io_req, sc);
2189 mempool_free(io_req, fnic->io_req_pool);
2190 }
2191
2192 /*
2193 * Any IO is returned during reset, it needs to call scsi_done
2194 * to return the scsi_cmnd to upper layer.
2195 */
2196 if (sc->scsi_done) {
2197 /* Set result to let upper SCSI layer retry */
2198 sc->result = DID_RESET << 16;
2199 sc->scsi_done(sc);
2200 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002201 }
2202
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002203 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2204
2205 /* walk again to check, if IOs are still pending in fw */
2206 if (fnic_is_abts_pending(fnic, lr_sc))
2207 ret = FAILED;
2208
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002209clean_pending_aborts_end:
2210 return ret;
2211}
2212
Hiral Patel03298552013-02-12 17:00:58 -08002213/**
2214 * fnic_scsi_host_start_tag
2215 * Allocates tagid from host's tag list
2216 **/
2217static inline int
2218fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2219{
2220 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2221 int tag, ret = SCSI_NO_TAG;
2222
2223 BUG_ON(!bqt);
2224 if (!bqt) {
2225 pr_err("Tags are not supported\n");
2226 goto end;
2227 }
2228
2229 do {
2230 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2231 if (tag >= bqt->max_depth) {
2232 pr_err("Tag allocation failure\n");
2233 goto end;
2234 }
2235 } while (test_and_set_bit(tag, bqt->tag_map));
2236
2237 bqt->tag_index[tag] = sc->request;
2238 sc->request->tag = tag;
2239 sc->tag = tag;
2240 if (!sc->request->special)
2241 sc->request->special = sc;
2242
2243 ret = tag;
2244
2245end:
2246 return ret;
2247}
2248
2249/**
2250 * fnic_scsi_host_end_tag
2251 * frees tag allocated by fnic_scsi_host_start_tag.
2252 **/
2253static inline void
2254fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2255{
2256 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2257 int tag = sc->request->tag;
2258
2259 if (tag == SCSI_NO_TAG)
2260 return;
2261
2262 BUG_ON(!bqt || !bqt->tag_index[tag]);
2263 if (!bqt)
2264 return;
2265
2266 bqt->tag_index[tag] = NULL;
2267 clear_bit(tag, bqt->tag_map);
2268
2269 return;
2270}
2271
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002272/*
2273 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2274 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2275 * on the LUN.
2276 */
2277int fnic_device_reset(struct scsi_cmnd *sc)
2278{
2279 struct fc_lport *lp;
2280 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002281 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002282 struct fc_rport *rport;
2283 int status;
2284 int ret = FAILED;
2285 spinlock_t *io_lock;
2286 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08002287 unsigned long start_time = 0;
Hiral Patel03298552013-02-12 17:00:58 -08002288 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07002289 struct fnic_stats *fnic_stats;
2290 struct reset_stats *reset_stats;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002291 int tag = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002292 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patel03298552013-02-12 17:00:58 -08002293 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
Satish Kharata36f5dd2016-03-18 11:22:49 -07002294 bool new_sc = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002295
2296 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01002297 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002298
2299 /* Get local-port, check ready and link up */
2300 lp = shost_priv(sc->device->host);
2301
2302 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002303 fnic_stats = &fnic->fnic_stats;
2304 reset_stats = &fnic->fnic_stats.reset_stats;
2305
2306 atomic64_inc(&reset_stats->device_resets);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002307
Roel Kluin0db6f432010-06-11 16:44:46 -07002308 rport = starget_to_rport(scsi_target(sc->device));
2309 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002310 "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
Hiral Patel03298552013-02-12 17:00:58 -08002311 rport->port_id, sc->device->lun, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002312
2313 if (lp->state != LPORT_ST_READY || !(lp->link_up))
2314 goto fnic_device_reset_end;
2315
2316 /* Check if remote port up */
Hiral Patel67125b02013-09-12 17:45:42 -07002317 if (fc_remote_port_chkready(rport)) {
2318 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002319 goto fnic_device_reset_end;
Hiral Patel67125b02013-09-12 17:45:42 -07002320 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002321
Hiral Patel14eb5d92013-02-12 17:01:01 -08002322 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
Hiral Patel03298552013-02-12 17:00:58 -08002323 /* Allocate tag if not present */
2324
2325 tag = sc->request->tag;
2326 if (unlikely(tag < 0)) {
Christoph Hellwig4d637162014-06-12 14:53:23 +02002327 /*
2328 * XXX(hch): current the midlayer fakes up a struct
2329 * request for the explicit reset ioctls, and those
2330 * don't have a tag allocated to them. The below
2331 * code pokes into midlayer structures to paper over
2332 * this design issue, but that won't work for blk-mq.
2333 *
2334 * Either someone who can actually test the hardware
2335 * will have to come up with a similar hack for the
2336 * blk-mq case, or we'll have to bite the bullet and
2337 * fix the way the EH ioctls work for real, but until
2338 * that happens we fail these explicit requests here.
2339 */
Christoph Hellwig4d637162014-06-12 14:53:23 +02002340
Hiral Patel03298552013-02-12 17:00:58 -08002341 tag = fnic_scsi_host_start_tag(fnic, sc);
2342 if (unlikely(tag == SCSI_NO_TAG))
2343 goto fnic_device_reset_end;
2344 tag_gen_flag = 1;
Satish Kharata36f5dd2016-03-18 11:22:49 -07002345 new_sc = 1;
Hiral Patel03298552013-02-12 17:00:58 -08002346 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002347 io_lock = fnic_io_lock_hash(fnic, sc);
2348 spin_lock_irqsave(io_lock, flags);
2349 io_req = (struct fnic_io_req *)CMD_SP(sc);
2350
2351 /*
2352 * If there is a io_req attached to this command, then use it,
2353 * else allocate a new one.
2354 */
2355 if (!io_req) {
2356 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2357 if (!io_req) {
2358 spin_unlock_irqrestore(io_lock, flags);
2359 goto fnic_device_reset_end;
2360 }
2361 memset(io_req, 0, sizeof(*io_req));
2362 io_req->port_id = rport->port_id;
2363 CMD_SP(sc) = (char *)io_req;
2364 }
2365 io_req->dr_done = &tm_done;
2366 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2367 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2368 spin_unlock_irqrestore(io_lock, flags);
2369
Hiral Patel03298552013-02-12 17:00:58 -08002370 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002371
2372 /*
2373 * issue the device reset, if enqueue failed, clean up the ioreq
2374 * and break assoc with scsi cmd
2375 */
2376 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2377 spin_lock_irqsave(io_lock, flags);
2378 io_req = (struct fnic_io_req *)CMD_SP(sc);
2379 if (io_req)
2380 io_req->dr_done = NULL;
2381 goto fnic_device_reset_clean;
2382 }
Hiral Patel03298552013-02-12 17:00:58 -08002383 spin_lock_irqsave(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002384 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08002385 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002386
2387 /*
2388 * Wait on the local completion for LUN reset. The io_req may be
2389 * freed while we wait since we hold no lock.
2390 */
2391 wait_for_completion_timeout(&tm_done,
2392 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2393
2394 spin_lock_irqsave(io_lock, flags);
2395 io_req = (struct fnic_io_req *)CMD_SP(sc);
2396 if (!io_req) {
2397 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002398 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2399 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002400 goto fnic_device_reset_end;
2401 }
2402 io_req->dr_done = NULL;
2403
2404 status = CMD_LR_STATUS(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002405
2406 /*
2407 * If lun reset not completed, bail out with failed. io_req
2408 * gets cleaned up during higher levels of EH
2409 */
2410 if (status == FCPIO_INVALID_CODE) {
Hiral Patel67125b02013-09-12 17:45:42 -07002411 atomic64_inc(&reset_stats->device_reset_timeouts);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002412 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2413 "Device reset timed out\n");
Hiral Patel03298552013-02-12 17:00:58 -08002414 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2415 spin_unlock_irqrestore(io_lock, flags);
2416 int_to_scsilun(sc->device->lun, &fc_lun);
2417 /*
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002418 * Issue abort and terminate on device reset request.
2419 * If q'ing of terminate fails, retry it after a delay.
Hiral Patel03298552013-02-12 17:00:58 -08002420 */
2421 while (1) {
2422 spin_lock_irqsave(io_lock, flags);
2423 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2424 spin_unlock_irqrestore(io_lock, flags);
2425 break;
2426 }
2427 spin_unlock_irqrestore(io_lock, flags);
2428 if (fnic_queue_abort_io_req(fnic,
2429 tag | FNIC_TAG_DEV_RST,
2430 FCPIO_ITMF_ABT_TASK_TERM,
2431 fc_lun.scsi_lun, io_req)) {
2432 wait_for_completion_timeout(&tm_done,
2433 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2434 } else {
2435 spin_lock_irqsave(io_lock, flags);
2436 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2437 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2438 io_req->abts_done = &tm_done;
2439 spin_unlock_irqrestore(io_lock, flags);
2440 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2441 "Abort and terminate issued on Device reset "
2442 "tag 0x%x sc 0x%p\n", tag, sc);
2443 break;
2444 }
2445 }
2446 while (1) {
2447 spin_lock_irqsave(io_lock, flags);
2448 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2449 spin_unlock_irqrestore(io_lock, flags);
2450 wait_for_completion_timeout(&tm_done,
2451 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2452 break;
2453 } else {
2454 io_req = (struct fnic_io_req *)CMD_SP(sc);
2455 io_req->abts_done = NULL;
2456 goto fnic_device_reset_clean;
2457 }
2458 }
2459 } else {
2460 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002461 }
2462
2463 /* Completed, but not successful, clean up the io_req, return fail */
2464 if (status != FCPIO_SUCCESS) {
2465 spin_lock_irqsave(io_lock, flags);
2466 FNIC_SCSI_DBG(KERN_DEBUG,
2467 fnic->lport->host,
2468 "Device reset completed - failed\n");
2469 io_req = (struct fnic_io_req *)CMD_SP(sc);
2470 goto fnic_device_reset_clean;
2471 }
2472
2473 /*
2474 * Clean up any aborts on this lun that have still not
2475 * completed. If any of these fail, then LUN reset fails.
2476 * clean_pending_aborts cleans all cmds on this lun except
2477 * the lun reset cmd. If all cmds get cleaned, the lun reset
2478 * succeeds
2479 */
Satish Kharata36f5dd2016-03-18 11:22:49 -07002480 if (fnic_clean_pending_aborts(fnic, sc, new_sc)) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002481 spin_lock_irqsave(io_lock, flags);
2482 io_req = (struct fnic_io_req *)CMD_SP(sc);
2483 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2484 "Device reset failed"
2485 " since could not abort all IOs\n");
2486 goto fnic_device_reset_clean;
2487 }
2488
2489 /* Clean lun reset command */
2490 spin_lock_irqsave(io_lock, flags);
2491 io_req = (struct fnic_io_req *)CMD_SP(sc);
2492 if (io_req)
2493 /* Completed, and successful */
2494 ret = SUCCESS;
2495
2496fnic_device_reset_clean:
2497 if (io_req)
2498 CMD_SP(sc) = NULL;
2499
2500 spin_unlock_irqrestore(io_lock, flags);
2501
2502 if (io_req) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002503 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002504 fnic_release_ioreq_buf(fnic, io_req, sc);
2505 mempool_free(io_req, fnic->io_req_pool);
2506 }
2507
2508fnic_device_reset_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08002509 FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2510 sc->request->tag, sc,
2511 jiffies_to_msecs(jiffies - start_time),
2512 0, ((u64)sc->cmnd[0] << 32 |
2513 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2514 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2515 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2516
Hiral Patel03298552013-02-12 17:00:58 -08002517 /* free tag if it is allocated */
2518 if (unlikely(tag_gen_flag))
2519 fnic_scsi_host_end_tag(fnic, sc);
2520
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002521 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2522 "Returning from device reset %s\n",
2523 (ret == SUCCESS) ?
2524 "SUCCESS" : "FAILED");
Hiral Patel67125b02013-09-12 17:45:42 -07002525
2526 if (ret == FAILED)
2527 atomic64_inc(&reset_stats->device_reset_failures);
2528
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002529 return ret;
2530}
2531
2532/* Clean up all IOs, clean up libFC local port */
2533int fnic_reset(struct Scsi_Host *shost)
2534{
2535 struct fc_lport *lp;
2536 struct fnic *fnic;
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002537 int ret = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07002538 struct reset_stats *reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002539
2540 lp = shost_priv(shost);
2541 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002542 reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002543
2544 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2545 "fnic_reset called\n");
2546
Hiral Patel67125b02013-09-12 17:45:42 -07002547 atomic64_inc(&reset_stats->fnic_resets);
2548
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002549 /*
2550 * Reset local port, this will clean up libFC exchanges,
2551 * reset remote port sessions, and if link is up, begin flogi
2552 */
Hannes Reinecke31c0a632016-10-18 10:01:36 +02002553 ret = fc_lport_reset(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002554
2555 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2556 "Returning from fnic reset %s\n",
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002557 (ret == 0) ?
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002558 "SUCCESS" : "FAILED");
2559
Hiral Patel67125b02013-09-12 17:45:42 -07002560 if (ret == 0)
2561 atomic64_inc(&reset_stats->fnic_reset_completions);
2562 else
2563 atomic64_inc(&reset_stats->fnic_reset_failures);
2564
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002565 return ret;
2566}
2567
2568/*
2569 * SCSI Error handling calls driver's eh_host_reset if all prior
2570 * error handling levels return FAILED. If host reset completes
2571 * successfully, and if link is up, then Fabric login begins.
2572 *
2573 * Host Reset is the highest level of error recovery. If this fails, then
2574 * host is offlined by SCSI.
2575 *
2576 */
2577int fnic_host_reset(struct scsi_cmnd *sc)
2578{
2579 int ret;
2580 unsigned long wait_host_tmo;
2581 struct Scsi_Host *shost = sc->device->host;
2582 struct fc_lport *lp = shost_priv(shost);
Satish Kharat9698b6f2016-12-14 13:20:41 -08002583 struct fnic *fnic = lport_priv(lp);
2584 unsigned long flags;
2585
2586 spin_lock_irqsave(&fnic->fnic_lock, flags);
2587 if (fnic->internal_reset_inprogress == 0) {
2588 fnic->internal_reset_inprogress = 1;
2589 } else {
2590 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2591 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2592 "host reset in progress skipping another host reset\n");
2593 return SUCCESS;
2594 }
2595 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002596
2597 /*
2598 * If fnic_reset is successful, wait for fabric login to complete
2599 * scsi-ml tries to send a TUR to every device if host reset is
2600 * successful, so before returning to scsi, fabric should be up
2601 */
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002602 ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002603 if (ret == SUCCESS) {
2604 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2605 ret = FAILED;
2606 while (time_before(jiffies, wait_host_tmo)) {
2607 if ((lp->state == LPORT_ST_READY) &&
2608 (lp->link_up)) {
2609 ret = SUCCESS;
2610 break;
2611 }
2612 ssleep(1);
2613 }
2614 }
2615
Satish Kharat9698b6f2016-12-14 13:20:41 -08002616 spin_lock_irqsave(&fnic->fnic_lock, flags);
2617 fnic->internal_reset_inprogress = 0;
2618 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002619 return ret;
2620}
2621
2622/*
2623 * This fxn is called from libFC when host is removed
2624 */
2625void fnic_scsi_abort_io(struct fc_lport *lp)
2626{
2627 int err = 0;
2628 unsigned long flags;
2629 enum fnic_state old_state;
2630 struct fnic *fnic = lport_priv(lp);
2631 DECLARE_COMPLETION_ONSTACK(remove_wait);
2632
2633 /* Issue firmware reset for fnic, wait for reset to complete */
Hiral Patel03298552013-02-12 17:00:58 -08002634retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002635 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002636 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2637 /* fw reset is in progress, poll for its completion */
2638 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2639 schedule_timeout(msecs_to_jiffies(100));
2640 goto retry_fw_reset;
2641 }
2642
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002643 fnic->remove_wait = &remove_wait;
2644 old_state = fnic->state;
2645 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002646 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002647 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2648
2649 err = fnic_fw_reset_handler(fnic);
2650 if (err) {
2651 spin_lock_irqsave(&fnic->fnic_lock, flags);
2652 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2653 fnic->state = old_state;
2654 fnic->remove_wait = NULL;
2655 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2656 return;
2657 }
2658
2659 /* Wait for firmware reset to complete */
2660 wait_for_completion_timeout(&remove_wait,
2661 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2662
2663 spin_lock_irqsave(&fnic->fnic_lock, flags);
2664 fnic->remove_wait = NULL;
2665 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2666 "fnic_scsi_abort_io %s\n",
2667 (fnic->state == FNIC_IN_ETH_MODE) ?
2668 "SUCCESS" : "FAILED");
2669 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2670
2671}
2672
2673/*
2674 * This fxn called from libFC to clean up driver IO state on link down
2675 */
2676void fnic_scsi_cleanup(struct fc_lport *lp)
2677{
2678 unsigned long flags;
2679 enum fnic_state old_state;
2680 struct fnic *fnic = lport_priv(lp);
2681
2682 /* issue fw reset */
Hiral Patel03298552013-02-12 17:00:58 -08002683retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002684 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002685 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2686 /* fw reset is in progress, poll for its completion */
2687 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2688 schedule_timeout(msecs_to_jiffies(100));
2689 goto retry_fw_reset;
2690 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002691 old_state = fnic->state;
2692 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002693 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002694 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2695
2696 if (fnic_fw_reset_handler(fnic)) {
2697 spin_lock_irqsave(&fnic->fnic_lock, flags);
2698 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2699 fnic->state = old_state;
2700 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2701 }
2702
2703}
2704
2705void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2706{
2707}
2708
2709void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2710{
2711 struct fnic *fnic = lport_priv(lp);
2712
2713 /* Non-zero sid, nothing to do */
2714 if (sid)
2715 goto call_fc_exch_mgr_reset;
2716
2717 if (did) {
2718 fnic_rport_exch_reset(fnic, did);
2719 goto call_fc_exch_mgr_reset;
2720 }
2721
2722 /*
2723 * sid = 0, did = 0
2724 * link down or device being removed
2725 */
2726 if (!fnic->in_remove)
2727 fnic_scsi_cleanup(lp);
2728 else
2729 fnic_scsi_abort_io(lp);
2730
2731 /* call libFC exch mgr reset to reset its exchanges */
2732call_fc_exch_mgr_reset:
2733 fc_exch_mgr_reset(lp, sid, did);
2734
2735}
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002736
2737/*
2738 * fnic_is_abts_pending() is a helper function that
2739 * walks through tag map to check if there is any IOs pending,if there is one,
2740 * then it returns 1 (true), otherwise 0 (false)
2741 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2742 * otherwise, it checks for all IOs.
2743 */
2744int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2745{
2746 int tag;
2747 struct fnic_io_req *io_req;
2748 spinlock_t *io_lock;
2749 unsigned long flags;
2750 int ret = 0;
2751 struct scsi_cmnd *sc;
2752 struct scsi_device *lun_dev = NULL;
2753
2754 if (lr_sc)
2755 lun_dev = lr_sc->device;
2756
2757 /* walk again to check, if IOs are still pending in fw */
Hiral Patelfc857992013-09-09 13:31:51 -07002758 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002759 sc = scsi_host_find_tag(fnic->lport->host, tag);
2760 /*
2761 * ignore this lun reset cmd or cmds that do not belong to
2762 * this lun
2763 */
2764 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2765 continue;
2766
2767 io_lock = fnic_io_lock_hash(fnic, sc);
2768 spin_lock_irqsave(io_lock, flags);
2769
2770 io_req = (struct fnic_io_req *)CMD_SP(sc);
2771
2772 if (!io_req || sc->device != lun_dev) {
2773 spin_unlock_irqrestore(io_lock, flags);
2774 continue;
2775 }
2776
2777 /*
2778 * Found IO that is still pending with firmware and
2779 * belongs to the LUN that we are resetting
2780 */
2781 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2782 "Found IO in %s on lun\n",
2783 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2784
Dan Carpenter5d65f912013-05-30 10:50:46 +03002785 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002786 ret = 1;
Dan Carpenter5d65f912013-05-30 10:50:46 +03002787 spin_unlock_irqrestore(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002788 }
2789
2790 return ret;
2791}