Merge tag 'sound-3.4' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound
[platform/kernel/linux-starfive.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-2012 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
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_eh.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi_transport_fc.h>
34
35 #include "lpfc_version.h"
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc.h"
43 #include "lpfc_scsi.h"
44 #include "lpfc_logmsg.h"
45 #include "lpfc_crtn.h"
46 #include "lpfc_vport.h"
47
48 #define LPFC_RESET_WAIT  2
49 #define LPFC_ABORT_WAIT  2
50
51 int _dump_buf_done;
52
53 static char *dif_op_str[] = {
54         "PROT_NORMAL",
55         "PROT_READ_INSERT",
56         "PROT_WRITE_STRIP",
57         "PROT_READ_STRIP",
58         "PROT_WRITE_INSERT",
59         "PROT_READ_PASS",
60         "PROT_WRITE_PASS",
61 };
62
63 static char *dif_grd_str[] = {
64         "NO_GUARD",
65         "DIF_CRC",
66         "DIX_IP",
67 };
68
69 struct scsi_dif_tuple {
70         __be16 guard_tag;       /* Checksum */
71         __be16 app_tag;         /* Opaque storage */
72         __be32 ref_tag;         /* Target LBA or indirect LBA */
73 };
74
75 static void
76 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
77 static void
78 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
79
80 static void
81 lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
82 {
83         void *src, *dst;
84         struct scatterlist *sgde = scsi_sglist(cmnd);
85
86         if (!_dump_buf_data) {
87                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
88                         "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
89                                 __func__);
90                 return;
91         }
92
93
94         if (!sgde) {
95                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
96                         "9051 BLKGRD: ERROR: data scatterlist is null\n");
97                 return;
98         }
99
100         dst = (void *) _dump_buf_data;
101         while (sgde) {
102                 src = sg_virt(sgde);
103                 memcpy(dst, src, sgde->length);
104                 dst += sgde->length;
105                 sgde = sg_next(sgde);
106         }
107 }
108
109 static void
110 lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
111 {
112         void *src, *dst;
113         struct scatterlist *sgde = scsi_prot_sglist(cmnd);
114
115         if (!_dump_buf_dif) {
116                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
117                         "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
118                                 __func__);
119                 return;
120         }
121
122         if (!sgde) {
123                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
124                         "9053 BLKGRD: ERROR: prot scatterlist is null\n");
125                 return;
126         }
127
128         dst = _dump_buf_dif;
129         while (sgde) {
130                 src = sg_virt(sgde);
131                 memcpy(dst, src, sgde->length);
132                 dst += sgde->length;
133                 sgde = sg_next(sgde);
134         }
135 }
136
137 /**
138  * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
139  * @phba: Pointer to HBA object.
140  * @lpfc_cmd: lpfc scsi command object pointer.
141  *
142  * This function is called from the lpfc_prep_task_mgmt_cmd function to
143  * set the last bit in the response sge entry.
144  **/
145 static void
146 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
147                                 struct lpfc_scsi_buf *lpfc_cmd)
148 {
149         struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
150         if (sgl) {
151                 sgl += 1;
152                 sgl->word2 = le32_to_cpu(sgl->word2);
153                 bf_set(lpfc_sli4_sge_last, sgl, 1);
154                 sgl->word2 = cpu_to_le32(sgl->word2);
155         }
156 }
157
158 /**
159  * lpfc_update_stats - Update statistical data for the command completion
160  * @phba: Pointer to HBA object.
161  * @lpfc_cmd: lpfc scsi command object pointer.
162  *
163  * This function is called when there is a command completion and this
164  * function updates the statistical data for the command completion.
165  **/
166 static void
167 lpfc_update_stats(struct lpfc_hba *phba, struct  lpfc_scsi_buf *lpfc_cmd)
168 {
169         struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
170         struct lpfc_nodelist *pnode = rdata->pnode;
171         struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
172         unsigned long flags;
173         struct Scsi_Host  *shost = cmd->device->host;
174         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
175         unsigned long latency;
176         int i;
177
178         if (cmd->result)
179                 return;
180
181         latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
182
183         spin_lock_irqsave(shost->host_lock, flags);
184         if (!vport->stat_data_enabled ||
185                 vport->stat_data_blocked ||
186                 !pnode ||
187                 !pnode->lat_data ||
188                 (phba->bucket_type == LPFC_NO_BUCKET)) {
189                 spin_unlock_irqrestore(shost->host_lock, flags);
190                 return;
191         }
192
193         if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
194                 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
195                         phba->bucket_step;
196                 /* check array subscript bounds */
197                 if (i < 0)
198                         i = 0;
199                 else if (i >= LPFC_MAX_BUCKET_COUNT)
200                         i = LPFC_MAX_BUCKET_COUNT - 1;
201         } else {
202                 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
203                         if (latency <= (phba->bucket_base +
204                                 ((1<<i)*phba->bucket_step)))
205                                 break;
206         }
207
208         pnode->lat_data[i].cmd_count++;
209         spin_unlock_irqrestore(shost->host_lock, flags);
210 }
211
212 /**
213  * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
214  * @phba: Pointer to HBA context object.
215  * @vport: Pointer to vport object.
216  * @ndlp: Pointer to FC node associated with the target.
217  * @lun: Lun number of the scsi device.
218  * @old_val: Old value of the queue depth.
219  * @new_val: New value of the queue depth.
220  *
221  * This function sends an event to the mgmt application indicating
222  * there is a change in the scsi device queue depth.
223  **/
224 static void
225 lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
226                 struct lpfc_vport  *vport,
227                 struct lpfc_nodelist *ndlp,
228                 uint32_t lun,
229                 uint32_t old_val,
230                 uint32_t new_val)
231 {
232         struct lpfc_fast_path_event *fast_path_evt;
233         unsigned long flags;
234
235         fast_path_evt = lpfc_alloc_fast_evt(phba);
236         if (!fast_path_evt)
237                 return;
238
239         fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
240                 FC_REG_SCSI_EVENT;
241         fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
242                 LPFC_EVENT_VARQUEDEPTH;
243
244         /* Report all luns with change in queue depth */
245         fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
246         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
247                 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
248                         &ndlp->nlp_portname, sizeof(struct lpfc_name));
249                 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
250                         &ndlp->nlp_nodename, sizeof(struct lpfc_name));
251         }
252
253         fast_path_evt->un.queue_depth_evt.oldval = old_val;
254         fast_path_evt->un.queue_depth_evt.newval = new_val;
255         fast_path_evt->vport = vport;
256
257         fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
258         spin_lock_irqsave(&phba->hbalock, flags);
259         list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
260         spin_unlock_irqrestore(&phba->hbalock, flags);
261         lpfc_worker_wake_up(phba);
262
263         return;
264 }
265
266 /**
267  * lpfc_change_queue_depth - Alter scsi device queue depth
268  * @sdev: Pointer the scsi device on which to change the queue depth.
269  * @qdepth: New queue depth to set the sdev to.
270  * @reason: The reason for the queue depth change.
271  *
272  * This function is called by the midlayer and the LLD to alter the queue
273  * depth for a scsi device. This function sets the queue depth to the new
274  * value and sends an event out to log the queue depth change.
275  **/
276 int
277 lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
278 {
279         struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
280         struct lpfc_hba   *phba = vport->phba;
281         struct lpfc_rport_data *rdata;
282         unsigned long new_queue_depth, old_queue_depth;
283
284         old_queue_depth = sdev->queue_depth;
285         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
286         new_queue_depth = sdev->queue_depth;
287         rdata = sdev->hostdata;
288         if (rdata)
289                 lpfc_send_sdev_queuedepth_change_event(phba, vport,
290                                                        rdata->pnode, sdev->lun,
291                                                        old_queue_depth,
292                                                        new_queue_depth);
293         return sdev->queue_depth;
294 }
295
296 /**
297  * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
298  * @phba: The Hba for which this call is being executed.
299  *
300  * This routine is called when there is resource error in driver or firmware.
301  * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
302  * posts at most 1 event each second. This routine wakes up worker thread of
303  * @phba to process WORKER_RAM_DOWN_EVENT event.
304  *
305  * This routine should be called with no lock held.
306  **/
307 void
308 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
309 {
310         unsigned long flags;
311         uint32_t evt_posted;
312
313         spin_lock_irqsave(&phba->hbalock, flags);
314         atomic_inc(&phba->num_rsrc_err);
315         phba->last_rsrc_error_time = jiffies;
316
317         if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
318                 spin_unlock_irqrestore(&phba->hbalock, flags);
319                 return;
320         }
321
322         phba->last_ramp_down_time = jiffies;
323
324         spin_unlock_irqrestore(&phba->hbalock, flags);
325
326         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
327         evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
328         if (!evt_posted)
329                 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
330         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
331
332         if (!evt_posted)
333                 lpfc_worker_wake_up(phba);
334         return;
335 }
336
337 /**
338  * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
339  * @phba: The Hba for which this call is being executed.
340  *
341  * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
342  * post at most 1 event every 5 minute after last_ramp_up_time or
343  * last_rsrc_error_time.  This routine wakes up worker thread of @phba
344  * to process WORKER_RAM_DOWN_EVENT event.
345  *
346  * This routine should be called with no lock held.
347  **/
348 static inline void
349 lpfc_rampup_queue_depth(struct lpfc_vport  *vport,
350                         uint32_t queue_depth)
351 {
352         unsigned long flags;
353         struct lpfc_hba *phba = vport->phba;
354         uint32_t evt_posted;
355         atomic_inc(&phba->num_cmd_success);
356
357         if (vport->cfg_lun_queue_depth <= queue_depth)
358                 return;
359         spin_lock_irqsave(&phba->hbalock, flags);
360         if (time_before(jiffies,
361                         phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
362             time_before(jiffies,
363                         phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
364                 spin_unlock_irqrestore(&phba->hbalock, flags);
365                 return;
366         }
367         phba->last_ramp_up_time = jiffies;
368         spin_unlock_irqrestore(&phba->hbalock, flags);
369
370         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
371         evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
372         if (!evt_posted)
373                 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
374         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
375
376         if (!evt_posted)
377                 lpfc_worker_wake_up(phba);
378         return;
379 }
380
381 /**
382  * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
383  * @phba: The Hba for which this call is being executed.
384  *
385  * This routine is called to  process WORKER_RAMP_DOWN_QUEUE event for worker
386  * thread.This routine reduces queue depth for all scsi device on each vport
387  * associated with @phba.
388  **/
389 void
390 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
391 {
392         struct lpfc_vport **vports;
393         struct Scsi_Host  *shost;
394         struct scsi_device *sdev;
395         unsigned long new_queue_depth;
396         unsigned long num_rsrc_err, num_cmd_success;
397         int i;
398
399         num_rsrc_err = atomic_read(&phba->num_rsrc_err);
400         num_cmd_success = atomic_read(&phba->num_cmd_success);
401
402         vports = lpfc_create_vport_work_array(phba);
403         if (vports != NULL)
404                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
405                         shost = lpfc_shost_from_vport(vports[i]);
406                         shost_for_each_device(sdev, shost) {
407                                 new_queue_depth =
408                                         sdev->queue_depth * num_rsrc_err /
409                                         (num_rsrc_err + num_cmd_success);
410                                 if (!new_queue_depth)
411                                         new_queue_depth = sdev->queue_depth - 1;
412                                 else
413                                         new_queue_depth = sdev->queue_depth -
414                                                                 new_queue_depth;
415                                 lpfc_change_queue_depth(sdev, new_queue_depth,
416                                                         SCSI_QDEPTH_DEFAULT);
417                         }
418                 }
419         lpfc_destroy_vport_work_array(phba, vports);
420         atomic_set(&phba->num_rsrc_err, 0);
421         atomic_set(&phba->num_cmd_success, 0);
422 }
423
424 /**
425  * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
426  * @phba: The Hba for which this call is being executed.
427  *
428  * This routine is called to  process WORKER_RAMP_UP_QUEUE event for worker
429  * thread.This routine increases queue depth for all scsi device on each vport
430  * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
431  * num_cmd_success to zero.
432  **/
433 void
434 lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
435 {
436         struct lpfc_vport **vports;
437         struct Scsi_Host  *shost;
438         struct scsi_device *sdev;
439         int i;
440
441         vports = lpfc_create_vport_work_array(phba);
442         if (vports != NULL)
443                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
444                         shost = lpfc_shost_from_vport(vports[i]);
445                         shost_for_each_device(sdev, shost) {
446                                 if (vports[i]->cfg_lun_queue_depth <=
447                                     sdev->queue_depth)
448                                         continue;
449                                 lpfc_change_queue_depth(sdev,
450                                                         sdev->queue_depth+1,
451                                                         SCSI_QDEPTH_RAMP_UP);
452                         }
453                 }
454         lpfc_destroy_vport_work_array(phba, vports);
455         atomic_set(&phba->num_rsrc_err, 0);
456         atomic_set(&phba->num_cmd_success, 0);
457 }
458
459 /**
460  * lpfc_scsi_dev_block - set all scsi hosts to block state
461  * @phba: Pointer to HBA context object.
462  *
463  * This function walks vport list and set each SCSI host to block state
464  * by invoking fc_remote_port_delete() routine. This function is invoked
465  * with EEH when device's PCI slot has been permanently disabled.
466  **/
467 void
468 lpfc_scsi_dev_block(struct lpfc_hba *phba)
469 {
470         struct lpfc_vport **vports;
471         struct Scsi_Host  *shost;
472         struct scsi_device *sdev;
473         struct fc_rport *rport;
474         int i;
475
476         vports = lpfc_create_vport_work_array(phba);
477         if (vports != NULL)
478                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
479                         shost = lpfc_shost_from_vport(vports[i]);
480                         shost_for_each_device(sdev, shost) {
481                                 rport = starget_to_rport(scsi_target(sdev));
482                                 fc_remote_port_delete(rport);
483                         }
484                 }
485         lpfc_destroy_vport_work_array(phba, vports);
486 }
487
488 /**
489  * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
490  * @vport: The virtual port for which this call being executed.
491  * @num_to_allocate: The requested number of buffers to allocate.
492  *
493  * This routine allocates a scsi buffer for device with SLI-3 interface spec,
494  * the scsi buffer contains all the necessary information needed to initiate
495  * a SCSI I/O. The non-DMAable buffer region contains information to build
496  * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
497  * and the initial BPL. In addition to allocating memory, the FCP CMND and
498  * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
499  *
500  * Return codes:
501  *   int - number of scsi buffers that were allocated.
502  *   0 = failure, less than num_to_alloc is a partial failure.
503  **/
504 static int
505 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
506 {
507         struct lpfc_hba *phba = vport->phba;
508         struct lpfc_scsi_buf *psb;
509         struct ulp_bde64 *bpl;
510         IOCB_t *iocb;
511         dma_addr_t pdma_phys_fcp_cmd;
512         dma_addr_t pdma_phys_fcp_rsp;
513         dma_addr_t pdma_phys_bpl;
514         uint16_t iotag;
515         int bcnt;
516
517         for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
518                 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
519                 if (!psb)
520                         break;
521
522                 /*
523                  * Get memory from the pci pool to map the virt space to pci
524                  * bus space for an I/O.  The DMA buffer includes space for the
525                  * struct fcp_cmnd, struct fcp_rsp and the number of bde's
526                  * necessary to support the sg_tablesize.
527                  */
528                 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
529                                         GFP_KERNEL, &psb->dma_handle);
530                 if (!psb->data) {
531                         kfree(psb);
532                         break;
533                 }
534
535                 /* Initialize virtual ptrs to dma_buf region. */
536                 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
537
538                 /* Allocate iotag for psb->cur_iocbq. */
539                 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
540                 if (iotag == 0) {
541                         pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
542                                         psb->data, psb->dma_handle);
543                         kfree(psb);
544                         break;
545                 }
546                 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
547
548                 psb->fcp_cmnd = psb->data;
549                 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
550                 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
551                         sizeof(struct fcp_rsp);
552
553                 /* Initialize local short-hand pointers. */
554                 bpl = psb->fcp_bpl;
555                 pdma_phys_fcp_cmd = psb->dma_handle;
556                 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
557                 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
558                         sizeof(struct fcp_rsp);
559
560                 /*
561                  * The first two bdes are the FCP_CMD and FCP_RSP. The balance
562                  * are sg list bdes.  Initialize the first two and leave the
563                  * rest for queuecommand.
564                  */
565                 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
566                 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
567                 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
568                 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
569                 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
570
571                 /* Setup the physical region for the FCP RSP */
572                 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
573                 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
574                 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
575                 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
576                 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
577
578                 /*
579                  * Since the IOCB for the FCP I/O is built into this
580                  * lpfc_scsi_buf, initialize it with all known data now.
581                  */
582                 iocb = &psb->cur_iocbq.iocb;
583                 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
584                 if ((phba->sli_rev == 3) &&
585                                 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
586                         /* fill in immediate fcp command BDE */
587                         iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
588                         iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
589                         iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
590                                         unsli3.fcp_ext.icd);
591                         iocb->un.fcpi64.bdl.addrHigh = 0;
592                         iocb->ulpBdeCount = 0;
593                         iocb->ulpLe = 0;
594                         /* fill in response BDE */
595                         iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
596                                                         BUFF_TYPE_BDE_64;
597                         iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
598                                 sizeof(struct fcp_rsp);
599                         iocb->unsli3.fcp_ext.rbde.addrLow =
600                                 putPaddrLow(pdma_phys_fcp_rsp);
601                         iocb->unsli3.fcp_ext.rbde.addrHigh =
602                                 putPaddrHigh(pdma_phys_fcp_rsp);
603                 } else {
604                         iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
605                         iocb->un.fcpi64.bdl.bdeSize =
606                                         (2 * sizeof(struct ulp_bde64));
607                         iocb->un.fcpi64.bdl.addrLow =
608                                         putPaddrLow(pdma_phys_bpl);
609                         iocb->un.fcpi64.bdl.addrHigh =
610                                         putPaddrHigh(pdma_phys_bpl);
611                         iocb->ulpBdeCount = 1;
612                         iocb->ulpLe = 1;
613                 }
614                 iocb->ulpClass = CLASS3;
615                 psb->status = IOSTAT_SUCCESS;
616                 /* Put it back into the SCSI buffer list */
617                 psb->cur_iocbq.context1  = psb;
618                 lpfc_release_scsi_buf_s3(phba, psb);
619
620         }
621
622         return bcnt;
623 }
624
625 /**
626  * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
627  * @vport: pointer to lpfc vport data structure.
628  *
629  * This routine is invoked by the vport cleanup for deletions and the cleanup
630  * for an ndlp on removal.
631  **/
632 void
633 lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
634 {
635         struct lpfc_hba *phba = vport->phba;
636         struct lpfc_scsi_buf *psb, *next_psb;
637         unsigned long iflag = 0;
638
639         spin_lock_irqsave(&phba->hbalock, iflag);
640         spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
641         list_for_each_entry_safe(psb, next_psb,
642                                 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
643                 if (psb->rdata && psb->rdata->pnode
644                         && psb->rdata->pnode->vport == vport)
645                         psb->rdata = NULL;
646         }
647         spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
648         spin_unlock_irqrestore(&phba->hbalock, iflag);
649 }
650
651 /**
652  * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
653  * @phba: pointer to lpfc hba data structure.
654  * @axri: pointer to the fcp xri abort wcqe structure.
655  *
656  * This routine is invoked by the worker thread to process a SLI4 fast-path
657  * FCP aborted xri.
658  **/
659 void
660 lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
661                           struct sli4_wcqe_xri_aborted *axri)
662 {
663         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
664         uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
665         struct lpfc_scsi_buf *psb, *next_psb;
666         unsigned long iflag = 0;
667         struct lpfc_iocbq *iocbq;
668         int i;
669         struct lpfc_nodelist *ndlp;
670         int rrq_empty = 0;
671         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
672
673         spin_lock_irqsave(&phba->hbalock, iflag);
674         spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
675         list_for_each_entry_safe(psb, next_psb,
676                 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
677                 if (psb->cur_iocbq.sli4_xritag == xri) {
678                         list_del(&psb->list);
679                         psb->exch_busy = 0;
680                         psb->status = IOSTAT_SUCCESS;
681                         spin_unlock(
682                                 &phba->sli4_hba.abts_scsi_buf_list_lock);
683                         if (psb->rdata && psb->rdata->pnode)
684                                 ndlp = psb->rdata->pnode;
685                         else
686                                 ndlp = NULL;
687
688                         rrq_empty = list_empty(&phba->active_rrq_list);
689                         spin_unlock_irqrestore(&phba->hbalock, iflag);
690                         if (ndlp) {
691                                 lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
692                                 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
693                         }
694                         lpfc_release_scsi_buf_s4(phba, psb);
695                         if (rrq_empty)
696                                 lpfc_worker_wake_up(phba);
697                         return;
698                 }
699         }
700         spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
701         for (i = 1; i <= phba->sli.last_iotag; i++) {
702                 iocbq = phba->sli.iocbq_lookup[i];
703
704                 if (!(iocbq->iocb_flag &  LPFC_IO_FCP) ||
705                         (iocbq->iocb_flag & LPFC_IO_LIBDFC))
706                         continue;
707                 if (iocbq->sli4_xritag != xri)
708                         continue;
709                 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
710                 psb->exch_busy = 0;
711                 spin_unlock_irqrestore(&phba->hbalock, iflag);
712                 if (pring->txq_cnt)
713                         lpfc_worker_wake_up(phba);
714                 return;
715
716         }
717         spin_unlock_irqrestore(&phba->hbalock, iflag);
718 }
719
720 /**
721  * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
722  * @phba: pointer to lpfc hba data structure.
723  *
724  * This routine walks the list of scsi buffers that have been allocated and
725  * repost them to the HBA by using SGL block post. This is needed after a
726  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
727  * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
728  * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
729  *
730  * Returns: 0 = success, non-zero failure.
731  **/
732 int
733 lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
734 {
735         struct lpfc_scsi_buf *psb;
736         int index, status, bcnt = 0, rcnt = 0, rc = 0;
737         LIST_HEAD(sblist);
738
739         for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
740                 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
741                 if (psb) {
742                         /* Remove from SCSI buffer list */
743                         list_del(&psb->list);
744                         /* Add it to a local SCSI buffer list */
745                         list_add_tail(&psb->list, &sblist);
746                         if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
747                                 bcnt = rcnt;
748                                 rcnt = 0;
749                         }
750                 } else
751                         /* A hole present in the XRI array, need to skip */
752                         bcnt = rcnt;
753
754                 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
755                         /* End of XRI array for SCSI buffer, complete */
756                         bcnt = rcnt;
757
758                 /* Continue until collect up to a nembed page worth of sgls */
759                 if (bcnt == 0)
760                         continue;
761                 /* Now, post the SCSI buffer list sgls as a block */
762                 if (!phba->sli4_hba.extents_in_use)
763                         status = lpfc_sli4_post_scsi_sgl_block(phba,
764                                                         &sblist,
765                                                         bcnt);
766                 else
767                         status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
768                                                         &sblist,
769                                                         bcnt);
770                 /* Reset SCSI buffer count for next round of posting */
771                 bcnt = 0;
772                 while (!list_empty(&sblist)) {
773                         list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
774                                          list);
775                         if (status) {
776                                 /* Put this back on the abort scsi list */
777                                 psb->exch_busy = 1;
778                                 rc++;
779                         } else {
780                                 psb->exch_busy = 0;
781                                 psb->status = IOSTAT_SUCCESS;
782                         }
783                         /* Put it back into the SCSI buffer list */
784                         lpfc_release_scsi_buf_s4(phba, psb);
785                 }
786         }
787         return rc;
788 }
789
790 /**
791  * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
792  * @vport: The virtual port for which this call being executed.
793  * @num_to_allocate: The requested number of buffers to allocate.
794  *
795  * This routine allocates a scsi buffer for device with SLI-4 interface spec,
796  * the scsi buffer contains all the necessary information needed to initiate
797  * a SCSI I/O.
798  *
799  * Return codes:
800  *   int - number of scsi buffers that were allocated.
801  *   0 = failure, less than num_to_alloc is a partial failure.
802  **/
803 static int
804 lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
805 {
806         struct lpfc_hba *phba = vport->phba;
807         struct lpfc_scsi_buf *psb;
808         struct sli4_sge *sgl;
809         IOCB_t *iocb;
810         dma_addr_t pdma_phys_fcp_cmd;
811         dma_addr_t pdma_phys_fcp_rsp;
812         dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
813         uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
814         int status = 0, index;
815         int bcnt;
816         int non_sequential_xri = 0;
817         LIST_HEAD(sblist);
818
819         for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
820                 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
821                 if (!psb)
822                         break;
823
824                 /*
825                  * Get memory from the pci pool to map the virt space to pci bus
826                  * space for an I/O.  The DMA buffer includes space for the
827                  * struct fcp_cmnd, struct fcp_rsp and the number of bde's
828                  * necessary to support the sg_tablesize.
829                  */
830                 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
831                                                 GFP_KERNEL, &psb->dma_handle);
832                 if (!psb->data) {
833                         kfree(psb);
834                         break;
835                 }
836
837                 /* Initialize virtual ptrs to dma_buf region. */
838                 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
839
840                 /* Allocate iotag for psb->cur_iocbq. */
841                 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
842                 if (iotag == 0) {
843                         pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
844                                 psb->data, psb->dma_handle);
845                         kfree(psb);
846                         break;
847                 }
848
849                 lxri = lpfc_sli4_next_xritag(phba);
850                 if (lxri == NO_XRI) {
851                         pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
852                               psb->data, psb->dma_handle);
853                         kfree(psb);
854                         break;
855                 }
856                 psb->cur_iocbq.sli4_lxritag = lxri;
857                 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
858                 if (last_xritag != NO_XRI
859                         && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
860                         non_sequential_xri = 1;
861                 } else
862                         list_add_tail(&psb->list, &sblist);
863                 last_xritag = psb->cur_iocbq.sli4_xritag;
864
865                 index = phba->sli4_hba.scsi_xri_cnt++;
866                 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
867
868                 psb->fcp_bpl = psb->data;
869                 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
870                         - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
871                 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
872                                         sizeof(struct fcp_cmnd));
873
874                 /* Initialize local short-hand pointers. */
875                 sgl = (struct sli4_sge *)psb->fcp_bpl;
876                 pdma_phys_bpl = psb->dma_handle;
877                 pdma_phys_fcp_cmd =
878                         (psb->dma_handle + phba->cfg_sg_dma_buf_size)
879                          - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
880                 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
881
882                 /*
883                  * The first two bdes are the FCP_CMD and FCP_RSP.  The balance
884                  * are sg list bdes.  Initialize the first two and leave the
885                  * rest for queuecommand.
886                  */
887                 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
888                 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
889                 sgl->word2 = le32_to_cpu(sgl->word2);
890                 bf_set(lpfc_sli4_sge_last, sgl, 0);
891                 sgl->word2 = cpu_to_le32(sgl->word2);
892                 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
893                 sgl++;
894
895                 /* Setup the physical region for the FCP RSP */
896                 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
897                 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
898                 sgl->word2 = le32_to_cpu(sgl->word2);
899                 bf_set(lpfc_sli4_sge_last, sgl, 1);
900                 sgl->word2 = cpu_to_le32(sgl->word2);
901                 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
902
903                 /*
904                  * Since the IOCB for the FCP I/O is built into this
905                  * lpfc_scsi_buf, initialize it with all known data now.
906                  */
907                 iocb = &psb->cur_iocbq.iocb;
908                 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
909                 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
910                 /* setting the BLP size to 2 * sizeof BDE may not be correct.
911                  * We are setting the bpl to point to out sgl. An sgl's
912                  * entries are 16 bytes, a bpl entries are 12 bytes.
913                  */
914                 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
915                 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
916                 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
917                 iocb->ulpBdeCount = 1;
918                 iocb->ulpLe = 1;
919                 iocb->ulpClass = CLASS3;
920                 psb->cur_iocbq.context1  = psb;
921                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
922                         pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
923                 else
924                         pdma_phys_bpl1 = 0;
925                 psb->dma_phys_bpl = pdma_phys_bpl;
926                 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
927                 if (non_sequential_xri) {
928                         status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
929                                                 pdma_phys_bpl1,
930                                                 psb->cur_iocbq.sli4_xritag);
931                         if (status) {
932                                 /* Put this back on the abort scsi list */
933                                 psb->exch_busy = 1;
934                         } else {
935                                 psb->exch_busy = 0;
936                                 psb->status = IOSTAT_SUCCESS;
937                         }
938                         /* Put it back into the SCSI buffer list */
939                         lpfc_release_scsi_buf_s4(phba, psb);
940                         break;
941                 }
942         }
943         if (bcnt) {
944                 if (!phba->sli4_hba.extents_in_use)
945                         status = lpfc_sli4_post_scsi_sgl_block(phba,
946                                                                 &sblist,
947                                                                 bcnt);
948                 else
949                         status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
950                                                                 &sblist,
951                                                                 bcnt);
952
953                 if (status) {
954                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
955                                         "3021 SCSI SGL post error %d\n",
956                                         status);
957                         bcnt = 0;
958                 }
959                 /* Reset SCSI buffer count for next round of posting */
960                 while (!list_empty(&sblist)) {
961                         list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
962                                  list);
963                         if (status) {
964                                 /* Put this back on the abort scsi list */
965                                 psb->exch_busy = 1;
966                         } else {
967                                 psb->exch_busy = 0;
968                                 psb->status = IOSTAT_SUCCESS;
969                         }
970                         /* Put it back into the SCSI buffer list */
971                         lpfc_release_scsi_buf_s4(phba, psb);
972                 }
973         }
974
975         return bcnt + non_sequential_xri;
976 }
977
978 /**
979  * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
980  * @vport: The virtual port for which this call being executed.
981  * @num_to_allocate: The requested number of buffers to allocate.
982  *
983  * This routine wraps the actual SCSI buffer allocator function pointer from
984  * the lpfc_hba struct.
985  *
986  * Return codes:
987  *   int - number of scsi buffers that were allocated.
988  *   0 = failure, less than num_to_alloc is a partial failure.
989  **/
990 static inline int
991 lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
992 {
993         return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
994 }
995
996 /**
997  * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
998  * @phba: The HBA for which this call is being executed.
999  *
1000  * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1001  * and returns to caller.
1002  *
1003  * Return codes:
1004  *   NULL - Error
1005  *   Pointer to lpfc_scsi_buf - Success
1006  **/
1007 static struct lpfc_scsi_buf*
1008 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1009 {
1010         struct  lpfc_scsi_buf * lpfc_cmd = NULL;
1011         struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
1012         unsigned long iflag = 0;
1013
1014         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1015         list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1016         if (lpfc_cmd) {
1017                 lpfc_cmd->seg_cnt = 0;
1018                 lpfc_cmd->nonsg_phys = 0;
1019                 lpfc_cmd->prot_seg_cnt = 0;
1020         }
1021         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1022         return  lpfc_cmd;
1023 }
1024 /**
1025  * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1026  * @phba: The HBA for which this call is being executed.
1027  *
1028  * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1029  * and returns to caller.
1030  *
1031  * Return codes:
1032  *   NULL - Error
1033  *   Pointer to lpfc_scsi_buf - Success
1034  **/
1035 static struct lpfc_scsi_buf*
1036 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1037 {
1038         struct lpfc_scsi_buf *lpfc_cmd ;
1039         unsigned long iflag = 0;
1040         int found = 0;
1041
1042         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1043         list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1044                                                         list) {
1045                 if (lpfc_test_rrq_active(phba, ndlp,
1046                                          lpfc_cmd->cur_iocbq.sli4_xritag))
1047                         continue;
1048                 list_del(&lpfc_cmd->list);
1049                 found = 1;
1050                 lpfc_cmd->seg_cnt = 0;
1051                 lpfc_cmd->nonsg_phys = 0;
1052                 lpfc_cmd->prot_seg_cnt = 0;
1053                 break;
1054         }
1055         spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1056                                                  iflag);
1057         if (!found)
1058                 return NULL;
1059         else
1060                 return  lpfc_cmd;
1061 }
1062 /**
1063  * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1064  * @phba: The HBA for which this call is being executed.
1065  *
1066  * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1067  * and returns to caller.
1068  *
1069  * Return codes:
1070  *   NULL - Error
1071  *   Pointer to lpfc_scsi_buf - Success
1072  **/
1073 static struct lpfc_scsi_buf*
1074 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1075 {
1076         return  phba->lpfc_get_scsi_buf(phba, ndlp);
1077 }
1078
1079 /**
1080  * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
1081  * @phba: The Hba for which this call is being executed.
1082  * @psb: The scsi buffer which is being released.
1083  *
1084  * This routine releases @psb scsi buffer by adding it to tail of @phba
1085  * lpfc_scsi_buf_list list.
1086  **/
1087 static void
1088 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1089 {
1090         unsigned long iflag = 0;
1091
1092         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1093         psb->pCmd = NULL;
1094         list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1095         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1096 }
1097
1098 /**
1099  * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1100  * @phba: The Hba for which this call is being executed.
1101  * @psb: The scsi buffer which is being released.
1102  *
1103  * This routine releases @psb scsi buffer by adding it to tail of @phba
1104  * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1105  * and cannot be reused for at least RA_TOV amount of time if it was
1106  * aborted.
1107  **/
1108 static void
1109 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1110 {
1111         unsigned long iflag = 0;
1112
1113         if (psb->exch_busy) {
1114                 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1115                                         iflag);
1116                 psb->pCmd = NULL;
1117                 list_add_tail(&psb->list,
1118                         &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1119                 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1120                                         iflag);
1121         } else {
1122
1123                 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1124                 psb->pCmd = NULL;
1125                 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1126                 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1127         }
1128 }
1129
1130 /**
1131  * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1132  * @phba: The Hba for which this call is being executed.
1133  * @psb: The scsi buffer which is being released.
1134  *
1135  * This routine releases @psb scsi buffer by adding it to tail of @phba
1136  * lpfc_scsi_buf_list list.
1137  **/
1138 static void
1139 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1140 {
1141
1142         phba->lpfc_release_scsi_buf(phba, psb);
1143 }
1144
1145 /**
1146  * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
1147  * @phba: The Hba for which this call is being executed.
1148  * @lpfc_cmd: The scsi buffer which is going to be mapped.
1149  *
1150  * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1151  * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1152  * through sg elements and format the bdea. This routine also initializes all
1153  * IOCB fields which are dependent on scsi command request buffer.
1154  *
1155  * Return codes:
1156  *   1 - Error
1157  *   0 - Success
1158  **/
1159 static int
1160 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1161 {
1162         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1163         struct scatterlist *sgel = NULL;
1164         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1165         struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1166         struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
1167         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1168         struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
1169         dma_addr_t physaddr;
1170         uint32_t num_bde = 0;
1171         int nseg, datadir = scsi_cmnd->sc_data_direction;
1172
1173         /*
1174          * There are three possibilities here - use scatter-gather segment, use
1175          * the single mapping, or neither.  Start the lpfc command prep by
1176          * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1177          * data bde entry.
1178          */
1179         bpl += 2;
1180         if (scsi_sg_count(scsi_cmnd)) {
1181                 /*
1182                  * The driver stores the segment count returned from pci_map_sg
1183                  * because this a count of dma-mappings used to map the use_sg
1184                  * pages.  They are not guaranteed to be the same for those
1185                  * architectures that implement an IOMMU.
1186                  */
1187
1188                 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1189                                   scsi_sg_count(scsi_cmnd), datadir);
1190                 if (unlikely(!nseg))
1191                         return 1;
1192
1193                 lpfc_cmd->seg_cnt = nseg;
1194                 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1195                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1196                                 "9064 BLKGRD: %s: Too many sg segments from "
1197                                "dma_map_sg.  Config %d, seg_cnt %d\n",
1198                                __func__, phba->cfg_sg_seg_cnt,
1199                                lpfc_cmd->seg_cnt);
1200                         scsi_dma_unmap(scsi_cmnd);
1201                         return 1;
1202                 }
1203
1204                 /*
1205                  * The driver established a maximum scatter-gather segment count
1206                  * during probe that limits the number of sg elements in any
1207                  * single scsi command.  Just run through the seg_cnt and format
1208                  * the bde's.
1209                  * When using SLI-3 the driver will try to fit all the BDEs into
1210                  * the IOCB. If it can't then the BDEs get added to a BPL as it
1211                  * does for SLI-2 mode.
1212                  */
1213                 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1214                         physaddr = sg_dma_address(sgel);
1215                         if (phba->sli_rev == 3 &&
1216                             !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1217                             !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
1218                             nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1219                                 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1220                                 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1221                                 data_bde->addrLow = putPaddrLow(physaddr);
1222                                 data_bde->addrHigh = putPaddrHigh(physaddr);
1223                                 data_bde++;
1224                         } else {
1225                                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1226                                 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1227                                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1228                                 bpl->addrLow =
1229                                         le32_to_cpu(putPaddrLow(physaddr));
1230                                 bpl->addrHigh =
1231                                         le32_to_cpu(putPaddrHigh(physaddr));
1232                                 bpl++;
1233                         }
1234                 }
1235         }
1236
1237         /*
1238          * Finish initializing those IOCB fields that are dependent on the
1239          * scsi_cmnd request_buffer.  Note that for SLI-2 the bdeSize is
1240          * explicitly reinitialized and for SLI-3 the extended bde count is
1241          * explicitly reinitialized since all iocb memory resources are reused.
1242          */
1243         if (phba->sli_rev == 3 &&
1244             !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1245             !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
1246                 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1247                         /*
1248                          * The extended IOCB format can only fit 3 BDE or a BPL.
1249                          * This I/O has more than 3 BDE so the 1st data bde will
1250                          * be a BPL that is filled in here.
1251                          */
1252                         physaddr = lpfc_cmd->dma_handle;
1253                         data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1254                         data_bde->tus.f.bdeSize = (num_bde *
1255                                                    sizeof(struct ulp_bde64));
1256                         physaddr += (sizeof(struct fcp_cmnd) +
1257                                      sizeof(struct fcp_rsp) +
1258                                      (2 * sizeof(struct ulp_bde64)));
1259                         data_bde->addrHigh = putPaddrHigh(physaddr);
1260                         data_bde->addrLow = putPaddrLow(physaddr);
1261                         /* ebde count includes the response bde and data bpl */
1262                         iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1263                 } else {
1264                         /* ebde count includes the response bde and data bdes */
1265                         iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1266                 }
1267         } else {
1268                 iocb_cmd->un.fcpi64.bdl.bdeSize =
1269                         ((num_bde + 2) * sizeof(struct ulp_bde64));
1270                 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1271         }
1272         fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1273
1274         /*
1275          * Due to difference in data length between DIF/non-DIF paths,
1276          * we need to set word 4 of IOCB here
1277          */
1278         iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1279         return 0;
1280 }
1281
1282 static inline unsigned
1283 lpfc_cmd_blksize(struct scsi_cmnd *sc)
1284 {
1285         return sc->device->sector_size;
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 = sc->device->hostdata;
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 guard_type = scsi_host_get_guard(sc->device->host);
1721         uint8_t ret = 0;
1722
1723         if (guard_type == SHOST_DIX_GUARD_IP) {
1724                 switch (scsi_get_prot_op(sc)) {
1725                 case SCSI_PROT_READ_INSERT:
1726                 case SCSI_PROT_WRITE_STRIP:
1727                         *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1728                         *txop = BG_OP_IN_CSUM_OUT_NODIF;
1729                         break;
1730
1731                 case SCSI_PROT_READ_STRIP:
1732                 case SCSI_PROT_WRITE_INSERT:
1733                         *rxop = BG_OP_IN_CRC_OUT_NODIF;
1734                         *txop = BG_OP_IN_NODIF_OUT_CRC;
1735                         break;
1736
1737                 case SCSI_PROT_READ_PASS:
1738                 case SCSI_PROT_WRITE_PASS:
1739                         *rxop = BG_OP_IN_CRC_OUT_CSUM;
1740                         *txop = BG_OP_IN_CSUM_OUT_CRC;
1741                         break;
1742
1743                 case SCSI_PROT_NORMAL:
1744                 default:
1745                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1746                                 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1747                                         scsi_get_prot_op(sc));
1748                         ret = 1;
1749                         break;
1750
1751                 }
1752         } else {
1753                 switch (scsi_get_prot_op(sc)) {
1754                 case SCSI_PROT_READ_STRIP:
1755                 case SCSI_PROT_WRITE_INSERT:
1756                         *rxop = BG_OP_IN_CRC_OUT_NODIF;
1757                         *txop = BG_OP_IN_NODIF_OUT_CRC;
1758                         break;
1759
1760                 case SCSI_PROT_READ_PASS:
1761                 case SCSI_PROT_WRITE_PASS:
1762                         *rxop = BG_OP_IN_CRC_OUT_CRC;
1763                         *txop = BG_OP_IN_CRC_OUT_CRC;
1764                         break;
1765
1766                 case SCSI_PROT_READ_INSERT:
1767                 case SCSI_PROT_WRITE_STRIP:
1768                         *rxop = BG_OP_IN_NODIF_OUT_CRC;
1769                         *txop = BG_OP_IN_CRC_OUT_NODIF;
1770                         break;
1771
1772                 case SCSI_PROT_NORMAL:
1773                 default:
1774                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1775                                 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1776                                         scsi_get_prot_op(sc));
1777                         ret = 1;
1778                         break;
1779                 }
1780         }
1781
1782         return ret;
1783 }
1784
1785 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1786 /**
1787  * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1788  * the specified SCSI command in order to force a guard tag error.
1789  * @phba: The Hba for which this call is being executed.
1790  * @sc: The SCSI command to examine
1791  * @txopt: (out) BlockGuard operation for transmitted data
1792  * @rxopt: (out) BlockGuard operation for received data
1793  *
1794  * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1795  *
1796  **/
1797 static int
1798 lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1799                 uint8_t *txop, uint8_t *rxop)
1800 {
1801         uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1802         uint8_t ret = 0;
1803
1804         if (guard_type == SHOST_DIX_GUARD_IP) {
1805                 switch (scsi_get_prot_op(sc)) {
1806                 case SCSI_PROT_READ_INSERT:
1807                 case SCSI_PROT_WRITE_STRIP:
1808                         *rxop = BG_OP_IN_NODIF_OUT_CRC;
1809                         *txop = BG_OP_IN_CRC_OUT_NODIF;
1810                         break;
1811
1812                 case SCSI_PROT_READ_STRIP:
1813                 case SCSI_PROT_WRITE_INSERT:
1814                         *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1815                         *txop = BG_OP_IN_NODIF_OUT_CSUM;
1816                         break;
1817
1818                 case SCSI_PROT_READ_PASS:
1819                 case SCSI_PROT_WRITE_PASS:
1820                         *rxop = BG_OP_IN_CSUM_OUT_CRC;
1821                         *txop = BG_OP_IN_CRC_OUT_CSUM;
1822                         break;
1823
1824                 case SCSI_PROT_NORMAL:
1825                 default:
1826                         break;
1827
1828                 }
1829         } else {
1830                 switch (scsi_get_prot_op(sc)) {
1831                 case SCSI_PROT_READ_STRIP:
1832                 case SCSI_PROT_WRITE_INSERT:
1833                         *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1834                         *txop = BG_OP_IN_NODIF_OUT_CSUM;
1835                         break;
1836
1837                 case SCSI_PROT_READ_PASS:
1838                 case SCSI_PROT_WRITE_PASS:
1839                         *rxop = BG_OP_IN_CSUM_OUT_CSUM;
1840                         *txop = BG_OP_IN_CSUM_OUT_CSUM;
1841                         break;
1842
1843                 case SCSI_PROT_READ_INSERT:
1844                 case SCSI_PROT_WRITE_STRIP:
1845                         *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1846                         *txop = BG_OP_IN_CSUM_OUT_NODIF;
1847                         break;
1848
1849                 case SCSI_PROT_NORMAL:
1850                 default:
1851                         break;
1852                 }
1853         }
1854
1855         return ret;
1856 }
1857 #endif
1858
1859 /**
1860  * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1861  * @phba: The Hba for which this call is being executed.
1862  * @sc: pointer to scsi command we're working on
1863  * @bpl: pointer to buffer list for protection groups
1864  * @datacnt: number of segments of data that have been dma mapped
1865  *
1866  * This function sets up BPL buffer list for protection groups of
1867  * type LPFC_PG_TYPE_NO_DIF
1868  *
1869  * This is usually used when the HBA is instructed to generate
1870  * DIFs and insert them into data stream (or strip DIF from
1871  * incoming data stream)
1872  *
1873  * The buffer list consists of just one protection group described
1874  * below:
1875  *                                +-------------------------+
1876  *   start of prot group  -->     |          PDE_5          |
1877  *                                +-------------------------+
1878  *                                |          PDE_6          |
1879  *                                +-------------------------+
1880  *                                |         Data BDE        |
1881  *                                +-------------------------+
1882  *                                |more Data BDE's ... (opt)|
1883  *                                +-------------------------+
1884  *
1885  *
1886  * Note: Data s/g buffers have been dma mapped
1887  *
1888  * Returns the number of BDEs added to the BPL.
1889  **/
1890 static int
1891 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1892                 struct ulp_bde64 *bpl, int datasegcnt)
1893 {
1894         struct scatterlist *sgde = NULL; /* s/g data entry */
1895         struct lpfc_pde5 *pde5 = NULL;
1896         struct lpfc_pde6 *pde6 = NULL;
1897         dma_addr_t physaddr;
1898         int i = 0, num_bde = 0, status;
1899         int datadir = sc->sc_data_direction;
1900         uint32_t rc;
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         if (datadir == DMA_FROM_DEVICE) {
1944                 bf_set(pde6_ce, pde6, checking);
1945                 bf_set(pde6_re, pde6, checking);
1946         }
1947         bf_set(pde6_ai, pde6, 1);
1948         bf_set(pde6_ae, pde6, 0);
1949         bf_set(pde6_apptagval, pde6, 0);
1950
1951         /* Endianness conversion if necessary for PDE6 */
1952         pde6->word0 = cpu_to_le32(pde6->word0);
1953         pde6->word1 = cpu_to_le32(pde6->word1);
1954         pde6->word2 = cpu_to_le32(pde6->word2);
1955
1956         /* advance bpl and increment bde count */
1957         num_bde++;
1958         bpl++;
1959
1960         /* assumption: caller has already run dma_map_sg on command data */
1961         scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1962                 physaddr = sg_dma_address(sgde);
1963                 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1964                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1965                 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1966                 if (datadir == DMA_TO_DEVICE)
1967                         bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1968                 else
1969                         bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1970                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1971                 bpl++;
1972                 num_bde++;
1973         }
1974
1975 out:
1976         return num_bde;
1977 }
1978
1979 /**
1980  * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1981  * @phba: The Hba for which this call is being executed.
1982  * @sc: pointer to scsi command we're working on
1983  * @bpl: pointer to buffer list for protection groups
1984  * @datacnt: number of segments of data that have been dma mapped
1985  * @protcnt: number of segment of protection data that have been dma mapped
1986  *
1987  * This function sets up BPL buffer list for protection groups of
1988  * type LPFC_PG_TYPE_DIF
1989  *
1990  * This is usually used when DIFs are in their own buffers,
1991  * separate from the data. The HBA can then by instructed
1992  * to place the DIFs in the outgoing stream.  For read operations,
1993  * The HBA could extract the DIFs and place it in DIF buffers.
1994  *
1995  * The buffer list for this type consists of one or more of the
1996  * protection groups described below:
1997  *                                    +-------------------------+
1998  *   start of first prot group  -->   |          PDE_5          |
1999  *                                    +-------------------------+
2000  *                                    |          PDE_6          |
2001  *                                    +-------------------------+
2002  *                                    |      PDE_7 (Prot BDE)   |
2003  *                                    +-------------------------+
2004  *                                    |        Data BDE         |
2005  *                                    +-------------------------+
2006  *                                    |more Data BDE's ... (opt)|
2007  *                                    +-------------------------+
2008  *   start of new  prot group  -->    |          PDE_5          |
2009  *                                    +-------------------------+
2010  *                                    |          ...            |
2011  *                                    +-------------------------+
2012  *
2013  * Note: It is assumed that both data and protection s/g buffers have been
2014  *       mapped for DMA
2015  *
2016  * Returns the number of BDEs added to the BPL.
2017  **/
2018 static int
2019 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2020                 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2021 {
2022         struct scatterlist *sgde = NULL; /* s/g data entry */
2023         struct scatterlist *sgpe = NULL; /* s/g prot entry */
2024         struct lpfc_pde5 *pde5 = NULL;
2025         struct lpfc_pde6 *pde6 = NULL;
2026         struct lpfc_pde7 *pde7 = NULL;
2027         dma_addr_t dataphysaddr, protphysaddr;
2028         unsigned short curr_data = 0, curr_prot = 0;
2029         unsigned int split_offset;
2030         unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2031         unsigned int protgrp_blks, protgrp_bytes;
2032         unsigned int remainder, subtotal;
2033         int status;
2034         int datadir = sc->sc_data_direction;
2035         unsigned char pgdone = 0, alldone = 0;
2036         unsigned blksize;
2037         uint32_t rc;
2038         uint32_t checking = 1;
2039         uint32_t reftag;
2040         uint8_t txop, rxop;
2041         int num_bde = 0;
2042
2043         sgpe = scsi_prot_sglist(sc);
2044         sgde = scsi_sglist(sc);
2045
2046         if (!sgpe || !sgde) {
2047                 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2048                                 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2049                                 sgpe, sgde);
2050                 return 0;
2051         }
2052
2053         status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2054         if (status)
2055                 goto out;
2056
2057         /* extract some info from the scsi command */
2058         blksize = lpfc_cmd_blksize(sc);
2059         reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2060
2061 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2062         rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2063         if (rc) {
2064                 if (rc & BG_ERR_SWAP)
2065                         lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2066                 if (rc & BG_ERR_CHECK)
2067                         checking = 0;
2068         }
2069 #endif
2070
2071         split_offset = 0;
2072         do {
2073                 /* setup PDE5 with what we have */
2074                 pde5 = (struct lpfc_pde5 *) bpl;
2075                 memset(pde5, 0, sizeof(struct lpfc_pde5));
2076                 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
2077
2078                 /* Endianness conversion if necessary for PDE5 */
2079                 pde5->word0 = cpu_to_le32(pde5->word0);
2080                 pde5->reftag = cpu_to_le32(reftag);
2081
2082                 /* advance bpl and increment bde count */
2083                 num_bde++;
2084                 bpl++;
2085                 pde6 = (struct lpfc_pde6 *) bpl;
2086
2087                 /* setup PDE6 with the rest of the info */
2088                 memset(pde6, 0, sizeof(struct lpfc_pde6));
2089                 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2090                 bf_set(pde6_optx, pde6, txop);
2091                 bf_set(pde6_oprx, pde6, rxop);
2092                 bf_set(pde6_ce, pde6, checking);
2093                 bf_set(pde6_re, pde6, checking);
2094                 bf_set(pde6_ai, pde6, 1);
2095                 bf_set(pde6_ae, pde6, 0);
2096                 bf_set(pde6_apptagval, pde6, 0);
2097
2098                 /* Endianness conversion if necessary for PDE6 */
2099                 pde6->word0 = cpu_to_le32(pde6->word0);
2100                 pde6->word1 = cpu_to_le32(pde6->word1);
2101                 pde6->word2 = cpu_to_le32(pde6->word2);
2102
2103                 /* advance bpl and increment bde count */
2104                 num_bde++;
2105                 bpl++;
2106
2107                 /* setup the first BDE that points to protection buffer */
2108                 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2109                 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2110
2111                 /* must be integer multiple of the DIF block length */
2112                 BUG_ON(protgroup_len % 8);
2113
2114                 pde7 = (struct lpfc_pde7 *) bpl;
2115                 memset(pde7, 0, sizeof(struct lpfc_pde7));
2116                 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2117
2118                 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2119                 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
2120
2121                 protgrp_blks = protgroup_len / 8;
2122                 protgrp_bytes = protgrp_blks * blksize;
2123
2124                 /* check if this pde is crossing the 4K boundary; if so split */
2125                 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2126                         protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2127                         protgroup_offset += protgroup_remainder;
2128                         protgrp_blks = protgroup_remainder / 8;
2129                         protgrp_bytes = protgrp_blks * blksize;
2130                 } else {
2131                         protgroup_offset = 0;
2132                         curr_prot++;
2133                 }
2134
2135                 num_bde++;
2136
2137                 /* setup BDE's for data blocks associated with DIF data */
2138                 pgdone = 0;
2139                 subtotal = 0; /* total bytes processed for current prot grp */
2140                 while (!pgdone) {
2141                         if (!sgde) {
2142                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2143                                         "9065 BLKGRD:%s Invalid data segment\n",
2144                                                 __func__);
2145                                 return 0;
2146                         }
2147                         bpl++;
2148                         dataphysaddr = sg_dma_address(sgde) + split_offset;
2149                         bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2150                         bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2151
2152                         remainder = sg_dma_len(sgde) - split_offset;
2153
2154                         if ((subtotal + remainder) <= protgrp_bytes) {
2155                                 /* we can use this whole buffer */
2156                                 bpl->tus.f.bdeSize = remainder;
2157                                 split_offset = 0;
2158
2159                                 if ((subtotal + remainder) == protgrp_bytes)
2160                                         pgdone = 1;
2161                         } else {
2162                                 /* must split this buffer with next prot grp */
2163                                 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2164                                 split_offset += bpl->tus.f.bdeSize;
2165                         }
2166
2167                         subtotal += bpl->tus.f.bdeSize;
2168
2169                         if (datadir == DMA_TO_DEVICE)
2170                                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2171                         else
2172                                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2173                         bpl->tus.w = le32_to_cpu(bpl->tus.w);
2174
2175                         num_bde++;
2176                         curr_data++;
2177
2178                         if (split_offset)
2179                                 break;
2180
2181                         /* Move to the next s/g segment if possible */
2182                         sgde = sg_next(sgde);
2183
2184                 }
2185
2186                 if (protgroup_offset) {
2187                         /* update the reference tag */
2188                         reftag += protgrp_blks;
2189                         bpl++;
2190                         continue;
2191                 }
2192
2193                 /* are we done ? */
2194                 if (curr_prot == protcnt) {
2195                         alldone = 1;
2196                 } else if (curr_prot < protcnt) {
2197                         /* advance to next prot buffer */
2198                         sgpe = sg_next(sgpe);
2199                         bpl++;
2200
2201                         /* update the reference tag */
2202                         reftag += protgrp_blks;
2203                 } else {
2204                         /* if we're here, we have a bug */
2205                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2206                                 "9054 BLKGRD: bug in %s\n", __func__);
2207                 }
2208
2209         } while (!alldone);
2210 out:
2211
2212         return num_bde;
2213 }
2214
2215 /**
2216  * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2217  * @phba: The Hba for which this call is being executed.
2218  * @sc: pointer to scsi command we're working on
2219  * @sgl: pointer to buffer list for protection groups
2220  * @datacnt: number of segments of data that have been dma mapped
2221  *
2222  * This function sets up SGL buffer list for protection groups of
2223  * type LPFC_PG_TYPE_NO_DIF
2224  *
2225  * This is usually used when the HBA is instructed to generate
2226  * DIFs and insert them into data stream (or strip DIF from
2227  * incoming data stream)
2228  *
2229  * The buffer list consists of just one protection group described
2230  * below:
2231  *                                +-------------------------+
2232  *   start of prot group  -->     |         DI_SEED         |
2233  *                                +-------------------------+
2234  *                                |         Data SGE        |
2235  *                                +-------------------------+
2236  *                                |more Data SGE's ... (opt)|
2237  *                                +-------------------------+
2238  *
2239  *
2240  * Note: Data s/g buffers have been dma mapped
2241  *
2242  * Returns the number of SGEs added to the SGL.
2243  **/
2244 static int
2245 lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2246                 struct sli4_sge *sgl, int datasegcnt)
2247 {
2248         struct scatterlist *sgde = NULL; /* s/g data entry */
2249         struct sli4_sge_diseed *diseed = NULL;
2250         dma_addr_t physaddr;
2251         int i = 0, num_sge = 0, status;
2252         int datadir = sc->sc_data_direction;
2253         uint32_t reftag;
2254         unsigned blksize;
2255         uint8_t txop, rxop;
2256         uint32_t rc;
2257         uint32_t checking = 1;
2258         uint32_t dma_len;
2259         uint32_t dma_offset = 0;
2260
2261         status  = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2262         if (status)
2263                 goto out;
2264
2265         /* extract some info from the scsi command for pde*/
2266         blksize = lpfc_cmd_blksize(sc);
2267         reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2268
2269 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2270         rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2271         if (rc) {
2272                 if (rc & BG_ERR_SWAP)
2273                         lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2274                 if (rc & BG_ERR_CHECK)
2275                         checking = 0;
2276         }
2277 #endif
2278
2279         /* setup DISEED with what we have */
2280         diseed = (struct sli4_sge_diseed *) sgl;
2281         memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2282         bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2283
2284         /* Endianness conversion if necessary */
2285         diseed->ref_tag = cpu_to_le32(reftag);
2286         diseed->ref_tag_tran = diseed->ref_tag;
2287
2288         /* setup DISEED with the rest of the info */
2289         bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2290         bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2291         if (datadir == DMA_FROM_DEVICE) {
2292                 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2293                 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2294         }
2295         bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2296         bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2297
2298         /* Endianness conversion if necessary for DISEED */
2299         diseed->word2 = cpu_to_le32(diseed->word2);
2300         diseed->word3 = cpu_to_le32(diseed->word3);
2301
2302         /* advance bpl and increment sge count */
2303         num_sge++;
2304         sgl++;
2305
2306         /* assumption: caller has already run dma_map_sg on command data */
2307         scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2308                 physaddr = sg_dma_address(sgde);
2309                 dma_len = sg_dma_len(sgde);
2310                 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2311                 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2312                 if ((i + 1) == datasegcnt)
2313                         bf_set(lpfc_sli4_sge_last, sgl, 1);
2314                 else
2315                         bf_set(lpfc_sli4_sge_last, sgl, 0);
2316                 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2317                 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2318
2319                 sgl->sge_len = cpu_to_le32(dma_len);
2320                 dma_offset += dma_len;
2321
2322                 sgl++;
2323                 num_sge++;
2324         }
2325
2326 out:
2327         return num_sge;
2328 }
2329
2330 /**
2331  * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2332  * @phba: The Hba for which this call is being executed.
2333  * @sc: pointer to scsi command we're working on
2334  * @sgl: pointer to buffer list for protection groups
2335  * @datacnt: number of segments of data that have been dma mapped
2336  * @protcnt: number of segment of protection data that have been dma mapped
2337  *
2338  * This function sets up SGL buffer list for protection groups of
2339  * type LPFC_PG_TYPE_DIF
2340  *
2341  * This is usually used when DIFs are in their own buffers,
2342  * separate from the data. The HBA can then by instructed
2343  * to place the DIFs in the outgoing stream.  For read operations,
2344  * The HBA could extract the DIFs and place it in DIF buffers.
2345  *
2346  * The buffer list for this type consists of one or more of the
2347  * protection groups described below:
2348  *                                    +-------------------------+
2349  *   start of first prot group  -->   |         DISEED          |
2350  *                                    +-------------------------+
2351  *                                    |      DIF (Prot SGE)     |
2352  *                                    +-------------------------+
2353  *                                    |        Data SGE         |
2354  *                                    +-------------------------+
2355  *                                    |more Data SGE's ... (opt)|
2356  *                                    +-------------------------+
2357  *   start of new  prot group  -->    |         DISEED          |
2358  *                                    +-------------------------+
2359  *                                    |          ...            |
2360  *                                    +-------------------------+
2361  *
2362  * Note: It is assumed that both data and protection s/g buffers have been
2363  *       mapped for DMA
2364  *
2365  * Returns the number of SGEs added to the SGL.
2366  **/
2367 static int
2368 lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2369                 struct sli4_sge *sgl, int datacnt, int protcnt)
2370 {
2371         struct scatterlist *sgde = NULL; /* s/g data entry */
2372         struct scatterlist *sgpe = NULL; /* s/g prot entry */
2373         struct sli4_sge_diseed *diseed = NULL;
2374         dma_addr_t dataphysaddr, protphysaddr;
2375         unsigned short curr_data = 0, curr_prot = 0;
2376         unsigned int split_offset;
2377         unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2378         unsigned int protgrp_blks, protgrp_bytes;
2379         unsigned int remainder, subtotal;
2380         int status;
2381         unsigned char pgdone = 0, alldone = 0;
2382         unsigned blksize;
2383         uint32_t reftag;
2384         uint8_t txop, rxop;
2385         uint32_t dma_len;
2386         uint32_t rc;
2387         uint32_t checking = 1;
2388         uint32_t dma_offset = 0;
2389         int num_sge = 0;
2390
2391         sgpe = scsi_prot_sglist(sc);
2392         sgde = scsi_sglist(sc);
2393
2394         if (!sgpe || !sgde) {
2395                 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2396                                 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2397                                 sgpe, sgde);
2398                 return 0;
2399         }
2400
2401         status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2402         if (status)
2403                 goto out;
2404
2405         /* extract some info from the scsi command */
2406         blksize = lpfc_cmd_blksize(sc);
2407         reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2408
2409 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2410         rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2411         if (rc) {
2412                 if (rc & BG_ERR_SWAP)
2413                         lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2414                 if (rc & BG_ERR_CHECK)
2415                         checking = 0;
2416         }
2417 #endif
2418
2419         split_offset = 0;
2420         do {
2421                 /* setup DISEED with what we have */
2422                 diseed = (struct sli4_sge_diseed *) sgl;
2423                 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2424                 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2425
2426                 /* Endianness conversion if necessary */
2427                 diseed->ref_tag = cpu_to_le32(reftag);
2428                 diseed->ref_tag_tran = diseed->ref_tag;
2429
2430                 /* setup DISEED with the rest of the info */
2431                 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2432                 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2433                 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2434                 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2435                 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2436                 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2437
2438                 /* Endianness conversion if necessary for DISEED */
2439                 diseed->word2 = cpu_to_le32(diseed->word2);
2440                 diseed->word3 = cpu_to_le32(diseed->word3);
2441
2442                 /* advance sgl and increment bde count */
2443                 num_sge++;
2444                 sgl++;
2445
2446                 /* setup the first BDE that points to protection buffer */
2447                 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2448                 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2449
2450                 /* must be integer multiple of the DIF block length */
2451                 BUG_ON(protgroup_len % 8);
2452
2453                 /* Now setup DIF SGE */
2454                 sgl->word2 = 0;
2455                 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2456                 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2457                 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2458                 sgl->word2 = cpu_to_le32(sgl->word2);
2459
2460                 protgrp_blks = protgroup_len / 8;
2461                 protgrp_bytes = protgrp_blks * blksize;
2462
2463                 /* check if DIF SGE is crossing the 4K boundary; if so split */
2464                 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2465                         protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2466                         protgroup_offset += protgroup_remainder;
2467                         protgrp_blks = protgroup_remainder / 8;
2468                         protgrp_bytes = protgrp_blks * blksize;
2469                 } else {
2470                         protgroup_offset = 0;
2471                         curr_prot++;
2472                 }
2473
2474                 num_sge++;
2475
2476                 /* setup SGE's for data blocks associated with DIF data */
2477                 pgdone = 0;
2478                 subtotal = 0; /* total bytes processed for current prot grp */
2479                 while (!pgdone) {
2480                         if (!sgde) {
2481                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2482                                         "9086 BLKGRD:%s Invalid data segment\n",
2483                                                 __func__);
2484                                 return 0;
2485                         }
2486                         sgl++;
2487                         dataphysaddr = sg_dma_address(sgde) + split_offset;
2488
2489                         remainder = sg_dma_len(sgde) - split_offset;
2490
2491                         if ((subtotal + remainder) <= protgrp_bytes) {
2492                                 /* we can use this whole buffer */
2493                                 dma_len = remainder;
2494                                 split_offset = 0;
2495
2496                                 if ((subtotal + remainder) == protgrp_bytes)
2497                                         pgdone = 1;
2498                         } else {
2499                                 /* must split this buffer with next prot grp */
2500                                 dma_len = protgrp_bytes - subtotal;
2501                                 split_offset += dma_len;
2502                         }
2503
2504                         subtotal += dma_len;
2505
2506                         sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2507                         sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2508                         bf_set(lpfc_sli4_sge_last, sgl, 0);
2509                         bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2510                         bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2511
2512                         sgl->sge_len = cpu_to_le32(dma_len);
2513                         dma_offset += dma_len;
2514
2515                         num_sge++;
2516                         curr_data++;
2517
2518                         if (split_offset)
2519                                 break;
2520
2521                         /* Move to the next s/g segment if possible */
2522                         sgde = sg_next(sgde);
2523                 }
2524
2525                 if (protgroup_offset) {
2526                         /* update the reference tag */
2527                         reftag += protgrp_blks;
2528                         sgl++;
2529                         continue;
2530                 }
2531
2532                 /* are we done ? */
2533                 if (curr_prot == protcnt) {
2534                         bf_set(lpfc_sli4_sge_last, sgl, 1);
2535                         alldone = 1;
2536                 } else if (curr_prot < protcnt) {
2537                         /* advance to next prot buffer */
2538                         sgpe = sg_next(sgpe);
2539                         sgl++;
2540
2541                         /* update the reference tag */
2542                         reftag += protgrp_blks;
2543                 } else {
2544                         /* if we're here, we have a bug */
2545                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2546                                 "9085 BLKGRD: bug in %s\n", __func__);
2547                 }
2548
2549         } while (!alldone);
2550
2551 out:
2552
2553         return num_sge;
2554 }
2555
2556 /**
2557  * lpfc_prot_group_type - Get prtotection group type of SCSI command
2558  * @phba: The Hba for which this call is being executed.
2559  * @sc: pointer to scsi command we're working on
2560  *
2561  * Given a SCSI command that supports DIF, determine composition of protection
2562  * groups involved in setting up buffer lists
2563  *
2564  * Returns: Protection group type (with or without DIF)
2565  *
2566  **/
2567 static int
2568 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2569 {
2570         int ret = LPFC_PG_TYPE_INVALID;
2571         unsigned char op = scsi_get_prot_op(sc);
2572
2573         switch (op) {
2574         case SCSI_PROT_READ_STRIP:
2575         case SCSI_PROT_WRITE_INSERT:
2576                 ret = LPFC_PG_TYPE_NO_DIF;
2577                 break;
2578         case SCSI_PROT_READ_INSERT:
2579         case SCSI_PROT_WRITE_STRIP:
2580         case SCSI_PROT_READ_PASS:
2581         case SCSI_PROT_WRITE_PASS:
2582                 ret = LPFC_PG_TYPE_DIF_BUF;
2583                 break;
2584         default:
2585                 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2586                                 "9021 Unsupported protection op:%d\n", op);
2587                 break;
2588         }
2589
2590         return ret;
2591 }
2592
2593 /**
2594  * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2595  * @phba: The Hba for which this call is being executed.
2596  * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2597  *
2598  * This is the protection/DIF aware version of
2599  * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2600  * two functions eventually, but for now, it's here
2601  **/
2602 static int
2603 lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
2604                 struct lpfc_scsi_buf *lpfc_cmd)
2605 {
2606         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2607         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2608         struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2609         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2610         uint32_t num_bde = 0;
2611         int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2612         int prot_group_type = 0;
2613         int diflen, fcpdl;
2614         unsigned blksize;
2615
2616         /*
2617          * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2618          *  fcp_rsp regions to the first data bde entry
2619          */
2620         bpl += 2;
2621         if (scsi_sg_count(scsi_cmnd)) {
2622                 /*
2623                  * The driver stores the segment count returned from pci_map_sg
2624                  * because this a count of dma-mappings used to map the use_sg
2625                  * pages.  They are not guaranteed to be the same for those
2626                  * architectures that implement an IOMMU.
2627                  */
2628                 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2629                                         scsi_sglist(scsi_cmnd),
2630                                         scsi_sg_count(scsi_cmnd), datadir);
2631                 if (unlikely(!datasegcnt))
2632                         return 1;
2633
2634                 lpfc_cmd->seg_cnt = datasegcnt;
2635                 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2636                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2637                                         "9067 BLKGRD: %s: Too many sg segments"
2638                                         " from dma_map_sg.  Config %d, seg_cnt"
2639                                         " %d\n",
2640                                         __func__, phba->cfg_sg_seg_cnt,
2641                                         lpfc_cmd->seg_cnt);
2642                         scsi_dma_unmap(scsi_cmnd);
2643                         return 1;
2644                 }
2645
2646                 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2647
2648                 switch (prot_group_type) {
2649                 case LPFC_PG_TYPE_NO_DIF:
2650                         num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2651                                         datasegcnt);
2652                         /* we should have 2 or more entries in buffer list */
2653                         if (num_bde < 2)
2654                                 goto err;
2655                         break;
2656                 case LPFC_PG_TYPE_DIF_BUF:{
2657                         /*
2658                          * This type indicates that protection buffers are
2659                          * passed to the driver, so that needs to be prepared
2660                          * for DMA
2661                          */
2662                         protsegcnt = dma_map_sg(&phba->pcidev->dev,
2663                                         scsi_prot_sglist(scsi_cmnd),
2664                                         scsi_prot_sg_count(scsi_cmnd), datadir);
2665                         if (unlikely(!protsegcnt)) {
2666                                 scsi_dma_unmap(scsi_cmnd);
2667                                 return 1;
2668                         }
2669
2670                         lpfc_cmd->prot_seg_cnt = protsegcnt;
2671                         if (lpfc_cmd->prot_seg_cnt
2672                             > phba->cfg_prot_sg_seg_cnt) {
2673                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2674                                         "9068 BLKGRD: %s: Too many prot sg "
2675                                         "segments from dma_map_sg.  Config %d,"
2676                                                 "prot_seg_cnt %d\n", __func__,
2677                                                 phba->cfg_prot_sg_seg_cnt,
2678                                                 lpfc_cmd->prot_seg_cnt);
2679                                 dma_unmap_sg(&phba->pcidev->dev,
2680                                              scsi_prot_sglist(scsi_cmnd),
2681                                              scsi_prot_sg_count(scsi_cmnd),
2682                                              datadir);
2683                                 scsi_dma_unmap(scsi_cmnd);
2684                                 return 1;
2685                         }
2686
2687                         num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2688                                         datasegcnt, protsegcnt);
2689                         /* we should have 3 or more entries in buffer list */
2690                         if (num_bde < 3)
2691                                 goto err;
2692                         break;
2693                 }
2694                 case LPFC_PG_TYPE_INVALID:
2695                 default:
2696                         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2697                                         "9022 Unexpected protection group %i\n",
2698                                         prot_group_type);
2699                         return 1;
2700                 }
2701         }
2702
2703         /*
2704          * Finish initializing those IOCB fields that are dependent on the
2705          * scsi_cmnd request_buffer.  Note that the bdeSize is explicitly
2706          * reinitialized since all iocb memory resources are used many times
2707          * for transmit, receive, and continuation bpl's.
2708          */
2709         iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2710         iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2711         iocb_cmd->ulpBdeCount = 1;
2712         iocb_cmd->ulpLe = 1;
2713
2714         fcpdl = scsi_bufflen(scsi_cmnd);
2715
2716         if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2717                 /*
2718                  * We are in DIF Type 1 mode
2719                  * Every data block has a 8 byte DIF (trailer)
2720                  * attached to it.  Must ajust FCP data length
2721                  */
2722                 blksize = lpfc_cmd_blksize(scsi_cmnd);
2723                 diflen = (fcpdl / blksize) * 8;
2724                 fcpdl += diflen;
2725         }
2726         fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2727
2728         /*
2729          * Due to difference in data length between DIF/non-DIF paths,
2730          * we need to set word 4 of IOCB here
2731          */
2732         iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2733
2734         return 0;
2735 err:
2736         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2737                         "9023 Could not setup all needed BDE's"
2738                         "prot_group_type=%d, num_bde=%d\n",
2739                         prot_group_type, num_bde);
2740         return 1;
2741 }
2742
2743 /*
2744  * This function checks for BlockGuard errors detected by
2745  * the HBA.  In case of errors, the ASC/ASCQ fields in the
2746  * sense buffer will be set accordingly, paired with
2747  * ILLEGAL_REQUEST to signal to the kernel that the HBA
2748  * detected corruption.
2749  *
2750  * Returns:
2751  *  0 - No error found
2752  *  1 - BlockGuard error found
2753  * -1 - Internal error (bad profile, ...etc)
2754  */
2755 static int
2756 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2757                         struct lpfc_iocbq *pIocbOut)
2758 {
2759         struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2760         struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2761         int ret = 0;
2762         uint32_t bghm = bgf->bghm;
2763         uint32_t bgstat = bgf->bgstat;
2764         uint64_t failing_sector = 0;
2765
2766         lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2767                         " 0x%x lba 0x%llx blk cnt 0x%x "
2768                         "bgstat=0x%x bghm=0x%x\n",
2769                         cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
2770                         blk_rq_sectors(cmd->request), bgstat, bghm);
2771
2772         spin_lock(&_dump_buf_lock);
2773         if (!_dump_buf_done) {
2774                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,  "9070 BLKGRD: Saving"
2775                         " Data for %u blocks to debugfs\n",
2776                                 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2777                 lpfc_debug_save_data(phba, cmd);
2778
2779                 /* If we have a prot sgl, save the DIF buffer */
2780                 if (lpfc_prot_group_type(phba, cmd) ==
2781                                 LPFC_PG_TYPE_DIF_BUF) {
2782                         lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2783                                 "Saving DIF for %u blocks to debugfs\n",
2784                                 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2785                         lpfc_debug_save_dif(phba, cmd);
2786                 }
2787
2788                 _dump_buf_done = 1;
2789         }
2790         spin_unlock(&_dump_buf_lock);
2791
2792         if (lpfc_bgs_get_invalid_prof(bgstat)) {
2793                 cmd->result = ScsiResult(DID_ERROR, 0);
2794                 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2795                         " BlockGuard profile. bgstat:0x%x\n",
2796                         bgstat);
2797                 ret = (-1);
2798                 goto out;
2799         }
2800
2801         if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2802                 cmd->result = ScsiResult(DID_ERROR, 0);
2803                 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2804                                 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
2805                                 bgstat);
2806                 ret = (-1);
2807                 goto out;
2808         }
2809
2810         if (lpfc_bgs_get_guard_err(bgstat)) {
2811                 ret = 1;
2812
2813                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2814                                 0x10, 0x1);
2815                 cmd->result = DRIVER_SENSE << 24
2816                         | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2817                 phba->bg_guard_err_cnt++;
2818                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2819                         "9055 BLKGRD: guard_tag error\n");
2820         }
2821
2822         if (lpfc_bgs_get_reftag_err(bgstat)) {
2823                 ret = 1;
2824
2825                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2826                                 0x10, 0x3);
2827                 cmd->result = DRIVER_SENSE << 24
2828                         | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2829
2830                 phba->bg_reftag_err_cnt++;
2831                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2832                         "9056 BLKGRD: ref_tag error\n");
2833         }
2834
2835         if (lpfc_bgs_get_apptag_err(bgstat)) {
2836                 ret = 1;
2837
2838                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2839                                 0x10, 0x2);
2840                 cmd->result = DRIVER_SENSE << 24
2841                         | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2842
2843                 phba->bg_apptag_err_cnt++;
2844                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2845                         "9061 BLKGRD: app_tag error\n");
2846         }
2847
2848         if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2849                 /*
2850                  * setup sense data descriptor 0 per SPC-4 as an information
2851                  * field, and put the failing LBA in it.
2852                  * This code assumes there was also a guard/app/ref tag error
2853                  * indication.
2854                  */
2855                 cmd->sense_buffer[7] = 0xc;   /* Additional sense length */
2856                 cmd->sense_buffer[8] = 0;     /* Information descriptor type */
2857                 cmd->sense_buffer[9] = 0xa;   /* Additional descriptor length */
2858                 cmd->sense_buffer[10] = 0x80; /* Validity bit */
2859
2860                 /* bghm is a "on the wire" FC frame based count */
2861                 switch (scsi_get_prot_op(cmd)) {
2862                 case SCSI_PROT_READ_INSERT:
2863                 case SCSI_PROT_WRITE_STRIP:
2864                         bghm /= cmd->device->sector_size;
2865                         break;
2866                 case SCSI_PROT_READ_STRIP:
2867                 case SCSI_PROT_WRITE_INSERT:
2868                 case SCSI_PROT_READ_PASS:
2869                 case SCSI_PROT_WRITE_PASS:
2870                         bghm /= (cmd->device->sector_size +
2871                                 sizeof(struct scsi_dif_tuple));
2872                         break;
2873                 }
2874
2875                 failing_sector = scsi_get_lba(cmd);
2876                 failing_sector += bghm;
2877
2878                 /* Descriptor Information */
2879                 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
2880         }
2881
2882         if (!ret) {
2883                 /* No error was reported - problem in FW? */
2884                 cmd->result = ScsiResult(DID_ERROR, 0);
2885                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2886                         "9057 BLKGRD: Unknown error reported!\n");
2887         }
2888
2889 out:
2890         return ret;
2891 }
2892
2893 /**
2894  * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2895  * @phba: The Hba for which this call is being executed.
2896  * @lpfc_cmd: The scsi buffer which is going to be mapped.
2897  *
2898  * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2899  * field of @lpfc_cmd for device with SLI-4 interface spec.
2900  *
2901  * Return codes:
2902  *      1 - Error
2903  *      0 - Success
2904  **/
2905 static int
2906 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2907 {
2908         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2909         struct scatterlist *sgel = NULL;
2910         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2911         struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
2912         struct sli4_sge *first_data_sgl;
2913         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2914         dma_addr_t physaddr;
2915         uint32_t num_bde = 0;
2916         uint32_t dma_len;
2917         uint32_t dma_offset = 0;
2918         int nseg;
2919         struct ulp_bde64 *bde;
2920
2921         /*
2922          * There are three possibilities here - use scatter-gather segment, use
2923          * the single mapping, or neither.  Start the lpfc command prep by
2924          * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2925          * data bde entry.
2926          */
2927         if (scsi_sg_count(scsi_cmnd)) {
2928                 /*
2929                  * The driver stores the segment count returned from pci_map_sg
2930                  * because this a count of dma-mappings used to map the use_sg
2931                  * pages.  They are not guaranteed to be the same for those
2932                  * architectures that implement an IOMMU.
2933                  */
2934
2935                 nseg = scsi_dma_map(scsi_cmnd);
2936                 if (unlikely(!nseg))
2937                         return 1;
2938                 sgl += 1;
2939                 /* clear the last flag in the fcp_rsp map entry */
2940                 sgl->word2 = le32_to_cpu(sgl->word2);
2941                 bf_set(lpfc_sli4_sge_last, sgl, 0);
2942                 sgl->word2 = cpu_to_le32(sgl->word2);
2943                 sgl += 1;
2944                 first_data_sgl = sgl;
2945                 lpfc_cmd->seg_cnt = nseg;
2946                 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2947                         lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
2948                                 " %s: Too many sg segments from "
2949                                 "dma_map_sg.  Config %d, seg_cnt %d\n",
2950                                 __func__, phba->cfg_sg_seg_cnt,
2951                                lpfc_cmd->seg_cnt);
2952                         scsi_dma_unmap(scsi_cmnd);
2953                         return 1;
2954                 }
2955
2956                 /*
2957                  * The driver established a maximum scatter-gather segment count
2958                  * during probe that limits the number of sg elements in any
2959                  * single scsi command.  Just run through the seg_cnt and format
2960                  * the sge's.
2961                  * When using SLI-3 the driver will try to fit all the BDEs into
2962                  * the IOCB. If it can't then the BDEs get added to a BPL as it
2963                  * does for SLI-2 mode.
2964                  */
2965                 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
2966                         physaddr = sg_dma_address(sgel);
2967                         dma_len = sg_dma_len(sgel);
2968                         sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2969                         sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2970                         sgl->word2 = le32_to_cpu(sgl->word2);
2971                         if ((num_bde + 1) == nseg)
2972                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
2973                         else
2974                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
2975                         bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2976                         bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2977                         sgl->word2 = cpu_to_le32(sgl->word2);
2978                         sgl->sge_len = cpu_to_le32(dma_len);
2979                         dma_offset += dma_len;
2980                         sgl++;
2981                 }
2982                 /* setup the performance hint (first data BDE) if enabled */
2983                 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
2984                         bde = (struct ulp_bde64 *)
2985                                         &(iocb_cmd->unsli3.sli3Words[5]);
2986                         bde->addrLow = first_data_sgl->addr_lo;
2987                         bde->addrHigh = first_data_sgl->addr_hi;
2988                         bde->tus.f.bdeSize =
2989                                         le32_to_cpu(first_data_sgl->sge_len);
2990                         bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2991                         bde->tus.w = cpu_to_le32(bde->tus.w);
2992                 }
2993         } else {
2994                 sgl += 1;
2995                 /* clear the last flag in the fcp_rsp map entry */
2996                 sgl->word2 = le32_to_cpu(sgl->word2);
2997                 bf_set(lpfc_sli4_sge_last, sgl, 1);
2998                 sgl->word2 = cpu_to_le32(sgl->word2);
2999         }
3000
3001         /*
3002          * Finish initializing those IOCB fields that are dependent on the
3003          * scsi_cmnd request_buffer.  Note that for SLI-2 the bdeSize is
3004          * explicitly reinitialized.
3005          * all iocb memory resources are reused.
3006          */
3007         fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3008
3009         /*
3010          * Due to difference in data length between DIF/non-DIF paths,
3011          * we need to set word 4 of IOCB here
3012          */
3013         iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3014         return 0;
3015 }
3016
3017 /**
3018  * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
3019  * @phba: The Hba for which this call is being executed.
3020  * @lpfc_cmd: The scsi buffer which is going to be adjusted.
3021  *
3022  * Adjust the data length to account for how much data
3023  * is actually on the wire.
3024  *
3025  * returns the adjusted data length
3026  **/
3027 static int
3028 lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
3029                 struct lpfc_scsi_buf *lpfc_cmd)
3030 {
3031         struct scsi_cmnd *sc = lpfc_cmd->pCmd;
3032         int diflen, fcpdl;
3033         unsigned blksize;
3034
3035         fcpdl = scsi_bufflen(sc);
3036
3037         /* Check if there is protection data on the wire */
3038         if (sc->sc_data_direction == DMA_FROM_DEVICE) {
3039                 /* Read */
3040                 if (scsi_get_prot_op(sc) ==  SCSI_PROT_READ_INSERT)
3041                         return fcpdl;
3042
3043         } else {
3044                 /* Write */
3045                 if (scsi_get_prot_op(sc) ==  SCSI_PROT_WRITE_STRIP)
3046                         return fcpdl;
3047         }
3048
3049         /* If protection data on the wire, adjust the count accordingly */
3050         blksize = lpfc_cmd_blksize(sc);
3051         diflen = (fcpdl / blksize) * 8;
3052         fcpdl += diflen;
3053         return fcpdl;
3054 }
3055
3056 /**
3057  * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3058  * @phba: The Hba for which this call is being executed.
3059  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3060  *
3061  * This is the protection/DIF aware version of
3062  * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3063  * two functions eventually, but for now, it's here
3064  **/
3065 static int
3066 lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3067                 struct lpfc_scsi_buf *lpfc_cmd)
3068 {
3069         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3070         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3071         struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3072         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3073         uint32_t num_bde = 0;
3074         int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3075         int prot_group_type = 0;
3076         int fcpdl;
3077
3078         /*
3079          * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3080          *  fcp_rsp regions to the first data bde entry
3081          */
3082         if (scsi_sg_count(scsi_cmnd)) {
3083                 /*
3084                  * The driver stores the segment count returned from pci_map_sg
3085                  * because this a count of dma-mappings used to map the use_sg
3086                  * pages.  They are not guaranteed to be the same for those
3087                  * architectures that implement an IOMMU.
3088                  */
3089                 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3090                                         scsi_sglist(scsi_cmnd),
3091                                         scsi_sg_count(scsi_cmnd), datadir);
3092                 if (unlikely(!datasegcnt))
3093                         return 1;
3094
3095                 sgl += 1;
3096                 /* clear the last flag in the fcp_rsp map entry */
3097                 sgl->word2 = le32_to_cpu(sgl->word2);
3098                 bf_set(lpfc_sli4_sge_last, sgl, 0);
3099                 sgl->word2 = cpu_to_le32(sgl->word2);
3100
3101                 sgl += 1;
3102                 lpfc_cmd->seg_cnt = datasegcnt;
3103                 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3104                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3105                                         "9087 BLKGRD: %s: Too many sg segments"
3106                                         " from dma_map_sg.  Config %d, seg_cnt"
3107                                         " %d\n",
3108                                         __func__, phba->cfg_sg_seg_cnt,
3109                                         lpfc_cmd->seg_cnt);
3110                         scsi_dma_unmap(scsi_cmnd);
3111                         return 1;
3112                 }
3113
3114                 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3115
3116                 switch (prot_group_type) {
3117                 case LPFC_PG_TYPE_NO_DIF:
3118                         num_bde = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3119                                         datasegcnt);
3120                         /* we should have 2 or more entries in buffer list */
3121                         if (num_bde < 2)
3122                                 goto err;
3123                         break;
3124                 case LPFC_PG_TYPE_DIF_BUF:{
3125                         /*
3126                          * This type indicates that protection buffers are
3127                          * passed to the driver, so that needs to be prepared
3128                          * for DMA
3129                          */
3130                         protsegcnt = dma_map_sg(&phba->pcidev->dev,
3131                                         scsi_prot_sglist(scsi_cmnd),
3132                                         scsi_prot_sg_count(scsi_cmnd), datadir);
3133                         if (unlikely(!protsegcnt)) {
3134                                 scsi_dma_unmap(scsi_cmnd);
3135                                 return 1;
3136                         }
3137
3138                         lpfc_cmd->prot_seg_cnt = protsegcnt;
3139                         if (lpfc_cmd->prot_seg_cnt
3140                             > phba->cfg_prot_sg_seg_cnt) {
3141                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3142                                         "9088 BLKGRD: %s: Too many prot sg "
3143                                         "segments from dma_map_sg.  Config %d,"
3144                                                 "prot_seg_cnt %d\n", __func__,
3145                                                 phba->cfg_prot_sg_seg_cnt,
3146                                                 lpfc_cmd->prot_seg_cnt);
3147                                 dma_unmap_sg(&phba->pcidev->dev,
3148                                              scsi_prot_sglist(scsi_cmnd),
3149                                              scsi_prot_sg_count(scsi_cmnd),
3150                                              datadir);
3151                                 scsi_dma_unmap(scsi_cmnd);
3152                                 return 1;
3153                         }
3154
3155                         num_bde = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3156                                         datasegcnt, protsegcnt);
3157                         /* we should have 3 or more entries in buffer list */
3158                         if (num_bde < 3)
3159                                 goto err;
3160                         break;
3161                 }
3162                 case LPFC_PG_TYPE_INVALID:
3163                 default:
3164                         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3165                                         "9083 Unexpected protection group %i\n",
3166                                         prot_group_type);
3167                         return 1;
3168                 }
3169         }
3170
3171         fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3172
3173         fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3174
3175         /*
3176          * Due to difference in data length between DIF/non-DIF paths,
3177          * we need to set word 4 of IOCB here
3178          */
3179         iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3180         lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF;
3181
3182         return 0;
3183 err:
3184         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3185                         "9084 Could not setup all needed BDE's"
3186                         "prot_group_type=%d, num_bde=%d\n",
3187                         prot_group_type, num_bde);
3188         return 1;
3189 }
3190
3191 /**
3192  * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3193  * @phba: The Hba for which this call is being executed.
3194  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3195  *
3196  * This routine wraps the actual DMA mapping function pointer from the
3197  * lpfc_hba struct.
3198  *
3199  * Return codes:
3200  *      1 - Error
3201  *      0 - Success
3202  **/
3203 static inline int
3204 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3205 {
3206         return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3207 }
3208
3209 /**
3210  * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3211  * using BlockGuard.
3212  * @phba: The Hba for which this call is being executed.
3213  * @lpfc_cmd: The scsi buffer which is going to be mapped.
3214  *
3215  * This routine wraps the actual DMA mapping function pointer from the
3216  * lpfc_hba struct.
3217  *
3218  * Return codes:
3219  *      1 - Error
3220  *      0 - Success
3221  **/
3222 static inline int
3223 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3224 {
3225         return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3226 }
3227
3228 /**
3229  * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
3230  * @phba: Pointer to hba context object.
3231  * @vport: Pointer to vport object.
3232  * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3233  * @rsp_iocb: Pointer to response iocb object which reported error.
3234  *
3235  * This function posts an event when there is a SCSI command reporting
3236  * error from the scsi device.
3237  **/
3238 static void
3239 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3240                 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3241         struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3242         struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3243         uint32_t resp_info = fcprsp->rspStatus2;
3244         uint32_t scsi_status = fcprsp->rspStatus3;
3245         uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3246         struct lpfc_fast_path_event *fast_path_evt = NULL;
3247         struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3248         unsigned long flags;
3249
3250         if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3251                 return;
3252
3253         /* If there is queuefull or busy condition send a scsi event */
3254         if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3255                 (cmnd->result == SAM_STAT_BUSY)) {
3256                 fast_path_evt = lpfc_alloc_fast_evt(phba);
3257                 if (!fast_path_evt)
3258                         return;
3259                 fast_path_evt->un.scsi_evt.event_type =
3260                         FC_REG_SCSI_EVENT;
3261                 fast_path_evt->un.scsi_evt.subcategory =
3262                 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3263                 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3264                 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3265                 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3266                         &pnode->nlp_portname, sizeof(struct lpfc_name));
3267                 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3268                         &pnode->nlp_nodename, sizeof(struct lpfc_name));
3269         } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3270                 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3271                 fast_path_evt = lpfc_alloc_fast_evt(phba);
3272                 if (!fast_path_evt)
3273                         return;
3274                 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3275                         FC_REG_SCSI_EVENT;
3276                 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3277                         LPFC_EVENT_CHECK_COND;
3278                 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3279                         cmnd->device->lun;
3280                 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3281                         &pnode->nlp_portname, sizeof(struct lpfc_name));
3282                 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3283                         &pnode->nlp_nodename, sizeof(struct lpfc_name));
3284                 fast_path_evt->un.check_cond_evt.sense_key =
3285                         cmnd->sense_buffer[2] & 0xf;
3286                 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3287                 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3288         } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3289                      fcpi_parm &&
3290                      ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3291                         ((scsi_status == SAM_STAT_GOOD) &&
3292                         !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3293                 /*
3294                  * If status is good or resid does not match with fcp_param and
3295                  * there is valid fcpi_parm, then there is a read_check error
3296                  */
3297                 fast_path_evt = lpfc_alloc_fast_evt(phba);
3298                 if (!fast_path_evt)
3299                         return;
3300                 fast_path_evt->un.read_check_error.header.event_type =
3301                         FC_REG_FABRIC_EVENT;
3302                 fast_path_evt->un.read_check_error.header.subcategory =
3303                         LPFC_EVENT_FCPRDCHKERR;
3304                 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3305                         &pnode->nlp_portname, sizeof(struct lpfc_name));
3306                 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3307                         &pnode->nlp_nodename, sizeof(struct lpfc_name));
3308                 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3309                 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3310                 fast_path_evt->un.read_check_error.fcpiparam =
3311                         fcpi_parm;
3312         } else
3313                 return;
3314
3315         fast_path_evt->vport = vport;
3316         spin_lock_irqsave(&phba->hbalock, flags);
3317         list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3318         spin_unlock_irqrestore(&phba->hbalock, flags);
3319         lpfc_worker_wake_up(phba);
3320         return;
3321 }
3322
3323 /**
3324  * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3325  * @phba: The HBA for which this call is being executed.
3326  * @psb: The scsi buffer which is going to be un-mapped.
3327  *
3328  * This routine does DMA un-mapping of scatter gather list of scsi command
3329  * field of @lpfc_cmd for device with SLI-3 interface spec.
3330  **/
3331 static void
3332 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
3333 {
3334         /*
3335          * There are only two special cases to consider.  (1) the scsi command
3336          * requested scatter-gather usage or (2) the scsi command allocated
3337          * a request buffer, but did not request use_sg.  There is a third
3338          * case, but it does not require resource deallocation.
3339          */
3340         if (psb->seg_cnt > 0)
3341                 scsi_dma_unmap(psb->pCmd);
3342         if (psb->prot_seg_cnt > 0)
3343                 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3344                                 scsi_prot_sg_count(psb->pCmd),
3345                                 psb->pCmd->sc_data_direction);
3346 }
3347
3348 /**
3349  * lpfc_handler_fcp_err - FCP response handler
3350  * @vport: The virtual port for which this call is being executed.
3351  * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3352  * @rsp_iocb: The response IOCB which contains FCP error.
3353  *
3354  * This routine is called to process response IOCB with status field
3355  * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3356  * based upon SCSI and FCP error.
3357  **/
3358 static void
3359 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3360                     struct lpfc_iocbq *rsp_iocb)
3361 {
3362         struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3363         struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3364         struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3365         uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3366         uint32_t resp_info = fcprsp->rspStatus2;
3367         uint32_t scsi_status = fcprsp->rspStatus3;
3368         uint32_t *lp;
3369         uint32_t host_status = DID_OK;
3370         uint32_t rsplen = 0;
3371         uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
3372
3373
3374         /*
3375          *  If this is a task management command, there is no
3376          *  scsi packet associated with this lpfc_cmd.  The driver
3377          *  consumes it.
3378          */
3379         if (fcpcmd->fcpCntl2) {
3380                 scsi_status = 0;
3381                 goto out;
3382         }
3383
3384         if (resp_info & RSP_LEN_VALID) {
3385                 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3386                 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
3387                         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3388                                  "2719 Invalid response length: "
3389                                  "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3390                                  cmnd->device->id,
3391                                  cmnd->device->lun, cmnd->cmnd[0],
3392                                  rsplen);
3393                         host_status = DID_ERROR;
3394                         goto out;
3395                 }
3396                 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3397                         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3398                                  "2757 Protocol failure detected during "
3399                                  "processing of FCP I/O op: "
3400                                  "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3401                                  cmnd->device->id,
3402                                  cmnd->device->lun, cmnd->cmnd[0],
3403                                  fcprsp->rspInfo3);
3404                         host_status = DID_ERROR;
3405                         goto out;
3406                 }
3407         }
3408
3409         if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3410                 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3411                 if (snslen > SCSI_SENSE_BUFFERSIZE)
3412                         snslen = SCSI_SENSE_BUFFERSIZE;
3413
3414                 if (resp_info & RSP_LEN_VALID)
3415                   rsplen = be32_to_cpu(fcprsp->rspRspLen);
3416                 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3417         }
3418         lp = (uint32_t *)cmnd->sense_buffer;
3419
3420         if (!scsi_status && (resp_info & RESID_UNDER) &&
3421                 vport->cfg_log_verbose & LOG_FCP_UNDER)
3422                 logit = LOG_FCP_UNDER;
3423
3424         lpfc_printf_vlog(vport, KERN_WARNING, logit,
3425                          "9024 FCP command x%x failed: x%x SNS x%x x%x "
3426                          "Data: x%x x%x x%x x%x x%x\n",
3427                          cmnd->cmnd[0], scsi_status,
3428                          be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3429                          be32_to_cpu(fcprsp->rspResId),
3430                          be32_to_cpu(fcprsp->rspSnsLen),
3431                          be32_to_cpu(fcprsp->rspRspLen),
3432                          fcprsp->rspInfo3);
3433
3434         scsi_set_resid(cmnd, 0);
3435         if (resp_info & RESID_UNDER) {
3436                 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
3437
3438                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
3439                                  "9025 FCP Read Underrun, expected %d, "
3440                                  "residual %d Data: x%x x%x x%x\n",
3441                                  be32_to_cpu(fcpcmd->fcpDl),
3442                                  scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3443                                  cmnd->underflow);
3444
3445                 /*
3446                  * If there is an under run check if under run reported by
3447                  * storage array is same as the under run reported by HBA.
3448                  * If this is not same, there is a dropped frame.
3449                  */
3450                 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3451                         fcpi_parm &&
3452                         (scsi_get_resid(cmnd) != fcpi_parm)) {
3453                         lpfc_printf_vlog(vport, KERN_WARNING,
3454                                          LOG_FCP | LOG_FCP_ERROR,
3455                                          "9026 FCP Read Check Error "
3456                                          "and Underrun Data: x%x x%x x%x x%x\n",
3457                                          be32_to_cpu(fcpcmd->fcpDl),
3458                                          scsi_get_resid(cmnd), fcpi_parm,
3459                                          cmnd->cmnd[0]);
3460                         scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3461                         host_status = DID_ERROR;
3462                 }
3463                 /*
3464                  * The cmnd->underflow is the minimum number of bytes that must
3465                  * be transferred for this command.  Provided a sense condition
3466                  * is not present, make sure the actual amount transferred is at
3467                  * least the underflow value or fail.
3468                  */
3469                 if (!(resp_info & SNS_LEN_VALID) &&
3470                     (scsi_status == SAM_STAT_GOOD) &&
3471                     (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3472                      < cmnd->underflow)) {
3473                         lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3474                                          "9027 FCP command x%x residual "
3475                                          "underrun converted to error "
3476                                          "Data: x%x x%x x%x\n",
3477                                          cmnd->cmnd[0], scsi_bufflen(cmnd),
3478                                          scsi_get_resid(cmnd), cmnd->underflow);
3479                         host_status = DID_ERROR;
3480                 }
3481         } else if (resp_info & RESID_OVER) {
3482                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3483                                  "9028 FCP command x%x residual overrun error. "
3484                                  "Data: x%x x%x\n", cmnd->cmnd[0],
3485                                  scsi_bufflen(cmnd), scsi_get_resid(cmnd));
3486                 host_status = DID_ERROR;
3487
3488         /*
3489          * Check SLI validation that all the transfer was actually done
3490          * (fcpi_parm should be zero). Apply check only to reads.
3491          */
3492         } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
3493                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
3494                                  "9029 FCP Read Check Error Data: "
3495                                  "x%x x%x x%x x%x x%x\n",
3496                                  be32_to_cpu(fcpcmd->fcpDl),
3497                                  be32_to_cpu(fcprsp->rspResId),
3498                                  fcpi_parm, cmnd->cmnd[0], scsi_status);
3499                 switch (scsi_status) {
3500                 case SAM_STAT_GOOD:
3501                 case SAM_STAT_CHECK_CONDITION:
3502                         /* Fabric dropped a data frame. Fail any successful
3503                          * command in which we detected dropped frames.
3504                          * A status of good or some check conditions could
3505                          * be considered a successful command.
3506                          */
3507                         host_status = DID_ERROR;
3508                         break;
3509                 }
3510                 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3511         }
3512
3513  out:
3514         cmnd->result = ScsiResult(host_status, scsi_status);
3515         lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
3516 }
3517
3518 /**
3519  * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
3520  * @phba: The Hba for which this call is being executed.
3521  * @pIocbIn: The command IOCBQ for the scsi cmnd.
3522  * @pIocbOut: The response IOCBQ for the scsi cmnd.
3523  *
3524  * This routine assigns scsi command result by looking into response IOCB
3525  * status field appropriately. This routine handles QUEUE FULL condition as
3526  * well by ramping down device queue depth.
3527  **/
3528 static void
3529 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3530                         struct lpfc_iocbq *pIocbOut)
3531 {
3532         struct lpfc_scsi_buf *lpfc_cmd =
3533                 (struct lpfc_scsi_buf *) pIocbIn->context1;
3534         struct lpfc_vport      *vport = pIocbIn->vport;
3535         struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3536         struct lpfc_nodelist *pnode = rdata->pnode;
3537         struct scsi_cmnd *cmd;
3538         int result;
3539         struct scsi_device *tmp_sdev;
3540         int depth;
3541         unsigned long flags;
3542         struct lpfc_fast_path_event *fast_path_evt;
3543         struct Scsi_Host *shost;
3544         uint32_t queue_depth, scsi_id;
3545         uint32_t logit = LOG_FCP;
3546
3547         /* Sanity check on return of outstanding command */
3548         if (!(lpfc_cmd->pCmd))
3549                 return;
3550         cmd = lpfc_cmd->pCmd;
3551         shost = cmd->device->host;
3552
3553         lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
3554         lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
3555         /* pick up SLI4 exhange busy status from HBA */
3556         lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3557
3558 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3559         if (lpfc_cmd->prot_data_type) {
3560                 struct scsi_dif_tuple *src = NULL;
3561
3562                 src =  (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3563                 /*
3564                  * Used to restore any changes to protection
3565                  * data for error injection.
3566                  */
3567                 switch (lpfc_cmd->prot_data_type) {
3568                 case LPFC_INJERR_REFTAG:
3569                         src->ref_tag =
3570                                 lpfc_cmd->prot_data;
3571                         break;
3572                 case LPFC_INJERR_APPTAG:
3573                         src->app_tag =
3574                                 (uint16_t)lpfc_cmd->prot_data;
3575                         break;
3576                 case LPFC_INJERR_GUARD:
3577                         src->guard_tag =
3578                                 (uint16_t)lpfc_cmd->prot_data;
3579                         break;
3580                 default:
3581                         break;
3582                 }
3583
3584                 lpfc_cmd->prot_data = 0;
3585                 lpfc_cmd->prot_data_type = 0;
3586                 lpfc_cmd->prot_data_segment = NULL;
3587         }
3588 #endif
3589         if (pnode && NLP_CHK_NODE_ACT(pnode))
3590                 atomic_dec(&pnode->cmd_pending);
3591
3592         if (lpfc_cmd->status) {
3593                 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3594                     (lpfc_cmd->result & IOERR_DRVR_MASK))
3595                         lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3596                 else if (lpfc_cmd->status >= IOSTAT_CNT)
3597                         lpfc_cmd->status = IOSTAT_DEFAULT;
3598                 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
3599                         && !lpfc_cmd->fcp_rsp->rspStatus3
3600                         && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
3601                         && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
3602                         logit = 0;
3603                 else
3604                         logit = LOG_FCP | LOG_FCP_UNDER;
3605                 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3606                          "9030 FCP cmd x%x failed <%d/%d> "
3607                          "status: x%x result: x%x Data: x%x x%x\n",
3608                          cmd->cmnd[0],
3609                          cmd->device ? cmd->device->id : 0xffff,
3610                          cmd->device ? cmd->device->lun : 0xffff,
3611                          lpfc_cmd->status, lpfc_cmd->result,
3612                          pIocbOut->iocb.ulpContext,
3613                          lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
3614
3615                 switch (lpfc_cmd->status) {
3616                 case IOSTAT_FCP_RSP_ERROR:
3617                         /* Call FCP RSP handler to determine result */
3618                         lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
3619                         break;
3620                 case IOSTAT_NPORT_BSY:
3621                 case IOSTAT_FABRIC_BSY:
3622                         cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
3623                         fast_path_evt = lpfc_alloc_fast_evt(phba);
3624                         if (!fast_path_evt)
3625                                 break;
3626                         fast_path_evt->un.fabric_evt.event_type =
3627                                 FC_REG_FABRIC_EVENT;
3628                         fast_path_evt->un.fabric_evt.subcategory =
3629                                 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3630                                 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3631                         if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3632                                 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3633                                         &pnode->nlp_portname,
3634                                         sizeof(struct lpfc_name));
3635                                 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3636                                         &pnode->nlp_nodename,
3637                                         sizeof(struct lpfc_name));
3638                         }
3639                         fast_path_evt->vport = vport;
3640                         fast_path_evt->work_evt.evt =
3641                                 LPFC_EVT_FASTPATH_MGMT_EVT;
3642                         spin_lock_irqsave(&phba->hbalock, flags);
3643                         list_add_tail(&fast_path_evt->work_evt.evt_listp,
3644                                 &phba->work_list);
3645                         spin_unlock_irqrestore(&phba->hbalock, flags);
3646                         lpfc_worker_wake_up(phba);
3647                         break;
3648                 case IOSTAT_LOCAL_REJECT:
3649                 case IOSTAT_REMOTE_STOP:
3650                         if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3651                             lpfc_cmd->result ==
3652                                         IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3653                             lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3654                             lpfc_cmd->result ==
3655                                         IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3656                                 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
3657                                 break;
3658                         }
3659                         if (lpfc_cmd->result == IOERR_INVALID_RPI ||
3660                             lpfc_cmd->result == IOERR_NO_RESOURCES ||
3661                             lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3662                             lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
3663                                 cmd->result = ScsiResult(DID_REQUEUE, 0);
3664                                 break;
3665                         }
3666                         if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3667                              lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3668                              pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3669                                 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3670                                         /*
3671                                          * This is a response for a BG enabled
3672                                          * cmd. Parse BG error
3673                                          */
3674                                         lpfc_parse_bg_err(phba, lpfc_cmd,
3675                                                         pIocbOut);
3676                                         break;
3677                                 } else {
3678                                         lpfc_printf_vlog(vport, KERN_WARNING,
3679                                                         LOG_BG,
3680                                                         "9031 non-zero BGSTAT "
3681                                                         "on unprotected cmd\n");
3682                                 }
3683                         }
3684                         if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3685                                 && (phba->sli_rev == LPFC_SLI_REV4)
3686                                 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3687                                 /* This IO was aborted by the target, we don't
3688                                  * know the rxid and because we did not send the
3689                                  * ABTS we cannot generate and RRQ.
3690                                  */
3691                                 lpfc_set_rrq_active(phba, pnode,
3692                                                 lpfc_cmd->cur_iocbq.sli4_xritag,
3693                                                 0, 0);
3694                         }
3695                 /* else: fall through */
3696                 default:
3697                         cmd->result = ScsiResult(DID_ERROR, 0);
3698                         break;
3699                 }
3700
3701                 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
3702                     || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3703                         cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
3704                                                  SAM_STAT_BUSY);
3705         } else
3706                 cmd->result = ScsiResult(DID_OK, 0);
3707
3708         if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3709                 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3710
3711                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3712                                  "0710 Iodone <%d/%d> cmd %p, error "
3713                                  "x%x SNS x%x x%x Data: x%x x%x\n",
3714                                  cmd->device->id, cmd->device->lun, cmd,
3715                                  cmd->result, *lp, *(lp + 3), cmd->retries,
3716                                  scsi_get_resid(cmd));
3717         }
3718
3719         lpfc_update_stats(phba, lpfc_cmd);
3720         result = cmd->result;
3721         if (vport->cfg_max_scsicmpl_time &&
3722            time_after(jiffies, lpfc_cmd->start_time +
3723                 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
3724                 spin_lock_irqsave(shost->host_lock, flags);
3725                 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3726                         if (pnode->cmd_qdepth >
3727                                 atomic_read(&pnode->cmd_pending) &&
3728                                 (atomic_read(&pnode->cmd_pending) >
3729                                 LPFC_MIN_TGT_QDEPTH) &&
3730                                 ((cmd->cmnd[0] == READ_10) ||
3731                                 (cmd->cmnd[0] == WRITE_10)))
3732                                 pnode->cmd_qdepth =
3733                                         atomic_read(&pnode->cmd_pending);
3734
3735                         pnode->last_change_time = jiffies;
3736                 }
3737                 spin_unlock_irqrestore(shost->host_lock, flags);
3738         } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3739                 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
3740                    time_after(jiffies, pnode->last_change_time +
3741                               msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
3742                         spin_lock_irqsave(shost->host_lock, flags);
3743                         depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
3744                                 / 100;
3745                         depth = depth ? depth : 1;
3746                         pnode->cmd_qdepth += depth;
3747                         if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
3748                                 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
3749                         pnode->last_change_time = jiffies;
3750                         spin_unlock_irqrestore(shost->host_lock, flags);
3751                 }
3752         }
3753
3754         lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
3755
3756         /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3757         queue_depth = cmd->device->queue_depth;
3758         scsi_id = cmd->device->id;
3759         cmd->scsi_done(cmd);
3760
3761         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3762                 /*
3763                  * If there is a thread waiting for command completion
3764                  * wake up the thread.
3765                  */
3766                 spin_lock_irqsave(shost->host_lock, flags);
3767                 lpfc_cmd->pCmd = NULL;
3768                 if (lpfc_cmd->waitq)
3769                         wake_up(lpfc_cmd->waitq);
3770                 spin_unlock_irqrestore(shost->host_lock, flags);
3771                 lpfc_release_scsi_buf(phba, lpfc_cmd);
3772                 return;
3773         }
3774
3775         if (!result)
3776                 lpfc_rampup_queue_depth(vport, queue_depth);
3777
3778         /*
3779          * Check for queue full.  If the lun is reporting queue full, then
3780          * back off the lun queue depth to prevent target overloads.
3781          */
3782         if (result == SAM_STAT_TASK_SET_FULL && pnode &&
3783             NLP_CHK_NODE_ACT(pnode)) {
3784                 shost_for_each_device(tmp_sdev, shost) {
3785                         if (tmp_sdev->id != scsi_id)
3786                                 continue;
3787                         depth = scsi_track_queue_full(tmp_sdev,
3788                                                       tmp_sdev->queue_depth-1);
3789                         if (depth <= 0)
3790                                 continue;
3791                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3792                                          "0711 detected queue full - lun queue "
3793                                          "depth adjusted to %d.\n", depth);
3794                         lpfc_send_sdev_queuedepth_change_event(phba, vport,
3795                                                                pnode,
3796                                                                tmp_sdev->lun,
3797                                                                depth+1, depth);
3798                 }
3799         }
3800
3801         /*
3802          * If there is a thread waiting for command completion
3803          * wake up the thread.
3804          */
3805         spin_lock_irqsave(shost->host_lock, flags);
3806         lpfc_cmd->pCmd = NULL;
3807         if (lpfc_cmd->waitq)
3808                 wake_up(lpfc_cmd->waitq);
3809         spin_unlock_irqrestore(shost->host_lock, flags);
3810
3811         lpfc_release_scsi_buf(phba, lpfc_cmd);
3812 }
3813
3814 /**
3815  * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
3816  * @data: A pointer to the immediate command data portion of the IOCB.
3817  * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3818  *
3819  * The routine copies the entire FCP command from @fcp_cmnd to @data while
3820  * byte swapping the data to big endian format for transmission on the wire.
3821  **/
3822 static void
3823 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3824 {
3825         int i, j;
3826         for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3827              i += sizeof(uint32_t), j++) {
3828                 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3829         }
3830 }
3831
3832 /**
3833  * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
3834  * @vport: The virtual port for which this call is being executed.
3835  * @lpfc_cmd: The scsi command which needs to send.
3836  * @pnode: Pointer to lpfc_nodelist.
3837  *
3838  * This routine initializes fcp_cmnd and iocb data structure from scsi command
3839  * to transfer for device with SLI3 interface spec.
3840  **/
3841 static void
3842 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3843                     struct lpfc_nodelist *pnode)
3844 {
3845         struct lpfc_hba *phba = vport->phba;
3846         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3847         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3848         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3849         struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
3850         int datadir = scsi_cmnd->sc_data_direction;
3851         char tag[2];
3852
3853         if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3854                 return;
3855
3856         lpfc_cmd->fcp_rsp->rspSnsLen = 0;
3857         /* clear task management bits */
3858         lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
3859
3860         int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3861                         &lpfc_cmd->fcp_cmnd->fcp_lun);
3862
3863         memset(&fcp_cmnd->fcpCdb[0], 0, LPFC_FCP_CDB_LEN);
3864         memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
3865         if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
3866                 switch (tag[0]) {
3867                 case HEAD_OF_QUEUE_TAG:
3868                         fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
3869                         break;
3870                 case ORDERED_QUEUE_TAG:
3871                         fcp_cmnd->fcpCntl1 = ORDERED_Q;
3872                         break;
3873                 default:
3874                         fcp_cmnd->fcpCntl1 = SIMPLE_Q;
3875                         break;
3876                 }
3877         } else
3878                 fcp_cmnd->fcpCntl1 = 0;
3879
3880         /*
3881          * There are three possibilities here - use scatter-gather segment, use
3882          * the single mapping, or neither.  Start the lpfc command prep by
3883          * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3884          * data bde entry.
3885          */
3886         if (scsi_sg_count(scsi_cmnd)) {
3887                 if (datadir == DMA_TO_DEVICE) {
3888                         iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
3889                         if (phba->sli_rev < LPFC_SLI_REV4) {
3890                                 iocb_cmd->un.fcpi.fcpi_parm = 0;
3891                                 iocb_cmd->ulpPU = 0;
3892                         } else
3893                                 iocb_cmd->ulpPU = PARM_READ_CHECK;
3894                         fcp_cmnd->fcpCntl3 = WRITE_DATA;
3895                         phba->fc4OutputRequests++;
3896                 } else {
3897                         iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
3898                         iocb_cmd->ulpPU = PARM_READ_CHECK;
3899                         fcp_cmnd->fcpCntl3 = READ_DATA;
3900                         phba->fc4InputRequests++;
3901                 }
3902         } else {
3903                 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
3904                 iocb_cmd->un.fcpi.fcpi_parm = 0;
3905                 iocb_cmd->ulpPU = 0;
3906                 fcp_cmnd->fcpCntl3 = 0;
3907                 phba->fc4ControlRequests++;
3908         }
3909         if (phba->sli_rev == 3 &&
3910             !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
3911                 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
3912         /*
3913          * Finish initializing those IOCB fields that are independent
3914          * of the scsi_cmnd request_buffer
3915          */
3916         piocbq->iocb.ulpContext = pnode->nlp_rpi;
3917         if (phba->sli_rev == LPFC_SLI_REV4)
3918                 piocbq->iocb.ulpContext =
3919                   phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
3920         if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
3921                 piocbq->iocb.ulpFCP2Rcvy = 1;
3922         else
3923                 piocbq->iocb.ulpFCP2Rcvy = 0;
3924
3925         piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
3926         piocbq->context1  = lpfc_cmd;
3927         piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
3928         piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
3929         piocbq->vport = vport;
3930 }
3931
3932 /**
3933  * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
3934  * @vport: The virtual port for which this call is being executed.
3935  * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3936  * @lun: Logical unit number.
3937  * @task_mgmt_cmd: SCSI task management command.
3938  *
3939  * This routine creates FCP information unit corresponding to @task_mgmt_cmd
3940  * for device with SLI-3 interface spec.
3941  *
3942  * Return codes:
3943  *   0 - Error
3944  *   1 - Success
3945  **/
3946 static int
3947 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
3948                              struct lpfc_scsi_buf *lpfc_cmd,
3949                              unsigned int lun,
3950                              uint8_t task_mgmt_cmd)
3951 {
3952         struct lpfc_iocbq *piocbq;
3953         IOCB_t *piocb;
3954         struct fcp_cmnd *fcp_cmnd;
3955         struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3956         struct lpfc_nodelist *ndlp = rdata->pnode;
3957
3958         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3959             ndlp->nlp_state != NLP_STE_MAPPED_NODE)
3960                 return 0;
3961
3962         piocbq = &(lpfc_cmd->cur_iocbq);
3963         piocbq->vport = vport;
3964
3965         piocb = &piocbq->iocb;
3966
3967         fcp_cmnd = lpfc_cmd->fcp_cmnd;
3968         /* Clear out any old data in the FCP command area */
3969         memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
3970         int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
3971         fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
3972         if (vport->phba->sli_rev == 3 &&
3973             !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
3974                 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
3975         piocb->ulpCommand = CMD_FCP_ICMND64_CR;
3976         piocb->ulpContext = ndlp->nlp_rpi;
3977         if (vport->phba->sli_rev == LPFC_SLI_REV4) {
3978                 piocb->ulpContext =
3979                   vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
3980         }
3981         if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
3982                 piocb->ulpFCP2Rcvy = 1;
3983         }
3984         piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
3985
3986         /* ulpTimeout is only one byte */
3987         if (lpfc_cmd->timeout > 0xff) {
3988                 /*
3989                  * Do not timeout the command at the firmware level.
3990                  * The driver will provide the timeout mechanism.
3991                  */
3992                 piocb->ulpTimeout = 0;
3993         } else
3994                 piocb->ulpTimeout = lpfc_cmd->timeout;
3995
3996         if (vport->phba->sli_rev == LPFC_SLI_REV4)
3997                 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
3998
3999         return 1;
4000 }
4001
4002 /**
4003  * lpfc_scsi_api_table_setup - Set up scsi api function jump table
4004  * @phba: The hba struct for which this call is being executed.
4005  * @dev_grp: The HBA PCI-Device group number.
4006  *
4007  * This routine sets up the SCSI interface API function jump table in @phba
4008  * struct.
4009  * Returns: 0 - success, -ENODEV - failure.
4010  **/
4011 int
4012 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4013 {
4014
4015         phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4016         phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
4017
4018         switch (dev_grp) {
4019         case LPFC_PCI_DEV_LP:
4020                 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4021                 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
4022                 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
4023                 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
4024                 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
4025                 break;
4026         case LPFC_PCI_DEV_OC:
4027                 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4028                 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
4029                 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
4030                 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
4031                 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
4032                 break;
4033         default:
4034                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4035                                 "1418 Invalid HBA PCI-device group: 0x%x\n",
4036                                 dev_grp);
4037                 return -ENODEV;
4038                 break;
4039         }
4040         phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
4041         phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4042         return 0;
4043 }
4044
4045 /**
4046  * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
4047  * @phba: The Hba for which this call is being executed.
4048  * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4049  * @rspiocbq: Pointer to lpfc_iocbq data structure.
4050  *
4051  * This routine is IOCB completion routine for device reset and target reset
4052  * routine. This routine release scsi buffer associated with lpfc_cmd.
4053  **/
4054 static void
4055 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4056                         struct lpfc_iocbq *cmdiocbq,
4057                         struct lpfc_iocbq *rspiocbq)
4058 {
4059         struct lpfc_scsi_buf *lpfc_cmd =
4060                 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4061         if (lpfc_cmd)
4062                 lpfc_release_scsi_buf(phba, lpfc_cmd);
4063         return;
4064 }
4065
4066 /**
4067  * lpfc_info - Info entry point of scsi_host_template data structure
4068  * @host: The scsi host for which this call is being executed.
4069  *
4070  * This routine provides module information about hba.
4071  *
4072  * Reutrn code:
4073  *   Pointer to char - Success.
4074  **/
4075 const char *
4076 lpfc_info(struct Scsi_Host *host)
4077 {
4078         struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4079         struct lpfc_hba   *phba = vport->phba;
4080         int len;
4081         static char  lpfcinfobuf[384];
4082
4083         memset(lpfcinfobuf,0,384);
4084         if (phba && phba->pcidev){
4085                 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4086                 len = strlen(lpfcinfobuf);
4087                 snprintf(lpfcinfobuf + len,
4088                         384-len,
4089                         " on PCI bus %02x device %02x irq %d",
4090                         phba->pcidev->bus->number,
4091                         phba->pcidev->devfn,
4092                         phba->pcidev->irq);
4093                 len = strlen(lpfcinfobuf);
4094                 if (phba->Port[0]) {
4095                         snprintf(lpfcinfobuf + len,
4096                                  384-len,
4097                                  " port %s",
4098                                  phba->Port);
4099                 }
4100                 len = strlen(lpfcinfobuf);
4101                 if (phba->sli4_hba.link_state.logical_speed) {
4102                         snprintf(lpfcinfobuf + len,
4103                                  384-len,
4104                                  " Logical Link Speed: %d Mbps",
4105                                  phba->sli4_hba.link_state.logical_speed * 10);
4106                 }
4107         }
4108         return lpfcinfobuf;
4109 }
4110
4111 /**
4112  * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
4113  * @phba: The Hba for which this call is being executed.
4114  *
4115  * This routine modifies fcp_poll_timer  field of @phba by cfg_poll_tmo.
4116  * The default value of cfg_poll_tmo is 10 milliseconds.
4117  **/
4118 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4119 {
4120         unsigned long  poll_tmo_expires =
4121                 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4122
4123         if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
4124                 mod_timer(&phba->fcp_poll_timer,
4125                           poll_tmo_expires);
4126 }
4127
4128 /**
4129  * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
4130  * @phba: The Hba for which this call is being executed.
4131  *
4132  * This routine starts the fcp_poll_timer of @phba.
4133  **/
4134 void lpfc_poll_start_timer(struct lpfc_hba * phba)
4135 {
4136         lpfc_poll_rearm_timer(phba);
4137 }
4138
4139 /**
4140  * lpfc_poll_timeout - Restart polling timer
4141  * @ptr: Map to lpfc_hba data structure pointer.
4142  *
4143  * This routine restarts fcp_poll timer, when FCP ring  polling is enable
4144  * and FCP Ring interrupt is disable.
4145  **/
4146
4147 void lpfc_poll_timeout(unsigned long ptr)
4148 {
4149         struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4150
4151         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4152                 lpfc_sli_handle_fast_ring_event(phba,
4153                         &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4154
4155                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4156                         lpfc_poll_rearm_timer(phba);
4157         }
4158 }
4159
4160 /**
4161  * lpfc_queuecommand - scsi_host_template queuecommand entry point
4162  * @cmnd: Pointer to scsi_cmnd data structure.
4163  * @done: Pointer to done routine.
4164  *
4165  * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4166  * This routine prepares an IOCB from scsi command and provides to firmware.
4167  * The @done callback is invoked after driver finished processing the command.
4168  *
4169  * Return value :
4170  *   0 - Success
4171  *   SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4172  **/
4173 static int
4174 lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
4175 {
4176         struct Scsi_Host  *shost = cmnd->device->host;
4177         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4178         struct lpfc_hba   *phba = vport->phba;
4179         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
4180         struct lpfc_nodelist *ndlp;
4181         struct lpfc_scsi_buf *lpfc_cmd;
4182         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
4183         int err;
4184
4185         err = fc_remote_port_chkready(rport);
4186         if (err) {
4187                 cmnd->result = err;
4188                 goto out_fail_command;
4189         }
4190         ndlp = rdata->pnode;
4191
4192         if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
4193                 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
4194
4195                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4196                                 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4197                                 " op:%02x str=%s without registering for"
4198                                 " BlockGuard - Rejecting command\n",
4199                                 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4200                                 dif_op_str[scsi_get_prot_op(cmnd)]);
4201                 goto out_fail_command;
4202         }
4203
4204         /*
4205          * Catch race where our node has transitioned, but the
4206          * transport is still transitioning.
4207          */
4208         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
4209                 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
4210                 goto out_fail_command;
4211         }
4212         if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
4213                 goto out_tgt_busy;
4214
4215         lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
4216         if (lpfc_cmd == NULL) {
4217                 lpfc_rampdown_queue_depth(phba);
4218
4219                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4220                                  "0707 driver's buffer pool is empty, "
4221                                  "IO busied\n");
4222                 goto out_host_busy;
4223         }
4224
4225         /*
4226          * Store the midlayer's command structure for the completion phase
4227          * and complete the command initialization.
4228          */
4229         lpfc_cmd->pCmd  = cmnd;
4230         lpfc_cmd->rdata = rdata;
4231         lpfc_cmd->timeout = 0;
4232         lpfc_cmd->start_time = jiffies;
4233         cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4234         cmnd->scsi_done = done;
4235
4236         if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
4237                 if (vport->phba->cfg_enable_bg) {
4238                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4239                                 "9033 BLKGRD: rcvd protected cmd:%02x op=%s "
4240                                 "guard=%s\n", cmnd->cmnd[0],
4241                                 dif_op_str[scsi_get_prot_op(cmnd)],
4242                                 dif_grd_str[scsi_host_get_guard(shost)]);
4243                         if (cmnd->cmnd[0] == READ_10)
4244                                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4245                                         "9035 BLKGRD: READ @ sector %llu, "
4246                                         "cnt %u, rpt %d\n",
4247                                         (unsigned long long)scsi_get_lba(cmnd),
4248                                         blk_rq_sectors(cmnd->request),
4249                                         (cmnd->cmnd[1]>>5));
4250                         else if (cmnd->cmnd[0] == WRITE_10)
4251                                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4252                                         "9036 BLKGRD: WRITE @ sector %llu, "
4253                                         "cnt %u, wpt %d\n",
4254                                         (unsigned long long)scsi_get_lba(cmnd),
4255                                         blk_rq_sectors(cmnd->request),
4256                                         (cmnd->cmnd[1]>>5));
4257                 }
4258
4259                 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4260         } else {
4261                 if (vport->phba->cfg_enable_bg) {
4262                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4263                                 "9038 BLKGRD: rcvd unprotected cmd:"
4264                                 "%02x op=%s guard=%s\n", cmnd->cmnd[0],
4265                                 dif_op_str[scsi_get_prot_op(cmnd)],
4266                                 dif_grd_str[scsi_host_get_guard(shost)]);
4267                         if (cmnd->cmnd[0] == READ_10)
4268                                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4269                                         "9040 dbg: READ @ sector %llu, "
4270                                         "cnt %u, rpt %d\n",
4271                                         (unsigned long long)scsi_get_lba(cmnd),
4272                                          blk_rq_sectors(cmnd->request),
4273                                         (cmnd->cmnd[1]>>5));
4274                         else if (cmnd->cmnd[0] == WRITE_10)
4275                                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4276                                         "9041 dbg: WRITE @ sector %llu, "
4277                                         "cnt %u, wpt %d\n",
4278                                         (unsigned long long)scsi_get_lba(cmnd),
4279                                         blk_rq_sectors(cmnd->request),
4280                                         (cmnd->cmnd[1]>>5));
4281                 }
4282                 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4283         }
4284
4285         if (err)
4286                 goto out_host_busy_free_buf;
4287
4288         lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
4289
4290         atomic_inc(&ndlp->cmd_pending);
4291         err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4292                                   &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
4293         if (err) {
4294                 atomic_dec(&ndlp->cmd_pending);
4295                 goto out_host_busy_free_buf;
4296         }
4297         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4298                 spin_unlock(shost->host_lock);
4299                 lpfc_sli_handle_fast_ring_event(phba,
4300                         &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4301
4302                 spin_lock(shost->host_lock);
4303                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4304                         lpfc_poll_rearm_timer(phba);
4305         }
4306
4307         return 0;
4308
4309  out_host_busy_free_buf:
4310         lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4311         lpfc_release_scsi_buf(phba, lpfc_cmd);
4312  out_host_busy:
4313         return SCSI_MLQUEUE_HOST_BUSY;
4314
4315  out_tgt_busy:
4316         return SCSI_MLQUEUE_TARGET_BUSY;
4317
4318  out_fail_command:
4319         done(cmnd);
4320         return 0;
4321 }
4322
4323 static DEF_SCSI_QCMD(lpfc_queuecommand)
4324
4325 /**
4326  * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
4327  * @cmnd: Pointer to scsi_cmnd data structure.
4328  *
4329  * This routine aborts @cmnd pending in base driver.
4330  *
4331  * Return code :
4332  *   0x2003 - Error
4333  *   0x2002 - Success
4334  **/
4335 static int
4336 lpfc_abort_handler(struct scsi_cmnd *cmnd)
4337 {
4338         struct Scsi_Host  *shost = cmnd->device->host;
4339         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4340         struct lpfc_hba   *phba = vport->phba;
4341         struct lpfc_iocbq *iocb;
4342         struct lpfc_iocbq *abtsiocb;
4343         struct lpfc_scsi_buf *lpfc_cmd;
4344         IOCB_t *cmd, *icmd;
4345         int ret = SUCCESS;
4346         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
4347
4348         ret = fc_block_scsi_eh(cmnd);
4349         if (ret)
4350                 return ret;
4351         lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
4352         if (!lpfc_cmd) {
4353                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4354                          "2873 SCSI Layer I/O Abort Request IO CMPL Status "
4355                          "x%x ID %d LUN %d\n",
4356                          ret, cmnd->device->id, cmnd->device->lun);
4357                 return SUCCESS;
4358         }
4359
4360         /*
4361          * If pCmd field of the corresponding lpfc_scsi_buf structure
4362          * points to a different SCSI command, then the driver has
4363          * already completed this command, but the midlayer did not
4364          * see the completion before the eh fired.  Just return
4365          * SUCCESS.
4366          */
4367         iocb = &lpfc_cmd->cur_iocbq;
4368         if (lpfc_cmd->pCmd != cmnd)
4369                 goto out;
4370
4371         BUG_ON(iocb->context1 != lpfc_cmd);
4372
4373         abtsiocb = lpfc_sli_get_iocbq(phba);
4374         if (abtsiocb == NULL) {
4375                 ret = FAILED;
4376                 goto out;
4377         }
4378
4379         /*
4380          * The scsi command can not be in txq and it is in flight because the
4381          * pCmd is still pointig at the SCSI command we have to abort. There
4382          * is no need to search the txcmplq. Just send an abort to the FW.
4383          */
4384
4385         cmd = &iocb->iocb;
4386         icmd = &abtsiocb->iocb;
4387         icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4388         icmd->un.acxri.abortContextTag = cmd->ulpContext;
4389         if (phba->sli_rev == LPFC_SLI_REV4)
4390                 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4391         else
4392                 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
4393
4394         icmd->ulpLe = 1;
4395         icmd->ulpClass = cmd->ulpClass;
4396
4397         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4398         abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
4399         abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
4400
4401         if (lpfc_is_link_up(phba))
4402                 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4403         else
4404                 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
4405
4406         abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
4407         abtsiocb->vport = vport;
4408         if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4409             IOCB_ERROR) {
4410                 lpfc_sli_release_iocbq(phba, abtsiocb);
4411                 ret = FAILED;
4412                 goto out;
4413         }
4414
4415         if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4416                 lpfc_sli_handle_fast_ring_event(phba,
4417                         &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4418
4419         lpfc_cmd->waitq = &waitq;
4420         /* Wait for abort to complete */
4421         wait_event_timeout(waitq,
4422                           (lpfc_cmd->pCmd != cmnd),
4423                            (2*vport->cfg_devloss_tmo*HZ));
4424
4425         spin_lock_irq(shost->host_lock);
4426         lpfc_cmd->waitq = NULL;
4427         spin_unlock_irq(shost->host_lock);
4428
4429         if (lpfc_cmd->pCmd == cmnd) {
4430                 ret = FAILED;
4431                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4432                                  "0748 abort handler timed out waiting "
4433                                  "for abort to complete: ret %#x, ID %d, "
4434                                  "LUN %d\n",
4435                                  ret, cmnd->device->id, cmnd->device->lun);
4436         }
4437
4438  out:
4439         lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4440                          "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
4441                          "LUN %d\n", ret, cmnd->device->id,
4442                          cmnd->device->lun);
4443         return ret;
4444 }
4445
4446 static char *
4447 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4448 {
4449         switch (task_mgmt_cmd) {
4450         case FCP_ABORT_TASK_SET:
4451                 return "ABORT_TASK_SET";
4452         case FCP_CLEAR_TASK_SET:
4453                 return "FCP_CLEAR_TASK_SET";
4454         case FCP_BUS_RESET:
4455                 return "FCP_BUS_RESET";
4456         case FCP_LUN_RESET:
4457                 return "FCP_LUN_RESET";
4458         case FCP_TARGET_RESET:
4459                 return "FCP_TARGET_RESET";
4460         case FCP_CLEAR_ACA:
4461                 return "FCP_CLEAR_ACA";
4462         case FCP_TERMINATE_TASK:
4463                 return "FCP_TERMINATE_TASK";
4464         default:
4465                 return "unknown";
4466         }
4467 }
4468
4469 /**
4470  * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4471  * @vport: The virtual port for which this call is being executed.
4472  * @rdata: Pointer to remote port local data
4473  * @tgt_id: Target ID of remote device.
4474  * @lun_id: Lun number for the TMF
4475  * @task_mgmt_cmd: type of TMF to send
4476  *
4477  * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4478  * a remote port.
4479  *
4480  * Return Code:
4481  *   0x2003 - Error
4482  *   0x2002 - Success.
4483  **/
4484 static int
4485 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4486                     unsigned  tgt_id, unsigned int lun_id,
4487                     uint8_t task_mgmt_cmd)
4488 {
4489         struct lpfc_hba   *phba = vport->phba;
4490         struct lpfc_scsi_buf *lpfc_cmd;
4491         struct lpfc_iocbq *iocbq;
4492         struct lpfc_iocbq *iocbqrsp;
4493         struct lpfc_nodelist *pnode = rdata->pnode;
4494         int ret;
4495         int status;
4496
4497         if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4498                 return FAILED;
4499
4500         lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
4501         if (lpfc_cmd == NULL)
4502                 return FAILED;
4503         lpfc_cmd->timeout = 60;
4504         lpfc_cmd->rdata = rdata;
4505
4506         status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4507                                            task_mgmt_cmd);
4508         if (!status) {
4509                 lpfc_release_scsi_buf(phba, lpfc_cmd);
4510                 return FAILED;
4511         }
4512
4513         iocbq = &lpfc_cmd->cur_iocbq;
4514         iocbqrsp = lpfc_sli_get_iocbq(phba);
4515         if (iocbqrsp == NULL) {
4516                 lpfc_release_scsi_buf(phba, lpfc_cmd);
4517                 return FAILED;
4518         }
4519
4520         lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4521                          "0702 Issue %s to TGT %d LUN %d "
4522                          "rpi x%x nlp_flag x%x Data: x%x x%x\n",
4523                          lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
4524                          pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4525                          iocbq->iocb_flag);
4526
4527         status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4528                                           iocbq, iocbqrsp, lpfc_cmd->timeout);
4529         if (status != IOCB_SUCCESS) {
4530                 if (status == IOCB_TIMEDOUT) {
4531                         iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4532                         ret = TIMEOUT_ERROR;
4533                 } else
4534                         ret = FAILED;
4535                 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4536                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4537                          "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4538                          "iocb_flag x%x\n",
4539                          lpfc_taskmgmt_name(task_mgmt_cmd),
4540                          tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
4541                          iocbqrsp->iocb.un.ulpWord[4],
4542                          iocbq->iocb_flag);
4543         } else if (status == IOCB_BUSY)
4544                 ret = FAILED;
4545         else
4546                 ret = SUCCESS;
4547
4548         lpfc_sli_release_iocbq(phba, iocbqrsp);
4549
4550         if (ret != TIMEOUT_ERROR)
4551                 lpfc_release_scsi_buf(phba, lpfc_cmd);
4552
4553         return ret;
4554 }
4555
4556 /**
4557  * lpfc_chk_tgt_mapped -
4558  * @vport: The virtual port to check on
4559  * @cmnd: Pointer to scsi_cmnd data structure.
4560  *
4561  * This routine delays until the scsi target (aka rport) for the
4562  * command exists (is present and logged in) or we declare it non-existent.
4563  *
4564  * Return code :
4565  *  0x2003 - Error
4566  *  0x2002 - Success
4567  **/
4568 static int
4569 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4570 {
4571         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
4572         struct lpfc_nodelist *pnode;
4573         unsigned long later;
4574
4575         if (!rdata) {
4576                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4577                         "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4578                 return FAILED;
4579         }
4580         pnode = rdata->pnode;
4581         /*
4582          * If target is not in a MAPPED state, delay until
4583          * target is rediscovered or devloss timeout expires.
4584          */
4585         later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4586         while (time_after(later, jiffies)) {
4587                 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4588                         return FAILED;
4589                 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4590                         return SUCCESS;
4591                 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
4592                 rdata = cmnd->device->hostdata;
4593                 if (!rdata)
4594                         return FAILED;
4595                 pnode = rdata->pnode;
4596         }
4597         if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4598             (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4599                 return FAILED;
4600         return SUCCESS;
4601 }
4602
4603 /**
4604  * lpfc_reset_flush_io_context -
4605  * @vport: The virtual port (scsi_host) for the flush context
4606  * @tgt_id: If aborting by Target contect - specifies the target id
4607  * @lun_id: If aborting by Lun context - specifies the lun id
4608  * @context: specifies the context level to flush at.
4609  *
4610  * After a reset condition via TMF, we need to flush orphaned i/o
4611  * contexts from the adapter. This routine aborts any contexts
4612  * outstanding, then waits for their completions. The wait is
4613  * bounded by devloss_tmo though.
4614  *
4615  * Return code :
4616  *  0x2003 - Error
4617  *  0x2002 - Success
4618  **/
4619 static int
4620 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4621                         uint64_t lun_id, lpfc_ctx_cmd context)
4622 {
4623         struct lpfc_hba   *phba = vport->phba;
4624         unsigned long later;
4625         int cnt;
4626
4627         cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4628         if (cnt)
4629                 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
4630                                     tgt_id, lun_id, context);
4631         later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4632         while (time_after(later, jiffies) && cnt) {
4633                 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
4634                 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4635         }
4636         if (cnt) {
4637                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4638                         "0724 I/O flush failure for context %s : cnt x%x\n",
4639                         ((context == LPFC_CTX_LUN) ? "LUN" :
4640                          ((context == LPFC_CTX_TGT) ? "TGT" :
4641                           ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
4642                         cnt);
4643                 return FAILED;
4644         }
4645         return SUCCESS;
4646 }
4647
4648 /**
4649  * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
4650  * @cmnd: Pointer to scsi_cmnd data structure.
4651  *
4652  * This routine does a device reset by sending a LUN_RESET task management
4653  * command.
4654  *
4655  * Return code :
4656  *  0x2003 - Error
4657  *  0x2002 - Success
4658  **/
4659 static int
4660 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
4661 {
4662         struct Scsi_Host  *shost = cmnd->device->host;
4663         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4664         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
4665         struct lpfc_nodelist *pnode;
4666         unsigned tgt_id = cmnd->device->id;
4667         unsigned int lun_id = cmnd->device->lun;
4668         struct lpfc_scsi_event_header scsi_event;
4669         int status;
4670
4671         if (!rdata) {
4672                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4673                         "0798 Device Reset rport failure: rdata x%p\n", rdata);
4674                 return FAILED;
4675         }
4676         pnode = rdata->pnode;
4677         status = fc_block_scsi_eh(cmnd);
4678         if (status)
4679                 return status;
4680
4681         status = lpfc_chk_tgt_mapped(vport, cmnd);
4682         if (status == FAILED) {
4683                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4684                         "0721 Device Reset rport failure: rdata x%p\n", rdata);
4685                 return FAILED;
4686         }
4687
4688         scsi_event.event_type = FC_REG_SCSI_EVENT;
4689         scsi_event.subcategory = LPFC_EVENT_LUNRESET;
4690         scsi_event.lun = lun_id;
4691         memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4692         memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4693
4694         fc_host_post_vendor_event(shost, fc_get_event_number(),
4695                 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4696
4697         status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4698                                                 FCP_LUN_RESET);
4699
4700         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4701                          "0713 SCSI layer issued Device Reset (%d, %d) "
4702                          "return x%x\n", tgt_id, lun_id, status);
4703
4704         /*
4705          * We have to clean up i/o as : they may be orphaned by the TMF;
4706          * or if the TMF failed, they may be in an indeterminate state.
4707          * So, continue on.
4708          * We will report success if all the i/o aborts successfully.
4709          */
4710         status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4711                                                 LPFC_CTX_LUN);
4712         return status;
4713 }
4714
4715 /**
4716  * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
4717  * @cmnd: Pointer to scsi_cmnd data structure.
4718  *
4719  * This routine does a target reset by sending a TARGET_RESET task management
4720  * command.
4721  *
4722  * Return code :
4723  *  0x2003 - Error
4724  *  0x2002 - Success
4725  **/
4726 static int
4727 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
4728 {
4729         struct Scsi_Host  *shost = cmnd->device->host;
4730         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4731         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
4732         struct lpfc_nodelist *pnode;
4733         unsigned tgt_id = cmnd->device->id;
4734         unsigned int lun_id = cmnd->device->lun;
4735         struct lpfc_scsi_event_header scsi_event;
4736         int status;
4737
4738         if (!rdata) {
4739                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4740                         "0799 Target Reset rport failure: rdata x%p\n", rdata);
4741                 return FAILED;
4742         }
4743         pnode = rdata->pnode;
4744         status = fc_block_scsi_eh(cmnd);
4745         if (status)
4746                 return status;
4747
4748         status = lpfc_chk_tgt_mapped(vport, cmnd);
4749         if (status == FAILED) {
4750                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4751                         "0722 Target Reset rport failure: rdata x%p\n", rdata);
4752                 return FAILED;
4753         }
4754
4755         scsi_event.event_type = FC_REG_SCSI_EVENT;
4756         scsi_event.subcategory = LPFC_EVENT_TGTRESET;
4757         scsi_event.lun = 0;
4758         memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4759         memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4760
4761         fc_host_post_vendor_event(shost, fc_get_event_number(),
4762                 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4763
4764         status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4765                                         FCP_TARGET_RESET);
4766
4767         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4768                          "0723 SCSI layer issued Target Reset (%d, %d) "
4769                          "return x%x\n", tgt_id, lun_id, status);
4770
4771         /*
4772          * We have to clean up i/o as : they may be orphaned by the TMF;
4773          * or if the TMF failed, they may be in an indeterminate state.
4774          * So, continue on.
4775          * We will report success if all the i/o aborts successfully.
4776          */
4777         status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4778                                         LPFC_CTX_TGT);
4779         return status;
4780 }
4781
4782 /**
4783  * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
4784  * @cmnd: Pointer to scsi_cmnd data structure.
4785  *
4786  * This routine does target reset to all targets on @cmnd->device->host.
4787  * This emulates Parallel SCSI Bus Reset Semantics.
4788  *
4789  * Return code :
4790  *  0x2003 - Error
4791  *  0x2002 - Success
4792  **/
4793 static int
4794 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
4795 {
4796         struct Scsi_Host  *shost = cmnd->device->host;
4797         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4798         struct lpfc_nodelist *ndlp = NULL;
4799         struct lpfc_scsi_event_header scsi_event;
4800         int match;
4801         int ret = SUCCESS, status, i;
4802
4803         scsi_event.event_type = FC_REG_SCSI_EVENT;
4804         scsi_event.subcategory = LPFC_EVENT_BUSRESET;
4805         scsi_event.lun = 0;
4806         memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
4807         memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
4808
4809         fc_host_post_vendor_event(shost, fc_get_event_number(),
4810                 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4811
4812         status = fc_block_scsi_eh(cmnd);
4813         if (status)
4814                 return status;
4815
4816         /*
4817          * Since the driver manages a single bus device, reset all
4818          * targets known to the driver.  Should any target reset
4819          * fail, this routine returns failure to the midlayer.
4820          */
4821         for (i = 0; i < LPFC_MAX_TARGET; i++) {
4822                 /* Search for mapped node by target ID */
4823                 match = 0;
4824                 spin_lock_irq(shost->host_lock);
4825                 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4826                         if (!NLP_CHK_NODE_ACT(ndlp))
4827                                 continue;
4828                         if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
4829                             ndlp->nlp_sid == i &&
4830                             ndlp->rport) {
4831                                 match = 1;
4832                                 break;
4833                         }
4834                 }
4835                 spin_unlock_irq(shost->host_lock);
4836                 if (!match)
4837                         continue;
4838
4839                 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
4840                                         i, 0, FCP_TARGET_RESET);
4841
4842                 if (status != SUCCESS) {
4843                         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4844                                          "0700 Bus Reset on target %d failed\n",
4845                                          i);
4846                         ret = FAILED;
4847                 }
4848         }
4849         /*
4850          * We have to clean up i/o as : they may be orphaned by the TMFs
4851          * above; or if any of the TMFs failed, they may be in an
4852          * indeterminate state.
4853          * We will report success if all the i/o aborts successfully.
4854          */
4855
4856         status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
4857         if (status != SUCCESS)
4858                 ret = FAILED;
4859
4860         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4861                          "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
4862         return ret;
4863 }
4864
4865 /**
4866  * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
4867  * @sdev: Pointer to scsi_device.
4868  *
4869  * This routine populates the cmds_per_lun count + 2 scsi_bufs into  this host's
4870  * globally available list of scsi buffers. This routine also makes sure scsi
4871  * buffer is not allocated more than HBA limit conveyed to midlayer. This list
4872  * of scsi buffer exists for the lifetime of the driver.
4873  *
4874  * Return codes:
4875  *   non-0 - Error
4876  *   0 - Success
4877  **/
4878 static int
4879 lpfc_slave_alloc(struct scsi_device *sdev)
4880 {
4881         struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4882         struct lpfc_hba   *phba = vport->phba;
4883         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
4884         uint32_t total = 0;
4885         uint32_t num_to_alloc = 0;
4886         int num_allocated = 0;
4887         uint32_t sdev_cnt;
4888
4889         if (!rport || fc_remote_port_chkready(rport))
4890                 return -ENXIO;
4891
4892         sdev->hostdata = rport->dd_data;
4893         sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
4894
4895         /*
4896          * Populate the cmds_per_lun count scsi_bufs into this host's globally
4897          * available list of scsi buffers.  Don't allocate more than the
4898          * HBA limit conveyed to the midlayer via the host structure.  The
4899          * formula accounts for the lun_queue_depth + error handlers + 1
4900          * extra.  This list of scsi bufs exists for the lifetime of the driver.
4901          */
4902         total = phba->total_scsi_bufs;
4903         num_to_alloc = vport->cfg_lun_queue_depth + 2;
4904
4905         /* If allocated buffers are enough do nothing */
4906         if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
4907                 return 0;
4908
4909         /* Allow some exchanges to be available always to complete discovery */
4910         if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
4911                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4912                                  "0704 At limitation of %d preallocated "
4913                                  "command buffers\n", total);
4914                 return 0;
4915         /* Allow some exchanges to be available always to complete discovery */
4916         } else if (total + num_to_alloc >
4917                 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
4918                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4919                                  "0705 Allocation request of %d "
4920                                  "command buffers will exceed max of %d.  "
4921                                  "Reducing allocation request to %d.\n",
4922                                  num_to_alloc, phba->cfg_hba_queue_depth,
4923                                  (phba->cfg_hba_queue_depth - total));
4924                 num_to_alloc = phba->cfg_hba_queue_depth - total;
4925         }
4926         num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
4927         if (num_to_alloc != num_allocated) {
4928                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4929                                  "0708 Allocation request of %d "
4930                                  "command buffers did not succeed.  "
4931                                  "Allocated %d buffers.\n",
4932                                  num_to_alloc, num_allocated);
4933         }
4934         if (num_allocated > 0)
4935                 phba->total_scsi_bufs += num_allocated;
4936         return 0;
4937 }
4938
4939 /**
4940  * lpfc_slave_configure - scsi_host_template slave_configure entry point
4941  * @sdev: Pointer to scsi_device.
4942  *
4943  * This routine configures following items
4944  *   - Tag command queuing support for @sdev if supported.
4945  *   - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
4946  *
4947  * Return codes:
4948  *   0 - Success
4949  **/
4950 static int
4951 lpfc_slave_configure(struct scsi_device *sdev)
4952 {
4953         struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4954         struct lpfc_hba   *phba = vport->phba;
4955
4956         if (sdev->tagged_supported)
4957                 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
4958         else
4959                 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
4960
4961         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4962                 lpfc_sli_handle_fast_ring_event(phba,
4963                         &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4964                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4965                         lpfc_poll_rearm_timer(phba);
4966         }
4967
4968         return 0;
4969 }
4970
4971 /**
4972  * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
4973  * @sdev: Pointer to scsi_device.
4974  *
4975  * This routine sets @sdev hostatdata filed to null.
4976  **/
4977 static void
4978 lpfc_slave_destroy(struct scsi_device *sdev)
4979 {
4980         struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4981         struct lpfc_hba   *phba = vport->phba;
4982         atomic_dec(&phba->sdev_cnt);
4983         sdev->hostdata = NULL;
4984         return;
4985 }
4986
4987
4988 struct scsi_host_template lpfc_template = {
4989         .module                 = THIS_MODULE,
4990         .name                   = LPFC_DRIVER_NAME,
4991         .info                   = lpfc_info,
4992         .queuecommand           = lpfc_queuecommand,
4993         .eh_abort_handler       = lpfc_abort_handler,
4994         .eh_device_reset_handler = lpfc_device_reset_handler,
4995         .eh_target_reset_handler = lpfc_target_reset_handler,
4996         .eh_bus_reset_handler   = lpfc_bus_reset_handler,
4997         .slave_alloc            = lpfc_slave_alloc,
4998         .slave_configure        = lpfc_slave_configure,
4999         .slave_destroy          = lpfc_slave_destroy,
5000         .scan_finished          = lpfc_scan_finished,
5001         .this_id                = -1,
5002         .sg_tablesize           = LPFC_DEFAULT_SG_SEG_CNT,
5003         .cmd_per_lun            = LPFC_CMD_PER_LUN,
5004         .use_clustering         = ENABLE_CLUSTERING,
5005         .shost_attrs            = lpfc_hba_attrs,
5006         .max_sectors            = 0xFFFF,
5007         .vendor_id              = LPFC_NL_VENDOR_ID,
5008         .change_queue_depth     = lpfc_change_queue_depth,
5009 };
5010
5011 struct scsi_host_template lpfc_vport_template = {
5012         .module                 = THIS_MODULE,
5013         .name                   = LPFC_DRIVER_NAME,
5014         .info                   = lpfc_info,
5015         .queuecommand           = lpfc_queuecommand,
5016         .eh_abort_handler       = lpfc_abort_handler,
5017         .eh_device_reset_handler = lpfc_device_reset_handler,
5018         .eh_target_reset_handler = lpfc_target_reset_handler,
5019         .eh_bus_reset_handler   = lpfc_bus_reset_handler,
5020         .slave_alloc            = lpfc_slave_alloc,
5021         .slave_configure        = lpfc_slave_configure,
5022         .slave_destroy          = lpfc_slave_destroy,
5023         .scan_finished          = lpfc_scan_finished,
5024         .this_id                = -1,
5025         .sg_tablesize           = LPFC_DEFAULT_SG_SEG_CNT,
5026         .cmd_per_lun            = LPFC_CMD_PER_LUN,
5027         .use_clustering         = ENABLE_CLUSTERING,
5028         .shost_attrs            = lpfc_vport_attrs,
5029         .max_sectors            = 0xFFFF,
5030         .change_queue_depth     = lpfc_change_queue_depth,
5031 };