1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/pci.h>
24#include <linux/slab.h>
25#include <linux/interrupt.h>
26#include <linux/export.h>
27#include <linux/delay.h>
28#include <asm/unaligned.h>
29#include <linux/t10-pi.h>
30#include <linux/crc-t10dif.h>
31#include <net/checksum.h>
32
33#include <scsi/scsi.h>
34#include <scsi/scsi_device.h>
35#include <scsi/scsi_eh.h>
36#include <scsi/scsi_host.h>
37#include <scsi/scsi_tcq.h>
38#include <scsi/scsi_transport_fc.h>
39
40#include "lpfc_version.h"
41#include "lpfc_hw4.h"
42#include "lpfc_hw.h"
43#include "lpfc_sli.h"
44#include "lpfc_sli4.h"
45#include "lpfc_nl.h"
46#include "lpfc_disc.h"
47#include "lpfc.h"
48#include "lpfc_scsi.h"
49#include "lpfc_logmsg.h"
50#include "lpfc_crtn.h"
51#include "lpfc_vport.h"
52
53#define LPFC_RESET_WAIT 2
54#define LPFC_ABORT_WAIT 2
55
56static char *dif_op_str[] = {
57 "PROT_NORMAL",
58 "PROT_READ_INSERT",
59 "PROT_WRITE_STRIP",
60 "PROT_READ_STRIP",
61 "PROT_WRITE_INSERT",
62 "PROT_READ_PASS",
63 "PROT_WRITE_PASS",
64};
65
66struct scsi_dif_tuple {
67 __be16 guard_tag;
68 __be16 app_tag;
69 __be32 ref_tag;
70};
71
72static struct lpfc_rport_data *
73lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
74{
75 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
76
77 if (vport->phba->cfg_fof)
78 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
79 else
80 return (struct lpfc_rport_data *)sdev->hostdata;
81}
82
83static void
84lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
85static void
86lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
87static int
88lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
89
90static inline unsigned
91lpfc_cmd_blksize(struct scsi_cmnd *sc)
92{
93 return sc->device->sector_size;
94}
95
96#define LPFC_CHECK_PROTECT_GUARD 1
97#define LPFC_CHECK_PROTECT_REF 2
98static inline unsigned
99lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
100{
101 return 1;
102}
103
104static inline unsigned
105lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
106{
107 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
108 return 0;
109 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
110 return 1;
111 return 0;
112}
113
114
115
116
117
118
119
120
121
122static void
123lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
124 struct lpfc_io_buf *lpfc_cmd)
125{
126 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
127 if (sgl) {
128 sgl += 1;
129 sgl->word2 = le32_to_cpu(sgl->word2);
130 bf_set(lpfc_sli4_sge_last, sgl, 1);
131 sgl->word2 = cpu_to_le32(sgl->word2);
132 }
133}
134
135
136
137
138
139
140
141
142
143static void
144lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
145{
146 struct lpfc_rport_data *rdata;
147 struct lpfc_nodelist *pnode;
148 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
149 unsigned long flags;
150 struct Scsi_Host *shost = cmd->device->host;
151 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
152 unsigned long latency;
153 int i;
154
155 if (!vport->stat_data_enabled ||
156 vport->stat_data_blocked ||
157 (cmd->result))
158 return;
159
160 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
161 rdata = lpfc_cmd->rdata;
162 pnode = rdata->pnode;
163
164 spin_lock_irqsave(shost->host_lock, flags);
165 if (!pnode ||
166 !pnode->lat_data ||
167 (phba->bucket_type == LPFC_NO_BUCKET)) {
168 spin_unlock_irqrestore(shost->host_lock, flags);
169 return;
170 }
171
172 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
173 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
174 phba->bucket_step;
175
176 if (i < 0)
177 i = 0;
178 else if (i >= LPFC_MAX_BUCKET_COUNT)
179 i = LPFC_MAX_BUCKET_COUNT - 1;
180 } else {
181 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
182 if (latency <= (phba->bucket_base +
183 ((1<<i)*phba->bucket_step)))
184 break;
185 }
186
187 pnode->lat_data[i].cmd_count++;
188 spin_unlock_irqrestore(shost->host_lock, flags);
189}
190
191
192
193
194
195
196
197
198
199
200
201
202void
203lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
204{
205 unsigned long flags;
206 uint32_t evt_posted;
207 unsigned long expires;
208
209 spin_lock_irqsave(&phba->hbalock, flags);
210 atomic_inc(&phba->num_rsrc_err);
211 phba->last_rsrc_error_time = jiffies;
212
213 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
214 if (time_after(expires, jiffies)) {
215 spin_unlock_irqrestore(&phba->hbalock, flags);
216 return;
217 }
218
219 phba->last_ramp_down_time = jiffies;
220
221 spin_unlock_irqrestore(&phba->hbalock, flags);
222
223 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
224 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
225 if (!evt_posted)
226 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
227 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
228
229 if (!evt_posted)
230 lpfc_worker_wake_up(phba);
231 return;
232}
233
234
235
236
237
238
239
240
241
242void
243lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
244{
245 struct lpfc_vport **vports;
246 struct Scsi_Host *shost;
247 struct scsi_device *sdev;
248 unsigned long new_queue_depth;
249 unsigned long num_rsrc_err, num_cmd_success;
250 int i;
251
252 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
253 num_cmd_success = atomic_read(&phba->num_cmd_success);
254
255
256
257
258
259
260 if (num_rsrc_err == 0)
261 return;
262
263 vports = lpfc_create_vport_work_array(phba);
264 if (vports != NULL)
265 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
266 shost = lpfc_shost_from_vport(vports[i]);
267 shost_for_each_device(sdev, shost) {
268 new_queue_depth =
269 sdev->queue_depth * num_rsrc_err /
270 (num_rsrc_err + num_cmd_success);
271 if (!new_queue_depth)
272 new_queue_depth = sdev->queue_depth - 1;
273 else
274 new_queue_depth = sdev->queue_depth -
275 new_queue_depth;
276 scsi_change_queue_depth(sdev, new_queue_depth);
277 }
278 }
279 lpfc_destroy_vport_work_array(phba, vports);
280 atomic_set(&phba->num_rsrc_err, 0);
281 atomic_set(&phba->num_cmd_success, 0);
282}
283
284
285
286
287
288
289
290
291
292void
293lpfc_scsi_dev_block(struct lpfc_hba *phba)
294{
295 struct lpfc_vport **vports;
296 struct Scsi_Host *shost;
297 struct scsi_device *sdev;
298 struct fc_rport *rport;
299 int i;
300
301 vports = lpfc_create_vport_work_array(phba);
302 if (vports != NULL)
303 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
304 shost = lpfc_shost_from_vport(vports[i]);
305 shost_for_each_device(sdev, shost) {
306 rport = starget_to_rport(scsi_target(sdev));
307 fc_remote_port_delete(rport);
308 }
309 }
310 lpfc_destroy_vport_work_array(phba, vports);
311}
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329static int
330lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
331{
332 struct lpfc_hba *phba = vport->phba;
333 struct lpfc_io_buf *psb;
334 struct ulp_bde64 *bpl;
335 IOCB_t *iocb;
336 dma_addr_t pdma_phys_fcp_cmd;
337 dma_addr_t pdma_phys_fcp_rsp;
338 dma_addr_t pdma_phys_sgl;
339 uint16_t iotag;
340 int bcnt, bpl_size;
341
342 bpl_size = phba->cfg_sg_dma_buf_size -
343 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
344
345 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
346 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
347 num_to_alloc, phba->cfg_sg_dma_buf_size,
348 (int)sizeof(struct fcp_cmnd),
349 (int)sizeof(struct fcp_rsp), bpl_size);
350
351 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
352 psb = kzalloc(sizeof(struct lpfc_io_buf), GFP_KERNEL);
353 if (!psb)
354 break;
355
356
357
358
359
360
361
362 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
363 GFP_KERNEL, &psb->dma_handle);
364 if (!psb->data) {
365 kfree(psb);
366 break;
367 }
368
369
370
371 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
372 if (iotag == 0) {
373 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
374 psb->data, psb->dma_handle);
375 kfree(psb);
376 break;
377 }
378 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
379
380 psb->fcp_cmnd = psb->data;
381 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
382 psb->dma_sgl = psb->data + sizeof(struct fcp_cmnd) +
383 sizeof(struct fcp_rsp);
384
385
386 bpl = (struct ulp_bde64 *)psb->dma_sgl;
387 pdma_phys_fcp_cmd = psb->dma_handle;
388 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
389 pdma_phys_sgl = psb->dma_handle + sizeof(struct fcp_cmnd) +
390 sizeof(struct fcp_rsp);
391
392
393
394
395
396
397 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
398 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
399 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
400 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
401 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
402
403
404 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
405 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
406 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
407 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
408 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
409
410
411
412
413
414 iocb = &psb->cur_iocbq.iocb;
415 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
416 if ((phba->sli_rev == 3) &&
417 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
418
419 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
420 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
421 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
422 unsli3.fcp_ext.icd);
423 iocb->un.fcpi64.bdl.addrHigh = 0;
424 iocb->ulpBdeCount = 0;
425 iocb->ulpLe = 0;
426
427 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
428 BUFF_TYPE_BDE_64;
429 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
430 sizeof(struct fcp_rsp);
431 iocb->unsli3.fcp_ext.rbde.addrLow =
432 putPaddrLow(pdma_phys_fcp_rsp);
433 iocb->unsli3.fcp_ext.rbde.addrHigh =
434 putPaddrHigh(pdma_phys_fcp_rsp);
435 } else {
436 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
437 iocb->un.fcpi64.bdl.bdeSize =
438 (2 * sizeof(struct ulp_bde64));
439 iocb->un.fcpi64.bdl.addrLow =
440 putPaddrLow(pdma_phys_sgl);
441 iocb->un.fcpi64.bdl.addrHigh =
442 putPaddrHigh(pdma_phys_sgl);
443 iocb->ulpBdeCount = 1;
444 iocb->ulpLe = 1;
445 }
446 iocb->ulpClass = CLASS3;
447 psb->status = IOSTAT_SUCCESS;
448
449 psb->cur_iocbq.context1 = psb;
450 spin_lock_init(&psb->buf_lock);
451 lpfc_release_scsi_buf_s3(phba, psb);
452
453 }
454
455 return bcnt;
456}
457
458
459
460
461
462
463
464
465void
466lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
467{
468 struct lpfc_hba *phba = vport->phba;
469 struct lpfc_io_buf *psb, *next_psb;
470 struct lpfc_sli4_hdw_queue *qp;
471 unsigned long iflag = 0;
472 int idx;
473
474 if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
475 return;
476
477 spin_lock_irqsave(&phba->hbalock, iflag);
478 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
479 qp = &phba->sli4_hba.hdwq[idx];
480
481 spin_lock(&qp->abts_io_buf_list_lock);
482 list_for_each_entry_safe(psb, next_psb,
483 &qp->lpfc_abts_io_buf_list, list) {
484 if (psb->cur_iocbq.iocb_flag == LPFC_IO_NVME)
485 continue;
486
487 if (psb->rdata && psb->rdata->pnode &&
488 psb->rdata->pnode->vport == vport)
489 psb->rdata = NULL;
490 }
491 spin_unlock(&qp->abts_io_buf_list_lock);
492 }
493 spin_unlock_irqrestore(&phba->hbalock, iflag);
494}
495
496
497
498
499
500
501
502
503
504void
505lpfc_sli4_io_xri_aborted(struct lpfc_hba *phba,
506 struct sli4_wcqe_xri_aborted *axri, int idx)
507{
508 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
509 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
510 struct lpfc_io_buf *psb, *next_psb;
511 struct lpfc_sli4_hdw_queue *qp;
512 unsigned long iflag = 0;
513 struct lpfc_iocbq *iocbq;
514 int i;
515 struct lpfc_nodelist *ndlp;
516 int rrq_empty = 0;
517 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
518
519 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
520 return;
521
522 qp = &phba->sli4_hba.hdwq[idx];
523 spin_lock_irqsave(&phba->hbalock, iflag);
524 spin_lock(&qp->abts_io_buf_list_lock);
525 list_for_each_entry_safe(psb, next_psb,
526 &qp->lpfc_abts_io_buf_list, list) {
527 if (psb->cur_iocbq.sli4_xritag == xri) {
528 list_del_init(&psb->list);
529 psb->exch_busy = 0;
530 psb->status = IOSTAT_SUCCESS;
531 if (psb->cur_iocbq.iocb_flag == LPFC_IO_NVME) {
532 qp->abts_nvme_io_bufs--;
533 spin_unlock(&qp->abts_io_buf_list_lock);
534 spin_unlock_irqrestore(&phba->hbalock, iflag);
535 lpfc_sli4_nvme_xri_aborted(phba, axri, psb);
536 return;
537 }
538 qp->abts_scsi_io_bufs--;
539 spin_unlock(&qp->abts_io_buf_list_lock);
540
541 if (psb->rdata && psb->rdata->pnode)
542 ndlp = psb->rdata->pnode;
543 else
544 ndlp = NULL;
545
546 rrq_empty = list_empty(&phba->active_rrq_list);
547 spin_unlock_irqrestore(&phba->hbalock, iflag);
548 if (ndlp) {
549 lpfc_set_rrq_active(phba, ndlp,
550 psb->cur_iocbq.sli4_lxritag, rxid, 1);
551 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
552 }
553 lpfc_release_scsi_buf_s4(phba, psb);
554 if (rrq_empty)
555 lpfc_worker_wake_up(phba);
556 return;
557 }
558 }
559 spin_unlock(&qp->abts_io_buf_list_lock);
560 for (i = 1; i <= phba->sli.last_iotag; i++) {
561 iocbq = phba->sli.iocbq_lookup[i];
562
563 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
564 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
565 continue;
566 if (iocbq->sli4_xritag != xri)
567 continue;
568 psb = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
569 psb->exch_busy = 0;
570 spin_unlock_irqrestore(&phba->hbalock, iflag);
571 if (!list_empty(&pring->txq))
572 lpfc_worker_wake_up(phba);
573 return;
574
575 }
576 spin_unlock_irqrestore(&phba->hbalock, iflag);
577}
578
579
580
581
582
583
584
585
586
587
588
589
590static struct lpfc_io_buf *
591lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
592 struct scsi_cmnd *cmnd)
593{
594 struct lpfc_io_buf *lpfc_cmd = NULL;
595 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
596 unsigned long iflag = 0;
597
598 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
599 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_io_buf,
600 list);
601 if (!lpfc_cmd) {
602 spin_lock(&phba->scsi_buf_list_put_lock);
603 list_splice(&phba->lpfc_scsi_buf_list_put,
604 &phba->lpfc_scsi_buf_list_get);
605 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
606 list_remove_head(scsi_buf_list_get, lpfc_cmd,
607 struct lpfc_io_buf, list);
608 spin_unlock(&phba->scsi_buf_list_put_lock);
609 }
610 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
611
612 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
613 atomic_inc(&ndlp->cmd_pending);
614 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
615 }
616 return lpfc_cmd;
617}
618
619
620
621
622
623
624
625
626
627
628
629static struct lpfc_io_buf *
630lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
631 struct scsi_cmnd *cmnd)
632{
633 struct lpfc_io_buf *lpfc_cmd;
634 struct lpfc_sli4_hdw_queue *qp;
635 struct sli4_sge *sgl;
636 IOCB_t *iocb;
637 dma_addr_t pdma_phys_fcp_rsp;
638 dma_addr_t pdma_phys_fcp_cmd;
639 uint32_t cpu, idx;
640 int tag;
641 struct fcp_cmd_rsp_buf *tmp = NULL;
642
643 cpu = raw_smp_processor_id();
644 if (cmnd && phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
645 tag = blk_mq_unique_tag(cmnd->request);
646 idx = blk_mq_unique_tag_to_hwq(tag);
647 } else {
648 idx = phba->sli4_hba.cpu_map[cpu].hdwq;
649 }
650
651 lpfc_cmd = lpfc_get_io_buf(phba, ndlp, idx,
652 !phba->cfg_xri_rebalancing);
653 if (!lpfc_cmd) {
654 qp = &phba->sli4_hba.hdwq[idx];
655 qp->empty_io_bufs++;
656 return NULL;
657 }
658
659
660
661
662 lpfc_cmd->cur_iocbq.iocb_flag = LPFC_IO_FCP;
663 lpfc_cmd->prot_seg_cnt = 0;
664 lpfc_cmd->seg_cnt = 0;
665 lpfc_cmd->timeout = 0;
666 lpfc_cmd->flags = 0;
667 lpfc_cmd->start_time = jiffies;
668 lpfc_cmd->waitq = NULL;
669 lpfc_cmd->cpu = cpu;
670#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
671 lpfc_cmd->prot_data_type = 0;
672#endif
673 tmp = lpfc_get_cmd_rsp_buf_per_hdwq(phba, lpfc_cmd);
674 if (!tmp)
675 return NULL;
676
677 lpfc_cmd->fcp_cmnd = tmp->fcp_cmnd;
678 lpfc_cmd->fcp_rsp = tmp->fcp_rsp;
679
680
681
682
683
684
685 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
686 pdma_phys_fcp_cmd = tmp->fcp_cmd_rsp_dma_handle;
687 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
688 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
689 sgl->word2 = le32_to_cpu(sgl->word2);
690 bf_set(lpfc_sli4_sge_last, sgl, 0);
691 sgl->word2 = cpu_to_le32(sgl->word2);
692 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
693 sgl++;
694
695
696 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
697 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
698 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
699 sgl->word2 = le32_to_cpu(sgl->word2);
700 bf_set(lpfc_sli4_sge_last, sgl, 1);
701 sgl->word2 = cpu_to_le32(sgl->word2);
702 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
703
704
705
706
707
708 iocb = &lpfc_cmd->cur_iocbq.iocb;
709 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
710 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
711
712
713
714
715 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
716 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
717 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
718 iocb->ulpBdeCount = 1;
719 iocb->ulpLe = 1;
720 iocb->ulpClass = CLASS3;
721
722 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
723 atomic_inc(&ndlp->cmd_pending);
724 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
725 }
726 return lpfc_cmd;
727}
728
729
730
731
732
733
734
735
736
737
738
739static struct lpfc_io_buf*
740lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
741 struct scsi_cmnd *cmnd)
742{
743 return phba->lpfc_get_scsi_buf(phba, ndlp, cmnd);
744}
745
746
747
748
749
750
751
752
753
754static void
755lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
756{
757 unsigned long iflag = 0;
758
759 psb->seg_cnt = 0;
760 psb->prot_seg_cnt = 0;
761
762 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
763 psb->pCmd = NULL;
764 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
765 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
766 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
767}
768
769
770
771
772
773
774
775
776
777
778
779static void
780lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
781{
782 struct lpfc_sli4_hdw_queue *qp;
783 unsigned long iflag = 0;
784
785 psb->seg_cnt = 0;
786 psb->prot_seg_cnt = 0;
787
788 qp = psb->hdwq;
789 if (psb->exch_busy) {
790 spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
791 psb->pCmd = NULL;
792 list_add_tail(&psb->list, &qp->lpfc_abts_io_buf_list);
793 qp->abts_scsi_io_bufs++;
794 spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
795 } else {
796 lpfc_release_io_buf(phba, (struct lpfc_io_buf *)psb, qp);
797 }
798}
799
800
801
802
803
804
805
806
807
808static void
809lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
810{
811 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
812 atomic_dec(&psb->ndlp->cmd_pending);
813
814 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
815 phba->lpfc_release_scsi_buf(phba, psb);
816}
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832static int
833lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
834{
835 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
836 struct scatterlist *sgel = NULL;
837 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
838 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
839 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
840 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
841 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
842 dma_addr_t physaddr;
843 uint32_t num_bde = 0;
844 int nseg, datadir = scsi_cmnd->sc_data_direction;
845
846
847
848
849
850
851
852 bpl += 2;
853 if (scsi_sg_count(scsi_cmnd)) {
854
855
856
857
858
859
860
861 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
862 scsi_sg_count(scsi_cmnd), datadir);
863 if (unlikely(!nseg))
864 return 1;
865
866 lpfc_cmd->seg_cnt = nseg;
867 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
868 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
869 "9064 BLKGRD: %s: Too many sg segments from "
870 "dma_map_sg. Config %d, seg_cnt %d\n",
871 __func__, phba->cfg_sg_seg_cnt,
872 lpfc_cmd->seg_cnt);
873 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
874 lpfc_cmd->seg_cnt = 0;
875 scsi_dma_unmap(scsi_cmnd);
876 return 2;
877 }
878
879
880
881
882
883
884
885
886
887
888 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
889 physaddr = sg_dma_address(sgel);
890 if (phba->sli_rev == 3 &&
891 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
892 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
893 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
894 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
895 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
896 data_bde->addrLow = putPaddrLow(physaddr);
897 data_bde->addrHigh = putPaddrHigh(physaddr);
898 data_bde++;
899 } else {
900 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
901 bpl->tus.f.bdeSize = sg_dma_len(sgel);
902 bpl->tus.w = le32_to_cpu(bpl->tus.w);
903 bpl->addrLow =
904 le32_to_cpu(putPaddrLow(physaddr));
905 bpl->addrHigh =
906 le32_to_cpu(putPaddrHigh(physaddr));
907 bpl++;
908 }
909 }
910 }
911
912
913
914
915
916
917
918 if (phba->sli_rev == 3 &&
919 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
920 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
921 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
922
923
924
925
926
927 physaddr = lpfc_cmd->dma_handle;
928 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
929 data_bde->tus.f.bdeSize = (num_bde *
930 sizeof(struct ulp_bde64));
931 physaddr += (sizeof(struct fcp_cmnd) +
932 sizeof(struct fcp_rsp) +
933 (2 * sizeof(struct ulp_bde64)));
934 data_bde->addrHigh = putPaddrHigh(physaddr);
935 data_bde->addrLow = putPaddrLow(physaddr);
936
937 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
938 } else {
939
940 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
941 }
942 } else {
943 iocb_cmd->un.fcpi64.bdl.bdeSize =
944 ((num_bde + 2) * sizeof(struct ulp_bde64));
945 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
946 }
947 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
948
949
950
951
952
953 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
954 return 0;
955}
956
957#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
958
959
960#define BG_ERR_INIT 0x1
961
962#define BG_ERR_TGT 0x2
963
964#define BG_ERR_SWAP 0x10
965
966
967
968
969#define BG_ERR_CHECK 0x20
970
971
972
973
974
975
976
977
978
979
980
981static int
982lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
983 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
984{
985 struct scatterlist *sgpe;
986 struct lpfc_io_buf *lpfc_cmd = NULL;
987 struct scsi_dif_tuple *src = NULL;
988 struct lpfc_nodelist *ndlp;
989 struct lpfc_rport_data *rdata;
990 uint32_t op = scsi_get_prot_op(sc);
991 uint32_t blksize;
992 uint32_t numblks;
993 sector_t lba;
994 int rc = 0;
995 int blockoff = 0;
996
997 if (op == SCSI_PROT_NORMAL)
998 return 0;
999
1000 sgpe = scsi_prot_sglist(sc);
1001 lba = scsi_get_lba(sc);
1002
1003
1004 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1005 blksize = lpfc_cmd_blksize(sc);
1006 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1007
1008
1009 if ((phba->lpfc_injerr_lba < lba) ||
1010 (phba->lpfc_injerr_lba >= (lba + numblks)))
1011 return 0;
1012 if (sgpe) {
1013 blockoff = phba->lpfc_injerr_lba - lba;
1014 numblks = sg_dma_len(sgpe) /
1015 sizeof(struct scsi_dif_tuple);
1016 if (numblks < blockoff)
1017 blockoff = numblks;
1018 }
1019 }
1020
1021
1022 rdata = lpfc_rport_data_from_scsi_device(sc->device);
1023 if (rdata && rdata->pnode) {
1024 ndlp = rdata->pnode;
1025
1026
1027 if (phba->lpfc_injerr_nportid &&
1028 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1029 return 0;
1030
1031
1032
1033
1034
1035 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1036 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1037 sizeof(struct lpfc_name)) != 0))
1038 return 0;
1039 }
1040
1041
1042 if (sgpe) {
1043 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1044 src += blockoff;
1045 lpfc_cmd = (struct lpfc_io_buf *)sc->host_scribble;
1046 }
1047
1048
1049 if (reftag) {
1050 if (phba->lpfc_injerr_wref_cnt) {
1051 switch (op) {
1052 case SCSI_PROT_WRITE_PASS:
1053 if (src) {
1054
1055
1056
1057
1058
1059
1060
1061
1062 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1063 "9076 BLKGRD: Injecting reftag error: "
1064 "write lba x%lx + x%x oldrefTag x%x\n",
1065 (unsigned long)lba, blockoff,
1066 be32_to_cpu(src->ref_tag));
1067
1068
1069
1070
1071
1072 if (lpfc_cmd) {
1073 lpfc_cmd->prot_data_type =
1074 LPFC_INJERR_REFTAG;
1075 lpfc_cmd->prot_data_segment =
1076 src;
1077 lpfc_cmd->prot_data =
1078 src->ref_tag;
1079 }
1080 src->ref_tag = cpu_to_be32(0xDEADBEEF);
1081 phba->lpfc_injerr_wref_cnt--;
1082 if (phba->lpfc_injerr_wref_cnt == 0) {
1083 phba->lpfc_injerr_nportid = 0;
1084 phba->lpfc_injerr_lba =
1085 LPFC_INJERR_LBA_OFF;
1086 memset(&phba->lpfc_injerr_wwpn,
1087 0, sizeof(struct lpfc_name));
1088 }
1089 rc = BG_ERR_TGT | BG_ERR_CHECK;
1090
1091 break;
1092 }
1093
1094 case SCSI_PROT_WRITE_INSERT:
1095
1096
1097
1098
1099
1100
1101 *reftag = 0xDEADBEEF;
1102 phba->lpfc_injerr_wref_cnt--;
1103 if (phba->lpfc_injerr_wref_cnt == 0) {
1104 phba->lpfc_injerr_nportid = 0;
1105 phba->lpfc_injerr_lba =
1106 LPFC_INJERR_LBA_OFF;
1107 memset(&phba->lpfc_injerr_wwpn,
1108 0, sizeof(struct lpfc_name));
1109 }
1110 rc = BG_ERR_TGT | BG_ERR_CHECK;
1111
1112 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1113 "9078 BLKGRD: Injecting reftag error: "
1114 "write lba x%lx\n", (unsigned long)lba);
1115 break;
1116 case SCSI_PROT_WRITE_STRIP:
1117
1118
1119
1120
1121
1122 *reftag = 0xDEADBEEF;
1123 phba->lpfc_injerr_wref_cnt--;
1124 if (phba->lpfc_injerr_wref_cnt == 0) {
1125 phba->lpfc_injerr_nportid = 0;
1126 phba->lpfc_injerr_lba =
1127 LPFC_INJERR_LBA_OFF;
1128 memset(&phba->lpfc_injerr_wwpn,
1129 0, sizeof(struct lpfc_name));
1130 }
1131 rc = BG_ERR_INIT;
1132
1133 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1134 "9077 BLKGRD: Injecting reftag error: "
1135 "write lba x%lx\n", (unsigned long)lba);
1136 break;
1137 }
1138 }
1139 if (phba->lpfc_injerr_rref_cnt) {
1140 switch (op) {
1141 case SCSI_PROT_READ_INSERT:
1142 case SCSI_PROT_READ_STRIP:
1143 case SCSI_PROT_READ_PASS:
1144
1145
1146
1147
1148
1149 *reftag = 0xDEADBEEF;
1150 phba->lpfc_injerr_rref_cnt--;
1151 if (phba->lpfc_injerr_rref_cnt == 0) {
1152 phba->lpfc_injerr_nportid = 0;
1153 phba->lpfc_injerr_lba =
1154 LPFC_INJERR_LBA_OFF;
1155 memset(&phba->lpfc_injerr_wwpn,
1156 0, sizeof(struct lpfc_name));
1157 }
1158 rc = BG_ERR_INIT;
1159
1160 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1161 "9079 BLKGRD: Injecting reftag error: "
1162 "read lba x%lx\n", (unsigned long)lba);
1163 break;
1164 }
1165 }
1166 }
1167
1168
1169 if (apptag) {
1170 if (phba->lpfc_injerr_wapp_cnt) {
1171 switch (op) {
1172 case SCSI_PROT_WRITE_PASS:
1173 if (src) {
1174
1175
1176
1177
1178
1179
1180
1181
1182 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1183 "9080 BLKGRD: Injecting apptag error: "
1184 "write lba x%lx + x%x oldappTag x%x\n",
1185 (unsigned long)lba, blockoff,
1186 be16_to_cpu(src->app_tag));
1187
1188
1189
1190
1191
1192 if (lpfc_cmd) {
1193 lpfc_cmd->prot_data_type =
1194 LPFC_INJERR_APPTAG;
1195 lpfc_cmd->prot_data_segment =
1196 src;
1197 lpfc_cmd->prot_data =
1198 src->app_tag;
1199 }
1200 src->app_tag = cpu_to_be16(0xDEAD);
1201 phba->lpfc_injerr_wapp_cnt--;
1202 if (phba->lpfc_injerr_wapp_cnt == 0) {
1203 phba->lpfc_injerr_nportid = 0;
1204 phba->lpfc_injerr_lba =
1205 LPFC_INJERR_LBA_OFF;
1206 memset(&phba->lpfc_injerr_wwpn,
1207 0, sizeof(struct lpfc_name));
1208 }
1209 rc = BG_ERR_TGT | BG_ERR_CHECK;
1210 break;
1211 }
1212
1213 case SCSI_PROT_WRITE_INSERT:
1214
1215
1216
1217
1218
1219
1220 *apptag = 0xDEAD;
1221 phba->lpfc_injerr_wapp_cnt--;
1222 if (phba->lpfc_injerr_wapp_cnt == 0) {
1223 phba->lpfc_injerr_nportid = 0;
1224 phba->lpfc_injerr_lba =
1225 LPFC_INJERR_LBA_OFF;
1226 memset(&phba->lpfc_injerr_wwpn,
1227 0, sizeof(struct lpfc_name));
1228 }
1229 rc = BG_ERR_TGT | BG_ERR_CHECK;
1230
1231 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1232 "0813 BLKGRD: Injecting apptag error: "
1233 "write lba x%lx\n", (unsigned long)lba);
1234 break;
1235 case SCSI_PROT_WRITE_STRIP:
1236
1237
1238
1239
1240
1241 *apptag = 0xDEAD;
1242 phba->lpfc_injerr_wapp_cnt--;
1243 if (phba->lpfc_injerr_wapp_cnt == 0) {
1244 phba->lpfc_injerr_nportid = 0;
1245 phba->lpfc_injerr_lba =
1246 LPFC_INJERR_LBA_OFF;
1247 memset(&phba->lpfc_injerr_wwpn,
1248 0, sizeof(struct lpfc_name));
1249 }
1250 rc = BG_ERR_INIT;
1251
1252 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1253 "0812 BLKGRD: Injecting apptag error: "
1254 "write lba x%lx\n", (unsigned long)lba);
1255 break;
1256 }
1257 }
1258 if (phba->lpfc_injerr_rapp_cnt) {
1259 switch (op) {
1260 case SCSI_PROT_READ_INSERT:
1261 case SCSI_PROT_READ_STRIP:
1262 case SCSI_PROT_READ_PASS:
1263
1264
1265
1266
1267
1268 *apptag = 0xDEAD;
1269 phba->lpfc_injerr_rapp_cnt--;
1270 if (phba->lpfc_injerr_rapp_cnt == 0) {
1271 phba->lpfc_injerr_nportid = 0;
1272 phba->lpfc_injerr_lba =
1273 LPFC_INJERR_LBA_OFF;
1274 memset(&phba->lpfc_injerr_wwpn,
1275 0, sizeof(struct lpfc_name));
1276 }
1277 rc = BG_ERR_INIT;
1278
1279 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1280 "0814 BLKGRD: Injecting apptag error: "
1281 "read lba x%lx\n", (unsigned long)lba);
1282 break;
1283 }
1284 }
1285 }
1286
1287
1288
1289 if (new_guard) {
1290 if (phba->lpfc_injerr_wgrd_cnt) {
1291 switch (op) {
1292 case SCSI_PROT_WRITE_PASS:
1293 rc = BG_ERR_CHECK;
1294
1295
1296 case SCSI_PROT_WRITE_INSERT:
1297
1298
1299
1300
1301
1302 phba->lpfc_injerr_wgrd_cnt--;
1303 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1304 phba->lpfc_injerr_nportid = 0;
1305 phba->lpfc_injerr_lba =
1306 LPFC_INJERR_LBA_OFF;
1307 memset(&phba->lpfc_injerr_wwpn,
1308 0, sizeof(struct lpfc_name));
1309 }
1310
1311 rc |= BG_ERR_TGT | BG_ERR_SWAP;
1312
1313
1314 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1315 "0817 BLKGRD: Injecting guard error: "
1316 "write lba x%lx\n", (unsigned long)lba);
1317 break;
1318 case SCSI_PROT_WRITE_STRIP:
1319
1320
1321
1322
1323
1324 phba->lpfc_injerr_wgrd_cnt--;
1325 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1326 phba->lpfc_injerr_nportid = 0;
1327 phba->lpfc_injerr_lba =
1328 LPFC_INJERR_LBA_OFF;
1329 memset(&phba->lpfc_injerr_wwpn,
1330 0, sizeof(struct lpfc_name));
1331 }
1332
1333 rc = BG_ERR_INIT | BG_ERR_SWAP;
1334
1335
1336 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1337 "0816 BLKGRD: Injecting guard error: "
1338 "write lba x%lx\n", (unsigned long)lba);
1339 break;
1340 }
1341 }
1342 if (phba->lpfc_injerr_rgrd_cnt) {
1343 switch (op) {
1344 case SCSI_PROT_READ_INSERT:
1345 case SCSI_PROT_READ_STRIP:
1346 case SCSI_PROT_READ_PASS:
1347
1348
1349
1350
1351
1352 phba->lpfc_injerr_rgrd_cnt--;
1353 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1354 phba->lpfc_injerr_nportid = 0;
1355 phba->lpfc_injerr_lba =
1356 LPFC_INJERR_LBA_OFF;
1357 memset(&phba->lpfc_injerr_wwpn,
1358 0, sizeof(struct lpfc_name));
1359 }
1360
1361 rc = BG_ERR_INIT | BG_ERR_SWAP;
1362
1363
1364 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1365 "0818 BLKGRD: Injecting guard error: "
1366 "read lba x%lx\n", (unsigned long)lba);
1367 }
1368 }
1369 }
1370
1371 return rc;
1372}
1373#endif
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386static int
1387lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1388 uint8_t *txop, uint8_t *rxop)
1389{
1390 uint8_t ret = 0;
1391
1392 if (lpfc_cmd_guard_csum(sc)) {
1393 switch (scsi_get_prot_op(sc)) {
1394 case SCSI_PROT_READ_INSERT:
1395 case SCSI_PROT_WRITE_STRIP:
1396 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1397 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1398 break;
1399
1400 case SCSI_PROT_READ_STRIP:
1401 case SCSI_PROT_WRITE_INSERT:
1402 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1403 *txop = BG_OP_IN_NODIF_OUT_CRC;
1404 break;
1405
1406 case SCSI_PROT_READ_PASS:
1407 case SCSI_PROT_WRITE_PASS:
1408 *rxop = BG_OP_IN_CRC_OUT_CSUM;
1409 *txop = BG_OP_IN_CSUM_OUT_CRC;
1410 break;
1411
1412 case SCSI_PROT_NORMAL:
1413 default:
1414 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1415 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1416 scsi_get_prot_op(sc));
1417 ret = 1;
1418 break;
1419
1420 }
1421 } else {
1422 switch (scsi_get_prot_op(sc)) {
1423 case SCSI_PROT_READ_STRIP:
1424 case SCSI_PROT_WRITE_INSERT:
1425 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1426 *txop = BG_OP_IN_NODIF_OUT_CRC;
1427 break;
1428
1429 case SCSI_PROT_READ_PASS:
1430 case SCSI_PROT_WRITE_PASS:
1431 *rxop = BG_OP_IN_CRC_OUT_CRC;
1432 *txop = BG_OP_IN_CRC_OUT_CRC;
1433 break;
1434
1435 case SCSI_PROT_READ_INSERT:
1436 case SCSI_PROT_WRITE_STRIP:
1437 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1438 *txop = BG_OP_IN_CRC_OUT_NODIF;
1439 break;
1440
1441 case SCSI_PROT_NORMAL:
1442 default:
1443 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1444 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1445 scsi_get_prot_op(sc));
1446 ret = 1;
1447 break;
1448 }
1449 }
1450
1451 return ret;
1452}
1453
1454#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466static int
1467lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1468 uint8_t *txop, uint8_t *rxop)
1469{
1470 uint8_t ret = 0;
1471
1472 if (lpfc_cmd_guard_csum(sc)) {
1473 switch (scsi_get_prot_op(sc)) {
1474 case SCSI_PROT_READ_INSERT:
1475 case SCSI_PROT_WRITE_STRIP:
1476 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1477 *txop = BG_OP_IN_CRC_OUT_NODIF;
1478 break;
1479
1480 case SCSI_PROT_READ_STRIP:
1481 case SCSI_PROT_WRITE_INSERT:
1482 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1483 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1484 break;
1485
1486 case SCSI_PROT_READ_PASS:
1487 case SCSI_PROT_WRITE_PASS:
1488 *rxop = BG_OP_IN_CSUM_OUT_CRC;
1489 *txop = BG_OP_IN_CRC_OUT_CSUM;
1490 break;
1491
1492 case SCSI_PROT_NORMAL:
1493 default:
1494 break;
1495
1496 }
1497 } else {
1498 switch (scsi_get_prot_op(sc)) {
1499 case SCSI_PROT_READ_STRIP:
1500 case SCSI_PROT_WRITE_INSERT:
1501 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1502 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1503 break;
1504
1505 case SCSI_PROT_READ_PASS:
1506 case SCSI_PROT_WRITE_PASS:
1507 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
1508 *txop = BG_OP_IN_CSUM_OUT_CSUM;
1509 break;
1510
1511 case SCSI_PROT_READ_INSERT:
1512 case SCSI_PROT_WRITE_STRIP:
1513 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1514 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1515 break;
1516
1517 case SCSI_PROT_NORMAL:
1518 default:
1519 break;
1520 }
1521 }
1522
1523 return ret;
1524}
1525#endif
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558static int
1559lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1560 struct ulp_bde64 *bpl, int datasegcnt)
1561{
1562 struct scatterlist *sgde = NULL;
1563 struct lpfc_pde5 *pde5 = NULL;
1564 struct lpfc_pde6 *pde6 = NULL;
1565 dma_addr_t physaddr;
1566 int i = 0, num_bde = 0, status;
1567 int datadir = sc->sc_data_direction;
1568#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1569 uint32_t rc;
1570#endif
1571 uint32_t checking = 1;
1572 uint32_t reftag;
1573 uint8_t txop, rxop;
1574
1575 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1576 if (status)
1577 goto out;
1578
1579
1580 reftag = (uint32_t)scsi_get_lba(sc);
1581
1582#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1583 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1584 if (rc) {
1585 if (rc & BG_ERR_SWAP)
1586 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1587 if (rc & BG_ERR_CHECK)
1588 checking = 0;
1589 }
1590#endif
1591
1592
1593 pde5 = (struct lpfc_pde5 *) bpl;
1594 memset(pde5, 0, sizeof(struct lpfc_pde5));
1595 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1596
1597
1598 pde5->word0 = cpu_to_le32(pde5->word0);
1599 pde5->reftag = cpu_to_le32(reftag);
1600
1601
1602 num_bde++;
1603 bpl++;
1604 pde6 = (struct lpfc_pde6 *) bpl;
1605
1606
1607 memset(pde6, 0, sizeof(struct lpfc_pde6));
1608 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1609 bf_set(pde6_optx, pde6, txop);
1610 bf_set(pde6_oprx, pde6, rxop);
1611
1612
1613
1614
1615
1616 if (datadir == DMA_FROM_DEVICE) {
1617 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
1618 bf_set(pde6_ce, pde6, checking);
1619 else
1620 bf_set(pde6_ce, pde6, 0);
1621
1622 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
1623 bf_set(pde6_re, pde6, checking);
1624 else
1625 bf_set(pde6_re, pde6, 0);
1626 }
1627 bf_set(pde6_ai, pde6, 1);
1628 bf_set(pde6_ae, pde6, 0);
1629 bf_set(pde6_apptagval, pde6, 0);
1630
1631
1632 pde6->word0 = cpu_to_le32(pde6->word0);
1633 pde6->word1 = cpu_to_le32(pde6->word1);
1634 pde6->word2 = cpu_to_le32(pde6->word2);
1635
1636
1637 num_bde++;
1638 bpl++;
1639
1640
1641 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1642 physaddr = sg_dma_address(sgde);
1643 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1644 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1645 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1646 if (datadir == DMA_TO_DEVICE)
1647 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1648 else
1649 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1650 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1651 bpl++;
1652 num_bde++;
1653 }
1654
1655out:
1656 return num_bde;
1657}
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698static int
1699lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1700 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1701{
1702 struct scatterlist *sgde = NULL;
1703 struct scatterlist *sgpe = NULL;
1704 struct lpfc_pde5 *pde5 = NULL;
1705 struct lpfc_pde6 *pde6 = NULL;
1706 struct lpfc_pde7 *pde7 = NULL;
1707 dma_addr_t dataphysaddr, protphysaddr;
1708 unsigned short curr_data = 0, curr_prot = 0;
1709 unsigned int split_offset;
1710 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
1711 unsigned int protgrp_blks, protgrp_bytes;
1712 unsigned int remainder, subtotal;
1713 int status;
1714 int datadir = sc->sc_data_direction;
1715 unsigned char pgdone = 0, alldone = 0;
1716 unsigned blksize;
1717#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1718 uint32_t rc;
1719#endif
1720 uint32_t checking = 1;
1721 uint32_t reftag;
1722 uint8_t txop, rxop;
1723 int num_bde = 0;
1724
1725 sgpe = scsi_prot_sglist(sc);
1726 sgde = scsi_sglist(sc);
1727
1728 if (!sgpe || !sgde) {
1729 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1730 "9020 Invalid s/g entry: data=x%px prot=x%px\n",
1731 sgpe, sgde);
1732 return 0;
1733 }
1734
1735 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1736 if (status)
1737 goto out;
1738
1739
1740 blksize = lpfc_cmd_blksize(sc);
1741 reftag = (uint32_t)scsi_get_lba(sc);
1742
1743#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1744 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1745 if (rc) {
1746 if (rc & BG_ERR_SWAP)
1747 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1748 if (rc & BG_ERR_CHECK)
1749 checking = 0;
1750 }
1751#endif
1752
1753 split_offset = 0;
1754 do {
1755
1756 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1757 return num_bde + 3;
1758
1759
1760 pde5 = (struct lpfc_pde5 *) bpl;
1761 memset(pde5, 0, sizeof(struct lpfc_pde5));
1762 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1763
1764
1765 pde5->word0 = cpu_to_le32(pde5->word0);
1766 pde5->reftag = cpu_to_le32(reftag);
1767
1768
1769 num_bde++;
1770 bpl++;
1771 pde6 = (struct lpfc_pde6 *) bpl;
1772
1773
1774 memset(pde6, 0, sizeof(struct lpfc_pde6));
1775 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1776 bf_set(pde6_optx, pde6, txop);
1777 bf_set(pde6_oprx, pde6, rxop);
1778
1779 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
1780 bf_set(pde6_ce, pde6, checking);
1781 else
1782 bf_set(pde6_ce, pde6, 0);
1783
1784 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
1785 bf_set(pde6_re, pde6, checking);
1786 else
1787 bf_set(pde6_re, pde6, 0);
1788
1789 bf_set(pde6_ai, pde6, 1);
1790 bf_set(pde6_ae, pde6, 0);
1791 bf_set(pde6_apptagval, pde6, 0);
1792
1793
1794 pde6->word0 = cpu_to_le32(pde6->word0);
1795 pde6->word1 = cpu_to_le32(pde6->word1);
1796 pde6->word2 = cpu_to_le32(pde6->word2);
1797
1798
1799 num_bde++;
1800 bpl++;
1801
1802
1803 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1804 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
1805
1806
1807 BUG_ON(protgroup_len % 8);
1808
1809 pde7 = (struct lpfc_pde7 *) bpl;
1810 memset(pde7, 0, sizeof(struct lpfc_pde7));
1811 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1812
1813 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1814 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1815
1816 protgrp_blks = protgroup_len / 8;
1817 protgrp_bytes = protgrp_blks * blksize;
1818
1819
1820 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1821 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1822 protgroup_offset += protgroup_remainder;
1823 protgrp_blks = protgroup_remainder / 8;
1824 protgrp_bytes = protgrp_blks * blksize;
1825 } else {
1826 protgroup_offset = 0;
1827 curr_prot++;
1828 }
1829
1830 num_bde++;
1831
1832
1833 pgdone = 0;
1834 subtotal = 0;
1835 while (!pgdone) {
1836
1837 if (num_bde >= phba->cfg_total_seg_cnt)
1838 return num_bde + 1;
1839
1840 if (!sgde) {
1841 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1842 "9065 BLKGRD:%s Invalid data segment\n",
1843 __func__);
1844 return 0;
1845 }
1846 bpl++;
1847 dataphysaddr = sg_dma_address(sgde) + split_offset;
1848 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1849 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1850
1851 remainder = sg_dma_len(sgde) - split_offset;
1852
1853 if ((subtotal + remainder) <= protgrp_bytes) {
1854
1855 bpl->tus.f.bdeSize = remainder;
1856 split_offset = 0;
1857
1858 if ((subtotal + remainder) == protgrp_bytes)
1859 pgdone = 1;
1860 } else {
1861
1862 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1863 split_offset += bpl->tus.f.bdeSize;
1864 }
1865
1866 subtotal += bpl->tus.f.bdeSize;
1867
1868 if (datadir == DMA_TO_DEVICE)
1869 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1870 else
1871 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1872 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1873
1874 num_bde++;
1875 curr_data++;
1876
1877 if (split_offset)
1878 break;
1879
1880
1881 sgde = sg_next(sgde);
1882
1883 }
1884
1885 if (protgroup_offset) {
1886
1887 reftag += protgrp_blks;
1888 bpl++;
1889 continue;
1890 }
1891
1892
1893 if (curr_prot == protcnt) {
1894 alldone = 1;
1895 } else if (curr_prot < protcnt) {
1896
1897 sgpe = sg_next(sgpe);
1898 bpl++;
1899
1900
1901 reftag += protgrp_blks;
1902 } else {
1903
1904 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1905 "9054 BLKGRD: bug in %s\n", __func__);
1906 }
1907
1908 } while (!alldone);
1909out:
1910
1911 return num_bde;
1912}
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943static int
1944lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1945 struct sli4_sge *sgl, int datasegcnt,
1946 struct lpfc_io_buf *lpfc_cmd)
1947{
1948 struct scatterlist *sgde = NULL;
1949 struct sli4_sge_diseed *diseed = NULL;
1950 dma_addr_t physaddr;
1951 int i = 0, num_sge = 0, status;
1952 uint32_t reftag;
1953 uint8_t txop, rxop;
1954#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1955 uint32_t rc;
1956#endif
1957 uint32_t checking = 1;
1958 uint32_t dma_len;
1959 uint32_t dma_offset = 0;
1960 struct sli4_hybrid_sgl *sgl_xtra = NULL;
1961 int j;
1962 bool lsp_just_set = false;
1963
1964 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1965 if (status)
1966 goto out;
1967
1968
1969 reftag = (uint32_t)scsi_get_lba(sc);
1970
1971#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1972 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1973 if (rc) {
1974 if (rc & BG_ERR_SWAP)
1975 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1976 if (rc & BG_ERR_CHECK)
1977 checking = 0;
1978 }
1979#endif
1980
1981
1982 diseed = (struct sli4_sge_diseed *) sgl;
1983 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
1984 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
1985
1986
1987 diseed->ref_tag = cpu_to_le32(reftag);
1988 diseed->ref_tag_tran = diseed->ref_tag;
1989
1990
1991
1992
1993
1994 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
1995 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
1996 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
1997 else
1998 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
1999
2000 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2001 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2002 else
2003 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2004 }
2005
2006
2007 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2008 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2009
2010 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2011 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2012
2013
2014 diseed->word2 = cpu_to_le32(diseed->word2);
2015 diseed->word3 = cpu_to_le32(diseed->word3);
2016
2017
2018 num_sge++;
2019 sgl++;
2020
2021
2022 sgde = scsi_sglist(sc);
2023 j = 3;
2024 for (i = 0; i < datasegcnt; i++) {
2025
2026 sgl->word2 = 0;
2027
2028
2029 if (!lsp_just_set && !((j + 1) % phba->border_sge_num) &&
2030 ((datasegcnt - 1) != i)) {
2031
2032 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2033
2034 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2035
2036 if (unlikely(!sgl_xtra)) {
2037 lpfc_cmd->seg_cnt = 0;
2038 return 0;
2039 }
2040 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2041 sgl_xtra->dma_phys_sgl));
2042 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2043 sgl_xtra->dma_phys_sgl));
2044
2045 } else {
2046 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2047 }
2048
2049 if (!(bf_get(lpfc_sli4_sge_type, sgl) & LPFC_SGE_TYPE_LSP)) {
2050 if ((datasegcnt - 1) == i)
2051 bf_set(lpfc_sli4_sge_last, sgl, 1);
2052 physaddr = sg_dma_address(sgde);
2053 dma_len = sg_dma_len(sgde);
2054 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2055 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2056
2057 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2058 sgl->word2 = cpu_to_le32(sgl->word2);
2059 sgl->sge_len = cpu_to_le32(dma_len);
2060
2061 dma_offset += dma_len;
2062 sgde = sg_next(sgde);
2063
2064 sgl++;
2065 num_sge++;
2066 lsp_just_set = false;
2067
2068 } else {
2069 sgl->word2 = cpu_to_le32(sgl->word2);
2070 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2071
2072 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2073 i = i - 1;
2074
2075 lsp_just_set = true;
2076 }
2077
2078 j++;
2079
2080 }
2081
2082out:
2083 return num_sge;
2084}
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123static int
2124lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2125 struct sli4_sge *sgl, int datacnt, int protcnt,
2126 struct lpfc_io_buf *lpfc_cmd)
2127{
2128 struct scatterlist *sgde = NULL;
2129 struct scatterlist *sgpe = NULL;
2130 struct sli4_sge_diseed *diseed = NULL;
2131 dma_addr_t dataphysaddr, protphysaddr;
2132 unsigned short curr_data = 0, curr_prot = 0;
2133 unsigned int split_offset;
2134 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2135 unsigned int protgrp_blks, protgrp_bytes;
2136 unsigned int remainder, subtotal;
2137 int status;
2138 unsigned char pgdone = 0, alldone = 0;
2139 unsigned blksize;
2140 uint32_t reftag;
2141 uint8_t txop, rxop;
2142 uint32_t dma_len;
2143#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2144 uint32_t rc;
2145#endif
2146 uint32_t checking = 1;
2147 uint32_t dma_offset = 0;
2148 int num_sge = 0, j = 2;
2149 struct sli4_hybrid_sgl *sgl_xtra = NULL;
2150
2151 sgpe = scsi_prot_sglist(sc);
2152 sgde = scsi_sglist(sc);
2153
2154 if (!sgpe || !sgde) {
2155 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2156 "9082 Invalid s/g entry: data=x%px prot=x%px\n",
2157 sgpe, sgde);
2158 return 0;
2159 }
2160
2161 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2162 if (status)
2163 goto out;
2164
2165
2166 blksize = lpfc_cmd_blksize(sc);
2167 reftag = (uint32_t)scsi_get_lba(sc);
2168
2169#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2170 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2171 if (rc) {
2172 if (rc & BG_ERR_SWAP)
2173 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2174 if (rc & BG_ERR_CHECK)
2175 checking = 0;
2176 }
2177#endif
2178
2179 split_offset = 0;
2180 do {
2181
2182 if ((num_sge >= (phba->cfg_total_seg_cnt - 2)) &&
2183 !(phba->cfg_xpsgl))
2184 return num_sge + 3;
2185
2186
2187 if (!((j + 1) % phba->border_sge_num) ||
2188 !((j + 2) % phba->border_sge_num) ||
2189 !((j + 3) % phba->border_sge_num)) {
2190 sgl->word2 = 0;
2191
2192
2193 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2194
2195 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2196
2197 if (unlikely(!sgl_xtra)) {
2198 goto out;
2199 } else {
2200 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2201 sgl_xtra->dma_phys_sgl));
2202 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2203 sgl_xtra->dma_phys_sgl));
2204 }
2205
2206 sgl->word2 = cpu_to_le32(sgl->word2);
2207 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2208
2209 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2210 j = 0;
2211 }
2212
2213
2214 diseed = (struct sli4_sge_diseed *) sgl;
2215 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2216 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2217
2218
2219 diseed->ref_tag = cpu_to_le32(reftag);
2220 diseed->ref_tag_tran = diseed->ref_tag;
2221
2222 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
2223 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2224
2225 } else {
2226 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2227
2228
2229
2230
2231
2232
2233
2234 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2235 txop = BG_OP_RAW_MODE;
2236 rxop = BG_OP_RAW_MODE;
2237 }
2238 }
2239
2240
2241 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2242 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2243 else
2244 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2245
2246
2247 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2248 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2249
2250 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2251 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2252
2253
2254 diseed->word2 = cpu_to_le32(diseed->word2);
2255 diseed->word3 = cpu_to_le32(diseed->word3);
2256
2257
2258 num_sge++;
2259
2260 sgl++;
2261 j++;
2262
2263
2264 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2265 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2266
2267
2268 BUG_ON(protgroup_len % 8);
2269
2270
2271 sgl->word2 = 0;
2272 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2273 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2274 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2275 sgl->word2 = cpu_to_le32(sgl->word2);
2276 sgl->sge_len = 0;
2277
2278 protgrp_blks = protgroup_len / 8;
2279 protgrp_bytes = protgrp_blks * blksize;
2280
2281
2282 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2283 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2284 protgroup_offset += protgroup_remainder;
2285 protgrp_blks = protgroup_remainder / 8;
2286 protgrp_bytes = protgrp_blks * blksize;
2287 } else {
2288 protgroup_offset = 0;
2289 curr_prot++;
2290 }
2291
2292 num_sge++;
2293
2294
2295 pgdone = 0;
2296 subtotal = 0;
2297
2298 sgl++;
2299 j++;
2300
2301 while (!pgdone) {
2302
2303 if ((num_sge >= phba->cfg_total_seg_cnt) &&
2304 !phba->cfg_xpsgl)
2305 return num_sge + 1;
2306
2307 if (!sgde) {
2308 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2309 "9086 BLKGRD:%s Invalid data segment\n",
2310 __func__);
2311 return 0;
2312 }
2313
2314 if (!((j + 1) % phba->border_sge_num)) {
2315 sgl->word2 = 0;
2316
2317
2318 bf_set(lpfc_sli4_sge_type, sgl,
2319 LPFC_SGE_TYPE_LSP);
2320
2321 sgl_xtra = lpfc_get_sgl_per_hdwq(phba,
2322 lpfc_cmd);
2323
2324 if (unlikely(!sgl_xtra)) {
2325 goto out;
2326 } else {
2327 sgl->addr_lo = cpu_to_le32(
2328 putPaddrLow(sgl_xtra->dma_phys_sgl));
2329 sgl->addr_hi = cpu_to_le32(
2330 putPaddrHigh(sgl_xtra->dma_phys_sgl));
2331 }
2332
2333 sgl->word2 = cpu_to_le32(sgl->word2);
2334 sgl->sge_len = cpu_to_le32(
2335 phba->cfg_sg_dma_buf_size);
2336
2337 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2338 } else {
2339 dataphysaddr = sg_dma_address(sgde) +
2340 split_offset;
2341
2342 remainder = sg_dma_len(sgde) - split_offset;
2343
2344 if ((subtotal + remainder) <= protgrp_bytes) {
2345
2346 dma_len = remainder;
2347 split_offset = 0;
2348
2349 if ((subtotal + remainder) ==
2350 protgrp_bytes)
2351 pgdone = 1;
2352 } else {
2353
2354
2355
2356 dma_len = protgrp_bytes - subtotal;
2357 split_offset += dma_len;
2358 }
2359
2360 subtotal += dma_len;
2361
2362 sgl->word2 = 0;
2363 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2364 dataphysaddr));
2365 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2366 dataphysaddr));
2367 bf_set(lpfc_sli4_sge_last, sgl, 0);
2368 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2369 bf_set(lpfc_sli4_sge_type, sgl,
2370 LPFC_SGE_TYPE_DATA);
2371
2372 sgl->sge_len = cpu_to_le32(dma_len);
2373 dma_offset += dma_len;
2374
2375 num_sge++;
2376 curr_data++;
2377
2378 if (split_offset) {
2379 sgl++;
2380 j++;
2381 break;
2382 }
2383
2384
2385 sgde = sg_next(sgde);
2386
2387 sgl++;
2388 }
2389
2390 j++;
2391 }
2392
2393 if (protgroup_offset) {
2394
2395 reftag += protgrp_blks;
2396 continue;
2397 }
2398
2399
2400 if (curr_prot == protcnt) {
2401
2402 sgl--;
2403 bf_set(lpfc_sli4_sge_last, sgl, 1);
2404 alldone = 1;
2405 } else if (curr_prot < protcnt) {
2406
2407 sgpe = sg_next(sgpe);
2408
2409
2410 reftag += protgrp_blks;
2411 } else {
2412
2413 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2414 "9085 BLKGRD: bug in %s\n", __func__);
2415 }
2416
2417 } while (!alldone);
2418
2419out:
2420
2421 return num_sge;
2422}
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435static int
2436lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2437{
2438 int ret = LPFC_PG_TYPE_INVALID;
2439 unsigned char op = scsi_get_prot_op(sc);
2440
2441 switch (op) {
2442 case SCSI_PROT_READ_STRIP:
2443 case SCSI_PROT_WRITE_INSERT:
2444 ret = LPFC_PG_TYPE_NO_DIF;
2445 break;
2446 case SCSI_PROT_READ_INSERT:
2447 case SCSI_PROT_WRITE_STRIP:
2448 case SCSI_PROT_READ_PASS:
2449 case SCSI_PROT_WRITE_PASS:
2450 ret = LPFC_PG_TYPE_DIF_BUF;
2451 break;
2452 default:
2453 if (phba)
2454 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2455 "9021 Unsupported protection op:%d\n",
2456 op);
2457 break;
2458 }
2459 return ret;
2460}
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472static int
2473lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2474 struct lpfc_io_buf *lpfc_cmd)
2475{
2476 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2477 int fcpdl;
2478
2479 fcpdl = scsi_bufflen(sc);
2480
2481
2482 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2483
2484 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2485 return fcpdl;
2486
2487 } else {
2488
2489 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2490 return fcpdl;
2491 }
2492
2493
2494
2495
2496
2497
2498 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
2499
2500 return fcpdl;
2501}
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515static int
2516lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
2517 struct lpfc_io_buf *lpfc_cmd)
2518{
2519 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2520 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2521 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
2522 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2523 uint32_t num_bde = 0;
2524 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2525 int prot_group_type = 0;
2526 int fcpdl;
2527 int ret = 1;
2528 struct lpfc_vport *vport = phba->pport;
2529
2530
2531
2532
2533
2534 bpl += 2;
2535 if (scsi_sg_count(scsi_cmnd)) {
2536
2537
2538
2539
2540
2541
2542 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2543 scsi_sglist(scsi_cmnd),
2544 scsi_sg_count(scsi_cmnd), datadir);
2545 if (unlikely(!datasegcnt))
2546 return 1;
2547
2548 lpfc_cmd->seg_cnt = datasegcnt;
2549
2550
2551 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2552 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
2553 ret = 2;
2554 goto err;
2555 }
2556
2557 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2558
2559 switch (prot_group_type) {
2560 case LPFC_PG_TYPE_NO_DIF:
2561
2562
2563 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt) {
2564 ret = 2;
2565 goto err;
2566 }
2567
2568 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2569 datasegcnt);
2570
2571 if (num_bde < 2) {
2572 ret = 2;
2573 goto err;
2574 }
2575 break;
2576
2577 case LPFC_PG_TYPE_DIF_BUF:
2578
2579
2580
2581
2582
2583 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2584 scsi_prot_sglist(scsi_cmnd),
2585 scsi_prot_sg_count(scsi_cmnd), datadir);
2586 if (unlikely(!protsegcnt)) {
2587 scsi_dma_unmap(scsi_cmnd);
2588 return 1;
2589 }
2590
2591 lpfc_cmd->prot_seg_cnt = protsegcnt;
2592
2593
2594
2595
2596
2597 if ((lpfc_cmd->prot_seg_cnt * 4) >
2598 (phba->cfg_total_seg_cnt - 2)) {
2599 ret = 2;
2600 goto err;
2601 }
2602
2603 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2604 datasegcnt, protsegcnt);
2605
2606 if ((num_bde < 3) ||
2607 (num_bde > phba->cfg_total_seg_cnt)) {
2608 ret = 2;
2609 goto err;
2610 }
2611 break;
2612
2613 case LPFC_PG_TYPE_INVALID:
2614 default:
2615 scsi_dma_unmap(scsi_cmnd);
2616 lpfc_cmd->seg_cnt = 0;
2617
2618 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2619 "9022 Unexpected protection group %i\n",
2620 prot_group_type);
2621 return 2;
2622 }
2623 }
2624
2625
2626
2627
2628
2629
2630
2631 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2632 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2633 iocb_cmd->ulpBdeCount = 1;
2634 iocb_cmd->ulpLe = 1;
2635
2636 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
2637 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2638
2639
2640
2641
2642
2643 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2644
2645
2646
2647
2648
2649 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2650 (fcpdl < vport->cfg_first_burst_size))
2651 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2652
2653 return 0;
2654err:
2655 if (lpfc_cmd->seg_cnt)
2656 scsi_dma_unmap(scsi_cmnd);
2657 if (lpfc_cmd->prot_seg_cnt)
2658 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2659 scsi_prot_sg_count(scsi_cmnd),
2660 scsi_cmnd->sc_data_direction);
2661
2662 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2663 "9023 Cannot setup S/G List for HBA"
2664 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2665 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2666 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
2667 prot_group_type, num_bde);
2668
2669 lpfc_cmd->seg_cnt = 0;
2670 lpfc_cmd->prot_seg_cnt = 0;
2671 return ret;
2672}
2673
2674
2675
2676
2677
2678
2679static uint16_t
2680lpfc_bg_crc(uint8_t *data, int count)
2681{
2682 uint16_t crc = 0;
2683 uint16_t x;
2684
2685 crc = crc_t10dif(data, count);
2686 x = cpu_to_be16(crc);
2687 return x;
2688}
2689
2690
2691
2692
2693
2694
2695static uint16_t
2696lpfc_bg_csum(uint8_t *data, int count)
2697{
2698 uint16_t ret;
2699
2700 ret = ip_compute_csum(data, count);
2701 return ret;
2702}
2703
2704
2705
2706
2707
2708static void
2709lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
2710{
2711 struct scatterlist *sgpe;
2712 struct scatterlist *sgde;
2713 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2714 struct scsi_dif_tuple *src = NULL;
2715 uint8_t *data_src = NULL;
2716 uint16_t guard_tag;
2717 uint16_t start_app_tag, app_tag;
2718 uint32_t start_ref_tag, ref_tag;
2719 int prot, protsegcnt;
2720 int err_type, len, data_len;
2721 int chk_ref, chk_app, chk_guard;
2722 uint16_t sum;
2723 unsigned blksize;
2724
2725 err_type = BGS_GUARD_ERR_MASK;
2726 sum = 0;
2727 guard_tag = 0;
2728
2729
2730 prot = scsi_get_prot_op(cmd);
2731 if ((prot == SCSI_PROT_READ_STRIP) ||
2732 (prot == SCSI_PROT_WRITE_INSERT) ||
2733 (prot == SCSI_PROT_NORMAL))
2734 goto out;
2735
2736
2737 chk_ref = 1;
2738 chk_app = 0;
2739 chk_guard = 0;
2740
2741
2742 sgpe = scsi_prot_sglist(cmd);
2743 protsegcnt = lpfc_cmd->prot_seg_cnt;
2744
2745 if (sgpe && protsegcnt) {
2746
2747
2748
2749
2750
2751 sgde = scsi_sglist(cmd);
2752 blksize = lpfc_cmd_blksize(cmd);
2753 data_src = (uint8_t *)sg_virt(sgde);
2754 data_len = sgde->length;
2755 if ((data_len & (blksize - 1)) == 0)
2756 chk_guard = 1;
2757
2758 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2759 start_ref_tag = (uint32_t)scsi_get_lba(cmd);
2760 start_app_tag = src->app_tag;
2761 len = sgpe->length;
2762 while (src && protsegcnt) {
2763 while (len) {
2764
2765
2766
2767
2768
2769 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2770 (src->app_tag == T10_PI_APP_ESCAPE)) {
2771 start_ref_tag++;
2772 goto skipit;
2773 }
2774
2775
2776 if (chk_guard) {
2777 guard_tag = src->guard_tag;
2778 if (lpfc_cmd_guard_csum(cmd))
2779 sum = lpfc_bg_csum(data_src,
2780 blksize);
2781 else
2782 sum = lpfc_bg_crc(data_src,
2783 blksize);
2784 if ((guard_tag != sum)) {
2785 err_type = BGS_GUARD_ERR_MASK;
2786 goto out;
2787 }
2788 }
2789
2790
2791 ref_tag = be32_to_cpu(src->ref_tag);
2792 if (chk_ref && (ref_tag != start_ref_tag)) {
2793 err_type = BGS_REFTAG_ERR_MASK;
2794 goto out;
2795 }
2796 start_ref_tag++;
2797
2798
2799 app_tag = src->app_tag;
2800 if (chk_app && (app_tag != start_app_tag)) {
2801 err_type = BGS_APPTAG_ERR_MASK;
2802 goto out;
2803 }
2804skipit:
2805 len -= sizeof(struct scsi_dif_tuple);
2806 if (len < 0)
2807 len = 0;
2808 src++;
2809
2810 data_src += blksize;
2811 data_len -= blksize;
2812
2813
2814
2815
2816
2817
2818 if (chk_guard && (data_len == 0)) {
2819 chk_guard = 0;
2820 sgde = sg_next(sgde);
2821 if (!sgde)
2822 goto out;
2823
2824 data_src = (uint8_t *)sg_virt(sgde);
2825 data_len = sgde->length;
2826 if ((data_len & (blksize - 1)) == 0)
2827 chk_guard = 1;
2828 }
2829 }
2830
2831
2832 sgpe = sg_next(sgpe);
2833 if (sgpe) {
2834 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2835 len = sgpe->length;
2836 } else {
2837 src = NULL;
2838 }
2839 protsegcnt--;
2840 }
2841 }
2842out:
2843 if (err_type == BGS_GUARD_ERR_MASK) {
2844 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2845 0x10, 0x1);
2846 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2847 SAM_STAT_CHECK_CONDITION;
2848 phba->bg_guard_err_cnt++;
2849 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2850 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
2851 (unsigned long)scsi_get_lba(cmd),
2852 sum, guard_tag);
2853
2854 } else if (err_type == BGS_REFTAG_ERR_MASK) {
2855 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2856 0x10, 0x3);
2857 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2858 SAM_STAT_CHECK_CONDITION;
2859
2860 phba->bg_reftag_err_cnt++;
2861 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2862 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
2863 (unsigned long)scsi_get_lba(cmd),
2864 ref_tag, start_ref_tag);
2865
2866 } else if (err_type == BGS_APPTAG_ERR_MASK) {
2867 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2868 0x10, 0x2);
2869 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2870 SAM_STAT_CHECK_CONDITION;
2871
2872 phba->bg_apptag_err_cnt++;
2873 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2874 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
2875 (unsigned long)scsi_get_lba(cmd),
2876 app_tag, start_app_tag);
2877 }
2878}
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893static int
2894lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
2895 struct lpfc_iocbq *pIocbOut)
2896{
2897 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2898 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2899 int ret = 0;
2900 uint32_t bghm = bgf->bghm;
2901 uint32_t bgstat = bgf->bgstat;
2902 uint64_t failing_sector = 0;
2903
2904 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2905 cmd->result = DID_ERROR << 16;
2906 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2907 "9072 BLKGRD: Invalid BG Profile in cmd"
2908 " 0x%x lba 0x%llx blk cnt 0x%x "
2909 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2910 (unsigned long long)scsi_get_lba(cmd),
2911 blk_rq_sectors(cmd->request), bgstat, bghm);
2912 ret = (-1);
2913 goto out;
2914 }
2915
2916 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2917 cmd->result = DID_ERROR << 16;
2918 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2919 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
2920 " 0x%x lba 0x%llx blk cnt 0x%x "
2921 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2922 (unsigned long long)scsi_get_lba(cmd),
2923 blk_rq_sectors(cmd->request), bgstat, bghm);
2924 ret = (-1);
2925 goto out;
2926 }
2927
2928 if (lpfc_bgs_get_guard_err(bgstat)) {
2929 ret = 1;
2930
2931 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2932 0x10, 0x1);
2933 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2934 SAM_STAT_CHECK_CONDITION;
2935 phba->bg_guard_err_cnt++;
2936 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2937 "9055 BLKGRD: Guard Tag error in cmd"
2938 " 0x%x lba 0x%llx blk cnt 0x%x "
2939 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2940 (unsigned long long)scsi_get_lba(cmd),
2941 blk_rq_sectors(cmd->request), bgstat, bghm);
2942 }
2943
2944 if (lpfc_bgs_get_reftag_err(bgstat)) {
2945 ret = 1;
2946
2947 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2948 0x10, 0x3);
2949 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2950 SAM_STAT_CHECK_CONDITION;
2951
2952 phba->bg_reftag_err_cnt++;
2953 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2954 "9056 BLKGRD: Ref Tag error in cmd"
2955 " 0x%x lba 0x%llx blk cnt 0x%x "
2956 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2957 (unsigned long long)scsi_get_lba(cmd),
2958 blk_rq_sectors(cmd->request), bgstat, bghm);
2959 }
2960
2961 if (lpfc_bgs_get_apptag_err(bgstat)) {
2962 ret = 1;
2963
2964 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2965 0x10, 0x2);
2966 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2967 SAM_STAT_CHECK_CONDITION;
2968
2969 phba->bg_apptag_err_cnt++;
2970 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2971 "9061 BLKGRD: App Tag error in cmd"
2972 " 0x%x lba 0x%llx blk cnt 0x%x "
2973 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2974 (unsigned long long)scsi_get_lba(cmd),
2975 blk_rq_sectors(cmd->request), bgstat, bghm);
2976 }
2977
2978 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2979
2980
2981
2982
2983
2984
2985 cmd->sense_buffer[7] = 0xc;
2986 cmd->sense_buffer[8] = 0;
2987 cmd->sense_buffer[9] = 0xa;
2988 cmd->sense_buffer[10] = 0x80;
2989
2990
2991 switch (scsi_get_prot_op(cmd)) {
2992 case SCSI_PROT_READ_INSERT:
2993 case SCSI_PROT_WRITE_STRIP:
2994 bghm /= cmd->device->sector_size;
2995 break;
2996 case SCSI_PROT_READ_STRIP:
2997 case SCSI_PROT_WRITE_INSERT:
2998 case SCSI_PROT_READ_PASS:
2999 case SCSI_PROT_WRITE_PASS:
3000 bghm /= (cmd->device->sector_size +
3001 sizeof(struct scsi_dif_tuple));
3002 break;
3003 }
3004
3005 failing_sector = scsi_get_lba(cmd);
3006 failing_sector += bghm;
3007
3008
3009 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
3010 }
3011
3012 if (!ret) {
3013
3014 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3015 "9057 BLKGRD: Unknown error in cmd"
3016 " 0x%x lba 0x%llx blk cnt 0x%x "
3017 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3018 (unsigned long long)scsi_get_lba(cmd),
3019 blk_rq_sectors(cmd->request), bgstat, bghm);
3020
3021
3022 lpfc_calc_bg_err(phba, lpfc_cmd);
3023 }
3024out:
3025 return ret;
3026}
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041static int
3042lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3043{
3044 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3045 struct scatterlist *sgel = NULL;
3046 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3047 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
3048 struct sli4_sge *first_data_sgl;
3049 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3050 dma_addr_t physaddr;
3051 uint32_t num_bde = 0;
3052 uint32_t dma_len;
3053 uint32_t dma_offset = 0;
3054 int nseg, i, j;
3055 struct ulp_bde64 *bde;
3056 bool lsp_just_set = false;
3057 struct sli4_hybrid_sgl *sgl_xtra = NULL;
3058
3059
3060
3061
3062
3063
3064
3065 if (scsi_sg_count(scsi_cmnd)) {
3066
3067
3068
3069
3070
3071
3072
3073 nseg = scsi_dma_map(scsi_cmnd);
3074 if (unlikely(nseg <= 0))
3075 return 1;
3076 sgl += 1;
3077
3078 sgl->word2 = le32_to_cpu(sgl->word2);
3079 bf_set(lpfc_sli4_sge_last, sgl, 0);
3080 sgl->word2 = cpu_to_le32(sgl->word2);
3081 sgl += 1;
3082 first_data_sgl = sgl;
3083 lpfc_cmd->seg_cnt = nseg;
3084 if (!phba->cfg_xpsgl &&
3085 lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3086 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3087 " %s: Too many sg segments from "
3088 "dma_map_sg. Config %d, seg_cnt %d\n",
3089 __func__, phba->cfg_sg_seg_cnt,
3090 lpfc_cmd->seg_cnt);
3091 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3092 lpfc_cmd->seg_cnt = 0;
3093 scsi_dma_unmap(scsi_cmnd);
3094 return 2;
3095 }
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108 sgel = scsi_sglist(scsi_cmnd);
3109 j = 2;
3110 for (i = 0; i < nseg; i++) {
3111 sgl->word2 = 0;
3112 if ((num_bde + 1) == nseg) {
3113 bf_set(lpfc_sli4_sge_last, sgl, 1);
3114 bf_set(lpfc_sli4_sge_type, sgl,
3115 LPFC_SGE_TYPE_DATA);
3116 } else {
3117 bf_set(lpfc_sli4_sge_last, sgl, 0);
3118
3119
3120 if (!lsp_just_set &&
3121 !((j + 1) % phba->border_sge_num) &&
3122 ((nseg - 1) != i)) {
3123
3124 bf_set(lpfc_sli4_sge_type, sgl,
3125 LPFC_SGE_TYPE_LSP);
3126
3127 sgl_xtra = lpfc_get_sgl_per_hdwq(
3128 phba, lpfc_cmd);
3129
3130 if (unlikely(!sgl_xtra)) {
3131 lpfc_cmd->seg_cnt = 0;
3132 scsi_dma_unmap(scsi_cmnd);
3133 return 1;
3134 }
3135 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3136 sgl_xtra->dma_phys_sgl));
3137 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3138 sgl_xtra->dma_phys_sgl));
3139
3140 } else {
3141 bf_set(lpfc_sli4_sge_type, sgl,
3142 LPFC_SGE_TYPE_DATA);
3143 }
3144 }
3145
3146 if (!(bf_get(lpfc_sli4_sge_type, sgl) &
3147 LPFC_SGE_TYPE_LSP)) {
3148 if ((nseg - 1) == i)
3149 bf_set(lpfc_sli4_sge_last, sgl, 1);
3150
3151 physaddr = sg_dma_address(sgel);
3152 dma_len = sg_dma_len(sgel);
3153 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3154 physaddr));
3155 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3156 physaddr));
3157
3158 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3159 sgl->word2 = cpu_to_le32(sgl->word2);
3160 sgl->sge_len = cpu_to_le32(dma_len);
3161
3162 dma_offset += dma_len;
3163 sgel = sg_next(sgel);
3164
3165 sgl++;
3166 lsp_just_set = false;
3167
3168 } else {
3169 sgl->word2 = cpu_to_le32(sgl->word2);
3170 sgl->sge_len = cpu_to_le32(
3171 phba->cfg_sg_dma_buf_size);
3172
3173 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
3174 i = i - 1;
3175
3176 lsp_just_set = true;
3177 }
3178
3179 j++;
3180 }
3181
3182
3183
3184
3185
3186 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3187 phba->cfg_enable_pbde) {
3188 bde = (struct ulp_bde64 *)
3189 &(iocb_cmd->unsli3.sli3Words[5]);
3190 bde->addrLow = first_data_sgl->addr_lo;
3191 bde->addrHigh = first_data_sgl->addr_hi;
3192 bde->tus.f.bdeSize =
3193 le32_to_cpu(first_data_sgl->sge_len);
3194 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3195 bde->tus.w = cpu_to_le32(bde->tus.w);
3196 }
3197 } else {
3198 sgl += 1;
3199
3200 sgl->word2 = le32_to_cpu(sgl->word2);
3201 bf_set(lpfc_sli4_sge_last, sgl, 1);
3202 sgl->word2 = cpu_to_le32(sgl->word2);
3203
3204 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3205 phba->cfg_enable_pbde) {
3206 bde = (struct ulp_bde64 *)
3207 &(iocb_cmd->unsli3.sli3Words[5]);
3208 memset(bde, 0, (sizeof(uint32_t) * 3));
3209 }
3210 }
3211
3212
3213
3214
3215
3216
3217
3218 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3219
3220
3221
3222
3223
3224 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3225
3226
3227
3228
3229
3230 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3231 scsi_cmnd->device->hostdata)->oas_enabled) {
3232 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3233 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3234 scsi_cmnd->device->hostdata)->priority;
3235 }
3236
3237 return 0;
3238}
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253static int
3254lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3255 struct lpfc_io_buf *lpfc_cmd)
3256{
3257 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3258 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3259 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
3260 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3261 uint32_t num_sge = 0;
3262 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3263 int prot_group_type = 0;
3264 int fcpdl;
3265 int ret = 1;
3266 struct lpfc_vport *vport = phba->pport;
3267
3268
3269
3270
3271
3272 if (scsi_sg_count(scsi_cmnd)) {
3273
3274
3275
3276
3277
3278
3279 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3280 scsi_sglist(scsi_cmnd),
3281 scsi_sg_count(scsi_cmnd), datadir);
3282 if (unlikely(!datasegcnt))
3283 return 1;
3284
3285 sgl += 1;
3286
3287 sgl->word2 = le32_to_cpu(sgl->word2);
3288 bf_set(lpfc_sli4_sge_last, sgl, 0);
3289 sgl->word2 = cpu_to_le32(sgl->word2);
3290
3291 sgl += 1;
3292 lpfc_cmd->seg_cnt = datasegcnt;
3293
3294
3295 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt &&
3296 !phba->cfg_xpsgl) {
3297 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3298 ret = 2;
3299 goto err;
3300 }
3301
3302 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3303
3304 switch (prot_group_type) {
3305 case LPFC_PG_TYPE_NO_DIF:
3306
3307 if (((lpfc_cmd->seg_cnt + 1) >
3308 phba->cfg_total_seg_cnt) &&
3309 !phba->cfg_xpsgl) {
3310 ret = 2;
3311 goto err;
3312 }
3313
3314 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3315 datasegcnt, lpfc_cmd);
3316
3317
3318 if (num_sge < 2) {
3319 ret = 2;
3320 goto err;
3321 }
3322 break;
3323
3324 case LPFC_PG_TYPE_DIF_BUF:
3325
3326
3327
3328
3329
3330 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3331 scsi_prot_sglist(scsi_cmnd),
3332 scsi_prot_sg_count(scsi_cmnd), datadir);
3333 if (unlikely(!protsegcnt)) {
3334 scsi_dma_unmap(scsi_cmnd);
3335 return 1;
3336 }
3337
3338 lpfc_cmd->prot_seg_cnt = protsegcnt;
3339
3340
3341
3342
3343 if (((lpfc_cmd->prot_seg_cnt * 3) >
3344 (phba->cfg_total_seg_cnt - 2)) &&
3345 !phba->cfg_xpsgl) {
3346 ret = 2;
3347 goto err;
3348 }
3349
3350 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3351 datasegcnt, protsegcnt, lpfc_cmd);
3352
3353
3354 if (num_sge < 3 ||
3355 (num_sge > phba->cfg_total_seg_cnt &&
3356 !phba->cfg_xpsgl)) {
3357 ret = 2;
3358 goto err;
3359 }
3360 break;
3361
3362 case LPFC_PG_TYPE_INVALID:
3363 default:
3364 scsi_dma_unmap(scsi_cmnd);
3365 lpfc_cmd->seg_cnt = 0;
3366
3367 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3368 "9083 Unexpected protection group %i\n",
3369 prot_group_type);
3370 return 2;
3371 }
3372 }
3373
3374 switch (scsi_get_prot_op(scsi_cmnd)) {
3375 case SCSI_PROT_WRITE_STRIP:
3376 case SCSI_PROT_READ_STRIP:
3377 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3378 break;
3379 case SCSI_PROT_WRITE_INSERT:
3380 case SCSI_PROT_READ_INSERT:
3381 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3382 break;
3383 case SCSI_PROT_WRITE_PASS:
3384 case SCSI_PROT_READ_PASS:
3385 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3386 break;
3387 }
3388
3389 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3390 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3391
3392
3393
3394
3395
3396 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3397
3398
3399
3400
3401
3402 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
3403 (fcpdl < vport->cfg_first_burst_size))
3404 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
3405
3406
3407
3408
3409
3410 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3411 scsi_cmnd->device->hostdata)->oas_enabled)
3412 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3413
3414 return 0;
3415err:
3416 if (lpfc_cmd->seg_cnt)
3417 scsi_dma_unmap(scsi_cmnd);
3418 if (lpfc_cmd->prot_seg_cnt)
3419 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3420 scsi_prot_sg_count(scsi_cmnd),
3421 scsi_cmnd->sc_data_direction);
3422
3423 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3424 "9084 Cannot setup S/G List for HBA"
3425 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3426 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3427 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3428 prot_group_type, num_sge);
3429
3430 lpfc_cmd->seg_cnt = 0;
3431 lpfc_cmd->prot_seg_cnt = 0;
3432 return ret;
3433}
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447static inline int
3448lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3449{
3450 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3451}
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466static inline int
3467lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3468{
3469 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3470}
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482static void
3483lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3484 struct lpfc_io_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3485 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3486 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3487 uint32_t resp_info = fcprsp->rspStatus2;
3488 uint32_t scsi_status = fcprsp->rspStatus3;
3489 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3490 struct lpfc_fast_path_event *fast_path_evt = NULL;
3491 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3492 unsigned long flags;
3493
3494 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3495 return;
3496
3497
3498 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3499 (cmnd->result == SAM_STAT_BUSY)) {
3500 fast_path_evt = lpfc_alloc_fast_evt(phba);
3501 if (!fast_path_evt)
3502 return;
3503 fast_path_evt->un.scsi_evt.event_type =
3504 FC_REG_SCSI_EVENT;
3505 fast_path_evt->un.scsi_evt.subcategory =
3506 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3507 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3508 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3509 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3510 &pnode->nlp_portname, sizeof(struct lpfc_name));
3511 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3512 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3513 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3514 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3515 fast_path_evt = lpfc_alloc_fast_evt(phba);
3516 if (!fast_path_evt)
3517 return;
3518 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3519 FC_REG_SCSI_EVENT;
3520 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3521 LPFC_EVENT_CHECK_COND;
3522 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3523 cmnd->device->lun;
3524 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3525 &pnode->nlp_portname, sizeof(struct lpfc_name));
3526 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3527 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3528 fast_path_evt->un.check_cond_evt.sense_key =
3529 cmnd->sense_buffer[2] & 0xf;
3530 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3531 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3532 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3533 fcpi_parm &&
3534 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3535 ((scsi_status == SAM_STAT_GOOD) &&
3536 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3537
3538
3539
3540
3541 fast_path_evt = lpfc_alloc_fast_evt(phba);
3542 if (!fast_path_evt)
3543 return;
3544 fast_path_evt->un.read_check_error.header.event_type =
3545 FC_REG_FABRIC_EVENT;
3546 fast_path_evt->un.read_check_error.header.subcategory =
3547 LPFC_EVENT_FCPRDCHKERR;
3548 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3549 &pnode->nlp_portname, sizeof(struct lpfc_name));
3550 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3551 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3552 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3553 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3554 fast_path_evt->un.read_check_error.fcpiparam =
3555 fcpi_parm;
3556 } else
3557 return;
3558
3559 fast_path_evt->vport = vport;
3560 spin_lock_irqsave(&phba->hbalock, flags);
3561 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3562 spin_unlock_irqrestore(&phba->hbalock, flags);
3563 lpfc_worker_wake_up(phba);
3564 return;
3565}
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575static void
3576lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
3577{
3578
3579
3580
3581
3582
3583
3584 if (psb->seg_cnt > 0)
3585 scsi_dma_unmap(psb->pCmd);
3586 if (psb->prot_seg_cnt > 0)
3587 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3588 scsi_prot_sg_count(psb->pCmd),
3589 psb->pCmd->sc_data_direction);
3590}
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602static void
3603lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
3604 struct lpfc_iocbq *rsp_iocb)
3605{
3606 struct lpfc_hba *phba = vport->phba;
3607 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3608 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3609 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3610 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3611 uint32_t resp_info = fcprsp->rspStatus2;
3612 uint32_t scsi_status = fcprsp->rspStatus3;
3613 uint32_t *lp;
3614 uint32_t host_status = DID_OK;
3615 uint32_t rsplen = 0;
3616 uint32_t fcpDl;
3617 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
3618
3619
3620
3621
3622
3623
3624
3625 if (fcpcmd->fcpCntl2) {
3626 scsi_status = 0;
3627 goto out;
3628 }
3629
3630 if (resp_info & RSP_LEN_VALID) {
3631 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3632 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
3633 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3634 "2719 Invalid response length: "
3635 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
3636 cmnd->device->id,
3637 cmnd->device->lun, cmnd->cmnd[0],
3638 rsplen);
3639 host_status = DID_ERROR;
3640 goto out;
3641 }
3642 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3643 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3644 "2757 Protocol failure detected during "
3645 "processing of FCP I/O op: "
3646 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
3647 cmnd->device->id,
3648 cmnd->device->lun, cmnd->cmnd[0],
3649 fcprsp->rspInfo3);
3650 host_status = DID_ERROR;
3651 goto out;
3652 }
3653 }
3654
3655 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3656 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3657 if (snslen > SCSI_SENSE_BUFFERSIZE)
3658 snslen = SCSI_SENSE_BUFFERSIZE;
3659
3660 if (resp_info & RSP_LEN_VALID)
3661 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3662 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3663 }
3664 lp = (uint32_t *)cmnd->sense_buffer;
3665
3666
3667 if (!scsi_status && (resp_info & RESID_UNDER)) {
3668
3669 if (vport->cfg_log_verbose & LOG_FCP)
3670 logit = LOG_FCP_ERROR;
3671
3672 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3673 logit = LOG_FCP_UNDER;
3674 }
3675
3676 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3677 "9024 FCP command x%x failed: x%x SNS x%x x%x "
3678 "Data: x%x x%x x%x x%x x%x\n",
3679 cmnd->cmnd[0], scsi_status,
3680 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3681 be32_to_cpu(fcprsp->rspResId),
3682 be32_to_cpu(fcprsp->rspSnsLen),
3683 be32_to_cpu(fcprsp->rspRspLen),
3684 fcprsp->rspInfo3);
3685
3686 scsi_set_resid(cmnd, 0);
3687 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
3688 if (resp_info & RESID_UNDER) {
3689 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
3690
3691 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
3692 "9025 FCP Underrun, expected %d, "
3693 "residual %d Data: x%x x%x x%x\n",
3694 fcpDl,
3695 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3696 cmnd->underflow);
3697
3698
3699
3700
3701
3702
3703 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
3704 lpfc_printf_vlog(vport, KERN_WARNING,
3705 LOG_FCP | LOG_FCP_ERROR,
3706 "9026 FCP Read Check Error "
3707 "and Underrun Data: x%x x%x x%x x%x\n",
3708 fcpDl,
3709 scsi_get_resid(cmnd), fcpi_parm,
3710 cmnd->cmnd[0]);
3711 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3712 host_status = DID_ERROR;
3713 }
3714
3715
3716
3717
3718
3719
3720 if (!(resp_info & SNS_LEN_VALID) &&
3721 (scsi_status == SAM_STAT_GOOD) &&
3722 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3723 < cmnd->underflow)) {
3724 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3725 "9027 FCP command x%x residual "
3726 "underrun converted to error "
3727 "Data: x%x x%x x%x\n",
3728 cmnd->cmnd[0], scsi_bufflen(cmnd),
3729 scsi_get_resid(cmnd), cmnd->underflow);
3730 host_status = DID_ERROR;
3731 }
3732 } else if (resp_info & RESID_OVER) {
3733 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3734 "9028 FCP command x%x residual overrun error. "
3735 "Data: x%x x%x\n", cmnd->cmnd[0],
3736 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
3737 host_status = DID_ERROR;
3738
3739
3740
3741
3742
3743 } else if (fcpi_parm) {
3744 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
3745 "9029 FCP %s Check Error xri x%x Data: "
3746 "x%x x%x x%x x%x x%x\n",
3747 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3748 "Read" : "Write"),
3749 ((phba->sli_rev == LPFC_SLI_REV4) ?
3750 lpfc_cmd->cur_iocbq.sli4_xritag :
3751 rsp_iocb->iocb.ulpContext),
3752 fcpDl, be32_to_cpu(fcprsp->rspResId),
3753 fcpi_parm, cmnd->cmnd[0], scsi_status);
3754
3755
3756
3757
3758
3759 if (fcpi_parm > fcpDl)
3760 goto out;
3761
3762 switch (scsi_status) {
3763 case SAM_STAT_GOOD:
3764 case SAM_STAT_CHECK_CONDITION:
3765
3766
3767
3768
3769
3770 host_status = DID_ERROR;
3771 break;
3772 }
3773 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3774 }
3775
3776 out:
3777 cmnd->result = host_status << 16 | scsi_status;
3778 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
3779}
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791static void
3792lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3793 struct lpfc_iocbq *pIocbOut)
3794{
3795 struct lpfc_io_buf *lpfc_cmd =
3796 (struct lpfc_io_buf *) pIocbIn->context1;
3797 struct lpfc_vport *vport = pIocbIn->vport;
3798 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3799 struct lpfc_nodelist *pnode = rdata->pnode;
3800 struct scsi_cmnd *cmd;
3801 unsigned long flags;
3802 struct lpfc_fast_path_event *fast_path_evt;
3803 struct Scsi_Host *shost;
3804 int idx;
3805 uint32_t logit = LOG_FCP;
3806#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3807 int cpu;
3808#endif
3809
3810
3811 spin_lock(&lpfc_cmd->buf_lock);
3812
3813
3814 cmd = lpfc_cmd->pCmd;
3815 if (!cmd) {
3816 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3817 "2621 IO completion: Not an active IO\n");
3818 spin_unlock(&lpfc_cmd->buf_lock);
3819 return;
3820 }
3821
3822 idx = lpfc_cmd->cur_iocbq.hba_wqidx;
3823 if (phba->sli4_hba.hdwq)
3824 phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
3825
3826#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3827 if (phba->cpucheck_on & LPFC_CHECK_SCSI_IO) {
3828 cpu = raw_smp_processor_id();
3829 if (cpu < LPFC_CHECK_CPU_CNT && phba->sli4_hba.hdwq)
3830 phba->sli4_hba.hdwq[idx].cpucheck_cmpl_io[cpu]++;
3831 }
3832#endif
3833 shost = cmd->device->host;
3834
3835 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
3836 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
3837
3838 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3839
3840#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3841 if (lpfc_cmd->prot_data_type) {
3842 struct scsi_dif_tuple *src = NULL;
3843
3844 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3845
3846
3847
3848
3849 switch (lpfc_cmd->prot_data_type) {
3850 case LPFC_INJERR_REFTAG:
3851 src->ref_tag =
3852 lpfc_cmd->prot_data;
3853 break;
3854 case LPFC_INJERR_APPTAG:
3855 src->app_tag =
3856 (uint16_t)lpfc_cmd->prot_data;
3857 break;
3858 case LPFC_INJERR_GUARD:
3859 src->guard_tag =
3860 (uint16_t)lpfc_cmd->prot_data;
3861 break;
3862 default:
3863 break;
3864 }
3865
3866 lpfc_cmd->prot_data = 0;
3867 lpfc_cmd->prot_data_type = 0;
3868 lpfc_cmd->prot_data_segment = NULL;
3869 }
3870#endif
3871
3872 if (lpfc_cmd->status) {
3873 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3874 (lpfc_cmd->result & IOERR_DRVR_MASK))
3875 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3876 else if (lpfc_cmd->status >= IOSTAT_CNT)
3877 lpfc_cmd->status = IOSTAT_DEFAULT;
3878 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3879 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3880 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3881 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
3882 logit = 0;
3883 else
3884 logit = LOG_FCP | LOG_FCP_UNDER;
3885 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3886 "9030 FCP cmd x%x failed <%d/%lld> "
3887 "status: x%x result: x%x "
3888 "sid: x%x did: x%x oxid: x%x "
3889 "Data: x%x x%x\n",
3890 cmd->cmnd[0],
3891 cmd->device ? cmd->device->id : 0xffff,
3892 cmd->device ? cmd->device->lun : 0xffff,
3893 lpfc_cmd->status, lpfc_cmd->result,
3894 vport->fc_myDID,
3895 (pnode) ? pnode->nlp_DID : 0,
3896 phba->sli_rev == LPFC_SLI_REV4 ?
3897 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
3898 pIocbOut->iocb.ulpContext,
3899 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
3900
3901 switch (lpfc_cmd->status) {
3902 case IOSTAT_FCP_RSP_ERROR:
3903
3904 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
3905 break;
3906 case IOSTAT_NPORT_BSY:
3907 case IOSTAT_FABRIC_BSY:
3908 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
3909 fast_path_evt = lpfc_alloc_fast_evt(phba);
3910 if (!fast_path_evt)
3911 break;
3912 fast_path_evt->un.fabric_evt.event_type =
3913 FC_REG_FABRIC_EVENT;
3914 fast_path_evt->un.fabric_evt.subcategory =
3915 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3916 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3917 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3918 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3919 &pnode->nlp_portname,
3920 sizeof(struct lpfc_name));
3921 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3922 &pnode->nlp_nodename,
3923 sizeof(struct lpfc_name));
3924 }
3925 fast_path_evt->vport = vport;
3926 fast_path_evt->work_evt.evt =
3927 LPFC_EVT_FASTPATH_MGMT_EVT;
3928 spin_lock_irqsave(&phba->hbalock, flags);
3929 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3930 &phba->work_list);
3931 spin_unlock_irqrestore(&phba->hbalock, flags);
3932 lpfc_worker_wake_up(phba);
3933 break;
3934 case IOSTAT_LOCAL_REJECT:
3935 case IOSTAT_REMOTE_STOP:
3936 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3937 lpfc_cmd->result ==
3938 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3939 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3940 lpfc_cmd->result ==
3941 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3942 cmd->result = DID_NO_CONNECT << 16;
3943 break;
3944 }
3945 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
3946 lpfc_cmd->result == IOERR_NO_RESOURCES ||
3947 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3948 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
3949 cmd->result = DID_REQUEUE << 16;
3950 break;
3951 }
3952 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3953 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3954 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3955 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3956
3957
3958
3959
3960 lpfc_parse_bg_err(phba, lpfc_cmd,
3961 pIocbOut);
3962 break;
3963 } else {
3964 lpfc_printf_vlog(vport, KERN_WARNING,
3965 LOG_BG,
3966 "9031 non-zero BGSTAT "
3967 "on unprotected cmd\n");
3968 }
3969 }
3970 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3971 && (phba->sli_rev == LPFC_SLI_REV4)
3972 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3973
3974
3975
3976
3977 lpfc_set_rrq_active(phba, pnode,
3978 lpfc_cmd->cur_iocbq.sli4_lxritag,
3979 0, 0);
3980 }
3981
3982 default:
3983 cmd->result = DID_ERROR << 16;
3984 break;
3985 }
3986
3987 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
3988 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3989 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
3990 SAM_STAT_BUSY;
3991 } else
3992 cmd->result = DID_OK << 16;
3993
3994 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3995 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3996
3997 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3998 "0710 Iodone <%d/%llu> cmd x%px, error "
3999 "x%x SNS x%x x%x Data: x%x x%x\n",
4000 cmd->device->id, cmd->device->lun, cmd,
4001 cmd->result, *lp, *(lp + 3), cmd->retries,
4002 scsi_get_resid(cmd));
4003 }
4004
4005 lpfc_update_stats(phba, lpfc_cmd);
4006 if (vport->cfg_max_scsicmpl_time &&
4007 time_after(jiffies, lpfc_cmd->start_time +
4008 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4009 spin_lock_irqsave(shost->host_lock, flags);
4010 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4011 if (pnode->cmd_qdepth >
4012 atomic_read(&pnode->cmd_pending) &&
4013 (atomic_read(&pnode->cmd_pending) >
4014 LPFC_MIN_TGT_QDEPTH) &&
4015 ((cmd->cmnd[0] == READ_10) ||
4016 (cmd->cmnd[0] == WRITE_10)))
4017 pnode->cmd_qdepth =
4018 atomic_read(&pnode->cmd_pending);
4019
4020 pnode->last_change_time = jiffies;
4021 }
4022 spin_unlock_irqrestore(shost->host_lock, flags);
4023 }
4024 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4025
4026 lpfc_cmd->pCmd = NULL;
4027 spin_unlock(&lpfc_cmd->buf_lock);
4028
4029
4030 cmd->scsi_done(cmd);
4031
4032
4033
4034
4035
4036 spin_lock(&lpfc_cmd->buf_lock);
4037 lpfc_cmd->cur_iocbq.iocb_flag &= ~LPFC_DRIVER_ABORTED;
4038 if (lpfc_cmd->waitq)
4039 wake_up(lpfc_cmd->waitq);
4040 spin_unlock(&lpfc_cmd->buf_lock);
4041
4042 lpfc_release_scsi_buf(phba, lpfc_cmd);
4043}
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053static void
4054lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4055{
4056 int i, j;
4057 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4058 i += sizeof(uint32_t), j++) {
4059 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4060 }
4061}
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072static void
4073lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
4074 struct lpfc_nodelist *pnode)
4075{
4076 struct lpfc_hba *phba = vport->phba;
4077 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4078 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4079 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4080 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4081 struct lpfc_sli4_hdw_queue *hdwq = NULL;
4082 int datadir = scsi_cmnd->sc_data_direction;
4083 int idx;
4084 uint8_t *ptr;
4085 bool sli4;
4086 uint32_t fcpdl;
4087
4088 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4089 return;
4090
4091 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
4092
4093 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
4094
4095 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4096 &lpfc_cmd->fcp_cmnd->fcp_lun);
4097
4098 ptr = &fcp_cmnd->fcpCdb[0];
4099 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4100 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4101 ptr += scsi_cmnd->cmd_len;
4102 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4103 }
4104
4105 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4106
4107 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4108 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
4109 idx = lpfc_cmd->hdwq_no;
4110 if (phba->sli4_hba.hdwq)
4111 hdwq = &phba->sli4_hba.hdwq[idx];
4112
4113
4114
4115
4116
4117
4118
4119 if (scsi_sg_count(scsi_cmnd)) {
4120 if (datadir == DMA_TO_DEVICE) {
4121 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
4122 iocb_cmd->ulpPU = PARM_READ_CHECK;
4123 if (vport->cfg_first_burst_size &&
4124 (pnode->nlp_flag & NLP_FIRSTBURST)) {
4125 fcpdl = scsi_bufflen(scsi_cmnd);
4126 if (fcpdl < vport->cfg_first_burst_size)
4127 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4128 else
4129 piocbq->iocb.un.fcpi.fcpi_XRdy =
4130 vport->cfg_first_burst_size;
4131 }
4132 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4133 if (hdwq)
4134 hdwq->scsi_cstat.output_requests++;
4135 } else {
4136 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4137 iocb_cmd->ulpPU = PARM_READ_CHECK;
4138 fcp_cmnd->fcpCntl3 = READ_DATA;
4139 if (hdwq)
4140 hdwq->scsi_cstat.input_requests++;
4141 }
4142 } else {
4143 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4144 iocb_cmd->un.fcpi.fcpi_parm = 0;
4145 iocb_cmd->ulpPU = 0;
4146 fcp_cmnd->fcpCntl3 = 0;
4147 if (hdwq)
4148 hdwq->scsi_cstat.control_requests++;
4149 }
4150 if (phba->sli_rev == 3 &&
4151 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4152 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
4153
4154
4155
4156
4157 piocbq->iocb.ulpContext = pnode->nlp_rpi;
4158 if (sli4)
4159 piocbq->iocb.ulpContext =
4160 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
4161 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4162 piocbq->iocb.ulpFCP2Rcvy = 1;
4163 else
4164 piocbq->iocb.ulpFCP2Rcvy = 0;
4165
4166 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4167 piocbq->context1 = lpfc_cmd;
4168 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4169 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
4170 piocbq->vport = vport;
4171}
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187static int
4188lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
4189 struct lpfc_io_buf *lpfc_cmd,
4190 uint64_t lun,
4191 uint8_t task_mgmt_cmd)
4192{
4193 struct lpfc_iocbq *piocbq;
4194 IOCB_t *piocb;
4195 struct fcp_cmnd *fcp_cmnd;
4196 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4197 struct lpfc_nodelist *ndlp = rdata->pnode;
4198
4199 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4200 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
4201 return 0;
4202
4203 piocbq = &(lpfc_cmd->cur_iocbq);
4204 piocbq->vport = vport;
4205
4206 piocb = &piocbq->iocb;
4207
4208 fcp_cmnd = lpfc_cmd->fcp_cmnd;
4209
4210 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4211 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
4212 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
4213 if (vport->phba->sli_rev == 3 &&
4214 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4215 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
4216 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
4217 piocb->ulpContext = ndlp->nlp_rpi;
4218 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4219 piocb->ulpContext =
4220 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4221 }
4222 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
4223 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4224 piocb->ulpPU = 0;
4225 piocb->un.fcpi.fcpi_parm = 0;
4226
4227
4228 if (lpfc_cmd->timeout > 0xff) {
4229
4230
4231
4232
4233 piocb->ulpTimeout = 0;
4234 } else
4235 piocb->ulpTimeout = lpfc_cmd->timeout;
4236
4237 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4238 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
4239
4240 return 1;
4241}
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252int
4253lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4254{
4255
4256 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4257 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
4258
4259 switch (dev_grp) {
4260 case LPFC_PCI_DEV_LP:
4261 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
4262 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
4263 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
4264 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
4265 break;
4266 case LPFC_PCI_DEV_OC:
4267 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
4268 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
4269 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
4270 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
4271 break;
4272 default:
4273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4274 "1418 Invalid HBA PCI-device group: 0x%x\n",
4275 dev_grp);
4276 return -ENODEV;
4277 break;
4278 }
4279 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
4280 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4281 return 0;
4282}
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293static void
4294lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4295 struct lpfc_iocbq *cmdiocbq,
4296 struct lpfc_iocbq *rspiocbq)
4297{
4298 struct lpfc_io_buf *lpfc_cmd =
4299 (struct lpfc_io_buf *) cmdiocbq->context1;
4300 if (lpfc_cmd)
4301 lpfc_release_scsi_buf(phba, lpfc_cmd);
4302 return;
4303}
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319int
4320lpfc_check_pci_resettable(const struct lpfc_hba *phba)
4321{
4322 const struct pci_dev *pdev = phba->pcidev;
4323 struct pci_dev *ptr = NULL;
4324 u8 counter = 0;
4325
4326
4327 list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
4328
4329 if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
4330 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4331 "8346 Non-Emulex vendor found: "
4332 "0x%04x\n", ptr->vendor);
4333 return -EBADSLT;
4334 }
4335
4336
4337 switch (ptr->device) {
4338 case PCI_DEVICE_ID_LANCER_FC:
4339 case PCI_DEVICE_ID_LANCER_G6_FC:
4340 case PCI_DEVICE_ID_LANCER_G7_FC:
4341 break;
4342 default:
4343 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4344 "8347 Invalid device found: "
4345 "0x%04x\n", ptr->device);
4346 return -EBADSLT;
4347 }
4348
4349
4350
4351
4352 if (ptr->devfn == 0) {
4353 if (++counter > 1) {
4354 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4355 "8348 More than one device on "
4356 "secondary bus found\n");
4357 return -EBADSLT;
4358 }
4359 }
4360 }
4361
4362 return 0;
4363}
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374const char *
4375lpfc_info(struct Scsi_Host *host)
4376{
4377 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4378 struct lpfc_hba *phba = vport->phba;
4379 int link_speed = 0;
4380 static char lpfcinfobuf[384];
4381 char tmp[384] = {0};
4382
4383 memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
4384 if (phba && phba->pcidev){
4385
4386 scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
4387 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4388 sizeof(lpfcinfobuf))
4389 goto buffer_done;
4390
4391
4392 scnprintf(tmp, sizeof(tmp),
4393 " on PCI bus %02x device %02x irq %d",
4394 phba->pcidev->bus->number, phba->pcidev->devfn,
4395 phba->pcidev->irq);
4396 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4397 sizeof(lpfcinfobuf))
4398 goto buffer_done;
4399
4400
4401 if (phba->Port[0]) {
4402 scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
4403 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4404 sizeof(lpfcinfobuf))
4405 goto buffer_done;
4406 }
4407
4408
4409 link_speed = lpfc_sli_port_speed_get(phba);
4410 if (link_speed != 0) {
4411 scnprintf(tmp, sizeof(tmp),
4412 " Logical Link Speed: %d Mbps", link_speed);
4413 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4414 sizeof(lpfcinfobuf))
4415 goto buffer_done;
4416 }
4417
4418
4419 if (!lpfc_check_pci_resettable(phba)) {
4420 scnprintf(tmp, sizeof(tmp), " PCI resettable");
4421 strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
4422 }
4423 }
4424
4425buffer_done:
4426 return lpfcinfobuf;
4427}
4428
4429
4430
4431
4432
4433
4434
4435
4436static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4437{
4438 unsigned long poll_tmo_expires =
4439 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4440
4441 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
4442 mod_timer(&phba->fcp_poll_timer,
4443 poll_tmo_expires);
4444}
4445
4446
4447
4448
4449
4450
4451
4452void lpfc_poll_start_timer(struct lpfc_hba * phba)
4453{
4454 lpfc_poll_rearm_timer(phba);
4455}
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465void lpfc_poll_timeout(struct timer_list *t)
4466{
4467 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
4468
4469 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4470 lpfc_sli_handle_fast_ring_event(phba,
4471 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4472
4473 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4474 lpfc_poll_rearm_timer(phba);
4475 }
4476}
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491static int
4492lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
4493{
4494 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4495 struct lpfc_hba *phba = vport->phba;
4496 struct lpfc_rport_data *rdata;
4497 struct lpfc_nodelist *ndlp;
4498 struct lpfc_io_buf *lpfc_cmd;
4499 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
4500 int err, idx;
4501#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4502 int cpu;
4503#endif
4504
4505 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
4506
4507
4508 if (unlikely(!rdata) || unlikely(!rport))
4509 goto out_fail_command;
4510
4511 err = fc_remote_port_chkready(rport);
4512 if (err) {
4513 cmnd->result = err;
4514 goto out_fail_command;
4515 }
4516 ndlp = rdata->pnode;
4517
4518 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
4519 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
4520
4521 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4522 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4523 " op:%02x str=%s without registering for"
4524 " BlockGuard - Rejecting command\n",
4525 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4526 dif_op_str[scsi_get_prot_op(cmnd)]);
4527 goto out_fail_command;
4528 }
4529
4530
4531
4532
4533
4534 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4535 goto out_tgt_busy;
4536 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
4537 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4538 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4539 "3377 Target Queue Full, scsi Id:%d "
4540 "Qdepth:%d Pending command:%d"
4541 " WWNN:%02x:%02x:%02x:%02x:"
4542 "%02x:%02x:%02x:%02x, "
4543 " WWPN:%02x:%02x:%02x:%02x:"
4544 "%02x:%02x:%02x:%02x",
4545 ndlp->nlp_sid, ndlp->cmd_qdepth,
4546 atomic_read(&ndlp->cmd_pending),
4547 ndlp->nlp_nodename.u.wwn[0],
4548 ndlp->nlp_nodename.u.wwn[1],
4549 ndlp->nlp_nodename.u.wwn[2],
4550 ndlp->nlp_nodename.u.wwn[3],
4551 ndlp->nlp_nodename.u.wwn[4],
4552 ndlp->nlp_nodename.u.wwn[5],
4553 ndlp->nlp_nodename.u.wwn[6],
4554 ndlp->nlp_nodename.u.wwn[7],
4555 ndlp->nlp_portname.u.wwn[0],
4556 ndlp->nlp_portname.u.wwn[1],
4557 ndlp->nlp_portname.u.wwn[2],
4558 ndlp->nlp_portname.u.wwn[3],
4559 ndlp->nlp_portname.u.wwn[4],
4560 ndlp->nlp_portname.u.wwn[5],
4561 ndlp->nlp_portname.u.wwn[6],
4562 ndlp->nlp_portname.u.wwn[7]);
4563 goto out_tgt_busy;
4564 }
4565 }
4566
4567 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp, cmnd);
4568 if (lpfc_cmd == NULL) {
4569 lpfc_rampdown_queue_depth(phba);
4570
4571 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4572 "0707 driver's buffer pool is empty, "
4573 "IO busied\n");
4574 goto out_host_busy;
4575 }
4576
4577
4578
4579
4580
4581 lpfc_cmd->pCmd = cmnd;
4582 lpfc_cmd->rdata = rdata;
4583 lpfc_cmd->ndlp = ndlp;
4584 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4585
4586 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
4587 if (vport->phba->cfg_enable_bg) {
4588 lpfc_printf_vlog(vport,
4589 KERN_INFO, LOG_SCSI_CMD,
4590 "9033 BLKGRD: rcvd %s cmd:x%x "
4591 "sector x%llx cnt %u pt %x\n",
4592 dif_op_str[scsi_get_prot_op(cmnd)],
4593 cmnd->cmnd[0],
4594 (unsigned long long)scsi_get_lba(cmnd),
4595 blk_rq_sectors(cmnd->request),
4596 (cmnd->cmnd[1]>>5));
4597 }
4598 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4599 } else {
4600 if (vport->phba->cfg_enable_bg) {
4601 lpfc_printf_vlog(vport,
4602 KERN_INFO, LOG_SCSI_CMD,
4603 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4604 "x%x sector x%llx cnt %u pt %x\n",
4605 cmnd->cmnd[0],
4606 (unsigned long long)scsi_get_lba(cmnd),
4607 blk_rq_sectors(cmnd->request),
4608 (cmnd->cmnd[1]>>5));
4609 }
4610 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4611 }
4612
4613 if (err == 2) {
4614 cmnd->result = DID_ERROR << 16;
4615 goto out_fail_command_release_buf;
4616 } else if (err) {
4617 goto out_host_busy_free_buf;
4618 }
4619
4620 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
4621
4622#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4623 if (phba->cpucheck_on & LPFC_CHECK_SCSI_IO) {
4624 cpu = raw_smp_processor_id();
4625 if (cpu < LPFC_CHECK_CPU_CNT) {
4626 struct lpfc_sli4_hdw_queue *hdwq =
4627 &phba->sli4_hba.hdwq[lpfc_cmd->hdwq_no];
4628 hdwq->cpucheck_xmt_io[cpu]++;
4629 }
4630 }
4631#endif
4632 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4633 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
4634 if (err) {
4635 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4636 "3376 FCP could not issue IOCB err %x"
4637 "FCP cmd x%x <%d/%llu> "
4638 "sid: x%x did: x%x oxid: x%x "
4639 "Data: x%x x%x x%x x%x\n",
4640 err, cmnd->cmnd[0],
4641 cmnd->device ? cmnd->device->id : 0xffff,
4642 cmnd->device ? cmnd->device->lun : (u64) -1,
4643 vport->fc_myDID, ndlp->nlp_DID,
4644 phba->sli_rev == LPFC_SLI_REV4 ?
4645 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4646 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4647 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4648 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4649 (uint32_t)
4650 (cmnd->request->timeout / 1000));
4651
4652 goto out_host_busy_free_buf;
4653 }
4654 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4655 lpfc_sli_handle_fast_ring_event(phba,
4656 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4657
4658 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4659 lpfc_poll_rearm_timer(phba);
4660 }
4661
4662 if (phba->cfg_xri_rebalancing)
4663 lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_cmd->hdwq_no);
4664
4665 return 0;
4666
4667 out_host_busy_free_buf:
4668 idx = lpfc_cmd->hdwq_no;
4669 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4670 if (phba->sli4_hba.hdwq) {
4671 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4672 case WRITE_DATA:
4673 phba->sli4_hba.hdwq[idx].scsi_cstat.output_requests--;
4674 break;
4675 case READ_DATA:
4676 phba->sli4_hba.hdwq[idx].scsi_cstat.input_requests--;
4677 break;
4678 default:
4679 phba->sli4_hba.hdwq[idx].scsi_cstat.control_requests--;
4680 }
4681 }
4682 lpfc_release_scsi_buf(phba, lpfc_cmd);
4683 out_host_busy:
4684 return SCSI_MLQUEUE_HOST_BUSY;
4685
4686 out_tgt_busy:
4687 return SCSI_MLQUEUE_TARGET_BUSY;
4688
4689 out_fail_command_release_buf:
4690 lpfc_release_scsi_buf(phba, lpfc_cmd);
4691
4692 out_fail_command:
4693 cmnd->scsi_done(cmnd);
4694 return 0;
4695}
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708static int
4709lpfc_abort_handler(struct scsi_cmnd *cmnd)
4710{
4711 struct Scsi_Host *shost = cmnd->device->host;
4712 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4713 struct lpfc_hba *phba = vport->phba;
4714 struct lpfc_iocbq *iocb;
4715 struct lpfc_iocbq *abtsiocb;
4716 struct lpfc_io_buf *lpfc_cmd;
4717 IOCB_t *cmd, *icmd;
4718 int ret = SUCCESS, status = 0;
4719 struct lpfc_sli_ring *pring_s4 = NULL;
4720 int ret_val;
4721 unsigned long flags;
4722 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
4723
4724 status = fc_block_scsi_eh(cmnd);
4725 if (status != 0 && status != SUCCESS)
4726 return status;
4727
4728 lpfc_cmd = (struct lpfc_io_buf *)cmnd->host_scribble;
4729 if (!lpfc_cmd)
4730 return ret;
4731
4732 spin_lock_irqsave(&phba->hbalock, flags);
4733
4734 if (phba->hba_flag & HBA_IOQ_FLUSH) {
4735 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4736 "3168 SCSI Layer abort requested I/O has been "
4737 "flushed by LLD.\n");
4738 ret = FAILED;
4739 goto out_unlock;
4740 }
4741
4742
4743 spin_lock(&lpfc_cmd->buf_lock);
4744
4745 if (!lpfc_cmd->pCmd) {
4746 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4747 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
4748 "x%x ID %d LUN %llu\n",
4749 SUCCESS, cmnd->device->id, cmnd->device->lun);
4750 goto out_unlock_buf;
4751 }
4752
4753 iocb = &lpfc_cmd->cur_iocbq;
4754 if (phba->sli_rev == LPFC_SLI_REV4) {
4755 pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].io_wq->pring;
4756 if (!pring_s4) {
4757 ret = FAILED;
4758 goto out_unlock_buf;
4759 }
4760 spin_lock(&pring_s4->ring_lock);
4761 }
4762
4763 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4764 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4765 "3169 SCSI Layer abort requested I/O has been "
4766 "cancelled by LLD.\n");
4767 ret = FAILED;
4768 goto out_unlock_ring;
4769 }
4770
4771
4772
4773
4774
4775
4776 if (lpfc_cmd->pCmd != cmnd) {
4777 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4778 "3170 SCSI Layer abort requested I/O has been "
4779 "completed by LLD.\n");
4780 goto out_unlock_ring;
4781 }
4782
4783 BUG_ON(iocb->context1 != lpfc_cmd);
4784
4785
4786 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4787 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4788 "3389 SCSI Layer I/O Abort Request is pending\n");
4789 if (phba->sli_rev == LPFC_SLI_REV4)
4790 spin_unlock(&pring_s4->ring_lock);
4791 spin_unlock(&lpfc_cmd->buf_lock);
4792 spin_unlock_irqrestore(&phba->hbalock, flags);
4793 goto wait_for_cmpl;
4794 }
4795
4796 abtsiocb = __lpfc_sli_get_iocbq(phba);
4797 if (abtsiocb == NULL) {
4798 ret = FAILED;
4799 goto out_unlock_ring;
4800 }
4801
4802
4803 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4804
4805
4806
4807
4808
4809
4810
4811 cmd = &iocb->iocb;
4812 icmd = &abtsiocb->iocb;
4813 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4814 icmd->un.acxri.abortContextTag = cmd->ulpContext;
4815 if (phba->sli_rev == LPFC_SLI_REV4)
4816 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4817 else
4818 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
4819
4820 icmd->ulpLe = 1;
4821 icmd->ulpClass = cmd->ulpClass;
4822
4823
4824 abtsiocb->hba_wqidx = iocb->hba_wqidx;
4825 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
4826 if (iocb->iocb_flag & LPFC_IO_FOF)
4827 abtsiocb->iocb_flag |= LPFC_IO_FOF;
4828
4829 if (lpfc_is_link_up(phba))
4830 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4831 else
4832 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
4833
4834 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
4835 abtsiocb->vport = vport;
4836 lpfc_cmd->waitq = &waitq;
4837 if (phba->sli_rev == LPFC_SLI_REV4) {
4838
4839 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4840 abtsiocb, 0);
4841 spin_unlock(&pring_s4->ring_lock);
4842 } else {
4843 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4844 abtsiocb, 0);
4845 }
4846
4847 spin_unlock_irqrestore(&phba->hbalock, flags);
4848
4849 if (ret_val == IOCB_ERROR) {
4850
4851 iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4852 lpfc_cmd->waitq = NULL;
4853 spin_unlock(&lpfc_cmd->buf_lock);
4854 lpfc_sli_release_iocbq(phba, abtsiocb);
4855 ret = FAILED;
4856 goto out;
4857 }
4858
4859 spin_unlock(&lpfc_cmd->buf_lock);
4860
4861 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4862 lpfc_sli_handle_fast_ring_event(phba,
4863 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4864
4865wait_for_cmpl:
4866
4867 wait_event_timeout(waitq,
4868 (lpfc_cmd->pCmd != cmnd),
4869 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
4870
4871 spin_lock(&lpfc_cmd->buf_lock);
4872
4873 if (lpfc_cmd->pCmd == cmnd) {
4874 ret = FAILED;
4875 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4876 "0748 abort handler timed out waiting "
4877 "for aborting I/O (xri:x%x) to complete: "
4878 "ret %#x, ID %d, LUN %llu\n",
4879 iocb->sli4_xritag, ret,
4880 cmnd->device->id, cmnd->device->lun);
4881 }
4882
4883 lpfc_cmd->waitq = NULL;
4884
4885 spin_unlock(&lpfc_cmd->buf_lock);
4886 goto out;
4887
4888out_unlock_ring:
4889 if (phba->sli_rev == LPFC_SLI_REV4)
4890 spin_unlock(&pring_s4->ring_lock);
4891out_unlock_buf:
4892 spin_unlock(&lpfc_cmd->buf_lock);
4893out_unlock:
4894 spin_unlock_irqrestore(&phba->hbalock, flags);
4895out:
4896 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4897 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
4898 "LUN %llu\n", ret, cmnd->device->id,
4899 cmnd->device->lun);
4900 return ret;
4901}
4902
4903static char *
4904lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4905{
4906 switch (task_mgmt_cmd) {
4907 case FCP_ABORT_TASK_SET:
4908 return "ABORT_TASK_SET";
4909 case FCP_CLEAR_TASK_SET:
4910 return "FCP_CLEAR_TASK_SET";
4911 case FCP_BUS_RESET:
4912 return "FCP_BUS_RESET";
4913 case FCP_LUN_RESET:
4914 return "FCP_LUN_RESET";
4915 case FCP_TARGET_RESET:
4916 return "FCP_TARGET_RESET";
4917 case FCP_CLEAR_ACA:
4918 return "FCP_CLEAR_ACA";
4919 case FCP_TERMINATE_TASK:
4920 return "FCP_TERMINATE_TASK";
4921 default:
4922 return "unknown";
4923 }
4924}
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938static int
4939lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
4940{
4941 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4942 uint32_t rsp_info;
4943 uint32_t rsp_len;
4944 uint8_t rsp_info_code;
4945 int ret = FAILED;
4946
4947
4948 if (fcprsp == NULL)
4949 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4950 "0703 fcp_rsp is missing\n");
4951 else {
4952 rsp_info = fcprsp->rspStatus2;
4953 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4954 rsp_info_code = fcprsp->rspInfo3;
4955
4956
4957 lpfc_printf_vlog(vport, KERN_INFO,
4958 LOG_FCP,
4959 "0706 fcp_rsp valid 0x%x,"
4960 " rsp len=%d code 0x%x\n",
4961 rsp_info,
4962 rsp_len, rsp_info_code);
4963
4964
4965
4966
4967
4968 if ((fcprsp->rspStatus2 & RSP_LEN_VALID) &&
4969 ((rsp_len == 8) || (rsp_len == 4))) {
4970 switch (rsp_info_code) {
4971 case RSP_NO_FAILURE:
4972 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4973 "0715 Task Mgmt No Failure\n");
4974 ret = SUCCESS;
4975 break;
4976 case RSP_TM_NOT_SUPPORTED:
4977 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4978 "0716 Task Mgmt Target "
4979 "reject\n");
4980 break;
4981 case RSP_TM_NOT_COMPLETED:
4982 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4983 "0717 Task Mgmt Target "
4984 "failed TM\n");
4985 break;
4986 case RSP_TM_INVALID_LU:
4987 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4988 "0718 Task Mgmt to invalid "
4989 "LUN\n");
4990 break;
4991 }
4992 }
4993 }
4994 return ret;
4995}
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013static int
5014lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
5015 unsigned int tgt_id, uint64_t lun_id,
5016 uint8_t task_mgmt_cmd)
5017{
5018 struct lpfc_hba *phba = vport->phba;
5019 struct lpfc_io_buf *lpfc_cmd;
5020 struct lpfc_iocbq *iocbq;
5021 struct lpfc_iocbq *iocbqrsp;
5022 struct lpfc_rport_data *rdata;
5023 struct lpfc_nodelist *pnode;
5024 int ret;
5025 int status;
5026
5027 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5028 if (!rdata || !rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
5029 return FAILED;
5030 pnode = rdata->pnode;
5031
5032 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode, NULL);
5033 if (lpfc_cmd == NULL)
5034 return FAILED;
5035 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
5036 lpfc_cmd->rdata = rdata;
5037 lpfc_cmd->pCmd = cmnd;
5038 lpfc_cmd->ndlp = pnode;
5039
5040 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5041 task_mgmt_cmd);
5042 if (!status) {
5043 lpfc_release_scsi_buf(phba, lpfc_cmd);
5044 return FAILED;
5045 }
5046
5047 iocbq = &lpfc_cmd->cur_iocbq;
5048 iocbqrsp = lpfc_sli_get_iocbq(phba);
5049 if (iocbqrsp == NULL) {
5050 lpfc_release_scsi_buf(phba, lpfc_cmd);
5051 return FAILED;
5052 }
5053 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
5054
5055 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5056 "0702 Issue %s to TGT %d LUN %llu "
5057 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
5058 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
5059 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5060 iocbq->iocb_flag);
5061
5062 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5063 iocbq, iocbqrsp, lpfc_cmd->timeout);
5064 if ((status != IOCB_SUCCESS) ||
5065 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
5066 if (status != IOCB_SUCCESS ||
5067 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
5068 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5069 "0727 TMF %s to TGT %d LUN %llu "
5070 "failed (%d, %d) iocb_flag x%x\n",
5071 lpfc_taskmgmt_name(task_mgmt_cmd),
5072 tgt_id, lun_id,
5073 iocbqrsp->iocb.ulpStatus,
5074 iocbqrsp->iocb.un.ulpWord[4],
5075 iocbq->iocb_flag);
5076
5077 if (status == IOCB_SUCCESS) {
5078 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5079
5080
5081 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5082 else
5083 ret = FAILED;
5084 } else if (status == IOCB_TIMEDOUT) {
5085 ret = TIMEOUT_ERROR;
5086 } else {
5087 ret = FAILED;
5088 }
5089 } else
5090 ret = SUCCESS;
5091
5092 lpfc_sli_release_iocbq(phba, iocbqrsp);
5093
5094 if (ret != TIMEOUT_ERROR)
5095 lpfc_release_scsi_buf(phba, lpfc_cmd);
5096
5097 return ret;
5098}
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112static int
5113lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5114{
5115 struct lpfc_rport_data *rdata;
5116 struct lpfc_nodelist *pnode;
5117 unsigned long later;
5118
5119 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5120 if (!rdata) {
5121 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5122 "0797 Tgt Map rport failure: rdata x%px\n", rdata);
5123 return FAILED;
5124 }
5125 pnode = rdata->pnode;
5126
5127
5128
5129
5130 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5131 while (time_after(later, jiffies)) {
5132 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5133 return FAILED;
5134 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5135 return SUCCESS;
5136 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5137 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5138 if (!rdata)
5139 return FAILED;
5140 pnode = rdata->pnode;
5141 }
5142 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5143 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5144 return FAILED;
5145 return SUCCESS;
5146}
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164static int
5165lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5166 uint64_t lun_id, lpfc_ctx_cmd context)
5167{
5168 struct lpfc_hba *phba = vport->phba;
5169 unsigned long later;
5170 int cnt;
5171
5172 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5173 if (cnt)
5174 lpfc_sli_abort_taskmgmt(vport,
5175 &phba->sli.sli3_ring[LPFC_FCP_RING],
5176 tgt_id, lun_id, context);
5177 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5178 while (time_after(later, jiffies) && cnt) {
5179 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5180 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5181 }
5182 if (cnt) {
5183 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5184 "0724 I/O flush failure for context %s : cnt x%x\n",
5185 ((context == LPFC_CTX_LUN) ? "LUN" :
5186 ((context == LPFC_CTX_TGT) ? "TGT" :
5187 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5188 cnt);
5189 return FAILED;
5190 }
5191 return SUCCESS;
5192}
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205static int
5206lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
5207{
5208 struct Scsi_Host *shost = cmnd->device->host;
5209 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5210 struct lpfc_rport_data *rdata;
5211 struct lpfc_nodelist *pnode;
5212 unsigned tgt_id = cmnd->device->id;
5213 uint64_t lun_id = cmnd->device->lun;
5214 struct lpfc_scsi_event_header scsi_event;
5215 int status;
5216
5217 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5218 if (!rdata || !rdata->pnode) {
5219 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5220 "0798 Device Reset rdata failure: rdata x%px\n",
5221 rdata);
5222 return FAILED;
5223 }
5224 pnode = rdata->pnode;
5225 status = fc_block_scsi_eh(cmnd);
5226 if (status != 0 && status != SUCCESS)
5227 return status;
5228
5229 status = lpfc_chk_tgt_mapped(vport, cmnd);
5230 if (status == FAILED) {
5231 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5232 "0721 Device Reset rport failure: rdata x%px\n", rdata);
5233 return FAILED;
5234 }
5235
5236 scsi_event.event_type = FC_REG_SCSI_EVENT;
5237 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5238 scsi_event.lun = lun_id;
5239 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5240 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5241
5242 fc_host_post_vendor_event(shost, fc_get_event_number(),
5243 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5244
5245 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
5246 FCP_LUN_RESET);
5247
5248 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5249 "0713 SCSI layer issued Device Reset (%d, %llu) "
5250 "return x%x\n", tgt_id, lun_id, status);
5251
5252
5253
5254
5255
5256
5257
5258 if (status == SUCCESS)
5259 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5260 LPFC_CTX_LUN);
5261
5262 return status;
5263}
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276static int
5277lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5278{
5279 struct Scsi_Host *shost = cmnd->device->host;
5280 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5281 struct lpfc_rport_data *rdata;
5282 struct lpfc_nodelist *pnode;
5283 unsigned tgt_id = cmnd->device->id;
5284 uint64_t lun_id = cmnd->device->lun;
5285 struct lpfc_scsi_event_header scsi_event;
5286 int status;
5287
5288 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5289 if (!rdata || !rdata->pnode) {
5290 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5291 "0799 Target Reset rdata failure: rdata x%px\n",
5292 rdata);
5293 return FAILED;
5294 }
5295 pnode = rdata->pnode;
5296 status = fc_block_scsi_eh(cmnd);
5297 if (status != 0 && status != SUCCESS)
5298 return status;
5299
5300 status = lpfc_chk_tgt_mapped(vport, cmnd);
5301 if (status == FAILED) {
5302 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5303 "0722 Target Reset rport failure: rdata x%px\n", rdata);
5304 if (pnode) {
5305 spin_lock_irq(shost->host_lock);
5306 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5307 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5308 spin_unlock_irq(shost->host_lock);
5309 }
5310 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5311 LPFC_CTX_TGT);
5312 return FAST_IO_FAIL;
5313 }
5314
5315 scsi_event.event_type = FC_REG_SCSI_EVENT;
5316 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5317 scsi_event.lun = 0;
5318 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5319 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5320
5321 fc_host_post_vendor_event(shost, fc_get_event_number(),
5322 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5323
5324 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
5325 FCP_TARGET_RESET);
5326
5327 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5328 "0723 SCSI layer issued Target Reset (%d, %llu) "
5329 "return x%x\n", tgt_id, lun_id, status);
5330
5331
5332
5333
5334
5335
5336
5337 if (status == SUCCESS)
5338 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5339 LPFC_CTX_TGT);
5340 return status;
5341}
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354static int
5355lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
5356{
5357 struct Scsi_Host *shost = cmnd->device->host;
5358 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5359 struct lpfc_nodelist *ndlp = NULL;
5360 struct lpfc_scsi_event_header scsi_event;
5361 int match;
5362 int ret = SUCCESS, status, i;
5363
5364 scsi_event.event_type = FC_REG_SCSI_EVENT;
5365 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5366 scsi_event.lun = 0;
5367 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5368 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5369
5370 fc_host_post_vendor_event(shost, fc_get_event_number(),
5371 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5372
5373 status = fc_block_scsi_eh(cmnd);
5374 if (status != 0 && status != SUCCESS)
5375 return status;
5376
5377
5378
5379
5380
5381
5382 for (i = 0; i < LPFC_MAX_TARGET; i++) {
5383
5384 match = 0;
5385 spin_lock_irq(shost->host_lock);
5386 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5387 if (!NLP_CHK_NODE_ACT(ndlp))
5388 continue;
5389 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5390 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5391 continue;
5392 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
5393 ndlp->nlp_sid == i &&
5394 ndlp->rport &&
5395 ndlp->nlp_type & NLP_FCP_TARGET) {
5396 match = 1;
5397 break;
5398 }
5399 }
5400 spin_unlock_irq(shost->host_lock);
5401 if (!match)
5402 continue;
5403
5404 status = lpfc_send_taskmgmt(vport, cmnd,
5405 i, 0, FCP_TARGET_RESET);
5406
5407 if (status != SUCCESS) {
5408 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5409 "0700 Bus Reset on target %d failed\n",
5410 i);
5411 ret = FAILED;
5412 }
5413 }
5414
5415
5416
5417
5418
5419
5420
5421 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5422 if (status != SUCCESS)
5423 ret = FAILED;
5424
5425 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5426 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
5427 return ret;
5428}
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446static int
5447lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5448{
5449 struct Scsi_Host *shost = cmnd->device->host;
5450 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5451 struct lpfc_hba *phba = vport->phba;
5452 int rc, ret = SUCCESS;
5453
5454 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5455 "3172 SCSI layer issued Host Reset Data:\n");
5456
5457 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5458 lpfc_offline(phba);
5459 rc = lpfc_sli_brdrestart(phba);
5460 if (rc)
5461 goto error;
5462
5463 rc = lpfc_online(phba);
5464 if (rc)
5465 goto error;
5466
5467 lpfc_unblock_mgmt_io(phba);
5468
5469 return ret;
5470error:
5471 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5472 "3323 Failed host reset\n");
5473 lpfc_unblock_mgmt_io(phba);
5474 return FAILED;
5475}
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490static int
5491lpfc_slave_alloc(struct scsi_device *sdev)
5492{
5493 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5494 struct lpfc_hba *phba = vport->phba;
5495 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
5496 uint32_t total = 0;
5497 uint32_t num_to_alloc = 0;
5498 int num_allocated = 0;
5499 uint32_t sdev_cnt;
5500 struct lpfc_device_data *device_data;
5501 unsigned long flags;
5502 struct lpfc_name target_wwpn;
5503
5504 if (!rport || fc_remote_port_chkready(rport))
5505 return -ENXIO;
5506
5507 if (phba->cfg_fof) {
5508
5509
5510
5511
5512
5513
5514 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5515 spin_lock_irqsave(&phba->devicelock, flags);
5516 device_data = __lpfc_get_device_data(phba,
5517 &phba->luns,
5518 &vport->fc_portname,
5519 &target_wwpn,
5520 sdev->lun);
5521 if (!device_data) {
5522 spin_unlock_irqrestore(&phba->devicelock, flags);
5523 device_data = lpfc_create_device_data(phba,
5524 &vport->fc_portname,
5525 &target_wwpn,
5526 sdev->lun,
5527 phba->cfg_XLanePriority,
5528 true);
5529 if (!device_data)
5530 return -ENOMEM;
5531 spin_lock_irqsave(&phba->devicelock, flags);
5532 list_add_tail(&device_data->listentry, &phba->luns);
5533 }
5534 device_data->rport_data = rport->dd_data;
5535 device_data->available = true;
5536 spin_unlock_irqrestore(&phba->devicelock, flags);
5537 sdev->hostdata = device_data;
5538 } else {
5539 sdev->hostdata = rport->dd_data;
5540 }
5541 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
5542
5543
5544 if (phba->sli_rev == LPFC_SLI_REV4)
5545 return 0;
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556 total = phba->total_scsi_bufs;
5557 num_to_alloc = vport->cfg_lun_queue_depth + 2;
5558
5559
5560 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5561 return 0;
5562
5563
5564 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5565 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5566 "0704 At limitation of %d preallocated "
5567 "command buffers\n", total);
5568 return 0;
5569
5570 } else if (total + num_to_alloc >
5571 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5572 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5573 "0705 Allocation request of %d "
5574 "command buffers will exceed max of %d. "
5575 "Reducing allocation request to %d.\n",
5576 num_to_alloc, phba->cfg_hba_queue_depth,
5577 (phba->cfg_hba_queue_depth - total));
5578 num_to_alloc = phba->cfg_hba_queue_depth - total;
5579 }
5580 num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
5581 if (num_to_alloc != num_allocated) {
5582 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5583 "0708 Allocation request of %d "
5584 "command buffers did not succeed. "
5585 "Allocated %d buffers.\n",
5586 num_to_alloc, num_allocated);
5587 }
5588 if (num_allocated > 0)
5589 phba->total_scsi_bufs += num_allocated;
5590 return 0;
5591}
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604static int
5605lpfc_slave_configure(struct scsi_device *sdev)
5606{
5607 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5608 struct lpfc_hba *phba = vport->phba;
5609
5610 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
5611
5612 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
5613 lpfc_sli_handle_fast_ring_event(phba,
5614 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
5615 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5616 lpfc_poll_rearm_timer(phba);
5617 }
5618
5619 return 0;
5620}
5621
5622
5623
5624
5625
5626
5627
5628static void
5629lpfc_slave_destroy(struct scsi_device *sdev)
5630{
5631 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5632 struct lpfc_hba *phba = vport->phba;
5633 unsigned long flags;
5634 struct lpfc_device_data *device_data = sdev->hostdata;
5635
5636 atomic_dec(&phba->sdev_cnt);
5637 if ((phba->cfg_fof) && (device_data)) {
5638 spin_lock_irqsave(&phba->devicelock, flags);
5639 device_data->available = false;
5640 if (!device_data->oas_enabled)
5641 lpfc_delete_device_data(phba, device_data);
5642 spin_unlock_irqrestore(&phba->devicelock, flags);
5643 }
5644 sdev->hostdata = NULL;
5645 return;
5646}
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666struct lpfc_device_data*
5667lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5668 struct lpfc_name *target_wwpn, uint64_t lun,
5669 uint32_t pri, bool atomic_create)
5670{
5671
5672 struct lpfc_device_data *lun_info;
5673 int memory_flags;
5674
5675 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5676 !(phba->cfg_fof))
5677 return NULL;
5678
5679
5680
5681 if (atomic_create)
5682 memory_flags = GFP_ATOMIC;
5683 else
5684 memory_flags = GFP_KERNEL;
5685 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5686 if (!lun_info)
5687 return NULL;
5688 INIT_LIST_HEAD(&lun_info->listentry);
5689 lun_info->rport_data = NULL;
5690 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5691 sizeof(struct lpfc_name));
5692 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5693 sizeof(struct lpfc_name));
5694 lun_info->device_id.lun = lun;
5695 lun_info->oas_enabled = false;
5696 lun_info->priority = pri;
5697 lun_info->available = false;
5698 return lun_info;
5699}
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709void
5710lpfc_delete_device_data(struct lpfc_hba *phba,
5711 struct lpfc_device_data *lun_info)
5712{
5713
5714 if (unlikely(!phba) || !lun_info ||
5715 !(phba->cfg_fof))
5716 return;
5717
5718 if (!list_empty(&lun_info->listentry))
5719 list_del(&lun_info->listentry);
5720 mempool_free(lun_info, phba->device_data_mem_pool);
5721 return;
5722}
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740struct lpfc_device_data*
5741__lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5742 struct lpfc_name *vport_wwpn,
5743 struct lpfc_name *target_wwpn, uint64_t lun)
5744{
5745
5746 struct lpfc_device_data *lun_info;
5747
5748 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
5749 !phba->cfg_fof)
5750 return NULL;
5751
5752
5753
5754 list_for_each_entry(lun_info, list, listentry) {
5755 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5756 sizeof(struct lpfc_name)) == 0) &&
5757 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5758 sizeof(struct lpfc_name)) == 0) &&
5759 (lun_info->device_id.lun == lun))
5760 return lun_info;
5761 }
5762
5763 return NULL;
5764}
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792bool
5793lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5794 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5795 struct lpfc_name *found_vport_wwpn,
5796 struct lpfc_name *found_target_wwpn,
5797 uint64_t *found_lun,
5798 uint32_t *found_lun_status,
5799 uint32_t *found_lun_pri)
5800{
5801
5802 unsigned long flags;
5803 struct lpfc_device_data *lun_info;
5804 struct lpfc_device_id *device_id;
5805 uint64_t lun;
5806 bool found = false;
5807
5808 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5809 !starting_lun || !found_vport_wwpn ||
5810 !found_target_wwpn || !found_lun || !found_lun_status ||
5811 (*starting_lun == NO_MORE_OAS_LUN) ||
5812 !phba->cfg_fof)
5813 return false;
5814
5815 lun = *starting_lun;
5816 *found_lun = NO_MORE_OAS_LUN;
5817 *starting_lun = NO_MORE_OAS_LUN;
5818
5819
5820
5821 spin_lock_irqsave(&phba->devicelock, flags);
5822 list_for_each_entry(lun_info, &phba->luns, listentry) {
5823 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5824 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5825 sizeof(struct lpfc_name)) == 0)) &&
5826 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5827 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5828 sizeof(struct lpfc_name)) == 0)) &&
5829 (lun_info->oas_enabled)) {
5830 device_id = &lun_info->device_id;
5831 if ((!found) &&
5832 ((lun == FIND_FIRST_OAS_LUN) ||
5833 (device_id->lun == lun))) {
5834 *found_lun = device_id->lun;
5835 memcpy(found_vport_wwpn,
5836 &device_id->vport_wwpn,
5837 sizeof(struct lpfc_name));
5838 memcpy(found_target_wwpn,
5839 &device_id->target_wwpn,
5840 sizeof(struct lpfc_name));
5841 if (lun_info->available)
5842 *found_lun_status =
5843 OAS_LUN_STATUS_EXISTS;
5844 else
5845 *found_lun_status = 0;
5846 *found_lun_pri = lun_info->priority;
5847 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5848 memset(vport_wwpn, 0x0,
5849 sizeof(struct lpfc_name));
5850 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5851 memset(target_wwpn, 0x0,
5852 sizeof(struct lpfc_name));
5853 found = true;
5854 } else if (found) {
5855 *starting_lun = device_id->lun;
5856 memcpy(vport_wwpn, &device_id->vport_wwpn,
5857 sizeof(struct lpfc_name));
5858 memcpy(target_wwpn, &device_id->target_wwpn,
5859 sizeof(struct lpfc_name));
5860 break;
5861 }
5862 }
5863 }
5864 spin_unlock_irqrestore(&phba->devicelock, flags);
5865 return found;
5866}
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888bool
5889lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5890 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
5891{
5892
5893 struct lpfc_device_data *lun_info;
5894 unsigned long flags;
5895
5896 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5897 !phba->cfg_fof)
5898 return false;
5899
5900 spin_lock_irqsave(&phba->devicelock, flags);
5901
5902
5903 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5904 target_wwpn, lun);
5905 if (lun_info) {
5906 if (!lun_info->oas_enabled)
5907 lun_info->oas_enabled = true;
5908 lun_info->priority = pri;
5909 spin_unlock_irqrestore(&phba->devicelock, flags);
5910 return true;
5911 }
5912
5913
5914 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
5915 pri, true);
5916 if (lun_info) {
5917 lun_info->oas_enabled = true;
5918 lun_info->priority = pri;
5919 lun_info->available = false;
5920 list_add_tail(&lun_info->listentry, &phba->luns);
5921 spin_unlock_irqrestore(&phba->devicelock, flags);
5922 return true;
5923 }
5924 spin_unlock_irqrestore(&phba->devicelock, flags);
5925 return false;
5926}
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947bool
5948lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5949 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
5950{
5951
5952 struct lpfc_device_data *lun_info;
5953 unsigned long flags;
5954
5955 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5956 !phba->cfg_fof)
5957 return false;
5958
5959 spin_lock_irqsave(&phba->devicelock, flags);
5960
5961
5962 lun_info = __lpfc_get_device_data(phba,
5963 &phba->luns, vport_wwpn,
5964 target_wwpn, lun);
5965 if (lun_info) {
5966 lun_info->oas_enabled = false;
5967 lun_info->priority = pri;
5968 if (!lun_info->available)
5969 lpfc_delete_device_data(phba, lun_info);
5970 spin_unlock_irqrestore(&phba->devicelock, flags);
5971 return true;
5972 }
5973
5974 spin_unlock_irqrestore(&phba->devicelock, flags);
5975 return false;
5976}
5977
5978static int
5979lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
5980{
5981 return SCSI_MLQUEUE_HOST_BUSY;
5982}
5983
5984static int
5985lpfc_no_handler(struct scsi_cmnd *cmnd)
5986{
5987 return FAILED;
5988}
5989
5990static int
5991lpfc_no_slave(struct scsi_device *sdev)
5992{
5993 return -ENODEV;
5994}
5995
5996struct scsi_host_template lpfc_template_nvme = {
5997 .module = THIS_MODULE,
5998 .name = LPFC_DRIVER_NAME,
5999 .proc_name = LPFC_DRIVER_NAME,
6000 .info = lpfc_info,
6001 .queuecommand = lpfc_no_command,
6002 .eh_abort_handler = lpfc_no_handler,
6003 .eh_device_reset_handler = lpfc_no_handler,
6004 .eh_target_reset_handler = lpfc_no_handler,
6005 .eh_bus_reset_handler = lpfc_no_handler,
6006 .eh_host_reset_handler = lpfc_no_handler,
6007 .slave_alloc = lpfc_no_slave,
6008 .slave_configure = lpfc_no_slave,
6009 .scan_finished = lpfc_scan_finished,
6010 .this_id = -1,
6011 .sg_tablesize = 1,
6012 .cmd_per_lun = 1,
6013 .shost_attrs = lpfc_hba_attrs,
6014 .max_sectors = 0xFFFF,
6015 .vendor_id = LPFC_NL_VENDOR_ID,
6016 .track_queue_depth = 0,
6017};
6018
6019struct scsi_host_template lpfc_template_no_hr = {
6020 .module = THIS_MODULE,
6021 .name = LPFC_DRIVER_NAME,
6022 .proc_name = LPFC_DRIVER_NAME,
6023 .info = lpfc_info,
6024 .queuecommand = lpfc_queuecommand,
6025 .eh_timed_out = fc_eh_timed_out,
6026 .eh_abort_handler = lpfc_abort_handler,
6027 .eh_device_reset_handler = lpfc_device_reset_handler,
6028 .eh_target_reset_handler = lpfc_target_reset_handler,
6029 .eh_bus_reset_handler = lpfc_bus_reset_handler,
6030 .slave_alloc = lpfc_slave_alloc,
6031 .slave_configure = lpfc_slave_configure,
6032 .slave_destroy = lpfc_slave_destroy,
6033 .scan_finished = lpfc_scan_finished,
6034 .this_id = -1,
6035 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6036 .cmd_per_lun = LPFC_CMD_PER_LUN,
6037 .shost_attrs = lpfc_hba_attrs,
6038 .max_sectors = 0xFFFFFFFF,
6039 .vendor_id = LPFC_NL_VENDOR_ID,
6040 .change_queue_depth = scsi_change_queue_depth,
6041 .track_queue_depth = 1,
6042};
6043
6044struct scsi_host_template lpfc_template = {
6045 .module = THIS_MODULE,
6046 .name = LPFC_DRIVER_NAME,
6047 .proc_name = LPFC_DRIVER_NAME,
6048 .info = lpfc_info,
6049 .queuecommand = lpfc_queuecommand,
6050 .eh_timed_out = fc_eh_timed_out,
6051 .eh_abort_handler = lpfc_abort_handler,
6052 .eh_device_reset_handler = lpfc_device_reset_handler,
6053 .eh_target_reset_handler = lpfc_target_reset_handler,
6054 .eh_bus_reset_handler = lpfc_bus_reset_handler,
6055 .eh_host_reset_handler = lpfc_host_reset_handler,
6056 .slave_alloc = lpfc_slave_alloc,
6057 .slave_configure = lpfc_slave_configure,
6058 .slave_destroy = lpfc_slave_destroy,
6059 .scan_finished = lpfc_scan_finished,
6060 .this_id = -1,
6061 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6062 .cmd_per_lun = LPFC_CMD_PER_LUN,
6063 .shost_attrs = lpfc_hba_attrs,
6064 .max_sectors = 0xFFFF,
6065 .vendor_id = LPFC_NL_VENDOR_ID,
6066 .change_queue_depth = scsi_change_queue_depth,
6067 .track_queue_depth = 1,
6068};
6069
6070struct scsi_host_template lpfc_vport_template = {
6071 .module = THIS_MODULE,
6072 .name = LPFC_DRIVER_NAME,
6073 .proc_name = LPFC_DRIVER_NAME,
6074 .info = lpfc_info,
6075 .queuecommand = lpfc_queuecommand,
6076 .eh_timed_out = fc_eh_timed_out,
6077 .eh_abort_handler = lpfc_abort_handler,
6078 .eh_device_reset_handler = lpfc_device_reset_handler,
6079 .eh_target_reset_handler = lpfc_target_reset_handler,
6080 .slave_alloc = lpfc_slave_alloc,
6081 .slave_configure = lpfc_slave_configure,
6082 .slave_destroy = lpfc_slave_destroy,
6083 .scan_finished = lpfc_scan_finished,
6084 .this_id = -1,
6085 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6086 .cmd_per_lun = LPFC_CMD_PER_LUN,
6087 .shost_attrs = lpfc_vport_attrs,
6088 .max_sectors = 0xFFFF,
6089 .change_queue_depth = scsi_change_queue_depth,
6090 .track_queue_depth = 1,
6091};
6092