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