blob: 51e6a63949514ad1cc454a416e538960affeec95 [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
James Smartea2151b2008-09-07 11:52:10 -040095/**
96 * lpfc_send_sdev_queuedepth_change_event: Posts a queuedepth change
97 * event.
98 * @phba: Pointer to HBA context object.
99 * @vport: Pointer to vport object.
100 * @ndlp: Pointer to FC node associated with the target.
101 * @lun: Lun number of the scsi device.
102 * @old_val: Old value of the queue depth.
103 * @new_val: New value of the queue depth.
104 *
105 * This function sends an event to the mgmt application indicating
106 * there is a change in the scsi device queue depth.
107 **/
108static void
109lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
110 struct lpfc_vport *vport,
111 struct lpfc_nodelist *ndlp,
112 uint32_t lun,
113 uint32_t old_val,
114 uint32_t new_val)
115{
116 struct lpfc_fast_path_event *fast_path_evt;
117 unsigned long flags;
118
119 fast_path_evt = lpfc_alloc_fast_evt(phba);
120 if (!fast_path_evt)
121 return;
122
123 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
124 FC_REG_SCSI_EVENT;
125 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
126 LPFC_EVENT_VARQUEDEPTH;
127
128 /* Report all luns with change in queue depth */
129 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
130 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
131 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
132 &ndlp->nlp_portname, sizeof(struct lpfc_name));
133 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
134 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
135 }
136
137 fast_path_evt->un.queue_depth_evt.oldval = old_val;
138 fast_path_evt->un.queue_depth_evt.newval = new_val;
139 fast_path_evt->vport = vport;
140
141 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
142 spin_lock_irqsave(&phba->hbalock, flags);
143 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
144 spin_unlock_irqrestore(&phba->hbalock, flags);
145 lpfc_worker_wake_up(phba);
146
147 return;
148}
149
James Smart9bad7672008-12-04 22:39:02 -0500150/**
James Smarteaf15d52008-12-04 22:39:29 -0500151 * lpfc_rampdown_queue_depth: Post RAMP_DOWN_QUEUE event to worker thread.
James Smart9bad7672008-12-04 22:39:02 -0500152 * @phba: The Hba for which this call is being executed.
153 *
154 * This routine is called when there is resource error in driver or firmware.
155 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
156 * posts at most 1 event each second. This routine wakes up worker thread of
157 * @phba to process WORKER_RAM_DOWN_EVENT event.
158 *
159 * This routine should be called with no lock held.
160 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500161void
James Smarteaf15d52008-12-04 22:39:29 -0500162lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500163{
164 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400165 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500166
167 spin_lock_irqsave(&phba->hbalock, flags);
168 atomic_inc(&phba->num_rsrc_err);
169 phba->last_rsrc_error_time = jiffies;
170
171 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
172 spin_unlock_irqrestore(&phba->hbalock, flags);
173 return;
174 }
175
176 phba->last_ramp_down_time = jiffies;
177
178 spin_unlock_irqrestore(&phba->hbalock, flags);
179
180 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400181 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
182 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500183 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500184 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
185
James Smart5e9d9b82008-06-14 22:52:53 -0400186 if (!evt_posted)
187 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500188 return;
189}
190
James Smart9bad7672008-12-04 22:39:02 -0500191/**
192 * lpfc_rampup_queue_depth: Post RAMP_UP_QUEUE event for worker thread.
193 * @phba: The Hba for which this call is being executed.
194 *
195 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
196 * post at most 1 event every 5 minute after last_ramp_up_time or
197 * last_rsrc_error_time. This routine wakes up worker thread of @phba
198 * to process WORKER_RAM_DOWN_EVENT event.
199 *
200 * This routine should be called with no lock held.
201 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500202static inline void
James Smart3de2a652007-08-02 11:09:59 -0400203lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smart92d7f7b2007-06-17 19:56:38 -0500204 struct scsi_device *sdev)
205{
206 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400207 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400208 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500209 atomic_inc(&phba->num_cmd_success);
210
James Smart3de2a652007-08-02 11:09:59 -0400211 if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500212 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500213 spin_lock_irqsave(&phba->hbalock, flags);
214 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
215 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
216 spin_unlock_irqrestore(&phba->hbalock, flags);
217 return;
218 }
James Smart92d7f7b2007-06-17 19:56:38 -0500219 phba->last_ramp_up_time = jiffies;
220 spin_unlock_irqrestore(&phba->hbalock, flags);
221
222 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400223 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
224 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500225 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500226 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
227
James Smart5e9d9b82008-06-14 22:52:53 -0400228 if (!evt_posted)
229 lpfc_worker_wake_up(phba);
230 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500231}
232
James Smart9bad7672008-12-04 22:39:02 -0500233/**
234 * lpfc_ramp_down_queue_handler: WORKER_RAMP_DOWN_QUEUE event handler.
235 * @phba: The Hba for which this call is being executed.
236 *
237 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
238 * thread.This routine reduces queue depth for all scsi device on each vport
239 * associated with @phba.
240 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500241void
242lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
243{
James Smart549e55c2007-08-02 11:09:51 -0400244 struct lpfc_vport **vports;
245 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500246 struct scsi_device *sdev;
James Smartea2151b2008-09-07 11:52:10 -0400247 unsigned long new_queue_depth, old_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500248 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400249 int i;
James Smartea2151b2008-09-07 11:52:10 -0400250 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500251
252 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
253 num_cmd_success = atomic_read(&phba->num_cmd_success);
254
James Smart549e55c2007-08-02 11:09:51 -0400255 vports = lpfc_create_vport_work_array(phba);
256 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500257 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400258 shost = lpfc_shost_from_vport(vports[i]);
259 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500260 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400261 sdev->queue_depth * num_rsrc_err /
262 (num_rsrc_err + num_cmd_success);
263 if (!new_queue_depth)
264 new_queue_depth = sdev->queue_depth - 1;
265 else
266 new_queue_depth = sdev->queue_depth -
267 new_queue_depth;
James Smartea2151b2008-09-07 11:52:10 -0400268 old_queue_depth = sdev->queue_depth;
James Smart549e55c2007-08-02 11:09:51 -0400269 if (sdev->ordered_tags)
270 scsi_adjust_queue_depth(sdev,
271 MSG_ORDERED_TAG,
272 new_queue_depth);
273 else
274 scsi_adjust_queue_depth(sdev,
275 MSG_SIMPLE_TAG,
276 new_queue_depth);
James Smartea2151b2008-09-07 11:52:10 -0400277 rdata = sdev->hostdata;
278 if (rdata)
279 lpfc_send_sdev_queuedepth_change_event(
280 phba, vports[i],
281 rdata->pnode,
282 sdev->lun, old_queue_depth,
283 new_queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400284 }
James Smart92d7f7b2007-06-17 19:56:38 -0500285 }
James Smart09372822008-01-11 01:52:54 -0500286 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500287 atomic_set(&phba->num_rsrc_err, 0);
288 atomic_set(&phba->num_cmd_success, 0);
289}
290
James Smart9bad7672008-12-04 22:39:02 -0500291/**
292 * lpfc_ramp_up_queue_handler: WORKER_RAMP_UP_QUEUE event handler.
293 * @phba: The Hba for which this call is being executed.
294 *
295 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
296 * thread.This routine increases queue depth for all scsi device on each vport
297 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
298 * num_cmd_success to zero.
299 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500300void
301lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
302{
James Smart549e55c2007-08-02 11:09:51 -0400303 struct lpfc_vport **vports;
304 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500305 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400306 int i;
James Smartea2151b2008-09-07 11:52:10 -0400307 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500308
James Smart549e55c2007-08-02 11:09:51 -0400309 vports = lpfc_create_vport_work_array(phba);
310 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500311 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400312 shost = lpfc_shost_from_vport(vports[i]);
313 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400314 if (vports[i]->cfg_lun_queue_depth <=
315 sdev->queue_depth)
316 continue;
James Smart549e55c2007-08-02 11:09:51 -0400317 if (sdev->ordered_tags)
318 scsi_adjust_queue_depth(sdev,
319 MSG_ORDERED_TAG,
320 sdev->queue_depth+1);
321 else
322 scsi_adjust_queue_depth(sdev,
323 MSG_SIMPLE_TAG,
324 sdev->queue_depth+1);
James Smartea2151b2008-09-07 11:52:10 -0400325 rdata = sdev->hostdata;
326 if (rdata)
327 lpfc_send_sdev_queuedepth_change_event(
328 phba, vports[i],
329 rdata->pnode,
330 sdev->lun,
331 sdev->queue_depth - 1,
332 sdev->queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400333 }
James Smart92d7f7b2007-06-17 19:56:38 -0500334 }
James Smart09372822008-01-11 01:52:54 -0500335 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500336 atomic_set(&phba->num_rsrc_err, 0);
337 atomic_set(&phba->num_cmd_success, 0);
338}
339
James Smarta8e497d2008-08-24 21:50:11 -0400340/**
341 * lpfc_scsi_dev_block: set all scsi hosts to block state.
342 * @phba: Pointer to HBA context object.
343 *
344 * This function walks vport list and set each SCSI host to block state
345 * by invoking fc_remote_port_delete() routine. This function is invoked
346 * with EEH when device's PCI slot has been permanently disabled.
347 **/
348void
349lpfc_scsi_dev_block(struct lpfc_hba *phba)
350{
351 struct lpfc_vport **vports;
352 struct Scsi_Host *shost;
353 struct scsi_device *sdev;
354 struct fc_rport *rport;
355 int i;
356
357 vports = lpfc_create_vport_work_array(phba);
358 if (vports != NULL)
359 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
360 shost = lpfc_shost_from_vport(vports[i]);
361 shost_for_each_device(sdev, shost) {
362 rport = starget_to_rport(scsi_target(sdev));
363 fc_remote_port_delete(rport);
364 }
365 }
366 lpfc_destroy_vport_work_array(phba, vports);
367}
368
James Smart9bad7672008-12-04 22:39:02 -0500369/**
370 * lpfc_new_scsi_buf: Scsi buffer allocator.
371 * @vport: The virtual port for which this call being executed.
372 *
dea31012005-04-17 16:05:31 -0500373 * This routine allocates a scsi buffer, which contains all the necessary
374 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
375 * contains information to build the IOCB. The DMAable region contains
James Smart9bad7672008-12-04 22:39:02 -0500376 * memory for the FCP CMND, FCP RSP, and the initial BPL. In addition to
377 * allocating memory, the FCP CMND and FCP RSP BDEs are setup in the BPL
dea31012005-04-17 16:05:31 -0500378 * and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500379 *
380 * Return codes:
381 * NULL - Error
382 * Pointer to lpfc_scsi_buf data structure - Success
383 **/
dea31012005-04-17 16:05:31 -0500384static struct lpfc_scsi_buf *
James Smart2e0fef82007-06-17 19:56:36 -0500385lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500386{
James Smart2e0fef82007-06-17 19:56:36 -0500387 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500388 struct lpfc_scsi_buf *psb;
389 struct ulp_bde64 *bpl;
390 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400391 dma_addr_t pdma_phys_fcp_cmd;
392 dma_addr_t pdma_phys_fcp_rsp;
393 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500394 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500395
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200396 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
dea31012005-04-17 16:05:31 -0500397 if (!psb)
398 return NULL;
dea31012005-04-17 16:05:31 -0500399
400 /*
401 * Get memory from the pci pool to map the virt space to pci bus space
402 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
403 * struct fcp_rsp and the number of bde's necessary to support the
404 * sg_tablesize.
405 */
406 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
407 &psb->dma_handle);
408 if (!psb->data) {
409 kfree(psb);
410 return NULL;
411 }
412
413 /* Initialize virtual ptrs to dma_buf region. */
414 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
415
James Bottomley604a3e32005-10-29 10:28:33 -0500416 /* Allocate iotag for psb->cur_iocbq. */
417 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
418 if (iotag == 0) {
419 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
420 psb->data, psb->dma_handle);
421 kfree (psb);
422 return NULL;
423 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400424 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500425
dea31012005-04-17 16:05:31 -0500426 psb->fcp_cmnd = psb->data;
427 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
428 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
429 sizeof(struct fcp_rsp);
430
431 /* Initialize local short-hand pointers. */
432 bpl = psb->fcp_bpl;
James Smart34b02dc2008-08-24 21:49:55 -0400433 pdma_phys_fcp_cmd = psb->dma_handle;
434 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
435 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
436 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500437
438 /*
439 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
440 * list bdes. Initialize the first two and leave the rest for
441 * queuecommand.
442 */
James Smart34b02dc2008-08-24 21:49:55 -0400443 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
444 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
445 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
446 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
447 bpl[0].tus.w = le32_to_cpu(bpl->tus.w);
dea31012005-04-17 16:05:31 -0500448
449 /* Setup the physical region for the FCP RSP */
James Smart34b02dc2008-08-24 21:49:55 -0400450 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
451 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
452 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
453 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
454 bpl[1].tus.w = le32_to_cpu(bpl->tus.w);
dea31012005-04-17 16:05:31 -0500455
456 /*
457 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
458 * initialize it with all known data now.
459 */
dea31012005-04-17 16:05:31 -0500460 iocb = &psb->cur_iocbq.iocb;
461 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
James Smart34b02dc2008-08-24 21:49:55 -0400462 if (phba->sli_rev == 3) {
463 /* fill in immediate fcp command BDE */
464 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
465 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
466 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
467 unsli3.fcp_ext.icd);
468 iocb->un.fcpi64.bdl.addrHigh = 0;
469 iocb->ulpBdeCount = 0;
470 iocb->ulpLe = 0;
471 /* fill in responce BDE */
472 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
473 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
474 sizeof(struct fcp_rsp);
475 iocb->unsli3.fcp_ext.rbde.addrLow =
476 putPaddrLow(pdma_phys_fcp_rsp);
477 iocb->unsli3.fcp_ext.rbde.addrHigh =
478 putPaddrHigh(pdma_phys_fcp_rsp);
479 } else {
480 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
481 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
482 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_bpl);
483 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_bpl);
484 iocb->ulpBdeCount = 1;
485 iocb->ulpLe = 1;
486 }
dea31012005-04-17 16:05:31 -0500487 iocb->ulpClass = CLASS3;
488
489 return psb;
490}
491
James Smart9bad7672008-12-04 22:39:02 -0500492/**
493 * lpfc_get_scsi_buf: Get a scsi buffer from lpfc_scsi_buf_list list of Hba.
494 * @phba: The Hba for which this call is being executed.
495 *
496 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
497 * and returns to caller.
498 *
499 * Return codes:
500 * NULL - Error
501 * Pointer to lpfc_scsi_buf - Success
502 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100503static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500504lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500505{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400506 struct lpfc_scsi_buf * lpfc_cmd = NULL;
507 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500508 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500509
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500510 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400511 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400512 if (lpfc_cmd) {
513 lpfc_cmd->seg_cnt = 0;
514 lpfc_cmd->nonsg_phys = 0;
515 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500516 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400517 return lpfc_cmd;
518}
519
James Smart9bad7672008-12-04 22:39:02 -0500520/**
521 * lpfc_release_scsi_buf: Return a scsi buffer back to hba lpfc_scsi_buf_list list.
522 * @phba: The Hba for which this call is being executed.
523 * @psb: The scsi buffer which is being released.
524 *
525 * This routine releases @psb scsi buffer by adding it to tail of @phba
526 * lpfc_scsi_buf_list list.
527 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400528static void
James Smart92d7f7b2007-06-17 19:56:38 -0500529lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400530{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500531 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500532
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500533 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400534 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500535 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500536 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500537}
538
James Smart9bad7672008-12-04 22:39:02 -0500539/**
540 * lpfc_scsi_prep_dma_buf: Routine to do DMA mapping for scsi buffer.
541 * @phba: The Hba for which this call is being executed.
542 * @lpfc_cmd: The scsi buffer which is going to be mapped.
543 *
544 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
545 * field of @lpfc_cmd. This routine scans through sg elements and format the
546 * bdea. This routine also initializes all IOCB fields which are dependent on
547 * scsi command request buffer.
548 *
549 * Return codes:
550 * 1 - Error
551 * 0 - Success
552 **/
dea31012005-04-17 16:05:31 -0500553static int
James Smart92d7f7b2007-06-17 19:56:38 -0500554lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500555{
556 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
557 struct scatterlist *sgel = NULL;
558 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
559 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
560 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400561 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -0500562 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -0400563 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500564 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500565
566 /*
567 * There are three possibilities here - use scatter-gather segment, use
568 * the single mapping, or neither. Start the lpfc command prep by
569 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
570 * data bde entry.
571 */
572 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700573 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500574 /*
575 * The driver stores the segment count returned from pci_map_sg
576 * because this a count of dma-mappings used to map the use_sg
577 * pages. They are not guaranteed to be the same for those
578 * architectures that implement an IOMMU.
579 */
dea31012005-04-17 16:05:31 -0500580
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700581 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
582 scsi_sg_count(scsi_cmnd), datadir);
583 if (unlikely(!nseg))
584 return 1;
585
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500586 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500587 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
588 printk(KERN_ERR "%s: Too many sg segments from "
589 "dma_map_sg. Config %d, seg_cnt %d",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700590 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -0500591 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500592 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500593 return 1;
594 }
595
596 /*
597 * The driver established a maximum scatter-gather segment count
598 * during probe that limits the number of sg elements in any
599 * single scsi command. Just run through the seg_cnt and format
600 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -0400601 * When using SLI-3 the driver will try to fit all the BDEs into
602 * the IOCB. If it can't then the BDEs get added to a BPL as it
603 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -0500604 */
James Smart34b02dc2008-08-24 21:49:55 -0400605 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -0500606 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -0400607 if (phba->sli_rev == 3 &&
608 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
609 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
610 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
611 data_bde->addrLow = putPaddrLow(physaddr);
612 data_bde->addrHigh = putPaddrHigh(physaddr);
613 data_bde++;
614 } else {
615 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
616 bpl->tus.f.bdeSize = sg_dma_len(sgel);
617 bpl->tus.w = le32_to_cpu(bpl->tus.w);
618 bpl->addrLow =
619 le32_to_cpu(putPaddrLow(physaddr));
620 bpl->addrHigh =
621 le32_to_cpu(putPaddrHigh(physaddr));
622 bpl++;
623 }
dea31012005-04-17 16:05:31 -0500624 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700625 }
dea31012005-04-17 16:05:31 -0500626
627 /*
628 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -0400629 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
630 * explicitly reinitialized and for SLI-3 the extended bde count is
631 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -0500632 */
James Smart34b02dc2008-08-24 21:49:55 -0400633 if (phba->sli_rev == 3) {
634 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
635 /*
636 * The extended IOCB format can only fit 3 BDE or a BPL.
637 * This I/O has more than 3 BDE so the 1st data bde will
638 * be a BPL that is filled in here.
639 */
640 physaddr = lpfc_cmd->dma_handle;
641 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
642 data_bde->tus.f.bdeSize = (num_bde *
643 sizeof(struct ulp_bde64));
644 physaddr += (sizeof(struct fcp_cmnd) +
645 sizeof(struct fcp_rsp) +
646 (2 * sizeof(struct ulp_bde64)));
647 data_bde->addrHigh = putPaddrHigh(physaddr);
648 data_bde->addrLow = putPaddrLow(physaddr);
649 /* ebde count includes the responce bde and data bpl */
650 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
651 } else {
652 /* ebde count includes the responce bde and data bdes */
653 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
654 }
655 } else {
656 iocb_cmd->un.fcpi64.bdl.bdeSize =
657 ((num_bde + 2) * sizeof(struct ulp_bde64));
658 }
James Smart09372822008-01-11 01:52:54 -0500659 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
dea31012005-04-17 16:05:31 -0500660 return 0;
661}
662
James Smartea2151b2008-09-07 11:52:10 -0400663/**
664 * lpfc_send_scsi_error_event: Posts an event when there is SCSI error.
665 * @phba: Pointer to hba context object.
666 * @vport: Pointer to vport object.
667 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
668 * @rsp_iocb: Pointer to response iocb object which reported error.
669 *
670 * This function posts an event when there is a SCSI command reporting
671 * error from the scsi device.
672 **/
673static void
674lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
675 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
676 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
677 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
678 uint32_t resp_info = fcprsp->rspStatus2;
679 uint32_t scsi_status = fcprsp->rspStatus3;
680 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
681 struct lpfc_fast_path_event *fast_path_evt = NULL;
682 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
683 unsigned long flags;
684
685 /* If there is queuefull or busy condition send a scsi event */
686 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
687 (cmnd->result == SAM_STAT_BUSY)) {
688 fast_path_evt = lpfc_alloc_fast_evt(phba);
689 if (!fast_path_evt)
690 return;
691 fast_path_evt->un.scsi_evt.event_type =
692 FC_REG_SCSI_EVENT;
693 fast_path_evt->un.scsi_evt.subcategory =
694 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
695 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
696 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
697 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
698 &pnode->nlp_portname, sizeof(struct lpfc_name));
699 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
700 &pnode->nlp_nodename, sizeof(struct lpfc_name));
701 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
702 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
703 fast_path_evt = lpfc_alloc_fast_evt(phba);
704 if (!fast_path_evt)
705 return;
706 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
707 FC_REG_SCSI_EVENT;
708 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
709 LPFC_EVENT_CHECK_COND;
710 fast_path_evt->un.check_cond_evt.scsi_event.lun =
711 cmnd->device->lun;
712 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
713 &pnode->nlp_portname, sizeof(struct lpfc_name));
714 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
715 &pnode->nlp_nodename, sizeof(struct lpfc_name));
716 fast_path_evt->un.check_cond_evt.sense_key =
717 cmnd->sense_buffer[2] & 0xf;
718 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
719 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
720 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
721 fcpi_parm &&
722 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
723 ((scsi_status == SAM_STAT_GOOD) &&
724 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
725 /*
726 * If status is good or resid does not match with fcp_param and
727 * there is valid fcpi_parm, then there is a read_check error
728 */
729 fast_path_evt = lpfc_alloc_fast_evt(phba);
730 if (!fast_path_evt)
731 return;
732 fast_path_evt->un.read_check_error.header.event_type =
733 FC_REG_FABRIC_EVENT;
734 fast_path_evt->un.read_check_error.header.subcategory =
735 LPFC_EVENT_FCPRDCHKERR;
736 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
737 &pnode->nlp_portname, sizeof(struct lpfc_name));
738 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
739 &pnode->nlp_nodename, sizeof(struct lpfc_name));
740 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
741 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
742 fast_path_evt->un.read_check_error.fcpiparam =
743 fcpi_parm;
744 } else
745 return;
746
747 fast_path_evt->vport = vport;
748 spin_lock_irqsave(&phba->hbalock, flags);
749 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
750 spin_unlock_irqrestore(&phba->hbalock, flags);
751 lpfc_worker_wake_up(phba);
752 return;
753}
James Smart9bad7672008-12-04 22:39:02 -0500754
755/**
756 * lpfc_scsi_unprep_dma_buf: Routine to un-map DMA mapping of scatter gather.
757 * @phba: The Hba for which this call is being executed.
758 * @psb: The scsi buffer which is going to be un-mapped.
759 *
760 * This routine does DMA un-mapping of scatter gather list of scsi command
761 * field of @lpfc_cmd.
762 **/
dea31012005-04-17 16:05:31 -0500763static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400764lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
765{
766 /*
767 * There are only two special cases to consider. (1) the scsi command
768 * requested scatter-gather usage or (2) the scsi command allocated
769 * a request buffer, but did not request use_sg. There is a third
770 * case, but it does not require resource deallocation.
771 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500772 if (psb->seg_cnt > 0)
773 scsi_dma_unmap(psb->pCmd);
James Smartbcf4dbf2006-07-06 15:50:08 -0400774}
775
James Smart9bad7672008-12-04 22:39:02 -0500776/**
777 * lpfc_handler_fcp_err: FCP response handler.
778 * @vport: The virtual port for which this call is being executed.
779 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
780 * @rsp_iocb: The response IOCB which contains FCP error.
781 *
782 * This routine is called to process response IOCB with status field
783 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
784 * based upon SCSI and FCP error.
785 **/
James Smartbcf4dbf2006-07-06 15:50:08 -0400786static void
James Smart2e0fef82007-06-17 19:56:36 -0500787lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
788 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -0500789{
790 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
791 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
792 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -0400793 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -0500794 uint32_t resp_info = fcprsp->rspStatus2;
795 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500796 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500797 uint32_t host_status = DID_OK;
798 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500799 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500800
James Smartea2151b2008-09-07 11:52:10 -0400801
dea31012005-04-17 16:05:31 -0500802 /*
803 * If this is a task management command, there is no
804 * scsi packet associated with this lpfc_cmd. The driver
805 * consumes it.
806 */
807 if (fcpcmd->fcpCntl2) {
808 scsi_status = 0;
809 goto out;
810 }
811
James Smartc7743952006-12-02 13:34:42 -0500812 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
813 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
814 if (snslen > SCSI_SENSE_BUFFERSIZE)
815 snslen = SCSI_SENSE_BUFFERSIZE;
816
817 if (resp_info & RSP_LEN_VALID)
818 rsplen = be32_to_cpu(fcprsp->rspRspLen);
819 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
820 }
821 lp = (uint32_t *)cmnd->sense_buffer;
822
823 if (!scsi_status && (resp_info & RESID_UNDER))
824 logit = LOG_FCP;
825
James Smarte8b62012007-08-02 11:10:09 -0400826 lpfc_printf_vlog(vport, KERN_WARNING, logit,
827 "0730 FCP command x%x failed: x%x SNS x%x x%x "
828 "Data: x%x x%x x%x x%x x%x\n",
829 cmnd->cmnd[0], scsi_status,
830 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
831 be32_to_cpu(fcprsp->rspResId),
832 be32_to_cpu(fcprsp->rspSnsLen),
833 be32_to_cpu(fcprsp->rspRspLen),
834 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -0500835
836 if (resp_info & RSP_LEN_VALID) {
837 rsplen = be32_to_cpu(fcprsp->rspRspLen);
838 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
839 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
840 host_status = DID_ERROR;
841 goto out;
842 }
843 }
844
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500845 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -0500846 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500847 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -0500848
James Smarte8b62012007-08-02 11:10:09 -0400849 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
850 "0716 FCP Read Underrun, expected %d, "
851 "residual %d Data: x%x x%x x%x\n",
852 be32_to_cpu(fcpcmd->fcpDl),
853 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
854 cmnd->underflow);
dea31012005-04-17 16:05:31 -0500855
856 /*
James Smart7054a602007-04-25 09:52:34 -0400857 * If there is an under run check if under run reported by
858 * storage array is same as the under run reported by HBA.
859 * If this is not same, there is a dropped frame.
860 */
861 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
862 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500863 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -0400864 lpfc_printf_vlog(vport, KERN_WARNING,
865 LOG_FCP | LOG_FCP_ERROR,
866 "0735 FCP Read Check Error "
867 "and Underrun Data: x%x x%x x%x x%x\n",
868 be32_to_cpu(fcpcmd->fcpDl),
869 scsi_get_resid(cmnd), fcpi_parm,
870 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500871 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -0400872 host_status = DID_ERROR;
873 }
874 /*
dea31012005-04-17 16:05:31 -0500875 * The cmnd->underflow is the minimum number of bytes that must
876 * be transfered for this command. Provided a sense condition
877 * is not present, make sure the actual amount transferred is at
878 * least the underflow value or fail.
879 */
880 if (!(resp_info & SNS_LEN_VALID) &&
881 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500882 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
883 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -0400884 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
885 "0717 FCP command x%x residual "
886 "underrun converted to error "
887 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -0400888 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -0400889 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -0500890 host_status = DID_ERROR;
891 }
892 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -0400893 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
894 "0720 FCP command x%x residual overrun error. "
895 "Data: x%x x%x \n", cmnd->cmnd[0],
896 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -0500897 host_status = DID_ERROR;
898
899 /*
900 * Check SLI validation that all the transfer was actually done
901 * (fcpi_parm should be zero). Apply check only to reads.
902 */
903 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
904 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -0400905 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
906 "0734 FCP Read Check Error Data: "
907 "x%x x%x x%x x%x\n",
908 be32_to_cpu(fcpcmd->fcpDl),
909 be32_to_cpu(fcprsp->rspResId),
910 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -0500911 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500912 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -0500913 }
914
915 out:
916 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -0400917 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -0500918}
919
James Smart9bad7672008-12-04 22:39:02 -0500920/**
921 * lpfc_scsi_cmd_iocb_cmpl: Scsi cmnd IOCB completion routine.
922 * @phba: The Hba for which this call is being executed.
923 * @pIocbIn: The command IOCBQ for the scsi cmnd.
924 * @pIocbOut: The response IOCBQ for the scsi cmnd .
925 *
926 * This routine assigns scsi command result by looking into response IOCB
927 * status field appropriately. This routine handles QUEUE FULL condition as
928 * well by ramping down device queue depth.
929 **/
dea31012005-04-17 16:05:31 -0500930static void
931lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
932 struct lpfc_iocbq *pIocbOut)
933{
934 struct lpfc_scsi_buf *lpfc_cmd =
935 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500936 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500937 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
938 struct lpfc_nodelist *pnode = rdata->pnode;
939 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500940 int result;
941 struct scsi_device *sdev, *tmp_sdev;
942 int depth = 0;
James Smartfa61a542008-01-11 01:52:42 -0500943 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -0400944 struct lpfc_fast_path_event *fast_path_evt;
dea31012005-04-17 16:05:31 -0500945
946 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
947 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart109f6ed2008-12-04 22:39:08 -0500948 if (pnode && NLP_CHK_NODE_ACT(pnode))
949 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -0500950
951 if (lpfc_cmd->status) {
952 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
953 (lpfc_cmd->result & IOERR_DRVR_MASK))
954 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
955 else if (lpfc_cmd->status >= IOSTAT_CNT)
956 lpfc_cmd->status = IOSTAT_DEFAULT;
957
James Smarte8b62012007-08-02 11:10:09 -0400958 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
959 "0729 FCP cmd x%x failed <%d/%d> "
960 "status: x%x result: x%x Data: x%x x%x\n",
961 cmd->cmnd[0],
962 cmd->device ? cmd->device->id : 0xffff,
963 cmd->device ? cmd->device->lun : 0xffff,
964 lpfc_cmd->status, lpfc_cmd->result,
965 pIocbOut->iocb.ulpContext,
966 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -0500967
968 switch (lpfc_cmd->status) {
969 case IOSTAT_FCP_RSP_ERROR:
970 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -0500971 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -0500972 break;
973 case IOSTAT_NPORT_BSY:
974 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -0400975 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -0400976 fast_path_evt = lpfc_alloc_fast_evt(phba);
977 if (!fast_path_evt)
978 break;
979 fast_path_evt->un.fabric_evt.event_type =
980 FC_REG_FABRIC_EVENT;
981 fast_path_evt->un.fabric_evt.subcategory =
982 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
983 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
984 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
985 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
986 &pnode->nlp_portname,
987 sizeof(struct lpfc_name));
988 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
989 &pnode->nlp_nodename,
990 sizeof(struct lpfc_name));
991 }
992 fast_path_evt->vport = vport;
993 fast_path_evt->work_evt.evt =
994 LPFC_EVT_FASTPATH_MGMT_EVT;
995 spin_lock_irqsave(&phba->hbalock, flags);
996 list_add_tail(&fast_path_evt->work_evt.evt_listp,
997 &phba->work_list);
998 spin_unlock_irqrestore(&phba->hbalock, flags);
999 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001000 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001001 case IOSTAT_LOCAL_REJECT:
James Smartd7c255b2008-08-24 21:50:00 -04001002 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05001003 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartd7c255b2008-08-24 21:50:00 -04001004 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
James Smart92d7f7b2007-06-17 19:56:38 -05001005 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04001006 break;
1007 } /* else: fall through */
dea31012005-04-17 16:05:31 -05001008 default:
1009 cmd->result = ScsiResult(DID_ERROR, 0);
1010 break;
1011 }
1012
James Smart58da1ff2008-04-07 10:15:56 -04001013 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001014 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04001015 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
1016 SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -05001017 } else {
1018 cmd->result = ScsiResult(DID_OK, 0);
1019 }
1020
1021 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
1022 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
1023
James Smarte8b62012007-08-02 11:10:09 -04001024 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1025 "0710 Iodone <%d/%d> cmd %p, error "
1026 "x%x SNS x%x x%x Data: x%x x%x\n",
1027 cmd->device->id, cmd->device->lun, cmd,
1028 cmd->result, *lp, *(lp + 3), cmd->retries,
1029 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05001030 }
1031
James Smartea2151b2008-09-07 11:52:10 -04001032 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001033 result = cmd->result;
1034 sdev = cmd->device;
James Smart977b5a02008-09-07 11:52:04 -04001035 if (vport->cfg_max_scsicmpl_time &&
1036 time_after(jiffies, lpfc_cmd->start_time +
1037 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
1038 spin_lock_irqsave(sdev->host->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05001039 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
1040 if (pnode->cmd_qdepth >
1041 atomic_read(&pnode->cmd_pending) &&
1042 (atomic_read(&pnode->cmd_pending) >
1043 LPFC_MIN_TGT_QDEPTH) &&
1044 ((cmd->cmnd[0] == READ_10) ||
1045 (cmd->cmnd[0] == WRITE_10)))
1046 pnode->cmd_qdepth =
1047 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04001048
James Smart109f6ed2008-12-04 22:39:08 -05001049 pnode->last_change_time = jiffies;
1050 }
James Smart977b5a02008-09-07 11:52:04 -04001051 spin_unlock_irqrestore(sdev->host->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05001052 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
1053 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
James Smart977b5a02008-09-07 11:52:04 -04001054 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05001055 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
1056 spin_lock_irqsave(sdev->host->host_lock, flags);
1057 pnode->cmd_qdepth += pnode->cmd_qdepth *
1058 LPFC_TGTQ_RAMPUP_PCENT / 100;
1059 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
1060 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
1061 pnode->last_change_time = jiffies;
1062 spin_unlock_irqrestore(sdev->host->host_lock, flags);
1063 }
James Smart977b5a02008-09-07 11:52:04 -04001064 }
1065
James Smart1dcb58e2007-04-25 09:51:30 -04001066 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001067 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001068
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001069 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05001070 /*
1071 * If there is a thread waiting for command completion
1072 * wake up the thread.
1073 */
1074 spin_lock_irqsave(sdev->host->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04001075 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05001076 if (lpfc_cmd->waitq)
1077 wake_up(lpfc_cmd->waitq);
1078 spin_unlock_irqrestore(sdev->host->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001079 lpfc_release_scsi_buf(phba, lpfc_cmd);
1080 return;
1081 }
1082
James Smart92d7f7b2007-06-17 19:56:38 -05001083
1084 if (!result)
James Smart3de2a652007-08-02 11:09:59 -04001085 lpfc_rampup_queue_depth(vport, sdev);
James Smart92d7f7b2007-06-17 19:56:38 -05001086
James Smart58da1ff2008-04-07 10:15:56 -04001087 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001088 ((jiffies - pnode->last_ramp_up_time) >
1089 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
1090 ((jiffies - pnode->last_q_full_time) >
1091 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
James Smart3de2a652007-08-02 11:09:59 -04001092 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001093 shost_for_each_device(tmp_sdev, sdev->host) {
James Smart3de2a652007-08-02 11:09:59 -04001094 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001095 if (tmp_sdev->id != sdev->id)
1096 continue;
1097 if (tmp_sdev->ordered_tags)
1098 scsi_adjust_queue_depth(tmp_sdev,
1099 MSG_ORDERED_TAG,
1100 tmp_sdev->queue_depth+1);
1101 else
1102 scsi_adjust_queue_depth(tmp_sdev,
1103 MSG_SIMPLE_TAG,
1104 tmp_sdev->queue_depth+1);
1105
1106 pnode->last_ramp_up_time = jiffies;
1107 }
1108 }
James Smartea2151b2008-09-07 11:52:10 -04001109 lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
1110 0xFFFFFFFF,
1111 sdev->queue_depth - 1, sdev->queue_depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001112 }
1113
1114 /*
1115 * Check for queue full. If the lun is reporting queue full, then
1116 * back off the lun queue depth to prevent target overloads.
1117 */
James Smart58da1ff2008-04-07 10:15:56 -04001118 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
1119 NLP_CHK_NODE_ACT(pnode)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001120 pnode->last_q_full_time = jiffies;
1121
1122 shost_for_each_device(tmp_sdev, sdev->host) {
1123 if (tmp_sdev->id != sdev->id)
1124 continue;
1125 depth = scsi_track_queue_full(tmp_sdev,
1126 tmp_sdev->queue_depth - 1);
1127 }
1128 /*
James Smart2e0fef82007-06-17 19:56:36 -05001129 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001130 * Modify the returned error code to store
1131 * the final depth value set by
1132 * scsi_track_queue_full.
1133 */
1134 if (depth == -1)
1135 depth = sdev->host->cmd_per_lun;
1136
1137 if (depth) {
James Smarte8b62012007-08-02 11:10:09 -04001138 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1139 "0711 detected queue full - lun queue "
1140 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04001141 lpfc_send_sdev_queuedepth_change_event(phba, vport,
1142 pnode, 0xFFFFFFFF,
1143 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001144 }
1145 }
1146
James Smartfa61a542008-01-11 01:52:42 -05001147 /*
1148 * If there is a thread waiting for command completion
1149 * wake up the thread.
1150 */
1151 spin_lock_irqsave(sdev->host->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04001152 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05001153 if (lpfc_cmd->waitq)
1154 wake_up(lpfc_cmd->waitq);
1155 spin_unlock_irqrestore(sdev->host->host_lock, flags);
1156
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001157 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001158}
1159
James Smart34b02dc2008-08-24 21:49:55 -04001160/**
1161 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB.
1162 * @data: A pointer to the immediate command data portion of the IOCB.
1163 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
1164 *
1165 * The routine copies the entire FCP command from @fcp_cmnd to @data while
1166 * byte swapping the data to big endian format for transmission on the wire.
1167 **/
1168static void
1169lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
1170{
1171 int i, j;
1172 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
1173 i += sizeof(uint32_t), j++) {
1174 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
1175 }
1176}
1177
James Smart9bad7672008-12-04 22:39:02 -05001178/**
1179 * lpfc_scsi_prep_cmnd: Routine to convert scsi cmnd to FCP information unit.
1180 * @vport: The virtual port for which this call is being executed.
1181 * @lpfc_cmd: The scsi command which needs to send.
1182 * @pnode: Pointer to lpfc_nodelist.
1183 *
1184 * This routine initializes fcp_cmnd and iocb data structure from scsi command
1185 * to transfer.
1186 **/
dea31012005-04-17 16:05:31 -05001187static void
James Smart2e0fef82007-06-17 19:56:36 -05001188lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
1189 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05001190{
James Smart2e0fef82007-06-17 19:56:36 -05001191 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001192 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1193 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1194 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1195 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
1196 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04001197 char tag[2];
dea31012005-04-17 16:05:31 -05001198
James Smart58da1ff2008-04-07 10:15:56 -04001199 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
1200 return;
1201
dea31012005-04-17 16:05:31 -05001202 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04001203 /* clear task management bits */
1204 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05001205
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04001206 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
1207 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05001208
1209 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
1210
James Smart7e2b19f2007-10-29 11:00:39 -04001211 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
1212 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05001213 case HEAD_OF_QUEUE_TAG:
1214 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
1215 break;
1216 case ORDERED_QUEUE_TAG:
1217 fcp_cmnd->fcpCntl1 = ORDERED_Q;
1218 break;
1219 default:
1220 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
1221 break;
1222 }
1223 } else
1224 fcp_cmnd->fcpCntl1 = 0;
1225
1226 /*
1227 * There are three possibilities here - use scatter-gather segment, use
1228 * the single mapping, or neither. Start the lpfc command prep by
1229 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1230 * data bde entry.
1231 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001232 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001233 if (datadir == DMA_TO_DEVICE) {
1234 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
1235 iocb_cmd->un.fcpi.fcpi_parm = 0;
1236 iocb_cmd->ulpPU = 0;
1237 fcp_cmnd->fcpCntl3 = WRITE_DATA;
1238 phba->fc4OutputRequests++;
1239 } else {
1240 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
1241 iocb_cmd->ulpPU = PARM_READ_CHECK;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001242 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001243 fcp_cmnd->fcpCntl3 = READ_DATA;
1244 phba->fc4InputRequests++;
1245 }
1246 } else {
1247 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
1248 iocb_cmd->un.fcpi.fcpi_parm = 0;
1249 iocb_cmd->ulpPU = 0;
1250 fcp_cmnd->fcpCntl3 = 0;
1251 phba->fc4ControlRequests++;
1252 }
James Smart34b02dc2008-08-24 21:49:55 -04001253 if (phba->sli_rev == 3)
1254 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05001255 /*
1256 * Finish initializing those IOCB fields that are independent
1257 * of the scsi_cmnd request_buffer
1258 */
1259 piocbq->iocb.ulpContext = pnode->nlp_rpi;
1260 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
1261 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05001262 else
1263 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05001264
1265 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
1266 piocbq->context1 = lpfc_cmd;
1267 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
1268 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05001269 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05001270}
1271
James Smart9bad7672008-12-04 22:39:02 -05001272/**
1273 * lpfc_scsi_prep_task_mgmt_cmnd: Convert scsi TM cmnd to FCP information unit.
1274 * @vport: The virtual port for which this call is being executed.
1275 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
1276 * @lun: Logical unit number.
1277 * @task_mgmt_cmd: SCSI task management command.
1278 *
1279 * This routine creates FCP information unit corresponding to @task_mgmt_cmd.
1280 *
1281 * Return codes:
1282 * 0 - Error
1283 * 1 - Success
1284 **/
dea31012005-04-17 16:05:31 -05001285static int
James Smart2e0fef82007-06-17 19:56:36 -05001286lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05001287 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04001288 unsigned int lun,
dea31012005-04-17 16:05:31 -05001289 uint8_t task_mgmt_cmd)
1290{
dea31012005-04-17 16:05:31 -05001291 struct lpfc_iocbq *piocbq;
1292 IOCB_t *piocb;
1293 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04001294 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05001295 struct lpfc_nodelist *ndlp = rdata->pnode;
1296
James Smart58da1ff2008-04-07 10:15:56 -04001297 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1298 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001299 return 0;
dea31012005-04-17 16:05:31 -05001300
dea31012005-04-17 16:05:31 -05001301 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001302 piocbq->vport = vport;
1303
dea31012005-04-17 16:05:31 -05001304 piocb = &piocbq->iocb;
1305
1306 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04001307 /* Clear out any old data in the FCP command area */
1308 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1309 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05001310 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smart34b02dc2008-08-24 21:49:55 -04001311 if (vport->phba->sli_rev == 3)
1312 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05001313 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05001314 piocb->ulpContext = ndlp->nlp_rpi;
1315 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
1316 piocb->ulpFCP2Rcvy = 1;
1317 }
1318 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
1319
1320 /* ulpTimeout is only one byte */
1321 if (lpfc_cmd->timeout > 0xff) {
1322 /*
1323 * Do not timeout the command at the firmware level.
1324 * The driver will provide the timeout mechanism.
1325 */
1326 piocb->ulpTimeout = 0;
1327 } else {
1328 piocb->ulpTimeout = lpfc_cmd->timeout;
1329 }
1330
James Smart2e0fef82007-06-17 19:56:36 -05001331 return 1;
dea31012005-04-17 16:05:31 -05001332}
1333
James Smart9bad7672008-12-04 22:39:02 -05001334/**
1335 * lpc_taskmgmt_def_cmpl: IOCB completion routine for task management command.
1336 * @phba: The Hba for which this call is being executed.
1337 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
1338 * @rspiocbq: Pointer to lpfc_iocbq data structure.
1339 *
1340 * This routine is IOCB completion routine for device reset and target reset
1341 * routine. This routine release scsi buffer associated with lpfc_cmd.
1342 **/
James Smart7054a602007-04-25 09:52:34 -04001343static void
1344lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
1345 struct lpfc_iocbq *cmdiocbq,
1346 struct lpfc_iocbq *rspiocbq)
1347{
1348 struct lpfc_scsi_buf *lpfc_cmd =
1349 (struct lpfc_scsi_buf *) cmdiocbq->context1;
1350 if (lpfc_cmd)
1351 lpfc_release_scsi_buf(phba, lpfc_cmd);
1352 return;
1353}
1354
James Smart9bad7672008-12-04 22:39:02 -05001355/**
1356 * lpfc_scsi_tgt_reset: Target reset handler.
1357 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure
1358 * @vport: The virtual port for which this call is being executed.
1359 * @tgt_id: Target ID.
1360 * @lun: Lun number.
1361 * @rdata: Pointer to lpfc_rport_data.
1362 *
1363 * This routine issues a TARGET RESET iocb to reset a target with @tgt_id ID.
1364 *
1365 * Return Code:
1366 * 0x2003 - Error
1367 * 0x2002 - Success.
1368 **/
dea31012005-04-17 16:05:31 -05001369static int
James Smart2e0fef82007-06-17 19:56:36 -05001370lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -04001371 unsigned tgt_id, unsigned int lun,
1372 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -05001373{
James Smart2e0fef82007-06-17 19:56:36 -05001374 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001375 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001376 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -05001377 int ret;
James Smart915caaa2008-06-14 22:52:38 -04001378 int status;
dea31012005-04-17 16:05:31 -05001379
James Smart58da1ff2008-04-07 10:15:56 -04001380 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartf5603512006-12-02 13:35:43 -05001381 return FAILED;
1382
James Smart0b18ac42006-05-01 21:50:40 -04001383 lpfc_cmd->rdata = rdata;
James Smart915caaa2008-06-14 22:52:38 -04001384 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -04001385 FCP_TARGET_RESET);
James Smart915caaa2008-06-14 22:52:38 -04001386 if (!status)
dea31012005-04-17 16:05:31 -05001387 return FAILED;
1388
dea31012005-04-17 16:05:31 -05001389 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001390 iocbqrsp = lpfc_sli_get_iocbq(phba);
1391
dea31012005-04-17 16:05:31 -05001392 if (!iocbqrsp)
1393 return FAILED;
dea31012005-04-17 16:05:31 -05001394
James Smart0b18ac42006-05-01 21:50:40 -04001395 /* Issue Target Reset to TGT <num> */
James Smarte8b62012007-08-02 11:10:09 -04001396 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1397 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
1398 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
James Smart915caaa2008-06-14 22:52:38 -04001399 status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001400 &phba->sli.ring[phba->sli.fcp_ring],
1401 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart915caaa2008-06-14 22:52:38 -04001402 if (status != IOCB_SUCCESS) {
1403 if (status == IOCB_TIMEDOUT) {
James Smart7054a602007-04-25 09:52:34 -04001404 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smart915caaa2008-06-14 22:52:38 -04001405 ret = TIMEOUT_ERROR;
1406 } else
1407 ret = FAILED;
dea31012005-04-17 16:05:31 -05001408 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -05001409 } else {
1410 ret = SUCCESS;
1411 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
1412 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
1413 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
1414 (lpfc_cmd->result & IOERR_DRVR_MASK))
1415 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
1416 }
1417
James Bottomley604a3e32005-10-29 10:28:33 -05001418 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05001419 return ret;
1420}
1421
James Smart9bad7672008-12-04 22:39:02 -05001422/**
1423 * lpfc_info: Info entry point of scsi_host_template data structure.
1424 * @host: The scsi host for which this call is being executed.
1425 *
1426 * This routine provides module information about hba.
1427 *
1428 * Reutrn code:
1429 * Pointer to char - Success.
1430 **/
dea31012005-04-17 16:05:31 -05001431const char *
1432lpfc_info(struct Scsi_Host *host)
1433{
James Smart2e0fef82007-06-17 19:56:36 -05001434 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
1435 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001436 int len;
1437 static char lpfcinfobuf[384];
1438
1439 memset(lpfcinfobuf,0,384);
1440 if (phba && phba->pcidev){
1441 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
1442 len = strlen(lpfcinfobuf);
1443 snprintf(lpfcinfobuf + len,
1444 384-len,
1445 " on PCI bus %02x device %02x irq %d",
1446 phba->pcidev->bus->number,
1447 phba->pcidev->devfn,
1448 phba->pcidev->irq);
1449 len = strlen(lpfcinfobuf);
1450 if (phba->Port[0]) {
1451 snprintf(lpfcinfobuf + len,
1452 384-len,
1453 " port %s",
1454 phba->Port);
1455 }
1456 }
1457 return lpfcinfobuf;
1458}
1459
James Smart9bad7672008-12-04 22:39:02 -05001460/**
1461 * lpfc_poll_rearm_time: Routine to modify fcp_poll timer of hba.
1462 * @phba: The Hba for which this call is being executed.
1463 *
1464 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
1465 * The default value of cfg_poll_tmo is 10 milliseconds.
1466 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001467static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
1468{
1469 unsigned long poll_tmo_expires =
1470 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
1471
1472 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
1473 mod_timer(&phba->fcp_poll_timer,
1474 poll_tmo_expires);
1475}
1476
James Smart9bad7672008-12-04 22:39:02 -05001477/**
1478 * lpfc_poll_start_timer: Routine to start fcp_poll_timer of HBA.
1479 * @phba: The Hba for which this call is being executed.
1480 *
1481 * This routine starts the fcp_poll_timer of @phba.
1482 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001483void lpfc_poll_start_timer(struct lpfc_hba * phba)
1484{
1485 lpfc_poll_rearm_timer(phba);
1486}
1487
James Smart9bad7672008-12-04 22:39:02 -05001488/**
1489 * lpfc_poll_timeout: Restart polling timer.
1490 * @ptr: Map to lpfc_hba data structure pointer.
1491 *
1492 * This routine restarts fcp_poll timer, when FCP ring polling is enable
1493 * and FCP Ring interrupt is disable.
1494 **/
1495
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001496void lpfc_poll_timeout(unsigned long ptr)
1497{
James Smart2e0fef82007-06-17 19:56:36 -05001498 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001499
1500 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1501 lpfc_sli_poll_fcp_ring (phba);
1502 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1503 lpfc_poll_rearm_timer(phba);
1504 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001505}
1506
James Smart9bad7672008-12-04 22:39:02 -05001507/**
1508 * lpfc_queuecommand: Queuecommand entry point of Scsi Host Templater data
1509 * structure.
1510 * @cmnd: Pointer to scsi_cmnd data structure.
1511 * @done: Pointer to done routine.
1512 *
1513 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
1514 * This routine prepares an IOCB from scsi command and provides to firmware.
1515 * The @done callback is invoked after driver finished processing the command.
1516 *
1517 * Return value :
1518 * 0 - Success
1519 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
1520 **/
dea31012005-04-17 16:05:31 -05001521static int
1522lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1523{
James Smart2e0fef82007-06-17 19:56:36 -05001524 struct Scsi_Host *shost = cmnd->device->host;
1525 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1526 struct lpfc_hba *phba = vport->phba;
1527 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001528 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1529 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001530 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001531 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001532 int err;
dea31012005-04-17 16:05:31 -05001533
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001534 err = fc_remote_port_chkready(rport);
1535 if (err) {
1536 cmnd->result = err;
dea31012005-04-17 16:05:31 -05001537 goto out_fail_command;
1538 }
1539
1540 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001541 * Catch race where our node has transitioned, but the
1542 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05001543 */
James Smartb522d7d2008-09-07 11:51:56 -04001544 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1545 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
1546 goto out_fail_command;
1547 }
James Smart109f6ed2008-12-04 22:39:08 -05001548 if (vport->cfg_max_scsicmpl_time &&
1549 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
James Smart977b5a02008-09-07 11:52:04 -04001550 goto out_host_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05001551
James Smarted957682007-06-17 19:56:37 -05001552 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001553 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05001554 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001555
James Smarte8b62012007-08-02 11:10:09 -04001556 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1557 "0707 driver's buffer pool is empty, "
1558 "IO busied\n");
dea31012005-04-17 16:05:31 -05001559 goto out_host_busy;
1560 }
1561
1562 /*
1563 * Store the midlayer's command structure for the completion phase
1564 * and complete the command initialization.
1565 */
1566 lpfc_cmd->pCmd = cmnd;
1567 lpfc_cmd->rdata = rdata;
1568 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04001569 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05001570 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
1571 cmnd->scsi_done = done;
1572
1573 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1574 if (err)
1575 goto out_host_busy_free_buf;
1576
James Smart2e0fef82007-06-17 19:56:36 -05001577 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05001578
James Smart977b5a02008-09-07 11:52:04 -04001579 atomic_inc(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05001580 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
James Smart92d7f7b2007-06-17 19:56:38 -05001581 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05001582 if (err) {
1583 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05001584 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05001585 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001586 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1587 lpfc_sli_poll_fcp_ring(phba);
1588 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1589 lpfc_poll_rearm_timer(phba);
1590 }
1591
dea31012005-04-17 16:05:31 -05001592 return 0;
1593
1594 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04001595 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001596 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001597 out_host_busy:
1598 return SCSI_MLQUEUE_HOST_BUSY;
1599
1600 out_fail_command:
1601 done(cmnd);
1602 return 0;
1603}
1604
James Smart9bad7672008-12-04 22:39:02 -05001605/**
1606 * lpfc_block_error_handler: Routine to block error handler.
1607 * @cmnd: Pointer to scsi_cmnd data structure.
1608 *
1609 * This routine blocks execution till fc_rport state is not FC_PORSTAT_BLCOEKD.
1610 **/
James Smarta90f5682006-08-17 11:58:04 -04001611static void
1612lpfc_block_error_handler(struct scsi_cmnd *cmnd)
1613{
1614 struct Scsi_Host *shost = cmnd->device->host;
1615 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1616
1617 spin_lock_irq(shost->host_lock);
1618 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1619 spin_unlock_irq(shost->host_lock);
1620 msleep(1000);
1621 spin_lock_irq(shost->host_lock);
1622 }
1623 spin_unlock_irq(shost->host_lock);
1624 return;
1625}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001626
James Smart9bad7672008-12-04 22:39:02 -05001627/**
1628 * lpfc_abort_handler: Eh_abort_handler entry point of Scsi Host Template data
1629 *structure.
1630 * @cmnd: Pointer to scsi_cmnd data structure.
1631 *
1632 * This routine aborts @cmnd pending in base driver.
1633 *
1634 * Return code :
1635 * 0x2003 - Error
1636 * 0x2002 - Success
1637 **/
dea31012005-04-17 16:05:31 -05001638static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001639lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001640{
James Smart2e0fef82007-06-17 19:56:36 -05001641 struct Scsi_Host *shost = cmnd->device->host;
1642 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1643 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001644 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001645 struct lpfc_iocbq *iocb;
1646 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05001647 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05001648 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001649 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05001650 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001651
James Smarta90f5682006-08-17 11:58:04 -04001652 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001653 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1654 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05001655
1656 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001657 * If pCmd field of the corresponding lpfc_scsi_buf structure
1658 * points to a different SCSI command, then the driver has
1659 * already completed this command, but the midlayer did not
1660 * see the completion before the eh fired. Just return
1661 * SUCCESS.
dea31012005-04-17 16:05:31 -05001662 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001663 iocb = &lpfc_cmd->cur_iocbq;
1664 if (lpfc_cmd->pCmd != cmnd)
1665 goto out;
dea31012005-04-17 16:05:31 -05001666
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001667 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05001668
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001669 abtsiocb = lpfc_sli_get_iocbq(phba);
1670 if (abtsiocb == NULL) {
1671 ret = FAILED;
dea31012005-04-17 16:05:31 -05001672 goto out;
1673 }
1674
dea31012005-04-17 16:05:31 -05001675 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001676 * The scsi command can not be in txq and it is in flight because the
1677 * pCmd is still pointig at the SCSI command we have to abort. There
1678 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05001679 */
dea31012005-04-17 16:05:31 -05001680
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001681 cmd = &iocb->iocb;
1682 icmd = &abtsiocb->iocb;
1683 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1684 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1685 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05001686
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001687 icmd->ulpLe = 1;
1688 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05001689 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001690 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1691 else
1692 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05001693
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001694 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05001695 abtsiocb->vport = vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001696 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1697 lpfc_sli_release_iocbq(phba, abtsiocb);
1698 ret = FAILED;
1699 goto out;
1700 }
1701
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001702 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1703 lpfc_sli_poll_fcp_ring (phba);
1704
James Smartfa61a542008-01-11 01:52:42 -05001705 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001706 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05001707 wait_event_timeout(waitq,
1708 (lpfc_cmd->pCmd != cmnd),
1709 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001710
James Smartfa61a542008-01-11 01:52:42 -05001711 spin_lock_irq(shost->host_lock);
1712 lpfc_cmd->waitq = NULL;
1713 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001714
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001715 if (lpfc_cmd->pCmd == cmnd) {
1716 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04001717 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1718 "0748 abort handler timed out waiting "
1719 "for abort to complete: ret %#x, ID %d, "
1720 "LUN %d, snum %#lx\n",
1721 ret, cmnd->device->id, cmnd->device->lun,
1722 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001723 }
1724
1725 out:
James Smarte8b62012007-08-02 11:10:09 -04001726 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1727 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
1728 "LUN %d snum %#lx\n", ret, cmnd->device->id,
1729 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001730 return ret;
dea31012005-04-17 16:05:31 -05001731}
1732
James Smart9bad7672008-12-04 22:39:02 -05001733/**
1734 * lpfc_device_reset_handler: eh_device_reset entry point of Scsi Host Template
1735 *data structure.
1736 * @cmnd: Pointer to scsi_cmnd data structure.
1737 *
1738 * This routine does a device reset by sending a TARGET_RESET task management
1739 * command.
1740 *
1741 * Return code :
1742 * 0x2003 - Error
1743 * 0ex2002 - Success
1744 **/
dea31012005-04-17 16:05:31 -05001745static int
James Smart7054a602007-04-25 09:52:34 -04001746lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001747{
James Smart2e0fef82007-06-17 19:56:36 -05001748 struct Scsi_Host *shost = cmnd->device->host;
1749 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1750 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001751 struct lpfc_scsi_buf *lpfc_cmd;
1752 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05001753 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1754 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart915caaa2008-06-14 22:52:38 -04001755 unsigned long later;
1756 int ret = SUCCESS;
1757 int status;
1758 int cnt;
James Smartea2151b2008-09-07 11:52:10 -04001759 struct lpfc_scsi_event_header scsi_event;
dea31012005-04-17 16:05:31 -05001760
James Smarta90f5682006-08-17 11:58:04 -04001761 lpfc_block_error_handler(cmnd);
dea31012005-04-17 16:05:31 -05001762 /*
1763 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001764 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001765 */
James Smart915caaa2008-06-14 22:52:38 -04001766 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1767 while (time_after(later, jiffies)) {
James Smart58da1ff2008-04-07 10:15:56 -04001768 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smart915caaa2008-06-14 22:52:38 -04001769 return FAILED;
James Smartf5603512006-12-02 13:35:43 -05001770 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001771 break;
James Smart915caaa2008-06-14 22:52:38 -04001772 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1773 rdata = cmnd->device->hostdata;
1774 if (!rdata)
1775 break;
1776 pnode = rdata->pnode;
dea31012005-04-17 16:05:31 -05001777 }
James Smartea2151b2008-09-07 11:52:10 -04001778
1779 scsi_event.event_type = FC_REG_SCSI_EVENT;
1780 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
1781 scsi_event.lun = 0;
1782 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
1783 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
1784
1785 fc_host_post_vendor_event(shost,
1786 fc_get_event_number(),
1787 sizeof(scsi_event),
1788 (char *)&scsi_event,
James Smartddcc50f2008-12-04 22:38:46 -05001789 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04001790
James Smart915caaa2008-06-14 22:52:38 -04001791 if (!rdata || pnode->nlp_state != NLP_STE_MAPPED_NODE) {
1792 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1793 "0721 LUN Reset rport "
1794 "failure: msec x%x rdata x%p\n",
1795 jiffies_to_msecs(jiffies - later), rdata);
1796 return FAILED;
1797 }
James Smart2e0fef82007-06-17 19:56:36 -05001798 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001799 if (lpfc_cmd == NULL)
James Smart915caaa2008-06-14 22:52:38 -04001800 return FAILED;
dea31012005-04-17 16:05:31 -05001801 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04001802 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05001803
James Smart915caaa2008-06-14 22:52:38 -04001804 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd,
1805 cmnd->device->lun,
1806 FCP_TARGET_RESET);
1807 if (!status) {
1808 lpfc_release_scsi_buf(phba, lpfc_cmd);
1809 return FAILED;
1810 }
dea31012005-04-17 16:05:31 -05001811 iocbq = &lpfc_cmd->cur_iocbq;
1812
1813 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001814 iocbqrsp = lpfc_sli_get_iocbq(phba);
James Smart915caaa2008-06-14 22:52:38 -04001815 if (iocbqrsp == NULL) {
1816 lpfc_release_scsi_buf(phba, lpfc_cmd);
1817 return FAILED;
1818 }
James Smarte8b62012007-08-02 11:10:09 -04001819 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1820 "0703 Issue target reset to TGT %d LUN %d "
1821 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
1822 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
James Smart915caaa2008-06-14 22:52:38 -04001823 status = lpfc_sli_issue_iocb_wait(phba,
1824 &phba->sli.ring[phba->sli.fcp_ring],
1825 iocbq, iocbqrsp, lpfc_cmd->timeout);
1826 if (status == IOCB_TIMEDOUT) {
James Smart7054a602007-04-25 09:52:34 -04001827 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smart915caaa2008-06-14 22:52:38 -04001828 ret = TIMEOUT_ERROR;
1829 } else {
1830 if (status != IOCB_SUCCESS)
1831 ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001832 lpfc_release_scsi_buf(phba, lpfc_cmd);
1833 }
James Smarte8b62012007-08-02 11:10:09 -04001834 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1835 "0713 SCSI layer issued device reset (%d, %d) "
1836 "return x%x status x%x result x%x\n",
1837 cmnd->device->id, cmnd->device->lun, ret,
James Smart915caaa2008-06-14 22:52:38 -04001838 iocbqrsp->iocb.ulpStatus,
1839 iocbqrsp->iocb.un.ulpWord[4]);
1840 lpfc_sli_release_iocbq(phba, iocbqrsp);
1841 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
1842 LPFC_CTX_TGT);
1843 if (cnt)
1844 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1845 cmnd->device->id, cmnd->device->lun,
1846 LPFC_CTX_TGT);
1847 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1848 while (time_after(later, jiffies) && cnt) {
1849 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
1850 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
1851 cmnd->device->lun, LPFC_CTX_TGT);
1852 }
1853 if (cnt) {
1854 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1855 "0719 device reset I/O flush failure: "
1856 "cnt x%x\n", cnt);
1857 ret = FAILED;
1858 }
dea31012005-04-17 16:05:31 -05001859 return ret;
1860}
1861
James Smart9bad7672008-12-04 22:39:02 -05001862/**
1863 * lpfc_bus_reset_handler: eh_bus_reset_handler entry point of Scsi Host
1864 * Template data structure.
1865 * @cmnd: Pointer to scsi_cmnd data structure.
1866 *
1867 * This routine does target reset to all target on @cmnd->device->host.
1868 *
1869 * Return Code:
1870 * 0x2003 - Error
1871 * 0x2002 - Success
1872 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04001873static int
James Smart7054a602007-04-25 09:52:34 -04001874lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001875{
James Smart2e0fef82007-06-17 19:56:36 -05001876 struct Scsi_Host *shost = cmnd->device->host;
1877 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1878 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001879 struct lpfc_nodelist *ndlp = NULL;
1880 int match;
James Smartd7c255b2008-08-24 21:50:00 -04001881 int ret = SUCCESS, status = SUCCESS, i;
James Smart915caaa2008-06-14 22:52:38 -04001882 int cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001883 struct lpfc_scsi_buf * lpfc_cmd;
James Smart915caaa2008-06-14 22:52:38 -04001884 unsigned long later;
James Smartea2151b2008-09-07 11:52:10 -04001885 struct lpfc_scsi_event_header scsi_event;
1886
1887 scsi_event.event_type = FC_REG_SCSI_EVENT;
1888 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
1889 scsi_event.lun = 0;
1890 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
1891 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
1892
1893 fc_host_post_vendor_event(shost,
1894 fc_get_event_number(),
1895 sizeof(scsi_event),
1896 (char *)&scsi_event,
James Smartddcc50f2008-12-04 22:38:46 -05001897 LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05001898
James Smarta90f5682006-08-17 11:58:04 -04001899 lpfc_block_error_handler(cmnd);
dea31012005-04-17 16:05:31 -05001900 /*
1901 * Since the driver manages a single bus device, reset all
1902 * targets known to the driver. Should any target reset
1903 * fail, this routine returns failure to the midlayer.
1904 */
James Smarte17da182006-07-06 15:49:25 -04001905 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04001906 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05001907 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001908 spin_lock_irq(shost->host_lock);
1909 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001910 if (!NLP_CHK_NODE_ACT(ndlp))
1911 continue;
James Smart685f0bf2007-04-25 09:53:08 -04001912 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04001913 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04001914 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001915 match = 1;
1916 break;
1917 }
1918 }
James Smart2e0fef82007-06-17 19:56:36 -05001919 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001920 if (!match)
1921 continue;
James Smart915caaa2008-06-14 22:52:38 -04001922 lpfc_cmd = lpfc_get_scsi_buf(phba);
1923 if (lpfc_cmd) {
1924 lpfc_cmd->timeout = 60;
1925 status = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1926 cmnd->device->lun,
1927 ndlp->rport->dd_data);
1928 if (status != TIMEOUT_ERROR)
1929 lpfc_release_scsi_buf(phba, lpfc_cmd);
1930 }
1931 if (!lpfc_cmd || status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04001932 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1933 "0700 Bus Reset on target %d failed\n",
1934 i);
James Smart915caaa2008-06-14 22:52:38 -04001935 ret = FAILED;
dea31012005-04-17 16:05:31 -05001936 }
1937 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001938 /*
1939 * All outstanding txcmplq I/Os should have been aborted by
1940 * the targets. Unfortunately, some targets do not abide by
1941 * this forcing the driver to double check.
1942 */
James Smart51ef4c22007-08-02 11:10:31 -04001943 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001944 if (cnt)
James Smart51ef4c22007-08-02 11:10:31 -04001945 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1946 0, 0, LPFC_CTX_HOST);
James Smart915caaa2008-06-14 22:52:38 -04001947 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1948 while (time_after(later, jiffies) && cnt) {
1949 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
James Smart51ef4c22007-08-02 11:10:31 -04001950 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001951 }
dea31012005-04-17 16:05:31 -05001952 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001953 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1954 "0715 Bus Reset I/O flush failure: "
1955 "cnt x%x left x%x\n", cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001956 ret = FAILED;
dea31012005-04-17 16:05:31 -05001957 }
James Smarte8b62012007-08-02 11:10:09 -04001958 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1959 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05001960 return ret;
1961}
1962
James Smart9bad7672008-12-04 22:39:02 -05001963/**
1964 * lpfc_slave_alloc: slave_alloc entry point of Scsi Host Template data
1965 * structure.
1966 * @sdev: Pointer to scsi_device.
1967 *
1968 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
1969 * globally available list of scsi buffers. This routine also makes sure scsi
1970 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
1971 * of scsi buffer exists for the lifetime of the driver.
1972 *
1973 * Return codes:
1974 * non-0 - Error
1975 * 0 - Success
1976 **/
dea31012005-04-17 16:05:31 -05001977static int
dea31012005-04-17 16:05:31 -05001978lpfc_slave_alloc(struct scsi_device *sdev)
1979{
James Smart2e0fef82007-06-17 19:56:36 -05001980 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1981 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001982 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001983 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001984 uint32_t total = 0, i;
1985 uint32_t num_to_alloc = 0;
1986 unsigned long flags;
dea31012005-04-17 16:05:31 -05001987
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001988 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001989 return -ENXIO;
1990
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001991 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001992
1993 /*
1994 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1995 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001996 * HBA limit conveyed to the midlayer via the host structure. The
1997 * formula accounts for the lun_queue_depth + error handlers + 1
1998 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001999 */
2000 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04002001 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05002002
2003 /* Allow some exchanges to be available always to complete discovery */
2004 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04002005 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2006 "0704 At limitation of %d preallocated "
2007 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05002008 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002009 /* Allow some exchanges to be available always to complete discovery */
2010 } else if (total + num_to_alloc >
2011 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04002012 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2013 "0705 Allocation request of %d "
2014 "command buffers will exceed max of %d. "
2015 "Reducing allocation request to %d.\n",
2016 num_to_alloc, phba->cfg_hba_queue_depth,
2017 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05002018 num_to_alloc = phba->cfg_hba_queue_depth - total;
2019 }
2020
2021 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05002022 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05002023 if (!scsi_buf) {
James Smarte8b62012007-08-02 11:10:09 -04002024 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2025 "0706 Failed to allocate "
2026 "command buffer\n");
dea31012005-04-17 16:05:31 -05002027 break;
2028 }
2029
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002030 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05002031 phba->total_scsi_bufs++;
2032 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002033 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05002034 }
2035 return 0;
2036}
2037
James Smart9bad7672008-12-04 22:39:02 -05002038/**
2039 * lpfc_slave_configure: slave_configure entry point of Scsi Host Templater data
2040 * structure.
2041 * @sdev: Pointer to scsi_device.
2042 *
2043 * This routine configures following items
2044 * - Tag command queuing support for @sdev if supported.
2045 * - Dev loss time out value of fc_rport.
2046 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
2047 *
2048 * Return codes:
2049 * 0 - Success
2050 **/
dea31012005-04-17 16:05:31 -05002051static int
2052lpfc_slave_configure(struct scsi_device *sdev)
2053{
James Smart2e0fef82007-06-17 19:56:36 -05002054 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
2055 struct lpfc_hba *phba = vport->phba;
2056 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05002057
2058 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04002059 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05002060 else
James Smart3de2a652007-08-02 11:09:59 -04002061 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05002062
2063 /*
2064 * Initialize the fc transport attributes for the target
2065 * containing this scsi device. Also note that the driver's
2066 * target pointer is stored in the starget_data for the
2067 * driver's sysfs entry point functions.
2068 */
James Smart3de2a652007-08-02 11:09:59 -04002069 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05002070
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002071 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2072 lpfc_sli_poll_fcp_ring(phba);
2073 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2074 lpfc_poll_rearm_timer(phba);
2075 }
2076
dea31012005-04-17 16:05:31 -05002077 return 0;
2078}
2079
James Smart9bad7672008-12-04 22:39:02 -05002080/**
2081 * lpfc_slave_destroy: slave_destroy entry point of SHT data structure.
2082 * @sdev: Pointer to scsi_device.
2083 *
2084 * This routine sets @sdev hostatdata filed to null.
2085 **/
dea31012005-04-17 16:05:31 -05002086static void
2087lpfc_slave_destroy(struct scsi_device *sdev)
2088{
2089 sdev->hostdata = NULL;
2090 return;
2091}
2092
James Smart92d7f7b2007-06-17 19:56:38 -05002093
dea31012005-04-17 16:05:31 -05002094struct scsi_host_template lpfc_template = {
2095 .module = THIS_MODULE,
2096 .name = LPFC_DRIVER_NAME,
2097 .info = lpfc_info,
2098 .queuecommand = lpfc_queuecommand,
2099 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04002100 .eh_device_reset_handler= lpfc_device_reset_handler,
2101 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05002102 .slave_alloc = lpfc_slave_alloc,
2103 .slave_configure = lpfc_slave_configure,
2104 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04002105 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05002106 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05002107 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05002108 .cmd_per_lun = LPFC_CMD_PER_LUN,
2109 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05002110 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04002111 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05002112};
James Smart3de2a652007-08-02 11:09:59 -04002113
2114struct scsi_host_template lpfc_vport_template = {
2115 .module = THIS_MODULE,
2116 .name = LPFC_DRIVER_NAME,
2117 .info = lpfc_info,
2118 .queuecommand = lpfc_queuecommand,
2119 .eh_abort_handler = lpfc_abort_handler,
2120 .eh_device_reset_handler= lpfc_device_reset_handler,
2121 .eh_bus_reset_handler = lpfc_bus_reset_handler,
2122 .slave_alloc = lpfc_slave_alloc,
2123 .slave_configure = lpfc_slave_configure,
2124 .slave_destroy = lpfc_slave_destroy,
2125 .scan_finished = lpfc_scan_finished,
2126 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05002127 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04002128 .cmd_per_lun = LPFC_CMD_PER_LUN,
2129 .use_clustering = ENABLE_CLUSTERING,
2130 .shost_attrs = lpfc_vport_attrs,
2131 .max_sectors = 0xFFFF,
2132};