pci: add module.h to files implicitly relying on its presence.
[linux-2.6-block.git] / drivers / scsi / lpfc / lpfc_scsi.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
792581de 4 * Copyright (C) 2004-2011 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>
a90f5684 24#include <linux/delay.h>
e2a0a9d6 25#include <asm/unaligned.h>
dea3101e 26
27#include <scsi/scsi.h>
28#include <scsi/scsi_device.h>
e2a0a9d6 29#include <scsi/scsi_eh.h>
dea3101e 30#include <scsi/scsi_host.h>
31#include <scsi/scsi_tcq.h>
32#include <scsi/scsi_transport_fc.h>
33
34#include "lpfc_version.h"
da0436e9 35#include "lpfc_hw4.h"
dea3101e 36#include "lpfc_hw.h"
37#include "lpfc_sli.h"
da0436e9 38#include "lpfc_sli4.h"
ea2151b4 39#include "lpfc_nl.h"
dea3101e 40#include "lpfc_disc.h"
41#include "lpfc_scsi.h"
42#include "lpfc.h"
43#include "lpfc_logmsg.h"
44#include "lpfc_crtn.h"
92d7f7b0 45#include "lpfc_vport.h"
dea3101e 46
47#define LPFC_RESET_WAIT 2
48#define LPFC_ABORT_WAIT 2
49
e2a0a9d6
JS
50int _dump_buf_done;
51
52static char *dif_op_str[] = {
53 "SCSI_PROT_NORMAL",
54 "SCSI_PROT_READ_INSERT",
55 "SCSI_PROT_WRITE_STRIP",
56 "SCSI_PROT_READ_STRIP",
57 "SCSI_PROT_WRITE_INSERT",
58 "SCSI_PROT_READ_PASS",
59 "SCSI_PROT_WRITE_PASS",
e2a0a9d6 60};
f9bb2da1
JS
61
62struct scsi_dif_tuple {
63 __be16 guard_tag; /* Checksum */
64 __be16 app_tag; /* Opaque storage */
65 __be32 ref_tag; /* Target LBA or indirect LBA */
66};
67
da0436e9
JS
68static void
69lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
1c6f4ef5
JS
70static void
71lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
e2a0a9d6
JS
72
73static void
6a9c52cf 74lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
e2a0a9d6
JS
75{
76 void *src, *dst;
77 struct scatterlist *sgde = scsi_sglist(cmnd);
78
79 if (!_dump_buf_data) {
6a9c52cf
JS
80 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
81 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
e2a0a9d6
JS
82 __func__);
83 return;
84 }
85
86
87 if (!sgde) {
6a9c52cf
JS
88 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
89 "9051 BLKGRD: ERROR: data scatterlist is null\n");
e2a0a9d6
JS
90 return;
91 }
92
93 dst = (void *) _dump_buf_data;
94 while (sgde) {
95 src = sg_virt(sgde);
96 memcpy(dst, src, sgde->length);
97 dst += sgde->length;
98 sgde = sg_next(sgde);
99 }
100}
101
102static void
6a9c52cf 103lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
e2a0a9d6
JS
104{
105 void *src, *dst;
106 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
107
108 if (!_dump_buf_dif) {
6a9c52cf
JS
109 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
110 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
e2a0a9d6
JS
111 __func__);
112 return;
113 }
114
115 if (!sgde) {
6a9c52cf
JS
116 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
117 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
e2a0a9d6
JS
118 return;
119 }
120
121 dst = _dump_buf_dif;
122 while (sgde) {
123 src = sg_virt(sgde);
124 memcpy(dst, src, sgde->length);
125 dst += sgde->length;
126 sgde = sg_next(sgde);
127 }
128}
129
f1126688
JS
130/**
131 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
132 * @phba: Pointer to HBA object.
133 * @lpfc_cmd: lpfc scsi command object pointer.
134 *
135 * This function is called from the lpfc_prep_task_mgmt_cmd function to
136 * set the last bit in the response sge entry.
137 **/
138static void
139lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
140 struct lpfc_scsi_buf *lpfc_cmd)
141{
142 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
143 if (sgl) {
144 sgl += 1;
145 sgl->word2 = le32_to_cpu(sgl->word2);
146 bf_set(lpfc_sli4_sge_last, sgl, 1);
147 sgl->word2 = cpu_to_le32(sgl->word2);
148 }
149}
150
ea2151b4 151/**
3621a710 152 * lpfc_update_stats - Update statistical data for the command completion
ea2151b4
JS
153 * @phba: Pointer to HBA object.
154 * @lpfc_cmd: lpfc scsi command object pointer.
155 *
156 * This function is called when there is a command completion and this
157 * function updates the statistical data for the command completion.
158 **/
159static void
160lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
161{
162 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
163 struct lpfc_nodelist *pnode = rdata->pnode;
164 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
165 unsigned long flags;
166 struct Scsi_Host *shost = cmd->device->host;
167 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
168 unsigned long latency;
169 int i;
170
171 if (cmd->result)
172 return;
173
9f1e1b50
JS
174 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
175
ea2151b4
JS
176 spin_lock_irqsave(shost->host_lock, flags);
177 if (!vport->stat_data_enabled ||
178 vport->stat_data_blocked ||
5989b8d4 179 !pnode ||
ea2151b4
JS
180 !pnode->lat_data ||
181 (phba->bucket_type == LPFC_NO_BUCKET)) {
182 spin_unlock_irqrestore(shost->host_lock, flags);
183 return;
184 }
ea2151b4
JS
185
186 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
187 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
188 phba->bucket_step;
9f1e1b50
JS
189 /* check array subscript bounds */
190 if (i < 0)
191 i = 0;
192 else if (i >= LPFC_MAX_BUCKET_COUNT)
193 i = LPFC_MAX_BUCKET_COUNT - 1;
ea2151b4
JS
194 } else {
195 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
196 if (latency <= (phba->bucket_base +
197 ((1<<i)*phba->bucket_step)))
198 break;
199 }
200
201 pnode->lat_data[i].cmd_count++;
202 spin_unlock_irqrestore(shost->host_lock, flags);
203}
204
ea2151b4 205/**
3621a710 206 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
ea2151b4
JS
207 * @phba: Pointer to HBA context object.
208 * @vport: Pointer to vport object.
209 * @ndlp: Pointer to FC node associated with the target.
210 * @lun: Lun number of the scsi device.
211 * @old_val: Old value of the queue depth.
212 * @new_val: New value of the queue depth.
213 *
214 * This function sends an event to the mgmt application indicating
215 * there is a change in the scsi device queue depth.
216 **/
217static void
218lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
219 struct lpfc_vport *vport,
220 struct lpfc_nodelist *ndlp,
221 uint32_t lun,
222 uint32_t old_val,
223 uint32_t new_val)
224{
225 struct lpfc_fast_path_event *fast_path_evt;
226 unsigned long flags;
227
228 fast_path_evt = lpfc_alloc_fast_evt(phba);
229 if (!fast_path_evt)
230 return;
231
232 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
233 FC_REG_SCSI_EVENT;
234 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
235 LPFC_EVENT_VARQUEDEPTH;
236
237 /* Report all luns with change in queue depth */
238 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
239 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
240 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
241 &ndlp->nlp_portname, sizeof(struct lpfc_name));
242 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
243 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
244 }
245
246 fast_path_evt->un.queue_depth_evt.oldval = old_val;
247 fast_path_evt->un.queue_depth_evt.newval = new_val;
248 fast_path_evt->vport = vport;
249
250 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
251 spin_lock_irqsave(&phba->hbalock, flags);
252 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
253 spin_unlock_irqrestore(&phba->hbalock, flags);
254 lpfc_worker_wake_up(phba);
255
256 return;
257}
258
5ffc266e
JS
259/**
260 * lpfc_change_queue_depth - Alter scsi device queue depth
261 * @sdev: Pointer the scsi device on which to change the queue depth.
262 * @qdepth: New queue depth to set the sdev to.
263 * @reason: The reason for the queue depth change.
264 *
265 * This function is called by the midlayer and the LLD to alter the queue
266 * depth for a scsi device. This function sets the queue depth to the new
267 * value and sends an event out to log the queue depth change.
268 **/
269int
270lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
271{
272 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
273 struct lpfc_hba *phba = vport->phba;
274 struct lpfc_rport_data *rdata;
275 unsigned long new_queue_depth, old_queue_depth;
276
277 old_queue_depth = sdev->queue_depth;
278 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
279 new_queue_depth = sdev->queue_depth;
280 rdata = sdev->hostdata;
281 if (rdata)
282 lpfc_send_sdev_queuedepth_change_event(phba, vport,
283 rdata->pnode, sdev->lun,
284 old_queue_depth,
285 new_queue_depth);
286 return sdev->queue_depth;
287}
288
9bad7671 289/**
3621a710 290 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
9bad7671
JS
291 * @phba: The Hba for which this call is being executed.
292 *
293 * This routine is called when there is resource error in driver or firmware.
294 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
295 * posts at most 1 event each second. This routine wakes up worker thread of
296 * @phba to process WORKER_RAM_DOWN_EVENT event.
297 *
298 * This routine should be called with no lock held.
299 **/
92d7f7b0 300void
eaf15d5b 301lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
92d7f7b0
JS
302{
303 unsigned long flags;
5e9d9b82 304 uint32_t evt_posted;
92d7f7b0
JS
305
306 spin_lock_irqsave(&phba->hbalock, flags);
307 atomic_inc(&phba->num_rsrc_err);
308 phba->last_rsrc_error_time = jiffies;
309
310 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
311 spin_unlock_irqrestore(&phba->hbalock, flags);
312 return;
313 }
314
315 phba->last_ramp_down_time = jiffies;
316
317 spin_unlock_irqrestore(&phba->hbalock, flags);
318
319 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
5e9d9b82
JS
320 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
321 if (!evt_posted)
92d7f7b0 322 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
92d7f7b0
JS
323 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
324
5e9d9b82
JS
325 if (!evt_posted)
326 lpfc_worker_wake_up(phba);
92d7f7b0
JS
327 return;
328}
329
9bad7671 330/**
3621a710 331 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
9bad7671
JS
332 * @phba: The Hba for which this call is being executed.
333 *
334 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
335 * post at most 1 event every 5 minute after last_ramp_up_time or
336 * last_rsrc_error_time. This routine wakes up worker thread of @phba
337 * to process WORKER_RAM_DOWN_EVENT event.
338 *
339 * This routine should be called with no lock held.
340 **/
92d7f7b0 341static inline void
3de2a653 342lpfc_rampup_queue_depth(struct lpfc_vport *vport,
a257bf90 343 uint32_t queue_depth)
92d7f7b0
JS
344{
345 unsigned long flags;
3de2a653 346 struct lpfc_hba *phba = vport->phba;
5e9d9b82 347 uint32_t evt_posted;
92d7f7b0
JS
348 atomic_inc(&phba->num_cmd_success);
349
a257bf90 350 if (vport->cfg_lun_queue_depth <= queue_depth)
92d7f7b0 351 return;
92d7f7b0 352 spin_lock_irqsave(&phba->hbalock, flags);
5ffc266e
JS
353 if (time_before(jiffies,
354 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
355 time_before(jiffies,
356 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
92d7f7b0
JS
357 spin_unlock_irqrestore(&phba->hbalock, flags);
358 return;
359 }
92d7f7b0
JS
360 phba->last_ramp_up_time = jiffies;
361 spin_unlock_irqrestore(&phba->hbalock, flags);
362
363 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
5e9d9b82
JS
364 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
365 if (!evt_posted)
92d7f7b0 366 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
92d7f7b0
JS
367 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
368
5e9d9b82
JS
369 if (!evt_posted)
370 lpfc_worker_wake_up(phba);
371 return;
92d7f7b0
JS
372}
373
9bad7671 374/**
3621a710 375 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
9bad7671
JS
376 * @phba: The Hba for which this call is being executed.
377 *
378 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
379 * thread.This routine reduces queue depth for all scsi device on each vport
380 * associated with @phba.
381 **/
92d7f7b0
JS
382void
383lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
384{
549e55cd
JS
385 struct lpfc_vport **vports;
386 struct Scsi_Host *shost;
92d7f7b0 387 struct scsi_device *sdev;
5ffc266e 388 unsigned long new_queue_depth;
92d7f7b0 389 unsigned long num_rsrc_err, num_cmd_success;
549e55cd 390 int i;
92d7f7b0
JS
391
392 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
393 num_cmd_success = atomic_read(&phba->num_cmd_success);
394
549e55cd
JS
395 vports = lpfc_create_vport_work_array(phba);
396 if (vports != NULL)
21e9a0a5 397 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd
JS
398 shost = lpfc_shost_from_vport(vports[i]);
399 shost_for_each_device(sdev, shost) {
92d7f7b0 400 new_queue_depth =
549e55cd
JS
401 sdev->queue_depth * num_rsrc_err /
402 (num_rsrc_err + num_cmd_success);
403 if (!new_queue_depth)
404 new_queue_depth = sdev->queue_depth - 1;
405 else
406 new_queue_depth = sdev->queue_depth -
407 new_queue_depth;
5ffc266e
JS
408 lpfc_change_queue_depth(sdev, new_queue_depth,
409 SCSI_QDEPTH_DEFAULT);
549e55cd 410 }
92d7f7b0 411 }
09372820 412 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0
JS
413 atomic_set(&phba->num_rsrc_err, 0);
414 atomic_set(&phba->num_cmd_success, 0);
415}
416
9bad7671 417/**
3621a710 418 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
9bad7671
JS
419 * @phba: The Hba for which this call is being executed.
420 *
421 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
422 * thread.This routine increases queue depth for all scsi device on each vport
423 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
424 * num_cmd_success to zero.
425 **/
92d7f7b0
JS
426void
427lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
428{
549e55cd
JS
429 struct lpfc_vport **vports;
430 struct Scsi_Host *shost;
92d7f7b0 431 struct scsi_device *sdev;
549e55cd
JS
432 int i;
433
434 vports = lpfc_create_vport_work_array(phba);
435 if (vports != NULL)
21e9a0a5 436 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
549e55cd
JS
437 shost = lpfc_shost_from_vport(vports[i]);
438 shost_for_each_device(sdev, shost) {
97eab634
JS
439 if (vports[i]->cfg_lun_queue_depth <=
440 sdev->queue_depth)
441 continue;
5ffc266e
JS
442 lpfc_change_queue_depth(sdev,
443 sdev->queue_depth+1,
444 SCSI_QDEPTH_RAMP_UP);
549e55cd 445 }
92d7f7b0 446 }
09372820 447 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0
JS
448 atomic_set(&phba->num_rsrc_err, 0);
449 atomic_set(&phba->num_cmd_success, 0);
450}
451
a8e497d5 452/**
3621a710 453 * lpfc_scsi_dev_block - set all scsi hosts to block state
a8e497d5
JS
454 * @phba: Pointer to HBA context object.
455 *
456 * This function walks vport list and set each SCSI host to block state
457 * by invoking fc_remote_port_delete() routine. This function is invoked
458 * with EEH when device's PCI slot has been permanently disabled.
459 **/
460void
461lpfc_scsi_dev_block(struct lpfc_hba *phba)
462{
463 struct lpfc_vport **vports;
464 struct Scsi_Host *shost;
465 struct scsi_device *sdev;
466 struct fc_rport *rport;
467 int i;
468
469 vports = lpfc_create_vport_work_array(phba);
470 if (vports != NULL)
21e9a0a5 471 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
a8e497d5
JS
472 shost = lpfc_shost_from_vport(vports[i]);
473 shost_for_each_device(sdev, shost) {
474 rport = starget_to_rport(scsi_target(sdev));
475 fc_remote_port_delete(rport);
476 }
477 }
478 lpfc_destroy_vport_work_array(phba, vports);
479}
480
9bad7671 481/**
3772a991 482 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
9bad7671 483 * @vport: The virtual port for which this call being executed.
3772a991 484 * @num_to_allocate: The requested number of buffers to allocate.
9bad7671 485 *
3772a991
JS
486 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
487 * the scsi buffer contains all the necessary information needed to initiate
488 * a SCSI I/O. The non-DMAable buffer region contains information to build
489 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
490 * and the initial BPL. In addition to allocating memory, the FCP CMND and
491 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
9bad7671
JS
492 *
493 * Return codes:
3772a991
JS
494 * int - number of scsi buffers that were allocated.
495 * 0 = failure, less than num_to_alloc is a partial failure.
9bad7671 496 **/
3772a991
JS
497static int
498lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea3101e 499{
2e0fef85 500 struct lpfc_hba *phba = vport->phba;
dea3101e 501 struct lpfc_scsi_buf *psb;
502 struct ulp_bde64 *bpl;
503 IOCB_t *iocb;
34b02dcd
JS
504 dma_addr_t pdma_phys_fcp_cmd;
505 dma_addr_t pdma_phys_fcp_rsp;
506 dma_addr_t pdma_phys_bpl;
604a3e30 507 uint16_t iotag;
3772a991 508 int bcnt;
dea3101e 509
3772a991
JS
510 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
511 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
512 if (!psb)
513 break;
dea3101e 514
3772a991
JS
515 /*
516 * Get memory from the pci pool to map the virt space to pci
517 * bus space for an I/O. The DMA buffer includes space for the
518 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
519 * necessary to support the sg_tablesize.
520 */
521 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
522 GFP_KERNEL, &psb->dma_handle);
523 if (!psb->data) {
524 kfree(psb);
525 break;
526 }
527
528 /* Initialize virtual ptrs to dma_buf region. */
529 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
530
531 /* Allocate iotag for psb->cur_iocbq. */
532 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
533 if (iotag == 0) {
534 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
535 psb->data, psb->dma_handle);
536 kfree(psb);
537 break;
538 }
539 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
540
541 psb->fcp_cmnd = psb->data;
542 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
543 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
34b02dcd 544 sizeof(struct fcp_rsp);
dea3101e 545
3772a991
JS
546 /* Initialize local short-hand pointers. */
547 bpl = psb->fcp_bpl;
548 pdma_phys_fcp_cmd = psb->dma_handle;
549 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
550 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
551 sizeof(struct fcp_rsp);
552
553 /*
554 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
555 * are sg list bdes. Initialize the first two and leave the
556 * rest for queuecommand.
557 */
558 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
559 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
560 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
561 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
562 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
563
564 /* Setup the physical region for the FCP RSP */
565 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
566 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
567 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
568 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
569 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
570
571 /*
572 * Since the IOCB for the FCP I/O is built into this
573 * lpfc_scsi_buf, initialize it with all known data now.
574 */
575 iocb = &psb->cur_iocbq.iocb;
576 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
577 if ((phba->sli_rev == 3) &&
578 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
579 /* fill in immediate fcp command BDE */
580 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
581 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
582 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
583 unsli3.fcp_ext.icd);
584 iocb->un.fcpi64.bdl.addrHigh = 0;
585 iocb->ulpBdeCount = 0;
586 iocb->ulpLe = 0;
25985edc 587 /* fill in response BDE */
3772a991
JS
588 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
589 BUFF_TYPE_BDE_64;
590 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
591 sizeof(struct fcp_rsp);
592 iocb->unsli3.fcp_ext.rbde.addrLow =
593 putPaddrLow(pdma_phys_fcp_rsp);
594 iocb->unsli3.fcp_ext.rbde.addrHigh =
595 putPaddrHigh(pdma_phys_fcp_rsp);
596 } else {
597 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
598 iocb->un.fcpi64.bdl.bdeSize =
599 (2 * sizeof(struct ulp_bde64));
600 iocb->un.fcpi64.bdl.addrLow =
601 putPaddrLow(pdma_phys_bpl);
602 iocb->un.fcpi64.bdl.addrHigh =
603 putPaddrHigh(pdma_phys_bpl);
604 iocb->ulpBdeCount = 1;
605 iocb->ulpLe = 1;
606 }
607 iocb->ulpClass = CLASS3;
608 psb->status = IOSTAT_SUCCESS;
da0436e9 609 /* Put it back into the SCSI buffer list */
eee8877e 610 psb->cur_iocbq.context1 = psb;
1c6f4ef5 611 lpfc_release_scsi_buf_s3(phba, psb);
dea3101e 612
34b02dcd 613 }
dea3101e 614
3772a991 615 return bcnt;
dea3101e 616}
617
1151e3ec
JS
618/**
619 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
620 * @vport: pointer to lpfc vport data structure.
621 *
622 * This routine is invoked by the vport cleanup for deletions and the cleanup
623 * for an ndlp on removal.
624 **/
625void
626lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
627{
628 struct lpfc_hba *phba = vport->phba;
629 struct lpfc_scsi_buf *psb, *next_psb;
630 unsigned long iflag = 0;
631
632 spin_lock_irqsave(&phba->hbalock, iflag);
633 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
634 list_for_each_entry_safe(psb, next_psb,
635 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
636 if (psb->rdata && psb->rdata->pnode
637 && psb->rdata->pnode->vport == vport)
638 psb->rdata = NULL;
639 }
640 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
641 spin_unlock_irqrestore(&phba->hbalock, iflag);
642}
643
da0436e9
JS
644/**
645 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
646 * @phba: pointer to lpfc hba data structure.
647 * @axri: pointer to the fcp xri abort wcqe structure.
648 *
649 * This routine is invoked by the worker thread to process a SLI4 fast-path
650 * FCP aborted xri.
651 **/
652void
653lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
654 struct sli4_wcqe_xri_aborted *axri)
655{
656 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
19ca7609 657 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
da0436e9
JS
658 struct lpfc_scsi_buf *psb, *next_psb;
659 unsigned long iflag = 0;
0f65ff68
JS
660 struct lpfc_iocbq *iocbq;
661 int i;
19ca7609
JS
662 struct lpfc_nodelist *ndlp;
663 int rrq_empty = 0;
589a52d6 664 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
da0436e9 665
0f65ff68
JS
666 spin_lock_irqsave(&phba->hbalock, iflag);
667 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
da0436e9
JS
668 list_for_each_entry_safe(psb, next_psb,
669 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
670 if (psb->cur_iocbq.sli4_xritag == xri) {
671 list_del(&psb->list);
341af102 672 psb->exch_busy = 0;
da0436e9 673 psb->status = IOSTAT_SUCCESS;
0f65ff68
JS
674 spin_unlock(
675 &phba->sli4_hba.abts_scsi_buf_list_lock);
1151e3ec
JS
676 if (psb->rdata && psb->rdata->pnode)
677 ndlp = psb->rdata->pnode;
678 else
679 ndlp = NULL;
680
19ca7609 681 rrq_empty = list_empty(&phba->active_rrq_list);
0f65ff68 682 spin_unlock_irqrestore(&phba->hbalock, iflag);
19ca7609
JS
683 if (ndlp)
684 lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
da0436e9 685 lpfc_release_scsi_buf_s4(phba, psb);
19ca7609
JS
686 if (rrq_empty)
687 lpfc_worker_wake_up(phba);
da0436e9
JS
688 return;
689 }
690 }
0f65ff68
JS
691 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
692 for (i = 1; i <= phba->sli.last_iotag; i++) {
693 iocbq = phba->sli.iocbq_lookup[i];
694
695 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
696 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
697 continue;
698 if (iocbq->sli4_xritag != xri)
699 continue;
700 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
701 psb->exch_busy = 0;
702 spin_unlock_irqrestore(&phba->hbalock, iflag);
589a52d6
JS
703 if (pring->txq_cnt)
704 lpfc_worker_wake_up(phba);
0f65ff68
JS
705 return;
706
707 }
708 spin_unlock_irqrestore(&phba->hbalock, iflag);
da0436e9
JS
709}
710
711/**
712 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
713 * @phba: pointer to lpfc hba data structure.
714 *
715 * This routine walks the list of scsi buffers that have been allocated and
716 * repost them to the HBA by using SGL block post. This is needed after a
717 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
718 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
719 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
720 *
721 * Returns: 0 = success, non-zero failure.
722 **/
723int
724lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
725{
726 struct lpfc_scsi_buf *psb;
727 int index, status, bcnt = 0, rcnt = 0, rc = 0;
728 LIST_HEAD(sblist);
729
730 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
731 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
732 if (psb) {
733 /* Remove from SCSI buffer list */
734 list_del(&psb->list);
735 /* Add it to a local SCSI buffer list */
736 list_add_tail(&psb->list, &sblist);
737 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
738 bcnt = rcnt;
739 rcnt = 0;
740 }
741 } else
742 /* A hole present in the XRI array, need to skip */
743 bcnt = rcnt;
744
745 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
746 /* End of XRI array for SCSI buffer, complete */
747 bcnt = rcnt;
748
749 /* Continue until collect up to a nembed page worth of sgls */
750 if (bcnt == 0)
751 continue;
752 /* Now, post the SCSI buffer list sgls as a block */
6d368e53
JS
753 if (!phba->sli4_hba.extents_in_use)
754 status = lpfc_sli4_post_scsi_sgl_block(phba,
755 &sblist,
756 bcnt);
757 else
758 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
759 &sblist,
760 bcnt);
da0436e9
JS
761 /* Reset SCSI buffer count for next round of posting */
762 bcnt = 0;
763 while (!list_empty(&sblist)) {
764 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
765 list);
766 if (status) {
767 /* Put this back on the abort scsi list */
341af102 768 psb->exch_busy = 1;
da0436e9 769 rc++;
341af102
JS
770 } else {
771 psb->exch_busy = 0;
da0436e9 772 psb->status = IOSTAT_SUCCESS;
341af102 773 }
da0436e9
JS
774 /* Put it back into the SCSI buffer list */
775 lpfc_release_scsi_buf_s4(phba, psb);
776 }
777 }
778 return rc;
779}
780
781/**
782 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
783 * @vport: The virtual port for which this call being executed.
784 * @num_to_allocate: The requested number of buffers to allocate.
785 *
786 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
787 * the scsi buffer contains all the necessary information needed to initiate
788 * a SCSI I/O.
789 *
790 * Return codes:
791 * int - number of scsi buffers that were allocated.
792 * 0 = failure, less than num_to_alloc is a partial failure.
793 **/
794static int
795lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
796{
797 struct lpfc_hba *phba = vport->phba;
798 struct lpfc_scsi_buf *psb;
799 struct sli4_sge *sgl;
800 IOCB_t *iocb;
801 dma_addr_t pdma_phys_fcp_cmd;
802 dma_addr_t pdma_phys_fcp_rsp;
803 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
6d368e53 804 uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
da0436e9
JS
805 int status = 0, index;
806 int bcnt;
807 int non_sequential_xri = 0;
da0436e9
JS
808 LIST_HEAD(sblist);
809
810 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
811 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
812 if (!psb)
813 break;
814
815 /*
816 * Get memory from the pci pool to map the virt space to pci bus
817 * space for an I/O. The DMA buffer includes space for the
818 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
819 * necessary to support the sg_tablesize.
820 */
821 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
822 GFP_KERNEL, &psb->dma_handle);
823 if (!psb->data) {
824 kfree(psb);
825 break;
826 }
827
828 /* Initialize virtual ptrs to dma_buf region. */
829 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
830
831 /* Allocate iotag for psb->cur_iocbq. */
832 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
833 if (iotag == 0) {
b92938b4
JS
834 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
835 psb->data, psb->dma_handle);
da0436e9
JS
836 kfree(psb);
837 break;
838 }
839
6d368e53
JS
840 lxri = lpfc_sli4_next_xritag(phba);
841 if (lxri == NO_XRI) {
da0436e9
JS
842 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
843 psb->data, psb->dma_handle);
844 kfree(psb);
845 break;
846 }
6d368e53
JS
847 psb->cur_iocbq.sli4_lxritag = lxri;
848 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
da0436e9
JS
849 if (last_xritag != NO_XRI
850 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
851 non_sequential_xri = 1;
852 } else
853 list_add_tail(&psb->list, &sblist);
854 last_xritag = psb->cur_iocbq.sli4_xritag;
855
856 index = phba->sli4_hba.scsi_xri_cnt++;
857 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
858
859 psb->fcp_bpl = psb->data;
860 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
861 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
862 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
863 sizeof(struct fcp_cmnd));
864
865 /* Initialize local short-hand pointers. */
866 sgl = (struct sli4_sge *)psb->fcp_bpl;
867 pdma_phys_bpl = psb->dma_handle;
868 pdma_phys_fcp_cmd =
869 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
870 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
871 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
872
873 /*
874 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
875 * are sg list bdes. Initialize the first two and leave the
876 * rest for queuecommand.
877 */
878 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
879 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
0558056c 880 sgl->word2 = le32_to_cpu(sgl->word2);
da0436e9
JS
881 bf_set(lpfc_sli4_sge_last, sgl, 0);
882 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74 883 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
da0436e9
JS
884 sgl++;
885
886 /* Setup the physical region for the FCP RSP */
887 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
888 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
0558056c 889 sgl->word2 = le32_to_cpu(sgl->word2);
da0436e9
JS
890 bf_set(lpfc_sli4_sge_last, sgl, 1);
891 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74 892 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
da0436e9
JS
893
894 /*
895 * Since the IOCB for the FCP I/O is built into this
896 * lpfc_scsi_buf, initialize it with all known data now.
897 */
898 iocb = &psb->cur_iocbq.iocb;
899 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
900 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
901 /* setting the BLP size to 2 * sizeof BDE may not be correct.
902 * We are setting the bpl to point to out sgl. An sgl's
903 * entries are 16 bytes, a bpl entries are 12 bytes.
904 */
905 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
906 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
907 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
908 iocb->ulpBdeCount = 1;
909 iocb->ulpLe = 1;
910 iocb->ulpClass = CLASS3;
eee8877e 911 psb->cur_iocbq.context1 = psb;
da0436e9
JS
912 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
913 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
914 else
915 pdma_phys_bpl1 = 0;
916 psb->dma_phys_bpl = pdma_phys_bpl;
917 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
918 if (non_sequential_xri) {
919 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
920 pdma_phys_bpl1,
921 psb->cur_iocbq.sli4_xritag);
922 if (status) {
923 /* Put this back on the abort scsi list */
341af102 924 psb->exch_busy = 1;
341af102
JS
925 } else {
926 psb->exch_busy = 0;
da0436e9 927 psb->status = IOSTAT_SUCCESS;
341af102 928 }
da0436e9
JS
929 /* Put it back into the SCSI buffer list */
930 lpfc_release_scsi_buf_s4(phba, psb);
931 break;
932 }
933 }
934 if (bcnt) {
6d368e53
JS
935 if (!phba->sli4_hba.extents_in_use)
936 status = lpfc_sli4_post_scsi_sgl_block(phba,
937 &sblist,
938 bcnt);
939 else
940 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
941 &sblist,
942 bcnt);
943
944 if (status) {
945 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
946 "3021 SCSI SGL post error %d\n",
947 status);
948 bcnt = 0;
949 }
da0436e9
JS
950 /* Reset SCSI buffer count for next round of posting */
951 while (!list_empty(&sblist)) {
952 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
953 list);
954 if (status) {
955 /* Put this back on the abort scsi list */
341af102 956 psb->exch_busy = 1;
341af102
JS
957 } else {
958 psb->exch_busy = 0;
da0436e9 959 psb->status = IOSTAT_SUCCESS;
341af102 960 }
da0436e9
JS
961 /* Put it back into the SCSI buffer list */
962 lpfc_release_scsi_buf_s4(phba, psb);
963 }
964 }
965
d7c47992 966 return bcnt + non_sequential_xri;
da0436e9
JS
967}
968
9bad7671 969/**
3772a991
JS
970 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
971 * @vport: The virtual port for which this call being executed.
972 * @num_to_allocate: The requested number of buffers to allocate.
973 *
974 * This routine wraps the actual SCSI buffer allocator function pointer from
975 * the lpfc_hba struct.
976 *
977 * Return codes:
978 * int - number of scsi buffers that were allocated.
979 * 0 = failure, less than num_to_alloc is a partial failure.
980 **/
981static inline int
982lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
983{
984 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
985}
986
987/**
19ca7609 988 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
3772a991 989 * @phba: The HBA for which this call is being executed.
9bad7671
JS
990 *
991 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
992 * and returns to caller.
993 *
994 * Return codes:
995 * NULL - Error
996 * Pointer to lpfc_scsi_buf - Success
997 **/
455c53ec 998static struct lpfc_scsi_buf*
19ca7609 999lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea3101e 1000{
0bd4ca25
JSEC
1001 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1002 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
875fbdfe 1003 unsigned long iflag = 0;
0bd4ca25 1004
875fbdfe 1005 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 1006 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1dcb58e5
JS
1007 if (lpfc_cmd) {
1008 lpfc_cmd->seg_cnt = 0;
1009 lpfc_cmd->nonsg_phys = 0;
e2a0a9d6 1010 lpfc_cmd->prot_seg_cnt = 0;
1dcb58e5 1011 }
875fbdfe 1012 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
0bd4ca25
JSEC
1013 return lpfc_cmd;
1014}
19ca7609
JS
1015/**
1016 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1017 * @phba: The HBA for which this call is being executed.
1018 *
1019 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1020 * and returns to caller.
1021 *
1022 * Return codes:
1023 * NULL - Error
1024 * Pointer to lpfc_scsi_buf - Success
1025 **/
1026static struct lpfc_scsi_buf*
1027lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1028{
1151e3ec 1029 struct lpfc_scsi_buf *lpfc_cmd ;
19ca7609
JS
1030 unsigned long iflag = 0;
1031 int found = 0;
1032
1033 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1151e3ec
JS
1034 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1035 list) {
19ca7609 1036 if (lpfc_test_rrq_active(phba, ndlp,
1151e3ec
JS
1037 lpfc_cmd->cur_iocbq.sli4_xritag))
1038 continue;
1039 list_del(&lpfc_cmd->list);
19ca7609
JS
1040 found = 1;
1041 lpfc_cmd->seg_cnt = 0;
1042 lpfc_cmd->nonsg_phys = 0;
1043 lpfc_cmd->prot_seg_cnt = 0;
1151e3ec 1044 break;
19ca7609 1045 }
1151e3ec
JS
1046 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1047 iflag);
1048 if (!found)
1049 return NULL;
1050 else
1051 return lpfc_cmd;
19ca7609
JS
1052}
1053/**
1054 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1055 * @phba: The HBA for which this call is being executed.
1056 *
1057 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1058 * and returns to caller.
1059 *
1060 * Return codes:
1061 * NULL - Error
1062 * Pointer to lpfc_scsi_buf - Success
1063 **/
1064static struct lpfc_scsi_buf*
1065lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1066{
1067 return phba->lpfc_get_scsi_buf(phba, ndlp);
1068}
dea3101e 1069
9bad7671 1070/**
3772a991 1071 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
9bad7671
JS
1072 * @phba: The Hba for which this call is being executed.
1073 * @psb: The scsi buffer which is being released.
1074 *
1075 * This routine releases @psb scsi buffer by adding it to tail of @phba
1076 * lpfc_scsi_buf_list list.
1077 **/
0bd4ca25 1078static void
3772a991 1079lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
0bd4ca25 1080{
875fbdfe 1081 unsigned long iflag = 0;
dea3101e 1082
875fbdfe 1083 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 1084 psb->pCmd = NULL;
dea3101e 1085 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
875fbdfe 1086 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea3101e 1087}
1088
da0436e9
JS
1089/**
1090 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1091 * @phba: The Hba for which this call is being executed.
1092 * @psb: The scsi buffer which is being released.
1093 *
1094 * This routine releases @psb scsi buffer by adding it to tail of @phba
1095 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1096 * and cannot be reused for at least RA_TOV amount of time if it was
1097 * aborted.
1098 **/
1099static void
1100lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1101{
1102 unsigned long iflag = 0;
1103
341af102 1104 if (psb->exch_busy) {
da0436e9
JS
1105 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1106 iflag);
1107 psb->pCmd = NULL;
1108 list_add_tail(&psb->list,
1109 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1110 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1111 iflag);
1112 } else {
1113
1114 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1115 psb->pCmd = NULL;
1116 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1117 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1118 }
1119}
1120
9bad7671 1121/**
3772a991
JS
1122 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1123 * @phba: The Hba for which this call is being executed.
1124 * @psb: The scsi buffer which is being released.
1125 *
1126 * This routine releases @psb scsi buffer by adding it to tail of @phba
1127 * lpfc_scsi_buf_list list.
1128 **/
1129static void
1130lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1131{
1132
1133 phba->lpfc_release_scsi_buf(phba, psb);
1134}
1135
1136/**
1137 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
9bad7671
JS
1138 * @phba: The Hba for which this call is being executed.
1139 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1140 *
1141 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3772a991
JS
1142 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1143 * through sg elements and format the bdea. This routine also initializes all
1144 * IOCB fields which are dependent on scsi command request buffer.
9bad7671
JS
1145 *
1146 * Return codes:
1147 * 1 - Error
1148 * 0 - Success
1149 **/
dea3101e 1150static int
3772a991 1151lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea3101e 1152{
1153 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1154 struct scatterlist *sgel = NULL;
1155 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1156 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
0f65ff68 1157 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea3101e 1158 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
34b02dcd 1159 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea3101e 1160 dma_addr_t physaddr;
34b02dcd 1161 uint32_t num_bde = 0;
a0b4f78f 1162 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea3101e 1163
1164 /*
1165 * There are three possibilities here - use scatter-gather segment, use
1166 * the single mapping, or neither. Start the lpfc command prep by
1167 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1168 * data bde entry.
1169 */
1170 bpl += 2;
c59fd9eb 1171 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 1172 /*
1173 * The driver stores the segment count returned from pci_map_sg
1174 * because this a count of dma-mappings used to map the use_sg
1175 * pages. They are not guaranteed to be the same for those
1176 * architectures that implement an IOMMU.
1177 */
dea3101e 1178
c59fd9eb
FT
1179 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1180 scsi_sg_count(scsi_cmnd), datadir);
1181 if (unlikely(!nseg))
1182 return 1;
1183
a0b4f78f 1184 lpfc_cmd->seg_cnt = nseg;
dea3101e 1185 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
6a9c52cf
JS
1186 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1187 "9064 BLKGRD: %s: Too many sg segments from "
e2a0a9d6 1188 "dma_map_sg. Config %d, seg_cnt %d\n",
cadbd4a5 1189 __func__, phba->cfg_sg_seg_cnt,
dea3101e 1190 lpfc_cmd->seg_cnt);
a0b4f78f 1191 scsi_dma_unmap(scsi_cmnd);
dea3101e 1192 return 1;
1193 }
1194
1195 /*
1196 * The driver established a maximum scatter-gather segment count
1197 * during probe that limits the number of sg elements in any
1198 * single scsi command. Just run through the seg_cnt and format
1199 * the bde's.
34b02dcd
JS
1200 * When using SLI-3 the driver will try to fit all the BDEs into
1201 * the IOCB. If it can't then the BDEs get added to a BPL as it
1202 * does for SLI-2 mode.
dea3101e 1203 */
34b02dcd 1204 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea3101e 1205 physaddr = sg_dma_address(sgel);
34b02dcd 1206 if (phba->sli_rev == 3 &&
e2a0a9d6 1207 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
0f65ff68 1208 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
34b02dcd
JS
1209 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1210 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1211 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1212 data_bde->addrLow = putPaddrLow(physaddr);
1213 data_bde->addrHigh = putPaddrHigh(physaddr);
1214 data_bde++;
1215 } else {
1216 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1217 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1218 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1219 bpl->addrLow =
1220 le32_to_cpu(putPaddrLow(physaddr));
1221 bpl->addrHigh =
1222 le32_to_cpu(putPaddrHigh(physaddr));
1223 bpl++;
1224 }
dea3101e 1225 }
c59fd9eb 1226 }
dea3101e 1227
1228 /*
1229 * Finish initializing those IOCB fields that are dependent on the
34b02dcd
JS
1230 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1231 * explicitly reinitialized and for SLI-3 the extended bde count is
1232 * explicitly reinitialized since all iocb memory resources are reused.
dea3101e 1233 */
e2a0a9d6 1234 if (phba->sli_rev == 3 &&
0f65ff68
JS
1235 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1236 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
34b02dcd
JS
1237 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1238 /*
1239 * The extended IOCB format can only fit 3 BDE or a BPL.
1240 * This I/O has more than 3 BDE so the 1st data bde will
1241 * be a BPL that is filled in here.
1242 */
1243 physaddr = lpfc_cmd->dma_handle;
1244 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1245 data_bde->tus.f.bdeSize = (num_bde *
1246 sizeof(struct ulp_bde64));
1247 physaddr += (sizeof(struct fcp_cmnd) +
1248 sizeof(struct fcp_rsp) +
1249 (2 * sizeof(struct ulp_bde64)));
1250 data_bde->addrHigh = putPaddrHigh(physaddr);
1251 data_bde->addrLow = putPaddrLow(physaddr);
25985edc 1252 /* ebde count includes the response bde and data bpl */
34b02dcd
JS
1253 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1254 } else {
25985edc 1255 /* ebde count includes the response bde and data bdes */
34b02dcd
JS
1256 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1257 }
1258 } else {
1259 iocb_cmd->un.fcpi64.bdl.bdeSize =
1260 ((num_bde + 2) * sizeof(struct ulp_bde64));
0f65ff68 1261 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
34b02dcd 1262 }
09372820 1263 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
e2a0a9d6
JS
1264
1265 /*
1266 * Due to difference in data length between DIF/non-DIF paths,
1267 * we need to set word 4 of IOCB here
1268 */
a257bf90 1269 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
e2a0a9d6
JS
1270 return 0;
1271}
1272
f9bb2da1
JS
1273static inline unsigned
1274lpfc_cmd_blksize(struct scsi_cmnd *sc)
1275{
1276 return sc->device->sector_size;
1277}
1278
1279#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1280/*
1281 * Given a scsi cmnd, determine the BlockGuard tags to be used with it
1282 * @sc: The SCSI command to examine
1283 * @reftag: (out) BlockGuard reference tag for transmitted data
1284 * @apptag: (out) BlockGuard application tag for transmitted data
1285 * @new_guard (in) Value to replace CRC with if needed
1286 *
1287 * Returns (1) if error injection was performed, (0) otherwise
1288 */
1289static int
1290lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1291 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1292{
1293 struct scatterlist *sgpe; /* s/g prot entry */
1294 struct scatterlist *sgde; /* s/g data entry */
1295 struct scsi_dif_tuple *src;
1296 uint32_t op = scsi_get_prot_op(sc);
1297 uint32_t blksize;
1298 uint32_t numblks;
1299 sector_t lba;
1300 int rc = 0;
1301
1302 if (op == SCSI_PROT_NORMAL)
1303 return 0;
1304
1305 lba = scsi_get_lba(sc);
1306 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1307 blksize = lpfc_cmd_blksize(sc);
1308 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1309
1310 /* Make sure we have the right LBA if one is specified */
1311 if ((phba->lpfc_injerr_lba < lba) ||
1312 (phba->lpfc_injerr_lba >= (lba + numblks)))
1313 return 0;
1314 }
1315
1316 sgpe = scsi_prot_sglist(sc);
1317 sgde = scsi_sglist(sc);
1318
1319 /* Should we change the Reference Tag */
1320 if (reftag) {
1321 /*
1322 * If we are SCSI_PROT_WRITE_STRIP, the protection data is
1323 * being stripped from the wire, thus it doesn't matter.
1324 */
1325 if ((op == SCSI_PROT_WRITE_PASS) ||
1326 (op == SCSI_PROT_WRITE_INSERT)) {
1327 if (phba->lpfc_injerr_wref_cnt) {
1328
1329 /* DEADBEEF will be the reftag on the wire */
1330 *reftag = 0xDEADBEEF;
1331 phba->lpfc_injerr_wref_cnt--;
1332 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1333 rc = 1;
1334
1335 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1336 "9081 BLKGRD: Injecting reftag error: "
1337 "write lba x%lx\n", (unsigned long)lba);
1338 }
1339 } else {
1340 if (phba->lpfc_injerr_rref_cnt) {
1341 *reftag = 0xDEADBEEF;
1342 phba->lpfc_injerr_rref_cnt--;
1343 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1344 rc = 1;
1345
1346 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1347 "9076 BLKGRD: Injecting reftag error: "
1348 "read lba x%lx\n", (unsigned long)lba);
1349 }
1350 }
1351 }
1352
1353 /* Should we change the Application Tag */
1354 if (apptag) {
1355 /*
1356 * If we are SCSI_PROT_WRITE_STRIP, the protection data is
1357 * being stripped from the wire, thus it doesn't matter.
1358 */
1359 if ((op == SCSI_PROT_WRITE_PASS) ||
1360 (op == SCSI_PROT_WRITE_INSERT)) {
1361 if (phba->lpfc_injerr_wapp_cnt) {
1362
1363 /* DEAD will be the apptag on the wire */
1364 *apptag = 0xDEAD;
1365 phba->lpfc_injerr_wapp_cnt--;
1366 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1367 rc = 1;
1368
1369 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1370 "9077 BLKGRD: Injecting apptag error: "
1371 "write lba x%lx\n", (unsigned long)lba);
1372 }
1373 } else {
1374 if (phba->lpfc_injerr_rapp_cnt) {
1375 *apptag = 0xDEAD;
1376 phba->lpfc_injerr_rapp_cnt--;
1377 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1378 rc = 1;
1379
1380 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1381 "9078 BLKGRD: Injecting apptag error: "
1382 "read lba x%lx\n", (unsigned long)lba);
1383 }
1384 }
1385 }
1386
1387 /* Should we change the Guard Tag */
1388
1389 /*
1390 * If we are SCSI_PROT_WRITE_INSERT, the protection data is
1391 * being on the wire is being fully generated on the HBA.
1392 * The host cannot change it or force an error.
1393 */
1394 if (((op == SCSI_PROT_WRITE_STRIP) ||
1395 (op == SCSI_PROT_WRITE_PASS)) &&
1396 phba->lpfc_injerr_wgrd_cnt) {
1397 if (sgpe) {
1398 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1399 /*
1400 * Just inject an error in the first
1401 * prot block.
1402 */
1403 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1404 "9079 BLKGRD: Injecting guard error: "
1405 "write lba x%lx oldGuard x%x refTag x%x\n",
1406 (unsigned long)lba, src->guard_tag,
1407 src->ref_tag);
1408
1409 src->guard_tag = (uint16_t)new_guard;
1410 phba->lpfc_injerr_wgrd_cnt--;
1411 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1412 rc = 1;
1413
1414 } else {
1415 blksize = lpfc_cmd_blksize(sc);
1416 /*
1417 * Jump past the first data block
1418 * and inject an error in the
1419 * prot data. The prot data is already
1420 * embedded after the regular data.
1421 */
1422 src = (struct scsi_dif_tuple *)
1423 (sg_virt(sgde) + blksize);
1424
1425 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1426 "9080 BLKGRD: Injecting guard error: "
1427 "write lba x%lx oldGuard x%x refTag x%x\n",
1428 (unsigned long)lba, src->guard_tag,
1429 src->ref_tag);
1430
1431 src->guard_tag = (uint16_t)new_guard;
1432 phba->lpfc_injerr_wgrd_cnt--;
1433 phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
1434 rc = 1;
1435 }
1436 }
1437 return rc;
1438}
1439#endif
1440
e2a0a9d6 1441/*
6c8eea54
JS
1442 * Given a scsi cmnd, determine the BlockGuard opcodes to be used with it
1443 * @sc: The SCSI command to examine
1444 * @txopt: (out) BlockGuard operation for transmitted data
1445 * @rxopt: (out) BlockGuard operation for received data
1446 *
1447 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1448 *
e2a0a9d6
JS
1449 */
1450static int
6c8eea54
JS
1451lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1452 uint8_t *txop, uint8_t *rxop)
e2a0a9d6
JS
1453{
1454 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
6c8eea54 1455 uint8_t ret = 0;
e2a0a9d6
JS
1456
1457 if (guard_type == SHOST_DIX_GUARD_IP) {
1458 switch (scsi_get_prot_op(sc)) {
1459 case SCSI_PROT_READ_INSERT:
1460 case SCSI_PROT_WRITE_STRIP:
6c8eea54
JS
1461 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1462 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
e2a0a9d6
JS
1463 break;
1464
1465 case SCSI_PROT_READ_STRIP:
1466 case SCSI_PROT_WRITE_INSERT:
6c8eea54
JS
1467 *txop = BG_OP_IN_NODIF_OUT_CRC;
1468 *rxop = BG_OP_IN_CRC_OUT_NODIF;
e2a0a9d6
JS
1469 break;
1470
c6af4042
MP
1471 case SCSI_PROT_READ_PASS:
1472 case SCSI_PROT_WRITE_PASS:
6c8eea54
JS
1473 *txop = BG_OP_IN_CSUM_OUT_CRC;
1474 *rxop = BG_OP_IN_CRC_OUT_CSUM;
e2a0a9d6
JS
1475 break;
1476
e2a0a9d6
JS
1477 case SCSI_PROT_NORMAL:
1478 default:
6a9c52cf 1479 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7c56b9fd
JS
1480 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1481 scsi_get_prot_op(sc));
6c8eea54 1482 ret = 1;
e2a0a9d6
JS
1483 break;
1484
1485 }
7c56b9fd 1486 } else {
e2a0a9d6
JS
1487 switch (scsi_get_prot_op(sc)) {
1488 case SCSI_PROT_READ_STRIP:
1489 case SCSI_PROT_WRITE_INSERT:
6c8eea54
JS
1490 *txop = BG_OP_IN_NODIF_OUT_CRC;
1491 *rxop = BG_OP_IN_CRC_OUT_NODIF;
e2a0a9d6
JS
1492 break;
1493
1494 case SCSI_PROT_READ_PASS:
1495 case SCSI_PROT_WRITE_PASS:
6c8eea54
JS
1496 *txop = BG_OP_IN_CRC_OUT_CRC;
1497 *rxop = BG_OP_IN_CRC_OUT_CRC;
e2a0a9d6
JS
1498 break;
1499
e2a0a9d6
JS
1500 case SCSI_PROT_READ_INSERT:
1501 case SCSI_PROT_WRITE_STRIP:
7c56b9fd
JS
1502 *txop = BG_OP_IN_CRC_OUT_NODIF;
1503 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1504 break;
1505
e2a0a9d6
JS
1506 case SCSI_PROT_NORMAL:
1507 default:
6a9c52cf 1508 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
7c56b9fd
JS
1509 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1510 scsi_get_prot_op(sc));
6c8eea54 1511 ret = 1;
e2a0a9d6
JS
1512 break;
1513 }
e2a0a9d6
JS
1514 }
1515
6c8eea54 1516 return ret;
e2a0a9d6
JS
1517}
1518
e2a0a9d6
JS
1519/*
1520 * This function sets up buffer list for protection groups of
1521 * type LPFC_PG_TYPE_NO_DIF
1522 *
1523 * This is usually used when the HBA is instructed to generate
1524 * DIFs and insert them into data stream (or strip DIF from
1525 * incoming data stream)
1526 *
1527 * The buffer list consists of just one protection group described
1528 * below:
1529 * +-------------------------+
6c8eea54
JS
1530 * start of prot group --> | PDE_5 |
1531 * +-------------------------+
1532 * | PDE_6 |
e2a0a9d6
JS
1533 * +-------------------------+
1534 * | Data BDE |
1535 * +-------------------------+
1536 * |more Data BDE's ... (opt)|
1537 * +-------------------------+
1538 *
1539 * @sc: pointer to scsi command we're working on
1540 * @bpl: pointer to buffer list for protection groups
1541 * @datacnt: number of segments of data that have been dma mapped
1542 *
1543 * Note: Data s/g buffers have been dma mapped
1544 */
1545static int
1546lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1547 struct ulp_bde64 *bpl, int datasegcnt)
1548{
1549 struct scatterlist *sgde = NULL; /* s/g data entry */
6c8eea54
JS
1550 struct lpfc_pde5 *pde5 = NULL;
1551 struct lpfc_pde6 *pde6 = NULL;
e2a0a9d6 1552 dma_addr_t physaddr;
6c8eea54 1553 int i = 0, num_bde = 0, status;
e2a0a9d6 1554 int datadir = sc->sc_data_direction;
e2a0a9d6 1555 uint32_t reftag;
7c56b9fd 1556 unsigned blksize;
6c8eea54 1557 uint8_t txop, rxop;
e2a0a9d6 1558
6c8eea54
JS
1559 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1560 if (status)
e2a0a9d6
JS
1561 goto out;
1562
6c8eea54 1563 /* extract some info from the scsi command for pde*/
e2a0a9d6 1564 blksize = lpfc_cmd_blksize(sc);
7c56b9fd 1565 reftag = scsi_get_lba(sc) & 0xffffffff;
e2a0a9d6 1566
f9bb2da1
JS
1567#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1568 /* reftag is the only error we can inject here */
1569 lpfc_bg_err_inject(phba, sc, &reftag, 0, 0);
1570#endif
1571
6c8eea54
JS
1572 /* setup PDE5 with what we have */
1573 pde5 = (struct lpfc_pde5 *) bpl;
1574 memset(pde5, 0, sizeof(struct lpfc_pde5));
1575 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
6c8eea54 1576
bc73905a 1577 /* Endianness conversion if necessary for PDE5 */
589a52d6 1578 pde5->word0 = cpu_to_le32(pde5->word0);
7c56b9fd 1579 pde5->reftag = cpu_to_le32(reftag);
589a52d6 1580
6c8eea54
JS
1581 /* advance bpl and increment bde count */
1582 num_bde++;
1583 bpl++;
1584 pde6 = (struct lpfc_pde6 *) bpl;
1585
1586 /* setup PDE6 with the rest of the info */
1587 memset(pde6, 0, sizeof(struct lpfc_pde6));
1588 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1589 bf_set(pde6_optx, pde6, txop);
1590 bf_set(pde6_oprx, pde6, rxop);
1591 if (datadir == DMA_FROM_DEVICE) {
1592 bf_set(pde6_ce, pde6, 1);
1593 bf_set(pde6_re, pde6, 1);
6c8eea54
JS
1594 }
1595 bf_set(pde6_ai, pde6, 1);
7c56b9fd
JS
1596 bf_set(pde6_ae, pde6, 0);
1597 bf_set(pde6_apptagval, pde6, 0);
e2a0a9d6 1598
bc73905a 1599 /* Endianness conversion if necessary for PDE6 */
589a52d6
JS
1600 pde6->word0 = cpu_to_le32(pde6->word0);
1601 pde6->word1 = cpu_to_le32(pde6->word1);
1602 pde6->word2 = cpu_to_le32(pde6->word2);
1603
6c8eea54 1604 /* advance bpl and increment bde count */
e2a0a9d6
JS
1605 num_bde++;
1606 bpl++;
1607
1608 /* assumption: caller has already run dma_map_sg on command data */
1609 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1610 physaddr = sg_dma_address(sgde);
1611 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1612 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1613 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1614 if (datadir == DMA_TO_DEVICE)
1615 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1616 else
1617 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1618 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1619 bpl++;
1620 num_bde++;
1621 }
1622
1623out:
1624 return num_bde;
1625}
1626
1627/*
1628 * This function sets up buffer list for protection groups of
1629 * type LPFC_PG_TYPE_DIF_BUF
1630 *
1631 * This is usually used when DIFs are in their own buffers,
1632 * separate from the data. The HBA can then by instructed
1633 * to place the DIFs in the outgoing stream. For read operations,
1634 * The HBA could extract the DIFs and place it in DIF buffers.
1635 *
1636 * The buffer list for this type consists of one or more of the
1637 * protection groups described below:
1638 * +-------------------------+
6c8eea54 1639 * start of first prot group --> | PDE_5 |
e2a0a9d6 1640 * +-------------------------+
6c8eea54
JS
1641 * | PDE_6 |
1642 * +-------------------------+
1643 * | PDE_7 (Prot BDE) |
e2a0a9d6
JS
1644 * +-------------------------+
1645 * | Data BDE |
1646 * +-------------------------+
1647 * |more Data BDE's ... (opt)|
1648 * +-------------------------+
6c8eea54 1649 * start of new prot group --> | PDE_5 |
e2a0a9d6
JS
1650 * +-------------------------+
1651 * | ... |
1652 * +-------------------------+
1653 *
1654 * @sc: pointer to scsi command we're working on
1655 * @bpl: pointer to buffer list for protection groups
1656 * @datacnt: number of segments of data that have been dma mapped
1657 * @protcnt: number of segment of protection data that have been dma mapped
1658 *
1659 * Note: It is assumed that both data and protection s/g buffers have been
1660 * mapped for DMA
1661 */
1662static int
1663lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1664 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1665{
1666 struct scatterlist *sgde = NULL; /* s/g data entry */
1667 struct scatterlist *sgpe = NULL; /* s/g prot entry */
6c8eea54
JS
1668 struct lpfc_pde5 *pde5 = NULL;
1669 struct lpfc_pde6 *pde6 = NULL;
7f86059a 1670 struct lpfc_pde7 *pde7 = NULL;
e2a0a9d6
JS
1671 dma_addr_t dataphysaddr, protphysaddr;
1672 unsigned short curr_data = 0, curr_prot = 0;
7f86059a
JS
1673 unsigned int split_offset;
1674 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
e2a0a9d6
JS
1675 unsigned int protgrp_blks, protgrp_bytes;
1676 unsigned int remainder, subtotal;
6c8eea54 1677 int status;
e2a0a9d6
JS
1678 int datadir = sc->sc_data_direction;
1679 unsigned char pgdone = 0, alldone = 0;
1680 unsigned blksize;
1681 uint32_t reftag;
6c8eea54 1682 uint8_t txop, rxop;
e2a0a9d6
JS
1683 int num_bde = 0;
1684
1685 sgpe = scsi_prot_sglist(sc);
1686 sgde = scsi_sglist(sc);
1687
1688 if (!sgpe || !sgde) {
1689 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1690 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1691 sgpe, sgde);
1692 return 0;
1693 }
1694
6c8eea54
JS
1695 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1696 if (status)
e2a0a9d6
JS
1697 goto out;
1698
6c8eea54 1699 /* extract some info from the scsi command */
e2a0a9d6 1700 blksize = lpfc_cmd_blksize(sc);
7c56b9fd 1701 reftag = scsi_get_lba(sc) & 0xffffffff;
e2a0a9d6 1702
f9bb2da1
JS
1703#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1704 /* reftag / guard tag are the only errors we can inject here */
1705 lpfc_bg_err_inject(phba, sc, &reftag, 0, 0xDEAD);
1706#endif
1707
e2a0a9d6
JS
1708 split_offset = 0;
1709 do {
6c8eea54
JS
1710 /* setup PDE5 with what we have */
1711 pde5 = (struct lpfc_pde5 *) bpl;
1712 memset(pde5, 0, sizeof(struct lpfc_pde5));
1713 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
e2a0a9d6 1714
bc73905a 1715 /* Endianness conversion if necessary for PDE5 */
589a52d6 1716 pde5->word0 = cpu_to_le32(pde5->word0);
7c56b9fd 1717 pde5->reftag = cpu_to_le32(reftag);
589a52d6 1718
6c8eea54
JS
1719 /* advance bpl and increment bde count */
1720 num_bde++;
1721 bpl++;
1722 pde6 = (struct lpfc_pde6 *) bpl;
1723
1724 /* setup PDE6 with the rest of the info */
1725 memset(pde6, 0, sizeof(struct lpfc_pde6));
1726 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1727 bf_set(pde6_optx, pde6, txop);
1728 bf_set(pde6_oprx, pde6, rxop);
1729 bf_set(pde6_ce, pde6, 1);
1730 bf_set(pde6_re, pde6, 1);
6c8eea54 1731 bf_set(pde6_ai, pde6, 1);
7c56b9fd
JS
1732 bf_set(pde6_ae, pde6, 0);
1733 bf_set(pde6_apptagval, pde6, 0);
6c8eea54 1734
bc73905a 1735 /* Endianness conversion if necessary for PDE6 */
589a52d6
JS
1736 pde6->word0 = cpu_to_le32(pde6->word0);
1737 pde6->word1 = cpu_to_le32(pde6->word1);
1738 pde6->word2 = cpu_to_le32(pde6->word2);
1739
6c8eea54 1740 /* advance bpl and increment bde count */
e2a0a9d6
JS
1741 num_bde++;
1742 bpl++;
1743
1744 /* setup the first BDE that points to protection buffer */
7f86059a
JS
1745 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1746 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
e2a0a9d6 1747
e2a0a9d6
JS
1748 /* must be integer multiple of the DIF block length */
1749 BUG_ON(protgroup_len % 8);
1750
7f86059a
JS
1751 pde7 = (struct lpfc_pde7 *) bpl;
1752 memset(pde7, 0, sizeof(struct lpfc_pde7));
1753 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1754
7c56b9fd
JS
1755 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1756 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
7f86059a 1757
e2a0a9d6
JS
1758 protgrp_blks = protgroup_len / 8;
1759 protgrp_bytes = protgrp_blks * blksize;
1760
7f86059a
JS
1761 /* check if this pde is crossing the 4K boundary; if so split */
1762 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1763 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1764 protgroup_offset += protgroup_remainder;
1765 protgrp_blks = protgroup_remainder / 8;
7c56b9fd 1766 protgrp_bytes = protgrp_blks * blksize;
7f86059a
JS
1767 } else {
1768 protgroup_offset = 0;
1769 curr_prot++;
1770 }
e2a0a9d6 1771
e2a0a9d6
JS
1772 num_bde++;
1773
1774 /* setup BDE's for data blocks associated with DIF data */
1775 pgdone = 0;
1776 subtotal = 0; /* total bytes processed for current prot grp */
1777 while (!pgdone) {
1778 if (!sgde) {
6a9c52cf
JS
1779 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1780 "9065 BLKGRD:%s Invalid data segment\n",
e2a0a9d6
JS
1781 __func__);
1782 return 0;
1783 }
1784 bpl++;
1785 dataphysaddr = sg_dma_address(sgde) + split_offset;
1786 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1787 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1788
1789 remainder = sg_dma_len(sgde) - split_offset;
1790
1791 if ((subtotal + remainder) <= protgrp_bytes) {
1792 /* we can use this whole buffer */
1793 bpl->tus.f.bdeSize = remainder;
1794 split_offset = 0;
1795
1796 if ((subtotal + remainder) == protgrp_bytes)
1797 pgdone = 1;
1798 } else {
1799 /* must split this buffer with next prot grp */
1800 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1801 split_offset += bpl->tus.f.bdeSize;
1802 }
1803
1804 subtotal += bpl->tus.f.bdeSize;
1805
1806 if (datadir == DMA_TO_DEVICE)
1807 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1808 else
1809 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1810 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1811
1812 num_bde++;
1813 curr_data++;
1814
1815 if (split_offset)
1816 break;
1817
1818 /* Move to the next s/g segment if possible */
1819 sgde = sg_next(sgde);
6c8eea54 1820
e2a0a9d6
JS
1821 }
1822
7f86059a
JS
1823 if (protgroup_offset) {
1824 /* update the reference tag */
1825 reftag += protgrp_blks;
1826 bpl++;
1827 continue;
1828 }
1829
e2a0a9d6
JS
1830 /* are we done ? */
1831 if (curr_prot == protcnt) {
1832 alldone = 1;
1833 } else if (curr_prot < protcnt) {
1834 /* advance to next prot buffer */
1835 sgpe = sg_next(sgpe);
1836 bpl++;
1837
1838 /* update the reference tag */
1839 reftag += protgrp_blks;
1840 } else {
1841 /* if we're here, we have a bug */
6a9c52cf
JS
1842 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1843 "9054 BLKGRD: bug in %s\n", __func__);
e2a0a9d6
JS
1844 }
1845
1846 } while (!alldone);
e2a0a9d6
JS
1847out:
1848
e2a0a9d6
JS
1849 return num_bde;
1850}
7f86059a 1851
e2a0a9d6
JS
1852/*
1853 * Given a SCSI command that supports DIF, determine composition of protection
1854 * groups involved in setting up buffer lists
1855 *
1856 * Returns:
1857 * for DIF (for both read and write)
1858 * */
1859static int
1860lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1861{
1862 int ret = LPFC_PG_TYPE_INVALID;
1863 unsigned char op = scsi_get_prot_op(sc);
1864
1865 switch (op) {
1866 case SCSI_PROT_READ_STRIP:
1867 case SCSI_PROT_WRITE_INSERT:
1868 ret = LPFC_PG_TYPE_NO_DIF;
1869 break;
1870 case SCSI_PROT_READ_INSERT:
1871 case SCSI_PROT_WRITE_STRIP:
1872 case SCSI_PROT_READ_PASS:
1873 case SCSI_PROT_WRITE_PASS:
e2a0a9d6
JS
1874 ret = LPFC_PG_TYPE_DIF_BUF;
1875 break;
1876 default:
1877 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1878 "9021 Unsupported protection op:%d\n", op);
1879 break;
1880 }
1881
1882 return ret;
1883}
1884
1885/*
1886 * This is the protection/DIF aware version of
1887 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1888 * two functions eventually, but for now, it's here
1889 */
1890static int
1891lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1892 struct lpfc_scsi_buf *lpfc_cmd)
1893{
1894 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1895 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1896 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1897 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1898 uint32_t num_bde = 0;
1899 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1900 int prot_group_type = 0;
1901 int diflen, fcpdl;
1902 unsigned blksize;
1903
1904 /*
1905 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1906 * fcp_rsp regions to the first data bde entry
1907 */
1908 bpl += 2;
1909 if (scsi_sg_count(scsi_cmnd)) {
1910 /*
1911 * The driver stores the segment count returned from pci_map_sg
1912 * because this a count of dma-mappings used to map the use_sg
1913 * pages. They are not guaranteed to be the same for those
1914 * architectures that implement an IOMMU.
1915 */
1916 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1917 scsi_sglist(scsi_cmnd),
1918 scsi_sg_count(scsi_cmnd), datadir);
1919 if (unlikely(!datasegcnt))
1920 return 1;
1921
1922 lpfc_cmd->seg_cnt = datasegcnt;
1923 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
6a9c52cf
JS
1924 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1925 "9067 BLKGRD: %s: Too many sg segments"
1926 " from dma_map_sg. Config %d, seg_cnt"
1927 " %d\n",
e2a0a9d6
JS
1928 __func__, phba->cfg_sg_seg_cnt,
1929 lpfc_cmd->seg_cnt);
1930 scsi_dma_unmap(scsi_cmnd);
1931 return 1;
1932 }
1933
1934 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1935
1936 switch (prot_group_type) {
1937 case LPFC_PG_TYPE_NO_DIF:
1938 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1939 datasegcnt);
c9404c9c 1940 /* we should have 2 or more entries in buffer list */
e2a0a9d6
JS
1941 if (num_bde < 2)
1942 goto err;
1943 break;
1944 case LPFC_PG_TYPE_DIF_BUF:{
1945 /*
1946 * This type indicates that protection buffers are
1947 * passed to the driver, so that needs to be prepared
1948 * for DMA
1949 */
1950 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1951 scsi_prot_sglist(scsi_cmnd),
1952 scsi_prot_sg_count(scsi_cmnd), datadir);
1953 if (unlikely(!protsegcnt)) {
1954 scsi_dma_unmap(scsi_cmnd);
1955 return 1;
1956 }
1957
1958 lpfc_cmd->prot_seg_cnt = protsegcnt;
1959 if (lpfc_cmd->prot_seg_cnt
1960 > phba->cfg_prot_sg_seg_cnt) {
6a9c52cf
JS
1961 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1962 "9068 BLKGRD: %s: Too many prot sg "
1963 "segments from dma_map_sg. Config %d,"
e2a0a9d6
JS
1964 "prot_seg_cnt %d\n", __func__,
1965 phba->cfg_prot_sg_seg_cnt,
1966 lpfc_cmd->prot_seg_cnt);
1967 dma_unmap_sg(&phba->pcidev->dev,
1968 scsi_prot_sglist(scsi_cmnd),
1969 scsi_prot_sg_count(scsi_cmnd),
1970 datadir);
1971 scsi_dma_unmap(scsi_cmnd);
1972 return 1;
1973 }
1974
1975 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1976 datasegcnt, protsegcnt);
c9404c9c 1977 /* we should have 3 or more entries in buffer list */
e2a0a9d6
JS
1978 if (num_bde < 3)
1979 goto err;
1980 break;
1981 }
1982 case LPFC_PG_TYPE_INVALID:
1983 default:
1984 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1985 "9022 Unexpected protection group %i\n",
1986 prot_group_type);
1987 return 1;
1988 }
1989 }
1990
1991 /*
1992 * Finish initializing those IOCB fields that are dependent on the
1993 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1994 * reinitialized since all iocb memory resources are used many times
1995 * for transmit, receive, and continuation bpl's.
1996 */
1997 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1998 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1999 iocb_cmd->ulpBdeCount = 1;
2000 iocb_cmd->ulpLe = 1;
2001
2002 fcpdl = scsi_bufflen(scsi_cmnd);
2003
2004 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2005 /*
2006 * We are in DIF Type 1 mode
2007 * Every data block has a 8 byte DIF (trailer)
2008 * attached to it. Must ajust FCP data length
2009 */
2010 blksize = lpfc_cmd_blksize(scsi_cmnd);
2011 diflen = (fcpdl / blksize) * 8;
2012 fcpdl += diflen;
2013 }
2014 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2015
2016 /*
2017 * Due to difference in data length between DIF/non-DIF paths,
2018 * we need to set word 4 of IOCB here
2019 */
2020 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2021
dea3101e 2022 return 0;
e2a0a9d6
JS
2023err:
2024 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2025 "9023 Could not setup all needed BDE's"
2026 "prot_group_type=%d, num_bde=%d\n",
2027 prot_group_type, num_bde);
2028 return 1;
2029}
2030
2031/*
2032 * This function checks for BlockGuard errors detected by
2033 * the HBA. In case of errors, the ASC/ASCQ fields in the
2034 * sense buffer will be set accordingly, paired with
2035 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2036 * detected corruption.
2037 *
2038 * Returns:
2039 * 0 - No error found
2040 * 1 - BlockGuard error found
2041 * -1 - Internal error (bad profile, ...etc)
2042 */
2043static int
2044lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2045 struct lpfc_iocbq *pIocbOut)
2046{
2047 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2048 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2049 int ret = 0;
2050 uint32_t bghm = bgf->bghm;
2051 uint32_t bgstat = bgf->bgstat;
2052 uint64_t failing_sector = 0;
2053
6a9c52cf
JS
2054 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2055 " 0x%x lba 0x%llx blk cnt 0x%x "
e2a0a9d6 2056 "bgstat=0x%x bghm=0x%x\n",
87b5c328 2057 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
83096ebf 2058 blk_rq_sectors(cmd->request), bgstat, bghm);
e2a0a9d6
JS
2059
2060 spin_lock(&_dump_buf_lock);
2061 if (!_dump_buf_done) {
6a9c52cf
JS
2062 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2063 " Data for %u blocks to debugfs\n",
e2a0a9d6 2064 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
6a9c52cf 2065 lpfc_debug_save_data(phba, cmd);
e2a0a9d6
JS
2066
2067 /* If we have a prot sgl, save the DIF buffer */
2068 if (lpfc_prot_group_type(phba, cmd) ==
2069 LPFC_PG_TYPE_DIF_BUF) {
6a9c52cf
JS
2070 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2071 "Saving DIF for %u blocks to debugfs\n",
2072 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2073 lpfc_debug_save_dif(phba, cmd);
e2a0a9d6
JS
2074 }
2075
2076 _dump_buf_done = 1;
2077 }
2078 spin_unlock(&_dump_buf_lock);
2079
2080 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2081 cmd->result = ScsiResult(DID_ERROR, 0);
6a9c52cf
JS
2082 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2083 " BlockGuard profile. bgstat:0x%x\n",
2084 bgstat);
e2a0a9d6
JS
2085 ret = (-1);
2086 goto out;
2087 }
2088
2089 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2090 cmd->result = ScsiResult(DID_ERROR, 0);
6a9c52cf
JS
2091 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2092 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
e2a0a9d6
JS
2093 bgstat);
2094 ret = (-1);
2095 goto out;
2096 }
2097
2098 if (lpfc_bgs_get_guard_err(bgstat)) {
2099 ret = 1;
2100
2101 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2102 0x10, 0x1);
1c9fbafc 2103 cmd->result = DRIVER_SENSE << 24
e2a0a9d6
JS
2104 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2105 phba->bg_guard_err_cnt++;
6a9c52cf
JS
2106 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2107 "9055 BLKGRD: guard_tag error\n");
e2a0a9d6
JS
2108 }
2109
2110 if (lpfc_bgs_get_reftag_err(bgstat)) {
2111 ret = 1;
2112
2113 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2114 0x10, 0x3);
1c9fbafc 2115 cmd->result = DRIVER_SENSE << 24
e2a0a9d6
JS
2116 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2117
2118 phba->bg_reftag_err_cnt++;
6a9c52cf
JS
2119 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2120 "9056 BLKGRD: ref_tag error\n");
e2a0a9d6
JS
2121 }
2122
2123 if (lpfc_bgs_get_apptag_err(bgstat)) {
2124 ret = 1;
2125
2126 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2127 0x10, 0x2);
1c9fbafc 2128 cmd->result = DRIVER_SENSE << 24
e2a0a9d6
JS
2129 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2130
2131 phba->bg_apptag_err_cnt++;
6a9c52cf
JS
2132 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2133 "9061 BLKGRD: app_tag error\n");
e2a0a9d6
JS
2134 }
2135
2136 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2137 /*
2138 * setup sense data descriptor 0 per SPC-4 as an information
7c56b9fd
JS
2139 * field, and put the failing LBA in it.
2140 * This code assumes there was also a guard/app/ref tag error
2141 * indication.
e2a0a9d6 2142 */
7c56b9fd
JS
2143 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2144 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2145 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2146 cmd->sense_buffer[10] = 0x80; /* Validity bit */
2344b5b6 2147 bghm /= cmd->device->sector_size;
e2a0a9d6
JS
2148
2149 failing_sector = scsi_get_lba(cmd);
2150 failing_sector += bghm;
2151
7c56b9fd
JS
2152 /* Descriptor Information */
2153 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
e2a0a9d6
JS
2154 }
2155
2156 if (!ret) {
2157 /* No error was reported - problem in FW? */
2158 cmd->result = ScsiResult(DID_ERROR, 0);
6a9c52cf
JS
2159 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2160 "9057 BLKGRD: no errors reported!\n");
e2a0a9d6
JS
2161 }
2162
2163out:
2164 return ret;
dea3101e 2165}
2166
da0436e9
JS
2167/**
2168 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2169 * @phba: The Hba for which this call is being executed.
2170 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2171 *
2172 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2173 * field of @lpfc_cmd for device with SLI-4 interface spec.
2174 *
2175 * Return codes:
6c8eea54
JS
2176 * 1 - Error
2177 * 0 - Success
da0436e9
JS
2178 **/
2179static int
2180lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2181{
2182 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2183 struct scatterlist *sgel = NULL;
2184 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2185 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
fedd3b7b 2186 struct sli4_sge *first_data_sgl;
da0436e9
JS
2187 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2188 dma_addr_t physaddr;
2189 uint32_t num_bde = 0;
2190 uint32_t dma_len;
2191 uint32_t dma_offset = 0;
2192 int nseg;
fedd3b7b 2193 struct ulp_bde64 *bde;
da0436e9
JS
2194
2195 /*
2196 * There are three possibilities here - use scatter-gather segment, use
2197 * the single mapping, or neither. Start the lpfc command prep by
2198 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2199 * data bde entry.
2200 */
2201 if (scsi_sg_count(scsi_cmnd)) {
2202 /*
2203 * The driver stores the segment count returned from pci_map_sg
2204 * because this a count of dma-mappings used to map the use_sg
2205 * pages. They are not guaranteed to be the same for those
2206 * architectures that implement an IOMMU.
2207 */
2208
2209 nseg = scsi_dma_map(scsi_cmnd);
2210 if (unlikely(!nseg))
2211 return 1;
2212 sgl += 1;
2213 /* clear the last flag in the fcp_rsp map entry */
2214 sgl->word2 = le32_to_cpu(sgl->word2);
2215 bf_set(lpfc_sli4_sge_last, sgl, 0);
2216 sgl->word2 = cpu_to_le32(sgl->word2);
2217 sgl += 1;
fedd3b7b 2218 first_data_sgl = sgl;
da0436e9
JS
2219 lpfc_cmd->seg_cnt = nseg;
2220 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
6a9c52cf
JS
2221 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
2222 " %s: Too many sg segments from "
2223 "dma_map_sg. Config %d, seg_cnt %d\n",
2224 __func__, phba->cfg_sg_seg_cnt,
da0436e9
JS
2225 lpfc_cmd->seg_cnt);
2226 scsi_dma_unmap(scsi_cmnd);
2227 return 1;
2228 }
2229
2230 /*
2231 * The driver established a maximum scatter-gather segment count
2232 * during probe that limits the number of sg elements in any
2233 * single scsi command. Just run through the seg_cnt and format
2234 * the sge's.
2235 * When using SLI-3 the driver will try to fit all the BDEs into
2236 * the IOCB. If it can't then the BDEs get added to a BPL as it
2237 * does for SLI-2 mode.
2238 */
2239 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
2240 physaddr = sg_dma_address(sgel);
2241 dma_len = sg_dma_len(sgel);
da0436e9
JS
2242 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2243 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
0558056c 2244 sgl->word2 = le32_to_cpu(sgl->word2);
da0436e9
JS
2245 if ((num_bde + 1) == nseg)
2246 bf_set(lpfc_sli4_sge_last, sgl, 1);
2247 else
2248 bf_set(lpfc_sli4_sge_last, sgl, 0);
2249 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
f9bb2da1 2250 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
da0436e9 2251 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74 2252 sgl->sge_len = cpu_to_le32(dma_len);
da0436e9
JS
2253 dma_offset += dma_len;
2254 sgl++;
2255 }
fedd3b7b
JS
2256 /* setup the performance hint (first data BDE) if enabled */
2257 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
2258 bde = (struct ulp_bde64 *)
2259 &(iocb_cmd->unsli3.sli3Words[5]);
2260 bde->addrLow = first_data_sgl->addr_lo;
2261 bde->addrHigh = first_data_sgl->addr_hi;
2262 bde->tus.f.bdeSize =
2263 le32_to_cpu(first_data_sgl->sge_len);
2264 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2265 bde->tus.w = cpu_to_le32(bde->tus.w);
2266 }
da0436e9
JS
2267 } else {
2268 sgl += 1;
2269 /* clear the last flag in the fcp_rsp map entry */
2270 sgl->word2 = le32_to_cpu(sgl->word2);
2271 bf_set(lpfc_sli4_sge_last, sgl, 1);
2272 sgl->word2 = cpu_to_le32(sgl->word2);
2273 }
2274
2275 /*
2276 * Finish initializing those IOCB fields that are dependent on the
2277 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
2278 * explicitly reinitialized.
2279 * all iocb memory resources are reused.
2280 */
2281 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
2282
2283 /*
2284 * Due to difference in data length between DIF/non-DIF paths,
2285 * we need to set word 4 of IOCB here
2286 */
2287 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
2288 return 0;
2289}
2290
3772a991
JS
2291/**
2292 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
2293 * @phba: The Hba for which this call is being executed.
2294 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2295 *
2296 * This routine wraps the actual DMA mapping function pointer from the
2297 * lpfc_hba struct.
2298 *
2299 * Return codes:
6c8eea54
JS
2300 * 1 - Error
2301 * 0 - Success
3772a991
JS
2302 **/
2303static inline int
2304lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2305{
2306 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2307}
2308
ea2151b4 2309/**
3621a710 2310 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
ea2151b4
JS
2311 * @phba: Pointer to hba context object.
2312 * @vport: Pointer to vport object.
2313 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
2314 * @rsp_iocb: Pointer to response iocb object which reported error.
2315 *
2316 * This function posts an event when there is a SCSI command reporting
2317 * error from the scsi device.
2318 **/
2319static void
2320lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
2321 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
2322 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2323 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2324 uint32_t resp_info = fcprsp->rspStatus2;
2325 uint32_t scsi_status = fcprsp->rspStatus3;
2326 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2327 struct lpfc_fast_path_event *fast_path_evt = NULL;
2328 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
2329 unsigned long flags;
2330
5989b8d4
JS
2331 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2332 return;
2333
ea2151b4
JS
2334 /* If there is queuefull or busy condition send a scsi event */
2335 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
2336 (cmnd->result == SAM_STAT_BUSY)) {
2337 fast_path_evt = lpfc_alloc_fast_evt(phba);
2338 if (!fast_path_evt)
2339 return;
2340 fast_path_evt->un.scsi_evt.event_type =
2341 FC_REG_SCSI_EVENT;
2342 fast_path_evt->un.scsi_evt.subcategory =
2343 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
2344 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
2345 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
2346 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
2347 &pnode->nlp_portname, sizeof(struct lpfc_name));
2348 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
2349 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2350 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
2351 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
2352 fast_path_evt = lpfc_alloc_fast_evt(phba);
2353 if (!fast_path_evt)
2354 return;
2355 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
2356 FC_REG_SCSI_EVENT;
2357 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
2358 LPFC_EVENT_CHECK_COND;
2359 fast_path_evt->un.check_cond_evt.scsi_event.lun =
2360 cmnd->device->lun;
2361 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
2362 &pnode->nlp_portname, sizeof(struct lpfc_name));
2363 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
2364 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2365 fast_path_evt->un.check_cond_evt.sense_key =
2366 cmnd->sense_buffer[2] & 0xf;
2367 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
2368 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
2369 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2370 fcpi_parm &&
2371 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
2372 ((scsi_status == SAM_STAT_GOOD) &&
2373 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
2374 /*
2375 * If status is good or resid does not match with fcp_param and
2376 * there is valid fcpi_parm, then there is a read_check error
2377 */
2378 fast_path_evt = lpfc_alloc_fast_evt(phba);
2379 if (!fast_path_evt)
2380 return;
2381 fast_path_evt->un.read_check_error.header.event_type =
2382 FC_REG_FABRIC_EVENT;
2383 fast_path_evt->un.read_check_error.header.subcategory =
2384 LPFC_EVENT_FCPRDCHKERR;
2385 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
2386 &pnode->nlp_portname, sizeof(struct lpfc_name));
2387 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2388 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2389 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2390 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2391 fast_path_evt->un.read_check_error.fcpiparam =
2392 fcpi_parm;
2393 } else
2394 return;
2395
2396 fast_path_evt->vport = vport;
2397 spin_lock_irqsave(&phba->hbalock, flags);
2398 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2399 spin_unlock_irqrestore(&phba->hbalock, flags);
2400 lpfc_worker_wake_up(phba);
2401 return;
2402}
9bad7671
JS
2403
2404/**
f1126688 2405 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3772a991 2406 * @phba: The HBA for which this call is being executed.
9bad7671
JS
2407 * @psb: The scsi buffer which is going to be un-mapped.
2408 *
2409 * This routine does DMA un-mapping of scatter gather list of scsi command
3772a991 2410 * field of @lpfc_cmd for device with SLI-3 interface spec.
9bad7671 2411 **/
bcf4dbfa 2412static void
f1126688 2413lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
bcf4dbfa
JS
2414{
2415 /*
2416 * There are only two special cases to consider. (1) the scsi command
2417 * requested scatter-gather usage or (2) the scsi command allocated
2418 * a request buffer, but did not request use_sg. There is a third
2419 * case, but it does not require resource deallocation.
2420 */
a0b4f78f
FT
2421 if (psb->seg_cnt > 0)
2422 scsi_dma_unmap(psb->pCmd);
e2a0a9d6
JS
2423 if (psb->prot_seg_cnt > 0)
2424 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2425 scsi_prot_sg_count(psb->pCmd),
2426 psb->pCmd->sc_data_direction);
bcf4dbfa
JS
2427}
2428
9bad7671 2429/**
3621a710 2430 * lpfc_handler_fcp_err - FCP response handler
9bad7671
JS
2431 * @vport: The virtual port for which this call is being executed.
2432 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2433 * @rsp_iocb: The response IOCB which contains FCP error.
2434 *
2435 * This routine is called to process response IOCB with status field
2436 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2437 * based upon SCSI and FCP error.
2438 **/
dea3101e 2439static void
2e0fef85
JS
2440lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2441 struct lpfc_iocbq *rsp_iocb)
dea3101e 2442{
2443 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2444 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2445 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
7054a606 2446 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea3101e 2447 uint32_t resp_info = fcprsp->rspStatus2;
2448 uint32_t scsi_status = fcprsp->rspStatus3;
c7743956 2449 uint32_t *lp;
dea3101e 2450 uint32_t host_status = DID_OK;
2451 uint32_t rsplen = 0;
c7743956 2452 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea3101e 2453
ea2151b4 2454
dea3101e 2455 /*
2456 * If this is a task management command, there is no
2457 * scsi packet associated with this lpfc_cmd. The driver
2458 * consumes it.
2459 */
2460 if (fcpcmd->fcpCntl2) {
2461 scsi_status = 0;
2462 goto out;
2463 }
2464
6a9c52cf
JS
2465 if (resp_info & RSP_LEN_VALID) {
2466 rsplen = be32_to_cpu(fcprsp->rspRspLen);
e40a02c1 2467 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
6a9c52cf
JS
2468 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2469 "2719 Invalid response length: "
2470 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2471 cmnd->device->id,
2472 cmnd->device->lun, cmnd->cmnd[0],
2473 rsplen);
2474 host_status = DID_ERROR;
2475 goto out;
2476 }
e40a02c1
JS
2477 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
2478 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2479 "2757 Protocol failure detected during "
2480 "processing of FCP I/O op: "
2481 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
2482 cmnd->device->id,
2483 cmnd->device->lun, cmnd->cmnd[0],
2484 fcprsp->rspInfo3);
2485 host_status = DID_ERROR;
2486 goto out;
2487 }
6a9c52cf
JS
2488 }
2489
c7743956
JS
2490 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2491 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2492 if (snslen > SCSI_SENSE_BUFFERSIZE)
2493 snslen = SCSI_SENSE_BUFFERSIZE;
2494
2495 if (resp_info & RSP_LEN_VALID)
2496 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2497 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2498 }
2499 lp = (uint32_t *)cmnd->sense_buffer;
2500
73d91e50
JS
2501 if (!scsi_status && (resp_info & RESID_UNDER) &&
2502 vport->cfg_log_verbose & LOG_FCP_UNDER)
2503 logit = LOG_FCP_UNDER;
c7743956 2504
e8b62011 2505 lpfc_printf_vlog(vport, KERN_WARNING, logit,
e2a0a9d6 2506 "9024 FCP command x%x failed: x%x SNS x%x x%x "
e8b62011
JS
2507 "Data: x%x x%x x%x x%x x%x\n",
2508 cmnd->cmnd[0], scsi_status,
2509 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2510 be32_to_cpu(fcprsp->rspResId),
2511 be32_to_cpu(fcprsp->rspSnsLen),
2512 be32_to_cpu(fcprsp->rspRspLen),
2513 fcprsp->rspInfo3);
dea3101e 2514
a0b4f78f 2515 scsi_set_resid(cmnd, 0);
dea3101e 2516 if (resp_info & RESID_UNDER) {
a0b4f78f 2517 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea3101e 2518
73d91e50 2519 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
e2a0a9d6 2520 "9025 FCP Read Underrun, expected %d, "
e8b62011
JS
2521 "residual %d Data: x%x x%x x%x\n",
2522 be32_to_cpu(fcpcmd->fcpDl),
2523 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2524 cmnd->underflow);
dea3101e 2525
7054a606
JS
2526 /*
2527 * If there is an under run check if under run reported by
2528 * storage array is same as the under run reported by HBA.
2529 * If this is not same, there is a dropped frame.
2530 */
2531 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2532 fcpi_parm &&
a0b4f78f 2533 (scsi_get_resid(cmnd) != fcpi_parm)) {
e8b62011
JS
2534 lpfc_printf_vlog(vport, KERN_WARNING,
2535 LOG_FCP | LOG_FCP_ERROR,
e2a0a9d6 2536 "9026 FCP Read Check Error "
e8b62011
JS
2537 "and Underrun Data: x%x x%x x%x x%x\n",
2538 be32_to_cpu(fcpcmd->fcpDl),
2539 scsi_get_resid(cmnd), fcpi_parm,
2540 cmnd->cmnd[0]);
a0b4f78f 2541 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
7054a606
JS
2542 host_status = DID_ERROR;
2543 }
dea3101e 2544 /*
2545 * The cmnd->underflow is the minimum number of bytes that must
25985edc 2546 * be transferred for this command. Provided a sense condition
dea3101e 2547 * is not present, make sure the actual amount transferred is at
2548 * least the underflow value or fail.
2549 */
2550 if (!(resp_info & SNS_LEN_VALID) &&
2551 (scsi_status == SAM_STAT_GOOD) &&
a0b4f78f
FT
2552 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2553 < cmnd->underflow)) {
e8b62011 2554 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
e2a0a9d6 2555 "9027 FCP command x%x residual "
e8b62011
JS
2556 "underrun converted to error "
2557 "Data: x%x x%x x%x\n",
66dbfbe6 2558 cmnd->cmnd[0], scsi_bufflen(cmnd),
e8b62011 2559 scsi_get_resid(cmnd), cmnd->underflow);
dea3101e 2560 host_status = DID_ERROR;
2561 }
2562 } else if (resp_info & RESID_OVER) {
e8b62011 2563 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
e2a0a9d6 2564 "9028 FCP command x%x residual overrun error. "
e4e74273 2565 "Data: x%x x%x\n", cmnd->cmnd[0],
e8b62011 2566 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea3101e 2567 host_status = DID_ERROR;
2568
2569 /*
2570 * Check SLI validation that all the transfer was actually done
2571 * (fcpi_parm should be zero). Apply check only to reads.
2572 */
eee8877e 2573 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
e8b62011 2574 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
e2a0a9d6 2575 "9029 FCP Read Check Error Data: "
eee8877e 2576 "x%x x%x x%x x%x x%x\n",
e8b62011
JS
2577 be32_to_cpu(fcpcmd->fcpDl),
2578 be32_to_cpu(fcprsp->rspResId),
eee8877e
JS
2579 fcpi_parm, cmnd->cmnd[0], scsi_status);
2580 switch (scsi_status) {
2581 case SAM_STAT_GOOD:
2582 case SAM_STAT_CHECK_CONDITION:
2583 /* Fabric dropped a data frame. Fail any successful
2584 * command in which we detected dropped frames.
2585 * A status of good or some check conditions could
2586 * be considered a successful command.
2587 */
2588 host_status = DID_ERROR;
2589 break;
2590 }
a0b4f78f 2591 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea3101e 2592 }
2593
2594 out:
2595 cmnd->result = ScsiResult(host_status, scsi_status);
ea2151b4 2596 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea3101e 2597}
2598
9bad7671 2599/**
3621a710 2600 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
9bad7671
JS
2601 * @phba: The Hba for which this call is being executed.
2602 * @pIocbIn: The command IOCBQ for the scsi cmnd.
3772a991 2603 * @pIocbOut: The response IOCBQ for the scsi cmnd.
9bad7671
JS
2604 *
2605 * This routine assigns scsi command result by looking into response IOCB
2606 * status field appropriately. This routine handles QUEUE FULL condition as
2607 * well by ramping down device queue depth.
2608 **/
dea3101e 2609static void
2610lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2611 struct lpfc_iocbq *pIocbOut)
2612{
2613 struct lpfc_scsi_buf *lpfc_cmd =
2614 (struct lpfc_scsi_buf *) pIocbIn->context1;
2e0fef85 2615 struct lpfc_vport *vport = pIocbIn->vport;
dea3101e 2616 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2617 struct lpfc_nodelist *pnode = rdata->pnode;
75baf696 2618 struct scsi_cmnd *cmd;
445cf4f4 2619 int result;
a257bf90 2620 struct scsi_device *tmp_sdev;
5ffc266e 2621 int depth;
fa61a54e 2622 unsigned long flags;
ea2151b4 2623 struct lpfc_fast_path_event *fast_path_evt;
75baf696 2624 struct Scsi_Host *shost;
a257bf90 2625 uint32_t queue_depth, scsi_id;
73d91e50 2626 uint32_t logit = LOG_FCP;
dea3101e 2627
75baf696
JS
2628 /* Sanity check on return of outstanding command */
2629 if (!(lpfc_cmd->pCmd))
2630 return;
2631 cmd = lpfc_cmd->pCmd;
2632 shost = cmd->device->host;
2633
dea3101e 2634 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2635 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
341af102
JS
2636 /* pick up SLI4 exhange busy status from HBA */
2637 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
2638
109f6ed0
JS
2639 if (pnode && NLP_CHK_NODE_ACT(pnode))
2640 atomic_dec(&pnode->cmd_pending);
dea3101e 2641
2642 if (lpfc_cmd->status) {
2643 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2644 (lpfc_cmd->result & IOERR_DRVR_MASK))
2645 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2646 else if (lpfc_cmd->status >= IOSTAT_CNT)
2647 lpfc_cmd->status = IOSTAT_DEFAULT;
73d91e50
JS
2648 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
2649 && !lpfc_cmd->fcp_rsp->rspStatus3
2650 && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
2651 && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
2652 logit = 0;
2653 else
2654 logit = LOG_FCP | LOG_FCP_UNDER;
2655 lpfc_printf_vlog(vport, KERN_WARNING, logit,
2656 "9030 FCP cmd x%x failed <%d/%d> "
2657 "status: x%x result: x%x Data: x%x x%x\n",
2658 cmd->cmnd[0],
2659 cmd->device ? cmd->device->id : 0xffff,
2660 cmd->device ? cmd->device->lun : 0xffff,
2661 lpfc_cmd->status, lpfc_cmd->result,
2662 pIocbOut->iocb.ulpContext,
2663 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea3101e 2664
2665 switch (lpfc_cmd->status) {
2666 case IOSTAT_FCP_RSP_ERROR:
2667 /* Call FCP RSP handler to determine result */
2e0fef85 2668 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea3101e 2669 break;
2670 case IOSTAT_NPORT_BSY:
2671 case IOSTAT_FABRIC_BSY:
0f1f53a7 2672 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
ea2151b4
JS
2673 fast_path_evt = lpfc_alloc_fast_evt(phba);
2674 if (!fast_path_evt)
2675 break;
2676 fast_path_evt->un.fabric_evt.event_type =
2677 FC_REG_FABRIC_EVENT;
2678 fast_path_evt->un.fabric_evt.subcategory =
2679 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2680 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2681 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2682 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2683 &pnode->nlp_portname,
2684 sizeof(struct lpfc_name));
2685 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2686 &pnode->nlp_nodename,
2687 sizeof(struct lpfc_name));
2688 }
2689 fast_path_evt->vport = vport;
2690 fast_path_evt->work_evt.evt =
2691 LPFC_EVT_FASTPATH_MGMT_EVT;
2692 spin_lock_irqsave(&phba->hbalock, flags);
2693 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2694 &phba->work_list);
2695 spin_unlock_irqrestore(&phba->hbalock, flags);
2696 lpfc_worker_wake_up(phba);
dea3101e 2697 break;
92d7f7b0 2698 case IOSTAT_LOCAL_REJECT:
1151e3ec 2699 case IOSTAT_REMOTE_STOP:
ab56dc2e
JS
2700 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
2701 lpfc_cmd->result ==
2702 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
2703 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
2704 lpfc_cmd->result ==
2705 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
2706 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
2707 break;
2708 }
d7c255b2 2709 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
92d7f7b0 2710 lpfc_cmd->result == IOERR_NO_RESOURCES ||
b92938b4
JS
2711 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
2712 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
92d7f7b0 2713 cmd->result = ScsiResult(DID_REQUEUE, 0);
58da1ffb 2714 break;
e2a0a9d6 2715 }
e2a0a9d6
JS
2716 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2717 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2718 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2719 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2720 /*
2721 * This is a response for a BG enabled
2722 * cmd. Parse BG error
2723 */
2724 lpfc_parse_bg_err(phba, lpfc_cmd,
2725 pIocbOut);
2726 break;
2727 } else {
2728 lpfc_printf_vlog(vport, KERN_WARNING,
2729 LOG_BG,
2730 "9031 non-zero BGSTAT "
6a9c52cf 2731 "on unprotected cmd\n");
e2a0a9d6
JS
2732 }
2733 }
1151e3ec
JS
2734 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
2735 && (phba->sli_rev == LPFC_SLI_REV4)
2736 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
2737 /* This IO was aborted by the target, we don't
2738 * know the rxid and because we did not send the
2739 * ABTS we cannot generate and RRQ.
2740 */
2741 lpfc_set_rrq_active(phba, pnode,
2742 lpfc_cmd->cur_iocbq.sli4_xritag,
2743 0, 0);
2744 }
e2a0a9d6 2745 /* else: fall through */
dea3101e 2746 default:
2747 cmd->result = ScsiResult(DID_ERROR, 0);
2748 break;
2749 }
2750
58da1ffb 2751 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
19a7b4ae 2752 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
0f1f53a7
JS
2753 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2754 SAM_STAT_BUSY);
ab56dc2e 2755 } else
dea3101e 2756 cmd->result = ScsiResult(DID_OK, 0);
dea3101e 2757
2758 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2759 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2760
e8b62011
JS
2761 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2762 "0710 Iodone <%d/%d> cmd %p, error "
2763 "x%x SNS x%x x%x Data: x%x x%x\n",
2764 cmd->device->id, cmd->device->lun, cmd,
2765 cmd->result, *lp, *(lp + 3), cmd->retries,
2766 scsi_get_resid(cmd));
dea3101e 2767 }
2768
ea2151b4 2769 lpfc_update_stats(phba, lpfc_cmd);
445cf4f4 2770 result = cmd->result;
977b5a0a
JS
2771 if (vport->cfg_max_scsicmpl_time &&
2772 time_after(jiffies, lpfc_cmd->start_time +
2773 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
a257bf90 2774 spin_lock_irqsave(shost->host_lock, flags);
109f6ed0
JS
2775 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2776 if (pnode->cmd_qdepth >
2777 atomic_read(&pnode->cmd_pending) &&
2778 (atomic_read(&pnode->cmd_pending) >
2779 LPFC_MIN_TGT_QDEPTH) &&
2780 ((cmd->cmnd[0] == READ_10) ||
2781 (cmd->cmnd[0] == WRITE_10)))
2782 pnode->cmd_qdepth =
2783 atomic_read(&pnode->cmd_pending);
2784
2785 pnode->last_change_time = jiffies;
2786 }
a257bf90 2787 spin_unlock_irqrestore(shost->host_lock, flags);
109f6ed0 2788 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
7dc517df 2789 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
977b5a0a 2790 time_after(jiffies, pnode->last_change_time +
109f6ed0 2791 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
a257bf90 2792 spin_lock_irqsave(shost->host_lock, flags);
7dc517df
JS
2793 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
2794 / 100;
2795 depth = depth ? depth : 1;
2796 pnode->cmd_qdepth += depth;
2797 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
2798 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
109f6ed0 2799 pnode->last_change_time = jiffies;
a257bf90 2800 spin_unlock_irqrestore(shost->host_lock, flags);
109f6ed0 2801 }
977b5a0a
JS
2802 }
2803
1dcb58e5 2804 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
a257bf90
JS
2805
2806 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2807 queue_depth = cmd->device->queue_depth;
2808 scsi_id = cmd->device->id;
0bd4ca25
JSEC
2809 cmd->scsi_done(cmd);
2810
b808608b 2811 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
fa61a54e
JS
2812 /*
2813 * If there is a thread waiting for command completion
2814 * wake up the thread.
2815 */
a257bf90 2816 spin_lock_irqsave(shost->host_lock, flags);
495a714c 2817 lpfc_cmd->pCmd = NULL;
fa61a54e
JS
2818 if (lpfc_cmd->waitq)
2819 wake_up(lpfc_cmd->waitq);
a257bf90 2820 spin_unlock_irqrestore(shost->host_lock, flags);
b808608b
JW
2821 lpfc_release_scsi_buf(phba, lpfc_cmd);
2822 return;
2823 }
2824
92d7f7b0 2825 if (!result)
a257bf90 2826 lpfc_rampup_queue_depth(vport, queue_depth);
92d7f7b0 2827
445cf4f4
JSEC
2828 /*
2829 * Check for queue full. If the lun is reporting queue full, then
2830 * back off the lun queue depth to prevent target overloads.
2831 */
58da1ffb
JS
2832 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2833 NLP_CHK_NODE_ACT(pnode)) {
a257bf90
JS
2834 shost_for_each_device(tmp_sdev, shost) {
2835 if (tmp_sdev->id != scsi_id)
445cf4f4
JSEC
2836 continue;
2837 depth = scsi_track_queue_full(tmp_sdev,
5ffc266e
JS
2838 tmp_sdev->queue_depth-1);
2839 if (depth <= 0)
2840 continue;
e8b62011
JS
2841 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2842 "0711 detected queue full - lun queue "
2843 "depth adjusted to %d.\n", depth);
ea2151b4 2844 lpfc_send_sdev_queuedepth_change_event(phba, vport,
5ffc266e
JS
2845 pnode,
2846 tmp_sdev->lun,
2847 depth+1, depth);
445cf4f4
JSEC
2848 }
2849 }
2850
fa61a54e
JS
2851 /*
2852 * If there is a thread waiting for command completion
2853 * wake up the thread.
2854 */
a257bf90 2855 spin_lock_irqsave(shost->host_lock, flags);
495a714c 2856 lpfc_cmd->pCmd = NULL;
fa61a54e
JS
2857 if (lpfc_cmd->waitq)
2858 wake_up(lpfc_cmd->waitq);
a257bf90 2859 spin_unlock_irqrestore(shost->host_lock, flags);
fa61a54e 2860
0bd4ca25 2861 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 2862}
2863
34b02dcd 2864/**
3621a710 2865 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
34b02dcd
JS
2866 * @data: A pointer to the immediate command data portion of the IOCB.
2867 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2868 *
2869 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2870 * byte swapping the data to big endian format for transmission on the wire.
2871 **/
2872static void
2873lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2874{
2875 int i, j;
2876 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2877 i += sizeof(uint32_t), j++) {
2878 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2879 }
2880}
2881
9bad7671 2882/**
f1126688 2883 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
9bad7671
JS
2884 * @vport: The virtual port for which this call is being executed.
2885 * @lpfc_cmd: The scsi command which needs to send.
2886 * @pnode: Pointer to lpfc_nodelist.
2887 *
2888 * This routine initializes fcp_cmnd and iocb data structure from scsi command
3772a991 2889 * to transfer for device with SLI3 interface spec.
9bad7671 2890 **/
dea3101e 2891static void
f1126688 2892lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2e0fef85 2893 struct lpfc_nodelist *pnode)
dea3101e 2894{
2e0fef85 2895 struct lpfc_hba *phba = vport->phba;
dea3101e 2896 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2897 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2898 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2899 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2900 int datadir = scsi_cmnd->sc_data_direction;
7e2b19fb 2901 char tag[2];
dea3101e 2902
58da1ffb
JS
2903 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2904 return;
2905
dea3101e 2906 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
69859dc4
JSEC
2907 /* clear task management bits */
2908 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea3101e 2909
91886523
JSEC
2910 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2911 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea3101e 2912
2913 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2914
7e2b19fb
JS
2915 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2916 switch (tag[0]) {
dea3101e 2917 case HEAD_OF_QUEUE_TAG:
2918 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2919 break;
2920 case ORDERED_QUEUE_TAG:
2921 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2922 break;
2923 default:
2924 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2925 break;
2926 }
2927 } else
2928 fcp_cmnd->fcpCntl1 = 0;
2929
2930 /*
2931 * There are three possibilities here - use scatter-gather segment, use
2932 * the single mapping, or neither. Start the lpfc command prep by
2933 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2934 * data bde entry.
2935 */
a0b4f78f 2936 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 2937 if (datadir == DMA_TO_DEVICE) {
2938 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
3772a991
JS
2939 if (phba->sli_rev < LPFC_SLI_REV4) {
2940 iocb_cmd->un.fcpi.fcpi_parm = 0;
2941 iocb_cmd->ulpPU = 0;
2942 } else
2943 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea3101e 2944 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2945 phba->fc4OutputRequests++;
2946 } else {
2947 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2948 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea3101e 2949 fcp_cmnd->fcpCntl3 = READ_DATA;
2950 phba->fc4InputRequests++;
2951 }
2952 } else {
2953 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2954 iocb_cmd->un.fcpi.fcpi_parm = 0;
2955 iocb_cmd->ulpPU = 0;
2956 fcp_cmnd->fcpCntl3 = 0;
2957 phba->fc4ControlRequests++;
2958 }
e2a0a9d6
JS
2959 if (phba->sli_rev == 3 &&
2960 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
34b02dcd 2961 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 2962 /*
2963 * Finish initializing those IOCB fields that are independent
2964 * of the scsi_cmnd request_buffer
2965 */
2966 piocbq->iocb.ulpContext = pnode->nlp_rpi;
6d368e53
JS
2967 if (phba->sli_rev == LPFC_SLI_REV4)
2968 piocbq->iocb.ulpContext =
2969 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea3101e 2970 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2971 piocbq->iocb.ulpFCP2Rcvy = 1;
09372820
JS
2972 else
2973 piocbq->iocb.ulpFCP2Rcvy = 0;
dea3101e 2974
2975 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2976 piocbq->context1 = lpfc_cmd;
2977 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2978 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2e0fef85 2979 piocbq->vport = vport;
dea3101e 2980}
2981
da0436e9 2982/**
6d368e53 2983 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
9bad7671
JS
2984 * @vport: The virtual port for which this call is being executed.
2985 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2986 * @lun: Logical unit number.
2987 * @task_mgmt_cmd: SCSI task management command.
2988 *
3772a991
JS
2989 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2990 * for device with SLI-3 interface spec.
9bad7671
JS
2991 *
2992 * Return codes:
2993 * 0 - Error
2994 * 1 - Success
2995 **/
dea3101e 2996static int
f1126688 2997lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea3101e 2998 struct lpfc_scsi_buf *lpfc_cmd,
420b630d 2999 unsigned int lun,
dea3101e 3000 uint8_t task_mgmt_cmd)
3001{
dea3101e 3002 struct lpfc_iocbq *piocbq;
3003 IOCB_t *piocb;
3004 struct fcp_cmnd *fcp_cmnd;
0b18ac42 3005 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea3101e 3006 struct lpfc_nodelist *ndlp = rdata->pnode;
3007
58da1ffb
JS
3008 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3009 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea3101e 3010 return 0;
dea3101e 3011
dea3101e 3012 piocbq = &(lpfc_cmd->cur_iocbq);
2e0fef85
JS
3013 piocbq->vport = vport;
3014
dea3101e 3015 piocb = &piocbq->iocb;
3016
3017 fcp_cmnd = lpfc_cmd->fcp_cmnd;
34b02dcd
JS
3018 /* Clear out any old data in the FCP command area */
3019 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
3020 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea3101e 3021 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
e2a0a9d6
JS
3022 if (vport->phba->sli_rev == 3 &&
3023 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
34b02dcd 3024 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 3025 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea3101e 3026 piocb->ulpContext = ndlp->nlp_rpi;
6d368e53
JS
3027 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
3028 piocb->ulpContext =
3029 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
3030 }
dea3101e 3031 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
3032 piocb->ulpFCP2Rcvy = 1;
3033 }
3034 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
3035
3036 /* ulpTimeout is only one byte */
3037 if (lpfc_cmd->timeout > 0xff) {
3038 /*
3039 * Do not timeout the command at the firmware level.
3040 * The driver will provide the timeout mechanism.
3041 */
3042 piocb->ulpTimeout = 0;
f1126688 3043 } else
dea3101e 3044 piocb->ulpTimeout = lpfc_cmd->timeout;
da0436e9 3045
f1126688
JS
3046 if (vport->phba->sli_rev == LPFC_SLI_REV4)
3047 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
3772a991 3048
f1126688 3049 return 1;
3772a991
JS
3050}
3051
3052/**
25985edc 3053 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
3772a991
JS
3054 * @phba: The hba struct for which this call is being executed.
3055 * @dev_grp: The HBA PCI-Device group number.
3056 *
3057 * This routine sets up the SCSI interface API function jump table in @phba
3058 * struct.
3059 * Returns: 0 - success, -ENODEV - failure.
3060 **/
3061int
3062lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3063{
3064
f1126688
JS
3065 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
3066 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
f1126688 3067
3772a991
JS
3068 switch (dev_grp) {
3069 case LPFC_PCI_DEV_LP:
3070 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
3071 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
3772a991 3072 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
19ca7609 3073 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
3772a991 3074 break;
da0436e9
JS
3075 case LPFC_PCI_DEV_OC:
3076 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
3077 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
da0436e9 3078 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
19ca7609 3079 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
da0436e9 3080 break;
3772a991
JS
3081 default:
3082 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3083 "1418 Invalid HBA PCI-device group: 0x%x\n",
3084 dev_grp);
3085 return -ENODEV;
3086 break;
3087 }
3772a991 3088 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
84d1b006 3089 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
3772a991
JS
3090 return 0;
3091}
3092
9bad7671 3093/**
3621a710 3094 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
9bad7671
JS
3095 * @phba: The Hba for which this call is being executed.
3096 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
3097 * @rspiocbq: Pointer to lpfc_iocbq data structure.
3098 *
3099 * This routine is IOCB completion routine for device reset and target reset
3100 * routine. This routine release scsi buffer associated with lpfc_cmd.
3101 **/
7054a606
JS
3102static void
3103lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
3104 struct lpfc_iocbq *cmdiocbq,
3105 struct lpfc_iocbq *rspiocbq)
3106{
3107 struct lpfc_scsi_buf *lpfc_cmd =
3108 (struct lpfc_scsi_buf *) cmdiocbq->context1;
3109 if (lpfc_cmd)
3110 lpfc_release_scsi_buf(phba, lpfc_cmd);
3111 return;
3112}
3113
9bad7671 3114/**
3621a710 3115 * lpfc_info - Info entry point of scsi_host_template data structure
9bad7671
JS
3116 * @host: The scsi host for which this call is being executed.
3117 *
3118 * This routine provides module information about hba.
3119 *
3120 * Reutrn code:
3121 * Pointer to char - Success.
3122 **/
dea3101e 3123const char *
3124lpfc_info(struct Scsi_Host *host)
3125{
2e0fef85
JS
3126 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
3127 struct lpfc_hba *phba = vport->phba;
dea3101e 3128 int len;
3129 static char lpfcinfobuf[384];
3130
3131 memset(lpfcinfobuf,0,384);
3132 if (phba && phba->pcidev){
3133 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
3134 len = strlen(lpfcinfobuf);
3135 snprintf(lpfcinfobuf + len,
3136 384-len,
3137 " on PCI bus %02x device %02x irq %d",
3138 phba->pcidev->bus->number,
3139 phba->pcidev->devfn,
3140 phba->pcidev->irq);
3141 len = strlen(lpfcinfobuf);
3142 if (phba->Port[0]) {
3143 snprintf(lpfcinfobuf + len,
3144 384-len,
3145 " port %s",
3146 phba->Port);
3147 }
65467b6b
JS
3148 len = strlen(lpfcinfobuf);
3149 if (phba->sli4_hba.link_state.logical_speed) {
3150 snprintf(lpfcinfobuf + len,
3151 384-len,
3152 " Logical Link Speed: %d Mbps",
3153 phba->sli4_hba.link_state.logical_speed * 10);
3154 }
dea3101e 3155 }
3156 return lpfcinfobuf;
3157}
3158
9bad7671 3159/**
3621a710 3160 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
9bad7671
JS
3161 * @phba: The Hba for which this call is being executed.
3162 *
3163 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
3164 * The default value of cfg_poll_tmo is 10 milliseconds.
3165 **/
875fbdfe
JSEC
3166static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
3167{
3168 unsigned long poll_tmo_expires =
3169 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
3170
3171 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
3172 mod_timer(&phba->fcp_poll_timer,
3173 poll_tmo_expires);
3174}
3175
9bad7671 3176/**
3621a710 3177 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
9bad7671
JS
3178 * @phba: The Hba for which this call is being executed.
3179 *
3180 * This routine starts the fcp_poll_timer of @phba.
3181 **/
875fbdfe
JSEC
3182void lpfc_poll_start_timer(struct lpfc_hba * phba)
3183{
3184 lpfc_poll_rearm_timer(phba);
3185}
3186
9bad7671 3187/**
3621a710 3188 * lpfc_poll_timeout - Restart polling timer
9bad7671
JS
3189 * @ptr: Map to lpfc_hba data structure pointer.
3190 *
3191 * This routine restarts fcp_poll timer, when FCP ring polling is enable
3192 * and FCP Ring interrupt is disable.
3193 **/
3194
875fbdfe
JSEC
3195void lpfc_poll_timeout(unsigned long ptr)
3196{
2e0fef85 3197 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
875fbdfe
JSEC
3198
3199 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
3200 lpfc_sli_handle_fast_ring_event(phba,
3201 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3202
875fbdfe
JSEC
3203 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3204 lpfc_poll_rearm_timer(phba);
3205 }
875fbdfe
JSEC
3206}
3207
9bad7671 3208/**
3621a710 3209 * lpfc_queuecommand - scsi_host_template queuecommand entry point
9bad7671
JS
3210 * @cmnd: Pointer to scsi_cmnd data structure.
3211 * @done: Pointer to done routine.
3212 *
3213 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
3214 * This routine prepares an IOCB from scsi command and provides to firmware.
3215 * The @done callback is invoked after driver finished processing the command.
3216 *
3217 * Return value :
3218 * 0 - Success
3219 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
3220 **/
dea3101e 3221static int
f281233d 3222lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
dea3101e 3223{
2e0fef85
JS
3224 struct Scsi_Host *shost = cmnd->device->host;
3225 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3226 struct lpfc_hba *phba = vport->phba;
dea3101e 3227 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1c6f4ef5 3228 struct lpfc_nodelist *ndlp;
0bd4ca25 3229 struct lpfc_scsi_buf *lpfc_cmd;
19a7b4ae 3230 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
19a7b4ae 3231 int err;
dea3101e 3232
19a7b4ae
JSEC
3233 err = fc_remote_port_chkready(rport);
3234 if (err) {
3235 cmnd->result = err;
dea3101e 3236 goto out_fail_command;
3237 }
1c6f4ef5 3238 ndlp = rdata->pnode;
dea3101e 3239
bf08611b
JS
3240 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
3241 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) ||
3242 (phba->sli_rev == LPFC_SLI_REV4))) {
e2a0a9d6 3243
6a9c52cf
JS
3244 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3245 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
3246 " op:%02x str=%s without registering for"
3247 " BlockGuard - Rejecting command\n",
e2a0a9d6
JS
3248 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3249 dif_op_str[scsi_get_prot_op(cmnd)]);
3250 goto out_fail_command;
3251 }
3252
dea3101e 3253 /*
19a7b4ae
JSEC
3254 * Catch race where our node has transitioned, but the
3255 * transport is still transitioning.
dea3101e 3256 */
b522d7d4 3257 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
f55ca84d 3258 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
b522d7d4
JS
3259 goto out_fail_command;
3260 }
7dc517df 3261 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
3496343d 3262 goto out_tgt_busy;
a93ce024 3263
19ca7609 3264 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea3101e 3265 if (lpfc_cmd == NULL) {
eaf15d5b 3266 lpfc_rampdown_queue_depth(phba);
92d7f7b0 3267
e8b62011
JS
3268 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3269 "0707 driver's buffer pool is empty, "
3270 "IO busied\n");
dea3101e 3271 goto out_host_busy;
3272 }
3273
3274 /*
3275 * Store the midlayer's command structure for the completion phase
3276 * and complete the command initialization.
3277 */
3278 lpfc_cmd->pCmd = cmnd;
3279 lpfc_cmd->rdata = rdata;
3280 lpfc_cmd->timeout = 0;
977b5a0a 3281 lpfc_cmd->start_time = jiffies;
dea3101e 3282 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
3283 cmnd->scsi_done = done;
3284
e2a0a9d6 3285 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
6a9c52cf
JS
3286 if (vport->phba->cfg_enable_bg) {
3287 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6
JS
3288 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
3289 "str=%s\n",
3290 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3291 dif_op_str[scsi_get_prot_op(cmnd)]);
6a9c52cf 3292 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 3293 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
e4e74273 3294 "%02x %02x %02x %02x %02x\n",
e2a0a9d6
JS
3295 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
3296 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
3297 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
3298 cmnd->cmnd[9]);
6a9c52cf
JS
3299 if (cmnd->cmnd[0] == READ_10)
3300 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 3301 "9035 BLKGRD: READ @ sector %llu, "
83096ebf
TH
3302 "count %u\n",
3303 (unsigned long long)scsi_get_lba(cmnd),
3304 blk_rq_sectors(cmnd->request));
6a9c52cf
JS
3305 else if (cmnd->cmnd[0] == WRITE_10)
3306 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 3307 "9036 BLKGRD: WRITE @ sector %llu, "
83096ebf 3308 "count %u cmd=%p\n",
87b5c328 3309 (unsigned long long)scsi_get_lba(cmnd),
83096ebf 3310 blk_rq_sectors(cmnd->request),
e2a0a9d6 3311 cmnd);
6a9c52cf 3312 }
e2a0a9d6
JS
3313
3314 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3315 } else {
6a9c52cf 3316 if (vport->phba->cfg_enable_bg) {
e2a0a9d6 3317 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
6a9c52cf
JS
3318 "9038 BLKGRD: rcvd unprotected cmd:"
3319 "%02x op:%02x str=%s\n",
3320 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3321 dif_op_str[scsi_get_prot_op(cmnd)]);
3322 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3323 "9039 BLKGRD: CDB: %02x %02x %02x "
3324 "%02x %02x %02x %02x %02x %02x %02x\n",
3325 cmnd->cmnd[0], cmnd->cmnd[1],
3326 cmnd->cmnd[2], cmnd->cmnd[3],
3327 cmnd->cmnd[4], cmnd->cmnd[5],
3328 cmnd->cmnd[6], cmnd->cmnd[7],
3329 cmnd->cmnd[8], cmnd->cmnd[9]);
3330 if (cmnd->cmnd[0] == READ_10)
3331 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3332 "9040 dbg: READ @ sector %llu, "
3333 "count %u\n",
3334 (unsigned long long)scsi_get_lba(cmnd),
83096ebf 3335 blk_rq_sectors(cmnd->request));
6a9c52cf
JS
3336 else if (cmnd->cmnd[0] == WRITE_10)
3337 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 3338 "9041 dbg: WRITE @ sector %llu, "
83096ebf 3339 "count %u cmd=%p\n",
87b5c328 3340 (unsigned long long)scsi_get_lba(cmnd),
83096ebf 3341 blk_rq_sectors(cmnd->request), cmnd);
6a9c52cf
JS
3342 else
3343 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
e2a0a9d6 3344 "9042 dbg: parser not implemented\n");
6a9c52cf 3345 }
e2a0a9d6
JS
3346 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3347 }
3348
dea3101e 3349 if (err)
3350 goto out_host_busy_free_buf;
3351
2e0fef85 3352 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea3101e 3353
977b5a0a 3354 atomic_inc(&ndlp->cmd_pending);
3772a991 3355 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
92d7f7b0 3356 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
eaf15d5b
JS
3357 if (err) {
3358 atomic_dec(&ndlp->cmd_pending);
dea3101e 3359 goto out_host_busy_free_buf;
eaf15d5b 3360 }
875fbdfe 3361 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
3362 spin_unlock(shost->host_lock);
3363 lpfc_sli_handle_fast_ring_event(phba,
3364 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3365
3366 spin_lock(shost->host_lock);
875fbdfe
JSEC
3367 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3368 lpfc_poll_rearm_timer(phba);
3369 }
3370
dea3101e 3371 return 0;
3372
3373 out_host_busy_free_buf:
bcf4dbfa 3374 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
0bd4ca25 3375 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 3376 out_host_busy:
3377 return SCSI_MLQUEUE_HOST_BUSY;
3378
3496343d
MC
3379 out_tgt_busy:
3380 return SCSI_MLQUEUE_TARGET_BUSY;
3381
dea3101e 3382 out_fail_command:
3383 done(cmnd);
3384 return 0;
3385}
3386
f281233d
JG
3387static DEF_SCSI_QCMD(lpfc_queuecommand)
3388
9bad7671 3389/**
3621a710 3390 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
9bad7671
JS
3391 * @cmnd: Pointer to scsi_cmnd data structure.
3392 *
3393 * This routine aborts @cmnd pending in base driver.
3394 *
3395 * Return code :
3396 * 0x2003 - Error
3397 * 0x2002 - Success
3398 **/
dea3101e 3399static int
63c59c3b 3400lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea3101e 3401{
2e0fef85
JS
3402 struct Scsi_Host *shost = cmnd->device->host;
3403 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3404 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
3405 struct lpfc_iocbq *iocb;
3406 struct lpfc_iocbq *abtsiocb;
dea3101e 3407 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 3408 IOCB_t *cmd, *icmd;
0bd4ca25 3409 int ret = SUCCESS;
fa61a54e 3410 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
dea3101e 3411
589a52d6
JS
3412 ret = fc_block_scsi_eh(cmnd);
3413 if (ret)
3414 return ret;
0bd4ca25 3415 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
eee8877e
JS
3416 if (!lpfc_cmd) {
3417 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3418 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
5cd049a5
CH
3419 "x%x ID %d LUN %d\n",
3420 ret, cmnd->device->id, cmnd->device->lun);
eee8877e
JS
3421 return SUCCESS;
3422 }
dea3101e 3423
0bd4ca25
JSEC
3424 /*
3425 * If pCmd field of the corresponding lpfc_scsi_buf structure
3426 * points to a different SCSI command, then the driver has
3427 * already completed this command, but the midlayer did not
3428 * see the completion before the eh fired. Just return
3429 * SUCCESS.
3430 */
3431 iocb = &lpfc_cmd->cur_iocbq;
3432 if (lpfc_cmd->pCmd != cmnd)
3433 goto out;
dea3101e 3434
0bd4ca25 3435 BUG_ON(iocb->context1 != lpfc_cmd);
dea3101e 3436
0bd4ca25
JSEC
3437 abtsiocb = lpfc_sli_get_iocbq(phba);
3438 if (abtsiocb == NULL) {
3439 ret = FAILED;
dea3101e 3440 goto out;
3441 }
3442
dea3101e 3443 /*
0bd4ca25
JSEC
3444 * The scsi command can not be in txq and it is in flight because the
3445 * pCmd is still pointig at the SCSI command we have to abort. There
3446 * is no need to search the txcmplq. Just send an abort to the FW.
dea3101e 3447 */
dea3101e 3448
0bd4ca25
JSEC
3449 cmd = &iocb->iocb;
3450 icmd = &abtsiocb->iocb;
3451 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3452 icmd->un.acxri.abortContextTag = cmd->ulpContext;
3772a991
JS
3453 if (phba->sli_rev == LPFC_SLI_REV4)
3454 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3455 else
3456 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 3457
0bd4ca25
JSEC
3458 icmd->ulpLe = 1;
3459 icmd->ulpClass = cmd->ulpClass;
5ffc266e
JS
3460
3461 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
3462 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
341af102 3463 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 3464
2e0fef85 3465 if (lpfc_is_link_up(phba))
0bd4ca25
JSEC
3466 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3467 else
3468 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 3469
0bd4ca25 3470 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
2e0fef85 3471 abtsiocb->vport = vport;
3772a991
JS
3472 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3473 IOCB_ERROR) {
0bd4ca25
JSEC
3474 lpfc_sli_release_iocbq(phba, abtsiocb);
3475 ret = FAILED;
3476 goto out;
3477 }
dea3101e 3478
875fbdfe 3479 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
45ed1190
JS
3480 lpfc_sli_handle_fast_ring_event(phba,
3481 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
875fbdfe 3482
fa61a54e 3483 lpfc_cmd->waitq = &waitq;
0bd4ca25 3484 /* Wait for abort to complete */
fa61a54e
JS
3485 wait_event_timeout(waitq,
3486 (lpfc_cmd->pCmd != cmnd),
3487 (2*vport->cfg_devloss_tmo*HZ));
875fbdfe 3488
fa61a54e
JS
3489 spin_lock_irq(shost->host_lock);
3490 lpfc_cmd->waitq = NULL;
3491 spin_unlock_irq(shost->host_lock);
dea3101e 3492
0bd4ca25
JSEC
3493 if (lpfc_cmd->pCmd == cmnd) {
3494 ret = FAILED;
e8b62011
JS
3495 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3496 "0748 abort handler timed out waiting "
3497 "for abort to complete: ret %#x, ID %d, "
5cd049a5
CH
3498 "LUN %d\n",
3499 ret, cmnd->device->id, cmnd->device->lun);
dea3101e 3500 }
3501
3502 out:
e8b62011
JS
3503 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3504 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
5cd049a5
CH
3505 "LUN %d\n", ret, cmnd->device->id,
3506 cmnd->device->lun);
63c59c3b 3507 return ret;
8fa728a2
JG
3508}
3509
bbb9d180
JS
3510static char *
3511lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3512{
3513 switch (task_mgmt_cmd) {
3514 case FCP_ABORT_TASK_SET:
3515 return "ABORT_TASK_SET";
3516 case FCP_CLEAR_TASK_SET:
3517 return "FCP_CLEAR_TASK_SET";
3518 case FCP_BUS_RESET:
3519 return "FCP_BUS_RESET";
3520 case FCP_LUN_RESET:
3521 return "FCP_LUN_RESET";
3522 case FCP_TARGET_RESET:
3523 return "FCP_TARGET_RESET";
3524 case FCP_CLEAR_ACA:
3525 return "FCP_CLEAR_ACA";
3526 case FCP_TERMINATE_TASK:
3527 return "FCP_TERMINATE_TASK";
3528 default:
3529 return "unknown";
3530 }
3531}
3532
9bad7671 3533/**
bbb9d180
JS
3534 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3535 * @vport: The virtual port for which this call is being executed.
3536 * @rdata: Pointer to remote port local data
3537 * @tgt_id: Target ID of remote device.
3538 * @lun_id: Lun number for the TMF
3539 * @task_mgmt_cmd: type of TMF to send
9bad7671 3540 *
bbb9d180
JS
3541 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3542 * a remote port.
9bad7671 3543 *
bbb9d180
JS
3544 * Return Code:
3545 * 0x2003 - Error
3546 * 0x2002 - Success.
9bad7671 3547 **/
dea3101e 3548static int
bbb9d180
JS
3549lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3550 unsigned tgt_id, unsigned int lun_id,
3551 uint8_t task_mgmt_cmd)
dea3101e 3552{
2e0fef85 3553 struct lpfc_hba *phba = vport->phba;
0bd4ca25 3554 struct lpfc_scsi_buf *lpfc_cmd;
bbb9d180
JS
3555 struct lpfc_iocbq *iocbq;
3556 struct lpfc_iocbq *iocbqrsp;
5989b8d4 3557 struct lpfc_nodelist *pnode = rdata->pnode;
bbb9d180 3558 int ret;
915caaaf 3559 int status;
dea3101e 3560
5989b8d4 3561 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
915caaaf 3562 return FAILED;
bbb9d180 3563
19ca7609 3564 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
dea3101e 3565 if (lpfc_cmd == NULL)
915caaaf 3566 return FAILED;
dea3101e 3567 lpfc_cmd->timeout = 60;
0b18ac42 3568 lpfc_cmd->rdata = rdata;
dea3101e 3569
bbb9d180
JS
3570 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3571 task_mgmt_cmd);
915caaaf
JS
3572 if (!status) {
3573 lpfc_release_scsi_buf(phba, lpfc_cmd);
3574 return FAILED;
3575 }
dea3101e 3576
bbb9d180 3577 iocbq = &lpfc_cmd->cur_iocbq;
0bd4ca25 3578 iocbqrsp = lpfc_sli_get_iocbq(phba);
915caaaf
JS
3579 if (iocbqrsp == NULL) {
3580 lpfc_release_scsi_buf(phba, lpfc_cmd);
3581 return FAILED;
3582 }
bbb9d180 3583
e8b62011 3584 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
bbb9d180 3585 "0702 Issue %s to TGT %d LUN %d "
6d368e53 3586 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
bbb9d180 3587 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
6d368e53
JS
3588 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
3589 iocbq->iocb_flag);
bbb9d180 3590
3772a991 3591 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
915caaaf 3592 iocbq, iocbqrsp, lpfc_cmd->timeout);
bbb9d180
JS
3593 if (status != IOCB_SUCCESS) {
3594 if (status == IOCB_TIMEDOUT) {
3595 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3596 ret = TIMEOUT_ERROR;
3597 } else
915caaaf 3598 ret = FAILED;
bbb9d180
JS
3599 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3600 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
6d368e53
JS
3601 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
3602 "iocb_flag x%x\n",
bbb9d180
JS
3603 lpfc_taskmgmt_name(task_mgmt_cmd),
3604 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
6d368e53
JS
3605 iocbqrsp->iocb.un.ulpWord[4],
3606 iocbq->iocb_flag);
2a9bf3d0
JS
3607 } else if (status == IOCB_BUSY)
3608 ret = FAILED;
3609 else
bbb9d180
JS
3610 ret = SUCCESS;
3611
6175c02a 3612 lpfc_sli_release_iocbq(phba, iocbqrsp);
bbb9d180
JS
3613
3614 if (ret != TIMEOUT_ERROR)
3615 lpfc_release_scsi_buf(phba, lpfc_cmd);
3616
3617 return ret;
3618}
3619
3620/**
3621 * lpfc_chk_tgt_mapped -
3622 * @vport: The virtual port to check on
3623 * @cmnd: Pointer to scsi_cmnd data structure.
3624 *
3625 * This routine delays until the scsi target (aka rport) for the
3626 * command exists (is present and logged in) or we declare it non-existent.
3627 *
3628 * Return code :
3629 * 0x2003 - Error
3630 * 0x2002 - Success
3631 **/
3632static int
3633lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3634{
3635 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1c6f4ef5 3636 struct lpfc_nodelist *pnode;
bbb9d180
JS
3637 unsigned long later;
3638
1c6f4ef5
JS
3639 if (!rdata) {
3640 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3641 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
3642 return FAILED;
3643 }
3644 pnode = rdata->pnode;
bbb9d180
JS
3645 /*
3646 * If target is not in a MAPPED state, delay until
3647 * target is rediscovered or devloss timeout expires.
3648 */
3649 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3650 while (time_after(later, jiffies)) {
3651 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3652 return FAILED;
3653 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3654 return SUCCESS;
3655 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3656 rdata = cmnd->device->hostdata;
3657 if (!rdata)
3658 return FAILED;
3659 pnode = rdata->pnode;
3660 }
3661 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3662 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3663 return FAILED;
3664 return SUCCESS;
3665}
3666
3667/**
3668 * lpfc_reset_flush_io_context -
3669 * @vport: The virtual port (scsi_host) for the flush context
3670 * @tgt_id: If aborting by Target contect - specifies the target id
3671 * @lun_id: If aborting by Lun context - specifies the lun id
3672 * @context: specifies the context level to flush at.
3673 *
3674 * After a reset condition via TMF, we need to flush orphaned i/o
3675 * contexts from the adapter. This routine aborts any contexts
3676 * outstanding, then waits for their completions. The wait is
3677 * bounded by devloss_tmo though.
3678 *
3679 * Return code :
3680 * 0x2003 - Error
3681 * 0x2002 - Success
3682 **/
3683static int
3684lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3685 uint64_t lun_id, lpfc_ctx_cmd context)
3686{
3687 struct lpfc_hba *phba = vport->phba;
3688 unsigned long later;
3689 int cnt;
3690
3691 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
6175c02a 3692 if (cnt)
51ef4c26 3693 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
bbb9d180 3694 tgt_id, lun_id, context);
915caaaf
JS
3695 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3696 while (time_after(later, jiffies) && cnt) {
3697 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
bbb9d180 3698 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
dea3101e 3699 }
dea3101e 3700 if (cnt) {
e8b62011 3701 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
bbb9d180
JS
3702 "0724 I/O flush failure for context %s : cnt x%x\n",
3703 ((context == LPFC_CTX_LUN) ? "LUN" :
3704 ((context == LPFC_CTX_TGT) ? "TGT" :
3705 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3706 cnt);
3707 return FAILED;
dea3101e 3708 }
bbb9d180
JS
3709 return SUCCESS;
3710}
3711
3712/**
3713 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
3714 * @cmnd: Pointer to scsi_cmnd data structure.
3715 *
3716 * This routine does a device reset by sending a LUN_RESET task management
3717 * command.
3718 *
3719 * Return code :
3720 * 0x2003 - Error
3721 * 0x2002 - Success
3722 **/
3723static int
3724lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
3725{
3726 struct Scsi_Host *shost = cmnd->device->host;
3727 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3728 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1c6f4ef5 3729 struct lpfc_nodelist *pnode;
bbb9d180
JS
3730 unsigned tgt_id = cmnd->device->id;
3731 unsigned int lun_id = cmnd->device->lun;
3732 struct lpfc_scsi_event_header scsi_event;
3733 int status;
3734
1c6f4ef5
JS
3735 if (!rdata) {
3736 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3737 "0798 Device Reset rport failure: rdata x%p\n", rdata);
3738 return FAILED;
3739 }
3740 pnode = rdata->pnode;
589a52d6
JS
3741 status = fc_block_scsi_eh(cmnd);
3742 if (status)
3743 return status;
bbb9d180
JS
3744
3745 status = lpfc_chk_tgt_mapped(vport, cmnd);
3746 if (status == FAILED) {
3747 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3748 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3749 return FAILED;
3750 }
3751
3752 scsi_event.event_type = FC_REG_SCSI_EVENT;
3753 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3754 scsi_event.lun = lun_id;
3755 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3756 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3757
3758 fc_host_post_vendor_event(shost, fc_get_event_number(),
3759 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3760
3761 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3762 FCP_LUN_RESET);
3763
3764 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3765 "0713 SCSI layer issued Device Reset (%d, %d) "
3766 "return x%x\n", tgt_id, lun_id, status);
3767
3768 /*
3769 * We have to clean up i/o as : they may be orphaned by the TMF;
3770 * or if the TMF failed, they may be in an indeterminate state.
3771 * So, continue on.
3772 * We will report success if all the i/o aborts successfully.
3773 */
3774 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3775 LPFC_CTX_LUN);
3776 return status;
3777}
3778
3779/**
3780 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3781 * @cmnd: Pointer to scsi_cmnd data structure.
3782 *
3783 * This routine does a target reset by sending a TARGET_RESET task management
3784 * command.
3785 *
3786 * Return code :
3787 * 0x2003 - Error
3788 * 0x2002 - Success
3789 **/
3790static int
3791lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3792{
3793 struct Scsi_Host *shost = cmnd->device->host;
3794 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3795 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1c6f4ef5 3796 struct lpfc_nodelist *pnode;
bbb9d180
JS
3797 unsigned tgt_id = cmnd->device->id;
3798 unsigned int lun_id = cmnd->device->lun;
3799 struct lpfc_scsi_event_header scsi_event;
3800 int status;
3801
1c6f4ef5
JS
3802 if (!rdata) {
3803 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3804 "0799 Target Reset rport failure: rdata x%p\n", rdata);
3805 return FAILED;
3806 }
3807 pnode = rdata->pnode;
589a52d6
JS
3808 status = fc_block_scsi_eh(cmnd);
3809 if (status)
3810 return status;
bbb9d180
JS
3811
3812 status = lpfc_chk_tgt_mapped(vport, cmnd);
3813 if (status == FAILED) {
3814 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3815 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3816 return FAILED;
3817 }
3818
3819 scsi_event.event_type = FC_REG_SCSI_EVENT;
3820 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3821 scsi_event.lun = 0;
3822 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3823 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3824
3825 fc_host_post_vendor_event(shost, fc_get_event_number(),
3826 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3827
3828 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3829 FCP_TARGET_RESET);
3830
3831 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3832 "0723 SCSI layer issued Target Reset (%d, %d) "
3833 "return x%x\n", tgt_id, lun_id, status);
3834
3835 /*
3836 * We have to clean up i/o as : they may be orphaned by the TMF;
3837 * or if the TMF failed, they may be in an indeterminate state.
3838 * So, continue on.
3839 * We will report success if all the i/o aborts successfully.
3840 */
3841 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3842 LPFC_CTX_TGT);
3843 return status;
dea3101e 3844}
3845
9bad7671 3846/**
3621a710 3847 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
9bad7671
JS
3848 * @cmnd: Pointer to scsi_cmnd data structure.
3849 *
bbb9d180
JS
3850 * This routine does target reset to all targets on @cmnd->device->host.
3851 * This emulates Parallel SCSI Bus Reset Semantics.
9bad7671 3852 *
bbb9d180
JS
3853 * Return code :
3854 * 0x2003 - Error
3855 * 0x2002 - Success
9bad7671 3856 **/
94d0e7b8 3857static int
7054a606 3858lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea3101e 3859{
2e0fef85
JS
3860 struct Scsi_Host *shost = cmnd->device->host;
3861 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea3101e 3862 struct lpfc_nodelist *ndlp = NULL;
ea2151b4 3863 struct lpfc_scsi_event_header scsi_event;
bbb9d180
JS
3864 int match;
3865 int ret = SUCCESS, status, i;
ea2151b4
JS
3866
3867 scsi_event.event_type = FC_REG_SCSI_EVENT;
3868 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3869 scsi_event.lun = 0;
3870 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3871 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3872
bbb9d180
JS
3873 fc_host_post_vendor_event(shost, fc_get_event_number(),
3874 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea3101e 3875
bf08611b
JS
3876 status = fc_block_scsi_eh(cmnd);
3877 if (status)
3878 return status;
bbb9d180 3879
dea3101e 3880 /*
3881 * Since the driver manages a single bus device, reset all
3882 * targets known to the driver. Should any target reset
3883 * fail, this routine returns failure to the midlayer.
3884 */
e17da18e 3885 for (i = 0; i < LPFC_MAX_TARGET; i++) {
685f0bf7 3886 /* Search for mapped node by target ID */
dea3101e 3887 match = 0;
2e0fef85
JS
3888 spin_lock_irq(shost->host_lock);
3889 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
3890 if (!NLP_CHK_NODE_ACT(ndlp))
3891 continue;
685f0bf7 3892 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
915caaaf 3893 ndlp->nlp_sid == i &&
685f0bf7 3894 ndlp->rport) {
dea3101e 3895 match = 1;
3896 break;
3897 }
3898 }
2e0fef85 3899 spin_unlock_irq(shost->host_lock);
dea3101e 3900 if (!match)
3901 continue;
bbb9d180
JS
3902
3903 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3904 i, 0, FCP_TARGET_RESET);
3905
3906 if (status != SUCCESS) {
e8b62011
JS
3907 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3908 "0700 Bus Reset on target %d failed\n",
3909 i);
915caaaf 3910 ret = FAILED;
dea3101e 3911 }
3912 }
6175c02a 3913 /*
bbb9d180
JS
3914 * We have to clean up i/o as : they may be orphaned by the TMFs
3915 * above; or if any of the TMFs failed, they may be in an
3916 * indeterminate state.
3917 * We will report success if all the i/o aborts successfully.
6175c02a 3918 */
bbb9d180
JS
3919
3920 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3921 if (status != SUCCESS)
0bd4ca25 3922 ret = FAILED;
bbb9d180 3923
e8b62011
JS
3924 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3925 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea3101e 3926 return ret;
3927}
3928
9bad7671 3929/**
3621a710 3930 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
9bad7671
JS
3931 * @sdev: Pointer to scsi_device.
3932 *
3933 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3934 * globally available list of scsi buffers. This routine also makes sure scsi
3935 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3936 * of scsi buffer exists for the lifetime of the driver.
3937 *
3938 * Return codes:
3939 * non-0 - Error
3940 * 0 - Success
3941 **/
dea3101e 3942static int
3943lpfc_slave_alloc(struct scsi_device *sdev)
3944{
2e0fef85
JS
3945 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3946 struct lpfc_hba *phba = vport->phba;
19a7b4ae 3947 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3772a991 3948 uint32_t total = 0;
dea3101e 3949 uint32_t num_to_alloc = 0;
3772a991 3950 int num_allocated = 0;
d7c47992 3951 uint32_t sdev_cnt;
dea3101e 3952
19a7b4ae 3953 if (!rport || fc_remote_port_chkready(rport))
dea3101e 3954 return -ENXIO;
3955
19a7b4ae 3956 sdev->hostdata = rport->dd_data;
d7c47992 3957 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea3101e 3958
3959 /*
3960 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3961 * available list of scsi buffers. Don't allocate more than the
a784efbf
JSEC
3962 * HBA limit conveyed to the midlayer via the host structure. The
3963 * formula accounts for the lun_queue_depth + error handlers + 1
3964 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea3101e 3965 */
3966 total = phba->total_scsi_bufs;
3de2a653 3967 num_to_alloc = vport->cfg_lun_queue_depth + 2;
92d7f7b0 3968
d7c47992
JS
3969 /* If allocated buffers are enough do nothing */
3970 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
3971 return 0;
3972
92d7f7b0
JS
3973 /* Allow some exchanges to be available always to complete discovery */
3974 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
3975 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3976 "0704 At limitation of %d preallocated "
3977 "command buffers\n", total);
dea3101e 3978 return 0;
92d7f7b0
JS
3979 /* Allow some exchanges to be available always to complete discovery */
3980 } else if (total + num_to_alloc >
3981 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
3982 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3983 "0705 Allocation request of %d "
3984 "command buffers will exceed max of %d. "
3985 "Reducing allocation request to %d.\n",
3986 num_to_alloc, phba->cfg_hba_queue_depth,
3987 (phba->cfg_hba_queue_depth - total));
dea3101e 3988 num_to_alloc = phba->cfg_hba_queue_depth - total;
3989 }
3772a991
JS
3990 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3991 if (num_to_alloc != num_allocated) {
3992 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3993 "0708 Allocation request of %d "
3994 "command buffers did not succeed. "
3995 "Allocated %d buffers.\n",
3996 num_to_alloc, num_allocated);
dea3101e 3997 }
1c6f4ef5
JS
3998 if (num_allocated > 0)
3999 phba->total_scsi_bufs += num_allocated;
dea3101e 4000 return 0;
4001}
4002
9bad7671 4003/**
3621a710 4004 * lpfc_slave_configure - scsi_host_template slave_configure entry point
9bad7671
JS
4005 * @sdev: Pointer to scsi_device.
4006 *
4007 * This routine configures following items
4008 * - Tag command queuing support for @sdev if supported.
9bad7671
JS
4009 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
4010 *
4011 * Return codes:
4012 * 0 - Success
4013 **/
dea3101e 4014static int
4015lpfc_slave_configure(struct scsi_device *sdev)
4016{
2e0fef85
JS
4017 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4018 struct lpfc_hba *phba = vport->phba;
dea3101e 4019
4020 if (sdev->tagged_supported)
3de2a653 4021 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 4022 else
3de2a653 4023 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 4024
875fbdfe 4025 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
45ed1190
JS
4026 lpfc_sli_handle_fast_ring_event(phba,
4027 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
875fbdfe
JSEC
4028 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4029 lpfc_poll_rearm_timer(phba);
4030 }
4031
dea3101e 4032 return 0;
4033}
4034
9bad7671 4035/**
3621a710 4036 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
9bad7671
JS
4037 * @sdev: Pointer to scsi_device.
4038 *
4039 * This routine sets @sdev hostatdata filed to null.
4040 **/
dea3101e 4041static void
4042lpfc_slave_destroy(struct scsi_device *sdev)
4043{
d7c47992
JS
4044 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4045 struct lpfc_hba *phba = vport->phba;
4046 atomic_dec(&phba->sdev_cnt);
dea3101e 4047 sdev->hostdata = NULL;
4048 return;
4049}
4050
92d7f7b0 4051
dea3101e 4052struct scsi_host_template lpfc_template = {
4053 .module = THIS_MODULE,
4054 .name = LPFC_DRIVER_NAME,
4055 .info = lpfc_info,
4056 .queuecommand = lpfc_queuecommand,
4057 .eh_abort_handler = lpfc_abort_handler,
bbb9d180
JS
4058 .eh_device_reset_handler = lpfc_device_reset_handler,
4059 .eh_target_reset_handler = lpfc_target_reset_handler,
7054a606 4060 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea3101e 4061 .slave_alloc = lpfc_slave_alloc,
4062 .slave_configure = lpfc_slave_configure,
4063 .slave_destroy = lpfc_slave_destroy,
47a8617c 4064 .scan_finished = lpfc_scan_finished,
dea3101e 4065 .this_id = -1,
83108bd3 4066 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea3101e 4067 .cmd_per_lun = LPFC_CMD_PER_LUN,
4068 .use_clustering = ENABLE_CLUSTERING,
2e0fef85 4069 .shost_attrs = lpfc_hba_attrs,
564b2960 4070 .max_sectors = 0xFFFF,
f1c3b0fc 4071 .vendor_id = LPFC_NL_VENDOR_ID,
5ffc266e 4072 .change_queue_depth = lpfc_change_queue_depth,
dea3101e 4073};
3de2a653
JS
4074
4075struct scsi_host_template lpfc_vport_template = {
4076 .module = THIS_MODULE,
4077 .name = LPFC_DRIVER_NAME,
4078 .info = lpfc_info,
4079 .queuecommand = lpfc_queuecommand,
4080 .eh_abort_handler = lpfc_abort_handler,
bbb9d180
JS
4081 .eh_device_reset_handler = lpfc_device_reset_handler,
4082 .eh_target_reset_handler = lpfc_target_reset_handler,
3de2a653
JS
4083 .eh_bus_reset_handler = lpfc_bus_reset_handler,
4084 .slave_alloc = lpfc_slave_alloc,
4085 .slave_configure = lpfc_slave_configure,
4086 .slave_destroy = lpfc_slave_destroy,
4087 .scan_finished = lpfc_scan_finished,
4088 .this_id = -1,
83108bd3 4089 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3de2a653
JS
4090 .cmd_per_lun = LPFC_CMD_PER_LUN,
4091 .use_clustering = ENABLE_CLUSTERING,
4092 .shost_attrs = lpfc_vport_attrs,
4093 .max_sectors = 0xFFFF,
5ffc266e 4094 .change_queue_depth = lpfc_change_queue_depth,
3de2a653 4095};