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