bf80cdefb5066a53333078e3a0719282c12d8a18
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_scsi.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2009 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/interrupt.h>
23 #include <linux/delay.h>
24 #include <asm/unaligned.h>
25
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_eh.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi_transport_fc.h>
32
33 #include "lpfc_version.h"
34 #include "lpfc_hw4.h"
35 #include "lpfc_hw.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_nl.h"
39 #include "lpfc_disc.h"
40 #include "lpfc_scsi.h"
41 #include "lpfc.h"
42 #include "lpfc_logmsg.h"
43 #include "lpfc_crtn.h"
44 #include "lpfc_vport.h"
45
46 #define LPFC_RESET_WAIT  2
47 #define LPFC_ABORT_WAIT  2
48
49 int _dump_buf_done;
50
51 static char *dif_op_str[] = {
52         "SCSI_PROT_NORMAL",
53         "SCSI_PROT_READ_INSERT",
54         "SCSI_PROT_WRITE_STRIP",
55         "SCSI_PROT_READ_STRIP",
56         "SCSI_PROT_WRITE_INSERT",
57         "SCSI_PROT_READ_PASS",
58         "SCSI_PROT_WRITE_PASS",
59 };
60 static void
61 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
62
63 static void
64 lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
65 {
66         void *src, *dst;
67         struct scatterlist *sgde = scsi_sglist(cmnd);
68
69         if (!_dump_buf_data) {
70                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
71                         "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
72                                 __func__);
73                 return;
74         }
75
76
77         if (!sgde) {
78                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
79                         "9051 BLKGRD: ERROR: data scatterlist is null\n");
80                 return;
81         }
82
83         dst = (void *) _dump_buf_data;
84         while (sgde) {
85                 src = sg_virt(sgde);
86                 memcpy(dst, src, sgde->length);
87                 dst += sgde->length;
88                 sgde = sg_next(sgde);
89         }
90 }
91
92 static void
93 lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
94 {
95         void *src, *dst;
96         struct scatterlist *sgde = scsi_prot_sglist(cmnd);
97
98         if (!_dump_buf_dif) {
99                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
100                         "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
101                                 __func__);
102                 return;
103         }
104
105         if (!sgde) {
106                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
107                         "9053 BLKGRD: ERROR: prot scatterlist is null\n");
108                 return;
109         }
110
111         dst = _dump_buf_dif;
112         while (sgde) {
113                 src = sg_virt(sgde);
114                 memcpy(dst, src, sgde->length);
115                 dst += sgde->length;
116                 sgde = sg_next(sgde);
117         }
118 }
119
120 /**
121  * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
122  * @phba: Pointer to HBA object.
123  * @lpfc_cmd: lpfc scsi command object pointer.
124  *
125  * This function is called from the lpfc_prep_task_mgmt_cmd function to
126  * set the last bit in the response sge entry.
127  **/
128 static void
129 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
130                                 struct lpfc_scsi_buf *lpfc_cmd)
131 {
132         struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
133         if (sgl) {
134                 sgl += 1;
135                 sgl->word2 = le32_to_cpu(sgl->word2);
136                 bf_set(lpfc_sli4_sge_last, sgl, 1);
137                 sgl->word2 = cpu_to_le32(sgl->word2);
138         }
139 }
140
141 /**
142  * lpfc_update_stats - Update statistical data for the command completion
143  * @phba: Pointer to HBA object.
144  * @lpfc_cmd: lpfc scsi command object pointer.
145  *
146  * This function is called when there is a command completion and this
147  * function updates the statistical data for the command completion.
148  **/
149 static void
150 lpfc_update_stats(struct lpfc_hba *phba, struct  lpfc_scsi_buf *lpfc_cmd)
151 {
152         struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
153         struct lpfc_nodelist *pnode = rdata->pnode;
154         struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
155         unsigned long flags;
156         struct Scsi_Host  *shost = cmd->device->host;
157         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
158         unsigned long latency;
159         int i;
160
161         if (cmd->result)
162                 return;
163
164         latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
165
166         spin_lock_irqsave(shost->host_lock, flags);
167         if (!vport->stat_data_enabled ||
168                 vport->stat_data_blocked ||
169                 !pnode->lat_data ||
170                 (phba->bucket_type == LPFC_NO_BUCKET)) {
171                 spin_unlock_irqrestore(shost->host_lock, flags);
172                 return;
173         }
174
175         if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
176                 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
177                         phba->bucket_step;
178                 /* check array subscript bounds */
179                 if (i < 0)
180                         i = 0;
181                 else if (i >= LPFC_MAX_BUCKET_COUNT)
182                         i = LPFC_MAX_BUCKET_COUNT - 1;
183         } else {
184                 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
185                         if (latency <= (phba->bucket_base +
186                                 ((1<<i)*phba->bucket_step)))
187                                 break;
188         }
189
190         pnode->lat_data[i].cmd_count++;
191         spin_unlock_irqrestore(shost->host_lock, flags);
192 }
193
194 /**
195  * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
196  * @phba: Pointer to HBA context object.
197  * @vport: Pointer to vport object.
198  * @ndlp: Pointer to FC node associated with the target.
199  * @lun: Lun number of the scsi device.
200  * @old_val: Old value of the queue depth.
201  * @new_val: New value of the queue depth.
202  *
203  * This function sends an event to the mgmt application indicating
204  * there is a change in the scsi device queue depth.
205  **/
206 static void
207 lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
208                 struct lpfc_vport  *vport,
209                 struct lpfc_nodelist *ndlp,
210                 uint32_t lun,
211                 uint32_t old_val,
212                 uint32_t new_val)
213 {
214         struct lpfc_fast_path_event *fast_path_evt;
215         unsigned long flags;
216
217         fast_path_evt = lpfc_alloc_fast_evt(phba);
218         if (!fast_path_evt)
219                 return;
220
221         fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
222                 FC_REG_SCSI_EVENT;
223         fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
224                 LPFC_EVENT_VARQUEDEPTH;
225
226         /* Report all luns with change in queue depth */
227         fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
228         if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
229                 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
230                         &ndlp->nlp_portname, sizeof(struct lpfc_name));
231                 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
232                         &ndlp->nlp_nodename, sizeof(struct lpfc_name));
233         }
234
235         fast_path_evt->un.queue_depth_evt.oldval = old_val;
236         fast_path_evt->un.queue_depth_evt.newval = new_val;
237         fast_path_evt->vport = vport;
238
239         fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
240         spin_lock_irqsave(&phba->hbalock, flags);
241         list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
242         spin_unlock_irqrestore(&phba->hbalock, flags);
243         lpfc_worker_wake_up(phba);
244
245         return;
246 }
247
248 /**
249  * lpfc_change_queue_depth - Alter scsi device queue depth
250  * @sdev: Pointer the scsi device on which to change the queue depth.
251  * @qdepth: New queue depth to set the sdev to.
252  * @reason: The reason for the queue depth change.
253  *
254  * This function is called by the midlayer and the LLD to alter the queue
255  * depth for a scsi device. This function sets the queue depth to the new
256  * value and sends an event out to log the queue depth change.
257  **/
258 int
259 lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
260 {
261         struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
262         struct lpfc_hba   *phba = vport->phba;
263         struct lpfc_rport_data *rdata;
264         unsigned long new_queue_depth, old_queue_depth;
265
266         old_queue_depth = sdev->queue_depth;
267         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
268         new_queue_depth = sdev->queue_depth;
269         rdata = sdev->hostdata;
270         if (rdata)
271                 lpfc_send_sdev_queuedepth_change_event(phba, vport,
272                                                        rdata->pnode, sdev->lun,
273                                                        old_queue_depth,
274                                                        new_queue_depth);
275         return sdev->queue_depth;
276 }
277
278 /**
279  * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
280  * @phba: The Hba for which this call is being executed.
281  *
282  * This routine is called when there is resource error in driver or firmware.
283  * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
284  * posts at most 1 event each second. This routine wakes up worker thread of
285  * @phba to process WORKER_RAM_DOWN_EVENT event.
286  *
287  * This routine should be called with no lock held.
288  **/
289 void
290 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
291 {
292         unsigned long flags;
293         uint32_t evt_posted;
294
295         spin_lock_irqsave(&phba->hbalock, flags);
296         atomic_inc(&phba->num_rsrc_err);
297         phba->last_rsrc_error_time = jiffies;
298
299         if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
300                 spin_unlock_irqrestore(&phba->hbalock, flags);
301                 return;
302         }
303
304         phba->last_ramp_down_time = jiffies;
305
306         spin_unlock_irqrestore(&phba->hbalock, flags);
307
308         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
309         evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
310         if (!evt_posted)
311                 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
312         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
313
314         if (!evt_posted)
315                 lpfc_worker_wake_up(phba);
316         return;
317 }
318
319 /**
320  * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
321  * @phba: The Hba for which this call is being executed.
322  *
323  * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
324  * post at most 1 event every 5 minute after last_ramp_up_time or
325  * last_rsrc_error_time.  This routine wakes up worker thread of @phba
326  * to process WORKER_RAM_DOWN_EVENT event.
327  *
328  * This routine should be called with no lock held.
329  **/
330 static inline void
331 lpfc_rampup_queue_depth(struct lpfc_vport  *vport,
332                         uint32_t queue_depth)
333 {
334         unsigned long flags;
335         struct lpfc_hba *phba = vport->phba;
336         uint32_t evt_posted;
337         atomic_inc(&phba->num_cmd_success);
338
339         if (vport->cfg_lun_queue_depth <= queue_depth)
340                 return;
341         spin_lock_irqsave(&phba->hbalock, flags);
342         if (time_before(jiffies,
343                         phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
344             time_before(jiffies,
345                         phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
346                 spin_unlock_irqrestore(&phba->hbalock, flags);
347                 return;
348         }
349         phba->last_ramp_up_time = jiffies;
350         spin_unlock_irqrestore(&phba->hbalock, flags);
351
352         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
353         evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
354         if (!evt_posted)
355                 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
356         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
357
358         if (!evt_posted)
359                 lpfc_worker_wake_up(phba);
360         return;
361 }
362
363 /**
364  * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
365  * @phba: The Hba for which this call is being executed.
366  *
367  * This routine is called to  process WORKER_RAMP_DOWN_QUEUE event for worker
368  * thread.This routine reduces queue depth for all scsi device on each vport
369  * associated with @phba.
370  **/
371 void
372 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
373 {
374         struct lpfc_vport **vports;
375         struct Scsi_Host  *shost;
376         struct scsi_device *sdev;
377         unsigned long new_queue_depth;
378         unsigned long num_rsrc_err, num_cmd_success;
379         int i;
380
381         num_rsrc_err = atomic_read(&phba->num_rsrc_err);
382         num_cmd_success = atomic_read(&phba->num_cmd_success);
383
384         vports = lpfc_create_vport_work_array(phba);
385         if (vports != NULL)
386                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
387                         shost = lpfc_shost_from_vport(vports[i]);
388                         shost_for_each_device(sdev, shost) {
389                                 new_queue_depth =
390                                         sdev->queue_depth * num_rsrc_err /
391                                         (num_rsrc_err + num_cmd_success);
392                                 if (!new_queue_depth)
393                                         new_queue_depth = sdev->queue_depth - 1;
394                                 else
395                                         new_queue_depth = sdev->queue_depth -
396                                                                 new_queue_depth;
397                                 lpfc_change_queue_depth(sdev, new_queue_depth,
398                                                         SCSI_QDEPTH_DEFAULT);
399                         }
400                 }
401         lpfc_destroy_vport_work_array(phba, vports);
402         atomic_set(&phba->num_rsrc_err, 0);
403         atomic_set(&phba->num_cmd_success, 0);
404 }
405
406 /**
407  * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
408  * @phba: The Hba for which this call is being executed.
409  *
410  * This routine is called to  process WORKER_RAMP_UP_QUEUE event for worker
411  * thread.This routine increases queue depth for all scsi device on each vport
412  * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
413  * num_cmd_success to zero.
414  **/
415 void
416 lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
417 {
418         struct lpfc_vport **vports;
419         struct Scsi_Host  *shost;
420         struct scsi_device *sdev;
421         int i;
422
423         vports = lpfc_create_vport_work_array(phba);
424         if (vports != NULL)
425                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
426                         shost = lpfc_shost_from_vport(vports[i]);
427                         shost_for_each_device(sdev, shost) {
428                                 if (vports[i]->cfg_lun_queue_depth <=
429                                     sdev->queue_depth)
430                                         continue;
431                                 lpfc_change_queue_depth(sdev,
432                                                         sdev->queue_depth+1,
433                                                         SCSI_QDEPTH_RAMP_UP);
434                         }
435                 }
436         lpfc_destroy_vport_work_array(phba, vports);
437         atomic_set(&phba->num_rsrc_err, 0);
438         atomic_set(&phba->num_cmd_success, 0);
439 }
440
441 /**
442  * lpfc_scsi_dev_block - set all scsi hosts to block state
443  * @phba: Pointer to HBA context object.
444  *
445  * This function walks vport list and set each SCSI host to block state
446  * by invoking fc_remote_port_delete() routine. This function is invoked
447  * with EEH when device's PCI slot has been permanently disabled.
448  **/
449 void
450 lpfc_scsi_dev_block(struct lpfc_hba *phba)
451 {
452         struct lpfc_vport **vports;
453         struct Scsi_Host  *shost;
454         struct scsi_device *sdev;
455         struct fc_rport *rport;
456         int i;
457
458         vports = lpfc_create_vport_work_array(phba);
459         if (vports != NULL)
460                 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
461                         shost = lpfc_shost_from_vport(vports[i]);
462                         shost_for_each_device(sdev, shost) {
463                                 rport = starget_to_rport(scsi_target(sdev));
464                                 fc_remote_port_delete(rport);
465                         }
466                 }
467         lpfc_destroy_vport_work_array(phba, vports);
468 }
469
470 /**
471  * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
472  * @vport: The virtual port for which this call being executed.
473  * @num_to_allocate: The requested number of buffers to allocate.
474  *
475  * This routine allocates a scsi buffer for device with SLI-3 interface spec,
476  * the scsi buffer contains all the necessary information needed to initiate
477  * a SCSI I/O. The non-DMAable buffer region contains information to build
478  * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
479  * and the initial BPL. In addition to allocating memory, the FCP CMND and
480  * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
481  *
482  * Return codes:
483  *   int - number of scsi buffers that were allocated.
484  *   0 = failure, less than num_to_alloc is a partial failure.
485  **/
486 static int
487 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
488 {
489         struct lpfc_hba *phba = vport->phba;
490         struct lpfc_scsi_buf *psb;
491         struct ulp_bde64 *bpl;
492         IOCB_t *iocb;
493         dma_addr_t pdma_phys_fcp_cmd;
494         dma_addr_t pdma_phys_fcp_rsp;
495         dma_addr_t pdma_phys_bpl;
496         uint16_t iotag;
497         int bcnt;
498
499         for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
500                 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
501                 if (!psb)
502                         break;
503
504                 /*
505                  * Get memory from the pci pool to map the virt space to pci
506                  * bus space for an I/O.  The DMA buffer includes space for the
507                  * struct fcp_cmnd, struct fcp_rsp and the number of bde's
508                  * necessary to support the sg_tablesize.
509                  */
510                 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
511                                         GFP_KERNEL, &psb->dma_handle);
512                 if (!psb->data) {
513                         kfree(psb);
514                         break;
515                 }
516
517                 /* Initialize virtual ptrs to dma_buf region. */
518                 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
519
520                 /* Allocate iotag for psb->cur_iocbq. */
521                 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
522                 if (iotag == 0) {
523                         pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
524                                         psb->data, psb->dma_handle);
525                         kfree(psb);
526                         break;
527                 }
528                 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
529
530                 psb->fcp_cmnd = psb->data;
531                 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
532                 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
533                         sizeof(struct fcp_rsp);
534
535                 /* Initialize local short-hand pointers. */
536                 bpl = psb->fcp_bpl;
537                 pdma_phys_fcp_cmd = psb->dma_handle;
538                 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
539                 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
540                         sizeof(struct fcp_rsp);
541
542                 /*
543                  * The first two bdes are the FCP_CMD and FCP_RSP. The balance
544                  * are sg list bdes.  Initialize the first two and leave the
545                  * rest for queuecommand.
546                  */
547                 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
548                 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
549                 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
550                 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
551                 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
552
553                 /* Setup the physical region for the FCP RSP */
554                 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
555                 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
556                 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
557                 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
558                 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
559
560                 /*
561                  * Since the IOCB for the FCP I/O is built into this
562                  * lpfc_scsi_buf, initialize it with all known data now.
563                  */
564                 iocb = &psb->cur_iocbq.iocb;
565                 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
566                 if ((phba->sli_rev == 3) &&
567                                 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
568                         /* fill in immediate fcp command BDE */
569                         iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
570                         iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
571                         iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
572                                         unsli3.fcp_ext.icd);
573                         iocb->un.fcpi64.bdl.addrHigh = 0;
574                         iocb->ulpBdeCount = 0;
575                         iocb->ulpLe = 0;
576                         /* fill in responce BDE */
577                         iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
578                                                         BUFF_TYPE_BDE_64;
579                         iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
580                                 sizeof(struct fcp_rsp);
581                         iocb->unsli3.fcp_ext.rbde.addrLow =
582                                 putPaddrLow(pdma_phys_fcp_rsp);
583                         iocb->unsli3.fcp_ext.rbde.addrHigh =
584                                 putPaddrHigh(pdma_phys_fcp_rsp);
585                 } else {
586                         iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
587                         iocb->un.fcpi64.bdl.bdeSize =
588                                         (2 * sizeof(struct ulp_bde64));
589                         iocb->un.fcpi64.bdl.addrLow =
590                                         putPaddrLow(pdma_phys_bpl);
591                         iocb->un.fcpi64.bdl.addrHigh =
592                                         putPaddrHigh(pdma_phys_bpl);
593                         iocb->ulpBdeCount = 1;
594                         iocb->ulpLe = 1;
595                 }
596                 iocb->ulpClass = CLASS3;
597                 psb->status = IOSTAT_SUCCESS;
598                 /* Put it back into the SCSI buffer list */
599                 lpfc_release_scsi_buf_s4(phba, psb);
600
601         }
602
603         return bcnt;
604 }
605
606 /**
607  * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
608  * @phba: pointer to lpfc hba data structure.
609  * @axri: pointer to the fcp xri abort wcqe structure.
610  *
611  * This routine is invoked by the worker thread to process a SLI4 fast-path
612  * FCP aborted xri.
613  **/
614 void
615 lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
616                           struct sli4_wcqe_xri_aborted *axri)
617 {
618         uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
619         struct lpfc_scsi_buf *psb, *next_psb;
620         unsigned long iflag = 0;
621
622         spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock, iflag);
623         list_for_each_entry_safe(psb, next_psb,
624                 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
625                 if (psb->cur_iocbq.sli4_xritag == xri) {
626                         list_del(&psb->list);
627                         psb->status = IOSTAT_SUCCESS;
628                         spin_unlock_irqrestore(
629                                 &phba->sli4_hba.abts_scsi_buf_list_lock,
630                                 iflag);
631                         lpfc_release_scsi_buf_s4(phba, psb);
632                         return;
633                 }
634         }
635         spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
636                                 iflag);
637 }
638
639 /**
640  * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
641  * @phba: pointer to lpfc hba data structure.
642  *
643  * This routine walks the list of scsi buffers that have been allocated and
644  * repost them to the HBA by using SGL block post. This is needed after a
645  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
646  * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
647  * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
648  *
649  * Returns: 0 = success, non-zero failure.
650  **/
651 int
652 lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
653 {
654         struct lpfc_scsi_buf *psb;
655         int index, status, bcnt = 0, rcnt = 0, rc = 0;
656         LIST_HEAD(sblist);
657
658         for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
659                 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
660                 if (psb) {
661                         /* Remove from SCSI buffer list */
662                         list_del(&psb->list);
663                         /* Add it to a local SCSI buffer list */
664                         list_add_tail(&psb->list, &sblist);
665                         if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
666                                 bcnt = rcnt;
667                                 rcnt = 0;
668                         }
669                 } else
670                         /* A hole present in the XRI array, need to skip */
671                         bcnt = rcnt;
672
673                 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
674                         /* End of XRI array for SCSI buffer, complete */
675                         bcnt = rcnt;
676
677                 /* Continue until collect up to a nembed page worth of sgls */
678                 if (bcnt == 0)
679                         continue;
680                 /* Now, post the SCSI buffer list sgls as a block */
681                 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
682                 /* Reset SCSI buffer count for next round of posting */
683                 bcnt = 0;
684                 while (!list_empty(&sblist)) {
685                         list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
686                                          list);
687                         if (status) {
688                                 /* Put this back on the abort scsi list */
689                                 psb->status = IOSTAT_LOCAL_REJECT;
690                                 psb->result = IOERR_ABORT_REQUESTED;
691                                 rc++;
692                         } else
693                                 psb->status = IOSTAT_SUCCESS;
694                         /* Put it back into the SCSI buffer list */
695                         lpfc_release_scsi_buf_s4(phba, psb);
696                 }
697         }
698         return rc;
699 }
700
701 /**
702  * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
703  * @vport: The virtual port for which this call being executed.
704  * @num_to_allocate: The requested number of buffers to allocate.
705  *
706  * This routine allocates a scsi buffer for device with SLI-4 interface spec,
707  * the scsi buffer contains all the necessary information needed to initiate
708  * a SCSI I/O.
709  *
710  * Return codes:
711  *   int - number of scsi buffers that were allocated.
712  *   0 = failure, less than num_to_alloc is a partial failure.
713  **/
714 static int
715 lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
716 {
717         struct lpfc_hba *phba = vport->phba;
718         struct lpfc_scsi_buf *psb;
719         struct sli4_sge *sgl;
720         IOCB_t *iocb;
721         dma_addr_t pdma_phys_fcp_cmd;
722         dma_addr_t pdma_phys_fcp_rsp;
723         dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
724         uint16_t iotag, last_xritag = NO_XRI;
725         int status = 0, index;
726         int bcnt;
727         int non_sequential_xri = 0;
728         int rc = 0;
729         LIST_HEAD(sblist);
730
731         for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
732                 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
733                 if (!psb)
734                         break;
735
736                 /*
737                  * Get memory from the pci pool to map the virt space to pci bus
738                  * space for an I/O.  The DMA buffer includes space for the
739                  * struct fcp_cmnd, struct fcp_rsp and the number of bde's
740                  * necessary to support the sg_tablesize.
741                  */
742                 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
743                                                 GFP_KERNEL, &psb->dma_handle);
744                 if (!psb->data) {
745                         kfree(psb);
746                         break;
747                 }
748
749                 /* Initialize virtual ptrs to dma_buf region. */
750                 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
751
752                 /* Allocate iotag for psb->cur_iocbq. */
753                 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
754                 if (iotag == 0) {
755                         kfree(psb);
756                         break;
757                 }
758
759                 psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
760                 if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
761                         pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
762                               psb->data, psb->dma_handle);
763                         kfree(psb);
764                         break;
765                 }
766                 if (last_xritag != NO_XRI
767                         && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
768                         non_sequential_xri = 1;
769                 } else
770                         list_add_tail(&psb->list, &sblist);
771                 last_xritag = psb->cur_iocbq.sli4_xritag;
772
773                 index = phba->sli4_hba.scsi_xri_cnt++;
774                 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
775
776                 psb->fcp_bpl = psb->data;
777                 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
778                         - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
779                 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
780                                         sizeof(struct fcp_cmnd));
781
782                 /* Initialize local short-hand pointers. */
783                 sgl = (struct sli4_sge *)psb->fcp_bpl;
784                 pdma_phys_bpl = psb->dma_handle;
785                 pdma_phys_fcp_cmd =
786                         (psb->dma_handle + phba->cfg_sg_dma_buf_size)
787                          - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
788                 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
789
790                 /*
791                  * The first two bdes are the FCP_CMD and FCP_RSP.  The balance
792                  * are sg list bdes.  Initialize the first two and leave the
793                  * rest for queuecommand.
794                  */
795                 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
796                 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
797                 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_cmnd));
798                 bf_set(lpfc_sli4_sge_last, sgl, 0);
799                 sgl->word2 = cpu_to_le32(sgl->word2);
800                 sgl->word3 = cpu_to_le32(sgl->word3);
801                 sgl++;
802
803                 /* Setup the physical region for the FCP RSP */
804                 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
805                 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
806                 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_rsp));
807                 bf_set(lpfc_sli4_sge_last, sgl, 1);
808                 sgl->word2 = cpu_to_le32(sgl->word2);
809                 sgl->word3 = cpu_to_le32(sgl->word3);
810
811                 /*
812                  * Since the IOCB for the FCP I/O is built into this
813                  * lpfc_scsi_buf, initialize it with all known data now.
814                  */
815                 iocb = &psb->cur_iocbq.iocb;
816                 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
817                 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
818                 /* setting the BLP size to 2 * sizeof BDE may not be correct.
819                  * We are setting the bpl to point to out sgl. An sgl's
820                  * entries are 16 bytes, a bpl entries are 12 bytes.
821                  */
822                 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
823                 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
824                 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
825                 iocb->ulpBdeCount = 1;
826                 iocb->ulpLe = 1;
827                 iocb->ulpClass = CLASS3;
828                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
829                         pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
830                 else
831                         pdma_phys_bpl1 = 0;
832                 psb->dma_phys_bpl = pdma_phys_bpl;
833                 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
834                 if (non_sequential_xri) {
835                         status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
836                                                 pdma_phys_bpl1,
837                                                 psb->cur_iocbq.sli4_xritag);
838                         if (status) {
839                                 /* Put this back on the abort scsi list */
840                                 psb->status = IOSTAT_LOCAL_REJECT;
841                                 psb->result = IOERR_ABORT_REQUESTED;
842                                 rc++;
843                         } else
844                                 psb->status = IOSTAT_SUCCESS;
845                         /* Put it back into the SCSI buffer list */
846                         lpfc_release_scsi_buf_s4(phba, psb);
847                         break;
848                 }
849         }
850         if (bcnt) {
851                 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
852                 /* Reset SCSI buffer count for next round of posting */
853                 while (!list_empty(&sblist)) {
854                         list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
855                                  list);
856                         if (status) {
857                                 /* Put this back on the abort scsi list */
858                                 psb->status = IOSTAT_LOCAL_REJECT;
859                                 psb->result = IOERR_ABORT_REQUESTED;
860                                 rc++;
861                         } else
862                                 psb->status = IOSTAT_SUCCESS;
863                         /* Put it back into the SCSI buffer list */
864                         lpfc_release_scsi_buf_s4(phba, psb);
865                 }
866         }
867
868         return bcnt + non_sequential_xri - rc;
869 }
870
871 /**
872  * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
873  * @vport: The virtual port for which this call being executed.
874  * @num_to_allocate: The requested number of buffers to allocate.
875  *
876  * This routine wraps the actual SCSI buffer allocator function pointer from
877  * the lpfc_hba struct.
878  *
879  * Return codes:
880  *   int - number of scsi buffers that were allocated.
881  *   0 = failure, less than num_to_alloc is a partial failure.
882  **/
883 static inline int
884 lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
885 {
886         return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
887 }
888
889 /**
890  * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
891  * @phba: The HBA for which this call is being executed.
892  *
893  * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
894  * and returns to caller.
895  *
896  * Return codes:
897  *   NULL - Error
898  *   Pointer to lpfc_scsi_buf - Success
899  **/
900 static struct lpfc_scsi_buf*
901 lpfc_get_scsi_buf(struct lpfc_hba * phba)
902 {
903         struct  lpfc_scsi_buf * lpfc_cmd = NULL;
904         struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
905         unsigned long iflag = 0;
906
907         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
908         list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
909         if (lpfc_cmd) {
910                 lpfc_cmd->seg_cnt = 0;
911                 lpfc_cmd->nonsg_phys = 0;
912                 lpfc_cmd->prot_seg_cnt = 0;
913         }
914         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
915         return  lpfc_cmd;
916 }
917
918 /**
919  * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
920  * @phba: The Hba for which this call is being executed.
921  * @psb: The scsi buffer which is being released.
922  *
923  * This routine releases @psb scsi buffer by adding it to tail of @phba
924  * lpfc_scsi_buf_list list.
925  **/
926 static void
927 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
928 {
929         unsigned long iflag = 0;
930
931         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
932         psb->pCmd = NULL;
933         list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
934         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
935 }
936
937 /**
938  * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
939  * @phba: The Hba for which this call is being executed.
940  * @psb: The scsi buffer which is being released.
941  *
942  * This routine releases @psb scsi buffer by adding it to tail of @phba
943  * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
944  * and cannot be reused for at least RA_TOV amount of time if it was
945  * aborted.
946  **/
947 static void
948 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
949 {
950         unsigned long iflag = 0;
951
952         if (psb->status == IOSTAT_LOCAL_REJECT
953                 && psb->result == IOERR_ABORT_REQUESTED) {
954                 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
955                                         iflag);
956                 psb->pCmd = NULL;
957                 list_add_tail(&psb->list,
958                         &phba->sli4_hba.lpfc_abts_scsi_buf_list);
959                 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
960                                         iflag);
961         } else {
962
963                 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
964                 psb->pCmd = NULL;
965                 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
966                 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
967         }
968 }
969
970 /**
971  * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
972  * @phba: The Hba for which this call is being executed.
973  * @psb: The scsi buffer which is being released.
974  *
975  * This routine releases @psb scsi buffer by adding it to tail of @phba
976  * lpfc_scsi_buf_list list.
977  **/
978 static void
979 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
980 {
981
982         phba->lpfc_release_scsi_buf(phba, psb);
983 }
984
985 /**
986  * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
987  * @phba: The Hba for which this call is being executed.
988  * @lpfc_cmd: The scsi buffer which is going to be mapped.
989  *
990  * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
991  * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
992  * through sg elements and format the bdea. This routine also initializes all
993  * IOCB fields which are dependent on scsi command request buffer.
994  *
995  * Return codes:
996  *   1 - Error
997  *   0 - Success
998  **/
999 static int
1000 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1001 {
1002         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1003         struct scatterlist *sgel = NULL;
1004         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1005         struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1006         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1007         struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
1008         dma_addr_t physaddr;
1009         uint32_t num_bde = 0;
1010         int nseg, datadir = scsi_cmnd->sc_data_direction;
1011
1012         /*
1013          * There are three possibilities here - use scatter-gather segment, use
1014          * the single mapping, or neither.  Start the lpfc command prep by
1015          * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1016          * data bde entry.
1017          */
1018         bpl += 2;
1019         if (scsi_sg_count(scsi_cmnd)) {
1020                 /*
1021                  * The driver stores the segment count returned from pci_map_sg
1022                  * because this a count of dma-mappings used to map the use_sg
1023                  * pages.  They are not guaranteed to be the same for those
1024                  * architectures that implement an IOMMU.
1025                  */
1026
1027                 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1028                                   scsi_sg_count(scsi_cmnd), datadir);
1029                 if (unlikely(!nseg))
1030                         return 1;
1031
1032                 lpfc_cmd->seg_cnt = nseg;
1033                 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1034                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1035                                 "9064 BLKGRD: %s: Too many sg segments from "
1036                                "dma_map_sg.  Config %d, seg_cnt %d\n",
1037                                __func__, phba->cfg_sg_seg_cnt,
1038                                lpfc_cmd->seg_cnt);
1039                         scsi_dma_unmap(scsi_cmnd);
1040                         return 1;
1041                 }
1042
1043                 /*
1044                  * The driver established a maximum scatter-gather segment count
1045                  * during probe that limits the number of sg elements in any
1046                  * single scsi command.  Just run through the seg_cnt and format
1047                  * the bde's.
1048                  * When using SLI-3 the driver will try to fit all the BDEs into
1049                  * the IOCB. If it can't then the BDEs get added to a BPL as it
1050                  * does for SLI-2 mode.
1051                  */
1052                 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1053                         physaddr = sg_dma_address(sgel);
1054                         if (phba->sli_rev == 3 &&
1055                             !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1056                             nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1057                                 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1058                                 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1059                                 data_bde->addrLow = putPaddrLow(physaddr);
1060                                 data_bde->addrHigh = putPaddrHigh(physaddr);
1061                                 data_bde++;
1062                         } else {
1063                                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1064                                 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1065                                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1066                                 bpl->addrLow =
1067                                         le32_to_cpu(putPaddrLow(physaddr));
1068                                 bpl->addrHigh =
1069                                         le32_to_cpu(putPaddrHigh(physaddr));
1070                                 bpl++;
1071                         }
1072                 }
1073         }
1074
1075         /*
1076          * Finish initializing those IOCB fields that are dependent on the
1077          * scsi_cmnd request_buffer.  Note that for SLI-2 the bdeSize is
1078          * explicitly reinitialized and for SLI-3 the extended bde count is
1079          * explicitly reinitialized since all iocb memory resources are reused.
1080          */
1081         if (phba->sli_rev == 3 &&
1082             !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
1083                 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1084                         /*
1085                          * The extended IOCB format can only fit 3 BDE or a BPL.
1086                          * This I/O has more than 3 BDE so the 1st data bde will
1087                          * be a BPL that is filled in here.
1088                          */
1089                         physaddr = lpfc_cmd->dma_handle;
1090                         data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1091                         data_bde->tus.f.bdeSize = (num_bde *
1092                                                    sizeof(struct ulp_bde64));
1093                         physaddr += (sizeof(struct fcp_cmnd) +
1094                                      sizeof(struct fcp_rsp) +
1095                                      (2 * sizeof(struct ulp_bde64)));
1096                         data_bde->addrHigh = putPaddrHigh(physaddr);
1097                         data_bde->addrLow = putPaddrLow(physaddr);
1098                         /* ebde count includes the responce bde and data bpl */
1099                         iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1100                 } else {
1101                         /* ebde count includes the responce bde and data bdes */
1102                         iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1103                 }
1104         } else {
1105                 iocb_cmd->un.fcpi64.bdl.bdeSize =
1106                         ((num_bde + 2) * sizeof(struct ulp_bde64));
1107         }
1108         fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1109
1110         /*
1111          * Due to difference in data length between DIF/non-DIF paths,
1112          * we need to set word 4 of IOCB here
1113          */
1114         iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1115         return 0;
1116 }
1117
1118 /*
1119  * Given a scsi cmnd, determine the BlockGuard profile to be used
1120  * with the cmd
1121  */
1122 static int
1123 lpfc_sc_to_sli_prof(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1124 {
1125         uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1126         uint8_t ret_prof = LPFC_PROF_INVALID;
1127
1128         if (guard_type == SHOST_DIX_GUARD_IP) {
1129                 switch (scsi_get_prot_op(sc)) {
1130                 case SCSI_PROT_READ_INSERT:
1131                 case SCSI_PROT_WRITE_STRIP:
1132                         ret_prof = LPFC_PROF_AST2;
1133                         break;
1134
1135                 case SCSI_PROT_READ_STRIP:
1136                 case SCSI_PROT_WRITE_INSERT:
1137                         ret_prof = LPFC_PROF_A1;
1138                         break;
1139
1140                 case SCSI_PROT_READ_PASS:
1141                 case SCSI_PROT_WRITE_PASS:
1142                         ret_prof = LPFC_PROF_AST1;
1143                         break;
1144
1145                 case SCSI_PROT_NORMAL:
1146                 default:
1147                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1148                                 "9063 BLKGRD:Bad op/guard:%d/%d combination\n",
1149                                         scsi_get_prot_op(sc), guard_type);
1150                         break;
1151
1152                 }
1153         } else if (guard_type == SHOST_DIX_GUARD_CRC) {
1154                 switch (scsi_get_prot_op(sc)) {
1155                 case SCSI_PROT_READ_STRIP:
1156                 case SCSI_PROT_WRITE_INSERT:
1157                         ret_prof = LPFC_PROF_A1;
1158                         break;
1159
1160                 case SCSI_PROT_READ_PASS:
1161                 case SCSI_PROT_WRITE_PASS:
1162                         ret_prof = LPFC_PROF_C1;
1163                         break;
1164
1165                 case SCSI_PROT_READ_INSERT:
1166                 case SCSI_PROT_WRITE_STRIP:
1167                 case SCSI_PROT_NORMAL:
1168                 default:
1169                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1170                                 "9075 BLKGRD: Bad op/guard:%d/%d combination\n",
1171                                         scsi_get_prot_op(sc), guard_type);
1172                         break;
1173                 }
1174         } else {
1175                 /* unsupported format */
1176                 BUG();
1177         }
1178
1179         return ret_prof;
1180 }
1181
1182 struct scsi_dif_tuple {
1183         __be16 guard_tag;       /* Checksum */
1184         __be16 app_tag;         /* Opaque storage */
1185         __be32 ref_tag;         /* Target LBA or indirect LBA */
1186 };
1187
1188 static inline unsigned
1189 lpfc_cmd_blksize(struct scsi_cmnd *sc)
1190 {
1191         return sc->device->sector_size;
1192 }
1193
1194 /**
1195  * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1196  * @sc:             in: SCSI command
1197  * @apptagmask:     out: app tag mask
1198  * @apptagval:      out: app tag value
1199  * @reftag:         out: ref tag (reference tag)
1200  *
1201  * Description:
1202  *   Extract DIF parameters from the command if possible.  Otherwise,
1203  *   use default parameters.
1204  *
1205  **/
1206 static inline void
1207 lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1208                 uint16_t *apptagval, uint32_t *reftag)
1209 {
1210         struct  scsi_dif_tuple *spt;
1211         unsigned char op = scsi_get_prot_op(sc);
1212         unsigned int protcnt = scsi_prot_sg_count(sc);
1213         static int cnt;
1214
1215         if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
1216                                 op == SCSI_PROT_WRITE_PASS)) {
1217
1218                 cnt++;
1219                 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1220                         scsi_prot_sglist(sc)[0].offset;
1221                 *apptagmask = 0;
1222                 *apptagval = 0;
1223                 *reftag = cpu_to_be32(spt->ref_tag);
1224
1225         } else {
1226                 /* SBC defines ref tag to be lower 32bits of LBA */
1227                 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1228                 *apptagmask = 0;
1229                 *apptagval = 0;
1230         }
1231 }
1232
1233 /*
1234  * This function sets up buffer list for protection groups of
1235  * type LPFC_PG_TYPE_NO_DIF
1236  *
1237  * This is usually used when the HBA is instructed to generate
1238  * DIFs and insert them into data stream (or strip DIF from
1239  * incoming data stream)
1240  *
1241  * The buffer list consists of just one protection group described
1242  * below:
1243  *                                +-------------------------+
1244  *   start of prot group  -->     |          PDE_1          |
1245  *                                +-------------------------+
1246  *                                |         Data BDE        |
1247  *                                +-------------------------+
1248  *                                |more Data BDE's ... (opt)|
1249  *                                +-------------------------+
1250  *
1251  * @sc: pointer to scsi command we're working on
1252  * @bpl: pointer to buffer list for protection groups
1253  * @datacnt: number of segments of data that have been dma mapped
1254  *
1255  * Note: Data s/g buffers have been dma mapped
1256  */
1257 static int
1258 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1259                 struct ulp_bde64 *bpl, int datasegcnt)
1260 {
1261         struct scatterlist *sgde = NULL; /* s/g data entry */
1262         struct lpfc_pde *pde1 = NULL;
1263         dma_addr_t physaddr;
1264         int i = 0, num_bde = 0;
1265         int datadir = sc->sc_data_direction;
1266         int prof = LPFC_PROF_INVALID;
1267         unsigned blksize;
1268         uint32_t reftag;
1269         uint16_t apptagmask, apptagval;
1270
1271         pde1 = (struct lpfc_pde *) bpl;
1272         prof = lpfc_sc_to_sli_prof(phba, sc);
1273
1274         if (prof == LPFC_PROF_INVALID)
1275                 goto out;
1276
1277         /* extract some info from the scsi command for PDE1*/
1278         blksize = lpfc_cmd_blksize(sc);
1279         lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1280
1281         /* setup PDE1 with what we have */
1282         lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1283                         BG_EC_STOP_ERR);
1284         lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1285
1286         num_bde++;
1287         bpl++;
1288
1289         /* assumption: caller has already run dma_map_sg on command data */
1290         scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1291                 physaddr = sg_dma_address(sgde);
1292                 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1293                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1294                 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1295                 if (datadir == DMA_TO_DEVICE)
1296                         bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1297                 else
1298                         bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1299                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1300                 bpl++;
1301                 num_bde++;
1302         }
1303
1304 out:
1305         return num_bde;
1306 }
1307
1308 /*
1309  * This function sets up buffer list for protection groups of
1310  * type LPFC_PG_TYPE_DIF_BUF
1311  *
1312  * This is usually used when DIFs are in their own buffers,
1313  * separate from the data. The HBA can then by instructed
1314  * to place the DIFs in the outgoing stream.  For read operations,
1315  * The HBA could extract the DIFs and place it in DIF buffers.
1316  *
1317  * The buffer list for this type consists of one or more of the
1318  * protection groups described below:
1319  *                                    +-------------------------+
1320  *   start of first prot group  -->   |          PDE_1          |
1321  *                                    +-------------------------+
1322  *                                    |      PDE_3 (Prot BDE)   |
1323  *                                    +-------------------------+
1324  *                                    |        Data BDE         |
1325  *                                    +-------------------------+
1326  *                                    |more Data BDE's ... (opt)|
1327  *                                    +-------------------------+
1328  *   start of new  prot group  -->    |          PDE_1          |
1329  *                                    +-------------------------+
1330  *                                    |          ...            |
1331  *                                    +-------------------------+
1332  *
1333  * @sc: pointer to scsi command we're working on
1334  * @bpl: pointer to buffer list for protection groups
1335  * @datacnt: number of segments of data that have been dma mapped
1336  * @protcnt: number of segment of protection data that have been dma mapped
1337  *
1338  * Note: It is assumed that both data and protection s/g buffers have been
1339  *       mapped for DMA
1340  */
1341 static int
1342 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1343                 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1344 {
1345         struct scatterlist *sgde = NULL; /* s/g data entry */
1346         struct scatterlist *sgpe = NULL; /* s/g prot entry */
1347         struct lpfc_pde *pde1 = NULL;
1348         struct ulp_bde64 *prot_bde = NULL;
1349         dma_addr_t dataphysaddr, protphysaddr;
1350         unsigned short curr_data = 0, curr_prot = 0;
1351         unsigned int split_offset, protgroup_len;
1352         unsigned int protgrp_blks, protgrp_bytes;
1353         unsigned int remainder, subtotal;
1354         int prof = LPFC_PROF_INVALID;
1355         int datadir = sc->sc_data_direction;
1356         unsigned char pgdone = 0, alldone = 0;
1357         unsigned blksize;
1358         uint32_t reftag;
1359         uint16_t apptagmask, apptagval;
1360         int num_bde = 0;
1361
1362         sgpe = scsi_prot_sglist(sc);
1363         sgde = scsi_sglist(sc);
1364
1365         if (!sgpe || !sgde) {
1366                 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1367                                 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1368                                 sgpe, sgde);
1369                 return 0;
1370         }
1371
1372         prof = lpfc_sc_to_sli_prof(phba, sc);
1373         if (prof == LPFC_PROF_INVALID)
1374                 goto out;
1375
1376         /* extract some info from the scsi command for PDE1*/
1377         blksize = lpfc_cmd_blksize(sc);
1378         lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1379
1380         split_offset = 0;
1381         do {
1382                 /* setup the first PDE_1 */
1383                 pde1 = (struct lpfc_pde *) bpl;
1384
1385                 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1386                                 BG_EC_STOP_ERR);
1387                 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1388
1389                 num_bde++;
1390                 bpl++;
1391
1392                 /* setup the first BDE that points to protection buffer */
1393                 prot_bde = (struct ulp_bde64 *) bpl;
1394                 protphysaddr = sg_dma_address(sgpe);
1395                 prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1396                 prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1397                 protgroup_len = sg_dma_len(sgpe);
1398
1399
1400                 /* must be integer multiple of the DIF block length */
1401                 BUG_ON(protgroup_len % 8);
1402
1403                 protgrp_blks = protgroup_len / 8;
1404                 protgrp_bytes = protgrp_blks * blksize;
1405
1406                 prot_bde->tus.f.bdeSize = protgroup_len;
1407                 if (datadir == DMA_TO_DEVICE)
1408                         prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1409                 else
1410                         prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1411                 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1412
1413                 curr_prot++;
1414                 num_bde++;
1415
1416                 /* setup BDE's for data blocks associated with DIF data */
1417                 pgdone = 0;
1418                 subtotal = 0; /* total bytes processed for current prot grp */
1419                 while (!pgdone) {
1420                         if (!sgde) {
1421                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1422                                         "9065 BLKGRD:%s Invalid data segment\n",
1423                                                 __func__);
1424                                 return 0;
1425                         }
1426                         bpl++;
1427                         dataphysaddr = sg_dma_address(sgde) + split_offset;
1428                         bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1429                         bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1430
1431                         remainder = sg_dma_len(sgde) - split_offset;
1432
1433                         if ((subtotal + remainder) <= protgrp_bytes) {
1434                                 /* we can use this whole buffer */
1435                                 bpl->tus.f.bdeSize = remainder;
1436                                 split_offset = 0;
1437
1438                                 if ((subtotal + remainder) == protgrp_bytes)
1439                                         pgdone = 1;
1440                         } else {
1441                                 /* must split this buffer with next prot grp */
1442                                 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1443                                 split_offset += bpl->tus.f.bdeSize;
1444                         }
1445
1446                         subtotal += bpl->tus.f.bdeSize;
1447
1448                         if (datadir == DMA_TO_DEVICE)
1449                                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1450                         else
1451                                 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1452                         bpl->tus.w = le32_to_cpu(bpl->tus.w);
1453
1454                         num_bde++;
1455                         curr_data++;
1456
1457                         if (split_offset)
1458                                 break;
1459
1460                         /* Move to the next s/g segment if possible */
1461                         sgde = sg_next(sgde);
1462                 }
1463
1464                 /* are we done ? */
1465                 if (curr_prot == protcnt) {
1466                         alldone = 1;
1467                 } else if (curr_prot < protcnt) {
1468                         /* advance to next prot buffer */
1469                         sgpe = sg_next(sgpe);
1470                         bpl++;
1471
1472                         /* update the reference tag */
1473                         reftag += protgrp_blks;
1474                 } else {
1475                         /* if we're here, we have a bug */
1476                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1477                                 "9054 BLKGRD: bug in %s\n", __func__);
1478                 }
1479
1480         } while (!alldone);
1481
1482 out:
1483
1484
1485         return num_bde;
1486 }
1487 /*
1488  * Given a SCSI command that supports DIF, determine composition of protection
1489  * groups involved in setting up buffer lists
1490  *
1491  * Returns:
1492  *                            for DIF (for both read and write)
1493  * */
1494 static int
1495 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1496 {
1497         int ret = LPFC_PG_TYPE_INVALID;
1498         unsigned char op = scsi_get_prot_op(sc);
1499
1500         switch (op) {
1501         case SCSI_PROT_READ_STRIP:
1502         case SCSI_PROT_WRITE_INSERT:
1503                 ret = LPFC_PG_TYPE_NO_DIF;
1504                 break;
1505         case SCSI_PROT_READ_INSERT:
1506         case SCSI_PROT_WRITE_STRIP:
1507         case SCSI_PROT_READ_PASS:
1508         case SCSI_PROT_WRITE_PASS:
1509                 ret = LPFC_PG_TYPE_DIF_BUF;
1510                 break;
1511         default:
1512                 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1513                                 "9021 Unsupported protection op:%d\n", op);
1514                 break;
1515         }
1516
1517         return ret;
1518 }
1519
1520 /*
1521  * This is the protection/DIF aware version of
1522  * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1523  * two functions eventually, but for now, it's here
1524  */
1525 static int
1526 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1527                 struct lpfc_scsi_buf *lpfc_cmd)
1528 {
1529         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1530         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1531         struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1532         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1533         uint32_t num_bde = 0;
1534         int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1535         int prot_group_type = 0;
1536         int diflen, fcpdl;
1537         unsigned blksize;
1538
1539         /*
1540          * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1541          *  fcp_rsp regions to the first data bde entry
1542          */
1543         bpl += 2;
1544         if (scsi_sg_count(scsi_cmnd)) {
1545                 /*
1546                  * The driver stores the segment count returned from pci_map_sg
1547                  * because this a count of dma-mappings used to map the use_sg
1548                  * pages.  They are not guaranteed to be the same for those
1549                  * architectures that implement an IOMMU.
1550                  */
1551                 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1552                                         scsi_sglist(scsi_cmnd),
1553                                         scsi_sg_count(scsi_cmnd), datadir);
1554                 if (unlikely(!datasegcnt))
1555                         return 1;
1556
1557                 lpfc_cmd->seg_cnt = datasegcnt;
1558                 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1559                         lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1560                                         "9067 BLKGRD: %s: Too many sg segments"
1561                                         " from dma_map_sg.  Config %d, seg_cnt"
1562                                         " %d\n",
1563                                         __func__, phba->cfg_sg_seg_cnt,
1564                                         lpfc_cmd->seg_cnt);
1565                         scsi_dma_unmap(scsi_cmnd);
1566                         return 1;
1567                 }
1568
1569                 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1570
1571                 switch (prot_group_type) {
1572                 case LPFC_PG_TYPE_NO_DIF:
1573                         num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1574                                         datasegcnt);
1575                         /* we shoud have 2 or more entries in buffer list */
1576                         if (num_bde < 2)
1577                                 goto err;
1578                         break;
1579                 case LPFC_PG_TYPE_DIF_BUF:{
1580                         /*
1581                          * This type indicates that protection buffers are
1582                          * passed to the driver, so that needs to be prepared
1583                          * for DMA
1584                          */
1585                         protsegcnt = dma_map_sg(&phba->pcidev->dev,
1586                                         scsi_prot_sglist(scsi_cmnd),
1587                                         scsi_prot_sg_count(scsi_cmnd), datadir);
1588                         if (unlikely(!protsegcnt)) {
1589                                 scsi_dma_unmap(scsi_cmnd);
1590                                 return 1;
1591                         }
1592
1593                         lpfc_cmd->prot_seg_cnt = protsegcnt;
1594                         if (lpfc_cmd->prot_seg_cnt
1595                             > phba->cfg_prot_sg_seg_cnt) {
1596                                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1597                                         "9068 BLKGRD: %s: Too many prot sg "
1598                                         "segments from dma_map_sg.  Config %d,"
1599                                                 "prot_seg_cnt %d\n", __func__,
1600                                                 phba->cfg_prot_sg_seg_cnt,
1601                                                 lpfc_cmd->prot_seg_cnt);
1602                                 dma_unmap_sg(&phba->pcidev->dev,
1603                                              scsi_prot_sglist(scsi_cmnd),
1604                                              scsi_prot_sg_count(scsi_cmnd),
1605                                              datadir);
1606                                 scsi_dma_unmap(scsi_cmnd);
1607                                 return 1;
1608                         }
1609
1610                         num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1611                                         datasegcnt, protsegcnt);
1612                         /* we shoud have 3 or more entries in buffer list */
1613                         if (num_bde < 3)
1614                                 goto err;
1615                         break;
1616                 }
1617                 case LPFC_PG_TYPE_INVALID:
1618                 default:
1619                         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1620                                         "9022 Unexpected protection group %i\n",
1621                                         prot_group_type);
1622                         return 1;
1623                 }
1624         }
1625
1626         /*
1627          * Finish initializing those IOCB fields that are dependent on the
1628          * scsi_cmnd request_buffer.  Note that the bdeSize is explicitly
1629          * reinitialized since all iocb memory resources are used many times
1630          * for transmit, receive, and continuation bpl's.
1631          */
1632         iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1633         iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1634         iocb_cmd->ulpBdeCount = 1;
1635         iocb_cmd->ulpLe = 1;
1636
1637         fcpdl = scsi_bufflen(scsi_cmnd);
1638
1639         if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1640                 /*
1641                  * We are in DIF Type 1 mode
1642                  * Every data block has a 8 byte DIF (trailer)
1643                  * attached to it.  Must ajust FCP data length
1644                  */
1645                 blksize = lpfc_cmd_blksize(scsi_cmnd);
1646                 diflen = (fcpdl / blksize) * 8;
1647                 fcpdl += diflen;
1648         }
1649         fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1650
1651         /*
1652          * Due to difference in data length between DIF/non-DIF paths,
1653          * we need to set word 4 of IOCB here
1654          */
1655         iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1656
1657         return 0;
1658 err:
1659         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1660                         "9023 Could not setup all needed BDE's"
1661                         "prot_group_type=%d, num_bde=%d\n",
1662                         prot_group_type, num_bde);
1663         return 1;
1664 }
1665
1666 /*
1667  * This function checks for BlockGuard errors detected by
1668  * the HBA.  In case of errors, the ASC/ASCQ fields in the
1669  * sense buffer will be set accordingly, paired with
1670  * ILLEGAL_REQUEST to signal to the kernel that the HBA
1671  * detected corruption.
1672  *
1673  * Returns:
1674  *  0 - No error found
1675  *  1 - BlockGuard error found
1676  * -1 - Internal error (bad profile, ...etc)
1677  */
1678 static int
1679 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1680                         struct lpfc_iocbq *pIocbOut)
1681 {
1682         struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1683         struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1684         int ret = 0;
1685         uint32_t bghm = bgf->bghm;
1686         uint32_t bgstat = bgf->bgstat;
1687         uint64_t failing_sector = 0;
1688
1689         lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
1690                         " 0x%x lba 0x%llx blk cnt 0x%x "
1691                         "bgstat=0x%x bghm=0x%x\n",
1692                         cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
1693                         blk_rq_sectors(cmd->request), bgstat, bghm);
1694
1695         spin_lock(&_dump_buf_lock);
1696         if (!_dump_buf_done) {
1697                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,  "9070 BLKGRD: Saving"
1698                         " Data for %u blocks to debugfs\n",
1699                                 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1700                 lpfc_debug_save_data(phba, cmd);
1701
1702                 /* If we have a prot sgl, save the DIF buffer */
1703                 if (lpfc_prot_group_type(phba, cmd) ==
1704                                 LPFC_PG_TYPE_DIF_BUF) {
1705                         lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
1706                                 "Saving DIF for %u blocks to debugfs\n",
1707                                 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1708                         lpfc_debug_save_dif(phba, cmd);
1709                 }
1710
1711                 _dump_buf_done = 1;
1712         }
1713         spin_unlock(&_dump_buf_lock);
1714
1715         if (lpfc_bgs_get_invalid_prof(bgstat)) {
1716                 cmd->result = ScsiResult(DID_ERROR, 0);
1717                 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
1718                         " BlockGuard profile. bgstat:0x%x\n",
1719                         bgstat);
1720                 ret = (-1);
1721                 goto out;
1722         }
1723
1724         if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1725                 cmd->result = ScsiResult(DID_ERROR, 0);
1726                 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
1727                                 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
1728                                 bgstat);
1729                 ret = (-1);
1730                 goto out;
1731         }
1732
1733         if (lpfc_bgs_get_guard_err(bgstat)) {
1734                 ret = 1;
1735
1736                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1737                                 0x10, 0x1);
1738                 cmd->result = DRIVER_SENSE << 24
1739                         | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1740                 phba->bg_guard_err_cnt++;
1741                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1742                         "9055 BLKGRD: guard_tag error\n");
1743         }
1744
1745         if (lpfc_bgs_get_reftag_err(bgstat)) {
1746                 ret = 1;
1747
1748                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1749                                 0x10, 0x3);
1750                 cmd->result = DRIVER_SENSE << 24
1751                         | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1752
1753                 phba->bg_reftag_err_cnt++;
1754                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1755                         "9056 BLKGRD: ref_tag error\n");
1756         }
1757
1758         if (lpfc_bgs_get_apptag_err(bgstat)) {
1759                 ret = 1;
1760
1761                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1762                                 0x10, 0x2);
1763                 cmd->result = DRIVER_SENSE << 24
1764                         | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1765
1766                 phba->bg_apptag_err_cnt++;
1767                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1768                         "9061 BLKGRD: app_tag error\n");
1769         }
1770
1771         if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1772                 /*
1773                  * setup sense data descriptor 0 per SPC-4 as an information
1774                  * field, and put the failing LBA in it
1775                  */
1776                 cmd->sense_buffer[8] = 0;     /* Information */
1777                 cmd->sense_buffer[9] = 0xa;   /* Add. length */
1778                 bghm /= cmd->device->sector_size;
1779
1780                 failing_sector = scsi_get_lba(cmd);
1781                 failing_sector += bghm;
1782
1783                 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1784         }
1785
1786         if (!ret) {
1787                 /* No error was reported - problem in FW? */
1788                 cmd->result = ScsiResult(DID_ERROR, 0);
1789                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1790                         "9057 BLKGRD: no errors reported!\n");
1791         }
1792
1793 out:
1794         return ret;
1795 }
1796
1797 /**
1798  * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1799  * @phba: The Hba for which this call is being executed.
1800  * @lpfc_cmd: The scsi buffer which is going to be mapped.
1801  *
1802  * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1803  * field of @lpfc_cmd for device with SLI-4 interface spec.
1804  *
1805  * Return codes:
1806  *      1 - Error
1807  *      0 - Success
1808  **/
1809 static int
1810 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1811 {
1812         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1813         struct scatterlist *sgel = NULL;
1814         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1815         struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
1816         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1817         dma_addr_t physaddr;
1818         uint32_t num_bde = 0;
1819         uint32_t dma_len;
1820         uint32_t dma_offset = 0;
1821         int nseg;
1822
1823         /*
1824          * There are three possibilities here - use scatter-gather segment, use
1825          * the single mapping, or neither.  Start the lpfc command prep by
1826          * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1827          * data bde entry.
1828          */
1829         if (scsi_sg_count(scsi_cmnd)) {
1830                 /*
1831                  * The driver stores the segment count returned from pci_map_sg
1832                  * because this a count of dma-mappings used to map the use_sg
1833                  * pages.  They are not guaranteed to be the same for those
1834                  * architectures that implement an IOMMU.
1835                  */
1836
1837                 nseg = scsi_dma_map(scsi_cmnd);
1838                 if (unlikely(!nseg))
1839                         return 1;
1840                 sgl += 1;
1841                 /* clear the last flag in the fcp_rsp map entry */
1842                 sgl->word2 = le32_to_cpu(sgl->word2);
1843                 bf_set(lpfc_sli4_sge_last, sgl, 0);
1844                 sgl->word2 = cpu_to_le32(sgl->word2);
1845                 sgl += 1;
1846
1847                 lpfc_cmd->seg_cnt = nseg;
1848                 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1849                         lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
1850                                 " %s: Too many sg segments from "
1851                                 "dma_map_sg.  Config %d, seg_cnt %d\n",
1852                                 __func__, phba->cfg_sg_seg_cnt,
1853                                lpfc_cmd->seg_cnt);
1854                         scsi_dma_unmap(scsi_cmnd);
1855                         return 1;
1856                 }
1857
1858                 /*
1859                  * The driver established a maximum scatter-gather segment count
1860                  * during probe that limits the number of sg elements in any
1861                  * single scsi command.  Just run through the seg_cnt and format
1862                  * the sge's.
1863                  * When using SLI-3 the driver will try to fit all the BDEs into
1864                  * the IOCB. If it can't then the BDEs get added to a BPL as it
1865                  * does for SLI-2 mode.
1866                  */
1867                 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1868                         physaddr = sg_dma_address(sgel);
1869                         dma_len = sg_dma_len(sgel);
1870                         bf_set(lpfc_sli4_sge_len, sgl, sg_dma_len(sgel));
1871                         sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1872                         sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1873                         if ((num_bde + 1) == nseg)
1874                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
1875                         else
1876                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
1877                         bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1878                         sgl->word2 = cpu_to_le32(sgl->word2);
1879                         sgl->word3 = cpu_to_le32(sgl->word3);
1880                         dma_offset += dma_len;
1881                         sgl++;
1882                 }
1883         } else {
1884                 sgl += 1;
1885                 /* clear the last flag in the fcp_rsp map entry */
1886                 sgl->word2 = le32_to_cpu(sgl->word2);
1887                 bf_set(lpfc_sli4_sge_last, sgl, 1);
1888                 sgl->word2 = cpu_to_le32(sgl->word2);
1889         }
1890
1891         /*
1892          * Finish initializing those IOCB fields that are dependent on the
1893          * scsi_cmnd request_buffer.  Note that for SLI-2 the bdeSize is
1894          * explicitly reinitialized.
1895          * all iocb memory resources are reused.
1896          */
1897         fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1898
1899         /*
1900          * Due to difference in data length between DIF/non-DIF paths,
1901          * we need to set word 4 of IOCB here
1902          */
1903         iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1904         return 0;
1905 }
1906
1907 /**
1908  * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
1909  * @phba: The Hba for which this call is being executed.
1910  * @lpfc_cmd: The scsi buffer which is going to be mapped.
1911  *
1912  * This routine wraps the actual DMA mapping function pointer from the
1913  * lpfc_hba struct.
1914  *
1915  * Return codes:
1916  *      1 - Error
1917  *      0 - Success
1918  **/
1919 static inline int
1920 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1921 {
1922         return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1923 }
1924
1925 /**
1926  * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
1927  * @phba: Pointer to hba context object.
1928  * @vport: Pointer to vport object.
1929  * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
1930  * @rsp_iocb: Pointer to response iocb object which reported error.
1931  *
1932  * This function posts an event when there is a SCSI command reporting
1933  * error from the scsi device.
1934  **/
1935 static void
1936 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
1937                 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
1938         struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
1939         struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
1940         uint32_t resp_info = fcprsp->rspStatus2;
1941         uint32_t scsi_status = fcprsp->rspStatus3;
1942         uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
1943         struct lpfc_fast_path_event *fast_path_evt = NULL;
1944         struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
1945         unsigned long flags;
1946
1947         /* If there is queuefull or busy condition send a scsi event */
1948         if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
1949                 (cmnd->result == SAM_STAT_BUSY)) {
1950                 fast_path_evt = lpfc_alloc_fast_evt(phba);
1951                 if (!fast_path_evt)
1952                         return;
1953                 fast_path_evt->un.scsi_evt.event_type =
1954                         FC_REG_SCSI_EVENT;
1955                 fast_path_evt->un.scsi_evt.subcategory =
1956                 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
1957                 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
1958                 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
1959                 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
1960                         &pnode->nlp_portname, sizeof(struct lpfc_name));
1961                 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
1962                         &pnode->nlp_nodename, sizeof(struct lpfc_name));
1963         } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
1964                 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
1965                 fast_path_evt = lpfc_alloc_fast_evt(phba);
1966                 if (!fast_path_evt)
1967                         return;
1968                 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
1969                         FC_REG_SCSI_EVENT;
1970                 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
1971                         LPFC_EVENT_CHECK_COND;
1972                 fast_path_evt->un.check_cond_evt.scsi_event.lun =
1973                         cmnd->device->lun;
1974                 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
1975                         &pnode->nlp_portname, sizeof(struct lpfc_name));
1976                 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
1977                         &pnode->nlp_nodename, sizeof(struct lpfc_name));
1978                 fast_path_evt->un.check_cond_evt.sense_key =
1979                         cmnd->sense_buffer[2] & 0xf;
1980                 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
1981                 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
1982         } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
1983                      fcpi_parm &&
1984                      ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
1985                         ((scsi_status == SAM_STAT_GOOD) &&
1986                         !(resp_info & (RESID_UNDER | RESID_OVER))))) {
1987                 /*
1988                  * If status is good or resid does not match with fcp_param and
1989                  * there is valid fcpi_parm, then there is a read_check error
1990                  */
1991                 fast_path_evt = lpfc_alloc_fast_evt(phba);
1992                 if (!fast_path_evt)
1993                         return;
1994                 fast_path_evt->un.read_check_error.header.event_type =
1995                         FC_REG_FABRIC_EVENT;
1996                 fast_path_evt->un.read_check_error.header.subcategory =
1997                         LPFC_EVENT_FCPRDCHKERR;
1998                 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
1999                         &pnode->nlp_portname, sizeof(struct lpfc_name));
2000                 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2001                         &pnode->nlp_nodename, sizeof(struct lpfc_name));
2002                 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2003                 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2004                 fast_path_evt->un.read_check_error.fcpiparam =
2005                         fcpi_parm;
2006         } else
2007                 return;
2008
2009         fast_path_evt->vport = vport;
2010         spin_lock_irqsave(&phba->hbalock, flags);
2011         list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2012         spin_unlock_irqrestore(&phba->hbalock, flags);
2013         lpfc_worker_wake_up(phba);
2014         return;
2015 }
2016
2017 /**
2018  * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
2019  * @phba: The HBA for which this call is being executed.
2020  * @psb: The scsi buffer which is going to be un-mapped.
2021  *
2022  * This routine does DMA un-mapping of scatter gather list of scsi command
2023  * field of @lpfc_cmd for device with SLI-3 interface spec.
2024  **/
2025 static void
2026 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
2027 {
2028         /*
2029          * There are only two special cases to consider.  (1) the scsi command
2030          * requested scatter-gather usage or (2) the scsi command allocated
2031          * a request buffer, but did not request use_sg.  There is a third
2032          * case, but it does not require resource deallocation.
2033          */
2034         if (psb->seg_cnt > 0)
2035                 scsi_dma_unmap(psb->pCmd);
2036         if (psb->prot_seg_cnt > 0)
2037                 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2038                                 scsi_prot_sg_count(psb->pCmd),
2039                                 psb->pCmd->sc_data_direction);
2040 }
2041
2042 /**
2043  * lpfc_handler_fcp_err - FCP response handler
2044  * @vport: The virtual port for which this call is being executed.
2045  * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2046  * @rsp_iocb: The response IOCB which contains FCP error.
2047  *
2048  * This routine is called to process response IOCB with status field
2049  * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2050  * based upon SCSI and FCP error.
2051  **/
2052 static void
2053 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2054                     struct lpfc_iocbq *rsp_iocb)
2055 {
2056         struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2057         struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2058         struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2059         uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2060         uint32_t resp_info = fcprsp->rspStatus2;
2061         uint32_t scsi_status = fcprsp->rspStatus3;
2062         uint32_t *lp;
2063         uint32_t host_status = DID_OK;
2064         uint32_t rsplen = 0;
2065         uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
2066
2067
2068         /*
2069          *  If this is a task management command, there is no
2070          *  scsi packet associated with this lpfc_cmd.  The driver
2071          *  consumes it.
2072          */
2073         if (fcpcmd->fcpCntl2) {
2074                 scsi_status = 0;
2075                 goto out;
2076         }
2077
2078         if (resp_info & RSP_LEN_VALID) {
2079                 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2080                 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
2081                     (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
2082                         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2083                                  "2719 Invalid response length: "
2084                                  "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2085                                  cmnd->device->id,
2086                                  cmnd->device->lun, cmnd->cmnd[0],
2087                                  rsplen);
2088                         host_status = DID_ERROR;
2089                         goto out;
2090                 }
2091         }
2092
2093         if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2094                 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2095                 if (snslen > SCSI_SENSE_BUFFERSIZE)
2096                         snslen = SCSI_SENSE_BUFFERSIZE;
2097
2098                 if (resp_info & RSP_LEN_VALID)
2099                   rsplen = be32_to_cpu(fcprsp->rspRspLen);
2100                 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2101         }
2102         lp = (uint32_t *)cmnd->sense_buffer;
2103
2104         if (!scsi_status && (resp_info & RESID_UNDER))
2105                 logit = LOG_FCP;
2106
2107         lpfc_printf_vlog(vport, KERN_WARNING, logit,
2108                          "9024 FCP command x%x failed: x%x SNS x%x x%x "
2109                          "Data: x%x x%x x%x x%x x%x\n",
2110                          cmnd->cmnd[0], scsi_status,
2111                          be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2112                          be32_to_cpu(fcprsp->rspResId),
2113                          be32_to_cpu(fcprsp->rspSnsLen),
2114                          be32_to_cpu(fcprsp->rspRspLen),
2115                          fcprsp->rspInfo3);
2116
2117         scsi_set_resid(cmnd, 0);
2118         if (resp_info & RESID_UNDER) {
2119                 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
2120
2121                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2122                                  "9025 FCP Read Underrun, expected %d, "
2123                                  "residual %d Data: x%x x%x x%x\n",
2124                                  be32_to_cpu(fcpcmd->fcpDl),
2125                                  scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2126                                  cmnd->underflow);
2127
2128                 /*
2129                  * If there is an under run check if under run reported by
2130                  * storage array is same as the under run reported by HBA.
2131                  * If this is not same, there is a dropped frame.
2132                  */
2133                 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2134                         fcpi_parm &&
2135                         (scsi_get_resid(cmnd) != fcpi_parm)) {
2136                         lpfc_printf_vlog(vport, KERN_WARNING,
2137                                          LOG_FCP | LOG_FCP_ERROR,
2138                                          "9026 FCP Read Check Error "
2139                                          "and Underrun Data: x%x x%x x%x x%x\n",
2140                                          be32_to_cpu(fcpcmd->fcpDl),
2141                                          scsi_get_resid(cmnd), fcpi_parm,
2142                                          cmnd->cmnd[0]);
2143                         scsi_set_resid(cmnd, scsi_bufflen(cmnd));
2144                         host_status = DID_ERROR;
2145                 }
2146                 /*
2147                  * The cmnd->underflow is the minimum number of bytes that must
2148                  * be transfered for this command.  Provided a sense condition
2149                  * is not present, make sure the actual amount transferred is at
2150                  * least the underflow value or fail.
2151                  */
2152                 if (!(resp_info & SNS_LEN_VALID) &&
2153                     (scsi_status == SAM_STAT_GOOD) &&
2154                     (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2155                      < cmnd->underflow)) {
2156                         lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2157                                          "9027 FCP command x%x residual "
2158                                          "underrun converted to error "
2159                                          "Data: x%x x%x x%x\n",
2160                                          cmnd->cmnd[0], scsi_bufflen(cmnd),
2161                                          scsi_get_resid(cmnd), cmnd->underflow);
2162                         host_status = DID_ERROR;
2163                 }
2164         } else if (resp_info & RESID_OVER) {
2165                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2166                                  "9028 FCP command x%x residual overrun error. "
2167                                  "Data: x%x x%x\n", cmnd->cmnd[0],
2168                                  scsi_bufflen(cmnd), scsi_get_resid(cmnd));
2169                 host_status = DID_ERROR;
2170
2171         /*
2172          * Check SLI validation that all the transfer was actually done
2173          * (fcpi_parm should be zero). Apply check only to reads.
2174          */
2175         } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
2176                         (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
2177                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
2178                                  "9029 FCP Read Check Error Data: "
2179                                  "x%x x%x x%x x%x\n",
2180                                  be32_to_cpu(fcpcmd->fcpDl),
2181                                  be32_to_cpu(fcprsp->rspResId),
2182                                  fcpi_parm, cmnd->cmnd[0]);
2183                 host_status = DID_ERROR;
2184                 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
2185         }
2186
2187  out:
2188         cmnd->result = ScsiResult(host_status, scsi_status);
2189         lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
2190 }
2191
2192 /**
2193  * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
2194  * @phba: The Hba for which this call is being executed.
2195  * @pIocbIn: The command IOCBQ for the scsi cmnd.
2196  * @pIocbOut: The response IOCBQ for the scsi cmnd.
2197  *
2198  * This routine assigns scsi command result by looking into response IOCB
2199  * status field appropriately. This routine handles QUEUE FULL condition as
2200  * well by ramping down device queue depth.
2201  **/
2202 static void
2203 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2204                         struct lpfc_iocbq *pIocbOut)
2205 {
2206         struct lpfc_scsi_buf *lpfc_cmd =
2207                 (struct lpfc_scsi_buf *) pIocbIn->context1;
2208         struct lpfc_vport      *vport = pIocbIn->vport;
2209         struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2210         struct lpfc_nodelist *pnode = rdata->pnode;
2211         struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2212         int result;
2213         struct scsi_device *tmp_sdev;
2214         int depth;
2215         unsigned long flags;
2216         struct lpfc_fast_path_event *fast_path_evt;
2217         struct Scsi_Host *shost = cmd->device->host;
2218         uint32_t queue_depth, scsi_id;
2219
2220         lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2221         lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
2222         if (pnode && NLP_CHK_NODE_ACT(pnode))
2223                 atomic_dec(&pnode->cmd_pending);
2224
2225         if (lpfc_cmd->status) {
2226                 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2227                     (lpfc_cmd->result & IOERR_DRVR_MASK))
2228                         lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2229                 else if (lpfc_cmd->status >= IOSTAT_CNT)
2230                         lpfc_cmd->status = IOSTAT_DEFAULT;
2231
2232                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2233                                  "9030 FCP cmd x%x failed <%d/%d> "
2234                                  "status: x%x result: x%x Data: x%x x%x\n",
2235                                  cmd->cmnd[0],
2236                                  cmd->device ? cmd->device->id : 0xffff,
2237                                  cmd->device ? cmd->device->lun : 0xffff,
2238                                  lpfc_cmd->status, lpfc_cmd->result,
2239                                  pIocbOut->iocb.ulpContext,
2240                                  lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
2241
2242                 switch (lpfc_cmd->status) {
2243                 case IOSTAT_FCP_RSP_ERROR:
2244                         /* Call FCP RSP handler to determine result */
2245                         lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
2246                         break;
2247                 case IOSTAT_NPORT_BSY:
2248                 case IOSTAT_FABRIC_BSY:
2249                         cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2250                         fast_path_evt = lpfc_alloc_fast_evt(phba);
2251                         if (!fast_path_evt)
2252                                 break;
2253                         fast_path_evt->un.fabric_evt.event_type =
2254                                 FC_REG_FABRIC_EVENT;
2255                         fast_path_evt->un.fabric_evt.subcategory =
2256                                 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2257                                 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2258                         if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2259                                 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2260                                         &pnode->nlp_portname,
2261                                         sizeof(struct lpfc_name));
2262                                 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2263                                         &pnode->nlp_nodename,
2264                                         sizeof(struct lpfc_name));
2265                         }
2266                         fast_path_evt->vport = vport;
2267                         fast_path_evt->work_evt.evt =
2268                                 LPFC_EVT_FASTPATH_MGMT_EVT;
2269                         spin_lock_irqsave(&phba->hbalock, flags);
2270                         list_add_tail(&fast_path_evt->work_evt.evt_listp,
2271                                 &phba->work_list);
2272                         spin_unlock_irqrestore(&phba->hbalock, flags);
2273                         lpfc_worker_wake_up(phba);
2274                         break;
2275                 case IOSTAT_LOCAL_REJECT:
2276                         if (lpfc_cmd->result == IOERR_INVALID_RPI ||
2277                             lpfc_cmd->result == IOERR_NO_RESOURCES ||
2278                             lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
2279                                 cmd->result = ScsiResult(DID_REQUEUE, 0);
2280                                 break;
2281                         }
2282
2283                         if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2284                              lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2285                              pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2286                                 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2287                                         /*
2288                                          * This is a response for a BG enabled
2289                                          * cmd. Parse BG error
2290                                          */
2291                                         lpfc_parse_bg_err(phba, lpfc_cmd,
2292                                                         pIocbOut);
2293                                         break;
2294                                 } else {
2295                                         lpfc_printf_vlog(vport, KERN_WARNING,
2296                                                         LOG_BG,
2297                                                         "9031 non-zero BGSTAT "
2298                                                         "on unprotected cmd\n");
2299                                 }
2300                         }
2301
2302                 /* else: fall through */
2303                 default:
2304                         cmd->result = ScsiResult(DID_ERROR, 0);
2305                         break;
2306                 }
2307
2308                 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
2309                     || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
2310                         cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2311                                                  SAM_STAT_BUSY);
2312         } else {
2313                 cmd->result = ScsiResult(DID_OK, 0);
2314         }
2315
2316         if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2317                 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2318
2319                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2320                                  "0710 Iodone <%d/%d> cmd %p, error "
2321                                  "x%x SNS x%x x%x Data: x%x x%x\n",
2322                                  cmd->device->id, cmd->device->lun, cmd,
2323                                  cmd->result, *lp, *(lp + 3), cmd->retries,
2324                                  scsi_get_resid(cmd));
2325         }
2326
2327         lpfc_update_stats(phba, lpfc_cmd);
2328         result = cmd->result;
2329         if (vport->cfg_max_scsicmpl_time &&
2330            time_after(jiffies, lpfc_cmd->start_time +
2331                 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
2332                 spin_lock_irqsave(shost->host_lock, flags);
2333                 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2334                         if (pnode->cmd_qdepth >
2335                                 atomic_read(&pnode->cmd_pending) &&
2336                                 (atomic_read(&pnode->cmd_pending) >
2337                                 LPFC_MIN_TGT_QDEPTH) &&
2338                                 ((cmd->cmnd[0] == READ_10) ||
2339                                 (cmd->cmnd[0] == WRITE_10)))
2340                                 pnode->cmd_qdepth =
2341                                         atomic_read(&pnode->cmd_pending);
2342
2343                         pnode->last_change_time = jiffies;
2344                 }
2345                 spin_unlock_irqrestore(shost->host_lock, flags);
2346         } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2347                 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
2348                    time_after(jiffies, pnode->last_change_time +
2349                               msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
2350                         spin_lock_irqsave(shost->host_lock, flags);
2351                         pnode->cmd_qdepth += pnode->cmd_qdepth *
2352                                 LPFC_TGTQ_RAMPUP_PCENT / 100;
2353                         if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
2354                                 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2355                         pnode->last_change_time = jiffies;
2356                         spin_unlock_irqrestore(shost->host_lock, flags);
2357                 }
2358         }
2359
2360         lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
2361
2362         /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2363         queue_depth = cmd->device->queue_depth;
2364         scsi_id = cmd->device->id;
2365         cmd->scsi_done(cmd);
2366
2367         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2368                 /*
2369                  * If there is a thread waiting for command completion
2370                  * wake up the thread.
2371                  */
2372                 spin_lock_irqsave(shost->host_lock, flags);
2373                 lpfc_cmd->pCmd = NULL;
2374                 if (lpfc_cmd->waitq)
2375                         wake_up(lpfc_cmd->waitq);
2376                 spin_unlock_irqrestore(shost->host_lock, flags);
2377                 lpfc_release_scsi_buf(phba, lpfc_cmd);
2378                 return;
2379         }
2380
2381         if (!result)
2382                 lpfc_rampup_queue_depth(vport, queue_depth);
2383
2384         /*
2385          * Check for queue full.  If the lun is reporting queue full, then
2386          * back off the lun queue depth to prevent target overloads.
2387          */
2388         if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2389             NLP_CHK_NODE_ACT(pnode)) {
2390                 shost_for_each_device(tmp_sdev, shost) {
2391                         if (tmp_sdev->id != scsi_id)
2392                                 continue;
2393                         depth = scsi_track_queue_full(tmp_sdev,
2394                                                       tmp_sdev->queue_depth-1);
2395                         if (depth <= 0)
2396                                 continue;
2397                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2398                                          "0711 detected queue full - lun queue "
2399                                          "depth adjusted to %d.\n", depth);
2400                         lpfc_send_sdev_queuedepth_change_event(phba, vport,
2401                                                                pnode,
2402                                                                tmp_sdev->lun,
2403                                                                depth+1, depth);
2404                 }
2405         }
2406
2407         /*
2408          * If there is a thread waiting for command completion
2409          * wake up the thread.
2410          */
2411         spin_lock_irqsave(shost->host_lock, flags);
2412         lpfc_cmd->pCmd = NULL;
2413         if (lpfc_cmd->waitq)
2414                 wake_up(lpfc_cmd->waitq);
2415         spin_unlock_irqrestore(shost->host_lock, flags);
2416
2417         lpfc_release_scsi_buf(phba, lpfc_cmd);
2418 }
2419
2420 /**
2421  * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
2422  * @data: A pointer to the immediate command data portion of the IOCB.
2423  * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2424  *
2425  * The routine copies the entire FCP command from @fcp_cmnd to @data while
2426  * byte swapping the data to big endian format for transmission on the wire.
2427  **/
2428 static void
2429 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2430 {
2431         int i, j;
2432         for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2433              i += sizeof(uint32_t), j++) {
2434                 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2435         }
2436 }
2437
2438 /**
2439  * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
2440  * @vport: The virtual port for which this call is being executed.
2441  * @lpfc_cmd: The scsi command which needs to send.
2442  * @pnode: Pointer to lpfc_nodelist.
2443  *
2444  * This routine initializes fcp_cmnd and iocb data structure from scsi command
2445  * to transfer for device with SLI3 interface spec.
2446  **/
2447 static void
2448 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2449                     struct lpfc_nodelist *pnode)
2450 {
2451         struct lpfc_hba *phba = vport->phba;
2452         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2453         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2454         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2455         struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2456         int datadir = scsi_cmnd->sc_data_direction;
2457         char tag[2];
2458
2459         if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2460                 return;
2461
2462         lpfc_cmd->fcp_rsp->rspSnsLen = 0;
2463         /* clear task management bits */
2464         lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
2465
2466         int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2467                         &lpfc_cmd->fcp_cmnd->fcp_lun);
2468
2469         memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2470
2471         if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2472                 switch (tag[0]) {
2473                 case HEAD_OF_QUEUE_TAG:
2474                         fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2475                         break;
2476                 case ORDERED_QUEUE_TAG:
2477                         fcp_cmnd->fcpCntl1 = ORDERED_Q;
2478                         break;
2479                 default:
2480                         fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2481                         break;
2482                 }
2483         } else
2484                 fcp_cmnd->fcpCntl1 = 0;
2485
2486         /*
2487          * There are three possibilities here - use scatter-gather segment, use
2488          * the single mapping, or neither.  Start the lpfc command prep by
2489          * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2490          * data bde entry.
2491          */
2492         if (scsi_sg_count(scsi_cmnd)) {
2493                 if (datadir == DMA_TO_DEVICE) {
2494                         iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
2495                         if (phba->sli_rev < LPFC_SLI_REV4) {
2496                                 iocb_cmd->un.fcpi.fcpi_parm = 0;
2497                                 iocb_cmd->ulpPU = 0;
2498                         } else
2499                                 iocb_cmd->ulpPU = PARM_READ_CHECK;
2500                         fcp_cmnd->fcpCntl3 = WRITE_DATA;
2501                         phba->fc4OutputRequests++;
2502                 } else {
2503                         iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2504                         iocb_cmd->ulpPU = PARM_READ_CHECK;
2505                         fcp_cmnd->fcpCntl3 = READ_DATA;
2506                         phba->fc4InputRequests++;
2507                 }
2508         } else {
2509                 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2510                 iocb_cmd->un.fcpi.fcpi_parm = 0;
2511                 iocb_cmd->ulpPU = 0;
2512                 fcp_cmnd->fcpCntl3 = 0;
2513                 phba->fc4ControlRequests++;
2514         }
2515         if (phba->sli_rev == 3 &&
2516             !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
2517                 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
2518         /*
2519          * Finish initializing those IOCB fields that are independent
2520          * of the scsi_cmnd request_buffer
2521          */
2522         piocbq->iocb.ulpContext = pnode->nlp_rpi;
2523         if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2524                 piocbq->iocb.ulpFCP2Rcvy = 1;
2525         else
2526                 piocbq->iocb.ulpFCP2Rcvy = 0;
2527
2528         piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2529         piocbq->context1  = lpfc_cmd;
2530         piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2531         piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2532         piocbq->vport = vport;
2533 }
2534
2535 /**
2536  * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
2537  * @vport: The virtual port for which this call is being executed.
2538  * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2539  * @lun: Logical unit number.
2540  * @task_mgmt_cmd: SCSI task management command.
2541  *
2542  * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2543  * for device with SLI-3 interface spec.
2544  *
2545  * Return codes:
2546  *   0 - Error
2547  *   1 - Success
2548  **/
2549 static int
2550 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
2551                              struct lpfc_scsi_buf *lpfc_cmd,
2552                              unsigned int lun,
2553                              uint8_t task_mgmt_cmd)
2554 {
2555         struct lpfc_iocbq *piocbq;
2556         IOCB_t *piocb;
2557         struct fcp_cmnd *fcp_cmnd;
2558         struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2559         struct lpfc_nodelist *ndlp = rdata->pnode;
2560
2561         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2562             ndlp->nlp_state != NLP_STE_MAPPED_NODE)
2563                 return 0;
2564
2565         piocbq = &(lpfc_cmd->cur_iocbq);
2566         piocbq->vport = vport;
2567
2568         piocb = &piocbq->iocb;
2569
2570         fcp_cmnd = lpfc_cmd->fcp_cmnd;
2571         /* Clear out any old data in the FCP command area */
2572         memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2573         int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
2574         fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
2575         if (vport->phba->sli_rev == 3 &&
2576             !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
2577                 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
2578         piocb->ulpCommand = CMD_FCP_ICMND64_CR;
2579         piocb->ulpContext = ndlp->nlp_rpi;
2580         if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2581                 piocb->ulpFCP2Rcvy = 1;
2582         }
2583         piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2584
2585         /* ulpTimeout is only one byte */
2586         if (lpfc_cmd->timeout > 0xff) {
2587                 /*
2588                  * Do not timeout the command at the firmware level.
2589                  * The driver will provide the timeout mechanism.
2590                  */
2591                 piocb->ulpTimeout = 0;
2592         } else
2593                 piocb->ulpTimeout = lpfc_cmd->timeout;
2594
2595         if (vport->phba->sli_rev == LPFC_SLI_REV4)
2596                 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
2597
2598         return 1;
2599 }
2600
2601 /**
2602  * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2603  * @phba: The hba struct for which this call is being executed.
2604  * @dev_grp: The HBA PCI-Device group number.
2605  *
2606  * This routine sets up the SCSI interface API function jump table in @phba
2607  * struct.
2608  * Returns: 0 - success, -ENODEV - failure.
2609  **/
2610 int
2611 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2612 {
2613
2614         phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2615         phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2616         phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2617
2618         switch (dev_grp) {
2619         case LPFC_PCI_DEV_LP:
2620                 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2621                 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
2622                 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2623                 break;
2624         case LPFC_PCI_DEV_OC:
2625                 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2626                 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
2627                 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2628                 break;
2629         default:
2630                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2631                                 "1418 Invalid HBA PCI-device group: 0x%x\n",
2632                                 dev_grp);
2633                 return -ENODEV;
2634                 break;
2635         }
2636         phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2637         phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
2638         return 0;
2639 }
2640
2641 /**
2642  * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
2643  * @phba: The Hba for which this call is being executed.
2644  * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2645  * @rspiocbq: Pointer to lpfc_iocbq data structure.
2646  *
2647  * This routine is IOCB completion routine for device reset and target reset
2648  * routine. This routine release scsi buffer associated with lpfc_cmd.
2649  **/
2650 static void
2651 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2652                         struct lpfc_iocbq *cmdiocbq,
2653                         struct lpfc_iocbq *rspiocbq)
2654 {
2655         struct lpfc_scsi_buf *lpfc_cmd =
2656                 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2657         if (lpfc_cmd)
2658                 lpfc_release_scsi_buf(phba, lpfc_cmd);
2659         return;
2660 }
2661
2662 /**
2663  * lpfc_info - Info entry point of scsi_host_template data structure
2664  * @host: The scsi host for which this call is being executed.
2665  *
2666  * This routine provides module information about hba.
2667  *
2668  * Reutrn code:
2669  *   Pointer to char - Success.
2670  **/
2671 const char *
2672 lpfc_info(struct Scsi_Host *host)
2673 {
2674         struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2675         struct lpfc_hba   *phba = vport->phba;
2676         int len;
2677         static char  lpfcinfobuf[384];
2678
2679         memset(lpfcinfobuf,0,384);
2680         if (phba && phba->pcidev){
2681                 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2682                 len = strlen(lpfcinfobuf);
2683                 snprintf(lpfcinfobuf + len,
2684                         384-len,
2685                         " on PCI bus %02x device %02x irq %d",
2686                         phba->pcidev->bus->number,
2687                         phba->pcidev->devfn,
2688                         phba->pcidev->irq);
2689                 len = strlen(lpfcinfobuf);
2690                 if (phba->Port[0]) {
2691                         snprintf(lpfcinfobuf + len,
2692                                  384-len,
2693                                  " port %s",
2694                                  phba->Port);
2695                 }
2696         }
2697         return lpfcinfobuf;
2698 }
2699
2700 /**
2701  * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
2702  * @phba: The Hba for which this call is being executed.
2703  *
2704  * This routine modifies fcp_poll_timer  field of @phba by cfg_poll_tmo.
2705  * The default value of cfg_poll_tmo is 10 milliseconds.
2706  **/
2707 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2708 {
2709         unsigned long  poll_tmo_expires =
2710                 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2711
2712         if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2713                 mod_timer(&phba->fcp_poll_timer,
2714                           poll_tmo_expires);
2715 }
2716
2717 /**
2718  * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
2719  * @phba: The Hba for which this call is being executed.
2720  *
2721  * This routine starts the fcp_poll_timer of @phba.
2722  **/
2723 void lpfc_poll_start_timer(struct lpfc_hba * phba)
2724 {
2725         lpfc_poll_rearm_timer(phba);
2726 }
2727
2728 /**
2729  * lpfc_poll_timeout - Restart polling timer
2730  * @ptr: Map to lpfc_hba data structure pointer.
2731  *
2732  * This routine restarts fcp_poll timer, when FCP ring  polling is enable
2733  * and FCP Ring interrupt is disable.
2734  **/
2735
2736 void lpfc_poll_timeout(unsigned long ptr)
2737 {
2738         struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
2739
2740         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2741                 lpfc_sli_handle_fast_ring_event(phba,
2742                         &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2743
2744                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2745                         lpfc_poll_rearm_timer(phba);
2746         }
2747 }
2748
2749 /**
2750  * lpfc_queuecommand - scsi_host_template queuecommand entry point
2751  * @cmnd: Pointer to scsi_cmnd data structure.
2752  * @done: Pointer to done routine.
2753  *
2754  * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2755  * This routine prepares an IOCB from scsi command and provides to firmware.
2756  * The @done callback is invoked after driver finished processing the command.
2757  *
2758  * Return value :
2759  *   0 - Success
2760  *   SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2761  **/
2762 static int
2763 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
2764 {
2765         struct Scsi_Host  *shost = cmnd->device->host;
2766         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2767         struct lpfc_hba   *phba = vport->phba;
2768         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
2769         struct lpfc_nodelist *ndlp = rdata->pnode;
2770         struct lpfc_scsi_buf *lpfc_cmd;
2771         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
2772         int err;
2773
2774         err = fc_remote_port_chkready(rport);
2775         if (err) {
2776                 cmnd->result = err;
2777                 goto out_fail_command;
2778         }
2779
2780         if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2781                 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2782
2783                 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2784                                 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
2785                                 " op:%02x str=%s without registering for"
2786                                 " BlockGuard - Rejecting command\n",
2787                                 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2788                                 dif_op_str[scsi_get_prot_op(cmnd)]);
2789                 goto out_fail_command;
2790         }
2791
2792         /*
2793          * Catch race where our node has transitioned, but the
2794          * transport is still transitioning.
2795          */
2796         if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2797                 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2798                 goto out_fail_command;
2799         }
2800         if (vport->cfg_max_scsicmpl_time &&
2801                 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
2802                 goto out_host_busy;
2803
2804         lpfc_cmd = lpfc_get_scsi_buf(phba);
2805         if (lpfc_cmd == NULL) {
2806                 lpfc_rampdown_queue_depth(phba);
2807
2808                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2809                                  "0707 driver's buffer pool is empty, "
2810                                  "IO busied\n");
2811                 goto out_host_busy;
2812         }
2813
2814         /*
2815          * Store the midlayer's command structure for the completion phase
2816          * and complete the command initialization.
2817          */
2818         lpfc_cmd->pCmd  = cmnd;
2819         lpfc_cmd->rdata = rdata;
2820         lpfc_cmd->timeout = 0;
2821         lpfc_cmd->start_time = jiffies;
2822         cmnd->host_scribble = (unsigned char *)lpfc_cmd;
2823         cmnd->scsi_done = done;
2824
2825         if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2826                 if (vport->phba->cfg_enable_bg) {
2827                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2828                                 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
2829                                 "str=%s\n",
2830                                 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2831                                 dif_op_str[scsi_get_prot_op(cmnd)]);
2832                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2833                                 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
2834                                 "%02x %02x %02x %02x %02x\n",
2835                                 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2836                                 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2837                                 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2838                                 cmnd->cmnd[9]);
2839                         if (cmnd->cmnd[0] == READ_10)
2840                                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2841                                         "9035 BLKGRD: READ @ sector %llu, "
2842                                         "count %u\n",
2843                                         (unsigned long long)scsi_get_lba(cmnd),
2844                                         blk_rq_sectors(cmnd->request));
2845                         else if (cmnd->cmnd[0] == WRITE_10)
2846                                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2847                                         "9036 BLKGRD: WRITE @ sector %llu, "
2848                                         "count %u cmd=%p\n",
2849                                         (unsigned long long)scsi_get_lba(cmnd),
2850                                         blk_rq_sectors(cmnd->request),
2851                                         cmnd);
2852                 }
2853
2854                 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
2855         } else {
2856                 if (vport->phba->cfg_enable_bg) {
2857                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2858                                         "9038 BLKGRD: rcvd unprotected cmd:"
2859                                         "%02x op:%02x str=%s\n",
2860                                         cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2861                                         dif_op_str[scsi_get_prot_op(cmnd)]);
2862                                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2863                                         "9039 BLKGRD: CDB: %02x %02x %02x "
2864                                         "%02x %02x %02x %02x %02x %02x %02x\n",
2865                                         cmnd->cmnd[0], cmnd->cmnd[1],
2866                                         cmnd->cmnd[2], cmnd->cmnd[3],
2867                                         cmnd->cmnd[4], cmnd->cmnd[5],
2868                                         cmnd->cmnd[6], cmnd->cmnd[7],
2869                                         cmnd->cmnd[8], cmnd->cmnd[9]);
2870                         if (cmnd->cmnd[0] == READ_10)
2871                                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2872                                         "9040 dbg: READ @ sector %llu, "
2873                                         "count %u\n",
2874                                         (unsigned long long)scsi_get_lba(cmnd),
2875                                          blk_rq_sectors(cmnd->request));
2876                         else if (cmnd->cmnd[0] == WRITE_10)
2877                                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2878                                          "9041 dbg: WRITE @ sector %llu, "
2879                                          "count %u cmd=%p\n",
2880                                          (unsigned long long)scsi_get_lba(cmnd),
2881                                          blk_rq_sectors(cmnd->request), cmnd);
2882                         else
2883                                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2884                                          "9042 dbg: parser not implemented\n");
2885                 }
2886                 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2887         }
2888
2889         if (err)
2890                 goto out_host_busy_free_buf;
2891
2892         lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
2893
2894         atomic_inc(&ndlp->cmd_pending);
2895         err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
2896                                   &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
2897         if (err) {
2898                 atomic_dec(&ndlp->cmd_pending);
2899                 goto out_host_busy_free_buf;
2900         }
2901         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2902                 spin_unlock(shost->host_lock);
2903                 lpfc_sli_handle_fast_ring_event(phba,
2904                         &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2905
2906                 spin_lock(shost->host_lock);
2907                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2908                         lpfc_poll_rearm_timer(phba);
2909         }
2910
2911         return 0;
2912
2913  out_host_busy_free_buf:
2914         lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
2915         lpfc_release_scsi_buf(phba, lpfc_cmd);
2916  out_host_busy:
2917         return SCSI_MLQUEUE_HOST_BUSY;
2918
2919  out_fail_command:
2920         done(cmnd);
2921         return 0;
2922 }
2923
2924 /**
2925  * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
2926  * @cmnd: Pointer to scsi_cmnd data structure.
2927  *
2928  * This routine aborts @cmnd pending in base driver.
2929  *
2930  * Return code :
2931  *   0x2003 - Error
2932  *   0x2002 - Success
2933  **/
2934 static int
2935 lpfc_abort_handler(struct scsi_cmnd *cmnd)
2936 {
2937         struct Scsi_Host  *shost = cmnd->device->host;
2938         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2939         struct lpfc_hba   *phba = vport->phba;
2940         struct lpfc_iocbq *iocb;
2941         struct lpfc_iocbq *abtsiocb;
2942         struct lpfc_scsi_buf *lpfc_cmd;
2943         IOCB_t *cmd, *icmd;
2944         int ret = SUCCESS;
2945         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
2946
2947         fc_block_scsi_eh(cmnd);
2948         lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
2949         BUG_ON(!lpfc_cmd);
2950
2951         /*
2952          * If pCmd field of the corresponding lpfc_scsi_buf structure
2953          * points to a different SCSI command, then the driver has
2954          * already completed this command, but the midlayer did not
2955          * see the completion before the eh fired.  Just return
2956          * SUCCESS.
2957          */
2958         iocb = &lpfc_cmd->cur_iocbq;
2959         if (lpfc_cmd->pCmd != cmnd)
2960                 goto out;
2961
2962         BUG_ON(iocb->context1 != lpfc_cmd);
2963
2964         abtsiocb = lpfc_sli_get_iocbq(phba);
2965         if (abtsiocb == NULL) {
2966                 ret = FAILED;
2967                 goto out;
2968         }
2969
2970         /*
2971          * The scsi command can not be in txq and it is in flight because the
2972          * pCmd is still pointig at the SCSI command we have to abort. There
2973          * is no need to search the txcmplq. Just send an abort to the FW.
2974          */
2975
2976         cmd = &iocb->iocb;
2977         icmd = &abtsiocb->iocb;
2978         icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
2979         icmd->un.acxri.abortContextTag = cmd->ulpContext;
2980         if (phba->sli_rev == LPFC_SLI_REV4)
2981                 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
2982         else
2983                 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
2984
2985         icmd->ulpLe = 1;
2986         icmd->ulpClass = cmd->ulpClass;
2987
2988         /* ABTS WQE must go to the same WQ as the WQE to be aborted */
2989         abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
2990
2991         if (lpfc_is_link_up(phba))
2992                 icmd->ulpCommand = CMD_ABORT_XRI_CN;
2993         else
2994                 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
2995
2996         abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
2997         abtsiocb->vport = vport;
2998         if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
2999             IOCB_ERROR) {
3000                 lpfc_sli_release_iocbq(phba, abtsiocb);
3001                 ret = FAILED;
3002                 goto out;
3003         }
3004
3005         if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3006                 lpfc_sli_handle_fast_ring_event(phba,
3007                         &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3008
3009         lpfc_cmd->waitq = &waitq;
3010         /* Wait for abort to complete */
3011         wait_event_timeout(waitq,
3012                           (lpfc_cmd->pCmd != cmnd),
3013                            (2*vport->cfg_devloss_tmo*HZ));
3014
3015         spin_lock_irq(shost->host_lock);
3016         lpfc_cmd->waitq = NULL;
3017         spin_unlock_irq(shost->host_lock);
3018
3019         if (lpfc_cmd->pCmd == cmnd) {
3020                 ret = FAILED;
3021                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3022                                  "0748 abort handler timed out waiting "
3023                                  "for abort to complete: ret %#x, ID %d, "
3024                                  "LUN %d, snum %#lx\n",
3025                                  ret, cmnd->device->id, cmnd->device->lun,
3026                                  cmnd->serial_number);
3027         }
3028
3029  out:
3030         lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3031                          "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3032                          "LUN %d snum %#lx\n", ret, cmnd->device->id,
3033                          cmnd->device->lun, cmnd->serial_number);
3034         return ret;
3035 }
3036
3037 static char *
3038 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3039 {
3040         switch (task_mgmt_cmd) {
3041         case FCP_ABORT_TASK_SET:
3042                 return "ABORT_TASK_SET";
3043         case FCP_CLEAR_TASK_SET:
3044                 return "FCP_CLEAR_TASK_SET";
3045         case FCP_BUS_RESET:
3046                 return "FCP_BUS_RESET";
3047         case FCP_LUN_RESET:
3048                 return "FCP_LUN_RESET";
3049         case FCP_TARGET_RESET:
3050                 return "FCP_TARGET_RESET";
3051         case FCP_CLEAR_ACA:
3052                 return "FCP_CLEAR_ACA";
3053         case FCP_TERMINATE_TASK:
3054                 return "FCP_TERMINATE_TASK";
3055         default:
3056                 return "unknown";
3057         }
3058 }
3059
3060 /**
3061  * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3062  * @vport: The virtual port for which this call is being executed.
3063  * @rdata: Pointer to remote port local data
3064  * @tgt_id: Target ID of remote device.
3065  * @lun_id: Lun number for the TMF
3066  * @task_mgmt_cmd: type of TMF to send
3067  *
3068  * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3069  * a remote port.
3070  *
3071  * Return Code:
3072  *   0x2003 - Error
3073  *   0x2002 - Success.
3074  **/
3075 static int
3076 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3077                     unsigned  tgt_id, unsigned int lun_id,
3078                     uint8_t task_mgmt_cmd)
3079 {
3080         struct lpfc_hba   *phba = vport->phba;
3081         struct lpfc_scsi_buf *lpfc_cmd;
3082         struct lpfc_iocbq *iocbq;
3083         struct lpfc_iocbq *iocbqrsp;
3084         int ret;
3085         int status;
3086
3087         if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
3088                 return FAILED;
3089
3090         lpfc_cmd = lpfc_get_scsi_buf(phba);
3091         if (lpfc_cmd == NULL)
3092                 return FAILED;
3093         lpfc_cmd->timeout = 60;
3094         lpfc_cmd->rdata = rdata;
3095
3096         status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3097                                            task_mgmt_cmd);
3098         if (!status) {
3099                 lpfc_release_scsi_buf(phba, lpfc_cmd);
3100                 return FAILED;
3101         }
3102
3103         iocbq = &lpfc_cmd->cur_iocbq;
3104         iocbqrsp = lpfc_sli_get_iocbq(phba);
3105         if (iocbqrsp == NULL) {
3106                 lpfc_release_scsi_buf(phba, lpfc_cmd);
3107                 return FAILED;
3108         }
3109
3110         lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3111                          "0702 Issue %s to TGT %d LUN %d "
3112                          "rpi x%x nlp_flag x%x\n",
3113                          lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
3114                          rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
3115
3116         status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3117                                           iocbq, iocbqrsp, lpfc_cmd->timeout);
3118         if (status != IOCB_SUCCESS) {
3119                 if (status == IOCB_TIMEDOUT) {
3120                         iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3121                         ret = TIMEOUT_ERROR;
3122                 } else
3123                         ret = FAILED;
3124                 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3125                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3126                          "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
3127                          lpfc_taskmgmt_name(task_mgmt_cmd),
3128                          tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
3129                          iocbqrsp->iocb.un.ulpWord[4]);
3130         } else
3131                 ret = SUCCESS;
3132
3133         lpfc_sli_release_iocbq(phba, iocbqrsp);
3134
3135         if (ret != TIMEOUT_ERROR)
3136                 lpfc_release_scsi_buf(phba, lpfc_cmd);
3137
3138         return ret;
3139 }
3140
3141 /**
3142  * lpfc_chk_tgt_mapped -
3143  * @vport: The virtual port to check on
3144  * @cmnd: Pointer to scsi_cmnd data structure.
3145  *
3146  * This routine delays until the scsi target (aka rport) for the
3147  * command exists (is present and logged in) or we declare it non-existent.
3148  *
3149  * Return code :
3150  *  0x2003 - Error
3151  *  0x2002 - Success
3152  **/
3153 static int
3154 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3155 {
3156         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3157         struct lpfc_nodelist *pnode = rdata->pnode;
3158         unsigned long later;
3159
3160         /*
3161          * If target is not in a MAPPED state, delay until
3162          * target is rediscovered or devloss timeout expires.
3163          */
3164         later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3165         while (time_after(later, jiffies)) {
3166                 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3167                         return FAILED;
3168                 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3169                         return SUCCESS;
3170                 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3171                 rdata = cmnd->device->hostdata;
3172                 if (!rdata)
3173                         return FAILED;
3174                 pnode = rdata->pnode;
3175         }
3176         if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3177             (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3178                 return FAILED;
3179         return SUCCESS;
3180 }
3181
3182 /**
3183  * lpfc_reset_flush_io_context -
3184  * @vport: The virtual port (scsi_host) for the flush context
3185  * @tgt_id: If aborting by Target contect - specifies the target id
3186  * @lun_id: If aborting by Lun context - specifies the lun id
3187  * @context: specifies the context level to flush at.
3188  *
3189  * After a reset condition via TMF, we need to flush orphaned i/o
3190  * contexts from the adapter. This routine aborts any contexts
3191  * outstanding, then waits for their completions. The wait is
3192  * bounded by devloss_tmo though.
3193  *
3194  * Return code :
3195  *  0x2003 - Error
3196  *  0x2002 - Success
3197  **/
3198 static int
3199 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3200                         uint64_t lun_id, lpfc_ctx_cmd context)
3201 {
3202         struct lpfc_hba   *phba = vport->phba;
3203         unsigned long later;
3204         int cnt;
3205
3206         cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3207         if (cnt)
3208                 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3209                                     tgt_id, lun_id, context);
3210         later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3211         while (time_after(later, jiffies) && cnt) {
3212                 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3213                 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3214         }
3215         if (cnt) {
3216                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3217                         "0724 I/O flush failure for context %s : cnt x%x\n",
3218                         ((context == LPFC_CTX_LUN) ? "LUN" :
3219                          ((context == LPFC_CTX_TGT) ? "TGT" :
3220                           ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3221                         cnt);
3222                 return FAILED;
3223         }
3224         return SUCCESS;
3225 }
3226
3227 /**
3228  * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
3229  * @cmnd: Pointer to scsi_cmnd data structure.
3230  *
3231  * This routine does a device reset by sending a LUN_RESET task management
3232  * command.
3233  *
3234  * Return code :
3235  *  0x2003 - Error
3236  *  0x2002 - Success
3237  **/
3238 static int
3239 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
3240 {
3241         struct Scsi_Host  *shost = cmnd->device->host;
3242         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3243         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3244         struct lpfc_nodelist *pnode = rdata->pnode;
3245         unsigned tgt_id = cmnd->device->id;
3246         unsigned int lun_id = cmnd->device->lun;
3247         struct lpfc_scsi_event_header scsi_event;
3248         int status;
3249
3250         fc_block_scsi_eh(cmnd);
3251
3252         status = lpfc_chk_tgt_mapped(vport, cmnd);
3253         if (status == FAILED) {
3254                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3255                         "0721 Device Reset rport failure: rdata x%p\n", rdata);
3256                 return FAILED;
3257         }
3258
3259         scsi_event.event_type = FC_REG_SCSI_EVENT;
3260         scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3261         scsi_event.lun = lun_id;
3262         memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3263         memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3264
3265         fc_host_post_vendor_event(shost, fc_get_event_number(),
3266                 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3267
3268         status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3269                                                 FCP_LUN_RESET);
3270
3271         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3272                          "0713 SCSI layer issued Device Reset (%d, %d) "
3273                          "return x%x\n", tgt_id, lun_id, status);
3274
3275         /*
3276          * We have to clean up i/o as : they may be orphaned by the TMF;
3277          * or if the TMF failed, they may be in an indeterminate state.
3278          * So, continue on.
3279          * We will report success if all the i/o aborts successfully.
3280          */
3281         status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3282                                                 LPFC_CTX_LUN);
3283         return status;
3284 }
3285
3286 /**
3287  * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3288  * @cmnd: Pointer to scsi_cmnd data structure.
3289  *
3290  * This routine does a target reset by sending a TARGET_RESET task management
3291  * command.
3292  *
3293  * Return code :
3294  *  0x2003 - Error
3295  *  0x2002 - Success
3296  **/
3297 static int
3298 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3299 {
3300         struct Scsi_Host  *shost = cmnd->device->host;
3301         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3302         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3303         struct lpfc_nodelist *pnode = rdata->pnode;
3304         unsigned tgt_id = cmnd->device->id;
3305         unsigned int lun_id = cmnd->device->lun;
3306         struct lpfc_scsi_event_header scsi_event;
3307         int status;
3308
3309         fc_block_scsi_eh(cmnd);
3310
3311         status = lpfc_chk_tgt_mapped(vport, cmnd);
3312         if (status == FAILED) {
3313                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3314                         "0722 Target Reset rport failure: rdata x%p\n", rdata);
3315                 return FAILED;
3316         }
3317
3318         scsi_event.event_type = FC_REG_SCSI_EVENT;
3319         scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3320         scsi_event.lun = 0;
3321         memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3322         memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3323
3324         fc_host_post_vendor_event(shost, fc_get_event_number(),
3325                 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3326
3327         status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3328                                         FCP_TARGET_RESET);
3329
3330         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3331                          "0723 SCSI layer issued Target Reset (%d, %d) "
3332                          "return x%x\n", tgt_id, lun_id, status);
3333
3334         /*
3335          * We have to clean up i/o as : they may be orphaned by the TMF;
3336          * or if the TMF failed, they may be in an indeterminate state.
3337          * So, continue on.
3338          * We will report success if all the i/o aborts successfully.
3339          */
3340         status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3341                                         LPFC_CTX_TGT);
3342         return status;
3343 }
3344
3345 /**
3346  * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
3347  * @cmnd: Pointer to scsi_cmnd data structure.
3348  *
3349  * This routine does target reset to all targets on @cmnd->device->host.
3350  * This emulates Parallel SCSI Bus Reset Semantics.
3351  *
3352  * Return code :
3353  *  0x2003 - Error
3354  *  0x2002 - Success
3355  **/
3356 static int
3357 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
3358 {
3359         struct Scsi_Host  *shost = cmnd->device->host;
3360         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3361         struct lpfc_nodelist *ndlp = NULL;
3362         struct lpfc_scsi_event_header scsi_event;
3363         int match;
3364         int ret = SUCCESS, status, i;
3365
3366         scsi_event.event_type = FC_REG_SCSI_EVENT;
3367         scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3368         scsi_event.lun = 0;
3369         memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3370         memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3371
3372         fc_host_post_vendor_event(shost, fc_get_event_number(),
3373                 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3374
3375         fc_block_scsi_eh(cmnd);
3376
3377         /*
3378          * Since the driver manages a single bus device, reset all
3379          * targets known to the driver.  Should any target reset
3380          * fail, this routine returns failure to the midlayer.
3381          */
3382         for (i = 0; i < LPFC_MAX_TARGET; i++) {
3383                 /* Search for mapped node by target ID */
3384                 match = 0;
3385                 spin_lock_irq(shost->host_lock);
3386                 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3387                         if (!NLP_CHK_NODE_ACT(ndlp))
3388                                 continue;
3389                         if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
3390                             ndlp->nlp_sid == i &&
3391                             ndlp->rport) {
3392                                 match = 1;
3393                                 break;
3394                         }
3395                 }
3396                 spin_unlock_irq(shost->host_lock);
3397                 if (!match)
3398                         continue;
3399
3400                 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3401                                         i, 0, FCP_TARGET_RESET);
3402
3403                 if (status != SUCCESS) {
3404                         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3405                                          "0700 Bus Reset on target %d failed\n",
3406                                          i);
3407                         ret = FAILED;
3408                 }
3409         }
3410         /*
3411          * We have to clean up i/o as : they may be orphaned by the TMFs
3412          * above; or if any of the TMFs failed, they may be in an
3413          * indeterminate state.
3414          * We will report success if all the i/o aborts successfully.
3415          */
3416
3417         status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3418         if (status != SUCCESS)
3419                 ret = FAILED;
3420
3421         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3422                          "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
3423         return ret;
3424 }
3425
3426 /**
3427  * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
3428  * @sdev: Pointer to scsi_device.
3429  *
3430  * This routine populates the cmds_per_lun count + 2 scsi_bufs into  this host's
3431  * globally available list of scsi buffers. This routine also makes sure scsi
3432  * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3433  * of scsi buffer exists for the lifetime of the driver.
3434  *
3435  * Return codes:
3436  *   non-0 - Error
3437  *   0 - Success
3438  **/
3439 static int
3440 lpfc_slave_alloc(struct scsi_device *sdev)
3441 {
3442         struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3443         struct lpfc_hba   *phba = vport->phba;
3444         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3445         uint32_t total = 0;
3446         uint32_t num_to_alloc = 0;
3447         int num_allocated = 0;
3448
3449         if (!rport || fc_remote_port_chkready(rport))
3450                 return -ENXIO;
3451
3452         sdev->hostdata = rport->dd_data;
3453
3454         /*
3455          * Populate the cmds_per_lun count scsi_bufs into this host's globally
3456          * available list of scsi buffers.  Don't allocate more than the
3457          * HBA limit conveyed to the midlayer via the host structure.  The
3458          * formula accounts for the lun_queue_depth + error handlers + 1
3459          * extra.  This list of scsi bufs exists for the lifetime of the driver.
3460          */
3461         total = phba->total_scsi_bufs;
3462         num_to_alloc = vport->cfg_lun_queue_depth + 2;
3463
3464         /* Allow some exchanges to be available always to complete discovery */
3465         if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
3466                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3467                                  "0704 At limitation of %d preallocated "
3468                                  "command buffers\n", total);
3469                 return 0;
3470         /* Allow some exchanges to be available always to complete discovery */
3471         } else if (total + num_to_alloc >
3472                 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
3473                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3474                                  "0705 Allocation request of %d "
3475                                  "command buffers will exceed max of %d.  "
3476                                  "Reducing allocation request to %d.\n",
3477                                  num_to_alloc, phba->cfg_hba_queue_depth,
3478                                  (phba->cfg_hba_queue_depth - total));
3479                 num_to_alloc = phba->cfg_hba_queue_depth - total;
3480         }
3481         num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3482         if (num_to_alloc != num_allocated) {
3483                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3484                                  "0708 Allocation request of %d "
3485                                  "command buffers did not succeed.  "
3486                                  "Allocated %d buffers.\n",
3487                                  num_to_alloc, num_allocated);
3488         }
3489         return 0;
3490 }
3491
3492 /**
3493  * lpfc_slave_configure - scsi_host_template slave_configure entry point
3494  * @sdev: Pointer to scsi_device.
3495  *
3496  * This routine configures following items
3497  *   - Tag command queuing support for @sdev if supported.
3498  *   - Dev loss time out value of fc_rport.
3499  *   - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3500  *
3501  * Return codes:
3502  *   0 - Success
3503  **/
3504 static int
3505 lpfc_slave_configure(struct scsi_device *sdev)
3506 {
3507         struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3508         struct lpfc_hba   *phba = vport->phba;
3509         struct fc_rport   *rport = starget_to_rport(sdev->sdev_target);
3510
3511         if (sdev->tagged_supported)
3512                 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
3513         else
3514                 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
3515
3516         /*
3517          * Initialize the fc transport attributes for the target
3518          * containing this scsi device.  Also note that the driver's
3519          * target pointer is stored in the starget_data for the
3520          * driver's sysfs entry point functions.
3521          */
3522         rport->dev_loss_tmo = vport->cfg_devloss_tmo;
3523
3524         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3525                 lpfc_sli_handle_fast_ring_event(phba,
3526                         &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3527                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3528                         lpfc_poll_rearm_timer(phba);
3529         }
3530
3531         return 0;
3532 }
3533
3534 /**
3535  * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
3536  * @sdev: Pointer to scsi_device.
3537  *
3538  * This routine sets @sdev hostatdata filed to null.
3539  **/
3540 static void
3541 lpfc_slave_destroy(struct scsi_device *sdev)
3542 {
3543         sdev->hostdata = NULL;
3544         return;
3545 }
3546
3547
3548 struct scsi_host_template lpfc_template = {
3549         .module                 = THIS_MODULE,
3550         .name                   = LPFC_DRIVER_NAME,
3551         .info                   = lpfc_info,
3552         .queuecommand           = lpfc_queuecommand,
3553         .eh_abort_handler       = lpfc_abort_handler,
3554         .eh_device_reset_handler = lpfc_device_reset_handler,
3555         .eh_target_reset_handler = lpfc_target_reset_handler,
3556         .eh_bus_reset_handler   = lpfc_bus_reset_handler,
3557         .slave_alloc            = lpfc_slave_alloc,
3558         .slave_configure        = lpfc_slave_configure,
3559         .slave_destroy          = lpfc_slave_destroy,
3560         .scan_finished          = lpfc_scan_finished,
3561         .this_id                = -1,
3562         .sg_tablesize           = LPFC_DEFAULT_SG_SEG_CNT,
3563         .cmd_per_lun            = LPFC_CMD_PER_LUN,
3564         .use_clustering         = ENABLE_CLUSTERING,
3565         .shost_attrs            = lpfc_hba_attrs,
3566         .max_sectors            = 0xFFFF,
3567         .vendor_id              = LPFC_NL_VENDOR_ID,
3568         .change_queue_depth     = lpfc_change_queue_depth,
3569 };
3570
3571 struct scsi_host_template lpfc_vport_template = {
3572         .module                 = THIS_MODULE,
3573         .name                   = LPFC_DRIVER_NAME,
3574         .info                   = lpfc_info,
3575         .queuecommand           = lpfc_queuecommand,
3576         .eh_abort_handler       = lpfc_abort_handler,
3577         .eh_device_reset_handler = lpfc_device_reset_handler,
3578         .eh_target_reset_handler = lpfc_target_reset_handler,
3579         .eh_bus_reset_handler   = lpfc_bus_reset_handler,
3580         .slave_alloc            = lpfc_slave_alloc,
3581         .slave_configure        = lpfc_slave_configure,
3582         .slave_destroy          = lpfc_slave_destroy,
3583         .scan_finished          = lpfc_scan_finished,
3584         .this_id                = -1,
3585         .sg_tablesize           = LPFC_DEFAULT_SG_SEG_CNT,
3586         .cmd_per_lun            = LPFC_CMD_PER_LUN,
3587         .use_clustering         = ENABLE_CLUSTERING,
3588         .shost_attrs            = lpfc_vport_attrs,
3589         .max_sectors            = 0xFFFF,
3590         .change_queue_depth     = lpfc_change_queue_depth,
3591 };