blob: 5f697ace970664f79d54ae77a8bfdc605cebc0a1 [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
James Smart9f1e1b52008-12-04 22:39:40 -050069 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
70
James Smartea2151b2008-09-07 11:52:10 -040071 spin_lock_irqsave(shost->host_lock, flags);
72 if (!vport->stat_data_enabled ||
73 vport->stat_data_blocked ||
74 !pnode->lat_data ||
75 (phba->bucket_type == LPFC_NO_BUCKET)) {
76 spin_unlock_irqrestore(shost->host_lock, flags);
77 return;
78 }
James Smartea2151b2008-09-07 11:52:10 -040079
80 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
81 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
82 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -050083 /* check array subscript bounds */
84 if (i < 0)
85 i = 0;
86 else if (i >= LPFC_MAX_BUCKET_COUNT)
87 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -040088 } else {
89 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
90 if (latency <= (phba->bucket_base +
91 ((1<<i)*phba->bucket_step)))
92 break;
93 }
94
95 pnode->lat_data[i].cmd_count++;
96 spin_unlock_irqrestore(shost->host_lock, flags);
97}
98
James Smartea2151b2008-09-07 11:52:10 -040099/**
100 * lpfc_send_sdev_queuedepth_change_event: Posts a queuedepth change
101 * event.
102 * @phba: Pointer to HBA context object.
103 * @vport: Pointer to vport object.
104 * @ndlp: Pointer to FC node associated with the target.
105 * @lun: Lun number of the scsi device.
106 * @old_val: Old value of the queue depth.
107 * @new_val: New value of the queue depth.
108 *
109 * This function sends an event to the mgmt application indicating
110 * there is a change in the scsi device queue depth.
111 **/
112static void
113lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
114 struct lpfc_vport *vport,
115 struct lpfc_nodelist *ndlp,
116 uint32_t lun,
117 uint32_t old_val,
118 uint32_t new_val)
119{
120 struct lpfc_fast_path_event *fast_path_evt;
121 unsigned long flags;
122
123 fast_path_evt = lpfc_alloc_fast_evt(phba);
124 if (!fast_path_evt)
125 return;
126
127 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
128 FC_REG_SCSI_EVENT;
129 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
130 LPFC_EVENT_VARQUEDEPTH;
131
132 /* Report all luns with change in queue depth */
133 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
134 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
135 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
136 &ndlp->nlp_portname, sizeof(struct lpfc_name));
137 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
138 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
139 }
140
141 fast_path_evt->un.queue_depth_evt.oldval = old_val;
142 fast_path_evt->un.queue_depth_evt.newval = new_val;
143 fast_path_evt->vport = vport;
144
145 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
146 spin_lock_irqsave(&phba->hbalock, flags);
147 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
148 spin_unlock_irqrestore(&phba->hbalock, flags);
149 lpfc_worker_wake_up(phba);
150
151 return;
152}
153
James Smart9bad7672008-12-04 22:39:02 -0500154/**
James Smarteaf15d52008-12-04 22:39:29 -0500155 * lpfc_rampdown_queue_depth: Post RAMP_DOWN_QUEUE event to worker thread.
James Smart9bad7672008-12-04 22:39:02 -0500156 * @phba: The Hba for which this call is being executed.
157 *
158 * This routine is called when there is resource error in driver or firmware.
159 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
160 * posts at most 1 event each second. This routine wakes up worker thread of
161 * @phba to process WORKER_RAM_DOWN_EVENT event.
162 *
163 * This routine should be called with no lock held.
164 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500165void
James Smarteaf15d52008-12-04 22:39:29 -0500166lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500167{
168 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400169 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500170
171 spin_lock_irqsave(&phba->hbalock, flags);
172 atomic_inc(&phba->num_rsrc_err);
173 phba->last_rsrc_error_time = jiffies;
174
175 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
176 spin_unlock_irqrestore(&phba->hbalock, flags);
177 return;
178 }
179
180 phba->last_ramp_down_time = jiffies;
181
182 spin_unlock_irqrestore(&phba->hbalock, flags);
183
184 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400185 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
186 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500187 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500188 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
189
James Smart5e9d9b82008-06-14 22:52:53 -0400190 if (!evt_posted)
191 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500192 return;
193}
194
James Smart9bad7672008-12-04 22:39:02 -0500195/**
196 * lpfc_rampup_queue_depth: Post RAMP_UP_QUEUE event for worker thread.
197 * @phba: The Hba for which this call is being executed.
198 *
199 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
200 * post at most 1 event every 5 minute after last_ramp_up_time or
201 * last_rsrc_error_time. This routine wakes up worker thread of @phba
202 * to process WORKER_RAM_DOWN_EVENT event.
203 *
204 * This routine should be called with no lock held.
205 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500206static inline void
James Smart3de2a652007-08-02 11:09:59 -0400207lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smart92d7f7b2007-06-17 19:56:38 -0500208 struct scsi_device *sdev)
209{
210 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400211 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400212 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500213 atomic_inc(&phba->num_cmd_success);
214
James Smart3de2a652007-08-02 11:09:59 -0400215 if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500216 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500217 spin_lock_irqsave(&phba->hbalock, flags);
218 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
219 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
220 spin_unlock_irqrestore(&phba->hbalock, flags);
221 return;
222 }
James Smart92d7f7b2007-06-17 19:56:38 -0500223 phba->last_ramp_up_time = jiffies;
224 spin_unlock_irqrestore(&phba->hbalock, flags);
225
226 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400227 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
228 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500229 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500230 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
231
James Smart5e9d9b82008-06-14 22:52:53 -0400232 if (!evt_posted)
233 lpfc_worker_wake_up(phba);
234 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500235}
236
James Smart9bad7672008-12-04 22:39:02 -0500237/**
238 * lpfc_ramp_down_queue_handler: WORKER_RAMP_DOWN_QUEUE event handler.
239 * @phba: The Hba for which this call is being executed.
240 *
241 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
242 * thread.This routine reduces queue depth for all scsi device on each vport
243 * associated with @phba.
244 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500245void
246lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
247{
James Smart549e55c2007-08-02 11:09:51 -0400248 struct lpfc_vport **vports;
249 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500250 struct scsi_device *sdev;
James Smartea2151b2008-09-07 11:52:10 -0400251 unsigned long new_queue_depth, old_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500252 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400253 int i;
James Smartea2151b2008-09-07 11:52:10 -0400254 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500255
256 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
257 num_cmd_success = atomic_read(&phba->num_cmd_success);
258
James Smart549e55c2007-08-02 11:09:51 -0400259 vports = lpfc_create_vport_work_array(phba);
260 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500261 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400262 shost = lpfc_shost_from_vport(vports[i]);
263 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500264 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400265 sdev->queue_depth * num_rsrc_err /
266 (num_rsrc_err + num_cmd_success);
267 if (!new_queue_depth)
268 new_queue_depth = sdev->queue_depth - 1;
269 else
270 new_queue_depth = sdev->queue_depth -
271 new_queue_depth;
James Smartea2151b2008-09-07 11:52:10 -0400272 old_queue_depth = sdev->queue_depth;
James Smart549e55c2007-08-02 11:09:51 -0400273 if (sdev->ordered_tags)
274 scsi_adjust_queue_depth(sdev,
275 MSG_ORDERED_TAG,
276 new_queue_depth);
277 else
278 scsi_adjust_queue_depth(sdev,
279 MSG_SIMPLE_TAG,
280 new_queue_depth);
James Smartea2151b2008-09-07 11:52:10 -0400281 rdata = sdev->hostdata;
282 if (rdata)
283 lpfc_send_sdev_queuedepth_change_event(
284 phba, vports[i],
285 rdata->pnode,
286 sdev->lun, old_queue_depth,
287 new_queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400288 }
James Smart92d7f7b2007-06-17 19:56:38 -0500289 }
James Smart09372822008-01-11 01:52:54 -0500290 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500291 atomic_set(&phba->num_rsrc_err, 0);
292 atomic_set(&phba->num_cmd_success, 0);
293}
294
James Smart9bad7672008-12-04 22:39:02 -0500295/**
296 * lpfc_ramp_up_queue_handler: WORKER_RAMP_UP_QUEUE event handler.
297 * @phba: The Hba for which this call is being executed.
298 *
299 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
300 * thread.This routine increases queue depth for all scsi device on each vport
301 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
302 * num_cmd_success to zero.
303 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500304void
305lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
306{
James Smart549e55c2007-08-02 11:09:51 -0400307 struct lpfc_vport **vports;
308 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500309 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400310 int i;
James Smartea2151b2008-09-07 11:52:10 -0400311 struct lpfc_rport_data *rdata;
James Smart92d7f7b2007-06-17 19:56:38 -0500312
James Smart549e55c2007-08-02 11:09:51 -0400313 vports = lpfc_create_vport_work_array(phba);
314 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500315 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400316 shost = lpfc_shost_from_vport(vports[i]);
317 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400318 if (vports[i]->cfg_lun_queue_depth <=
319 sdev->queue_depth)
320 continue;
James Smart549e55c2007-08-02 11:09:51 -0400321 if (sdev->ordered_tags)
322 scsi_adjust_queue_depth(sdev,
323 MSG_ORDERED_TAG,
324 sdev->queue_depth+1);
325 else
326 scsi_adjust_queue_depth(sdev,
327 MSG_SIMPLE_TAG,
328 sdev->queue_depth+1);
James Smartea2151b2008-09-07 11:52:10 -0400329 rdata = sdev->hostdata;
330 if (rdata)
331 lpfc_send_sdev_queuedepth_change_event(
332 phba, vports[i],
333 rdata->pnode,
334 sdev->lun,
335 sdev->queue_depth - 1,
336 sdev->queue_depth);
James Smart549e55c2007-08-02 11:09:51 -0400337 }
James Smart92d7f7b2007-06-17 19:56:38 -0500338 }
James Smart09372822008-01-11 01:52:54 -0500339 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500340 atomic_set(&phba->num_rsrc_err, 0);
341 atomic_set(&phba->num_cmd_success, 0);
342}
343
James Smarta8e497d2008-08-24 21:50:11 -0400344/**
345 * lpfc_scsi_dev_block: set all scsi hosts to block state.
346 * @phba: Pointer to HBA context object.
347 *
348 * This function walks vport list and set each SCSI host to block state
349 * by invoking fc_remote_port_delete() routine. This function is invoked
350 * with EEH when device's PCI slot has been permanently disabled.
351 **/
352void
353lpfc_scsi_dev_block(struct lpfc_hba *phba)
354{
355 struct lpfc_vport **vports;
356 struct Scsi_Host *shost;
357 struct scsi_device *sdev;
358 struct fc_rport *rport;
359 int i;
360
361 vports = lpfc_create_vport_work_array(phba);
362 if (vports != NULL)
363 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
364 shost = lpfc_shost_from_vport(vports[i]);
365 shost_for_each_device(sdev, shost) {
366 rport = starget_to_rport(scsi_target(sdev));
367 fc_remote_port_delete(rport);
368 }
369 }
370 lpfc_destroy_vport_work_array(phba, vports);
371}
372
James Smart9bad7672008-12-04 22:39:02 -0500373/**
374 * lpfc_new_scsi_buf: Scsi buffer allocator.
375 * @vport: The virtual port for which this call being executed.
376 *
dea31012005-04-17 16:05:31 -0500377 * This routine allocates a scsi buffer, which contains all the necessary
378 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
379 * contains information to build the IOCB. The DMAable region contains
James Smart9bad7672008-12-04 22:39:02 -0500380 * memory for the FCP CMND, FCP RSP, and the initial BPL. In addition to
381 * allocating memory, the FCP CMND and FCP RSP BDEs are setup in the BPL
dea31012005-04-17 16:05:31 -0500382 * and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500383 *
384 * Return codes:
385 * NULL - Error
386 * Pointer to lpfc_scsi_buf data structure - Success
387 **/
dea31012005-04-17 16:05:31 -0500388static struct lpfc_scsi_buf *
James Smart2e0fef82007-06-17 19:56:36 -0500389lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500390{
James Smart2e0fef82007-06-17 19:56:36 -0500391 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500392 struct lpfc_scsi_buf *psb;
393 struct ulp_bde64 *bpl;
394 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400395 dma_addr_t pdma_phys_fcp_cmd;
396 dma_addr_t pdma_phys_fcp_rsp;
397 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500398 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500399
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200400 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
dea31012005-04-17 16:05:31 -0500401 if (!psb)
402 return NULL;
dea31012005-04-17 16:05:31 -0500403
404 /*
405 * Get memory from the pci pool to map the virt space to pci bus space
406 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
407 * struct fcp_rsp and the number of bde's necessary to support the
408 * sg_tablesize.
409 */
410 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
411 &psb->dma_handle);
412 if (!psb->data) {
413 kfree(psb);
414 return NULL;
415 }
416
417 /* Initialize virtual ptrs to dma_buf region. */
418 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
419
James Bottomley604a3e32005-10-29 10:28:33 -0500420 /* Allocate iotag for psb->cur_iocbq. */
421 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
422 if (iotag == 0) {
423 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
424 psb->data, psb->dma_handle);
425 kfree (psb);
426 return NULL;
427 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400428 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500429
dea31012005-04-17 16:05:31 -0500430 psb->fcp_cmnd = psb->data;
431 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
432 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
433 sizeof(struct fcp_rsp);
434
435 /* Initialize local short-hand pointers. */
436 bpl = psb->fcp_bpl;
James Smart34b02dc2008-08-24 21:49:55 -0400437 pdma_phys_fcp_cmd = psb->dma_handle;
438 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
439 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
440 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500441
442 /*
443 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
444 * list bdes. Initialize the first two and leave the rest for
445 * queuecommand.
446 */
James Smart34b02dc2008-08-24 21:49:55 -0400447 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
448 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
449 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
450 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
James Smart9f1e1b52008-12-04 22:39:40 -0500451 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500452
453 /* Setup the physical region for the FCP RSP */
James Smart34b02dc2008-08-24 21:49:55 -0400454 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
455 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
456 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
457 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
James Smart9f1e1b52008-12-04 22:39:40 -0500458 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
dea31012005-04-17 16:05:31 -0500459
460 /*
461 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
462 * initialize it with all known data now.
463 */
dea31012005-04-17 16:05:31 -0500464 iocb = &psb->cur_iocbq.iocb;
465 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
James Smart34b02dc2008-08-24 21:49:55 -0400466 if (phba->sli_rev == 3) {
467 /* fill in immediate fcp command BDE */
468 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
469 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
470 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
471 unsli3.fcp_ext.icd);
472 iocb->un.fcpi64.bdl.addrHigh = 0;
473 iocb->ulpBdeCount = 0;
474 iocb->ulpLe = 0;
475 /* fill in responce BDE */
476 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
477 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
478 sizeof(struct fcp_rsp);
479 iocb->unsli3.fcp_ext.rbde.addrLow =
480 putPaddrLow(pdma_phys_fcp_rsp);
481 iocb->unsli3.fcp_ext.rbde.addrHigh =
482 putPaddrHigh(pdma_phys_fcp_rsp);
483 } else {
484 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
485 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
486 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_bpl);
487 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_bpl);
488 iocb->ulpBdeCount = 1;
489 iocb->ulpLe = 1;
490 }
dea31012005-04-17 16:05:31 -0500491 iocb->ulpClass = CLASS3;
492
493 return psb;
494}
495
James Smart9bad7672008-12-04 22:39:02 -0500496/**
497 * lpfc_get_scsi_buf: Get a scsi buffer from lpfc_scsi_buf_list list of Hba.
498 * @phba: The Hba for which this call is being executed.
499 *
500 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
501 * and returns to caller.
502 *
503 * Return codes:
504 * NULL - Error
505 * Pointer to lpfc_scsi_buf - Success
506 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100507static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500508lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500509{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400510 struct lpfc_scsi_buf * lpfc_cmd = NULL;
511 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500512 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500513
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500514 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400515 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400516 if (lpfc_cmd) {
517 lpfc_cmd->seg_cnt = 0;
518 lpfc_cmd->nonsg_phys = 0;
519 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500520 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400521 return lpfc_cmd;
522}
523
James Smart9bad7672008-12-04 22:39:02 -0500524/**
525 * lpfc_release_scsi_buf: Return a scsi buffer back to hba lpfc_scsi_buf_list list.
526 * @phba: The Hba for which this call is being executed.
527 * @psb: The scsi buffer which is being released.
528 *
529 * This routine releases @psb scsi buffer by adding it to tail of @phba
530 * lpfc_scsi_buf_list list.
531 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400532static void
James Smart92d7f7b2007-06-17 19:56:38 -0500533lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400534{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500535 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500536
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500537 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400538 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500539 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500540 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500541}
542
James Smart9bad7672008-12-04 22:39:02 -0500543/**
544 * lpfc_scsi_prep_dma_buf: Routine to do DMA mapping for scsi buffer.
545 * @phba: The Hba for which this call is being executed.
546 * @lpfc_cmd: The scsi buffer which is going to be mapped.
547 *
548 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
549 * field of @lpfc_cmd. This routine scans through sg elements and format the
550 * bdea. This routine also initializes all IOCB fields which are dependent on
551 * scsi command request buffer.
552 *
553 * Return codes:
554 * 1 - Error
555 * 0 - Success
556 **/
dea31012005-04-17 16:05:31 -0500557static int
James Smart92d7f7b2007-06-17 19:56:38 -0500558lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500559{
560 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
561 struct scatterlist *sgel = NULL;
562 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
563 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
564 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400565 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -0500566 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -0400567 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500568 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500569
570 /*
571 * There are three possibilities here - use scatter-gather segment, use
572 * the single mapping, or neither. Start the lpfc command prep by
573 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
574 * data bde entry.
575 */
576 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700577 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500578 /*
579 * The driver stores the segment count returned from pci_map_sg
580 * because this a count of dma-mappings used to map the use_sg
581 * pages. They are not guaranteed to be the same for those
582 * architectures that implement an IOMMU.
583 */
dea31012005-04-17 16:05:31 -0500584
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700585 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
586 scsi_sg_count(scsi_cmnd), datadir);
587 if (unlikely(!nseg))
588 return 1;
589
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500590 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500591 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
592 printk(KERN_ERR "%s: Too many sg segments from "
593 "dma_map_sg. Config %d, seg_cnt %d",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700594 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -0500595 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500596 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500597 return 1;
598 }
599
600 /*
601 * The driver established a maximum scatter-gather segment count
602 * during probe that limits the number of sg elements in any
603 * single scsi command. Just run through the seg_cnt and format
604 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -0400605 * When using SLI-3 the driver will try to fit all the BDEs into
606 * the IOCB. If it can't then the BDEs get added to a BPL as it
607 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -0500608 */
James Smart34b02dc2008-08-24 21:49:55 -0400609 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -0500610 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -0400611 if (phba->sli_rev == 3 &&
612 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
613 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
614 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
615 data_bde->addrLow = putPaddrLow(physaddr);
616 data_bde->addrHigh = putPaddrHigh(physaddr);
617 data_bde++;
618 } else {
619 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
620 bpl->tus.f.bdeSize = sg_dma_len(sgel);
621 bpl->tus.w = le32_to_cpu(bpl->tus.w);
622 bpl->addrLow =
623 le32_to_cpu(putPaddrLow(physaddr));
624 bpl->addrHigh =
625 le32_to_cpu(putPaddrHigh(physaddr));
626 bpl++;
627 }
dea31012005-04-17 16:05:31 -0500628 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700629 }
dea31012005-04-17 16:05:31 -0500630
631 /*
632 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -0400633 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
634 * explicitly reinitialized and for SLI-3 the extended bde count is
635 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -0500636 */
James Smart34b02dc2008-08-24 21:49:55 -0400637 if (phba->sli_rev == 3) {
638 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
639 /*
640 * The extended IOCB format can only fit 3 BDE or a BPL.
641 * This I/O has more than 3 BDE so the 1st data bde will
642 * be a BPL that is filled in here.
643 */
644 physaddr = lpfc_cmd->dma_handle;
645 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
646 data_bde->tus.f.bdeSize = (num_bde *
647 sizeof(struct ulp_bde64));
648 physaddr += (sizeof(struct fcp_cmnd) +
649 sizeof(struct fcp_rsp) +
650 (2 * sizeof(struct ulp_bde64)));
651 data_bde->addrHigh = putPaddrHigh(physaddr);
652 data_bde->addrLow = putPaddrLow(physaddr);
653 /* ebde count includes the responce bde and data bpl */
654 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
655 } else {
656 /* ebde count includes the responce bde and data bdes */
657 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
658 }
659 } else {
660 iocb_cmd->un.fcpi64.bdl.bdeSize =
661 ((num_bde + 2) * sizeof(struct ulp_bde64));
662 }
James Smart09372822008-01-11 01:52:54 -0500663 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
dea31012005-04-17 16:05:31 -0500664 return 0;
665}
666
James Smartea2151b2008-09-07 11:52:10 -0400667/**
668 * lpfc_send_scsi_error_event: Posts an event when there is SCSI error.
669 * @phba: Pointer to hba context object.
670 * @vport: Pointer to vport object.
671 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
672 * @rsp_iocb: Pointer to response iocb object which reported error.
673 *
674 * This function posts an event when there is a SCSI command reporting
675 * error from the scsi device.
676 **/
677static void
678lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
679 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
680 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
681 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
682 uint32_t resp_info = fcprsp->rspStatus2;
683 uint32_t scsi_status = fcprsp->rspStatus3;
684 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
685 struct lpfc_fast_path_event *fast_path_evt = NULL;
686 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
687 unsigned long flags;
688
689 /* If there is queuefull or busy condition send a scsi event */
690 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
691 (cmnd->result == SAM_STAT_BUSY)) {
692 fast_path_evt = lpfc_alloc_fast_evt(phba);
693 if (!fast_path_evt)
694 return;
695 fast_path_evt->un.scsi_evt.event_type =
696 FC_REG_SCSI_EVENT;
697 fast_path_evt->un.scsi_evt.subcategory =
698 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
699 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
700 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
701 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
702 &pnode->nlp_portname, sizeof(struct lpfc_name));
703 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
704 &pnode->nlp_nodename, sizeof(struct lpfc_name));
705 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
706 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
707 fast_path_evt = lpfc_alloc_fast_evt(phba);
708 if (!fast_path_evt)
709 return;
710 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
711 FC_REG_SCSI_EVENT;
712 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
713 LPFC_EVENT_CHECK_COND;
714 fast_path_evt->un.check_cond_evt.scsi_event.lun =
715 cmnd->device->lun;
716 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
717 &pnode->nlp_portname, sizeof(struct lpfc_name));
718 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
719 &pnode->nlp_nodename, sizeof(struct lpfc_name));
720 fast_path_evt->un.check_cond_evt.sense_key =
721 cmnd->sense_buffer[2] & 0xf;
722 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
723 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
724 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
725 fcpi_parm &&
726 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
727 ((scsi_status == SAM_STAT_GOOD) &&
728 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
729 /*
730 * If status is good or resid does not match with fcp_param and
731 * there is valid fcpi_parm, then there is a read_check error
732 */
733 fast_path_evt = lpfc_alloc_fast_evt(phba);
734 if (!fast_path_evt)
735 return;
736 fast_path_evt->un.read_check_error.header.event_type =
737 FC_REG_FABRIC_EVENT;
738 fast_path_evt->un.read_check_error.header.subcategory =
739 LPFC_EVENT_FCPRDCHKERR;
740 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
741 &pnode->nlp_portname, sizeof(struct lpfc_name));
742 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
743 &pnode->nlp_nodename, sizeof(struct lpfc_name));
744 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
745 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
746 fast_path_evt->un.read_check_error.fcpiparam =
747 fcpi_parm;
748 } else
749 return;
750
751 fast_path_evt->vport = vport;
752 spin_lock_irqsave(&phba->hbalock, flags);
753 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
754 spin_unlock_irqrestore(&phba->hbalock, flags);
755 lpfc_worker_wake_up(phba);
756 return;
757}
James Smart9bad7672008-12-04 22:39:02 -0500758
759/**
760 * lpfc_scsi_unprep_dma_buf: Routine to un-map DMA mapping of scatter gather.
761 * @phba: The Hba for which this call is being executed.
762 * @psb: The scsi buffer which is going to be un-mapped.
763 *
764 * This routine does DMA un-mapping of scatter gather list of scsi command
765 * field of @lpfc_cmd.
766 **/
dea31012005-04-17 16:05:31 -0500767static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400768lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
769{
770 /*
771 * There are only two special cases to consider. (1) the scsi command
772 * requested scatter-gather usage or (2) the scsi command allocated
773 * a request buffer, but did not request use_sg. There is a third
774 * case, but it does not require resource deallocation.
775 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500776 if (psb->seg_cnt > 0)
777 scsi_dma_unmap(psb->pCmd);
James Smartbcf4dbf2006-07-06 15:50:08 -0400778}
779
James Smart9bad7672008-12-04 22:39:02 -0500780/**
781 * lpfc_handler_fcp_err: FCP response handler.
782 * @vport: The virtual port for which this call is being executed.
783 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
784 * @rsp_iocb: The response IOCB which contains FCP error.
785 *
786 * This routine is called to process response IOCB with status field
787 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
788 * based upon SCSI and FCP error.
789 **/
James Smartbcf4dbf2006-07-06 15:50:08 -0400790static void
James Smart2e0fef82007-06-17 19:56:36 -0500791lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
792 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -0500793{
794 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
795 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
796 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -0400797 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -0500798 uint32_t resp_info = fcprsp->rspStatus2;
799 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500800 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500801 uint32_t host_status = DID_OK;
802 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500803 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500804
James Smartea2151b2008-09-07 11:52:10 -0400805
dea31012005-04-17 16:05:31 -0500806 /*
807 * If this is a task management command, there is no
808 * scsi packet associated with this lpfc_cmd. The driver
809 * consumes it.
810 */
811 if (fcpcmd->fcpCntl2) {
812 scsi_status = 0;
813 goto out;
814 }
815
James Smartc7743952006-12-02 13:34:42 -0500816 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
817 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
818 if (snslen > SCSI_SENSE_BUFFERSIZE)
819 snslen = SCSI_SENSE_BUFFERSIZE;
820
821 if (resp_info & RSP_LEN_VALID)
822 rsplen = be32_to_cpu(fcprsp->rspRspLen);
823 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
824 }
825 lp = (uint32_t *)cmnd->sense_buffer;
826
827 if (!scsi_status && (resp_info & RESID_UNDER))
828 logit = LOG_FCP;
829
James Smarte8b62012007-08-02 11:10:09 -0400830 lpfc_printf_vlog(vport, KERN_WARNING, logit,
831 "0730 FCP command x%x failed: x%x SNS x%x x%x "
832 "Data: x%x x%x x%x x%x x%x\n",
833 cmnd->cmnd[0], scsi_status,
834 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
835 be32_to_cpu(fcprsp->rspResId),
836 be32_to_cpu(fcprsp->rspSnsLen),
837 be32_to_cpu(fcprsp->rspRspLen),
838 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -0500839
840 if (resp_info & RSP_LEN_VALID) {
841 rsplen = be32_to_cpu(fcprsp->rspRspLen);
842 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
843 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
844 host_status = DID_ERROR;
845 goto out;
846 }
847 }
848
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500849 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -0500850 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500851 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -0500852
James Smarte8b62012007-08-02 11:10:09 -0400853 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
854 "0716 FCP Read Underrun, expected %d, "
855 "residual %d Data: x%x x%x x%x\n",
856 be32_to_cpu(fcpcmd->fcpDl),
857 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
858 cmnd->underflow);
dea31012005-04-17 16:05:31 -0500859
860 /*
James Smart7054a602007-04-25 09:52:34 -0400861 * If there is an under run check if under run reported by
862 * storage array is same as the under run reported by HBA.
863 * If this is not same, there is a dropped frame.
864 */
865 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
866 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500867 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -0400868 lpfc_printf_vlog(vport, KERN_WARNING,
869 LOG_FCP | LOG_FCP_ERROR,
870 "0735 FCP Read Check Error "
871 "and Underrun Data: x%x x%x x%x x%x\n",
872 be32_to_cpu(fcpcmd->fcpDl),
873 scsi_get_resid(cmnd), fcpi_parm,
874 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500875 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -0400876 host_status = DID_ERROR;
877 }
878 /*
dea31012005-04-17 16:05:31 -0500879 * The cmnd->underflow is the minimum number of bytes that must
880 * be transfered for this command. Provided a sense condition
881 * is not present, make sure the actual amount transferred is at
882 * least the underflow value or fail.
883 */
884 if (!(resp_info & SNS_LEN_VALID) &&
885 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500886 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
887 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -0400888 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
889 "0717 FCP command x%x residual "
890 "underrun converted to error "
891 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -0400892 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -0400893 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -0500894 host_status = DID_ERROR;
895 }
896 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -0400897 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
898 "0720 FCP command x%x residual overrun error. "
899 "Data: x%x x%x \n", cmnd->cmnd[0],
900 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -0500901 host_status = DID_ERROR;
902
903 /*
904 * Check SLI validation that all the transfer was actually done
905 * (fcpi_parm should be zero). Apply check only to reads.
906 */
907 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
908 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -0400909 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
910 "0734 FCP Read Check Error Data: "
911 "x%x x%x x%x x%x\n",
912 be32_to_cpu(fcpcmd->fcpDl),
913 be32_to_cpu(fcprsp->rspResId),
914 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -0500915 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500916 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -0500917 }
918
919 out:
920 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -0400921 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -0500922}
923
James Smart9bad7672008-12-04 22:39:02 -0500924/**
925 * lpfc_scsi_cmd_iocb_cmpl: Scsi cmnd IOCB completion routine.
926 * @phba: The Hba for which this call is being executed.
927 * @pIocbIn: The command IOCBQ for the scsi cmnd.
928 * @pIocbOut: The response IOCBQ for the scsi cmnd .
929 *
930 * This routine assigns scsi command result by looking into response IOCB
931 * status field appropriately. This routine handles QUEUE FULL condition as
932 * well by ramping down device queue depth.
933 **/
dea31012005-04-17 16:05:31 -0500934static void
935lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
936 struct lpfc_iocbq *pIocbOut)
937{
938 struct lpfc_scsi_buf *lpfc_cmd =
939 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500940 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500941 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
942 struct lpfc_nodelist *pnode = rdata->pnode;
943 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500944 int result;
945 struct scsi_device *sdev, *tmp_sdev;
946 int depth = 0;
James Smartfa61a542008-01-11 01:52:42 -0500947 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -0400948 struct lpfc_fast_path_event *fast_path_evt;
dea31012005-04-17 16:05:31 -0500949
950 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
951 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart109f6ed2008-12-04 22:39:08 -0500952 if (pnode && NLP_CHK_NODE_ACT(pnode))
953 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -0500954
955 if (lpfc_cmd->status) {
956 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
957 (lpfc_cmd->result & IOERR_DRVR_MASK))
958 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
959 else if (lpfc_cmd->status >= IOSTAT_CNT)
960 lpfc_cmd->status = IOSTAT_DEFAULT;
961
James Smarte8b62012007-08-02 11:10:09 -0400962 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
963 "0729 FCP cmd x%x failed <%d/%d> "
964 "status: x%x result: x%x Data: x%x x%x\n",
965 cmd->cmnd[0],
966 cmd->device ? cmd->device->id : 0xffff,
967 cmd->device ? cmd->device->lun : 0xffff,
968 lpfc_cmd->status, lpfc_cmd->result,
969 pIocbOut->iocb.ulpContext,
970 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -0500971
972 switch (lpfc_cmd->status) {
973 case IOSTAT_FCP_RSP_ERROR:
974 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -0500975 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -0500976 break;
977 case IOSTAT_NPORT_BSY:
978 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -0400979 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -0400980 fast_path_evt = lpfc_alloc_fast_evt(phba);
981 if (!fast_path_evt)
982 break;
983 fast_path_evt->un.fabric_evt.event_type =
984 FC_REG_FABRIC_EVENT;
985 fast_path_evt->un.fabric_evt.subcategory =
986 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
987 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
988 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
989 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
990 &pnode->nlp_portname,
991 sizeof(struct lpfc_name));
992 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
993 &pnode->nlp_nodename,
994 sizeof(struct lpfc_name));
995 }
996 fast_path_evt->vport = vport;
997 fast_path_evt->work_evt.evt =
998 LPFC_EVT_FASTPATH_MGMT_EVT;
999 spin_lock_irqsave(&phba->hbalock, flags);
1000 list_add_tail(&fast_path_evt->work_evt.evt_listp,
1001 &phba->work_list);
1002 spin_unlock_irqrestore(&phba->hbalock, flags);
1003 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001004 break;
James Smart92d7f7b2007-06-17 19:56:38 -05001005 case IOSTAT_LOCAL_REJECT:
James Smartd7c255b2008-08-24 21:50:00 -04001006 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05001007 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartd7c255b2008-08-24 21:50:00 -04001008 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
James Smart92d7f7b2007-06-17 19:56:38 -05001009 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04001010 break;
1011 } /* else: fall through */
dea31012005-04-17 16:05:31 -05001012 default:
1013 cmd->result = ScsiResult(DID_ERROR, 0);
1014 break;
1015 }
1016
James Smart58da1ff2008-04-07 10:15:56 -04001017 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001018 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04001019 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
1020 SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -05001021 } else {
1022 cmd->result = ScsiResult(DID_OK, 0);
1023 }
1024
1025 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
1026 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
1027
James Smarte8b62012007-08-02 11:10:09 -04001028 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1029 "0710 Iodone <%d/%d> cmd %p, error "
1030 "x%x SNS x%x x%x Data: x%x x%x\n",
1031 cmd->device->id, cmd->device->lun, cmd,
1032 cmd->result, *lp, *(lp + 3), cmd->retries,
1033 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05001034 }
1035
James Smartea2151b2008-09-07 11:52:10 -04001036 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001037 result = cmd->result;
1038 sdev = cmd->device;
James Smart977b5a02008-09-07 11:52:04 -04001039 if (vport->cfg_max_scsicmpl_time &&
1040 time_after(jiffies, lpfc_cmd->start_time +
1041 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
1042 spin_lock_irqsave(sdev->host->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05001043 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
1044 if (pnode->cmd_qdepth >
1045 atomic_read(&pnode->cmd_pending) &&
1046 (atomic_read(&pnode->cmd_pending) >
1047 LPFC_MIN_TGT_QDEPTH) &&
1048 ((cmd->cmnd[0] == READ_10) ||
1049 (cmd->cmnd[0] == WRITE_10)))
1050 pnode->cmd_qdepth =
1051 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04001052
James Smart109f6ed2008-12-04 22:39:08 -05001053 pnode->last_change_time = jiffies;
1054 }
James Smart977b5a02008-09-07 11:52:04 -04001055 spin_unlock_irqrestore(sdev->host->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05001056 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
1057 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
James Smart977b5a02008-09-07 11:52:04 -04001058 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05001059 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
1060 spin_lock_irqsave(sdev->host->host_lock, flags);
1061 pnode->cmd_qdepth += pnode->cmd_qdepth *
1062 LPFC_TGTQ_RAMPUP_PCENT / 100;
1063 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
1064 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
1065 pnode->last_change_time = jiffies;
1066 spin_unlock_irqrestore(sdev->host->host_lock, flags);
1067 }
James Smart977b5a02008-09-07 11:52:04 -04001068 }
1069
James Smart1dcb58e2007-04-25 09:51:30 -04001070 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001071 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001072
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001073 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05001074 /*
1075 * If there is a thread waiting for command completion
1076 * wake up the thread.
1077 */
1078 spin_lock_irqsave(sdev->host->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04001079 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05001080 if (lpfc_cmd->waitq)
1081 wake_up(lpfc_cmd->waitq);
1082 spin_unlock_irqrestore(sdev->host->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05001083 lpfc_release_scsi_buf(phba, lpfc_cmd);
1084 return;
1085 }
1086
James Smart92d7f7b2007-06-17 19:56:38 -05001087
1088 if (!result)
James Smart3de2a652007-08-02 11:09:59 -04001089 lpfc_rampup_queue_depth(vport, sdev);
James Smart92d7f7b2007-06-17 19:56:38 -05001090
James Smart58da1ff2008-04-07 10:15:56 -04001091 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001092 ((jiffies - pnode->last_ramp_up_time) >
1093 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
1094 ((jiffies - pnode->last_q_full_time) >
1095 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
James Smart3de2a652007-08-02 11:09:59 -04001096 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001097 shost_for_each_device(tmp_sdev, sdev->host) {
James Smart3de2a652007-08-02 11:09:59 -04001098 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001099 if (tmp_sdev->id != sdev->id)
1100 continue;
1101 if (tmp_sdev->ordered_tags)
1102 scsi_adjust_queue_depth(tmp_sdev,
1103 MSG_ORDERED_TAG,
1104 tmp_sdev->queue_depth+1);
1105 else
1106 scsi_adjust_queue_depth(tmp_sdev,
1107 MSG_SIMPLE_TAG,
1108 tmp_sdev->queue_depth+1);
1109
1110 pnode->last_ramp_up_time = jiffies;
1111 }
1112 }
James Smartea2151b2008-09-07 11:52:10 -04001113 lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
1114 0xFFFFFFFF,
1115 sdev->queue_depth - 1, sdev->queue_depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001116 }
1117
1118 /*
1119 * Check for queue full. If the lun is reporting queue full, then
1120 * back off the lun queue depth to prevent target overloads.
1121 */
James Smart58da1ff2008-04-07 10:15:56 -04001122 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
1123 NLP_CHK_NODE_ACT(pnode)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001124 pnode->last_q_full_time = jiffies;
1125
1126 shost_for_each_device(tmp_sdev, sdev->host) {
1127 if (tmp_sdev->id != sdev->id)
1128 continue;
1129 depth = scsi_track_queue_full(tmp_sdev,
1130 tmp_sdev->queue_depth - 1);
1131 }
1132 /*
James Smart2e0fef82007-06-17 19:56:36 -05001133 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001134 * Modify the returned error code to store
1135 * the final depth value set by
1136 * scsi_track_queue_full.
1137 */
1138 if (depth == -1)
1139 depth = sdev->host->cmd_per_lun;
1140
1141 if (depth) {
James Smarte8b62012007-08-02 11:10:09 -04001142 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1143 "0711 detected queue full - lun queue "
1144 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04001145 lpfc_send_sdev_queuedepth_change_event(phba, vport,
1146 pnode, 0xFFFFFFFF,
1147 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05001148 }
1149 }
1150
James Smartfa61a542008-01-11 01:52:42 -05001151 /*
1152 * If there is a thread waiting for command completion
1153 * wake up the thread.
1154 */
1155 spin_lock_irqsave(sdev->host->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04001156 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05001157 if (lpfc_cmd->waitq)
1158 wake_up(lpfc_cmd->waitq);
1159 spin_unlock_irqrestore(sdev->host->host_lock, flags);
1160
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001161 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001162}
1163
James Smart34b02dc2008-08-24 21:49:55 -04001164/**
1165 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB.
1166 * @data: A pointer to the immediate command data portion of the IOCB.
1167 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
1168 *
1169 * The routine copies the entire FCP command from @fcp_cmnd to @data while
1170 * byte swapping the data to big endian format for transmission on the wire.
1171 **/
1172static void
1173lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
1174{
1175 int i, j;
1176 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
1177 i += sizeof(uint32_t), j++) {
1178 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
1179 }
1180}
1181
James Smart9bad7672008-12-04 22:39:02 -05001182/**
1183 * lpfc_scsi_prep_cmnd: Routine to convert scsi cmnd to FCP information unit.
1184 * @vport: The virtual port for which this call is being executed.
1185 * @lpfc_cmd: The scsi command which needs to send.
1186 * @pnode: Pointer to lpfc_nodelist.
1187 *
1188 * This routine initializes fcp_cmnd and iocb data structure from scsi command
1189 * to transfer.
1190 **/
dea31012005-04-17 16:05:31 -05001191static void
James Smart2e0fef82007-06-17 19:56:36 -05001192lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
1193 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05001194{
James Smart2e0fef82007-06-17 19:56:36 -05001195 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001196 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1197 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1198 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1199 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
1200 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04001201 char tag[2];
dea31012005-04-17 16:05:31 -05001202
James Smart58da1ff2008-04-07 10:15:56 -04001203 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
1204 return;
1205
dea31012005-04-17 16:05:31 -05001206 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04001207 /* clear task management bits */
1208 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05001209
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04001210 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
1211 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05001212
1213 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
1214
James Smart7e2b19f2007-10-29 11:00:39 -04001215 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
1216 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05001217 case HEAD_OF_QUEUE_TAG:
1218 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
1219 break;
1220 case ORDERED_QUEUE_TAG:
1221 fcp_cmnd->fcpCntl1 = ORDERED_Q;
1222 break;
1223 default:
1224 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
1225 break;
1226 }
1227 } else
1228 fcp_cmnd->fcpCntl1 = 0;
1229
1230 /*
1231 * There are three possibilities here - use scatter-gather segment, use
1232 * the single mapping, or neither. Start the lpfc command prep by
1233 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1234 * data bde entry.
1235 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001236 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001237 if (datadir == DMA_TO_DEVICE) {
1238 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
1239 iocb_cmd->un.fcpi.fcpi_parm = 0;
1240 iocb_cmd->ulpPU = 0;
1241 fcp_cmnd->fcpCntl3 = WRITE_DATA;
1242 phba->fc4OutputRequests++;
1243 } else {
1244 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
1245 iocb_cmd->ulpPU = PARM_READ_CHECK;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001246 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001247 fcp_cmnd->fcpCntl3 = READ_DATA;
1248 phba->fc4InputRequests++;
1249 }
1250 } else {
1251 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
1252 iocb_cmd->un.fcpi.fcpi_parm = 0;
1253 iocb_cmd->ulpPU = 0;
1254 fcp_cmnd->fcpCntl3 = 0;
1255 phba->fc4ControlRequests++;
1256 }
James Smart34b02dc2008-08-24 21:49:55 -04001257 if (phba->sli_rev == 3)
1258 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05001259 /*
1260 * Finish initializing those IOCB fields that are independent
1261 * of the scsi_cmnd request_buffer
1262 */
1263 piocbq->iocb.ulpContext = pnode->nlp_rpi;
1264 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
1265 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05001266 else
1267 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05001268
1269 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
1270 piocbq->context1 = lpfc_cmd;
1271 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
1272 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05001273 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05001274}
1275
James Smart9bad7672008-12-04 22:39:02 -05001276/**
1277 * lpfc_scsi_prep_task_mgmt_cmnd: Convert scsi TM cmnd to FCP information unit.
1278 * @vport: The virtual port for which this call is being executed.
1279 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
1280 * @lun: Logical unit number.
1281 * @task_mgmt_cmd: SCSI task management command.
1282 *
1283 * This routine creates FCP information unit corresponding to @task_mgmt_cmd.
1284 *
1285 * Return codes:
1286 * 0 - Error
1287 * 1 - Success
1288 **/
dea31012005-04-17 16:05:31 -05001289static int
James Smart2e0fef82007-06-17 19:56:36 -05001290lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05001291 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04001292 unsigned int lun,
dea31012005-04-17 16:05:31 -05001293 uint8_t task_mgmt_cmd)
1294{
dea31012005-04-17 16:05:31 -05001295 struct lpfc_iocbq *piocbq;
1296 IOCB_t *piocb;
1297 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04001298 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05001299 struct lpfc_nodelist *ndlp = rdata->pnode;
1300
James Smart58da1ff2008-04-07 10:15:56 -04001301 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1302 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001303 return 0;
dea31012005-04-17 16:05:31 -05001304
dea31012005-04-17 16:05:31 -05001305 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05001306 piocbq->vport = vport;
1307
dea31012005-04-17 16:05:31 -05001308 piocb = &piocbq->iocb;
1309
1310 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04001311 /* Clear out any old data in the FCP command area */
1312 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1313 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05001314 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smart34b02dc2008-08-24 21:49:55 -04001315 if (vport->phba->sli_rev == 3)
1316 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05001317 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05001318 piocb->ulpContext = ndlp->nlp_rpi;
1319 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
1320 piocb->ulpFCP2Rcvy = 1;
1321 }
1322 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
1323
1324 /* ulpTimeout is only one byte */
1325 if (lpfc_cmd->timeout > 0xff) {
1326 /*
1327 * Do not timeout the command at the firmware level.
1328 * The driver will provide the timeout mechanism.
1329 */
1330 piocb->ulpTimeout = 0;
1331 } else {
1332 piocb->ulpTimeout = lpfc_cmd->timeout;
1333 }
1334
James Smart2e0fef82007-06-17 19:56:36 -05001335 return 1;
dea31012005-04-17 16:05:31 -05001336}
1337
James Smart9bad7672008-12-04 22:39:02 -05001338/**
1339 * lpc_taskmgmt_def_cmpl: IOCB completion routine for task management command.
1340 * @phba: The Hba for which this call is being executed.
1341 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
1342 * @rspiocbq: Pointer to lpfc_iocbq data structure.
1343 *
1344 * This routine is IOCB completion routine for device reset and target reset
1345 * routine. This routine release scsi buffer associated with lpfc_cmd.
1346 **/
James Smart7054a602007-04-25 09:52:34 -04001347static void
1348lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
1349 struct lpfc_iocbq *cmdiocbq,
1350 struct lpfc_iocbq *rspiocbq)
1351{
1352 struct lpfc_scsi_buf *lpfc_cmd =
1353 (struct lpfc_scsi_buf *) cmdiocbq->context1;
1354 if (lpfc_cmd)
1355 lpfc_release_scsi_buf(phba, lpfc_cmd);
1356 return;
1357}
1358
James Smart9bad7672008-12-04 22:39:02 -05001359/**
1360 * lpfc_scsi_tgt_reset: Target reset handler.
1361 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure
1362 * @vport: The virtual port for which this call is being executed.
1363 * @tgt_id: Target ID.
1364 * @lun: Lun number.
1365 * @rdata: Pointer to lpfc_rport_data.
1366 *
1367 * This routine issues a TARGET RESET iocb to reset a target with @tgt_id ID.
1368 *
1369 * Return Code:
1370 * 0x2003 - Error
1371 * 0x2002 - Success.
1372 **/
dea31012005-04-17 16:05:31 -05001373static int
James Smart2e0fef82007-06-17 19:56:36 -05001374lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -04001375 unsigned tgt_id, unsigned int lun,
1376 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -05001377{
James Smart2e0fef82007-06-17 19:56:36 -05001378 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001379 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001380 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -05001381 int ret;
James Smart915caaa2008-06-14 22:52:38 -04001382 int status;
dea31012005-04-17 16:05:31 -05001383
James Smart58da1ff2008-04-07 10:15:56 -04001384 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartf5603512006-12-02 13:35:43 -05001385 return FAILED;
1386
James Smart0b18ac42006-05-01 21:50:40 -04001387 lpfc_cmd->rdata = rdata;
James Smart915caaa2008-06-14 22:52:38 -04001388 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -04001389 FCP_TARGET_RESET);
James Smart915caaa2008-06-14 22:52:38 -04001390 if (!status)
dea31012005-04-17 16:05:31 -05001391 return FAILED;
1392
dea31012005-04-17 16:05:31 -05001393 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001394 iocbqrsp = lpfc_sli_get_iocbq(phba);
1395
dea31012005-04-17 16:05:31 -05001396 if (!iocbqrsp)
1397 return FAILED;
dea31012005-04-17 16:05:31 -05001398
James Smart0b18ac42006-05-01 21:50:40 -04001399 /* Issue Target Reset to TGT <num> */
James Smarte8b62012007-08-02 11:10:09 -04001400 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1401 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
1402 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
James Smart915caaa2008-06-14 22:52:38 -04001403 status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001404 &phba->sli.ring[phba->sli.fcp_ring],
1405 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart915caaa2008-06-14 22:52:38 -04001406 if (status != IOCB_SUCCESS) {
1407 if (status == IOCB_TIMEDOUT) {
James Smart7054a602007-04-25 09:52:34 -04001408 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smart915caaa2008-06-14 22:52:38 -04001409 ret = TIMEOUT_ERROR;
1410 } else
1411 ret = FAILED;
dea31012005-04-17 16:05:31 -05001412 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -05001413 } else {
1414 ret = SUCCESS;
1415 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
1416 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
1417 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
1418 (lpfc_cmd->result & IOERR_DRVR_MASK))
1419 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
1420 }
1421
James Bottomley604a3e32005-10-29 10:28:33 -05001422 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05001423 return ret;
1424}
1425
James Smart9bad7672008-12-04 22:39:02 -05001426/**
1427 * lpfc_info: Info entry point of scsi_host_template data structure.
1428 * @host: The scsi host for which this call is being executed.
1429 *
1430 * This routine provides module information about hba.
1431 *
1432 * Reutrn code:
1433 * Pointer to char - Success.
1434 **/
dea31012005-04-17 16:05:31 -05001435const char *
1436lpfc_info(struct Scsi_Host *host)
1437{
James Smart2e0fef82007-06-17 19:56:36 -05001438 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
1439 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001440 int len;
1441 static char lpfcinfobuf[384];
1442
1443 memset(lpfcinfobuf,0,384);
1444 if (phba && phba->pcidev){
1445 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
1446 len = strlen(lpfcinfobuf);
1447 snprintf(lpfcinfobuf + len,
1448 384-len,
1449 " on PCI bus %02x device %02x irq %d",
1450 phba->pcidev->bus->number,
1451 phba->pcidev->devfn,
1452 phba->pcidev->irq);
1453 len = strlen(lpfcinfobuf);
1454 if (phba->Port[0]) {
1455 snprintf(lpfcinfobuf + len,
1456 384-len,
1457 " port %s",
1458 phba->Port);
1459 }
1460 }
1461 return lpfcinfobuf;
1462}
1463
James Smart9bad7672008-12-04 22:39:02 -05001464/**
1465 * lpfc_poll_rearm_time: Routine to modify fcp_poll timer of hba.
1466 * @phba: The Hba for which this call is being executed.
1467 *
1468 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
1469 * The default value of cfg_poll_tmo is 10 milliseconds.
1470 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001471static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
1472{
1473 unsigned long poll_tmo_expires =
1474 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
1475
1476 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
1477 mod_timer(&phba->fcp_poll_timer,
1478 poll_tmo_expires);
1479}
1480
James Smart9bad7672008-12-04 22:39:02 -05001481/**
1482 * lpfc_poll_start_timer: Routine to start fcp_poll_timer of HBA.
1483 * @phba: The Hba for which this call is being executed.
1484 *
1485 * This routine starts the fcp_poll_timer of @phba.
1486 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001487void lpfc_poll_start_timer(struct lpfc_hba * phba)
1488{
1489 lpfc_poll_rearm_timer(phba);
1490}
1491
James Smart9bad7672008-12-04 22:39:02 -05001492/**
1493 * lpfc_poll_timeout: Restart polling timer.
1494 * @ptr: Map to lpfc_hba data structure pointer.
1495 *
1496 * This routine restarts fcp_poll timer, when FCP ring polling is enable
1497 * and FCP Ring interrupt is disable.
1498 **/
1499
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001500void lpfc_poll_timeout(unsigned long ptr)
1501{
James Smart2e0fef82007-06-17 19:56:36 -05001502 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001503
1504 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1505 lpfc_sli_poll_fcp_ring (phba);
1506 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1507 lpfc_poll_rearm_timer(phba);
1508 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001509}
1510
James Smart9bad7672008-12-04 22:39:02 -05001511/**
1512 * lpfc_queuecommand: Queuecommand entry point of Scsi Host Templater data
1513 * structure.
1514 * @cmnd: Pointer to scsi_cmnd data structure.
1515 * @done: Pointer to done routine.
1516 *
1517 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
1518 * This routine prepares an IOCB from scsi command and provides to firmware.
1519 * The @done callback is invoked after driver finished processing the command.
1520 *
1521 * Return value :
1522 * 0 - Success
1523 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
1524 **/
dea31012005-04-17 16:05:31 -05001525static int
1526lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
1527{
James Smart2e0fef82007-06-17 19:56:36 -05001528 struct Scsi_Host *shost = cmnd->device->host;
1529 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1530 struct lpfc_hba *phba = vport->phba;
1531 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001532 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1533 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001534 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001535 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001536 int err;
dea31012005-04-17 16:05:31 -05001537
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001538 err = fc_remote_port_chkready(rport);
1539 if (err) {
1540 cmnd->result = err;
dea31012005-04-17 16:05:31 -05001541 goto out_fail_command;
1542 }
1543
1544 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001545 * Catch race where our node has transitioned, but the
1546 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05001547 */
James Smartb522d7d2008-09-07 11:51:56 -04001548 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1549 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
1550 goto out_fail_command;
1551 }
James Smart109f6ed2008-12-04 22:39:08 -05001552 if (vport->cfg_max_scsicmpl_time &&
1553 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
James Smart977b5a02008-09-07 11:52:04 -04001554 goto out_host_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05001555
James Smarted957682007-06-17 19:56:37 -05001556 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001557 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05001558 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001559
James Smarte8b62012007-08-02 11:10:09 -04001560 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1561 "0707 driver's buffer pool is empty, "
1562 "IO busied\n");
dea31012005-04-17 16:05:31 -05001563 goto out_host_busy;
1564 }
1565
1566 /*
1567 * Store the midlayer's command structure for the completion phase
1568 * and complete the command initialization.
1569 */
1570 lpfc_cmd->pCmd = cmnd;
1571 lpfc_cmd->rdata = rdata;
1572 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04001573 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05001574 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
1575 cmnd->scsi_done = done;
1576
1577 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1578 if (err)
1579 goto out_host_busy_free_buf;
1580
James Smart2e0fef82007-06-17 19:56:36 -05001581 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05001582
James Smart977b5a02008-09-07 11:52:04 -04001583 atomic_inc(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05001584 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
James Smart92d7f7b2007-06-17 19:56:38 -05001585 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05001586 if (err) {
1587 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05001588 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05001589 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001590 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1591 lpfc_sli_poll_fcp_ring(phba);
1592 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1593 lpfc_poll_rearm_timer(phba);
1594 }
1595
dea31012005-04-17 16:05:31 -05001596 return 0;
1597
1598 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04001599 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001600 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001601 out_host_busy:
1602 return SCSI_MLQUEUE_HOST_BUSY;
1603
1604 out_fail_command:
1605 done(cmnd);
1606 return 0;
1607}
1608
James Smart9bad7672008-12-04 22:39:02 -05001609/**
1610 * lpfc_block_error_handler: Routine to block error handler.
1611 * @cmnd: Pointer to scsi_cmnd data structure.
1612 *
1613 * This routine blocks execution till fc_rport state is not FC_PORSTAT_BLCOEKD.
1614 **/
James Smarta90f5682006-08-17 11:58:04 -04001615static void
1616lpfc_block_error_handler(struct scsi_cmnd *cmnd)
1617{
1618 struct Scsi_Host *shost = cmnd->device->host;
1619 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1620
1621 spin_lock_irq(shost->host_lock);
1622 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1623 spin_unlock_irq(shost->host_lock);
1624 msleep(1000);
1625 spin_lock_irq(shost->host_lock);
1626 }
1627 spin_unlock_irq(shost->host_lock);
1628 return;
1629}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001630
James Smart9bad7672008-12-04 22:39:02 -05001631/**
1632 * lpfc_abort_handler: Eh_abort_handler entry point of Scsi Host Template data
1633 *structure.
1634 * @cmnd: Pointer to scsi_cmnd data structure.
1635 *
1636 * This routine aborts @cmnd pending in base driver.
1637 *
1638 * Return code :
1639 * 0x2003 - Error
1640 * 0x2002 - Success
1641 **/
dea31012005-04-17 16:05:31 -05001642static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001643lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001644{
James Smart2e0fef82007-06-17 19:56:36 -05001645 struct Scsi_Host *shost = cmnd->device->host;
1646 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1647 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001648 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001649 struct lpfc_iocbq *iocb;
1650 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05001651 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05001652 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001653 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05001654 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001655
James Smarta90f5682006-08-17 11:58:04 -04001656 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001657 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1658 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05001659
1660 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001661 * If pCmd field of the corresponding lpfc_scsi_buf structure
1662 * points to a different SCSI command, then the driver has
1663 * already completed this command, but the midlayer did not
1664 * see the completion before the eh fired. Just return
1665 * SUCCESS.
dea31012005-04-17 16:05:31 -05001666 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001667 iocb = &lpfc_cmd->cur_iocbq;
1668 if (lpfc_cmd->pCmd != cmnd)
1669 goto out;
dea31012005-04-17 16:05:31 -05001670
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001671 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05001672
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001673 abtsiocb = lpfc_sli_get_iocbq(phba);
1674 if (abtsiocb == NULL) {
1675 ret = FAILED;
dea31012005-04-17 16:05:31 -05001676 goto out;
1677 }
1678
dea31012005-04-17 16:05:31 -05001679 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001680 * The scsi command can not be in txq and it is in flight because the
1681 * pCmd is still pointig at the SCSI command we have to abort. There
1682 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05001683 */
dea31012005-04-17 16:05:31 -05001684
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001685 cmd = &iocb->iocb;
1686 icmd = &abtsiocb->iocb;
1687 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1688 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1689 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05001690
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001691 icmd->ulpLe = 1;
1692 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05001693 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001694 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1695 else
1696 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05001697
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001698 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05001699 abtsiocb->vport = vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001700 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1701 lpfc_sli_release_iocbq(phba, abtsiocb);
1702 ret = FAILED;
1703 goto out;
1704 }
1705
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001706 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1707 lpfc_sli_poll_fcp_ring (phba);
1708
James Smartfa61a542008-01-11 01:52:42 -05001709 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001710 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05001711 wait_event_timeout(waitq,
1712 (lpfc_cmd->pCmd != cmnd),
1713 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001714
James Smartfa61a542008-01-11 01:52:42 -05001715 spin_lock_irq(shost->host_lock);
1716 lpfc_cmd->waitq = NULL;
1717 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001718
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001719 if (lpfc_cmd->pCmd == cmnd) {
1720 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04001721 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1722 "0748 abort handler timed out waiting "
1723 "for abort to complete: ret %#x, ID %d, "
1724 "LUN %d, snum %#lx\n",
1725 ret, cmnd->device->id, cmnd->device->lun,
1726 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001727 }
1728
1729 out:
James Smarte8b62012007-08-02 11:10:09 -04001730 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1731 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
1732 "LUN %d snum %#lx\n", ret, cmnd->device->id,
1733 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001734 return ret;
dea31012005-04-17 16:05:31 -05001735}
1736
James Smart9bad7672008-12-04 22:39:02 -05001737/**
1738 * lpfc_device_reset_handler: eh_device_reset entry point of Scsi Host Template
1739 *data structure.
1740 * @cmnd: Pointer to scsi_cmnd data structure.
1741 *
1742 * This routine does a device reset by sending a TARGET_RESET task management
1743 * command.
1744 *
1745 * Return code :
1746 * 0x2003 - Error
1747 * 0ex2002 - Success
1748 **/
dea31012005-04-17 16:05:31 -05001749static int
James Smart7054a602007-04-25 09:52:34 -04001750lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001751{
James Smart2e0fef82007-06-17 19:56:36 -05001752 struct Scsi_Host *shost = cmnd->device->host;
1753 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1754 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001755 struct lpfc_scsi_buf *lpfc_cmd;
1756 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05001757 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1758 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart915caaa2008-06-14 22:52:38 -04001759 unsigned long later;
1760 int ret = SUCCESS;
1761 int status;
1762 int cnt;
James Smartea2151b2008-09-07 11:52:10 -04001763 struct lpfc_scsi_event_header scsi_event;
dea31012005-04-17 16:05:31 -05001764
James Smarta90f5682006-08-17 11:58:04 -04001765 lpfc_block_error_handler(cmnd);
dea31012005-04-17 16:05:31 -05001766 /*
1767 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001768 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001769 */
James Smart915caaa2008-06-14 22:52:38 -04001770 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1771 while (time_after(later, jiffies)) {
James Smart58da1ff2008-04-07 10:15:56 -04001772 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smart915caaa2008-06-14 22:52:38 -04001773 return FAILED;
James Smartf5603512006-12-02 13:35:43 -05001774 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001775 break;
James Smart915caaa2008-06-14 22:52:38 -04001776 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1777 rdata = cmnd->device->hostdata;
1778 if (!rdata)
1779 break;
1780 pnode = rdata->pnode;
dea31012005-04-17 16:05:31 -05001781 }
James Smartea2151b2008-09-07 11:52:10 -04001782
1783 scsi_event.event_type = FC_REG_SCSI_EVENT;
1784 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
1785 scsi_event.lun = 0;
1786 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
1787 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
1788
1789 fc_host_post_vendor_event(shost,
1790 fc_get_event_number(),
1791 sizeof(scsi_event),
1792 (char *)&scsi_event,
James Smartddcc50f2008-12-04 22:38:46 -05001793 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04001794
James Smart915caaa2008-06-14 22:52:38 -04001795 if (!rdata || pnode->nlp_state != NLP_STE_MAPPED_NODE) {
1796 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1797 "0721 LUN Reset rport "
1798 "failure: msec x%x rdata x%p\n",
1799 jiffies_to_msecs(jiffies - later), rdata);
1800 return FAILED;
1801 }
James Smart2e0fef82007-06-17 19:56:36 -05001802 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001803 if (lpfc_cmd == NULL)
James Smart915caaa2008-06-14 22:52:38 -04001804 return FAILED;
dea31012005-04-17 16:05:31 -05001805 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04001806 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05001807
James Smart915caaa2008-06-14 22:52:38 -04001808 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd,
1809 cmnd->device->lun,
1810 FCP_TARGET_RESET);
1811 if (!status) {
1812 lpfc_release_scsi_buf(phba, lpfc_cmd);
1813 return FAILED;
1814 }
dea31012005-04-17 16:05:31 -05001815 iocbq = &lpfc_cmd->cur_iocbq;
1816
1817 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001818 iocbqrsp = lpfc_sli_get_iocbq(phba);
James Smart915caaa2008-06-14 22:52:38 -04001819 if (iocbqrsp == NULL) {
1820 lpfc_release_scsi_buf(phba, lpfc_cmd);
1821 return FAILED;
1822 }
James Smarte8b62012007-08-02 11:10:09 -04001823 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1824 "0703 Issue target reset to TGT %d LUN %d "
1825 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
1826 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
James Smart915caaa2008-06-14 22:52:38 -04001827 status = lpfc_sli_issue_iocb_wait(phba,
1828 &phba->sli.ring[phba->sli.fcp_ring],
1829 iocbq, iocbqrsp, lpfc_cmd->timeout);
1830 if (status == IOCB_TIMEDOUT) {
James Smart7054a602007-04-25 09:52:34 -04001831 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smart915caaa2008-06-14 22:52:38 -04001832 ret = TIMEOUT_ERROR;
1833 } else {
1834 if (status != IOCB_SUCCESS)
1835 ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001836 lpfc_release_scsi_buf(phba, lpfc_cmd);
1837 }
James Smarte8b62012007-08-02 11:10:09 -04001838 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1839 "0713 SCSI layer issued device reset (%d, %d) "
1840 "return x%x status x%x result x%x\n",
1841 cmnd->device->id, cmnd->device->lun, ret,
James Smart915caaa2008-06-14 22:52:38 -04001842 iocbqrsp->iocb.ulpStatus,
1843 iocbqrsp->iocb.un.ulpWord[4]);
1844 lpfc_sli_release_iocbq(phba, iocbqrsp);
1845 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
1846 LPFC_CTX_TGT);
1847 if (cnt)
1848 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1849 cmnd->device->id, cmnd->device->lun,
1850 LPFC_CTX_TGT);
1851 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1852 while (time_after(later, jiffies) && cnt) {
1853 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
1854 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
1855 cmnd->device->lun, LPFC_CTX_TGT);
1856 }
1857 if (cnt) {
1858 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1859 "0719 device reset I/O flush failure: "
1860 "cnt x%x\n", cnt);
1861 ret = FAILED;
1862 }
dea31012005-04-17 16:05:31 -05001863 return ret;
1864}
1865
James Smart9bad7672008-12-04 22:39:02 -05001866/**
1867 * lpfc_bus_reset_handler: eh_bus_reset_handler entry point of Scsi Host
1868 * Template data structure.
1869 * @cmnd: Pointer to scsi_cmnd data structure.
1870 *
1871 * This routine does target reset to all target on @cmnd->device->host.
1872 *
1873 * Return Code:
1874 * 0x2003 - Error
1875 * 0x2002 - Success
1876 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04001877static int
James Smart7054a602007-04-25 09:52:34 -04001878lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001879{
James Smart2e0fef82007-06-17 19:56:36 -05001880 struct Scsi_Host *shost = cmnd->device->host;
1881 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1882 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001883 struct lpfc_nodelist *ndlp = NULL;
1884 int match;
James Smartd7c255b2008-08-24 21:50:00 -04001885 int ret = SUCCESS, status = SUCCESS, i;
James Smart915caaa2008-06-14 22:52:38 -04001886 int cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001887 struct lpfc_scsi_buf * lpfc_cmd;
James Smart915caaa2008-06-14 22:52:38 -04001888 unsigned long later;
James Smartea2151b2008-09-07 11:52:10 -04001889 struct lpfc_scsi_event_header scsi_event;
1890
1891 scsi_event.event_type = FC_REG_SCSI_EVENT;
1892 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
1893 scsi_event.lun = 0;
1894 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
1895 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
1896
1897 fc_host_post_vendor_event(shost,
1898 fc_get_event_number(),
1899 sizeof(scsi_event),
1900 (char *)&scsi_event,
James Smartddcc50f2008-12-04 22:38:46 -05001901 LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05001902
James Smarta90f5682006-08-17 11:58:04 -04001903 lpfc_block_error_handler(cmnd);
dea31012005-04-17 16:05:31 -05001904 /*
1905 * Since the driver manages a single bus device, reset all
1906 * targets known to the driver. Should any target reset
1907 * fail, this routine returns failure to the midlayer.
1908 */
James Smarte17da182006-07-06 15:49:25 -04001909 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04001910 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05001911 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001912 spin_lock_irq(shost->host_lock);
1913 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001914 if (!NLP_CHK_NODE_ACT(ndlp))
1915 continue;
James Smart685f0bf2007-04-25 09:53:08 -04001916 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04001917 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04001918 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001919 match = 1;
1920 break;
1921 }
1922 }
James Smart2e0fef82007-06-17 19:56:36 -05001923 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001924 if (!match)
1925 continue;
James Smart915caaa2008-06-14 22:52:38 -04001926 lpfc_cmd = lpfc_get_scsi_buf(phba);
1927 if (lpfc_cmd) {
1928 lpfc_cmd->timeout = 60;
1929 status = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1930 cmnd->device->lun,
1931 ndlp->rport->dd_data);
1932 if (status != TIMEOUT_ERROR)
1933 lpfc_release_scsi_buf(phba, lpfc_cmd);
1934 }
1935 if (!lpfc_cmd || status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04001936 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1937 "0700 Bus Reset on target %d failed\n",
1938 i);
James Smart915caaa2008-06-14 22:52:38 -04001939 ret = FAILED;
dea31012005-04-17 16:05:31 -05001940 }
1941 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001942 /*
1943 * All outstanding txcmplq I/Os should have been aborted by
1944 * the targets. Unfortunately, some targets do not abide by
1945 * this forcing the driver to double check.
1946 */
James Smart51ef4c22007-08-02 11:10:31 -04001947 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001948 if (cnt)
James Smart51ef4c22007-08-02 11:10:31 -04001949 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1950 0, 0, LPFC_CTX_HOST);
James Smart915caaa2008-06-14 22:52:38 -04001951 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1952 while (time_after(later, jiffies) && cnt) {
1953 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
James Smart51ef4c22007-08-02 11:10:31 -04001954 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001955 }
dea31012005-04-17 16:05:31 -05001956 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001957 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1958 "0715 Bus Reset I/O flush failure: "
1959 "cnt x%x left x%x\n", cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001960 ret = FAILED;
dea31012005-04-17 16:05:31 -05001961 }
James Smarte8b62012007-08-02 11:10:09 -04001962 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1963 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05001964 return ret;
1965}
1966
James Smart9bad7672008-12-04 22:39:02 -05001967/**
1968 * lpfc_slave_alloc: slave_alloc entry point of Scsi Host Template data
1969 * structure.
1970 * @sdev: Pointer to scsi_device.
1971 *
1972 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
1973 * globally available list of scsi buffers. This routine also makes sure scsi
1974 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
1975 * of scsi buffer exists for the lifetime of the driver.
1976 *
1977 * Return codes:
1978 * non-0 - Error
1979 * 0 - Success
1980 **/
dea31012005-04-17 16:05:31 -05001981static int
dea31012005-04-17 16:05:31 -05001982lpfc_slave_alloc(struct scsi_device *sdev)
1983{
James Smart2e0fef82007-06-17 19:56:36 -05001984 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1985 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001986 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001987 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001988 uint32_t total = 0, i;
1989 uint32_t num_to_alloc = 0;
1990 unsigned long flags;
dea31012005-04-17 16:05:31 -05001991
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001992 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001993 return -ENXIO;
1994
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001995 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001996
1997 /*
1998 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1999 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04002000 * HBA limit conveyed to the midlayer via the host structure. The
2001 * formula accounts for the lun_queue_depth + error handlers + 1
2002 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05002003 */
2004 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04002005 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05002006
2007 /* Allow some exchanges to be available always to complete discovery */
2008 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04002009 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2010 "0704 At limitation of %d preallocated "
2011 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05002012 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05002013 /* Allow some exchanges to be available always to complete discovery */
2014 } else if (total + num_to_alloc >
2015 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04002016 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2017 "0705 Allocation request of %d "
2018 "command buffers will exceed max of %d. "
2019 "Reducing allocation request to %d.\n",
2020 num_to_alloc, phba->cfg_hba_queue_depth,
2021 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05002022 num_to_alloc = phba->cfg_hba_queue_depth - total;
2023 }
2024
2025 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05002026 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05002027 if (!scsi_buf) {
James Smarte8b62012007-08-02 11:10:09 -04002028 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2029 "0706 Failed to allocate "
2030 "command buffer\n");
dea31012005-04-17 16:05:31 -05002031 break;
2032 }
2033
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002034 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05002035 phba->total_scsi_bufs++;
2036 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002037 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05002038 }
2039 return 0;
2040}
2041
James Smart9bad7672008-12-04 22:39:02 -05002042/**
2043 * lpfc_slave_configure: slave_configure entry point of Scsi Host Templater data
2044 * structure.
2045 * @sdev: Pointer to scsi_device.
2046 *
2047 * This routine configures following items
2048 * - Tag command queuing support for @sdev if supported.
2049 * - Dev loss time out value of fc_rport.
2050 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
2051 *
2052 * Return codes:
2053 * 0 - Success
2054 **/
dea31012005-04-17 16:05:31 -05002055static int
2056lpfc_slave_configure(struct scsi_device *sdev)
2057{
James Smart2e0fef82007-06-17 19:56:36 -05002058 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
2059 struct lpfc_hba *phba = vport->phba;
2060 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05002061
2062 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04002063 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05002064 else
James Smart3de2a652007-08-02 11:09:59 -04002065 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05002066
2067 /*
2068 * Initialize the fc transport attributes for the target
2069 * containing this scsi device. Also note that the driver's
2070 * target pointer is stored in the starget_data for the
2071 * driver's sysfs entry point functions.
2072 */
James Smart3de2a652007-08-02 11:09:59 -04002073 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05002074
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002075 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2076 lpfc_sli_poll_fcp_ring(phba);
2077 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2078 lpfc_poll_rearm_timer(phba);
2079 }
2080
dea31012005-04-17 16:05:31 -05002081 return 0;
2082}
2083
James Smart9bad7672008-12-04 22:39:02 -05002084/**
2085 * lpfc_slave_destroy: slave_destroy entry point of SHT data structure.
2086 * @sdev: Pointer to scsi_device.
2087 *
2088 * This routine sets @sdev hostatdata filed to null.
2089 **/
dea31012005-04-17 16:05:31 -05002090static void
2091lpfc_slave_destroy(struct scsi_device *sdev)
2092{
2093 sdev->hostdata = NULL;
2094 return;
2095}
2096
James Smart92d7f7b2007-06-17 19:56:38 -05002097
dea31012005-04-17 16:05:31 -05002098struct scsi_host_template lpfc_template = {
2099 .module = THIS_MODULE,
2100 .name = LPFC_DRIVER_NAME,
2101 .info = lpfc_info,
2102 .queuecommand = lpfc_queuecommand,
2103 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04002104 .eh_device_reset_handler= lpfc_device_reset_handler,
2105 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05002106 .slave_alloc = lpfc_slave_alloc,
2107 .slave_configure = lpfc_slave_configure,
2108 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04002109 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05002110 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05002111 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05002112 .cmd_per_lun = LPFC_CMD_PER_LUN,
2113 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05002114 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04002115 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05002116};
James Smart3de2a652007-08-02 11:09:59 -04002117
2118struct scsi_host_template lpfc_vport_template = {
2119 .module = THIS_MODULE,
2120 .name = LPFC_DRIVER_NAME,
2121 .info = lpfc_info,
2122 .queuecommand = lpfc_queuecommand,
2123 .eh_abort_handler = lpfc_abort_handler,
2124 .eh_device_reset_handler= lpfc_device_reset_handler,
2125 .eh_bus_reset_handler = lpfc_bus_reset_handler,
2126 .slave_alloc = lpfc_slave_alloc,
2127 .slave_configure = lpfc_slave_configure,
2128 .slave_destroy = lpfc_slave_destroy,
2129 .scan_finished = lpfc_scan_finished,
2130 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05002131 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04002132 .cmd_per_lun = LPFC_CMD_PER_LUN,
2133 .use_clustering = ENABLE_CLUSTERING,
2134 .shost_attrs = lpfc_vport_attrs,
2135 .max_sectors = 0xFFFF,
2136};