[SCSI] lpfc 8.3.34: Fix parameter field in CQE to mask for LOCAL_REJECT status
[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
582dd796 3555 * (fcpi_parm should be zero).
dea3101e 3556 */
582dd796 3557 } else if (fcpi_parm) {
e8b62011 3558 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
582dd796 3559 "9029 FCP Data Transfer Check Error: "
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
e3d2b802 3618 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea3101e 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];
027140ea
JS
3922 uint8_t *ptr;
3923 bool sli4;
dea3101e 3924
58da1ffb
JS
3925 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3926 return;
3927
dea3101e 3928 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
69859dc4
JSEC
3929 /* clear task management bits */
3930 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea3101e 3931
91886523
JSEC
3932 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3933 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea3101e 3934
027140ea
JS
3935 ptr = &fcp_cmnd->fcpCdb[0];
3936 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
3937 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
3938 ptr += scsi_cmnd->cmd_len;
3939 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
3940 }
3941
7e2b19fb
JS
3942 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
3943 switch (tag[0]) {
dea3101e 3944 case HEAD_OF_QUEUE_TAG:
3945 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
3946 break;
3947 case ORDERED_QUEUE_TAG:
3948 fcp_cmnd->fcpCntl1 = ORDERED_Q;
3949 break;
3950 default:
3951 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
3952 break;
3953 }
3954 } else
3955 fcp_cmnd->fcpCntl1 = 0;
3956
027140ea
JS
3957 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
3958
dea3101e 3959 /*
3960 * There are three possibilities here - use scatter-gather segment, use
3961 * the single mapping, or neither. Start the lpfc command prep by
3962 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3963 * data bde entry.
3964 */
a0b4f78f 3965 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 3966 if (datadir == DMA_TO_DEVICE) {
3967 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
027140ea
JS
3968 if (sli4)
3969 iocb_cmd->ulpPU = PARM_READ_CHECK;
3970 else {
3772a991
JS
3971 iocb_cmd->un.fcpi.fcpi_parm = 0;
3972 iocb_cmd->ulpPU = 0;
027140ea 3973 }
dea3101e 3974 fcp_cmnd->fcpCntl3 = WRITE_DATA;
3975 phba->fc4OutputRequests++;
3976 } else {
3977 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
3978 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea3101e 3979 fcp_cmnd->fcpCntl3 = READ_DATA;
3980 phba->fc4InputRequests++;
3981 }
3982 } else {
3983 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
3984 iocb_cmd->un.fcpi.fcpi_parm = 0;
3985 iocb_cmd->ulpPU = 0;
3986 fcp_cmnd->fcpCntl3 = 0;
3987 phba->fc4ControlRequests++;
3988 }
e2a0a9d6
JS
3989 if (phba->sli_rev == 3 &&
3990 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
34b02dcd 3991 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 3992 /*
3993 * Finish initializing those IOCB fields that are independent
3994 * of the scsi_cmnd request_buffer
3995 */
3996 piocbq->iocb.ulpContext = pnode->nlp_rpi;
027140ea 3997 if (sli4)
6d368e53
JS
3998 piocbq->iocb.ulpContext =
3999 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea3101e 4000 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4001 piocbq->iocb.ulpFCP2Rcvy = 1;
09372820
JS
4002 else
4003 piocbq->iocb.ulpFCP2Rcvy = 0;
dea3101e 4004
4005 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4006 piocbq->context1 = lpfc_cmd;
4007 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4008 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2e0fef85 4009 piocbq->vport = vport;
dea3101e 4010}
4011
da0436e9 4012/**
6d368e53 4013 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
9bad7671
JS
4014 * @vport: The virtual port for which this call is being executed.
4015 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4016 * @lun: Logical unit number.
4017 * @task_mgmt_cmd: SCSI task management command.
4018 *
3772a991
JS
4019 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4020 * for device with SLI-3 interface spec.
9bad7671
JS
4021 *
4022 * Return codes:
4023 * 0 - Error
4024 * 1 - Success
4025 **/
dea3101e 4026static int
f1126688 4027lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea3101e 4028 struct lpfc_scsi_buf *lpfc_cmd,
420b630d 4029 unsigned int lun,
dea3101e 4030 uint8_t task_mgmt_cmd)
4031{
dea3101e 4032 struct lpfc_iocbq *piocbq;
4033 IOCB_t *piocb;
4034 struct fcp_cmnd *fcp_cmnd;
0b18ac42 4035 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea3101e 4036 struct lpfc_nodelist *ndlp = rdata->pnode;
4037
58da1ffb
JS
4038 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4039 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea3101e 4040 return 0;
dea3101e 4041
dea3101e 4042 piocbq = &(lpfc_cmd->cur_iocbq);
2e0fef85
JS
4043 piocbq->vport = vport;
4044
dea3101e 4045 piocb = &piocbq->iocb;
4046
4047 fcp_cmnd = lpfc_cmd->fcp_cmnd;
34b02dcd
JS
4048 /* Clear out any old data in the FCP command area */
4049 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4050 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea3101e 4051 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
e2a0a9d6
JS
4052 if (vport->phba->sli_rev == 3 &&
4053 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
34b02dcd 4054 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 4055 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea3101e 4056 piocb->ulpContext = ndlp->nlp_rpi;
6d368e53
JS
4057 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4058 piocb->ulpContext =
4059 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4060 }
dea3101e 4061 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4062 piocb->ulpFCP2Rcvy = 1;
4063 }
4064 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4065
4066 /* ulpTimeout is only one byte */
4067 if (lpfc_cmd->timeout > 0xff) {
4068 /*
4069 * Do not timeout the command at the firmware level.
4070 * The driver will provide the timeout mechanism.
4071 */
4072 piocb->ulpTimeout = 0;
f1126688 4073 } else
dea3101e 4074 piocb->ulpTimeout = lpfc_cmd->timeout;
da0436e9 4075
f1126688
JS
4076 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4077 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
3772a991 4078
f1126688 4079 return 1;
3772a991
JS
4080}
4081
4082/**
25985edc 4083 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
3772a991
JS
4084 * @phba: The hba struct for which this call is being executed.
4085 * @dev_grp: The HBA PCI-Device group number.
4086 *
4087 * This routine sets up the SCSI interface API function jump table in @phba
4088 * struct.
4089 * Returns: 0 - success, -ENODEV - failure.
4090 **/
4091int
4092lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4093{
4094
f1126688
JS
4095 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4096 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
f1126688 4097
3772a991
JS
4098 switch (dev_grp) {
4099 case LPFC_PCI_DEV_LP:
4100 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4101 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
acd6859b 4102 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
3772a991 4103 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
19ca7609 4104 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
3772a991 4105 break;
da0436e9
JS
4106 case LPFC_PCI_DEV_OC:
4107 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4108 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
acd6859b 4109 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
da0436e9 4110 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
19ca7609 4111 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
da0436e9 4112 break;
3772a991
JS
4113 default:
4114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4115 "1418 Invalid HBA PCI-device group: 0x%x\n",
4116 dev_grp);
4117 return -ENODEV;
4118 break;
4119 }
3772a991 4120 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
84d1b006 4121 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
3772a991
JS
4122 return 0;
4123}
4124
9bad7671 4125/**
3621a710 4126 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
9bad7671
JS
4127 * @phba: The Hba for which this call is being executed.
4128 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4129 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4130 *
4131 * This routine is IOCB completion routine for device reset and target reset
4132 * routine. This routine release scsi buffer associated with lpfc_cmd.
4133 **/
7054a606
JS
4134static void
4135lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4136 struct lpfc_iocbq *cmdiocbq,
4137 struct lpfc_iocbq *rspiocbq)
4138{
4139 struct lpfc_scsi_buf *lpfc_cmd =
4140 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4141 if (lpfc_cmd)
4142 lpfc_release_scsi_buf(phba, lpfc_cmd);
4143 return;
4144}
4145
9bad7671 4146/**
3621a710 4147 * lpfc_info - Info entry point of scsi_host_template data structure
9bad7671
JS
4148 * @host: The scsi host for which this call is being executed.
4149 *
4150 * This routine provides module information about hba.
4151 *
4152 * Reutrn code:
4153 * Pointer to char - Success.
4154 **/
dea3101e 4155const char *
4156lpfc_info(struct Scsi_Host *host)
4157{
2e0fef85
JS
4158 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4159 struct lpfc_hba *phba = vport->phba;
dea3101e 4160 int len;
4161 static char lpfcinfobuf[384];
4162
4163 memset(lpfcinfobuf,0,384);
4164 if (phba && phba->pcidev){
4165 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4166 len = strlen(lpfcinfobuf);
4167 snprintf(lpfcinfobuf + len,
4168 384-len,
4169 " on PCI bus %02x device %02x irq %d",
4170 phba->pcidev->bus->number,
4171 phba->pcidev->devfn,
4172 phba->pcidev->irq);
4173 len = strlen(lpfcinfobuf);
4174 if (phba->Port[0]) {
4175 snprintf(lpfcinfobuf + len,
4176 384-len,
4177 " port %s",
4178 phba->Port);
4179 }
65467b6b
JS
4180 len = strlen(lpfcinfobuf);
4181 if (phba->sli4_hba.link_state.logical_speed) {
4182 snprintf(lpfcinfobuf + len,
4183 384-len,
4184 " Logical Link Speed: %d Mbps",
4185 phba->sli4_hba.link_state.logical_speed * 10);
4186 }
dea3101e 4187 }
4188 return lpfcinfobuf;
4189}
4190
9bad7671 4191/**
3621a710 4192 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
9bad7671
JS
4193 * @phba: The Hba for which this call is being executed.
4194 *
4195 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4196 * The default value of cfg_poll_tmo is 10 milliseconds.
4197 **/
875fbdfe
JSEC
4198static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4199{
4200 unsigned long poll_tmo_expires =
4201 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4202
4203 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
4204 mod_timer(&phba->fcp_poll_timer,
4205 poll_tmo_expires);
4206}
4207
9bad7671 4208/**
3621a710 4209 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
9bad7671
JS
4210 * @phba: The Hba for which this call is being executed.
4211 *
4212 * This routine starts the fcp_poll_timer of @phba.
4213 **/
875fbdfe
JSEC
4214void lpfc_poll_start_timer(struct lpfc_hba * phba)
4215{
4216 lpfc_poll_rearm_timer(phba);
4217}
4218
9bad7671 4219/**
3621a710 4220 * lpfc_poll_timeout - Restart polling timer
9bad7671
JS
4221 * @ptr: Map to lpfc_hba data structure pointer.
4222 *
4223 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4224 * and FCP Ring interrupt is disable.
4225 **/
4226
875fbdfe
JSEC
4227void lpfc_poll_timeout(unsigned long ptr)
4228{
2e0fef85 4229 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
875fbdfe
JSEC
4230
4231 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
4232 lpfc_sli_handle_fast_ring_event(phba,
4233 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4234
875fbdfe
JSEC
4235 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4236 lpfc_poll_rearm_timer(phba);
4237 }
875fbdfe
JSEC
4238}
4239
9bad7671 4240/**
3621a710 4241 * lpfc_queuecommand - scsi_host_template queuecommand entry point
9bad7671
JS
4242 * @cmnd: Pointer to scsi_cmnd data structure.
4243 * @done: Pointer to done routine.
4244 *
4245 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4246 * This routine prepares an IOCB from scsi command and provides to firmware.
4247 * The @done callback is invoked after driver finished processing the command.
4248 *
4249 * Return value :
4250 * 0 - Success
4251 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4252 **/
dea3101e 4253static int
b9a7c631 4254lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea3101e 4255{
2e0fef85
JS
4256 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4257 struct lpfc_hba *phba = vport->phba;
dea3101e 4258 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1c6f4ef5 4259 struct lpfc_nodelist *ndlp;
0bd4ca25 4260 struct lpfc_scsi_buf *lpfc_cmd;
19a7b4ae 4261 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
19a7b4ae 4262 int err;
dea3101e 4263
19a7b4ae
JSEC
4264 err = fc_remote_port_chkready(rport);
4265 if (err) {
4266 cmnd->result = err;
dea3101e 4267 goto out_fail_command;
4268 }
1c6f4ef5 4269 ndlp = rdata->pnode;
dea3101e 4270
bf08611b 4271 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
acd6859b 4272 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
e2a0a9d6 4273
6a9c52cf
JS
4274 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4275 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4276 " op:%02x str=%s without registering for"
4277 " BlockGuard - Rejecting command\n",
e2a0a9d6
JS
4278 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4279 dif_op_str[scsi_get_prot_op(cmnd)]);
4280 goto out_fail_command;
4281 }
4282
dea3101e 4283 /*
19a7b4ae
JSEC
4284 * Catch race where our node has transitioned, but the
4285 * transport is still transitioning.
dea3101e 4286 */
6b415f5d
JS
4287 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4288 goto out_tgt_busy;
7dc517df 4289 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
3496343d 4290 goto out_tgt_busy;
a93ce024 4291
19ca7609 4292 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea3101e 4293 if (lpfc_cmd == NULL) {
eaf15d5b 4294 lpfc_rampdown_queue_depth(phba);
92d7f7b0 4295
e8b62011
JS
4296 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4297 "0707 driver's buffer pool is empty, "
4298 "IO busied\n");
dea3101e 4299 goto out_host_busy;
4300 }
4301
4302 /*
4303 * Store the midlayer's command structure for the completion phase
4304 * and complete the command initialization.
4305 */
4306 lpfc_cmd->pCmd = cmnd;
4307 lpfc_cmd->rdata = rdata;
4308 lpfc_cmd->timeout = 0;
977b5a0a 4309 lpfc_cmd->start_time = jiffies;
dea3101e 4310 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea3101e 4311
e2a0a9d6 4312 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
6a9c52cf
JS
4313 if (vport->phba->cfg_enable_bg) {
4314 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
9a6b09c0
JS
4315 "9033 BLKGRD: rcvd protected cmd:%02x op=%s "
4316 "guard=%s\n", cmnd->cmnd[0],
4317 dif_op_str[scsi_get_prot_op(cmnd)],
4318 dif_grd_str[scsi_host_get_guard(shost)]);
6a9c52cf
JS
4319 if (cmnd->cmnd[0] == READ_10)
4320 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 4321 "9035 BLKGRD: READ @ sector %llu, "
9a6b09c0 4322 "cnt %u, rpt %d\n",
83096ebf 4323 (unsigned long long)scsi_get_lba(cmnd),
9a6b09c0
JS
4324 blk_rq_sectors(cmnd->request),
4325 (cmnd->cmnd[1]>>5));
6a9c52cf
JS
4326 else if (cmnd->cmnd[0] == WRITE_10)
4327 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 4328 "9036 BLKGRD: WRITE @ sector %llu, "
9a6b09c0 4329 "cnt %u, wpt %d\n",
87b5c328 4330 (unsigned long long)scsi_get_lba(cmnd),
83096ebf 4331 blk_rq_sectors(cmnd->request),
9a6b09c0 4332 (cmnd->cmnd[1]>>5));
6a9c52cf 4333 }
e2a0a9d6
JS
4334
4335 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4336 } else {
6a9c52cf 4337 if (vport->phba->cfg_enable_bg) {
e2a0a9d6 4338 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
9a6b09c0
JS
4339 "9038 BLKGRD: rcvd unprotected cmd:"
4340 "%02x op=%s guard=%s\n", cmnd->cmnd[0],
4341 dif_op_str[scsi_get_prot_op(cmnd)],
4342 dif_grd_str[scsi_host_get_guard(shost)]);
6a9c52cf
JS
4343 if (cmnd->cmnd[0] == READ_10)
4344 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4345 "9040 dbg: READ @ sector %llu, "
9a6b09c0 4346 "cnt %u, rpt %d\n",
6a9c52cf 4347 (unsigned long long)scsi_get_lba(cmnd),
9a6b09c0
JS
4348 blk_rq_sectors(cmnd->request),
4349 (cmnd->cmnd[1]>>5));
6a9c52cf
JS
4350 else if (cmnd->cmnd[0] == WRITE_10)
4351 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
9a6b09c0
JS
4352 "9041 dbg: WRITE @ sector %llu, "
4353 "cnt %u, wpt %d\n",
4354 (unsigned long long)scsi_get_lba(cmnd),
4355 blk_rq_sectors(cmnd->request),
4356 (cmnd->cmnd[1]>>5));
6a9c52cf 4357 }
e2a0a9d6
JS
4358 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4359 }
4360
dea3101e 4361 if (err)
4362 goto out_host_busy_free_buf;
4363
2e0fef85 4364 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea3101e 4365
977b5a0a 4366 atomic_inc(&ndlp->cmd_pending);
3772a991 4367 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
92d7f7b0 4368 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
eaf15d5b
JS
4369 if (err) {
4370 atomic_dec(&ndlp->cmd_pending);
dea3101e 4371 goto out_host_busy_free_buf;
eaf15d5b 4372 }
875fbdfe 4373 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
4374 lpfc_sli_handle_fast_ring_event(phba,
4375 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4376
875fbdfe
JSEC
4377 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4378 lpfc_poll_rearm_timer(phba);
4379 }
4380
dea3101e 4381 return 0;
4382
4383 out_host_busy_free_buf:
bcf4dbfa 4384 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
0bd4ca25 4385 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 4386 out_host_busy:
4387 return SCSI_MLQUEUE_HOST_BUSY;
4388
3496343d
MC
4389 out_tgt_busy:
4390 return SCSI_MLQUEUE_TARGET_BUSY;
4391
dea3101e 4392 out_fail_command:
b9a7c631 4393 cmnd->scsi_done(cmnd);
dea3101e 4394 return 0;
4395}
4396
f281233d 4397
9bad7671 4398/**
3621a710 4399 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
9bad7671
JS
4400 * @cmnd: Pointer to scsi_cmnd data structure.
4401 *
4402 * This routine aborts @cmnd pending in base driver.
4403 *
4404 * Return code :
4405 * 0x2003 - Error
4406 * 0x2002 - Success
4407 **/
dea3101e 4408static int
63c59c3b 4409lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea3101e 4410{
2e0fef85
JS
4411 struct Scsi_Host *shost = cmnd->device->host;
4412 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4413 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
4414 struct lpfc_iocbq *iocb;
4415 struct lpfc_iocbq *abtsiocb;
dea3101e 4416 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 4417 IOCB_t *cmd, *icmd;
3a70730a 4418 int ret = SUCCESS, status = 0;
fa61a54e 4419 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
dea3101e 4420
3a70730a 4421 status = fc_block_scsi_eh(cmnd);
908e18e4 4422 if (status != 0 && status != SUCCESS)
3a70730a 4423 return status;
4f2e66c6
JS
4424
4425 spin_lock_irq(&phba->hbalock);
4426 /* driver queued commands are in process of being flushed */
4427 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
4428 spin_unlock_irq(&phba->hbalock);
4429 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4430 "3168 SCSI Layer abort requested I/O has been "
4431 "flushed by LLD.\n");
4432 return FAILED;
4433 }
4434
0bd4ca25 4435 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
eee8877e 4436 if (!lpfc_cmd) {
4f2e66c6 4437 spin_unlock_irq(&phba->hbalock);
eee8877e
JS
4438 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4439 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
5cd049a5 4440 "x%x ID %d LUN %d\n",
3a70730a 4441 SUCCESS, cmnd->device->id, cmnd->device->lun);
eee8877e
JS
4442 return SUCCESS;
4443 }
dea3101e 4444
4f2e66c6
JS
4445 iocb = &lpfc_cmd->cur_iocbq;
4446 /* the command is in process of being cancelled */
4447 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4448 spin_unlock_irq(&phba->hbalock);
4449 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4450 "3169 SCSI Layer abort requested I/O has been "
4451 "cancelled by LLD.\n");
4452 return FAILED;
4453 }
0bd4ca25
JSEC
4454 /*
4455 * If pCmd field of the corresponding lpfc_scsi_buf structure
4456 * points to a different SCSI command, then the driver has
4457 * already completed this command, but the midlayer did not
4f2e66c6 4458 * see the completion before the eh fired. Just return SUCCESS.
0bd4ca25 4459 */
4f2e66c6
JS
4460 if (lpfc_cmd->pCmd != cmnd) {
4461 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4462 "3170 SCSI Layer abort requested I/O has been "
4463 "completed by LLD.\n");
4464 goto out_unlock;
4465 }
dea3101e 4466
0bd4ca25 4467 BUG_ON(iocb->context1 != lpfc_cmd);
dea3101e 4468
4f2e66c6 4469 abtsiocb = __lpfc_sli_get_iocbq(phba);
0bd4ca25
JSEC
4470 if (abtsiocb == NULL) {
4471 ret = FAILED;
4f2e66c6 4472 goto out_unlock;
dea3101e 4473 }
4474
dea3101e 4475 /*
0bd4ca25
JSEC
4476 * The scsi command can not be in txq and it is in flight because the
4477 * pCmd is still pointig at the SCSI command we have to abort. There
4478 * is no need to search the txcmplq. Just send an abort to the FW.
dea3101e 4479 */
dea3101e 4480
0bd4ca25
JSEC
4481 cmd = &iocb->iocb;
4482 icmd = &abtsiocb->iocb;
4483 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4484 icmd->un.acxri.abortContextTag = cmd->ulpContext;
3772a991
JS
4485 if (phba->sli_rev == LPFC_SLI_REV4)
4486 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4487 else
4488 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 4489
0bd4ca25
JSEC
4490 icmd->ulpLe = 1;
4491 icmd->ulpClass = cmd->ulpClass;
5ffc266e
JS
4492
4493 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4494 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
341af102 4495 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 4496
2e0fef85 4497 if (lpfc_is_link_up(phba))
0bd4ca25
JSEC
4498 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4499 else
4500 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 4501
0bd4ca25 4502 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
2e0fef85 4503 abtsiocb->vport = vport;
4f2e66c6
JS
4504 /* no longer need the lock after this point */
4505 spin_unlock_irq(&phba->hbalock);
4506
3772a991
JS
4507 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4508 IOCB_ERROR) {
0bd4ca25
JSEC
4509 lpfc_sli_release_iocbq(phba, abtsiocb);
4510 ret = FAILED;
4511 goto out;
4512 }
dea3101e 4513
875fbdfe 4514 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
45ed1190
JS
4515 lpfc_sli_handle_fast_ring_event(phba,
4516 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
875fbdfe 4517
fa61a54e 4518 lpfc_cmd->waitq = &waitq;
0bd4ca25 4519 /* Wait for abort to complete */
fa61a54e
JS
4520 wait_event_timeout(waitq,
4521 (lpfc_cmd->pCmd != cmnd),
4522 (2*vport->cfg_devloss_tmo*HZ));
fa61a54e 4523 lpfc_cmd->waitq = NULL;
dea3101e 4524
0bd4ca25
JSEC
4525 if (lpfc_cmd->pCmd == cmnd) {
4526 ret = FAILED;
e8b62011
JS
4527 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4528 "0748 abort handler timed out waiting "
4529 "for abort to complete: ret %#x, ID %d, "
5cd049a5
CH
4530 "LUN %d\n",
4531 ret, cmnd->device->id, cmnd->device->lun);
dea3101e 4532 }
4f2e66c6 4533 goto out;
dea3101e 4534
4f2e66c6
JS
4535out_unlock:
4536 spin_unlock_irq(&phba->hbalock);
4537out:
e8b62011
JS
4538 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4539 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
5cd049a5
CH
4540 "LUN %d\n", ret, cmnd->device->id,
4541 cmnd->device->lun);
63c59c3b 4542 return ret;
8fa728a2
JG
4543}
4544
bbb9d180
JS
4545static char *
4546lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4547{
4548 switch (task_mgmt_cmd) {
4549 case FCP_ABORT_TASK_SET:
4550 return "ABORT_TASK_SET";
4551 case FCP_CLEAR_TASK_SET:
4552 return "FCP_CLEAR_TASK_SET";
4553 case FCP_BUS_RESET:
4554 return "FCP_BUS_RESET";
4555 case FCP_LUN_RESET:
4556 return "FCP_LUN_RESET";
4557 case FCP_TARGET_RESET:
4558 return "FCP_TARGET_RESET";
4559 case FCP_CLEAR_ACA:
4560 return "FCP_CLEAR_ACA";
4561 case FCP_TERMINATE_TASK:
4562 return "FCP_TERMINATE_TASK";
4563 default:
4564 return "unknown";
4565 }
4566}
4567
9bad7671 4568/**
bbb9d180
JS
4569 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4570 * @vport: The virtual port for which this call is being executed.
4571 * @rdata: Pointer to remote port local data
4572 * @tgt_id: Target ID of remote device.
4573 * @lun_id: Lun number for the TMF
4574 * @task_mgmt_cmd: type of TMF to send
9bad7671 4575 *
bbb9d180
JS
4576 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4577 * a remote port.
9bad7671 4578 *
bbb9d180
JS
4579 * Return Code:
4580 * 0x2003 - Error
4581 * 0x2002 - Success.
9bad7671 4582 **/
dea3101e 4583static int
bbb9d180
JS
4584lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4585 unsigned tgt_id, unsigned int lun_id,
4586 uint8_t task_mgmt_cmd)
dea3101e 4587{
2e0fef85 4588 struct lpfc_hba *phba = vport->phba;
0bd4ca25 4589 struct lpfc_scsi_buf *lpfc_cmd;
bbb9d180
JS
4590 struct lpfc_iocbq *iocbq;
4591 struct lpfc_iocbq *iocbqrsp;
5989b8d4 4592 struct lpfc_nodelist *pnode = rdata->pnode;
bbb9d180 4593 int ret;
915caaaf 4594 int status;
dea3101e 4595
5989b8d4 4596 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
915caaaf 4597 return FAILED;
bbb9d180 4598
19ca7609 4599 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
dea3101e 4600 if (lpfc_cmd == NULL)
915caaaf 4601 return FAILED;
dea3101e 4602 lpfc_cmd->timeout = 60;
0b18ac42 4603 lpfc_cmd->rdata = rdata;
dea3101e 4604
bbb9d180
JS
4605 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4606 task_mgmt_cmd);
915caaaf
JS
4607 if (!status) {
4608 lpfc_release_scsi_buf(phba, lpfc_cmd);
4609 return FAILED;
4610 }
dea3101e 4611
bbb9d180 4612 iocbq = &lpfc_cmd->cur_iocbq;
0bd4ca25 4613 iocbqrsp = lpfc_sli_get_iocbq(phba);
915caaaf
JS
4614 if (iocbqrsp == NULL) {
4615 lpfc_release_scsi_buf(phba, lpfc_cmd);
4616 return FAILED;
4617 }
bbb9d180 4618
e8b62011 4619 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
bbb9d180 4620 "0702 Issue %s to TGT %d LUN %d "
6d368e53 4621 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
bbb9d180 4622 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
6d368e53
JS
4623 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4624 iocbq->iocb_flag);
bbb9d180 4625
3772a991 4626 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
915caaaf 4627 iocbq, iocbqrsp, lpfc_cmd->timeout);
bbb9d180
JS
4628 if (status != IOCB_SUCCESS) {
4629 if (status == IOCB_TIMEDOUT) {
4630 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4631 ret = TIMEOUT_ERROR;
4632 } else
915caaaf 4633 ret = FAILED;
bbb9d180
JS
4634 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4635 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
6d368e53
JS
4636 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4637 "iocb_flag x%x\n",
bbb9d180
JS
4638 lpfc_taskmgmt_name(task_mgmt_cmd),
4639 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
6d368e53
JS
4640 iocbqrsp->iocb.un.ulpWord[4],
4641 iocbq->iocb_flag);
2a9bf3d0
JS
4642 } else if (status == IOCB_BUSY)
4643 ret = FAILED;
4644 else
bbb9d180
JS
4645 ret = SUCCESS;
4646
6175c02a 4647 lpfc_sli_release_iocbq(phba, iocbqrsp);
bbb9d180
JS
4648
4649 if (ret != TIMEOUT_ERROR)
4650 lpfc_release_scsi_buf(phba, lpfc_cmd);
4651
4652 return ret;
4653}
4654
4655/**
4656 * lpfc_chk_tgt_mapped -
4657 * @vport: The virtual port to check on
4658 * @cmnd: Pointer to scsi_cmnd data structure.
4659 *
4660 * This routine delays until the scsi target (aka rport) for the
4661 * command exists (is present and logged in) or we declare it non-existent.
4662 *
4663 * Return code :
4664 * 0x2003 - Error
4665 * 0x2002 - Success
4666 **/
4667static int
4668lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4669{
4670 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1c6f4ef5 4671 struct lpfc_nodelist *pnode;
bbb9d180
JS
4672 unsigned long later;
4673
1c6f4ef5
JS
4674 if (!rdata) {
4675 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4676 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4677 return FAILED;
4678 }
4679 pnode = rdata->pnode;
bbb9d180
JS
4680 /*
4681 * If target is not in a MAPPED state, delay until
4682 * target is rediscovered or devloss timeout expires.
4683 */
4684 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4685 while (time_after(later, jiffies)) {
4686 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4687 return FAILED;
4688 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4689 return SUCCESS;
4690 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
4691 rdata = cmnd->device->hostdata;
4692 if (!rdata)
4693 return FAILED;
4694 pnode = rdata->pnode;
4695 }
4696 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4697 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4698 return FAILED;
4699 return SUCCESS;
4700}
4701
4702/**
4703 * lpfc_reset_flush_io_context -
4704 * @vport: The virtual port (scsi_host) for the flush context
4705 * @tgt_id: If aborting by Target contect - specifies the target id
4706 * @lun_id: If aborting by Lun context - specifies the lun id
4707 * @context: specifies the context level to flush at.
4708 *
4709 * After a reset condition via TMF, we need to flush orphaned i/o
4710 * contexts from the adapter. This routine aborts any contexts
4711 * outstanding, then waits for their completions. The wait is
4712 * bounded by devloss_tmo though.
4713 *
4714 * Return code :
4715 * 0x2003 - Error
4716 * 0x2002 - Success
4717 **/
4718static int
4719lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4720 uint64_t lun_id, lpfc_ctx_cmd context)
4721{
4722 struct lpfc_hba *phba = vport->phba;
4723 unsigned long later;
4724 int cnt;
4725
4726 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
6175c02a 4727 if (cnt)
51ef4c26 4728 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
bbb9d180 4729 tgt_id, lun_id, context);
915caaaf
JS
4730 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4731 while (time_after(later, jiffies) && cnt) {
4732 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
bbb9d180 4733 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
dea3101e 4734 }
dea3101e 4735 if (cnt) {
e8b62011 4736 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
bbb9d180
JS
4737 "0724 I/O flush failure for context %s : cnt x%x\n",
4738 ((context == LPFC_CTX_LUN) ? "LUN" :
4739 ((context == LPFC_CTX_TGT) ? "TGT" :
4740 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
4741 cnt);
4742 return FAILED;
dea3101e 4743 }
bbb9d180
JS
4744 return SUCCESS;
4745}
4746
4747/**
4748 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
4749 * @cmnd: Pointer to scsi_cmnd data structure.
4750 *
4751 * This routine does a device reset by sending a LUN_RESET task management
4752 * command.
4753 *
4754 * Return code :
4755 * 0x2003 - Error
4756 * 0x2002 - Success
4757 **/
4758static int
4759lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
4760{
4761 struct Scsi_Host *shost = cmnd->device->host;
4762 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4763 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1c6f4ef5 4764 struct lpfc_nodelist *pnode;
bbb9d180
JS
4765 unsigned tgt_id = cmnd->device->id;
4766 unsigned int lun_id = cmnd->device->lun;
4767 struct lpfc_scsi_event_header scsi_event;
3a70730a 4768 int status, ret = SUCCESS;
bbb9d180 4769
1c6f4ef5
JS
4770 if (!rdata) {
4771 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4772 "0798 Device Reset rport failure: rdata x%p\n", rdata);
4773 return FAILED;
4774 }
4775 pnode = rdata->pnode;
589a52d6 4776 status = fc_block_scsi_eh(cmnd);
908e18e4 4777 if (status != 0 && status != SUCCESS)
589a52d6 4778 return status;
bbb9d180
JS
4779
4780 status = lpfc_chk_tgt_mapped(vport, cmnd);
4781 if (status == FAILED) {
4782 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4783 "0721 Device Reset rport failure: rdata x%p\n", rdata);
4784 return FAILED;
4785 }
4786
4787 scsi_event.event_type = FC_REG_SCSI_EVENT;
4788 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
4789 scsi_event.lun = lun_id;
4790 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4791 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4792
4793 fc_host_post_vendor_event(shost, fc_get_event_number(),
4794 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4795
4796 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4797 FCP_LUN_RESET);
4798
4799 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4800 "0713 SCSI layer issued Device Reset (%d, %d) "
4801 "return x%x\n", tgt_id, lun_id, status);
4802
4803 /*
4804 * We have to clean up i/o as : they may be orphaned by the TMF;
4805 * or if the TMF failed, they may be in an indeterminate state.
4806 * So, continue on.
4807 * We will report success if all the i/o aborts successfully.
4808 */
3a70730a 4809 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
bbb9d180 4810 LPFC_CTX_LUN);
3a70730a 4811 return ret;
bbb9d180
JS
4812}
4813
4814/**
4815 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
4816 * @cmnd: Pointer to scsi_cmnd data structure.
4817 *
4818 * This routine does a target reset by sending a TARGET_RESET task management
4819 * command.
4820 *
4821 * Return code :
4822 * 0x2003 - Error
4823 * 0x2002 - Success
4824 **/
4825static int
4826lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
4827{
4828 struct Scsi_Host *shost = cmnd->device->host;
4829 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4830 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1c6f4ef5 4831 struct lpfc_nodelist *pnode;
bbb9d180
JS
4832 unsigned tgt_id = cmnd->device->id;
4833 unsigned int lun_id = cmnd->device->lun;
4834 struct lpfc_scsi_event_header scsi_event;
3a70730a 4835 int status, ret = SUCCESS;
bbb9d180 4836
1c6f4ef5
JS
4837 if (!rdata) {
4838 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4839 "0799 Target Reset rport failure: rdata x%p\n", rdata);
4840 return FAILED;
4841 }
4842 pnode = rdata->pnode;
589a52d6 4843 status = fc_block_scsi_eh(cmnd);
908e18e4 4844 if (status != 0 && status != SUCCESS)
589a52d6 4845 return status;
bbb9d180
JS
4846
4847 status = lpfc_chk_tgt_mapped(vport, cmnd);
4848 if (status == FAILED) {
4849 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4850 "0722 Target Reset rport failure: rdata x%p\n", rdata);
4851 return FAILED;
4852 }
4853
4854 scsi_event.event_type = FC_REG_SCSI_EVENT;
4855 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
4856 scsi_event.lun = 0;
4857 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4858 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4859
4860 fc_host_post_vendor_event(shost, fc_get_event_number(),
4861 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4862
4863 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4864 FCP_TARGET_RESET);
4865
4866 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4867 "0723 SCSI layer issued Target Reset (%d, %d) "
4868 "return x%x\n", tgt_id, lun_id, status);
4869
4870 /*
4871 * We have to clean up i/o as : they may be orphaned by the TMF;
4872 * or if the TMF failed, they may be in an indeterminate state.
4873 * So, continue on.
4874 * We will report success if all the i/o aborts successfully.
4875 */
3a70730a
JS
4876 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4877 LPFC_CTX_TGT);
4878 return ret;
dea3101e 4879}
4880
9bad7671 4881/**
3621a710 4882 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
9bad7671
JS
4883 * @cmnd: Pointer to scsi_cmnd data structure.
4884 *
bbb9d180
JS
4885 * This routine does target reset to all targets on @cmnd->device->host.
4886 * This emulates Parallel SCSI Bus Reset Semantics.
9bad7671 4887 *
bbb9d180
JS
4888 * Return code :
4889 * 0x2003 - Error
4890 * 0x2002 - Success
9bad7671 4891 **/
94d0e7b8 4892static int
7054a606 4893lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea3101e 4894{
2e0fef85
JS
4895 struct Scsi_Host *shost = cmnd->device->host;
4896 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea3101e 4897 struct lpfc_nodelist *ndlp = NULL;
ea2151b4 4898 struct lpfc_scsi_event_header scsi_event;
bbb9d180
JS
4899 int match;
4900 int ret = SUCCESS, status, i;
ea2151b4
JS
4901
4902 scsi_event.event_type = FC_REG_SCSI_EVENT;
4903 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
4904 scsi_event.lun = 0;
4905 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
4906 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
4907
bbb9d180
JS
4908 fc_host_post_vendor_event(shost, fc_get_event_number(),
4909 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea3101e 4910
bf08611b 4911 status = fc_block_scsi_eh(cmnd);
908e18e4 4912 if (status != 0 && status != SUCCESS)
bf08611b 4913 return status;
bbb9d180 4914
dea3101e 4915 /*
4916 * Since the driver manages a single bus device, reset all
4917 * targets known to the driver. Should any target reset
4918 * fail, this routine returns failure to the midlayer.
4919 */
e17da18e 4920 for (i = 0; i < LPFC_MAX_TARGET; i++) {
685f0bf7 4921 /* Search for mapped node by target ID */
dea3101e 4922 match = 0;
2e0fef85
JS
4923 spin_lock_irq(shost->host_lock);
4924 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
4925 if (!NLP_CHK_NODE_ACT(ndlp))
4926 continue;
685f0bf7 4927 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
915caaaf 4928 ndlp->nlp_sid == i &&
685f0bf7 4929 ndlp->rport) {
dea3101e 4930 match = 1;
4931 break;
4932 }
4933 }
2e0fef85 4934 spin_unlock_irq(shost->host_lock);
dea3101e 4935 if (!match)
4936 continue;
bbb9d180
JS
4937
4938 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
4939 i, 0, FCP_TARGET_RESET);
4940
4941 if (status != SUCCESS) {
e8b62011
JS
4942 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4943 "0700 Bus Reset on target %d failed\n",
4944 i);
915caaaf 4945 ret = FAILED;
dea3101e 4946 }
4947 }
6175c02a 4948 /*
bbb9d180
JS
4949 * We have to clean up i/o as : they may be orphaned by the TMFs
4950 * above; or if any of the TMFs failed, they may be in an
4951 * indeterminate state.
4952 * We will report success if all the i/o aborts successfully.
6175c02a 4953 */
bbb9d180
JS
4954
4955 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
4956 if (status != SUCCESS)
0bd4ca25 4957 ret = FAILED;
bbb9d180 4958
e8b62011
JS
4959 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4960 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea3101e 4961 return ret;
4962}
4963
27b01b82
JS
4964/**
4965 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
4966 * @cmnd: Pointer to scsi_cmnd data structure.
4967 *
4968 * This routine does host reset to the adaptor port. It brings the HBA
4969 * offline, performs a board restart, and then brings the board back online.
4970 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
4971 * reject all outstanding SCSI commands to the host and error returned
4972 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
4973 * of error handling, it will only return error if resetting of the adapter
4974 * is not successful; in all other cases, will return success.
4975 *
4976 * Return code :
4977 * 0x2003 - Error
4978 * 0x2002 - Success
4979 **/
4980static int
4981lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
4982{
4983 struct Scsi_Host *shost = cmnd->device->host;
4984 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4985 struct lpfc_hba *phba = vport->phba;
4986 int rc, ret = SUCCESS;
4987
618a5230 4988 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
27b01b82
JS
4989 lpfc_offline(phba);
4990 rc = lpfc_sli_brdrestart(phba);
4991 if (rc)
4992 ret = FAILED;
4993 lpfc_online(phba);
4994 lpfc_unblock_mgmt_io(phba);
4995
4996 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
4997 "3172 SCSI layer issued Host Reset Data: x%x\n", ret);
4998 return ret;
4999}
5000
9bad7671 5001/**
3621a710 5002 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
9bad7671
JS
5003 * @sdev: Pointer to scsi_device.
5004 *
5005 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5006 * globally available list of scsi buffers. This routine also makes sure scsi
5007 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5008 * of scsi buffer exists for the lifetime of the driver.
5009 *
5010 * Return codes:
5011 * non-0 - Error
5012 * 0 - Success
5013 **/
dea3101e 5014static int
5015lpfc_slave_alloc(struct scsi_device *sdev)
5016{
2e0fef85
JS
5017 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5018 struct lpfc_hba *phba = vport->phba;
19a7b4ae 5019 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3772a991 5020 uint32_t total = 0;
dea3101e 5021 uint32_t num_to_alloc = 0;
3772a991 5022 int num_allocated = 0;
d7c47992 5023 uint32_t sdev_cnt;
dea3101e 5024
19a7b4ae 5025 if (!rport || fc_remote_port_chkready(rport))
dea3101e 5026 return -ENXIO;
5027
19a7b4ae 5028 sdev->hostdata = rport->dd_data;
d7c47992 5029 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea3101e 5030
5031 /*
5032 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5033 * available list of scsi buffers. Don't allocate more than the
a784efbf
JSEC
5034 * HBA limit conveyed to the midlayer via the host structure. The
5035 * formula accounts for the lun_queue_depth + error handlers + 1
5036 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea3101e 5037 */
5038 total = phba->total_scsi_bufs;
3de2a653 5039 num_to_alloc = vport->cfg_lun_queue_depth + 2;
92d7f7b0 5040
d7c47992
JS
5041 /* If allocated buffers are enough do nothing */
5042 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5043 return 0;
5044
92d7f7b0
JS
5045 /* Allow some exchanges to be available always to complete discovery */
5046 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
5047 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5048 "0704 At limitation of %d preallocated "
5049 "command buffers\n", total);
dea3101e 5050 return 0;
92d7f7b0
JS
5051 /* Allow some exchanges to be available always to complete discovery */
5052 } else if (total + num_to_alloc >
5053 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
5054 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5055 "0705 Allocation request of %d "
5056 "command buffers will exceed max of %d. "
5057 "Reducing allocation request to %d.\n",
5058 num_to_alloc, phba->cfg_hba_queue_depth,
5059 (phba->cfg_hba_queue_depth - total));
dea3101e 5060 num_to_alloc = phba->cfg_hba_queue_depth - total;
5061 }
3772a991
JS
5062 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5063 if (num_to_alloc != num_allocated) {
5064 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5065 "0708 Allocation request of %d "
5066 "command buffers did not succeed. "
5067 "Allocated %d buffers.\n",
5068 num_to_alloc, num_allocated);
dea3101e 5069 }
1c6f4ef5
JS
5070 if (num_allocated > 0)
5071 phba->total_scsi_bufs += num_allocated;
dea3101e 5072 return 0;
5073}
5074
9bad7671 5075/**
3621a710 5076 * lpfc_slave_configure - scsi_host_template slave_configure entry point
9bad7671
JS
5077 * @sdev: Pointer to scsi_device.
5078 *
5079 * This routine configures following items
5080 * - Tag command queuing support for @sdev if supported.
9bad7671
JS
5081 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5082 *
5083 * Return codes:
5084 * 0 - Success
5085 **/
dea3101e 5086static int
5087lpfc_slave_configure(struct scsi_device *sdev)
5088{
2e0fef85
JS
5089 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5090 struct lpfc_hba *phba = vport->phba;
dea3101e 5091
5092 if (sdev->tagged_supported)
3de2a653 5093 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 5094 else
3de2a653 5095 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 5096
875fbdfe 5097 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
5098 lpfc_sli_handle_fast_ring_event(phba,
5099 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
875fbdfe
JSEC
5100 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5101 lpfc_poll_rearm_timer(phba);
5102 }
5103
dea3101e 5104 return 0;
5105}
5106
9bad7671 5107/**
3621a710 5108 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
9bad7671
JS
5109 * @sdev: Pointer to scsi_device.
5110 *
5111 * This routine sets @sdev hostatdata filed to null.
5112 **/
dea3101e 5113static void
5114lpfc_slave_destroy(struct scsi_device *sdev)
5115{
d7c47992
JS
5116 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5117 struct lpfc_hba *phba = vport->phba;
5118 atomic_dec(&phba->sdev_cnt);
dea3101e 5119 sdev->hostdata = NULL;
5120 return;
5121}
5122
92d7f7b0 5123
dea3101e 5124struct scsi_host_template lpfc_template = {
5125 .module = THIS_MODULE,
5126 .name = LPFC_DRIVER_NAME,
5127 .info = lpfc_info,
5128 .queuecommand = lpfc_queuecommand,
5129 .eh_abort_handler = lpfc_abort_handler,
bbb9d180
JS
5130 .eh_device_reset_handler = lpfc_device_reset_handler,
5131 .eh_target_reset_handler = lpfc_target_reset_handler,
7054a606 5132 .eh_bus_reset_handler = lpfc_bus_reset_handler,
27b01b82 5133 .eh_host_reset_handler = lpfc_host_reset_handler,
dea3101e 5134 .slave_alloc = lpfc_slave_alloc,
5135 .slave_configure = lpfc_slave_configure,
5136 .slave_destroy = lpfc_slave_destroy,
47a8617c 5137 .scan_finished = lpfc_scan_finished,
dea3101e 5138 .this_id = -1,
83108bd3 5139 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea3101e 5140 .cmd_per_lun = LPFC_CMD_PER_LUN,
5141 .use_clustering = ENABLE_CLUSTERING,
2e0fef85 5142 .shost_attrs = lpfc_hba_attrs,
564b2960 5143 .max_sectors = 0xFFFF,
f1c3b0fc 5144 .vendor_id = LPFC_NL_VENDOR_ID,
5ffc266e 5145 .change_queue_depth = lpfc_change_queue_depth,
dea3101e 5146};
3de2a653
JS
5147
5148struct scsi_host_template lpfc_vport_template = {
5149 .module = THIS_MODULE,
5150 .name = LPFC_DRIVER_NAME,
5151 .info = lpfc_info,
5152 .queuecommand = lpfc_queuecommand,
5153 .eh_abort_handler = lpfc_abort_handler,
bbb9d180
JS
5154 .eh_device_reset_handler = lpfc_device_reset_handler,
5155 .eh_target_reset_handler = lpfc_target_reset_handler,
3de2a653
JS
5156 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5157 .slave_alloc = lpfc_slave_alloc,
5158 .slave_configure = lpfc_slave_configure,
5159 .slave_destroy = lpfc_slave_destroy,
5160 .scan_finished = lpfc_scan_finished,
5161 .this_id = -1,
83108bd3 5162 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3de2a653
JS
5163 .cmd_per_lun = LPFC_CMD_PER_LUN,
5164 .use_clustering = ENABLE_CLUSTERING,
5165 .shost_attrs = lpfc_vport_attrs,
5166 .max_sectors = 0xFFFF,
5ffc266e 5167 .change_queue_depth = lpfc_change_queue_depth,
3de2a653 5168};