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