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