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