blob: 155b286f1a9d3cc8b366a6a5b7610ae562337f62 [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;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700333
334 if (sg_count) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700335 /* For each SGE, create a device desc entry */
336 desc = io_req->sgl_list;
337 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
338 desc->addr = cpu_to_le64(sg_dma_address(sg));
339 desc->len = cpu_to_le32(sg_dma_len(sg));
340 desc->_resvd = 0;
341 desc++;
342 }
343
344 io_req->sgl_list_pa = pci_map_single
345 (fnic->pdev,
346 io_req->sgl_list,
347 sizeof(io_req->sgl_list[0]) * sg_count,
348 PCI_DMA_TODEVICE);
349 }
350
351 io_req->sense_buf_pa = pci_map_single(fnic->pdev,
352 sc->sense_buffer,
353 SCSI_SENSE_BUFFERSIZE,
354 PCI_DMA_FROMDEVICE);
355
356 int_to_scsilun(sc->device->lun, &fc_lun);
357
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700358 /* Enqueue the descriptor in the Copy WQ */
359 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
360
361 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
362 free_wq_copy_descs(fnic, wq);
363
364 if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
365 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800366 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
367 "fnic_queue_wq_copy_desc failure - no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -0700368 atomic64_inc(&misc_stats->io_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700369 return SCSI_MLQUEUE_HOST_BUSY;
370 }
371
372 flags = 0;
373 if (sc->sc_data_direction == DMA_FROM_DEVICE)
374 flags = FCPIO_ICMND_RDDATA;
375 else if (sc->sc_data_direction == DMA_TO_DEVICE)
376 flags = FCPIO_ICMND_WRDATA;
377
378 exch_flags = 0;
379 if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
380 (rp->flags & FC_RP_FLAGS_RETRY))
381 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
382
383 fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
384 0, exch_flags, io_req->sgl_cnt,
385 SCSI_SENSE_BUFFERSIZE,
386 io_req->sgl_list_pa,
387 io_req->sense_buf_pa,
388 0, /* scsi cmd ref, always 0 */
Christoph Hellwig50668632014-10-30 14:30:06 +0100389 FCPIO_ICMND_PTA_SIMPLE,
390 /* scsi pri and tag */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700391 flags, /* command flags */
Abhijeet Joglekar4b536622009-10-21 16:28:25 -0700392 sc->cmnd, sc->cmd_len,
393 scsi_bufflen(sc),
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700394 fc_lun.scsi_lun, io_req->port_id,
395 rport->maxframe_size, rp->r_a_tov,
396 rp->e_d_tov);
397
Hiral Patel67125b02013-09-12 17:45:42 -0700398 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
399 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
400 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
401 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
402 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
403
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700404 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
405 return 0;
406}
407
408/*
409 * fnic_queuecommand
410 * Routine to send a scsi cdb
411 * Called with host_lock held and interrupts disabled.
412 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500413static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700414{
Hiral Patel03298552013-02-12 17:00:58 -0800415 struct fc_lport *lp = shost_priv(sc->device->host);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700416 struct fc_rport *rport;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800417 struct fnic_io_req *io_req = NULL;
Hiral Patel03298552013-02-12 17:00:58 -0800418 struct fnic *fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -0700419 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700420 struct vnic_wq_copy *wq;
421 int ret;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800422 u64 cmd_trace;
423 int sg_count = 0;
Hiral Shah41df7b02014-11-10 12:54:36 -0800424 unsigned long flags = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700425 unsigned long ptr;
Hiral Shah35061e22014-11-10 12:54:34 -0800426 struct fc_rport_priv *rdata;
Hiral Shah41df7b02014-11-10 12:54:36 -0800427 spinlock_t *io_lock = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700428
Hiral Patel03298552013-02-12 17:00:58 -0800429 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
430 return SCSI_MLQUEUE_HOST_BUSY;
431
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700432 rport = starget_to_rport(scsi_target(sc->device));
433 ret = fc_remote_port_chkready(rport);
434 if (ret) {
Hiral Patel67125b02013-09-12 17:45:42 -0700435 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700436 sc->result = ret;
437 done(sc);
438 return 0;
439 }
440
Hiral Shah35061e22014-11-10 12:54:34 -0800441 rdata = lp->tt.rport_lookup(lp, rport->port_id);
442 if (!rdata || (rdata->rp_state == RPORT_ST_DELETE)) {
443 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
444 "returning IO as rport is removed\n");
445 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
446 sc->result = DID_NO_CONNECT;
447 done(sc);
448 return 0;
449 }
450
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700451 if (lp->state != LPORT_ST_READY || !(lp->link_up))
452 return SCSI_MLQUEUE_HOST_BUSY;
453
Hiral Patel03298552013-02-12 17:00:58 -0800454 atomic_inc(&fnic->in_flight);
455
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700456 /*
457 * Release host lock, use driver resource specific locks from here.
458 * Don't re-enable interrupts in case they were disabled prior to the
459 * caller disabling them.
460 */
461 spin_unlock(lp->host->host_lock);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800462 CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
463 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700464
465 /* Get a new io_req for this SCSI IO */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700466 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
467 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -0700468 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700469 ret = SCSI_MLQUEUE_HOST_BUSY;
470 goto out;
471 }
472 memset(io_req, 0, sizeof(*io_req));
473
474 /* Map the data buffer */
475 sg_count = scsi_dma_map(sc);
476 if (sg_count < 0) {
Hiral Patel4d7007b2013-02-12 17:01:02 -0800477 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
478 sc->request->tag, sc, 0, sc->cmnd[0],
479 sg_count, CMD_STATE(sc));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700480 mempool_free(io_req, fnic->io_req_pool);
481 goto out;
482 }
483
484 /* Determine the type of scatter/gather list we need */
485 io_req->sgl_cnt = sg_count;
486 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
487 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
488 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
489
490 if (sg_count) {
491 io_req->sgl_list =
492 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
Abhijeet Joglekar0c79c742011-06-13 21:21:01 -0700493 GFP_ATOMIC);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700494 if (!io_req->sgl_list) {
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 scsi_dma_unmap(sc);
498 mempool_free(io_req, fnic->io_req_pool);
499 goto out;
500 }
501
502 /* Cache sgl list allocated address before alignment */
503 io_req->sgl_list_alloc = io_req->sgl_list;
504 ptr = (unsigned long) io_req->sgl_list;
505 if (ptr % FNIC_SG_DESC_ALIGN) {
506 io_req->sgl_list = (struct host_sg_desc *)
507 (((unsigned long) ptr
508 + FNIC_SG_DESC_ALIGN - 1)
509 & ~(FNIC_SG_DESC_ALIGN - 1));
510 }
511 }
512
Hiral Shah41df7b02014-11-10 12:54:36 -0800513 /*
514 * Will acquire lock defore setting to IO initialized.
515 */
516
517 io_lock = fnic_io_lock_hash(fnic, sc);
518 spin_lock_irqsave(io_lock, flags);
519
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700520 /* initialize rest of io_req */
521 io_req->port_id = rport->port_id;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800522 io_req->start_time = jiffies;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700523 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
524 CMD_SP(sc) = (char *)io_req;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800525 CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700526 sc->scsi_done = done;
527
528 /* create copy wq desc and enqueue it */
529 wq = &fnic->wq_copy[0];
530 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
531 if (ret) {
532 /*
533 * In case another thread cancelled the request,
534 * refetch the pointer under the lock.
535 */
Hiral Patel4d7007b2013-02-12 17:01:02 -0800536 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
537 sc->request->tag, sc, 0, 0, 0,
538 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700539 io_req = (struct fnic_io_req *)CMD_SP(sc);
540 CMD_SP(sc) = NULL;
541 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
542 spin_unlock_irqrestore(io_lock, flags);
543 if (io_req) {
544 fnic_release_ioreq_buf(fnic, io_req, sc);
545 mempool_free(io_req, fnic->io_req_pool);
546 }
Hiral Shah41df7b02014-11-10 12:54:36 -0800547 atomic_dec(&fnic->in_flight);
548 /* acquire host lock before returning to SCSI */
549 spin_lock(lp->host->host_lock);
550 return ret;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800551 } else {
Hiral Patel67125b02013-09-12 17:45:42 -0700552 atomic64_inc(&fnic_stats->io_stats.active_ios);
553 atomic64_inc(&fnic_stats->io_stats.num_ios);
554 if (atomic64_read(&fnic_stats->io_stats.active_ios) >
555 atomic64_read(&fnic_stats->io_stats.max_active_ios))
556 atomic64_set(&fnic_stats->io_stats.max_active_ios,
557 atomic64_read(&fnic_stats->io_stats.active_ios));
558
Hiral Patel14eb5d92013-02-12 17:01:01 -0800559 /* REVISIT: Use per IO lock in the final code */
560 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700561 }
562out:
Hiral Patel4d7007b2013-02-12 17:01:02 -0800563 cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
564 (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
565 (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
566 sc->cmnd[5]);
567
568 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
569 sc->request->tag, sc, io_req,
570 sg_count, cmd_trace,
571 (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
Hiral Shah41df7b02014-11-10 12:54:36 -0800572
573 /* if only we issued IO, will we have the io lock */
574 if (CMD_FLAGS(sc) & FNIC_IO_INITIALIZED)
575 spin_unlock_irqrestore(io_lock, flags);
576
Hiral Patel03298552013-02-12 17:00:58 -0800577 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700578 /* acquire host lock before returning to SCSI */
579 spin_lock(lp->host->host_lock);
580 return ret;
581}
582
Jeff Garzikf2812332010-11-16 02:10:29 -0500583DEF_SCSI_QCMD(fnic_queuecommand)
584
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700585/*
586 * fnic_fcpio_fw_reset_cmpl_handler
587 * Routine to handle fw reset completion
588 */
589static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
590 struct fcpio_fw_req *desc)
591{
592 u8 type;
593 u8 hdr_status;
594 struct fcpio_tag tag;
595 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700596 unsigned long flags;
Hiral Patel67125b02013-09-12 17:45:42 -0700597 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700598
599 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
600
Hiral Patel67125b02013-09-12 17:45:42 -0700601 atomic64_inc(&reset_stats->fw_reset_completions);
602
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700603 /* Clean up all outstanding io requests */
604 fnic_cleanup_io(fnic, SCSI_NO_TAG);
605
Hiral Patel67125b02013-09-12 17:45:42 -0700606 atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
607 atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
608
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700609 spin_lock_irqsave(&fnic->fnic_lock, flags);
610
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700611 /* fnic should be in FC_TRANS_ETH_MODE */
612 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
613 /* Check status of reset completion */
614 if (!hdr_status) {
615 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
616 "reset cmpl success\n");
617 /* Ready to send flogi out */
618 fnic->state = FNIC_IN_ETH_MODE;
619 } else {
620 FNIC_SCSI_DBG(KERN_DEBUG,
621 fnic->lport->host,
622 "fnic fw_reset : failed %s\n",
623 fnic_fcpio_status_to_str(hdr_status));
624
625 /*
626 * Unable to change to eth mode, cannot send out flogi
627 * Change state to fc mode, so that subsequent Flogi
628 * requests from libFC will cause more attempts to
629 * reset the firmware. Free the cached flogi
630 */
631 fnic->state = FNIC_IN_FC_MODE;
Hiral Patel67125b02013-09-12 17:45:42 -0700632 atomic64_inc(&reset_stats->fw_reset_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700633 ret = -1;
634 }
635 } else {
636 FNIC_SCSI_DBG(KERN_DEBUG,
637 fnic->lport->host,
638 "Unexpected state %s while processing"
639 " reset cmpl\n", fnic_state_to_str(fnic->state));
Hiral Patel67125b02013-09-12 17:45:42 -0700640 atomic64_inc(&reset_stats->fw_reset_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700641 ret = -1;
642 }
643
644 /* Thread removing device blocks till firmware reset is complete */
645 if (fnic->remove_wait)
646 complete(fnic->remove_wait);
647
648 /*
649 * If fnic is being removed, or fw reset failed
650 * free the flogi frame. Else, send it out
651 */
652 if (fnic->remove_wait || ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700653 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Joe Eykholt78112e52009-11-03 11:49:22 -0800654 skb_queue_purge(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700655 goto reset_cmpl_handler_end;
656 }
657
658 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
659
Joe Eykholt78112e52009-11-03 11:49:22 -0800660 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700661
662 reset_cmpl_handler_end:
Hiral Patel03298552013-02-12 17:00:58 -0800663 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
664
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700665 return ret;
666}
667
668/*
669 * fnic_fcpio_flogi_reg_cmpl_handler
670 * Routine to handle flogi register completion
671 */
672static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
673 struct fcpio_fw_req *desc)
674{
675 u8 type;
676 u8 hdr_status;
677 struct fcpio_tag tag;
678 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700679 unsigned long flags;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700680
681 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
682
683 /* Update fnic state based on status of flogi reg completion */
684 spin_lock_irqsave(&fnic->fnic_lock, flags);
685
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700686 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
687
688 /* Check flogi registration completion status */
689 if (!hdr_status) {
690 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
691 "flog reg succeeded\n");
692 fnic->state = FNIC_IN_FC_MODE;
693 } else {
694 FNIC_SCSI_DBG(KERN_DEBUG,
695 fnic->lport->host,
696 "fnic flogi reg :failed %s\n",
697 fnic_fcpio_status_to_str(hdr_status));
698 fnic->state = FNIC_IN_ETH_MODE;
699 ret = -1;
700 }
701 } else {
702 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
703 "Unexpected fnic state %s while"
704 " processing flogi reg completion\n",
705 fnic_state_to_str(fnic->state));
706 ret = -1;
707 }
708
Joe Eykholt78112e52009-11-03 11:49:22 -0800709 if (!ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700710 if (fnic->stop_rx_link_events) {
711 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
712 goto reg_cmpl_handler_end;
713 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700714 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
715
Joe Eykholt78112e52009-11-03 11:49:22 -0800716 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700717 queue_work(fnic_event_queue, &fnic->frame_work);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700718 } else {
719 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700720 }
721
722reg_cmpl_handler_end:
723 return ret;
724}
725
726static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
727 u16 request_out)
728{
729 if (wq->to_clean_index <= wq->to_use_index) {
730 /* out of range, stale request_out index */
731 if (request_out < wq->to_clean_index ||
732 request_out >= wq->to_use_index)
733 return 0;
734 } else {
735 /* out of range, stale request_out index */
736 if (request_out < wq->to_clean_index &&
737 request_out >= wq->to_use_index)
738 return 0;
739 }
740 /* request_out index is in range */
741 return 1;
742}
743
744
745/*
746 * Mark that ack received and store the Ack index. If there are multiple
747 * acks received before Tx thread cleans it up, the latest value will be
748 * used which is correct behavior. This state should be in the copy Wq
749 * instead of in the fnic
750 */
751static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
752 unsigned int cq_index,
753 struct fcpio_fw_req *desc)
754{
755 struct vnic_wq_copy *wq;
756 u16 request_out = desc->u.ack.request_out;
757 unsigned long flags;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800758 u64 *ox_id_tag = (u64 *)(void *)desc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700759
760 /* mark the ack state */
761 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
762 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
763
Hiral Patel67125b02013-09-12 17:45:42 -0700764 fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700765 if (is_ack_index_in_range(wq, request_out)) {
766 fnic->fw_ack_index[0] = request_out;
767 fnic->fw_ack_recd[0] = 1;
Hiral Patel67125b02013-09-12 17:45:42 -0700768 } else
769 atomic64_inc(
770 &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
771
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700772 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800773 FNIC_TRACE(fnic_fcpio_ack_handler,
774 fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
775 ox_id_tag[4], ox_id_tag[5]);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700776}
777
778/*
779 * fnic_fcpio_icmnd_cmpl_handler
780 * Routine to handle icmnd completions
781 */
782static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
783 struct fcpio_fw_req *desc)
784{
785 u8 type;
786 u8 hdr_status;
787 struct fcpio_tag tag;
788 u32 id;
789 u64 xfer_len = 0;
790 struct fcpio_icmnd_cmpl *icmnd_cmpl;
791 struct fnic_io_req *io_req;
792 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -0700793 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700794 unsigned long flags;
795 spinlock_t *io_lock;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800796 u64 cmd_trace;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800797 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700798
799 /* Decode the cmpl description to get the io_req id */
800 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
801 fcpio_tag_id_dec(&tag, &id);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800802 icmnd_cmpl = &desc->u.icmnd_cmpl;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700803
Hiral Patelfc857992013-09-09 13:31:51 -0700804 if (id >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -0800805 shost_printk(KERN_ERR, fnic->lport->host,
806 "Tag out of range tag %x hdr status = %s\n",
807 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700808 return;
Hiral Patel03298552013-02-12 17:00:58 -0800809 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700810
811 sc = scsi_host_find_tag(fnic->lport->host, id);
812 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800813 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -0700814 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800815 shost_printk(KERN_ERR, fnic->lport->host,
816 "icmnd_cmpl sc is null - "
817 "hdr status = %s tag = 0x%x desc = 0x%p\n",
818 fnic_fcpio_status_to_str(hdr_status), id, desc);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800819 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
820 fnic->lport->host->host_no, id,
821 ((u64)icmnd_cmpl->_resvd0[1] << 16 |
822 (u64)icmnd_cmpl->_resvd0[0]),
823 ((u64)hdr_status << 16 |
824 (u64)icmnd_cmpl->scsi_status << 8 |
825 (u64)icmnd_cmpl->flags), desc,
826 (u64)icmnd_cmpl->residual, 0);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700827 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800828 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700829
830 io_lock = fnic_io_lock_hash(fnic, sc);
831 spin_lock_irqsave(io_lock, flags);
832 io_req = (struct fnic_io_req *)CMD_SP(sc);
833 WARN_ON_ONCE(!io_req);
834 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -0700835 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800836 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700837 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800838 shost_printk(KERN_ERR, fnic->lport->host,
839 "icmnd_cmpl io_req is null - "
840 "hdr status = %s tag = 0x%x sc 0x%p\n",
841 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700842 return;
843 }
Hiral Patel14eb5d92013-02-12 17:01:01 -0800844 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700845
846 /* firmware completed the io */
847 io_req->io_completed = 1;
848
849 /*
850 * if SCSI-ML has already issued abort on this command,
851 * ignore completion of the IO. The abts path will clean it up
852 */
853 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
854 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800855 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
856 switch (hdr_status) {
857 case FCPIO_SUCCESS:
858 CMD_FLAGS(sc) |= FNIC_IO_DONE;
859 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
860 "icmnd_cmpl ABTS pending hdr status = %s "
861 "sc 0x%p scsi_status %x residual %d\n",
862 fnic_fcpio_status_to_str(hdr_status), sc,
863 icmnd_cmpl->scsi_status,
864 icmnd_cmpl->residual);
865 break;
866 case FCPIO_ABORTED:
867 CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
868 break;
869 default:
870 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
871 "icmnd_cmpl abts pending "
872 "hdr status = %s tag = 0x%x sc = 0x%p\n",
873 fnic_fcpio_status_to_str(hdr_status),
874 id, sc);
875 break;
876 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700877 return;
878 }
879
880 /* Mark the IO as complete */
881 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
882
883 icmnd_cmpl = &desc->u.icmnd_cmpl;
884
885 switch (hdr_status) {
886 case FCPIO_SUCCESS:
887 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
888 xfer_len = scsi_bufflen(sc);
889 scsi_set_resid(sc, icmnd_cmpl->residual);
890
891 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
892 xfer_len -= icmnd_cmpl->residual;
893
Hiral Patel67125b02013-09-12 17:45:42 -0700894 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
895 atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700896 break;
897
898 case FCPIO_TIMEOUT: /* request was timed out */
Hiral Patel67125b02013-09-12 17:45:42 -0700899 atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700900 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
901 break;
902
903 case FCPIO_ABORTED: /* request was aborted */
Hiral Patel67125b02013-09-12 17:45:42 -0700904 atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700905 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
906 break;
907
908 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
Hiral Patel67125b02013-09-12 17:45:42 -0700909 atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700910 scsi_set_resid(sc, icmnd_cmpl->residual);
911 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
912 break;
913
914 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
Hiral Patel67125b02013-09-12 17:45:42 -0700915 atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700916 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
917 break;
Hiral Patel67125b02013-09-12 17:45:42 -0700918
919 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
920 atomic64_inc(&fnic_stats->io_stats.io_not_found);
921 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
922 break;
923
924 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
925 atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
926 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
927 break;
928
929 case FCPIO_FW_ERR: /* request was terminated due fw error */
930 atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
931 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
932 break;
933
934 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
935 atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
936 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
937 break;
938
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700939 case FCPIO_INVALID_HEADER: /* header contains invalid data */
940 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
941 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700942 default:
943 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
944 fnic_fcpio_status_to_str(hdr_status));
945 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
946 break;
947 }
948
Hiral Patel67125b02013-09-12 17:45:42 -0700949 if (hdr_status != FCPIO_SUCCESS) {
950 atomic64_inc(&fnic_stats->io_stats.io_failures);
951 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
952 fnic_fcpio_status_to_str(hdr_status));
953 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700954 /* Break link with the SCSI command */
955 CMD_SP(sc) = NULL;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800956 CMD_FLAGS(sc) |= FNIC_IO_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700957
958 spin_unlock_irqrestore(io_lock, flags);
959
960 fnic_release_ioreq_buf(fnic, io_req, sc);
961
962 mempool_free(io_req, fnic->io_req_pool);
963
Hiral Patel4d7007b2013-02-12 17:01:02 -0800964 cmd_trace = ((u64)hdr_status << 56) |
965 (u64)icmnd_cmpl->scsi_status << 48 |
966 (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
967 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
968 (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
969
970 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
971 sc->device->host->host_no, id, sc,
972 ((u64)icmnd_cmpl->_resvd0[1] << 56 |
973 (u64)icmnd_cmpl->_resvd0[0] << 48 |
974 jiffies_to_msecs(jiffies - start_time)),
975 desc, cmd_trace,
976 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
977
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700978 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
979 fnic->lport->host_stats.fcp_input_requests++;
980 fnic->fcp_input_bytes += xfer_len;
981 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
982 fnic->lport->host_stats.fcp_output_requests++;
983 fnic->fcp_output_bytes += xfer_len;
984 } else
985 fnic->lport->host_stats.fcp_control_requests++;
986
Hiral Patel67125b02013-09-12 17:45:42 -0700987 atomic64_dec(&fnic_stats->io_stats.active_ios);
988 if (atomic64_read(&fnic->io_cmpl_skip))
989 atomic64_dec(&fnic->io_cmpl_skip);
990 else
991 atomic64_inc(&fnic_stats->io_stats.io_completions);
992
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700993 /* Call SCSI completion function to complete the IO */
994 if (sc->scsi_done)
995 sc->scsi_done(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700996}
997
998/* fnic_fcpio_itmf_cmpl_handler
999 * Routine to handle itmf completions
1000 */
1001static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1002 struct fcpio_fw_req *desc)
1003{
1004 u8 type;
1005 u8 hdr_status;
1006 struct fcpio_tag tag;
1007 u32 id;
1008 struct scsi_cmnd *sc;
1009 struct fnic_io_req *io_req;
Hiral Patel67125b02013-09-12 17:45:42 -07001010 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1011 struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1012 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1013 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001014 unsigned long flags;
1015 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001016 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001017
1018 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1019 fcpio_tag_id_dec(&tag, &id);
1020
Hiral Patelfc857992013-09-09 13:31:51 -07001021 if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -08001022 shost_printk(KERN_ERR, fnic->lport->host,
1023 "Tag out of range tag %x hdr status = %s\n",
1024 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001025 return;
Hiral Patel03298552013-02-12 17:00:58 -08001026 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001027
1028 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1029 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001030 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -07001031 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001032 shost_printk(KERN_ERR, fnic->lport->host,
1033 "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1034 fnic_fcpio_status_to_str(hdr_status), id);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001035 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001036 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001037 io_lock = fnic_io_lock_hash(fnic, sc);
1038 spin_lock_irqsave(io_lock, flags);
1039 io_req = (struct fnic_io_req *)CMD_SP(sc);
1040 WARN_ON_ONCE(!io_req);
1041 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001042 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001043 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001044 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1045 shost_printk(KERN_ERR, fnic->lport->host,
1046 "itmf_cmpl io_req is null - "
1047 "hdr status = %s tag = 0x%x sc 0x%p\n",
1048 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001049 return;
1050 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001051 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001052
Hiral Patel03298552013-02-12 17:00:58 -08001053 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1054 /* Abort and terminate completion of device reset req */
1055 /* REVISIT : Add asserts about various flags */
1056 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1057 "dev reset abts cmpl recd. id %x status %s\n",
1058 id, fnic_fcpio_status_to_str(hdr_status));
1059 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1060 CMD_ABTS_STATUS(sc) = hdr_status;
1061 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1062 if (io_req->abts_done)
1063 complete(io_req->abts_done);
1064 spin_unlock_irqrestore(io_lock, flags);
1065 } else if (id & FNIC_TAG_ABORT) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001066 /* Completion of abort cmd */
Hiral Patel67125b02013-09-12 17:45:42 -07001067 switch (hdr_status) {
1068 case FCPIO_SUCCESS:
1069 break;
1070 case FCPIO_TIMEOUT:
1071 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1072 atomic64_inc(&abts_stats->abort_fw_timeouts);
1073 else
1074 atomic64_inc(
1075 &term_stats->terminate_fw_timeouts);
1076 break;
1077 case FCPIO_IO_NOT_FOUND:
1078 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1079 atomic64_inc(&abts_stats->abort_io_not_found);
1080 else
1081 atomic64_inc(
1082 &term_stats->terminate_io_not_found);
1083 break;
1084 default:
1085 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1086 atomic64_inc(&abts_stats->abort_failures);
1087 else
1088 atomic64_inc(
1089 &term_stats->terminate_failures);
1090 break;
1091 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001092 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1093 /* This is a late completion. Ignore it */
1094 spin_unlock_irqrestore(io_lock, flags);
1095 return;
1096 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001097 CMD_ABTS_STATUS(sc) = hdr_status;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001098 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Hiral Patel67125b02013-09-12 17:45:42 -07001099
1100 atomic64_dec(&fnic_stats->io_stats.active_ios);
1101 if (atomic64_read(&fnic->io_cmpl_skip))
1102 atomic64_dec(&fnic->io_cmpl_skip);
1103 else
1104 atomic64_inc(&fnic_stats->io_stats.io_completions);
1105
1106 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1107 atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1108
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001109 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1110 "abts cmpl recd. id %d status %s\n",
1111 (int)(id & FNIC_TAG_MASK),
1112 fnic_fcpio_status_to_str(hdr_status));
1113
1114 /*
1115 * If scsi_eh thread is blocked waiting for abts to complete,
1116 * signal completion to it. IO will be cleaned in the thread
1117 * else clean it in this context
1118 */
1119 if (io_req->abts_done) {
1120 complete(io_req->abts_done);
1121 spin_unlock_irqrestore(io_lock, flags);
1122 } else {
1123 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1124 "abts cmpl, completing IO\n");
1125 CMD_SP(sc) = NULL;
1126 sc->result = (DID_ERROR << 16);
1127
1128 spin_unlock_irqrestore(io_lock, flags);
1129
1130 fnic_release_ioreq_buf(fnic, io_req, sc);
1131 mempool_free(io_req, fnic->io_req_pool);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001132 if (sc->scsi_done) {
1133 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1134 sc->device->host->host_no, id,
1135 sc,
1136 jiffies_to_msecs(jiffies - start_time),
1137 desc,
1138 (((u64)hdr_status << 40) |
1139 (u64)sc->cmnd[0] << 32 |
1140 (u64)sc->cmnd[2] << 24 |
1141 (u64)sc->cmnd[3] << 16 |
1142 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1143 (((u64)CMD_FLAGS(sc) << 32) |
1144 CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001145 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001146 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001147 }
1148
1149 } else if (id & FNIC_TAG_DEV_RST) {
1150 /* Completion of device reset */
1151 CMD_LR_STATUS(sc) = hdr_status;
Hiral Patel03298552013-02-12 17:00:58 -08001152 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1153 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001154 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001155 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1156 sc->device->host->host_no, id, sc,
1157 jiffies_to_msecs(jiffies - start_time),
1158 desc, 0,
1159 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001160 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1161 "Terminate pending "
1162 "dev reset cmpl recd. id %d status %s\n",
1163 (int)(id & FNIC_TAG_MASK),
1164 fnic_fcpio_status_to_str(hdr_status));
1165 return;
1166 }
1167 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1168 /* Need to wait for terminate completion */
1169 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001170 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1171 sc->device->host->host_no, id, sc,
1172 jiffies_to_msecs(jiffies - start_time),
1173 desc, 0,
1174 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001175 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1176 "dev reset cmpl recd after time out. "
1177 "id %d status %s\n",
1178 (int)(id & FNIC_TAG_MASK),
1179 fnic_fcpio_status_to_str(hdr_status));
1180 return;
1181 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001182 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
Hiral Patel03298552013-02-12 17:00:58 -08001183 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001184 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1185 "dev reset cmpl recd. id %d status %s\n",
1186 (int)(id & FNIC_TAG_MASK),
1187 fnic_fcpio_status_to_str(hdr_status));
1188 if (io_req->dr_done)
1189 complete(io_req->dr_done);
1190 spin_unlock_irqrestore(io_lock, flags);
1191
1192 } else {
1193 shost_printk(KERN_ERR, fnic->lport->host,
1194 "Unexpected itmf io state %s tag %x\n",
1195 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1196 spin_unlock_irqrestore(io_lock, flags);
1197 }
1198
1199}
1200
1201/*
1202 * fnic_fcpio_cmpl_handler
1203 * Routine to service the cq for wq_copy
1204 */
1205static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1206 unsigned int cq_index,
1207 struct fcpio_fw_req *desc)
1208{
1209 struct fnic *fnic = vnic_dev_priv(vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001210
1211 switch (desc->hdr.type) {
Hiral Patel67125b02013-09-12 17:45:42 -07001212 case FCPIO_ICMND_CMPL: /* fw completed a command */
1213 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1214 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1215 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1216 case FCPIO_RESET_CMPL: /* fw completed reset */
1217 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1218 break;
1219 default:
1220 break;
1221 }
1222
1223 switch (desc->hdr.type) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001224 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1225 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1226 break;
1227
1228 case FCPIO_ICMND_CMPL: /* fw completed a command */
1229 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1230 break;
1231
1232 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1233 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1234 break;
1235
1236 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
Joe Eykholt78112e52009-11-03 11:49:22 -08001237 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
Hiral Patel03298552013-02-12 17:00:58 -08001238 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001239 break;
1240
1241 case FCPIO_RESET_CMPL: /* fw completed reset */
Hiral Patel03298552013-02-12 17:00:58 -08001242 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001243 break;
1244
1245 default:
1246 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1247 "firmware completion type %d\n",
1248 desc->hdr.type);
1249 break;
1250 }
1251
Hiral Patel03298552013-02-12 17:00:58 -08001252 return 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001253}
1254
1255/*
1256 * fnic_wq_copy_cmpl_handler
1257 * Routine to process wq copy
1258 */
1259int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1260{
1261 unsigned int wq_work_done = 0;
1262 unsigned int i, cq_index;
1263 unsigned int cur_work_done;
1264
1265 for (i = 0; i < fnic->wq_copy_count; i++) {
1266 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1267 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1268 fnic_fcpio_cmpl_handler,
1269 copy_work_to_do);
1270 wq_work_done += cur_work_done;
1271 }
1272 return wq_work_done;
1273}
1274
1275static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1276{
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001277 int i;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001278 struct fnic_io_req *io_req;
1279 unsigned long flags = 0;
1280 struct scsi_cmnd *sc;
1281 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001282 unsigned long start_time = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07001283 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001284
Hiral Patelfc857992013-09-09 13:31:51 -07001285 for (i = 0; i < fnic->fnic_max_tag_id; i++) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001286 if (i == exclude_id)
1287 continue;
1288
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001289 io_lock = fnic_io_lock_tag(fnic, i);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001290 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001291 sc = scsi_host_find_tag(fnic->lport->host, i);
1292 if (!sc) {
1293 spin_unlock_irqrestore(io_lock, flags);
1294 continue;
1295 }
1296
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001297 io_req = (struct fnic_io_req *)CMD_SP(sc);
Hiral Patel03298552013-02-12 17:00:58 -08001298 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1299 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1300 /*
1301 * We will be here only when FW completes reset
1302 * without sending completions for outstanding ios.
1303 */
1304 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1305 if (io_req && io_req->dr_done)
1306 complete(io_req->dr_done);
1307 else if (io_req && io_req->abts_done)
1308 complete(io_req->abts_done);
1309 spin_unlock_irqrestore(io_lock, flags);
1310 continue;
1311 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1312 spin_unlock_irqrestore(io_lock, flags);
1313 continue;
1314 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001315 if (!io_req) {
1316 spin_unlock_irqrestore(io_lock, flags);
1317 goto cleanup_scsi_cmd;
1318 }
1319
1320 CMD_SP(sc) = NULL;
1321
1322 spin_unlock_irqrestore(io_lock, flags);
1323
1324 /*
1325 * If there is a scsi_cmnd associated with this io_req, then
1326 * free the corresponding state
1327 */
Hiral Patel14eb5d92013-02-12 17:01:01 -08001328 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001329 fnic_release_ioreq_buf(fnic, io_req, sc);
1330 mempool_free(io_req, fnic->io_req_pool);
1331
1332cleanup_scsi_cmd:
1333 sc->result = DID_TRANSPORT_DISRUPTED << 16;
Hiral Shah66818662014-04-18 12:28:18 -07001334 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1335 "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1336 __func__, (jiffies - start_time));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001337
Hiral Patel67125b02013-09-12 17:45:42 -07001338 if (atomic64_read(&fnic->io_cmpl_skip))
1339 atomic64_dec(&fnic->io_cmpl_skip);
1340 else
1341 atomic64_inc(&fnic_stats->io_stats.io_completions);
1342
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001343 /* Complete the command to SCSI */
Hiral Patel4d7007b2013-02-12 17:01:02 -08001344 if (sc->scsi_done) {
1345 FNIC_TRACE(fnic_cleanup_io,
1346 sc->device->host->host_no, i, sc,
1347 jiffies_to_msecs(jiffies - start_time),
1348 0, ((u64)sc->cmnd[0] << 32 |
1349 (u64)sc->cmnd[2] << 24 |
1350 (u64)sc->cmnd[3] << 16 |
1351 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1352 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1353
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001354 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001355 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001356 }
1357}
1358
1359void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1360 struct fcpio_host_req *desc)
1361{
1362 u32 id;
1363 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1364 struct fnic_io_req *io_req;
1365 struct scsi_cmnd *sc;
1366 unsigned long flags;
1367 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001368 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001369
1370 /* get the tag reference */
1371 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1372 id &= FNIC_TAG_MASK;
1373
Hiral Patelfc857992013-09-09 13:31:51 -07001374 if (id >= fnic->fnic_max_tag_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001375 return;
1376
1377 sc = scsi_host_find_tag(fnic->lport->host, id);
1378 if (!sc)
1379 return;
1380
1381 io_lock = fnic_io_lock_hash(fnic, sc);
1382 spin_lock_irqsave(io_lock, flags);
1383
1384 /* Get the IO context which this desc refers to */
1385 io_req = (struct fnic_io_req *)CMD_SP(sc);
1386
1387 /* fnic interrupts are turned off by now */
1388
1389 if (!io_req) {
1390 spin_unlock_irqrestore(io_lock, flags);
1391 goto wq_copy_cleanup_scsi_cmd;
1392 }
1393
1394 CMD_SP(sc) = NULL;
1395
1396 spin_unlock_irqrestore(io_lock, flags);
1397
Hiral Patel14eb5d92013-02-12 17:01:01 -08001398 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001399 fnic_release_ioreq_buf(fnic, io_req, sc);
1400 mempool_free(io_req, fnic->io_req_pool);
1401
1402wq_copy_cleanup_scsi_cmd:
1403 sc->result = DID_NO_CONNECT << 16;
1404 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1405 " DID_NO_CONNECT\n");
1406
Hiral Patel4d7007b2013-02-12 17:01:02 -08001407 if (sc->scsi_done) {
1408 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1409 sc->device->host->host_no, id, sc,
1410 jiffies_to_msecs(jiffies - start_time),
1411 0, ((u64)sc->cmnd[0] << 32 |
1412 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1413 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1414 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1415
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001416 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001417 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001418}
1419
1420static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1421 u32 task_req, u8 *fc_lun,
1422 struct fnic_io_req *io_req)
1423{
1424 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001425 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001426 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001427 unsigned long flags;
1428
Hiral Patel03298552013-02-12 17:00:58 -08001429 spin_lock_irqsave(host->host_lock, flags);
1430 if (unlikely(fnic_chk_state_flags_locked(fnic,
1431 FNIC_FLAGS_IO_BLOCKED))) {
1432 spin_unlock_irqrestore(host->host_lock, flags);
1433 return 1;
1434 } else
1435 atomic_inc(&fnic->in_flight);
1436 spin_unlock_irqrestore(host->host_lock, flags);
1437
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001438 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1439
1440 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1441 free_wq_copy_descs(fnic, wq);
1442
1443 if (!vnic_wq_copy_desc_avail(wq)) {
1444 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001445 atomic_dec(&fnic->in_flight);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001446 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001447 "fnic_queue_abort_io_req: failure: no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07001448 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001449 return 1;
1450 }
1451 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1452 0, task_req, tag, fc_lun, io_req->port_id,
1453 fnic->config.ra_tov, fnic->config.ed_tov);
1454
Hiral Patel67125b02013-09-12 17:45:42 -07001455 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1456 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1457 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1458 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1459 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1460
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001461 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001462 atomic_dec(&fnic->in_flight);
1463
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001464 return 0;
1465}
1466
Hiral Patel03298552013-02-12 17:00:58 -08001467static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001468{
1469 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001470 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001471 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001472 struct fnic_io_req *io_req;
1473 spinlock_t *io_lock;
1474 unsigned long flags;
1475 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -07001476 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1477 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001478 struct scsi_lun fc_lun;
1479 enum fnic_ioreq_state old_ioreq_state;
1480
1481 FNIC_SCSI_DBG(KERN_DEBUG,
1482 fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001483 "fnic_rport_exch_reset called portid 0x%06x\n",
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001484 port_id);
1485
1486 if (fnic->in_remove)
1487 return;
1488
Hiral Patelfc857992013-09-09 13:31:51 -07001489 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001490 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001491 io_lock = fnic_io_lock_tag(fnic, tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001492 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001493 sc = scsi_host_find_tag(fnic->lport->host, tag);
1494 if (!sc) {
1495 spin_unlock_irqrestore(io_lock, flags);
1496 continue;
1497 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001498
1499 io_req = (struct fnic_io_req *)CMD_SP(sc);
1500
1501 if (!io_req || io_req->port_id != port_id) {
1502 spin_unlock_irqrestore(io_lock, flags);
1503 continue;
1504 }
1505
Hiral Patel03298552013-02-12 17:00:58 -08001506 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001507 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001508 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1509 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1510 sc);
1511 spin_unlock_irqrestore(io_lock, flags);
1512 continue;
1513 }
1514
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001515 /*
1516 * Found IO that is still pending with firmware and
1517 * belongs to rport that went away
1518 */
1519 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1520 spin_unlock_irqrestore(io_lock, flags);
1521 continue;
1522 }
Hiral Patel03298552013-02-12 17:00:58 -08001523 if (io_req->abts_done) {
1524 shost_printk(KERN_ERR, fnic->lport->host,
1525 "fnic_rport_exch_reset: io_req->abts_done is set "
1526 "state is %s\n",
1527 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1528 }
1529
Hiral Patel14eb5d92013-02-12 17:01:01 -08001530 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1531 shost_printk(KERN_ERR, fnic->lport->host,
1532 "rport_exch_reset "
1533 "IO not yet issued %p tag 0x%x flags "
1534 "%x state %d\n",
1535 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1536 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001537 old_ioreq_state = CMD_STATE(sc);
1538 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1539 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001540 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001541 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001542 abt_tag = (tag | FNIC_TAG_DEV_RST);
1543 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1544 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1545 sc);
1546 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001547
1548 BUG_ON(io_req->abts_done);
1549
1550 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1551 "fnic_rport_reset_exch: Issuing abts\n");
1552
1553 spin_unlock_irqrestore(io_lock, flags);
1554
1555 /* Now queue the abort command to firmware */
1556 int_to_scsilun(sc->device->lun, &fc_lun);
1557
Hiral Patel03298552013-02-12 17:00:58 -08001558 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001559 FCPIO_ITMF_ABT_TASK_TERM,
1560 fc_lun.scsi_lun, io_req)) {
1561 /*
1562 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001563 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001564 * aborted later by scsi_eh, or cleaned up during
1565 * lun reset
1566 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001567 spin_lock_irqsave(io_lock, flags);
1568 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1569 CMD_STATE(sc) = old_ioreq_state;
1570 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001571 } else {
1572 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001573 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1574 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001575 else
1576 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001577 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001578 atomic64_inc(&term_stats->terminates);
1579 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001580 }
1581 }
Hiral Patel67125b02013-09-12 17:45:42 -07001582 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1583 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001584
1585}
1586
1587void fnic_terminate_rport_io(struct fc_rport *rport)
1588{
1589 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001590 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001591 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001592 struct fnic_io_req *io_req;
1593 spinlock_t *io_lock;
1594 unsigned long flags;
1595 struct scsi_cmnd *sc;
1596 struct scsi_lun fc_lun;
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001597 struct fc_rport_libfc_priv *rdata;
1598 struct fc_lport *lport;
1599 struct fnic *fnic;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001600 struct fc_rport *cmd_rport;
Hiral Patel67125b02013-09-12 17:45:42 -07001601 struct reset_stats *reset_stats;
1602 struct terminate_stats *term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001603 enum fnic_ioreq_state old_ioreq_state;
1604
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001605 if (!rport) {
1606 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1607 return;
1608 }
1609 rdata = rport->dd_data;
1610
1611 if (!rdata) {
1612 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1613 return;
1614 }
1615 lport = rdata->local_port;
1616
1617 if (!lport) {
1618 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1619 return;
1620 }
1621 fnic = lport_priv(lport);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001622 FNIC_SCSI_DBG(KERN_DEBUG,
1623 fnic->lport->host, "fnic_terminate_rport_io called"
Hiral Patel03298552013-02-12 17:00:58 -08001624 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1625 rport->port_name, rport->node_name, rport,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001626 rport->port_id);
1627
1628 if (fnic->in_remove)
1629 return;
1630
Hiral Patel67125b02013-09-12 17:45:42 -07001631 reset_stats = &fnic->fnic_stats.reset_stats;
1632 term_stats = &fnic->fnic_stats.term_stats;
1633
Hiral Patelfc857992013-09-09 13:31:51 -07001634 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001635 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001636 io_lock = fnic_io_lock_tag(fnic, tag);
1637 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001638 sc = scsi_host_find_tag(fnic->lport->host, tag);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001639 if (!sc) {
1640 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001641 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001642 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001643
1644 cmd_rport = starget_to_rport(scsi_target(sc->device));
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001645 if (rport != cmd_rport) {
1646 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001647 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001648 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001649
1650 io_req = (struct fnic_io_req *)CMD_SP(sc);
1651
1652 if (!io_req || rport != cmd_rport) {
1653 spin_unlock_irqrestore(io_lock, flags);
1654 continue;
1655 }
1656
Hiral Patel03298552013-02-12 17:00:58 -08001657 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001658 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001659 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1660 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1661 sc);
1662 spin_unlock_irqrestore(io_lock, flags);
1663 continue;
1664 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001665 /*
1666 * Found IO that is still pending with firmware and
1667 * belongs to rport that went away
1668 */
1669 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1670 spin_unlock_irqrestore(io_lock, flags);
1671 continue;
1672 }
Hiral Patel03298552013-02-12 17:00:58 -08001673 if (io_req->abts_done) {
1674 shost_printk(KERN_ERR, fnic->lport->host,
1675 "fnic_terminate_rport_io: io_req->abts_done is set "
1676 "state is %s\n",
1677 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1678 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001679 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1680 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1681 "fnic_terminate_rport_io "
1682 "IO not yet issued %p tag 0x%x flags "
1683 "%x state %d\n",
1684 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1685 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001686 old_ioreq_state = CMD_STATE(sc);
1687 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1688 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001689 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001690 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001691 abt_tag = (tag | FNIC_TAG_DEV_RST);
1692 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1693 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1694 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001695
1696 BUG_ON(io_req->abts_done);
1697
1698 FNIC_SCSI_DBG(KERN_DEBUG,
1699 fnic->lport->host,
1700 "fnic_terminate_rport_io: Issuing abts\n");
1701
1702 spin_unlock_irqrestore(io_lock, flags);
1703
1704 /* Now queue the abort command to firmware */
1705 int_to_scsilun(sc->device->lun, &fc_lun);
1706
Hiral Patel03298552013-02-12 17:00:58 -08001707 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001708 FCPIO_ITMF_ABT_TASK_TERM,
1709 fc_lun.scsi_lun, io_req)) {
1710 /*
1711 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001712 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001713 * aborted later by scsi_eh, or cleaned up during
1714 * lun reset
1715 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001716 spin_lock_irqsave(io_lock, flags);
1717 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1718 CMD_STATE(sc) = old_ioreq_state;
1719 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001720 } else {
1721 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001722 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1723 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001724 else
1725 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001726 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001727 atomic64_inc(&term_stats->terminates);
1728 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001729 }
1730 }
Hiral Patel67125b02013-09-12 17:45:42 -07001731 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1732 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001733
1734}
1735
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001736/*
1737 * This function is exported to SCSI for sending abort cmnds.
1738 * A SCSI IO is represented by a io_req in the driver.
1739 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1740 */
1741int fnic_abort_cmd(struct scsi_cmnd *sc)
1742{
1743 struct fc_lport *lp;
1744 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001745 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001746 struct fc_rport *rport;
1747 spinlock_t *io_lock;
1748 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001749 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001750 int ret = SUCCESS;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001751 u32 task_req = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001752 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07001753 struct fnic_stats *fnic_stats;
1754 struct abort_stats *abts_stats;
1755 struct terminate_stats *term_stats;
Hiral Shah66818662014-04-18 12:28:18 -07001756 enum fnic_ioreq_state old_ioreq_state;
Hiral Patel03298552013-02-12 17:00:58 -08001757 int tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001758 DECLARE_COMPLETION_ONSTACK(tm_done);
1759
1760 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001761 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001762
1763 /* Get local-port, check ready and link up */
1764 lp = shost_priv(sc->device->host);
1765
1766 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07001767 fnic_stats = &fnic->fnic_stats;
1768 abts_stats = &fnic->fnic_stats.abts_stats;
1769 term_stats = &fnic->fnic_stats.term_stats;
1770
Roel Kluin0db6f432010-06-11 16:44:46 -07001771 rport = starget_to_rport(scsi_target(sc->device));
Hiral Patel03298552013-02-12 17:00:58 -08001772 tag = sc->request->tag;
1773 FNIC_SCSI_DBG(KERN_DEBUG,
1774 fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001775 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
Hiral Patel03298552013-02-12 17:00:58 -08001776 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1777
1778 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1779
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001780 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1781 ret = FAILED;
1782 goto fnic_abort_cmd_end;
1783 }
1784
1785 /*
1786 * Avoid a race between SCSI issuing the abort and the device
1787 * completing the command.
1788 *
1789 * If the command is already completed by the fw cmpl code,
1790 * we just return SUCCESS from here. This means that the abort
1791 * succeeded. In the SCSI ML, since the timeout for command has
1792 * happened, the completion wont actually complete the command
1793 * and it will be considered as an aborted command
1794 *
1795 * The CMD_SP will not be cleared except while holding io_req_lock.
1796 */
1797 io_lock = fnic_io_lock_hash(fnic, sc);
1798 spin_lock_irqsave(io_lock, flags);
1799 io_req = (struct fnic_io_req *)CMD_SP(sc);
1800 if (!io_req) {
1801 spin_unlock_irqrestore(io_lock, flags);
1802 goto fnic_abort_cmd_end;
1803 }
1804
1805 io_req->abts_done = &tm_done;
1806
1807 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1808 spin_unlock_irqrestore(io_lock, flags);
1809 goto wait_pending;
1810 }
1811 /*
1812 * Command is still pending, need to abort it
1813 * If the firmware completes the command after this point,
1814 * the completion wont be done till mid-layer, since abort
1815 * has already started.
1816 */
Hiral Shah66818662014-04-18 12:28:18 -07001817 old_ioreq_state = CMD_STATE(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001818 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1819 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1820
1821 spin_unlock_irqrestore(io_lock, flags);
1822
1823 /*
1824 * Check readiness of the remote port. If the path to remote
1825 * port is up, then send abts to the remote port to terminate
1826 * the IO. Else, just locally terminate the IO in the firmware
1827 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001828 if (fc_remote_port_chkready(rport) == 0)
1829 task_req = FCPIO_ITMF_ABT_TASK;
Hiral Patel67125b02013-09-12 17:45:42 -07001830 else {
1831 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001832 task_req = FCPIO_ITMF_ABT_TASK_TERM;
Hiral Patel67125b02013-09-12 17:45:42 -07001833 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001834
1835 /* Now queue the abort command to firmware */
1836 int_to_scsilun(sc->device->lun, &fc_lun);
1837
1838 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1839 fc_lun.scsi_lun, io_req)) {
1840 spin_lock_irqsave(io_lock, flags);
Hiral Shah66818662014-04-18 12:28:18 -07001841 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1842 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001843 io_req = (struct fnic_io_req *)CMD_SP(sc);
1844 if (io_req)
1845 io_req->abts_done = NULL;
1846 spin_unlock_irqrestore(io_lock, flags);
1847 ret = FAILED;
1848 goto fnic_abort_cmd_end;
1849 }
Hiral Patel67125b02013-09-12 17:45:42 -07001850 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001851 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001852 atomic64_inc(&fnic_stats->abts_stats.aborts);
1853 } else {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001854 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001855 atomic64_inc(&fnic_stats->term_stats.terminates);
1856 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001857
1858 /*
1859 * We queued an abort IO, wait for its completion.
1860 * Once the firmware completes the abort command, it will
1861 * wake up this thread.
1862 */
1863 wait_pending:
1864 wait_for_completion_timeout(&tm_done,
1865 msecs_to_jiffies
1866 (2 * fnic->config.ra_tov +
1867 fnic->config.ed_tov));
1868
1869 /* Check the abort status */
1870 spin_lock_irqsave(io_lock, flags);
1871
1872 io_req = (struct fnic_io_req *)CMD_SP(sc);
1873 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001874 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001875 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001876 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001877 ret = FAILED;
1878 goto fnic_abort_cmd_end;
1879 }
1880 io_req->abts_done = NULL;
1881
1882 /* fw did not complete abort, timed out */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001883 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001884 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001885 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel67125b02013-09-12 17:45:42 -07001886 atomic64_inc(&abts_stats->abort_drv_timeouts);
1887 } else {
Hiral Patel67125b02013-09-12 17:45:42 -07001888 atomic64_inc(&term_stats->terminate_drv_timeouts);
1889 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001890 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001891 ret = FAILED;
1892 goto fnic_abort_cmd_end;
1893 }
1894
Anil Chintalapati (achintal)efc7a282014-12-23 19:40:00 +00001895 /* IO out of order */
1896
1897 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1898 spin_unlock_irqrestore(io_lock, flags);
1899 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1900 "Issuing Host reset due to out of order IO\n");
1901
1902 if (fnic_host_reset(sc) == FAILED) {
1903 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1904 "fnic_host_reset failed.\n");
1905 }
1906 ret = FAILED;
1907 goto fnic_abort_cmd_end;
1908 }
1909
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001910 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1911
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001912 /*
1913 * firmware completed the abort, check the status,
1914 * free the io_req irrespective of failure or success
1915 */
1916 if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1917 ret = FAILED;
1918
1919 CMD_SP(sc) = NULL;
1920
1921 spin_unlock_irqrestore(io_lock, flags);
1922
Hiral Patel14eb5d92013-02-12 17:01:01 -08001923 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001924 fnic_release_ioreq_buf(fnic, io_req, sc);
1925 mempool_free(io_req, fnic->io_req_pool);
1926
1927fnic_abort_cmd_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08001928 FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1929 sc->request->tag, sc,
1930 jiffies_to_msecs(jiffies - start_time),
1931 0, ((u64)sc->cmnd[0] << 32 |
1932 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1933 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1934 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1935
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001936 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel14eb5d92013-02-12 17:01:01 -08001937 "Returning from abort cmd type %x %s\n", task_req,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001938 (ret == SUCCESS) ?
1939 "SUCCESS" : "FAILED");
1940 return ret;
1941}
1942
1943static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1944 struct scsi_cmnd *sc,
1945 struct fnic_io_req *io_req)
1946{
1947 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001948 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001949 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001950 struct scsi_lun fc_lun;
1951 int ret = 0;
1952 unsigned long intr_flags;
1953
Hiral Patel03298552013-02-12 17:00:58 -08001954 spin_lock_irqsave(host->host_lock, intr_flags);
1955 if (unlikely(fnic_chk_state_flags_locked(fnic,
1956 FNIC_FLAGS_IO_BLOCKED))) {
1957 spin_unlock_irqrestore(host->host_lock, intr_flags);
1958 return FAILED;
1959 } else
1960 atomic_inc(&fnic->in_flight);
1961 spin_unlock_irqrestore(host->host_lock, intr_flags);
1962
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001963 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1964
1965 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1966 free_wq_copy_descs(fnic, wq);
1967
1968 if (!vnic_wq_copy_desc_avail(wq)) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001969 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1970 "queue_dr_io_req failure - no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07001971 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001972 ret = -EAGAIN;
1973 goto lr_io_req_end;
1974 }
1975
1976 /* fill in the lun info */
1977 int_to_scsilun(sc->device->lun, &fc_lun);
1978
1979 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1980 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1981 fc_lun.scsi_lun, io_req->port_id,
1982 fnic->config.ra_tov, fnic->config.ed_tov);
1983
Hiral Patel67125b02013-09-12 17:45:42 -07001984 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1985 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1986 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1987 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1988 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1989
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001990lr_io_req_end:
1991 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel03298552013-02-12 17:00:58 -08001992 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001993
1994 return ret;
1995}
1996
1997/*
1998 * Clean up any pending aborts on the lun
1999 * For each outstanding IO on this lun, whose abort is not completed by fw,
2000 * issue a local abort. Wait for abort to complete. Return 0 if all commands
2001 * successfully aborted, 1 otherwise
2002 */
2003static int fnic_clean_pending_aborts(struct fnic *fnic,
2004 struct scsi_cmnd *lr_sc)
2005{
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002006 int tag, abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002007 struct fnic_io_req *io_req;
2008 spinlock_t *io_lock;
2009 unsigned long flags;
2010 int ret = 0;
2011 struct scsi_cmnd *sc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002012 struct scsi_lun fc_lun;
2013 struct scsi_device *lun_dev = lr_sc->device;
2014 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002015 enum fnic_ioreq_state old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002016
Hiral Patelfc857992013-09-09 13:31:51 -07002017 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002018 io_lock = fnic_io_lock_tag(fnic, tag);
2019 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002020 sc = scsi_host_find_tag(fnic->lport->host, tag);
2021 /*
2022 * ignore this lun reset cmd or cmds that do not belong to
2023 * this lun
2024 */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002025 if (!sc || sc == lr_sc || sc->device != lun_dev) {
2026 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002027 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002028 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002029
2030 io_req = (struct fnic_io_req *)CMD_SP(sc);
2031
2032 if (!io_req || sc->device != lun_dev) {
2033 spin_unlock_irqrestore(io_lock, flags);
2034 continue;
2035 }
2036
2037 /*
2038 * Found IO that is still pending with firmware and
2039 * belongs to the LUN that we are resetting
2040 */
2041 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2042 "Found IO in %s on lun\n",
2043 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2044
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002045 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2046 spin_unlock_irqrestore(io_lock, flags);
2047 continue;
2048 }
2049 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08002050 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002051 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2052 "%s dev rst not pending sc 0x%p\n", __func__,
2053 sc);
2054 spin_unlock_irqrestore(io_lock, flags);
2055 continue;
2056 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002057
2058 if (io_req->abts_done)
2059 shost_printk(KERN_ERR, fnic->lport->host,
2060 "%s: io_req->abts_done is set state is %s\n",
2061 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002062 old_ioreq_state = CMD_STATE(sc);
2063 /*
2064 * Any pending IO issued prior to reset is expected to be
2065 * in abts pending state, if not we need to set
2066 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2067 * When IO is completed, the IO will be handed over and
2068 * handled in this function.
2069 */
2070 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2071
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002072 BUG_ON(io_req->abts_done);
2073
2074 abt_tag = tag;
2075 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2076 abt_tag |= FNIC_TAG_DEV_RST;
2077 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2078 "%s: dev rst sc 0x%p\n", __func__, sc);
2079 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002080
2081 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2082 io_req->abts_done = &tm_done;
2083 spin_unlock_irqrestore(io_lock, flags);
2084
2085 /* Now queue the abort command to firmware */
2086 int_to_scsilun(sc->device->lun, &fc_lun);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002087
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002088 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002089 FCPIO_ITMF_ABT_TASK_TERM,
2090 fc_lun.scsi_lun, io_req)) {
2091 spin_lock_irqsave(io_lock, flags);
2092 io_req = (struct fnic_io_req *)CMD_SP(sc);
2093 if (io_req)
2094 io_req->abts_done = NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002095 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2096 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002097 spin_unlock_irqrestore(io_lock, flags);
2098 ret = 1;
2099 goto clean_pending_aborts_end;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002100 } else {
2101 spin_lock_irqsave(io_lock, flags);
2102 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2103 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2104 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002105 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08002106 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002107
2108 wait_for_completion_timeout(&tm_done,
2109 msecs_to_jiffies
2110 (fnic->config.ed_tov));
2111
2112 /* Recheck cmd state to check if it is now aborted */
2113 spin_lock_irqsave(io_lock, flags);
2114 io_req = (struct fnic_io_req *)CMD_SP(sc);
2115 if (!io_req) {
2116 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002117 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002118 continue;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002119 }
2120
2121 io_req->abts_done = NULL;
2122
2123 /* if abort is still pending with fw, fail */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002124 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002125 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002126 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002127 ret = 1;
2128 goto clean_pending_aborts_end;
2129 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002130 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002131 CMD_SP(sc) = NULL;
2132 spin_unlock_irqrestore(io_lock, flags);
2133
2134 fnic_release_ioreq_buf(fnic, io_req, sc);
2135 mempool_free(io_req, fnic->io_req_pool);
2136 }
2137
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002138 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2139
2140 /* walk again to check, if IOs are still pending in fw */
2141 if (fnic_is_abts_pending(fnic, lr_sc))
2142 ret = FAILED;
2143
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002144clean_pending_aborts_end:
2145 return ret;
2146}
2147
Hiral Patel03298552013-02-12 17:00:58 -08002148/**
2149 * fnic_scsi_host_start_tag
2150 * Allocates tagid from host's tag list
2151 **/
2152static inline int
2153fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2154{
2155 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2156 int tag, ret = SCSI_NO_TAG;
2157
2158 BUG_ON(!bqt);
2159 if (!bqt) {
2160 pr_err("Tags are not supported\n");
2161 goto end;
2162 }
2163
2164 do {
2165 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2166 if (tag >= bqt->max_depth) {
2167 pr_err("Tag allocation failure\n");
2168 goto end;
2169 }
2170 } while (test_and_set_bit(tag, bqt->tag_map));
2171
2172 bqt->tag_index[tag] = sc->request;
2173 sc->request->tag = tag;
2174 sc->tag = tag;
2175 if (!sc->request->special)
2176 sc->request->special = sc;
2177
2178 ret = tag;
2179
2180end:
2181 return ret;
2182}
2183
2184/**
2185 * fnic_scsi_host_end_tag
2186 * frees tag allocated by fnic_scsi_host_start_tag.
2187 **/
2188static inline void
2189fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2190{
2191 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2192 int tag = sc->request->tag;
2193
2194 if (tag == SCSI_NO_TAG)
2195 return;
2196
2197 BUG_ON(!bqt || !bqt->tag_index[tag]);
2198 if (!bqt)
2199 return;
2200
2201 bqt->tag_index[tag] = NULL;
2202 clear_bit(tag, bqt->tag_map);
2203
2204 return;
2205}
2206
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002207/*
2208 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2209 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2210 * on the LUN.
2211 */
2212int fnic_device_reset(struct scsi_cmnd *sc)
2213{
2214 struct fc_lport *lp;
2215 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002216 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002217 struct fc_rport *rport;
2218 int status;
2219 int ret = FAILED;
2220 spinlock_t *io_lock;
2221 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08002222 unsigned long start_time = 0;
Hiral Patel03298552013-02-12 17:00:58 -08002223 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07002224 struct fnic_stats *fnic_stats;
2225 struct reset_stats *reset_stats;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002226 int tag = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002227 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patel03298552013-02-12 17:00:58 -08002228 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002229
2230 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01002231 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002232
2233 /* Get local-port, check ready and link up */
2234 lp = shost_priv(sc->device->host);
2235
2236 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002237 fnic_stats = &fnic->fnic_stats;
2238 reset_stats = &fnic->fnic_stats.reset_stats;
2239
2240 atomic64_inc(&reset_stats->device_resets);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002241
Roel Kluin0db6f432010-06-11 16:44:46 -07002242 rport = starget_to_rport(scsi_target(sc->device));
2243 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002244 "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
Hiral Patel03298552013-02-12 17:00:58 -08002245 rport->port_id, sc->device->lun, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002246
2247 if (lp->state != LPORT_ST_READY || !(lp->link_up))
2248 goto fnic_device_reset_end;
2249
2250 /* Check if remote port up */
Hiral Patel67125b02013-09-12 17:45:42 -07002251 if (fc_remote_port_chkready(rport)) {
2252 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002253 goto fnic_device_reset_end;
Hiral Patel67125b02013-09-12 17:45:42 -07002254 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002255
Hiral Patel14eb5d92013-02-12 17:01:01 -08002256 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
Hiral Patel03298552013-02-12 17:00:58 -08002257 /* Allocate tag if not present */
2258
2259 tag = sc->request->tag;
2260 if (unlikely(tag < 0)) {
Christoph Hellwig4d637162014-06-12 14:53:23 +02002261 /*
2262 * XXX(hch): current the midlayer fakes up a struct
2263 * request for the explicit reset ioctls, and those
2264 * don't have a tag allocated to them. The below
2265 * code pokes into midlayer structures to paper over
2266 * this design issue, but that won't work for blk-mq.
2267 *
2268 * Either someone who can actually test the hardware
2269 * will have to come up with a similar hack for the
2270 * blk-mq case, or we'll have to bite the bullet and
2271 * fix the way the EH ioctls work for real, but until
2272 * that happens we fail these explicit requests here.
2273 */
2274 if (shost_use_blk_mq(sc->device->host))
2275 goto fnic_device_reset_end;
2276
Hiral Patel03298552013-02-12 17:00:58 -08002277 tag = fnic_scsi_host_start_tag(fnic, sc);
2278 if (unlikely(tag == SCSI_NO_TAG))
2279 goto fnic_device_reset_end;
2280 tag_gen_flag = 1;
2281 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002282 io_lock = fnic_io_lock_hash(fnic, sc);
2283 spin_lock_irqsave(io_lock, flags);
2284 io_req = (struct fnic_io_req *)CMD_SP(sc);
2285
2286 /*
2287 * If there is a io_req attached to this command, then use it,
2288 * else allocate a new one.
2289 */
2290 if (!io_req) {
2291 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2292 if (!io_req) {
2293 spin_unlock_irqrestore(io_lock, flags);
2294 goto fnic_device_reset_end;
2295 }
2296 memset(io_req, 0, sizeof(*io_req));
2297 io_req->port_id = rport->port_id;
2298 CMD_SP(sc) = (char *)io_req;
2299 }
2300 io_req->dr_done = &tm_done;
2301 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2302 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2303 spin_unlock_irqrestore(io_lock, flags);
2304
Hiral Patel03298552013-02-12 17:00:58 -08002305 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002306
2307 /*
2308 * issue the device reset, if enqueue failed, clean up the ioreq
2309 * and break assoc with scsi cmd
2310 */
2311 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2312 spin_lock_irqsave(io_lock, flags);
2313 io_req = (struct fnic_io_req *)CMD_SP(sc);
2314 if (io_req)
2315 io_req->dr_done = NULL;
2316 goto fnic_device_reset_clean;
2317 }
Hiral Patel03298552013-02-12 17:00:58 -08002318 spin_lock_irqsave(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002319 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08002320 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002321
2322 /*
2323 * Wait on the local completion for LUN reset. The io_req may be
2324 * freed while we wait since we hold no lock.
2325 */
2326 wait_for_completion_timeout(&tm_done,
2327 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2328
2329 spin_lock_irqsave(io_lock, flags);
2330 io_req = (struct fnic_io_req *)CMD_SP(sc);
2331 if (!io_req) {
2332 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002333 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2334 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002335 goto fnic_device_reset_end;
2336 }
2337 io_req->dr_done = NULL;
2338
2339 status = CMD_LR_STATUS(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002340
2341 /*
2342 * If lun reset not completed, bail out with failed. io_req
2343 * gets cleaned up during higher levels of EH
2344 */
2345 if (status == FCPIO_INVALID_CODE) {
Hiral Patel67125b02013-09-12 17:45:42 -07002346 atomic64_inc(&reset_stats->device_reset_timeouts);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002347 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2348 "Device reset timed out\n");
Hiral Patel03298552013-02-12 17:00:58 -08002349 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2350 spin_unlock_irqrestore(io_lock, flags);
2351 int_to_scsilun(sc->device->lun, &fc_lun);
2352 /*
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002353 * Issue abort and terminate on device reset request.
2354 * If q'ing of terminate fails, retry it after a delay.
Hiral Patel03298552013-02-12 17:00:58 -08002355 */
2356 while (1) {
2357 spin_lock_irqsave(io_lock, flags);
2358 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2359 spin_unlock_irqrestore(io_lock, flags);
2360 break;
2361 }
2362 spin_unlock_irqrestore(io_lock, flags);
2363 if (fnic_queue_abort_io_req(fnic,
2364 tag | FNIC_TAG_DEV_RST,
2365 FCPIO_ITMF_ABT_TASK_TERM,
2366 fc_lun.scsi_lun, io_req)) {
2367 wait_for_completion_timeout(&tm_done,
2368 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2369 } else {
2370 spin_lock_irqsave(io_lock, flags);
2371 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2372 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2373 io_req->abts_done = &tm_done;
2374 spin_unlock_irqrestore(io_lock, flags);
2375 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2376 "Abort and terminate issued on Device reset "
2377 "tag 0x%x sc 0x%p\n", tag, sc);
2378 break;
2379 }
2380 }
2381 while (1) {
2382 spin_lock_irqsave(io_lock, flags);
2383 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2384 spin_unlock_irqrestore(io_lock, flags);
2385 wait_for_completion_timeout(&tm_done,
2386 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2387 break;
2388 } else {
2389 io_req = (struct fnic_io_req *)CMD_SP(sc);
2390 io_req->abts_done = NULL;
2391 goto fnic_device_reset_clean;
2392 }
2393 }
2394 } else {
2395 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002396 }
2397
2398 /* Completed, but not successful, clean up the io_req, return fail */
2399 if (status != FCPIO_SUCCESS) {
2400 spin_lock_irqsave(io_lock, flags);
2401 FNIC_SCSI_DBG(KERN_DEBUG,
2402 fnic->lport->host,
2403 "Device reset completed - failed\n");
2404 io_req = (struct fnic_io_req *)CMD_SP(sc);
2405 goto fnic_device_reset_clean;
2406 }
2407
2408 /*
2409 * Clean up any aborts on this lun that have still not
2410 * completed. If any of these fail, then LUN reset fails.
2411 * clean_pending_aborts cleans all cmds on this lun except
2412 * the lun reset cmd. If all cmds get cleaned, the lun reset
2413 * succeeds
2414 */
2415 if (fnic_clean_pending_aborts(fnic, sc)) {
2416 spin_lock_irqsave(io_lock, flags);
2417 io_req = (struct fnic_io_req *)CMD_SP(sc);
2418 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2419 "Device reset failed"
2420 " since could not abort all IOs\n");
2421 goto fnic_device_reset_clean;
2422 }
2423
2424 /* Clean lun reset command */
2425 spin_lock_irqsave(io_lock, flags);
2426 io_req = (struct fnic_io_req *)CMD_SP(sc);
2427 if (io_req)
2428 /* Completed, and successful */
2429 ret = SUCCESS;
2430
2431fnic_device_reset_clean:
2432 if (io_req)
2433 CMD_SP(sc) = NULL;
2434
2435 spin_unlock_irqrestore(io_lock, flags);
2436
2437 if (io_req) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002438 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002439 fnic_release_ioreq_buf(fnic, io_req, sc);
2440 mempool_free(io_req, fnic->io_req_pool);
2441 }
2442
2443fnic_device_reset_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08002444 FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2445 sc->request->tag, sc,
2446 jiffies_to_msecs(jiffies - start_time),
2447 0, ((u64)sc->cmnd[0] << 32 |
2448 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2449 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2450 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2451
Hiral Patel03298552013-02-12 17:00:58 -08002452 /* free tag if it is allocated */
2453 if (unlikely(tag_gen_flag))
2454 fnic_scsi_host_end_tag(fnic, sc);
2455
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002456 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2457 "Returning from device reset %s\n",
2458 (ret == SUCCESS) ?
2459 "SUCCESS" : "FAILED");
Hiral Patel67125b02013-09-12 17:45:42 -07002460
2461 if (ret == FAILED)
2462 atomic64_inc(&reset_stats->device_reset_failures);
2463
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002464 return ret;
2465}
2466
2467/* Clean up all IOs, clean up libFC local port */
2468int fnic_reset(struct Scsi_Host *shost)
2469{
2470 struct fc_lport *lp;
2471 struct fnic *fnic;
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002472 int ret = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07002473 struct reset_stats *reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002474
2475 lp = shost_priv(shost);
2476 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002477 reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002478
2479 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2480 "fnic_reset called\n");
2481
Hiral Patel67125b02013-09-12 17:45:42 -07002482 atomic64_inc(&reset_stats->fnic_resets);
2483
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002484 /*
2485 * Reset local port, this will clean up libFC exchanges,
2486 * reset remote port sessions, and if link is up, begin flogi
2487 */
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002488 ret = lp->tt.lport_reset(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002489
2490 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2491 "Returning from fnic reset %s\n",
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002492 (ret == 0) ?
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002493 "SUCCESS" : "FAILED");
2494
Hiral Patel67125b02013-09-12 17:45:42 -07002495 if (ret == 0)
2496 atomic64_inc(&reset_stats->fnic_reset_completions);
2497 else
2498 atomic64_inc(&reset_stats->fnic_reset_failures);
2499
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002500 return ret;
2501}
2502
2503/*
2504 * SCSI Error handling calls driver's eh_host_reset if all prior
2505 * error handling levels return FAILED. If host reset completes
2506 * successfully, and if link is up, then Fabric login begins.
2507 *
2508 * Host Reset is the highest level of error recovery. If this fails, then
2509 * host is offlined by SCSI.
2510 *
2511 */
2512int fnic_host_reset(struct scsi_cmnd *sc)
2513{
2514 int ret;
2515 unsigned long wait_host_tmo;
2516 struct Scsi_Host *shost = sc->device->host;
2517 struct fc_lport *lp = shost_priv(shost);
2518
2519 /*
2520 * If fnic_reset is successful, wait for fabric login to complete
2521 * scsi-ml tries to send a TUR to every device if host reset is
2522 * successful, so before returning to scsi, fabric should be up
2523 */
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002524 ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002525 if (ret == SUCCESS) {
2526 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2527 ret = FAILED;
2528 while (time_before(jiffies, wait_host_tmo)) {
2529 if ((lp->state == LPORT_ST_READY) &&
2530 (lp->link_up)) {
2531 ret = SUCCESS;
2532 break;
2533 }
2534 ssleep(1);
2535 }
2536 }
2537
2538 return ret;
2539}
2540
2541/*
2542 * This fxn is called from libFC when host is removed
2543 */
2544void fnic_scsi_abort_io(struct fc_lport *lp)
2545{
2546 int err = 0;
2547 unsigned long flags;
2548 enum fnic_state old_state;
2549 struct fnic *fnic = lport_priv(lp);
2550 DECLARE_COMPLETION_ONSTACK(remove_wait);
2551
2552 /* Issue firmware reset for fnic, wait for reset to complete */
Hiral Patel03298552013-02-12 17:00:58 -08002553retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002554 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002555 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2556 /* fw reset is in progress, poll for its completion */
2557 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2558 schedule_timeout(msecs_to_jiffies(100));
2559 goto retry_fw_reset;
2560 }
2561
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002562 fnic->remove_wait = &remove_wait;
2563 old_state = fnic->state;
2564 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002565 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002566 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2567
2568 err = fnic_fw_reset_handler(fnic);
2569 if (err) {
2570 spin_lock_irqsave(&fnic->fnic_lock, flags);
2571 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2572 fnic->state = old_state;
2573 fnic->remove_wait = NULL;
2574 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2575 return;
2576 }
2577
2578 /* Wait for firmware reset to complete */
2579 wait_for_completion_timeout(&remove_wait,
2580 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2581
2582 spin_lock_irqsave(&fnic->fnic_lock, flags);
2583 fnic->remove_wait = NULL;
2584 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2585 "fnic_scsi_abort_io %s\n",
2586 (fnic->state == FNIC_IN_ETH_MODE) ?
2587 "SUCCESS" : "FAILED");
2588 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2589
2590}
2591
2592/*
2593 * This fxn called from libFC to clean up driver IO state on link down
2594 */
2595void fnic_scsi_cleanup(struct fc_lport *lp)
2596{
2597 unsigned long flags;
2598 enum fnic_state old_state;
2599 struct fnic *fnic = lport_priv(lp);
2600
2601 /* issue fw reset */
Hiral Patel03298552013-02-12 17:00:58 -08002602retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002603 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002604 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2605 /* fw reset is in progress, poll for its completion */
2606 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2607 schedule_timeout(msecs_to_jiffies(100));
2608 goto retry_fw_reset;
2609 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002610 old_state = fnic->state;
2611 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002612 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002613 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2614
2615 if (fnic_fw_reset_handler(fnic)) {
2616 spin_lock_irqsave(&fnic->fnic_lock, flags);
2617 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2618 fnic->state = old_state;
2619 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2620 }
2621
2622}
2623
2624void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2625{
2626}
2627
2628void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2629{
2630 struct fnic *fnic = lport_priv(lp);
2631
2632 /* Non-zero sid, nothing to do */
2633 if (sid)
2634 goto call_fc_exch_mgr_reset;
2635
2636 if (did) {
2637 fnic_rport_exch_reset(fnic, did);
2638 goto call_fc_exch_mgr_reset;
2639 }
2640
2641 /*
2642 * sid = 0, did = 0
2643 * link down or device being removed
2644 */
2645 if (!fnic->in_remove)
2646 fnic_scsi_cleanup(lp);
2647 else
2648 fnic_scsi_abort_io(lp);
2649
2650 /* call libFC exch mgr reset to reset its exchanges */
2651call_fc_exch_mgr_reset:
2652 fc_exch_mgr_reset(lp, sid, did);
2653
2654}
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002655
2656/*
2657 * fnic_is_abts_pending() is a helper function that
2658 * walks through tag map to check if there is any IOs pending,if there is one,
2659 * then it returns 1 (true), otherwise 0 (false)
2660 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2661 * otherwise, it checks for all IOs.
2662 */
2663int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2664{
2665 int tag;
2666 struct fnic_io_req *io_req;
2667 spinlock_t *io_lock;
2668 unsigned long flags;
2669 int ret = 0;
2670 struct scsi_cmnd *sc;
2671 struct scsi_device *lun_dev = NULL;
2672
2673 if (lr_sc)
2674 lun_dev = lr_sc->device;
2675
2676 /* walk again to check, if IOs are still pending in fw */
Hiral Patelfc857992013-09-09 13:31:51 -07002677 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002678 sc = scsi_host_find_tag(fnic->lport->host, tag);
2679 /*
2680 * ignore this lun reset cmd or cmds that do not belong to
2681 * this lun
2682 */
2683 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2684 continue;
2685
2686 io_lock = fnic_io_lock_hash(fnic, sc);
2687 spin_lock_irqsave(io_lock, flags);
2688
2689 io_req = (struct fnic_io_req *)CMD_SP(sc);
2690
2691 if (!io_req || sc->device != lun_dev) {
2692 spin_unlock_irqrestore(io_lock, flags);
2693 continue;
2694 }
2695
2696 /*
2697 * Found IO that is still pending with firmware and
2698 * belongs to the LUN that we are resetting
2699 */
2700 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2701 "Found IO in %s on lun\n",
2702 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2703
Dan Carpenter5d65f912013-05-30 10:50:46 +03002704 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002705 ret = 1;
Dan Carpenter5d65f912013-05-30 10:50:46 +03002706 spin_unlock_irqrestore(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002707 }
2708
2709 return ret;
2710}