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