mm-only debug patch...
[mmotm.git] / drivers / scsi / fnic / fnic_scsi.c
blobc04b7e8ebb2f5118d8db1c65eec3f523c5cc851e
1 /*
2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
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.
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.
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>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/fc/fc_els.h>
35 #include <scsi/fc/fc_fcoe.h>
36 #include <scsi/libfc.h>
37 #include <scsi/fc_frame.h>
38 #include "fnic_io.h"
39 #include "fnic.h"
41 const char *fnic_state_str[] = {
42 [FNIC_IN_FC_MODE] = "FNIC_IN_FC_MODE",
43 [FNIC_IN_FC_TRANS_ETH_MODE] = "FNIC_IN_FC_TRANS_ETH_MODE",
44 [FNIC_IN_ETH_MODE] = "FNIC_IN_ETH_MODE",
45 [FNIC_IN_ETH_TRANS_FC_MODE] = "FNIC_IN_ETH_TRANS_FC_MODE",
48 static const char *fnic_ioreq_state_str[] = {
49 [FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
50 [FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
51 [FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
52 [FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
55 static const char *fcpio_status_str[] = {
56 [FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
57 [FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
58 [FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
59 [FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
60 [FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
61 [FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
62 [FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
63 [FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
64 [FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
65 [FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
66 [FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
67 [FCPIO_FW_ERR] = "FCPIO_FW_ERR",
68 [FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
69 [FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
70 [FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
71 [FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
72 [FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
73 [FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
74 [FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
77 const char *fnic_state_to_str(unsigned int state)
79 if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
80 return "unknown";
82 return fnic_state_str[state];
85 static const char *fnic_ioreq_state_to_str(unsigned int state)
87 if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
88 !fnic_ioreq_state_str[state])
89 return "unknown";
91 return fnic_ioreq_state_str[state];
94 static const char *fnic_fcpio_status_to_str(unsigned int status)
96 if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
97 return "unknown";
99 return fcpio_status_str[status];
102 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
104 static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
105 struct scsi_cmnd *sc)
107 u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
109 return &fnic->io_req_lock[hash];
113 * Unmap the data buffer and sense buffer for an io_req,
114 * also unmap and free the device-private scatter/gather list.
116 static void fnic_release_ioreq_buf(struct fnic *fnic,
117 struct fnic_io_req *io_req,
118 struct scsi_cmnd *sc)
120 if (io_req->sgl_list_pa)
121 pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
122 sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
123 PCI_DMA_TODEVICE);
124 scsi_dma_unmap(sc);
126 if (io_req->sgl_cnt)
127 mempool_free(io_req->sgl_list_alloc,
128 fnic->io_sgl_pool[io_req->sgl_type]);
129 if (io_req->sense_buf_pa)
130 pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
131 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
134 /* Free up Copy Wq descriptors. Called with copy_wq lock held */
135 static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
137 /* if no Ack received from firmware, then nothing to clean */
138 if (!fnic->fw_ack_recd[0])
139 return 1;
142 * Update desc_available count based on number of freed descriptors
143 * Account for wraparound
145 if (wq->to_clean_index <= fnic->fw_ack_index[0])
146 wq->ring.desc_avail += (fnic->fw_ack_index[0]
147 - wq->to_clean_index + 1);
148 else
149 wq->ring.desc_avail += (wq->ring.desc_count
150 - wq->to_clean_index
151 + fnic->fw_ack_index[0] + 1);
154 * just bump clean index to ack_index+1 accounting for wraparound
155 * this will essentially free up all descriptors between
156 * to_clean_index and fw_ack_index, both inclusive
158 wq->to_clean_index =
159 (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
161 /* we have processed the acks received so far */
162 fnic->fw_ack_recd[0] = 0;
163 return 0;
168 * fnic_fw_reset_handler
169 * Routine to send reset msg to fw
171 int fnic_fw_reset_handler(struct fnic *fnic)
173 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
174 int ret = 0;
175 unsigned long flags;
177 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
179 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
180 free_wq_copy_descs(fnic, wq);
182 if (!vnic_wq_copy_desc_avail(wq))
183 ret = -EAGAIN;
184 else
185 fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
187 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
189 if (!ret)
190 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
191 "Issued fw reset\n");
192 else
193 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
194 "Failed to issue fw reset\n");
195 return ret;
200 * fnic_flogi_reg_handler
201 * Routine to send flogi register msg to fw
203 int fnic_flogi_reg_handler(struct fnic *fnic)
205 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
206 u8 gw_mac[ETH_ALEN];
207 int ret = 0;
208 unsigned long flags;
210 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
212 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
213 free_wq_copy_descs(fnic, wq);
215 if (!vnic_wq_copy_desc_avail(wq)) {
216 ret = -EAGAIN;
217 goto flogi_reg_ioreq_end;
220 if (fnic->fcoui_mode)
221 memset(gw_mac, 0xff, ETH_ALEN);
222 else
223 memcpy(gw_mac, fnic->dest_addr, ETH_ALEN);
225 fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
226 FCPIO_FLOGI_REG_GW_DEST,
227 fnic->s_id,
228 gw_mac);
230 flogi_reg_ioreq_end:
231 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
233 if (!ret)
234 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
235 "flog reg issued\n");
237 return ret;
241 * fnic_queue_wq_copy_desc
242 * Routine to enqueue a wq copy desc
244 static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
245 struct vnic_wq_copy *wq,
246 struct fnic_io_req *io_req,
247 struct scsi_cmnd *sc,
248 int sg_count)
250 struct scatterlist *sg;
251 struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
252 struct fc_rport_libfc_priv *rp = rport->dd_data;
253 struct host_sg_desc *desc;
254 u8 pri_tag = 0;
255 unsigned int i;
256 unsigned long intr_flags;
257 int flags;
258 u8 exch_flags;
259 struct scsi_lun fc_lun;
260 char msg[2];
262 if (sg_count) {
263 /* For each SGE, create a device desc entry */
264 desc = io_req->sgl_list;
265 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
266 desc->addr = cpu_to_le64(sg_dma_address(sg));
267 desc->len = cpu_to_le32(sg_dma_len(sg));
268 desc->_resvd = 0;
269 desc++;
272 io_req->sgl_list_pa = pci_map_single
273 (fnic->pdev,
274 io_req->sgl_list,
275 sizeof(io_req->sgl_list[0]) * sg_count,
276 PCI_DMA_TODEVICE);
279 io_req->sense_buf_pa = pci_map_single(fnic->pdev,
280 sc->sense_buffer,
281 SCSI_SENSE_BUFFERSIZE,
282 PCI_DMA_FROMDEVICE);
284 int_to_scsilun(sc->device->lun, &fc_lun);
286 pri_tag = FCPIO_ICMND_PTA_SIMPLE;
287 msg[0] = MSG_SIMPLE_TAG;
288 scsi_populate_tag_msg(sc, msg);
289 if (msg[0] == MSG_ORDERED_TAG)
290 pri_tag = FCPIO_ICMND_PTA_ORDERED;
292 /* Enqueue the descriptor in the Copy WQ */
293 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
295 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
296 free_wq_copy_descs(fnic, wq);
298 if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
299 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
300 return SCSI_MLQUEUE_HOST_BUSY;
303 flags = 0;
304 if (sc->sc_data_direction == DMA_FROM_DEVICE)
305 flags = FCPIO_ICMND_RDDATA;
306 else if (sc->sc_data_direction == DMA_TO_DEVICE)
307 flags = FCPIO_ICMND_WRDATA;
309 exch_flags = 0;
310 if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
311 (rp->flags & FC_RP_FLAGS_RETRY))
312 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
314 fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
315 0, exch_flags, io_req->sgl_cnt,
316 SCSI_SENSE_BUFFERSIZE,
317 io_req->sgl_list_pa,
318 io_req->sense_buf_pa,
319 0, /* scsi cmd ref, always 0 */
320 pri_tag, /* scsi pri and tag */
321 flags, /* command flags */
322 sc->cmnd, scsi_bufflen(sc),
323 fc_lun.scsi_lun, io_req->port_id,
324 rport->maxframe_size, rp->r_a_tov,
325 rp->e_d_tov);
327 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
328 return 0;
332 * fnic_queuecommand
333 * Routine to send a scsi cdb
334 * Called with host_lock held and interrupts disabled.
336 int fnic_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
338 struct fc_lport *lp;
339 struct fc_rport *rport;
340 struct fnic_io_req *io_req;
341 struct fnic *fnic;
342 struct vnic_wq_copy *wq;
343 int ret;
344 int sg_count;
345 unsigned long flags;
346 unsigned long ptr;
348 rport = starget_to_rport(scsi_target(sc->device));
349 ret = fc_remote_port_chkready(rport);
350 if (ret) {
351 sc->result = ret;
352 done(sc);
353 return 0;
356 lp = shost_priv(sc->device->host);
357 if (lp->state != LPORT_ST_READY || !(lp->link_up))
358 return SCSI_MLQUEUE_HOST_BUSY;
361 * Release host lock, use driver resource specific locks from here.
362 * Don't re-enable interrupts in case they were disabled prior to the
363 * caller disabling them.
365 spin_unlock(lp->host->host_lock);
367 /* Get a new io_req for this SCSI IO */
368 fnic = lport_priv(lp);
370 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
371 if (!io_req) {
372 ret = SCSI_MLQUEUE_HOST_BUSY;
373 goto out;
375 memset(io_req, 0, sizeof(*io_req));
377 /* Map the data buffer */
378 sg_count = scsi_dma_map(sc);
379 if (sg_count < 0) {
380 mempool_free(io_req, fnic->io_req_pool);
381 goto out;
384 /* Determine the type of scatter/gather list we need */
385 io_req->sgl_cnt = sg_count;
386 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
387 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
388 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
390 if (sg_count) {
391 io_req->sgl_list =
392 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
393 GFP_ATOMIC | GFP_DMA);
394 if (!io_req->sgl_list) {
395 ret = SCSI_MLQUEUE_HOST_BUSY;
396 scsi_dma_unmap(sc);
397 mempool_free(io_req, fnic->io_req_pool);
398 goto out;
401 /* Cache sgl list allocated address before alignment */
402 io_req->sgl_list_alloc = io_req->sgl_list;
403 ptr = (unsigned long) io_req->sgl_list;
404 if (ptr % FNIC_SG_DESC_ALIGN) {
405 io_req->sgl_list = (struct host_sg_desc *)
406 (((unsigned long) ptr
407 + FNIC_SG_DESC_ALIGN - 1)
408 & ~(FNIC_SG_DESC_ALIGN - 1));
412 /* initialize rest of io_req */
413 io_req->port_id = rport->port_id;
414 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
415 CMD_SP(sc) = (char *)io_req;
416 sc->scsi_done = done;
418 /* create copy wq desc and enqueue it */
419 wq = &fnic->wq_copy[0];
420 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
421 if (ret) {
423 * In case another thread cancelled the request,
424 * refetch the pointer under the lock.
426 spinlock_t *io_lock = fnic_io_lock_hash(fnic, sc);
428 spin_lock_irqsave(io_lock, flags);
429 io_req = (struct fnic_io_req *)CMD_SP(sc);
430 CMD_SP(sc) = NULL;
431 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
432 spin_unlock_irqrestore(io_lock, flags);
433 if (io_req) {
434 fnic_release_ioreq_buf(fnic, io_req, sc);
435 mempool_free(io_req, fnic->io_req_pool);
438 out:
439 /* acquire host lock before returning to SCSI */
440 spin_lock(lp->host->host_lock);
441 return ret;
445 * fnic_fcpio_fw_reset_cmpl_handler
446 * Routine to handle fw reset completion
448 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
449 struct fcpio_fw_req *desc)
451 u8 type;
452 u8 hdr_status;
453 struct fcpio_tag tag;
454 int ret = 0;
455 struct fc_frame *flogi;
456 unsigned long flags;
458 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
460 /* Clean up all outstanding io requests */
461 fnic_cleanup_io(fnic, SCSI_NO_TAG);
463 spin_lock_irqsave(&fnic->fnic_lock, flags);
465 flogi = fnic->flogi;
466 fnic->flogi = NULL;
468 /* fnic should be in FC_TRANS_ETH_MODE */
469 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
470 /* Check status of reset completion */
471 if (!hdr_status) {
472 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
473 "reset cmpl success\n");
474 /* Ready to send flogi out */
475 fnic->state = FNIC_IN_ETH_MODE;
476 } else {
477 FNIC_SCSI_DBG(KERN_DEBUG,
478 fnic->lport->host,
479 "fnic fw_reset : failed %s\n",
480 fnic_fcpio_status_to_str(hdr_status));
483 * Unable to change to eth mode, cannot send out flogi
484 * Change state to fc mode, so that subsequent Flogi
485 * requests from libFC will cause more attempts to
486 * reset the firmware. Free the cached flogi
488 fnic->state = FNIC_IN_FC_MODE;
489 ret = -1;
491 } else {
492 FNIC_SCSI_DBG(KERN_DEBUG,
493 fnic->lport->host,
494 "Unexpected state %s while processing"
495 " reset cmpl\n", fnic_state_to_str(fnic->state));
496 ret = -1;
499 /* Thread removing device blocks till firmware reset is complete */
500 if (fnic->remove_wait)
501 complete(fnic->remove_wait);
504 * If fnic is being removed, or fw reset failed
505 * free the flogi frame. Else, send it out
507 if (fnic->remove_wait || ret) {
508 fnic->flogi_oxid = FC_XID_UNKNOWN;
509 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
510 if (flogi)
511 dev_kfree_skb_irq(fp_skb(flogi));
512 goto reset_cmpl_handler_end;
515 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
517 if (flogi)
518 ret = fnic_send_frame(fnic, flogi);
520 reset_cmpl_handler_end:
521 return ret;
525 * fnic_fcpio_flogi_reg_cmpl_handler
526 * Routine to handle flogi register completion
528 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
529 struct fcpio_fw_req *desc)
531 u8 type;
532 u8 hdr_status;
533 struct fcpio_tag tag;
534 int ret = 0;
535 struct fc_frame *flogi_resp = NULL;
536 unsigned long flags;
537 struct sk_buff *skb;
539 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
541 /* Update fnic state based on status of flogi reg completion */
542 spin_lock_irqsave(&fnic->fnic_lock, flags);
544 flogi_resp = fnic->flogi_resp;
545 fnic->flogi_resp = NULL;
547 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
549 /* Check flogi registration completion status */
550 if (!hdr_status) {
551 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
552 "flog reg succeeded\n");
553 fnic->state = FNIC_IN_FC_MODE;
554 } else {
555 FNIC_SCSI_DBG(KERN_DEBUG,
556 fnic->lport->host,
557 "fnic flogi reg :failed %s\n",
558 fnic_fcpio_status_to_str(hdr_status));
559 fnic->state = FNIC_IN_ETH_MODE;
560 ret = -1;
562 } else {
563 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
564 "Unexpected fnic state %s while"
565 " processing flogi reg completion\n",
566 fnic_state_to_str(fnic->state));
567 ret = -1;
570 /* Successful flogi reg cmpl, pass frame to LibFC */
571 if (!ret && flogi_resp) {
572 if (fnic->stop_rx_link_events) {
573 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
574 goto reg_cmpl_handler_end;
576 skb = (struct sk_buff *)flogi_resp;
577 /* Use fr_flags to indicate whether flogi resp or not */
578 fr_flags(flogi_resp) = 1;
579 fr_dev(flogi_resp) = fnic->lport;
580 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
582 skb_queue_tail(&fnic->frame_queue, skb);
583 queue_work(fnic_event_queue, &fnic->frame_work);
585 } else {
586 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
587 if (flogi_resp)
588 dev_kfree_skb_irq(fp_skb(flogi_resp));
591 reg_cmpl_handler_end:
592 return ret;
595 static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
596 u16 request_out)
598 if (wq->to_clean_index <= wq->to_use_index) {
599 /* out of range, stale request_out index */
600 if (request_out < wq->to_clean_index ||
601 request_out >= wq->to_use_index)
602 return 0;
603 } else {
604 /* out of range, stale request_out index */
605 if (request_out < wq->to_clean_index &&
606 request_out >= wq->to_use_index)
607 return 0;
609 /* request_out index is in range */
610 return 1;
615 * Mark that ack received and store the Ack index. If there are multiple
616 * acks received before Tx thread cleans it up, the latest value will be
617 * used which is correct behavior. This state should be in the copy Wq
618 * instead of in the fnic
620 static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
621 unsigned int cq_index,
622 struct fcpio_fw_req *desc)
624 struct vnic_wq_copy *wq;
625 u16 request_out = desc->u.ack.request_out;
626 unsigned long flags;
628 /* mark the ack state */
629 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
630 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
632 if (is_ack_index_in_range(wq, request_out)) {
633 fnic->fw_ack_index[0] = request_out;
634 fnic->fw_ack_recd[0] = 1;
636 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
640 * fnic_fcpio_icmnd_cmpl_handler
641 * Routine to handle icmnd completions
643 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
644 struct fcpio_fw_req *desc)
646 u8 type;
647 u8 hdr_status;
648 struct fcpio_tag tag;
649 u32 id;
650 u64 xfer_len = 0;
651 struct fcpio_icmnd_cmpl *icmnd_cmpl;
652 struct fnic_io_req *io_req;
653 struct scsi_cmnd *sc;
654 unsigned long flags;
655 spinlock_t *io_lock;
657 /* Decode the cmpl description to get the io_req id */
658 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
659 fcpio_tag_id_dec(&tag, &id);
661 if (id >= FNIC_MAX_IO_REQ)
662 return;
664 sc = scsi_host_find_tag(fnic->lport->host, id);
665 WARN_ON_ONCE(!sc);
666 if (!sc)
667 return;
669 io_lock = fnic_io_lock_hash(fnic, sc);
670 spin_lock_irqsave(io_lock, flags);
671 io_req = (struct fnic_io_req *)CMD_SP(sc);
672 WARN_ON_ONCE(!io_req);
673 if (!io_req) {
674 spin_unlock_irqrestore(io_lock, flags);
675 return;
678 /* firmware completed the io */
679 io_req->io_completed = 1;
682 * if SCSI-ML has already issued abort on this command,
683 * ignore completion of the IO. The abts path will clean it up
685 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
686 spin_unlock_irqrestore(io_lock, flags);
687 return;
690 /* Mark the IO as complete */
691 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
693 icmnd_cmpl = &desc->u.icmnd_cmpl;
695 switch (hdr_status) {
696 case FCPIO_SUCCESS:
697 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
698 xfer_len = scsi_bufflen(sc);
699 scsi_set_resid(sc, icmnd_cmpl->residual);
701 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
702 xfer_len -= icmnd_cmpl->residual;
705 * If queue_full, then try to reduce queue depth for all
706 * LUNS on the target. Todo: this should be accompanied
707 * by a periodic queue_depth rampup based on successful
708 * IO completion.
710 if (icmnd_cmpl->scsi_status == QUEUE_FULL) {
711 struct scsi_device *t_sdev;
712 int qd = 0;
714 shost_for_each_device(t_sdev, sc->device->host) {
715 if (t_sdev->id != sc->device->id)
716 continue;
718 if (t_sdev->queue_depth > 1) {
719 qd = scsi_track_queue_full
720 (t_sdev,
721 t_sdev->queue_depth - 1);
722 if (qd == -1)
723 qd = t_sdev->host->cmd_per_lun;
724 shost_printk(KERN_INFO,
725 fnic->lport->host,
726 "scsi[%d:%d:%d:%d"
727 "] queue full detected,"
728 "new depth = %d\n",
729 t_sdev->host->host_no,
730 t_sdev->channel,
731 t_sdev->id, t_sdev->lun,
732 t_sdev->queue_depth);
736 break;
738 case FCPIO_TIMEOUT: /* request was timed out */
739 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
740 break;
742 case FCPIO_ABORTED: /* request was aborted */
743 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
744 break;
746 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
747 scsi_set_resid(sc, icmnd_cmpl->residual);
748 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
749 break;
751 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
752 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
753 break;
754 case FCPIO_INVALID_HEADER: /* header contains invalid data */
755 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
756 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
757 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
758 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
759 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
760 case FCPIO_FW_ERR: /* request was terminated due fw error */
761 default:
762 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
763 fnic_fcpio_status_to_str(hdr_status));
764 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
765 break;
768 /* Break link with the SCSI command */
769 CMD_SP(sc) = NULL;
771 spin_unlock_irqrestore(io_lock, flags);
773 fnic_release_ioreq_buf(fnic, io_req, sc);
775 mempool_free(io_req, fnic->io_req_pool);
777 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
778 fnic->lport->host_stats.fcp_input_requests++;
779 fnic->fcp_input_bytes += xfer_len;
780 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
781 fnic->lport->host_stats.fcp_output_requests++;
782 fnic->fcp_output_bytes += xfer_len;
783 } else
784 fnic->lport->host_stats.fcp_control_requests++;
786 /* Call SCSI completion function to complete the IO */
787 if (sc->scsi_done)
788 sc->scsi_done(sc);
792 /* fnic_fcpio_itmf_cmpl_handler
793 * Routine to handle itmf completions
795 static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
796 struct fcpio_fw_req *desc)
798 u8 type;
799 u8 hdr_status;
800 struct fcpio_tag tag;
801 u32 id;
802 struct scsi_cmnd *sc;
803 struct fnic_io_req *io_req;
804 unsigned long flags;
805 spinlock_t *io_lock;
807 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
808 fcpio_tag_id_dec(&tag, &id);
810 if ((id & FNIC_TAG_MASK) >= FNIC_MAX_IO_REQ)
811 return;
813 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
814 WARN_ON_ONCE(!sc);
815 if (!sc)
816 return;
818 io_lock = fnic_io_lock_hash(fnic, sc);
819 spin_lock_irqsave(io_lock, flags);
820 io_req = (struct fnic_io_req *)CMD_SP(sc);
821 WARN_ON_ONCE(!io_req);
822 if (!io_req) {
823 spin_unlock_irqrestore(io_lock, flags);
824 return;
827 if (id & FNIC_TAG_ABORT) {
828 /* Completion of abort cmd */
829 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
830 /* This is a late completion. Ignore it */
831 spin_unlock_irqrestore(io_lock, flags);
832 return;
834 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
835 CMD_ABTS_STATUS(sc) = hdr_status;
837 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
838 "abts cmpl recd. id %d status %s\n",
839 (int)(id & FNIC_TAG_MASK),
840 fnic_fcpio_status_to_str(hdr_status));
843 * If scsi_eh thread is blocked waiting for abts to complete,
844 * signal completion to it. IO will be cleaned in the thread
845 * else clean it in this context
847 if (io_req->abts_done) {
848 complete(io_req->abts_done);
849 spin_unlock_irqrestore(io_lock, flags);
850 } else {
851 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
852 "abts cmpl, completing IO\n");
853 CMD_SP(sc) = NULL;
854 sc->result = (DID_ERROR << 16);
856 spin_unlock_irqrestore(io_lock, flags);
858 fnic_release_ioreq_buf(fnic, io_req, sc);
859 mempool_free(io_req, fnic->io_req_pool);
860 if (sc->scsi_done)
861 sc->scsi_done(sc);
864 } else if (id & FNIC_TAG_DEV_RST) {
865 /* Completion of device reset */
866 CMD_LR_STATUS(sc) = hdr_status;
867 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
868 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
869 "dev reset cmpl recd. id %d status %s\n",
870 (int)(id & FNIC_TAG_MASK),
871 fnic_fcpio_status_to_str(hdr_status));
872 if (io_req->dr_done)
873 complete(io_req->dr_done);
874 spin_unlock_irqrestore(io_lock, flags);
876 } else {
877 shost_printk(KERN_ERR, fnic->lport->host,
878 "Unexpected itmf io state %s tag %x\n",
879 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
880 spin_unlock_irqrestore(io_lock, flags);
886 * fnic_fcpio_cmpl_handler
887 * Routine to service the cq for wq_copy
889 static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
890 unsigned int cq_index,
891 struct fcpio_fw_req *desc)
893 struct fnic *fnic = vnic_dev_priv(vdev);
894 int ret = 0;
896 switch (desc->hdr.type) {
897 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
898 fnic_fcpio_ack_handler(fnic, cq_index, desc);
899 break;
901 case FCPIO_ICMND_CMPL: /* fw completed a command */
902 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
903 break;
905 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
906 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
907 break;
909 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
910 ret = fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
911 break;
913 case FCPIO_RESET_CMPL: /* fw completed reset */
914 ret = fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
915 break;
917 default:
918 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
919 "firmware completion type %d\n",
920 desc->hdr.type);
921 break;
924 return ret;
928 * fnic_wq_copy_cmpl_handler
929 * Routine to process wq copy
931 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
933 unsigned int wq_work_done = 0;
934 unsigned int i, cq_index;
935 unsigned int cur_work_done;
937 for (i = 0; i < fnic->wq_copy_count; i++) {
938 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
939 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
940 fnic_fcpio_cmpl_handler,
941 copy_work_to_do);
942 wq_work_done += cur_work_done;
944 return wq_work_done;
947 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
949 unsigned int i;
950 struct fnic_io_req *io_req;
951 unsigned long flags = 0;
952 struct scsi_cmnd *sc;
953 spinlock_t *io_lock;
955 for (i = 0; i < FNIC_MAX_IO_REQ; i++) {
956 if (i == exclude_id)
957 continue;
959 sc = scsi_host_find_tag(fnic->lport->host, i);
960 if (!sc)
961 continue;
963 io_lock = fnic_io_lock_hash(fnic, sc);
964 spin_lock_irqsave(io_lock, flags);
965 io_req = (struct fnic_io_req *)CMD_SP(sc);
966 if (!io_req) {
967 spin_unlock_irqrestore(io_lock, flags);
968 goto cleanup_scsi_cmd;
971 CMD_SP(sc) = NULL;
973 spin_unlock_irqrestore(io_lock, flags);
976 * If there is a scsi_cmnd associated with this io_req, then
977 * free the corresponding state
979 fnic_release_ioreq_buf(fnic, io_req, sc);
980 mempool_free(io_req, fnic->io_req_pool);
982 cleanup_scsi_cmd:
983 sc->result = DID_TRANSPORT_DISRUPTED << 16;
984 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "fnic_cleanup_io:"
985 " DID_TRANSPORT_DISRUPTED\n");
987 /* Complete the command to SCSI */
988 if (sc->scsi_done)
989 sc->scsi_done(sc);
993 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
994 struct fcpio_host_req *desc)
996 u32 id;
997 struct fnic *fnic = vnic_dev_priv(wq->vdev);
998 struct fnic_io_req *io_req;
999 struct scsi_cmnd *sc;
1000 unsigned long flags;
1001 spinlock_t *io_lock;
1003 /* get the tag reference */
1004 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1005 id &= FNIC_TAG_MASK;
1007 if (id >= FNIC_MAX_IO_REQ)
1008 return;
1010 sc = scsi_host_find_tag(fnic->lport->host, id);
1011 if (!sc)
1012 return;
1014 io_lock = fnic_io_lock_hash(fnic, sc);
1015 spin_lock_irqsave(io_lock, flags);
1017 /* Get the IO context which this desc refers to */
1018 io_req = (struct fnic_io_req *)CMD_SP(sc);
1020 /* fnic interrupts are turned off by now */
1022 if (!io_req) {
1023 spin_unlock_irqrestore(io_lock, flags);
1024 goto wq_copy_cleanup_scsi_cmd;
1027 CMD_SP(sc) = NULL;
1029 spin_unlock_irqrestore(io_lock, flags);
1031 fnic_release_ioreq_buf(fnic, io_req, sc);
1032 mempool_free(io_req, fnic->io_req_pool);
1034 wq_copy_cleanup_scsi_cmd:
1035 sc->result = DID_NO_CONNECT << 16;
1036 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1037 " DID_NO_CONNECT\n");
1039 if (sc->scsi_done)
1040 sc->scsi_done(sc);
1043 static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1044 u32 task_req, u8 *fc_lun,
1045 struct fnic_io_req *io_req)
1047 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1048 unsigned long flags;
1050 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1052 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1053 free_wq_copy_descs(fnic, wq);
1055 if (!vnic_wq_copy_desc_avail(wq)) {
1056 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1057 return 1;
1059 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1060 0, task_req, tag, fc_lun, io_req->port_id,
1061 fnic->config.ra_tov, fnic->config.ed_tov);
1063 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1064 return 0;
1067 void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1069 int tag;
1070 struct fnic_io_req *io_req;
1071 spinlock_t *io_lock;
1072 unsigned long flags;
1073 struct scsi_cmnd *sc;
1074 struct scsi_lun fc_lun;
1075 enum fnic_ioreq_state old_ioreq_state;
1077 FNIC_SCSI_DBG(KERN_DEBUG,
1078 fnic->lport->host,
1079 "fnic_rport_reset_exch called portid 0x%06x\n",
1080 port_id);
1082 if (fnic->in_remove)
1083 return;
1085 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1086 sc = scsi_host_find_tag(fnic->lport->host, tag);
1087 if (!sc)
1088 continue;
1090 io_lock = fnic_io_lock_hash(fnic, sc);
1091 spin_lock_irqsave(io_lock, flags);
1093 io_req = (struct fnic_io_req *)CMD_SP(sc);
1095 if (!io_req || io_req->port_id != port_id) {
1096 spin_unlock_irqrestore(io_lock, flags);
1097 continue;
1101 * Found IO that is still pending with firmware and
1102 * belongs to rport that went away
1104 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1105 spin_unlock_irqrestore(io_lock, flags);
1106 continue;
1108 old_ioreq_state = CMD_STATE(sc);
1109 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1110 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1112 BUG_ON(io_req->abts_done);
1114 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1115 "fnic_rport_reset_exch: Issuing abts\n");
1117 spin_unlock_irqrestore(io_lock, flags);
1119 /* Now queue the abort command to firmware */
1120 int_to_scsilun(sc->device->lun, &fc_lun);
1122 if (fnic_queue_abort_io_req(fnic, tag,
1123 FCPIO_ITMF_ABT_TASK_TERM,
1124 fc_lun.scsi_lun, io_req)) {
1126 * Revert the cmd state back to old state, if
1127 * it hasnt changed in between. This cmd will get
1128 * aborted later by scsi_eh, or cleaned up during
1129 * lun reset
1131 io_lock = fnic_io_lock_hash(fnic, sc);
1133 spin_lock_irqsave(io_lock, flags);
1134 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1135 CMD_STATE(sc) = old_ioreq_state;
1136 spin_unlock_irqrestore(io_lock, flags);
1142 void fnic_terminate_rport_io(struct fc_rport *rport)
1144 int tag;
1145 struct fnic_io_req *io_req;
1146 spinlock_t *io_lock;
1147 unsigned long flags;
1148 struct scsi_cmnd *sc;
1149 struct scsi_lun fc_lun;
1150 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1151 struct fc_lport *lport = rdata->local_port;
1152 struct fnic *fnic = lport_priv(lport);
1153 struct fc_rport *cmd_rport;
1154 enum fnic_ioreq_state old_ioreq_state;
1156 FNIC_SCSI_DBG(KERN_DEBUG,
1157 fnic->lport->host, "fnic_terminate_rport_io called"
1158 " wwpn 0x%llx, wwnn0x%llx, portid 0x%06x\n",
1159 rport->port_name, rport->node_name,
1160 rport->port_id);
1162 if (fnic->in_remove)
1163 return;
1165 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1166 sc = scsi_host_find_tag(fnic->lport->host, tag);
1167 if (!sc)
1168 continue;
1170 cmd_rport = starget_to_rport(scsi_target(sc->device));
1171 if (rport != cmd_rport)
1172 continue;
1174 io_lock = fnic_io_lock_hash(fnic, sc);
1175 spin_lock_irqsave(io_lock, flags);
1177 io_req = (struct fnic_io_req *)CMD_SP(sc);
1179 if (!io_req || rport != cmd_rport) {
1180 spin_unlock_irqrestore(io_lock, flags);
1181 continue;
1185 * Found IO that is still pending with firmware and
1186 * belongs to rport that went away
1188 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1189 spin_unlock_irqrestore(io_lock, flags);
1190 continue;
1192 old_ioreq_state = CMD_STATE(sc);
1193 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1194 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1196 BUG_ON(io_req->abts_done);
1198 FNIC_SCSI_DBG(KERN_DEBUG,
1199 fnic->lport->host,
1200 "fnic_terminate_rport_io: Issuing abts\n");
1202 spin_unlock_irqrestore(io_lock, flags);
1204 /* Now queue the abort command to firmware */
1205 int_to_scsilun(sc->device->lun, &fc_lun);
1207 if (fnic_queue_abort_io_req(fnic, tag,
1208 FCPIO_ITMF_ABT_TASK_TERM,
1209 fc_lun.scsi_lun, io_req)) {
1211 * Revert the cmd state back to old state, if
1212 * it hasnt changed in between. This cmd will get
1213 * aborted later by scsi_eh, or cleaned up during
1214 * lun reset
1216 io_lock = fnic_io_lock_hash(fnic, sc);
1218 spin_lock_irqsave(io_lock, flags);
1219 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1220 CMD_STATE(sc) = old_ioreq_state;
1221 spin_unlock_irqrestore(io_lock, flags);
1227 static void fnic_block_error_handler(struct scsi_cmnd *sc)
1229 struct Scsi_Host *shost = sc->device->host;
1230 struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
1231 unsigned long flags;
1233 spin_lock_irqsave(shost->host_lock, flags);
1234 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1235 spin_unlock_irqrestore(shost->host_lock, flags);
1236 msleep(1000);
1237 spin_lock_irqsave(shost->host_lock, flags);
1239 spin_unlock_irqrestore(shost->host_lock, flags);
1244 * This function is exported to SCSI for sending abort cmnds.
1245 * A SCSI IO is represented by a io_req in the driver.
1246 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1248 int fnic_abort_cmd(struct scsi_cmnd *sc)
1250 struct fc_lport *lp;
1251 struct fnic *fnic;
1252 struct fnic_io_req *io_req;
1253 struct fc_rport *rport;
1254 spinlock_t *io_lock;
1255 unsigned long flags;
1256 int ret = SUCCESS;
1257 u32 task_req;
1258 struct scsi_lun fc_lun;
1259 DECLARE_COMPLETION_ONSTACK(tm_done);
1261 /* Wait for rport to unblock */
1262 fnic_block_error_handler(sc);
1264 /* Get local-port, check ready and link up */
1265 lp = shost_priv(sc->device->host);
1267 fnic = lport_priv(lp);
1268 rport = starget_to_rport(scsi_target(sc->device));
1269 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1270 "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %d\n",
1271 rport->port_id, sc->device->lun, sc->request->tag);
1273 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1274 ret = FAILED;
1275 goto fnic_abort_cmd_end;
1279 * Avoid a race between SCSI issuing the abort and the device
1280 * completing the command.
1282 * If the command is already completed by the fw cmpl code,
1283 * we just return SUCCESS from here. This means that the abort
1284 * succeeded. In the SCSI ML, since the timeout for command has
1285 * happened, the completion wont actually complete the command
1286 * and it will be considered as an aborted command
1288 * The CMD_SP will not be cleared except while holding io_req_lock.
1290 io_lock = fnic_io_lock_hash(fnic, sc);
1291 spin_lock_irqsave(io_lock, flags);
1292 io_req = (struct fnic_io_req *)CMD_SP(sc);
1293 if (!io_req) {
1294 spin_unlock_irqrestore(io_lock, flags);
1295 goto fnic_abort_cmd_end;
1298 io_req->abts_done = &tm_done;
1300 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1301 spin_unlock_irqrestore(io_lock, flags);
1302 goto wait_pending;
1305 * Command is still pending, need to abort it
1306 * If the firmware completes the command after this point,
1307 * the completion wont be done till mid-layer, since abort
1308 * has already started.
1310 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1311 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1313 spin_unlock_irqrestore(io_lock, flags);
1316 * Check readiness of the remote port. If the path to remote
1317 * port is up, then send abts to the remote port to terminate
1318 * the IO. Else, just locally terminate the IO in the firmware
1320 if (fc_remote_port_chkready(rport) == 0)
1321 task_req = FCPIO_ITMF_ABT_TASK;
1322 else
1323 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1325 /* Now queue the abort command to firmware */
1326 int_to_scsilun(sc->device->lun, &fc_lun);
1328 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1329 fc_lun.scsi_lun, io_req)) {
1330 spin_lock_irqsave(io_lock, flags);
1331 io_req = (struct fnic_io_req *)CMD_SP(sc);
1332 if (io_req)
1333 io_req->abts_done = NULL;
1334 spin_unlock_irqrestore(io_lock, flags);
1335 ret = FAILED;
1336 goto fnic_abort_cmd_end;
1340 * We queued an abort IO, wait for its completion.
1341 * Once the firmware completes the abort command, it will
1342 * wake up this thread.
1344 wait_pending:
1345 wait_for_completion_timeout(&tm_done,
1346 msecs_to_jiffies
1347 (2 * fnic->config.ra_tov +
1348 fnic->config.ed_tov));
1350 /* Check the abort status */
1351 spin_lock_irqsave(io_lock, flags);
1353 io_req = (struct fnic_io_req *)CMD_SP(sc);
1354 if (!io_req) {
1355 spin_unlock_irqrestore(io_lock, flags);
1356 ret = FAILED;
1357 goto fnic_abort_cmd_end;
1359 io_req->abts_done = NULL;
1361 /* fw did not complete abort, timed out */
1362 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1363 spin_unlock_irqrestore(io_lock, flags);
1364 ret = FAILED;
1365 goto fnic_abort_cmd_end;
1369 * firmware completed the abort, check the status,
1370 * free the io_req irrespective of failure or success
1372 if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1373 ret = FAILED;
1375 CMD_SP(sc) = NULL;
1377 spin_unlock_irqrestore(io_lock, flags);
1379 fnic_release_ioreq_buf(fnic, io_req, sc);
1380 mempool_free(io_req, fnic->io_req_pool);
1382 fnic_abort_cmd_end:
1383 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1384 "Returning from abort cmd %s\n",
1385 (ret == SUCCESS) ?
1386 "SUCCESS" : "FAILED");
1387 return ret;
1390 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1391 struct scsi_cmnd *sc,
1392 struct fnic_io_req *io_req)
1394 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1395 struct scsi_lun fc_lun;
1396 int ret = 0;
1397 unsigned long intr_flags;
1399 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1401 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1402 free_wq_copy_descs(fnic, wq);
1404 if (!vnic_wq_copy_desc_avail(wq)) {
1405 ret = -EAGAIN;
1406 goto lr_io_req_end;
1409 /* fill in the lun info */
1410 int_to_scsilun(sc->device->lun, &fc_lun);
1412 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1413 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1414 fc_lun.scsi_lun, io_req->port_id,
1415 fnic->config.ra_tov, fnic->config.ed_tov);
1417 lr_io_req_end:
1418 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
1420 return ret;
1424 * Clean up any pending aborts on the lun
1425 * For each outstanding IO on this lun, whose abort is not completed by fw,
1426 * issue a local abort. Wait for abort to complete. Return 0 if all commands
1427 * successfully aborted, 1 otherwise
1429 static int fnic_clean_pending_aborts(struct fnic *fnic,
1430 struct scsi_cmnd *lr_sc)
1432 int tag;
1433 struct fnic_io_req *io_req;
1434 spinlock_t *io_lock;
1435 unsigned long flags;
1436 int ret = 0;
1437 struct scsi_cmnd *sc;
1438 struct scsi_lun fc_lun;
1439 struct scsi_device *lun_dev = lr_sc->device;
1440 DECLARE_COMPLETION_ONSTACK(tm_done);
1442 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1443 sc = scsi_host_find_tag(fnic->lport->host, tag);
1445 * ignore this lun reset cmd or cmds that do not belong to
1446 * this lun
1448 if (!sc || sc == lr_sc || sc->device != lun_dev)
1449 continue;
1451 io_lock = fnic_io_lock_hash(fnic, sc);
1452 spin_lock_irqsave(io_lock, flags);
1454 io_req = (struct fnic_io_req *)CMD_SP(sc);
1456 if (!io_req || sc->device != lun_dev) {
1457 spin_unlock_irqrestore(io_lock, flags);
1458 continue;
1462 * Found IO that is still pending with firmware and
1463 * belongs to the LUN that we are resetting
1465 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1466 "Found IO in %s on lun\n",
1467 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1469 BUG_ON(CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING);
1471 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1472 io_req->abts_done = &tm_done;
1473 spin_unlock_irqrestore(io_lock, flags);
1475 /* Now queue the abort command to firmware */
1476 int_to_scsilun(sc->device->lun, &fc_lun);
1478 if (fnic_queue_abort_io_req(fnic, tag,
1479 FCPIO_ITMF_ABT_TASK_TERM,
1480 fc_lun.scsi_lun, io_req)) {
1481 spin_lock_irqsave(io_lock, flags);
1482 io_req = (struct fnic_io_req *)CMD_SP(sc);
1483 if (io_req)
1484 io_req->abts_done = NULL;
1485 spin_unlock_irqrestore(io_lock, flags);
1486 ret = 1;
1487 goto clean_pending_aborts_end;
1490 wait_for_completion_timeout(&tm_done,
1491 msecs_to_jiffies
1492 (fnic->config.ed_tov));
1494 /* Recheck cmd state to check if it is now aborted */
1495 spin_lock_irqsave(io_lock, flags);
1496 io_req = (struct fnic_io_req *)CMD_SP(sc);
1497 if (!io_req) {
1498 spin_unlock_irqrestore(io_lock, flags);
1499 ret = 1;
1500 goto clean_pending_aborts_end;
1503 io_req->abts_done = NULL;
1505 /* if abort is still pending with fw, fail */
1506 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1507 spin_unlock_irqrestore(io_lock, flags);
1508 ret = 1;
1509 goto clean_pending_aborts_end;
1511 CMD_SP(sc) = NULL;
1512 spin_unlock_irqrestore(io_lock, flags);
1514 fnic_release_ioreq_buf(fnic, io_req, sc);
1515 mempool_free(io_req, fnic->io_req_pool);
1518 clean_pending_aborts_end:
1519 return ret;
1523 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
1524 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
1525 * on the LUN.
1527 int fnic_device_reset(struct scsi_cmnd *sc)
1529 struct fc_lport *lp;
1530 struct fnic *fnic;
1531 struct fnic_io_req *io_req;
1532 struct fc_rport *rport;
1533 int status;
1534 int ret = FAILED;
1535 spinlock_t *io_lock;
1536 unsigned long flags;
1537 DECLARE_COMPLETION_ONSTACK(tm_done);
1539 /* Wait for rport to unblock */
1540 fnic_block_error_handler(sc);
1542 /* Get local-port, check ready and link up */
1543 lp = shost_priv(sc->device->host);
1545 fnic = lport_priv(lp);
1547 rport = starget_to_rport(scsi_target(sc->device));
1548 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1549 "Device reset called FCID 0x%x, LUN 0x%x\n",
1550 rport->port_id, sc->device->lun);
1552 if (lp->state != LPORT_ST_READY || !(lp->link_up))
1553 goto fnic_device_reset_end;
1555 /* Check if remote port up */
1556 if (fc_remote_port_chkready(rport))
1557 goto fnic_device_reset_end;
1559 io_lock = fnic_io_lock_hash(fnic, sc);
1560 spin_lock_irqsave(io_lock, flags);
1561 io_req = (struct fnic_io_req *)CMD_SP(sc);
1564 * If there is a io_req attached to this command, then use it,
1565 * else allocate a new one.
1567 if (!io_req) {
1568 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
1569 if (!io_req) {
1570 spin_unlock_irqrestore(io_lock, flags);
1571 goto fnic_device_reset_end;
1573 memset(io_req, 0, sizeof(*io_req));
1574 io_req->port_id = rport->port_id;
1575 CMD_SP(sc) = (char *)io_req;
1577 io_req->dr_done = &tm_done;
1578 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
1579 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
1580 spin_unlock_irqrestore(io_lock, flags);
1582 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %d\n",
1583 sc->request->tag);
1586 * issue the device reset, if enqueue failed, clean up the ioreq
1587 * and break assoc with scsi cmd
1589 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
1590 spin_lock_irqsave(io_lock, flags);
1591 io_req = (struct fnic_io_req *)CMD_SP(sc);
1592 if (io_req)
1593 io_req->dr_done = NULL;
1594 goto fnic_device_reset_clean;
1598 * Wait on the local completion for LUN reset. The io_req may be
1599 * freed while we wait since we hold no lock.
1601 wait_for_completion_timeout(&tm_done,
1602 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
1604 spin_lock_irqsave(io_lock, flags);
1605 io_req = (struct fnic_io_req *)CMD_SP(sc);
1606 if (!io_req) {
1607 spin_unlock_irqrestore(io_lock, flags);
1608 goto fnic_device_reset_end;
1610 io_req->dr_done = NULL;
1612 status = CMD_LR_STATUS(sc);
1613 spin_unlock_irqrestore(io_lock, flags);
1616 * If lun reset not completed, bail out with failed. io_req
1617 * gets cleaned up during higher levels of EH
1619 if (status == FCPIO_INVALID_CODE) {
1620 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1621 "Device reset timed out\n");
1622 goto fnic_device_reset_end;
1625 /* Completed, but not successful, clean up the io_req, return fail */
1626 if (status != FCPIO_SUCCESS) {
1627 spin_lock_irqsave(io_lock, flags);
1628 FNIC_SCSI_DBG(KERN_DEBUG,
1629 fnic->lport->host,
1630 "Device reset completed - failed\n");
1631 io_req = (struct fnic_io_req *)CMD_SP(sc);
1632 goto fnic_device_reset_clean;
1636 * Clean up any aborts on this lun that have still not
1637 * completed. If any of these fail, then LUN reset fails.
1638 * clean_pending_aborts cleans all cmds on this lun except
1639 * the lun reset cmd. If all cmds get cleaned, the lun reset
1640 * succeeds
1642 if (fnic_clean_pending_aborts(fnic, sc)) {
1643 spin_lock_irqsave(io_lock, flags);
1644 io_req = (struct fnic_io_req *)CMD_SP(sc);
1645 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1646 "Device reset failed"
1647 " since could not abort all IOs\n");
1648 goto fnic_device_reset_clean;
1651 /* Clean lun reset command */
1652 spin_lock_irqsave(io_lock, flags);
1653 io_req = (struct fnic_io_req *)CMD_SP(sc);
1654 if (io_req)
1655 /* Completed, and successful */
1656 ret = SUCCESS;
1658 fnic_device_reset_clean:
1659 if (io_req)
1660 CMD_SP(sc) = NULL;
1662 spin_unlock_irqrestore(io_lock, flags);
1664 if (io_req) {
1665 fnic_release_ioreq_buf(fnic, io_req, sc);
1666 mempool_free(io_req, fnic->io_req_pool);
1669 fnic_device_reset_end:
1670 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1671 "Returning from device reset %s\n",
1672 (ret == SUCCESS) ?
1673 "SUCCESS" : "FAILED");
1674 return ret;
1677 /* Clean up all IOs, clean up libFC local port */
1678 int fnic_reset(struct Scsi_Host *shost)
1680 struct fc_lport *lp;
1681 struct fnic *fnic;
1682 int ret = SUCCESS;
1684 lp = shost_priv(shost);
1685 fnic = lport_priv(lp);
1687 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1688 "fnic_reset called\n");
1691 * Reset local port, this will clean up libFC exchanges,
1692 * reset remote port sessions, and if link is up, begin flogi
1694 if (lp->tt.lport_reset(lp))
1695 ret = FAILED;
1697 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1698 "Returning from fnic reset %s\n",
1699 (ret == SUCCESS) ?
1700 "SUCCESS" : "FAILED");
1702 return ret;
1706 * SCSI Error handling calls driver's eh_host_reset if all prior
1707 * error handling levels return FAILED. If host reset completes
1708 * successfully, and if link is up, then Fabric login begins.
1710 * Host Reset is the highest level of error recovery. If this fails, then
1711 * host is offlined by SCSI.
1714 int fnic_host_reset(struct scsi_cmnd *sc)
1716 int ret;
1717 unsigned long wait_host_tmo;
1718 struct Scsi_Host *shost = sc->device->host;
1719 struct fc_lport *lp = shost_priv(shost);
1722 * If fnic_reset is successful, wait for fabric login to complete
1723 * scsi-ml tries to send a TUR to every device if host reset is
1724 * successful, so before returning to scsi, fabric should be up
1726 ret = fnic_reset(shost);
1727 if (ret == SUCCESS) {
1728 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
1729 ret = FAILED;
1730 while (time_before(jiffies, wait_host_tmo)) {
1731 if ((lp->state == LPORT_ST_READY) &&
1732 (lp->link_up)) {
1733 ret = SUCCESS;
1734 break;
1736 ssleep(1);
1740 return ret;
1744 * This fxn is called from libFC when host is removed
1746 void fnic_scsi_abort_io(struct fc_lport *lp)
1748 int err = 0;
1749 unsigned long flags;
1750 enum fnic_state old_state;
1751 struct fnic *fnic = lport_priv(lp);
1752 DECLARE_COMPLETION_ONSTACK(remove_wait);
1754 /* Issue firmware reset for fnic, wait for reset to complete */
1755 spin_lock_irqsave(&fnic->fnic_lock, flags);
1756 fnic->remove_wait = &remove_wait;
1757 old_state = fnic->state;
1758 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1759 vnic_dev_del_addr(fnic->vdev, fnic->data_src_addr);
1760 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1762 err = fnic_fw_reset_handler(fnic);
1763 if (err) {
1764 spin_lock_irqsave(&fnic->fnic_lock, flags);
1765 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
1766 fnic->state = old_state;
1767 fnic->remove_wait = NULL;
1768 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1769 return;
1772 /* Wait for firmware reset to complete */
1773 wait_for_completion_timeout(&remove_wait,
1774 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
1776 spin_lock_irqsave(&fnic->fnic_lock, flags);
1777 fnic->remove_wait = NULL;
1778 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1779 "fnic_scsi_abort_io %s\n",
1780 (fnic->state == FNIC_IN_ETH_MODE) ?
1781 "SUCCESS" : "FAILED");
1782 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1787 * This fxn called from libFC to clean up driver IO state on link down
1789 void fnic_scsi_cleanup(struct fc_lport *lp)
1791 unsigned long flags;
1792 enum fnic_state old_state;
1793 struct fnic *fnic = lport_priv(lp);
1795 /* issue fw reset */
1796 spin_lock_irqsave(&fnic->fnic_lock, flags);
1797 old_state = fnic->state;
1798 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1799 vnic_dev_del_addr(fnic->vdev, fnic->data_src_addr);
1800 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1802 if (fnic_fw_reset_handler(fnic)) {
1803 spin_lock_irqsave(&fnic->fnic_lock, flags);
1804 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
1805 fnic->state = old_state;
1806 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1811 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
1815 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
1817 struct fnic *fnic = lport_priv(lp);
1819 /* Non-zero sid, nothing to do */
1820 if (sid)
1821 goto call_fc_exch_mgr_reset;
1823 if (did) {
1824 fnic_rport_exch_reset(fnic, did);
1825 goto call_fc_exch_mgr_reset;
1829 * sid = 0, did = 0
1830 * link down or device being removed
1832 if (!fnic->in_remove)
1833 fnic_scsi_cleanup(lp);
1834 else
1835 fnic_scsi_abort_io(lp);
1837 /* call libFC exch mgr reset to reset its exchanges */
1838 call_fc_exch_mgr_reset:
1839 fc_exch_mgr_reset(lp, sid, did);