Merge tag 'Wimplicit-fallthrough-5.2-rc1' of git://git.kernel.org/pub/scm/linux/kerne...
[sfrench/cifs-2.6.git] / drivers / scsi / lpfc / lpfc_scsi.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
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
56 int _dump_buf_done = 1;
57
58 static char *dif_op_str[] = {
59         "PROT_NORMAL",
60         "PROT_READ_INSERT",
61         "PROT_WRITE_STRIP",
62         "PROT_READ_STRIP",
63         "PROT_WRITE_INSERT",
64         "PROT_READ_PASS",
65         "PROT_WRITE_PASS",
66 };
67
68 struct scsi_dif_tuple {
69         __be16 guard_tag;       /* Checksum */
70         __be16 app_tag;         /* Opaque storage */
71         __be32 ref_tag;         /* Target LBA or indirect LBA */
72 };
73
74 static struct lpfc_rport_data *
75 lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
76 {
77         struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
78
79         if (vport->phba->cfg_fof)
80                 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
81         else
82                 return (struct lpfc_rport_data *)sdev->hostdata;
83 }
84
85 static void
86 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
87 static void
88 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
89 static int
90 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
91
92 static void
93 lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
94 {
95         void *src, *dst;
96         struct scatterlist *sgde = scsi_sglist(cmnd);
97
98         if (!_dump_buf_data) {
99                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
100                         "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
101                                 __func__);
102                 return;
103         }
104
105
106         if (!sgde) {
107                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
108                         "9051 BLKGRD: ERROR: data scatterlist is null\n");
109                 return;
110         }
111
112         dst = (void *) _dump_buf_data;
113         while (sgde) {
114                 src = sg_virt(sgde);
115                 memcpy(dst, src, sgde->length);
116                 dst += sgde->length;
117                 sgde = sg_next(sgde);
118         }
119 }
120
121 static void
122 lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
123 {
124         void *src, *dst;
125         struct scatterlist *sgde = scsi_prot_sglist(cmnd);
126
127         if (!_dump_buf_dif) {
128                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
129                         "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
130                                 __func__);
131                 return;
132         }
133
134         if (!sgde) {
135                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
136                         "9053 BLKGRD: ERROR: prot scatterlist is null\n");
137                 return;
138         }
139
140         dst = _dump_buf_dif;
141         while (sgde) {
142                 src = sg_virt(sgde);
143                 memcpy(dst, src, sgde->length);
144                 dst += sgde->length;
145                 sgde = sg_next(sgde);
146         }
147 }
148
149 static inline unsigned
150 lpfc_cmd_blksize(struct scsi_cmnd *sc)
151 {
152         return sc->device->sector_size;
153 }
154
155 #define LPFC_CHECK_PROTECT_GUARD        1
156 #define LPFC_CHECK_PROTECT_REF          2
157 static inline unsigned
158 lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
159 {
160         return 1;
161 }
162
163 static inline unsigned
164 lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
165 {
166         if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
167                 return 0;
168         if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
169                 return 1;
170         return 0;
171 }
172
173 /**
174  * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
175  * @phba: Pointer to HBA object.
176  * @lpfc_cmd: lpfc scsi command object pointer.
177  *
178  * This function is called from the lpfc_prep_task_mgmt_cmd function to
179  * set the last bit in the response sge entry.
180  **/
181 static void
182 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
183                                 struct lpfc_io_buf *lpfc_cmd)
184 {
185         struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
186         if (sgl) {
187                 sgl += 1;
188                 sgl->word2 = le32_to_cpu(sgl->word2);
189                 bf_set(lpfc_sli4_sge_last, sgl, 1);
190                 sgl->word2 = cpu_to_le32(sgl->word2);
191         }
192 }
193
194 /**
195  * lpfc_update_stats - Update statistical data for the command completion
196  * @phba: Pointer to HBA object.
197  * @lpfc_cmd: lpfc scsi command object pointer.
198  *
199  * This function is called when there is a command completion and this
200  * function updates the statistical data for the command completion.
201  **/
202 static void
203 lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
204 {
205         struct lpfc_rport_data *rdata;
206         struct lpfc_nodelist *pnode;
207         struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
208         unsigned long flags;
209         struct Scsi_Host  *shost = cmd->device->host;
210         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
211         unsigned long latency;
212         int i;
213
214         if (!vport->stat_data_enabled ||
215             vport->stat_data_blocked ||
216             (cmd->result))
217                 return;
218
219         latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
220         rdata = lpfc_cmd->rdata;
221         pnode = rdata->pnode;
222
223         spin_lock_irqsave(shost->host_lock, flags);
224         if (!pnode ||
225             !pnode->lat_data ||
226             (phba->bucket_type == LPFC_NO_BUCKET)) {
227                 spin_unlock_irqrestore(shost->host_lock, flags);
228                 return;
229         }
230
231         if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
232                 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
233                         phba->bucket_step;
234                 /* check array subscript bounds */
235                 if (i < 0)
236                         i = 0;
237                 else if (i >= LPFC_MAX_BUCKET_COUNT)
238                         i = LPFC_MAX_BUCKET_COUNT - 1;
239         } else {
240                 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
241                         if (latency <= (phba->bucket_base +
242                                 ((1<<i)*phba->bucket_step)))
243                                 break;
244         }
245
246         pnode->lat_data[i].cmd_count++;
247         spin_unlock_irqrestore(shost->host_lock, flags);
248 }
249
250 /**
251  * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
252  * @phba: The Hba for which this call is being executed.
253  *
254  * This routine is called when there is resource error in driver or firmware.
255  * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
256  * posts at most 1 event each second. This routine wakes up worker thread of
257  * @phba to process WORKER_RAM_DOWN_EVENT event.
258  *
259  * This routine should be called with no lock held.
260  **/
261 void
262 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
263 {
264         unsigned long flags;
265         uint32_t evt_posted;
266         unsigned long expires;
267
268         spin_lock_irqsave(&phba->hbalock, flags);
269         atomic_inc(&phba->num_rsrc_err);
270         phba->last_rsrc_error_time = jiffies;
271
272         expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
273         if (time_after(expires, jiffies)) {
274                 spin_unlock_irqrestore(&phba->hbalock, flags);
275                 return;
276         }
277
278         phba->last_ramp_down_time = jiffies;
279
280         spin_unlock_irqrestore(&phba->hbalock, flags);
281
282         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
283         evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
284         if (!evt_posted)
285                 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
286         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
287
288         if (!evt_posted)
289                 lpfc_worker_wake_up(phba);
290         return;
291 }
292
293 /**
294  * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
295  * @phba: The Hba for which this call is being executed.
296  *
297  * This routine is called to  process WORKER_RAMP_DOWN_QUEUE event for worker
298  * thread.This routine reduces queue depth for all scsi device on each vport
299  * associated with @phba.
300  **/
301 void
302 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
303 {
304         struct lpfc_vport **vports;
305         struct Scsi_Host  *shost;
306         struct scsi_device *sdev;
307         unsigned long new_queue_depth;
308         unsigned long num_rsrc_err, num_cmd_success;
309         int i;
310
311         num_rsrc_err = atomic_read(&phba->num_rsrc_err);
312         num_cmd_success = atomic_read(&phba->num_cmd_success);
313
314         /*
315          * The error and success command counters are global per
316          * driver instance.  If another handler has already
317          * operated on this error event, just exit.
318          */
319         if (num_rsrc_err == 0)
320                 return;
321
322         vports = lpfc_create_vport_work_array(phba);
323         if (vports != NULL)
324                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
325                         shost = lpfc_shost_from_vport(vports[i]);
326                         shost_for_each_device(sdev, shost) {
327                                 new_queue_depth =
328                                         sdev->queue_depth * num_rsrc_err /
329                                         (num_rsrc_err + num_cmd_success);
330                                 if (!new_queue_depth)
331                                         new_queue_depth = sdev->queue_depth - 1;
332                                 else
333                                         new_queue_depth = sdev->queue_depth -
334                                                                 new_queue_depth;
335                                 scsi_change_queue_depth(sdev, new_queue_depth);
336                         }
337                 }
338         lpfc_destroy_vport_work_array(phba, vports);
339         atomic_set(&phba->num_rsrc_err, 0);
340         atomic_set(&phba->num_cmd_success, 0);
341 }
342
343 /**
344  * lpfc_scsi_dev_block - set all scsi hosts to block state
345  * @phba: Pointer to HBA context object.
346  *
347  * This function walks vport list and set each SCSI host to block state
348  * by invoking fc_remote_port_delete() routine. This function is invoked
349  * with EEH when device's PCI slot has been permanently disabled.
350  **/
351 void
352 lpfc_scsi_dev_block(struct lpfc_hba *phba)
353 {
354         struct lpfc_vport **vports;
355         struct Scsi_Host  *shost;
356         struct scsi_device *sdev;
357         struct fc_rport *rport;
358         int i;
359
360         vports = lpfc_create_vport_work_array(phba);
361         if (vports != NULL)
362                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
363                         shost = lpfc_shost_from_vport(vports[i]);
364                         shost_for_each_device(sdev, shost) {
365                                 rport = starget_to_rport(scsi_target(sdev));
366                                 fc_remote_port_delete(rport);
367                         }
368                 }
369         lpfc_destroy_vport_work_array(phba, vports);
370 }
371
372 /**
373  * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
374  * @vport: The virtual port for which this call being executed.
375  * @num_to_allocate: The requested number of buffers to allocate.
376  *
377  * This routine allocates a scsi buffer for device with SLI-3 interface spec,
378  * the scsi buffer contains all the necessary information needed to initiate
379  * a SCSI I/O. The non-DMAable buffer region contains information to build
380  * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
381  * and the initial BPL. In addition to allocating memory, the FCP CMND and
382  * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
383  *
384  * Return codes:
385  *   int - number of scsi buffers that were allocated.
386  *   0 = failure, less than num_to_alloc is a partial failure.
387  **/
388 static int
389 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
390 {
391         struct lpfc_hba *phba = vport->phba;
392         struct lpfc_io_buf *psb;
393         struct ulp_bde64 *bpl;
394         IOCB_t *iocb;
395         dma_addr_t pdma_phys_fcp_cmd;
396         dma_addr_t pdma_phys_fcp_rsp;
397         dma_addr_t pdma_phys_sgl;
398         uint16_t iotag;
399         int bcnt, bpl_size;
400
401         bpl_size = phba->cfg_sg_dma_buf_size -
402                 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
403
404         lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
405                          "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
406                          num_to_alloc, phba->cfg_sg_dma_buf_size,
407                          (int)sizeof(struct fcp_cmnd),
408                          (int)sizeof(struct fcp_rsp), bpl_size);
409
410         for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
411                 psb = kzalloc(sizeof(struct lpfc_io_buf), GFP_KERNEL);
412                 if (!psb)
413                         break;
414
415                 /*
416                  * Get memory from the pci pool to map the virt space to pci
417                  * bus space for an I/O.  The DMA buffer includes space for the
418                  * struct fcp_cmnd, struct fcp_rsp and the number of bde's
419                  * necessary to support the sg_tablesize.
420                  */
421                 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
422                                         GFP_KERNEL, &psb->dma_handle);
423                 if (!psb->data) {
424                         kfree(psb);
425                         break;
426                 }
427
428
429                 /* Allocate iotag for psb->cur_iocbq. */
430                 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
431                 if (iotag == 0) {
432                         dma_pool_free(phba->lpfc_sg_dma_buf_pool,
433                                       psb->data, psb->dma_handle);
434                         kfree(psb);
435                         break;
436                 }
437                 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
438
439                 psb->fcp_cmnd = psb->data;
440                 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
441                 psb->dma_sgl = psb->data + sizeof(struct fcp_cmnd) +
442                         sizeof(struct fcp_rsp);
443
444                 /* Initialize local short-hand pointers. */
445                 bpl = (struct ulp_bde64 *)psb->dma_sgl;
446                 pdma_phys_fcp_cmd = psb->dma_handle;
447                 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
448                 pdma_phys_sgl = psb->dma_handle + sizeof(struct fcp_cmnd) +
449                         sizeof(struct fcp_rsp);
450
451                 /*
452                  * The first two bdes are the FCP_CMD and FCP_RSP. The balance
453                  * are sg list bdes.  Initialize the first two and leave the
454                  * rest for queuecommand.
455                  */
456                 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
457                 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
458                 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
459                 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
460                 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
461
462                 /* Setup the physical region for the FCP RSP */
463                 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
464                 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
465                 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
466                 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
467                 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
468
469                 /*
470                  * Since the IOCB for the FCP I/O is built into this
471                  * lpfc_scsi_buf, initialize it with all known data now.
472                  */
473                 iocb = &psb->cur_iocbq.iocb;
474                 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
475                 if ((phba->sli_rev == 3) &&
476                                 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
477                         /* fill in immediate fcp command BDE */
478                         iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
479                         iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
480                         iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
481                                         unsli3.fcp_ext.icd);
482                         iocb->un.fcpi64.bdl.addrHigh = 0;
483                         iocb->ulpBdeCount = 0;
484                         iocb->ulpLe = 0;
485                         /* fill in response BDE */
486                         iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
487                                                         BUFF_TYPE_BDE_64;
488                         iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
489                                 sizeof(struct fcp_rsp);
490                         iocb->unsli3.fcp_ext.rbde.addrLow =
491                                 putPaddrLow(pdma_phys_fcp_rsp);
492                         iocb->unsli3.fcp_ext.rbde.addrHigh =
493                                 putPaddrHigh(pdma_phys_fcp_rsp);
494                 } else {
495                         iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
496                         iocb->un.fcpi64.bdl.bdeSize =
497                                         (2 * sizeof(struct ulp_bde64));
498                         iocb->un.fcpi64.bdl.addrLow =
499                                         putPaddrLow(pdma_phys_sgl);
500                         iocb->un.fcpi64.bdl.addrHigh =
501                                         putPaddrHigh(pdma_phys_sgl);
502                         iocb->ulpBdeCount = 1;
503                         iocb->ulpLe = 1;
504                 }
505                 iocb->ulpClass = CLASS3;
506                 psb->status = IOSTAT_SUCCESS;
507                 /* Put it back into the SCSI buffer list */
508                 psb->cur_iocbq.context1  = psb;
509                 spin_lock_init(&psb->buf_lock);
510                 lpfc_release_scsi_buf_s3(phba, psb);
511
512         }
513
514         return bcnt;
515 }
516
517 /**
518  * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
519  * @vport: pointer to lpfc vport data structure.
520  *
521  * This routine is invoked by the vport cleanup for deletions and the cleanup
522  * for an ndlp on removal.
523  **/
524 void
525 lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
526 {
527         struct lpfc_hba *phba = vport->phba;
528         struct lpfc_io_buf *psb, *next_psb;
529         struct lpfc_sli4_hdw_queue *qp;
530         unsigned long iflag = 0;
531         int idx;
532
533         if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
534                 return;
535
536         spin_lock_irqsave(&phba->hbalock, iflag);
537         for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
538                 qp = &phba->sli4_hba.hdwq[idx];
539
540                 spin_lock(&qp->abts_scsi_buf_list_lock);
541                 list_for_each_entry_safe(psb, next_psb,
542                                          &qp->lpfc_abts_scsi_buf_list, list) {
543                         if (psb->rdata && psb->rdata->pnode &&
544                             psb->rdata->pnode->vport == vport)
545                                 psb->rdata = NULL;
546                 }
547                 spin_unlock(&qp->abts_scsi_buf_list_lock);
548         }
549         spin_unlock_irqrestore(&phba->hbalock, iflag);
550 }
551
552 /**
553  * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
554  * @phba: pointer to lpfc hba data structure.
555  * @axri: pointer to the fcp xri abort wcqe structure.
556  *
557  * This routine is invoked by the worker thread to process a SLI4 fast-path
558  * FCP aborted xri.
559  **/
560 void
561 lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
562                           struct sli4_wcqe_xri_aborted *axri, int idx)
563 {
564         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
565         uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
566         struct lpfc_io_buf *psb, *next_psb;
567         struct lpfc_sli4_hdw_queue *qp;
568         unsigned long iflag = 0;
569         struct lpfc_iocbq *iocbq;
570         int i;
571         struct lpfc_nodelist *ndlp;
572         int rrq_empty = 0;
573         struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
574
575         if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
576                 return;
577
578         qp = &phba->sli4_hba.hdwq[idx];
579         spin_lock_irqsave(&phba->hbalock, iflag);
580         spin_lock(&qp->abts_scsi_buf_list_lock);
581         list_for_each_entry_safe(psb, next_psb,
582                 &qp->lpfc_abts_scsi_buf_list, list) {
583                 if (psb->cur_iocbq.sli4_xritag == xri) {
584                         list_del(&psb->list);
585                         qp->abts_scsi_io_bufs--;
586                         psb->exch_busy = 0;
587                         psb->status = IOSTAT_SUCCESS;
588                         spin_unlock(
589                                 &qp->abts_scsi_buf_list_lock);
590                         if (psb->rdata && psb->rdata->pnode)
591                                 ndlp = psb->rdata->pnode;
592                         else
593                                 ndlp = NULL;
594
595                         rrq_empty = list_empty(&phba->active_rrq_list);
596                         spin_unlock_irqrestore(&phba->hbalock, iflag);
597                         if (ndlp) {
598                                 lpfc_set_rrq_active(phba, ndlp,
599                                         psb->cur_iocbq.sli4_lxritag, rxid, 1);
600                                 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
601                         }
602                         lpfc_release_scsi_buf_s4(phba, psb);
603                         if (rrq_empty)
604                                 lpfc_worker_wake_up(phba);
605                         return;
606                 }
607         }
608         spin_unlock(&qp->abts_scsi_buf_list_lock);
609         for (i = 1; i <= phba->sli.last_iotag; i++) {
610                 iocbq = phba->sli.iocbq_lookup[i];
611
612                 if (!(iocbq->iocb_flag &  LPFC_IO_FCP) ||
613                         (iocbq->iocb_flag & LPFC_IO_LIBDFC))
614                         continue;
615                 if (iocbq->sli4_xritag != xri)
616                         continue;
617                 psb = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
618                 psb->exch_busy = 0;
619                 spin_unlock_irqrestore(&phba->hbalock, iflag);
620                 if (!list_empty(&pring->txq))
621                         lpfc_worker_wake_up(phba);
622                 return;
623
624         }
625         spin_unlock_irqrestore(&phba->hbalock, iflag);
626 }
627
628 /**
629  * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
630  * @phba: The HBA for which this call is being executed.
631  *
632  * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
633  * and returns to caller.
634  *
635  * Return codes:
636  *   NULL - Error
637  *   Pointer to lpfc_scsi_buf - Success
638  **/
639 static struct lpfc_io_buf *
640 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
641                      struct scsi_cmnd *cmnd)
642 {
643         struct lpfc_io_buf *lpfc_cmd = NULL;
644         struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
645         unsigned long iflag = 0;
646
647         spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
648         list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_io_buf,
649                          list);
650         if (!lpfc_cmd) {
651                 spin_lock(&phba->scsi_buf_list_put_lock);
652                 list_splice(&phba->lpfc_scsi_buf_list_put,
653                             &phba->lpfc_scsi_buf_list_get);
654                 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
655                 list_remove_head(scsi_buf_list_get, lpfc_cmd,
656                                  struct lpfc_io_buf, list);
657                 spin_unlock(&phba->scsi_buf_list_put_lock);
658         }
659         spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
660
661         if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
662                 atomic_inc(&ndlp->cmd_pending);
663                 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
664         }
665         return  lpfc_cmd;
666 }
667 /**
668  * lpfc_get_scsi_buf_s4 - Get a scsi buffer from io_buf_list of the HBA
669  * @phba: The HBA for which this call is being executed.
670  *
671  * This routine removes a scsi buffer from head of @hdwq io_buf_list
672  * and returns to caller.
673  *
674  * Return codes:
675  *   NULL - Error
676  *   Pointer to lpfc_scsi_buf - Success
677  **/
678 static struct lpfc_io_buf *
679 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
680                      struct scsi_cmnd *cmnd)
681 {
682         struct lpfc_io_buf *lpfc_cmd;
683         struct lpfc_sli4_hdw_queue *qp;
684         struct sli4_sge *sgl;
685         IOCB_t *iocb;
686         dma_addr_t pdma_phys_fcp_rsp;
687         dma_addr_t pdma_phys_fcp_cmd;
688         uint32_t sgl_size, cpu, idx;
689         int tag;
690
691         cpu = smp_processor_id();
692         if (cmnd && phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
693                 tag = blk_mq_unique_tag(cmnd->request);
694                 idx = blk_mq_unique_tag_to_hwq(tag);
695         } else {
696                 idx = phba->sli4_hba.cpu_map[cpu].hdwq;
697         }
698
699         lpfc_cmd = lpfc_get_io_buf(phba, ndlp, idx,
700                                    !phba->cfg_xri_rebalancing);
701         if (!lpfc_cmd) {
702                 qp = &phba->sli4_hba.hdwq[idx];
703                 qp->empty_io_bufs++;
704                 return NULL;
705         }
706
707         sgl_size = phba->cfg_sg_dma_buf_size -
708                 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
709
710         /* Setup key fields in buffer that may have been changed
711          * if other protocols used this buffer.
712          */
713         lpfc_cmd->cur_iocbq.iocb_flag = LPFC_IO_FCP;
714         lpfc_cmd->prot_seg_cnt = 0;
715         lpfc_cmd->seg_cnt = 0;
716         lpfc_cmd->timeout = 0;
717         lpfc_cmd->flags = 0;
718         lpfc_cmd->start_time = jiffies;
719         lpfc_cmd->waitq = NULL;
720         lpfc_cmd->cpu = cpu;
721 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
722         lpfc_cmd->prot_data_type = 0;
723 #endif
724         lpfc_cmd->fcp_cmnd = (lpfc_cmd->data + sgl_size);
725         lpfc_cmd->fcp_rsp = (struct fcp_rsp *)((uint8_t *)lpfc_cmd->fcp_cmnd +
726                                 sizeof(struct fcp_cmnd));
727
728         /*
729          * The first two SGEs are the FCP_CMD and FCP_RSP.
730          * The balance are sg list bdes. Initialize the
731          * first two and leave the rest for queuecommand.
732          */
733         sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
734         pdma_phys_fcp_cmd = (lpfc_cmd->dma_handle + sgl_size);
735         sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
736         sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
737         sgl->word2 = le32_to_cpu(sgl->word2);
738         bf_set(lpfc_sli4_sge_last, sgl, 0);
739         sgl->word2 = cpu_to_le32(sgl->word2);
740         sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
741         sgl++;
742
743         /* Setup the physical region for the FCP RSP */
744         pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
745         sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
746         sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
747         sgl->word2 = le32_to_cpu(sgl->word2);
748         bf_set(lpfc_sli4_sge_last, sgl, 1);
749         sgl->word2 = cpu_to_le32(sgl->word2);
750         sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
751
752         /*
753          * Since the IOCB for the FCP I/O is built into this
754          * lpfc_io_buf, initialize it with all known data now.
755          */
756         iocb = &lpfc_cmd->cur_iocbq.iocb;
757         iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
758         iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
759         /* setting the BLP size to 2 * sizeof BDE may not be correct.
760          * We are setting the bpl to point to out sgl. An sgl's
761          * entries are 16 bytes, a bpl entries are 12 bytes.
762          */
763         iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
764         iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
765         iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
766         iocb->ulpBdeCount = 1;
767         iocb->ulpLe = 1;
768         iocb->ulpClass = CLASS3;
769
770         if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
771                 atomic_inc(&ndlp->cmd_pending);
772                 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
773         }
774         return  lpfc_cmd;
775 }
776 /**
777  * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
778  * @phba: The HBA for which this call is being executed.
779  *
780  * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
781  * and returns to caller.
782  *
783  * Return codes:
784  *   NULL - Error
785  *   Pointer to lpfc_scsi_buf - Success
786  **/
787 static struct lpfc_io_buf*
788 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
789                   struct scsi_cmnd *cmnd)
790 {
791         return  phba->lpfc_get_scsi_buf(phba, ndlp, cmnd);
792 }
793
794 /**
795  * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
796  * @phba: The Hba for which this call is being executed.
797  * @psb: The scsi buffer which is being released.
798  *
799  * This routine releases @psb scsi buffer by adding it to tail of @phba
800  * lpfc_scsi_buf_list list.
801  **/
802 static void
803 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
804 {
805         unsigned long iflag = 0;
806
807         psb->seg_cnt = 0;
808         psb->prot_seg_cnt = 0;
809
810         spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
811         psb->pCmd = NULL;
812         psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
813         list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
814         spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
815 }
816
817 /**
818  * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
819  * @phba: The Hba for which this call is being executed.
820  * @psb: The scsi buffer which is being released.
821  *
822  * This routine releases @psb scsi buffer by adding it to tail of @hdwq
823  * io_buf_list list. For SLI4 XRI's are tied to the scsi buffer
824  * and cannot be reused for at least RA_TOV amount of time if it was
825  * aborted.
826  **/
827 static void
828 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
829 {
830         struct lpfc_sli4_hdw_queue *qp;
831         unsigned long iflag = 0;
832
833         psb->seg_cnt = 0;
834         psb->prot_seg_cnt = 0;
835
836         qp = psb->hdwq;
837         if (psb->exch_busy) {
838                 spin_lock_irqsave(&qp->abts_scsi_buf_list_lock, iflag);
839                 psb->pCmd = NULL;
840                 list_add_tail(&psb->list, &qp->lpfc_abts_scsi_buf_list);
841                 qp->abts_scsi_io_bufs++;
842                 spin_unlock_irqrestore(&qp->abts_scsi_buf_list_lock, iflag);
843         } else {
844                 lpfc_release_io_buf(phba, (struct lpfc_io_buf *)psb, qp);
845         }
846 }
847
848 /**
849  * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
850  * @phba: The Hba for which this call is being executed.
851  * @psb: The scsi buffer which is being released.
852  *
853  * This routine releases @psb scsi buffer by adding it to tail of @phba
854  * lpfc_scsi_buf_list list.
855  **/
856 static void
857 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
858 {
859         if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
860                 atomic_dec(&psb->ndlp->cmd_pending);
861
862         psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
863         phba->lpfc_release_scsi_buf(phba, psb);
864 }
865
866 /**
867  * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
868  * @phba: The Hba for which this call is being executed.
869  * @lpfc_cmd: The scsi buffer which is going to be mapped.
870  *
871  * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
872  * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
873  * through sg elements and format the bde. This routine also initializes all
874  * IOCB fields which are dependent on scsi command request buffer.
875  *
876  * Return codes:
877  *   1 - Error
878  *   0 - Success
879  **/
880 static int
881 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
882 {
883         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
884         struct scatterlist *sgel = NULL;
885         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
886         struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
887         struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
888         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
889         struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
890         dma_addr_t physaddr;
891         uint32_t num_bde = 0;
892         int nseg, datadir = scsi_cmnd->sc_data_direction;
893
894         /*
895          * There are three possibilities here - use scatter-gather segment, use
896          * the single mapping, or neither.  Start the lpfc command prep by
897          * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
898          * data bde entry.
899          */
900         bpl += 2;
901         if (scsi_sg_count(scsi_cmnd)) {
902                 /*
903                  * The driver stores the segment count returned from pci_map_sg
904                  * because this a count of dma-mappings used to map the use_sg
905                  * pages.  They are not guaranteed to be the same for those
906                  * architectures that implement an IOMMU.
907                  */
908
909                 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
910                                   scsi_sg_count(scsi_cmnd), datadir);
911                 if (unlikely(!nseg))
912                         return 1;
913
914                 lpfc_cmd->seg_cnt = nseg;
915                 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
916                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
917                                 "9064 BLKGRD: %s: Too many sg segments from "
918                                "dma_map_sg.  Config %d, seg_cnt %d\n",
919                                __func__, phba->cfg_sg_seg_cnt,
920                                lpfc_cmd->seg_cnt);
921                         lpfc_cmd->seg_cnt = 0;
922                         scsi_dma_unmap(scsi_cmnd);
923                         return 1;
924                 }
925
926                 /*
927                  * The driver established a maximum scatter-gather segment count
928                  * during probe that limits the number of sg elements in any
929                  * single scsi command.  Just run through the seg_cnt and format
930                  * the bde's.
931                  * When using SLI-3 the driver will try to fit all the BDEs into
932                  * the IOCB. If it can't then the BDEs get added to a BPL as it
933                  * does for SLI-2 mode.
934                  */
935                 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
936                         physaddr = sg_dma_address(sgel);
937                         if (phba->sli_rev == 3 &&
938                             !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
939                             !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
940                             nseg <= LPFC_EXT_DATA_BDE_COUNT) {
941                                 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
942                                 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
943                                 data_bde->addrLow = putPaddrLow(physaddr);
944                                 data_bde->addrHigh = putPaddrHigh(physaddr);
945                                 data_bde++;
946                         } else {
947                                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
948                                 bpl->tus.f.bdeSize = sg_dma_len(sgel);
949                                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
950                                 bpl->addrLow =
951                                         le32_to_cpu(putPaddrLow(physaddr));
952                                 bpl->addrHigh =
953                                         le32_to_cpu(putPaddrHigh(physaddr));
954                                 bpl++;
955                         }
956                 }
957         }
958
959         /*
960          * Finish initializing those IOCB fields that are dependent on the
961          * scsi_cmnd request_buffer.  Note that for SLI-2 the bdeSize is
962          * explicitly reinitialized and for SLI-3 the extended bde count is
963          * explicitly reinitialized since all iocb memory resources are reused.
964          */
965         if (phba->sli_rev == 3 &&
966             !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
967             !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
968                 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
969                         /*
970                          * The extended IOCB format can only fit 3 BDE or a BPL.
971                          * This I/O has more than 3 BDE so the 1st data bde will
972                          * be a BPL that is filled in here.
973                          */
974                         physaddr = lpfc_cmd->dma_handle;
975                         data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
976                         data_bde->tus.f.bdeSize = (num_bde *
977                                                    sizeof(struct ulp_bde64));
978                         physaddr += (sizeof(struct fcp_cmnd) +
979                                      sizeof(struct fcp_rsp) +
980                                      (2 * sizeof(struct ulp_bde64)));
981                         data_bde->addrHigh = putPaddrHigh(physaddr);
982                         data_bde->addrLow = putPaddrLow(physaddr);
983                         /* ebde count includes the response bde and data bpl */
984                         iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
985                 } else {
986                         /* ebde count includes the response bde and data bdes */
987                         iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
988                 }
989         } else {
990                 iocb_cmd->un.fcpi64.bdl.bdeSize =
991                         ((num_bde + 2) * sizeof(struct ulp_bde64));
992                 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
993         }
994         fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
995
996         /*
997          * Due to difference in data length between DIF/non-DIF paths,
998          * we need to set word 4 of IOCB here
999          */
1000         iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1001         return 0;
1002 }
1003
1004 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1005
1006 /* Return BG_ERR_INIT if error injection is detected by Initiator */
1007 #define BG_ERR_INIT     0x1
1008 /* Return BG_ERR_TGT if error injection is detected by Target */
1009 #define BG_ERR_TGT      0x2
1010 /* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
1011 #define BG_ERR_SWAP     0x10
1012 /**
1013  * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
1014  * error injection
1015  **/
1016 #define BG_ERR_CHECK    0x20
1017
1018 /**
1019  * lpfc_bg_err_inject - Determine if we should inject an error
1020  * @phba: The Hba for which this call is being executed.
1021  * @sc: The SCSI command to examine
1022  * @reftag: (out) BlockGuard reference tag for transmitted data
1023  * @apptag: (out) BlockGuard application tag for transmitted data
1024  * @new_guard (in) Value to replace CRC with if needed
1025  *
1026  * Returns BG_ERR_* bit mask or 0 if request ignored
1027  **/
1028 static int
1029 lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1030                 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1031 {
1032         struct scatterlist *sgpe; /* s/g prot entry */
1033         struct lpfc_io_buf *lpfc_cmd = NULL;
1034         struct scsi_dif_tuple *src = NULL;
1035         struct lpfc_nodelist *ndlp;
1036         struct lpfc_rport_data *rdata;
1037         uint32_t op = scsi_get_prot_op(sc);
1038         uint32_t blksize;
1039         uint32_t numblks;
1040         sector_t lba;
1041         int rc = 0;
1042         int blockoff = 0;
1043
1044         if (op == SCSI_PROT_NORMAL)
1045                 return 0;
1046
1047         sgpe = scsi_prot_sglist(sc);
1048         lba = scsi_get_lba(sc);
1049
1050         /* First check if we need to match the LBA */
1051         if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1052                 blksize = lpfc_cmd_blksize(sc);
1053                 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1054
1055                 /* Make sure we have the right LBA if one is specified */
1056                 if ((phba->lpfc_injerr_lba < lba) ||
1057                         (phba->lpfc_injerr_lba >= (lba + numblks)))
1058                         return 0;
1059                 if (sgpe) {
1060                         blockoff = phba->lpfc_injerr_lba - lba;
1061                         numblks = sg_dma_len(sgpe) /
1062                                 sizeof(struct scsi_dif_tuple);
1063                         if (numblks < blockoff)
1064                                 blockoff = numblks;
1065                 }
1066         }
1067
1068         /* Next check if we need to match the remote NPortID or WWPN */
1069         rdata = lpfc_rport_data_from_scsi_device(sc->device);
1070         if (rdata && rdata->pnode) {
1071                 ndlp = rdata->pnode;
1072
1073                 /* Make sure we have the right NPortID if one is specified */
1074                 if (phba->lpfc_injerr_nportid  &&
1075                         (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1076                         return 0;
1077
1078                 /*
1079                  * Make sure we have the right WWPN if one is specified.
1080                  * wwn[0] should be a non-zero NAA in a good WWPN.
1081                  */
1082                 if (phba->lpfc_injerr_wwpn.u.wwn[0]  &&
1083                         (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1084                                 sizeof(struct lpfc_name)) != 0))
1085                         return 0;
1086         }
1087
1088         /* Setup a ptr to the protection data if the SCSI host provides it */
1089         if (sgpe) {
1090                 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1091                 src += blockoff;
1092                 lpfc_cmd = (struct lpfc_io_buf *)sc->host_scribble;
1093         }
1094
1095         /* Should we change the Reference Tag */
1096         if (reftag) {
1097                 if (phba->lpfc_injerr_wref_cnt) {
1098                         switch (op) {
1099                         case SCSI_PROT_WRITE_PASS:
1100                                 if (src) {
1101                                         /*
1102                                          * For WRITE_PASS, force the error
1103                                          * to be sent on the wire. It should
1104                                          * be detected by the Target.
1105                                          * If blockoff != 0 error will be
1106                                          * inserted in middle of the IO.
1107                                          */
1108
1109                                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1110                                         "9076 BLKGRD: Injecting reftag error: "
1111                                         "write lba x%lx + x%x oldrefTag x%x\n",
1112                                         (unsigned long)lba, blockoff,
1113                                         be32_to_cpu(src->ref_tag));
1114
1115                                         /*
1116                                          * Save the old ref_tag so we can
1117                                          * restore it on completion.
1118                                          */
1119                                         if (lpfc_cmd) {
1120                                                 lpfc_cmd->prot_data_type =
1121                                                         LPFC_INJERR_REFTAG;
1122                                                 lpfc_cmd->prot_data_segment =
1123                                                         src;
1124                                                 lpfc_cmd->prot_data =
1125                                                         src->ref_tag;
1126                                         }
1127                                         src->ref_tag = cpu_to_be32(0xDEADBEEF);
1128                                         phba->lpfc_injerr_wref_cnt--;
1129                                         if (phba->lpfc_injerr_wref_cnt == 0) {
1130                                                 phba->lpfc_injerr_nportid = 0;
1131                                                 phba->lpfc_injerr_lba =
1132                                                         LPFC_INJERR_LBA_OFF;
1133                                                 memset(&phba->lpfc_injerr_wwpn,
1134                                                   0, sizeof(struct lpfc_name));
1135                                         }
1136                                         rc = BG_ERR_TGT | BG_ERR_CHECK;
1137
1138                                         break;
1139                                 }
1140                                 /* fall through */
1141                         case SCSI_PROT_WRITE_INSERT:
1142                                 /*
1143                                  * For WRITE_INSERT, force the error
1144                                  * to be sent on the wire. It should be
1145                                  * detected by the Target.
1146                                  */
1147                                 /* DEADBEEF will be the reftag on the wire */
1148                                 *reftag = 0xDEADBEEF;
1149                                 phba->lpfc_injerr_wref_cnt--;
1150                                 if (phba->lpfc_injerr_wref_cnt == 0) {
1151                                         phba->lpfc_injerr_nportid = 0;
1152                                         phba->lpfc_injerr_lba =
1153                                         LPFC_INJERR_LBA_OFF;
1154                                         memset(&phba->lpfc_injerr_wwpn,
1155                                                 0, sizeof(struct lpfc_name));
1156                                 }
1157                                 rc = BG_ERR_TGT | BG_ERR_CHECK;
1158
1159                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1160                                         "9078 BLKGRD: Injecting reftag error: "
1161                                         "write lba x%lx\n", (unsigned long)lba);
1162                                 break;
1163                         case SCSI_PROT_WRITE_STRIP:
1164                                 /*
1165                                  * For WRITE_STRIP and WRITE_PASS,
1166                                  * force the error on data
1167                                  * being copied from SLI-Host to SLI-Port.
1168                                  */
1169                                 *reftag = 0xDEADBEEF;
1170                                 phba->lpfc_injerr_wref_cnt--;
1171                                 if (phba->lpfc_injerr_wref_cnt == 0) {
1172                                         phba->lpfc_injerr_nportid = 0;
1173                                         phba->lpfc_injerr_lba =
1174                                                 LPFC_INJERR_LBA_OFF;
1175                                         memset(&phba->lpfc_injerr_wwpn,
1176                                                 0, sizeof(struct lpfc_name));
1177                                 }
1178                                 rc = BG_ERR_INIT;
1179
1180                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1181                                         "9077 BLKGRD: Injecting reftag error: "
1182                                         "write lba x%lx\n", (unsigned long)lba);
1183                                 break;
1184                         }
1185                 }
1186                 if (phba->lpfc_injerr_rref_cnt) {
1187                         switch (op) {
1188                         case SCSI_PROT_READ_INSERT:
1189                         case SCSI_PROT_READ_STRIP:
1190                         case SCSI_PROT_READ_PASS:
1191                                 /*
1192                                  * For READ_STRIP and READ_PASS, force the
1193                                  * error on data being read off the wire. It
1194                                  * should force an IO error to the driver.
1195                                  */
1196                                 *reftag = 0xDEADBEEF;
1197                                 phba->lpfc_injerr_rref_cnt--;
1198                                 if (phba->lpfc_injerr_rref_cnt == 0) {
1199                                         phba->lpfc_injerr_nportid = 0;
1200                                         phba->lpfc_injerr_lba =
1201                                                 LPFC_INJERR_LBA_OFF;
1202                                         memset(&phba->lpfc_injerr_wwpn,
1203                                                 0, sizeof(struct lpfc_name));
1204                                 }
1205                                 rc = BG_ERR_INIT;
1206
1207                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1208                                         "9079 BLKGRD: Injecting reftag error: "
1209                                         "read lba x%lx\n", (unsigned long)lba);
1210                                 break;
1211                         }
1212                 }
1213         }
1214
1215         /* Should we change the Application Tag */
1216         if (apptag) {
1217                 if (phba->lpfc_injerr_wapp_cnt) {
1218                         switch (op) {
1219                         case SCSI_PROT_WRITE_PASS:
1220                                 if (src) {
1221                                         /*
1222                                          * For WRITE_PASS, force the error
1223                                          * to be sent on the wire. It should
1224                                          * be detected by the Target.
1225                                          * If blockoff != 0 error will be
1226                                          * inserted in middle of the IO.
1227                                          */
1228
1229                                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1230                                         "9080 BLKGRD: Injecting apptag error: "
1231                                         "write lba x%lx + x%x oldappTag x%x\n",
1232                                         (unsigned long)lba, blockoff,
1233                                         be16_to_cpu(src->app_tag));
1234
1235                                         /*
1236                                          * Save the old app_tag so we can
1237                                          * restore it on completion.
1238                                          */
1239                                         if (lpfc_cmd) {
1240                                                 lpfc_cmd->prot_data_type =
1241                                                         LPFC_INJERR_APPTAG;
1242                                                 lpfc_cmd->prot_data_segment =
1243                                                         src;
1244                                                 lpfc_cmd->prot_data =
1245                                                         src->app_tag;
1246                                         }
1247                                         src->app_tag = cpu_to_be16(0xDEAD);
1248                                         phba->lpfc_injerr_wapp_cnt--;
1249                                         if (phba->lpfc_injerr_wapp_cnt == 0) {
1250                                                 phba->lpfc_injerr_nportid = 0;
1251                                                 phba->lpfc_injerr_lba =
1252                                                         LPFC_INJERR_LBA_OFF;
1253                                                 memset(&phba->lpfc_injerr_wwpn,
1254                                                   0, sizeof(struct lpfc_name));
1255                                         }
1256                                         rc = BG_ERR_TGT | BG_ERR_CHECK;
1257                                         break;
1258                                 }
1259                                 /* fall through */
1260                         case SCSI_PROT_WRITE_INSERT:
1261                                 /*
1262                                  * For WRITE_INSERT, force the
1263                                  * error to be sent on the wire. It should be
1264                                  * detected by the Target.
1265                                  */
1266                                 /* DEAD will be the apptag on the wire */
1267                                 *apptag = 0xDEAD;
1268                                 phba->lpfc_injerr_wapp_cnt--;
1269                                 if (phba->lpfc_injerr_wapp_cnt == 0) {
1270                                         phba->lpfc_injerr_nportid = 0;
1271                                         phba->lpfc_injerr_lba =
1272                                                 LPFC_INJERR_LBA_OFF;
1273                                         memset(&phba->lpfc_injerr_wwpn,
1274                                                 0, sizeof(struct lpfc_name));
1275                                 }
1276                                 rc = BG_ERR_TGT | BG_ERR_CHECK;
1277
1278                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1279                                         "0813 BLKGRD: Injecting apptag error: "
1280                                         "write lba x%lx\n", (unsigned long)lba);
1281                                 break;
1282                         case SCSI_PROT_WRITE_STRIP:
1283                                 /*
1284                                  * For WRITE_STRIP and WRITE_PASS,
1285                                  * force the error on data
1286                                  * being copied from SLI-Host to SLI-Port.
1287                                  */
1288                                 *apptag = 0xDEAD;
1289                                 phba->lpfc_injerr_wapp_cnt--;
1290                                 if (phba->lpfc_injerr_wapp_cnt == 0) {
1291                                         phba->lpfc_injerr_nportid = 0;
1292                                         phba->lpfc_injerr_lba =
1293                                                 LPFC_INJERR_LBA_OFF;
1294                                         memset(&phba->lpfc_injerr_wwpn,
1295                                                 0, sizeof(struct lpfc_name));
1296                                 }
1297                                 rc = BG_ERR_INIT;
1298
1299                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1300                                         "0812 BLKGRD: Injecting apptag error: "
1301                                         "write lba x%lx\n", (unsigned long)lba);
1302                                 break;
1303                         }
1304                 }
1305                 if (phba->lpfc_injerr_rapp_cnt) {
1306                         switch (op) {
1307                         case SCSI_PROT_READ_INSERT:
1308                         case SCSI_PROT_READ_STRIP:
1309                         case SCSI_PROT_READ_PASS:
1310                                 /*
1311                                  * For READ_STRIP and READ_PASS, force the
1312                                  * error on data being read off the wire. It
1313                                  * should force an IO error to the driver.
1314                                  */
1315                                 *apptag = 0xDEAD;
1316                                 phba->lpfc_injerr_rapp_cnt--;
1317                                 if (phba->lpfc_injerr_rapp_cnt == 0) {
1318                                         phba->lpfc_injerr_nportid = 0;
1319                                         phba->lpfc_injerr_lba =
1320                                                 LPFC_INJERR_LBA_OFF;
1321                                         memset(&phba->lpfc_injerr_wwpn,
1322                                                 0, sizeof(struct lpfc_name));
1323                                 }
1324                                 rc = BG_ERR_INIT;
1325
1326                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1327                                         "0814 BLKGRD: Injecting apptag error: "
1328                                         "read lba x%lx\n", (unsigned long)lba);
1329                                 break;
1330                         }
1331                 }
1332         }
1333
1334
1335         /* Should we change the Guard Tag */
1336         if (new_guard) {
1337                 if (phba->lpfc_injerr_wgrd_cnt) {
1338                         switch (op) {
1339                         case SCSI_PROT_WRITE_PASS:
1340                                 rc = BG_ERR_CHECK;
1341                                 /* fall through */
1342
1343                         case SCSI_PROT_WRITE_INSERT:
1344                                 /*
1345                                  * For WRITE_INSERT, force the
1346                                  * error to be sent on the wire. It should be
1347                                  * detected by the Target.
1348                                  */
1349                                 phba->lpfc_injerr_wgrd_cnt--;
1350                                 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1351                                         phba->lpfc_injerr_nportid = 0;
1352                                         phba->lpfc_injerr_lba =
1353                                                 LPFC_INJERR_LBA_OFF;
1354                                         memset(&phba->lpfc_injerr_wwpn,
1355                                                 0, sizeof(struct lpfc_name));
1356                                 }
1357
1358                                 rc |= BG_ERR_TGT | BG_ERR_SWAP;
1359                                 /* Signals the caller to swap CRC->CSUM */
1360
1361                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1362                                         "0817 BLKGRD: Injecting guard error: "
1363                                         "write lba x%lx\n", (unsigned long)lba);
1364                                 break;
1365                         case SCSI_PROT_WRITE_STRIP:
1366                                 /*
1367                                  * For WRITE_STRIP and WRITE_PASS,
1368                                  * force the error on data
1369                                  * being copied from SLI-Host to SLI-Port.
1370                                  */
1371                                 phba->lpfc_injerr_wgrd_cnt--;
1372                                 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1373                                         phba->lpfc_injerr_nportid = 0;
1374                                         phba->lpfc_injerr_lba =
1375                                                 LPFC_INJERR_LBA_OFF;
1376                                         memset(&phba->lpfc_injerr_wwpn,
1377                                                 0, sizeof(struct lpfc_name));
1378                                 }
1379
1380                                 rc = BG_ERR_INIT | BG_ERR_SWAP;
1381                                 /* Signals the caller to swap CRC->CSUM */
1382
1383                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1384                                         "0816 BLKGRD: Injecting guard error: "
1385                                         "write lba x%lx\n", (unsigned long)lba);
1386                                 break;
1387                         }
1388                 }
1389                 if (phba->lpfc_injerr_rgrd_cnt) {
1390                         switch (op) {
1391                         case SCSI_PROT_READ_INSERT:
1392                         case SCSI_PROT_READ_STRIP:
1393                         case SCSI_PROT_READ_PASS:
1394                                 /*
1395                                  * For READ_STRIP and READ_PASS, force the
1396                                  * error on data being read off the wire. It
1397                                  * should force an IO error to the driver.
1398                                  */
1399                                 phba->lpfc_injerr_rgrd_cnt--;
1400                                 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1401                                         phba->lpfc_injerr_nportid = 0;
1402                                         phba->lpfc_injerr_lba =
1403                                                 LPFC_INJERR_LBA_OFF;
1404                                         memset(&phba->lpfc_injerr_wwpn,
1405                                                 0, sizeof(struct lpfc_name));
1406                                 }
1407
1408                                 rc = BG_ERR_INIT | BG_ERR_SWAP;
1409                                 /* Signals the caller to swap CRC->CSUM */
1410
1411                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1412                                         "0818 BLKGRD: Injecting guard error: "
1413                                         "read lba x%lx\n", (unsigned long)lba);
1414                         }
1415                 }
1416         }
1417
1418         return rc;
1419 }
1420 #endif
1421
1422 /**
1423  * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1424  * the specified SCSI command.
1425  * @phba: The Hba for which this call is being executed.
1426  * @sc: The SCSI command to examine
1427  * @txopt: (out) BlockGuard operation for transmitted data
1428  * @rxopt: (out) BlockGuard operation for received data
1429  *
1430  * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1431  *
1432  **/
1433 static int
1434 lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1435                 uint8_t *txop, uint8_t *rxop)
1436 {
1437         uint8_t ret = 0;
1438
1439         if (lpfc_cmd_guard_csum(sc)) {
1440                 switch (scsi_get_prot_op(sc)) {
1441                 case SCSI_PROT_READ_INSERT:
1442                 case SCSI_PROT_WRITE_STRIP:
1443                         *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1444                         *txop = BG_OP_IN_CSUM_OUT_NODIF;
1445                         break;
1446
1447                 case SCSI_PROT_READ_STRIP:
1448                 case SCSI_PROT_WRITE_INSERT:
1449                         *rxop = BG_OP_IN_CRC_OUT_NODIF;
1450                         *txop = BG_OP_IN_NODIF_OUT_CRC;
1451                         break;
1452
1453                 case SCSI_PROT_READ_PASS:
1454                 case SCSI_PROT_WRITE_PASS:
1455                         *rxop = BG_OP_IN_CRC_OUT_CSUM;
1456                         *txop = BG_OP_IN_CSUM_OUT_CRC;
1457                         break;
1458
1459                 case SCSI_PROT_NORMAL:
1460                 default:
1461                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1462                                 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1463                                         scsi_get_prot_op(sc));
1464                         ret = 1;
1465                         break;
1466
1467                 }
1468         } else {
1469                 switch (scsi_get_prot_op(sc)) {
1470                 case SCSI_PROT_READ_STRIP:
1471                 case SCSI_PROT_WRITE_INSERT:
1472                         *rxop = BG_OP_IN_CRC_OUT_NODIF;
1473                         *txop = BG_OP_IN_NODIF_OUT_CRC;
1474                         break;
1475
1476                 case SCSI_PROT_READ_PASS:
1477                 case SCSI_PROT_WRITE_PASS:
1478                         *rxop = BG_OP_IN_CRC_OUT_CRC;
1479                         *txop = BG_OP_IN_CRC_OUT_CRC;
1480                         break;
1481
1482                 case SCSI_PROT_READ_INSERT:
1483                 case SCSI_PROT_WRITE_STRIP:
1484                         *rxop = BG_OP_IN_NODIF_OUT_CRC;
1485                         *txop = BG_OP_IN_CRC_OUT_NODIF;
1486                         break;
1487
1488                 case SCSI_PROT_NORMAL:
1489                 default:
1490                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1491                                 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1492                                         scsi_get_prot_op(sc));
1493                         ret = 1;
1494                         break;
1495                 }
1496         }
1497
1498         return ret;
1499 }
1500
1501 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1502 /**
1503  * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1504  * the specified SCSI command in order to force a guard tag error.
1505  * @phba: The Hba for which this call is being executed.
1506  * @sc: The SCSI command to examine
1507  * @txopt: (out) BlockGuard operation for transmitted data
1508  * @rxopt: (out) BlockGuard operation for received data
1509  *
1510  * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1511  *
1512  **/
1513 static int
1514 lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1515                 uint8_t *txop, uint8_t *rxop)
1516 {
1517         uint8_t ret = 0;
1518
1519         if (lpfc_cmd_guard_csum(sc)) {
1520                 switch (scsi_get_prot_op(sc)) {
1521                 case SCSI_PROT_READ_INSERT:
1522                 case SCSI_PROT_WRITE_STRIP:
1523                         *rxop = BG_OP_IN_NODIF_OUT_CRC;
1524                         *txop = BG_OP_IN_CRC_OUT_NODIF;
1525                         break;
1526
1527                 case SCSI_PROT_READ_STRIP:
1528                 case SCSI_PROT_WRITE_INSERT:
1529                         *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1530                         *txop = BG_OP_IN_NODIF_OUT_CSUM;
1531                         break;
1532
1533                 case SCSI_PROT_READ_PASS:
1534                 case SCSI_PROT_WRITE_PASS:
1535                         *rxop = BG_OP_IN_CSUM_OUT_CRC;
1536                         *txop = BG_OP_IN_CRC_OUT_CSUM;
1537                         break;
1538
1539                 case SCSI_PROT_NORMAL:
1540                 default:
1541                         break;
1542
1543                 }
1544         } else {
1545                 switch (scsi_get_prot_op(sc)) {
1546                 case SCSI_PROT_READ_STRIP:
1547                 case SCSI_PROT_WRITE_INSERT:
1548                         *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1549                         *txop = BG_OP_IN_NODIF_OUT_CSUM;
1550                         break;
1551
1552                 case SCSI_PROT_READ_PASS:
1553                 case SCSI_PROT_WRITE_PASS:
1554                         *rxop = BG_OP_IN_CSUM_OUT_CSUM;
1555                         *txop = BG_OP_IN_CSUM_OUT_CSUM;
1556                         break;
1557
1558                 case SCSI_PROT_READ_INSERT:
1559                 case SCSI_PROT_WRITE_STRIP:
1560                         *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1561                         *txop = BG_OP_IN_CSUM_OUT_NODIF;
1562                         break;
1563
1564                 case SCSI_PROT_NORMAL:
1565                 default:
1566                         break;
1567                 }
1568         }
1569
1570         return ret;
1571 }
1572 #endif
1573
1574 /**
1575  * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1576  * @phba: The Hba for which this call is being executed.
1577  * @sc: pointer to scsi command we're working on
1578  * @bpl: pointer to buffer list for protection groups
1579  * @datacnt: number of segments of data that have been dma mapped
1580  *
1581  * This function sets up BPL buffer list for protection groups of
1582  * type LPFC_PG_TYPE_NO_DIF
1583  *
1584  * This is usually used when the HBA is instructed to generate
1585  * DIFs and insert them into data stream (or strip DIF from
1586  * incoming data stream)
1587  *
1588  * The buffer list consists of just one protection group described
1589  * below:
1590  *                                +-------------------------+
1591  *   start of prot group  -->     |          PDE_5          |
1592  *                                +-------------------------+
1593  *                                |          PDE_6          |
1594  *                                +-------------------------+
1595  *                                |         Data BDE        |
1596  *                                +-------------------------+
1597  *                                |more Data BDE's ... (opt)|
1598  *                                +-------------------------+
1599  *
1600  *
1601  * Note: Data s/g buffers have been dma mapped
1602  *
1603  * Returns the number of BDEs added to the BPL.
1604  **/
1605 static int
1606 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1607                 struct ulp_bde64 *bpl, int datasegcnt)
1608 {
1609         struct scatterlist *sgde = NULL; /* s/g data entry */
1610         struct lpfc_pde5 *pde5 = NULL;
1611         struct lpfc_pde6 *pde6 = NULL;
1612         dma_addr_t physaddr;
1613         int i = 0, num_bde = 0, status;
1614         int datadir = sc->sc_data_direction;
1615 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1616         uint32_t rc;
1617 #endif
1618         uint32_t checking = 1;
1619         uint32_t reftag;
1620         uint8_t txop, rxop;
1621
1622         status  = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1623         if (status)
1624                 goto out;
1625
1626         /* extract some info from the scsi command for pde*/
1627         reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
1628
1629 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1630         rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1631         if (rc) {
1632                 if (rc & BG_ERR_SWAP)
1633                         lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1634                 if (rc & BG_ERR_CHECK)
1635                         checking = 0;
1636         }
1637 #endif
1638
1639         /* setup PDE5 with what we have */
1640         pde5 = (struct lpfc_pde5 *) bpl;
1641         memset(pde5, 0, sizeof(struct lpfc_pde5));
1642         bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1643
1644         /* Endianness conversion if necessary for PDE5 */
1645         pde5->word0 = cpu_to_le32(pde5->word0);
1646         pde5->reftag = cpu_to_le32(reftag);
1647
1648         /* advance bpl and increment bde count */
1649         num_bde++;
1650         bpl++;
1651         pde6 = (struct lpfc_pde6 *) bpl;
1652
1653         /* setup PDE6 with the rest of the info */
1654         memset(pde6, 0, sizeof(struct lpfc_pde6));
1655         bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1656         bf_set(pde6_optx, pde6, txop);
1657         bf_set(pde6_oprx, pde6, rxop);
1658
1659         /*
1660          * We only need to check the data on READs, for WRITEs
1661          * protection data is automatically generated, not checked.
1662          */
1663         if (datadir == DMA_FROM_DEVICE) {
1664                 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
1665                         bf_set(pde6_ce, pde6, checking);
1666                 else
1667                         bf_set(pde6_ce, pde6, 0);
1668
1669                 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
1670                         bf_set(pde6_re, pde6, checking);
1671                 else
1672                         bf_set(pde6_re, pde6, 0);
1673         }
1674         bf_set(pde6_ai, pde6, 1);
1675         bf_set(pde6_ae, pde6, 0);
1676         bf_set(pde6_apptagval, pde6, 0);
1677
1678         /* Endianness conversion if necessary for PDE6 */
1679         pde6->word0 = cpu_to_le32(pde6->word0);
1680         pde6->word1 = cpu_to_le32(pde6->word1);
1681         pde6->word2 = cpu_to_le32(pde6->word2);
1682
1683         /* advance bpl and increment bde count */
1684         num_bde++;
1685         bpl++;
1686
1687         /* assumption: caller has already run dma_map_sg on command data */
1688         scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1689                 physaddr = sg_dma_address(sgde);
1690                 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1691                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1692                 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1693                 if (datadir == DMA_TO_DEVICE)
1694                         bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1695                 else
1696                         bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1697                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1698                 bpl++;
1699                 num_bde++;
1700         }
1701
1702 out:
1703         return num_bde;
1704 }
1705
1706 /**
1707  * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1708  * @phba: The Hba for which this call is being executed.
1709  * @sc: pointer to scsi command we're working on
1710  * @bpl: pointer to buffer list for protection groups
1711  * @datacnt: number of segments of data that have been dma mapped
1712  * @protcnt: number of segment of protection data that have been dma mapped
1713  *
1714  * This function sets up BPL buffer list for protection groups of
1715  * type LPFC_PG_TYPE_DIF
1716  *
1717  * This is usually used when DIFs are in their own buffers,
1718  * separate from the data. The HBA can then by instructed
1719  * to place the DIFs in the outgoing stream.  For read operations,
1720  * The HBA could extract the DIFs and place it in DIF buffers.
1721  *
1722  * The buffer list for this type consists of one or more of the
1723  * protection groups described below:
1724  *                                    +-------------------------+
1725  *   start of first prot group  -->   |          PDE_5          |
1726  *                                    +-------------------------+
1727  *                                    |          PDE_6          |
1728  *                                    +-------------------------+
1729  *                                    |      PDE_7 (Prot BDE)   |
1730  *                                    +-------------------------+
1731  *                                    |        Data BDE         |
1732  *                                    +-------------------------+
1733  *                                    |more Data BDE's ... (opt)|
1734  *                                    +-------------------------+
1735  *   start of new  prot group  -->    |          PDE_5          |
1736  *                                    +-------------------------+
1737  *                                    |          ...            |
1738  *                                    +-------------------------+
1739  *
1740  * Note: It is assumed that both data and protection s/g buffers have been
1741  *       mapped for DMA
1742  *
1743  * Returns the number of BDEs added to the BPL.
1744  **/
1745 static int
1746 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1747                 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1748 {
1749         struct scatterlist *sgde = NULL; /* s/g data entry */
1750         struct scatterlist *sgpe = NULL; /* s/g prot entry */
1751         struct lpfc_pde5 *pde5 = NULL;
1752         struct lpfc_pde6 *pde6 = NULL;
1753         struct lpfc_pde7 *pde7 = NULL;
1754         dma_addr_t dataphysaddr, protphysaddr;
1755         unsigned short curr_data = 0, curr_prot = 0;
1756         unsigned int split_offset;
1757         unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
1758         unsigned int protgrp_blks, protgrp_bytes;
1759         unsigned int remainder, subtotal;
1760         int status;
1761         int datadir = sc->sc_data_direction;
1762         unsigned char pgdone = 0, alldone = 0;
1763         unsigned blksize;
1764 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1765         uint32_t rc;
1766 #endif
1767         uint32_t checking = 1;
1768         uint32_t reftag;
1769         uint8_t txop, rxop;
1770         int num_bde = 0;
1771
1772         sgpe = scsi_prot_sglist(sc);
1773         sgde = scsi_sglist(sc);
1774
1775         if (!sgpe || !sgde) {
1776                 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1777                                 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1778                                 sgpe, sgde);
1779                 return 0;
1780         }
1781
1782         status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1783         if (status)
1784                 goto out;
1785
1786         /* extract some info from the scsi command */
1787         blksize = lpfc_cmd_blksize(sc);
1788         reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
1789
1790 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1791         rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1792         if (rc) {
1793                 if (rc & BG_ERR_SWAP)
1794                         lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1795                 if (rc & BG_ERR_CHECK)
1796                         checking = 0;
1797         }
1798 #endif
1799
1800         split_offset = 0;
1801         do {
1802                 /* Check to see if we ran out of space */
1803                 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1804                         return num_bde + 3;
1805
1806                 /* setup PDE5 with what we have */
1807                 pde5 = (struct lpfc_pde5 *) bpl;
1808                 memset(pde5, 0, sizeof(struct lpfc_pde5));
1809                 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1810
1811                 /* Endianness conversion if necessary for PDE5 */
1812                 pde5->word0 = cpu_to_le32(pde5->word0);
1813                 pde5->reftag = cpu_to_le32(reftag);
1814
1815                 /* advance bpl and increment bde count */
1816                 num_bde++;
1817                 bpl++;
1818                 pde6 = (struct lpfc_pde6 *) bpl;
1819
1820                 /* setup PDE6 with the rest of the info */
1821                 memset(pde6, 0, sizeof(struct lpfc_pde6));
1822                 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1823                 bf_set(pde6_optx, pde6, txop);
1824                 bf_set(pde6_oprx, pde6, rxop);
1825
1826                 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
1827                         bf_set(pde6_ce, pde6, checking);
1828                 else
1829                         bf_set(pde6_ce, pde6, 0);
1830
1831                 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
1832                         bf_set(pde6_re, pde6, checking);
1833                 else
1834                         bf_set(pde6_re, pde6, 0);
1835
1836                 bf_set(pde6_ai, pde6, 1);
1837                 bf_set(pde6_ae, pde6, 0);
1838                 bf_set(pde6_apptagval, pde6, 0);
1839
1840                 /* Endianness conversion if necessary for PDE6 */
1841                 pde6->word0 = cpu_to_le32(pde6->word0);
1842                 pde6->word1 = cpu_to_le32(pde6->word1);
1843                 pde6->word2 = cpu_to_le32(pde6->word2);
1844
1845                 /* advance bpl and increment bde count */
1846                 num_bde++;
1847                 bpl++;
1848
1849                 /* setup the first BDE that points to protection buffer */
1850                 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1851                 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
1852
1853                 /* must be integer multiple of the DIF block length */
1854                 BUG_ON(protgroup_len % 8);
1855
1856                 pde7 = (struct lpfc_pde7 *) bpl;
1857                 memset(pde7, 0, sizeof(struct lpfc_pde7));
1858                 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1859
1860                 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1861                 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1862
1863                 protgrp_blks = protgroup_len / 8;
1864                 protgrp_bytes = protgrp_blks * blksize;
1865
1866                 /* check if this pde is crossing the 4K boundary; if so split */
1867                 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1868                         protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1869                         protgroup_offset += protgroup_remainder;
1870                         protgrp_blks = protgroup_remainder / 8;
1871                         protgrp_bytes = protgrp_blks * blksize;
1872                 } else {
1873                         protgroup_offset = 0;
1874                         curr_prot++;
1875                 }
1876
1877                 num_bde++;
1878
1879                 /* setup BDE's for data blocks associated with DIF data */
1880                 pgdone = 0;
1881                 subtotal = 0; /* total bytes processed for current prot grp */
1882                 while (!pgdone) {
1883                         /* Check to see if we ran out of space */
1884                         if (num_bde >= phba->cfg_total_seg_cnt)
1885                                 return num_bde + 1;
1886
1887                         if (!sgde) {
1888                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1889                                         "9065 BLKGRD:%s Invalid data segment\n",
1890                                                 __func__);
1891                                 return 0;
1892                         }
1893                         bpl++;
1894                         dataphysaddr = sg_dma_address(sgde) + split_offset;
1895                         bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1896                         bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1897
1898                         remainder = sg_dma_len(sgde) - split_offset;
1899
1900                         if ((subtotal + remainder) <= protgrp_bytes) {
1901                                 /* we can use this whole buffer */
1902                                 bpl->tus.f.bdeSize = remainder;
1903                                 split_offset = 0;
1904
1905                                 if ((subtotal + remainder) == protgrp_bytes)
1906                                         pgdone = 1;
1907                         } else {
1908                                 /* must split this buffer with next prot grp */
1909                                 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1910                                 split_offset += bpl->tus.f.bdeSize;
1911                         }
1912
1913                         subtotal += bpl->tus.f.bdeSize;
1914
1915                         if (datadir == DMA_TO_DEVICE)
1916                                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1917                         else
1918                                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1919                         bpl->tus.w = le32_to_cpu(bpl->tus.w);
1920
1921                         num_bde++;
1922                         curr_data++;
1923
1924                         if (split_offset)
1925                                 break;
1926
1927                         /* Move to the next s/g segment if possible */
1928                         sgde = sg_next(sgde);
1929
1930                 }
1931
1932                 if (protgroup_offset) {
1933                         /* update the reference tag */
1934                         reftag += protgrp_blks;
1935                         bpl++;
1936                         continue;
1937                 }
1938
1939                 /* are we done ? */
1940                 if (curr_prot == protcnt) {
1941                         alldone = 1;
1942                 } else if (curr_prot < protcnt) {
1943                         /* advance to next prot buffer */
1944                         sgpe = sg_next(sgpe);
1945                         bpl++;
1946
1947                         /* update the reference tag */
1948                         reftag += protgrp_blks;
1949                 } else {
1950                         /* if we're here, we have a bug */
1951                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1952                                 "9054 BLKGRD: bug in %s\n", __func__);
1953                 }
1954
1955         } while (!alldone);
1956 out:
1957
1958         return num_bde;
1959 }
1960
1961 /**
1962  * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
1963  * @phba: The Hba for which this call is being executed.
1964  * @sc: pointer to scsi command we're working on
1965  * @sgl: pointer to buffer list for protection groups
1966  * @datacnt: number of segments of data that have been dma mapped
1967  *
1968  * This function sets up SGL buffer list for protection groups of
1969  * type LPFC_PG_TYPE_NO_DIF
1970  *
1971  * This is usually used when the HBA is instructed to generate
1972  * DIFs and insert them into data stream (or strip DIF from
1973  * incoming data stream)
1974  *
1975  * The buffer list consists of just one protection group described
1976  * below:
1977  *                                +-------------------------+
1978  *   start of prot group  -->     |         DI_SEED         |
1979  *                                +-------------------------+
1980  *                                |         Data SGE        |
1981  *                                +-------------------------+
1982  *                                |more Data SGE's ... (opt)|
1983  *                                +-------------------------+
1984  *
1985  *
1986  * Note: Data s/g buffers have been dma mapped
1987  *
1988  * Returns the number of SGEs added to the SGL.
1989  **/
1990 static int
1991 lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1992                 struct sli4_sge *sgl, int datasegcnt)
1993 {
1994         struct scatterlist *sgde = NULL; /* s/g data entry */
1995         struct sli4_sge_diseed *diseed = NULL;
1996         dma_addr_t physaddr;
1997         int i = 0, num_sge = 0, status;
1998         uint32_t reftag;
1999         uint8_t txop, rxop;
2000 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2001         uint32_t rc;
2002 #endif
2003         uint32_t checking = 1;
2004         uint32_t dma_len;
2005         uint32_t dma_offset = 0;
2006
2007         status  = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2008         if (status)
2009                 goto out;
2010
2011         /* extract some info from the scsi command for pde*/
2012         reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2013
2014 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2015         rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2016         if (rc) {
2017                 if (rc & BG_ERR_SWAP)
2018                         lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2019                 if (rc & BG_ERR_CHECK)
2020                         checking = 0;
2021         }
2022 #endif
2023
2024         /* setup DISEED with what we have */
2025         diseed = (struct sli4_sge_diseed *) sgl;
2026         memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2027         bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2028
2029         /* Endianness conversion if necessary */
2030         diseed->ref_tag = cpu_to_le32(reftag);
2031         diseed->ref_tag_tran = diseed->ref_tag;
2032
2033         /*
2034          * We only need to check the data on READs, for WRITEs
2035          * protection data is automatically generated, not checked.
2036          */
2037         if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2038                 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2039                         bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2040                 else
2041                         bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2042
2043                 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2044                         bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2045                 else
2046                         bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2047         }
2048
2049         /* setup DISEED with the rest of the info */
2050         bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2051         bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2052
2053         bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2054         bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2055
2056         /* Endianness conversion if necessary for DISEED */
2057         diseed->word2 = cpu_to_le32(diseed->word2);
2058         diseed->word3 = cpu_to_le32(diseed->word3);
2059
2060         /* advance bpl and increment sge count */
2061         num_sge++;
2062         sgl++;
2063
2064         /* assumption: caller has already run dma_map_sg on command data */
2065         scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2066                 physaddr = sg_dma_address(sgde);
2067                 dma_len = sg_dma_len(sgde);
2068                 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2069                 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2070                 if ((i + 1) == datasegcnt)
2071                         bf_set(lpfc_sli4_sge_last, sgl, 1);
2072                 else
2073                         bf_set(lpfc_sli4_sge_last, sgl, 0);
2074                 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2075                 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2076
2077                 sgl->sge_len = cpu_to_le32(dma_len);
2078                 dma_offset += dma_len;
2079
2080                 sgl++;
2081                 num_sge++;
2082         }
2083
2084 out:
2085         return num_sge;
2086 }
2087
2088 /**
2089  * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2090  * @phba: The Hba for which this call is being executed.
2091  * @sc: pointer to scsi command we're working on
2092  * @sgl: pointer to buffer list for protection groups
2093  * @datacnt: number of segments of data that have been dma mapped
2094  * @protcnt: number of segment of protection data that have been dma mapped
2095  *
2096  * This function sets up SGL buffer list for protection groups of
2097  * type LPFC_PG_TYPE_DIF
2098  *
2099  * This is usually used when DIFs are in their own buffers,
2100  * separate from the data. The HBA can then by instructed
2101  * to place the DIFs in the outgoing stream.  For read operations,
2102  * The HBA could extract the DIFs and place it in DIF buffers.
2103  *
2104  * The buffer list for this type consists of one or more of the
2105  * protection groups described below:
2106  *                                    +-------------------------+
2107  *   start of first prot group  -->   |         DISEED          |
2108  *                                    +-------------------------+
2109  *                                    |      DIF (Prot SGE)     |
2110  *                                    +-------------------------+
2111  *                                    |        Data SGE         |
2112  *                                    +-------------------------+
2113  *                                    |more Data SGE's ... (opt)|
2114  *                                    +-------------------------+
2115  *   start of new  prot group  -->    |         DISEED          |
2116  *                                    +-------------------------+
2117  *                                    |          ...            |
2118  *                                    +-------------------------+
2119  *
2120  * Note: It is assumed that both data and protection s/g buffers have been
2121  *       mapped for DMA
2122  *
2123  * Returns the number of SGEs added to the SGL.
2124  **/
2125 static int
2126 lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2127                 struct sli4_sge *sgl, int datacnt, int protcnt)
2128 {
2129         struct scatterlist *sgde = NULL; /* s/g data entry */
2130         struct scatterlist *sgpe = NULL; /* s/g prot entry */
2131         struct sli4_sge_diseed *diseed = NULL;
2132         dma_addr_t dataphysaddr, protphysaddr;
2133         unsigned short curr_data = 0, curr_prot = 0;
2134         unsigned int split_offset;
2135         unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2136         unsigned int protgrp_blks, protgrp_bytes;
2137         unsigned int remainder, subtotal;
2138         int status;
2139         unsigned char pgdone = 0, alldone = 0;
2140         unsigned blksize;
2141         uint32_t reftag;
2142         uint8_t txop, rxop;
2143         uint32_t dma_len;
2144 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2145         uint32_t rc;
2146 #endif
2147         uint32_t checking = 1;
2148         uint32_t dma_offset = 0;
2149         int num_sge = 0;
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=0x%p prot=0x%p\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         /* extract some info from the scsi command */
2166         blksize = lpfc_cmd_blksize(sc);
2167         reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
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                 /* Check to see if we ran out of space */
2182                 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2183                         return num_sge + 3;
2184
2185                 /* setup DISEED with what we have */
2186                 diseed = (struct sli4_sge_diseed *) sgl;
2187                 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2188                 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2189
2190                 /* Endianness conversion if necessary */
2191                 diseed->ref_tag = cpu_to_le32(reftag);
2192                 diseed->ref_tag_tran = diseed->ref_tag;
2193
2194                 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
2195                         bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2196
2197                 } else {
2198                         bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2199                         /*
2200                          * When in this mode, the hardware will replace
2201                          * the guard tag from the host with a
2202                          * newly generated good CRC for the wire.
2203                          * Switch to raw mode here to avoid this
2204                          * behavior. What the host sends gets put on the wire.
2205                          */
2206                         if (txop == BG_OP_IN_CRC_OUT_CRC) {
2207                                 txop = BG_OP_RAW_MODE;
2208                                 rxop = BG_OP_RAW_MODE;
2209                         }
2210                 }
2211
2212
2213                 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2214                         bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2215                 else
2216                         bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2217
2218                 /* setup DISEED with the rest of the info */
2219                 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2220                 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2221
2222                 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2223                 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2224
2225                 /* Endianness conversion if necessary for DISEED */
2226                 diseed->word2 = cpu_to_le32(diseed->word2);
2227                 diseed->word3 = cpu_to_le32(diseed->word3);
2228
2229                 /* advance sgl and increment bde count */
2230                 num_sge++;
2231                 sgl++;
2232
2233                 /* setup the first BDE that points to protection buffer */
2234                 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2235                 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2236
2237                 /* must be integer multiple of the DIF block length */
2238                 BUG_ON(protgroup_len % 8);
2239
2240                 /* Now setup DIF SGE */
2241                 sgl->word2 = 0;
2242                 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2243                 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2244                 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2245                 sgl->word2 = cpu_to_le32(sgl->word2);
2246
2247                 protgrp_blks = protgroup_len / 8;
2248                 protgrp_bytes = protgrp_blks * blksize;
2249
2250                 /* check if DIF SGE is crossing the 4K boundary; if so split */
2251                 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2252                         protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2253                         protgroup_offset += protgroup_remainder;
2254                         protgrp_blks = protgroup_remainder / 8;
2255                         protgrp_bytes = protgrp_blks * blksize;
2256                 } else {
2257                         protgroup_offset = 0;
2258                         curr_prot++;
2259                 }
2260
2261                 num_sge++;
2262
2263                 /* setup SGE's for data blocks associated with DIF data */
2264                 pgdone = 0;
2265                 subtotal = 0; /* total bytes processed for current prot grp */
2266                 while (!pgdone) {
2267                         /* Check to see if we ran out of space */
2268                         if (num_sge >= phba->cfg_total_seg_cnt)
2269                                 return num_sge + 1;
2270
2271                         if (!sgde) {
2272                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2273                                         "9086 BLKGRD:%s Invalid data segment\n",
2274                                                 __func__);
2275                                 return 0;
2276                         }
2277                         sgl++;
2278                         dataphysaddr = sg_dma_address(sgde) + split_offset;
2279
2280                         remainder = sg_dma_len(sgde) - split_offset;
2281
2282                         if ((subtotal + remainder) <= protgrp_bytes) {
2283                                 /* we can use this whole buffer */
2284                                 dma_len = remainder;
2285                                 split_offset = 0;
2286
2287                                 if ((subtotal + remainder) == protgrp_bytes)
2288                                         pgdone = 1;
2289                         } else {
2290                                 /* must split this buffer with next prot grp */
2291                                 dma_len = protgrp_bytes - subtotal;
2292                                 split_offset += dma_len;
2293                         }
2294
2295                         subtotal += dma_len;
2296
2297                         sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2298                         sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2299                         bf_set(lpfc_sli4_sge_last, sgl, 0);
2300                         bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2301                         bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2302
2303                         sgl->sge_len = cpu_to_le32(dma_len);
2304                         dma_offset += dma_len;
2305
2306                         num_sge++;
2307                         curr_data++;
2308
2309                         if (split_offset)
2310                                 break;
2311
2312                         /* Move to the next s/g segment if possible */
2313                         sgde = sg_next(sgde);
2314                 }
2315
2316                 if (protgroup_offset) {
2317                         /* update the reference tag */
2318                         reftag += protgrp_blks;
2319                         sgl++;
2320                         continue;
2321                 }
2322
2323                 /* are we done ? */
2324                 if (curr_prot == protcnt) {
2325                         bf_set(lpfc_sli4_sge_last, sgl, 1);
2326                         alldone = 1;
2327                 } else if (curr_prot < protcnt) {
2328                         /* advance to next prot buffer */
2329                         sgpe = sg_next(sgpe);
2330                         sgl++;
2331
2332                         /* update the reference tag */
2333                         reftag += protgrp_blks;
2334                 } else {
2335                         /* if we're here, we have a bug */
2336                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2337                                 "9085 BLKGRD: bug in %s\n", __func__);
2338                 }
2339
2340         } while (!alldone);
2341
2342 out:
2343
2344         return num_sge;
2345 }
2346
2347 /**
2348  * lpfc_prot_group_type - Get prtotection group type of SCSI command
2349  * @phba: The Hba for which this call is being executed.
2350  * @sc: pointer to scsi command we're working on
2351  *
2352  * Given a SCSI command that supports DIF, determine composition of protection
2353  * groups involved in setting up buffer lists
2354  *
2355  * Returns: Protection group type (with or without DIF)
2356  *
2357  **/
2358 static int
2359 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2360 {
2361         int ret = LPFC_PG_TYPE_INVALID;
2362         unsigned char op = scsi_get_prot_op(sc);
2363
2364         switch (op) {
2365         case SCSI_PROT_READ_STRIP:
2366         case SCSI_PROT_WRITE_INSERT:
2367                 ret = LPFC_PG_TYPE_NO_DIF;
2368                 break;
2369         case SCSI_PROT_READ_INSERT:
2370         case SCSI_PROT_WRITE_STRIP:
2371         case SCSI_PROT_READ_PASS:
2372         case SCSI_PROT_WRITE_PASS:
2373                 ret = LPFC_PG_TYPE_DIF_BUF;
2374                 break;
2375         default:
2376                 if (phba)
2377                         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2378                                         "9021 Unsupported protection op:%d\n",
2379                                         op);
2380                 break;
2381         }
2382         return ret;
2383 }
2384
2385 /**
2386  * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2387  * @phba: The Hba for which this call is being executed.
2388  * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2389  *
2390  * Adjust the data length to account for how much data
2391  * is actually on the wire.
2392  *
2393  * returns the adjusted data length
2394  **/
2395 static int
2396 lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2397                        struct lpfc_io_buf *lpfc_cmd)
2398 {
2399         struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2400         int fcpdl;
2401
2402         fcpdl = scsi_bufflen(sc);
2403
2404         /* Check if there is protection data on the wire */
2405         if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2406                 /* Read check for protection data */
2407                 if (scsi_get_prot_op(sc) ==  SCSI_PROT_READ_INSERT)
2408                         return fcpdl;
2409
2410         } else {
2411                 /* Write check for protection data */
2412                 if (scsi_get_prot_op(sc) ==  SCSI_PROT_WRITE_STRIP)
2413                         return fcpdl;
2414         }
2415
2416         /*
2417          * If we are in DIF Type 1 mode every data block has a 8 byte
2418          * DIF (trailer) attached to it. Must ajust FCP data length
2419          * to account for the protection data.
2420          */
2421         fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
2422
2423         return fcpdl;
2424 }
2425
2426 /**
2427  * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2428  * @phba: The Hba for which this call is being executed.
2429  * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2430  *
2431  * This is the protection/DIF aware version of
2432  * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2433  * two functions eventually, but for now, it's here
2434  **/
2435 static int
2436 lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
2437                 struct lpfc_io_buf *lpfc_cmd)
2438 {
2439         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2440         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2441         struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
2442         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2443         uint32_t num_bde = 0;
2444         int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2445         int prot_group_type = 0;
2446         int fcpdl;
2447         struct lpfc_vport *vport = phba->pport;
2448
2449         /*
2450          * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2451          *  fcp_rsp regions to the first data bde entry
2452          */
2453         bpl += 2;
2454         if (scsi_sg_count(scsi_cmnd)) {
2455                 /*
2456                  * The driver stores the segment count returned from pci_map_sg
2457                  * because this a count of dma-mappings used to map the use_sg
2458                  * pages.  They are not guaranteed to be the same for those
2459                  * architectures that implement an IOMMU.
2460                  */
2461                 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2462                                         scsi_sglist(scsi_cmnd),
2463                                         scsi_sg_count(scsi_cmnd), datadir);
2464                 if (unlikely(!datasegcnt))
2465                         return 1;
2466
2467                 lpfc_cmd->seg_cnt = datasegcnt;
2468
2469                 /* First check if data segment count from SCSI Layer is good */
2470                 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2471                         goto err;
2472
2473                 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2474
2475                 switch (prot_group_type) {
2476                 case LPFC_PG_TYPE_NO_DIF:
2477
2478                         /* Here we need to add a PDE5 and PDE6 to the count */
2479                         if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2480                                 goto err;
2481
2482                         num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2483                                         datasegcnt);
2484                         /* we should have 2 or more entries in buffer list */
2485                         if (num_bde < 2)
2486                                 goto err;
2487                         break;
2488
2489                 case LPFC_PG_TYPE_DIF_BUF:
2490                         /*
2491                          * This type indicates that protection buffers are
2492                          * passed to the driver, so that needs to be prepared
2493                          * for DMA
2494                          */
2495                         protsegcnt = dma_map_sg(&phba->pcidev->dev,
2496                                         scsi_prot_sglist(scsi_cmnd),
2497                                         scsi_prot_sg_count(scsi_cmnd), datadir);
2498                         if (unlikely(!protsegcnt)) {
2499                                 scsi_dma_unmap(scsi_cmnd);
2500                                 return 1;
2501                         }
2502
2503                         lpfc_cmd->prot_seg_cnt = protsegcnt;
2504
2505                         /*
2506                          * There is a minimun of 4 BPLs used for every
2507                          * protection data segment.
2508                          */
2509                         if ((lpfc_cmd->prot_seg_cnt * 4) >
2510                             (phba->cfg_total_seg_cnt - 2))
2511                                 goto err;
2512
2513                         num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2514                                         datasegcnt, protsegcnt);
2515                         /* we should have 3 or more entries in buffer list */
2516                         if ((num_bde < 3) ||
2517                             (num_bde > phba->cfg_total_seg_cnt))
2518                                 goto err;
2519                         break;
2520
2521                 case LPFC_PG_TYPE_INVALID:
2522                 default:
2523                         scsi_dma_unmap(scsi_cmnd);
2524                         lpfc_cmd->seg_cnt = 0;
2525
2526                         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2527                                         "9022 Unexpected protection group %i\n",
2528                                         prot_group_type);
2529                         return 1;
2530                 }
2531         }
2532
2533         /*
2534          * Finish initializing those IOCB fields that are dependent on the
2535          * scsi_cmnd request_buffer.  Note that the bdeSize is explicitly
2536          * reinitialized since all iocb memory resources are used many times
2537          * for transmit, receive, and continuation bpl's.
2538          */
2539         iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2540         iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2541         iocb_cmd->ulpBdeCount = 1;
2542         iocb_cmd->ulpLe = 1;
2543
2544         fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
2545         fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2546
2547         /*
2548          * Due to difference in data length between DIF/non-DIF paths,
2549          * we need to set word 4 of IOCB here
2550          */
2551         iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2552
2553         /*
2554          * For First burst, we may need to adjust the initial transfer
2555          * length for DIF
2556          */
2557         if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2558             (fcpdl < vport->cfg_first_burst_size))
2559                 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2560
2561         return 0;
2562 err:
2563         if (lpfc_cmd->seg_cnt)
2564                 scsi_dma_unmap(scsi_cmnd);
2565         if (lpfc_cmd->prot_seg_cnt)
2566                 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2567                              scsi_prot_sg_count(scsi_cmnd),
2568                              scsi_cmnd->sc_data_direction);
2569
2570         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2571                         "9023 Cannot setup S/G List for HBA"
2572                         "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2573                         lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2574                         phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
2575                         prot_group_type, num_bde);
2576
2577         lpfc_cmd->seg_cnt = 0;
2578         lpfc_cmd->prot_seg_cnt = 0;
2579         return 1;
2580 }
2581
2582 /*
2583  * This function calcuates the T10 DIF guard tag
2584  * on the specified data using a CRC algorithmn
2585  * using crc_t10dif.
2586  */
2587 static uint16_t
2588 lpfc_bg_crc(uint8_t *data, int count)
2589 {
2590         uint16_t crc = 0;
2591         uint16_t x;
2592
2593         crc = crc_t10dif(data, count);
2594         x = cpu_to_be16(crc);
2595         return x;
2596 }
2597
2598 /*
2599  * This function calcuates the T10 DIF guard tag
2600  * on the specified data using a CSUM algorithmn
2601  * using ip_compute_csum.
2602  */
2603 static uint16_t
2604 lpfc_bg_csum(uint8_t *data, int count)
2605 {
2606         uint16_t ret;
2607
2608         ret = ip_compute_csum(data, count);
2609         return ret;
2610 }
2611
2612 /*
2613  * This function examines the protection data to try to determine
2614  * what type of T10-DIF error occurred.
2615  */
2616 static void
2617 lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
2618 {
2619         struct scatterlist *sgpe; /* s/g prot entry */
2620         struct scatterlist *sgde; /* s/g data entry */
2621         struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2622         struct scsi_dif_tuple *src = NULL;
2623         uint8_t *data_src = NULL;
2624         uint16_t guard_tag;
2625         uint16_t start_app_tag, app_tag;
2626         uint32_t start_ref_tag, ref_tag;
2627         int prot, protsegcnt;
2628         int err_type, len, data_len;
2629         int chk_ref, chk_app, chk_guard;
2630         uint16_t sum;
2631         unsigned blksize;
2632
2633         err_type = BGS_GUARD_ERR_MASK;
2634         sum = 0;
2635         guard_tag = 0;
2636
2637         /* First check to see if there is protection data to examine */
2638         prot = scsi_get_prot_op(cmd);
2639         if ((prot == SCSI_PROT_READ_STRIP) ||
2640             (prot == SCSI_PROT_WRITE_INSERT) ||
2641             (prot == SCSI_PROT_NORMAL))
2642                 goto out;
2643
2644         /* Currently the driver just supports ref_tag and guard_tag checking */
2645         chk_ref = 1;
2646         chk_app = 0;
2647         chk_guard = 0;
2648
2649         /* Setup a ptr to the protection data provided by the SCSI host */
2650         sgpe = scsi_prot_sglist(cmd);
2651         protsegcnt = lpfc_cmd->prot_seg_cnt;
2652
2653         if (sgpe && protsegcnt) {
2654
2655                 /*
2656                  * We will only try to verify guard tag if the segment
2657                  * data length is a multiple of the blksize.
2658                  */
2659                 sgde = scsi_sglist(cmd);
2660                 blksize = lpfc_cmd_blksize(cmd);
2661                 data_src = (uint8_t *)sg_virt(sgde);
2662                 data_len = sgde->length;
2663                 if ((data_len & (blksize - 1)) == 0)
2664                         chk_guard = 1;
2665
2666                 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2667                 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
2668                 start_app_tag = src->app_tag;
2669                 len = sgpe->length;
2670                 while (src && protsegcnt) {
2671                         while (len) {
2672
2673                                 /*
2674                                  * First check to see if a protection data
2675                                  * check is valid
2676                                  */
2677                                 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2678                                     (src->app_tag == T10_PI_APP_ESCAPE)) {
2679                                         start_ref_tag++;
2680                                         goto skipit;
2681                                 }
2682
2683                                 /* First Guard Tag checking */
2684                                 if (chk_guard) {
2685                                         guard_tag = src->guard_tag;
2686                                         if (lpfc_cmd_guard_csum(cmd))
2687                                                 sum = lpfc_bg_csum(data_src,
2688                                                                    blksize);
2689                                         else
2690                                                 sum = lpfc_bg_crc(data_src,
2691                                                                   blksize);
2692                                         if ((guard_tag != sum)) {
2693                                                 err_type = BGS_GUARD_ERR_MASK;
2694                                                 goto out;
2695                                         }
2696                                 }
2697
2698                                 /* Reference Tag checking */
2699                                 ref_tag = be32_to_cpu(src->ref_tag);
2700                                 if (chk_ref && (ref_tag != start_ref_tag)) {
2701                                         err_type = BGS_REFTAG_ERR_MASK;
2702                                         goto out;
2703                                 }
2704                                 start_ref_tag++;
2705
2706                                 /* App Tag checking */
2707                                 app_tag = src->app_tag;
2708                                 if (chk_app && (app_tag != start_app_tag)) {
2709                                         err_type = BGS_APPTAG_ERR_MASK;
2710                                         goto out;
2711                                 }
2712 skipit:
2713                                 len -= sizeof(struct scsi_dif_tuple);
2714                                 if (len < 0)
2715                                         len = 0;
2716                                 src++;
2717
2718                                 data_src += blksize;
2719                                 data_len -= blksize;
2720
2721                                 /*
2722                                  * Are we at the end of the Data segment?
2723                                  * The data segment is only used for Guard
2724                                  * tag checking.
2725                                  */
2726                                 if (chk_guard && (data_len == 0)) {
2727                                         chk_guard = 0;
2728                                         sgde = sg_next(sgde);
2729                                         if (!sgde)
2730                                                 goto out;
2731
2732                                         data_src = (uint8_t *)sg_virt(sgde);
2733                                         data_len = sgde->length;
2734                                         if ((data_len & (blksize - 1)) == 0)
2735                                                 chk_guard = 1;
2736                                 }
2737                         }
2738
2739                         /* Goto the next Protection data segment */
2740                         sgpe = sg_next(sgpe);
2741                         if (sgpe) {
2742                                 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2743                                 len = sgpe->length;
2744                         } else {
2745                                 src = NULL;
2746                         }
2747                         protsegcnt--;
2748                 }
2749         }
2750 out:
2751         if (err_type == BGS_GUARD_ERR_MASK) {
2752                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2753                                         0x10, 0x1);
2754                 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2755                               SAM_STAT_CHECK_CONDITION;
2756                 phba->bg_guard_err_cnt++;
2757                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2758                                 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
2759                                 (unsigned long)scsi_get_lba(cmd),
2760                                 sum, guard_tag);
2761
2762         } else if (err_type == BGS_REFTAG_ERR_MASK) {
2763                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2764                                         0x10, 0x3);
2765                 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2766                               SAM_STAT_CHECK_CONDITION;
2767
2768                 phba->bg_reftag_err_cnt++;
2769                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2770                                 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
2771                                 (unsigned long)scsi_get_lba(cmd),
2772                                 ref_tag, start_ref_tag);
2773
2774         } else if (err_type == BGS_APPTAG_ERR_MASK) {
2775                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2776                                         0x10, 0x2);
2777                 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2778                               SAM_STAT_CHECK_CONDITION;
2779
2780                 phba->bg_apptag_err_cnt++;
2781                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2782                                 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
2783                                 (unsigned long)scsi_get_lba(cmd),
2784                                 app_tag, start_app_tag);
2785         }
2786 }
2787
2788
2789 /*
2790  * This function checks for BlockGuard errors detected by
2791  * the HBA.  In case of errors, the ASC/ASCQ fields in the
2792  * sense buffer will be set accordingly, paired with
2793  * ILLEGAL_REQUEST to signal to the kernel that the HBA
2794  * detected corruption.
2795  *
2796  * Returns:
2797  *  0 - No error found
2798  *  1 - BlockGuard error found
2799  * -1 - Internal error (bad profile, ...etc)
2800  */
2801 static int
2802 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
2803                   struct lpfc_iocbq *pIocbOut)
2804 {
2805         struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2806         struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2807         int ret = 0;
2808         uint32_t bghm = bgf->bghm;
2809         uint32_t bgstat = bgf->bgstat;
2810         uint64_t failing_sector = 0;
2811
2812         spin_lock(&_dump_buf_lock);
2813         if (!_dump_buf_done) {
2814                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,  "9070 BLKGRD: Saving"
2815                         " Data for %u blocks to debugfs\n",
2816                                 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2817                 lpfc_debug_save_data(phba, cmd);
2818
2819                 /* If we have a prot sgl, save the DIF buffer */
2820                 if (lpfc_prot_group_type(phba, cmd) ==
2821                                 LPFC_PG_TYPE_DIF_BUF) {
2822                         lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2823                                 "Saving DIF for %u blocks to debugfs\n",
2824                                 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2825                         lpfc_debug_save_dif(phba, cmd);
2826                 }
2827
2828                 _dump_buf_done = 1;
2829         }
2830         spin_unlock(&_dump_buf_lock);
2831
2832         if (lpfc_bgs_get_invalid_prof(bgstat)) {
2833                 cmd->result = DID_ERROR << 16;
2834                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2835                                 "9072 BLKGRD: Invalid BG Profile in cmd"
2836                                 " 0x%x lba 0x%llx blk cnt 0x%x "
2837                                 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2838                                 (unsigned long long)scsi_get_lba(cmd),
2839                                 blk_rq_sectors(cmd->request), bgstat, bghm);
2840                 ret = (-1);
2841                 goto out;
2842         }
2843
2844         if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2845                 cmd->result = DID_ERROR << 16;
2846                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2847                                 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
2848                                 " 0x%x lba 0x%llx blk cnt 0x%x "
2849                                 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2850                                 (unsigned long long)scsi_get_lba(cmd),
2851                                 blk_rq_sectors(cmd->request), bgstat, bghm);
2852                 ret = (-1);
2853                 goto out;
2854         }
2855
2856         if (lpfc_bgs_get_guard_err(bgstat)) {
2857                 ret = 1;
2858
2859                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2860                                 0x10, 0x1);
2861                 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2862                               SAM_STAT_CHECK_CONDITION;
2863                 phba->bg_guard_err_cnt++;
2864                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2865                                 "9055 BLKGRD: Guard Tag error in cmd"
2866                                 " 0x%x lba 0x%llx blk cnt 0x%x "
2867                                 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2868                                 (unsigned long long)scsi_get_lba(cmd),
2869                                 blk_rq_sectors(cmd->request), bgstat, bghm);
2870         }
2871
2872         if (lpfc_bgs_get_reftag_err(bgstat)) {
2873                 ret = 1;
2874
2875                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2876                                 0x10, 0x3);
2877                 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2878                               SAM_STAT_CHECK_CONDITION;
2879
2880                 phba->bg_reftag_err_cnt++;
2881                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2882                                 "9056 BLKGRD: Ref Tag error in cmd"
2883                                 " 0x%x lba 0x%llx blk cnt 0x%x "
2884                                 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2885                                 (unsigned long long)scsi_get_lba(cmd),
2886                                 blk_rq_sectors(cmd->request), bgstat, bghm);
2887         }
2888
2889         if (lpfc_bgs_get_apptag_err(bgstat)) {
2890                 ret = 1;