1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 <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>
39#include "fnic_io.h"
40#include "fnic.h"
41
42const char *fnic_state_str[] = {
43 [FNIC_IN_FC_MODE] = "FNIC_IN_FC_MODE",
44 [FNIC_IN_FC_TRANS_ETH_MODE] = "FNIC_IN_FC_TRANS_ETH_MODE",
45 [FNIC_IN_ETH_MODE] = "FNIC_IN_ETH_MODE",
46 [FNIC_IN_ETH_TRANS_FC_MODE] = "FNIC_IN_ETH_TRANS_FC_MODE",
47};
48
49static const char *fnic_ioreq_state_str[] = {
50 [FNIC_IOREQ_NOT_INITED] = "FNIC_IOREQ_NOT_INITED",
51 [FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
52 [FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
53 [FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
54 [FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
55};
56
57static const char *fcpio_status_str[] = {
58 [FCPIO_SUCCESS] = "FCPIO_SUCCESS",
59 [FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
60 [FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
61 [FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
62 [FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
63 [FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
64 [FCPIO_ABORTED] = "FCPIO_ABORTED",
65 [FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
66 [FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
67 [FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
68 [FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
69 [FCPIO_FW_ERR] = "FCPIO_FW_ERR",
70 [FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
71 [FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
72 [FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
73 [FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
74 [FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
75 [FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
76 [FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
77};
78
79const char *fnic_state_to_str(unsigned int state)
80{
81 if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
82 return "unknown";
83
84 return fnic_state_str[state];
85}
86
87static const char *fnic_ioreq_state_to_str(unsigned int state)
88{
89 if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
90 !fnic_ioreq_state_str[state])
91 return "unknown";
92
93 return fnic_ioreq_state_str[state];
94}
95
96static const char *fnic_fcpio_status_to_str(unsigned int status)
97{
98 if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
99 return "unknown";
100
101 return fcpio_status_str[status];
102}
103
104static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
105
106static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
107 struct scsi_cmnd *sc)
108{
109 u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
110
111 return &fnic->io_req_lock[hash];
112}
113
114static inline spinlock_t *fnic_io_lock_tag(struct fnic *fnic,
115 int tag)
116{
117 return &fnic->io_req_lock[tag & (FNIC_IO_LOCKS - 1)];
118}
119
120
121
122
123
124static void fnic_release_ioreq_buf(struct fnic *fnic,
125 struct fnic_io_req *io_req,
126 struct scsi_cmnd *sc)
127{
128 if (io_req->sgl_list_pa)
129 pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
130 sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
131 PCI_DMA_TODEVICE);
132 scsi_dma_unmap(sc);
133
134 if (io_req->sgl_cnt)
135 mempool_free(io_req->sgl_list_alloc,
136 fnic->io_sgl_pool[io_req->sgl_type]);
137 if (io_req->sense_buf_pa)
138 pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
139 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
140}
141
142
143static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
144{
145
146 if (!fnic->fw_ack_recd[0])
147 return 1;
148
149
150
151
152
153 if (wq->to_clean_index <= fnic->fw_ack_index[0])
154 wq->ring.desc_avail += (fnic->fw_ack_index[0]
155 - wq->to_clean_index + 1);
156 else
157 wq->ring.desc_avail += (wq->ring.desc_count
158 - wq->to_clean_index
159 + fnic->fw_ack_index[0] + 1);
160
161
162
163
164
165
166 wq->to_clean_index =
167 (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
168
169
170 fnic->fw_ack_recd[0] = 0;
171 return 0;
172}
173
174
175
176
177
178
179void
180__fnic_set_state_flags(struct fnic *fnic, unsigned long st_flags,
181 unsigned long clearbits)
182{
183 struct Scsi_Host *host = fnic->lport->host;
184 int sh_locked = spin_is_locked(host->host_lock);
185 unsigned long flags = 0;
186
187 if (!sh_locked)
188 spin_lock_irqsave(host->host_lock, flags);
189
190 if (clearbits)
191 fnic->state_flags &= ~st_flags;
192 else
193 fnic->state_flags |= st_flags;
194
195 if (!sh_locked)
196 spin_unlock_irqrestore(host->host_lock, flags);
197
198 return;
199}
200
201
202
203
204
205
206int fnic_fw_reset_handler(struct fnic *fnic)
207{
208 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
209 int ret = 0;
210 unsigned long flags;
211
212
213 fnic_set_state_flags(fnic, FNIC_FLAGS_FWRESET);
214
215 skb_queue_purge(&fnic->frame_queue);
216 skb_queue_purge(&fnic->tx_queue);
217
218
219 while (atomic_read(&fnic->in_flight))
220 schedule_timeout(msecs_to_jiffies(1));
221
222 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
223
224 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
225 free_wq_copy_descs(fnic, wq);
226
227 if (!vnic_wq_copy_desc_avail(wq))
228 ret = -EAGAIN;
229 else {
230 fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
231 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
232 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
233 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
234 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
235 atomic64_read(
236 &fnic->fnic_stats.fw_stats.active_fw_reqs));
237 }
238
239 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
240
241 if (!ret) {
242 atomic64_inc(&fnic->fnic_stats.reset_stats.fw_resets);
243 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
244 "Issued fw reset\n");
245 } else {
246 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
247 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
248 "Failed to issue fw reset\n");
249 }
250
251 return ret;
252}
253
254
255
256
257
258
259int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
260{
261 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
262 enum fcpio_flogi_reg_format_type format;
263 struct fc_lport *lp = fnic->lport;
264 u8 gw_mac[ETH_ALEN];
265 int ret = 0;
266 unsigned long flags;
267
268 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
269
270 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
271 free_wq_copy_descs(fnic, wq);
272
273 if (!vnic_wq_copy_desc_avail(wq)) {
274 ret = -EAGAIN;
275 goto flogi_reg_ioreq_end;
276 }
277
278 if (fnic->ctlr.map_dest) {
279 memset(gw_mac, 0xff, ETH_ALEN);
280 format = FCPIO_FLOGI_REG_DEF_DEST;
281 } else {
282 memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
283 format = FCPIO_FLOGI_REG_GW_DEST;
284 }
285
286 if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
287 fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
288 fc_id, gw_mac,
289 fnic->data_src_addr,
290 lp->r_a_tov, lp->e_d_tov);
291 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
292 "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
293 fc_id, fnic->data_src_addr, gw_mac);
294 } else {
295 fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
296 format, fc_id, gw_mac);
297 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
298 "FLOGI reg issued fcid %x map %d dest %pM\n",
299 fc_id, fnic->ctlr.map_dest, gw_mac);
300 }
301
302 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
303 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
304 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
305 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
306 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
307
308flogi_reg_ioreq_end:
309 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
310 return ret;
311}
312
313
314
315
316
317static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
318 struct vnic_wq_copy *wq,
319 struct fnic_io_req *io_req,
320 struct scsi_cmnd *sc,
321 int sg_count)
322{
323 struct scatterlist *sg;
324 struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
325 struct fc_rport_libfc_priv *rp = rport->dd_data;
326 struct host_sg_desc *desc;
327 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
328 u8 pri_tag = 0;
329 unsigned int i;
330 unsigned long intr_flags;
331 int flags;
332 u8 exch_flags;
333 struct scsi_lun fc_lun;
334 char msg[2];
335
336 if (sg_count) {
337
338 desc = io_req->sgl_list;
339 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
340 desc->addr = cpu_to_le64(sg_dma_address(sg));
341 desc->len = cpu_to_le32(sg_dma_len(sg));
342 desc->_resvd = 0;
343 desc++;
344 }
345
346 io_req->sgl_list_pa = pci_map_single
347 (fnic->pdev,
348 io_req->sgl_list,
349 sizeof(io_req->sgl_list[0]) * sg_count,
350 PCI_DMA_TODEVICE);
351 }
352
353 io_req->sense_buf_pa = pci_map_single(fnic->pdev,
354 sc->sense_buffer,
355 SCSI_SENSE_BUFFERSIZE,
356 PCI_DMA_FROMDEVICE);
357
358 int_to_scsilun(sc->device->lun, &fc_lun);
359
360 pri_tag = FCPIO_ICMND_PTA_SIMPLE;
361 msg[0] = MSG_SIMPLE_TAG;
362 scsi_populate_tag_msg(sc, msg);
363 if (msg[0] == MSG_ORDERED_TAG)
364 pri_tag = FCPIO_ICMND_PTA_ORDERED;
365
366
367 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
368
369 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
370 free_wq_copy_descs(fnic, wq);
371
372 if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
373 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
374 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
375 "fnic_queue_wq_copy_desc failure - no descriptors\n");
376 atomic64_inc(&misc_stats->io_cpwq_alloc_failures);
377 return SCSI_MLQUEUE_HOST_BUSY;
378 }
379
380 flags = 0;
381 if (sc->sc_data_direction == DMA_FROM_DEVICE)
382 flags = FCPIO_ICMND_RDDATA;
383 else if (sc->sc_data_direction == DMA_TO_DEVICE)
384 flags = FCPIO_ICMND_WRDATA;
385
386 exch_flags = 0;
387 if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
388 (rp->flags & FC_RP_FLAGS_RETRY))
389 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
390
391 fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
392 0, exch_flags, io_req->sgl_cnt,
393 SCSI_SENSE_BUFFERSIZE,
394 io_req->sgl_list_pa,
395 io_req->sense_buf_pa,
396 0,
397 pri_tag,
398 flags,
399 sc->cmnd, sc->cmd_len,
400 scsi_bufflen(sc),
401 fc_lun.scsi_lun, io_req->port_id,
402 rport->maxframe_size, rp->r_a_tov,
403 rp->e_d_tov);
404
405 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
406 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
407 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
408 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
409 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
410
411 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
412 return 0;
413}
414
415
416
417
418
419
420static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
421{
422 struct fc_lport *lp = shost_priv(sc->device->host);
423 struct fc_rport *rport;
424 struct fnic_io_req *io_req = NULL;
425 struct fnic *fnic = lport_priv(lp);
426 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
427 struct vnic_wq_copy *wq;
428 int ret;
429 u64 cmd_trace;
430 int sg_count = 0;
431 unsigned long flags;
432 unsigned long ptr;
433
434 if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
435 return SCSI_MLQUEUE_HOST_BUSY;
436
437 rport = starget_to_rport(scsi_target(sc->device));
438 ret = fc_remote_port_chkready(rport);
439 if (ret) {
440 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
441 sc->result = ret;
442 done(sc);
443 return 0;
444 }
445
446 if (lp->state != LPORT_ST_READY || !(lp->link_up))
447 return SCSI_MLQUEUE_HOST_BUSY;
448
449 atomic_inc(&fnic->in_flight);
450
451
452
453
454
455
456 spin_unlock(lp->host->host_lock);
457 CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
458 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
459
460
461 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
462 if (!io_req) {
463 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
464 ret = SCSI_MLQUEUE_HOST_BUSY;
465 goto out;
466 }
467 memset(io_req, 0, sizeof(*io_req));
468
469
470 sg_count = scsi_dma_map(sc);
471 if (sg_count < 0) {
472 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
473 sc->request->tag, sc, 0, sc->cmnd[0],
474 sg_count, CMD_STATE(sc));
475 mempool_free(io_req, fnic->io_req_pool);
476 goto out;
477 }
478
479
480 io_req->sgl_cnt = sg_count;
481 io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
482 if (sg_count > FNIC_DFLT_SG_DESC_CNT)
483 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
484
485 if (sg_count) {
486 io_req->sgl_list =
487 mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
488 GFP_ATOMIC);
489 if (!io_req->sgl_list) {
490 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
491 ret = SCSI_MLQUEUE_HOST_BUSY;
492 scsi_dma_unmap(sc);
493 mempool_free(io_req, fnic->io_req_pool);
494 goto out;
495 }
496
497
498 io_req->sgl_list_alloc = io_req->sgl_list;
499 ptr = (unsigned long) io_req->sgl_list;
500 if (ptr % FNIC_SG_DESC_ALIGN) {
501 io_req->sgl_list = (struct host_sg_desc *)
502 (((unsigned long) ptr
503 + FNIC_SG_DESC_ALIGN - 1)
504 & ~(FNIC_SG_DESC_ALIGN - 1));
505 }
506 }
507
508
509 io_req->port_id = rport->port_id;
510 io_req->start_time = jiffies;
511 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
512 CMD_SP(sc) = (char *)io_req;
513 CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
514 sc->scsi_done = done;
515
516
517 wq = &fnic->wq_copy[0];
518 ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
519 if (ret) {
520
521
522
523
524 spinlock_t *io_lock = fnic_io_lock_hash(fnic, sc);
525 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
526 sc->request->tag, sc, 0, 0, 0,
527 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
528 spin_lock_irqsave(io_lock, flags);
529 io_req = (struct fnic_io_req *)CMD_SP(sc);
530 CMD_SP(sc) = NULL;
531 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
532 spin_unlock_irqrestore(io_lock, flags);
533 if (io_req) {
534 fnic_release_ioreq_buf(fnic, io_req, sc);
535 mempool_free(io_req, fnic->io_req_pool);
536 }
537 } else {
538 atomic64_inc(&fnic_stats->io_stats.active_ios);
539 atomic64_inc(&fnic_stats->io_stats.num_ios);
540 if (atomic64_read(&fnic_stats->io_stats.active_ios) >
541 atomic64_read(&fnic_stats->io_stats.max_active_ios))
542 atomic64_set(&fnic_stats->io_stats.max_active_ios,
543 atomic64_read(&fnic_stats->io_stats.active_ios));
544
545
546 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
547 }
548out:
549 cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
550 (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
551 (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
552 sc->cmnd[5]);
553
554 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
555 sc->request->tag, sc, io_req,
556 sg_count, cmd_trace,
557 (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
558 atomic_dec(&fnic->in_flight);
559
560 spin_lock(lp->host->host_lock);
561 return ret;
562}
563
564DEF_SCSI_QCMD(fnic_queuecommand)
565
566
567
568
569
570static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
571 struct fcpio_fw_req *desc)
572{
573 u8 type;
574 u8 hdr_status;
575 struct fcpio_tag tag;
576 int ret = 0;
577 unsigned long flags;
578 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
579
580 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
581
582 atomic64_inc(&reset_stats->fw_reset_completions);
583
584
585 fnic_cleanup_io(fnic, SCSI_NO_TAG);
586
587 atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
588 atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
589
590 spin_lock_irqsave(&fnic->fnic_lock, flags);
591
592
593 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
594
595 if (!hdr_status) {
596 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
597 "reset cmpl success\n");
598
599 fnic->state = FNIC_IN_ETH_MODE;
600 } else {
601 FNIC_SCSI_DBG(KERN_DEBUG,
602 fnic->lport->host,
603 "fnic fw_reset : failed %s\n",
604 fnic_fcpio_status_to_str(hdr_status));
605
606
607
608
609
610
611
612 fnic->state = FNIC_IN_FC_MODE;
613 atomic64_inc(&reset_stats->fw_reset_failures);
614 ret = -1;
615 }
616 } else {
617 FNIC_SCSI_DBG(KERN_DEBUG,
618 fnic->lport->host,
619 "Unexpected state %s while processing"
620 " reset cmpl\n", fnic_state_to_str(fnic->state));
621 atomic64_inc(&reset_stats->fw_reset_failures);
622 ret = -1;
623 }
624
625
626 if (fnic->remove_wait)
627 complete(fnic->remove_wait);
628
629
630
631
632
633 if (fnic->remove_wait || ret) {
634 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
635 skb_queue_purge(&fnic->tx_queue);
636 goto reset_cmpl_handler_end;
637 }
638
639 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
640
641 fnic_flush_tx(fnic);
642
643 reset_cmpl_handler_end:
644 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
645
646 return ret;
647}
648
649
650
651
652
653static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
654 struct fcpio_fw_req *desc)
655{
656 u8 type;
657 u8 hdr_status;
658 struct fcpio_tag tag;
659 int ret = 0;
660 unsigned long flags;
661
662 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
663
664
665 spin_lock_irqsave(&fnic->fnic_lock, flags);
666
667 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
668
669
670 if (!hdr_status) {
671 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
672 "flog reg succeeded\n");
673 fnic->state = FNIC_IN_FC_MODE;
674 } else {
675 FNIC_SCSI_DBG(KERN_DEBUG,
676 fnic->lport->host,
677 "fnic flogi reg :failed %s\n",
678 fnic_fcpio_status_to_str(hdr_status));
679 fnic->state = FNIC_IN_ETH_MODE;
680 ret = -1;
681 }
682 } else {
683 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
684 "Unexpected fnic state %s while"
685 " processing flogi reg completion\n",
686 fnic_state_to_str(fnic->state));
687 ret = -1;
688 }
689
690 if (!ret) {
691 if (fnic->stop_rx_link_events) {
692 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
693 goto reg_cmpl_handler_end;
694 }
695 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
696
697 fnic_flush_tx(fnic);
698 queue_work(fnic_event_queue, &fnic->frame_work);
699 } else {
700 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
701 }
702
703reg_cmpl_handler_end:
704 return ret;
705}
706
707static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
708 u16 request_out)
709{
710 if (wq->to_clean_index <= wq->to_use_index) {
711
712 if (request_out < wq->to_clean_index ||
713 request_out >= wq->to_use_index)
714 return 0;
715 } else {
716
717 if (request_out < wq->to_clean_index &&
718 request_out >= wq->to_use_index)
719 return 0;
720 }
721
722 return 1;
723}
724
725
726
727
728
729
730
731
732static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
733 unsigned int cq_index,
734 struct fcpio_fw_req *desc)
735{
736 struct vnic_wq_copy *wq;
737 u16 request_out = desc->u.ack.request_out;
738 unsigned long flags;
739 u64 *ox_id_tag = (u64 *)(void *)desc;
740
741
742 wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
743 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
744
745 fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
746 if (is_ack_index_in_range(wq, request_out)) {
747 fnic->fw_ack_index[0] = request_out;
748 fnic->fw_ack_recd[0] = 1;
749 } else
750 atomic64_inc(
751 &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
752
753 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
754 FNIC_TRACE(fnic_fcpio_ack_handler,
755 fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
756 ox_id_tag[4], ox_id_tag[5]);
757}
758
759
760
761
762
763static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
764 struct fcpio_fw_req *desc)
765{
766 u8 type;
767 u8 hdr_status;
768 struct fcpio_tag tag;
769 u32 id;
770 u64 xfer_len = 0;
771 struct fcpio_icmnd_cmpl *icmnd_cmpl;
772 struct fnic_io_req *io_req;
773 struct scsi_cmnd *sc;
774 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
775 unsigned long flags;
776 spinlock_t *io_lock;
777 u64 cmd_trace;
778 unsigned long start_time;
779
780
781 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
782 fcpio_tag_id_dec(&tag, &id);
783 icmnd_cmpl = &desc->u.icmnd_cmpl;
784
785 if (id >= fnic->fnic_max_tag_id) {
786 shost_printk(KERN_ERR, fnic->lport->host,
787 "Tag out of range tag %x hdr status = %s\n",
788 id, fnic_fcpio_status_to_str(hdr_status));
789 return;
790 }
791
792 sc = scsi_host_find_tag(fnic->lport->host, id);
793 WARN_ON_ONCE(!sc);
794 if (!sc) {
795 atomic64_inc(&fnic_stats->io_stats.sc_null);
796 shost_printk(KERN_ERR, fnic->lport->host,
797 "icmnd_cmpl sc is null - "
798 "hdr status = %s tag = 0x%x desc = 0x%p\n",
799 fnic_fcpio_status_to_str(hdr_status), id, desc);
800 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
801 fnic->lport->host->host_no, id,
802 ((u64)icmnd_cmpl->_resvd0[1] << 16 |
803 (u64)icmnd_cmpl->_resvd0[0]),
804 ((u64)hdr_status << 16 |
805 (u64)icmnd_cmpl->scsi_status << 8 |
806 (u64)icmnd_cmpl->flags), desc,
807 (u64)icmnd_cmpl->residual, 0);
808 return;
809 }
810
811 io_lock = fnic_io_lock_hash(fnic, sc);
812 spin_lock_irqsave(io_lock, flags);
813 io_req = (struct fnic_io_req *)CMD_SP(sc);
814 WARN_ON_ONCE(!io_req);
815 if (!io_req) {
816 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
817 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
818 spin_unlock_irqrestore(io_lock, flags);
819 shost_printk(KERN_ERR, fnic->lport->host,
820 "icmnd_cmpl io_req is null - "
821 "hdr status = %s tag = 0x%x sc 0x%p\n",
822 fnic_fcpio_status_to_str(hdr_status), id, sc);
823 return;
824 }
825 start_time = io_req->start_time;
826
827
828 io_req->io_completed = 1;
829
830
831
832
833
834 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
835 spin_unlock_irqrestore(io_lock, flags);
836 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
837 switch (hdr_status) {
838 case FCPIO_SUCCESS:
839 CMD_FLAGS(sc) |= FNIC_IO_DONE;
840 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
841 "icmnd_cmpl ABTS pending hdr status = %s "
842 "sc 0x%p scsi_status %x residual %d\n",
843 fnic_fcpio_status_to_str(hdr_status), sc,
844 icmnd_cmpl->scsi_status,
845 icmnd_cmpl->residual);
846 break;
847 case FCPIO_ABORTED:
848 CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
849 break;
850 default:
851 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
852 "icmnd_cmpl abts pending "
853 "hdr status = %s tag = 0x%x sc = 0x%p\n",
854 fnic_fcpio_status_to_str(hdr_status),
855 id, sc);
856 break;
857 }
858 return;
859 }
860
861
862 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
863
864 icmnd_cmpl = &desc->u.icmnd_cmpl;
865
866 switch (hdr_status) {
867 case FCPIO_SUCCESS:
868 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
869 xfer_len = scsi_bufflen(sc);
870 scsi_set_resid(sc, icmnd_cmpl->residual);
871
872 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
873 xfer_len -= icmnd_cmpl->residual;
874
875 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
876 atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
877 break;
878
879 case FCPIO_TIMEOUT:
880 atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
881 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
882 break;
883
884 case FCPIO_ABORTED:
885 atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
886 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
887 break;
888
889 case FCPIO_DATA_CNT_MISMATCH:
890 atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
891 scsi_set_resid(sc, icmnd_cmpl->residual);
892 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
893 break;
894
895 case FCPIO_OUT_OF_RESOURCE:
896 atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
897 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
898 break;
899
900 case FCPIO_IO_NOT_FOUND:
901 atomic64_inc(&fnic_stats->io_stats.io_not_found);
902 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
903 break;
904
905 case FCPIO_SGL_INVALID:
906 atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
907 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
908 break;
909
910 case FCPIO_FW_ERR:
911 atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
912 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
913 break;
914
915 case FCPIO_MSS_INVALID:
916 atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
917 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
918 break;
919
920 case FCPIO_INVALID_HEADER:
921 case FCPIO_INVALID_PARAM:
922 case FCPIO_REQ_NOT_SUPPORTED:
923 default:
924 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
925 fnic_fcpio_status_to_str(hdr_status));
926 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
927 break;
928 }
929
930 if (hdr_status != FCPIO_SUCCESS) {
931 atomic64_inc(&fnic_stats->io_stats.io_failures);
932 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
933 fnic_fcpio_status_to_str(hdr_status));
934 }
935
936 CMD_SP(sc) = NULL;
937 CMD_FLAGS(sc) |= FNIC_IO_DONE;
938
939 spin_unlock_irqrestore(io_lock, flags);
940
941 fnic_release_ioreq_buf(fnic, io_req, sc);
942
943 mempool_free(io_req, fnic->io_req_pool);
944
945 cmd_trace = ((u64)hdr_status << 56) |
946 (u64)icmnd_cmpl->scsi_status << 48 |
947 (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
948 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
949 (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
950
951 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
952 sc->device->host->host_no, id, sc,
953 ((u64)icmnd_cmpl->_resvd0[1] << 56 |
954 (u64)icmnd_cmpl->_resvd0[0] << 48 |
955 jiffies_to_msecs(jiffies - start_time)),
956 desc, cmd_trace,
957 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
958
959 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
960 fnic->lport->host_stats.fcp_input_requests++;
961 fnic->fcp_input_bytes += xfer_len;
962 } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
963 fnic->lport->host_stats.fcp_output_requests++;
964 fnic->fcp_output_bytes += xfer_len;
965 } else
966 fnic->lport->host_stats.fcp_control_requests++;
967
968 atomic64_dec(&fnic_stats->io_stats.active_ios);
969 if (atomic64_read(&fnic->io_cmpl_skip))
970 atomic64_dec(&fnic->io_cmpl_skip);
971 else
972 atomic64_inc(&fnic_stats->io_stats.io_completions);
973
974
975 if (sc->scsi_done)
976 sc->scsi_done(sc);
977}
978
979
980
981
982static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
983 struct fcpio_fw_req *desc)
984{
985 u8 type;
986 u8 hdr_status;
987 struct fcpio_tag tag;
988 u32 id;
989 struct scsi_cmnd *sc;
990 struct fnic_io_req *io_req;
991 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
992 struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
993 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
994 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
995 unsigned long flags;
996 spinlock_t *io_lock;
997 unsigned long start_time;
998
999 fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1000 fcpio_tag_id_dec(&tag, &id);
1001
1002 if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
1003 shost_printk(KERN_ERR, fnic->lport->host,
1004 "Tag out of range tag %x hdr status = %s\n",
1005 id, fnic_fcpio_status_to_str(hdr_status));
1006 return;
1007 }
1008
1009 sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1010 WARN_ON_ONCE(!sc);
1011 if (!sc) {
1012 atomic64_inc(&fnic_stats->io_stats.sc_null);
1013 shost_printk(KERN_ERR, fnic->lport->host,
1014 "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1015 fnic_fcpio_status_to_str(hdr_status), id);
1016 return;
1017 }
1018 io_lock = fnic_io_lock_hash(fnic, sc);
1019 spin_lock_irqsave(io_lock, flags);
1020 io_req = (struct fnic_io_req *)CMD_SP(sc);
1021 WARN_ON_ONCE(!io_req);
1022 if (!io_req) {
1023 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1024 spin_unlock_irqrestore(io_lock, flags);
1025 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1026 shost_printk(KERN_ERR, fnic->lport->host,
1027 "itmf_cmpl io_req is null - "
1028 "hdr status = %s tag = 0x%x sc 0x%p\n",
1029 fnic_fcpio_status_to_str(hdr_status), id, sc);
1030 return;
1031 }
1032 start_time = io_req->start_time;
1033
1034 if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1035
1036
1037 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1038 "dev reset abts cmpl recd. id %x status %s\n",
1039 id, fnic_fcpio_status_to_str(hdr_status));
1040 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1041 CMD_ABTS_STATUS(sc) = hdr_status;
1042 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1043 if (io_req->abts_done)
1044 complete(io_req->abts_done);
1045 spin_unlock_irqrestore(io_lock, flags);
1046 } else if (id & FNIC_TAG_ABORT) {
1047
1048 switch (hdr_status) {
1049 case FCPIO_SUCCESS:
1050 break;
1051 case FCPIO_TIMEOUT:
1052 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1053 atomic64_inc(&abts_stats->abort_fw_timeouts);
1054 else
1055 atomic64_inc(
1056 &term_stats->terminate_fw_timeouts);
1057 break;
1058 case FCPIO_IO_NOT_FOUND:
1059 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1060 atomic64_inc(&abts_stats->abort_io_not_found);
1061 else
1062 atomic64_inc(
1063 &term_stats->terminate_io_not_found);
1064 break;
1065 default:
1066 if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1067 atomic64_inc(&abts_stats->abort_failures);
1068 else
1069 atomic64_inc(
1070 &term_stats->terminate_failures);
1071 break;
1072 }
1073 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1074
1075 spin_unlock_irqrestore(io_lock, flags);
1076 return;
1077 }
1078 CMD_ABTS_STATUS(sc) = hdr_status;
1079 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
1080
1081 atomic64_dec(&fnic_stats->io_stats.active_ios);
1082 if (atomic64_read(&fnic->io_cmpl_skip))
1083 atomic64_dec(&fnic->io_cmpl_skip);
1084 else
1085 atomic64_inc(&fnic_stats->io_stats.io_completions);
1086
1087 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1088 atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1089
1090 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1091 "abts cmpl recd. id %d status %s\n",
1092 (int)(id & FNIC_TAG_MASK),
1093 fnic_fcpio_status_to_str(hdr_status));
1094
1095
1096
1097
1098
1099
1100 if (io_req->abts_done) {
1101 complete(io_req->abts_done);
1102 spin_unlock_irqrestore(io_lock, flags);
1103 } else {
1104 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1105 "abts cmpl, completing IO\n");
1106 CMD_SP(sc) = NULL;
1107 sc->result = (DID_ERROR << 16);
1108
1109 spin_unlock_irqrestore(io_lock, flags);
1110
1111 fnic_release_ioreq_buf(fnic, io_req, sc);
1112 mempool_free(io_req, fnic->io_req_pool);
1113 if (sc->scsi_done) {
1114 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1115 sc->device->host->host_no, id,
1116 sc,
1117 jiffies_to_msecs(jiffies - start_time),
1118 desc,
1119 (((u64)hdr_status << 40) |
1120 (u64)sc->cmnd[0] << 32 |
1121 (u64)sc->cmnd[2] << 24 |
1122 (u64)sc->cmnd[3] << 16 |
1123 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1124 (((u64)CMD_FLAGS(sc) << 32) |
1125 CMD_STATE(sc)));
1126 sc->scsi_done(sc);
1127 }
1128 }
1129
1130 } else if (id & FNIC_TAG_DEV_RST) {
1131
1132 CMD_LR_STATUS(sc) = hdr_status;
1133 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1134 spin_unlock_irqrestore(io_lock, flags);
1135 CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
1136 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1137 sc->device->host->host_no, id, sc,
1138 jiffies_to_msecs(jiffies - start_time),
1139 desc, 0,
1140 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1141 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1142 "Terminate pending "
1143 "dev reset cmpl recd. id %d status %s\n",
1144 (int)(id & FNIC_TAG_MASK),
1145 fnic_fcpio_status_to_str(hdr_status));
1146 return;
1147 }
1148 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1149
1150 spin_unlock_irqrestore(io_lock, flags);
1151 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1152 sc->device->host->host_no, id, sc,
1153 jiffies_to_msecs(jiffies - start_time),
1154 desc, 0,
1155 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1156 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1157 "dev reset cmpl recd after time out. "
1158 "id %d status %s\n",
1159 (int)(id & FNIC_TAG_MASK),
1160 fnic_fcpio_status_to_str(hdr_status));
1161 return;
1162 }
1163 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
1164 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1165 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1166 "dev reset cmpl recd. id %d status %s\n",
1167 (int)(id & FNIC_TAG_MASK),
1168 fnic_fcpio_status_to_str(hdr_status));
1169 if (io_req->dr_done)
1170 complete(io_req->dr_done);
1171 spin_unlock_irqrestore(io_lock, flags);
1172
1173 } else {
1174 shost_printk(KERN_ERR, fnic->lport->host,
1175 "Unexpected itmf io state %s tag %x\n",
1176 fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1177 spin_unlock_irqrestore(io_lock, flags);
1178 }
1179
1180}
1181
1182
1183
1184
1185
1186static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1187 unsigned int cq_index,
1188 struct fcpio_fw_req *desc)
1189{
1190 struct fnic *fnic = vnic_dev_priv(vdev);
1191
1192 switch (desc->hdr.type) {
1193 case FCPIO_ICMND_CMPL:
1194 case FCPIO_ITMF_CMPL:
1195 case FCPIO_FLOGI_REG_CMPL:
1196 case FCPIO_FLOGI_FIP_REG_CMPL:
1197 case FCPIO_RESET_CMPL:
1198 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1199 break;
1200 default:
1201 break;
1202 }
1203
1204 switch (desc->hdr.type) {
1205 case FCPIO_ACK:
1206 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1207 break;
1208
1209 case FCPIO_ICMND_CMPL:
1210 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1211 break;
1212
1213 case FCPIO_ITMF_CMPL:
1214 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1215 break;
1216
1217 case FCPIO_FLOGI_REG_CMPL:
1218 case FCPIO_FLOGI_FIP_REG_CMPL:
1219 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
1220 break;
1221
1222 case FCPIO_RESET_CMPL:
1223 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
1224 break;
1225
1226 default:
1227 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1228 "firmware completion type %d\n",
1229 desc->hdr.type);
1230 break;
1231 }
1232
1233 return 0;
1234}
1235
1236
1237
1238
1239
1240int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1241{
1242 unsigned int wq_work_done = 0;
1243 unsigned int i, cq_index;
1244 unsigned int cur_work_done;
1245
1246 for (i = 0; i < fnic->wq_copy_count; i++) {
1247 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1248 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1249 fnic_fcpio_cmpl_handler,
1250 copy_work_to_do);
1251 wq_work_done += cur_work_done;
1252 }
1253 return wq_work_done;
1254}
1255
1256static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1257{
1258 int i;
1259 struct fnic_io_req *io_req;
1260 unsigned long flags = 0;
1261 struct scsi_cmnd *sc;
1262 spinlock_t *io_lock;
1263 unsigned long start_time = 0;
1264 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1265
1266 for (i = 0; i < fnic->fnic_max_tag_id; i++) {
1267 if (i == exclude_id)
1268 continue;
1269
1270 io_lock = fnic_io_lock_tag(fnic, i);
1271 spin_lock_irqsave(io_lock, flags);
1272 sc = scsi_host_find_tag(fnic->lport->host, i);
1273 if (!sc) {
1274 spin_unlock_irqrestore(io_lock, flags);
1275 continue;
1276 }
1277
1278 io_req = (struct fnic_io_req *)CMD_SP(sc);
1279 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1280 !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1281
1282
1283
1284
1285 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1286 if (io_req && io_req->dr_done)
1287 complete(io_req->dr_done);
1288 else if (io_req && io_req->abts_done)
1289 complete(io_req->abts_done);
1290 spin_unlock_irqrestore(io_lock, flags);
1291 continue;
1292 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1293 spin_unlock_irqrestore(io_lock, flags);
1294 continue;
1295 }
1296 if (!io_req) {
1297 spin_unlock_irqrestore(io_lock, flags);
1298 goto cleanup_scsi_cmd;
1299 }
1300
1301 CMD_SP(sc) = NULL;
1302
1303 spin_unlock_irqrestore(io_lock, flags);
1304
1305
1306
1307
1308
1309 start_time = io_req->start_time;
1310 fnic_release_ioreq_buf(fnic, io_req, sc);
1311 mempool_free(io_req, fnic->io_req_pool);
1312
1313cleanup_scsi_cmd:
1314 sc->result = DID_TRANSPORT_DISRUPTED << 16;
1315 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "fnic_cleanup_io:"
1316 " DID_TRANSPORT_DISRUPTED\n");
1317
1318 if (atomic64_read(&fnic->io_cmpl_skip))
1319 atomic64_dec(&fnic->io_cmpl_skip);
1320 else
1321 atomic64_inc(&fnic_stats->io_stats.io_completions);
1322
1323
1324 if (sc->scsi_done) {
1325 FNIC_TRACE(fnic_cleanup_io,
1326 sc->device->host->host_no, i, sc,
1327 jiffies_to_msecs(jiffies - start_time),
1328 0, ((u64)sc->cmnd[0] << 32 |
1329 (u64)sc->cmnd[2] << 24 |
1330 (u64)sc->cmnd[3] << 16 |
1331 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1332 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1333
1334 sc->scsi_done(sc);
1335 }
1336 }
1337}
1338
1339void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1340 struct fcpio_host_req *desc)
1341{
1342 u32 id;
1343 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1344 struct fnic_io_req *io_req;
1345 struct scsi_cmnd *sc;
1346 unsigned long flags;
1347 spinlock_t *io_lock;
1348 unsigned long start_time = 0;
1349
1350
1351 fcpio_tag_id_dec(&desc->hdr.tag, &id);
1352 id &= FNIC_TAG_MASK;
1353
1354 if (id >= fnic->fnic_max_tag_id)
1355 return;
1356
1357 sc = scsi_host_find_tag(fnic->lport->host, id);
1358 if (!sc)
1359 return;
1360
1361 io_lock = fnic_io_lock_hash(fnic, sc);
1362 spin_lock_irqsave(io_lock, flags);
1363
1364
1365 io_req = (struct fnic_io_req *)CMD_SP(sc);
1366
1367
1368
1369 if (!io_req) {
1370 spin_unlock_irqrestore(io_lock, flags);
1371 goto wq_copy_cleanup_scsi_cmd;
1372 }
1373
1374 CMD_SP(sc) = NULL;
1375
1376 spin_unlock_irqrestore(io_lock, flags);
1377
1378 start_time = io_req->start_time;
1379 fnic_release_ioreq_buf(fnic, io_req, sc);
1380 mempool_free(io_req, fnic->io_req_pool);
1381
1382wq_copy_cleanup_scsi_cmd:
1383 sc->result = DID_NO_CONNECT << 16;
1384 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1385 " DID_NO_CONNECT\n");
1386
1387 if (sc->scsi_done) {
1388 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1389 sc->device->host->host_no, id, sc,
1390 jiffies_to_msecs(jiffies - start_time),
1391 0, ((u64)sc->cmnd[0] << 32 |
1392 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1393 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1394 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1395
1396 sc->scsi_done(sc);
1397 }
1398}
1399
1400static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1401 u32 task_req, u8 *fc_lun,
1402 struct fnic_io_req *io_req)
1403{
1404 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1405 struct Scsi_Host *host = fnic->lport->host;
1406 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1407 unsigned long flags;
1408
1409 spin_lock_irqsave(host->host_lock, flags);
1410 if (unlikely(fnic_chk_state_flags_locked(fnic,
1411 FNIC_FLAGS_IO_BLOCKED))) {
1412 spin_unlock_irqrestore(host->host_lock, flags);
1413 return 1;
1414 } else
1415 atomic_inc(&fnic->in_flight);
1416 spin_unlock_irqrestore(host->host_lock, flags);
1417
1418 spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1419
1420 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1421 free_wq_copy_descs(fnic, wq);
1422
1423 if (!vnic_wq_copy_desc_avail(wq)) {
1424 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1425 atomic_dec(&fnic->in_flight);
1426 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1427 "fnic_queue_abort_io_req: failure: no descriptors\n");
1428 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
1429 return 1;
1430 }
1431 fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1432 0, task_req, tag, fc_lun, io_req->port_id,
1433 fnic->config.ra_tov, fnic->config.ed_tov);
1434
1435 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1436 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1437 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1438 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1439 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1440
1441 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1442 atomic_dec(&fnic->in_flight);
1443
1444 return 0;
1445}
1446
1447static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1448{
1449 int tag;
1450 int abt_tag;
1451 int term_cnt = 0;
1452 struct fnic_io_req *io_req;
1453 spinlock_t *io_lock;
1454 unsigned long flags;
1455 struct scsi_cmnd *sc;
1456 struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1457 struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1458 struct scsi_lun fc_lun;
1459 enum fnic_ioreq_state old_ioreq_state;
1460
1461 FNIC_SCSI_DBG(KERN_DEBUG,
1462 fnic->lport->host,
1463 "fnic_rport_exch_reset called portid 0x%06x\n",
1464 port_id);
1465
1466 if (fnic->in_remove)
1467 return;
1468
1469 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1470 abt_tag = tag;
1471 io_lock = fnic_io_lock_tag(fnic, tag);
1472 spin_lock_irqsave(io_lock, flags);
1473 sc = scsi_host_find_tag(fnic->lport->host, tag);
1474 if (!sc) {
1475 spin_unlock_irqrestore(io_lock, flags);
1476 continue;
1477 }
1478
1479 io_req = (struct fnic_io_req *)CMD_SP(sc);
1480
1481 if (!io_req || io_req->port_id != port_id) {
1482 spin_unlock_irqrestore(io_lock, flags);
1483 continue;
1484 }
1485
1486 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1487 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1488 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1489 "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1490 sc);
1491 spin_unlock_irqrestore(io_lock, flags);
1492 continue;
1493 }
1494
1495
1496
1497
1498
1499 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1500 spin_unlock_irqrestore(io_lock, flags);
1501 continue;
1502 }
1503 if (io_req->abts_done) {
1504 shost_printk(KERN_ERR, fnic->lport->host,
1505 "fnic_rport_exch_reset: io_req->abts_done is set "
1506 "state is %s\n",
1507 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1508 }
1509
1510 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1511 shost_printk(KERN_ERR, fnic->lport->host,
1512 "rport_exch_reset "
1513 "IO not yet issued %p tag 0x%x flags "
1514 "%x state %d\n",
1515 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1516 }
1517 old_ioreq_state = CMD_STATE(sc);
1518 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1519 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1520 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1521 atomic64_inc(&reset_stats->device_reset_terminates);
1522 abt_tag = (tag | FNIC_TAG_DEV_RST);
1523 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1524 "fnic_rport_exch_reset dev rst sc 0x%p\n",
1525 sc);
1526 }
1527
1528 BUG_ON(io_req->abts_done);
1529
1530 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1531 "fnic_rport_reset_exch: Issuing abts\n");
1532
1533 spin_unlock_irqrestore(io_lock, flags);
1534
1535
1536 int_to_scsilun(sc->device->lun, &fc_lun);
1537
1538 if (fnic_queue_abort_io_req(fnic, abt_tag,
1539 FCPIO_ITMF_ABT_TASK_TERM,
1540 fc_lun.scsi_lun, io_req)) {
1541
1542
1543
1544
1545
1546
1547 spin_lock_irqsave(io_lock, flags);
1548 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1549 CMD_STATE(sc) = old_ioreq_state;
1550 spin_unlock_irqrestore(io_lock, flags);
1551 } else {
1552 spin_lock_irqsave(io_lock, flags);
1553 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1554 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1555 else
1556 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1557 spin_unlock_irqrestore(io_lock, flags);
1558 atomic64_inc(&term_stats->terminates);
1559 term_cnt++;
1560 }
1561 }
1562 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1563 atomic64_set(&term_stats->max_terminates, term_cnt);
1564
1565}
1566
1567void fnic_terminate_rport_io(struct fc_rport *rport)
1568{
1569 int tag;
1570 int abt_tag;
1571 int term_cnt = 0;
1572 struct fnic_io_req *io_req;
1573 spinlock_t *io_lock;
1574 unsigned long flags;
1575 struct scsi_cmnd *sc;
1576 struct scsi_lun fc_lun;
1577 struct fc_rport_libfc_priv *rdata;
1578 struct fc_lport *lport;
1579 struct fnic *fnic;
1580 struct fc_rport *cmd_rport;
1581 struct reset_stats *reset_stats;
1582 struct terminate_stats *term_stats;
1583 enum fnic_ioreq_state old_ioreq_state;
1584
1585 if (!rport) {
1586 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1587 return;
1588 }
1589 rdata = rport->dd_data;
1590
1591 if (!rdata) {
1592 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1593 return;
1594 }
1595 lport = rdata->local_port;
1596
1597 if (!lport) {
1598 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1599 return;
1600 }
1601 fnic = lport_priv(lport);
1602 FNIC_SCSI_DBG(KERN_DEBUG,
1603 fnic->lport->host, "fnic_terminate_rport_io called"
1604 " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1605 rport->port_name, rport->node_name, rport,
1606 rport->port_id);
1607
1608 if (fnic->in_remove)
1609 return;
1610
1611 reset_stats = &fnic->fnic_stats.reset_stats;
1612 term_stats = &fnic->fnic_stats.term_stats;
1613
1614 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1615 abt_tag = tag;
1616 io_lock = fnic_io_lock_tag(fnic, tag);
1617 spin_lock_irqsave(io_lock, flags);
1618 sc = scsi_host_find_tag(fnic->lport->host, tag);
1619 if (!sc) {
1620 spin_unlock_irqrestore(io_lock, flags);
1621 continue;
1622 }
1623
1624 cmd_rport = starget_to_rport(scsi_target(sc->device));
1625 if (rport != cmd_rport) {
1626 spin_unlock_irqrestore(io_lock, flags);
1627 continue;
1628 }
1629
1630 io_req = (struct fnic_io_req *)CMD_SP(sc);
1631
1632 if (!io_req || rport != cmd_rport) {
1633 spin_unlock_irqrestore(io_lock, flags);
1634 continue;
1635 }
1636
1637 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1638 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1639 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1640 "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1641 sc);
1642 spin_unlock_irqrestore(io_lock, flags);
1643 continue;
1644 }
1645
1646
1647
1648
1649 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1650 spin_unlock_irqrestore(io_lock, flags);
1651 continue;
1652 }
1653 if (io_req->abts_done) {
1654 shost_printk(KERN_ERR, fnic->lport->host,
1655 "fnic_terminate_rport_io: io_req->abts_done is set "
1656 "state is %s\n",
1657 fnic_ioreq_state_to_str(CMD_STATE(sc)));
1658 }
1659 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1660 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1661 "fnic_terminate_rport_io "
1662 "IO not yet issued %p tag 0x%x flags "
1663 "%x state %d\n",
1664 sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1665 }
1666 old_ioreq_state = CMD_STATE(sc);
1667 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1668 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1669 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1670 atomic64_inc(&reset_stats->device_reset_terminates);
1671 abt_tag = (tag | FNIC_TAG_DEV_RST);
1672 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1673 "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1674 }
1675
1676 BUG_ON(io_req->abts_done);
1677
1678 FNIC_SCSI_DBG(KERN_DEBUG,
1679 fnic->lport->host,
1680 "fnic_terminate_rport_io: Issuing abts\n");
1681
1682 spin_unlock_irqrestore(io_lock, flags);
1683
1684
1685 int_to_scsilun(sc->device->lun, &fc_lun);
1686
1687 if (fnic_queue_abort_io_req(fnic, abt_tag,
1688 FCPIO_ITMF_ABT_TASK_TERM,
1689 fc_lun.scsi_lun, io_req)) {
1690
1691
1692
1693
1694
1695
1696 spin_lock_irqsave(io_lock, flags);
1697 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1698 CMD_STATE(sc) = old_ioreq_state;
1699 spin_unlock_irqrestore(io_lock, flags);
1700 } else {
1701 spin_lock_irqsave(io_lock, flags);
1702 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1703 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1704 else
1705 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1706 spin_unlock_irqrestore(io_lock, flags);
1707 atomic64_inc(&term_stats->terminates);
1708 term_cnt++;
1709 }
1710 }
1711 if (term_cnt > atomic64_read(&term_stats->max_terminates))
1712 atomic64_set(&term_stats->max_terminates, term_cnt);
1713
1714}
1715
1716
1717
1718
1719
1720
1721int fnic_abort_cmd(struct scsi_cmnd *sc)
1722{
1723 struct fc_lport *lp;
1724 struct fnic *fnic;
1725 struct fnic_io_req *io_req = NULL;
1726 struct fc_rport *rport;
1727 spinlock_t *io_lock;
1728 unsigned long flags;
1729 unsigned long start_time = 0;
1730 int ret = SUCCESS;
1731 u32 task_req = 0;
1732 struct scsi_lun fc_lun;
1733 struct fnic_stats *fnic_stats;
1734 struct abort_stats *abts_stats;
1735 struct terminate_stats *term_stats;
1736 int tag;
1737 DECLARE_COMPLETION_ONSTACK(tm_done);
1738
1739
1740 fc_block_scsi_eh(sc);
1741
1742
1743 lp = shost_priv(sc->device->host);
1744
1745 fnic = lport_priv(lp);
1746 fnic_stats = &fnic->fnic_stats;
1747 abts_stats = &fnic->fnic_stats.abts_stats;
1748 term_stats = &fnic->fnic_stats.term_stats;
1749
1750 rport = starget_to_rport(scsi_target(sc->device));
1751 tag = sc->request->tag;
1752 FNIC_SCSI_DBG(KERN_DEBUG,
1753 fnic->lport->host,
1754 "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %x flags %x\n",
1755 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1756
1757 CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1758
1759 if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1760 ret = FAILED;
1761 goto fnic_abort_cmd_end;
1762 }
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776 io_lock = fnic_io_lock_hash(fnic, sc);
1777 spin_lock_irqsave(io_lock, flags);
1778 io_req = (struct fnic_io_req *)CMD_SP(sc);
1779 if (!io_req) {
1780 spin_unlock_irqrestore(io_lock, flags);
1781 goto fnic_abort_cmd_end;
1782 }
1783
1784 io_req->abts_done = &tm_done;
1785
1786 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1787 spin_unlock_irqrestore(io_lock, flags);
1788 goto wait_pending;
1789 }
1790
1791
1792
1793
1794
1795
1796 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1797 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1798
1799 spin_unlock_irqrestore(io_lock, flags);
1800
1801
1802
1803
1804
1805
1806 if (fc_remote_port_chkready(rport) == 0)
1807 task_req = FCPIO_ITMF_ABT_TASK;
1808 else {
1809 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
1810 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1811 }
1812
1813
1814 int_to_scsilun(sc->device->lun, &fc_lun);
1815
1816 if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1817 fc_lun.scsi_lun, io_req)) {
1818 spin_lock_irqsave(io_lock, flags);
1819 io_req = (struct fnic_io_req *)CMD_SP(sc);
1820 if (io_req)
1821 io_req->abts_done = NULL;
1822 spin_unlock_irqrestore(io_lock, flags);
1823 ret = FAILED;
1824 goto fnic_abort_cmd_end;
1825 }
1826 if (task_req == FCPIO_ITMF_ABT_TASK) {
1827 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
1828 atomic64_inc(&fnic_stats->abts_stats.aborts);
1829 } else {
1830 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
1831 atomic64_inc(&fnic_stats->term_stats.terminates);
1832 }
1833
1834
1835
1836
1837
1838
1839 wait_pending:
1840 wait_for_completion_timeout(&tm_done,
1841 msecs_to_jiffies
1842 (2 * fnic->config.ra_tov +
1843 fnic->config.ed_tov));
1844
1845
1846 spin_lock_irqsave(io_lock, flags);
1847
1848 io_req = (struct fnic_io_req *)CMD_SP(sc);
1849 if (!io_req) {
1850 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1851 spin_unlock_irqrestore(io_lock, flags);
1852 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1853 ret = FAILED;
1854 goto fnic_abort_cmd_end;
1855 }
1856 io_req->abts_done = NULL;
1857
1858
1859 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
1860 spin_unlock_irqrestore(io_lock, flags);
1861 if (task_req == FCPIO_ITMF_ABT_TASK) {
1862 FNIC_SCSI_DBG(KERN_INFO,
1863 fnic->lport->host, "Abort Driver Timeout\n");
1864 atomic64_inc(&abts_stats->abort_drv_timeouts);
1865 } else {
1866 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1867 "Terminate Driver Timeout\n");
1868 atomic64_inc(&term_stats->terminate_drv_timeouts);
1869 }
1870 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
1871 ret = FAILED;
1872 goto fnic_abort_cmd_end;
1873 }
1874
1875 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1876
1877
1878
1879
1880
1881 if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1882 ret = FAILED;
1883
1884 CMD_SP(sc) = NULL;
1885
1886 spin_unlock_irqrestore(io_lock, flags);
1887
1888 start_time = io_req->start_time;
1889 fnic_release_ioreq_buf(fnic, io_req, sc);
1890 mempool_free(io_req, fnic->io_req_pool);
1891
1892fnic_abort_cmd_end:
1893 FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1894 sc->request->tag, sc,
1895 jiffies_to_msecs(jiffies - start_time),
1896 0, ((u64)sc->cmnd[0] << 32 |
1897 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1898 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1899 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1900
1901 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1902 "Returning from abort cmd type %x %s\n", task_req,
1903 (ret == SUCCESS) ?
1904 "SUCCESS" : "FAILED");
1905 return ret;
1906}
1907
1908static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1909 struct scsi_cmnd *sc,
1910 struct fnic_io_req *io_req)
1911{
1912 struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1913 struct Scsi_Host *host = fnic->lport->host;
1914 struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1915 struct scsi_lun fc_lun;
1916 int ret = 0;
1917 unsigned long intr_flags;
1918
1919 spin_lock_irqsave(host->host_lock, intr_flags);
1920 if (unlikely(fnic_chk_state_flags_locked(fnic,
1921 FNIC_FLAGS_IO_BLOCKED))) {
1922 spin_unlock_irqrestore(host->host_lock, intr_flags);
1923 return FAILED;
1924 } else
1925 atomic_inc(&fnic->in_flight);
1926 spin_unlock_irqrestore(host->host_lock, intr_flags);
1927
1928 spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1929
1930 if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1931 free_wq_copy_descs(fnic, wq);
1932
1933 if (!vnic_wq_copy_desc_avail(wq)) {
1934 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1935 "queue_dr_io_req failure - no descriptors\n");
1936 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
1937 ret = -EAGAIN;
1938 goto lr_io_req_end;
1939 }
1940
1941
1942 int_to_scsilun(sc->device->lun, &fc_lun);
1943
1944 fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1945 0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1946 fc_lun.scsi_lun, io_req->port_id,
1947 fnic->config.ra_tov, fnic->config.ed_tov);
1948
1949 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1950 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1951 atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1952 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1953 atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1954
1955lr_io_req_end:
1956 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
1957 atomic_dec(&fnic->in_flight);
1958
1959 return ret;
1960}
1961
1962
1963
1964
1965
1966
1967
1968static int fnic_clean_pending_aborts(struct fnic *fnic,
1969 struct scsi_cmnd *lr_sc)
1970{
1971 int tag, abt_tag;
1972 struct fnic_io_req *io_req;
1973 spinlock_t *io_lock;
1974 unsigned long flags;
1975 int ret = 0;
1976 struct scsi_cmnd *sc;
1977 struct scsi_lun fc_lun;
1978 struct scsi_device *lun_dev = lr_sc->device;
1979 DECLARE_COMPLETION_ONSTACK(tm_done);
1980 enum fnic_ioreq_state old_ioreq_state;
1981
1982 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1983 io_lock = fnic_io_lock_tag(fnic, tag);
1984 spin_lock_irqsave(io_lock, flags);
1985 sc = scsi_host_find_tag(fnic->lport->host, tag);
1986
1987
1988
1989
1990 if (!sc || sc == lr_sc || sc->device != lun_dev) {
1991 spin_unlock_irqrestore(io_lock, flags);
1992 continue;
1993 }
1994
1995 io_req = (struct fnic_io_req *)CMD_SP(sc);
1996
1997 if (!io_req || sc->device != lun_dev) {
1998 spin_unlock_irqrestore(io_lock, flags);
1999 continue;
2000 }
2001
2002
2003
2004
2005
2006 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2007 "Found IO in %s on lun\n",
2008 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2009
2010 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2011 spin_unlock_irqrestore(io_lock, flags);
2012 continue;
2013 }
2014 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
2015 (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
2016 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2017 "%s dev rst not pending sc 0x%p\n", __func__,
2018 sc);
2019 spin_unlock_irqrestore(io_lock, flags);
2020 continue;
2021 }
2022
2023 if (io_req->abts_done)
2024 shost_printk(KERN_ERR, fnic->lport->host,
2025 "%s: io_req->abts_done is set state is %s\n",
2026 __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
2027 old_ioreq_state = CMD_STATE(sc);
2028
2029
2030
2031
2032
2033
2034
2035 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2036
2037 BUG_ON(io_req->abts_done);
2038
2039 abt_tag = tag;
2040 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2041 abt_tag |= FNIC_TAG_DEV_RST;
2042 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2043 "%s: dev rst sc 0x%p\n", __func__, sc);
2044 }
2045
2046 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2047 io_req->abts_done = &tm_done;
2048 spin_unlock_irqrestore(io_lock, flags);
2049
2050
2051 int_to_scsilun(sc->device->lun, &fc_lun);
2052
2053 if (fnic_queue_abort_io_req(fnic, abt_tag,
2054 FCPIO_ITMF_ABT_TASK_TERM,
2055 fc_lun.scsi_lun, io_req)) {
2056 spin_lock_irqsave(io_lock, flags);
2057 io_req = (struct fnic_io_req *)CMD_SP(sc);
2058 if (io_req)
2059 io_req->abts_done = NULL;
2060 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2061 CMD_STATE(sc) = old_ioreq_state;
2062 spin_unlock_irqrestore(io_lock, flags);
2063 ret = 1;
2064 goto clean_pending_aborts_end;
2065 } else {
2066 spin_lock_irqsave(io_lock, flags);
2067 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2068 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2069 spin_unlock_irqrestore(io_lock, flags);
2070 }
2071 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
2072
2073 wait_for_completion_timeout(&tm_done,
2074 msecs_to_jiffies
2075 (fnic->config.ed_tov));
2076
2077
2078 spin_lock_irqsave(io_lock, flags);
2079 io_req = (struct fnic_io_req *)CMD_SP(sc);
2080 if (!io_req) {
2081 spin_unlock_irqrestore(io_lock, flags);
2082 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
2083 continue;
2084 }
2085
2086 io_req->abts_done = NULL;
2087
2088
2089 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
2090 spin_unlock_irqrestore(io_lock, flags);
2091 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
2092 ret = 1;
2093 goto clean_pending_aborts_end;
2094 }
2095 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
2096 CMD_SP(sc) = NULL;
2097 spin_unlock_irqrestore(io_lock, flags);
2098
2099 fnic_release_ioreq_buf(fnic, io_req, sc);
2100 mempool_free(io_req, fnic->io_req_pool);
2101 }
2102
2103 schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2104
2105
2106 if (fnic_is_abts_pending(fnic, lr_sc))
2107 ret = FAILED;
2108
2109clean_pending_aborts_end:
2110 return ret;
2111}
2112
2113
2114
2115
2116
2117static inline int
2118fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2119{
2120 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2121 int tag, ret = SCSI_NO_TAG;
2122
2123 BUG_ON(!bqt);
2124 if (!bqt) {
2125 pr_err("Tags are not supported\n");
2126 goto end;
2127 }
2128
2129 do {
2130 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2131 if (tag >= bqt->max_depth) {
2132 pr_err("Tag allocation failure\n");
2133 goto end;
2134 }
2135 } while (test_and_set_bit(tag, bqt->tag_map));
2136
2137 bqt->tag_index[tag] = sc->request;
2138 sc->request->tag = tag;
2139 sc->tag = tag;
2140 if (!sc->request->special)
2141 sc->request->special = sc;
2142
2143 ret = tag;
2144
2145end:
2146 return ret;
2147}
2148
2149
2150
2151
2152
2153static inline void
2154fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2155{
2156 struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2157 int tag = sc->request->tag;
2158
2159 if (tag == SCSI_NO_TAG)
2160 return;
2161
2162 BUG_ON(!bqt || !bqt->tag_index[tag]);
2163 if (!bqt)
2164 return;
2165
2166 bqt->tag_index[tag] = NULL;
2167 clear_bit(tag, bqt->tag_map);
2168
2169 return;
2170}
2171
2172
2173
2174
2175
2176
2177int fnic_device_reset(struct scsi_cmnd *sc)
2178{
2179 struct fc_lport *lp;
2180 struct fnic *fnic;
2181 struct fnic_io_req *io_req = NULL;
2182 struct fc_rport *rport;
2183 int status;
2184 int ret = FAILED;
2185 spinlock_t *io_lock;
2186 unsigned long flags;
2187 unsigned long start_time = 0;
2188 struct scsi_lun fc_lun;
2189 struct fnic_stats *fnic_stats;
2190 struct reset_stats *reset_stats;
2191 int tag = 0;
2192 DECLARE_COMPLETION_ONSTACK(tm_done);
2193 int tag_gen_flag = 0;
2194
2195
2196 fc_block_scsi_eh(sc);
2197
2198
2199 lp = shost_priv(sc->device->host);
2200
2201 fnic = lport_priv(lp);
2202 fnic_stats = &fnic->fnic_stats;
2203 reset_stats = &fnic->fnic_stats.reset_stats;
2204
2205 atomic64_inc(&reset_stats->device_resets);
2206
2207 rport = starget_to_rport(scsi_target(sc->device));
2208 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2209 "Device reset called FCID 0x%x, LUN 0x%x sc 0x%p\n",
2210 rport->port_id, sc->device->lun, sc);
2211
2212 if (lp->state != LPORT_ST_READY || !(lp->link_up))
2213 goto fnic_device_reset_end;
2214
2215
2216 if (fc_remote_port_chkready(rport)) {
2217 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
2218 goto fnic_device_reset_end;
2219 }
2220
2221 CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
2222
2223
2224 tag = sc->request->tag;
2225 if (unlikely(tag < 0)) {
2226 tag = fnic_scsi_host_start_tag(fnic, sc);
2227 if (unlikely(tag == SCSI_NO_TAG))
2228 goto fnic_device_reset_end;
2229 tag_gen_flag = 1;
2230 }
2231 io_lock = fnic_io_lock_hash(fnic, sc);
2232 spin_lock_irqsave(io_lock, flags);
2233 io_req = (struct fnic_io_req *)CMD_SP(sc);
2234
2235
2236
2237
2238
2239 if (!io_req) {
2240 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2241 if (!io_req) {
2242 spin_unlock_irqrestore(io_lock, flags);
2243 goto fnic_device_reset_end;
2244 }
2245 memset(io_req, 0, sizeof(*io_req));
2246 io_req->port_id = rport->port_id;
2247 CMD_SP(sc) = (char *)io_req;
2248 }
2249 io_req->dr_done = &tm_done;
2250 CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2251 CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2252 spin_unlock_irqrestore(io_lock, flags);
2253
2254 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
2255
2256
2257
2258
2259
2260 if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2261 spin_lock_irqsave(io_lock, flags);
2262 io_req = (struct fnic_io_req *)CMD_SP(sc);
2263 if (io_req)
2264 io_req->dr_done = NULL;
2265 goto fnic_device_reset_clean;
2266 }
2267 spin_lock_irqsave(io_lock, flags);
2268 CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
2269 spin_unlock_irqrestore(io_lock, flags);
2270
2271
2272
2273
2274
2275 wait_for_completion_timeout(&tm_done,
2276 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2277
2278 spin_lock_irqsave(io_lock, flags);
2279 io_req = (struct fnic_io_req *)CMD_SP(sc);
2280 if (!io_req) {
2281 spin_unlock_irqrestore(io_lock, flags);
2282 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2283 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
2284 goto fnic_device_reset_end;
2285 }
2286 io_req->dr_done = NULL;
2287
2288 status = CMD_LR_STATUS(sc);
2289
2290
2291
2292
2293
2294 if (status == FCPIO_INVALID_CODE) {
2295 atomic64_inc(&reset_stats->device_reset_timeouts);
2296 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2297 "Device reset timed out\n");
2298 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2299 spin_unlock_irqrestore(io_lock, flags);
2300 int_to_scsilun(sc->device->lun, &fc_lun);
2301
2302
2303
2304
2305 while (1) {
2306 spin_lock_irqsave(io_lock, flags);
2307 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2308 spin_unlock_irqrestore(io_lock, flags);
2309 break;
2310 }
2311 spin_unlock_irqrestore(io_lock, flags);
2312 if (fnic_queue_abort_io_req(fnic,
2313 tag | FNIC_TAG_DEV_RST,
2314 FCPIO_ITMF_ABT_TASK_TERM,
2315 fc_lun.scsi_lun, io_req)) {
2316 wait_for_completion_timeout(&tm_done,
2317 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2318 } else {
2319 spin_lock_irqsave(io_lock, flags);
2320 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2321 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2322 io_req->abts_done = &tm_done;
2323 spin_unlock_irqrestore(io_lock, flags);
2324 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2325 "Abort and terminate issued on Device reset "
2326 "tag 0x%x sc 0x%p\n", tag, sc);
2327 break;
2328 }
2329 }
2330 while (1) {
2331 spin_lock_irqsave(io_lock, flags);
2332 if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2333 spin_unlock_irqrestore(io_lock, flags);
2334 wait_for_completion_timeout(&tm_done,
2335 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2336 break;
2337 } else {
2338 io_req = (struct fnic_io_req *)CMD_SP(sc);
2339 io_req->abts_done = NULL;
2340 goto fnic_device_reset_clean;
2341 }
2342 }
2343 } else {
2344 spin_unlock_irqrestore(io_lock, flags);
2345 }
2346
2347
2348 if (status != FCPIO_SUCCESS) {
2349 spin_lock_irqsave(io_lock, flags);
2350 FNIC_SCSI_DBG(KERN_DEBUG,
2351 fnic->lport->host,
2352 "Device reset completed - failed\n");
2353 io_req = (struct fnic_io_req *)CMD_SP(sc);
2354 goto fnic_device_reset_clean;
2355 }
2356
2357
2358
2359
2360
2361
2362
2363
2364 if (fnic_clean_pending_aborts(fnic, sc)) {
2365 spin_lock_irqsave(io_lock, flags);
2366 io_req = (struct fnic_io_req *)CMD_SP(sc);
2367 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2368 "Device reset failed"
2369 " since could not abort all IOs\n");
2370 goto fnic_device_reset_clean;
2371 }
2372
2373
2374 spin_lock_irqsave(io_lock, flags);
2375 io_req = (struct fnic_io_req *)CMD_SP(sc);
2376 if (io_req)
2377
2378 ret = SUCCESS;
2379
2380fnic_device_reset_clean:
2381 if (io_req)
2382 CMD_SP(sc) = NULL;
2383
2384 spin_unlock_irqrestore(io_lock, flags);
2385
2386 if (io_req) {
2387 start_time = io_req->start_time;
2388 fnic_release_ioreq_buf(fnic, io_req, sc);
2389 mempool_free(io_req, fnic->io_req_pool);
2390 }
2391
2392fnic_device_reset_end:
2393 FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2394 sc->request->tag, sc,
2395 jiffies_to_msecs(jiffies - start_time),
2396 0, ((u64)sc->cmnd[0] << 32 |
2397 (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2398 (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2399 (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2400
2401
2402 if (unlikely(tag_gen_flag))
2403 fnic_scsi_host_end_tag(fnic, sc);
2404
2405 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2406 "Returning from device reset %s\n",
2407 (ret == SUCCESS) ?
2408 "SUCCESS" : "FAILED");
2409
2410 if (ret == FAILED)
2411 atomic64_inc(&reset_stats->device_reset_failures);
2412
2413 return ret;
2414}
2415
2416
2417int fnic_reset(struct Scsi_Host *shost)
2418{
2419 struct fc_lport *lp;
2420 struct fnic *fnic;
2421 int ret = 0;
2422 struct reset_stats *reset_stats;
2423
2424 lp = shost_priv(shost);
2425 fnic = lport_priv(lp);
2426 reset_stats = &fnic->fnic_stats.reset_stats;
2427
2428 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2429 "fnic_reset called\n");
2430
2431 atomic64_inc(&reset_stats->fnic_resets);
2432
2433
2434
2435
2436
2437 ret = lp->tt.lport_reset(lp);
2438
2439 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2440 "Returning from fnic reset %s\n",
2441 (ret == 0) ?
2442 "SUCCESS" : "FAILED");
2443
2444 if (ret == 0)
2445 atomic64_inc(&reset_stats->fnic_reset_completions);
2446 else
2447 atomic64_inc(&reset_stats->fnic_reset_failures);
2448
2449 return ret;
2450}
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461int fnic_host_reset(struct scsi_cmnd *sc)
2462{
2463 int ret;
2464 unsigned long wait_host_tmo;
2465 struct Scsi_Host *shost = sc->device->host;
2466 struct fc_lport *lp = shost_priv(shost);
2467
2468
2469
2470
2471
2472
2473 ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
2474 if (ret == SUCCESS) {
2475 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2476 ret = FAILED;
2477 while (time_before(jiffies, wait_host_tmo)) {
2478 if ((lp->state == LPORT_ST_READY) &&
2479 (lp->link_up)) {
2480 ret = SUCCESS;
2481 break;
2482 }
2483 ssleep(1);
2484 }
2485 }
2486
2487 return ret;
2488}
2489
2490
2491
2492
2493void fnic_scsi_abort_io(struct fc_lport *lp)
2494{
2495 int err = 0;
2496 unsigned long flags;
2497 enum fnic_state old_state;
2498 struct fnic *fnic = lport_priv(lp);
2499 DECLARE_COMPLETION_ONSTACK(remove_wait);
2500
2501
2502retry_fw_reset:
2503 spin_lock_irqsave(&fnic->fnic_lock, flags);
2504 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2505
2506 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2507 schedule_timeout(msecs_to_jiffies(100));
2508 goto retry_fw_reset;
2509 }
2510
2511 fnic->remove_wait = &remove_wait;
2512 old_state = fnic->state;
2513 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2514 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2515 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2516
2517 err = fnic_fw_reset_handler(fnic);
2518 if (err) {
2519 spin_lock_irqsave(&fnic->fnic_lock, flags);
2520 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2521 fnic->state = old_state;
2522 fnic->remove_wait = NULL;
2523 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2524 return;
2525 }
2526
2527
2528 wait_for_completion_timeout(&remove_wait,
2529 msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2530
2531 spin_lock_irqsave(&fnic->fnic_lock, flags);
2532 fnic->remove_wait = NULL;
2533 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2534 "fnic_scsi_abort_io %s\n",
2535 (fnic->state == FNIC_IN_ETH_MODE) ?
2536 "SUCCESS" : "FAILED");
2537 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2538
2539}
2540
2541
2542
2543
2544void fnic_scsi_cleanup(struct fc_lport *lp)
2545{
2546 unsigned long flags;
2547 enum fnic_state old_state;
2548 struct fnic *fnic = lport_priv(lp);
2549
2550
2551retry_fw_reset:
2552 spin_lock_irqsave(&fnic->fnic_lock, flags);
2553 if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2554
2555 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2556 schedule_timeout(msecs_to_jiffies(100));
2557 goto retry_fw_reset;
2558 }
2559 old_state = fnic->state;
2560 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2561 fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2562 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2563
2564 if (fnic_fw_reset_handler(fnic)) {
2565 spin_lock_irqsave(&fnic->fnic_lock, flags);
2566 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2567 fnic->state = old_state;
2568 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2569 }
2570
2571}
2572
2573void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2574{
2575}
2576
2577void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2578{
2579 struct fnic *fnic = lport_priv(lp);
2580
2581
2582 if (sid)
2583 goto call_fc_exch_mgr_reset;
2584
2585 if (did) {
2586 fnic_rport_exch_reset(fnic, did);
2587 goto call_fc_exch_mgr_reset;
2588 }
2589
2590
2591
2592
2593
2594 if (!fnic->in_remove)
2595 fnic_scsi_cleanup(lp);
2596 else
2597 fnic_scsi_abort_io(lp);
2598
2599
2600call_fc_exch_mgr_reset:
2601 fc_exch_mgr_reset(lp, sid, did);
2602
2603}
2604
2605
2606
2607
2608
2609
2610
2611
2612int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2613{
2614 int tag;
2615 struct fnic_io_req *io_req;
2616 spinlock_t *io_lock;
2617 unsigned long flags;
2618 int ret = 0;
2619 struct scsi_cmnd *sc;
2620 struct scsi_device *lun_dev = NULL;
2621
2622 if (lr_sc)
2623 lun_dev = lr_sc->device;
2624
2625
2626 for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2627 sc = scsi_host_find_tag(fnic->lport->host, tag);
2628
2629
2630
2631
2632 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2633 continue;
2634
2635 io_lock = fnic_io_lock_hash(fnic, sc);
2636 spin_lock_irqsave(io_lock, flags);
2637
2638 io_req = (struct fnic_io_req *)CMD_SP(sc);
2639
2640 if (!io_req || sc->device != lun_dev) {
2641 spin_unlock_irqrestore(io_lock, flags);
2642 continue;
2643 }
2644
2645
2646
2647
2648
2649 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2650 "Found IO in %s on lun\n",
2651 fnic_ioreq_state_to_str(CMD_STATE(sc)));
2652
2653 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2654 ret = 1;
2655 spin_unlock_irqrestore(io_lock, flags);
2656 }
2657
2658 return ret;
2659}
2660