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