blob: 961bdf5d31cd9ddfb9f14fbbf2ffcbeb685482c2 [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 u8 pri_tag = 0;
329 unsigned int i;
330 unsigned long intr_flags;
331 int flags;
332 u8 exch_flags;
333 struct scsi_lun fc_lun;
334 char msg[2];
335
336 if (sg_count) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700337 /* For each SGE, create a device desc entry */
338 desc = io_req->sgl_list;
339 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
340 desc->addr = cpu_to_le64(sg_dma_address(sg));
341 desc->len = cpu_to_le32(sg_dma_len(sg));
342 desc->_resvd = 0;
343 desc++;
344 }
345
346 io_req->sgl_list_pa = pci_map_single
347 (fnic->pdev,
348 io_req->sgl_list,
349 sizeof(io_req->sgl_list[0]) * sg_count,
350 PCI_DMA_TODEVICE);
351 }
352
353 io_req->sense_buf_pa = pci_map_single(fnic->pdev,
354 sc->sense_buffer,
355 SCSI_SENSE_BUFFERSIZE,
356 PCI_DMA_FROMDEVICE);
357
358 int_to_scsilun(sc->device->lun, &fc_lun);
359
360 pri_tag = FCPIO_ICMND_PTA_SIMPLE;
361 msg[0] = MSG_SIMPLE_TAG;
362 scsi_populate_tag_msg(sc, msg);
363 if (msg[0] == MSG_ORDERED_TAG)
364 pri_tag = FCPIO_ICMND_PTA_ORDERED;
365
366 /* Enqueue the descriptor in the Copy WQ */
367 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
368
369 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
370 free_wq_copy_descs(fnic, wq);
371
372 if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
373 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800374 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
375 "fnic_queue_wq_copy_desc failure - no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -0700376 atomic64_inc(&misc_stats->io_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700377 return SCSI_MLQUEUE_HOST_BUSY;
378 }
379
380 flags = 0;
381 if (sc->sc_data_direction == DMA_FROM_DEVICE)
382 flags = FCPIO_ICMND_RDDATA;
383 else if (sc->sc_data_direction == DMA_TO_DEVICE)
384 flags = FCPIO_ICMND_WRDATA;
385
386 exch_flags = 0;
387 if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
388 (rp->flags & FC_RP_FLAGS_RETRY))
389 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
390
391 fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
392 0, exch_flags, io_req->sgl_cnt,
393 SCSI_SENSE_BUFFERSIZE,
394 io_req->sgl_list_pa,
395 io_req->sense_buf_pa,
396 0, /* scsi cmd ref, always 0 */
397 pri_tag, /* scsi pri and tag */
398 flags, /* command flags */
Abhijeet Joglekar4b536622009-10-21 16:28:25 -0700399 sc->cmnd, sc->cmd_len,
400 scsi_bufflen(sc),
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700401 fc_lun.scsi_lun, io_req->port_id,
402 rport->maxframe_size, rp->r_a_tov,
403 rp->e_d_tov);
404
Hiral Patel67125b02013-09-12 17:45:42 -0700405 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
406 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
407 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
408 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
409 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
410
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700411 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
412 return 0;
413}
414
415/*
416 * fnic_queuecommand
417 * Routine to send a scsi cdb
418 * Called with host_lock held and interrupts disabled.
419 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500420static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700421{
Hiral Patel03298552013-02-12 17:00:58 -0800422 struct fc_lport *lp = shost_priv(sc->device->host);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700423 struct fc_rport *rport;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800424 struct fnic_io_req *io_req = NULL;
Hiral Patel03298552013-02-12 17:00:58 -0800425 struct fnic *fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -0700426 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700427 struct vnic_wq_copy *wq;
428 int ret;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800429 u64 cmd_trace;
430 int sg_count = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700431 unsigned long flags;
432 unsigned long ptr;
433
Hiral Patel03298552013-02-12 17:00:58 -0800434 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
435 return SCSI_MLQUEUE_HOST_BUSY;
436
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700437 rport = starget_to_rport(scsi_target(sc->device));
438 ret = fc_remote_port_chkready(rport);
439 if (ret) {
Hiral Patel67125b02013-09-12 17:45:42 -0700440 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700441 sc->result = ret;
442 done(sc);
443 return 0;
444 }
445
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700446 if (lp->state != LPORT_ST_READY || !(lp->link_up))
447 return SCSI_MLQUEUE_HOST_BUSY;
448
Hiral Patel03298552013-02-12 17:00:58 -0800449 atomic_inc(&fnic->in_flight);
450
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700451 /*
452 * Release host lock, use driver resource specific locks from here.
453 * Don't re-enable interrupts in case they were disabled prior to the
454 * caller disabling them.
455 */
456 spin_unlock(lp->host->host_lock);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800457 CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
458 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700459
460 /* Get a new io_req for this SCSI IO */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700461 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
462 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -0700463 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700464 ret = SCSI_MLQUEUE_HOST_BUSY;
465 goto out;
466 }
467 memset(io_req, 0, sizeof(*io_req));
468
469 /* Map the data buffer */
470 sg_count = scsi_dma_map(sc);
471 if (sg_count < 0) {
Hiral Patel4d7007b2013-02-12 17:01:02 -0800472 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
473 sc->request->tag, sc, 0, sc->cmnd[0],
474 sg_count, CMD_STATE(sc));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700475 mempool_free(io_req, fnic->io_req_pool);
476 goto out;
477 }
478
479 /* Determine the type of scatter/gather list we need */
480 io_req->sgl_cnt = sg_count;
481 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
482 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
483 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
484
485 if (sg_count) {
486 io_req->sgl_list =
487 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
Abhijeet Joglekar0c79c742011-06-13 21:21:01 -0700488 GFP_ATOMIC);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700489 if (!io_req->sgl_list) {
Hiral Patel67125b02013-09-12 17:45:42 -0700490 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700491 ret = SCSI_MLQUEUE_HOST_BUSY;
492 scsi_dma_unmap(sc);
493 mempool_free(io_req, fnic->io_req_pool);
494 goto out;
495 }
496
497 /* Cache sgl list allocated address before alignment */
498 io_req->sgl_list_alloc = io_req->sgl_list;
499 ptr = (unsigned long) io_req->sgl_list;
500 if (ptr % FNIC_SG_DESC_ALIGN) {
501 io_req->sgl_list = (struct host_sg_desc *)
502 (((unsigned long) ptr
503 + FNIC_SG_DESC_ALIGN - 1)
504 & ~(FNIC_SG_DESC_ALIGN - 1));
505 }
506 }
507
508 /* initialize rest of io_req */
509 io_req->port_id = rport->port_id;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800510 io_req->start_time = jiffies;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700511 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
512 CMD_SP(sc) = (char *)io_req;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800513 CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700514 sc->scsi_done = done;
515
516 /* create copy wq desc and enqueue it */
517 wq = &fnic->wq_copy[0];
518 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
519 if (ret) {
520 /*
521 * In case another thread cancelled the request,
522 * refetch the pointer under the lock.
523 */
524 spinlock_t *io_lock = fnic_io_lock_hash(fnic, sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800525 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
526 sc->request->tag, sc, 0, 0, 0,
527 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700528 spin_lock_irqsave(io_lock, flags);
529 io_req = (struct fnic_io_req *)CMD_SP(sc);
530 CMD_SP(sc) = NULL;
531 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
532 spin_unlock_irqrestore(io_lock, flags);
533 if (io_req) {
534 fnic_release_ioreq_buf(fnic, io_req, sc);
535 mempool_free(io_req, fnic->io_req_pool);
536 }
Hiral Patel14eb5d92013-02-12 17:01:01 -0800537 } else {
Hiral Patel67125b02013-09-12 17:45:42 -0700538 atomic64_inc(&fnic_stats->io_stats.active_ios);
539 atomic64_inc(&fnic_stats->io_stats.num_ios);
540 if (atomic64_read(&fnic_stats->io_stats.active_ios) >
541 atomic64_read(&fnic_stats->io_stats.max_active_ios))
542 atomic64_set(&fnic_stats->io_stats.max_active_ios,
543 atomic64_read(&fnic_stats->io_stats.active_ios));
544
Hiral Patel14eb5d92013-02-12 17:01:01 -0800545 /* REVISIT: Use per IO lock in the final code */
546 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700547 }
548out:
Hiral Patel4d7007b2013-02-12 17:01:02 -0800549 cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
550 (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
551 (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
552 sc->cmnd[5]);
553
554 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
555 sc->request->tag, sc, io_req,
556 sg_count, cmd_trace,
557 (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -0800558 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700559 /* acquire host lock before returning to SCSI */
560 spin_lock(lp->host->host_lock);
561 return ret;
562}
563
Jeff Garzikf2812332010-11-16 02:10:29 -0500564DEF_SCSI_QCMD(fnic_queuecommand)
565
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700566/*
567 * fnic_fcpio_fw_reset_cmpl_handler
568 * Routine to handle fw reset completion
569 */
570static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
571 struct fcpio_fw_req *desc)
572{
573 u8 type;
574 u8 hdr_status;
575 struct fcpio_tag tag;
576 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700577 unsigned long flags;
Hiral Patel67125b02013-09-12 17:45:42 -0700578 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700579
580 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
581
Hiral Patel67125b02013-09-12 17:45:42 -0700582 atomic64_inc(&reset_stats->fw_reset_completions);
583
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700584 /* Clean up all outstanding io requests */
585 fnic_cleanup_io(fnic, SCSI_NO_TAG);
586
Hiral Patel67125b02013-09-12 17:45:42 -0700587 atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
588 atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
589
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700590 spin_lock_irqsave(&fnic->fnic_lock, flags);
591
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700592 /* fnic should be in FC_TRANS_ETH_MODE */
593 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
594 /* Check status of reset completion */
595 if (!hdr_status) {
596 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
597 "reset cmpl success\n");
598 /* Ready to send flogi out */
599 fnic->state = FNIC_IN_ETH_MODE;
600 } else {
601 FNIC_SCSI_DBG(KERN_DEBUG,
602 fnic->lport->host,
603 "fnic fw_reset : failed %s\n",
604 fnic_fcpio_status_to_str(hdr_status));
605
606 /*
607 * Unable to change to eth mode, cannot send out flogi
608 * Change state to fc mode, so that subsequent Flogi
609 * requests from libFC will cause more attempts to
610 * reset the firmware. Free the cached flogi
611 */
612 fnic->state = FNIC_IN_FC_MODE;
Hiral Patel67125b02013-09-12 17:45:42 -0700613 atomic64_inc(&reset_stats->fw_reset_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700614 ret = -1;
615 }
616 } else {
617 FNIC_SCSI_DBG(KERN_DEBUG,
618 fnic->lport->host,
619 "Unexpected state %s while processing"
620 " reset cmpl\n", fnic_state_to_str(fnic->state));
Hiral Patel67125b02013-09-12 17:45:42 -0700621 atomic64_inc(&reset_stats->fw_reset_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700622 ret = -1;
623 }
624
625 /* Thread removing device blocks till firmware reset is complete */
626 if (fnic->remove_wait)
627 complete(fnic->remove_wait);
628
629 /*
630 * If fnic is being removed, or fw reset failed
631 * free the flogi frame. Else, send it out
632 */
633 if (fnic->remove_wait || ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700634 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Joe Eykholt78112e52009-11-03 11:49:22 -0800635 skb_queue_purge(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700636 goto reset_cmpl_handler_end;
637 }
638
639 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
640
Joe Eykholt78112e52009-11-03 11:49:22 -0800641 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700642
643 reset_cmpl_handler_end:
Hiral Patel03298552013-02-12 17:00:58 -0800644 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
645
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700646 return ret;
647}
648
649/*
650 * fnic_fcpio_flogi_reg_cmpl_handler
651 * Routine to handle flogi register completion
652 */
653static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
654 struct fcpio_fw_req *desc)
655{
656 u8 type;
657 u8 hdr_status;
658 struct fcpio_tag tag;
659 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700660 unsigned long flags;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700661
662 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
663
664 /* Update fnic state based on status of flogi reg completion */
665 spin_lock_irqsave(&fnic->fnic_lock, flags);
666
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700667 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
668
669 /* Check flogi registration completion status */
670 if (!hdr_status) {
671 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
672 "flog reg succeeded\n");
673 fnic->state = FNIC_IN_FC_MODE;
674 } else {
675 FNIC_SCSI_DBG(KERN_DEBUG,
676 fnic->lport->host,
677 "fnic flogi reg :failed %s\n",
678 fnic_fcpio_status_to_str(hdr_status));
679 fnic->state = FNIC_IN_ETH_MODE;
680 ret = -1;
681 }
682 } else {
683 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
684 "Unexpected fnic state %s while"
685 " processing flogi reg completion\n",
686 fnic_state_to_str(fnic->state));
687 ret = -1;
688 }
689
Joe Eykholt78112e52009-11-03 11:49:22 -0800690 if (!ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700691 if (fnic->stop_rx_link_events) {
692 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
693 goto reg_cmpl_handler_end;
694 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700695 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
696
Joe Eykholt78112e52009-11-03 11:49:22 -0800697 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700698 queue_work(fnic_event_queue, &fnic->frame_work);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700699 } else {
700 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700701 }
702
703reg_cmpl_handler_end:
704 return ret;
705}
706
707static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
708 u16 request_out)
709{
710 if (wq->to_clean_index <= wq->to_use_index) {
711 /* out of range, stale request_out index */
712 if (request_out < wq->to_clean_index ||
713 request_out >= wq->to_use_index)
714 return 0;
715 } else {
716 /* out of range, stale request_out index */
717 if (request_out < wq->to_clean_index &&
718 request_out >= wq->to_use_index)
719 return 0;
720 }
721 /* request_out index is in range */
722 return 1;
723}
724
725
726/*
727 * Mark that ack received and store the Ack index. If there are multiple
728 * acks received before Tx thread cleans it up, the latest value will be
729 * used which is correct behavior. This state should be in the copy Wq
730 * instead of in the fnic
731 */
732static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
733 unsigned int cq_index,
734 struct fcpio_fw_req *desc)
735{
736 struct vnic_wq_copy *wq;
737 u16 request_out = desc->u.ack.request_out;
738 unsigned long flags;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800739 u64 *ox_id_tag = (u64 *)(void *)desc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700740
741 /* mark the ack state */
742 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
743 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
744
Hiral Patel67125b02013-09-12 17:45:42 -0700745 fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700746 if (is_ack_index_in_range(wq, request_out)) {
747 fnic->fw_ack_index[0] = request_out;
748 fnic->fw_ack_recd[0] = 1;
Hiral Patel67125b02013-09-12 17:45:42 -0700749 } else
750 atomic64_inc(
751 &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
752
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700753 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800754 FNIC_TRACE(fnic_fcpio_ack_handler,
755 fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
756 ox_id_tag[4], ox_id_tag[5]);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700757}
758
759/*
760 * fnic_fcpio_icmnd_cmpl_handler
761 * Routine to handle icmnd completions
762 */
763static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
764 struct fcpio_fw_req *desc)
765{
766 u8 type;
767 u8 hdr_status;
768 struct fcpio_tag tag;
769 u32 id;
770 u64 xfer_len = 0;
771 struct fcpio_icmnd_cmpl *icmnd_cmpl;
772 struct fnic_io_req *io_req;
773 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -0700774 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700775 unsigned long flags;
776 spinlock_t *io_lock;
Hiral Patel4d7007b2013-02-12 17:01:02 -0800777 u64 cmd_trace;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800778 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700779
780 /* Decode the cmpl description to get the io_req id */
781 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
782 fcpio_tag_id_dec(&tag, &id);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800783 icmnd_cmpl = &desc->u.icmnd_cmpl;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700784
Hiral Patelfc857992013-09-09 13:31:51 -0700785 if (id >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -0800786 shost_printk(KERN_ERR, fnic->lport->host,
787 "Tag out of range tag %x hdr status = %s\n",
788 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700789 return;
Hiral Patel03298552013-02-12 17:00:58 -0800790 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700791
792 sc = scsi_host_find_tag(fnic->lport->host, id);
793 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800794 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -0700795 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800796 shost_printk(KERN_ERR, fnic->lport->host,
797 "icmnd_cmpl sc is null - "
798 "hdr status = %s tag = 0x%x desc = 0x%p\n",
799 fnic_fcpio_status_to_str(hdr_status), id, desc);
Hiral Patel4d7007b2013-02-12 17:01:02 -0800800 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
801 fnic->lport->host->host_no, id,
802 ((u64)icmnd_cmpl->_resvd0[1] << 16 |
803 (u64)icmnd_cmpl->_resvd0[0]),
804 ((u64)hdr_status << 16 |
805 (u64)icmnd_cmpl->scsi_status << 8 |
806 (u64)icmnd_cmpl->flags), desc,
807 (u64)icmnd_cmpl->residual, 0);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700808 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800809 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700810
811 io_lock = fnic_io_lock_hash(fnic, sc);
812 spin_lock_irqsave(io_lock, flags);
813 io_req = (struct fnic_io_req *)CMD_SP(sc);
814 WARN_ON_ONCE(!io_req);
815 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -0700816 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800817 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700818 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800819 shost_printk(KERN_ERR, fnic->lport->host,
820 "icmnd_cmpl io_req is null - "
821 "hdr status = %s tag = 0x%x sc 0x%p\n",
822 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700823 return;
824 }
Hiral Patel14eb5d92013-02-12 17:01:01 -0800825 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700826
827 /* firmware completed the io */
828 io_req->io_completed = 1;
829
830 /*
831 * if SCSI-ML has already issued abort on this command,
832 * ignore completion of the IO. The abts path will clean it up
833 */
834 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
835 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800836 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
837 switch (hdr_status) {
838 case FCPIO_SUCCESS:
839 CMD_FLAGS(sc) |= FNIC_IO_DONE;
840 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
841 "icmnd_cmpl ABTS pending hdr status = %s "
842 "sc 0x%p scsi_status %x residual %d\n",
843 fnic_fcpio_status_to_str(hdr_status), sc,
844 icmnd_cmpl->scsi_status,
845 icmnd_cmpl->residual);
846 break;
847 case FCPIO_ABORTED:
848 CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
849 break;
850 default:
851 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
852 "icmnd_cmpl abts pending "
853 "hdr status = %s tag = 0x%x sc = 0x%p\n",
854 fnic_fcpio_status_to_str(hdr_status),
855 id, sc);
856 break;
857 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700858 return;
859 }
860
861 /* Mark the IO as complete */
862 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
863
864 icmnd_cmpl = &desc->u.icmnd_cmpl;
865
866 switch (hdr_status) {
867 case FCPIO_SUCCESS:
868 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
869 xfer_len = scsi_bufflen(sc);
870 scsi_set_resid(sc, icmnd_cmpl->residual);
871
872 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
873 xfer_len -= icmnd_cmpl->residual;
874
Hiral Patel67125b02013-09-12 17:45:42 -0700875 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
876 atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700877 break;
878
879 case FCPIO_TIMEOUT: /* request was timed out */
Hiral Patel67125b02013-09-12 17:45:42 -0700880 atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700881 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
882 break;
883
884 case FCPIO_ABORTED: /* request was aborted */
Hiral Patel67125b02013-09-12 17:45:42 -0700885 atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700886 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
887 break;
888
889 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
Hiral Patel67125b02013-09-12 17:45:42 -0700890 atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700891 scsi_set_resid(sc, icmnd_cmpl->residual);
892 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
893 break;
894
895 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
Hiral Patel67125b02013-09-12 17:45:42 -0700896 atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700897 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
898 break;
Hiral Patel67125b02013-09-12 17:45:42 -0700899
900 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
901 atomic64_inc(&fnic_stats->io_stats.io_not_found);
902 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
903 break;
904
905 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
906 atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
907 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
908 break;
909
910 case FCPIO_FW_ERR: /* request was terminated due fw error */
911 atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
912 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
913 break;
914
915 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
916 atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
917 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
918 break;
919
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700920 case FCPIO_INVALID_HEADER: /* header contains invalid data */
921 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
922 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700923 default:
924 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
925 fnic_fcpio_status_to_str(hdr_status));
926 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
927 break;
928 }
929
Hiral Patel67125b02013-09-12 17:45:42 -0700930 if (hdr_status != FCPIO_SUCCESS) {
931 atomic64_inc(&fnic_stats->io_stats.io_failures);
932 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
933 fnic_fcpio_status_to_str(hdr_status));
934 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700935 /* Break link with the SCSI command */
936 CMD_SP(sc) = NULL;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800937 CMD_FLAGS(sc) |= FNIC_IO_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700938
939 spin_unlock_irqrestore(io_lock, flags);
940
941 fnic_release_ioreq_buf(fnic, io_req, sc);
942
943 mempool_free(io_req, fnic->io_req_pool);
944
Hiral Patel4d7007b2013-02-12 17:01:02 -0800945 cmd_trace = ((u64)hdr_status << 56) |
946 (u64)icmnd_cmpl->scsi_status << 48 |
947 (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
948 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
949 (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
950
951 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
952 sc->device->host->host_no, id, sc,
953 ((u64)icmnd_cmpl->_resvd0[1] << 56 |
954 (u64)icmnd_cmpl->_resvd0[0] << 48 |
955 jiffies_to_msecs(jiffies - start_time)),
956 desc, cmd_trace,
957 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
958
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700959 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
960 fnic->lport->host_stats.fcp_input_requests++;
961 fnic->fcp_input_bytes += xfer_len;
962 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
963 fnic->lport->host_stats.fcp_output_requests++;
964 fnic->fcp_output_bytes += xfer_len;
965 } else
966 fnic->lport->host_stats.fcp_control_requests++;
967
Hiral Patel67125b02013-09-12 17:45:42 -0700968 atomic64_dec(&fnic_stats->io_stats.active_ios);
969 if (atomic64_read(&fnic->io_cmpl_skip))
970 atomic64_dec(&fnic->io_cmpl_skip);
971 else
972 atomic64_inc(&fnic_stats->io_stats.io_completions);
973
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700974 /* Call SCSI completion function to complete the IO */
975 if (sc->scsi_done)
976 sc->scsi_done(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700977}
978
979/* fnic_fcpio_itmf_cmpl_handler
980 * Routine to handle itmf completions
981 */
982static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
983 struct fcpio_fw_req *desc)
984{
985 u8 type;
986 u8 hdr_status;
987 struct fcpio_tag tag;
988 u32 id;
989 struct scsi_cmnd *sc;
990 struct fnic_io_req *io_req;
Hiral Patel67125b02013-09-12 17:45:42 -0700991 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
992 struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
993 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
994 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700995 unsigned long flags;
996 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800997 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700998
999 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1000 fcpio_tag_id_dec(&tag, &id);
1001
Hiral Patelfc857992013-09-09 13:31:51 -07001002 if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
Hiral Patel03298552013-02-12 17:00:58 -08001003 shost_printk(KERN_ERR, fnic->lport->host,
1004 "Tag out of range tag %x hdr status = %s\n",
1005 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001006 return;
Hiral Patel03298552013-02-12 17:00:58 -08001007 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001008
1009 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1010 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001011 if (!sc) {
Hiral Patel67125b02013-09-12 17:45:42 -07001012 atomic64_inc(&fnic_stats->io_stats.sc_null);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001013 shost_printk(KERN_ERR, fnic->lport->host,
1014 "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1015 fnic_fcpio_status_to_str(hdr_status), id);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001016 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001017 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001018 io_lock = fnic_io_lock_hash(fnic, sc);
1019 spin_lock_irqsave(io_lock, flags);
1020 io_req = (struct fnic_io_req *)CMD_SP(sc);
1021 WARN_ON_ONCE(!io_req);
1022 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001023 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001024 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001025 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1026 shost_printk(KERN_ERR, fnic->lport->host,
1027 "itmf_cmpl io_req is null - "
1028 "hdr status = %s tag = 0x%x sc 0x%p\n",
1029 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001030 return;
1031 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001032 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001033
Hiral Patel03298552013-02-12 17:00:58 -08001034 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1035 /* Abort and terminate completion of device reset req */
1036 /* REVISIT : Add asserts about various flags */
1037 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1038 "dev reset abts cmpl recd. id %x status %s\n",
1039 id, fnic_fcpio_status_to_str(hdr_status));
1040 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1041 CMD_ABTS_STATUS(sc) = hdr_status;
1042 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1043 if (io_req->abts_done)
1044 complete(io_req->abts_done);
1045 spin_unlock_irqrestore(io_lock, flags);
1046 } else if (id & FNIC_TAG_ABORT) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001047 /* Completion of abort cmd */
Hiral Patel67125b02013-09-12 17:45:42 -07001048 switch (hdr_status) {
1049 case FCPIO_SUCCESS:
1050 break;
1051 case FCPIO_TIMEOUT:
1052 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1053 atomic64_inc(&abts_stats->abort_fw_timeouts);
1054 else
1055 atomic64_inc(
1056 &term_stats->terminate_fw_timeouts);
1057 break;
1058 case FCPIO_IO_NOT_FOUND:
1059 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1060 atomic64_inc(&abts_stats->abort_io_not_found);
1061 else
1062 atomic64_inc(
1063 &term_stats->terminate_io_not_found);
1064 break;
1065 default:
1066 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1067 atomic64_inc(&abts_stats->abort_failures);
1068 else
1069 atomic64_inc(
1070 &term_stats->terminate_failures);
1071 break;
1072 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001073 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1074 /* This is a late completion. Ignore it */
1075 spin_unlock_irqrestore(io_lock, flags);
1076 return;
1077 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001078 CMD_ABTS_STATUS(sc) = hdr_status;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001079 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Hiral Patel67125b02013-09-12 17:45:42 -07001080
1081 atomic64_dec(&fnic_stats->io_stats.active_ios);
1082 if (atomic64_read(&fnic->io_cmpl_skip))
1083 atomic64_dec(&fnic->io_cmpl_skip);
1084 else
1085 atomic64_inc(&fnic_stats->io_stats.io_completions);
1086
1087 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1088 atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1089
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001090 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1091 "abts cmpl recd. id %d status %s\n",
1092 (int)(id & FNIC_TAG_MASK),
1093 fnic_fcpio_status_to_str(hdr_status));
1094
1095 /*
1096 * If scsi_eh thread is blocked waiting for abts to complete,
1097 * signal completion to it. IO will be cleaned in the thread
1098 * else clean it in this context
1099 */
1100 if (io_req->abts_done) {
1101 complete(io_req->abts_done);
1102 spin_unlock_irqrestore(io_lock, flags);
1103 } else {
1104 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1105 "abts cmpl, completing IO\n");
1106 CMD_SP(sc) = NULL;
1107 sc->result = (DID_ERROR << 16);
1108
1109 spin_unlock_irqrestore(io_lock, flags);
1110
1111 fnic_release_ioreq_buf(fnic, io_req, sc);
1112 mempool_free(io_req, fnic->io_req_pool);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001113 if (sc->scsi_done) {
1114 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1115 sc->device->host->host_no, id,
1116 sc,
1117 jiffies_to_msecs(jiffies - start_time),
1118 desc,
1119 (((u64)hdr_status << 40) |
1120 (u64)sc->cmnd[0] << 32 |
1121 (u64)sc->cmnd[2] << 24 |
1122 (u64)sc->cmnd[3] << 16 |
1123 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1124 (((u64)CMD_FLAGS(sc) << 32) |
1125 CMD_STATE(sc)));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001126 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001127 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001128 }
1129
1130 } else if (id & FNIC_TAG_DEV_RST) {
1131 /* Completion of device reset */
1132 CMD_LR_STATUS(sc) = hdr_status;
Hiral Patel03298552013-02-12 17:00:58 -08001133 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1134 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001135 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001136 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1137 sc->device->host->host_no, id, sc,
1138 jiffies_to_msecs(jiffies - start_time),
1139 desc, 0,
1140 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001141 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1142 "Terminate pending "
1143 "dev reset cmpl recd. id %d status %s\n",
1144 (int)(id & FNIC_TAG_MASK),
1145 fnic_fcpio_status_to_str(hdr_status));
1146 return;
1147 }
1148 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1149 /* Need to wait for terminate completion */
1150 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001151 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1152 sc->device->host->host_no, id, sc,
1153 jiffies_to_msecs(jiffies - start_time),
1154 desc, 0,
1155 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
Hiral Patel03298552013-02-12 17:00:58 -08001156 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1157 "dev reset cmpl recd after time out. "
1158 "id %d status %s\n",
1159 (int)(id & FNIC_TAG_MASK),
1160 fnic_fcpio_status_to_str(hdr_status));
1161 return;
1162 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001163 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
Hiral Patel03298552013-02-12 17:00:58 -08001164 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001165 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1166 "dev reset cmpl recd. id %d status %s\n",
1167 (int)(id & FNIC_TAG_MASK),
1168 fnic_fcpio_status_to_str(hdr_status));
1169 if (io_req->dr_done)
1170 complete(io_req->dr_done);
1171 spin_unlock_irqrestore(io_lock, flags);
1172
1173 } else {
1174 shost_printk(KERN_ERR, fnic->lport->host,
1175 "Unexpected itmf io state %s tag %x\n",
1176 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1177 spin_unlock_irqrestore(io_lock, flags);
1178 }
1179
1180}
1181
1182/*
1183 * fnic_fcpio_cmpl_handler
1184 * Routine to service the cq for wq_copy
1185 */
1186static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1187 unsigned int cq_index,
1188 struct fcpio_fw_req *desc)
1189{
1190 struct fnic *fnic = vnic_dev_priv(vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001191
1192 switch (desc->hdr.type) {
Hiral Patel67125b02013-09-12 17:45:42 -07001193 case FCPIO_ICMND_CMPL: /* fw completed a command */
1194 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1195 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1196 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1197 case FCPIO_RESET_CMPL: /* fw completed reset */
1198 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1199 break;
1200 default:
1201 break;
1202 }
1203
1204 switch (desc->hdr.type) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001205 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1206 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1207 break;
1208
1209 case FCPIO_ICMND_CMPL: /* fw completed a command */
1210 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1211 break;
1212
1213 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1214 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1215 break;
1216
1217 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
Joe Eykholt78112e52009-11-03 11:49:22 -08001218 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
Hiral Patel03298552013-02-12 17:00:58 -08001219 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001220 break;
1221
1222 case FCPIO_RESET_CMPL: /* fw completed reset */
Hiral Patel03298552013-02-12 17:00:58 -08001223 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001224 break;
1225
1226 default:
1227 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1228 "firmware completion type %d\n",
1229 desc->hdr.type);
1230 break;
1231 }
1232
Hiral Patel03298552013-02-12 17:00:58 -08001233 return 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001234}
1235
1236/*
1237 * fnic_wq_copy_cmpl_handler
1238 * Routine to process wq copy
1239 */
1240int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1241{
1242 unsigned int wq_work_done = 0;
1243 unsigned int i, cq_index;
1244 unsigned int cur_work_done;
1245
1246 for (i = 0; i < fnic->wq_copy_count; i++) {
1247 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1248 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1249 fnic_fcpio_cmpl_handler,
1250 copy_work_to_do);
1251 wq_work_done += cur_work_done;
1252 }
1253 return wq_work_done;
1254}
1255
1256static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1257{
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001258 int i;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001259 struct fnic_io_req *io_req;
1260 unsigned long flags = 0;
1261 struct scsi_cmnd *sc;
1262 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001263 unsigned long start_time = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07001264 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001265
Hiral Patelfc857992013-09-09 13:31:51 -07001266 for (i = 0; i < fnic->fnic_max_tag_id; i++) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001267 if (i == exclude_id)
1268 continue;
1269
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001270 io_lock = fnic_io_lock_tag(fnic, i);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001271 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001272 sc = scsi_host_find_tag(fnic->lport->host, i);
1273 if (!sc) {
1274 spin_unlock_irqrestore(io_lock, flags);
1275 continue;
1276 }
1277
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001278 io_req = (struct fnic_io_req *)CMD_SP(sc);
Hiral Patel03298552013-02-12 17:00:58 -08001279 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1280 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1281 /*
1282 * We will be here only when FW completes reset
1283 * without sending completions for outstanding ios.
1284 */
1285 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1286 if (io_req && io_req->dr_done)
1287 complete(io_req->dr_done);
1288 else if (io_req && io_req->abts_done)
1289 complete(io_req->abts_done);
1290 spin_unlock_irqrestore(io_lock, flags);
1291 continue;
1292 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1293 spin_unlock_irqrestore(io_lock, flags);
1294 continue;
1295 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001296 if (!io_req) {
1297 spin_unlock_irqrestore(io_lock, flags);
1298 goto cleanup_scsi_cmd;
1299 }
1300
1301 CMD_SP(sc) = NULL;
1302
1303 spin_unlock_irqrestore(io_lock, flags);
1304
1305 /*
1306 * If there is a scsi_cmnd associated with this io_req, then
1307 * free the corresponding state
1308 */
Hiral Patel14eb5d92013-02-12 17:01:01 -08001309 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001310 fnic_release_ioreq_buf(fnic, io_req, sc);
1311 mempool_free(io_req, fnic->io_req_pool);
1312
1313cleanup_scsi_cmd:
1314 sc->result = DID_TRANSPORT_DISRUPTED << 16;
Hiral Shah66818662014-04-18 12:28:18 -07001315 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1316 "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1317 __func__, (jiffies - start_time));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001318
Hiral Patel67125b02013-09-12 17:45:42 -07001319 if (atomic64_read(&fnic->io_cmpl_skip))
1320 atomic64_dec(&fnic->io_cmpl_skip);
1321 else
1322 atomic64_inc(&fnic_stats->io_stats.io_completions);
1323
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001324 /* Complete the command to SCSI */
Hiral Patel4d7007b2013-02-12 17:01:02 -08001325 if (sc->scsi_done) {
1326 FNIC_TRACE(fnic_cleanup_io,
1327 sc->device->host->host_no, i, sc,
1328 jiffies_to_msecs(jiffies - start_time),
1329 0, ((u64)sc->cmnd[0] << 32 |
1330 (u64)sc->cmnd[2] << 24 |
1331 (u64)sc->cmnd[3] << 16 |
1332 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1333 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1334
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001335 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001336 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001337 }
1338}
1339
1340void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1341 struct fcpio_host_req *desc)
1342{
1343 u32 id;
1344 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1345 struct fnic_io_req *io_req;
1346 struct scsi_cmnd *sc;
1347 unsigned long flags;
1348 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001349 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001350
1351 /* get the tag reference */
1352 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1353 id &= FNIC_TAG_MASK;
1354
Hiral Patelfc857992013-09-09 13:31:51 -07001355 if (id >= fnic->fnic_max_tag_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001356 return;
1357
1358 sc = scsi_host_find_tag(fnic->lport->host, id);
1359 if (!sc)
1360 return;
1361
1362 io_lock = fnic_io_lock_hash(fnic, sc);
1363 spin_lock_irqsave(io_lock, flags);
1364
1365 /* Get the IO context which this desc refers to */
1366 io_req = (struct fnic_io_req *)CMD_SP(sc);
1367
1368 /* fnic interrupts are turned off by now */
1369
1370 if (!io_req) {
1371 spin_unlock_irqrestore(io_lock, flags);
1372 goto wq_copy_cleanup_scsi_cmd;
1373 }
1374
1375 CMD_SP(sc) = NULL;
1376
1377 spin_unlock_irqrestore(io_lock, flags);
1378
Hiral Patel14eb5d92013-02-12 17:01:01 -08001379 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001380 fnic_release_ioreq_buf(fnic, io_req, sc);
1381 mempool_free(io_req, fnic->io_req_pool);
1382
1383wq_copy_cleanup_scsi_cmd:
1384 sc->result = DID_NO_CONNECT << 16;
1385 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1386 " DID_NO_CONNECT\n");
1387
Hiral Patel4d7007b2013-02-12 17:01:02 -08001388 if (sc->scsi_done) {
1389 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1390 sc->device->host->host_no, id, sc,
1391 jiffies_to_msecs(jiffies - start_time),
1392 0, ((u64)sc->cmnd[0] << 32 |
1393 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1394 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1395 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1396
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001397 sc->scsi_done(sc);
Hiral Patel4d7007b2013-02-12 17:01:02 -08001398 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001399}
1400
1401static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1402 u32 task_req, u8 *fc_lun,
1403 struct fnic_io_req *io_req)
1404{
1405 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001406 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001407 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001408 unsigned long flags;
1409
Hiral Patel03298552013-02-12 17:00:58 -08001410 spin_lock_irqsave(host->host_lock, flags);
1411 if (unlikely(fnic_chk_state_flags_locked(fnic,
1412 FNIC_FLAGS_IO_BLOCKED))) {
1413 spin_unlock_irqrestore(host->host_lock, flags);
1414 return 1;
1415 } else
1416 atomic_inc(&fnic->in_flight);
1417 spin_unlock_irqrestore(host->host_lock, flags);
1418
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001419 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1420
1421 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1422 free_wq_copy_descs(fnic, wq);
1423
1424 if (!vnic_wq_copy_desc_avail(wq)) {
1425 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001426 atomic_dec(&fnic->in_flight);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001427 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001428 "fnic_queue_abort_io_req: failure: no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07001429 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001430 return 1;
1431 }
1432 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1433 0, task_req, tag, fc_lun, io_req->port_id,
1434 fnic->config.ra_tov, fnic->config.ed_tov);
1435
Hiral Patel67125b02013-09-12 17:45:42 -07001436 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1437 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1438 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1439 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1440 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1441
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001442 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001443 atomic_dec(&fnic->in_flight);
1444
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001445 return 0;
1446}
1447
Hiral Patel03298552013-02-12 17:00:58 -08001448static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001449{
1450 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001451 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001452 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001453 struct fnic_io_req *io_req;
1454 spinlock_t *io_lock;
1455 unsigned long flags;
1456 struct scsi_cmnd *sc;
Hiral Patel67125b02013-09-12 17:45:42 -07001457 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1458 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001459 struct scsi_lun fc_lun;
1460 enum fnic_ioreq_state old_ioreq_state;
1461
1462 FNIC_SCSI_DBG(KERN_DEBUG,
1463 fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001464 "fnic_rport_exch_reset called portid 0x%06x\n",
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001465 port_id);
1466
1467 if (fnic->in_remove)
1468 return;
1469
Hiral Patelfc857992013-09-09 13:31:51 -07001470 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001471 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001472 io_lock = fnic_io_lock_tag(fnic, tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001473 spin_lock_irqsave(io_lock, flags);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001474 sc = scsi_host_find_tag(fnic->lport->host, tag);
1475 if (!sc) {
1476 spin_unlock_irqrestore(io_lock, flags);
1477 continue;
1478 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001479
1480 io_req = (struct fnic_io_req *)CMD_SP(sc);
1481
1482 if (!io_req || io_req->port_id != port_id) {
1483 spin_unlock_irqrestore(io_lock, flags);
1484 continue;
1485 }
1486
Hiral Patel03298552013-02-12 17:00:58 -08001487 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001488 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001489 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1490 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1491 sc);
1492 spin_unlock_irqrestore(io_lock, flags);
1493 continue;
1494 }
1495
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001496 /*
1497 * Found IO that is still pending with firmware and
1498 * belongs to rport that went away
1499 */
1500 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1501 spin_unlock_irqrestore(io_lock, flags);
1502 continue;
1503 }
Hiral Patel03298552013-02-12 17:00:58 -08001504 if (io_req->abts_done) {
1505 shost_printk(KERN_ERR, fnic->lport->host,
1506 "fnic_rport_exch_reset: io_req->abts_done is set "
1507 "state is %s\n",
1508 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1509 }
1510
Hiral Patel14eb5d92013-02-12 17:01:01 -08001511 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1512 shost_printk(KERN_ERR, fnic->lport->host,
1513 "rport_exch_reset "
1514 "IO not yet issued %p tag 0x%x flags "
1515 "%x state %d\n",
1516 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1517 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001518 old_ioreq_state = CMD_STATE(sc);
1519 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1520 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001521 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001522 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001523 abt_tag = (tag | FNIC_TAG_DEV_RST);
1524 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1525 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1526 sc);
1527 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001528
1529 BUG_ON(io_req->abts_done);
1530
1531 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1532 "fnic_rport_reset_exch: Issuing abts\n");
1533
1534 spin_unlock_irqrestore(io_lock, flags);
1535
1536 /* Now queue the abort command to firmware */
1537 int_to_scsilun(sc->device->lun, &fc_lun);
1538
Hiral Patel03298552013-02-12 17:00:58 -08001539 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001540 FCPIO_ITMF_ABT_TASK_TERM,
1541 fc_lun.scsi_lun, io_req)) {
1542 /*
1543 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001544 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001545 * aborted later by scsi_eh, or cleaned up during
1546 * lun reset
1547 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001548 spin_lock_irqsave(io_lock, flags);
1549 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1550 CMD_STATE(sc) = old_ioreq_state;
1551 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001552 } else {
1553 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001554 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1555 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001556 else
1557 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001558 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001559 atomic64_inc(&term_stats->terminates);
1560 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001561 }
1562 }
Hiral Patel67125b02013-09-12 17:45:42 -07001563 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1564 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001565
1566}
1567
1568void fnic_terminate_rport_io(struct fc_rport *rport)
1569{
1570 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001571 int abt_tag;
Hiral Patel67125b02013-09-12 17:45:42 -07001572 int term_cnt = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001573 struct fnic_io_req *io_req;
1574 spinlock_t *io_lock;
1575 unsigned long flags;
1576 struct scsi_cmnd *sc;
1577 struct scsi_lun fc_lun;
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001578 struct fc_rport_libfc_priv *rdata;
1579 struct fc_lport *lport;
1580 struct fnic *fnic;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001581 struct fc_rport *cmd_rport;
Hiral Patel67125b02013-09-12 17:45:42 -07001582 struct reset_stats *reset_stats;
1583 struct terminate_stats *term_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001584 enum fnic_ioreq_state old_ioreq_state;
1585
Sesidhar Beddeld0385d92013-09-09 13:31:50 -07001586 if (!rport) {
1587 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1588 return;
1589 }
1590 rdata = rport->dd_data;
1591
1592 if (!rdata) {
1593 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1594 return;
1595 }
1596 lport = rdata->local_port;
1597
1598 if (!lport) {
1599 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1600 return;
1601 }
1602 fnic = lport_priv(lport);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001603 FNIC_SCSI_DBG(KERN_DEBUG,
1604 fnic->lport->host, "fnic_terminate_rport_io called"
Hiral Patel03298552013-02-12 17:00:58 -08001605 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1606 rport->port_name, rport->node_name, rport,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001607 rport->port_id);
1608
1609 if (fnic->in_remove)
1610 return;
1611
Hiral Patel67125b02013-09-12 17:45:42 -07001612 reset_stats = &fnic->fnic_stats.reset_stats;
1613 term_stats = &fnic->fnic_stats.term_stats;
1614
Hiral Patelfc857992013-09-09 13:31:51 -07001615 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001616 abt_tag = tag;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001617 io_lock = fnic_io_lock_tag(fnic, tag);
1618 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001619 sc = scsi_host_find_tag(fnic->lport->host, tag);
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001620 if (!sc) {
1621 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001622 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001623 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001624
1625 cmd_rport = starget_to_rport(scsi_target(sc->device));
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001626 if (rport != cmd_rport) {
1627 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001628 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001629 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001630
1631 io_req = (struct fnic_io_req *)CMD_SP(sc);
1632
1633 if (!io_req || rport != cmd_rport) {
1634 spin_unlock_irqrestore(io_lock, flags);
1635 continue;
1636 }
1637
Hiral Patel03298552013-02-12 17:00:58 -08001638 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001639 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001640 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1641 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1642 sc);
1643 spin_unlock_irqrestore(io_lock, flags);
1644 continue;
1645 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001646 /*
1647 * Found IO that is still pending with firmware and
1648 * belongs to rport that went away
1649 */
1650 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1651 spin_unlock_irqrestore(io_lock, flags);
1652 continue;
1653 }
Hiral Patel03298552013-02-12 17:00:58 -08001654 if (io_req->abts_done) {
1655 shost_printk(KERN_ERR, fnic->lport->host,
1656 "fnic_terminate_rport_io: io_req->abts_done is set "
1657 "state is %s\n",
1658 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1659 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001660 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1661 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1662 "fnic_terminate_rport_io "
1663 "IO not yet issued %p tag 0x%x flags "
1664 "%x state %d\n",
1665 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1666 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001667 old_ioreq_state = CMD_STATE(sc);
1668 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1669 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001670 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
Hiral Patel67125b02013-09-12 17:45:42 -07001671 atomic64_inc(&reset_stats->device_reset_terminates);
Hiral Patel03298552013-02-12 17:00:58 -08001672 abt_tag = (tag | FNIC_TAG_DEV_RST);
1673 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1674 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1675 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001676
1677 BUG_ON(io_req->abts_done);
1678
1679 FNIC_SCSI_DBG(KERN_DEBUG,
1680 fnic->lport->host,
1681 "fnic_terminate_rport_io: Issuing abts\n");
1682
1683 spin_unlock_irqrestore(io_lock, flags);
1684
1685 /* Now queue the abort command to firmware */
1686 int_to_scsilun(sc->device->lun, &fc_lun);
1687
Hiral Patel03298552013-02-12 17:00:58 -08001688 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001689 FCPIO_ITMF_ABT_TASK_TERM,
1690 fc_lun.scsi_lun, io_req)) {
1691 /*
1692 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001693 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001694 * aborted later by scsi_eh, or cleaned up during
1695 * lun reset
1696 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001697 spin_lock_irqsave(io_lock, flags);
1698 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1699 CMD_STATE(sc) = old_ioreq_state;
1700 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001701 } else {
1702 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001703 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1704 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001705 else
1706 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001707 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001708 atomic64_inc(&term_stats->terminates);
1709 term_cnt++;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001710 }
1711 }
Hiral Patel67125b02013-09-12 17:45:42 -07001712 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1713 atomic64_set(&term_stats->max_terminates, term_cnt);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001714
1715}
1716
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001717/*
1718 * This function is exported to SCSI for sending abort cmnds.
1719 * A SCSI IO is represented by a io_req in the driver.
1720 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1721 */
1722int fnic_abort_cmd(struct scsi_cmnd *sc)
1723{
1724 struct fc_lport *lp;
1725 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08001726 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001727 struct fc_rport *rport;
1728 spinlock_t *io_lock;
1729 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001730 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001731 int ret = SUCCESS;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001732 u32 task_req = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001733 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07001734 struct fnic_stats *fnic_stats;
1735 struct abort_stats *abts_stats;
1736 struct terminate_stats *term_stats;
Hiral Shah66818662014-04-18 12:28:18 -07001737 enum fnic_ioreq_state old_ioreq_state;
Hiral Patel03298552013-02-12 17:00:58 -08001738 int tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001739 DECLARE_COMPLETION_ONSTACK(tm_done);
1740
1741 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001742 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001743
1744 /* Get local-port, check ready and link up */
1745 lp = shost_priv(sc->device->host);
1746
1747 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07001748 fnic_stats = &fnic->fnic_stats;
1749 abts_stats = &fnic->fnic_stats.abts_stats;
1750 term_stats = &fnic->fnic_stats.term_stats;
1751
Roel Kluin0db6f432010-06-11 16:44:46 -07001752 rport = starget_to_rport(scsi_target(sc->device));
Hiral Patel03298552013-02-12 17:00:58 -08001753 tag = sc->request->tag;
1754 FNIC_SCSI_DBG(KERN_DEBUG,
1755 fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001756 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
Hiral Patel03298552013-02-12 17:00:58 -08001757 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1758
1759 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1760
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001761 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1762 ret = FAILED;
1763 goto fnic_abort_cmd_end;
1764 }
1765
1766 /*
1767 * Avoid a race between SCSI issuing the abort and the device
1768 * completing the command.
1769 *
1770 * If the command is already completed by the fw cmpl code,
1771 * we just return SUCCESS from here. This means that the abort
1772 * succeeded. In the SCSI ML, since the timeout for command has
1773 * happened, the completion wont actually complete the command
1774 * and it will be considered as an aborted command
1775 *
1776 * The CMD_SP will not be cleared except while holding io_req_lock.
1777 */
1778 io_lock = fnic_io_lock_hash(fnic, sc);
1779 spin_lock_irqsave(io_lock, flags);
1780 io_req = (struct fnic_io_req *)CMD_SP(sc);
1781 if (!io_req) {
1782 spin_unlock_irqrestore(io_lock, flags);
1783 goto fnic_abort_cmd_end;
1784 }
1785
1786 io_req->abts_done = &tm_done;
1787
1788 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1789 spin_unlock_irqrestore(io_lock, flags);
1790 goto wait_pending;
1791 }
1792 /*
1793 * Command is still pending, need to abort it
1794 * If the firmware completes the command after this point,
1795 * the completion wont be done till mid-layer, since abort
1796 * has already started.
1797 */
Hiral Shah66818662014-04-18 12:28:18 -07001798 old_ioreq_state = CMD_STATE(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001799 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1800 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1801
1802 spin_unlock_irqrestore(io_lock, flags);
1803
1804 /*
1805 * Check readiness of the remote port. If the path to remote
1806 * port is up, then send abts to the remote port to terminate
1807 * the IO. Else, just locally terminate the IO in the firmware
1808 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001809 if (fc_remote_port_chkready(rport) == 0)
1810 task_req = FCPIO_ITMF_ABT_TASK;
Hiral Patel67125b02013-09-12 17:45:42 -07001811 else {
1812 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001813 task_req = FCPIO_ITMF_ABT_TASK_TERM;
Hiral Patel67125b02013-09-12 17:45:42 -07001814 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001815
1816 /* Now queue the abort command to firmware */
1817 int_to_scsilun(sc->device->lun, &fc_lun);
1818
1819 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1820 fc_lun.scsi_lun, io_req)) {
1821 spin_lock_irqsave(io_lock, flags);
Hiral Shah66818662014-04-18 12:28:18 -07001822 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1823 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001824 io_req = (struct fnic_io_req *)CMD_SP(sc);
1825 if (io_req)
1826 io_req->abts_done = NULL;
1827 spin_unlock_irqrestore(io_lock, flags);
1828 ret = FAILED;
1829 goto fnic_abort_cmd_end;
1830 }
Hiral Patel67125b02013-09-12 17:45:42 -07001831 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001832 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001833 atomic64_inc(&fnic_stats->abts_stats.aborts);
1834 } else {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001835 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
Hiral Patel67125b02013-09-12 17:45:42 -07001836 atomic64_inc(&fnic_stats->term_stats.terminates);
1837 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001838
1839 /*
1840 * We queued an abort IO, wait for its completion.
1841 * Once the firmware completes the abort command, it will
1842 * wake up this thread.
1843 */
1844 wait_pending:
1845 wait_for_completion_timeout(&tm_done,
1846 msecs_to_jiffies
1847 (2 * fnic->config.ra_tov +
1848 fnic->config.ed_tov));
1849
1850 /* Check the abort status */
1851 spin_lock_irqsave(io_lock, flags);
1852
1853 io_req = (struct fnic_io_req *)CMD_SP(sc);
1854 if (!io_req) {
Hiral Patel67125b02013-09-12 17:45:42 -07001855 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001856 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001857 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001858 ret = FAILED;
1859 goto fnic_abort_cmd_end;
1860 }
1861 io_req->abts_done = NULL;
1862
1863 /* fw did not complete abort, timed out */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001864 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001865 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001866 if (task_req == FCPIO_ITMF_ABT_TASK) {
Hiral Patel67125b02013-09-12 17:45:42 -07001867 atomic64_inc(&abts_stats->abort_drv_timeouts);
1868 } else {
Hiral Patel67125b02013-09-12 17:45:42 -07001869 atomic64_inc(&term_stats->terminate_drv_timeouts);
1870 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001871 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001872 ret = FAILED;
1873 goto fnic_abort_cmd_end;
1874 }
1875
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001876 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1877
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001878 /*
1879 * firmware completed the abort, check the status,
1880 * free the io_req irrespective of failure or success
1881 */
1882 if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1883 ret = FAILED;
1884
1885 CMD_SP(sc) = NULL;
1886
1887 spin_unlock_irqrestore(io_lock, flags);
1888
Hiral Patel14eb5d92013-02-12 17:01:01 -08001889 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001890 fnic_release_ioreq_buf(fnic, io_req, sc);
1891 mempool_free(io_req, fnic->io_req_pool);
1892
1893fnic_abort_cmd_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08001894 FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1895 sc->request->tag, sc,
1896 jiffies_to_msecs(jiffies - start_time),
1897 0, ((u64)sc->cmnd[0] << 32 |
1898 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1899 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1900 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1901
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001902 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel14eb5d92013-02-12 17:01:01 -08001903 "Returning from abort cmd type %x %s\n", task_req,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001904 (ret == SUCCESS) ?
1905 "SUCCESS" : "FAILED");
1906 return ret;
1907}
1908
1909static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1910 struct scsi_cmnd *sc,
1911 struct fnic_io_req *io_req)
1912{
1913 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001914 struct Scsi_Host *host = fnic->lport->host;
Hiral Patel67125b02013-09-12 17:45:42 -07001915 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001916 struct scsi_lun fc_lun;
1917 int ret = 0;
1918 unsigned long intr_flags;
1919
Hiral Patel03298552013-02-12 17:00:58 -08001920 spin_lock_irqsave(host->host_lock, intr_flags);
1921 if (unlikely(fnic_chk_state_flags_locked(fnic,
1922 FNIC_FLAGS_IO_BLOCKED))) {
1923 spin_unlock_irqrestore(host->host_lock, intr_flags);
1924 return FAILED;
1925 } else
1926 atomic_inc(&fnic->in_flight);
1927 spin_unlock_irqrestore(host->host_lock, intr_flags);
1928
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001929 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1930
1931 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1932 free_wq_copy_descs(fnic, wq);
1933
1934 if (!vnic_wq_copy_desc_avail(wq)) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001935 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1936 "queue_dr_io_req failure - no descriptors\n");
Hiral Patel67125b02013-09-12 17:45:42 -07001937 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001938 ret = -EAGAIN;
1939 goto lr_io_req_end;
1940 }
1941
1942 /* fill in the lun info */
1943 int_to_scsilun(sc->device->lun, &fc_lun);
1944
1945 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1946 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1947 fc_lun.scsi_lun, io_req->port_id,
1948 fnic->config.ra_tov, fnic->config.ed_tov);
1949
Hiral Patel67125b02013-09-12 17:45:42 -07001950 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1951 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1952 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1953 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1954 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1955
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001956lr_io_req_end:
1957 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel03298552013-02-12 17:00:58 -08001958 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001959
1960 return ret;
1961}
1962
1963/*
1964 * Clean up any pending aborts on the lun
1965 * For each outstanding IO on this lun, whose abort is not completed by fw,
1966 * issue a local abort. Wait for abort to complete. Return 0 if all commands
1967 * successfully aborted, 1 otherwise
1968 */
1969static int fnic_clean_pending_aborts(struct fnic *fnic,
1970 struct scsi_cmnd *lr_sc)
1971{
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001972 int tag, abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001973 struct fnic_io_req *io_req;
1974 spinlock_t *io_lock;
1975 unsigned long flags;
1976 int ret = 0;
1977 struct scsi_cmnd *sc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001978 struct scsi_lun fc_lun;
1979 struct scsi_device *lun_dev = lr_sc->device;
1980 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001981 enum fnic_ioreq_state old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001982
Hiral Patelfc857992013-09-09 13:31:51 -07001983 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001984 io_lock = fnic_io_lock_tag(fnic, tag);
1985 spin_lock_irqsave(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001986 sc = scsi_host_find_tag(fnic->lport->host, tag);
1987 /*
1988 * ignore this lun reset cmd or cmds that do not belong to
1989 * this lun
1990 */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001991 if (!sc || sc == lr_sc || sc->device != lun_dev) {
1992 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001993 continue;
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07001994 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001995
1996 io_req = (struct fnic_io_req *)CMD_SP(sc);
1997
1998 if (!io_req || sc->device != lun_dev) {
1999 spin_unlock_irqrestore(io_lock, flags);
2000 continue;
2001 }
2002
2003 /*
2004 * Found IO that is still pending with firmware and
2005 * belongs to the LUN that we are resetting
2006 */
2007 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2008 "Found IO in %s on lun\n",
2009 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2010
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002011 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2012 spin_unlock_irqrestore(io_lock, flags);
2013 continue;
2014 }
2015 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08002016 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002017 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2018 "%s dev rst not pending sc 0x%p\n", __func__,
2019 sc);
2020 spin_unlock_irqrestore(io_lock, flags);
2021 continue;
2022 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002023
2024 if (io_req->abts_done)
2025 shost_printk(KERN_ERR, fnic->lport->host,
2026 "%s: io_req->abts_done is set state is %s\n",
2027 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002028 old_ioreq_state = CMD_STATE(sc);
2029 /*
2030 * Any pending IO issued prior to reset is expected to be
2031 * in abts pending state, if not we need to set
2032 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2033 * When IO is completed, the IO will be handed over and
2034 * handled in this function.
2035 */
2036 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2037
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002038 BUG_ON(io_req->abts_done);
2039
2040 abt_tag = tag;
2041 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2042 abt_tag |= FNIC_TAG_DEV_RST;
2043 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2044 "%s: dev rst sc 0x%p\n", __func__, sc);
2045 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002046
2047 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2048 io_req->abts_done = &tm_done;
2049 spin_unlock_irqrestore(io_lock, flags);
2050
2051 /* Now queue the abort command to firmware */
2052 int_to_scsilun(sc->device->lun, &fc_lun);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002053
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002054 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002055 FCPIO_ITMF_ABT_TASK_TERM,
2056 fc_lun.scsi_lun, io_req)) {
2057 spin_lock_irqsave(io_lock, flags);
2058 io_req = (struct fnic_io_req *)CMD_SP(sc);
2059 if (io_req)
2060 io_req->abts_done = NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002061 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2062 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002063 spin_unlock_irqrestore(io_lock, flags);
2064 ret = 1;
2065 goto clean_pending_aborts_end;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002066 } else {
2067 spin_lock_irqsave(io_lock, flags);
2068 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2069 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2070 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002071 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08002072 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002073
2074 wait_for_completion_timeout(&tm_done,
2075 msecs_to_jiffies
2076 (fnic->config.ed_tov));
2077
2078 /* Recheck cmd state to check if it is now aborted */
2079 spin_lock_irqsave(io_lock, flags);
2080 io_req = (struct fnic_io_req *)CMD_SP(sc);
2081 if (!io_req) {
2082 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002083 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002084 continue;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002085 }
2086
2087 io_req->abts_done = NULL;
2088
2089 /* if abort is still pending with fw, fail */
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002090 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002091 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002092 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002093 ret = 1;
2094 goto clean_pending_aborts_end;
2095 }
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002096 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002097 CMD_SP(sc) = NULL;
2098 spin_unlock_irqrestore(io_lock, flags);
2099
2100 fnic_release_ioreq_buf(fnic, io_req, sc);
2101 mempool_free(io_req, fnic->io_req_pool);
2102 }
2103
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002104 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2105
2106 /* walk again to check, if IOs are still pending in fw */
2107 if (fnic_is_abts_pending(fnic, lr_sc))
2108 ret = FAILED;
2109
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002110clean_pending_aborts_end:
2111 return ret;
2112}
2113
Hiral Patel03298552013-02-12 17:00:58 -08002114/**
2115 * fnic_scsi_host_start_tag
2116 * Allocates tagid from host's tag list
2117 **/
2118static inline int
2119fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2120{
2121 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2122 int tag, ret = SCSI_NO_TAG;
2123
2124 BUG_ON(!bqt);
2125 if (!bqt) {
2126 pr_err("Tags are not supported\n");
2127 goto end;
2128 }
2129
2130 do {
2131 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2132 if (tag >= bqt->max_depth) {
2133 pr_err("Tag allocation failure\n");
2134 goto end;
2135 }
2136 } while (test_and_set_bit(tag, bqt->tag_map));
2137
2138 bqt->tag_index[tag] = sc->request;
2139 sc->request->tag = tag;
2140 sc->tag = tag;
2141 if (!sc->request->special)
2142 sc->request->special = sc;
2143
2144 ret = tag;
2145
2146end:
2147 return ret;
2148}
2149
2150/**
2151 * fnic_scsi_host_end_tag
2152 * frees tag allocated by fnic_scsi_host_start_tag.
2153 **/
2154static inline void
2155fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2156{
2157 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2158 int tag = sc->request->tag;
2159
2160 if (tag == SCSI_NO_TAG)
2161 return;
2162
2163 BUG_ON(!bqt || !bqt->tag_index[tag]);
2164 if (!bqt)
2165 return;
2166
2167 bqt->tag_index[tag] = NULL;
2168 clear_bit(tag, bqt->tag_map);
2169
2170 return;
2171}
2172
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002173/*
2174 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2175 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2176 * on the LUN.
2177 */
2178int fnic_device_reset(struct scsi_cmnd *sc)
2179{
2180 struct fc_lport *lp;
2181 struct fnic *fnic;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002182 struct fnic_io_req *io_req = NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002183 struct fc_rport *rport;
2184 int status;
2185 int ret = FAILED;
2186 spinlock_t *io_lock;
2187 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08002188 unsigned long start_time = 0;
Hiral Patel03298552013-02-12 17:00:58 -08002189 struct scsi_lun fc_lun;
Hiral Patel67125b02013-09-12 17:45:42 -07002190 struct fnic_stats *fnic_stats;
2191 struct reset_stats *reset_stats;
Hiral Patel4d7007b2013-02-12 17:01:02 -08002192 int tag = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002193 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patel03298552013-02-12 17:00:58 -08002194 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002195
2196 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01002197 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002198
2199 /* Get local-port, check ready and link up */
2200 lp = shost_priv(sc->device->host);
2201
2202 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002203 fnic_stats = &fnic->fnic_stats;
2204 reset_stats = &fnic->fnic_stats.reset_stats;
2205
2206 atomic64_inc(&reset_stats->device_resets);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002207
Roel Kluin0db6f432010-06-11 16:44:46 -07002208 rport = starget_to_rport(scsi_target(sc->device));
2209 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002210 "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
Hiral Patel03298552013-02-12 17:00:58 -08002211 rport->port_id, sc->device->lun, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002212
2213 if (lp->state != LPORT_ST_READY || !(lp->link_up))
2214 goto fnic_device_reset_end;
2215
2216 /* Check if remote port up */
Hiral Patel67125b02013-09-12 17:45:42 -07002217 if (fc_remote_port_chkready(rport)) {
2218 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002219 goto fnic_device_reset_end;
Hiral Patel67125b02013-09-12 17:45:42 -07002220 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002221
Hiral Patel14eb5d92013-02-12 17:01:01 -08002222 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
Hiral Patel03298552013-02-12 17:00:58 -08002223 /* Allocate tag if not present */
2224
2225 tag = sc->request->tag;
2226 if (unlikely(tag < 0)) {
Christoph Hellwig4d637162014-06-12 14:53:23 +02002227 /*
2228 * XXX(hch): current the midlayer fakes up a struct
2229 * request for the explicit reset ioctls, and those
2230 * don't have a tag allocated to them. The below
2231 * code pokes into midlayer structures to paper over
2232 * this design issue, but that won't work for blk-mq.
2233 *
2234 * Either someone who can actually test the hardware
2235 * will have to come up with a similar hack for the
2236 * blk-mq case, or we'll have to bite the bullet and
2237 * fix the way the EH ioctls work for real, but until
2238 * that happens we fail these explicit requests here.
2239 */
2240 if (shost_use_blk_mq(sc->device->host))
2241 goto fnic_device_reset_end;
2242
Hiral Patel03298552013-02-12 17:00:58 -08002243 tag = fnic_scsi_host_start_tag(fnic, sc);
2244 if (unlikely(tag == SCSI_NO_TAG))
2245 goto fnic_device_reset_end;
2246 tag_gen_flag = 1;
2247 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002248 io_lock = fnic_io_lock_hash(fnic, sc);
2249 spin_lock_irqsave(io_lock, flags);
2250 io_req = (struct fnic_io_req *)CMD_SP(sc);
2251
2252 /*
2253 * If there is a io_req attached to this command, then use it,
2254 * else allocate a new one.
2255 */
2256 if (!io_req) {
2257 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2258 if (!io_req) {
2259 spin_unlock_irqrestore(io_lock, flags);
2260 goto fnic_device_reset_end;
2261 }
2262 memset(io_req, 0, sizeof(*io_req));
2263 io_req->port_id = rport->port_id;
2264 CMD_SP(sc) = (char *)io_req;
2265 }
2266 io_req->dr_done = &tm_done;
2267 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2268 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2269 spin_unlock_irqrestore(io_lock, flags);
2270
Hiral Patel03298552013-02-12 17:00:58 -08002271 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002272
2273 /*
2274 * issue the device reset, if enqueue failed, clean up the ioreq
2275 * and break assoc with scsi cmd
2276 */
2277 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2278 spin_lock_irqsave(io_lock, flags);
2279 io_req = (struct fnic_io_req *)CMD_SP(sc);
2280 if (io_req)
2281 io_req->dr_done = NULL;
2282 goto fnic_device_reset_clean;
2283 }
Hiral Patel03298552013-02-12 17:00:58 -08002284 spin_lock_irqsave(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08002285 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08002286 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002287
2288 /*
2289 * Wait on the local completion for LUN reset. The io_req may be
2290 * freed while we wait since we hold no lock.
2291 */
2292 wait_for_completion_timeout(&tm_done,
2293 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2294
2295 spin_lock_irqsave(io_lock, flags);
2296 io_req = (struct fnic_io_req *)CMD_SP(sc);
2297 if (!io_req) {
2298 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002299 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2300 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002301 goto fnic_device_reset_end;
2302 }
2303 io_req->dr_done = NULL;
2304
2305 status = CMD_LR_STATUS(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002306
2307 /*
2308 * If lun reset not completed, bail out with failed. io_req
2309 * gets cleaned up during higher levels of EH
2310 */
2311 if (status == FCPIO_INVALID_CODE) {
Hiral Patel67125b02013-09-12 17:45:42 -07002312 atomic64_inc(&reset_stats->device_reset_timeouts);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002313 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2314 "Device reset timed out\n");
Hiral Patel03298552013-02-12 17:00:58 -08002315 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2316 spin_unlock_irqrestore(io_lock, flags);
2317 int_to_scsilun(sc->device->lun, &fc_lun);
2318 /*
Sesidhar Beddel1259c5d2013-09-09 13:31:49 -07002319 * Issue abort and terminate on device reset request.
2320 * If q'ing of terminate fails, retry it after a delay.
Hiral Patel03298552013-02-12 17:00:58 -08002321 */
2322 while (1) {
2323 spin_lock_irqsave(io_lock, flags);
2324 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2325 spin_unlock_irqrestore(io_lock, flags);
2326 break;
2327 }
2328 spin_unlock_irqrestore(io_lock, flags);
2329 if (fnic_queue_abort_io_req(fnic,
2330 tag | FNIC_TAG_DEV_RST,
2331 FCPIO_ITMF_ABT_TASK_TERM,
2332 fc_lun.scsi_lun, io_req)) {
2333 wait_for_completion_timeout(&tm_done,
2334 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2335 } else {
2336 spin_lock_irqsave(io_lock, flags);
2337 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2338 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2339 io_req->abts_done = &tm_done;
2340 spin_unlock_irqrestore(io_lock, flags);
2341 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2342 "Abort and terminate issued on Device reset "
2343 "tag 0x%x sc 0x%p\n", tag, sc);
2344 break;
2345 }
2346 }
2347 while (1) {
2348 spin_lock_irqsave(io_lock, flags);
2349 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2350 spin_unlock_irqrestore(io_lock, flags);
2351 wait_for_completion_timeout(&tm_done,
2352 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2353 break;
2354 } else {
2355 io_req = (struct fnic_io_req *)CMD_SP(sc);
2356 io_req->abts_done = NULL;
2357 goto fnic_device_reset_clean;
2358 }
2359 }
2360 } else {
2361 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002362 }
2363
2364 /* Completed, but not successful, clean up the io_req, return fail */
2365 if (status != FCPIO_SUCCESS) {
2366 spin_lock_irqsave(io_lock, flags);
2367 FNIC_SCSI_DBG(KERN_DEBUG,
2368 fnic->lport->host,
2369 "Device reset completed - failed\n");
2370 io_req = (struct fnic_io_req *)CMD_SP(sc);
2371 goto fnic_device_reset_clean;
2372 }
2373
2374 /*
2375 * Clean up any aborts on this lun that have still not
2376 * completed. If any of these fail, then LUN reset fails.
2377 * clean_pending_aborts cleans all cmds on this lun except
2378 * the lun reset cmd. If all cmds get cleaned, the lun reset
2379 * succeeds
2380 */
2381 if (fnic_clean_pending_aborts(fnic, sc)) {
2382 spin_lock_irqsave(io_lock, flags);
2383 io_req = (struct fnic_io_req *)CMD_SP(sc);
2384 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2385 "Device reset failed"
2386 " since could not abort all IOs\n");
2387 goto fnic_device_reset_clean;
2388 }
2389
2390 /* Clean lun reset command */
2391 spin_lock_irqsave(io_lock, flags);
2392 io_req = (struct fnic_io_req *)CMD_SP(sc);
2393 if (io_req)
2394 /* Completed, and successful */
2395 ret = SUCCESS;
2396
2397fnic_device_reset_clean:
2398 if (io_req)
2399 CMD_SP(sc) = NULL;
2400
2401 spin_unlock_irqrestore(io_lock, flags);
2402
2403 if (io_req) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002404 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002405 fnic_release_ioreq_buf(fnic, io_req, sc);
2406 mempool_free(io_req, fnic->io_req_pool);
2407 }
2408
2409fnic_device_reset_end:
Hiral Patel4d7007b2013-02-12 17:01:02 -08002410 FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2411 sc->request->tag, sc,
2412 jiffies_to_msecs(jiffies - start_time),
2413 0, ((u64)sc->cmnd[0] << 32 |
2414 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2415 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2416 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2417
Hiral Patel03298552013-02-12 17:00:58 -08002418 /* free tag if it is allocated */
2419 if (unlikely(tag_gen_flag))
2420 fnic_scsi_host_end_tag(fnic, sc);
2421
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002422 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2423 "Returning from device reset %s\n",
2424 (ret == SUCCESS) ?
2425 "SUCCESS" : "FAILED");
Hiral Patel67125b02013-09-12 17:45:42 -07002426
2427 if (ret == FAILED)
2428 atomic64_inc(&reset_stats->device_reset_failures);
2429
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002430 return ret;
2431}
2432
2433/* Clean up all IOs, clean up libFC local port */
2434int fnic_reset(struct Scsi_Host *shost)
2435{
2436 struct fc_lport *lp;
2437 struct fnic *fnic;
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002438 int ret = 0;
Hiral Patel67125b02013-09-12 17:45:42 -07002439 struct reset_stats *reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002440
2441 lp = shost_priv(shost);
2442 fnic = lport_priv(lp);
Hiral Patel67125b02013-09-12 17:45:42 -07002443 reset_stats = &fnic->fnic_stats.reset_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002444
2445 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2446 "fnic_reset called\n");
2447
Hiral Patel67125b02013-09-12 17:45:42 -07002448 atomic64_inc(&reset_stats->fnic_resets);
2449
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002450 /*
2451 * Reset local port, this will clean up libFC exchanges,
2452 * reset remote port sessions, and if link is up, begin flogi
2453 */
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002454 ret = lp->tt.lport_reset(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002455
2456 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2457 "Returning from fnic reset %s\n",
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002458 (ret == 0) ?
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002459 "SUCCESS" : "FAILED");
2460
Hiral Patel67125b02013-09-12 17:45:42 -07002461 if (ret == 0)
2462 atomic64_inc(&reset_stats->fnic_reset_completions);
2463 else
2464 atomic64_inc(&reset_stats->fnic_reset_failures);
2465
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002466 return ret;
2467}
2468
2469/*
2470 * SCSI Error handling calls driver's eh_host_reset if all prior
2471 * error handling levels return FAILED. If host reset completes
2472 * successfully, and if link is up, then Fabric login begins.
2473 *
2474 * Host Reset is the highest level of error recovery. If this fails, then
2475 * host is offlined by SCSI.
2476 *
2477 */
2478int fnic_host_reset(struct scsi_cmnd *sc)
2479{
2480 int ret;
2481 unsigned long wait_host_tmo;
2482 struct Scsi_Host *shost = sc->device->host;
2483 struct fc_lport *lp = shost_priv(shost);
2484
2485 /*
2486 * If fnic_reset is successful, wait for fabric login to complete
2487 * scsi-ml tries to send a TUR to every device if host reset is
2488 * successful, so before returning to scsi, fabric should be up
2489 */
Narsimhulu Musini441fbd22013-09-12 17:45:41 -07002490 ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002491 if (ret == SUCCESS) {
2492 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2493 ret = FAILED;
2494 while (time_before(jiffies, wait_host_tmo)) {
2495 if ((lp->state == LPORT_ST_READY) &&
2496 (lp->link_up)) {
2497 ret = SUCCESS;
2498 break;
2499 }
2500 ssleep(1);
2501 }
2502 }
2503
2504 return ret;
2505}
2506
2507/*
2508 * This fxn is called from libFC when host is removed
2509 */
2510void fnic_scsi_abort_io(struct fc_lport *lp)
2511{
2512 int err = 0;
2513 unsigned long flags;
2514 enum fnic_state old_state;
2515 struct fnic *fnic = lport_priv(lp);
2516 DECLARE_COMPLETION_ONSTACK(remove_wait);
2517
2518 /* Issue firmware reset for fnic, wait for reset to complete */
Hiral Patel03298552013-02-12 17:00:58 -08002519retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002520 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002521 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2522 /* fw reset is in progress, poll for its completion */
2523 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2524 schedule_timeout(msecs_to_jiffies(100));
2525 goto retry_fw_reset;
2526 }
2527
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002528 fnic->remove_wait = &remove_wait;
2529 old_state = fnic->state;
2530 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002531 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002532 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2533
2534 err = fnic_fw_reset_handler(fnic);
2535 if (err) {
2536 spin_lock_irqsave(&fnic->fnic_lock, flags);
2537 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2538 fnic->state = old_state;
2539 fnic->remove_wait = NULL;
2540 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2541 return;
2542 }
2543
2544 /* Wait for firmware reset to complete */
2545 wait_for_completion_timeout(&remove_wait,
2546 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2547
2548 spin_lock_irqsave(&fnic->fnic_lock, flags);
2549 fnic->remove_wait = NULL;
2550 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2551 "fnic_scsi_abort_io %s\n",
2552 (fnic->state == FNIC_IN_ETH_MODE) ?
2553 "SUCCESS" : "FAILED");
2554 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2555
2556}
2557
2558/*
2559 * This fxn called from libFC to clean up driver IO state on link down
2560 */
2561void fnic_scsi_cleanup(struct fc_lport *lp)
2562{
2563 unsigned long flags;
2564 enum fnic_state old_state;
2565 struct fnic *fnic = lport_priv(lp);
2566
2567 /* issue fw reset */
Hiral Patel03298552013-02-12 17:00:58 -08002568retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002569 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002570 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2571 /* fw reset is in progress, poll for its completion */
2572 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2573 schedule_timeout(msecs_to_jiffies(100));
2574 goto retry_fw_reset;
2575 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002576 old_state = fnic->state;
2577 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002578 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002579 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2580
2581 if (fnic_fw_reset_handler(fnic)) {
2582 spin_lock_irqsave(&fnic->fnic_lock, flags);
2583 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2584 fnic->state = old_state;
2585 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2586 }
2587
2588}
2589
2590void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2591{
2592}
2593
2594void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2595{
2596 struct fnic *fnic = lport_priv(lp);
2597
2598 /* Non-zero sid, nothing to do */
2599 if (sid)
2600 goto call_fc_exch_mgr_reset;
2601
2602 if (did) {
2603 fnic_rport_exch_reset(fnic, did);
2604 goto call_fc_exch_mgr_reset;
2605 }
2606
2607 /*
2608 * sid = 0, did = 0
2609 * link down or device being removed
2610 */
2611 if (!fnic->in_remove)
2612 fnic_scsi_cleanup(lp);
2613 else
2614 fnic_scsi_abort_io(lp);
2615
2616 /* call libFC exch mgr reset to reset its exchanges */
2617call_fc_exch_mgr_reset:
2618 fc_exch_mgr_reset(lp, sid, did);
2619
2620}
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002621
2622/*
2623 * fnic_is_abts_pending() is a helper function that
2624 * walks through tag map to check if there is any IOs pending,if there is one,
2625 * then it returns 1 (true), otherwise 0 (false)
2626 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2627 * otherwise, it checks for all IOs.
2628 */
2629int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2630{
2631 int tag;
2632 struct fnic_io_req *io_req;
2633 spinlock_t *io_lock;
2634 unsigned long flags;
2635 int ret = 0;
2636 struct scsi_cmnd *sc;
2637 struct scsi_device *lun_dev = NULL;
2638
2639 if (lr_sc)
2640 lun_dev = lr_sc->device;
2641
2642 /* walk again to check, if IOs are still pending in fw */
Hiral Patelfc857992013-09-09 13:31:51 -07002643 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002644 sc = scsi_host_find_tag(fnic->lport->host, tag);
2645 /*
2646 * ignore this lun reset cmd or cmds that do not belong to
2647 * this lun
2648 */
2649 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2650 continue;
2651
2652 io_lock = fnic_io_lock_hash(fnic, sc);
2653 spin_lock_irqsave(io_lock, flags);
2654
2655 io_req = (struct fnic_io_req *)CMD_SP(sc);
2656
2657 if (!io_req || sc->device != lun_dev) {
2658 spin_unlock_irqrestore(io_lock, flags);
2659 continue;
2660 }
2661
2662 /*
2663 * Found IO that is still pending with firmware and
2664 * belongs to the LUN that we are resetting
2665 */
2666 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2667 "Found IO in %s on lun\n",
2668 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2669
Dan Carpenter5d65f912013-05-30 10:50:46 +03002670 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002671 ret = 1;
Dan Carpenter5d65f912013-05-30 10:50:46 +03002672 spin_unlock_irqrestore(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002673 }
2674
2675 return ret;
2676}