blob: 64830814da0db1b2bc3eac6a121b036c9f7307f2 [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[] = {
50 [FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
51 [FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
52 [FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
53 [FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
54};
55
56static const char *fcpio_status_str[] = {
57 [FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
58 [FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
59 [FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
60 [FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
61 [FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
62 [FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
63 [FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
64 [FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
65 [FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
66 [FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
67 [FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
68 [FCPIO_FW_ERR] = "FCPIO_FW_ERR",
69 [FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
70 [FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
71 [FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
72 [FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
73 [FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
74 [FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
75 [FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
76};
77
78const char *fnic_state_to_str(unsigned int state)
79{
80 if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
81 return "unknown";
82
83 return fnic_state_str[state];
84}
85
86static const char *fnic_ioreq_state_to_str(unsigned int state)
87{
88 if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
89 !fnic_ioreq_state_str[state])
90 return "unknown";
91
92 return fnic_ioreq_state_str[state];
93}
94
95static const char *fnic_fcpio_status_to_str(unsigned int status)
96{
97 if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
98 return "unknown";
99
100 return fcpio_status_str[status];
101}
102
103static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
104
105static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
106 struct scsi_cmnd *sc)
107{
108 u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
109
110 return &fnic->io_req_lock[hash];
111}
112
113/*
114 * Unmap the data buffer and sense buffer for an io_req,
115 * also unmap and free the device-private scatter/gather list.
116 */
117static void fnic_release_ioreq_buf(struct fnic *fnic,
118 struct fnic_io_req *io_req,
119 struct scsi_cmnd *sc)
120{
121 if (io_req->sgl_list_pa)
122 pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
123 sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
124 PCI_DMA_TODEVICE);
125 scsi_dma_unmap(sc);
126
127 if (io_req->sgl_cnt)
128 mempool_free(io_req->sgl_list_alloc,
129 fnic->io_sgl_pool[io_req->sgl_type]);
130 if (io_req->sense_buf_pa)
131 pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
132 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
133}
134
135/* Free up Copy Wq descriptors. Called with copy_wq lock held */
136static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
137{
138 /* if no Ack received from firmware, then nothing to clean */
139 if (!fnic->fw_ack_recd[0])
140 return 1;
141
142 /*
143 * Update desc_available count based on number of freed descriptors
144 * Account for wraparound
145 */
146 if (wq->to_clean_index <= fnic->fw_ack_index[0])
147 wq->ring.desc_avail += (fnic->fw_ack_index[0]
148 - wq->to_clean_index + 1);
149 else
150 wq->ring.desc_avail += (wq->ring.desc_count
151 - wq->to_clean_index
152 + fnic->fw_ack_index[0] + 1);
153
154 /*
155 * just bump clean index to ack_index+1 accounting for wraparound
156 * this will essentially free up all descriptors between
157 * to_clean_index and fw_ack_index, both inclusive
158 */
159 wq->to_clean_index =
160 (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
161
162 /* we have processed the acks received so far */
163 fnic->fw_ack_recd[0] = 0;
164 return 0;
165}
166
167
Hiral Patel03298552013-02-12 17:00:58 -0800168/**
169 * __fnic_set_state_flags
170 * Sets/Clears bits in fnic's state_flags
171 **/
172void
173__fnic_set_state_flags(struct fnic *fnic, unsigned long st_flags,
174 unsigned long clearbits)
175{
176 struct Scsi_Host *host = fnic->lport->host;
177 int sh_locked = spin_is_locked(host->host_lock);
178 unsigned long flags = 0;
179
180 if (!sh_locked)
181 spin_lock_irqsave(host->host_lock, flags);
182
183 if (clearbits)
184 fnic->state_flags &= ~st_flags;
185 else
186 fnic->state_flags |= st_flags;
187
188 if (!sh_locked)
189 spin_unlock_irqrestore(host->host_lock, flags);
190
191 return;
192}
193
194
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700195/*
196 * fnic_fw_reset_handler
197 * Routine to send reset msg to fw
198 */
199int fnic_fw_reset_handler(struct fnic *fnic)
200{
201 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
202 int ret = 0;
203 unsigned long flags;
204
Hiral Patel03298552013-02-12 17:00:58 -0800205 /* indicate fwreset to io path */
206 fnic_set_state_flags(fnic, FNIC_FLAGS_FWRESET);
207
Joe Eykholt78112e52009-11-03 11:49:22 -0800208 skb_queue_purge(&fnic->frame_queue);
209 skb_queue_purge(&fnic->tx_queue);
210
Hiral Patel03298552013-02-12 17:00:58 -0800211 /* wait for io cmpl */
212 while (atomic_read(&fnic->in_flight))
213 schedule_timeout(msecs_to_jiffies(1));
214
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700215 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
216
217 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
218 free_wq_copy_descs(fnic, wq);
219
220 if (!vnic_wq_copy_desc_avail(wq))
221 ret = -EAGAIN;
222 else
223 fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
224
225 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
226
227 if (!ret)
228 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
229 "Issued fw reset\n");
Hiral Patel03298552013-02-12 17:00:58 -0800230 else {
231 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700232 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
233 "Failed to issue fw reset\n");
Hiral Patel03298552013-02-12 17:00:58 -0800234 }
235
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700236 return ret;
237}
238
239
240/*
241 * fnic_flogi_reg_handler
242 * Routine to send flogi register msg to fw
243 */
Joe Eykholt78112e52009-11-03 11:49:22 -0800244int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700245{
246 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Joe Eykholt78112e52009-11-03 11:49:22 -0800247 enum fcpio_flogi_reg_format_type format;
248 struct fc_lport *lp = fnic->lport;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700249 u8 gw_mac[ETH_ALEN];
250 int ret = 0;
251 unsigned long flags;
252
253 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
254
255 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
256 free_wq_copy_descs(fnic, wq);
257
258 if (!vnic_wq_copy_desc_avail(wq)) {
259 ret = -EAGAIN;
260 goto flogi_reg_ioreq_end;
261 }
262
Joe Eykholt78112e52009-11-03 11:49:22 -0800263 if (fnic->ctlr.map_dest) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700264 memset(gw_mac, 0xff, ETH_ALEN);
Joe Eykholt78112e52009-11-03 11:49:22 -0800265 format = FCPIO_FLOGI_REG_DEF_DEST;
266 } else {
267 memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
268 format = FCPIO_FLOGI_REG_GW_DEST;
269 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700270
Joe Eykholt78112e52009-11-03 11:49:22 -0800271 if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
272 fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
273 fc_id, gw_mac,
274 fnic->data_src_addr,
275 lp->r_a_tov, lp->e_d_tov);
276 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
277 "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
278 fc_id, fnic->data_src_addr, gw_mac);
279 } else {
280 fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
281 format, fc_id, gw_mac);
282 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
283 "FLOGI reg issued fcid %x map %d dest %pM\n",
284 fc_id, fnic->ctlr.map_dest, gw_mac);
285 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700286
287flogi_reg_ioreq_end:
288 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700289 return ret;
290}
291
292/*
293 * fnic_queue_wq_copy_desc
294 * Routine to enqueue a wq copy desc
295 */
296static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
297 struct vnic_wq_copy *wq,
298 struct fnic_io_req *io_req,
299 struct scsi_cmnd *sc,
Roel Kluin87a2d342009-06-23 01:06:40 +0200300 int sg_count)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700301{
302 struct scatterlist *sg;
303 struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
304 struct fc_rport_libfc_priv *rp = rport->dd_data;
305 struct host_sg_desc *desc;
306 u8 pri_tag = 0;
307 unsigned int i;
308 unsigned long intr_flags;
309 int flags;
310 u8 exch_flags;
311 struct scsi_lun fc_lun;
312 char msg[2];
313
314 if (sg_count) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700315 /* For each SGE, create a device desc entry */
316 desc = io_req->sgl_list;
317 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
318 desc->addr = cpu_to_le64(sg_dma_address(sg));
319 desc->len = cpu_to_le32(sg_dma_len(sg));
320 desc->_resvd = 0;
321 desc++;
322 }
323
324 io_req->sgl_list_pa = pci_map_single
325 (fnic->pdev,
326 io_req->sgl_list,
327 sizeof(io_req->sgl_list[0]) * sg_count,
328 PCI_DMA_TODEVICE);
329 }
330
331 io_req->sense_buf_pa = pci_map_single(fnic->pdev,
332 sc->sense_buffer,
333 SCSI_SENSE_BUFFERSIZE,
334 PCI_DMA_FROMDEVICE);
335
336 int_to_scsilun(sc->device->lun, &fc_lun);
337
338 pri_tag = FCPIO_ICMND_PTA_SIMPLE;
339 msg[0] = MSG_SIMPLE_TAG;
340 scsi_populate_tag_msg(sc, msg);
341 if (msg[0] == MSG_ORDERED_TAG)
342 pri_tag = FCPIO_ICMND_PTA_ORDERED;
343
344 /* Enqueue the descriptor in the Copy WQ */
345 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
346
347 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
348 free_wq_copy_descs(fnic, wq);
349
350 if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
351 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
352 return SCSI_MLQUEUE_HOST_BUSY;
353 }
354
355 flags = 0;
356 if (sc->sc_data_direction == DMA_FROM_DEVICE)
357 flags = FCPIO_ICMND_RDDATA;
358 else if (sc->sc_data_direction == DMA_TO_DEVICE)
359 flags = FCPIO_ICMND_WRDATA;
360
361 exch_flags = 0;
362 if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
363 (rp->flags & FC_RP_FLAGS_RETRY))
364 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
365
366 fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
367 0, exch_flags, io_req->sgl_cnt,
368 SCSI_SENSE_BUFFERSIZE,
369 io_req->sgl_list_pa,
370 io_req->sense_buf_pa,
371 0, /* scsi cmd ref, always 0 */
372 pri_tag, /* scsi pri and tag */
373 flags, /* command flags */
Abhijeet Joglekar4b536622009-10-21 16:28:25 -0700374 sc->cmnd, sc->cmd_len,
375 scsi_bufflen(sc),
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700376 fc_lun.scsi_lun, io_req->port_id,
377 rport->maxframe_size, rp->r_a_tov,
378 rp->e_d_tov);
379
380 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
381 return 0;
382}
383
384/*
385 * fnic_queuecommand
386 * Routine to send a scsi cdb
387 * Called with host_lock held and interrupts disabled.
388 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500389static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700390{
Hiral Patel03298552013-02-12 17:00:58 -0800391 struct fc_lport *lp = shost_priv(sc->device->host);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700392 struct fc_rport *rport;
393 struct fnic_io_req *io_req;
Hiral Patel03298552013-02-12 17:00:58 -0800394 struct fnic *fnic = lport_priv(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700395 struct vnic_wq_copy *wq;
396 int ret;
Roel Kluin87a2d342009-06-23 01:06:40 +0200397 int sg_count;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700398 unsigned long flags;
399 unsigned long ptr;
400
Hiral Patel03298552013-02-12 17:00:58 -0800401 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
402 return SCSI_MLQUEUE_HOST_BUSY;
403
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700404 rport = starget_to_rport(scsi_target(sc->device));
405 ret = fc_remote_port_chkready(rport);
406 if (ret) {
407 sc->result = ret;
408 done(sc);
409 return 0;
410 }
411
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700412 if (lp->state != LPORT_ST_READY || !(lp->link_up))
413 return SCSI_MLQUEUE_HOST_BUSY;
414
Hiral Patel03298552013-02-12 17:00:58 -0800415 atomic_inc(&fnic->in_flight);
416
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700417 /*
418 * Release host lock, use driver resource specific locks from here.
419 * Don't re-enable interrupts in case they were disabled prior to the
420 * caller disabling them.
421 */
422 spin_unlock(lp->host->host_lock);
Hiral Patel03298552013-02-12 17:00:58 -0800423 CMD_FLAGS(sc) = FNIC_CDB_REQ;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700424
425 /* Get a new io_req for this SCSI IO */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700426 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
427 if (!io_req) {
428 ret = SCSI_MLQUEUE_HOST_BUSY;
429 goto out;
430 }
431 memset(io_req, 0, sizeof(*io_req));
432
433 /* Map the data buffer */
434 sg_count = scsi_dma_map(sc);
435 if (sg_count < 0) {
436 mempool_free(io_req, fnic->io_req_pool);
437 goto out;
438 }
439
440 /* Determine the type of scatter/gather list we need */
441 io_req->sgl_cnt = sg_count;
442 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
443 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
444 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
445
446 if (sg_count) {
447 io_req->sgl_list =
448 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
Abhijeet Joglekar0c79c742011-06-13 21:21:01 -0700449 GFP_ATOMIC);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700450 if (!io_req->sgl_list) {
451 ret = SCSI_MLQUEUE_HOST_BUSY;
452 scsi_dma_unmap(sc);
453 mempool_free(io_req, fnic->io_req_pool);
454 goto out;
455 }
456
457 /* Cache sgl list allocated address before alignment */
458 io_req->sgl_list_alloc = io_req->sgl_list;
459 ptr = (unsigned long) io_req->sgl_list;
460 if (ptr % FNIC_SG_DESC_ALIGN) {
461 io_req->sgl_list = (struct host_sg_desc *)
462 (((unsigned long) ptr
463 + FNIC_SG_DESC_ALIGN - 1)
464 & ~(FNIC_SG_DESC_ALIGN - 1));
465 }
466 }
467
468 /* initialize rest of io_req */
469 io_req->port_id = rport->port_id;
470 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
471 CMD_SP(sc) = (char *)io_req;
472 sc->scsi_done = done;
473
474 /* create copy wq desc and enqueue it */
475 wq = &fnic->wq_copy[0];
476 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
477 if (ret) {
478 /*
479 * In case another thread cancelled the request,
480 * refetch the pointer under the lock.
481 */
482 spinlock_t *io_lock = fnic_io_lock_hash(fnic, sc);
483
484 spin_lock_irqsave(io_lock, flags);
485 io_req = (struct fnic_io_req *)CMD_SP(sc);
486 CMD_SP(sc) = NULL;
487 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
488 spin_unlock_irqrestore(io_lock, flags);
489 if (io_req) {
490 fnic_release_ioreq_buf(fnic, io_req, sc);
491 mempool_free(io_req, fnic->io_req_pool);
492 }
493 }
494out:
Hiral Patel03298552013-02-12 17:00:58 -0800495 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700496 /* acquire host lock before returning to SCSI */
497 spin_lock(lp->host->host_lock);
498 return ret;
499}
500
Jeff Garzikf2812332010-11-16 02:10:29 -0500501DEF_SCSI_QCMD(fnic_queuecommand)
502
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700503/*
504 * fnic_fcpio_fw_reset_cmpl_handler
505 * Routine to handle fw reset completion
506 */
507static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
508 struct fcpio_fw_req *desc)
509{
510 u8 type;
511 u8 hdr_status;
512 struct fcpio_tag tag;
513 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700514 unsigned long flags;
515
516 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
517
518 /* Clean up all outstanding io requests */
519 fnic_cleanup_io(fnic, SCSI_NO_TAG);
520
521 spin_lock_irqsave(&fnic->fnic_lock, flags);
522
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700523 /* fnic should be in FC_TRANS_ETH_MODE */
524 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
525 /* Check status of reset completion */
526 if (!hdr_status) {
527 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
528 "reset cmpl success\n");
529 /* Ready to send flogi out */
530 fnic->state = FNIC_IN_ETH_MODE;
531 } else {
532 FNIC_SCSI_DBG(KERN_DEBUG,
533 fnic->lport->host,
534 "fnic fw_reset : failed %s\n",
535 fnic_fcpio_status_to_str(hdr_status));
536
537 /*
538 * Unable to change to eth mode, cannot send out flogi
539 * Change state to fc mode, so that subsequent Flogi
540 * requests from libFC will cause more attempts to
541 * reset the firmware. Free the cached flogi
542 */
543 fnic->state = FNIC_IN_FC_MODE;
544 ret = -1;
545 }
546 } else {
547 FNIC_SCSI_DBG(KERN_DEBUG,
548 fnic->lport->host,
549 "Unexpected state %s while processing"
550 " reset cmpl\n", fnic_state_to_str(fnic->state));
551 ret = -1;
552 }
553
554 /* Thread removing device blocks till firmware reset is complete */
555 if (fnic->remove_wait)
556 complete(fnic->remove_wait);
557
558 /*
559 * If fnic is being removed, or fw reset failed
560 * free the flogi frame. Else, send it out
561 */
562 if (fnic->remove_wait || ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700563 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Joe Eykholt78112e52009-11-03 11:49:22 -0800564 skb_queue_purge(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700565 goto reset_cmpl_handler_end;
566 }
567
568 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
569
Joe Eykholt78112e52009-11-03 11:49:22 -0800570 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700571
572 reset_cmpl_handler_end:
Hiral Patel03298552013-02-12 17:00:58 -0800573 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
574
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700575 return ret;
576}
577
578/*
579 * fnic_fcpio_flogi_reg_cmpl_handler
580 * Routine to handle flogi register completion
581 */
582static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
583 struct fcpio_fw_req *desc)
584{
585 u8 type;
586 u8 hdr_status;
587 struct fcpio_tag tag;
588 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700589 unsigned long flags;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700590
591 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
592
593 /* Update fnic state based on status of flogi reg completion */
594 spin_lock_irqsave(&fnic->fnic_lock, flags);
595
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700596 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
597
598 /* Check flogi registration completion status */
599 if (!hdr_status) {
600 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
601 "flog reg succeeded\n");
602 fnic->state = FNIC_IN_FC_MODE;
603 } else {
604 FNIC_SCSI_DBG(KERN_DEBUG,
605 fnic->lport->host,
606 "fnic flogi reg :failed %s\n",
607 fnic_fcpio_status_to_str(hdr_status));
608 fnic->state = FNIC_IN_ETH_MODE;
609 ret = -1;
610 }
611 } else {
612 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
613 "Unexpected fnic state %s while"
614 " processing flogi reg completion\n",
615 fnic_state_to_str(fnic->state));
616 ret = -1;
617 }
618
Joe Eykholt78112e52009-11-03 11:49:22 -0800619 if (!ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700620 if (fnic->stop_rx_link_events) {
621 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
622 goto reg_cmpl_handler_end;
623 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700624 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
625
Joe Eykholt78112e52009-11-03 11:49:22 -0800626 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700627 queue_work(fnic_event_queue, &fnic->frame_work);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700628 } else {
629 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700630 }
631
632reg_cmpl_handler_end:
633 return ret;
634}
635
636static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
637 u16 request_out)
638{
639 if (wq->to_clean_index <= wq->to_use_index) {
640 /* out of range, stale request_out index */
641 if (request_out < wq->to_clean_index ||
642 request_out >= wq->to_use_index)
643 return 0;
644 } else {
645 /* out of range, stale request_out index */
646 if (request_out < wq->to_clean_index &&
647 request_out >= wq->to_use_index)
648 return 0;
649 }
650 /* request_out index is in range */
651 return 1;
652}
653
654
655/*
656 * Mark that ack received and store the Ack index. If there are multiple
657 * acks received before Tx thread cleans it up, the latest value will be
658 * used which is correct behavior. This state should be in the copy Wq
659 * instead of in the fnic
660 */
661static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
662 unsigned int cq_index,
663 struct fcpio_fw_req *desc)
664{
665 struct vnic_wq_copy *wq;
666 u16 request_out = desc->u.ack.request_out;
667 unsigned long flags;
668
669 /* mark the ack state */
670 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
671 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
672
673 if (is_ack_index_in_range(wq, request_out)) {
674 fnic->fw_ack_index[0] = request_out;
675 fnic->fw_ack_recd[0] = 1;
676 }
677 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
678}
679
680/*
681 * fnic_fcpio_icmnd_cmpl_handler
682 * Routine to handle icmnd completions
683 */
684static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
685 struct fcpio_fw_req *desc)
686{
687 u8 type;
688 u8 hdr_status;
689 struct fcpio_tag tag;
690 u32 id;
691 u64 xfer_len = 0;
692 struct fcpio_icmnd_cmpl *icmnd_cmpl;
693 struct fnic_io_req *io_req;
694 struct scsi_cmnd *sc;
695 unsigned long flags;
696 spinlock_t *io_lock;
697
698 /* Decode the cmpl description to get the io_req id */
699 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
700 fcpio_tag_id_dec(&tag, &id);
701
Hiral Patel03298552013-02-12 17:00:58 -0800702 if (id >= FNIC_MAX_IO_REQ) {
703 shost_printk(KERN_ERR, fnic->lport->host,
704 "Tag out of range tag %x hdr status = %s\n",
705 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700706 return;
Hiral Patel03298552013-02-12 17:00:58 -0800707 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700708
709 sc = scsi_host_find_tag(fnic->lport->host, id);
710 WARN_ON_ONCE(!sc);
711 if (!sc)
712 return;
713
714 io_lock = fnic_io_lock_hash(fnic, sc);
715 spin_lock_irqsave(io_lock, flags);
716 io_req = (struct fnic_io_req *)CMD_SP(sc);
717 WARN_ON_ONCE(!io_req);
718 if (!io_req) {
719 spin_unlock_irqrestore(io_lock, flags);
720 return;
721 }
722
723 /* firmware completed the io */
724 io_req->io_completed = 1;
725
726 /*
727 * if SCSI-ML has already issued abort on this command,
728 * ignore completion of the IO. The abts path will clean it up
729 */
730 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
731 spin_unlock_irqrestore(io_lock, flags);
732 return;
733 }
734
735 /* Mark the IO as complete */
736 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
737
738 icmnd_cmpl = &desc->u.icmnd_cmpl;
739
740 switch (hdr_status) {
741 case FCPIO_SUCCESS:
742 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
743 xfer_len = scsi_bufflen(sc);
744 scsi_set_resid(sc, icmnd_cmpl->residual);
745
746 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
747 xfer_len -= icmnd_cmpl->residual;
748
749 /*
750 * If queue_full, then try to reduce queue depth for all
751 * LUNS on the target. Todo: this should be accompanied
752 * by a periodic queue_depth rampup based on successful
753 * IO completion.
754 */
755 if (icmnd_cmpl->scsi_status == QUEUE_FULL) {
756 struct scsi_device *t_sdev;
757 int qd = 0;
758
759 shost_for_each_device(t_sdev, sc->device->host) {
760 if (t_sdev->id != sc->device->id)
761 continue;
762
763 if (t_sdev->queue_depth > 1) {
764 qd = scsi_track_queue_full
765 (t_sdev,
766 t_sdev->queue_depth - 1);
767 if (qd == -1)
768 qd = t_sdev->host->cmd_per_lun;
769 shost_printk(KERN_INFO,
770 fnic->lport->host,
771 "scsi[%d:%d:%d:%d"
772 "] queue full detected,"
773 "new depth = %d\n",
774 t_sdev->host->host_no,
775 t_sdev->channel,
776 t_sdev->id, t_sdev->lun,
777 t_sdev->queue_depth);
778 }
779 }
780 }
781 break;
782
783 case FCPIO_TIMEOUT: /* request was timed out */
784 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
785 break;
786
787 case FCPIO_ABORTED: /* request was aborted */
788 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
789 break;
790
791 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
792 scsi_set_resid(sc, icmnd_cmpl->residual);
793 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
794 break;
795
796 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
797 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
798 break;
799 case FCPIO_INVALID_HEADER: /* header contains invalid data */
800 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
801 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
802 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
803 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
804 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
805 case FCPIO_FW_ERR: /* request was terminated due fw error */
806 default:
807 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
808 fnic_fcpio_status_to_str(hdr_status));
809 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
810 break;
811 }
812
813 /* Break link with the SCSI command */
814 CMD_SP(sc) = NULL;
815
816 spin_unlock_irqrestore(io_lock, flags);
817
818 fnic_release_ioreq_buf(fnic, io_req, sc);
819
820 mempool_free(io_req, fnic->io_req_pool);
821
822 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
823 fnic->lport->host_stats.fcp_input_requests++;
824 fnic->fcp_input_bytes += xfer_len;
825 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
826 fnic->lport->host_stats.fcp_output_requests++;
827 fnic->fcp_output_bytes += xfer_len;
828 } else
829 fnic->lport->host_stats.fcp_control_requests++;
830
831 /* Call SCSI completion function to complete the IO */
832 if (sc->scsi_done)
833 sc->scsi_done(sc);
834
835}
836
837/* fnic_fcpio_itmf_cmpl_handler
838 * Routine to handle itmf completions
839 */
840static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
841 struct fcpio_fw_req *desc)
842{
843 u8 type;
844 u8 hdr_status;
845 struct fcpio_tag tag;
846 u32 id;
847 struct scsi_cmnd *sc;
848 struct fnic_io_req *io_req;
849 unsigned long flags;
850 spinlock_t *io_lock;
851
852 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
853 fcpio_tag_id_dec(&tag, &id);
854
Hiral Patel03298552013-02-12 17:00:58 -0800855 if ((id & FNIC_TAG_MASK) >= FNIC_MAX_IO_REQ) {
856 shost_printk(KERN_ERR, fnic->lport->host,
857 "Tag out of range tag %x hdr status = %s\n",
858 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700859 return;
Hiral Patel03298552013-02-12 17:00:58 -0800860 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700861
862 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
863 WARN_ON_ONCE(!sc);
864 if (!sc)
865 return;
866
867 io_lock = fnic_io_lock_hash(fnic, sc);
868 spin_lock_irqsave(io_lock, flags);
869 io_req = (struct fnic_io_req *)CMD_SP(sc);
870 WARN_ON_ONCE(!io_req);
871 if (!io_req) {
872 spin_unlock_irqrestore(io_lock, flags);
873 return;
874 }
875
Hiral Patel03298552013-02-12 17:00:58 -0800876 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
877 /* Abort and terminate completion of device reset req */
878 /* REVISIT : Add asserts about various flags */
879 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
880 "dev reset abts cmpl recd. id %x status %s\n",
881 id, fnic_fcpio_status_to_str(hdr_status));
882 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
883 CMD_ABTS_STATUS(sc) = hdr_status;
884 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
885 if (io_req->abts_done)
886 complete(io_req->abts_done);
887 spin_unlock_irqrestore(io_lock, flags);
888 } else if (id & FNIC_TAG_ABORT) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700889 /* Completion of abort cmd */
890 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
891 /* This is a late completion. Ignore it */
892 spin_unlock_irqrestore(io_lock, flags);
893 return;
894 }
895 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
896 CMD_ABTS_STATUS(sc) = hdr_status;
897
898 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
899 "abts cmpl recd. id %d status %s\n",
900 (int)(id & FNIC_TAG_MASK),
901 fnic_fcpio_status_to_str(hdr_status));
902
903 /*
904 * If scsi_eh thread is blocked waiting for abts to complete,
905 * signal completion to it. IO will be cleaned in the thread
906 * else clean it in this context
907 */
908 if (io_req->abts_done) {
909 complete(io_req->abts_done);
910 spin_unlock_irqrestore(io_lock, flags);
911 } else {
912 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
913 "abts cmpl, completing IO\n");
914 CMD_SP(sc) = NULL;
915 sc->result = (DID_ERROR << 16);
916
917 spin_unlock_irqrestore(io_lock, flags);
918
919 fnic_release_ioreq_buf(fnic, io_req, sc);
920 mempool_free(io_req, fnic->io_req_pool);
921 if (sc->scsi_done)
922 sc->scsi_done(sc);
923 }
924
925 } else if (id & FNIC_TAG_DEV_RST) {
926 /* Completion of device reset */
927 CMD_LR_STATUS(sc) = hdr_status;
Hiral Patel03298552013-02-12 17:00:58 -0800928 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
929 spin_unlock_irqrestore(io_lock, flags);
930 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
931 "Terminate pending "
932 "dev reset cmpl recd. id %d status %s\n",
933 (int)(id & FNIC_TAG_MASK),
934 fnic_fcpio_status_to_str(hdr_status));
935 return;
936 }
937 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
938 /* Need to wait for terminate completion */
939 spin_unlock_irqrestore(io_lock, flags);
940 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
941 "dev reset cmpl recd after time out. "
942 "id %d status %s\n",
943 (int)(id & FNIC_TAG_MASK),
944 fnic_fcpio_status_to_str(hdr_status));
945 return;
946 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700947 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
Hiral Patel03298552013-02-12 17:00:58 -0800948 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700949 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
950 "dev reset cmpl recd. id %d status %s\n",
951 (int)(id & FNIC_TAG_MASK),
952 fnic_fcpio_status_to_str(hdr_status));
953 if (io_req->dr_done)
954 complete(io_req->dr_done);
955 spin_unlock_irqrestore(io_lock, flags);
956
957 } else {
958 shost_printk(KERN_ERR, fnic->lport->host,
959 "Unexpected itmf io state %s tag %x\n",
960 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
961 spin_unlock_irqrestore(io_lock, flags);
962 }
963
964}
965
966/*
967 * fnic_fcpio_cmpl_handler
968 * Routine to service the cq for wq_copy
969 */
970static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
971 unsigned int cq_index,
972 struct fcpio_fw_req *desc)
973{
974 struct fnic *fnic = vnic_dev_priv(vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700975
976 switch (desc->hdr.type) {
977 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
978 fnic_fcpio_ack_handler(fnic, cq_index, desc);
979 break;
980
981 case FCPIO_ICMND_CMPL: /* fw completed a command */
982 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
983 break;
984
985 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
986 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
987 break;
988
989 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
Joe Eykholt78112e52009-11-03 11:49:22 -0800990 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
Hiral Patel03298552013-02-12 17:00:58 -0800991 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700992 break;
993
994 case FCPIO_RESET_CMPL: /* fw completed reset */
Hiral Patel03298552013-02-12 17:00:58 -0800995 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700996 break;
997
998 default:
999 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1000 "firmware completion type %d\n",
1001 desc->hdr.type);
1002 break;
1003 }
1004
Hiral Patel03298552013-02-12 17:00:58 -08001005 return 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001006}
1007
1008/*
1009 * fnic_wq_copy_cmpl_handler
1010 * Routine to process wq copy
1011 */
1012int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1013{
1014 unsigned int wq_work_done = 0;
1015 unsigned int i, cq_index;
1016 unsigned int cur_work_done;
1017
1018 for (i = 0; i < fnic->wq_copy_count; i++) {
1019 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1020 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1021 fnic_fcpio_cmpl_handler,
1022 copy_work_to_do);
1023 wq_work_done += cur_work_done;
1024 }
1025 return wq_work_done;
1026}
1027
1028static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1029{
1030 unsigned int i;
1031 struct fnic_io_req *io_req;
1032 unsigned long flags = 0;
1033 struct scsi_cmnd *sc;
1034 spinlock_t *io_lock;
1035
1036 for (i = 0; i < FNIC_MAX_IO_REQ; i++) {
1037 if (i == exclude_id)
1038 continue;
1039
1040 sc = scsi_host_find_tag(fnic->lport->host, i);
1041 if (!sc)
1042 continue;
1043
1044 io_lock = fnic_io_lock_hash(fnic, sc);
1045 spin_lock_irqsave(io_lock, flags);
1046 io_req = (struct fnic_io_req *)CMD_SP(sc);
Hiral Patel03298552013-02-12 17:00:58 -08001047 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1048 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1049 /*
1050 * We will be here only when FW completes reset
1051 * without sending completions for outstanding ios.
1052 */
1053 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1054 if (io_req && io_req->dr_done)
1055 complete(io_req->dr_done);
1056 else if (io_req && io_req->abts_done)
1057 complete(io_req->abts_done);
1058 spin_unlock_irqrestore(io_lock, flags);
1059 continue;
1060 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1061 spin_unlock_irqrestore(io_lock, flags);
1062 continue;
1063 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001064 if (!io_req) {
1065 spin_unlock_irqrestore(io_lock, flags);
1066 goto cleanup_scsi_cmd;
1067 }
1068
1069 CMD_SP(sc) = NULL;
1070
1071 spin_unlock_irqrestore(io_lock, flags);
1072
1073 /*
1074 * If there is a scsi_cmnd associated with this io_req, then
1075 * free the corresponding state
1076 */
1077 fnic_release_ioreq_buf(fnic, io_req, sc);
1078 mempool_free(io_req, fnic->io_req_pool);
1079
1080cleanup_scsi_cmd:
1081 sc->result = DID_TRANSPORT_DISRUPTED << 16;
1082 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "fnic_cleanup_io:"
1083 " DID_TRANSPORT_DISRUPTED\n");
1084
1085 /* Complete the command to SCSI */
1086 if (sc->scsi_done)
1087 sc->scsi_done(sc);
1088 }
1089}
1090
1091void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1092 struct fcpio_host_req *desc)
1093{
1094 u32 id;
1095 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1096 struct fnic_io_req *io_req;
1097 struct scsi_cmnd *sc;
1098 unsigned long flags;
1099 spinlock_t *io_lock;
1100
1101 /* get the tag reference */
1102 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1103 id &= FNIC_TAG_MASK;
1104
1105 if (id >= FNIC_MAX_IO_REQ)
1106 return;
1107
1108 sc = scsi_host_find_tag(fnic->lport->host, id);
1109 if (!sc)
1110 return;
1111
1112 io_lock = fnic_io_lock_hash(fnic, sc);
1113 spin_lock_irqsave(io_lock, flags);
1114
1115 /* Get the IO context which this desc refers to */
1116 io_req = (struct fnic_io_req *)CMD_SP(sc);
1117
1118 /* fnic interrupts are turned off by now */
1119
1120 if (!io_req) {
1121 spin_unlock_irqrestore(io_lock, flags);
1122 goto wq_copy_cleanup_scsi_cmd;
1123 }
1124
1125 CMD_SP(sc) = NULL;
1126
1127 spin_unlock_irqrestore(io_lock, flags);
1128
1129 fnic_release_ioreq_buf(fnic, io_req, sc);
1130 mempool_free(io_req, fnic->io_req_pool);
1131
1132wq_copy_cleanup_scsi_cmd:
1133 sc->result = DID_NO_CONNECT << 16;
1134 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1135 " DID_NO_CONNECT\n");
1136
1137 if (sc->scsi_done)
1138 sc->scsi_done(sc);
1139}
1140
1141static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1142 u32 task_req, u8 *fc_lun,
1143 struct fnic_io_req *io_req)
1144{
1145 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001146 struct Scsi_Host *host = fnic->lport->host;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001147 unsigned long flags;
1148
Hiral Patel03298552013-02-12 17:00:58 -08001149 spin_lock_irqsave(host->host_lock, flags);
1150 if (unlikely(fnic_chk_state_flags_locked(fnic,
1151 FNIC_FLAGS_IO_BLOCKED))) {
1152 spin_unlock_irqrestore(host->host_lock, flags);
1153 return 1;
1154 } else
1155 atomic_inc(&fnic->in_flight);
1156 spin_unlock_irqrestore(host->host_lock, flags);
1157
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001158 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1159
1160 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1161 free_wq_copy_descs(fnic, wq);
1162
1163 if (!vnic_wq_copy_desc_avail(wq)) {
1164 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001165 atomic_dec(&fnic->in_flight);
1166 shost_printk(KERN_DEBUG, fnic->lport->host,
1167 "fnic_queue_abort_io_req: failure: no descriptors\n");
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001168 return 1;
1169 }
1170 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1171 0, task_req, tag, fc_lun, io_req->port_id,
1172 fnic->config.ra_tov, fnic->config.ed_tov);
1173
1174 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001175 atomic_dec(&fnic->in_flight);
1176
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001177 return 0;
1178}
1179
Hiral Patel03298552013-02-12 17:00:58 -08001180static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001181{
1182 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001183 int abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001184 struct fnic_io_req *io_req;
1185 spinlock_t *io_lock;
1186 unsigned long flags;
1187 struct scsi_cmnd *sc;
1188 struct scsi_lun fc_lun;
1189 enum fnic_ioreq_state old_ioreq_state;
1190
1191 FNIC_SCSI_DBG(KERN_DEBUG,
1192 fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001193 "fnic_rport_exch_reset called portid 0x%06x\n",
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001194 port_id);
1195
1196 if (fnic->in_remove)
1197 return;
1198
1199 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001200 abt_tag = tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001201 sc = scsi_host_find_tag(fnic->lport->host, tag);
1202 if (!sc)
1203 continue;
1204
1205 io_lock = fnic_io_lock_hash(fnic, sc);
1206 spin_lock_irqsave(io_lock, flags);
1207
1208 io_req = (struct fnic_io_req *)CMD_SP(sc);
1209
1210 if (!io_req || io_req->port_id != port_id) {
1211 spin_unlock_irqrestore(io_lock, flags);
1212 continue;
1213 }
1214
Hiral Patel03298552013-02-12 17:00:58 -08001215 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1216 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_PENDING))) {
1217 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1218 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1219 sc);
1220 spin_unlock_irqrestore(io_lock, flags);
1221 continue;
1222 }
1223
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001224 /*
1225 * Found IO that is still pending with firmware and
1226 * belongs to rport that went away
1227 */
1228 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1229 spin_unlock_irqrestore(io_lock, flags);
1230 continue;
1231 }
Hiral Patel03298552013-02-12 17:00:58 -08001232 if (io_req->abts_done) {
1233 shost_printk(KERN_ERR, fnic->lport->host,
1234 "fnic_rport_exch_reset: io_req->abts_done is set "
1235 "state is %s\n",
1236 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1237 }
1238
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001239 old_ioreq_state = CMD_STATE(sc);
1240 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1241 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001242 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1243 abt_tag = (tag | FNIC_TAG_DEV_RST);
1244 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1245 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1246 sc);
1247 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001248
1249 BUG_ON(io_req->abts_done);
1250
1251 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1252 "fnic_rport_reset_exch: Issuing abts\n");
1253
1254 spin_unlock_irqrestore(io_lock, flags);
1255
1256 /* Now queue the abort command to firmware */
1257 int_to_scsilun(sc->device->lun, &fc_lun);
1258
Hiral Patel03298552013-02-12 17:00:58 -08001259 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001260 FCPIO_ITMF_ABT_TASK_TERM,
1261 fc_lun.scsi_lun, io_req)) {
1262 /*
1263 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001264 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001265 * aborted later by scsi_eh, or cleaned up during
1266 * lun reset
1267 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001268 spin_lock_irqsave(io_lock, flags);
1269 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1270 CMD_STATE(sc) = old_ioreq_state;
1271 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001272 } else {
1273 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001274 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1275 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001276 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001277 }
1278 }
1279
1280}
1281
1282void fnic_terminate_rport_io(struct fc_rport *rport)
1283{
1284 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001285 int abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001286 struct fnic_io_req *io_req;
1287 spinlock_t *io_lock;
1288 unsigned long flags;
1289 struct scsi_cmnd *sc;
1290 struct scsi_lun fc_lun;
1291 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1292 struct fc_lport *lport = rdata->local_port;
1293 struct fnic *fnic = lport_priv(lport);
1294 struct fc_rport *cmd_rport;
1295 enum fnic_ioreq_state old_ioreq_state;
1296
1297 FNIC_SCSI_DBG(KERN_DEBUG,
1298 fnic->lport->host, "fnic_terminate_rport_io called"
Hiral Patel03298552013-02-12 17:00:58 -08001299 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1300 rport->port_name, rport->node_name, rport,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001301 rport->port_id);
1302
1303 if (fnic->in_remove)
1304 return;
1305
1306 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001307 abt_tag = tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001308 sc = scsi_host_find_tag(fnic->lport->host, tag);
1309 if (!sc)
1310 continue;
1311
1312 cmd_rport = starget_to_rport(scsi_target(sc->device));
1313 if (rport != cmd_rport)
1314 continue;
1315
1316 io_lock = fnic_io_lock_hash(fnic, sc);
1317 spin_lock_irqsave(io_lock, flags);
1318
1319 io_req = (struct fnic_io_req *)CMD_SP(sc);
1320
1321 if (!io_req || rport != cmd_rport) {
1322 spin_unlock_irqrestore(io_lock, flags);
1323 continue;
1324 }
1325
Hiral Patel03298552013-02-12 17:00:58 -08001326 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1327 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_PENDING))) {
1328 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1329 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1330 sc);
1331 spin_unlock_irqrestore(io_lock, flags);
1332 continue;
1333 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001334 /*
1335 * Found IO that is still pending with firmware and
1336 * belongs to rport that went away
1337 */
1338 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1339 spin_unlock_irqrestore(io_lock, flags);
1340 continue;
1341 }
Hiral Patel03298552013-02-12 17:00:58 -08001342 if (io_req->abts_done) {
1343 shost_printk(KERN_ERR, fnic->lport->host,
1344 "fnic_terminate_rport_io: io_req->abts_done is set "
1345 "state is %s\n",
1346 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1347 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001348 old_ioreq_state = CMD_STATE(sc);
1349 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1350 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001351 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1352 abt_tag = (tag | FNIC_TAG_DEV_RST);
1353 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1354 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1355 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001356
1357 BUG_ON(io_req->abts_done);
1358
1359 FNIC_SCSI_DBG(KERN_DEBUG,
1360 fnic->lport->host,
1361 "fnic_terminate_rport_io: Issuing abts\n");
1362
1363 spin_unlock_irqrestore(io_lock, flags);
1364
1365 /* Now queue the abort command to firmware */
1366 int_to_scsilun(sc->device->lun, &fc_lun);
1367
Hiral Patel03298552013-02-12 17:00:58 -08001368 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001369 FCPIO_ITMF_ABT_TASK_TERM,
1370 fc_lun.scsi_lun, io_req)) {
1371 /*
1372 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001373 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001374 * aborted later by scsi_eh, or cleaned up during
1375 * lun reset
1376 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001377 spin_lock_irqsave(io_lock, flags);
1378 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1379 CMD_STATE(sc) = old_ioreq_state;
1380 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001381 } else {
1382 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001383 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1384 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001385 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001386 }
1387 }
1388
1389}
1390
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001391/*
1392 * This function is exported to SCSI for sending abort cmnds.
1393 * A SCSI IO is represented by a io_req in the driver.
1394 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1395 */
1396int fnic_abort_cmd(struct scsi_cmnd *sc)
1397{
1398 struct fc_lport *lp;
1399 struct fnic *fnic;
1400 struct fnic_io_req *io_req;
1401 struct fc_rport *rport;
1402 spinlock_t *io_lock;
1403 unsigned long flags;
1404 int ret = SUCCESS;
1405 u32 task_req;
1406 struct scsi_lun fc_lun;
Hiral Patel03298552013-02-12 17:00:58 -08001407 int tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001408 DECLARE_COMPLETION_ONSTACK(tm_done);
1409
1410 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001411 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001412
1413 /* Get local-port, check ready and link up */
1414 lp = shost_priv(sc->device->host);
1415
1416 fnic = lport_priv(lp);
Roel Kluin0db6f432010-06-11 16:44:46 -07001417 rport = starget_to_rport(scsi_target(sc->device));
Hiral Patel03298552013-02-12 17:00:58 -08001418 tag = sc->request->tag;
1419 FNIC_SCSI_DBG(KERN_DEBUG,
1420 fnic->lport->host,
1421 "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %x flags %x\n",
1422 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1423
1424 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1425
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001426
1427 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1428 ret = FAILED;
1429 goto fnic_abort_cmd_end;
1430 }
1431
1432 /*
1433 * Avoid a race between SCSI issuing the abort and the device
1434 * completing the command.
1435 *
1436 * If the command is already completed by the fw cmpl code,
1437 * we just return SUCCESS from here. This means that the abort
1438 * succeeded. In the SCSI ML, since the timeout for command has
1439 * happened, the completion wont actually complete the command
1440 * and it will be considered as an aborted command
1441 *
1442 * The CMD_SP will not be cleared except while holding io_req_lock.
1443 */
1444 io_lock = fnic_io_lock_hash(fnic, sc);
1445 spin_lock_irqsave(io_lock, flags);
1446 io_req = (struct fnic_io_req *)CMD_SP(sc);
1447 if (!io_req) {
1448 spin_unlock_irqrestore(io_lock, flags);
1449 goto fnic_abort_cmd_end;
1450 }
1451
1452 io_req->abts_done = &tm_done;
1453
1454 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1455 spin_unlock_irqrestore(io_lock, flags);
1456 goto wait_pending;
1457 }
1458 /*
1459 * Command is still pending, need to abort it
1460 * If the firmware completes the command after this point,
1461 * the completion wont be done till mid-layer, since abort
1462 * has already started.
1463 */
1464 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1465 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1466
1467 spin_unlock_irqrestore(io_lock, flags);
1468
1469 /*
1470 * Check readiness of the remote port. If the path to remote
1471 * port is up, then send abts to the remote port to terminate
1472 * the IO. Else, just locally terminate the IO in the firmware
1473 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001474 if (fc_remote_port_chkready(rport) == 0)
1475 task_req = FCPIO_ITMF_ABT_TASK;
1476 else
1477 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1478
1479 /* Now queue the abort command to firmware */
1480 int_to_scsilun(sc->device->lun, &fc_lun);
1481
1482 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1483 fc_lun.scsi_lun, io_req)) {
1484 spin_lock_irqsave(io_lock, flags);
1485 io_req = (struct fnic_io_req *)CMD_SP(sc);
1486 if (io_req)
1487 io_req->abts_done = NULL;
1488 spin_unlock_irqrestore(io_lock, flags);
1489 ret = FAILED;
1490 goto fnic_abort_cmd_end;
1491 }
1492
1493 /*
1494 * We queued an abort IO, wait for its completion.
1495 * Once the firmware completes the abort command, it will
1496 * wake up this thread.
1497 */
1498 wait_pending:
1499 wait_for_completion_timeout(&tm_done,
1500 msecs_to_jiffies
1501 (2 * fnic->config.ra_tov +
1502 fnic->config.ed_tov));
1503
1504 /* Check the abort status */
1505 spin_lock_irqsave(io_lock, flags);
1506
1507 io_req = (struct fnic_io_req *)CMD_SP(sc);
1508 if (!io_req) {
1509 spin_unlock_irqrestore(io_lock, flags);
1510 ret = FAILED;
1511 goto fnic_abort_cmd_end;
1512 }
1513 io_req->abts_done = NULL;
1514
1515 /* fw did not complete abort, timed out */
1516 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1517 spin_unlock_irqrestore(io_lock, flags);
1518 ret = FAILED;
1519 goto fnic_abort_cmd_end;
1520 }
1521
1522 /*
1523 * firmware completed the abort, check the status,
1524 * free the io_req irrespective of failure or success
1525 */
1526 if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1527 ret = FAILED;
1528
1529 CMD_SP(sc) = NULL;
1530
1531 spin_unlock_irqrestore(io_lock, flags);
1532
1533 fnic_release_ioreq_buf(fnic, io_req, sc);
1534 mempool_free(io_req, fnic->io_req_pool);
1535
1536fnic_abort_cmd_end:
1537 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1538 "Returning from abort cmd %s\n",
1539 (ret == SUCCESS) ?
1540 "SUCCESS" : "FAILED");
1541 return ret;
1542}
1543
1544static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1545 struct scsi_cmnd *sc,
1546 struct fnic_io_req *io_req)
1547{
1548 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001549 struct Scsi_Host *host = fnic->lport->host;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001550 struct scsi_lun fc_lun;
1551 int ret = 0;
1552 unsigned long intr_flags;
1553
Hiral Patel03298552013-02-12 17:00:58 -08001554 spin_lock_irqsave(host->host_lock, intr_flags);
1555 if (unlikely(fnic_chk_state_flags_locked(fnic,
1556 FNIC_FLAGS_IO_BLOCKED))) {
1557 spin_unlock_irqrestore(host->host_lock, intr_flags);
1558 return FAILED;
1559 } else
1560 atomic_inc(&fnic->in_flight);
1561 spin_unlock_irqrestore(host->host_lock, intr_flags);
1562
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001563 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1564
1565 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1566 free_wq_copy_descs(fnic, wq);
1567
1568 if (!vnic_wq_copy_desc_avail(wq)) {
1569 ret = -EAGAIN;
1570 goto lr_io_req_end;
1571 }
1572
1573 /* fill in the lun info */
1574 int_to_scsilun(sc->device->lun, &fc_lun);
1575
1576 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1577 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1578 fc_lun.scsi_lun, io_req->port_id,
1579 fnic->config.ra_tov, fnic->config.ed_tov);
1580
1581lr_io_req_end:
1582 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel03298552013-02-12 17:00:58 -08001583 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001584
1585 return ret;
1586}
1587
1588/*
1589 * Clean up any pending aborts on the lun
1590 * For each outstanding IO on this lun, whose abort is not completed by fw,
1591 * issue a local abort. Wait for abort to complete. Return 0 if all commands
1592 * successfully aborted, 1 otherwise
1593 */
1594static int fnic_clean_pending_aborts(struct fnic *fnic,
1595 struct scsi_cmnd *lr_sc)
1596{
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001597 int tag, abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001598 struct fnic_io_req *io_req;
1599 spinlock_t *io_lock;
1600 unsigned long flags;
1601 int ret = 0;
1602 struct scsi_cmnd *sc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001603 struct scsi_lun fc_lun;
1604 struct scsi_device *lun_dev = lr_sc->device;
1605 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001606 enum fnic_ioreq_state old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001607
1608 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1609 sc = scsi_host_find_tag(fnic->lport->host, tag);
1610 /*
1611 * ignore this lun reset cmd or cmds that do not belong to
1612 * this lun
1613 */
1614 if (!sc || sc == lr_sc || sc->device != lun_dev)
1615 continue;
1616
1617 io_lock = fnic_io_lock_hash(fnic, sc);
1618 spin_lock_irqsave(io_lock, flags);
1619
1620 io_req = (struct fnic_io_req *)CMD_SP(sc);
1621
1622 if (!io_req || sc->device != lun_dev) {
1623 spin_unlock_irqrestore(io_lock, flags);
1624 continue;
1625 }
1626
1627 /*
1628 * Found IO that is still pending with firmware and
1629 * belongs to the LUN that we are resetting
1630 */
1631 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1632 "Found IO in %s on lun\n",
1633 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1634
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001635 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1636 spin_unlock_irqrestore(io_lock, flags);
1637 continue;
1638 }
1639 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1640 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_PENDING))) {
1641 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1642 "%s dev rst not pending sc 0x%p\n", __func__,
1643 sc);
1644 spin_unlock_irqrestore(io_lock, flags);
1645 continue;
1646 }
1647 old_ioreq_state = CMD_STATE(sc);
1648 /*
1649 * Any pending IO issued prior to reset is expected to be
1650 * in abts pending state, if not we need to set
1651 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
1652 * When IO is completed, the IO will be handed over and
1653 * handled in this function.
1654 */
1655 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1656
1657 if (io_req->abts_done)
1658 shost_printk(KERN_ERR, fnic->lport->host,
1659 "%s: io_req->abts_done is set state is %s\n",
1660 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
1661
1662 BUG_ON(io_req->abts_done);
1663
1664 abt_tag = tag;
1665 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1666 abt_tag |= FNIC_TAG_DEV_RST;
1667 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1668 "%s: dev rst sc 0x%p\n", __func__, sc);
1669 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001670
1671 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1672 io_req->abts_done = &tm_done;
1673 spin_unlock_irqrestore(io_lock, flags);
1674
1675 /* Now queue the abort command to firmware */
1676 int_to_scsilun(sc->device->lun, &fc_lun);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001677
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001678 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001679 FCPIO_ITMF_ABT_TASK_TERM,
1680 fc_lun.scsi_lun, io_req)) {
1681 spin_lock_irqsave(io_lock, flags);
1682 io_req = (struct fnic_io_req *)CMD_SP(sc);
1683 if (io_req)
1684 io_req->abts_done = NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001685 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1686 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001687 spin_unlock_irqrestore(io_lock, flags);
1688 ret = 1;
1689 goto clean_pending_aborts_end;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001690 } else {
1691 spin_lock_irqsave(io_lock, flags);
1692 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1693 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1694 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001695 }
1696
1697 wait_for_completion_timeout(&tm_done,
1698 msecs_to_jiffies
1699 (fnic->config.ed_tov));
1700
1701 /* Recheck cmd state to check if it is now aborted */
1702 spin_lock_irqsave(io_lock, flags);
1703 io_req = (struct fnic_io_req *)CMD_SP(sc);
1704 if (!io_req) {
1705 spin_unlock_irqrestore(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001706 continue;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001707 }
1708
1709 io_req->abts_done = NULL;
1710
1711 /* if abort is still pending with fw, fail */
1712 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1713 spin_unlock_irqrestore(io_lock, flags);
1714 ret = 1;
1715 goto clean_pending_aborts_end;
1716 }
1717 CMD_SP(sc) = NULL;
1718 spin_unlock_irqrestore(io_lock, flags);
1719
1720 fnic_release_ioreq_buf(fnic, io_req, sc);
1721 mempool_free(io_req, fnic->io_req_pool);
1722 }
1723
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001724 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
1725
1726 /* walk again to check, if IOs are still pending in fw */
1727 if (fnic_is_abts_pending(fnic, lr_sc))
1728 ret = FAILED;
1729
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001730clean_pending_aborts_end:
1731 return ret;
1732}
1733
Hiral Patel03298552013-02-12 17:00:58 -08001734/**
1735 * fnic_scsi_host_start_tag
1736 * Allocates tagid from host's tag list
1737 **/
1738static inline int
1739fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
1740{
1741 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
1742 int tag, ret = SCSI_NO_TAG;
1743
1744 BUG_ON(!bqt);
1745 if (!bqt) {
1746 pr_err("Tags are not supported\n");
1747 goto end;
1748 }
1749
1750 do {
1751 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
1752 if (tag >= bqt->max_depth) {
1753 pr_err("Tag allocation failure\n");
1754 goto end;
1755 }
1756 } while (test_and_set_bit(tag, bqt->tag_map));
1757
1758 bqt->tag_index[tag] = sc->request;
1759 sc->request->tag = tag;
1760 sc->tag = tag;
1761 if (!sc->request->special)
1762 sc->request->special = sc;
1763
1764 ret = tag;
1765
1766end:
1767 return ret;
1768}
1769
1770/**
1771 * fnic_scsi_host_end_tag
1772 * frees tag allocated by fnic_scsi_host_start_tag.
1773 **/
1774static inline void
1775fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
1776{
1777 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
1778 int tag = sc->request->tag;
1779
1780 if (tag == SCSI_NO_TAG)
1781 return;
1782
1783 BUG_ON(!bqt || !bqt->tag_index[tag]);
1784 if (!bqt)
1785 return;
1786
1787 bqt->tag_index[tag] = NULL;
1788 clear_bit(tag, bqt->tag_map);
1789
1790 return;
1791}
1792
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001793/*
1794 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
1795 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
1796 * on the LUN.
1797 */
1798int fnic_device_reset(struct scsi_cmnd *sc)
1799{
1800 struct fc_lport *lp;
1801 struct fnic *fnic;
1802 struct fnic_io_req *io_req;
1803 struct fc_rport *rport;
1804 int status;
1805 int ret = FAILED;
1806 spinlock_t *io_lock;
1807 unsigned long flags;
Hiral Patel03298552013-02-12 17:00:58 -08001808 struct scsi_lun fc_lun;
1809 int tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001810 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patel03298552013-02-12 17:00:58 -08001811 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001812
1813 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001814 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001815
1816 /* Get local-port, check ready and link up */
1817 lp = shost_priv(sc->device->host);
1818
1819 fnic = lport_priv(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001820
Roel Kluin0db6f432010-06-11 16:44:46 -07001821 rport = starget_to_rport(scsi_target(sc->device));
1822 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001823 "Device reset called FCID 0x%x, LUN 0x%x sc 0x%p\n",
1824 rport->port_id, sc->device->lun, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001825
1826 if (lp->state != LPORT_ST_READY || !(lp->link_up))
1827 goto fnic_device_reset_end;
1828
1829 /* Check if remote port up */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001830 if (fc_remote_port_chkready(rport))
1831 goto fnic_device_reset_end;
1832
Hiral Patel03298552013-02-12 17:00:58 -08001833 CMD_FLAGS(sc) = (FNIC_DEVICE_RESET | FNIC_BLOCKING_REQ);
1834 /* Allocate tag if not present */
1835
1836 tag = sc->request->tag;
1837 if (unlikely(tag < 0)) {
1838 tag = fnic_scsi_host_start_tag(fnic, sc);
1839 if (unlikely(tag == SCSI_NO_TAG))
1840 goto fnic_device_reset_end;
1841 tag_gen_flag = 1;
1842 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001843 io_lock = fnic_io_lock_hash(fnic, sc);
1844 spin_lock_irqsave(io_lock, flags);
1845 io_req = (struct fnic_io_req *)CMD_SP(sc);
1846
1847 /*
1848 * If there is a io_req attached to this command, then use it,
1849 * else allocate a new one.
1850 */
1851 if (!io_req) {
1852 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
1853 if (!io_req) {
1854 spin_unlock_irqrestore(io_lock, flags);
1855 goto fnic_device_reset_end;
1856 }
1857 memset(io_req, 0, sizeof(*io_req));
1858 io_req->port_id = rport->port_id;
1859 CMD_SP(sc) = (char *)io_req;
1860 }
1861 io_req->dr_done = &tm_done;
1862 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
1863 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
1864 spin_unlock_irqrestore(io_lock, flags);
1865
Hiral Patel03298552013-02-12 17:00:58 -08001866 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001867
1868 /*
1869 * issue the device reset, if enqueue failed, clean up the ioreq
1870 * and break assoc with scsi cmd
1871 */
1872 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
1873 spin_lock_irqsave(io_lock, flags);
1874 io_req = (struct fnic_io_req *)CMD_SP(sc);
1875 if (io_req)
1876 io_req->dr_done = NULL;
1877 goto fnic_device_reset_clean;
1878 }
Hiral Patel03298552013-02-12 17:00:58 -08001879 spin_lock_irqsave(io_lock, flags);
1880 CMD_FLAGS(sc) |= FNIC_DEV_RST_PENDING;
1881 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001882
1883 /*
1884 * Wait on the local completion for LUN reset. The io_req may be
1885 * freed while we wait since we hold no lock.
1886 */
1887 wait_for_completion_timeout(&tm_done,
1888 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
1889
1890 spin_lock_irqsave(io_lock, flags);
1891 io_req = (struct fnic_io_req *)CMD_SP(sc);
1892 if (!io_req) {
1893 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001894 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1895 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001896 goto fnic_device_reset_end;
1897 }
1898 io_req->dr_done = NULL;
1899
1900 status = CMD_LR_STATUS(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001901
1902 /*
1903 * If lun reset not completed, bail out with failed. io_req
1904 * gets cleaned up during higher levels of EH
1905 */
1906 if (status == FCPIO_INVALID_CODE) {
1907 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1908 "Device reset timed out\n");
Hiral Patel03298552013-02-12 17:00:58 -08001909 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
1910 spin_unlock_irqrestore(io_lock, flags);
1911 int_to_scsilun(sc->device->lun, &fc_lun);
1912 /*
1913 * Issue abort and terminate on the device reset request.
1914 * If q'ing of the abort fails, retry issue it after a delay.
1915 */
1916 while (1) {
1917 spin_lock_irqsave(io_lock, flags);
1918 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
1919 spin_unlock_irqrestore(io_lock, flags);
1920 break;
1921 }
1922 spin_unlock_irqrestore(io_lock, flags);
1923 if (fnic_queue_abort_io_req(fnic,
1924 tag | FNIC_TAG_DEV_RST,
1925 FCPIO_ITMF_ABT_TASK_TERM,
1926 fc_lun.scsi_lun, io_req)) {
1927 wait_for_completion_timeout(&tm_done,
1928 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
1929 } else {
1930 spin_lock_irqsave(io_lock, flags);
1931 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1932 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1933 io_req->abts_done = &tm_done;
1934 spin_unlock_irqrestore(io_lock, flags);
1935 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1936 "Abort and terminate issued on Device reset "
1937 "tag 0x%x sc 0x%p\n", tag, sc);
1938 break;
1939 }
1940 }
1941 while (1) {
1942 spin_lock_irqsave(io_lock, flags);
1943 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1944 spin_unlock_irqrestore(io_lock, flags);
1945 wait_for_completion_timeout(&tm_done,
1946 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
1947 break;
1948 } else {
1949 io_req = (struct fnic_io_req *)CMD_SP(sc);
1950 io_req->abts_done = NULL;
1951 goto fnic_device_reset_clean;
1952 }
1953 }
1954 } else {
1955 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001956 }
1957
1958 /* Completed, but not successful, clean up the io_req, return fail */
1959 if (status != FCPIO_SUCCESS) {
1960 spin_lock_irqsave(io_lock, flags);
1961 FNIC_SCSI_DBG(KERN_DEBUG,
1962 fnic->lport->host,
1963 "Device reset completed - failed\n");
1964 io_req = (struct fnic_io_req *)CMD_SP(sc);
1965 goto fnic_device_reset_clean;
1966 }
1967
1968 /*
1969 * Clean up any aborts on this lun that have still not
1970 * completed. If any of these fail, then LUN reset fails.
1971 * clean_pending_aborts cleans all cmds on this lun except
1972 * the lun reset cmd. If all cmds get cleaned, the lun reset
1973 * succeeds
1974 */
1975 if (fnic_clean_pending_aborts(fnic, sc)) {
1976 spin_lock_irqsave(io_lock, flags);
1977 io_req = (struct fnic_io_req *)CMD_SP(sc);
1978 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1979 "Device reset failed"
1980 " since could not abort all IOs\n");
1981 goto fnic_device_reset_clean;
1982 }
1983
1984 /* Clean lun reset command */
1985 spin_lock_irqsave(io_lock, flags);
1986 io_req = (struct fnic_io_req *)CMD_SP(sc);
1987 if (io_req)
1988 /* Completed, and successful */
1989 ret = SUCCESS;
1990
1991fnic_device_reset_clean:
1992 if (io_req)
1993 CMD_SP(sc) = NULL;
1994
1995 spin_unlock_irqrestore(io_lock, flags);
1996
1997 if (io_req) {
1998 fnic_release_ioreq_buf(fnic, io_req, sc);
1999 mempool_free(io_req, fnic->io_req_pool);
2000 }
2001
2002fnic_device_reset_end:
Hiral Patel03298552013-02-12 17:00:58 -08002003 /* free tag if it is allocated */
2004 if (unlikely(tag_gen_flag))
2005 fnic_scsi_host_end_tag(fnic, sc);
2006
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002007 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2008 "Returning from device reset %s\n",
2009 (ret == SUCCESS) ?
2010 "SUCCESS" : "FAILED");
2011 return ret;
2012}
2013
2014/* Clean up all IOs, clean up libFC local port */
2015int fnic_reset(struct Scsi_Host *shost)
2016{
2017 struct fc_lport *lp;
2018 struct fnic *fnic;
2019 int ret = SUCCESS;
2020
2021 lp = shost_priv(shost);
2022 fnic = lport_priv(lp);
2023
2024 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2025 "fnic_reset called\n");
2026
2027 /*
2028 * Reset local port, this will clean up libFC exchanges,
2029 * reset remote port sessions, and if link is up, begin flogi
2030 */
2031 if (lp->tt.lport_reset(lp))
2032 ret = FAILED;
2033
2034 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2035 "Returning from fnic reset %s\n",
2036 (ret == SUCCESS) ?
2037 "SUCCESS" : "FAILED");
2038
2039 return ret;
2040}
2041
2042/*
2043 * SCSI Error handling calls driver's eh_host_reset if all prior
2044 * error handling levels return FAILED. If host reset completes
2045 * successfully, and if link is up, then Fabric login begins.
2046 *
2047 * Host Reset is the highest level of error recovery. If this fails, then
2048 * host is offlined by SCSI.
2049 *
2050 */
2051int fnic_host_reset(struct scsi_cmnd *sc)
2052{
2053 int ret;
2054 unsigned long wait_host_tmo;
2055 struct Scsi_Host *shost = sc->device->host;
2056 struct fc_lport *lp = shost_priv(shost);
2057
2058 /*
2059 * If fnic_reset is successful, wait for fabric login to complete
2060 * scsi-ml tries to send a TUR to every device if host reset is
2061 * successful, so before returning to scsi, fabric should be up
2062 */
2063 ret = fnic_reset(shost);
2064 if (ret == SUCCESS) {
2065 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2066 ret = FAILED;
2067 while (time_before(jiffies, wait_host_tmo)) {
2068 if ((lp->state == LPORT_ST_READY) &&
2069 (lp->link_up)) {
2070 ret = SUCCESS;
2071 break;
2072 }
2073 ssleep(1);
2074 }
2075 }
2076
2077 return ret;
2078}
2079
2080/*
2081 * This fxn is called from libFC when host is removed
2082 */
2083void fnic_scsi_abort_io(struct fc_lport *lp)
2084{
2085 int err = 0;
2086 unsigned long flags;
2087 enum fnic_state old_state;
2088 struct fnic *fnic = lport_priv(lp);
2089 DECLARE_COMPLETION_ONSTACK(remove_wait);
2090
2091 /* Issue firmware reset for fnic, wait for reset to complete */
Hiral Patel03298552013-02-12 17:00:58 -08002092retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002093 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002094 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2095 /* fw reset is in progress, poll for its completion */
2096 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2097 schedule_timeout(msecs_to_jiffies(100));
2098 goto retry_fw_reset;
2099 }
2100
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002101 fnic->remove_wait = &remove_wait;
2102 old_state = fnic->state;
2103 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002104 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002105 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2106
2107 err = fnic_fw_reset_handler(fnic);
2108 if (err) {
2109 spin_lock_irqsave(&fnic->fnic_lock, flags);
2110 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2111 fnic->state = old_state;
2112 fnic->remove_wait = NULL;
2113 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2114 return;
2115 }
2116
2117 /* Wait for firmware reset to complete */
2118 wait_for_completion_timeout(&remove_wait,
2119 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2120
2121 spin_lock_irqsave(&fnic->fnic_lock, flags);
2122 fnic->remove_wait = NULL;
2123 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2124 "fnic_scsi_abort_io %s\n",
2125 (fnic->state == FNIC_IN_ETH_MODE) ?
2126 "SUCCESS" : "FAILED");
2127 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2128
2129}
2130
2131/*
2132 * This fxn called from libFC to clean up driver IO state on link down
2133 */
2134void fnic_scsi_cleanup(struct fc_lport *lp)
2135{
2136 unsigned long flags;
2137 enum fnic_state old_state;
2138 struct fnic *fnic = lport_priv(lp);
2139
2140 /* issue fw reset */
Hiral Patel03298552013-02-12 17:00:58 -08002141retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002142 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002143 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2144 /* fw reset is in progress, poll for its completion */
2145 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2146 schedule_timeout(msecs_to_jiffies(100));
2147 goto retry_fw_reset;
2148 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002149 old_state = fnic->state;
2150 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002151 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002152 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2153
2154 if (fnic_fw_reset_handler(fnic)) {
2155 spin_lock_irqsave(&fnic->fnic_lock, flags);
2156 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2157 fnic->state = old_state;
2158 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2159 }
2160
2161}
2162
2163void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2164{
2165}
2166
2167void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2168{
2169 struct fnic *fnic = lport_priv(lp);
2170
2171 /* Non-zero sid, nothing to do */
2172 if (sid)
2173 goto call_fc_exch_mgr_reset;
2174
2175 if (did) {
2176 fnic_rport_exch_reset(fnic, did);
2177 goto call_fc_exch_mgr_reset;
2178 }
2179
2180 /*
2181 * sid = 0, did = 0
2182 * link down or device being removed
2183 */
2184 if (!fnic->in_remove)
2185 fnic_scsi_cleanup(lp);
2186 else
2187 fnic_scsi_abort_io(lp);
2188
2189 /* call libFC exch mgr reset to reset its exchanges */
2190call_fc_exch_mgr_reset:
2191 fc_exch_mgr_reset(lp, sid, did);
2192
2193}
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002194
2195/*
2196 * fnic_is_abts_pending() is a helper function that
2197 * walks through tag map to check if there is any IOs pending,if there is one,
2198 * then it returns 1 (true), otherwise 0 (false)
2199 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2200 * otherwise, it checks for all IOs.
2201 */
2202int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2203{
2204 int tag;
2205 struct fnic_io_req *io_req;
2206 spinlock_t *io_lock;
2207 unsigned long flags;
2208 int ret = 0;
2209 struct scsi_cmnd *sc;
2210 struct scsi_device *lun_dev = NULL;
2211
2212 if (lr_sc)
2213 lun_dev = lr_sc->device;
2214
2215 /* walk again to check, if IOs are still pending in fw */
2216 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
2217 sc = scsi_host_find_tag(fnic->lport->host, tag);
2218 /*
2219 * ignore this lun reset cmd or cmds that do not belong to
2220 * this lun
2221 */
2222 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2223 continue;
2224
2225 io_lock = fnic_io_lock_hash(fnic, sc);
2226 spin_lock_irqsave(io_lock, flags);
2227
2228 io_req = (struct fnic_io_req *)CMD_SP(sc);
2229
2230 if (!io_req || sc->device != lun_dev) {
2231 spin_unlock_irqrestore(io_lock, flags);
2232 continue;
2233 }
2234
2235 /*
2236 * Found IO that is still pending with firmware and
2237 * belongs to the LUN that we are resetting
2238 */
2239 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2240 "Found IO in %s on lun\n",
2241 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2242
2243 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2244 spin_unlock_irqrestore(io_lock, flags);
2245 ret = 1;
2246 continue;
2247 }
2248 }
2249
2250 return ret;
2251}