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