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