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