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