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