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