blob: bd1867411821886bc096dc2e267d1399dd774d7e [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smarte47c9092008-02-08 18:49:26 -05004 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * 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. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/pci.h>
23#include <linux/interrupt.h>
James Smarta90f5682006-08-17 11:58:04 -040024#include <linux/delay.h>
dea31012005-04-17 16:05:31 -050025
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"
James Smartea2151b2008-09-07 11:52:10 -040035#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050036#include "lpfc_disc.h"
37#include "lpfc_scsi.h"
38#include "lpfc.h"
39#include "lpfc_logmsg.h"
40#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050041#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050042
43#define LPFC_RESET_WAIT 2
44#define LPFC_ABORT_WAIT 2
45
James Smartea2151b2008-09-07 11:52:10 -040046/**
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
dea31012005-04-17 16:05:31 -0500151/*
James Smart92d7f7b2007-06-17 19:56:38 -0500152 * 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;
James Smart5e9d9b82008-06-14 22:52:53 -0400159 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500160
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);
James Smart5e9d9b82008-06-14 22:52:53 -0400175 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
176 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500177 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500178 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
179
James Smart5e9d9b82008-06-14 22:52:53 -0400180 if (!evt_posted)
181 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500182 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
James Smart3de2a652007-08-02 11:09:59 -0400190lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smart92d7f7b2007-06-17 19:56:38 -0500191 struct scsi_device *sdev)
192{
193 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400194 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400195 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500196 atomic_inc(&phba->num_cmd_success);
197
James Smart3de2a652007-08-02 11:09:59 -0400198 if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500199 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500200 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 }
James Smart92d7f7b2007-06-17 19:56:38 -0500206 phba->last_ramp_up_time = jiffies;
207 spin_unlock_irqrestore(&phba->hbalock, flags);
208
209 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400210 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
211 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500212 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500213 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
214
James Smart5e9d9b82008-06-14 22:52:53 -0400215 if (!evt_posted)
216 lpfc_worker_wake_up(phba);
217 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500218}
219
220void
221lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
222{
James Smart549e55c2007-08-02 11:09:51 -0400223 struct lpfc_vport **vports;
224 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500225 struct scsi_device *sdev;
James Smartea2151b2008-09-07 11:52:10 -0400226 unsigned long new_queue_depth, old_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500227 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400228 int i;
James Smartea2151b2008-09-07 11:52:10 -0400229 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500230
231 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
232 num_cmd_success = atomic_read(&phba->num_cmd_success);
233
James Smart549e55c2007-08-02 11:09:51 -0400234 vports = lpfc_create_vport_work_array(phba);
235 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500236 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400237 shost = lpfc_shost_from_vport(vports[i]);
238 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500239 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400240 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;
James Smartea2151b2008-09-07 11:52:10 -0400247 old_queue_depth = sdev->queue_depth;
James Smart549e55c2007-08-02 11:09:51 -0400248 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);
James Smartea2151b2008-09-07 11:52:10 -0400256 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);
James Smart549e55c2007-08-02 11:09:51 -0400263 }
James Smart92d7f7b2007-06-17 19:56:38 -0500264 }
James Smart09372822008-01-11 01:52:54 -0500265 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500266 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{
James Smart549e55c2007-08-02 11:09:51 -0400273 struct lpfc_vport **vports;
274 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500275 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400276 int i;
James Smartea2151b2008-09-07 11:52:10 -0400277 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500278
James Smart549e55c2007-08-02 11:09:51 -0400279 vports = lpfc_create_vport_work_array(phba);
280 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500281 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400282 shost = lpfc_shost_from_vport(vports[i]);
283 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400284 if (vports[i]->cfg_lun_queue_depth <=
285 sdev->queue_depth)
286 continue;
James Smart549e55c2007-08-02 11:09:51 -0400287 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);
James Smartea2151b2008-09-07 11:52:10 -0400295 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);
James Smart549e55c2007-08-02 11:09:51 -0400303 }
James Smart92d7f7b2007-06-17 19:56:38 -0500304 }
James Smart09372822008-01-11 01:52:54 -0500305 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500306 atomic_set(&phba->num_rsrc_err, 0);
307 atomic_set(&phba->num_cmd_success, 0);
308}
309
James Smarta8e497d2008-08-24 21:50:11 -0400310/**
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
James Smart92d7f7b2007-06-17 19:56:38 -0500339/*
dea31012005-04-17 16:05:31 -0500340 * 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 *
James Smart2e0fef82007-06-17 19:56:36 -0500348lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500349{
James Smart2e0fef82007-06-17 19:56:36 -0500350 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500351 struct lpfc_scsi_buf *psb;
352 struct ulp_bde64 *bpl;
353 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400354 dma_addr_t pdma_phys_fcp_cmd;
355 dma_addr_t pdma_phys_fcp_rsp;
356 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500357 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500358
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200359 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
dea31012005-04-17 16:05:31 -0500360 if (!psb)
361 return NULL;
dea31012005-04-17 16:05:31 -0500362
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
James Bottomley604a3e32005-10-29 10:28:33 -0500379 /* 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 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400387 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500388
dea31012005-04-17 16:05:31 -0500389 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;
James Smart34b02dc2008-08-24 21:49:55 -0400396 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);
dea31012005-04-17 16:05:31 -0500400
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 */
James Smart34b02dc2008-08-24 21:49:55 -0400406 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);
dea31012005-04-17 16:05:31 -0500411
412 /* Setup the physical region for the FCP RSP */
James Smart34b02dc2008-08-24 21:49:55 -0400413 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);
dea31012005-04-17 16:05:31 -0500418
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 */
dea31012005-04-17 16:05:31 -0500423 iocb = &psb->cur_iocbq.iocb;
424 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
James Smart34b02dc2008-08-24 21:49:55 -0400425 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 }
dea31012005-04-17 16:05:31 -0500450 iocb->ulpClass = CLASS3;
451
452 return psb;
453}
454
Adrian Bunk455c53e2006-01-06 20:21:28 +0100455static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500456lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500457{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400458 struct lpfc_scsi_buf * lpfc_cmd = NULL;
459 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500460 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500461
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500462 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400463 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400464 if (lpfc_cmd) {
465 lpfc_cmd->seg_cnt = 0;
466 lpfc_cmd->nonsg_phys = 0;
467 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500468 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400469 return lpfc_cmd;
470}
471
472static void
James Smart92d7f7b2007-06-17 19:56:38 -0500473lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400474{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500475 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500476
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500477 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400478 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500479 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500480 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500481}
482
483static int
James Smart92d7f7b2007-06-17 19:56:38 -0500484lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500485{
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;
James Smart34b02dc2008-08-24 21:49:55 -0400491 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -0500492 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -0400493 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500494 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500495
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;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700503 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500504 /*
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 */
dea31012005-04-17 16:05:31 -0500510
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700511 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
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500516 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500517 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",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700520 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -0500521 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500522 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500523 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.
James Smart34b02dc2008-08-24 21:49:55 -0400531 * 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.
dea31012005-04-17 16:05:31 -0500534 */
James Smart34b02dc2008-08-24 21:49:55 -0400535 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -0500536 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -0400537 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 }
dea31012005-04-17 16:05:31 -0500554 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700555 }
dea31012005-04-17 16:05:31 -0500556
557 /*
558 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -0400559 * 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.
dea31012005-04-17 16:05:31 -0500562 */
James Smart34b02dc2008-08-24 21:49:55 -0400563 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 }
James Smart09372822008-01-11 01:52:54 -0500589 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
dea31012005-04-17 16:05:31 -0500590 return 0;
591}
592
James Smartea2151b2008-09-07 11:52:10 -0400593/**
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}
dea31012005-04-17 16:05:31 -0500684static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400685lpfc_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 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500693 if (psb->seg_cnt > 0)
694 scsi_dma_unmap(psb->pCmd);
James Smartbcf4dbf2006-07-06 15:50:08 -0400695}
696
697static void
James Smart2e0fef82007-06-17 19:56:36 -0500698lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
699 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -0500700{
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;
James Smart7054a602007-04-25 09:52:34 -0400704 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -0500705 uint32_t resp_info = fcprsp->rspStatus2;
706 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500707 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500708 uint32_t host_status = DID_OK;
709 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500710 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500711
James Smartea2151b2008-09-07 11:52:10 -0400712
dea31012005-04-17 16:05:31 -0500713 /*
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
James Smartc7743952006-12-02 13:34:42 -0500723 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
James Smarte8b62012007-08-02 11:10:09 -0400737 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);
dea31012005-04-17 16:05:31 -0500746
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
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500756 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -0500757 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500758 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -0500759
James Smarte8b62012007-08-02 11:10:09 -0400760 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);
dea31012005-04-17 16:05:31 -0500766
767 /*
James Smart7054a602007-04-25 09:52:34 -0400768 * 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 &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500774 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -0400775 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]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500782 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -0400783 host_status = DID_ERROR;
784 }
785 /*
dea31012005-04-17 16:05:31 -0500786 * 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) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500793 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
794 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -0400795 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",
James Smart66dbfbe2007-08-05 06:08:38 -0400799 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -0400800 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -0500801 host_status = DID_ERROR;
802 }
803 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -0400804 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));
dea31012005-04-17 16:05:31 -0500808 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)) {
James Smarte8b62012007-08-02 11:10:09 -0400816 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]);
dea31012005-04-17 16:05:31 -0500822 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500823 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -0500824 }
825
826 out:
827 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -0400828 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -0500829}
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;
James Smart2e0fef82007-06-17 19:56:36 -0500837 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500838 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
839 struct lpfc_nodelist *pnode = rdata->pnode;
840 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500841 int result;
842 struct scsi_device *sdev, *tmp_sdev;
843 int depth = 0;
James Smartfa61a542008-01-11 01:52:42 -0500844 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -0400845 struct lpfc_fast_path_event *fast_path_evt;
dea31012005-04-17 16:05:31 -0500846
847 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
848 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart977b5a02008-09-07 11:52:04 -0400849 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -0500850
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
James Smarte8b62012007-08-02 11:10:09 -0400858 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);
dea31012005-04-17 16:05:31 -0500867
868 switch (lpfc_cmd->status) {
869 case IOSTAT_FCP_RSP_ERROR:
870 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -0500871 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -0500872 break;
873 case IOSTAT_NPORT_BSY:
874 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -0400875 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -0400876 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);
dea31012005-04-17 16:05:31 -0500900 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500901 case IOSTAT_LOCAL_REJECT:
James Smartd7c255b2008-08-24 21:50:00 -0400902 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -0500903 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartd7c255b2008-08-24 21:50:00 -0400904 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
James Smart92d7f7b2007-06-17 19:56:38 -0500905 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -0400906 break;
907 } /* else: fall through */
dea31012005-04-17 16:05:31 -0500908 default:
909 cmd->result = ScsiResult(DID_ERROR, 0);
910 break;
911 }
912
James Smart58da1ff2008-04-07 10:15:56 -0400913 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400914 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -0400915 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
916 SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500917 } 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
James Smarte8b62012007-08-02 11:10:09 -0400924 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));
dea31012005-04-17 16:05:31 -0500930 }
931
James Smartea2151b2008-09-07 11:52:10 -0400932 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500933 result = cmd->result;
934 sdev = cmd->device;
James Smart977b5a02008-09-07 11:52:04 -0400935 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
James Smart1dcb58e2007-04-25 09:51:30 -0400958 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500959 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400960
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500961 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -0500962 /*
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);
James Smart495a7142008-06-14 22:52:59 -0400967 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -0500968 if (lpfc_cmd->waitq)
969 wake_up(lpfc_cmd->waitq);
970 spin_unlock_irqrestore(sdev->host->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500971 lpfc_release_scsi_buf(phba, lpfc_cmd);
972 return;
973 }
974
James Smart92d7f7b2007-06-17 19:56:38 -0500975
976 if (!result)
James Smart3de2a652007-08-02 11:09:59 -0400977 lpfc_rampup_queue_depth(vport, sdev);
James Smart92d7f7b2007-06-17 19:56:38 -0500978
James Smart58da1ff2008-04-07 10:15:56 -0400979 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500980 ((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) &&
James Smart3de2a652007-08-02 11:09:59 -0400984 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500985 shost_for_each_device(tmp_sdev, sdev->host) {
James Smart3de2a652007-08-02 11:09:59 -0400986 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500987 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 }
James Smartea2151b2008-09-07 11:52:10 -04001001 lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
1002 0xFFFFFFFF,
1003 sdev->queue_depth - 1, sdev->queue_depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001004 }
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 */
James Smart58da1ff2008-04-07 10:15:56 -04001010 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
1011 NLP_CHK_NODE_ACT(pnode)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001012 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 /*
James Smart2e0fef82007-06-17 19:56:36 -05001021 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001022 * 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) {
James Smarte8b62012007-08-02 11:10:09 -04001030 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1031 "0711 detected queue full - lun queue "
1032 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04001033 lpfc_send_sdev_queuedepth_change_event(phba, vport,
1034 pnode, 0xFFFFFFFF,
1035 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001036 }
1037 }
1038
James Smartfa61a542008-01-11 01:52:42 -05001039 /*
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);
James Smart495a7142008-06-14 22:52:59 -04001044 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05001045 if (lpfc_cmd->waitq)
1046 wake_up(lpfc_cmd->waitq);
1047 spin_unlock_irqrestore(sdev->host->host_lock, flags);
1048
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001049 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001050}
1051
James Smart34b02dc2008-08-24 21:49:55 -04001052/**
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
dea31012005-04-17 16:05:31 -05001070static void
James Smart2e0fef82007-06-17 19:56:36 -05001071lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
1072 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05001073{
James Smart2e0fef82007-06-17 19:56:36 -05001074 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001075 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;
James Smart7e2b19f2007-10-29 11:00:39 -04001080 char tag[2];
dea31012005-04-17 16:05:31 -05001081
James Smart58da1ff2008-04-07 10:15:56 -04001082 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
1083 return;
1084
dea31012005-04-17 16:05:31 -05001085 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04001086 /* clear task management bits */
1087 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05001088
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04001089 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
1090 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05001091
1092 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
1093
James Smart7e2b19f2007-10-29 11:00:39 -04001094 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
1095 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05001096 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 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001115 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001116 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;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001125 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001126 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 }
James Smart34b02dc2008-08-24 21:49:55 -04001136 if (phba->sli_rev == 3)
1137 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05001138 /*
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;
James Smart09372822008-01-11 01:52:54 -05001145 else
1146 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05001147
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;
James Smart2e0fef82007-06-17 19:56:36 -05001152 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05001153}
1154
1155static int
James Smart2e0fef82007-06-17 19:56:36 -05001156lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05001157 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04001158 unsigned int lun,
dea31012005-04-17 16:05:31 -05001159 uint8_t task_mgmt_cmd)
1160{
dea31012005-04-17 16:05:31 -05001161 struct lpfc_iocbq *piocbq;
1162 IOCB_t *piocb;
1163 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04001164 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05001165 struct lpfc_nodelist *ndlp = rdata->pnode;
1166
James Smart58da1ff2008-04-07 10:15:56 -04001167 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1168 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001169 return 0;
dea31012005-04-17 16:05:31 -05001170
dea31012005-04-17 16:05:31 -05001171 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001172 piocbq->vport = vport;
1173
dea31012005-04-17 16:05:31 -05001174 piocb = &piocbq->iocb;
1175
1176 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04001177 /* 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);
dea31012005-04-17 16:05:31 -05001180 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smart34b02dc2008-08-24 21:49:55 -04001181 if (vport->phba->sli_rev == 3)
1182 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05001183 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05001184 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
James Smart2e0fef82007-06-17 19:56:36 -05001201 return 1;
dea31012005-04-17 16:05:31 -05001202}
1203
James Smart7054a602007-04-25 09:52:34 -04001204static 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
dea31012005-04-17 16:05:31 -05001216static int
James Smart2e0fef82007-06-17 19:56:36 -05001217lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -04001218 unsigned tgt_id, unsigned int lun,
1219 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -05001220{
James Smart2e0fef82007-06-17 19:56:36 -05001221 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001222 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001223 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -05001224 int ret;
James Smart915caaa2008-06-14 22:52:38 -04001225 int status;
dea31012005-04-17 16:05:31 -05001226
James Smart58da1ff2008-04-07 10:15:56 -04001227 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartf5603512006-12-02 13:35:43 -05001228 return FAILED;
1229
James Smart0b18ac42006-05-01 21:50:40 -04001230 lpfc_cmd->rdata = rdata;
James Smart915caaa2008-06-14 22:52:38 -04001231 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -04001232 FCP_TARGET_RESET);
James Smart915caaa2008-06-14 22:52:38 -04001233 if (!status)
dea31012005-04-17 16:05:31 -05001234 return FAILED;
1235
dea31012005-04-17 16:05:31 -05001236 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001237 iocbqrsp = lpfc_sli_get_iocbq(phba);
1238
dea31012005-04-17 16:05:31 -05001239 if (!iocbqrsp)
1240 return FAILED;
dea31012005-04-17 16:05:31 -05001241
James Smart0b18ac42006-05-01 21:50:40 -04001242 /* Issue Target Reset to TGT <num> */
James Smarte8b62012007-08-02 11:10:09 -04001243 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);
James Smart915caaa2008-06-14 22:52:38 -04001246 status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001247 &phba->sli.ring[phba->sli.fcp_ring],
1248 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart915caaa2008-06-14 22:52:38 -04001249 if (status != IOCB_SUCCESS) {
1250 if (status == IOCB_TIMEDOUT) {
James Smart7054a602007-04-25 09:52:34 -04001251 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smart915caaa2008-06-14 22:52:38 -04001252 ret = TIMEOUT_ERROR;
1253 } else
1254 ret = FAILED;
dea31012005-04-17 16:05:31 -05001255 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -05001256 } 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
James Bottomley604a3e32005-10-29 10:28:33 -05001265 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05001266 return ret;
1267}
1268
dea31012005-04-17 16:05:31 -05001269const char *
1270lpfc_info(struct Scsi_Host *host)
1271{
James Smart2e0fef82007-06-17 19:56:36 -05001272 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
1273 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001274 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
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001298static __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{
James Smart2e0fef82007-06-17 19:56:36 -05001315 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001316
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 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001322}
1323
dea31012005-04-17 16:05:31 -05001324static int
1325lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1326{
James Smart2e0fef82007-06-17 19:56:36 -05001327 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;
dea31012005-04-17 16:05:31 -05001331 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1332 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001333 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001334 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001335 int err;
dea31012005-04-17 16:05:31 -05001336
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001337 err = fc_remote_port_chkready(rport);
1338 if (err) {
1339 cmnd->result = err;
dea31012005-04-17 16:05:31 -05001340 goto out_fail_command;
1341 }
1342
1343 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001344 * Catch race where our node has transitioned, but the
1345 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05001346 */
James Smartb522d7d2008-09-07 11:51:56 -04001347 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1348 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
1349 goto out_fail_command;
1350 }
James Smart977b5a02008-09-07 11:52:04 -04001351 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
1352 goto out_host_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05001353
James Smarted957682007-06-17 19:56:37 -05001354 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001355 if (lpfc_cmd == NULL) {
James Smart92d7f7b2007-06-17 19:56:38 -05001356 lpfc_adjust_queue_depth(phba);
1357
James Smarte8b62012007-08-02 11:10:09 -04001358 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1359 "0707 driver's buffer pool is empty, "
1360 "IO busied\n");
dea31012005-04-17 16:05:31 -05001361 goto out_host_busy;
1362 }
1363
James Smartea2151b2008-09-07 11:52:10 -04001364 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05001365 /*
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;
James Smart977b5a02008-09-07 11:52:04 -04001372 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05001373 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
James Smart2e0fef82007-06-17 19:56:36 -05001380 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05001381
James Smart977b5a02008-09-07 11:52:04 -04001382 atomic_inc(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05001383 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
James Smart92d7f7b2007-06-17 19:56:38 -05001384 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
dea31012005-04-17 16:05:31 -05001385 if (err)
1386 goto out_host_busy_free_buf;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001387
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
dea31012005-04-17 16:05:31 -05001394 return 0;
1395
1396 out_host_busy_free_buf:
James Smart977b5a02008-09-07 11:52:04 -04001397 atomic_dec(&ndlp->cmd_pending);
James Smartbcf4dbf2006-07-06 15:50:08 -04001398 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001399 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001400 out_host_busy:
1401 return SCSI_MLQUEUE_HOST_BUSY;
1402
1403 out_fail_command:
1404 done(cmnd);
1405 return 0;
1406}
1407
James Smarta90f5682006-08-17 11:58:04 -04001408static 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}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001423
dea31012005-04-17 16:05:31 -05001424static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001425lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001426{
James Smart2e0fef82007-06-17 19:56:36 -05001427 struct Scsi_Host *shost = cmnd->device->host;
1428 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1429 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001430 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001431 struct lpfc_iocbq *iocb;
1432 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05001433 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05001434 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001435 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05001436 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001437
James Smarta90f5682006-08-17 11:58:04 -04001438 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001439 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1440 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05001441
1442 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001443 * 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.
dea31012005-04-17 16:05:31 -05001448 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001449 iocb = &lpfc_cmd->cur_iocbq;
1450 if (lpfc_cmd->pCmd != cmnd)
1451 goto out;
dea31012005-04-17 16:05:31 -05001452
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001453 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05001454
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001455 abtsiocb = lpfc_sli_get_iocbq(phba);
1456 if (abtsiocb == NULL) {
1457 ret = FAILED;
dea31012005-04-17 16:05:31 -05001458 goto out;
1459 }
1460
dea31012005-04-17 16:05:31 -05001461 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001462 * 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.
dea31012005-04-17 16:05:31 -05001465 */
dea31012005-04-17 16:05:31 -05001466
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001467 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;
dea31012005-04-17 16:05:31 -05001472
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001473 icmd->ulpLe = 1;
1474 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05001475 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001476 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1477 else
1478 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05001479
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001480 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05001481 abtsiocb->vport = vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001482 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 }
1487
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001488 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1489 lpfc_sli_poll_fcp_ring (phba);
1490
James Smartfa61a542008-01-11 01:52:42 -05001491 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001492 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05001493 wait_event_timeout(waitq,
1494 (lpfc_cmd->pCmd != cmnd),
1495 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001496
James Smartfa61a542008-01-11 01:52:42 -05001497 spin_lock_irq(shost->host_lock);
1498 lpfc_cmd->waitq = NULL;
1499 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001500
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001501 if (lpfc_cmd->pCmd == cmnd) {
1502 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04001503 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);
dea31012005-04-17 16:05:31 -05001509 }
1510
1511 out:
James Smarte8b62012007-08-02 11:10:09 -04001512 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);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001516 return ret;
dea31012005-04-17 16:05:31 -05001517}
1518
1519static int
James Smart7054a602007-04-25 09:52:34 -04001520lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001521{
James Smart2e0fef82007-06-17 19:56:36 -05001522 struct Scsi_Host *shost = cmnd->device->host;
1523 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1524 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001525 struct lpfc_scsi_buf *lpfc_cmd;
1526 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05001527 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1528 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart915caaa2008-06-14 22:52:38 -04001529 unsigned long later;
1530 int ret = SUCCESS;
1531 int status;
1532 int cnt;
James Smartea2151b2008-09-07 11:52:10 -04001533 struct lpfc_scsi_event_header scsi_event;
dea31012005-04-17 16:05:31 -05001534
James Smarta90f5682006-08-17 11:58:04 -04001535 lpfc_block_error_handler(cmnd);
dea31012005-04-17 16:05:31 -05001536 /*
1537 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001538 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001539 */
James Smart915caaa2008-06-14 22:52:38 -04001540 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1541 while (time_after(later, jiffies)) {
James Smart58da1ff2008-04-07 10:15:56 -04001542 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smart915caaa2008-06-14 22:52:38 -04001543 return FAILED;
James Smartf5603512006-12-02 13:35:43 -05001544 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001545 break;
James Smart915caaa2008-06-14 22:52:38 -04001546 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1547 rdata = cmnd->device->hostdata;
1548 if (!rdata)
1549 break;
1550 pnode = rdata->pnode;
dea31012005-04-17 16:05:31 -05001551 }
James Smartea2151b2008-09-07 11:52:10 -04001552
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,
1563 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1564
James Smart915caaa2008-06-14 22:52:38 -04001565 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;
1571 }
James Smart2e0fef82007-06-17 19:56:36 -05001572 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001573 if (lpfc_cmd == NULL)
James Smart915caaa2008-06-14 22:52:38 -04001574 return FAILED;
dea31012005-04-17 16:05:31 -05001575 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04001576 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05001577
James Smart915caaa2008-06-14 22:52:38 -04001578 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 }
dea31012005-04-17 16:05:31 -05001585 iocbq = &lpfc_cmd->cur_iocbq;
1586
1587 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001588 iocbqrsp = lpfc_sli_get_iocbq(phba);
James Smart915caaa2008-06-14 22:52:38 -04001589 if (iocbqrsp == NULL) {
1590 lpfc_release_scsi_buf(phba, lpfc_cmd);
1591 return FAILED;
1592 }
James Smarte8b62012007-08-02 11:10:09 -04001593 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);
James Smart915caaa2008-06-14 22:52:38 -04001597 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) {
James Smart7054a602007-04-25 09:52:34 -04001601 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smart915caaa2008-06-14 22:52:38 -04001602 ret = TIMEOUT_ERROR;
1603 } else {
1604 if (status != IOCB_SUCCESS)
1605 ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001606 lpfc_release_scsi_buf(phba, lpfc_cmd);
1607 }
James Smarte8b62012007-08-02 11:10:09 -04001608 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,
James Smart915caaa2008-06-14 22:52:38 -04001612 iocbqrsp->iocb.ulpStatus,
1613 iocbqrsp->iocb.un.ulpWord[4]);
1614 lpfc_sli_release_iocbq(phba, iocbqrsp);
1615 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
1616 LPFC_CTX_TGT);
1617 if (cnt)
1618 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1619 cmnd->device->id, cmnd->device->lun,
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));
1624 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
1625 cmnd->device->lun, LPFC_CTX_TGT);
1626 }
1627 if (cnt) {
1628 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1629 "0719 device reset I/O flush failure: "
1630 "cnt x%x\n", cnt);
1631 ret = FAILED;
1632 }
dea31012005-04-17 16:05:31 -05001633 return ret;
1634}
1635
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04001636static int
James Smart7054a602007-04-25 09:52:34 -04001637lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001638{
James Smart2e0fef82007-06-17 19:56:36 -05001639 struct Scsi_Host *shost = cmnd->device->host;
1640 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1641 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001642 struct lpfc_nodelist *ndlp = NULL;
1643 int match;
James Smartd7c255b2008-08-24 21:50:00 -04001644 int ret = SUCCESS, status = SUCCESS, i;
James Smart915caaa2008-06-14 22:52:38 -04001645 int cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001646 struct lpfc_scsi_buf * lpfc_cmd;
James Smart915caaa2008-06-14 22:52:38 -04001647 unsigned long later;
James Smartea2151b2008-09-07 11:52:10 -04001648 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,
1660 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
dea31012005-04-17 16:05:31 -05001661
James Smarta90f5682006-08-17 11:58:04 -04001662 lpfc_block_error_handler(cmnd);
dea31012005-04-17 16:05:31 -05001663 /*
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 */
James Smarte17da182006-07-06 15:49:25 -04001668 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04001669 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05001670 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001671 spin_lock_irq(shost->host_lock);
1672 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001673 if (!NLP_CHK_NODE_ACT(ndlp))
1674 continue;
James Smart685f0bf2007-04-25 09:53:08 -04001675 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04001676 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04001677 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001678 match = 1;
1679 break;
1680 }
1681 }
James Smart2e0fef82007-06-17 19:56:36 -05001682 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001683 if (!match)
1684 continue;
James Smart915caaa2008-06-14 22:52:38 -04001685 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) {
James Smarte8b62012007-08-02 11:10:09 -04001695 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1696 "0700 Bus Reset on target %d failed\n",
1697 i);
James Smart915caaa2008-06-14 22:52:38 -04001698 ret = FAILED;
dea31012005-04-17 16:05:31 -05001699 }
1700 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001701 /*
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 */
James Smart51ef4c22007-08-02 11:10:31 -04001706 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001707 if (cnt)
James Smart51ef4c22007-08-02 11:10:31 -04001708 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1709 0, 0, LPFC_CTX_HOST);
James Smart915caaa2008-06-14 22:52:38 -04001710 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));
James Smart51ef4c22007-08-02 11:10:31 -04001713 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001714 }
dea31012005-04-17 16:05:31 -05001715 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001716 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);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001719 ret = FAILED;
dea31012005-04-17 16:05:31 -05001720 }
James Smarte8b62012007-08-02 11:10:09 -04001721 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1722 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05001723 return ret;
1724}
1725
1726static int
dea31012005-04-17 16:05:31 -05001727lpfc_slave_alloc(struct scsi_device *sdev)
1728{
James Smart2e0fef82007-06-17 19:56:36 -05001729 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1730 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001731 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001732 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001733 uint32_t total = 0, i;
1734 uint32_t num_to_alloc = 0;
1735 unsigned long flags;
dea31012005-04-17 16:05:31 -05001736
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001737 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001738 return -ENXIO;
1739
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001740 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001741
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
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001745 * 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.
dea31012005-04-17 16:05:31 -05001748 */
1749 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04001750 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05001751
1752 /* Allow some exchanges to be available always to complete discovery */
1753 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04001754 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1755 "0704 At limitation of %d preallocated "
1756 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05001757 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001758 /* 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 ) {
James Smarte8b62012007-08-02 11:10:09 -04001761 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));
dea31012005-04-17 16:05:31 -05001767 num_to_alloc = phba->cfg_hba_queue_depth - total;
1768 }
1769
1770 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05001771 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05001772 if (!scsi_buf) {
James Smarte8b62012007-08-02 11:10:09 -04001773 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1774 "0706 Failed to allocate "
1775 "command buffer\n");
dea31012005-04-17 16:05:31 -05001776 break;
1777 }
1778
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001779 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001780 phba->total_scsi_bufs++;
1781 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001782 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001783 }
1784 return 0;
1785}
1786
1787static int
1788lpfc_slave_configure(struct scsi_device *sdev)
1789{
James Smart2e0fef82007-06-17 19:56:36 -05001790 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);
dea31012005-04-17 16:05:31 -05001793
1794 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04001795 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001796 else
James Smart3de2a652007-08-02 11:09:59 -04001797 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001798
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 */
James Smart3de2a652007-08-02 11:09:59 -04001805 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05001806
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001807 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
dea31012005-04-17 16:05:31 -05001813 return 0;
1814}
1815
1816static void
1817lpfc_slave_destroy(struct scsi_device *sdev)
1818{
1819 sdev->hostdata = NULL;
1820 return;
1821}
1822
James Smart92d7f7b2007-06-17 19:56:38 -05001823
dea31012005-04-17 16:05:31 -05001824struct 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,
James Smart7054a602007-04-25 09:52:34 -04001830 .eh_device_reset_handler= lpfc_device_reset_handler,
1831 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05001832 .slave_alloc = lpfc_slave_alloc,
1833 .slave_configure = lpfc_slave_configure,
1834 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04001835 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05001836 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05001837 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05001838 .cmd_per_lun = LPFC_CMD_PER_LUN,
1839 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05001840 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001841 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001842};
James Smart3de2a652007-08-02 11:09:59 -04001843
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,
James Smart83108bd2008-01-11 01:53:09 -05001857 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04001858 .cmd_per_lun = LPFC_CMD_PER_LUN,
1859 .use_clustering = ENABLE_CLUSTERING,
1860 .shost_attrs = lpfc_vport_attrs,
1861 .max_sectors = 0xFFFF,
1862};