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