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