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