[SCSI] lpfc 8.3.0 : Add support for Power Management Suspend/Resume operations
[linux-2.6-block.git] / drivers / scsi / lpfc / lpfc_scsi.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
e47c9093 4 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea3101e 20 *******************************************************************/
21
dea3101e 22#include <linux/pci.h>
23#include <linux/interrupt.h>
a90f5684 24#include <linux/delay.h>
dea3101e 25
26#include <scsi/scsi.h>
27#include <scsi/scsi_device.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_tcq.h>
30#include <scsi/scsi_transport_fc.h>
31
32#include "lpfc_version.h"
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
ea2151b4 35#include "lpfc_nl.h"
dea3101e 36#include "lpfc_disc.h"
37#include "lpfc_scsi.h"
38#include "lpfc.h"
39#include "lpfc_logmsg.h"
40#include "lpfc_crtn.h"
92d7f7b0 41#include "lpfc_vport.h"
dea3101e 42
43#define LPFC_RESET_WAIT 2
44#define LPFC_ABORT_WAIT 2
45
ea2151b4
JS
46/**
47 * lpfc_update_stats: Update statistical data for the command completion.
48 * @phba: Pointer to HBA object.
49 * @lpfc_cmd: lpfc scsi command object pointer.
50 *
51 * This function is called when there is a command completion and this
52 * function updates the statistical data for the command completion.
53 **/
54static void
55lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
56{
57 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
58 struct lpfc_nodelist *pnode = rdata->pnode;
59 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
60 unsigned long flags;
61 struct Scsi_Host *shost = cmd->device->host;
62 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
63 unsigned long latency;
64 int i;
65
66 if (cmd->result)
67 return;
68
69 spin_lock_irqsave(shost->host_lock, flags);
70 if (!vport->stat_data_enabled ||
71 vport->stat_data_blocked ||
72 !pnode->lat_data ||
73 (phba->bucket_type == LPFC_NO_BUCKET)) {
74 spin_unlock_irqrestore(shost->host_lock, flags);
75 return;
76 }
77 latency = jiffies_to_msecs(jiffies - lpfc_cmd->start_time);
78
79 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
80 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
81 phba->bucket_step;
82 if (i >= LPFC_MAX_BUCKET_COUNT)
83 i = LPFC_MAX_BUCKET_COUNT;
84 } else {
85 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
86 if (latency <= (phba->bucket_base +
87 ((1<<i)*phba->bucket_step)))
88 break;
89 }
90
91 pnode->lat_data[i].cmd_count++;
92 spin_unlock_irqrestore(shost->host_lock, flags);
93}
94
95
96/**
97 * lpfc_send_sdev_queuedepth_change_event: Posts a queuedepth change
98 * event.
99 * @phba: Pointer to HBA context object.
100 * @vport: Pointer to vport object.
101 * @ndlp: Pointer to FC node associated with the target.
102 * @lun: Lun number of the scsi device.
103 * @old_val: Old value of the queue depth.
104 * @new_val: New value of the queue depth.
105 *
106 * This function sends an event to the mgmt application indicating
107 * there is a change in the scsi device queue depth.
108 **/
109static void
110lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
111 struct lpfc_vport *vport,
112 struct lpfc_nodelist *ndlp,
113 uint32_t lun,
114 uint32_t old_val,
115 uint32_t new_val)
116{
117 struct lpfc_fast_path_event *fast_path_evt;
118 unsigned long flags;
119
120 fast_path_evt = lpfc_alloc_fast_evt(phba);
121 if (!fast_path_evt)
122 return;
123
124 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
125 FC_REG_SCSI_EVENT;
126 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
127 LPFC_EVENT_VARQUEDEPTH;
128
129 /* Report all luns with change in queue depth */
130 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
131 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
132 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
133 &ndlp->nlp_portname, sizeof(struct lpfc_name));
134 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
135 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
136 }
137
138 fast_path_evt->un.queue_depth_evt.oldval = old_val;
139 fast_path_evt->un.queue_depth_evt.newval = new_val;
140 fast_path_evt->vport = vport;
141
142 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
143 spin_lock_irqsave(&phba->hbalock, flags);
144 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
145 spin_unlock_irqrestore(&phba->hbalock, flags);
146 lpfc_worker_wake_up(phba);
147
148 return;
149}
150
92d7f7b0
JS
151/*
152 * This function is called with no lock held when there is a resource
153 * error in driver or in firmware.
154 */
155void
156lpfc_adjust_queue_depth(struct lpfc_hba *phba)
157{
158 unsigned long flags;
5e9d9b82 159 uint32_t evt_posted;
92d7f7b0
JS
160
161 spin_lock_irqsave(&phba->hbalock, flags);
162 atomic_inc(&phba->num_rsrc_err);
163 phba->last_rsrc_error_time = jiffies;
164
165 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
166 spin_unlock_irqrestore(&phba->hbalock, flags);
167 return;
168 }
169
170 phba->last_ramp_down_time = jiffies;
171
172 spin_unlock_irqrestore(&phba->hbalock, flags);
173
174 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
5e9d9b82
JS
175 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
176 if (!evt_posted)
92d7f7b0 177 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
92d7f7b0
JS
178 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
179
5e9d9b82
JS
180 if (!evt_posted)
181 lpfc_worker_wake_up(phba);
92d7f7b0
JS
182 return;
183}
184
185/*
186 * This function is called with no lock held when there is a successful
187 * SCSI command completion.
188 */
189static inline void
3de2a653 190lpfc_rampup_queue_depth(struct lpfc_vport *vport,
92d7f7b0
JS
191 struct scsi_device *sdev)
192{
193 unsigned long flags;
3de2a653 194 struct lpfc_hba *phba = vport->phba;
5e9d9b82 195 uint32_t evt_posted;
92d7f7b0
JS
196 atomic_inc(&phba->num_cmd_success);
197
3de2a653 198 if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
92d7f7b0 199 return;
92d7f7b0
JS
200 spin_lock_irqsave(&phba->hbalock, flags);
201 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
202 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
203 spin_unlock_irqrestore(&phba->hbalock, flags);
204 return;
205 }
92d7f7b0
JS
206 phba->last_ramp_up_time = jiffies;
207 spin_unlock_irqrestore(&phba->hbalock, flags);
208
209 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
5e9d9b82
JS
210 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
211 if (!evt_posted)
92d7f7b0 212 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
92d7f7b0
JS
213 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
214
5e9d9b82
JS
215 if (!evt_posted)
216 lpfc_worker_wake_up(phba);
217 return;
92d7f7b0
JS
218}
219
220void
221lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
222{
549e55cd
JS
223 struct lpfc_vport **vports;
224 struct Scsi_Host *shost;
92d7f7b0 225 struct scsi_device *sdev;
ea2151b4 226 unsigned long new_queue_depth, old_queue_depth;
92d7f7b0 227 unsigned long num_rsrc_err, num_cmd_success;
549e55cd 228 int i;
ea2151b4 229 struct lpfc_rport_data *rdata;
92d7f7b0
JS
230
231 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
232 num_cmd_success = atomic_read(&phba->num_cmd_success);
233
549e55cd
JS
234 vports = lpfc_create_vport_work_array(phba);
235 if (vports != NULL)
09372820 236 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
549e55cd
JS
237 shost = lpfc_shost_from_vport(vports[i]);
238 shost_for_each_device(sdev, shost) {
92d7f7b0 239 new_queue_depth =
549e55cd
JS
240 sdev->queue_depth * num_rsrc_err /
241 (num_rsrc_err + num_cmd_success);
242 if (!new_queue_depth)
243 new_queue_depth = sdev->queue_depth - 1;
244 else
245 new_queue_depth = sdev->queue_depth -
246 new_queue_depth;
ea2151b4 247 old_queue_depth = sdev->queue_depth;
549e55cd
JS
248 if (sdev->ordered_tags)
249 scsi_adjust_queue_depth(sdev,
250 MSG_ORDERED_TAG,
251 new_queue_depth);
252 else
253 scsi_adjust_queue_depth(sdev,
254 MSG_SIMPLE_TAG,
255 new_queue_depth);
ea2151b4
JS
256 rdata = sdev->hostdata;
257 if (rdata)
258 lpfc_send_sdev_queuedepth_change_event(
259 phba, vports[i],
260 rdata->pnode,
261 sdev->lun, old_queue_depth,
262 new_queue_depth);
549e55cd 263 }
92d7f7b0 264 }
09372820 265 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0
JS
266 atomic_set(&phba->num_rsrc_err, 0);
267 atomic_set(&phba->num_cmd_success, 0);
268}
269
270void
271lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
272{
549e55cd
JS
273 struct lpfc_vport **vports;
274 struct Scsi_Host *shost;
92d7f7b0 275 struct scsi_device *sdev;
549e55cd 276 int i;
ea2151b4 277 struct lpfc_rport_data *rdata;
549e55cd
JS
278
279 vports = lpfc_create_vport_work_array(phba);
280 if (vports != NULL)
09372820 281 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
549e55cd
JS
282 shost = lpfc_shost_from_vport(vports[i]);
283 shost_for_each_device(sdev, shost) {
97eab634
JS
284 if (vports[i]->cfg_lun_queue_depth <=
285 sdev->queue_depth)
286 continue;
549e55cd
JS
287 if (sdev->ordered_tags)
288 scsi_adjust_queue_depth(sdev,
289 MSG_ORDERED_TAG,
290 sdev->queue_depth+1);
291 else
292 scsi_adjust_queue_depth(sdev,
293 MSG_SIMPLE_TAG,
294 sdev->queue_depth+1);
ea2151b4
JS
295 rdata = sdev->hostdata;
296 if (rdata)
297 lpfc_send_sdev_queuedepth_change_event(
298 phba, vports[i],
299 rdata->pnode,
300 sdev->lun,
301 sdev->queue_depth - 1,
302 sdev->queue_depth);
549e55cd 303 }
92d7f7b0 304 }
09372820 305 lpfc_destroy_vport_work_array(phba, vports);
92d7f7b0
JS
306 atomic_set(&phba->num_rsrc_err, 0);
307 atomic_set(&phba->num_cmd_success, 0);
308}
309
a8e497d5
JS
310/**
311 * lpfc_scsi_dev_block: set all scsi hosts to block state.
312 * @phba: Pointer to HBA context object.
313 *
314 * This function walks vport list and set each SCSI host to block state
315 * by invoking fc_remote_port_delete() routine. This function is invoked
316 * with EEH when device's PCI slot has been permanently disabled.
317 **/
318void
319lpfc_scsi_dev_block(struct lpfc_hba *phba)
320{
321 struct lpfc_vport **vports;
322 struct Scsi_Host *shost;
323 struct scsi_device *sdev;
324 struct fc_rport *rport;
325 int i;
326
327 vports = lpfc_create_vport_work_array(phba);
328 if (vports != NULL)
329 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
330 shost = lpfc_shost_from_vport(vports[i]);
331 shost_for_each_device(sdev, shost) {
332 rport = starget_to_rport(scsi_target(sdev));
333 fc_remote_port_delete(rport);
334 }
335 }
336 lpfc_destroy_vport_work_array(phba, vports);
337}
338
dea3101e 339/*
340 * This routine allocates a scsi buffer, which contains all the necessary
341 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
342 * contains information to build the IOCB. The DMAable region contains
343 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
344 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
345 * and the BPL BDE is setup in the IOCB.
346 */
347static struct lpfc_scsi_buf *
2e0fef85 348lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea3101e 349{
2e0fef85 350 struct lpfc_hba *phba = vport->phba;
dea3101e 351 struct lpfc_scsi_buf *psb;
352 struct ulp_bde64 *bpl;
353 IOCB_t *iocb;
34b02dcd
JS
354 dma_addr_t pdma_phys_fcp_cmd;
355 dma_addr_t pdma_phys_fcp_rsp;
356 dma_addr_t pdma_phys_bpl;
604a3e30 357 uint16_t iotag;
dea3101e 358
bbfbbbc1 359 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
dea3101e 360 if (!psb)
361 return NULL;
dea3101e 362
363 /*
364 * Get memory from the pci pool to map the virt space to pci bus space
365 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
366 * struct fcp_rsp and the number of bde's necessary to support the
367 * sg_tablesize.
368 */
369 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
370 &psb->dma_handle);
371 if (!psb->data) {
372 kfree(psb);
373 return NULL;
374 }
375
376 /* Initialize virtual ptrs to dma_buf region. */
377 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
378
604a3e30
JB
379 /* Allocate iotag for psb->cur_iocbq. */
380 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
381 if (iotag == 0) {
382 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
383 psb->data, psb->dma_handle);
384 kfree (psb);
385 return NULL;
386 }
0bd4ca25 387 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
604a3e30 388
dea3101e 389 psb->fcp_cmnd = psb->data;
390 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
391 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
392 sizeof(struct fcp_rsp);
393
394 /* Initialize local short-hand pointers. */
395 bpl = psb->fcp_bpl;
34b02dcd
JS
396 pdma_phys_fcp_cmd = psb->dma_handle;
397 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
398 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
399 sizeof(struct fcp_rsp);
dea3101e 400
401 /*
402 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
403 * list bdes. Initialize the first two and leave the rest for
404 * queuecommand.
405 */
34b02dcd
JS
406 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
407 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
408 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
409 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
410 bpl[0].tus.w = le32_to_cpu(bpl->tus.w);
dea3101e 411
412 /* Setup the physical region for the FCP RSP */
34b02dcd
JS
413 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
414 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
415 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
416 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
417 bpl[1].tus.w = le32_to_cpu(bpl->tus.w);
dea3101e 418
419 /*
420 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
421 * initialize it with all known data now.
422 */
dea3101e 423 iocb = &psb->cur_iocbq.iocb;
424 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
34b02dcd
JS
425 if (phba->sli_rev == 3) {
426 /* fill in immediate fcp command BDE */
427 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
428 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
429 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
430 unsli3.fcp_ext.icd);
431 iocb->un.fcpi64.bdl.addrHigh = 0;
432 iocb->ulpBdeCount = 0;
433 iocb->ulpLe = 0;
434 /* fill in responce BDE */
435 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
436 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
437 sizeof(struct fcp_rsp);
438 iocb->unsli3.fcp_ext.rbde.addrLow =
439 putPaddrLow(pdma_phys_fcp_rsp);
440 iocb->unsli3.fcp_ext.rbde.addrHigh =
441 putPaddrHigh(pdma_phys_fcp_rsp);
442 } else {
443 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
444 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
445 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_bpl);
446 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_bpl);
447 iocb->ulpBdeCount = 1;
448 iocb->ulpLe = 1;
449 }
dea3101e 450 iocb->ulpClass = CLASS3;
451
452 return psb;
453}
454
455c53ec 455static struct lpfc_scsi_buf*
875fbdfe 456lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea3101e 457{
0bd4ca25
JSEC
458 struct lpfc_scsi_buf * lpfc_cmd = NULL;
459 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
875fbdfe 460 unsigned long iflag = 0;
0bd4ca25 461
875fbdfe 462 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 463 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1dcb58e5
JS
464 if (lpfc_cmd) {
465 lpfc_cmd->seg_cnt = 0;
466 lpfc_cmd->nonsg_phys = 0;
467 }
875fbdfe 468 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
0bd4ca25
JSEC
469 return lpfc_cmd;
470}
dea3101e 471
0bd4ca25 472static void
92d7f7b0 473lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
0bd4ca25 474{
875fbdfe 475 unsigned long iflag = 0;
dea3101e 476
875fbdfe 477 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 478 psb->pCmd = NULL;
dea3101e 479 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
875fbdfe 480 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea3101e 481}
482
483static int
92d7f7b0 484lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea3101e 485{
486 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
487 struct scatterlist *sgel = NULL;
488 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
489 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
490 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
34b02dcd 491 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea3101e 492 dma_addr_t physaddr;
34b02dcd 493 uint32_t num_bde = 0;
a0b4f78f 494 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea3101e 495
496 /*
497 * There are three possibilities here - use scatter-gather segment, use
498 * the single mapping, or neither. Start the lpfc command prep by
499 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
500 * data bde entry.
501 */
502 bpl += 2;
c59fd9eb 503 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 504 /*
505 * The driver stores the segment count returned from pci_map_sg
506 * because this a count of dma-mappings used to map the use_sg
507 * pages. They are not guaranteed to be the same for those
508 * architectures that implement an IOMMU.
509 */
dea3101e 510
c59fd9eb
FT
511 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
512 scsi_sg_count(scsi_cmnd), datadir);
513 if (unlikely(!nseg))
514 return 1;
515
a0b4f78f 516 lpfc_cmd->seg_cnt = nseg;
dea3101e 517 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
518 printk(KERN_ERR "%s: Too many sg segments from "
519 "dma_map_sg. Config %d, seg_cnt %d",
cadbd4a5 520 __func__, phba->cfg_sg_seg_cnt,
dea3101e 521 lpfc_cmd->seg_cnt);
a0b4f78f 522 scsi_dma_unmap(scsi_cmnd);
dea3101e 523 return 1;
524 }
525
526 /*
527 * The driver established a maximum scatter-gather segment count
528 * during probe that limits the number of sg elements in any
529 * single scsi command. Just run through the seg_cnt and format
530 * the bde's.
34b02dcd
JS
531 * When using SLI-3 the driver will try to fit all the BDEs into
532 * the IOCB. If it can't then the BDEs get added to a BPL as it
533 * does for SLI-2 mode.
dea3101e 534 */
34b02dcd 535 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea3101e 536 physaddr = sg_dma_address(sgel);
34b02dcd
JS
537 if (phba->sli_rev == 3 &&
538 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
539 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
540 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
541 data_bde->addrLow = putPaddrLow(physaddr);
542 data_bde->addrHigh = putPaddrHigh(physaddr);
543 data_bde++;
544 } else {
545 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
546 bpl->tus.f.bdeSize = sg_dma_len(sgel);
547 bpl->tus.w = le32_to_cpu(bpl->tus.w);
548 bpl->addrLow =
549 le32_to_cpu(putPaddrLow(physaddr));
550 bpl->addrHigh =
551 le32_to_cpu(putPaddrHigh(physaddr));
552 bpl++;
553 }
dea3101e 554 }
c59fd9eb 555 }
dea3101e 556
557 /*
558 * Finish initializing those IOCB fields that are dependent on the
34b02dcd
JS
559 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
560 * explicitly reinitialized and for SLI-3 the extended bde count is
561 * explicitly reinitialized since all iocb memory resources are reused.
dea3101e 562 */
34b02dcd
JS
563 if (phba->sli_rev == 3) {
564 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
565 /*
566 * The extended IOCB format can only fit 3 BDE or a BPL.
567 * This I/O has more than 3 BDE so the 1st data bde will
568 * be a BPL that is filled in here.
569 */
570 physaddr = lpfc_cmd->dma_handle;
571 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
572 data_bde->tus.f.bdeSize = (num_bde *
573 sizeof(struct ulp_bde64));
574 physaddr += (sizeof(struct fcp_cmnd) +
575 sizeof(struct fcp_rsp) +
576 (2 * sizeof(struct ulp_bde64)));
577 data_bde->addrHigh = putPaddrHigh(physaddr);
578 data_bde->addrLow = putPaddrLow(physaddr);
579 /* ebde count includes the responce bde and data bpl */
580 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
581 } else {
582 /* ebde count includes the responce bde and data bdes */
583 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
584 }
585 } else {
586 iocb_cmd->un.fcpi64.bdl.bdeSize =
587 ((num_bde + 2) * sizeof(struct ulp_bde64));
588 }
09372820 589 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
dea3101e 590 return 0;
591}
592
ea2151b4
JS
593/**
594 * lpfc_send_scsi_error_event: Posts an event when there is SCSI error.
595 * @phba: Pointer to hba context object.
596 * @vport: Pointer to vport object.
597 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
598 * @rsp_iocb: Pointer to response iocb object which reported error.
599 *
600 * This function posts an event when there is a SCSI command reporting
601 * error from the scsi device.
602 **/
603static void
604lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
605 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
606 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
607 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
608 uint32_t resp_info = fcprsp->rspStatus2;
609 uint32_t scsi_status = fcprsp->rspStatus3;
610 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
611 struct lpfc_fast_path_event *fast_path_evt = NULL;
612 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
613 unsigned long flags;
614
615 /* If there is queuefull or busy condition send a scsi event */
616 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
617 (cmnd->result == SAM_STAT_BUSY)) {
618 fast_path_evt = lpfc_alloc_fast_evt(phba);
619 if (!fast_path_evt)
620 return;
621 fast_path_evt->un.scsi_evt.event_type =
622 FC_REG_SCSI_EVENT;
623 fast_path_evt->un.scsi_evt.subcategory =
624 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
625 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
626 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
627 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
628 &pnode->nlp_portname, sizeof(struct lpfc_name));
629 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
630 &pnode->nlp_nodename, sizeof(struct lpfc_name));
631 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
632 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
633 fast_path_evt = lpfc_alloc_fast_evt(phba);
634 if (!fast_path_evt)
635 return;
636 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
637 FC_REG_SCSI_EVENT;
638 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
639 LPFC_EVENT_CHECK_COND;
640 fast_path_evt->un.check_cond_evt.scsi_event.lun =
641 cmnd->device->lun;
642 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
643 &pnode->nlp_portname, sizeof(struct lpfc_name));
644 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
645 &pnode->nlp_nodename, sizeof(struct lpfc_name));
646 fast_path_evt->un.check_cond_evt.sense_key =
647 cmnd->sense_buffer[2] & 0xf;
648 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
649 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
650 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
651 fcpi_parm &&
652 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
653 ((scsi_status == SAM_STAT_GOOD) &&
654 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
655 /*
656 * If status is good or resid does not match with fcp_param and
657 * there is valid fcpi_parm, then there is a read_check error
658 */
659 fast_path_evt = lpfc_alloc_fast_evt(phba);
660 if (!fast_path_evt)
661 return;
662 fast_path_evt->un.read_check_error.header.event_type =
663 FC_REG_FABRIC_EVENT;
664 fast_path_evt->un.read_check_error.header.subcategory =
665 LPFC_EVENT_FCPRDCHKERR;
666 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
667 &pnode->nlp_portname, sizeof(struct lpfc_name));
668 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
669 &pnode->nlp_nodename, sizeof(struct lpfc_name));
670 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
671 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
672 fast_path_evt->un.read_check_error.fcpiparam =
673 fcpi_parm;
674 } else
675 return;
676
677 fast_path_evt->vport = vport;
678 spin_lock_irqsave(&phba->hbalock, flags);
679 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
680 spin_unlock_irqrestore(&phba->hbalock, flags);
681 lpfc_worker_wake_up(phba);
682 return;
683}
bcf4dbfa
JS
684static void
685lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
686{
687 /*
688 * There are only two special cases to consider. (1) the scsi command
689 * requested scatter-gather usage or (2) the scsi command allocated
690 * a request buffer, but did not request use_sg. There is a third
691 * case, but it does not require resource deallocation.
692 */
a0b4f78f
FT
693 if (psb->seg_cnt > 0)
694 scsi_dma_unmap(psb->pCmd);
bcf4dbfa
JS
695}
696
dea3101e 697static void
2e0fef85
JS
698lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
699 struct lpfc_iocbq *rsp_iocb)
dea3101e 700{
701 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
702 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
703 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
7054a606 704 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea3101e 705 uint32_t resp_info = fcprsp->rspStatus2;
706 uint32_t scsi_status = fcprsp->rspStatus3;
c7743956 707 uint32_t *lp;
dea3101e 708 uint32_t host_status = DID_OK;
709 uint32_t rsplen = 0;
c7743956 710 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea3101e 711
ea2151b4 712
dea3101e 713 /*
714 * If this is a task management command, there is no
715 * scsi packet associated with this lpfc_cmd. The driver
716 * consumes it.
717 */
718 if (fcpcmd->fcpCntl2) {
719 scsi_status = 0;
720 goto out;
721 }
722
c7743956
JS
723 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
724 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
725 if (snslen > SCSI_SENSE_BUFFERSIZE)
726 snslen = SCSI_SENSE_BUFFERSIZE;
727
728 if (resp_info & RSP_LEN_VALID)
729 rsplen = be32_to_cpu(fcprsp->rspRspLen);
730 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
731 }
732 lp = (uint32_t *)cmnd->sense_buffer;
733
734 if (!scsi_status && (resp_info & RESID_UNDER))
735 logit = LOG_FCP;
736
e8b62011
JS
737 lpfc_printf_vlog(vport, KERN_WARNING, logit,
738 "0730 FCP command x%x failed: x%x SNS x%x x%x "
739 "Data: x%x x%x x%x x%x x%x\n",
740 cmnd->cmnd[0], scsi_status,
741 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
742 be32_to_cpu(fcprsp->rspResId),
743 be32_to_cpu(fcprsp->rspSnsLen),
744 be32_to_cpu(fcprsp->rspRspLen),
745 fcprsp->rspInfo3);
dea3101e 746
747 if (resp_info & RSP_LEN_VALID) {
748 rsplen = be32_to_cpu(fcprsp->rspRspLen);
749 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
750 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
751 host_status = DID_ERROR;
752 goto out;
753 }
754 }
755
a0b4f78f 756 scsi_set_resid(cmnd, 0);
dea3101e 757 if (resp_info & RESID_UNDER) {
a0b4f78f 758 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea3101e 759
e8b62011
JS
760 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
761 "0716 FCP Read Underrun, expected %d, "
762 "residual %d Data: x%x x%x x%x\n",
763 be32_to_cpu(fcpcmd->fcpDl),
764 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
765 cmnd->underflow);
dea3101e 766
7054a606
JS
767 /*
768 * If there is an under run check if under run reported by
769 * storage array is same as the under run reported by HBA.
770 * If this is not same, there is a dropped frame.
771 */
772 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
773 fcpi_parm &&
a0b4f78f 774 (scsi_get_resid(cmnd) != fcpi_parm)) {
e8b62011
JS
775 lpfc_printf_vlog(vport, KERN_WARNING,
776 LOG_FCP | LOG_FCP_ERROR,
777 "0735 FCP Read Check Error "
778 "and Underrun Data: x%x x%x x%x x%x\n",
779 be32_to_cpu(fcpcmd->fcpDl),
780 scsi_get_resid(cmnd), fcpi_parm,
781 cmnd->cmnd[0]);
a0b4f78f 782 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
7054a606
JS
783 host_status = DID_ERROR;
784 }
dea3101e 785 /*
786 * The cmnd->underflow is the minimum number of bytes that must
787 * be transfered for this command. Provided a sense condition
788 * is not present, make sure the actual amount transferred is at
789 * least the underflow value or fail.
790 */
791 if (!(resp_info & SNS_LEN_VALID) &&
792 (scsi_status == SAM_STAT_GOOD) &&
a0b4f78f
FT
793 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
794 < cmnd->underflow)) {
e8b62011
JS
795 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
796 "0717 FCP command x%x residual "
797 "underrun converted to error "
798 "Data: x%x x%x x%x\n",
66dbfbe6 799 cmnd->cmnd[0], scsi_bufflen(cmnd),
e8b62011 800 scsi_get_resid(cmnd), cmnd->underflow);
dea3101e 801 host_status = DID_ERROR;
802 }
803 } else if (resp_info & RESID_OVER) {
e8b62011
JS
804 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
805 "0720 FCP command x%x residual overrun error. "
806 "Data: x%x x%x \n", cmnd->cmnd[0],
807 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea3101e 808 host_status = DID_ERROR;
809
810 /*
811 * Check SLI validation that all the transfer was actually done
812 * (fcpi_parm should be zero). Apply check only to reads.
813 */
814 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
815 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
e8b62011
JS
816 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
817 "0734 FCP Read Check Error Data: "
818 "x%x x%x x%x x%x\n",
819 be32_to_cpu(fcpcmd->fcpDl),
820 be32_to_cpu(fcprsp->rspResId),
821 fcpi_parm, cmnd->cmnd[0]);
dea3101e 822 host_status = DID_ERROR;
a0b4f78f 823 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea3101e 824 }
825
826 out:
827 cmnd->result = ScsiResult(host_status, scsi_status);
ea2151b4 828 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea3101e 829}
830
831static void
832lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
833 struct lpfc_iocbq *pIocbOut)
834{
835 struct lpfc_scsi_buf *lpfc_cmd =
836 (struct lpfc_scsi_buf *) pIocbIn->context1;
2e0fef85 837 struct lpfc_vport *vport = pIocbIn->vport;
dea3101e 838 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
839 struct lpfc_nodelist *pnode = rdata->pnode;
840 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
445cf4f4
JSEC
841 int result;
842 struct scsi_device *sdev, *tmp_sdev;
843 int depth = 0;
fa61a54e 844 unsigned long flags;
ea2151b4 845 struct lpfc_fast_path_event *fast_path_evt;
dea3101e 846
847 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
848 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
977b5a0a 849 atomic_dec(&pnode->cmd_pending);
dea3101e 850
851 if (lpfc_cmd->status) {
852 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
853 (lpfc_cmd->result & IOERR_DRVR_MASK))
854 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
855 else if (lpfc_cmd->status >= IOSTAT_CNT)
856 lpfc_cmd->status = IOSTAT_DEFAULT;
857
e8b62011
JS
858 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
859 "0729 FCP cmd x%x failed <%d/%d> "
860 "status: x%x result: x%x Data: x%x x%x\n",
861 cmd->cmnd[0],
862 cmd->device ? cmd->device->id : 0xffff,
863 cmd->device ? cmd->device->lun : 0xffff,
864 lpfc_cmd->status, lpfc_cmd->result,
865 pIocbOut->iocb.ulpContext,
866 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea3101e 867
868 switch (lpfc_cmd->status) {
869 case IOSTAT_FCP_RSP_ERROR:
870 /* Call FCP RSP handler to determine result */
2e0fef85 871 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea3101e 872 break;
873 case IOSTAT_NPORT_BSY:
874 case IOSTAT_FABRIC_BSY:
0f1f53a7 875 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
ea2151b4
JS
876 fast_path_evt = lpfc_alloc_fast_evt(phba);
877 if (!fast_path_evt)
878 break;
879 fast_path_evt->un.fabric_evt.event_type =
880 FC_REG_FABRIC_EVENT;
881 fast_path_evt->un.fabric_evt.subcategory =
882 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
883 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
884 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
885 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
886 &pnode->nlp_portname,
887 sizeof(struct lpfc_name));
888 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
889 &pnode->nlp_nodename,
890 sizeof(struct lpfc_name));
891 }
892 fast_path_evt->vport = vport;
893 fast_path_evt->work_evt.evt =
894 LPFC_EVT_FASTPATH_MGMT_EVT;
895 spin_lock_irqsave(&phba->hbalock, flags);
896 list_add_tail(&fast_path_evt->work_evt.evt_listp,
897 &phba->work_list);
898 spin_unlock_irqrestore(&phba->hbalock, flags);
899 lpfc_worker_wake_up(phba);
dea3101e 900 break;
92d7f7b0 901 case IOSTAT_LOCAL_REJECT:
d7c255b2 902 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
92d7f7b0 903 lpfc_cmd->result == IOERR_NO_RESOURCES ||
d7c255b2 904 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
92d7f7b0 905 cmd->result = ScsiResult(DID_REQUEUE, 0);
58da1ffb
JS
906 break;
907 } /* else: fall through */
dea3101e 908 default:
909 cmd->result = ScsiResult(DID_ERROR, 0);
910 break;
911 }
912
58da1ffb 913 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
19a7b4ae 914 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
0f1f53a7
JS
915 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
916 SAM_STAT_BUSY);
dea3101e 917 } else {
918 cmd->result = ScsiResult(DID_OK, 0);
919 }
920
921 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
922 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
923
e8b62011
JS
924 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
925 "0710 Iodone <%d/%d> cmd %p, error "
926 "x%x SNS x%x x%x Data: x%x x%x\n",
927 cmd->device->id, cmd->device->lun, cmd,
928 cmd->result, *lp, *(lp + 3), cmd->retries,
929 scsi_get_resid(cmd));
dea3101e 930 }
931
ea2151b4 932 lpfc_update_stats(phba, lpfc_cmd);
445cf4f4
JSEC
933 result = cmd->result;
934 sdev = cmd->device;
977b5a0a
JS
935 if (vport->cfg_max_scsicmpl_time &&
936 time_after(jiffies, lpfc_cmd->start_time +
937 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
938 spin_lock_irqsave(sdev->host->host_lock, flags);
939 if ((pnode->cmd_qdepth > atomic_read(&pnode->cmd_pending) &&
940 (atomic_read(&pnode->cmd_pending) > LPFC_MIN_TGT_QDEPTH) &&
941 ((cmd->cmnd[0] == READ_10) || (cmd->cmnd[0] == WRITE_10))))
942 pnode->cmd_qdepth = atomic_read(&pnode->cmd_pending);
943
944 pnode->last_change_time = jiffies;
945 spin_unlock_irqrestore(sdev->host->host_lock, flags);
946 } else if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
947 time_after(jiffies, pnode->last_change_time +
948 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
949 spin_lock_irqsave(sdev->host->host_lock, flags);
950 pnode->cmd_qdepth += pnode->cmd_qdepth *
951 LPFC_TGTQ_RAMPUP_PCENT / 100;
952 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
953 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
954 pnode->last_change_time = jiffies;
955 spin_unlock_irqrestore(sdev->host->host_lock, flags);
956 }
957
1dcb58e5 958 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
0bd4ca25
JSEC
959 cmd->scsi_done(cmd);
960
b808608b 961 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
fa61a54e
JS
962 /*
963 * If there is a thread waiting for command completion
964 * wake up the thread.
965 */
966 spin_lock_irqsave(sdev->host->host_lock, flags);
495a714c 967 lpfc_cmd->pCmd = NULL;
fa61a54e
JS
968 if (lpfc_cmd->waitq)
969 wake_up(lpfc_cmd->waitq);
970 spin_unlock_irqrestore(sdev->host->host_lock, flags);
b808608b
JW
971 lpfc_release_scsi_buf(phba, lpfc_cmd);
972 return;
973 }
974
92d7f7b0
JS
975
976 if (!result)
3de2a653 977 lpfc_rampup_queue_depth(vport, sdev);
92d7f7b0 978
58da1ffb 979 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
445cf4f4
JSEC
980 ((jiffies - pnode->last_ramp_up_time) >
981 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
982 ((jiffies - pnode->last_q_full_time) >
983 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
3de2a653 984 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
445cf4f4 985 shost_for_each_device(tmp_sdev, sdev->host) {
3de2a653 986 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
445cf4f4
JSEC
987 if (tmp_sdev->id != sdev->id)
988 continue;
989 if (tmp_sdev->ordered_tags)
990 scsi_adjust_queue_depth(tmp_sdev,
991 MSG_ORDERED_TAG,
992 tmp_sdev->queue_depth+1);
993 else
994 scsi_adjust_queue_depth(tmp_sdev,
995 MSG_SIMPLE_TAG,
996 tmp_sdev->queue_depth+1);
997
998 pnode->last_ramp_up_time = jiffies;
999 }
1000 }
ea2151b4
JS
1001 lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
1002 0xFFFFFFFF,
1003 sdev->queue_depth - 1, sdev->queue_depth);
445cf4f4
JSEC
1004 }
1005
1006 /*
1007 * Check for queue full. If the lun is reporting queue full, then
1008 * back off the lun queue depth to prevent target overloads.
1009 */
58da1ffb
JS
1010 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
1011 NLP_CHK_NODE_ACT(pnode)) {
445cf4f4
JSEC
1012 pnode->last_q_full_time = jiffies;
1013
1014 shost_for_each_device(tmp_sdev, sdev->host) {
1015 if (tmp_sdev->id != sdev->id)
1016 continue;
1017 depth = scsi_track_queue_full(tmp_sdev,
1018 tmp_sdev->queue_depth - 1);
1019 }
1020 /*
2e0fef85 1021 * The queue depth cannot be lowered any more.
445cf4f4
JSEC
1022 * Modify the returned error code to store
1023 * the final depth value set by
1024 * scsi_track_queue_full.
1025 */
1026 if (depth == -1)
1027 depth = sdev->host->cmd_per_lun;
1028
1029 if (depth) {
e8b62011
JS
1030 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1031 "0711 detected queue full - lun queue "
1032 "depth adjusted to %d.\n", depth);
ea2151b4
JS
1033 lpfc_send_sdev_queuedepth_change_event(phba, vport,
1034 pnode, 0xFFFFFFFF,
1035 depth+1, depth);
445cf4f4
JSEC
1036 }
1037 }
1038
fa61a54e
JS
1039 /*
1040 * If there is a thread waiting for command completion
1041 * wake up the thread.
1042 */
1043 spin_lock_irqsave(sdev->host->host_lock, flags);
495a714c 1044 lpfc_cmd->pCmd = NULL;
fa61a54e
JS
1045 if (lpfc_cmd->waitq)
1046 wake_up(lpfc_cmd->waitq);
1047 spin_unlock_irqrestore(sdev->host->host_lock, flags);
1048
0bd4ca25 1049 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 1050}
1051
34b02dcd
JS
1052/**
1053 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB.
1054 * @data: A pointer to the immediate command data portion of the IOCB.
1055 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
1056 *
1057 * The routine copies the entire FCP command from @fcp_cmnd to @data while
1058 * byte swapping the data to big endian format for transmission on the wire.
1059 **/
1060static void
1061lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
1062{
1063 int i, j;
1064 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
1065 i += sizeof(uint32_t), j++) {
1066 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
1067 }
1068}
1069
dea3101e 1070static void
2e0fef85
JS
1071lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
1072 struct lpfc_nodelist *pnode)
dea3101e 1073{
2e0fef85 1074 struct lpfc_hba *phba = vport->phba;
dea3101e 1075 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1076 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1077 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1078 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
1079 int datadir = scsi_cmnd->sc_data_direction;
7e2b19fb 1080 char tag[2];
dea3101e 1081
58da1ffb
JS
1082 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
1083 return;
1084
dea3101e 1085 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
69859dc4
JSEC
1086 /* clear task management bits */
1087 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea3101e 1088
91886523
JSEC
1089 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
1090 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea3101e 1091
1092 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
1093
7e2b19fb
JS
1094 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
1095 switch (tag[0]) {
dea3101e 1096 case HEAD_OF_QUEUE_TAG:
1097 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
1098 break;
1099 case ORDERED_QUEUE_TAG:
1100 fcp_cmnd->fcpCntl1 = ORDERED_Q;
1101 break;
1102 default:
1103 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
1104 break;
1105 }
1106 } else
1107 fcp_cmnd->fcpCntl1 = 0;
1108
1109 /*
1110 * There are three possibilities here - use scatter-gather segment, use
1111 * the single mapping, or neither. Start the lpfc command prep by
1112 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1113 * data bde entry.
1114 */
a0b4f78f 1115 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 1116 if (datadir == DMA_TO_DEVICE) {
1117 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
1118 iocb_cmd->un.fcpi.fcpi_parm = 0;
1119 iocb_cmd->ulpPU = 0;
1120 fcp_cmnd->fcpCntl3 = WRITE_DATA;
1121 phba->fc4OutputRequests++;
1122 } else {
1123 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
1124 iocb_cmd->ulpPU = PARM_READ_CHECK;
a0b4f78f 1125 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea3101e 1126 fcp_cmnd->fcpCntl3 = READ_DATA;
1127 phba->fc4InputRequests++;
1128 }
1129 } else {
1130 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
1131 iocb_cmd->un.fcpi.fcpi_parm = 0;
1132 iocb_cmd->ulpPU = 0;
1133 fcp_cmnd->fcpCntl3 = 0;
1134 phba->fc4ControlRequests++;
1135 }
34b02dcd
JS
1136 if (phba->sli_rev == 3)
1137 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 1138 /*
1139 * Finish initializing those IOCB fields that are independent
1140 * of the scsi_cmnd request_buffer
1141 */
1142 piocbq->iocb.ulpContext = pnode->nlp_rpi;
1143 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
1144 piocbq->iocb.ulpFCP2Rcvy = 1;
09372820
JS
1145 else
1146 piocbq->iocb.ulpFCP2Rcvy = 0;
dea3101e 1147
1148 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
1149 piocbq->context1 = lpfc_cmd;
1150 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
1151 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2e0fef85 1152 piocbq->vport = vport;
dea3101e 1153}
1154
1155static int
2e0fef85 1156lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea3101e 1157 struct lpfc_scsi_buf *lpfc_cmd,
420b630d 1158 unsigned int lun,
dea3101e 1159 uint8_t task_mgmt_cmd)
1160{
dea3101e 1161 struct lpfc_iocbq *piocbq;
1162 IOCB_t *piocb;
1163 struct fcp_cmnd *fcp_cmnd;
0b18ac42 1164 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea3101e 1165 struct lpfc_nodelist *ndlp = rdata->pnode;
1166
58da1ffb
JS
1167 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1168 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea3101e 1169 return 0;
dea3101e 1170
dea3101e 1171 piocbq = &(lpfc_cmd->cur_iocbq);
2e0fef85
JS
1172 piocbq->vport = vport;
1173
dea3101e 1174 piocb = &piocbq->iocb;
1175
1176 fcp_cmnd = lpfc_cmd->fcp_cmnd;
34b02dcd
JS
1177 /* Clear out any old data in the FCP command area */
1178 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1179 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea3101e 1180 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
34b02dcd
JS
1181 if (vport->phba->sli_rev == 3)
1182 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea3101e 1183 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea3101e 1184 piocb->ulpContext = ndlp->nlp_rpi;
1185 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
1186 piocb->ulpFCP2Rcvy = 1;
1187 }
1188 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
1189
1190 /* ulpTimeout is only one byte */
1191 if (lpfc_cmd->timeout > 0xff) {
1192 /*
1193 * Do not timeout the command at the firmware level.
1194 * The driver will provide the timeout mechanism.
1195 */
1196 piocb->ulpTimeout = 0;
1197 } else {
1198 piocb->ulpTimeout = lpfc_cmd->timeout;
1199 }
1200
2e0fef85 1201 return 1;
dea3101e 1202}
1203
7054a606
JS
1204static void
1205lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
1206 struct lpfc_iocbq *cmdiocbq,
1207 struct lpfc_iocbq *rspiocbq)
1208{
1209 struct lpfc_scsi_buf *lpfc_cmd =
1210 (struct lpfc_scsi_buf *) cmdiocbq->context1;
1211 if (lpfc_cmd)
1212 lpfc_release_scsi_buf(phba, lpfc_cmd);
1213 return;
1214}
1215
dea3101e 1216static int
2e0fef85 1217lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
420b630d
JS
1218 unsigned tgt_id, unsigned int lun,
1219 struct lpfc_rport_data *rdata)
dea3101e 1220{
2e0fef85 1221 struct lpfc_hba *phba = vport->phba;
dea3101e 1222 struct lpfc_iocbq *iocbq;
0bd4ca25 1223 struct lpfc_iocbq *iocbqrsp;
dea3101e 1224 int ret;
915caaaf 1225 int status;
dea3101e 1226
58da1ffb 1227 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
f5603511
JS
1228 return FAILED;
1229
0b18ac42 1230 lpfc_cmd->rdata = rdata;
915caaaf 1231 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
420b630d 1232 FCP_TARGET_RESET);
915caaaf 1233 if (!status)
dea3101e 1234 return FAILED;
1235
dea3101e 1236 iocbq = &lpfc_cmd->cur_iocbq;
0bd4ca25
JSEC
1237 iocbqrsp = lpfc_sli_get_iocbq(phba);
1238
dea3101e 1239 if (!iocbqrsp)
1240 return FAILED;
dea3101e 1241
0b18ac42 1242 /* Issue Target Reset to TGT <num> */
e8b62011
JS
1243 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1244 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
1245 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
915caaaf 1246 status = lpfc_sli_issue_iocb_wait(phba,
68876920
JSEC
1247 &phba->sli.ring[phba->sli.fcp_ring],
1248 iocbq, iocbqrsp, lpfc_cmd->timeout);
915caaaf
JS
1249 if (status != IOCB_SUCCESS) {
1250 if (status == IOCB_TIMEDOUT) {
7054a606 1251 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
915caaaf
JS
1252 ret = TIMEOUT_ERROR;
1253 } else
1254 ret = FAILED;
dea3101e 1255 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea3101e 1256 } else {
1257 ret = SUCCESS;
1258 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
1259 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
1260 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
1261 (lpfc_cmd->result & IOERR_DRVR_MASK))
1262 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
1263 }
1264
604a3e30 1265 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea3101e 1266 return ret;
1267}
1268
dea3101e 1269const char *
1270lpfc_info(struct Scsi_Host *host)
1271{
2e0fef85
JS
1272 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
1273 struct lpfc_hba *phba = vport->phba;
dea3101e 1274 int len;
1275 static char lpfcinfobuf[384];
1276
1277 memset(lpfcinfobuf,0,384);
1278 if (phba && phba->pcidev){
1279 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
1280 len = strlen(lpfcinfobuf);
1281 snprintf(lpfcinfobuf + len,
1282 384-len,
1283 " on PCI bus %02x device %02x irq %d",
1284 phba->pcidev->bus->number,
1285 phba->pcidev->devfn,
1286 phba->pcidev->irq);
1287 len = strlen(lpfcinfobuf);
1288 if (phba->Port[0]) {
1289 snprintf(lpfcinfobuf + len,
1290 384-len,
1291 " port %s",
1292 phba->Port);
1293 }
1294 }
1295 return lpfcinfobuf;
1296}
1297
875fbdfe
JSEC
1298static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
1299{
1300 unsigned long poll_tmo_expires =
1301 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
1302
1303 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
1304 mod_timer(&phba->fcp_poll_timer,
1305 poll_tmo_expires);
1306}
1307
1308void lpfc_poll_start_timer(struct lpfc_hba * phba)
1309{
1310 lpfc_poll_rearm_timer(phba);
1311}
1312
1313void lpfc_poll_timeout(unsigned long ptr)
1314{
2e0fef85 1315 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
875fbdfe
JSEC
1316
1317 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1318 lpfc_sli_poll_fcp_ring (phba);
1319 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1320 lpfc_poll_rearm_timer(phba);
1321 }
875fbdfe
JSEC
1322}
1323
dea3101e 1324static int
1325lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1326{
2e0fef85
JS
1327 struct Scsi_Host *shost = cmnd->device->host;
1328 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1329 struct lpfc_hba *phba = vport->phba;
1330 struct lpfc_sli *psli = &phba->sli;
dea3101e 1331 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1332 struct lpfc_nodelist *ndlp = rdata->pnode;
0bd4ca25 1333 struct lpfc_scsi_buf *lpfc_cmd;
19a7b4ae 1334 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
19a7b4ae 1335 int err;
dea3101e 1336
19a7b4ae
JSEC
1337 err = fc_remote_port_chkready(rport);
1338 if (err) {
1339 cmnd->result = err;
dea3101e 1340 goto out_fail_command;
1341 }
1342
1343 /*
19a7b4ae
JSEC
1344 * Catch race where our node has transitioned, but the
1345 * transport is still transitioning.
dea3101e 1346 */
b522d7d4
JS
1347 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1348 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
1349 goto out_fail_command;
1350 }
977b5a0a
JS
1351 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
1352 goto out_host_busy;
a93ce024 1353
ed957684 1354 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea3101e 1355 if (lpfc_cmd == NULL) {
92d7f7b0
JS
1356 lpfc_adjust_queue_depth(phba);
1357
e8b62011
JS
1358 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1359 "0707 driver's buffer pool is empty, "
1360 "IO busied\n");
dea3101e 1361 goto out_host_busy;
1362 }
1363
ea2151b4 1364 lpfc_cmd->start_time = jiffies;
dea3101e 1365 /*
1366 * Store the midlayer's command structure for the completion phase
1367 * and complete the command initialization.
1368 */
1369 lpfc_cmd->pCmd = cmnd;
1370 lpfc_cmd->rdata = rdata;
1371 lpfc_cmd->timeout = 0;
977b5a0a 1372 lpfc_cmd->start_time = jiffies;
dea3101e 1373 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
1374 cmnd->scsi_done = done;
1375
1376 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1377 if (err)
1378 goto out_host_busy_free_buf;
1379
2e0fef85 1380 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea3101e 1381
977b5a0a 1382 atomic_inc(&ndlp->cmd_pending);
dea3101e 1383 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
92d7f7b0 1384 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
dea3101e 1385 if (err)
1386 goto out_host_busy_free_buf;
875fbdfe
JSEC
1387
1388 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1389 lpfc_sli_poll_fcp_ring(phba);
1390 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1391 lpfc_poll_rearm_timer(phba);
1392 }
1393
dea3101e 1394 return 0;
1395
1396 out_host_busy_free_buf:
977b5a0a 1397 atomic_dec(&ndlp->cmd_pending);
bcf4dbfa 1398 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
0bd4ca25 1399 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 1400 out_host_busy:
1401 return SCSI_MLQUEUE_HOST_BUSY;
1402
1403 out_fail_command:
1404 done(cmnd);
1405 return 0;
1406}
1407
a90f5684
JS
1408static void
1409lpfc_block_error_handler(struct scsi_cmnd *cmnd)
1410{
1411 struct Scsi_Host *shost = cmnd->device->host;
1412 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1413
1414 spin_lock_irq(shost->host_lock);
1415 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1416 spin_unlock_irq(shost->host_lock);
1417 msleep(1000);
1418 spin_lock_irq(shost->host_lock);
1419 }
1420 spin_unlock_irq(shost->host_lock);
1421 return;
1422}
63c59c3b 1423
dea3101e 1424static int
63c59c3b 1425lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea3101e 1426{
2e0fef85
JS
1427 struct Scsi_Host *shost = cmnd->device->host;
1428 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1429 struct lpfc_hba *phba = vport->phba;
dea3101e 1430 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
0bd4ca25
JSEC
1431 struct lpfc_iocbq *iocb;
1432 struct lpfc_iocbq *abtsiocb;
dea3101e 1433 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 1434 IOCB_t *cmd, *icmd;
0bd4ca25 1435 int ret = SUCCESS;
fa61a54e 1436 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
dea3101e 1437
a90f5684 1438 lpfc_block_error_handler(cmnd);
0bd4ca25
JSEC
1439 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1440 BUG_ON(!lpfc_cmd);
dea3101e 1441
0bd4ca25
JSEC
1442 /*
1443 * If pCmd field of the corresponding lpfc_scsi_buf structure
1444 * points to a different SCSI command, then the driver has
1445 * already completed this command, but the midlayer did not
1446 * see the completion before the eh fired. Just return
1447 * SUCCESS.
1448 */
1449 iocb = &lpfc_cmd->cur_iocbq;
1450 if (lpfc_cmd->pCmd != cmnd)
1451 goto out;
dea3101e 1452
0bd4ca25 1453 BUG_ON(iocb->context1 != lpfc_cmd);
dea3101e 1454
0bd4ca25
JSEC
1455 abtsiocb = lpfc_sli_get_iocbq(phba);
1456 if (abtsiocb == NULL) {
1457 ret = FAILED;
dea3101e 1458 goto out;
1459 }
1460
dea3101e 1461 /*
0bd4ca25
JSEC
1462 * The scsi command can not be in txq and it is in flight because the
1463 * pCmd is still pointig at the SCSI command we have to abort. There
1464 * is no need to search the txcmplq. Just send an abort to the FW.
dea3101e 1465 */
dea3101e 1466
0bd4ca25
JSEC
1467 cmd = &iocb->iocb;
1468 icmd = &abtsiocb->iocb;
1469 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1470 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1471 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 1472
0bd4ca25
JSEC
1473 icmd->ulpLe = 1;
1474 icmd->ulpClass = cmd->ulpClass;
2e0fef85 1475 if (lpfc_is_link_up(phba))
0bd4ca25
JSEC
1476 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1477 else
1478 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 1479
0bd4ca25 1480 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
2e0fef85 1481 abtsiocb->vport = vport;
0bd4ca25
JSEC
1482 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1483 lpfc_sli_release_iocbq(phba, abtsiocb);
1484 ret = FAILED;
1485 goto out;
1486 }
dea3101e 1487
875fbdfe
JSEC
1488 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1489 lpfc_sli_poll_fcp_ring (phba);
1490
fa61a54e 1491 lpfc_cmd->waitq = &waitq;
0bd4ca25 1492 /* Wait for abort to complete */
fa61a54e
JS
1493 wait_event_timeout(waitq,
1494 (lpfc_cmd->pCmd != cmnd),
1495 (2*vport->cfg_devloss_tmo*HZ));
875fbdfe 1496
fa61a54e
JS
1497 spin_lock_irq(shost->host_lock);
1498 lpfc_cmd->waitq = NULL;
1499 spin_unlock_irq(shost->host_lock);
dea3101e 1500
0bd4ca25
JSEC
1501 if (lpfc_cmd->pCmd == cmnd) {
1502 ret = FAILED;
e8b62011
JS
1503 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1504 "0748 abort handler timed out waiting "
1505 "for abort to complete: ret %#x, ID %d, "
1506 "LUN %d, snum %#lx\n",
1507 ret, cmnd->device->id, cmnd->device->lun,
1508 cmnd->serial_number);
dea3101e 1509 }
1510
1511 out:
e8b62011
JS
1512 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1513 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
1514 "LUN %d snum %#lx\n", ret, cmnd->device->id,
1515 cmnd->device->lun, cmnd->serial_number);
63c59c3b 1516 return ret;
8fa728a2
JG
1517}
1518
dea3101e 1519static int
7054a606 1520lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea3101e 1521{
2e0fef85
JS
1522 struct Scsi_Host *shost = cmnd->device->host;
1523 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1524 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
1525 struct lpfc_scsi_buf *lpfc_cmd;
1526 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea3101e 1527 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1528 struct lpfc_nodelist *pnode = rdata->pnode;
915caaaf
JS
1529 unsigned long later;
1530 int ret = SUCCESS;
1531 int status;
1532 int cnt;
ea2151b4 1533 struct lpfc_scsi_event_header scsi_event;
dea3101e 1534
a90f5684 1535 lpfc_block_error_handler(cmnd);
dea3101e 1536 /*
1537 * If target is not in a MAPPED state, delay the reset until
c01f3208 1538 * target is rediscovered or devloss timeout expires.
dea3101e 1539 */
915caaaf
JS
1540 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1541 while (time_after(later, jiffies)) {
58da1ffb 1542 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
915caaaf 1543 return FAILED;
f5603511 1544 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea3101e 1545 break;
915caaaf
JS
1546 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1547 rdata = cmnd->device->hostdata;
1548 if (!rdata)
1549 break;
1550 pnode = rdata->pnode;
1551 }
ea2151b4
JS
1552
1553 scsi_event.event_type = FC_REG_SCSI_EVENT;
1554 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
1555 scsi_event.lun = 0;
1556 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
1557 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
1558
1559 fc_host_post_vendor_event(shost,
1560 fc_get_event_number(),
1561 sizeof(scsi_event),
1562 (char *)&scsi_event,
ddcc50f0 1563 LPFC_NL_VENDOR_ID);
ea2151b4 1564
915caaaf
JS
1565 if (!rdata || pnode->nlp_state != NLP_STE_MAPPED_NODE) {
1566 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1567 "0721 LUN Reset rport "
1568 "failure: msec x%x rdata x%p\n",
1569 jiffies_to_msecs(jiffies - later), rdata);
1570 return FAILED;
dea3101e 1571 }
2e0fef85 1572 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea3101e 1573 if (lpfc_cmd == NULL)
915caaaf 1574 return FAILED;
dea3101e 1575 lpfc_cmd->timeout = 60;
0b18ac42 1576 lpfc_cmd->rdata = rdata;
dea3101e 1577
915caaaf
JS
1578 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd,
1579 cmnd->device->lun,
1580 FCP_TARGET_RESET);
1581 if (!status) {
1582 lpfc_release_scsi_buf(phba, lpfc_cmd);
1583 return FAILED;
1584 }
dea3101e 1585 iocbq = &lpfc_cmd->cur_iocbq;
1586
1587 /* get a buffer for this IOCB command response */
0bd4ca25 1588 iocbqrsp = lpfc_sli_get_iocbq(phba);
915caaaf
JS
1589 if (iocbqrsp == NULL) {
1590 lpfc_release_scsi_buf(phba, lpfc_cmd);
1591 return FAILED;
1592 }
e8b62011
JS
1593 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1594 "0703 Issue target reset to TGT %d LUN %d "
1595 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
1596 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
915caaaf
JS
1597 status = lpfc_sli_issue_iocb_wait(phba,
1598 &phba->sli.ring[phba->sli.fcp_ring],
1599 iocbq, iocbqrsp, lpfc_cmd->timeout);
1600 if (status == IOCB_TIMEDOUT) {
7054a606 1601 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
915caaaf
JS
1602 ret = TIMEOUT_ERROR;
1603 } else {
1604 if (status != IOCB_SUCCESS)
1605 ret = FAILED;
1606 lpfc_release_scsi_buf(phba, lpfc_cmd);
1607 }
1608 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1609 "0713 SCSI layer issued device reset (%d, %d) "
1610 "return x%x status x%x result x%x\n",
1611 cmnd->device->id, cmnd->device->lun, ret,
1612 iocbqrsp->iocb.ulpStatus,
1613 iocbqrsp->iocb.un.ulpWord[4]);
6175c02a 1614 lpfc_sli_release_iocbq(phba, iocbqrsp);
51ef4c26 1615 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
915caaaf 1616 LPFC_CTX_TGT);
6175c02a 1617 if (cnt)
51ef4c26 1618 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
6175c02a 1619 cmnd->device->id, cmnd->device->lun,
915caaaf
JS
1620 LPFC_CTX_TGT);
1621 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1622 while (time_after(later, jiffies) && cnt) {
1623 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
51ef4c26 1624 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
915caaaf 1625 cmnd->device->lun, LPFC_CTX_TGT);
dea3101e 1626 }
dea3101e 1627 if (cnt) {
e8b62011
JS
1628 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1629 "0719 device reset I/O flush failure: "
1630 "cnt x%x\n", cnt);
0bd4ca25 1631 ret = FAILED;
dea3101e 1632 }
dea3101e 1633 return ret;
1634}
1635
94d0e7b8 1636static int
7054a606 1637lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea3101e 1638{
2e0fef85
JS
1639 struct Scsi_Host *shost = cmnd->device->host;
1640 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1641 struct lpfc_hba *phba = vport->phba;
dea3101e 1642 struct lpfc_nodelist *ndlp = NULL;
1643 int match;
d7c255b2 1644 int ret = SUCCESS, status = SUCCESS, i;
915caaaf 1645 int cnt;
0bd4ca25 1646 struct lpfc_scsi_buf * lpfc_cmd;
915caaaf 1647 unsigned long later;
ea2151b4
JS
1648 struct lpfc_scsi_event_header scsi_event;
1649
1650 scsi_event.event_type = FC_REG_SCSI_EVENT;
1651 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
1652 scsi_event.lun = 0;
1653 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
1654 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
1655
1656 fc_host_post_vendor_event(shost,
1657 fc_get_event_number(),
1658 sizeof(scsi_event),
1659 (char *)&scsi_event,
ddcc50f0 1660 LPFC_NL_VENDOR_ID);
dea3101e 1661
a90f5684 1662 lpfc_block_error_handler(cmnd);
dea3101e 1663 /*
1664 * Since the driver manages a single bus device, reset all
1665 * targets known to the driver. Should any target reset
1666 * fail, this routine returns failure to the midlayer.
1667 */
e17da18e 1668 for (i = 0; i < LPFC_MAX_TARGET; i++) {
685f0bf7 1669 /* Search for mapped node by target ID */
dea3101e 1670 match = 0;
2e0fef85
JS
1671 spin_lock_irq(shost->host_lock);
1672 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
e47c9093
JS
1673 if (!NLP_CHK_NODE_ACT(ndlp))
1674 continue;
685f0bf7 1675 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
915caaaf 1676 ndlp->nlp_sid == i &&
685f0bf7 1677 ndlp->rport) {
dea3101e 1678 match = 1;
1679 break;
1680 }
1681 }
2e0fef85 1682 spin_unlock_irq(shost->host_lock);
dea3101e 1683 if (!match)
1684 continue;
915caaaf
JS
1685 lpfc_cmd = lpfc_get_scsi_buf(phba);
1686 if (lpfc_cmd) {
1687 lpfc_cmd->timeout = 60;
1688 status = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1689 cmnd->device->lun,
1690 ndlp->rport->dd_data);
1691 if (status != TIMEOUT_ERROR)
1692 lpfc_release_scsi_buf(phba, lpfc_cmd);
1693 }
1694 if (!lpfc_cmd || status != SUCCESS) {
e8b62011
JS
1695 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1696 "0700 Bus Reset on target %d failed\n",
1697 i);
915caaaf 1698 ret = FAILED;
dea3101e 1699 }
1700 }
6175c02a
JSEC
1701 /*
1702 * All outstanding txcmplq I/Os should have been aborted by
1703 * the targets. Unfortunately, some targets do not abide by
1704 * this forcing the driver to double check.
1705 */
51ef4c26 1706 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
6175c02a 1707 if (cnt)
51ef4c26
JS
1708 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1709 0, 0, LPFC_CTX_HOST);
915caaaf
JS
1710 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1711 while (time_after(later, jiffies) && cnt) {
1712 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
51ef4c26 1713 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
dea3101e 1714 }
dea3101e 1715 if (cnt) {
e8b62011
JS
1716 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1717 "0715 Bus Reset I/O flush failure: "
1718 "cnt x%x left x%x\n", cnt, i);
0bd4ca25 1719 ret = FAILED;
6175c02a 1720 }
e8b62011
JS
1721 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1722 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea3101e 1723 return ret;
1724}
1725
1726static int
1727lpfc_slave_alloc(struct scsi_device *sdev)
1728{
2e0fef85
JS
1729 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1730 struct lpfc_hba *phba = vport->phba;
dea3101e 1731 struct lpfc_scsi_buf *scsi_buf = NULL;
19a7b4ae 1732 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea3101e 1733 uint32_t total = 0, i;
1734 uint32_t num_to_alloc = 0;
1735 unsigned long flags;
dea3101e 1736
19a7b4ae 1737 if (!rport || fc_remote_port_chkready(rport))
dea3101e 1738 return -ENXIO;
1739
19a7b4ae 1740 sdev->hostdata = rport->dd_data;
dea3101e 1741
1742 /*
1743 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1744 * available list of scsi buffers. Don't allocate more than the
a784efbf
JSEC
1745 * HBA limit conveyed to the midlayer via the host structure. The
1746 * formula accounts for the lun_queue_depth + error handlers + 1
1747 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea3101e 1748 */
1749 total = phba->total_scsi_bufs;
3de2a653 1750 num_to_alloc = vport->cfg_lun_queue_depth + 2;
92d7f7b0
JS
1751
1752 /* Allow some exchanges to be available always to complete discovery */
1753 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
1754 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1755 "0704 At limitation of %d preallocated "
1756 "command buffers\n", total);
dea3101e 1757 return 0;
92d7f7b0
JS
1758 /* Allow some exchanges to be available always to complete discovery */
1759 } else if (total + num_to_alloc >
1760 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
1761 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1762 "0705 Allocation request of %d "
1763 "command buffers will exceed max of %d. "
1764 "Reducing allocation request to %d.\n",
1765 num_to_alloc, phba->cfg_hba_queue_depth,
1766 (phba->cfg_hba_queue_depth - total));
dea3101e 1767 num_to_alloc = phba->cfg_hba_queue_depth - total;
1768 }
1769
1770 for (i = 0; i < num_to_alloc; i++) {
2e0fef85 1771 scsi_buf = lpfc_new_scsi_buf(vport);
dea3101e 1772 if (!scsi_buf) {
e8b62011
JS
1773 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1774 "0706 Failed to allocate "
1775 "command buffer\n");
dea3101e 1776 break;
1777 }
1778
875fbdfe 1779 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea3101e 1780 phba->total_scsi_bufs++;
1781 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
875fbdfe 1782 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea3101e 1783 }
1784 return 0;
1785}
1786
1787static int
1788lpfc_slave_configure(struct scsi_device *sdev)
1789{
2e0fef85
JS
1790 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1791 struct lpfc_hba *phba = vport->phba;
1792 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea3101e 1793
1794 if (sdev->tagged_supported)
3de2a653 1795 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 1796 else
3de2a653 1797 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 1798
1799 /*
1800 * Initialize the fc transport attributes for the target
1801 * containing this scsi device. Also note that the driver's
1802 * target pointer is stored in the starget_data for the
1803 * driver's sysfs entry point functions.
1804 */
3de2a653 1805 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea3101e 1806
875fbdfe
JSEC
1807 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1808 lpfc_sli_poll_fcp_ring(phba);
1809 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1810 lpfc_poll_rearm_timer(phba);
1811 }
1812
dea3101e 1813 return 0;
1814}
1815
1816static void
1817lpfc_slave_destroy(struct scsi_device *sdev)
1818{
1819 sdev->hostdata = NULL;
1820 return;
1821}
1822
92d7f7b0 1823
dea3101e 1824struct scsi_host_template lpfc_template = {
1825 .module = THIS_MODULE,
1826 .name = LPFC_DRIVER_NAME,
1827 .info = lpfc_info,
1828 .queuecommand = lpfc_queuecommand,
1829 .eh_abort_handler = lpfc_abort_handler,
7054a606
JS
1830 .eh_device_reset_handler= lpfc_device_reset_handler,
1831 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea3101e 1832 .slave_alloc = lpfc_slave_alloc,
1833 .slave_configure = lpfc_slave_configure,
1834 .slave_destroy = lpfc_slave_destroy,
47a8617c 1835 .scan_finished = lpfc_scan_finished,
dea3101e 1836 .this_id = -1,
83108bd3 1837 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea3101e 1838 .cmd_per_lun = LPFC_CMD_PER_LUN,
1839 .use_clustering = ENABLE_CLUSTERING,
2e0fef85 1840 .shost_attrs = lpfc_hba_attrs,
564b2960 1841 .max_sectors = 0xFFFF,
dea3101e 1842};
3de2a653
JS
1843
1844struct scsi_host_template lpfc_vport_template = {
1845 .module = THIS_MODULE,
1846 .name = LPFC_DRIVER_NAME,
1847 .info = lpfc_info,
1848 .queuecommand = lpfc_queuecommand,
1849 .eh_abort_handler = lpfc_abort_handler,
1850 .eh_device_reset_handler= lpfc_device_reset_handler,
1851 .eh_bus_reset_handler = lpfc_bus_reset_handler,
1852 .slave_alloc = lpfc_slave_alloc,
1853 .slave_configure = lpfc_slave_configure,
1854 .slave_destroy = lpfc_slave_destroy,
1855 .scan_finished = lpfc_scan_finished,
1856 .this_id = -1,
83108bd3 1857 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3de2a653
JS
1858 .cmd_per_lun = LPFC_CMD_PER_LUN,
1859 .use_clustering = ENABLE_CLUSTERING,
1860 .shost_attrs = lpfc_vport_attrs,
1861 .max_sectors = 0xFFFF,
1862};