blob: 8d26d7a9f01bc35c727970778befe5a79c8ed20f [file] [log] [blame]
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001/*
2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
4 *
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16 * SOFTWARE.
17 */
18#include <linux/mempool.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/workqueue.h>
22#include <linux/pci.h>
23#include <linux/scatterlist.h>
24#include <linux/skbuff.h>
25#include <linux/spinlock.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
28#include <linux/delay.h>
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"
40
41const 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",
46};
47
48static 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",
53};
54
55static 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",
75};
76
77const char *fnic_state_to_str(unsigned int state)
78{
79 if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
80 return "unknown";
81
82 return fnic_state_str[state];
83}
84
85static const char *fnic_ioreq_state_to_str(unsigned int state)
86{
87 if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
88 !fnic_ioreq_state_str[state])
89 return "unknown";
90
91 return fnic_ioreq_state_str[state];
92}
93
94static const char *fnic_fcpio_status_to_str(unsigned int status)
95{
96 if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
97 return "unknown";
98
99 return fcpio_status_str[status];
100}
101
102static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
103
104static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
105 struct scsi_cmnd *sc)
106{
107 u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
108
109 return &fnic->io_req_lock[hash];
110}
111
112/*
113 * Unmap the data buffer and sense buffer for an io_req,
114 * also unmap and free the device-private scatter/gather list.
115 */
116static void fnic_release_ioreq_buf(struct fnic *fnic,
117 struct fnic_io_req *io_req,
118 struct scsi_cmnd *sc)
119{
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);
125
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);
132}
133
134/* Free up Copy Wq descriptors. Called with copy_wq lock held */
135static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
136{
137 /* if no Ack received from firmware, then nothing to clean */
138 if (!fnic->fw_ack_recd[0])
139 return 1;
140
141 /*
142 * Update desc_available count based on number of freed descriptors
143 * Account for wraparound
144 */
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);
152
153 /*
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
157 */
158 wq->to_clean_index =
159 (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
160
161 /* we have processed the acks received so far */
162 fnic->fw_ack_recd[0] = 0;
163 return 0;
164}
165
166
167/*
168 * fnic_fw_reset_handler
169 * Routine to send reset msg to fw
170 */
171int fnic_fw_reset_handler(struct fnic *fnic)
172{
173 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
174 int ret = 0;
175 unsigned long flags;
176
177 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
178
179 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
180 free_wq_copy_descs(fnic, wq);
181
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);
186
187 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
188
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;
196}
197
198
199/*
200 * fnic_flogi_reg_handler
201 * Routine to send flogi register msg to fw
202 */
203int fnic_flogi_reg_handler(struct fnic *fnic)
204{
205 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
206 u8 gw_mac[ETH_ALEN];
207 int ret = 0;
208 unsigned long flags;
209
210 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
211
212 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
213 free_wq_copy_descs(fnic, wq);
214
215 if (!vnic_wq_copy_desc_avail(wq)) {
216 ret = -EAGAIN;
217 goto flogi_reg_ioreq_end;
218 }
219
220 if (fnic->fcoui_mode)
221 memset(gw_mac, 0xff, ETH_ALEN);
222 else
223 memcpy(gw_mac, fnic->dest_addr, ETH_ALEN);
224
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);
229
230flogi_reg_ioreq_end:
231 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
232
233 if (!ret)
234 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
235 "flog reg issued\n");
236
237 return ret;
238}
239
240/*
241 * fnic_queue_wq_copy_desc
242 * Routine to enqueue a wq copy desc
243 */
244static 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,
Roel Kluin87a2d342009-06-23 01:06:40 +0200248 int sg_count)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700249{
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];
261
262 if (sg_count) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700263 /* 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++;
270 }
271
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);
277 }
278
279 io_req->sense_buf_pa = pci_map_single(fnic->pdev,
280 sc->sense_buffer,
281 SCSI_SENSE_BUFFERSIZE,
282 PCI_DMA_FROMDEVICE);
283
284 int_to_scsilun(sc->device->lun, &fc_lun);
285
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;
291
292 /* Enqueue the descriptor in the Copy WQ */
293 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
294
295 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
296 free_wq_copy_descs(fnic, wq);
297
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;
301 }
302
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;
308
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;
313
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 */
Abhijeet Joglekar4b536622009-10-21 16:28:25 -0700322 sc->cmnd, sc->cmd_len,
323 scsi_bufflen(sc),
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700324 fc_lun.scsi_lun, io_req->port_id,
325 rport->maxframe_size, rp->r_a_tov,
326 rp->e_d_tov);
327
328 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
329 return 0;
330}
331
332/*
333 * fnic_queuecommand
334 * Routine to send a scsi cdb
335 * Called with host_lock held and interrupts disabled.
336 */
337int fnic_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
338{
339 struct fc_lport *lp;
340 struct fc_rport *rport;
341 struct fnic_io_req *io_req;
342 struct fnic *fnic;
343 struct vnic_wq_copy *wq;
344 int ret;
Roel Kluin87a2d342009-06-23 01:06:40 +0200345 int sg_count;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700346 unsigned long flags;
347 unsigned long ptr;
348
349 rport = starget_to_rport(scsi_target(sc->device));
350 ret = fc_remote_port_chkready(rport);
351 if (ret) {
352 sc->result = ret;
353 done(sc);
354 return 0;
355 }
356
357 lp = shost_priv(sc->device->host);
358 if (lp->state != LPORT_ST_READY || !(lp->link_up))
359 return SCSI_MLQUEUE_HOST_BUSY;
360
361 /*
362 * Release host lock, use driver resource specific locks from here.
363 * Don't re-enable interrupts in case they were disabled prior to the
364 * caller disabling them.
365 */
366 spin_unlock(lp->host->host_lock);
367
368 /* Get a new io_req for this SCSI IO */
369 fnic = lport_priv(lp);
370
371 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
372 if (!io_req) {
373 ret = SCSI_MLQUEUE_HOST_BUSY;
374 goto out;
375 }
376 memset(io_req, 0, sizeof(*io_req));
377
378 /* Map the data buffer */
379 sg_count = scsi_dma_map(sc);
380 if (sg_count < 0) {
381 mempool_free(io_req, fnic->io_req_pool);
382 goto out;
383 }
384
385 /* Determine the type of scatter/gather list we need */
386 io_req->sgl_cnt = sg_count;
387 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
388 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
389 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
390
391 if (sg_count) {
392 io_req->sgl_list =
393 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
394 GFP_ATOMIC | GFP_DMA);
395 if (!io_req->sgl_list) {
396 ret = SCSI_MLQUEUE_HOST_BUSY;
397 scsi_dma_unmap(sc);
398 mempool_free(io_req, fnic->io_req_pool);
399 goto out;
400 }
401
402 /* Cache sgl list allocated address before alignment */
403 io_req->sgl_list_alloc = io_req->sgl_list;
404 ptr = (unsigned long) io_req->sgl_list;
405 if (ptr % FNIC_SG_DESC_ALIGN) {
406 io_req->sgl_list = (struct host_sg_desc *)
407 (((unsigned long) ptr
408 + FNIC_SG_DESC_ALIGN - 1)
409 & ~(FNIC_SG_DESC_ALIGN - 1));
410 }
411 }
412
413 /* initialize rest of io_req */
414 io_req->port_id = rport->port_id;
415 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
416 CMD_SP(sc) = (char *)io_req;
417 sc->scsi_done = done;
418
419 /* create copy wq desc and enqueue it */
420 wq = &fnic->wq_copy[0];
421 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
422 if (ret) {
423 /*
424 * In case another thread cancelled the request,
425 * refetch the pointer under the lock.
426 */
427 spinlock_t *io_lock = fnic_io_lock_hash(fnic, sc);
428
429 spin_lock_irqsave(io_lock, flags);
430 io_req = (struct fnic_io_req *)CMD_SP(sc);
431 CMD_SP(sc) = NULL;
432 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
433 spin_unlock_irqrestore(io_lock, flags);
434 if (io_req) {
435 fnic_release_ioreq_buf(fnic, io_req, sc);
436 mempool_free(io_req, fnic->io_req_pool);
437 }
438 }
439out:
440 /* acquire host lock before returning to SCSI */
441 spin_lock(lp->host->host_lock);
442 return ret;
443}
444
445/*
446 * fnic_fcpio_fw_reset_cmpl_handler
447 * Routine to handle fw reset completion
448 */
449static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
450 struct fcpio_fw_req *desc)
451{
452 u8 type;
453 u8 hdr_status;
454 struct fcpio_tag tag;
455 int ret = 0;
456 struct fc_frame *flogi;
457 unsigned long flags;
458
459 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
460
461 /* Clean up all outstanding io requests */
462 fnic_cleanup_io(fnic, SCSI_NO_TAG);
463
464 spin_lock_irqsave(&fnic->fnic_lock, flags);
465
466 flogi = fnic->flogi;
467 fnic->flogi = NULL;
468
469 /* fnic should be in FC_TRANS_ETH_MODE */
470 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
471 /* Check status of reset completion */
472 if (!hdr_status) {
473 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
474 "reset cmpl success\n");
475 /* Ready to send flogi out */
476 fnic->state = FNIC_IN_ETH_MODE;
477 } else {
478 FNIC_SCSI_DBG(KERN_DEBUG,
479 fnic->lport->host,
480 "fnic fw_reset : failed %s\n",
481 fnic_fcpio_status_to_str(hdr_status));
482
483 /*
484 * Unable to change to eth mode, cannot send out flogi
485 * Change state to fc mode, so that subsequent Flogi
486 * requests from libFC will cause more attempts to
487 * reset the firmware. Free the cached flogi
488 */
489 fnic->state = FNIC_IN_FC_MODE;
490 ret = -1;
491 }
492 } else {
493 FNIC_SCSI_DBG(KERN_DEBUG,
494 fnic->lport->host,
495 "Unexpected state %s while processing"
496 " reset cmpl\n", fnic_state_to_str(fnic->state));
497 ret = -1;
498 }
499
500 /* Thread removing device blocks till firmware reset is complete */
501 if (fnic->remove_wait)
502 complete(fnic->remove_wait);
503
504 /*
505 * If fnic is being removed, or fw reset failed
506 * free the flogi frame. Else, send it out
507 */
508 if (fnic->remove_wait || ret) {
509 fnic->flogi_oxid = FC_XID_UNKNOWN;
510 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
511 if (flogi)
512 dev_kfree_skb_irq(fp_skb(flogi));
513 goto reset_cmpl_handler_end;
514 }
515
516 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
517
518 if (flogi)
519 ret = fnic_send_frame(fnic, flogi);
520
521 reset_cmpl_handler_end:
522 return ret;
523}
524
525/*
526 * fnic_fcpio_flogi_reg_cmpl_handler
527 * Routine to handle flogi register completion
528 */
529static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
530 struct fcpio_fw_req *desc)
531{
532 u8 type;
533 u8 hdr_status;
534 struct fcpio_tag tag;
535 int ret = 0;
536 struct fc_frame *flogi_resp = NULL;
537 unsigned long flags;
538 struct sk_buff *skb;
539
540 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
541
542 /* Update fnic state based on status of flogi reg completion */
543 spin_lock_irqsave(&fnic->fnic_lock, flags);
544
545 flogi_resp = fnic->flogi_resp;
546 fnic->flogi_resp = NULL;
547
548 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
549
550 /* Check flogi registration completion status */
551 if (!hdr_status) {
552 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
553 "flog reg succeeded\n");
554 fnic->state = FNIC_IN_FC_MODE;
555 } else {
556 FNIC_SCSI_DBG(KERN_DEBUG,
557 fnic->lport->host,
558 "fnic flogi reg :failed %s\n",
559 fnic_fcpio_status_to_str(hdr_status));
560 fnic->state = FNIC_IN_ETH_MODE;
561 ret = -1;
562 }
563 } else {
564 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
565 "Unexpected fnic state %s while"
566 " processing flogi reg completion\n",
567 fnic_state_to_str(fnic->state));
568 ret = -1;
569 }
570
571 /* Successful flogi reg cmpl, pass frame to LibFC */
572 if (!ret && flogi_resp) {
573 if (fnic->stop_rx_link_events) {
574 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
575 goto reg_cmpl_handler_end;
576 }
577 skb = (struct sk_buff *)flogi_resp;
578 /* Use fr_flags to indicate whether flogi resp or not */
579 fr_flags(flogi_resp) = 1;
580 fr_dev(flogi_resp) = fnic->lport;
581 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
582
583 skb_queue_tail(&fnic->frame_queue, skb);
584 queue_work(fnic_event_queue, &fnic->frame_work);
585
586 } else {
587 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
588 if (flogi_resp)
589 dev_kfree_skb_irq(fp_skb(flogi_resp));
590 }
591
592reg_cmpl_handler_end:
593 return ret;
594}
595
596static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
597 u16 request_out)
598{
599 if (wq->to_clean_index <= 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)
603 return 0;
604 } else {
605 /* out of range, stale request_out index */
606 if (request_out < wq->to_clean_index &&
607 request_out >= wq->to_use_index)
608 return 0;
609 }
610 /* request_out index is in range */
611 return 1;
612}
613
614
615/*
616 * Mark that ack received and store the Ack index. If there are multiple
617 * acks received before Tx thread cleans it up, the latest value will be
618 * used which is correct behavior. This state should be in the copy Wq
619 * instead of in the fnic
620 */
621static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
622 unsigned int cq_index,
623 struct fcpio_fw_req *desc)
624{
625 struct vnic_wq_copy *wq;
626 u16 request_out = desc->u.ack.request_out;
627 unsigned long flags;
628
629 /* mark the ack state */
630 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
631 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
632
633 if (is_ack_index_in_range(wq, request_out)) {
634 fnic->fw_ack_index[0] = request_out;
635 fnic->fw_ack_recd[0] = 1;
636 }
637 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
638}
639
640/*
641 * fnic_fcpio_icmnd_cmpl_handler
642 * Routine to handle icmnd completions
643 */
644static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
645 struct fcpio_fw_req *desc)
646{
647 u8 type;
648 u8 hdr_status;
649 struct fcpio_tag tag;
650 u32 id;
651 u64 xfer_len = 0;
652 struct fcpio_icmnd_cmpl *icmnd_cmpl;
653 struct fnic_io_req *io_req;
654 struct scsi_cmnd *sc;
655 unsigned long flags;
656 spinlock_t *io_lock;
657
658 /* Decode the cmpl description to get the io_req id */
659 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
660 fcpio_tag_id_dec(&tag, &id);
661
662 if (id >= FNIC_MAX_IO_REQ)
663 return;
664
665 sc = scsi_host_find_tag(fnic->lport->host, id);
666 WARN_ON_ONCE(!sc);
667 if (!sc)
668 return;
669
670 io_lock = fnic_io_lock_hash(fnic, sc);
671 spin_lock_irqsave(io_lock, flags);
672 io_req = (struct fnic_io_req *)CMD_SP(sc);
673 WARN_ON_ONCE(!io_req);
674 if (!io_req) {
675 spin_unlock_irqrestore(io_lock, flags);
676 return;
677 }
678
679 /* firmware completed the io */
680 io_req->io_completed = 1;
681
682 /*
683 * if SCSI-ML has already issued abort on this command,
684 * ignore completion of the IO. The abts path will clean it up
685 */
686 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
687 spin_unlock_irqrestore(io_lock, flags);
688 return;
689 }
690
691 /* Mark the IO as complete */
692 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
693
694 icmnd_cmpl = &desc->u.icmnd_cmpl;
695
696 switch (hdr_status) {
697 case FCPIO_SUCCESS:
698 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
699 xfer_len = scsi_bufflen(sc);
700 scsi_set_resid(sc, icmnd_cmpl->residual);
701
702 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
703 xfer_len -= icmnd_cmpl->residual;
704
705 /*
706 * If queue_full, then try to reduce queue depth for all
707 * LUNS on the target. Todo: this should be accompanied
708 * by a periodic queue_depth rampup based on successful
709 * IO completion.
710 */
711 if (icmnd_cmpl->scsi_status == QUEUE_FULL) {
712 struct scsi_device *t_sdev;
713 int qd = 0;
714
715 shost_for_each_device(t_sdev, sc->device->host) {
716 if (t_sdev->id != sc->device->id)
717 continue;
718
719 if (t_sdev->queue_depth > 1) {
720 qd = scsi_track_queue_full
721 (t_sdev,
722 t_sdev->queue_depth - 1);
723 if (qd == -1)
724 qd = t_sdev->host->cmd_per_lun;
725 shost_printk(KERN_INFO,
726 fnic->lport->host,
727 "scsi[%d:%d:%d:%d"
728 "] queue full detected,"
729 "new depth = %d\n",
730 t_sdev->host->host_no,
731 t_sdev->channel,
732 t_sdev->id, t_sdev->lun,
733 t_sdev->queue_depth);
734 }
735 }
736 }
737 break;
738
739 case FCPIO_TIMEOUT: /* request was timed out */
740 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
741 break;
742
743 case FCPIO_ABORTED: /* request was aborted */
744 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
745 break;
746
747 case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
748 scsi_set_resid(sc, icmnd_cmpl->residual);
749 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
750 break;
751
752 case FCPIO_OUT_OF_RESOURCE: /* out of resources to complete request */
753 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
754 break;
755 case FCPIO_INVALID_HEADER: /* header contains invalid data */
756 case FCPIO_INVALID_PARAM: /* some parameter in request invalid */
757 case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
758 case FCPIO_IO_NOT_FOUND: /* requested I/O was not found */
759 case FCPIO_SGL_INVALID: /* request was aborted due to sgl error */
760 case FCPIO_MSS_INVALID: /* request was aborted due to mss error */
761 case FCPIO_FW_ERR: /* request was terminated due fw error */
762 default:
763 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
764 fnic_fcpio_status_to_str(hdr_status));
765 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
766 break;
767 }
768
769 /* Break link with the SCSI command */
770 CMD_SP(sc) = NULL;
771
772 spin_unlock_irqrestore(io_lock, flags);
773
774 fnic_release_ioreq_buf(fnic, io_req, sc);
775
776 mempool_free(io_req, fnic->io_req_pool);
777
778 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
779 fnic->lport->host_stats.fcp_input_requests++;
780 fnic->fcp_input_bytes += xfer_len;
781 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
782 fnic->lport->host_stats.fcp_output_requests++;
783 fnic->fcp_output_bytes += xfer_len;
784 } else
785 fnic->lport->host_stats.fcp_control_requests++;
786
787 /* Call SCSI completion function to complete the IO */
788 if (sc->scsi_done)
789 sc->scsi_done(sc);
790
791}
792
793/* fnic_fcpio_itmf_cmpl_handler
794 * Routine to handle itmf completions
795 */
796static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
797 struct fcpio_fw_req *desc)
798{
799 u8 type;
800 u8 hdr_status;
801 struct fcpio_tag tag;
802 u32 id;
803 struct scsi_cmnd *sc;
804 struct fnic_io_req *io_req;
805 unsigned long flags;
806 spinlock_t *io_lock;
807
808 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
809 fcpio_tag_id_dec(&tag, &id);
810
811 if ((id & FNIC_TAG_MASK) >= FNIC_MAX_IO_REQ)
812 return;
813
814 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
815 WARN_ON_ONCE(!sc);
816 if (!sc)
817 return;
818
819 io_lock = fnic_io_lock_hash(fnic, sc);
820 spin_lock_irqsave(io_lock, flags);
821 io_req = (struct fnic_io_req *)CMD_SP(sc);
822 WARN_ON_ONCE(!io_req);
823 if (!io_req) {
824 spin_unlock_irqrestore(io_lock, flags);
825 return;
826 }
827
828 if (id & FNIC_TAG_ABORT) {
829 /* Completion of abort cmd */
830 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
831 /* This is a late completion. Ignore it */
832 spin_unlock_irqrestore(io_lock, flags);
833 return;
834 }
835 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
836 CMD_ABTS_STATUS(sc) = hdr_status;
837
838 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
839 "abts cmpl recd. id %d status %s\n",
840 (int)(id & FNIC_TAG_MASK),
841 fnic_fcpio_status_to_str(hdr_status));
842
843 /*
844 * If scsi_eh thread is blocked waiting for abts to complete,
845 * signal completion to it. IO will be cleaned in the thread
846 * else clean it in this context
847 */
848 if (io_req->abts_done) {
849 complete(io_req->abts_done);
850 spin_unlock_irqrestore(io_lock, flags);
851 } else {
852 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
853 "abts cmpl, completing IO\n");
854 CMD_SP(sc) = NULL;
855 sc->result = (DID_ERROR << 16);
856
857 spin_unlock_irqrestore(io_lock, flags);
858
859 fnic_release_ioreq_buf(fnic, io_req, sc);
860 mempool_free(io_req, fnic->io_req_pool);
861 if (sc->scsi_done)
862 sc->scsi_done(sc);
863 }
864
865 } else if (id & FNIC_TAG_DEV_RST) {
866 /* Completion of device reset */
867 CMD_LR_STATUS(sc) = hdr_status;
868 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
869 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
870 "dev reset cmpl recd. id %d status %s\n",
871 (int)(id & FNIC_TAG_MASK),
872 fnic_fcpio_status_to_str(hdr_status));
873 if (io_req->dr_done)
874 complete(io_req->dr_done);
875 spin_unlock_irqrestore(io_lock, flags);
876
877 } else {
878 shost_printk(KERN_ERR, fnic->lport->host,
879 "Unexpected itmf io state %s tag %x\n",
880 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
881 spin_unlock_irqrestore(io_lock, flags);
882 }
883
884}
885
886/*
887 * fnic_fcpio_cmpl_handler
888 * Routine to service the cq for wq_copy
889 */
890static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
891 unsigned int cq_index,
892 struct fcpio_fw_req *desc)
893{
894 struct fnic *fnic = vnic_dev_priv(vdev);
895 int ret = 0;
896
897 switch (desc->hdr.type) {
898 case FCPIO_ACK: /* fw copied copy wq desc to its queue */
899 fnic_fcpio_ack_handler(fnic, cq_index, desc);
900 break;
901
902 case FCPIO_ICMND_CMPL: /* fw completed a command */
903 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
904 break;
905
906 case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
907 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
908 break;
909
910 case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
911 ret = fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
912 break;
913
914 case FCPIO_RESET_CMPL: /* fw completed reset */
915 ret = fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
916 break;
917
918 default:
919 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
920 "firmware completion type %d\n",
921 desc->hdr.type);
922 break;
923 }
924
925 return ret;
926}
927
928/*
929 * fnic_wq_copy_cmpl_handler
930 * Routine to process wq copy
931 */
932int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
933{
934 unsigned int wq_work_done = 0;
935 unsigned int i, cq_index;
936 unsigned int cur_work_done;
937
938 for (i = 0; i < fnic->wq_copy_count; i++) {
939 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
940 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
941 fnic_fcpio_cmpl_handler,
942 copy_work_to_do);
943 wq_work_done += cur_work_done;
944 }
945 return wq_work_done;
946}
947
948static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
949{
950 unsigned int i;
951 struct fnic_io_req *io_req;
952 unsigned long flags = 0;
953 struct scsi_cmnd *sc;
954 spinlock_t *io_lock;
955
956 for (i = 0; i < FNIC_MAX_IO_REQ; i++) {
957 if (i == exclude_id)
958 continue;
959
960 sc = scsi_host_find_tag(fnic->lport->host, i);
961 if (!sc)
962 continue;
963
964 io_lock = fnic_io_lock_hash(fnic, sc);
965 spin_lock_irqsave(io_lock, flags);
966 io_req = (struct fnic_io_req *)CMD_SP(sc);
967 if (!io_req) {
968 spin_unlock_irqrestore(io_lock, flags);
969 goto cleanup_scsi_cmd;
970 }
971
972 CMD_SP(sc) = NULL;
973
974 spin_unlock_irqrestore(io_lock, flags);
975
976 /*
977 * If there is a scsi_cmnd associated with this io_req, then
978 * free the corresponding state
979 */
980 fnic_release_ioreq_buf(fnic, io_req, sc);
981 mempool_free(io_req, fnic->io_req_pool);
982
983cleanup_scsi_cmd:
984 sc->result = DID_TRANSPORT_DISRUPTED << 16;
985 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "fnic_cleanup_io:"
986 " DID_TRANSPORT_DISRUPTED\n");
987
988 /* Complete the command to SCSI */
989 if (sc->scsi_done)
990 sc->scsi_done(sc);
991 }
992}
993
994void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
995 struct fcpio_host_req *desc)
996{
997 u32 id;
998 struct fnic *fnic = vnic_dev_priv(wq->vdev);
999 struct fnic_io_req *io_req;
1000 struct scsi_cmnd *sc;
1001 unsigned long flags;
1002 spinlock_t *io_lock;
1003
1004 /* get the tag reference */
1005 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1006 id &= FNIC_TAG_MASK;
1007
1008 if (id >= FNIC_MAX_IO_REQ)
1009 return;
1010
1011 sc = scsi_host_find_tag(fnic->lport->host, id);
1012 if (!sc)
1013 return;
1014
1015 io_lock = fnic_io_lock_hash(fnic, sc);
1016 spin_lock_irqsave(io_lock, flags);
1017
1018 /* Get the IO context which this desc refers to */
1019 io_req = (struct fnic_io_req *)CMD_SP(sc);
1020
1021 /* fnic interrupts are turned off by now */
1022
1023 if (!io_req) {
1024 spin_unlock_irqrestore(io_lock, flags);
1025 goto wq_copy_cleanup_scsi_cmd;
1026 }
1027
1028 CMD_SP(sc) = NULL;
1029
1030 spin_unlock_irqrestore(io_lock, flags);
1031
1032 fnic_release_ioreq_buf(fnic, io_req, sc);
1033 mempool_free(io_req, fnic->io_req_pool);
1034
1035wq_copy_cleanup_scsi_cmd:
1036 sc->result = DID_NO_CONNECT << 16;
1037 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1038 " DID_NO_CONNECT\n");
1039
1040 if (sc->scsi_done)
1041 sc->scsi_done(sc);
1042}
1043
1044static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1045 u32 task_req, u8 *fc_lun,
1046 struct fnic_io_req *io_req)
1047{
1048 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1049 unsigned long flags;
1050
1051 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1052
1053 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1054 free_wq_copy_descs(fnic, wq);
1055
1056 if (!vnic_wq_copy_desc_avail(wq)) {
1057 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1058 return 1;
1059 }
1060 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1061 0, task_req, tag, fc_lun, io_req->port_id,
1062 fnic->config.ra_tov, fnic->config.ed_tov);
1063
1064 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1065 return 0;
1066}
1067
1068void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1069{
1070 int tag;
1071 struct fnic_io_req *io_req;
1072 spinlock_t *io_lock;
1073 unsigned long flags;
1074 struct scsi_cmnd *sc;
1075 struct scsi_lun fc_lun;
1076 enum fnic_ioreq_state old_ioreq_state;
1077
1078 FNIC_SCSI_DBG(KERN_DEBUG,
1079 fnic->lport->host,
1080 "fnic_rport_reset_exch called portid 0x%06x\n",
1081 port_id);
1082
1083 if (fnic->in_remove)
1084 return;
1085
1086 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1087 sc = scsi_host_find_tag(fnic->lport->host, tag);
1088 if (!sc)
1089 continue;
1090
1091 io_lock = fnic_io_lock_hash(fnic, sc);
1092 spin_lock_irqsave(io_lock, flags);
1093
1094 io_req = (struct fnic_io_req *)CMD_SP(sc);
1095
1096 if (!io_req || io_req->port_id != port_id) {
1097 spin_unlock_irqrestore(io_lock, flags);
1098 continue;
1099 }
1100
1101 /*
1102 * Found IO that is still pending with firmware and
1103 * belongs to rport that went away
1104 */
1105 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1106 spin_unlock_irqrestore(io_lock, flags);
1107 continue;
1108 }
1109 old_ioreq_state = CMD_STATE(sc);
1110 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1111 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1112
1113 BUG_ON(io_req->abts_done);
1114
1115 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1116 "fnic_rport_reset_exch: Issuing abts\n");
1117
1118 spin_unlock_irqrestore(io_lock, flags);
1119
1120 /* Now queue the abort command to firmware */
1121 int_to_scsilun(sc->device->lun, &fc_lun);
1122
1123 if (fnic_queue_abort_io_req(fnic, tag,
1124 FCPIO_ITMF_ABT_TASK_TERM,
1125 fc_lun.scsi_lun, io_req)) {
1126 /*
1127 * Revert the cmd state back to old state, if
1128 * it hasnt changed in between. This cmd will get
1129 * aborted later by scsi_eh, or cleaned up during
1130 * lun reset
1131 */
1132 io_lock = fnic_io_lock_hash(fnic, sc);
1133
1134 spin_lock_irqsave(io_lock, flags);
1135 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1136 CMD_STATE(sc) = old_ioreq_state;
1137 spin_unlock_irqrestore(io_lock, flags);
1138 }
1139 }
1140
1141}
1142
1143void fnic_terminate_rport_io(struct fc_rport *rport)
1144{
1145 int tag;
1146 struct fnic_io_req *io_req;
1147 spinlock_t *io_lock;
1148 unsigned long flags;
1149 struct scsi_cmnd *sc;
1150 struct scsi_lun fc_lun;
1151 struct fc_rport_libfc_priv *rdata = rport->dd_data;
1152 struct fc_lport *lport = rdata->local_port;
1153 struct fnic *fnic = lport_priv(lport);
1154 struct fc_rport *cmd_rport;
1155 enum fnic_ioreq_state old_ioreq_state;
1156
1157 FNIC_SCSI_DBG(KERN_DEBUG,
1158 fnic->lport->host, "fnic_terminate_rport_io called"
1159 " wwpn 0x%llx, wwnn0x%llx, portid 0x%06x\n",
1160 rport->port_name, rport->node_name,
1161 rport->port_id);
1162
1163 if (fnic->in_remove)
1164 return;
1165
1166 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1167 sc = scsi_host_find_tag(fnic->lport->host, tag);
1168 if (!sc)
1169 continue;
1170
1171 cmd_rport = starget_to_rport(scsi_target(sc->device));
1172 if (rport != cmd_rport)
1173 continue;
1174
1175 io_lock = fnic_io_lock_hash(fnic, sc);
1176 spin_lock_irqsave(io_lock, flags);
1177
1178 io_req = (struct fnic_io_req *)CMD_SP(sc);
1179
1180 if (!io_req || rport != cmd_rport) {
1181 spin_unlock_irqrestore(io_lock, flags);
1182 continue;
1183 }
1184
1185 /*
1186 * Found IO that is still pending with firmware and
1187 * belongs to rport that went away
1188 */
1189 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1190 spin_unlock_irqrestore(io_lock, flags);
1191 continue;
1192 }
1193 old_ioreq_state = CMD_STATE(sc);
1194 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1195 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1196
1197 BUG_ON(io_req->abts_done);
1198
1199 FNIC_SCSI_DBG(KERN_DEBUG,
1200 fnic->lport->host,
1201 "fnic_terminate_rport_io: Issuing abts\n");
1202
1203 spin_unlock_irqrestore(io_lock, flags);
1204
1205 /* Now queue the abort command to firmware */
1206 int_to_scsilun(sc->device->lun, &fc_lun);
1207
1208 if (fnic_queue_abort_io_req(fnic, tag,
1209 FCPIO_ITMF_ABT_TASK_TERM,
1210 fc_lun.scsi_lun, io_req)) {
1211 /*
1212 * Revert the cmd state back to old state, if
1213 * it hasnt changed in between. This cmd will get
1214 * aborted later by scsi_eh, or cleaned up during
1215 * lun reset
1216 */
1217 io_lock = fnic_io_lock_hash(fnic, sc);
1218
1219 spin_lock_irqsave(io_lock, flags);
1220 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1221 CMD_STATE(sc) = old_ioreq_state;
1222 spin_unlock_irqrestore(io_lock, flags);
1223 }
1224 }
1225
1226}
1227
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001228/*
1229 * This function is exported to SCSI for sending abort cmnds.
1230 * A SCSI IO is represented by a io_req in the driver.
1231 * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1232 */
1233int fnic_abort_cmd(struct scsi_cmnd *sc)
1234{
1235 struct fc_lport *lp;
1236 struct fnic *fnic;
1237 struct fnic_io_req *io_req;
1238 struct fc_rport *rport;
1239 spinlock_t *io_lock;
1240 unsigned long flags;
1241 int ret = SUCCESS;
1242 u32 task_req;
1243 struct scsi_lun fc_lun;
1244 DECLARE_COMPLETION_ONSTACK(tm_done);
1245
1246 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001247 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001248
1249 /* Get local-port, check ready and link up */
1250 lp = shost_priv(sc->device->host);
1251
1252 fnic = lport_priv(lp);
1253 FNIC_SCSI_DBG(KERN_DEBUG,
1254 fnic->lport->host,
1255 "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %d\n",
1256 (starget_to_rport(scsi_target(sc->device)))->port_id,
1257 sc->device->lun, sc->request->tag);
1258
1259 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1260 ret = FAILED;
1261 goto fnic_abort_cmd_end;
1262 }
1263
1264 /*
1265 * Avoid a race between SCSI issuing the abort and the device
1266 * completing the command.
1267 *
1268 * If the command is already completed by the fw cmpl code,
1269 * we just return SUCCESS from here. This means that the abort
1270 * succeeded. In the SCSI ML, since the timeout for command has
1271 * happened, the completion wont actually complete the command
1272 * and it will be considered as an aborted command
1273 *
1274 * The CMD_SP will not be cleared except while holding io_req_lock.
1275 */
1276 io_lock = fnic_io_lock_hash(fnic, sc);
1277 spin_lock_irqsave(io_lock, flags);
1278 io_req = (struct fnic_io_req *)CMD_SP(sc);
1279 if (!io_req) {
1280 spin_unlock_irqrestore(io_lock, flags);
1281 goto fnic_abort_cmd_end;
1282 }
1283
1284 io_req->abts_done = &tm_done;
1285
1286 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1287 spin_unlock_irqrestore(io_lock, flags);
1288 goto wait_pending;
1289 }
1290 /*
1291 * Command is still pending, need to abort it
1292 * If the firmware completes the command after this point,
1293 * the completion wont be done till mid-layer, since abort
1294 * has already started.
1295 */
1296 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1297 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1298
1299 spin_unlock_irqrestore(io_lock, flags);
1300
1301 /*
1302 * Check readiness of the remote port. If the path to remote
1303 * port is up, then send abts to the remote port to terminate
1304 * the IO. Else, just locally terminate the IO in the firmware
1305 */
1306 rport = starget_to_rport(scsi_target(sc->device));
1307 if (fc_remote_port_chkready(rport) == 0)
1308 task_req = FCPIO_ITMF_ABT_TASK;
1309 else
1310 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1311
1312 /* Now queue the abort command to firmware */
1313 int_to_scsilun(sc->device->lun, &fc_lun);
1314
1315 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1316 fc_lun.scsi_lun, io_req)) {
1317 spin_lock_irqsave(io_lock, flags);
1318 io_req = (struct fnic_io_req *)CMD_SP(sc);
1319 if (io_req)
1320 io_req->abts_done = NULL;
1321 spin_unlock_irqrestore(io_lock, flags);
1322 ret = FAILED;
1323 goto fnic_abort_cmd_end;
1324 }
1325
1326 /*
1327 * We queued an abort IO, wait for its completion.
1328 * Once the firmware completes the abort command, it will
1329 * wake up this thread.
1330 */
1331 wait_pending:
1332 wait_for_completion_timeout(&tm_done,
1333 msecs_to_jiffies
1334 (2 * fnic->config.ra_tov +
1335 fnic->config.ed_tov));
1336
1337 /* Check the abort status */
1338 spin_lock_irqsave(io_lock, flags);
1339
1340 io_req = (struct fnic_io_req *)CMD_SP(sc);
1341 if (!io_req) {
1342 spin_unlock_irqrestore(io_lock, flags);
1343 ret = FAILED;
1344 goto fnic_abort_cmd_end;
1345 }
1346 io_req->abts_done = NULL;
1347
1348 /* fw did not complete abort, timed out */
1349 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1350 spin_unlock_irqrestore(io_lock, flags);
1351 ret = FAILED;
1352 goto fnic_abort_cmd_end;
1353 }
1354
1355 /*
1356 * firmware completed the abort, check the status,
1357 * free the io_req irrespective of failure or success
1358 */
1359 if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1360 ret = FAILED;
1361
1362 CMD_SP(sc) = NULL;
1363
1364 spin_unlock_irqrestore(io_lock, flags);
1365
1366 fnic_release_ioreq_buf(fnic, io_req, sc);
1367 mempool_free(io_req, fnic->io_req_pool);
1368
1369fnic_abort_cmd_end:
1370 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1371 "Returning from abort cmd %s\n",
1372 (ret == SUCCESS) ?
1373 "SUCCESS" : "FAILED");
1374 return ret;
1375}
1376
1377static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1378 struct scsi_cmnd *sc,
1379 struct fnic_io_req *io_req)
1380{
1381 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1382 struct scsi_lun fc_lun;
1383 int ret = 0;
1384 unsigned long intr_flags;
1385
1386 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1387
1388 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1389 free_wq_copy_descs(fnic, wq);
1390
1391 if (!vnic_wq_copy_desc_avail(wq)) {
1392 ret = -EAGAIN;
1393 goto lr_io_req_end;
1394 }
1395
1396 /* fill in the lun info */
1397 int_to_scsilun(sc->device->lun, &fc_lun);
1398
1399 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1400 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1401 fc_lun.scsi_lun, io_req->port_id,
1402 fnic->config.ra_tov, fnic->config.ed_tov);
1403
1404lr_io_req_end:
1405 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
1406
1407 return ret;
1408}
1409
1410/*
1411 * Clean up any pending aborts on the lun
1412 * For each outstanding IO on this lun, whose abort is not completed by fw,
1413 * issue a local abort. Wait for abort to complete. Return 0 if all commands
1414 * successfully aborted, 1 otherwise
1415 */
1416static int fnic_clean_pending_aborts(struct fnic *fnic,
1417 struct scsi_cmnd *lr_sc)
1418{
1419 int tag;
1420 struct fnic_io_req *io_req;
1421 spinlock_t *io_lock;
1422 unsigned long flags;
1423 int ret = 0;
1424 struct scsi_cmnd *sc;
1425 struct fc_rport *rport;
1426 struct scsi_lun fc_lun;
1427 struct scsi_device *lun_dev = lr_sc->device;
1428 DECLARE_COMPLETION_ONSTACK(tm_done);
1429
1430 for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1431 sc = scsi_host_find_tag(fnic->lport->host, tag);
1432 /*
1433 * ignore this lun reset cmd or cmds that do not belong to
1434 * this lun
1435 */
1436 if (!sc || sc == lr_sc || sc->device != lun_dev)
1437 continue;
1438
1439 io_lock = fnic_io_lock_hash(fnic, sc);
1440 spin_lock_irqsave(io_lock, flags);
1441
1442 io_req = (struct fnic_io_req *)CMD_SP(sc);
1443
1444 if (!io_req || sc->device != lun_dev) {
1445 spin_unlock_irqrestore(io_lock, flags);
1446 continue;
1447 }
1448
1449 /*
1450 * Found IO that is still pending with firmware and
1451 * belongs to the LUN that we are resetting
1452 */
1453 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1454 "Found IO in %s on lun\n",
1455 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1456
1457 BUG_ON(CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING);
1458
1459 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1460 io_req->abts_done = &tm_done;
1461 spin_unlock_irqrestore(io_lock, flags);
1462
1463 /* Now queue the abort command to firmware */
1464 int_to_scsilun(sc->device->lun, &fc_lun);
1465 rport = starget_to_rport(scsi_target(sc->device));
1466
1467 if (fnic_queue_abort_io_req(fnic, tag,
1468 FCPIO_ITMF_ABT_TASK_TERM,
1469 fc_lun.scsi_lun, io_req)) {
1470 spin_lock_irqsave(io_lock, flags);
1471 io_req = (struct fnic_io_req *)CMD_SP(sc);
1472 if (io_req)
1473 io_req->abts_done = NULL;
1474 spin_unlock_irqrestore(io_lock, flags);
1475 ret = 1;
1476 goto clean_pending_aborts_end;
1477 }
1478
1479 wait_for_completion_timeout(&tm_done,
1480 msecs_to_jiffies
1481 (fnic->config.ed_tov));
1482
1483 /* Recheck cmd state to check if it is now aborted */
1484 spin_lock_irqsave(io_lock, flags);
1485 io_req = (struct fnic_io_req *)CMD_SP(sc);
1486 if (!io_req) {
1487 spin_unlock_irqrestore(io_lock, flags);
1488 ret = 1;
1489 goto clean_pending_aborts_end;
1490 }
1491
1492 io_req->abts_done = NULL;
1493
1494 /* if abort is still pending with fw, fail */
1495 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1496 spin_unlock_irqrestore(io_lock, flags);
1497 ret = 1;
1498 goto clean_pending_aborts_end;
1499 }
1500 CMD_SP(sc) = NULL;
1501 spin_unlock_irqrestore(io_lock, flags);
1502
1503 fnic_release_ioreq_buf(fnic, io_req, sc);
1504 mempool_free(io_req, fnic->io_req_pool);
1505 }
1506
1507clean_pending_aborts_end:
1508 return ret;
1509}
1510
1511/*
1512 * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
1513 * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
1514 * on the LUN.
1515 */
1516int fnic_device_reset(struct scsi_cmnd *sc)
1517{
1518 struct fc_lport *lp;
1519 struct fnic *fnic;
1520 struct fnic_io_req *io_req;
1521 struct fc_rport *rport;
1522 int status;
1523 int ret = FAILED;
1524 spinlock_t *io_lock;
1525 unsigned long flags;
1526 DECLARE_COMPLETION_ONSTACK(tm_done);
1527
1528 /* Wait for rport to unblock */
Christof Schmitt65d430f2009-10-30 17:59:29 +01001529 fc_block_scsi_eh(sc);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001530
1531 /* Get local-port, check ready and link up */
1532 lp = shost_priv(sc->device->host);
1533
1534 fnic = lport_priv(lp);
1535 FNIC_SCSI_DBG(KERN_DEBUG,
1536 fnic->lport->host,
1537 "Device reset called FCID 0x%x, LUN 0x%x\n",
1538 (starget_to_rport(scsi_target(sc->device)))->port_id,
1539 sc->device->lun);
1540
1541
1542 if (lp->state != LPORT_ST_READY || !(lp->link_up))
1543 goto fnic_device_reset_end;
1544
1545 /* Check if remote port up */
1546 rport = starget_to_rport(scsi_target(sc->device));
1547 if (fc_remote_port_chkready(rport))
1548 goto fnic_device_reset_end;
1549
1550 io_lock = fnic_io_lock_hash(fnic, sc);
1551 spin_lock_irqsave(io_lock, flags);
1552 io_req = (struct fnic_io_req *)CMD_SP(sc);
1553
1554 /*
1555 * If there is a io_req attached to this command, then use it,
1556 * else allocate a new one.
1557 */
1558 if (!io_req) {
1559 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
1560 if (!io_req) {
1561 spin_unlock_irqrestore(io_lock, flags);
1562 goto fnic_device_reset_end;
1563 }
1564 memset(io_req, 0, sizeof(*io_req));
1565 io_req->port_id = rport->port_id;
1566 CMD_SP(sc) = (char *)io_req;
1567 }
1568 io_req->dr_done = &tm_done;
1569 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
1570 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
1571 spin_unlock_irqrestore(io_lock, flags);
1572
1573 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %d\n",
1574 sc->request->tag);
1575
1576 /*
1577 * issue the device reset, if enqueue failed, clean up the ioreq
1578 * and break assoc with scsi cmd
1579 */
1580 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
1581 spin_lock_irqsave(io_lock, flags);
1582 io_req = (struct fnic_io_req *)CMD_SP(sc);
1583 if (io_req)
1584 io_req->dr_done = NULL;
1585 goto fnic_device_reset_clean;
1586 }
1587
1588 /*
1589 * Wait on the local completion for LUN reset. The io_req may be
1590 * freed while we wait since we hold no lock.
1591 */
1592 wait_for_completion_timeout(&tm_done,
1593 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
1594
1595 spin_lock_irqsave(io_lock, flags);
1596 io_req = (struct fnic_io_req *)CMD_SP(sc);
1597 if (!io_req) {
1598 spin_unlock_irqrestore(io_lock, flags);
1599 goto fnic_device_reset_end;
1600 }
1601 io_req->dr_done = NULL;
1602
1603 status = CMD_LR_STATUS(sc);
1604 spin_unlock_irqrestore(io_lock, flags);
1605
1606 /*
1607 * If lun reset not completed, bail out with failed. io_req
1608 * gets cleaned up during higher levels of EH
1609 */
1610 if (status == FCPIO_INVALID_CODE) {
1611 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1612 "Device reset timed out\n");
1613 goto fnic_device_reset_end;
1614 }
1615
1616 /* Completed, but not successful, clean up the io_req, return fail */
1617 if (status != FCPIO_SUCCESS) {
1618 spin_lock_irqsave(io_lock, flags);
1619 FNIC_SCSI_DBG(KERN_DEBUG,
1620 fnic->lport->host,
1621 "Device reset completed - failed\n");
1622 io_req = (struct fnic_io_req *)CMD_SP(sc);
1623 goto fnic_device_reset_clean;
1624 }
1625
1626 /*
1627 * Clean up any aborts on this lun that have still not
1628 * completed. If any of these fail, then LUN reset fails.
1629 * clean_pending_aborts cleans all cmds on this lun except
1630 * the lun reset cmd. If all cmds get cleaned, the lun reset
1631 * succeeds
1632 */
1633 if (fnic_clean_pending_aborts(fnic, sc)) {
1634 spin_lock_irqsave(io_lock, flags);
1635 io_req = (struct fnic_io_req *)CMD_SP(sc);
1636 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1637 "Device reset failed"
1638 " since could not abort all IOs\n");
1639 goto fnic_device_reset_clean;
1640 }
1641
1642 /* Clean lun reset command */
1643 spin_lock_irqsave(io_lock, flags);
1644 io_req = (struct fnic_io_req *)CMD_SP(sc);
1645 if (io_req)
1646 /* Completed, and successful */
1647 ret = SUCCESS;
1648
1649fnic_device_reset_clean:
1650 if (io_req)
1651 CMD_SP(sc) = NULL;
1652
1653 spin_unlock_irqrestore(io_lock, flags);
1654
1655 if (io_req) {
1656 fnic_release_ioreq_buf(fnic, io_req, sc);
1657 mempool_free(io_req, fnic->io_req_pool);
1658 }
1659
1660fnic_device_reset_end:
1661 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1662 "Returning from device reset %s\n",
1663 (ret == SUCCESS) ?
1664 "SUCCESS" : "FAILED");
1665 return ret;
1666}
1667
1668/* Clean up all IOs, clean up libFC local port */
1669int fnic_reset(struct Scsi_Host *shost)
1670{
1671 struct fc_lport *lp;
1672 struct fnic *fnic;
1673 int ret = SUCCESS;
1674
1675 lp = shost_priv(shost);
1676 fnic = lport_priv(lp);
1677
1678 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1679 "fnic_reset called\n");
1680
1681 /*
1682 * Reset local port, this will clean up libFC exchanges,
1683 * reset remote port sessions, and if link is up, begin flogi
1684 */
1685 if (lp->tt.lport_reset(lp))
1686 ret = FAILED;
1687
1688 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1689 "Returning from fnic reset %s\n",
1690 (ret == SUCCESS) ?
1691 "SUCCESS" : "FAILED");
1692
1693 return ret;
1694}
1695
1696/*
1697 * SCSI Error handling calls driver's eh_host_reset if all prior
1698 * error handling levels return FAILED. If host reset completes
1699 * successfully, and if link is up, then Fabric login begins.
1700 *
1701 * Host Reset is the highest level of error recovery. If this fails, then
1702 * host is offlined by SCSI.
1703 *
1704 */
1705int fnic_host_reset(struct scsi_cmnd *sc)
1706{
1707 int ret;
1708 unsigned long wait_host_tmo;
1709 struct Scsi_Host *shost = sc->device->host;
1710 struct fc_lport *lp = shost_priv(shost);
1711
1712 /*
1713 * If fnic_reset is successful, wait for fabric login to complete
1714 * scsi-ml tries to send a TUR to every device if host reset is
1715 * successful, so before returning to scsi, fabric should be up
1716 */
1717 ret = fnic_reset(shost);
1718 if (ret == SUCCESS) {
1719 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
1720 ret = FAILED;
1721 while (time_before(jiffies, wait_host_tmo)) {
1722 if ((lp->state == LPORT_ST_READY) &&
1723 (lp->link_up)) {
1724 ret = SUCCESS;
1725 break;
1726 }
1727 ssleep(1);
1728 }
1729 }
1730
1731 return ret;
1732}
1733
1734/*
1735 * This fxn is called from libFC when host is removed
1736 */
1737void fnic_scsi_abort_io(struct fc_lport *lp)
1738{
1739 int err = 0;
1740 unsigned long flags;
1741 enum fnic_state old_state;
1742 struct fnic *fnic = lport_priv(lp);
1743 DECLARE_COMPLETION_ONSTACK(remove_wait);
1744
1745 /* Issue firmware reset for fnic, wait for reset to complete */
1746 spin_lock_irqsave(&fnic->fnic_lock, flags);
1747 fnic->remove_wait = &remove_wait;
1748 old_state = fnic->state;
1749 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1750 vnic_dev_del_addr(fnic->vdev, fnic->data_src_addr);
1751 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1752
1753 err = fnic_fw_reset_handler(fnic);
1754 if (err) {
1755 spin_lock_irqsave(&fnic->fnic_lock, flags);
1756 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
1757 fnic->state = old_state;
1758 fnic->remove_wait = NULL;
1759 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1760 return;
1761 }
1762
1763 /* Wait for firmware reset to complete */
1764 wait_for_completion_timeout(&remove_wait,
1765 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
1766
1767 spin_lock_irqsave(&fnic->fnic_lock, flags);
1768 fnic->remove_wait = NULL;
1769 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1770 "fnic_scsi_abort_io %s\n",
1771 (fnic->state == FNIC_IN_ETH_MODE) ?
1772 "SUCCESS" : "FAILED");
1773 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1774
1775}
1776
1777/*
1778 * This fxn called from libFC to clean up driver IO state on link down
1779 */
1780void fnic_scsi_cleanup(struct fc_lport *lp)
1781{
1782 unsigned long flags;
1783 enum fnic_state old_state;
1784 struct fnic *fnic = lport_priv(lp);
1785
1786 /* issue fw reset */
1787 spin_lock_irqsave(&fnic->fnic_lock, flags);
1788 old_state = fnic->state;
1789 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1790 vnic_dev_del_addr(fnic->vdev, fnic->data_src_addr);
1791 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1792
1793 if (fnic_fw_reset_handler(fnic)) {
1794 spin_lock_irqsave(&fnic->fnic_lock, flags);
1795 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
1796 fnic->state = old_state;
1797 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1798 }
1799
1800}
1801
1802void fnic_empty_scsi_cleanup(struct fc_lport *lp)
1803{
1804}
1805
1806void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
1807{
1808 struct fnic *fnic = lport_priv(lp);
1809
1810 /* Non-zero sid, nothing to do */
1811 if (sid)
1812 goto call_fc_exch_mgr_reset;
1813
1814 if (did) {
1815 fnic_rport_exch_reset(fnic, did);
1816 goto call_fc_exch_mgr_reset;
1817 }
1818
1819 /*
1820 * sid = 0, did = 0
1821 * link down or device being removed
1822 */
1823 if (!fnic->in_remove)
1824 fnic_scsi_cleanup(lp);
1825 else
1826 fnic_scsi_abort_io(lp);
1827
1828 /* call libFC exch mgr reset to reset its exchanges */
1829call_fc_exch_mgr_reset:
1830 fc_exch_mgr_reset(lp, sid, did);
1831
1832}