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
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 <linux/gfp.h>
30 #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>
42 const 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",
49 static const char *fnic_ioreq_state_str
[] = {
50 [FNIC_IOREQ_CMD_PENDING
] = "FNIC_IOREQ_CMD_PENDING",
51 [FNIC_IOREQ_ABTS_PENDING
] = "FNIC_IOREQ_ABTS_PENDING",
52 [FNIC_IOREQ_ABTS_COMPLETE
] = "FNIC_IOREQ_ABTS_COMPLETE",
53 [FNIC_IOREQ_CMD_COMPLETE
] = "FNIC_IOREQ_CMD_COMPLETE",
56 static const char *fcpio_status_str
[] = {
57 [FCPIO_SUCCESS
] = "FCPIO_SUCCESS", /*0x0*/
58 [FCPIO_INVALID_HEADER
] = "FCPIO_INVALID_HEADER",
59 [FCPIO_OUT_OF_RESOURCE
] = "FCPIO_OUT_OF_RESOURCE",
60 [FCPIO_INVALID_PARAM
] = "FCPIO_INVALID_PARAM]",
61 [FCPIO_REQ_NOT_SUPPORTED
] = "FCPIO_REQ_NOT_SUPPORTED",
62 [FCPIO_IO_NOT_FOUND
] = "FCPIO_IO_NOT_FOUND",
63 [FCPIO_ABORTED
] = "FCPIO_ABORTED", /*0x41*/
64 [FCPIO_TIMEOUT
] = "FCPIO_TIMEOUT",
65 [FCPIO_SGL_INVALID
] = "FCPIO_SGL_INVALID",
66 [FCPIO_MSS_INVALID
] = "FCPIO_MSS_INVALID",
67 [FCPIO_DATA_CNT_MISMATCH
] = "FCPIO_DATA_CNT_MISMATCH",
68 [FCPIO_FW_ERR
] = "FCPIO_FW_ERR",
69 [FCPIO_ITMF_REJECTED
] = "FCPIO_ITMF_REJECTED",
70 [FCPIO_ITMF_FAILED
] = "FCPIO_ITMF_FAILED",
71 [FCPIO_ITMF_INCORRECT_LUN
] = "FCPIO_ITMF_INCORRECT_LUN",
72 [FCPIO_CMND_REJECTED
] = "FCPIO_CMND_REJECTED",
73 [FCPIO_NO_PATH_AVAIL
] = "FCPIO_NO_PATH_AVAIL",
74 [FCPIO_PATH_FAILED
] = "FCPIO_PATH_FAILED",
75 [FCPIO_LUNMAP_CHNG_PEND
] = "FCPIO_LUNHMAP_CHNG_PEND",
78 const char *fnic_state_to_str(unsigned int state
)
80 if (state
>= ARRAY_SIZE(fnic_state_str
) || !fnic_state_str
[state
])
83 return fnic_state_str
[state
];
86 static const char *fnic_ioreq_state_to_str(unsigned int state
)
88 if (state
>= ARRAY_SIZE(fnic_ioreq_state_str
) ||
89 !fnic_ioreq_state_str
[state
])
92 return fnic_ioreq_state_str
[state
];
95 static const char *fnic_fcpio_status_to_str(unsigned int status
)
97 if (status
>= ARRAY_SIZE(fcpio_status_str
) || !fcpio_status_str
[status
])
100 return fcpio_status_str
[status
];
103 static void fnic_cleanup_io(struct fnic
*fnic
, int exclude_id
);
105 static inline spinlock_t
*fnic_io_lock_hash(struct fnic
*fnic
,
106 struct scsi_cmnd
*sc
)
108 u32 hash
= sc
->request
->tag
& (FNIC_IO_LOCKS
- 1);
110 return &fnic
->io_req_lock
[hash
];
114 * Unmap the data buffer and sense buffer for an io_req,
115 * also unmap and free the device-private scatter/gather list.
117 static void fnic_release_ioreq_buf(struct fnic
*fnic
,
118 struct fnic_io_req
*io_req
,
119 struct scsi_cmnd
*sc
)
121 if (io_req
->sgl_list_pa
)
122 pci_unmap_single(fnic
->pdev
, io_req
->sgl_list_pa
,
123 sizeof(io_req
->sgl_list
[0]) * io_req
->sgl_cnt
,
128 mempool_free(io_req
->sgl_list_alloc
,
129 fnic
->io_sgl_pool
[io_req
->sgl_type
]);
130 if (io_req
->sense_buf_pa
)
131 pci_unmap_single(fnic
->pdev
, io_req
->sense_buf_pa
,
132 SCSI_SENSE_BUFFERSIZE
, PCI_DMA_FROMDEVICE
);
135 /* Free up Copy Wq descriptors. Called with copy_wq lock held */
136 static int free_wq_copy_descs(struct fnic
*fnic
, struct vnic_wq_copy
*wq
)
138 /* if no Ack received from firmware, then nothing to clean */
139 if (!fnic
->fw_ack_recd
[0])
143 * Update desc_available count based on number of freed descriptors
144 * Account for wraparound
146 if (wq
->to_clean_index
<= fnic
->fw_ack_index
[0])
147 wq
->ring
.desc_avail
+= (fnic
->fw_ack_index
[0]
148 - wq
->to_clean_index
+ 1);
150 wq
->ring
.desc_avail
+= (wq
->ring
.desc_count
152 + fnic
->fw_ack_index
[0] + 1);
155 * just bump clean index to ack_index+1 accounting for wraparound
156 * this will essentially free up all descriptors between
157 * to_clean_index and fw_ack_index, both inclusive
160 (fnic
->fw_ack_index
[0] + 1) % wq
->ring
.desc_count
;
162 /* we have processed the acks received so far */
163 fnic
->fw_ack_recd
[0] = 0;
169 * fnic_fw_reset_handler
170 * Routine to send reset msg to fw
172 int fnic_fw_reset_handler(struct fnic
*fnic
)
174 struct vnic_wq_copy
*wq
= &fnic
->wq_copy
[0];
178 skb_queue_purge(&fnic
->frame_queue
);
179 skb_queue_purge(&fnic
->tx_queue
);
181 spin_lock_irqsave(&fnic
->wq_copy_lock
[0], flags
);
183 if (vnic_wq_copy_desc_avail(wq
) <= fnic
->wq_copy_desc_low
[0])
184 free_wq_copy_descs(fnic
, wq
);
186 if (!vnic_wq_copy_desc_avail(wq
))
189 fnic_queue_wq_copy_desc_fw_reset(wq
, SCSI_NO_TAG
);
191 spin_unlock_irqrestore(&fnic
->wq_copy_lock
[0], flags
);
194 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
195 "Issued fw reset\n");
197 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
198 "Failed to issue fw reset\n");
204 * fnic_flogi_reg_handler
205 * Routine to send flogi register msg to fw
207 int fnic_flogi_reg_handler(struct fnic
*fnic
, u32 fc_id
)
209 struct vnic_wq_copy
*wq
= &fnic
->wq_copy
[0];
210 enum fcpio_flogi_reg_format_type format
;
211 struct fc_lport
*lp
= fnic
->lport
;
216 spin_lock_irqsave(&fnic
->wq_copy_lock
[0], flags
);
218 if (vnic_wq_copy_desc_avail(wq
) <= fnic
->wq_copy_desc_low
[0])
219 free_wq_copy_descs(fnic
, wq
);
221 if (!vnic_wq_copy_desc_avail(wq
)) {
223 goto flogi_reg_ioreq_end
;
226 if (fnic
->ctlr
.map_dest
) {
227 memset(gw_mac
, 0xff, ETH_ALEN
);
228 format
= FCPIO_FLOGI_REG_DEF_DEST
;
230 memcpy(gw_mac
, fnic
->ctlr
.dest_addr
, ETH_ALEN
);
231 format
= FCPIO_FLOGI_REG_GW_DEST
;
234 if ((fnic
->config
.flags
& VFCF_FIP_CAPABLE
) && !fnic
->ctlr
.map_dest
) {
235 fnic_queue_wq_copy_desc_fip_reg(wq
, SCSI_NO_TAG
,
238 lp
->r_a_tov
, lp
->e_d_tov
);
239 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
240 "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
241 fc_id
, fnic
->data_src_addr
, gw_mac
);
243 fnic_queue_wq_copy_desc_flogi_reg(wq
, SCSI_NO_TAG
,
244 format
, fc_id
, gw_mac
);
245 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
246 "FLOGI reg issued fcid %x map %d dest %pM\n",
247 fc_id
, fnic
->ctlr
.map_dest
, gw_mac
);
251 spin_unlock_irqrestore(&fnic
->wq_copy_lock
[0], flags
);
256 * fnic_queue_wq_copy_desc
257 * Routine to enqueue a wq copy desc
259 static inline int fnic_queue_wq_copy_desc(struct fnic
*fnic
,
260 struct vnic_wq_copy
*wq
,
261 struct fnic_io_req
*io_req
,
262 struct scsi_cmnd
*sc
,
265 struct scatterlist
*sg
;
266 struct fc_rport
*rport
= starget_to_rport(scsi_target(sc
->device
));
267 struct fc_rport_libfc_priv
*rp
= rport
->dd_data
;
268 struct host_sg_desc
*desc
;
271 unsigned long intr_flags
;
274 struct scsi_lun fc_lun
;
278 /* For each SGE, create a device desc entry */
279 desc
= io_req
->sgl_list
;
280 for_each_sg(scsi_sglist(sc
), sg
, sg_count
, i
) {
281 desc
->addr
= cpu_to_le64(sg_dma_address(sg
));
282 desc
->len
= cpu_to_le32(sg_dma_len(sg
));
287 io_req
->sgl_list_pa
= pci_map_single
290 sizeof(io_req
->sgl_list
[0]) * sg_count
,
294 io_req
->sense_buf_pa
= pci_map_single(fnic
->pdev
,
296 SCSI_SENSE_BUFFERSIZE
,
299 int_to_scsilun(sc
->device
->lun
, &fc_lun
);
301 pri_tag
= FCPIO_ICMND_PTA_SIMPLE
;
302 msg
[0] = MSG_SIMPLE_TAG
;
303 scsi_populate_tag_msg(sc
, msg
);
304 if (msg
[0] == MSG_ORDERED_TAG
)
305 pri_tag
= FCPIO_ICMND_PTA_ORDERED
;
307 /* Enqueue the descriptor in the Copy WQ */
308 spin_lock_irqsave(&fnic
->wq_copy_lock
[0], intr_flags
);
310 if (vnic_wq_copy_desc_avail(wq
) <= fnic
->wq_copy_desc_low
[0])
311 free_wq_copy_descs(fnic
, wq
);
313 if (unlikely(!vnic_wq_copy_desc_avail(wq
))) {
314 spin_unlock_irqrestore(&fnic
->wq_copy_lock
[0], intr_flags
);
315 return SCSI_MLQUEUE_HOST_BUSY
;
319 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
320 flags
= FCPIO_ICMND_RDDATA
;
321 else if (sc
->sc_data_direction
== DMA_TO_DEVICE
)
322 flags
= FCPIO_ICMND_WRDATA
;
325 if ((fnic
->config
.flags
& VFCF_FCP_SEQ_LVL_ERR
) &&
326 (rp
->flags
& FC_RP_FLAGS_RETRY
))
327 exch_flags
|= FCPIO_ICMND_SRFLAG_RETRY
;
329 fnic_queue_wq_copy_desc_icmnd_16(wq
, sc
->request
->tag
,
330 0, exch_flags
, io_req
->sgl_cnt
,
331 SCSI_SENSE_BUFFERSIZE
,
333 io_req
->sense_buf_pa
,
334 0, /* scsi cmd ref, always 0 */
335 pri_tag
, /* scsi pri and tag */
336 flags
, /* command flags */
337 sc
->cmnd
, sc
->cmd_len
,
339 fc_lun
.scsi_lun
, io_req
->port_id
,
340 rport
->maxframe_size
, rp
->r_a_tov
,
343 spin_unlock_irqrestore(&fnic
->wq_copy_lock
[0], intr_flags
);
349 * Routine to send a scsi cdb
350 * Called with host_lock held and interrupts disabled.
352 int fnic_queuecommand(struct scsi_cmnd
*sc
, void (*done
)(struct scsi_cmnd
*))
355 struct fc_rport
*rport
;
356 struct fnic_io_req
*io_req
;
358 struct vnic_wq_copy
*wq
;
364 rport
= starget_to_rport(scsi_target(sc
->device
));
365 ret
= fc_remote_port_chkready(rport
);
372 lp
= shost_priv(sc
->device
->host
);
373 if (lp
->state
!= LPORT_ST_READY
|| !(lp
->link_up
))
374 return SCSI_MLQUEUE_HOST_BUSY
;
377 * Release host lock, use driver resource specific locks from here.
378 * Don't re-enable interrupts in case they were disabled prior to the
379 * caller disabling them.
381 spin_unlock(lp
->host
->host_lock
);
383 /* Get a new io_req for this SCSI IO */
384 fnic
= lport_priv(lp
);
386 io_req
= mempool_alloc(fnic
->io_req_pool
, GFP_ATOMIC
);
388 ret
= SCSI_MLQUEUE_HOST_BUSY
;
391 memset(io_req
, 0, sizeof(*io_req
));
393 /* Map the data buffer */
394 sg_count
= scsi_dma_map(sc
);
396 mempool_free(io_req
, fnic
->io_req_pool
);
400 /* Determine the type of scatter/gather list we need */
401 io_req
->sgl_cnt
= sg_count
;
402 io_req
->sgl_type
= FNIC_SGL_CACHE_DFLT
;
403 if (sg_count
> FNIC_DFLT_SG_DESC_CNT
)
404 io_req
->sgl_type
= FNIC_SGL_CACHE_MAX
;
408 mempool_alloc(fnic
->io_sgl_pool
[io_req
->sgl_type
],
409 GFP_ATOMIC
| GFP_DMA
);
410 if (!io_req
->sgl_list
) {
411 ret
= SCSI_MLQUEUE_HOST_BUSY
;
413 mempool_free(io_req
, fnic
->io_req_pool
);
417 /* Cache sgl list allocated address before alignment */
418 io_req
->sgl_list_alloc
= io_req
->sgl_list
;
419 ptr
= (unsigned long) io_req
->sgl_list
;
420 if (ptr
% FNIC_SG_DESC_ALIGN
) {
421 io_req
->sgl_list
= (struct host_sg_desc
*)
422 (((unsigned long) ptr
423 + FNIC_SG_DESC_ALIGN
- 1)
424 & ~(FNIC_SG_DESC_ALIGN
- 1));
428 /* initialize rest of io_req */
429 io_req
->port_id
= rport
->port_id
;
430 CMD_STATE(sc
) = FNIC_IOREQ_CMD_PENDING
;
431 CMD_SP(sc
) = (char *)io_req
;
432 sc
->scsi_done
= done
;
434 /* create copy wq desc and enqueue it */
435 wq
= &fnic
->wq_copy
[0];
436 ret
= fnic_queue_wq_copy_desc(fnic
, wq
, io_req
, sc
, sg_count
);
439 * In case another thread cancelled the request,
440 * refetch the pointer under the lock.
442 spinlock_t
*io_lock
= fnic_io_lock_hash(fnic
, sc
);
444 spin_lock_irqsave(io_lock
, flags
);
445 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
447 CMD_STATE(sc
) = FNIC_IOREQ_CMD_COMPLETE
;
448 spin_unlock_irqrestore(io_lock
, flags
);
450 fnic_release_ioreq_buf(fnic
, io_req
, sc
);
451 mempool_free(io_req
, fnic
->io_req_pool
);
455 /* acquire host lock before returning to SCSI */
456 spin_lock(lp
->host
->host_lock
);
461 * fnic_fcpio_fw_reset_cmpl_handler
462 * Routine to handle fw reset completion
464 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic
*fnic
,
465 struct fcpio_fw_req
*desc
)
469 struct fcpio_tag tag
;
473 fcpio_header_dec(&desc
->hdr
, &type
, &hdr_status
, &tag
);
475 /* Clean up all outstanding io requests */
476 fnic_cleanup_io(fnic
, SCSI_NO_TAG
);
478 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
480 /* fnic should be in FC_TRANS_ETH_MODE */
481 if (fnic
->state
== FNIC_IN_FC_TRANS_ETH_MODE
) {
482 /* Check status of reset completion */
484 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
485 "reset cmpl success\n");
486 /* Ready to send flogi out */
487 fnic
->state
= FNIC_IN_ETH_MODE
;
489 FNIC_SCSI_DBG(KERN_DEBUG
,
491 "fnic fw_reset : failed %s\n",
492 fnic_fcpio_status_to_str(hdr_status
));
495 * Unable to change to eth mode, cannot send out flogi
496 * Change state to fc mode, so that subsequent Flogi
497 * requests from libFC will cause more attempts to
498 * reset the firmware. Free the cached flogi
500 fnic
->state
= FNIC_IN_FC_MODE
;
504 FNIC_SCSI_DBG(KERN_DEBUG
,
506 "Unexpected state %s while processing"
507 " reset cmpl\n", fnic_state_to_str(fnic
->state
));
511 /* Thread removing device blocks till firmware reset is complete */
512 if (fnic
->remove_wait
)
513 complete(fnic
->remove_wait
);
516 * If fnic is being removed, or fw reset failed
517 * free the flogi frame. Else, send it out
519 if (fnic
->remove_wait
|| ret
) {
520 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
521 skb_queue_purge(&fnic
->tx_queue
);
522 goto reset_cmpl_handler_end
;
525 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
529 reset_cmpl_handler_end
:
534 * fnic_fcpio_flogi_reg_cmpl_handler
535 * Routine to handle flogi register completion
537 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic
*fnic
,
538 struct fcpio_fw_req
*desc
)
542 struct fcpio_tag tag
;
546 fcpio_header_dec(&desc
->hdr
, &type
, &hdr_status
, &tag
);
548 /* Update fnic state based on status of flogi reg completion */
549 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
551 if (fnic
->state
== FNIC_IN_ETH_TRANS_FC_MODE
) {
553 /* Check flogi registration completion status */
555 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
556 "flog reg succeeded\n");
557 fnic
->state
= FNIC_IN_FC_MODE
;
559 FNIC_SCSI_DBG(KERN_DEBUG
,
561 "fnic flogi reg :failed %s\n",
562 fnic_fcpio_status_to_str(hdr_status
));
563 fnic
->state
= FNIC_IN_ETH_MODE
;
567 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
568 "Unexpected fnic state %s while"
569 " processing flogi reg completion\n",
570 fnic_state_to_str(fnic
->state
));
575 if (fnic
->stop_rx_link_events
) {
576 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
577 goto reg_cmpl_handler_end
;
579 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
582 queue_work(fnic_event_queue
, &fnic
->frame_work
);
584 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
587 reg_cmpl_handler_end
:
591 static inline int is_ack_index_in_range(struct vnic_wq_copy
*wq
,
594 if (wq
->to_clean_index
<= wq
->to_use_index
) {
595 /* out of range, stale request_out index */
596 if (request_out
< wq
->to_clean_index
||
597 request_out
>= wq
->to_use_index
)
600 /* out of range, stale request_out index */
601 if (request_out
< wq
->to_clean_index
&&
602 request_out
>= wq
->to_use_index
)
605 /* request_out index is in range */
611 * Mark that ack received and store the Ack index. If there are multiple
612 * acks received before Tx thread cleans it up, the latest value will be
613 * used which is correct behavior. This state should be in the copy Wq
614 * instead of in the fnic
616 static inline void fnic_fcpio_ack_handler(struct fnic
*fnic
,
617 unsigned int cq_index
,
618 struct fcpio_fw_req
*desc
)
620 struct vnic_wq_copy
*wq
;
621 u16 request_out
= desc
->u
.ack
.request_out
;
624 /* mark the ack state */
625 wq
= &fnic
->wq_copy
[cq_index
- fnic
->raw_wq_count
- fnic
->rq_count
];
626 spin_lock_irqsave(&fnic
->wq_copy_lock
[0], flags
);
628 if (is_ack_index_in_range(wq
, request_out
)) {
629 fnic
->fw_ack_index
[0] = request_out
;
630 fnic
->fw_ack_recd
[0] = 1;
632 spin_unlock_irqrestore(&fnic
->wq_copy_lock
[0], flags
);
636 * fnic_fcpio_icmnd_cmpl_handler
637 * Routine to handle icmnd completions
639 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic
*fnic
,
640 struct fcpio_fw_req
*desc
)
644 struct fcpio_tag tag
;
647 struct fcpio_icmnd_cmpl
*icmnd_cmpl
;
648 struct fnic_io_req
*io_req
;
649 struct scsi_cmnd
*sc
;
653 /* Decode the cmpl description to get the io_req id */
654 fcpio_header_dec(&desc
->hdr
, &type
, &hdr_status
, &tag
);
655 fcpio_tag_id_dec(&tag
, &id
);
657 if (id
>= FNIC_MAX_IO_REQ
)
660 sc
= scsi_host_find_tag(fnic
->lport
->host
, id
);
665 io_lock
= fnic_io_lock_hash(fnic
, sc
);
666 spin_lock_irqsave(io_lock
, flags
);
667 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
668 WARN_ON_ONCE(!io_req
);
670 spin_unlock_irqrestore(io_lock
, flags
);
674 /* firmware completed the io */
675 io_req
->io_completed
= 1;
678 * if SCSI-ML has already issued abort on this command,
679 * ignore completion of the IO. The abts path will clean it up
681 if (CMD_STATE(sc
) == FNIC_IOREQ_ABTS_PENDING
) {
682 spin_unlock_irqrestore(io_lock
, flags
);
686 /* Mark the IO as complete */
687 CMD_STATE(sc
) = FNIC_IOREQ_CMD_COMPLETE
;
689 icmnd_cmpl
= &desc
->u
.icmnd_cmpl
;
691 switch (hdr_status
) {
693 sc
->result
= (DID_OK
<< 16) | icmnd_cmpl
->scsi_status
;
694 xfer_len
= scsi_bufflen(sc
);
695 scsi_set_resid(sc
, icmnd_cmpl
->residual
);
697 if (icmnd_cmpl
->flags
& FCPIO_ICMND_CMPL_RESID_UNDER
)
698 xfer_len
-= icmnd_cmpl
->residual
;
701 * If queue_full, then try to reduce queue depth for all
702 * LUNS on the target. Todo: this should be accompanied
703 * by a periodic queue_depth rampup based on successful
706 if (icmnd_cmpl
->scsi_status
== QUEUE_FULL
) {
707 struct scsi_device
*t_sdev
;
710 shost_for_each_device(t_sdev
, sc
->device
->host
) {
711 if (t_sdev
->id
!= sc
->device
->id
)
714 if (t_sdev
->queue_depth
> 1) {
715 qd
= scsi_track_queue_full
717 t_sdev
->queue_depth
- 1);
719 qd
= t_sdev
->host
->cmd_per_lun
;
720 shost_printk(KERN_INFO
,
723 "] queue full detected,"
725 t_sdev
->host
->host_no
,
727 t_sdev
->id
, t_sdev
->lun
,
728 t_sdev
->queue_depth
);
734 case FCPIO_TIMEOUT
: /* request was timed out */
735 sc
->result
= (DID_TIME_OUT
<< 16) | icmnd_cmpl
->scsi_status
;
738 case FCPIO_ABORTED
: /* request was aborted */
739 sc
->result
= (DID_ERROR
<< 16) | icmnd_cmpl
->scsi_status
;
742 case FCPIO_DATA_CNT_MISMATCH
: /* recv/sent more/less data than exp. */
743 scsi_set_resid(sc
, icmnd_cmpl
->residual
);
744 sc
->result
= (DID_ERROR
<< 16) | icmnd_cmpl
->scsi_status
;
747 case FCPIO_OUT_OF_RESOURCE
: /* out of resources to complete request */
748 sc
->result
= (DID_REQUEUE
<< 16) | icmnd_cmpl
->scsi_status
;
750 case FCPIO_INVALID_HEADER
: /* header contains invalid data */
751 case FCPIO_INVALID_PARAM
: /* some parameter in request invalid */
752 case FCPIO_REQ_NOT_SUPPORTED
:/* request type is not supported */
753 case FCPIO_IO_NOT_FOUND
: /* requested I/O was not found */
754 case FCPIO_SGL_INVALID
: /* request was aborted due to sgl error */
755 case FCPIO_MSS_INVALID
: /* request was aborted due to mss error */
756 case FCPIO_FW_ERR
: /* request was terminated due fw error */
758 shost_printk(KERN_ERR
, fnic
->lport
->host
, "hdr status = %s\n",
759 fnic_fcpio_status_to_str(hdr_status
));
760 sc
->result
= (DID_ERROR
<< 16) | icmnd_cmpl
->scsi_status
;
764 /* Break link with the SCSI command */
767 spin_unlock_irqrestore(io_lock
, flags
);
769 fnic_release_ioreq_buf(fnic
, io_req
, sc
);
771 mempool_free(io_req
, fnic
->io_req_pool
);
773 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
) {
774 fnic
->lport
->host_stats
.fcp_input_requests
++;
775 fnic
->fcp_input_bytes
+= xfer_len
;
776 } else if (sc
->sc_data_direction
== DMA_TO_DEVICE
) {
777 fnic
->lport
->host_stats
.fcp_output_requests
++;
778 fnic
->fcp_output_bytes
+= xfer_len
;
780 fnic
->lport
->host_stats
.fcp_control_requests
++;
782 /* Call SCSI completion function to complete the IO */
788 /* fnic_fcpio_itmf_cmpl_handler
789 * Routine to handle itmf completions
791 static void fnic_fcpio_itmf_cmpl_handler(struct fnic
*fnic
,
792 struct fcpio_fw_req
*desc
)
796 struct fcpio_tag tag
;
798 struct scsi_cmnd
*sc
;
799 struct fnic_io_req
*io_req
;
803 fcpio_header_dec(&desc
->hdr
, &type
, &hdr_status
, &tag
);
804 fcpio_tag_id_dec(&tag
, &id
);
806 if ((id
& FNIC_TAG_MASK
) >= FNIC_MAX_IO_REQ
)
809 sc
= scsi_host_find_tag(fnic
->lport
->host
, id
& FNIC_TAG_MASK
);
814 io_lock
= fnic_io_lock_hash(fnic
, sc
);
815 spin_lock_irqsave(io_lock
, flags
);
816 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
817 WARN_ON_ONCE(!io_req
);
819 spin_unlock_irqrestore(io_lock
, flags
);
823 if (id
& FNIC_TAG_ABORT
) {
824 /* Completion of abort cmd */
825 if (CMD_STATE(sc
) != FNIC_IOREQ_ABTS_PENDING
) {
826 /* This is a late completion. Ignore it */
827 spin_unlock_irqrestore(io_lock
, flags
);
830 CMD_STATE(sc
) = FNIC_IOREQ_ABTS_COMPLETE
;
831 CMD_ABTS_STATUS(sc
) = hdr_status
;
833 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
834 "abts cmpl recd. id %d status %s\n",
835 (int)(id
& FNIC_TAG_MASK
),
836 fnic_fcpio_status_to_str(hdr_status
));
839 * If scsi_eh thread is blocked waiting for abts to complete,
840 * signal completion to it. IO will be cleaned in the thread
841 * else clean it in this context
843 if (io_req
->abts_done
) {
844 complete(io_req
->abts_done
);
845 spin_unlock_irqrestore(io_lock
, flags
);
847 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
848 "abts cmpl, completing IO\n");
850 sc
->result
= (DID_ERROR
<< 16);
852 spin_unlock_irqrestore(io_lock
, flags
);
854 fnic_release_ioreq_buf(fnic
, io_req
, sc
);
855 mempool_free(io_req
, fnic
->io_req_pool
);
860 } else if (id
& FNIC_TAG_DEV_RST
) {
861 /* Completion of device reset */
862 CMD_LR_STATUS(sc
) = hdr_status
;
863 CMD_STATE(sc
) = FNIC_IOREQ_CMD_COMPLETE
;
864 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
865 "dev reset cmpl recd. id %d status %s\n",
866 (int)(id
& FNIC_TAG_MASK
),
867 fnic_fcpio_status_to_str(hdr_status
));
869 complete(io_req
->dr_done
);
870 spin_unlock_irqrestore(io_lock
, flags
);
873 shost_printk(KERN_ERR
, fnic
->lport
->host
,
874 "Unexpected itmf io state %s tag %x\n",
875 fnic_ioreq_state_to_str(CMD_STATE(sc
)), id
);
876 spin_unlock_irqrestore(io_lock
, flags
);
882 * fnic_fcpio_cmpl_handler
883 * Routine to service the cq for wq_copy
885 static int fnic_fcpio_cmpl_handler(struct vnic_dev
*vdev
,
886 unsigned int cq_index
,
887 struct fcpio_fw_req
*desc
)
889 struct fnic
*fnic
= vnic_dev_priv(vdev
);
892 switch (desc
->hdr
.type
) {
893 case FCPIO_ACK
: /* fw copied copy wq desc to its queue */
894 fnic_fcpio_ack_handler(fnic
, cq_index
, desc
);
897 case FCPIO_ICMND_CMPL
: /* fw completed a command */
898 fnic_fcpio_icmnd_cmpl_handler(fnic
, desc
);
901 case FCPIO_ITMF_CMPL
: /* fw completed itmf (abort cmd, lun reset)*/
902 fnic_fcpio_itmf_cmpl_handler(fnic
, desc
);
905 case FCPIO_FLOGI_REG_CMPL
: /* fw completed flogi_reg */
906 case FCPIO_FLOGI_FIP_REG_CMPL
: /* fw completed flogi_fip_reg */
907 ret
= fnic_fcpio_flogi_reg_cmpl_handler(fnic
, desc
);
910 case FCPIO_RESET_CMPL
: /* fw completed reset */
911 ret
= fnic_fcpio_fw_reset_cmpl_handler(fnic
, desc
);
915 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
916 "firmware completion type %d\n",
925 * fnic_wq_copy_cmpl_handler
926 * Routine to process wq copy
928 int fnic_wq_copy_cmpl_handler(struct fnic
*fnic
, int copy_work_to_do
)
930 unsigned int wq_work_done
= 0;
931 unsigned int i
, cq_index
;
932 unsigned int cur_work_done
;
934 for (i
= 0; i
< fnic
->wq_copy_count
; i
++) {
935 cq_index
= i
+ fnic
->raw_wq_count
+ fnic
->rq_count
;
936 cur_work_done
= vnic_cq_copy_service(&fnic
->cq
[cq_index
],
937 fnic_fcpio_cmpl_handler
,
939 wq_work_done
+= cur_work_done
;
944 static void fnic_cleanup_io(struct fnic
*fnic
, int exclude_id
)
947 struct fnic_io_req
*io_req
;
948 unsigned long flags
= 0;
949 struct scsi_cmnd
*sc
;
952 for (i
= 0; i
< FNIC_MAX_IO_REQ
; i
++) {
956 sc
= scsi_host_find_tag(fnic
->lport
->host
, i
);
960 io_lock
= fnic_io_lock_hash(fnic
, sc
);
961 spin_lock_irqsave(io_lock
, flags
);
962 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
964 spin_unlock_irqrestore(io_lock
, flags
);
965 goto cleanup_scsi_cmd
;
970 spin_unlock_irqrestore(io_lock
, flags
);
973 * If there is a scsi_cmnd associated with this io_req, then
974 * free the corresponding state
976 fnic_release_ioreq_buf(fnic
, io_req
, sc
);
977 mempool_free(io_req
, fnic
->io_req_pool
);
980 sc
->result
= DID_TRANSPORT_DISRUPTED
<< 16;
981 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
, "fnic_cleanup_io:"
982 " DID_TRANSPORT_DISRUPTED\n");
984 /* Complete the command to SCSI */
990 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy
*wq
,
991 struct fcpio_host_req
*desc
)
994 struct fnic
*fnic
= vnic_dev_priv(wq
->vdev
);
995 struct fnic_io_req
*io_req
;
996 struct scsi_cmnd
*sc
;
1000 /* get the tag reference */
1001 fcpio_tag_id_dec(&desc
->hdr
.tag
, &id
);
1002 id
&= FNIC_TAG_MASK
;
1004 if (id
>= FNIC_MAX_IO_REQ
)
1007 sc
= scsi_host_find_tag(fnic
->lport
->host
, id
);
1011 io_lock
= fnic_io_lock_hash(fnic
, sc
);
1012 spin_lock_irqsave(io_lock
, flags
);
1014 /* Get the IO context which this desc refers to */
1015 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1017 /* fnic interrupts are turned off by now */
1020 spin_unlock_irqrestore(io_lock
, flags
);
1021 goto wq_copy_cleanup_scsi_cmd
;
1026 spin_unlock_irqrestore(io_lock
, flags
);
1028 fnic_release_ioreq_buf(fnic
, io_req
, sc
);
1029 mempool_free(io_req
, fnic
->io_req_pool
);
1031 wq_copy_cleanup_scsi_cmd
:
1032 sc
->result
= DID_NO_CONNECT
<< 16;
1033 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
, "wq_copy_cleanup_handler:"
1034 " DID_NO_CONNECT\n");
1040 static inline int fnic_queue_abort_io_req(struct fnic
*fnic
, int tag
,
1041 u32 task_req
, u8
*fc_lun
,
1042 struct fnic_io_req
*io_req
)
1044 struct vnic_wq_copy
*wq
= &fnic
->wq_copy
[0];
1045 unsigned long flags
;
1047 spin_lock_irqsave(&fnic
->wq_copy_lock
[0], flags
);
1049 if (vnic_wq_copy_desc_avail(wq
) <= fnic
->wq_copy_desc_low
[0])
1050 free_wq_copy_descs(fnic
, wq
);
1052 if (!vnic_wq_copy_desc_avail(wq
)) {
1053 spin_unlock_irqrestore(&fnic
->wq_copy_lock
[0], flags
);
1056 fnic_queue_wq_copy_desc_itmf(wq
, tag
| FNIC_TAG_ABORT
,
1057 0, task_req
, tag
, fc_lun
, io_req
->port_id
,
1058 fnic
->config
.ra_tov
, fnic
->config
.ed_tov
);
1060 spin_unlock_irqrestore(&fnic
->wq_copy_lock
[0], flags
);
1064 void fnic_rport_exch_reset(struct fnic
*fnic
, u32 port_id
)
1067 struct fnic_io_req
*io_req
;
1068 spinlock_t
*io_lock
;
1069 unsigned long flags
;
1070 struct scsi_cmnd
*sc
;
1071 struct scsi_lun fc_lun
;
1072 enum fnic_ioreq_state old_ioreq_state
;
1074 FNIC_SCSI_DBG(KERN_DEBUG
,
1076 "fnic_rport_reset_exch called portid 0x%06x\n",
1079 if (fnic
->in_remove
)
1082 for (tag
= 0; tag
< FNIC_MAX_IO_REQ
; tag
++) {
1083 sc
= scsi_host_find_tag(fnic
->lport
->host
, tag
);
1087 io_lock
= fnic_io_lock_hash(fnic
, sc
);
1088 spin_lock_irqsave(io_lock
, flags
);
1090 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1092 if (!io_req
|| io_req
->port_id
!= port_id
) {
1093 spin_unlock_irqrestore(io_lock
, flags
);
1098 * Found IO that is still pending with firmware and
1099 * belongs to rport that went away
1101 if (CMD_STATE(sc
) == FNIC_IOREQ_ABTS_PENDING
) {
1102 spin_unlock_irqrestore(io_lock
, flags
);
1105 old_ioreq_state
= CMD_STATE(sc
);
1106 CMD_STATE(sc
) = FNIC_IOREQ_ABTS_PENDING
;
1107 CMD_ABTS_STATUS(sc
) = FCPIO_INVALID_CODE
;
1109 BUG_ON(io_req
->abts_done
);
1111 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
1112 "fnic_rport_reset_exch: Issuing abts\n");
1114 spin_unlock_irqrestore(io_lock
, flags
);
1116 /* Now queue the abort command to firmware */
1117 int_to_scsilun(sc
->device
->lun
, &fc_lun
);
1119 if (fnic_queue_abort_io_req(fnic
, tag
,
1120 FCPIO_ITMF_ABT_TASK_TERM
,
1121 fc_lun
.scsi_lun
, io_req
)) {
1123 * Revert the cmd state back to old state, if
1124 * it hasnt changed in between. This cmd will get
1125 * aborted later by scsi_eh, or cleaned up during
1128 io_lock
= fnic_io_lock_hash(fnic
, sc
);
1130 spin_lock_irqsave(io_lock
, flags
);
1131 if (CMD_STATE(sc
) == FNIC_IOREQ_ABTS_PENDING
)
1132 CMD_STATE(sc
) = old_ioreq_state
;
1133 spin_unlock_irqrestore(io_lock
, flags
);
1139 void fnic_terminate_rport_io(struct fc_rport
*rport
)
1142 struct fnic_io_req
*io_req
;
1143 spinlock_t
*io_lock
;
1144 unsigned long flags
;
1145 struct scsi_cmnd
*sc
;
1146 struct scsi_lun fc_lun
;
1147 struct fc_rport_libfc_priv
*rdata
= rport
->dd_data
;
1148 struct fc_lport
*lport
= rdata
->local_port
;
1149 struct fnic
*fnic
= lport_priv(lport
);
1150 struct fc_rport
*cmd_rport
;
1151 enum fnic_ioreq_state old_ioreq_state
;
1153 FNIC_SCSI_DBG(KERN_DEBUG
,
1154 fnic
->lport
->host
, "fnic_terminate_rport_io called"
1155 " wwpn 0x%llx, wwnn0x%llx, portid 0x%06x\n",
1156 rport
->port_name
, rport
->node_name
,
1159 if (fnic
->in_remove
)
1162 for (tag
= 0; tag
< FNIC_MAX_IO_REQ
; tag
++) {
1163 sc
= scsi_host_find_tag(fnic
->lport
->host
, tag
);
1167 cmd_rport
= starget_to_rport(scsi_target(sc
->device
));
1168 if (rport
!= cmd_rport
)
1171 io_lock
= fnic_io_lock_hash(fnic
, sc
);
1172 spin_lock_irqsave(io_lock
, flags
);
1174 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1176 if (!io_req
|| rport
!= cmd_rport
) {
1177 spin_unlock_irqrestore(io_lock
, flags
);
1182 * Found IO that is still pending with firmware and
1183 * belongs to rport that went away
1185 if (CMD_STATE(sc
) == FNIC_IOREQ_ABTS_PENDING
) {
1186 spin_unlock_irqrestore(io_lock
, flags
);
1189 old_ioreq_state
= CMD_STATE(sc
);
1190 CMD_STATE(sc
) = FNIC_IOREQ_ABTS_PENDING
;
1191 CMD_ABTS_STATUS(sc
) = FCPIO_INVALID_CODE
;
1193 BUG_ON(io_req
->abts_done
);
1195 FNIC_SCSI_DBG(KERN_DEBUG
,
1197 "fnic_terminate_rport_io: Issuing abts\n");
1199 spin_unlock_irqrestore(io_lock
, flags
);
1201 /* Now queue the abort command to firmware */
1202 int_to_scsilun(sc
->device
->lun
, &fc_lun
);
1204 if (fnic_queue_abort_io_req(fnic
, tag
,
1205 FCPIO_ITMF_ABT_TASK_TERM
,
1206 fc_lun
.scsi_lun
, io_req
)) {
1208 * Revert the cmd state back to old state, if
1209 * it hasnt changed in between. This cmd will get
1210 * aborted later by scsi_eh, or cleaned up during
1213 io_lock
= fnic_io_lock_hash(fnic
, sc
);
1215 spin_lock_irqsave(io_lock
, flags
);
1216 if (CMD_STATE(sc
) == FNIC_IOREQ_ABTS_PENDING
)
1217 CMD_STATE(sc
) = old_ioreq_state
;
1218 spin_unlock_irqrestore(io_lock
, flags
);
1225 * This function is exported to SCSI for sending abort cmnds.
1226 * A SCSI IO is represented by a io_req in the driver.
1227 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1229 int fnic_abort_cmd(struct scsi_cmnd
*sc
)
1231 struct fc_lport
*lp
;
1233 struct fnic_io_req
*io_req
;
1234 struct fc_rport
*rport
;
1235 spinlock_t
*io_lock
;
1236 unsigned long flags
;
1239 struct scsi_lun fc_lun
;
1240 DECLARE_COMPLETION_ONSTACK(tm_done
);
1242 /* Wait for rport to unblock */
1243 fc_block_scsi_eh(sc
);
1245 /* Get local-port, check ready and link up */
1246 lp
= shost_priv(sc
->device
->host
);
1248 fnic
= lport_priv(lp
);
1249 FNIC_SCSI_DBG(KERN_DEBUG
,
1251 "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %d\n",
1252 (starget_to_rport(scsi_target(sc
->device
)))->port_id
,
1253 sc
->device
->lun
, sc
->request
->tag
);
1255 if (lp
->state
!= LPORT_ST_READY
|| !(lp
->link_up
)) {
1257 goto fnic_abort_cmd_end
;
1261 * Avoid a race between SCSI issuing the abort and the device
1262 * completing the command.
1264 * If the command is already completed by the fw cmpl code,
1265 * we just return SUCCESS from here. This means that the abort
1266 * succeeded. In the SCSI ML, since the timeout for command has
1267 * happened, the completion wont actually complete the command
1268 * and it will be considered as an aborted command
1270 * The CMD_SP will not be cleared except while holding io_req_lock.
1272 io_lock
= fnic_io_lock_hash(fnic
, sc
);
1273 spin_lock_irqsave(io_lock
, flags
);
1274 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1276 spin_unlock_irqrestore(io_lock
, flags
);
1277 goto fnic_abort_cmd_end
;
1280 io_req
->abts_done
= &tm_done
;
1282 if (CMD_STATE(sc
) == FNIC_IOREQ_ABTS_PENDING
) {
1283 spin_unlock_irqrestore(io_lock
, flags
);
1287 * Command is still pending, need to abort it
1288 * If the firmware completes the command after this point,
1289 * the completion wont be done till mid-layer, since abort
1290 * has already started.
1292 CMD_STATE(sc
) = FNIC_IOREQ_ABTS_PENDING
;
1293 CMD_ABTS_STATUS(sc
) = FCPIO_INVALID_CODE
;
1295 spin_unlock_irqrestore(io_lock
, flags
);
1298 * Check readiness of the remote port. If the path to remote
1299 * port is up, then send abts to the remote port to terminate
1300 * the IO. Else, just locally terminate the IO in the firmware
1302 rport
= starget_to_rport(scsi_target(sc
->device
));
1303 if (fc_remote_port_chkready(rport
) == 0)
1304 task_req
= FCPIO_ITMF_ABT_TASK
;
1306 task_req
= FCPIO_ITMF_ABT_TASK_TERM
;
1308 /* Now queue the abort command to firmware */
1309 int_to_scsilun(sc
->device
->lun
, &fc_lun
);
1311 if (fnic_queue_abort_io_req(fnic
, sc
->request
->tag
, task_req
,
1312 fc_lun
.scsi_lun
, io_req
)) {
1313 spin_lock_irqsave(io_lock
, flags
);
1314 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1316 io_req
->abts_done
= NULL
;
1317 spin_unlock_irqrestore(io_lock
, flags
);
1319 goto fnic_abort_cmd_end
;
1323 * We queued an abort IO, wait for its completion.
1324 * Once the firmware completes the abort command, it will
1325 * wake up this thread.
1328 wait_for_completion_timeout(&tm_done
,
1330 (2 * fnic
->config
.ra_tov
+
1331 fnic
->config
.ed_tov
));
1333 /* Check the abort status */
1334 spin_lock_irqsave(io_lock
, flags
);
1336 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1338 spin_unlock_irqrestore(io_lock
, flags
);
1340 goto fnic_abort_cmd_end
;
1342 io_req
->abts_done
= NULL
;
1344 /* fw did not complete abort, timed out */
1345 if (CMD_STATE(sc
) == FNIC_IOREQ_ABTS_PENDING
) {
1346 spin_unlock_irqrestore(io_lock
, flags
);
1348 goto fnic_abort_cmd_end
;
1352 * firmware completed the abort, check the status,
1353 * free the io_req irrespective of failure or success
1355 if (CMD_ABTS_STATUS(sc
) != FCPIO_SUCCESS
)
1360 spin_unlock_irqrestore(io_lock
, flags
);
1362 fnic_release_ioreq_buf(fnic
, io_req
, sc
);
1363 mempool_free(io_req
, fnic
->io_req_pool
);
1366 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
1367 "Returning from abort cmd %s\n",
1369 "SUCCESS" : "FAILED");
1373 static inline int fnic_queue_dr_io_req(struct fnic
*fnic
,
1374 struct scsi_cmnd
*sc
,
1375 struct fnic_io_req
*io_req
)
1377 struct vnic_wq_copy
*wq
= &fnic
->wq_copy
[0];
1378 struct scsi_lun fc_lun
;
1380 unsigned long intr_flags
;
1382 spin_lock_irqsave(&fnic
->wq_copy_lock
[0], intr_flags
);
1384 if (vnic_wq_copy_desc_avail(wq
) <= fnic
->wq_copy_desc_low
[0])
1385 free_wq_copy_descs(fnic
, wq
);
1387 if (!vnic_wq_copy_desc_avail(wq
)) {
1392 /* fill in the lun info */
1393 int_to_scsilun(sc
->device
->lun
, &fc_lun
);
1395 fnic_queue_wq_copy_desc_itmf(wq
, sc
->request
->tag
| FNIC_TAG_DEV_RST
,
1396 0, FCPIO_ITMF_LUN_RESET
, SCSI_NO_TAG
,
1397 fc_lun
.scsi_lun
, io_req
->port_id
,
1398 fnic
->config
.ra_tov
, fnic
->config
.ed_tov
);
1401 spin_unlock_irqrestore(&fnic
->wq_copy_lock
[0], intr_flags
);
1407 * Clean up any pending aborts on the lun
1408 * For each outstanding IO on this lun, whose abort is not completed by fw,
1409 * issue a local abort. Wait for abort to complete. Return 0 if all commands
1410 * successfully aborted, 1 otherwise
1412 static int fnic_clean_pending_aborts(struct fnic
*fnic
,
1413 struct scsi_cmnd
*lr_sc
)
1416 struct fnic_io_req
*io_req
;
1417 spinlock_t
*io_lock
;
1418 unsigned long flags
;
1420 struct scsi_cmnd
*sc
;
1421 struct fc_rport
*rport
;
1422 struct scsi_lun fc_lun
;
1423 struct scsi_device
*lun_dev
= lr_sc
->device
;
1424 DECLARE_COMPLETION_ONSTACK(tm_done
);
1426 for (tag
= 0; tag
< FNIC_MAX_IO_REQ
; tag
++) {
1427 sc
= scsi_host_find_tag(fnic
->lport
->host
, tag
);
1429 * ignore this lun reset cmd or cmds that do not belong to
1432 if (!sc
|| sc
== lr_sc
|| sc
->device
!= lun_dev
)
1435 io_lock
= fnic_io_lock_hash(fnic
, sc
);
1436 spin_lock_irqsave(io_lock
, flags
);
1438 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1440 if (!io_req
|| sc
->device
!= lun_dev
) {
1441 spin_unlock_irqrestore(io_lock
, flags
);
1446 * Found IO that is still pending with firmware and
1447 * belongs to the LUN that we are resetting
1449 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
1450 "Found IO in %s on lun\n",
1451 fnic_ioreq_state_to_str(CMD_STATE(sc
)));
1453 BUG_ON(CMD_STATE(sc
) != FNIC_IOREQ_ABTS_PENDING
);
1455 CMD_ABTS_STATUS(sc
) = FCPIO_INVALID_CODE
;
1456 io_req
->abts_done
= &tm_done
;
1457 spin_unlock_irqrestore(io_lock
, flags
);
1459 /* Now queue the abort command to firmware */
1460 int_to_scsilun(sc
->device
->lun
, &fc_lun
);
1461 rport
= starget_to_rport(scsi_target(sc
->device
));
1463 if (fnic_queue_abort_io_req(fnic
, tag
,
1464 FCPIO_ITMF_ABT_TASK_TERM
,
1465 fc_lun
.scsi_lun
, io_req
)) {
1466 spin_lock_irqsave(io_lock
, flags
);
1467 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1469 io_req
->abts_done
= NULL
;
1470 spin_unlock_irqrestore(io_lock
, flags
);
1472 goto clean_pending_aborts_end
;
1475 wait_for_completion_timeout(&tm_done
,
1477 (fnic
->config
.ed_tov
));
1479 /* Recheck cmd state to check if it is now aborted */
1480 spin_lock_irqsave(io_lock
, flags
);
1481 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1483 spin_unlock_irqrestore(io_lock
, flags
);
1485 goto clean_pending_aborts_end
;
1488 io_req
->abts_done
= NULL
;
1490 /* if abort is still pending with fw, fail */
1491 if (CMD_STATE(sc
) == FNIC_IOREQ_ABTS_PENDING
) {
1492 spin_unlock_irqrestore(io_lock
, flags
);
1494 goto clean_pending_aborts_end
;
1497 spin_unlock_irqrestore(io_lock
, flags
);
1499 fnic_release_ioreq_buf(fnic
, io_req
, sc
);
1500 mempool_free(io_req
, fnic
->io_req_pool
);
1503 clean_pending_aborts_end
:
1508 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
1509 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
1512 int fnic_device_reset(struct scsi_cmnd
*sc
)
1514 struct fc_lport
*lp
;
1516 struct fnic_io_req
*io_req
;
1517 struct fc_rport
*rport
;
1520 spinlock_t
*io_lock
;
1521 unsigned long flags
;
1522 DECLARE_COMPLETION_ONSTACK(tm_done
);
1524 /* Wait for rport to unblock */
1525 fc_block_scsi_eh(sc
);
1527 /* Get local-port, check ready and link up */
1528 lp
= shost_priv(sc
->device
->host
);
1530 fnic
= lport_priv(lp
);
1531 FNIC_SCSI_DBG(KERN_DEBUG
,
1533 "Device reset called FCID 0x%x, LUN 0x%x\n",
1534 (starget_to_rport(scsi_target(sc
->device
)))->port_id
,
1538 if (lp
->state
!= LPORT_ST_READY
|| !(lp
->link_up
))
1539 goto fnic_device_reset_end
;
1541 /* Check if remote port up */
1542 rport
= starget_to_rport(scsi_target(sc
->device
));
1543 if (fc_remote_port_chkready(rport
))
1544 goto fnic_device_reset_end
;
1546 io_lock
= fnic_io_lock_hash(fnic
, sc
);
1547 spin_lock_irqsave(io_lock
, flags
);
1548 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1551 * If there is a io_req attached to this command, then use it,
1552 * else allocate a new one.
1555 io_req
= mempool_alloc(fnic
->io_req_pool
, GFP_ATOMIC
);
1557 spin_unlock_irqrestore(io_lock
, flags
);
1558 goto fnic_device_reset_end
;
1560 memset(io_req
, 0, sizeof(*io_req
));
1561 io_req
->port_id
= rport
->port_id
;
1562 CMD_SP(sc
) = (char *)io_req
;
1564 io_req
->dr_done
= &tm_done
;
1565 CMD_STATE(sc
) = FNIC_IOREQ_CMD_PENDING
;
1566 CMD_LR_STATUS(sc
) = FCPIO_INVALID_CODE
;
1567 spin_unlock_irqrestore(io_lock
, flags
);
1569 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
, "TAG %d\n",
1573 * issue the device reset, if enqueue failed, clean up the ioreq
1574 * and break assoc with scsi cmd
1576 if (fnic_queue_dr_io_req(fnic
, sc
, io_req
)) {
1577 spin_lock_irqsave(io_lock
, flags
);
1578 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1580 io_req
->dr_done
= NULL
;
1581 goto fnic_device_reset_clean
;
1585 * Wait on the local completion for LUN reset. The io_req may be
1586 * freed while we wait since we hold no lock.
1588 wait_for_completion_timeout(&tm_done
,
1589 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT
));
1591 spin_lock_irqsave(io_lock
, flags
);
1592 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1594 spin_unlock_irqrestore(io_lock
, flags
);
1595 goto fnic_device_reset_end
;
1597 io_req
->dr_done
= NULL
;
1599 status
= CMD_LR_STATUS(sc
);
1600 spin_unlock_irqrestore(io_lock
, flags
);
1603 * If lun reset not completed, bail out with failed. io_req
1604 * gets cleaned up during higher levels of EH
1606 if (status
== FCPIO_INVALID_CODE
) {
1607 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
1608 "Device reset timed out\n");
1609 goto fnic_device_reset_end
;
1612 /* Completed, but not successful, clean up the io_req, return fail */
1613 if (status
!= FCPIO_SUCCESS
) {
1614 spin_lock_irqsave(io_lock
, flags
);
1615 FNIC_SCSI_DBG(KERN_DEBUG
,
1617 "Device reset completed - failed\n");
1618 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1619 goto fnic_device_reset_clean
;
1623 * Clean up any aborts on this lun that have still not
1624 * completed. If any of these fail, then LUN reset fails.
1625 * clean_pending_aborts cleans all cmds on this lun except
1626 * the lun reset cmd. If all cmds get cleaned, the lun reset
1629 if (fnic_clean_pending_aborts(fnic
, sc
)) {
1630 spin_lock_irqsave(io_lock
, flags
);
1631 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1632 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
1633 "Device reset failed"
1634 " since could not abort all IOs\n");
1635 goto fnic_device_reset_clean
;
1638 /* Clean lun reset command */
1639 spin_lock_irqsave(io_lock
, flags
);
1640 io_req
= (struct fnic_io_req
*)CMD_SP(sc
);
1642 /* Completed, and successful */
1645 fnic_device_reset_clean
:
1649 spin_unlock_irqrestore(io_lock
, flags
);
1652 fnic_release_ioreq_buf(fnic
, io_req
, sc
);
1653 mempool_free(io_req
, fnic
->io_req_pool
);
1656 fnic_device_reset_end
:
1657 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
1658 "Returning from device reset %s\n",
1660 "SUCCESS" : "FAILED");
1664 /* Clean up all IOs, clean up libFC local port */
1665 int fnic_reset(struct Scsi_Host
*shost
)
1667 struct fc_lport
*lp
;
1671 lp
= shost_priv(shost
);
1672 fnic
= lport_priv(lp
);
1674 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
1675 "fnic_reset called\n");
1678 * Reset local port, this will clean up libFC exchanges,
1679 * reset remote port sessions, and if link is up, begin flogi
1681 if (lp
->tt
.lport_reset(lp
))
1684 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
1685 "Returning from fnic reset %s\n",
1687 "SUCCESS" : "FAILED");
1693 * SCSI Error handling calls driver's eh_host_reset if all prior
1694 * error handling levels return FAILED. If host reset completes
1695 * successfully, and if link is up, then Fabric login begins.
1697 * Host Reset is the highest level of error recovery. If this fails, then
1698 * host is offlined by SCSI.
1701 int fnic_host_reset(struct scsi_cmnd
*sc
)
1704 unsigned long wait_host_tmo
;
1705 struct Scsi_Host
*shost
= sc
->device
->host
;
1706 struct fc_lport
*lp
= shost_priv(shost
);
1709 * If fnic_reset is successful, wait for fabric login to complete
1710 * scsi-ml tries to send a TUR to every device if host reset is
1711 * successful, so before returning to scsi, fabric should be up
1713 ret
= fnic_reset(shost
);
1714 if (ret
== SUCCESS
) {
1715 wait_host_tmo
= jiffies
+ FNIC_HOST_RESET_SETTLE_TIME
* HZ
;
1717 while (time_before(jiffies
, wait_host_tmo
)) {
1718 if ((lp
->state
== LPORT_ST_READY
) &&
1731 * This fxn is called from libFC when host is removed
1733 void fnic_scsi_abort_io(struct fc_lport
*lp
)
1736 unsigned long flags
;
1737 enum fnic_state old_state
;
1738 struct fnic
*fnic
= lport_priv(lp
);
1739 DECLARE_COMPLETION_ONSTACK(remove_wait
);
1741 /* Issue firmware reset for fnic, wait for reset to complete */
1742 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
1743 fnic
->remove_wait
= &remove_wait
;
1744 old_state
= fnic
->state
;
1745 fnic
->state
= FNIC_IN_FC_TRANS_ETH_MODE
;
1746 fnic_update_mac_locked(fnic
, fnic
->ctlr
.ctl_src_addr
);
1747 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
1749 err
= fnic_fw_reset_handler(fnic
);
1751 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
1752 if (fnic
->state
== FNIC_IN_FC_TRANS_ETH_MODE
)
1753 fnic
->state
= old_state
;
1754 fnic
->remove_wait
= NULL
;
1755 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
1759 /* Wait for firmware reset to complete */
1760 wait_for_completion_timeout(&remove_wait
,
1761 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT
));
1763 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
1764 fnic
->remove_wait
= NULL
;
1765 FNIC_SCSI_DBG(KERN_DEBUG
, fnic
->lport
->host
,
1766 "fnic_scsi_abort_io %s\n",
1767 (fnic
->state
== FNIC_IN_ETH_MODE
) ?
1768 "SUCCESS" : "FAILED");
1769 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
1774 * This fxn called from libFC to clean up driver IO state on link down
1776 void fnic_scsi_cleanup(struct fc_lport
*lp
)
1778 unsigned long flags
;
1779 enum fnic_state old_state
;
1780 struct fnic
*fnic
= lport_priv(lp
);
1782 /* issue fw reset */
1783 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
1784 old_state
= fnic
->state
;
1785 fnic
->state
= FNIC_IN_FC_TRANS_ETH_MODE
;
1786 fnic_update_mac_locked(fnic
, fnic
->ctlr
.ctl_src_addr
);
1787 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
1789 if (fnic_fw_reset_handler(fnic
)) {
1790 spin_lock_irqsave(&fnic
->fnic_lock
, flags
);
1791 if (fnic
->state
== FNIC_IN_FC_TRANS_ETH_MODE
)
1792 fnic
->state
= old_state
;
1793 spin_unlock_irqrestore(&fnic
->fnic_lock
, flags
);
1798 void fnic_empty_scsi_cleanup(struct fc_lport
*lp
)
1802 void fnic_exch_mgr_reset(struct fc_lport
*lp
, u32 sid
, u32 did
)
1804 struct fnic
*fnic
= lport_priv(lp
);
1806 /* Non-zero sid, nothing to do */
1808 goto call_fc_exch_mgr_reset
;
1811 fnic_rport_exch_reset(fnic
, did
);
1812 goto call_fc_exch_mgr_reset
;
1817 * link down or device being removed
1819 if (!fnic
->in_remove
)
1820 fnic_scsi_cleanup(lp
);
1822 fnic_scsi_abort_io(lp
);
1824 /* call libFC exch mgr reset to reset its exchanges */
1825 call_fc_exch_mgr_reset
:
1826 fc_exch_mgr_reset(lp
, sid
, did
);