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