blob: 7cb653309125b6f9cb86ce984273278109efc886 [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
114/*
115 * Unmap the data buffer and sense buffer for an io_req,
116 * also unmap and free the device-private scatter/gather list.
117 */
118static void fnic_release_ioreq_buf(struct fnic *fnic,
119 struct fnic_io_req *io_req,
120 struct scsi_cmnd *sc)
121{
122 if (io_req->sgl_list_pa)
123 pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
124 sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
125 PCI_DMA_TODEVICE);
126 scsi_dma_unmap(sc);
127
128 if (io_req->sgl_cnt)
129 mempool_free(io_req->sgl_list_alloc,
130 fnic->io_sgl_pool[io_req->sgl_type]);
131 if (io_req->sense_buf_pa)
132 pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
133 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
134}
135
136/* Free up Copy Wq descriptors. Called with copy_wq lock held */
137static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
138{
139 /* if no Ack received from firmware, then nothing to clean */
140 if (!fnic->fw_ack_recd[0])
141 return 1;
142
143 /*
144 * Update desc_available count based on number of freed descriptors
145 * Account for wraparound
146 */
147 if (wq->to_clean_index <= fnic->fw_ack_index[0])
148 wq->ring.desc_avail += (fnic->fw_ack_index[0]
149 - wq->to_clean_index + 1);
150 else
151 wq->ring.desc_avail += (wq->ring.desc_count
152 - wq->to_clean_index
153 + fnic->fw_ack_index[0] + 1);
154
155 /*
156 * just bump clean index to ack_index+1 accounting for wraparound
157 * this will essentially free up all descriptors between
158 * to_clean_index and fw_ack_index, both inclusive
159 */
160 wq->to_clean_index =
161 (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
162
163 /* we have processed the acks received so far */
164 fnic->fw_ack_recd[0] = 0;
165 return 0;
166}
167
168
Hiral Patel03298552013-02-12 17:00:58 -0800169/**
170 * __fnic_set_state_flags
171 * Sets/Clears bits in fnic's state_flags
172 **/
173void
174__fnic_set_state_flags(struct fnic *fnic, unsigned long st_flags,
175 unsigned long clearbits)
176{
177 struct Scsi_Host *host = fnic->lport->host;
178 int sh_locked = spin_is_locked(host->host_lock);
179 unsigned long flags = 0;
180
181 if (!sh_locked)
182 spin_lock_irqsave(host->host_lock, flags);
183
184 if (clearbits)
185 fnic->state_flags &= ~st_flags;
186 else
187 fnic->state_flags |= st_flags;
188
189 if (!sh_locked)
190 spin_unlock_irqrestore(host->host_lock, flags);
191
192 return;
193}
194
195
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700196/*
197 * fnic_fw_reset_handler
198 * Routine to send reset msg to fw
199 */
200int fnic_fw_reset_handler(struct fnic *fnic)
201{
202 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
203 int ret = 0;
204 unsigned long flags;
205
Hiral Patel03298552013-02-12 17:00:58 -0800206 /* indicate fwreset to io path */
207 fnic_set_state_flags(fnic, FNIC_FLAGS_FWRESET);
208
Joe Eykholt78112e52009-11-03 11:49:22 -0800209 skb_queue_purge(&fnic->frame_queue);
210 skb_queue_purge(&fnic->tx_queue);
211
Hiral Patel03298552013-02-12 17:00:58 -0800212 /* wait for io cmpl */
213 while (atomic_read(&fnic->in_flight))
214 schedule_timeout(msecs_to_jiffies(1));
215
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700216 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
217
218 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
219 free_wq_copy_descs(fnic, wq);
220
221 if (!vnic_wq_copy_desc_avail(wq))
222 ret = -EAGAIN;
223 else
224 fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
225
226 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
227
228 if (!ret)
229 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
230 "Issued fw reset\n");
Hiral Patel03298552013-02-12 17:00:58 -0800231 else {
232 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700233 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
234 "Failed to issue fw reset\n");
Hiral Patel03298552013-02-12 17:00:58 -0800235 }
236
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700237 return ret;
238}
239
240
241/*
242 * fnic_flogi_reg_handler
243 * Routine to send flogi register msg to fw
244 */
Joe Eykholt78112e52009-11-03 11:49:22 -0800245int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700246{
247 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Joe Eykholt78112e52009-11-03 11:49:22 -0800248 enum fcpio_flogi_reg_format_type format;
249 struct fc_lport *lp = fnic->lport;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700250 u8 gw_mac[ETH_ALEN];
251 int ret = 0;
252 unsigned long flags;
253
254 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
255
256 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
257 free_wq_copy_descs(fnic, wq);
258
259 if (!vnic_wq_copy_desc_avail(wq)) {
260 ret = -EAGAIN;
261 goto flogi_reg_ioreq_end;
262 }
263
Joe Eykholt78112e52009-11-03 11:49:22 -0800264 if (fnic->ctlr.map_dest) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700265 memset(gw_mac, 0xff, ETH_ALEN);
Joe Eykholt78112e52009-11-03 11:49:22 -0800266 format = FCPIO_FLOGI_REG_DEF_DEST;
267 } else {
268 memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
269 format = FCPIO_FLOGI_REG_GW_DEST;
270 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700271
Joe Eykholt78112e52009-11-03 11:49:22 -0800272 if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
273 fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
274 fc_id, gw_mac,
275 fnic->data_src_addr,
276 lp->r_a_tov, lp->e_d_tov);
277 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
278 "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
279 fc_id, fnic->data_src_addr, gw_mac);
280 } else {
281 fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
282 format, fc_id, gw_mac);
283 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
284 "FLOGI reg issued fcid %x map %d dest %pM\n",
285 fc_id, fnic->ctlr.map_dest, gw_mac);
286 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700287
288flogi_reg_ioreq_end:
289 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700290 return ret;
291}
292
293/*
294 * fnic_queue_wq_copy_desc
295 * Routine to enqueue a wq copy desc
296 */
297static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
298 struct vnic_wq_copy *wq,
299 struct fnic_io_req *io_req,
300 struct scsi_cmnd *sc,
Roel Kluin87a2d342009-06-23 01:06:40 +0200301 int sg_count)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700302{
303 struct scatterlist *sg;
304 struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
305 struct fc_rport_libfc_priv *rp = rport->dd_data;
306 struct host_sg_desc *desc;
307 u8 pri_tag = 0;
308 unsigned int i;
309 unsigned long intr_flags;
310 int flags;
311 u8 exch_flags;
312 struct scsi_lun fc_lun;
313 char msg[2];
314
315 if (sg_count) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700316 /* For each SGE, create a device desc entry */
317 desc = io_req->sgl_list;
318 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
319 desc->addr = cpu_to_le64(sg_dma_address(sg));
320 desc->len = cpu_to_le32(sg_dma_len(sg));
321 desc->_resvd = 0;
322 desc++;
323 }
324
325 io_req->sgl_list_pa = pci_map_single
326 (fnic->pdev,
327 io_req->sgl_list,
328 sizeof(io_req->sgl_list[0]) * sg_count,
329 PCI_DMA_TODEVICE);
330 }
331
332 io_req->sense_buf_pa = pci_map_single(fnic->pdev,
333 sc->sense_buffer,
334 SCSI_SENSE_BUFFERSIZE,
335 PCI_DMA_FROMDEVICE);
336
337 int_to_scsilun(sc->device->lun, &fc_lun);
338
339 pri_tag = FCPIO_ICMND_PTA_SIMPLE;
340 msg[0] = MSG_SIMPLE_TAG;
341 scsi_populate_tag_msg(sc, msg);
342 if (msg[0] == MSG_ORDERED_TAG)
343 pri_tag = FCPIO_ICMND_PTA_ORDERED;
344
345 /* Enqueue the descriptor in the Copy WQ */
346 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
347
348 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
349 free_wq_copy_descs(fnic, wq);
350
351 if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
352 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800353 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
354 "fnic_queue_wq_copy_desc failure - no descriptors\n");
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700355 return SCSI_MLQUEUE_HOST_BUSY;
356 }
357
358 flags = 0;
359 if (sc->sc_data_direction == DMA_FROM_DEVICE)
360 flags = FCPIO_ICMND_RDDATA;
361 else if (sc->sc_data_direction == DMA_TO_DEVICE)
362 flags = FCPIO_ICMND_WRDATA;
363
364 exch_flags = 0;
365 if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
366 (rp->flags & FC_RP_FLAGS_RETRY))
367 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
368
369 fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
370 0, exch_flags, io_req->sgl_cnt,
371 SCSI_SENSE_BUFFERSIZE,
372 io_req->sgl_list_pa,
373 io_req->sense_buf_pa,
374 0, /* scsi cmd ref, always 0 */
375 pri_tag, /* scsi pri and tag */
376 flags, /* command flags */
Abhijeet Joglekar4b536622009-10-21 16:28:25 -0700377 sc->cmnd, sc->cmd_len,
378 scsi_bufflen(sc),
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700379 fc_lun.scsi_lun, io_req->port_id,
380 rport->maxframe_size, rp->r_a_tov,
381 rp->e_d_tov);
382
383 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
384 return 0;
385}
386
387/*
388 * fnic_queuecommand
389 * Routine to send a scsi cdb
390 * Called with host_lock held and interrupts disabled.
391 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500392static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700393{
Hiral Patel03298552013-02-12 17:00:58 -0800394 struct fc_lport *lp = shost_priv(sc->device->host);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700395 struct fc_rport *rport;
396 struct fnic_io_req *io_req;
Hiral Patel03298552013-02-12 17:00:58 -0800397 struct fnic *fnic = lport_priv(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700398 struct vnic_wq_copy *wq;
399 int ret;
Roel Kluin87a2d342009-06-23 01:06:40 +0200400 int sg_count;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700401 unsigned long flags;
402 unsigned long ptr;
403
Hiral Patel03298552013-02-12 17:00:58 -0800404 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
405 return SCSI_MLQUEUE_HOST_BUSY;
406
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700407 rport = starget_to_rport(scsi_target(sc->device));
408 ret = fc_remote_port_chkready(rport);
409 if (ret) {
410 sc->result = ret;
411 done(sc);
412 return 0;
413 }
414
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700415 if (lp->state != LPORT_ST_READY || !(lp->link_up))
416 return SCSI_MLQUEUE_HOST_BUSY;
417
Hiral Patel03298552013-02-12 17:00:58 -0800418 atomic_inc(&fnic->in_flight);
419
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700420 /*
421 * Release host lock, use driver resource specific locks from here.
422 * Don't re-enable interrupts in case they were disabled prior to the
423 * caller disabling them.
424 */
425 spin_unlock(lp->host->host_lock);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800426 CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
427 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700428
429 /* Get a new io_req for this SCSI IO */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700430 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
431 if (!io_req) {
432 ret = SCSI_MLQUEUE_HOST_BUSY;
433 goto out;
434 }
435 memset(io_req, 0, sizeof(*io_req));
436
437 /* Map the data buffer */
438 sg_count = scsi_dma_map(sc);
439 if (sg_count < 0) {
440 mempool_free(io_req, fnic->io_req_pool);
441 goto out;
442 }
443
444 /* Determine the type of scatter/gather list we need */
445 io_req->sgl_cnt = sg_count;
446 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
447 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
448 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
449
450 if (sg_count) {
451 io_req->sgl_list =
452 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
Abhijeet Joglekar0c79c742011-06-13 21:21:01 -0700453 GFP_ATOMIC);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700454 if (!io_req->sgl_list) {
455 ret = SCSI_MLQUEUE_HOST_BUSY;
456 scsi_dma_unmap(sc);
457 mempool_free(io_req, fnic->io_req_pool);
458 goto out;
459 }
460
461 /* Cache sgl list allocated address before alignment */
462 io_req->sgl_list_alloc = io_req->sgl_list;
463 ptr = (unsigned long) io_req->sgl_list;
464 if (ptr % FNIC_SG_DESC_ALIGN) {
465 io_req->sgl_list = (struct host_sg_desc *)
466 (((unsigned long) ptr
467 + FNIC_SG_DESC_ALIGN - 1)
468 & ~(FNIC_SG_DESC_ALIGN - 1));
469 }
470 }
471
472 /* initialize rest of io_req */
473 io_req->port_id = rport->port_id;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800474 io_req->start_time = jiffies;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700475 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
476 CMD_SP(sc) = (char *)io_req;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800477 CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700478 sc->scsi_done = done;
479
480 /* create copy wq desc and enqueue it */
481 wq = &fnic->wq_copy[0];
482 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
483 if (ret) {
484 /*
485 * In case another thread cancelled the request,
486 * refetch the pointer under the lock.
487 */
488 spinlock_t *io_lock = fnic_io_lock_hash(fnic, sc);
489
490 spin_lock_irqsave(io_lock, flags);
491 io_req = (struct fnic_io_req *)CMD_SP(sc);
492 CMD_SP(sc) = NULL;
493 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
494 spin_unlock_irqrestore(io_lock, flags);
495 if (io_req) {
496 fnic_release_ioreq_buf(fnic, io_req, sc);
497 mempool_free(io_req, fnic->io_req_pool);
498 }
Hiral Patel14eb5d92013-02-12 17:01:01 -0800499 } else {
500 /* REVISIT: Use per IO lock in the final code */
501 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700502 }
503out:
Hiral Patel03298552013-02-12 17:00:58 -0800504 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700505 /* acquire host lock before returning to SCSI */
506 spin_lock(lp->host->host_lock);
507 return ret;
508}
509
Jeff Garzikf2812332010-11-16 02:10:29 -0500510DEF_SCSI_QCMD(fnic_queuecommand)
511
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700512/*
513 * fnic_fcpio_fw_reset_cmpl_handler
514 * Routine to handle fw reset completion
515 */
516static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
517 struct fcpio_fw_req *desc)
518{
519 u8 type;
520 u8 hdr_status;
521 struct fcpio_tag tag;
522 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700523 unsigned long flags;
524
525 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
526
527 /* Clean up all outstanding io requests */
528 fnic_cleanup_io(fnic, SCSI_NO_TAG);
529
530 spin_lock_irqsave(&fnic->fnic_lock, flags);
531
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700532 /* fnic should be in FC_TRANS_ETH_MODE */
533 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
534 /* Check status of reset completion */
535 if (!hdr_status) {
536 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
537 "reset cmpl success\n");
538 /* Ready to send flogi out */
539 fnic->state = FNIC_IN_ETH_MODE;
540 } else {
541 FNIC_SCSI_DBG(KERN_DEBUG,
542 fnic->lport->host,
543 "fnic fw_reset : failed %s\n",
544 fnic_fcpio_status_to_str(hdr_status));
545
546 /*
547 * Unable to change to eth mode, cannot send out flogi
548 * Change state to fc mode, so that subsequent Flogi
549 * requests from libFC will cause more attempts to
550 * reset the firmware. Free the cached flogi
551 */
552 fnic->state = FNIC_IN_FC_MODE;
553 ret = -1;
554 }
555 } else {
556 FNIC_SCSI_DBG(KERN_DEBUG,
557 fnic->lport->host,
558 "Unexpected state %s while processing"
559 " reset cmpl\n", fnic_state_to_str(fnic->state));
560 ret = -1;
561 }
562
563 /* Thread removing device blocks till firmware reset is complete */
564 if (fnic->remove_wait)
565 complete(fnic->remove_wait);
566
567 /*
568 * If fnic is being removed, or fw reset failed
569 * free the flogi frame. Else, send it out
570 */
571 if (fnic->remove_wait || ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700572 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Joe Eykholt78112e52009-11-03 11:49:22 -0800573 skb_queue_purge(&fnic->tx_queue);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700574 goto reset_cmpl_handler_end;
575 }
576
577 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
578
Joe Eykholt78112e52009-11-03 11:49:22 -0800579 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700580
581 reset_cmpl_handler_end:
Hiral Patel03298552013-02-12 17:00:58 -0800582 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
583
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700584 return ret;
585}
586
587/*
588 * fnic_fcpio_flogi_reg_cmpl_handler
589 * Routine to handle flogi register completion
590 */
591static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
592 struct fcpio_fw_req *desc)
593{
594 u8 type;
595 u8 hdr_status;
596 struct fcpio_tag tag;
597 int ret = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700598 unsigned long flags;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700599
600 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
601
602 /* Update fnic state based on status of flogi reg completion */
603 spin_lock_irqsave(&fnic->fnic_lock, flags);
604
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700605 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
606
607 /* Check flogi registration completion status */
608 if (!hdr_status) {
609 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
610 "flog reg succeeded\n");
611 fnic->state = FNIC_IN_FC_MODE;
612 } else {
613 FNIC_SCSI_DBG(KERN_DEBUG,
614 fnic->lport->host,
615 "fnic flogi reg :failed %s\n",
616 fnic_fcpio_status_to_str(hdr_status));
617 fnic->state = FNIC_IN_ETH_MODE;
618 ret = -1;
619 }
620 } else {
621 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
622 "Unexpected fnic state %s while"
623 " processing flogi reg completion\n",
624 fnic_state_to_str(fnic->state));
625 ret = -1;
626 }
627
Joe Eykholt78112e52009-11-03 11:49:22 -0800628 if (!ret) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700629 if (fnic->stop_rx_link_events) {
630 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
631 goto reg_cmpl_handler_end;
632 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700633 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
634
Joe Eykholt78112e52009-11-03 11:49:22 -0800635 fnic_flush_tx(fnic);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700636 queue_work(fnic_event_queue, &fnic->frame_work);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700637 } else {
638 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700639 }
640
641reg_cmpl_handler_end:
642 return ret;
643}
644
645static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
646 u16 request_out)
647{
648 if (wq->to_clean_index <= wq->to_use_index) {
649 /* out of range, stale request_out index */
650 if (request_out < wq->to_clean_index ||
651 request_out >= wq->to_use_index)
652 return 0;
653 } else {
654 /* out of range, stale request_out index */
655 if (request_out < wq->to_clean_index &&
656 request_out >= wq->to_use_index)
657 return 0;
658 }
659 /* request_out index is in range */
660 return 1;
661}
662
663
664/*
665 * Mark that ack received and store the Ack index. If there are multiple
666 * acks received before Tx thread cleans it up, the latest value will be
667 * used which is correct behavior. This state should be in the copy Wq
668 * instead of in the fnic
669 */
670static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
671 unsigned int cq_index,
672 struct fcpio_fw_req *desc)
673{
674 struct vnic_wq_copy *wq;
675 u16 request_out = desc->u.ack.request_out;
676 unsigned long flags;
677
678 /* mark the ack state */
679 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
680 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
681
682 if (is_ack_index_in_range(wq, request_out)) {
683 fnic->fw_ack_index[0] = request_out;
684 fnic->fw_ack_recd[0] = 1;
685 }
686 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
687}
688
689/*
690 * fnic_fcpio_icmnd_cmpl_handler
691 * Routine to handle icmnd completions
692 */
693static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
694 struct fcpio_fw_req *desc)
695{
696 u8 type;
697 u8 hdr_status;
698 struct fcpio_tag tag;
699 u32 id;
700 u64 xfer_len = 0;
701 struct fcpio_icmnd_cmpl *icmnd_cmpl;
702 struct fnic_io_req *io_req;
703 struct scsi_cmnd *sc;
704 unsigned long flags;
705 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800706 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700707
708 /* Decode the cmpl description to get the io_req id */
709 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
710 fcpio_tag_id_dec(&tag, &id);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800711 icmnd_cmpl = &desc->u.icmnd_cmpl;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700712
Hiral Patel03298552013-02-12 17:00:58 -0800713 if (id >= FNIC_MAX_IO_REQ) {
714 shost_printk(KERN_ERR, fnic->lport->host,
715 "Tag out of range tag %x hdr status = %s\n",
716 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700717 return;
Hiral Patel03298552013-02-12 17:00:58 -0800718 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700719
720 sc = scsi_host_find_tag(fnic->lport->host, id);
721 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800722 if (!sc) {
723 shost_printk(KERN_ERR, fnic->lport->host,
724 "icmnd_cmpl sc is null - "
725 "hdr status = %s tag = 0x%x desc = 0x%p\n",
726 fnic_fcpio_status_to_str(hdr_status), id, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700727 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800728 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700729
730 io_lock = fnic_io_lock_hash(fnic, sc);
731 spin_lock_irqsave(io_lock, flags);
732 io_req = (struct fnic_io_req *)CMD_SP(sc);
733 WARN_ON_ONCE(!io_req);
734 if (!io_req) {
Hiral Patel14eb5d92013-02-12 17:01:01 -0800735 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700736 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800737 shost_printk(KERN_ERR, fnic->lport->host,
738 "icmnd_cmpl io_req is null - "
739 "hdr status = %s tag = 0x%x sc 0x%p\n",
740 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700741 return;
742 }
Hiral Patel14eb5d92013-02-12 17:01:01 -0800743 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700744
745 /* firmware completed the io */
746 io_req->io_completed = 1;
747
748 /*
749 * if SCSI-ML has already issued abort on this command,
750 * ignore completion of the IO. The abts path will clean it up
751 */
752 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
753 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800754 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
755 switch (hdr_status) {
756 case FCPIO_SUCCESS:
757 CMD_FLAGS(sc) |= FNIC_IO_DONE;
758 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
759 "icmnd_cmpl ABTS pending hdr status = %s "
760 "sc 0x%p scsi_status %x residual %d\n",
761 fnic_fcpio_status_to_str(hdr_status), sc,
762 icmnd_cmpl->scsi_status,
763 icmnd_cmpl->residual);
764 break;
765 case FCPIO_ABORTED:
766 CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
767 break;
768 default:
769 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
770 "icmnd_cmpl abts pending "
771 "hdr status = %s tag = 0x%x sc = 0x%p\n",
772 fnic_fcpio_status_to_str(hdr_status),
773 id, sc);
774 break;
775 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700776 return;
777 }
778
779 /* Mark the IO as complete */
780 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
781
782 icmnd_cmpl = &desc->u.icmnd_cmpl;
783
784 switch (hdr_status) {
785 case FCPIO_SUCCESS:
786 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
787 xfer_len = scsi_bufflen(sc);
788 scsi_set_resid(sc, icmnd_cmpl->residual);
789
790 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
791 xfer_len -= icmnd_cmpl->residual;
792
793 /*
794 * If queue_full, then try to reduce queue depth for all
795 * LUNS on the target. Todo: this should be accompanied
796 * by a periodic queue_depth rampup based on successful
797 * IO completion.
798 */
799 if (icmnd_cmpl->scsi_status == QUEUE_FULL) {
800 struct scsi_device *t_sdev;
801 int qd = 0;
802
803 shost_for_each_device(t_sdev, sc->device->host) {
804 if (t_sdev->id != sc->device->id)
805 continue;
806
807 if (t_sdev->queue_depth > 1) {
808 qd = scsi_track_queue_full
809 (t_sdev,
810 t_sdev->queue_depth - 1);
811 if (qd == -1)
812 qd = t_sdev->host->cmd_per_lun;
813 shost_printk(KERN_INFO,
814 fnic->lport->host,
815 "scsi[%d:%d:%d:%d"
816 "] queue full detected,"
817 "new depth = %d\n",
818 t_sdev->host->host_no,
819 t_sdev->channel,
820 t_sdev->id, t_sdev->lun,
821 t_sdev->queue_depth);
822 }
823 }
824 }
825 break;
826
827 case FCPIO_TIMEOUT: /* request was timed out */
828 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
829 break;
830
831 case FCPIO_ABORTED: /* request was aborted */
832 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
833 break;
834
835 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
836 scsi_set_resid(sc, icmnd_cmpl->residual);
837 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
838 break;
839
840 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
841 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
842 break;
843 case FCPIO_INVALID_HEADER: /* header contains invalid data */
844 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
845 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
846 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
847 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
848 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
849 case FCPIO_FW_ERR: /* request was terminated due fw error */
850 default:
851 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
852 fnic_fcpio_status_to_str(hdr_status));
853 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
854 break;
855 }
856
857 /* Break link with the SCSI command */
858 CMD_SP(sc) = NULL;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800859 CMD_FLAGS(sc) |= FNIC_IO_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700860
861 spin_unlock_irqrestore(io_lock, flags);
862
863 fnic_release_ioreq_buf(fnic, io_req, sc);
864
865 mempool_free(io_req, fnic->io_req_pool);
866
867 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
868 fnic->lport->host_stats.fcp_input_requests++;
869 fnic->fcp_input_bytes += xfer_len;
870 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
871 fnic->lport->host_stats.fcp_output_requests++;
872 fnic->fcp_output_bytes += xfer_len;
873 } else
874 fnic->lport->host_stats.fcp_control_requests++;
875
876 /* Call SCSI completion function to complete the IO */
877 if (sc->scsi_done)
878 sc->scsi_done(sc);
879
880}
881
882/* fnic_fcpio_itmf_cmpl_handler
883 * Routine to handle itmf completions
884 */
885static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
886 struct fcpio_fw_req *desc)
887{
888 u8 type;
889 u8 hdr_status;
890 struct fcpio_tag tag;
891 u32 id;
892 struct scsi_cmnd *sc;
893 struct fnic_io_req *io_req;
894 unsigned long flags;
895 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800896 unsigned long start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700897
898 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
899 fcpio_tag_id_dec(&tag, &id);
900
Hiral Patel03298552013-02-12 17:00:58 -0800901 if ((id & FNIC_TAG_MASK) >= FNIC_MAX_IO_REQ) {
902 shost_printk(KERN_ERR, fnic->lport->host,
903 "Tag out of range tag %x hdr status = %s\n",
904 id, fnic_fcpio_status_to_str(hdr_status));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700905 return;
Hiral Patel03298552013-02-12 17:00:58 -0800906 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700907
908 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
909 WARN_ON_ONCE(!sc);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800910 if (!sc) {
911 shost_printk(KERN_ERR, fnic->lport->host,
912 "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
913 fnic_fcpio_status_to_str(hdr_status), id);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700914 return;
Hiral Patel14eb5d92013-02-12 17:01:01 -0800915 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700916 io_lock = fnic_io_lock_hash(fnic, sc);
917 spin_lock_irqsave(io_lock, flags);
918 io_req = (struct fnic_io_req *)CMD_SP(sc);
919 WARN_ON_ONCE(!io_req);
920 if (!io_req) {
921 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800922 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
923 shost_printk(KERN_ERR, fnic->lport->host,
924 "itmf_cmpl io_req is null - "
925 "hdr status = %s tag = 0x%x sc 0x%p\n",
926 fnic_fcpio_status_to_str(hdr_status), id, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700927 return;
928 }
Hiral Patel14eb5d92013-02-12 17:01:01 -0800929 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700930
Hiral Patel03298552013-02-12 17:00:58 -0800931 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
932 /* Abort and terminate completion of device reset req */
933 /* REVISIT : Add asserts about various flags */
934 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
935 "dev reset abts cmpl recd. id %x status %s\n",
936 id, fnic_fcpio_status_to_str(hdr_status));
937 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
938 CMD_ABTS_STATUS(sc) = hdr_status;
939 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
940 if (io_req->abts_done)
941 complete(io_req->abts_done);
942 spin_unlock_irqrestore(io_lock, flags);
943 } else if (id & FNIC_TAG_ABORT) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700944 /* Completion of abort cmd */
945 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
946 /* This is a late completion. Ignore it */
947 spin_unlock_irqrestore(io_lock, flags);
948 return;
949 }
950 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
951 CMD_ABTS_STATUS(sc) = hdr_status;
952
Hiral Patel14eb5d92013-02-12 17:01:01 -0800953 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700954 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
955 "abts cmpl recd. id %d status %s\n",
956 (int)(id & FNIC_TAG_MASK),
957 fnic_fcpio_status_to_str(hdr_status));
958
959 /*
960 * If scsi_eh thread is blocked waiting for abts to complete,
961 * signal completion to it. IO will be cleaned in the thread
962 * else clean it in this context
963 */
964 if (io_req->abts_done) {
965 complete(io_req->abts_done);
966 spin_unlock_irqrestore(io_lock, flags);
967 } else {
968 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
969 "abts cmpl, completing IO\n");
970 CMD_SP(sc) = NULL;
971 sc->result = (DID_ERROR << 16);
972
973 spin_unlock_irqrestore(io_lock, flags);
974
975 fnic_release_ioreq_buf(fnic, io_req, sc);
976 mempool_free(io_req, fnic->io_req_pool);
977 if (sc->scsi_done)
978 sc->scsi_done(sc);
979 }
980
981 } else if (id & FNIC_TAG_DEV_RST) {
982 /* Completion of device reset */
983 CMD_LR_STATUS(sc) = hdr_status;
Hiral Patel03298552013-02-12 17:00:58 -0800984 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
985 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -0800986 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
Hiral Patel03298552013-02-12 17:00:58 -0800987 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
988 "Terminate pending "
989 "dev reset cmpl recd. id %d status %s\n",
990 (int)(id & FNIC_TAG_MASK),
991 fnic_fcpio_status_to_str(hdr_status));
992 return;
993 }
994 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
995 /* Need to wait for terminate completion */
996 spin_unlock_irqrestore(io_lock, flags);
997 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
998 "dev reset cmpl recd after time out. "
999 "id %d status %s\n",
1000 (int)(id & FNIC_TAG_MASK),
1001 fnic_fcpio_status_to_str(hdr_status));
1002 return;
1003 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001004 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
Hiral Patel03298552013-02-12 17:00:58 -08001005 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001006 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1007 "dev reset cmpl recd. id %d status %s\n",
1008 (int)(id & FNIC_TAG_MASK),
1009 fnic_fcpio_status_to_str(hdr_status));
1010 if (io_req->dr_done)
1011 complete(io_req->dr_done);
1012 spin_unlock_irqrestore(io_lock, flags);
1013
1014 } else {
1015 shost_printk(KERN_ERR, fnic->lport->host,
1016 "Unexpected itmf io state %s tag %x\n",
1017 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1018 spin_unlock_irqrestore(io_lock, flags);
1019 }
1020
1021}
1022
1023/*
1024 * fnic_fcpio_cmpl_handler
1025 * Routine to service the cq for wq_copy
1026 */
1027static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1028 unsigned int cq_index,
1029 struct fcpio_fw_req *desc)
1030{
1031 struct fnic *fnic = vnic_dev_priv(vdev);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001032
1033 switch (desc->hdr.type) {
1034 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1035 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1036 break;
1037
1038 case FCPIO_ICMND_CMPL: /* fw completed a command */
1039 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1040 break;
1041
1042 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1043 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1044 break;
1045
1046 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
Joe Eykholt78112e52009-11-03 11:49:22 -08001047 case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
Hiral Patel03298552013-02-12 17:00:58 -08001048 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001049 break;
1050
1051 case FCPIO_RESET_CMPL: /* fw completed reset */
Hiral Patel03298552013-02-12 17:00:58 -08001052 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001053 break;
1054
1055 default:
1056 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1057 "firmware completion type %d\n",
1058 desc->hdr.type);
1059 break;
1060 }
1061
Hiral Patel03298552013-02-12 17:00:58 -08001062 return 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001063}
1064
1065/*
1066 * fnic_wq_copy_cmpl_handler
1067 * Routine to process wq copy
1068 */
1069int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1070{
1071 unsigned int wq_work_done = 0;
1072 unsigned int i, cq_index;
1073 unsigned int cur_work_done;
1074
1075 for (i = 0; i < fnic->wq_copy_count; i++) {
1076 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1077 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1078 fnic_fcpio_cmpl_handler,
1079 copy_work_to_do);
1080 wq_work_done += cur_work_done;
1081 }
1082 return wq_work_done;
1083}
1084
1085static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1086{
1087 unsigned int i;
1088 struct fnic_io_req *io_req;
1089 unsigned long flags = 0;
1090 struct scsi_cmnd *sc;
1091 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001092 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001093
1094 for (i = 0; i < FNIC_MAX_IO_REQ; i++) {
1095 if (i == exclude_id)
1096 continue;
1097
1098 sc = scsi_host_find_tag(fnic->lport->host, i);
1099 if (!sc)
1100 continue;
1101
1102 io_lock = fnic_io_lock_hash(fnic, sc);
1103 spin_lock_irqsave(io_lock, flags);
1104 io_req = (struct fnic_io_req *)CMD_SP(sc);
Hiral Patel03298552013-02-12 17:00:58 -08001105 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1106 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1107 /*
1108 * We will be here only when FW completes reset
1109 * without sending completions for outstanding ios.
1110 */
1111 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1112 if (io_req && io_req->dr_done)
1113 complete(io_req->dr_done);
1114 else if (io_req && io_req->abts_done)
1115 complete(io_req->abts_done);
1116 spin_unlock_irqrestore(io_lock, flags);
1117 continue;
1118 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1119 spin_unlock_irqrestore(io_lock, flags);
1120 continue;
1121 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001122 if (!io_req) {
1123 spin_unlock_irqrestore(io_lock, flags);
1124 goto cleanup_scsi_cmd;
1125 }
1126
1127 CMD_SP(sc) = NULL;
1128
1129 spin_unlock_irqrestore(io_lock, flags);
1130
1131 /*
1132 * If there is a scsi_cmnd associated with this io_req, then
1133 * free the corresponding state
1134 */
Hiral Patel14eb5d92013-02-12 17:01:01 -08001135 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001136 fnic_release_ioreq_buf(fnic, io_req, sc);
1137 mempool_free(io_req, fnic->io_req_pool);
1138
1139cleanup_scsi_cmd:
1140 sc->result = DID_TRANSPORT_DISRUPTED << 16;
1141 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "fnic_cleanup_io:"
1142 " DID_TRANSPORT_DISRUPTED\n");
1143
1144 /* Complete the command to SCSI */
1145 if (sc->scsi_done)
1146 sc->scsi_done(sc);
1147 }
1148}
1149
1150void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1151 struct fcpio_host_req *desc)
1152{
1153 u32 id;
1154 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1155 struct fnic_io_req *io_req;
1156 struct scsi_cmnd *sc;
1157 unsigned long flags;
1158 spinlock_t *io_lock;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001159 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001160
1161 /* get the tag reference */
1162 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1163 id &= FNIC_TAG_MASK;
1164
1165 if (id >= FNIC_MAX_IO_REQ)
1166 return;
1167
1168 sc = scsi_host_find_tag(fnic->lport->host, id);
1169 if (!sc)
1170 return;
1171
1172 io_lock = fnic_io_lock_hash(fnic, sc);
1173 spin_lock_irqsave(io_lock, flags);
1174
1175 /* Get the IO context which this desc refers to */
1176 io_req = (struct fnic_io_req *)CMD_SP(sc);
1177
1178 /* fnic interrupts are turned off by now */
1179
1180 if (!io_req) {
1181 spin_unlock_irqrestore(io_lock, flags);
1182 goto wq_copy_cleanup_scsi_cmd;
1183 }
1184
1185 CMD_SP(sc) = NULL;
1186
1187 spin_unlock_irqrestore(io_lock, flags);
1188
Hiral Patel14eb5d92013-02-12 17:01:01 -08001189 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001190 fnic_release_ioreq_buf(fnic, io_req, sc);
1191 mempool_free(io_req, fnic->io_req_pool);
1192
1193wq_copy_cleanup_scsi_cmd:
1194 sc->result = DID_NO_CONNECT << 16;
1195 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1196 " DID_NO_CONNECT\n");
1197
1198 if (sc->scsi_done)
1199 sc->scsi_done(sc);
1200}
1201
1202static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1203 u32 task_req, u8 *fc_lun,
1204 struct fnic_io_req *io_req)
1205{
1206 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001207 struct Scsi_Host *host = fnic->lport->host;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001208 unsigned long flags;
1209
Hiral Patel03298552013-02-12 17:00:58 -08001210 spin_lock_irqsave(host->host_lock, flags);
1211 if (unlikely(fnic_chk_state_flags_locked(fnic,
1212 FNIC_FLAGS_IO_BLOCKED))) {
1213 spin_unlock_irqrestore(host->host_lock, flags);
1214 return 1;
1215 } else
1216 atomic_inc(&fnic->in_flight);
1217 spin_unlock_irqrestore(host->host_lock, flags);
1218
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001219 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1220
1221 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1222 free_wq_copy_descs(fnic, wq);
1223
1224 if (!vnic_wq_copy_desc_avail(wq)) {
1225 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001226 atomic_dec(&fnic->in_flight);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001227 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001228 "fnic_queue_abort_io_req: failure: no descriptors\n");
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001229 return 1;
1230 }
1231 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1232 0, task_req, tag, fc_lun, io_req->port_id,
1233 fnic->config.ra_tov, fnic->config.ed_tov);
1234
1235 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
Hiral Patel03298552013-02-12 17:00:58 -08001236 atomic_dec(&fnic->in_flight);
1237
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001238 return 0;
1239}
1240
Hiral Patel03298552013-02-12 17:00:58 -08001241static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001242{
1243 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001244 int abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001245 struct fnic_io_req *io_req;
1246 spinlock_t *io_lock;
1247 unsigned long flags;
1248 struct scsi_cmnd *sc;
1249 struct scsi_lun fc_lun;
1250 enum fnic_ioreq_state old_ioreq_state;
1251
1252 FNIC_SCSI_DBG(KERN_DEBUG,
1253 fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001254 "fnic_rport_exch_reset called portid 0x%06x\n",
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001255 port_id);
1256
1257 if (fnic->in_remove)
1258 return;
1259
1260 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001261 abt_tag = tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001262 sc = scsi_host_find_tag(fnic->lport->host, tag);
1263 if (!sc)
1264 continue;
1265
1266 io_lock = fnic_io_lock_hash(fnic, sc);
1267 spin_lock_irqsave(io_lock, flags);
1268
1269 io_req = (struct fnic_io_req *)CMD_SP(sc);
1270
1271 if (!io_req || io_req->port_id != port_id) {
1272 spin_unlock_irqrestore(io_lock, flags);
1273 continue;
1274 }
1275
Hiral Patel03298552013-02-12 17:00:58 -08001276 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001277 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001278 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1279 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1280 sc);
1281 spin_unlock_irqrestore(io_lock, flags);
1282 continue;
1283 }
1284
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001285 /*
1286 * Found IO that is still pending with firmware and
1287 * belongs to rport that went away
1288 */
1289 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1290 spin_unlock_irqrestore(io_lock, flags);
1291 continue;
1292 }
Hiral Patel03298552013-02-12 17:00:58 -08001293 if (io_req->abts_done) {
1294 shost_printk(KERN_ERR, fnic->lport->host,
1295 "fnic_rport_exch_reset: io_req->abts_done is set "
1296 "state is %s\n",
1297 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1298 }
1299
Hiral Patel14eb5d92013-02-12 17:01:01 -08001300 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1301 shost_printk(KERN_ERR, fnic->lport->host,
1302 "rport_exch_reset "
1303 "IO not yet issued %p tag 0x%x flags "
1304 "%x state %d\n",
1305 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1306 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001307 old_ioreq_state = CMD_STATE(sc);
1308 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1309 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001310 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1311 abt_tag = (tag | FNIC_TAG_DEV_RST);
1312 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1313 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1314 sc);
1315 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001316
1317 BUG_ON(io_req->abts_done);
1318
1319 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1320 "fnic_rport_reset_exch: Issuing abts\n");
1321
1322 spin_unlock_irqrestore(io_lock, flags);
1323
1324 /* Now queue the abort command to firmware */
1325 int_to_scsilun(sc->device->lun, &fc_lun);
1326
Hiral Patel03298552013-02-12 17:00:58 -08001327 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001328 FCPIO_ITMF_ABT_TASK_TERM,
1329 fc_lun.scsi_lun, io_req)) {
1330 /*
1331 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001332 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001333 * aborted later by scsi_eh, or cleaned up during
1334 * lun reset
1335 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001336 spin_lock_irqsave(io_lock, flags);
1337 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1338 CMD_STATE(sc) = old_ioreq_state;
1339 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001340 } else {
1341 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001342 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1343 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001344 else
1345 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001346 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001347 }
1348 }
1349
1350}
1351
1352void fnic_terminate_rport_io(struct fc_rport *rport)
1353{
1354 int tag;
Hiral Patel03298552013-02-12 17:00:58 -08001355 int abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001356 struct fnic_io_req *io_req;
1357 spinlock_t *io_lock;
1358 unsigned long flags;
1359 struct scsi_cmnd *sc;
1360 struct scsi_lun fc_lun;
1361 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1362 struct fc_lport *lport = rdata->local_port;
1363 struct fnic *fnic = lport_priv(lport);
1364 struct fc_rport *cmd_rport;
1365 enum fnic_ioreq_state old_ioreq_state;
1366
1367 FNIC_SCSI_DBG(KERN_DEBUG,
1368 fnic->lport->host, "fnic_terminate_rport_io called"
Hiral Patel03298552013-02-12 17:00:58 -08001369 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1370 rport->port_name, rport->node_name, rport,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001371 rport->port_id);
1372
1373 if (fnic->in_remove)
1374 return;
1375
1376 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
Hiral Patel03298552013-02-12 17:00:58 -08001377 abt_tag = tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001378 sc = scsi_host_find_tag(fnic->lport->host, tag);
1379 if (!sc)
1380 continue;
1381
1382 cmd_rport = starget_to_rport(scsi_target(sc->device));
1383 if (rport != cmd_rport)
1384 continue;
1385
1386 io_lock = fnic_io_lock_hash(fnic, sc);
1387 spin_lock_irqsave(io_lock, flags);
1388
1389 io_req = (struct fnic_io_req *)CMD_SP(sc);
1390
1391 if (!io_req || rport != cmd_rport) {
1392 spin_unlock_irqrestore(io_lock, flags);
1393 continue;
1394 }
1395
Hiral Patel03298552013-02-12 17:00:58 -08001396 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001397 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patel03298552013-02-12 17:00:58 -08001398 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1399 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1400 sc);
1401 spin_unlock_irqrestore(io_lock, flags);
1402 continue;
1403 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001404 /*
1405 * Found IO that is still pending with firmware and
1406 * belongs to rport that went away
1407 */
1408 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1409 spin_unlock_irqrestore(io_lock, flags);
1410 continue;
1411 }
Hiral Patel03298552013-02-12 17:00:58 -08001412 if (io_req->abts_done) {
1413 shost_printk(KERN_ERR, fnic->lport->host,
1414 "fnic_terminate_rport_io: io_req->abts_done is set "
1415 "state is %s\n",
1416 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1417 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001418 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1419 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1420 "fnic_terminate_rport_io "
1421 "IO not yet issued %p tag 0x%x flags "
1422 "%x state %d\n",
1423 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1424 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001425 old_ioreq_state = CMD_STATE(sc);
1426 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1427 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
Hiral Patel03298552013-02-12 17:00:58 -08001428 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1429 abt_tag = (tag | FNIC_TAG_DEV_RST);
1430 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1431 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1432 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001433
1434 BUG_ON(io_req->abts_done);
1435
1436 FNIC_SCSI_DBG(KERN_DEBUG,
1437 fnic->lport->host,
1438 "fnic_terminate_rport_io: Issuing abts\n");
1439
1440 spin_unlock_irqrestore(io_lock, flags);
1441
1442 /* Now queue the abort command to firmware */
1443 int_to_scsilun(sc->device->lun, &fc_lun);
1444
Hiral Patel03298552013-02-12 17:00:58 -08001445 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001446 FCPIO_ITMF_ABT_TASK_TERM,
1447 fc_lun.scsi_lun, io_req)) {
1448 /*
1449 * Revert the cmd state back to old state, if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001450 * it hasn't changed in between. This cmd will get
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001451 * aborted later by scsi_eh, or cleaned up during
1452 * lun reset
1453 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001454 spin_lock_irqsave(io_lock, flags);
1455 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1456 CMD_STATE(sc) = old_ioreq_state;
1457 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001458 } else {
1459 spin_lock_irqsave(io_lock, flags);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001460 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1461 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001462 else
1463 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001464 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001465 }
1466 }
1467
1468}
1469
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001470/*
1471 * This function is exported to SCSI for sending abort cmnds.
1472 * A SCSI IO is represented by a io_req in the driver.
1473 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1474 */
1475int fnic_abort_cmd(struct scsi_cmnd *sc)
1476{
1477 struct fc_lport *lp;
1478 struct fnic *fnic;
1479 struct fnic_io_req *io_req;
1480 struct fc_rport *rport;
1481 spinlock_t *io_lock;
1482 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001483 unsigned long start_time = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001484 int ret = SUCCESS;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001485 u32 task_req = 0;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001486 struct scsi_lun fc_lun;
Hiral Patel03298552013-02-12 17:00:58 -08001487 int tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001488 DECLARE_COMPLETION_ONSTACK(tm_done);
1489
1490 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001491 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001492
1493 /* Get local-port, check ready and link up */
1494 lp = shost_priv(sc->device->host);
1495
1496 fnic = lport_priv(lp);
Roel Kluin0db6f432010-06-11 16:44:46 -07001497 rport = starget_to_rport(scsi_target(sc->device));
Hiral Patel03298552013-02-12 17:00:58 -08001498 tag = sc->request->tag;
1499 FNIC_SCSI_DBG(KERN_DEBUG,
1500 fnic->lport->host,
1501 "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %x flags %x\n",
1502 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1503
1504 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1505
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001506
1507 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1508 ret = FAILED;
1509 goto fnic_abort_cmd_end;
1510 }
1511
1512 /*
1513 * Avoid a race between SCSI issuing the abort and the device
1514 * completing the command.
1515 *
1516 * If the command is already completed by the fw cmpl code,
1517 * we just return SUCCESS from here. This means that the abort
1518 * succeeded. In the SCSI ML, since the timeout for command has
1519 * happened, the completion wont actually complete the command
1520 * and it will be considered as an aborted command
1521 *
1522 * The CMD_SP will not be cleared except while holding io_req_lock.
1523 */
1524 io_lock = fnic_io_lock_hash(fnic, sc);
1525 spin_lock_irqsave(io_lock, flags);
1526 io_req = (struct fnic_io_req *)CMD_SP(sc);
1527 if (!io_req) {
1528 spin_unlock_irqrestore(io_lock, flags);
1529 goto fnic_abort_cmd_end;
1530 }
1531
1532 io_req->abts_done = &tm_done;
1533
1534 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1535 spin_unlock_irqrestore(io_lock, flags);
1536 goto wait_pending;
1537 }
1538 /*
1539 * Command is still pending, need to abort it
1540 * If the firmware completes the command after this point,
1541 * the completion wont be done till mid-layer, since abort
1542 * has already started.
1543 */
1544 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1545 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1546
1547 spin_unlock_irqrestore(io_lock, flags);
1548
1549 /*
1550 * Check readiness of the remote port. If the path to remote
1551 * port is up, then send abts to the remote port to terminate
1552 * the IO. Else, just locally terminate the IO in the firmware
1553 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001554 if (fc_remote_port_chkready(rport) == 0)
1555 task_req = FCPIO_ITMF_ABT_TASK;
1556 else
1557 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1558
1559 /* Now queue the abort command to firmware */
1560 int_to_scsilun(sc->device->lun, &fc_lun);
1561
1562 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1563 fc_lun.scsi_lun, io_req)) {
1564 spin_lock_irqsave(io_lock, flags);
1565 io_req = (struct fnic_io_req *)CMD_SP(sc);
1566 if (io_req)
1567 io_req->abts_done = NULL;
1568 spin_unlock_irqrestore(io_lock, flags);
1569 ret = FAILED;
1570 goto fnic_abort_cmd_end;
1571 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001572 if (task_req == FCPIO_ITMF_ABT_TASK)
1573 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
1574 else
1575 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001576
1577 /*
1578 * We queued an abort IO, wait for its completion.
1579 * Once the firmware completes the abort command, it will
1580 * wake up this thread.
1581 */
1582 wait_pending:
1583 wait_for_completion_timeout(&tm_done,
1584 msecs_to_jiffies
1585 (2 * fnic->config.ra_tov +
1586 fnic->config.ed_tov));
1587
1588 /* Check the abort status */
1589 spin_lock_irqsave(io_lock, flags);
1590
1591 io_req = (struct fnic_io_req *)CMD_SP(sc);
1592 if (!io_req) {
1593 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001594 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001595 ret = FAILED;
1596 goto fnic_abort_cmd_end;
1597 }
1598 io_req->abts_done = NULL;
1599
1600 /* fw did not complete abort, timed out */
1601 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1602 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001603 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001604 ret = FAILED;
1605 goto fnic_abort_cmd_end;
1606 }
1607
1608 /*
1609 * firmware completed the abort, check the status,
1610 * free the io_req irrespective of failure or success
1611 */
1612 if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1613 ret = FAILED;
1614
1615 CMD_SP(sc) = NULL;
1616
1617 spin_unlock_irqrestore(io_lock, flags);
1618
Hiral Patel14eb5d92013-02-12 17:01:01 -08001619 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001620 fnic_release_ioreq_buf(fnic, io_req, sc);
1621 mempool_free(io_req, fnic->io_req_pool);
1622
1623fnic_abort_cmd_end:
1624 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel14eb5d92013-02-12 17:01:01 -08001625 "Returning from abort cmd type %x %s\n", task_req,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001626 (ret == SUCCESS) ?
1627 "SUCCESS" : "FAILED");
1628 return ret;
1629}
1630
1631static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1632 struct scsi_cmnd *sc,
1633 struct fnic_io_req *io_req)
1634{
1635 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
Hiral Patel03298552013-02-12 17:00:58 -08001636 struct Scsi_Host *host = fnic->lport->host;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001637 struct scsi_lun fc_lun;
1638 int ret = 0;
1639 unsigned long intr_flags;
1640
Hiral Patel03298552013-02-12 17:00:58 -08001641 spin_lock_irqsave(host->host_lock, intr_flags);
1642 if (unlikely(fnic_chk_state_flags_locked(fnic,
1643 FNIC_FLAGS_IO_BLOCKED))) {
1644 spin_unlock_irqrestore(host->host_lock, intr_flags);
1645 return FAILED;
1646 } else
1647 atomic_inc(&fnic->in_flight);
1648 spin_unlock_irqrestore(host->host_lock, intr_flags);
1649
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001650 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1651
1652 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1653 free_wq_copy_descs(fnic, wq);
1654
1655 if (!vnic_wq_copy_desc_avail(wq)) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08001656 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1657 "queue_dr_io_req failure - no descriptors\n");
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001658 ret = -EAGAIN;
1659 goto lr_io_req_end;
1660 }
1661
1662 /* fill in the lun info */
1663 int_to_scsilun(sc->device->lun, &fc_lun);
1664
1665 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1666 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1667 fc_lun.scsi_lun, io_req->port_id,
1668 fnic->config.ra_tov, fnic->config.ed_tov);
1669
1670lr_io_req_end:
1671 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
Hiral Patel03298552013-02-12 17:00:58 -08001672 atomic_dec(&fnic->in_flight);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001673
1674 return ret;
1675}
1676
1677/*
1678 * Clean up any pending aborts on the lun
1679 * For each outstanding IO on this lun, whose abort is not completed by fw,
1680 * issue a local abort. Wait for abort to complete. Return 0 if all commands
1681 * successfully aborted, 1 otherwise
1682 */
1683static int fnic_clean_pending_aborts(struct fnic *fnic,
1684 struct scsi_cmnd *lr_sc)
1685{
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001686 int tag, abt_tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001687 struct fnic_io_req *io_req;
1688 spinlock_t *io_lock;
1689 unsigned long flags;
1690 int ret = 0;
1691 struct scsi_cmnd *sc;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001692 struct scsi_lun fc_lun;
1693 struct scsi_device *lun_dev = lr_sc->device;
1694 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001695 enum fnic_ioreq_state old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001696
1697 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1698 sc = scsi_host_find_tag(fnic->lport->host, tag);
1699 /*
1700 * ignore this lun reset cmd or cmds that do not belong to
1701 * this lun
1702 */
1703 if (!sc || sc == lr_sc || sc->device != lun_dev)
1704 continue;
1705
1706 io_lock = fnic_io_lock_hash(fnic, sc);
1707 spin_lock_irqsave(io_lock, flags);
1708
1709 io_req = (struct fnic_io_req *)CMD_SP(sc);
1710
1711 if (!io_req || sc->device != lun_dev) {
1712 spin_unlock_irqrestore(io_lock, flags);
1713 continue;
1714 }
1715
1716 /*
1717 * Found IO that is still pending with firmware and
1718 * belongs to the LUN that we are resetting
1719 */
1720 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1721 "Found IO in %s on lun\n",
1722 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1723
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001724 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1725 spin_unlock_irqrestore(io_lock, flags);
1726 continue;
1727 }
1728 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
Hiral Patel14eb5d92013-02-12 17:01:01 -08001729 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001730 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1731 "%s dev rst not pending sc 0x%p\n", __func__,
1732 sc);
1733 spin_unlock_irqrestore(io_lock, flags);
1734 continue;
1735 }
1736 old_ioreq_state = CMD_STATE(sc);
1737 /*
1738 * Any pending IO issued prior to reset is expected to be
1739 * in abts pending state, if not we need to set
1740 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
1741 * When IO is completed, the IO will be handed over and
1742 * handled in this function.
1743 */
1744 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1745
1746 if (io_req->abts_done)
1747 shost_printk(KERN_ERR, fnic->lport->host,
1748 "%s: io_req->abts_done is set state is %s\n",
1749 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
1750
1751 BUG_ON(io_req->abts_done);
1752
1753 abt_tag = tag;
1754 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1755 abt_tag |= FNIC_TAG_DEV_RST;
1756 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1757 "%s: dev rst sc 0x%p\n", __func__, sc);
1758 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001759
1760 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1761 io_req->abts_done = &tm_done;
1762 spin_unlock_irqrestore(io_lock, flags);
1763
1764 /* Now queue the abort command to firmware */
1765 int_to_scsilun(sc->device->lun, &fc_lun);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001766
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001767 if (fnic_queue_abort_io_req(fnic, abt_tag,
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001768 FCPIO_ITMF_ABT_TASK_TERM,
1769 fc_lun.scsi_lun, io_req)) {
1770 spin_lock_irqsave(io_lock, flags);
1771 io_req = (struct fnic_io_req *)CMD_SP(sc);
1772 if (io_req)
1773 io_req->abts_done = NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001774 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1775 CMD_STATE(sc) = old_ioreq_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001776 spin_unlock_irqrestore(io_lock, flags);
1777 ret = 1;
1778 goto clean_pending_aborts_end;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001779 } else {
1780 spin_lock_irqsave(io_lock, flags);
1781 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1782 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1783 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001784 }
Hiral Patel14eb5d92013-02-12 17:01:01 -08001785 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001786
1787 wait_for_completion_timeout(&tm_done,
1788 msecs_to_jiffies
1789 (fnic->config.ed_tov));
1790
1791 /* Recheck cmd state to check if it is now aborted */
1792 spin_lock_irqsave(io_lock, flags);
1793 io_req = (struct fnic_io_req *)CMD_SP(sc);
1794 if (!io_req) {
1795 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001796 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001797 continue;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001798 }
1799
1800 io_req->abts_done = NULL;
1801
1802 /* if abort is still pending with fw, fail */
1803 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1804 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001805 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001806 ret = 1;
1807 goto clean_pending_aborts_end;
1808 }
1809 CMD_SP(sc) = NULL;
1810 spin_unlock_irqrestore(io_lock, flags);
1811
1812 fnic_release_ioreq_buf(fnic, io_req, sc);
1813 mempool_free(io_req, fnic->io_req_pool);
1814 }
1815
Hiral Patela0bf1ca2013-02-12 17:01:00 -08001816 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
1817
1818 /* walk again to check, if IOs are still pending in fw */
1819 if (fnic_is_abts_pending(fnic, lr_sc))
1820 ret = FAILED;
1821
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001822clean_pending_aborts_end:
1823 return ret;
1824}
1825
Hiral Patel03298552013-02-12 17:00:58 -08001826/**
1827 * fnic_scsi_host_start_tag
1828 * Allocates tagid from host's tag list
1829 **/
1830static inline int
1831fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
1832{
1833 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
1834 int tag, ret = SCSI_NO_TAG;
1835
1836 BUG_ON(!bqt);
1837 if (!bqt) {
1838 pr_err("Tags are not supported\n");
1839 goto end;
1840 }
1841
1842 do {
1843 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
1844 if (tag >= bqt->max_depth) {
1845 pr_err("Tag allocation failure\n");
1846 goto end;
1847 }
1848 } while (test_and_set_bit(tag, bqt->tag_map));
1849
1850 bqt->tag_index[tag] = sc->request;
1851 sc->request->tag = tag;
1852 sc->tag = tag;
1853 if (!sc->request->special)
1854 sc->request->special = sc;
1855
1856 ret = tag;
1857
1858end:
1859 return ret;
1860}
1861
1862/**
1863 * fnic_scsi_host_end_tag
1864 * frees tag allocated by fnic_scsi_host_start_tag.
1865 **/
1866static inline void
1867fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
1868{
1869 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
1870 int tag = sc->request->tag;
1871
1872 if (tag == SCSI_NO_TAG)
1873 return;
1874
1875 BUG_ON(!bqt || !bqt->tag_index[tag]);
1876 if (!bqt)
1877 return;
1878
1879 bqt->tag_index[tag] = NULL;
1880 clear_bit(tag, bqt->tag_map);
1881
1882 return;
1883}
1884
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001885/*
1886 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
1887 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
1888 * on the LUN.
1889 */
1890int fnic_device_reset(struct scsi_cmnd *sc)
1891{
1892 struct fc_lport *lp;
1893 struct fnic *fnic;
1894 struct fnic_io_req *io_req;
1895 struct fc_rport *rport;
1896 int status;
1897 int ret = FAILED;
1898 spinlock_t *io_lock;
1899 unsigned long flags;
Hiral Patel14eb5d92013-02-12 17:01:01 -08001900 unsigned long start_time = 0;
Hiral Patel03298552013-02-12 17:00:58 -08001901 struct scsi_lun fc_lun;
1902 int tag;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001903 DECLARE_COMPLETION_ONSTACK(tm_done);
Hiral Patel03298552013-02-12 17:00:58 -08001904 int tag_gen_flag = 0; /*to track tags allocated by fnic driver*/
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001905
1906 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001907 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001908
1909 /* Get local-port, check ready and link up */
1910 lp = shost_priv(sc->device->host);
1911
1912 fnic = lport_priv(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001913
Roel Kluin0db6f432010-06-11 16:44:46 -07001914 rport = starget_to_rport(scsi_target(sc->device));
1915 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
Hiral Patel03298552013-02-12 17:00:58 -08001916 "Device reset called FCID 0x%x, LUN 0x%x sc 0x%p\n",
1917 rport->port_id, sc->device->lun, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001918
1919 if (lp->state != LPORT_ST_READY || !(lp->link_up))
1920 goto fnic_device_reset_end;
1921
1922 /* Check if remote port up */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001923 if (fc_remote_port_chkready(rport))
1924 goto fnic_device_reset_end;
1925
Hiral Patel14eb5d92013-02-12 17:01:01 -08001926 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
Hiral Patel03298552013-02-12 17:00:58 -08001927 /* Allocate tag if not present */
1928
1929 tag = sc->request->tag;
1930 if (unlikely(tag < 0)) {
1931 tag = fnic_scsi_host_start_tag(fnic, sc);
1932 if (unlikely(tag == SCSI_NO_TAG))
1933 goto fnic_device_reset_end;
1934 tag_gen_flag = 1;
1935 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001936 io_lock = fnic_io_lock_hash(fnic, sc);
1937 spin_lock_irqsave(io_lock, flags);
1938 io_req = (struct fnic_io_req *)CMD_SP(sc);
1939
1940 /*
1941 * If there is a io_req attached to this command, then use it,
1942 * else allocate a new one.
1943 */
1944 if (!io_req) {
1945 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
1946 if (!io_req) {
1947 spin_unlock_irqrestore(io_lock, flags);
1948 goto fnic_device_reset_end;
1949 }
1950 memset(io_req, 0, sizeof(*io_req));
1951 io_req->port_id = rport->port_id;
1952 CMD_SP(sc) = (char *)io_req;
1953 }
1954 io_req->dr_done = &tm_done;
1955 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
1956 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
1957 spin_unlock_irqrestore(io_lock, flags);
1958
Hiral Patel03298552013-02-12 17:00:58 -08001959 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001960
1961 /*
1962 * issue the device reset, if enqueue failed, clean up the ioreq
1963 * and break assoc with scsi cmd
1964 */
1965 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
1966 spin_lock_irqsave(io_lock, flags);
1967 io_req = (struct fnic_io_req *)CMD_SP(sc);
1968 if (io_req)
1969 io_req->dr_done = NULL;
1970 goto fnic_device_reset_clean;
1971 }
Hiral Patel03298552013-02-12 17:00:58 -08001972 spin_lock_irqsave(io_lock, flags);
Hiral Patel14eb5d92013-02-12 17:01:01 -08001973 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
Hiral Patel03298552013-02-12 17:00:58 -08001974 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001975
1976 /*
1977 * Wait on the local completion for LUN reset. The io_req may be
1978 * freed while we wait since we hold no lock.
1979 */
1980 wait_for_completion_timeout(&tm_done,
1981 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
1982
1983 spin_lock_irqsave(io_lock, flags);
1984 io_req = (struct fnic_io_req *)CMD_SP(sc);
1985 if (!io_req) {
1986 spin_unlock_irqrestore(io_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08001987 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1988 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001989 goto fnic_device_reset_end;
1990 }
1991 io_req->dr_done = NULL;
1992
1993 status = CMD_LR_STATUS(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001994
1995 /*
1996 * If lun reset not completed, bail out with failed. io_req
1997 * gets cleaned up during higher levels of EH
1998 */
1999 if (status == FCPIO_INVALID_CODE) {
2000 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2001 "Device reset timed out\n");
Hiral Patel03298552013-02-12 17:00:58 -08002002 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2003 spin_unlock_irqrestore(io_lock, flags);
2004 int_to_scsilun(sc->device->lun, &fc_lun);
2005 /*
2006 * Issue abort and terminate on the device reset request.
2007 * If q'ing of the abort fails, retry issue it after a delay.
2008 */
2009 while (1) {
2010 spin_lock_irqsave(io_lock, flags);
2011 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2012 spin_unlock_irqrestore(io_lock, flags);
2013 break;
2014 }
2015 spin_unlock_irqrestore(io_lock, flags);
2016 if (fnic_queue_abort_io_req(fnic,
2017 tag | FNIC_TAG_DEV_RST,
2018 FCPIO_ITMF_ABT_TASK_TERM,
2019 fc_lun.scsi_lun, io_req)) {
2020 wait_for_completion_timeout(&tm_done,
2021 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2022 } else {
2023 spin_lock_irqsave(io_lock, flags);
2024 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2025 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2026 io_req->abts_done = &tm_done;
2027 spin_unlock_irqrestore(io_lock, flags);
2028 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2029 "Abort and terminate issued on Device reset "
2030 "tag 0x%x sc 0x%p\n", tag, sc);
2031 break;
2032 }
2033 }
2034 while (1) {
2035 spin_lock_irqsave(io_lock, flags);
2036 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2037 spin_unlock_irqrestore(io_lock, flags);
2038 wait_for_completion_timeout(&tm_done,
2039 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2040 break;
2041 } else {
2042 io_req = (struct fnic_io_req *)CMD_SP(sc);
2043 io_req->abts_done = NULL;
2044 goto fnic_device_reset_clean;
2045 }
2046 }
2047 } else {
2048 spin_unlock_irqrestore(io_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002049 }
2050
2051 /* Completed, but not successful, clean up the io_req, return fail */
2052 if (status != FCPIO_SUCCESS) {
2053 spin_lock_irqsave(io_lock, flags);
2054 FNIC_SCSI_DBG(KERN_DEBUG,
2055 fnic->lport->host,
2056 "Device reset completed - failed\n");
2057 io_req = (struct fnic_io_req *)CMD_SP(sc);
2058 goto fnic_device_reset_clean;
2059 }
2060
2061 /*
2062 * Clean up any aborts on this lun that have still not
2063 * completed. If any of these fail, then LUN reset fails.
2064 * clean_pending_aborts cleans all cmds on this lun except
2065 * the lun reset cmd. If all cmds get cleaned, the lun reset
2066 * succeeds
2067 */
2068 if (fnic_clean_pending_aborts(fnic, sc)) {
2069 spin_lock_irqsave(io_lock, flags);
2070 io_req = (struct fnic_io_req *)CMD_SP(sc);
2071 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2072 "Device reset failed"
2073 " since could not abort all IOs\n");
2074 goto fnic_device_reset_clean;
2075 }
2076
2077 /* Clean lun reset command */
2078 spin_lock_irqsave(io_lock, flags);
2079 io_req = (struct fnic_io_req *)CMD_SP(sc);
2080 if (io_req)
2081 /* Completed, and successful */
2082 ret = SUCCESS;
2083
2084fnic_device_reset_clean:
2085 if (io_req)
2086 CMD_SP(sc) = NULL;
2087
2088 spin_unlock_irqrestore(io_lock, flags);
2089
2090 if (io_req) {
Hiral Patel14eb5d92013-02-12 17:01:01 -08002091 start_time = io_req->start_time;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002092 fnic_release_ioreq_buf(fnic, io_req, sc);
2093 mempool_free(io_req, fnic->io_req_pool);
2094 }
2095
2096fnic_device_reset_end:
Hiral Patel03298552013-02-12 17:00:58 -08002097 /* free tag if it is allocated */
2098 if (unlikely(tag_gen_flag))
2099 fnic_scsi_host_end_tag(fnic, sc);
2100
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002101 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2102 "Returning from device reset %s\n",
2103 (ret == SUCCESS) ?
2104 "SUCCESS" : "FAILED");
2105 return ret;
2106}
2107
2108/* Clean up all IOs, clean up libFC local port */
2109int fnic_reset(struct Scsi_Host *shost)
2110{
2111 struct fc_lport *lp;
2112 struct fnic *fnic;
2113 int ret = SUCCESS;
2114
2115 lp = shost_priv(shost);
2116 fnic = lport_priv(lp);
2117
2118 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2119 "fnic_reset called\n");
2120
2121 /*
2122 * Reset local port, this will clean up libFC exchanges,
2123 * reset remote port sessions, and if link is up, begin flogi
2124 */
2125 if (lp->tt.lport_reset(lp))
2126 ret = FAILED;
2127
2128 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2129 "Returning from fnic reset %s\n",
2130 (ret == SUCCESS) ?
2131 "SUCCESS" : "FAILED");
2132
2133 return ret;
2134}
2135
2136/*
2137 * SCSI Error handling calls driver's eh_host_reset if all prior
2138 * error handling levels return FAILED. If host reset completes
2139 * successfully, and if link is up, then Fabric login begins.
2140 *
2141 * Host Reset is the highest level of error recovery. If this fails, then
2142 * host is offlined by SCSI.
2143 *
2144 */
2145int fnic_host_reset(struct scsi_cmnd *sc)
2146{
2147 int ret;
2148 unsigned long wait_host_tmo;
2149 struct Scsi_Host *shost = sc->device->host;
2150 struct fc_lport *lp = shost_priv(shost);
2151
2152 /*
2153 * If fnic_reset is successful, wait for fabric login to complete
2154 * scsi-ml tries to send a TUR to every device if host reset is
2155 * successful, so before returning to scsi, fabric should be up
2156 */
2157 ret = fnic_reset(shost);
2158 if (ret == SUCCESS) {
2159 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2160 ret = FAILED;
2161 while (time_before(jiffies, wait_host_tmo)) {
2162 if ((lp->state == LPORT_ST_READY) &&
2163 (lp->link_up)) {
2164 ret = SUCCESS;
2165 break;
2166 }
2167 ssleep(1);
2168 }
2169 }
2170
2171 return ret;
2172}
2173
2174/*
2175 * This fxn is called from libFC when host is removed
2176 */
2177void fnic_scsi_abort_io(struct fc_lport *lp)
2178{
2179 int err = 0;
2180 unsigned long flags;
2181 enum fnic_state old_state;
2182 struct fnic *fnic = lport_priv(lp);
2183 DECLARE_COMPLETION_ONSTACK(remove_wait);
2184
2185 /* Issue firmware reset for fnic, wait for reset to complete */
Hiral Patel03298552013-02-12 17:00:58 -08002186retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002187 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002188 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2189 /* fw reset is in progress, poll for its completion */
2190 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2191 schedule_timeout(msecs_to_jiffies(100));
2192 goto retry_fw_reset;
2193 }
2194
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002195 fnic->remove_wait = &remove_wait;
2196 old_state = fnic->state;
2197 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002198 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002199 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2200
2201 err = fnic_fw_reset_handler(fnic);
2202 if (err) {
2203 spin_lock_irqsave(&fnic->fnic_lock, flags);
2204 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2205 fnic->state = old_state;
2206 fnic->remove_wait = NULL;
2207 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2208 return;
2209 }
2210
2211 /* Wait for firmware reset to complete */
2212 wait_for_completion_timeout(&remove_wait,
2213 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2214
2215 spin_lock_irqsave(&fnic->fnic_lock, flags);
2216 fnic->remove_wait = NULL;
2217 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2218 "fnic_scsi_abort_io %s\n",
2219 (fnic->state == FNIC_IN_ETH_MODE) ?
2220 "SUCCESS" : "FAILED");
2221 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2222
2223}
2224
2225/*
2226 * This fxn called from libFC to clean up driver IO state on link down
2227 */
2228void fnic_scsi_cleanup(struct fc_lport *lp)
2229{
2230 unsigned long flags;
2231 enum fnic_state old_state;
2232 struct fnic *fnic = lport_priv(lp);
2233
2234 /* issue fw reset */
Hiral Patel03298552013-02-12 17:00:58 -08002235retry_fw_reset:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002236 spin_lock_irqsave(&fnic->fnic_lock, flags);
Hiral Patel03298552013-02-12 17:00:58 -08002237 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2238 /* fw reset is in progress, poll for its completion */
2239 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2240 schedule_timeout(msecs_to_jiffies(100));
2241 goto retry_fw_reset;
2242 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002243 old_state = fnic->state;
2244 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -08002245 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07002246 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2247
2248 if (fnic_fw_reset_handler(fnic)) {
2249 spin_lock_irqsave(&fnic->fnic_lock, flags);
2250 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2251 fnic->state = old_state;
2252 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2253 }
2254
2255}
2256
2257void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2258{
2259}
2260
2261void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2262{
2263 struct fnic *fnic = lport_priv(lp);
2264
2265 /* Non-zero sid, nothing to do */
2266 if (sid)
2267 goto call_fc_exch_mgr_reset;
2268
2269 if (did) {
2270 fnic_rport_exch_reset(fnic, did);
2271 goto call_fc_exch_mgr_reset;
2272 }
2273
2274 /*
2275 * sid = 0, did = 0
2276 * link down or device being removed
2277 */
2278 if (!fnic->in_remove)
2279 fnic_scsi_cleanup(lp);
2280 else
2281 fnic_scsi_abort_io(lp);
2282
2283 /* call libFC exch mgr reset to reset its exchanges */
2284call_fc_exch_mgr_reset:
2285 fc_exch_mgr_reset(lp, sid, did);
2286
2287}
Hiral Patela0bf1ca2013-02-12 17:01:00 -08002288
2289/*
2290 * fnic_is_abts_pending() is a helper function that
2291 * walks through tag map to check if there is any IOs pending,if there is one,
2292 * then it returns 1 (true), otherwise 0 (false)
2293 * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2294 * otherwise, it checks for all IOs.
2295 */
2296int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2297{
2298 int tag;
2299 struct fnic_io_req *io_req;
2300 spinlock_t *io_lock;
2301 unsigned long flags;
2302 int ret = 0;
2303 struct scsi_cmnd *sc;
2304 struct scsi_device *lun_dev = NULL;
2305
2306 if (lr_sc)
2307 lun_dev = lr_sc->device;
2308
2309 /* walk again to check, if IOs are still pending in fw */
2310 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
2311 sc = scsi_host_find_tag(fnic->lport->host, tag);
2312 /*
2313 * ignore this lun reset cmd or cmds that do not belong to
2314 * this lun
2315 */
2316 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2317 continue;
2318
2319 io_lock = fnic_io_lock_hash(fnic, sc);
2320 spin_lock_irqsave(io_lock, flags);
2321
2322 io_req = (struct fnic_io_req *)CMD_SP(sc);
2323
2324 if (!io_req || sc->device != lun_dev) {
2325 spin_unlock_irqrestore(io_lock, flags);
2326 continue;
2327 }
2328
2329 /*
2330 * Found IO that is still pending with firmware and
2331 * belongs to the LUN that we are resetting
2332 */
2333 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2334 "Found IO in %s on lun\n",
2335 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2336
2337 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2338 spin_unlock_irqrestore(io_lock, flags);
2339 ret = 1;
2340 continue;
2341 }
2342 }
2343
2344 return ret;
2345}