blob: 3a658953486cf9a1e73027c9be828a0c12be5e41 [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 Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 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 *******************************************************************/
dea31012005-04-17 16:05:31 -050021#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050023#include <linux/interrupt.h>
James Smarta90f5682006-08-17 11:58:04 -040024#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050025#include <asm/unaligned.h>
dea31012005-04-17 16:05:31 -050026
27#include <scsi/scsi.h>
28#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050029#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050030#include <scsi/scsi_host.h>
31#include <scsi/scsi_tcq.h>
32#include <scsi/scsi_transport_fc.h>
33
34#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040035#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050036#include "lpfc_hw.h"
37#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040039#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_disc.h"
41#include "lpfc_scsi.h"
42#include "lpfc.h"
43#include "lpfc_logmsg.h"
44#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050045#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050046
47#define LPFC_RESET_WAIT 2
48#define LPFC_ABORT_WAIT 2
49
James Smarte2a0a9d2008-12-04 22:40:02 -050050int _dump_buf_done;
51
52static char *dif_op_str[] = {
53 "SCSI_PROT_NORMAL",
54 "SCSI_PROT_READ_INSERT",
55 "SCSI_PROT_WRITE_STRIP",
56 "SCSI_PROT_READ_STRIP",
57 "SCSI_PROT_WRITE_INSERT",
58 "SCSI_PROT_READ_PASS",
59 "SCSI_PROT_WRITE_PASS",
James Smarte2a0a9d2008-12-04 22:40:02 -050060};
James Smartda0436e2009-05-22 14:51:39 -040061static void
62lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050063static void
64lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050065
66static void
James Smart6a9c52c2009-10-02 15:16:51 -040067lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050068{
69 void *src, *dst;
70 struct scatterlist *sgde = scsi_sglist(cmnd);
71
72 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040073 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
74 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050075 __func__);
76 return;
77 }
78
79
80 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040081 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
82 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050083 return;
84 }
85
86 dst = (void *) _dump_buf_data;
87 while (sgde) {
88 src = sg_virt(sgde);
89 memcpy(dst, src, sgde->length);
90 dst += sgde->length;
91 sgde = sg_next(sgde);
92 }
93}
94
95static void
James Smart6a9c52c2009-10-02 15:16:51 -040096lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050097{
98 void *src, *dst;
99 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
100
101 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400102 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
103 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500104 __func__);
105 return;
106 }
107
108 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400109 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
110 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500111 return;
112 }
113
114 dst = _dump_buf_dif;
115 while (sgde) {
116 src = sg_virt(sgde);
117 memcpy(dst, src, sgde->length);
118 dst += sgde->length;
119 sgde = sg_next(sgde);
120 }
121}
122
James Smartea2151b2008-09-07 11:52:10 -0400123/**
James Smartf1126682009-06-10 17:22:44 -0400124 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
125 * @phba: Pointer to HBA object.
126 * @lpfc_cmd: lpfc scsi command object pointer.
127 *
128 * This function is called from the lpfc_prep_task_mgmt_cmd function to
129 * set the last bit in the response sge entry.
130 **/
131static void
132lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
133 struct lpfc_scsi_buf *lpfc_cmd)
134{
135 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
136 if (sgl) {
137 sgl += 1;
138 sgl->word2 = le32_to_cpu(sgl->word2);
139 bf_set(lpfc_sli4_sge_last, sgl, 1);
140 sgl->word2 = cpu_to_le32(sgl->word2);
141 }
142}
143
144/**
James Smart3621a712009-04-06 18:47:14 -0400145 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400146 * @phba: Pointer to HBA object.
147 * @lpfc_cmd: lpfc scsi command object pointer.
148 *
149 * This function is called when there is a command completion and this
150 * function updates the statistical data for the command completion.
151 **/
152static void
153lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
154{
155 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
156 struct lpfc_nodelist *pnode = rdata->pnode;
157 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
158 unsigned long flags;
159 struct Scsi_Host *shost = cmd->device->host;
160 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
161 unsigned long latency;
162 int i;
163
164 if (cmd->result)
165 return;
166
James Smart9f1e1b52008-12-04 22:39:40 -0500167 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
168
James Smartea2151b2008-09-07 11:52:10 -0400169 spin_lock_irqsave(shost->host_lock, flags);
170 if (!vport->stat_data_enabled ||
171 vport->stat_data_blocked ||
172 !pnode->lat_data ||
173 (phba->bucket_type == LPFC_NO_BUCKET)) {
174 spin_unlock_irqrestore(shost->host_lock, flags);
175 return;
176 }
James Smartea2151b2008-09-07 11:52:10 -0400177
178 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
179 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
180 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500181 /* check array subscript bounds */
182 if (i < 0)
183 i = 0;
184 else if (i >= LPFC_MAX_BUCKET_COUNT)
185 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400186 } else {
187 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
188 if (latency <= (phba->bucket_base +
189 ((1<<i)*phba->bucket_step)))
190 break;
191 }
192
193 pnode->lat_data[i].cmd_count++;
194 spin_unlock_irqrestore(shost->host_lock, flags);
195}
196
James Smartea2151b2008-09-07 11:52:10 -0400197/**
James Smart3621a712009-04-06 18:47:14 -0400198 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400199 * @phba: Pointer to HBA context object.
200 * @vport: Pointer to vport object.
201 * @ndlp: Pointer to FC node associated with the target.
202 * @lun: Lun number of the scsi device.
203 * @old_val: Old value of the queue depth.
204 * @new_val: New value of the queue depth.
205 *
206 * This function sends an event to the mgmt application indicating
207 * there is a change in the scsi device queue depth.
208 **/
209static void
210lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
211 struct lpfc_vport *vport,
212 struct lpfc_nodelist *ndlp,
213 uint32_t lun,
214 uint32_t old_val,
215 uint32_t new_val)
216{
217 struct lpfc_fast_path_event *fast_path_evt;
218 unsigned long flags;
219
220 fast_path_evt = lpfc_alloc_fast_evt(phba);
221 if (!fast_path_evt)
222 return;
223
224 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
225 FC_REG_SCSI_EVENT;
226 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
227 LPFC_EVENT_VARQUEDEPTH;
228
229 /* Report all luns with change in queue depth */
230 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
231 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
232 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
233 &ndlp->nlp_portname, sizeof(struct lpfc_name));
234 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
235 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
236 }
237
238 fast_path_evt->un.queue_depth_evt.oldval = old_val;
239 fast_path_evt->un.queue_depth_evt.newval = new_val;
240 fast_path_evt->vport = vport;
241
242 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
243 spin_lock_irqsave(&phba->hbalock, flags);
244 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
245 spin_unlock_irqrestore(&phba->hbalock, flags);
246 lpfc_worker_wake_up(phba);
247
248 return;
249}
250
James Smart9bad7672008-12-04 22:39:02 -0500251/**
James Smart5ffc2662009-11-18 15:39:44 -0500252 * lpfc_change_queue_depth - Alter scsi device queue depth
253 * @sdev: Pointer the scsi device on which to change the queue depth.
254 * @qdepth: New queue depth to set the sdev to.
255 * @reason: The reason for the queue depth change.
256 *
257 * This function is called by the midlayer and the LLD to alter the queue
258 * depth for a scsi device. This function sets the queue depth to the new
259 * value and sends an event out to log the queue depth change.
260 **/
261int
262lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
263{
264 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
265 struct lpfc_hba *phba = vport->phba;
266 struct lpfc_rport_data *rdata;
267 unsigned long new_queue_depth, old_queue_depth;
268
269 old_queue_depth = sdev->queue_depth;
270 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
271 new_queue_depth = sdev->queue_depth;
272 rdata = sdev->hostdata;
273 if (rdata)
274 lpfc_send_sdev_queuedepth_change_event(phba, vport,
275 rdata->pnode, sdev->lun,
276 old_queue_depth,
277 new_queue_depth);
278 return sdev->queue_depth;
279}
280
281/**
James Smart3621a712009-04-06 18:47:14 -0400282 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500283 * @phba: The Hba for which this call is being executed.
284 *
285 * This routine is called when there is resource error in driver or firmware.
286 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
287 * posts at most 1 event each second. This routine wakes up worker thread of
288 * @phba to process WORKER_RAM_DOWN_EVENT event.
289 *
290 * This routine should be called with no lock held.
291 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500292void
James Smarteaf15d52008-12-04 22:39:29 -0500293lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500294{
295 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400296 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500297
298 spin_lock_irqsave(&phba->hbalock, flags);
299 atomic_inc(&phba->num_rsrc_err);
300 phba->last_rsrc_error_time = jiffies;
301
302 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
303 spin_unlock_irqrestore(&phba->hbalock, flags);
304 return;
305 }
306
307 phba->last_ramp_down_time = jiffies;
308
309 spin_unlock_irqrestore(&phba->hbalock, flags);
310
311 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400312 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
313 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500314 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500315 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
316
James Smart5e9d9b82008-06-14 22:52:53 -0400317 if (!evt_posted)
318 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500319 return;
320}
321
James Smart9bad7672008-12-04 22:39:02 -0500322/**
James Smart3621a712009-04-06 18:47:14 -0400323 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500324 * @phba: The Hba for which this call is being executed.
325 *
326 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
327 * post at most 1 event every 5 minute after last_ramp_up_time or
328 * last_rsrc_error_time. This routine wakes up worker thread of @phba
329 * to process WORKER_RAM_DOWN_EVENT event.
330 *
331 * This routine should be called with no lock held.
332 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500333static inline void
James Smart3de2a652007-08-02 11:09:59 -0400334lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400335 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500336{
337 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400338 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400339 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500340 atomic_inc(&phba->num_cmd_success);
341
James Smarta257bf92009-04-06 18:48:10 -0400342 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500343 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500344 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500345 if (time_before(jiffies,
346 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
347 time_before(jiffies,
348 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500349 spin_unlock_irqrestore(&phba->hbalock, flags);
350 return;
351 }
James Smart92d7f7b2007-06-17 19:56:38 -0500352 phba->last_ramp_up_time = jiffies;
353 spin_unlock_irqrestore(&phba->hbalock, flags);
354
355 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400356 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
357 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500358 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500359 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
360
James Smart5e9d9b82008-06-14 22:52:53 -0400361 if (!evt_posted)
362 lpfc_worker_wake_up(phba);
363 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500364}
365
James Smart9bad7672008-12-04 22:39:02 -0500366/**
James Smart3621a712009-04-06 18:47:14 -0400367 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500368 * @phba: The Hba for which this call is being executed.
369 *
370 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
371 * thread.This routine reduces queue depth for all scsi device on each vport
372 * associated with @phba.
373 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500374void
375lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
376{
James Smart549e55c2007-08-02 11:09:51 -0400377 struct lpfc_vport **vports;
378 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500379 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500380 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500381 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400382 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500383
384 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
385 num_cmd_success = atomic_read(&phba->num_cmd_success);
386
James Smart549e55c2007-08-02 11:09:51 -0400387 vports = lpfc_create_vport_work_array(phba);
388 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400389 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400390 shost = lpfc_shost_from_vport(vports[i]);
391 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500392 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400393 sdev->queue_depth * num_rsrc_err /
394 (num_rsrc_err + num_cmd_success);
395 if (!new_queue_depth)
396 new_queue_depth = sdev->queue_depth - 1;
397 else
398 new_queue_depth = sdev->queue_depth -
399 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500400 lpfc_change_queue_depth(sdev, new_queue_depth,
401 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400402 }
James Smart92d7f7b2007-06-17 19:56:38 -0500403 }
James Smart09372822008-01-11 01:52:54 -0500404 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500405 atomic_set(&phba->num_rsrc_err, 0);
406 atomic_set(&phba->num_cmd_success, 0);
407}
408
James Smart9bad7672008-12-04 22:39:02 -0500409/**
James Smart3621a712009-04-06 18:47:14 -0400410 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500411 * @phba: The Hba for which this call is being executed.
412 *
413 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
414 * thread.This routine increases queue depth for all scsi device on each vport
415 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
416 * num_cmd_success to zero.
417 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500418void
419lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
420{
James Smart549e55c2007-08-02 11:09:51 -0400421 struct lpfc_vport **vports;
422 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500423 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400424 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500425
James Smart549e55c2007-08-02 11:09:51 -0400426 vports = lpfc_create_vport_work_array(phba);
427 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400428 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400429 shost = lpfc_shost_from_vport(vports[i]);
430 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400431 if (vports[i]->cfg_lun_queue_depth <=
432 sdev->queue_depth)
433 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500434 lpfc_change_queue_depth(sdev,
435 sdev->queue_depth+1,
436 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400437 }
James Smart92d7f7b2007-06-17 19:56:38 -0500438 }
James Smart09372822008-01-11 01:52:54 -0500439 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500440 atomic_set(&phba->num_rsrc_err, 0);
441 atomic_set(&phba->num_cmd_success, 0);
442}
443
James Smarta8e497d2008-08-24 21:50:11 -0400444/**
James Smart3621a712009-04-06 18:47:14 -0400445 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400446 * @phba: Pointer to HBA context object.
447 *
448 * This function walks vport list and set each SCSI host to block state
449 * by invoking fc_remote_port_delete() routine. This function is invoked
450 * with EEH when device's PCI slot has been permanently disabled.
451 **/
452void
453lpfc_scsi_dev_block(struct lpfc_hba *phba)
454{
455 struct lpfc_vport **vports;
456 struct Scsi_Host *shost;
457 struct scsi_device *sdev;
458 struct fc_rport *rport;
459 int i;
460
461 vports = lpfc_create_vport_work_array(phba);
462 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400463 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400464 shost = lpfc_shost_from_vport(vports[i]);
465 shost_for_each_device(sdev, shost) {
466 rport = starget_to_rport(scsi_target(sdev));
467 fc_remote_port_delete(rport);
468 }
469 }
470 lpfc_destroy_vport_work_array(phba, vports);
471}
472
James Smart9bad7672008-12-04 22:39:02 -0500473/**
James Smart3772a992009-05-22 14:50:54 -0400474 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500475 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400476 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500477 *
James Smart3772a992009-05-22 14:50:54 -0400478 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
479 * the scsi buffer contains all the necessary information needed to initiate
480 * a SCSI I/O. The non-DMAable buffer region contains information to build
481 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
482 * and the initial BPL. In addition to allocating memory, the FCP CMND and
483 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500484 *
485 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400486 * int - number of scsi buffers that were allocated.
487 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500488 **/
James Smart3772a992009-05-22 14:50:54 -0400489static int
490lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500491{
James Smart2e0fef82007-06-17 19:56:36 -0500492 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500493 struct lpfc_scsi_buf *psb;
494 struct ulp_bde64 *bpl;
495 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400496 dma_addr_t pdma_phys_fcp_cmd;
497 dma_addr_t pdma_phys_fcp_rsp;
498 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500499 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400500 int bcnt;
dea31012005-04-17 16:05:31 -0500501
James Smart3772a992009-05-22 14:50:54 -0400502 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
503 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
504 if (!psb)
505 break;
dea31012005-04-17 16:05:31 -0500506
James Smart3772a992009-05-22 14:50:54 -0400507 /*
508 * Get memory from the pci pool to map the virt space to pci
509 * bus space for an I/O. The DMA buffer includes space for the
510 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
511 * necessary to support the sg_tablesize.
512 */
513 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
514 GFP_KERNEL, &psb->dma_handle);
515 if (!psb->data) {
516 kfree(psb);
517 break;
518 }
dea31012005-04-17 16:05:31 -0500519
James Smart3772a992009-05-22 14:50:54 -0400520 /* Initialize virtual ptrs to dma_buf region. */
521 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500522
James Smart3772a992009-05-22 14:50:54 -0400523 /* Allocate iotag for psb->cur_iocbq. */
524 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
525 if (iotag == 0) {
526 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
527 psb->data, psb->dma_handle);
528 kfree(psb);
529 break;
530 }
531 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500532
James Smart3772a992009-05-22 14:50:54 -0400533 psb->fcp_cmnd = psb->data;
534 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
535 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400536 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500537
James Smart3772a992009-05-22 14:50:54 -0400538 /* Initialize local short-hand pointers. */
539 bpl = psb->fcp_bpl;
540 pdma_phys_fcp_cmd = psb->dma_handle;
541 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
542 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
543 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500544
James Smart3772a992009-05-22 14:50:54 -0400545 /*
546 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
547 * are sg list bdes. Initialize the first two and leave the
548 * rest for queuecommand.
549 */
550 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
551 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
552 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
553 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
554 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500555
James Smart3772a992009-05-22 14:50:54 -0400556 /* Setup the physical region for the FCP RSP */
557 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
558 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
559 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
560 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
561 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
562
563 /*
564 * Since the IOCB for the FCP I/O is built into this
565 * lpfc_scsi_buf, initialize it with all known data now.
566 */
567 iocb = &psb->cur_iocbq.iocb;
568 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
569 if ((phba->sli_rev == 3) &&
570 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
571 /* fill in immediate fcp command BDE */
572 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
573 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
574 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
575 unsli3.fcp_ext.icd);
576 iocb->un.fcpi64.bdl.addrHigh = 0;
577 iocb->ulpBdeCount = 0;
578 iocb->ulpLe = 0;
579 /* fill in responce BDE */
580 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
581 BUFF_TYPE_BDE_64;
582 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
583 sizeof(struct fcp_rsp);
584 iocb->unsli3.fcp_ext.rbde.addrLow =
585 putPaddrLow(pdma_phys_fcp_rsp);
586 iocb->unsli3.fcp_ext.rbde.addrHigh =
587 putPaddrHigh(pdma_phys_fcp_rsp);
588 } else {
589 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
590 iocb->un.fcpi64.bdl.bdeSize =
591 (2 * sizeof(struct ulp_bde64));
592 iocb->un.fcpi64.bdl.addrLow =
593 putPaddrLow(pdma_phys_bpl);
594 iocb->un.fcpi64.bdl.addrHigh =
595 putPaddrHigh(pdma_phys_bpl);
596 iocb->ulpBdeCount = 1;
597 iocb->ulpLe = 1;
598 }
599 iocb->ulpClass = CLASS3;
600 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400601 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400602 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500603 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400604
James Smart34b02dc2008-08-24 21:49:55 -0400605 }
dea31012005-04-17 16:05:31 -0500606
James Smart3772a992009-05-22 14:50:54 -0400607 return bcnt;
dea31012005-04-17 16:05:31 -0500608}
609
James Smart9bad7672008-12-04 22:39:02 -0500610/**
James Smartda0436e2009-05-22 14:51:39 -0400611 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
612 * @phba: pointer to lpfc hba data structure.
613 * @axri: pointer to the fcp xri abort wcqe structure.
614 *
615 * This routine is invoked by the worker thread to process a SLI4 fast-path
616 * FCP aborted xri.
617 **/
618void
619lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
620 struct sli4_wcqe_xri_aborted *axri)
621{
622 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
623 struct lpfc_scsi_buf *psb, *next_psb;
624 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500625 struct lpfc_iocbq *iocbq;
626 int i;
James Smart589a52d2010-07-14 15:30:54 -0400627 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400628
James Smart0f65ff62010-02-26 14:14:23 -0500629 spin_lock_irqsave(&phba->hbalock, iflag);
630 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400631 list_for_each_entry_safe(psb, next_psb,
632 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
633 if (psb->cur_iocbq.sli4_xritag == xri) {
634 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500635 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400636 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500637 spin_unlock(
638 &phba->sli4_hba.abts_scsi_buf_list_lock);
639 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400640 lpfc_release_scsi_buf_s4(phba, psb);
641 return;
642 }
643 }
James Smart0f65ff62010-02-26 14:14:23 -0500644 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
645 for (i = 1; i <= phba->sli.last_iotag; i++) {
646 iocbq = phba->sli.iocbq_lookup[i];
647
648 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
649 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
650 continue;
651 if (iocbq->sli4_xritag != xri)
652 continue;
653 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
654 psb->exch_busy = 0;
655 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart589a52d2010-07-14 15:30:54 -0400656 if (pring->txq_cnt)
657 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500658 return;
659
660 }
661 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400662}
663
664/**
665 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
666 * @phba: pointer to lpfc hba data structure.
667 *
668 * This routine walks the list of scsi buffers that have been allocated and
669 * repost them to the HBA by using SGL block post. This is needed after a
670 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
671 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
672 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
673 *
674 * Returns: 0 = success, non-zero failure.
675 **/
676int
677lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
678{
679 struct lpfc_scsi_buf *psb;
680 int index, status, bcnt = 0, rcnt = 0, rc = 0;
681 LIST_HEAD(sblist);
682
683 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
684 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
685 if (psb) {
686 /* Remove from SCSI buffer list */
687 list_del(&psb->list);
688 /* Add it to a local SCSI buffer list */
689 list_add_tail(&psb->list, &sblist);
690 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
691 bcnt = rcnt;
692 rcnt = 0;
693 }
694 } else
695 /* A hole present in the XRI array, need to skip */
696 bcnt = rcnt;
697
698 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
699 /* End of XRI array for SCSI buffer, complete */
700 bcnt = rcnt;
701
702 /* Continue until collect up to a nembed page worth of sgls */
703 if (bcnt == 0)
704 continue;
705 /* Now, post the SCSI buffer list sgls as a block */
706 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
707 /* Reset SCSI buffer count for next round of posting */
708 bcnt = 0;
709 while (!list_empty(&sblist)) {
710 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
711 list);
712 if (status) {
713 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500714 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400715 rc++;
James Smart341af102010-01-26 23:07:37 -0500716 } else {
717 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400718 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500719 }
James Smartda0436e2009-05-22 14:51:39 -0400720 /* Put it back into the SCSI buffer list */
721 lpfc_release_scsi_buf_s4(phba, psb);
722 }
723 }
724 return rc;
725}
726
727/**
728 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
729 * @vport: The virtual port for which this call being executed.
730 * @num_to_allocate: The requested number of buffers to allocate.
731 *
732 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
733 * the scsi buffer contains all the necessary information needed to initiate
734 * a SCSI I/O.
735 *
736 * Return codes:
737 * int - number of scsi buffers that were allocated.
738 * 0 = failure, less than num_to_alloc is a partial failure.
739 **/
740static int
741lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
742{
743 struct lpfc_hba *phba = vport->phba;
744 struct lpfc_scsi_buf *psb;
745 struct sli4_sge *sgl;
746 IOCB_t *iocb;
747 dma_addr_t pdma_phys_fcp_cmd;
748 dma_addr_t pdma_phys_fcp_rsp;
749 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
750 uint16_t iotag, last_xritag = NO_XRI;
751 int status = 0, index;
752 int bcnt;
753 int non_sequential_xri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400754 LIST_HEAD(sblist);
755
756 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
757 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
758 if (!psb)
759 break;
760
761 /*
762 * Get memory from the pci pool to map the virt space to pci bus
763 * space for an I/O. The DMA buffer includes space for the
764 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
765 * necessary to support the sg_tablesize.
766 */
767 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
768 GFP_KERNEL, &psb->dma_handle);
769 if (!psb->data) {
770 kfree(psb);
771 break;
772 }
773
774 /* Initialize virtual ptrs to dma_buf region. */
775 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
776
777 /* Allocate iotag for psb->cur_iocbq. */
778 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
779 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400780 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
781 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400782 kfree(psb);
783 break;
784 }
785
786 psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
787 if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
788 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
789 psb->data, psb->dma_handle);
790 kfree(psb);
791 break;
792 }
793 if (last_xritag != NO_XRI
794 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
795 non_sequential_xri = 1;
796 } else
797 list_add_tail(&psb->list, &sblist);
798 last_xritag = psb->cur_iocbq.sli4_xritag;
799
800 index = phba->sli4_hba.scsi_xri_cnt++;
801 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
802
803 psb->fcp_bpl = psb->data;
804 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
805 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
806 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
807 sizeof(struct fcp_cmnd));
808
809 /* Initialize local short-hand pointers. */
810 sgl = (struct sli4_sge *)psb->fcp_bpl;
811 pdma_phys_bpl = psb->dma_handle;
812 pdma_phys_fcp_cmd =
813 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
814 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
815 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
816
817 /*
818 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
819 * are sg list bdes. Initialize the first two and leave the
820 * rest for queuecommand.
821 */
822 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
823 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smartda0436e2009-05-22 14:51:39 -0400824 bf_set(lpfc_sli4_sge_last, sgl, 0);
825 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500826 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400827 sgl++;
828
829 /* Setup the physical region for the FCP RSP */
830 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
831 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400832 bf_set(lpfc_sli4_sge_last, sgl, 1);
833 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500834 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400835
836 /*
837 * Since the IOCB for the FCP I/O is built into this
838 * lpfc_scsi_buf, initialize it with all known data now.
839 */
840 iocb = &psb->cur_iocbq.iocb;
841 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
842 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
843 /* setting the BLP size to 2 * sizeof BDE may not be correct.
844 * We are setting the bpl to point to out sgl. An sgl's
845 * entries are 16 bytes, a bpl entries are 12 bytes.
846 */
847 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
848 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
849 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
850 iocb->ulpBdeCount = 1;
851 iocb->ulpLe = 1;
852 iocb->ulpClass = CLASS3;
James Smarteee88772010-09-29 11:19:08 -0400853 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -0400854 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
855 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
856 else
857 pdma_phys_bpl1 = 0;
858 psb->dma_phys_bpl = pdma_phys_bpl;
859 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
860 if (non_sequential_xri) {
861 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
862 pdma_phys_bpl1,
863 psb->cur_iocbq.sli4_xritag);
864 if (status) {
865 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500866 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500867 } else {
868 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400869 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500870 }
James Smartda0436e2009-05-22 14:51:39 -0400871 /* Put it back into the SCSI buffer list */
872 lpfc_release_scsi_buf_s4(phba, psb);
873 break;
874 }
875 }
876 if (bcnt) {
877 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
878 /* Reset SCSI buffer count for next round of posting */
879 while (!list_empty(&sblist)) {
880 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
881 list);
882 if (status) {
883 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500884 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500885 } else {
886 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400887 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500888 }
James Smartda0436e2009-05-22 14:51:39 -0400889 /* Put it back into the SCSI buffer list */
890 lpfc_release_scsi_buf_s4(phba, psb);
891 }
892 }
893
James Smartd7c47992010-06-08 18:31:54 -0400894 return bcnt + non_sequential_xri;
James Smartda0436e2009-05-22 14:51:39 -0400895}
896
897/**
James Smart3772a992009-05-22 14:50:54 -0400898 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
899 * @vport: The virtual port for which this call being executed.
900 * @num_to_allocate: The requested number of buffers to allocate.
901 *
902 * This routine wraps the actual SCSI buffer allocator function pointer from
903 * the lpfc_hba struct.
904 *
905 * Return codes:
906 * int - number of scsi buffers that were allocated.
907 * 0 = failure, less than num_to_alloc is a partial failure.
908 **/
909static inline int
910lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
911{
912 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
913}
914
915/**
916 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
917 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500918 *
919 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
920 * and returns to caller.
921 *
922 * Return codes:
923 * NULL - Error
924 * Pointer to lpfc_scsi_buf - Success
925 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100926static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500927lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500928{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400929 struct lpfc_scsi_buf * lpfc_cmd = NULL;
930 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500931 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500932
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500933 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400934 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400935 if (lpfc_cmd) {
936 lpfc_cmd->seg_cnt = 0;
937 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -0500938 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -0400939 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500940 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400941 return lpfc_cmd;
942}
943
James Smart9bad7672008-12-04 22:39:02 -0500944/**
James Smart3772a992009-05-22 14:50:54 -0400945 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500946 * @phba: The Hba for which this call is being executed.
947 * @psb: The scsi buffer which is being released.
948 *
949 * This routine releases @psb scsi buffer by adding it to tail of @phba
950 * lpfc_scsi_buf_list list.
951 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400952static void
James Smart3772a992009-05-22 14:50:54 -0400953lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400954{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500955 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500956
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500957 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400958 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500959 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500960 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500961}
962
James Smart9bad7672008-12-04 22:39:02 -0500963/**
James Smartda0436e2009-05-22 14:51:39 -0400964 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
965 * @phba: The Hba for which this call is being executed.
966 * @psb: The scsi buffer which is being released.
967 *
968 * This routine releases @psb scsi buffer by adding it to tail of @phba
969 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
970 * and cannot be reused for at least RA_TOV amount of time if it was
971 * aborted.
972 **/
973static void
974lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
975{
976 unsigned long iflag = 0;
977
James Smart341af102010-01-26 23:07:37 -0500978 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -0400979 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
980 iflag);
981 psb->pCmd = NULL;
982 list_add_tail(&psb->list,
983 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
984 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
985 iflag);
986 } else {
987
988 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
989 psb->pCmd = NULL;
990 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
991 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
992 }
993}
994
995/**
James Smart3772a992009-05-22 14:50:54 -0400996 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
997 * @phba: The Hba for which this call is being executed.
998 * @psb: The scsi buffer which is being released.
999 *
1000 * This routine releases @psb scsi buffer by adding it to tail of @phba
1001 * lpfc_scsi_buf_list list.
1002 **/
1003static void
1004lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1005{
1006
1007 phba->lpfc_release_scsi_buf(phba, psb);
1008}
1009
1010/**
1011 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001012 * @phba: The Hba for which this call is being executed.
1013 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1014 *
1015 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001016 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1017 * through sg elements and format the bdea. This routine also initializes all
1018 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001019 *
1020 * Return codes:
1021 * 1 - Error
1022 * 0 - Success
1023 **/
dea31012005-04-17 16:05:31 -05001024static int
James Smart3772a992009-05-22 14:50:54 -04001025lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001026{
1027 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1028 struct scatterlist *sgel = NULL;
1029 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1030 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001031 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001032 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001033 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001034 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001035 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001036 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001037
1038 /*
1039 * There are three possibilities here - use scatter-gather segment, use
1040 * the single mapping, or neither. Start the lpfc command prep by
1041 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1042 * data bde entry.
1043 */
1044 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001045 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001046 /*
1047 * The driver stores the segment count returned from pci_map_sg
1048 * because this a count of dma-mappings used to map the use_sg
1049 * pages. They are not guaranteed to be the same for those
1050 * architectures that implement an IOMMU.
1051 */
dea31012005-04-17 16:05:31 -05001052
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001053 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1054 scsi_sg_count(scsi_cmnd), datadir);
1055 if (unlikely(!nseg))
1056 return 1;
1057
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001058 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001059 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001060 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1061 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001062 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001063 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001064 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001065 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001066 return 1;
1067 }
1068
1069 /*
1070 * The driver established a maximum scatter-gather segment count
1071 * during probe that limits the number of sg elements in any
1072 * single scsi command. Just run through the seg_cnt and format
1073 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001074 * When using SLI-3 the driver will try to fit all the BDEs into
1075 * the IOCB. If it can't then the BDEs get added to a BPL as it
1076 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001077 */
James Smart34b02dc2008-08-24 21:49:55 -04001078 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001079 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001080 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001081 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001082 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001083 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1084 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1085 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1086 data_bde->addrLow = putPaddrLow(physaddr);
1087 data_bde->addrHigh = putPaddrHigh(physaddr);
1088 data_bde++;
1089 } else {
1090 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1091 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1092 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1093 bpl->addrLow =
1094 le32_to_cpu(putPaddrLow(physaddr));
1095 bpl->addrHigh =
1096 le32_to_cpu(putPaddrHigh(physaddr));
1097 bpl++;
1098 }
dea31012005-04-17 16:05:31 -05001099 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001100 }
dea31012005-04-17 16:05:31 -05001101
1102 /*
1103 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001104 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1105 * explicitly reinitialized and for SLI-3 the extended bde count is
1106 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001107 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001108 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001109 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1110 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001111 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1112 /*
1113 * The extended IOCB format can only fit 3 BDE or a BPL.
1114 * This I/O has more than 3 BDE so the 1st data bde will
1115 * be a BPL that is filled in here.
1116 */
1117 physaddr = lpfc_cmd->dma_handle;
1118 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1119 data_bde->tus.f.bdeSize = (num_bde *
1120 sizeof(struct ulp_bde64));
1121 physaddr += (sizeof(struct fcp_cmnd) +
1122 sizeof(struct fcp_rsp) +
1123 (2 * sizeof(struct ulp_bde64)));
1124 data_bde->addrHigh = putPaddrHigh(physaddr);
1125 data_bde->addrLow = putPaddrLow(physaddr);
1126 /* ebde count includes the responce bde and data bpl */
1127 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1128 } else {
1129 /* ebde count includes the responce bde and data bdes */
1130 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1131 }
1132 } else {
1133 iocb_cmd->un.fcpi64.bdl.bdeSize =
1134 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001135 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001136 }
James Smart09372822008-01-11 01:52:54 -05001137 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001138
1139 /*
1140 * Due to difference in data length between DIF/non-DIF paths,
1141 * we need to set word 4 of IOCB here
1142 */
James Smarta257bf92009-04-06 18:48:10 -04001143 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001144 return 0;
1145}
1146
James Smarte2a0a9d2008-12-04 22:40:02 -05001147/*
James Smart6c8eea52010-04-06 14:49:53 -04001148 * Given a scsi cmnd, determine the BlockGuard opcodes to be used with it
1149 * @sc: The SCSI command to examine
1150 * @txopt: (out) BlockGuard operation for transmitted data
1151 * @rxopt: (out) BlockGuard operation for received data
1152 *
1153 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1154 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001155 */
1156static int
James Smart6c8eea52010-04-06 14:49:53 -04001157lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1158 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001159{
1160 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001161 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001162
1163 if (guard_type == SHOST_DIX_GUARD_IP) {
1164 switch (scsi_get_prot_op(sc)) {
1165 case SCSI_PROT_READ_INSERT:
1166 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001167 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1168 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001169 break;
1170
1171 case SCSI_PROT_READ_STRIP:
1172 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001173 *txop = BG_OP_IN_NODIF_OUT_CRC;
1174 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001175 break;
1176
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001177 case SCSI_PROT_READ_PASS:
1178 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001179 *txop = BG_OP_IN_CSUM_OUT_CRC;
1180 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001181 break;
1182
James Smarte2a0a9d2008-12-04 22:40:02 -05001183 case SCSI_PROT_NORMAL:
1184 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001185 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart6c8eea52010-04-06 14:49:53 -04001186 "9063 BLKGRD: Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001187 scsi_get_prot_op(sc), guard_type);
James Smart6c8eea52010-04-06 14:49:53 -04001188 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001189 break;
1190
1191 }
1192 } else if (guard_type == SHOST_DIX_GUARD_CRC) {
1193 switch (scsi_get_prot_op(sc)) {
1194 case SCSI_PROT_READ_STRIP:
1195 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001196 *txop = BG_OP_IN_NODIF_OUT_CRC;
1197 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001198 break;
1199
1200 case SCSI_PROT_READ_PASS:
1201 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001202 *txop = BG_OP_IN_CRC_OUT_CRC;
1203 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001204 break;
1205
James Smarte2a0a9d2008-12-04 22:40:02 -05001206 case SCSI_PROT_READ_INSERT:
1207 case SCSI_PROT_WRITE_STRIP:
1208 case SCSI_PROT_NORMAL:
1209 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001210 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1211 "9075 BLKGRD: Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001212 scsi_get_prot_op(sc), guard_type);
James Smart6c8eea52010-04-06 14:49:53 -04001213 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001214 break;
1215 }
1216 } else {
1217 /* unsupported format */
1218 BUG();
1219 }
1220
James Smart6c8eea52010-04-06 14:49:53 -04001221 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001222}
1223
1224struct scsi_dif_tuple {
1225 __be16 guard_tag; /* Checksum */
1226 __be16 app_tag; /* Opaque storage */
1227 __be32 ref_tag; /* Target LBA or indirect LBA */
1228};
1229
1230static inline unsigned
1231lpfc_cmd_blksize(struct scsi_cmnd *sc)
1232{
1233 return sc->device->sector_size;
1234}
1235
1236/**
1237 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1238 * @sc: in: SCSI command
James Smart3621a712009-04-06 18:47:14 -04001239 * @apptagmask: out: app tag mask
1240 * @apptagval: out: app tag value
1241 * @reftag: out: ref tag (reference tag)
James Smarte2a0a9d2008-12-04 22:40:02 -05001242 *
1243 * Description:
Martin Olsson98a17082009-04-22 18:21:29 +02001244 * Extract DIF parameters from the command if possible. Otherwise,
1245 * use default parameters.
James Smarte2a0a9d2008-12-04 22:40:02 -05001246 *
1247 **/
1248static inline void
1249lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1250 uint16_t *apptagval, uint32_t *reftag)
1251{
1252 struct scsi_dif_tuple *spt;
1253 unsigned char op = scsi_get_prot_op(sc);
1254 unsigned int protcnt = scsi_prot_sg_count(sc);
1255 static int cnt;
1256
1257 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001258 op == SCSI_PROT_WRITE_PASS)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001259
1260 cnt++;
1261 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1262 scsi_prot_sglist(sc)[0].offset;
1263 *apptagmask = 0;
1264 *apptagval = 0;
1265 *reftag = cpu_to_be32(spt->ref_tag);
1266
1267 } else {
1268 /* SBC defines ref tag to be lower 32bits of LBA */
1269 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1270 *apptagmask = 0;
1271 *apptagval = 0;
1272 }
1273}
1274
1275/*
1276 * This function sets up buffer list for protection groups of
1277 * type LPFC_PG_TYPE_NO_DIF
1278 *
1279 * This is usually used when the HBA is instructed to generate
1280 * DIFs and insert them into data stream (or strip DIF from
1281 * incoming data stream)
1282 *
1283 * The buffer list consists of just one protection group described
1284 * below:
1285 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001286 * start of prot group --> | PDE_5 |
1287 * +-------------------------+
1288 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001289 * +-------------------------+
1290 * | Data BDE |
1291 * +-------------------------+
1292 * |more Data BDE's ... (opt)|
1293 * +-------------------------+
1294 *
1295 * @sc: pointer to scsi command we're working on
1296 * @bpl: pointer to buffer list for protection groups
1297 * @datacnt: number of segments of data that have been dma mapped
1298 *
1299 * Note: Data s/g buffers have been dma mapped
1300 */
1301static int
1302lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1303 struct ulp_bde64 *bpl, int datasegcnt)
1304{
1305 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001306 struct lpfc_pde5 *pde5 = NULL;
1307 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001308 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001309 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001310 int datadir = sc->sc_data_direction;
James Smarte2a0a9d2008-12-04 22:40:02 -05001311 unsigned blksize;
1312 uint32_t reftag;
1313 uint16_t apptagmask, apptagval;
James Smart6c8eea52010-04-06 14:49:53 -04001314 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001315
James Smart6c8eea52010-04-06 14:49:53 -04001316 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1317 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001318 goto out;
1319
James Smart6c8eea52010-04-06 14:49:53 -04001320 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001321 blksize = lpfc_cmd_blksize(sc);
1322 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1323
James Smart6c8eea52010-04-06 14:49:53 -04001324 /* setup PDE5 with what we have */
1325 pde5 = (struct lpfc_pde5 *) bpl;
1326 memset(pde5, 0, sizeof(struct lpfc_pde5));
1327 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1328 pde5->reftag = reftag;
James Smarte2a0a9d2008-12-04 22:40:02 -05001329
James Smartbc739052010-08-04 16:11:18 -04001330 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001331 pde5->word0 = cpu_to_le32(pde5->word0);
1332 pde5->reftag = cpu_to_le32(pde5->reftag);
1333
James Smart6c8eea52010-04-06 14:49:53 -04001334 /* advance bpl and increment bde count */
1335 num_bde++;
1336 bpl++;
1337 pde6 = (struct lpfc_pde6 *) bpl;
1338
1339 /* setup PDE6 with the rest of the info */
1340 memset(pde6, 0, sizeof(struct lpfc_pde6));
1341 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1342 bf_set(pde6_optx, pde6, txop);
1343 bf_set(pde6_oprx, pde6, rxop);
1344 if (datadir == DMA_FROM_DEVICE) {
1345 bf_set(pde6_ce, pde6, 1);
1346 bf_set(pde6_re, pde6, 1);
1347 bf_set(pde6_ae, pde6, 1);
1348 }
1349 bf_set(pde6_ai, pde6, 1);
1350 bf_set(pde6_apptagval, pde6, apptagval);
1351
James Smartbc739052010-08-04 16:11:18 -04001352 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001353 pde6->word0 = cpu_to_le32(pde6->word0);
1354 pde6->word1 = cpu_to_le32(pde6->word1);
1355 pde6->word2 = cpu_to_le32(pde6->word2);
1356
James Smart6c8eea52010-04-06 14:49:53 -04001357 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001358 num_bde++;
1359 bpl++;
1360
1361 /* assumption: caller has already run dma_map_sg on command data */
1362 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1363 physaddr = sg_dma_address(sgde);
1364 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1365 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1366 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1367 if (datadir == DMA_TO_DEVICE)
1368 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1369 else
1370 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1371 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1372 bpl++;
1373 num_bde++;
1374 }
1375
1376out:
1377 return num_bde;
1378}
1379
1380/*
1381 * This function sets up buffer list for protection groups of
1382 * type LPFC_PG_TYPE_DIF_BUF
1383 *
1384 * This is usually used when DIFs are in their own buffers,
1385 * separate from the data. The HBA can then by instructed
1386 * to place the DIFs in the outgoing stream. For read operations,
1387 * The HBA could extract the DIFs and place it in DIF buffers.
1388 *
1389 * The buffer list for this type consists of one or more of the
1390 * protection groups described below:
1391 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001392 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001393 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001394 * | PDE_6 |
1395 * +-------------------------+
1396 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001397 * +-------------------------+
1398 * | Data BDE |
1399 * +-------------------------+
1400 * |more Data BDE's ... (opt)|
1401 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001402 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001403 * +-------------------------+
1404 * | ... |
1405 * +-------------------------+
1406 *
1407 * @sc: pointer to scsi command we're working on
1408 * @bpl: pointer to buffer list for protection groups
1409 * @datacnt: number of segments of data that have been dma mapped
1410 * @protcnt: number of segment of protection data that have been dma mapped
1411 *
1412 * Note: It is assumed that both data and protection s/g buffers have been
1413 * mapped for DMA
1414 */
1415static int
1416lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1417 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1418{
1419 struct scatterlist *sgde = NULL; /* s/g data entry */
1420 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001421 struct lpfc_pde5 *pde5 = NULL;
1422 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001423 struct ulp_bde64 *prot_bde = NULL;
1424 dma_addr_t dataphysaddr, protphysaddr;
1425 unsigned short curr_data = 0, curr_prot = 0;
1426 unsigned int split_offset, protgroup_len;
1427 unsigned int protgrp_blks, protgrp_bytes;
1428 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001429 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001430 int datadir = sc->sc_data_direction;
1431 unsigned char pgdone = 0, alldone = 0;
1432 unsigned blksize;
1433 uint32_t reftag;
1434 uint16_t apptagmask, apptagval;
James Smart6c8eea52010-04-06 14:49:53 -04001435 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001436 int num_bde = 0;
1437
1438 sgpe = scsi_prot_sglist(sc);
1439 sgde = scsi_sglist(sc);
1440
1441 if (!sgpe || !sgde) {
1442 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1443 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1444 sgpe, sgde);
1445 return 0;
1446 }
1447
James Smart6c8eea52010-04-06 14:49:53 -04001448 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1449 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001450 goto out;
1451
James Smart6c8eea52010-04-06 14:49:53 -04001452 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001453 blksize = lpfc_cmd_blksize(sc);
1454 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1455
1456 split_offset = 0;
1457 do {
James Smart6c8eea52010-04-06 14:49:53 -04001458 /* setup PDE5 with what we have */
1459 pde5 = (struct lpfc_pde5 *) bpl;
1460 memset(pde5, 0, sizeof(struct lpfc_pde5));
1461 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1462 pde5->reftag = reftag;
James Smarte2a0a9d2008-12-04 22:40:02 -05001463
James Smartbc739052010-08-04 16:11:18 -04001464 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001465 pde5->word0 = cpu_to_le32(pde5->word0);
1466 pde5->reftag = cpu_to_le32(pde5->reftag);
1467
James Smart6c8eea52010-04-06 14:49:53 -04001468 /* advance bpl and increment bde count */
1469 num_bde++;
1470 bpl++;
1471 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001472
James Smart6c8eea52010-04-06 14:49:53 -04001473 /* setup PDE6 with the rest of the info */
1474 memset(pde6, 0, sizeof(struct lpfc_pde6));
1475 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1476 bf_set(pde6_optx, pde6, txop);
1477 bf_set(pde6_oprx, pde6, rxop);
1478 bf_set(pde6_ce, pde6, 1);
1479 bf_set(pde6_re, pde6, 1);
1480 bf_set(pde6_ae, pde6, 1);
1481 bf_set(pde6_ai, pde6, 1);
1482 bf_set(pde6_apptagval, pde6, apptagval);
1483
James Smartbc739052010-08-04 16:11:18 -04001484 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001485 pde6->word0 = cpu_to_le32(pde6->word0);
1486 pde6->word1 = cpu_to_le32(pde6->word1);
1487 pde6->word2 = cpu_to_le32(pde6->word2);
1488
James Smart6c8eea52010-04-06 14:49:53 -04001489 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001490 num_bde++;
1491 bpl++;
1492
1493 /* setup the first BDE that points to protection buffer */
1494 prot_bde = (struct ulp_bde64 *) bpl;
1495 protphysaddr = sg_dma_address(sgpe);
James Smart6c8eea52010-04-06 14:49:53 -04001496 prot_bde->addrHigh = le32_to_cpu(putPaddrLow(protphysaddr));
1497 prot_bde->addrLow = le32_to_cpu(putPaddrHigh(protphysaddr));
James Smarte2a0a9d2008-12-04 22:40:02 -05001498 protgroup_len = sg_dma_len(sgpe);
1499
James Smarte2a0a9d2008-12-04 22:40:02 -05001500 /* must be integer multiple of the DIF block length */
1501 BUG_ON(protgroup_len % 8);
1502
1503 protgrp_blks = protgroup_len / 8;
1504 protgrp_bytes = protgrp_blks * blksize;
1505
1506 prot_bde->tus.f.bdeSize = protgroup_len;
James Smart6c8eea52010-04-06 14:49:53 -04001507 prot_bde->tus.f.bdeFlags = LPFC_PDE7_DESCRIPTOR;
James Smarte2a0a9d2008-12-04 22:40:02 -05001508 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1509
1510 curr_prot++;
1511 num_bde++;
1512
1513 /* setup BDE's for data blocks associated with DIF data */
1514 pgdone = 0;
1515 subtotal = 0; /* total bytes processed for current prot grp */
1516 while (!pgdone) {
1517 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001518 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1519 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001520 __func__);
1521 return 0;
1522 }
1523 bpl++;
1524 dataphysaddr = sg_dma_address(sgde) + split_offset;
1525 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1526 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1527
1528 remainder = sg_dma_len(sgde) - split_offset;
1529
1530 if ((subtotal + remainder) <= protgrp_bytes) {
1531 /* we can use this whole buffer */
1532 bpl->tus.f.bdeSize = remainder;
1533 split_offset = 0;
1534
1535 if ((subtotal + remainder) == protgrp_bytes)
1536 pgdone = 1;
1537 } else {
1538 /* must split this buffer with next prot grp */
1539 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1540 split_offset += bpl->tus.f.bdeSize;
1541 }
1542
1543 subtotal += bpl->tus.f.bdeSize;
1544
1545 if (datadir == DMA_TO_DEVICE)
1546 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1547 else
1548 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1549 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1550
1551 num_bde++;
1552 curr_data++;
1553
1554 if (split_offset)
1555 break;
1556
1557 /* Move to the next s/g segment if possible */
1558 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001559
James Smarte2a0a9d2008-12-04 22:40:02 -05001560 }
1561
1562 /* are we done ? */
1563 if (curr_prot == protcnt) {
1564 alldone = 1;
1565 } else if (curr_prot < protcnt) {
1566 /* advance to next prot buffer */
1567 sgpe = sg_next(sgpe);
1568 bpl++;
1569
1570 /* update the reference tag */
1571 reftag += protgrp_blks;
1572 } else {
1573 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001574 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1575 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001576 }
1577
1578 } while (!alldone);
1579
1580out:
1581
James Smarte2a0a9d2008-12-04 22:40:02 -05001582 return num_bde;
1583}
1584/*
1585 * Given a SCSI command that supports DIF, determine composition of protection
1586 * groups involved in setting up buffer lists
1587 *
1588 * Returns:
1589 * for DIF (for both read and write)
1590 * */
1591static int
1592lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1593{
1594 int ret = LPFC_PG_TYPE_INVALID;
1595 unsigned char op = scsi_get_prot_op(sc);
1596
1597 switch (op) {
1598 case SCSI_PROT_READ_STRIP:
1599 case SCSI_PROT_WRITE_INSERT:
1600 ret = LPFC_PG_TYPE_NO_DIF;
1601 break;
1602 case SCSI_PROT_READ_INSERT:
1603 case SCSI_PROT_WRITE_STRIP:
1604 case SCSI_PROT_READ_PASS:
1605 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001606 ret = LPFC_PG_TYPE_DIF_BUF;
1607 break;
1608 default:
1609 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1610 "9021 Unsupported protection op:%d\n", op);
1611 break;
1612 }
1613
1614 return ret;
1615}
1616
1617/*
1618 * This is the protection/DIF aware version of
1619 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1620 * two functions eventually, but for now, it's here
1621 */
1622static int
1623lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1624 struct lpfc_scsi_buf *lpfc_cmd)
1625{
1626 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1627 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1628 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1629 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1630 uint32_t num_bde = 0;
1631 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1632 int prot_group_type = 0;
1633 int diflen, fcpdl;
1634 unsigned blksize;
1635
1636 /*
1637 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1638 * fcp_rsp regions to the first data bde entry
1639 */
1640 bpl += 2;
1641 if (scsi_sg_count(scsi_cmnd)) {
1642 /*
1643 * The driver stores the segment count returned from pci_map_sg
1644 * because this a count of dma-mappings used to map the use_sg
1645 * pages. They are not guaranteed to be the same for those
1646 * architectures that implement an IOMMU.
1647 */
1648 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1649 scsi_sglist(scsi_cmnd),
1650 scsi_sg_count(scsi_cmnd), datadir);
1651 if (unlikely(!datasegcnt))
1652 return 1;
1653
1654 lpfc_cmd->seg_cnt = datasegcnt;
1655 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001656 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1657 "9067 BLKGRD: %s: Too many sg segments"
1658 " from dma_map_sg. Config %d, seg_cnt"
1659 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001660 __func__, phba->cfg_sg_seg_cnt,
1661 lpfc_cmd->seg_cnt);
1662 scsi_dma_unmap(scsi_cmnd);
1663 return 1;
1664 }
1665
1666 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1667
1668 switch (prot_group_type) {
1669 case LPFC_PG_TYPE_NO_DIF:
1670 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1671 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001672 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001673 if (num_bde < 2)
1674 goto err;
1675 break;
1676 case LPFC_PG_TYPE_DIF_BUF:{
1677 /*
1678 * This type indicates that protection buffers are
1679 * passed to the driver, so that needs to be prepared
1680 * for DMA
1681 */
1682 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1683 scsi_prot_sglist(scsi_cmnd),
1684 scsi_prot_sg_count(scsi_cmnd), datadir);
1685 if (unlikely(!protsegcnt)) {
1686 scsi_dma_unmap(scsi_cmnd);
1687 return 1;
1688 }
1689
1690 lpfc_cmd->prot_seg_cnt = protsegcnt;
1691 if (lpfc_cmd->prot_seg_cnt
1692 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001693 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1694 "9068 BLKGRD: %s: Too many prot sg "
1695 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05001696 "prot_seg_cnt %d\n", __func__,
1697 phba->cfg_prot_sg_seg_cnt,
1698 lpfc_cmd->prot_seg_cnt);
1699 dma_unmap_sg(&phba->pcidev->dev,
1700 scsi_prot_sglist(scsi_cmnd),
1701 scsi_prot_sg_count(scsi_cmnd),
1702 datadir);
1703 scsi_dma_unmap(scsi_cmnd);
1704 return 1;
1705 }
1706
1707 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1708 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001709 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001710 if (num_bde < 3)
1711 goto err;
1712 break;
1713 }
1714 case LPFC_PG_TYPE_INVALID:
1715 default:
1716 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1717 "9022 Unexpected protection group %i\n",
1718 prot_group_type);
1719 return 1;
1720 }
1721 }
1722
1723 /*
1724 * Finish initializing those IOCB fields that are dependent on the
1725 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1726 * reinitialized since all iocb memory resources are used many times
1727 * for transmit, receive, and continuation bpl's.
1728 */
1729 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1730 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1731 iocb_cmd->ulpBdeCount = 1;
1732 iocb_cmd->ulpLe = 1;
1733
1734 fcpdl = scsi_bufflen(scsi_cmnd);
1735
1736 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1737 /*
1738 * We are in DIF Type 1 mode
1739 * Every data block has a 8 byte DIF (trailer)
1740 * attached to it. Must ajust FCP data length
1741 */
1742 blksize = lpfc_cmd_blksize(scsi_cmnd);
1743 diflen = (fcpdl / blksize) * 8;
1744 fcpdl += diflen;
1745 }
1746 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1747
1748 /*
1749 * Due to difference in data length between DIF/non-DIF paths,
1750 * we need to set word 4 of IOCB here
1751 */
1752 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1753
1754 return 0;
1755err:
1756 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1757 "9023 Could not setup all needed BDE's"
1758 "prot_group_type=%d, num_bde=%d\n",
1759 prot_group_type, num_bde);
1760 return 1;
1761}
1762
1763/*
1764 * This function checks for BlockGuard errors detected by
1765 * the HBA. In case of errors, the ASC/ASCQ fields in the
1766 * sense buffer will be set accordingly, paired with
1767 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1768 * detected corruption.
1769 *
1770 * Returns:
1771 * 0 - No error found
1772 * 1 - BlockGuard error found
1773 * -1 - Internal error (bad profile, ...etc)
1774 */
1775static int
1776lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1777 struct lpfc_iocbq *pIocbOut)
1778{
1779 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1780 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1781 int ret = 0;
1782 uint32_t bghm = bgf->bghm;
1783 uint32_t bgstat = bgf->bgstat;
1784 uint64_t failing_sector = 0;
1785
James Smart6a9c52c2009-10-02 15:16:51 -04001786 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
1787 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05001788 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05001789 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09001790 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05001791
1792 spin_lock(&_dump_buf_lock);
1793 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04001794 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
1795 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001796 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04001797 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001798
1799 /* If we have a prot sgl, save the DIF buffer */
1800 if (lpfc_prot_group_type(phba, cmd) ==
1801 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04001802 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
1803 "Saving DIF for %u blocks to debugfs\n",
1804 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1805 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001806 }
1807
1808 _dump_buf_done = 1;
1809 }
1810 spin_unlock(&_dump_buf_lock);
1811
1812 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1813 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001814 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
1815 " BlockGuard profile. bgstat:0x%x\n",
1816 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05001817 ret = (-1);
1818 goto out;
1819 }
1820
1821 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1822 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001823 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
1824 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001825 bgstat);
1826 ret = (-1);
1827 goto out;
1828 }
1829
1830 if (lpfc_bgs_get_guard_err(bgstat)) {
1831 ret = 1;
1832
1833 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1834 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001835 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001836 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1837 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001838 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1839 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001840 }
1841
1842 if (lpfc_bgs_get_reftag_err(bgstat)) {
1843 ret = 1;
1844
1845 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1846 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001847 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001848 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1849
1850 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001851 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1852 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001853 }
1854
1855 if (lpfc_bgs_get_apptag_err(bgstat)) {
1856 ret = 1;
1857
1858 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1859 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001860 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001861 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1862
1863 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001864 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1865 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001866 }
1867
1868 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1869 /*
1870 * setup sense data descriptor 0 per SPC-4 as an information
1871 * field, and put the failing LBA in it
1872 */
1873 cmd->sense_buffer[8] = 0; /* Information */
1874 cmd->sense_buffer[9] = 0xa; /* Add. length */
David Howells2344b5b2009-04-14 17:08:34 +01001875 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05001876
1877 failing_sector = scsi_get_lba(cmd);
1878 failing_sector += bghm;
1879
1880 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1881 }
1882
1883 if (!ret) {
1884 /* No error was reported - problem in FW? */
1885 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001886 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1887 "9057 BLKGRD: no errors reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001888 }
1889
1890out:
1891 return ret;
1892}
1893
James Smartea2151b2008-09-07 11:52:10 -04001894/**
James Smartda0436e2009-05-22 14:51:39 -04001895 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1896 * @phba: The Hba for which this call is being executed.
1897 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1898 *
1899 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1900 * field of @lpfc_cmd for device with SLI-4 interface spec.
1901 *
1902 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04001903 * 1 - Error
1904 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04001905 **/
1906static int
1907lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1908{
1909 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1910 struct scatterlist *sgel = NULL;
1911 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1912 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
1913 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1914 dma_addr_t physaddr;
1915 uint32_t num_bde = 0;
1916 uint32_t dma_len;
1917 uint32_t dma_offset = 0;
1918 int nseg;
1919
1920 /*
1921 * There are three possibilities here - use scatter-gather segment, use
1922 * the single mapping, or neither. Start the lpfc command prep by
1923 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1924 * data bde entry.
1925 */
1926 if (scsi_sg_count(scsi_cmnd)) {
1927 /*
1928 * The driver stores the segment count returned from pci_map_sg
1929 * because this a count of dma-mappings used to map the use_sg
1930 * pages. They are not guaranteed to be the same for those
1931 * architectures that implement an IOMMU.
1932 */
1933
1934 nseg = scsi_dma_map(scsi_cmnd);
1935 if (unlikely(!nseg))
1936 return 1;
1937 sgl += 1;
1938 /* clear the last flag in the fcp_rsp map entry */
1939 sgl->word2 = le32_to_cpu(sgl->word2);
1940 bf_set(lpfc_sli4_sge_last, sgl, 0);
1941 sgl->word2 = cpu_to_le32(sgl->word2);
1942 sgl += 1;
1943
1944 lpfc_cmd->seg_cnt = nseg;
1945 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001946 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
1947 " %s: Too many sg segments from "
1948 "dma_map_sg. Config %d, seg_cnt %d\n",
1949 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04001950 lpfc_cmd->seg_cnt);
1951 scsi_dma_unmap(scsi_cmnd);
1952 return 1;
1953 }
1954
1955 /*
1956 * The driver established a maximum scatter-gather segment count
1957 * during probe that limits the number of sg elements in any
1958 * single scsi command. Just run through the seg_cnt and format
1959 * the sge's.
1960 * When using SLI-3 the driver will try to fit all the BDEs into
1961 * the IOCB. If it can't then the BDEs get added to a BPL as it
1962 * does for SLI-2 mode.
1963 */
1964 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1965 physaddr = sg_dma_address(sgel);
1966 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04001967 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1968 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1969 if ((num_bde + 1) == nseg)
1970 bf_set(lpfc_sli4_sge_last, sgl, 1);
1971 else
1972 bf_set(lpfc_sli4_sge_last, sgl, 0);
1973 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1974 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001975 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04001976 dma_offset += dma_len;
1977 sgl++;
1978 }
1979 } else {
1980 sgl += 1;
1981 /* clear the last flag in the fcp_rsp map entry */
1982 sgl->word2 = le32_to_cpu(sgl->word2);
1983 bf_set(lpfc_sli4_sge_last, sgl, 1);
1984 sgl->word2 = cpu_to_le32(sgl->word2);
1985 }
1986
1987 /*
1988 * Finish initializing those IOCB fields that are dependent on the
1989 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1990 * explicitly reinitialized.
1991 * all iocb memory resources are reused.
1992 */
1993 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1994
1995 /*
1996 * Due to difference in data length between DIF/non-DIF paths,
1997 * we need to set word 4 of IOCB here
1998 */
1999 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
2000 return 0;
2001}
2002
2003/**
James Smart3772a992009-05-22 14:50:54 -04002004 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
2005 * @phba: The Hba for which this call is being executed.
2006 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2007 *
2008 * This routine wraps the actual DMA mapping function pointer from the
2009 * lpfc_hba struct.
2010 *
2011 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002012 * 1 - Error
2013 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04002014 **/
2015static inline int
2016lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2017{
2018 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2019}
2020
2021/**
James Smart3621a712009-04-06 18:47:14 -04002022 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04002023 * @phba: Pointer to hba context object.
2024 * @vport: Pointer to vport object.
2025 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
2026 * @rsp_iocb: Pointer to response iocb object which reported error.
2027 *
2028 * This function posts an event when there is a SCSI command reporting
2029 * error from the scsi device.
2030 **/
2031static void
2032lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
2033 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
2034 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2035 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2036 uint32_t resp_info = fcprsp->rspStatus2;
2037 uint32_t scsi_status = fcprsp->rspStatus3;
2038 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2039 struct lpfc_fast_path_event *fast_path_evt = NULL;
2040 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
2041 unsigned long flags;
2042
2043 /* If there is queuefull or busy condition send a scsi event */
2044 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
2045 (cmnd->result == SAM_STAT_BUSY)) {
2046 fast_path_evt = lpfc_alloc_fast_evt(phba);
2047 if (!fast_path_evt)
2048 return;
2049 fast_path_evt->un.scsi_evt.event_type =
2050 FC_REG_SCSI_EVENT;
2051 fast_path_evt->un.scsi_evt.subcategory =
2052 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
2053 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
2054 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
2055 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
2056 &pnode->nlp_portname, sizeof(struct lpfc_name));
2057 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
2058 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2059 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
2060 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
2061 fast_path_evt = lpfc_alloc_fast_evt(phba);
2062 if (!fast_path_evt)
2063 return;
2064 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
2065 FC_REG_SCSI_EVENT;
2066 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
2067 LPFC_EVENT_CHECK_COND;
2068 fast_path_evt->un.check_cond_evt.scsi_event.lun =
2069 cmnd->device->lun;
2070 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
2071 &pnode->nlp_portname, sizeof(struct lpfc_name));
2072 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
2073 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2074 fast_path_evt->un.check_cond_evt.sense_key =
2075 cmnd->sense_buffer[2] & 0xf;
2076 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
2077 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
2078 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2079 fcpi_parm &&
2080 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
2081 ((scsi_status == SAM_STAT_GOOD) &&
2082 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
2083 /*
2084 * If status is good or resid does not match with fcp_param and
2085 * there is valid fcpi_parm, then there is a read_check error
2086 */
2087 fast_path_evt = lpfc_alloc_fast_evt(phba);
2088 if (!fast_path_evt)
2089 return;
2090 fast_path_evt->un.read_check_error.header.event_type =
2091 FC_REG_FABRIC_EVENT;
2092 fast_path_evt->un.read_check_error.header.subcategory =
2093 LPFC_EVENT_FCPRDCHKERR;
2094 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
2095 &pnode->nlp_portname, sizeof(struct lpfc_name));
2096 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2097 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2098 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2099 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2100 fast_path_evt->un.read_check_error.fcpiparam =
2101 fcpi_parm;
2102 } else
2103 return;
2104
2105 fast_path_evt->vport = vport;
2106 spin_lock_irqsave(&phba->hbalock, flags);
2107 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2108 spin_unlock_irqrestore(&phba->hbalock, flags);
2109 lpfc_worker_wake_up(phba);
2110 return;
2111}
James Smart9bad7672008-12-04 22:39:02 -05002112
2113/**
James Smartf1126682009-06-10 17:22:44 -04002114 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04002115 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05002116 * @psb: The scsi buffer which is going to be un-mapped.
2117 *
2118 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04002119 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002120 **/
dea31012005-04-17 16:05:31 -05002121static void
James Smartf1126682009-06-10 17:22:44 -04002122lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04002123{
2124 /*
2125 * There are only two special cases to consider. (1) the scsi command
2126 * requested scatter-gather usage or (2) the scsi command allocated
2127 * a request buffer, but did not request use_sg. There is a third
2128 * case, but it does not require resource deallocation.
2129 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002130 if (psb->seg_cnt > 0)
2131 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002132 if (psb->prot_seg_cnt > 0)
2133 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2134 scsi_prot_sg_count(psb->pCmd),
2135 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04002136}
2137
James Smart9bad7672008-12-04 22:39:02 -05002138/**
James Smart3621a712009-04-06 18:47:14 -04002139 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05002140 * @vport: The virtual port for which this call is being executed.
2141 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2142 * @rsp_iocb: The response IOCB which contains FCP error.
2143 *
2144 * This routine is called to process response IOCB with status field
2145 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2146 * based upon SCSI and FCP error.
2147 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04002148static void
James Smart2e0fef82007-06-17 19:56:36 -05002149lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2150 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05002151{
2152 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2153 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2154 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04002155 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05002156 uint32_t resp_info = fcprsp->rspStatus2;
2157 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05002158 uint32_t *lp;
dea31012005-04-17 16:05:31 -05002159 uint32_t host_status = DID_OK;
2160 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05002161 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05002162
James Smartea2151b2008-09-07 11:52:10 -04002163
dea31012005-04-17 16:05:31 -05002164 /*
2165 * If this is a task management command, there is no
2166 * scsi packet associated with this lpfc_cmd. The driver
2167 * consumes it.
2168 */
2169 if (fcpcmd->fcpCntl2) {
2170 scsi_status = 0;
2171 goto out;
2172 }
2173
James Smart6a9c52c2009-10-02 15:16:51 -04002174 if (resp_info & RSP_LEN_VALID) {
2175 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05002176 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04002177 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2178 "2719 Invalid response length: "
2179 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2180 cmnd->device->id,
2181 cmnd->device->lun, cmnd->cmnd[0],
2182 rsplen);
2183 host_status = DID_ERROR;
2184 goto out;
2185 }
James Smarte40a02c2010-02-26 14:13:54 -05002186 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
2187 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2188 "2757 Protocol failure detected during "
2189 "processing of FCP I/O op: "
2190 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
2191 cmnd->device->id,
2192 cmnd->device->lun, cmnd->cmnd[0],
2193 fcprsp->rspInfo3);
2194 host_status = DID_ERROR;
2195 goto out;
2196 }
James Smart6a9c52c2009-10-02 15:16:51 -04002197 }
2198
James Smartc7743952006-12-02 13:34:42 -05002199 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2200 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2201 if (snslen > SCSI_SENSE_BUFFERSIZE)
2202 snslen = SCSI_SENSE_BUFFERSIZE;
2203
2204 if (resp_info & RSP_LEN_VALID)
2205 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2206 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2207 }
2208 lp = (uint32_t *)cmnd->sense_buffer;
2209
2210 if (!scsi_status && (resp_info & RESID_UNDER))
2211 logit = LOG_FCP;
2212
James Smarte8b62012007-08-02 11:10:09 -04002213 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05002214 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04002215 "Data: x%x x%x x%x x%x x%x\n",
2216 cmnd->cmnd[0], scsi_status,
2217 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2218 be32_to_cpu(fcprsp->rspResId),
2219 be32_to_cpu(fcprsp->rspSnsLen),
2220 be32_to_cpu(fcprsp->rspRspLen),
2221 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05002222
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002223 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05002224 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002225 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05002226
James Smarte8b62012007-08-02 11:10:09 -04002227 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002228 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04002229 "residual %d Data: x%x x%x x%x\n",
2230 be32_to_cpu(fcpcmd->fcpDl),
2231 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2232 cmnd->underflow);
dea31012005-04-17 16:05:31 -05002233
2234 /*
James Smart7054a602007-04-25 09:52:34 -04002235 * If there is an under run check if under run reported by
2236 * storage array is same as the under run reported by HBA.
2237 * If this is not same, there is a dropped frame.
2238 */
2239 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2240 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002241 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04002242 lpfc_printf_vlog(vport, KERN_WARNING,
2243 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002244 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04002245 "and Underrun Data: x%x x%x x%x x%x\n",
2246 be32_to_cpu(fcpcmd->fcpDl),
2247 scsi_get_resid(cmnd), fcpi_parm,
2248 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002249 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04002250 host_status = DID_ERROR;
2251 }
2252 /*
dea31012005-04-17 16:05:31 -05002253 * The cmnd->underflow is the minimum number of bytes that must
2254 * be transfered for this command. Provided a sense condition
2255 * is not present, make sure the actual amount transferred is at
2256 * least the underflow value or fail.
2257 */
2258 if (!(resp_info & SNS_LEN_VALID) &&
2259 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002260 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2261 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04002262 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002263 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04002264 "underrun converted to error "
2265 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04002266 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04002267 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05002268 host_status = DID_ERROR;
2269 }
2270 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04002271 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002272 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04002273 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04002274 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05002275 host_status = DID_ERROR;
2276
2277 /*
2278 * Check SLI validation that all the transfer was actually done
2279 * (fcpi_parm should be zero). Apply check only to reads.
2280 */
James Smarteee88772010-09-29 11:19:08 -04002281 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04002282 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002283 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04002284 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002285 be32_to_cpu(fcpcmd->fcpDl),
2286 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04002287 fcpi_parm, cmnd->cmnd[0], scsi_status);
2288 switch (scsi_status) {
2289 case SAM_STAT_GOOD:
2290 case SAM_STAT_CHECK_CONDITION:
2291 /* Fabric dropped a data frame. Fail any successful
2292 * command in which we detected dropped frames.
2293 * A status of good or some check conditions could
2294 * be considered a successful command.
2295 */
2296 host_status = DID_ERROR;
2297 break;
2298 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002299 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05002300 }
2301
2302 out:
2303 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04002304 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05002305}
2306
James Smart9bad7672008-12-04 22:39:02 -05002307/**
James Smart3621a712009-04-06 18:47:14 -04002308 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05002309 * @phba: The Hba for which this call is being executed.
2310 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04002311 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05002312 *
2313 * This routine assigns scsi command result by looking into response IOCB
2314 * status field appropriately. This routine handles QUEUE FULL condition as
2315 * well by ramping down device queue depth.
2316 **/
dea31012005-04-17 16:05:31 -05002317static void
2318lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2319 struct lpfc_iocbq *pIocbOut)
2320{
2321 struct lpfc_scsi_buf *lpfc_cmd =
2322 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002323 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05002324 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2325 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04002326 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002327 int result;
James Smarta257bf92009-04-06 18:48:10 -04002328 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05002329 int depth;
James Smartfa61a542008-01-11 01:52:42 -05002330 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04002331 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04002332 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04002333 uint32_t queue_depth, scsi_id;
dea31012005-04-17 16:05:31 -05002334
James Smart75baf692010-06-08 18:31:21 -04002335 /* Sanity check on return of outstanding command */
2336 if (!(lpfc_cmd->pCmd))
2337 return;
2338 cmd = lpfc_cmd->pCmd;
2339 shost = cmd->device->host;
2340
dea31012005-04-17 16:05:31 -05002341 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2342 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05002343 /* pick up SLI4 exhange busy status from HBA */
2344 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
2345
James Smart109f6ed2008-12-04 22:39:08 -05002346 if (pnode && NLP_CHK_NODE_ACT(pnode))
2347 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05002348
2349 if (lpfc_cmd->status) {
2350 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2351 (lpfc_cmd->result & IOERR_DRVR_MASK))
2352 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2353 else if (lpfc_cmd->status >= IOSTAT_CNT)
2354 lpfc_cmd->status = IOSTAT_DEFAULT;
2355
James Smarte8b62012007-08-02 11:10:09 -04002356 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002357 "9030 FCP cmd x%x failed <%d/%d> "
James Smarte8b62012007-08-02 11:10:09 -04002358 "status: x%x result: x%x Data: x%x x%x\n",
2359 cmd->cmnd[0],
2360 cmd->device ? cmd->device->id : 0xffff,
2361 cmd->device ? cmd->device->lun : 0xffff,
2362 lpfc_cmd->status, lpfc_cmd->result,
2363 pIocbOut->iocb.ulpContext,
2364 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05002365
2366 switch (lpfc_cmd->status) {
2367 case IOSTAT_FCP_RSP_ERROR:
2368 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05002369 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05002370 break;
2371 case IOSTAT_NPORT_BSY:
2372 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04002373 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04002374 fast_path_evt = lpfc_alloc_fast_evt(phba);
2375 if (!fast_path_evt)
2376 break;
2377 fast_path_evt->un.fabric_evt.event_type =
2378 FC_REG_FABRIC_EVENT;
2379 fast_path_evt->un.fabric_evt.subcategory =
2380 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2381 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2382 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2383 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2384 &pnode->nlp_portname,
2385 sizeof(struct lpfc_name));
2386 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2387 &pnode->nlp_nodename,
2388 sizeof(struct lpfc_name));
2389 }
2390 fast_path_evt->vport = vport;
2391 fast_path_evt->work_evt.evt =
2392 LPFC_EVT_FASTPATH_MGMT_EVT;
2393 spin_lock_irqsave(&phba->hbalock, flags);
2394 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2395 &phba->work_list);
2396 spin_unlock_irqrestore(&phba->hbalock, flags);
2397 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002398 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002399 case IOSTAT_LOCAL_REJECT:
James Smartd7c255b2008-08-24 21:50:00 -04002400 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05002401 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04002402 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
2403 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05002404 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04002405 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05002406 }
2407
2408 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2409 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2410 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2411 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2412 /*
2413 * This is a response for a BG enabled
2414 * cmd. Parse BG error
2415 */
2416 lpfc_parse_bg_err(phba, lpfc_cmd,
2417 pIocbOut);
2418 break;
2419 } else {
2420 lpfc_printf_vlog(vport, KERN_WARNING,
2421 LOG_BG,
2422 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04002423 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002424 }
2425 }
2426
2427 /* else: fall through */
dea31012005-04-17 16:05:31 -05002428 default:
2429 cmd->result = ScsiResult(DID_ERROR, 0);
2430 break;
2431 }
2432
James Smart58da1ff2008-04-07 10:15:56 -04002433 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002434 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04002435 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2436 SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -05002437 } else {
2438 cmd->result = ScsiResult(DID_OK, 0);
2439 }
2440
2441 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2442 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2443
James Smarte8b62012007-08-02 11:10:09 -04002444 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2445 "0710 Iodone <%d/%d> cmd %p, error "
2446 "x%x SNS x%x x%x Data: x%x x%x\n",
2447 cmd->device->id, cmd->device->lun, cmd,
2448 cmd->result, *lp, *(lp + 3), cmd->retries,
2449 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05002450 }
2451
James Smartea2151b2008-09-07 11:52:10 -04002452 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002453 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04002454 if (vport->cfg_max_scsicmpl_time &&
2455 time_after(jiffies, lpfc_cmd->start_time +
2456 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04002457 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002458 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2459 if (pnode->cmd_qdepth >
2460 atomic_read(&pnode->cmd_pending) &&
2461 (atomic_read(&pnode->cmd_pending) >
2462 LPFC_MIN_TGT_QDEPTH) &&
2463 ((cmd->cmnd[0] == READ_10) ||
2464 (cmd->cmnd[0] == WRITE_10)))
2465 pnode->cmd_qdepth =
2466 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04002467
James Smart109f6ed2008-12-04 22:39:08 -05002468 pnode->last_change_time = jiffies;
2469 }
James Smarta257bf92009-04-06 18:48:10 -04002470 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002471 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04002472 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04002473 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05002474 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04002475 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04002476 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
2477 / 100;
2478 depth = depth ? depth : 1;
2479 pnode->cmd_qdepth += depth;
2480 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
2481 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05002482 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04002483 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002484 }
James Smart977b5a02008-09-07 11:52:04 -04002485 }
2486
James Smart1dcb58e2007-04-25 09:51:30 -04002487 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04002488
2489 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2490 queue_depth = cmd->device->queue_depth;
2491 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05002492 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002493
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002494 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05002495 /*
2496 * If there is a thread waiting for command completion
2497 * wake up the thread.
2498 */
James Smarta257bf92009-04-06 18:48:10 -04002499 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002500 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002501 if (lpfc_cmd->waitq)
2502 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002503 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002504 lpfc_release_scsi_buf(phba, lpfc_cmd);
2505 return;
2506 }
2507
James Smart92d7f7b2007-06-17 19:56:38 -05002508 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04002509 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05002510
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002511 /*
2512 * Check for queue full. If the lun is reporting queue full, then
2513 * back off the lun queue depth to prevent target overloads.
2514 */
James Smart58da1ff2008-04-07 10:15:56 -04002515 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2516 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04002517 shost_for_each_device(tmp_sdev, shost) {
2518 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002519 continue;
2520 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05002521 tmp_sdev->queue_depth-1);
2522 if (depth <= 0)
2523 continue;
James Smarte8b62012007-08-02 11:10:09 -04002524 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2525 "0711 detected queue full - lun queue "
2526 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04002527 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05002528 pnode,
2529 tmp_sdev->lun,
2530 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002531 }
2532 }
2533
James Smartfa61a542008-01-11 01:52:42 -05002534 /*
2535 * If there is a thread waiting for command completion
2536 * wake up the thread.
2537 */
James Smarta257bf92009-04-06 18:48:10 -04002538 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002539 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002540 if (lpfc_cmd->waitq)
2541 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002542 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002543
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002544 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002545}
2546
James Smart34b02dc2008-08-24 21:49:55 -04002547/**
James Smart3621a712009-04-06 18:47:14 -04002548 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002549 * @data: A pointer to the immediate command data portion of the IOCB.
2550 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2551 *
2552 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2553 * byte swapping the data to big endian format for transmission on the wire.
2554 **/
2555static void
2556lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2557{
2558 int i, j;
2559 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2560 i += sizeof(uint32_t), j++) {
2561 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2562 }
2563}
2564
James Smart9bad7672008-12-04 22:39:02 -05002565/**
James Smartf1126682009-06-10 17:22:44 -04002566 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002567 * @vport: The virtual port for which this call is being executed.
2568 * @lpfc_cmd: The scsi command which needs to send.
2569 * @pnode: Pointer to lpfc_nodelist.
2570 *
2571 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002572 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002573 **/
dea31012005-04-17 16:05:31 -05002574static void
James Smartf1126682009-06-10 17:22:44 -04002575lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002576 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002577{
James Smart2e0fef82007-06-17 19:56:36 -05002578 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002579 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2580 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2581 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2582 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2583 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002584 char tag[2];
dea31012005-04-17 16:05:31 -05002585
James Smart58da1ff2008-04-07 10:15:56 -04002586 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2587 return;
2588
dea31012005-04-17 16:05:31 -05002589 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002590 /* clear task management bits */
2591 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002592
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002593 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2594 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002595
2596 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2597
James Smart7e2b19f2007-10-29 11:00:39 -04002598 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2599 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002600 case HEAD_OF_QUEUE_TAG:
2601 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2602 break;
2603 case ORDERED_QUEUE_TAG:
2604 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2605 break;
2606 default:
2607 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2608 break;
2609 }
2610 } else
2611 fcp_cmnd->fcpCntl1 = 0;
2612
2613 /*
2614 * There are three possibilities here - use scatter-gather segment, use
2615 * the single mapping, or neither. Start the lpfc command prep by
2616 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2617 * data bde entry.
2618 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002619 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002620 if (datadir == DMA_TO_DEVICE) {
2621 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002622 if (phba->sli_rev < LPFC_SLI_REV4) {
2623 iocb_cmd->un.fcpi.fcpi_parm = 0;
2624 iocb_cmd->ulpPU = 0;
2625 } else
2626 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002627 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2628 phba->fc4OutputRequests++;
2629 } else {
2630 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2631 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002632 fcp_cmnd->fcpCntl3 = READ_DATA;
2633 phba->fc4InputRequests++;
2634 }
2635 } else {
2636 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2637 iocb_cmd->un.fcpi.fcpi_parm = 0;
2638 iocb_cmd->ulpPU = 0;
2639 fcp_cmnd->fcpCntl3 = 0;
2640 phba->fc4ControlRequests++;
2641 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002642 if (phba->sli_rev == 3 &&
2643 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002644 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002645 /*
2646 * Finish initializing those IOCB fields that are independent
2647 * of the scsi_cmnd request_buffer
2648 */
2649 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2650 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2651 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002652 else
2653 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002654
2655 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2656 piocbq->context1 = lpfc_cmd;
2657 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2658 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002659 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002660}
2661
James Smart9bad7672008-12-04 22:39:02 -05002662/**
James Smartf1126682009-06-10 17:22:44 -04002663 * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002664 * @vport: The virtual port for which this call is being executed.
2665 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2666 * @lun: Logical unit number.
2667 * @task_mgmt_cmd: SCSI task management command.
2668 *
James Smart3772a992009-05-22 14:50:54 -04002669 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2670 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002671 *
2672 * Return codes:
2673 * 0 - Error
2674 * 1 - Success
2675 **/
dea31012005-04-17 16:05:31 -05002676static int
James Smartf1126682009-06-10 17:22:44 -04002677lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05002678 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04002679 unsigned int lun,
dea31012005-04-17 16:05:31 -05002680 uint8_t task_mgmt_cmd)
2681{
dea31012005-04-17 16:05:31 -05002682 struct lpfc_iocbq *piocbq;
2683 IOCB_t *piocb;
2684 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04002685 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05002686 struct lpfc_nodelist *ndlp = rdata->pnode;
2687
James Smart58da1ff2008-04-07 10:15:56 -04002688 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2689 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05002690 return 0;
dea31012005-04-17 16:05:31 -05002691
dea31012005-04-17 16:05:31 -05002692 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002693 piocbq->vport = vport;
2694
dea31012005-04-17 16:05:31 -05002695 piocb = &piocbq->iocb;
2696
2697 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04002698 /* Clear out any old data in the FCP command area */
2699 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2700 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002701 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05002702 if (vport->phba->sli_rev == 3 &&
2703 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002704 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002705 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05002706 piocb->ulpContext = ndlp->nlp_rpi;
2707 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2708 piocb->ulpFCP2Rcvy = 1;
2709 }
2710 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2711
2712 /* ulpTimeout is only one byte */
2713 if (lpfc_cmd->timeout > 0xff) {
2714 /*
2715 * Do not timeout the command at the firmware level.
2716 * The driver will provide the timeout mechanism.
2717 */
2718 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04002719 } else
dea31012005-04-17 16:05:31 -05002720 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04002721
2722 if (vport->phba->sli_rev == LPFC_SLI_REV4)
2723 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002724
James Smart2e0fef82007-06-17 19:56:36 -05002725 return 1;
dea31012005-04-17 16:05:31 -05002726}
2727
James Smart9bad7672008-12-04 22:39:02 -05002728/**
James Smart3772a992009-05-22 14:50:54 -04002729 * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2730 * @phba: The hba struct for which this call is being executed.
2731 * @dev_grp: The HBA PCI-Device group number.
2732 *
2733 * This routine sets up the SCSI interface API function jump table in @phba
2734 * struct.
2735 * Returns: 0 - success, -ENODEV - failure.
2736 **/
2737int
2738lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2739{
2740
James Smartf1126682009-06-10 17:22:44 -04002741 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2742 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2743 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2744
James Smart3772a992009-05-22 14:50:54 -04002745 switch (dev_grp) {
2746 case LPFC_PCI_DEV_LP:
2747 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2748 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04002749 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2750 break;
James Smartda0436e2009-05-22 14:51:39 -04002751 case LPFC_PCI_DEV_OC:
2752 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2753 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04002754 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2755 break;
James Smart3772a992009-05-22 14:50:54 -04002756 default:
2757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2758 "1418 Invalid HBA PCI-device group: 0x%x\n",
2759 dev_grp);
2760 return -ENODEV;
2761 break;
2762 }
2763 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2764 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05002765 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04002766 return 0;
2767}
2768
2769/**
James Smart3621a712009-04-06 18:47:14 -04002770 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05002771 * @phba: The Hba for which this call is being executed.
2772 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2773 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2774 *
2775 * This routine is IOCB completion routine for device reset and target reset
2776 * routine. This routine release scsi buffer associated with lpfc_cmd.
2777 **/
James Smart7054a602007-04-25 09:52:34 -04002778static void
2779lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2780 struct lpfc_iocbq *cmdiocbq,
2781 struct lpfc_iocbq *rspiocbq)
2782{
2783 struct lpfc_scsi_buf *lpfc_cmd =
2784 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2785 if (lpfc_cmd)
2786 lpfc_release_scsi_buf(phba, lpfc_cmd);
2787 return;
2788}
2789
James Smart9bad7672008-12-04 22:39:02 -05002790/**
James Smart3621a712009-04-06 18:47:14 -04002791 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05002792 * @host: The scsi host for which this call is being executed.
2793 *
2794 * This routine provides module information about hba.
2795 *
2796 * Reutrn code:
2797 * Pointer to char - Success.
2798 **/
dea31012005-04-17 16:05:31 -05002799const char *
2800lpfc_info(struct Scsi_Host *host)
2801{
James Smart2e0fef82007-06-17 19:56:36 -05002802 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2803 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002804 int len;
2805 static char lpfcinfobuf[384];
2806
2807 memset(lpfcinfobuf,0,384);
2808 if (phba && phba->pcidev){
2809 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2810 len = strlen(lpfcinfobuf);
2811 snprintf(lpfcinfobuf + len,
2812 384-len,
2813 " on PCI bus %02x device %02x irq %d",
2814 phba->pcidev->bus->number,
2815 phba->pcidev->devfn,
2816 phba->pcidev->irq);
2817 len = strlen(lpfcinfobuf);
2818 if (phba->Port[0]) {
2819 snprintf(lpfcinfobuf + len,
2820 384-len,
2821 " port %s",
2822 phba->Port);
2823 }
James Smart65467b62010-01-26 23:08:29 -05002824 len = strlen(lpfcinfobuf);
2825 if (phba->sli4_hba.link_state.logical_speed) {
2826 snprintf(lpfcinfobuf + len,
2827 384-len,
2828 " Logical Link Speed: %d Mbps",
2829 phba->sli4_hba.link_state.logical_speed * 10);
2830 }
dea31012005-04-17 16:05:31 -05002831 }
2832 return lpfcinfobuf;
2833}
2834
James Smart9bad7672008-12-04 22:39:02 -05002835/**
James Smart3621a712009-04-06 18:47:14 -04002836 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05002837 * @phba: The Hba for which this call is being executed.
2838 *
2839 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2840 * The default value of cfg_poll_tmo is 10 milliseconds.
2841 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002842static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2843{
2844 unsigned long poll_tmo_expires =
2845 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2846
2847 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2848 mod_timer(&phba->fcp_poll_timer,
2849 poll_tmo_expires);
2850}
2851
James Smart9bad7672008-12-04 22:39:02 -05002852/**
James Smart3621a712009-04-06 18:47:14 -04002853 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05002854 * @phba: The Hba for which this call is being executed.
2855 *
2856 * This routine starts the fcp_poll_timer of @phba.
2857 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002858void lpfc_poll_start_timer(struct lpfc_hba * phba)
2859{
2860 lpfc_poll_rearm_timer(phba);
2861}
2862
James Smart9bad7672008-12-04 22:39:02 -05002863/**
James Smart3621a712009-04-06 18:47:14 -04002864 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05002865 * @ptr: Map to lpfc_hba data structure pointer.
2866 *
2867 * This routine restarts fcp_poll timer, when FCP ring polling is enable
2868 * and FCP Ring interrupt is disable.
2869 **/
2870
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002871void lpfc_poll_timeout(unsigned long ptr)
2872{
James Smart2e0fef82007-06-17 19:56:36 -05002873 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002874
2875 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04002876 lpfc_sli_handle_fast_ring_event(phba,
2877 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2878
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002879 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2880 lpfc_poll_rearm_timer(phba);
2881 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002882}
2883
James Smart9bad7672008-12-04 22:39:02 -05002884/**
James Smart3621a712009-04-06 18:47:14 -04002885 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05002886 * @cmnd: Pointer to scsi_cmnd data structure.
2887 * @done: Pointer to done routine.
2888 *
2889 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2890 * This routine prepares an IOCB from scsi command and provides to firmware.
2891 * The @done callback is invoked after driver finished processing the command.
2892 *
2893 * Return value :
2894 * 0 - Success
2895 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2896 **/
dea31012005-04-17 16:05:31 -05002897static int
2898lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
2899{
James Smart2e0fef82007-06-17 19:56:36 -05002900 struct Scsi_Host *shost = cmnd->device->host;
2901 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2902 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002903 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05002904 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002905 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002906 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002907 int err;
dea31012005-04-17 16:05:31 -05002908
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002909 err = fc_remote_port_chkready(rport);
2910 if (err) {
2911 cmnd->result = err;
dea31012005-04-17 16:05:31 -05002912 goto out_fail_command;
2913 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002914 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05002915
James Smarte2a0a9d2008-12-04 22:40:02 -05002916 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2917 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2918
James Smart6a9c52c2009-10-02 15:16:51 -04002919 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2920 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
2921 " op:%02x str=%s without registering for"
2922 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002923 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2924 dif_op_str[scsi_get_prot_op(cmnd)]);
2925 goto out_fail_command;
2926 }
2927
dea31012005-04-17 16:05:31 -05002928 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002929 * Catch race where our node has transitioned, but the
2930 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05002931 */
James Smartb522d7d2008-09-07 11:51:56 -04002932 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2933 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2934 goto out_fail_command;
2935 }
James Smart7dc517d2010-07-14 15:32:10 -04002936 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
James Smart977b5a02008-09-07 11:52:04 -04002937 goto out_host_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05002938
James Smarted957682007-06-17 19:56:37 -05002939 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05002940 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05002941 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002942
James Smarte8b62012007-08-02 11:10:09 -04002943 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2944 "0707 driver's buffer pool is empty, "
2945 "IO busied\n");
dea31012005-04-17 16:05:31 -05002946 goto out_host_busy;
2947 }
2948
2949 /*
2950 * Store the midlayer's command structure for the completion phase
2951 * and complete the command initialization.
2952 */
2953 lpfc_cmd->pCmd = cmnd;
2954 lpfc_cmd->rdata = rdata;
2955 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04002956 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05002957 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
2958 cmnd->scsi_done = done;
2959
James Smarte2a0a9d2008-12-04 22:40:02 -05002960 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04002961 if (vport->phba->cfg_enable_bg) {
2962 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002963 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
2964 "str=%s\n",
2965 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2966 dif_op_str[scsi_get_prot_op(cmnd)]);
James Smart6a9c52c2009-10-02 15:16:51 -04002967 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002968 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04002969 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002970 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2971 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2972 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2973 cmnd->cmnd[9]);
James Smart6a9c52c2009-10-02 15:16:51 -04002974 if (cmnd->cmnd[0] == READ_10)
2975 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002976 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002977 "count %u\n",
2978 (unsigned long long)scsi_get_lba(cmnd),
2979 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04002980 else if (cmnd->cmnd[0] == WRITE_10)
2981 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002982 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002983 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002984 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002985 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05002986 cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04002987 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002988
2989 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
2990 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04002991 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05002992 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart6a9c52c2009-10-02 15:16:51 -04002993 "9038 BLKGRD: rcvd unprotected cmd:"
2994 "%02x op:%02x str=%s\n",
2995 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2996 dif_op_str[scsi_get_prot_op(cmnd)]);
2997 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2998 "9039 BLKGRD: CDB: %02x %02x %02x "
2999 "%02x %02x %02x %02x %02x %02x %02x\n",
3000 cmnd->cmnd[0], cmnd->cmnd[1],
3001 cmnd->cmnd[2], cmnd->cmnd[3],
3002 cmnd->cmnd[4], cmnd->cmnd[5],
3003 cmnd->cmnd[6], cmnd->cmnd[7],
3004 cmnd->cmnd[8], cmnd->cmnd[9]);
3005 if (cmnd->cmnd[0] == READ_10)
3006 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3007 "9040 dbg: READ @ sector %llu, "
3008 "count %u\n",
3009 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003010 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04003011 else if (cmnd->cmnd[0] == WRITE_10)
3012 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003013 "9041 dbg: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003014 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05003015 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003016 blk_rq_sectors(cmnd->request), cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04003017 else
3018 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003019 "9042 dbg: parser not implemented\n");
James Smart6a9c52c2009-10-02 15:16:51 -04003020 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003021 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3022 }
3023
dea31012005-04-17 16:05:31 -05003024 if (err)
3025 goto out_host_busy_free_buf;
3026
James Smart2e0fef82007-06-17 19:56:36 -05003027 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05003028
James Smart977b5a02008-09-07 11:52:04 -04003029 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04003030 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05003031 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05003032 if (err) {
3033 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05003034 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05003035 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003036 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003037 spin_unlock(shost->host_lock);
3038 lpfc_sli_handle_fast_ring_event(phba,
3039 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3040
3041 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003042 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3043 lpfc_poll_rearm_timer(phba);
3044 }
3045
dea31012005-04-17 16:05:31 -05003046 return 0;
3047
3048 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04003049 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003050 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003051 out_host_busy:
3052 return SCSI_MLQUEUE_HOST_BUSY;
3053
3054 out_fail_command:
3055 done(cmnd);
3056 return 0;
3057}
3058
James Smart9bad7672008-12-04 22:39:02 -05003059/**
James Smart3621a712009-04-06 18:47:14 -04003060 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003061 * @cmnd: Pointer to scsi_cmnd data structure.
3062 *
3063 * This routine aborts @cmnd pending in base driver.
3064 *
3065 * Return code :
3066 * 0x2003 - Error
3067 * 0x2002 - Success
3068 **/
dea31012005-04-17 16:05:31 -05003069static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05003070lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003071{
James Smart2e0fef82007-06-17 19:56:36 -05003072 struct Scsi_Host *shost = cmnd->device->host;
3073 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3074 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003075 struct lpfc_iocbq *iocb;
3076 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05003077 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05003078 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003079 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05003080 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003081
James Smart589a52d2010-07-14 15:30:54 -04003082 ret = fc_block_scsi_eh(cmnd);
3083 if (ret)
3084 return ret;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003085 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04003086 if (!lpfc_cmd) {
3087 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3088 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
3089 "x%x ID %d "
3090 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3091 cmnd->device->lun, cmnd->serial_number);
3092 return SUCCESS;
3093 }
dea31012005-04-17 16:05:31 -05003094
3095 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003096 * If pCmd field of the corresponding lpfc_scsi_buf structure
3097 * points to a different SCSI command, then the driver has
3098 * already completed this command, but the midlayer did not
3099 * see the completion before the eh fired. Just return
3100 * SUCCESS.
dea31012005-04-17 16:05:31 -05003101 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003102 iocb = &lpfc_cmd->cur_iocbq;
3103 if (lpfc_cmd->pCmd != cmnd)
3104 goto out;
dea31012005-04-17 16:05:31 -05003105
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003106 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05003107
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003108 abtsiocb = lpfc_sli_get_iocbq(phba);
3109 if (abtsiocb == NULL) {
3110 ret = FAILED;
dea31012005-04-17 16:05:31 -05003111 goto out;
3112 }
3113
dea31012005-04-17 16:05:31 -05003114 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003115 * The scsi command can not be in txq and it is in flight because the
3116 * pCmd is still pointig at the SCSI command we have to abort. There
3117 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05003118 */
dea31012005-04-17 16:05:31 -05003119
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003120 cmd = &iocb->iocb;
3121 icmd = &abtsiocb->iocb;
3122 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3123 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04003124 if (phba->sli_rev == LPFC_SLI_REV4)
3125 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3126 else
3127 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003128
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003129 icmd->ulpLe = 1;
3130 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05003131
3132 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
3133 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05003134 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05003135
James Smart2e0fef82007-06-17 19:56:36 -05003136 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003137 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3138 else
3139 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05003140
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003141 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05003142 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04003143 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3144 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003145 lpfc_sli_release_iocbq(phba, abtsiocb);
3146 ret = FAILED;
3147 goto out;
3148 }
3149
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003150 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04003151 lpfc_sli_handle_fast_ring_event(phba,
3152 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003153
James Smartfa61a542008-01-11 01:52:42 -05003154 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003155 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05003156 wait_event_timeout(waitq,
3157 (lpfc_cmd->pCmd != cmnd),
3158 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003159
James Smartfa61a542008-01-11 01:52:42 -05003160 spin_lock_irq(shost->host_lock);
3161 lpfc_cmd->waitq = NULL;
3162 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003163
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003164 if (lpfc_cmd->pCmd == cmnd) {
3165 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04003166 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3167 "0748 abort handler timed out waiting "
3168 "for abort to complete: ret %#x, ID %d, "
3169 "LUN %d, snum %#lx\n",
3170 ret, cmnd->device->id, cmnd->device->lun,
3171 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05003172 }
3173
3174 out:
James Smarte8b62012007-08-02 11:10:09 -04003175 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3176 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3177 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3178 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003179 return ret;
dea31012005-04-17 16:05:31 -05003180}
3181
James Smartbbb9d182009-06-10 17:23:16 -04003182static char *
3183lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3184{
3185 switch (task_mgmt_cmd) {
3186 case FCP_ABORT_TASK_SET:
3187 return "ABORT_TASK_SET";
3188 case FCP_CLEAR_TASK_SET:
3189 return "FCP_CLEAR_TASK_SET";
3190 case FCP_BUS_RESET:
3191 return "FCP_BUS_RESET";
3192 case FCP_LUN_RESET:
3193 return "FCP_LUN_RESET";
3194 case FCP_TARGET_RESET:
3195 return "FCP_TARGET_RESET";
3196 case FCP_CLEAR_ACA:
3197 return "FCP_CLEAR_ACA";
3198 case FCP_TERMINATE_TASK:
3199 return "FCP_TERMINATE_TASK";
3200 default:
3201 return "unknown";
3202 }
3203}
3204
3205/**
3206 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3207 * @vport: The virtual port for which this call is being executed.
3208 * @rdata: Pointer to remote port local data
3209 * @tgt_id: Target ID of remote device.
3210 * @lun_id: Lun number for the TMF
3211 * @task_mgmt_cmd: type of TMF to send
3212 *
3213 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3214 * a remote port.
3215 *
3216 * Return Code:
3217 * 0x2003 - Error
3218 * 0x2002 - Success.
3219 **/
3220static int
3221lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3222 unsigned tgt_id, unsigned int lun_id,
3223 uint8_t task_mgmt_cmd)
3224{
3225 struct lpfc_hba *phba = vport->phba;
3226 struct lpfc_scsi_buf *lpfc_cmd;
3227 struct lpfc_iocbq *iocbq;
3228 struct lpfc_iocbq *iocbqrsp;
3229 int ret;
3230 int status;
3231
3232 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
3233 return FAILED;
3234
3235 lpfc_cmd = lpfc_get_scsi_buf(phba);
3236 if (lpfc_cmd == NULL)
3237 return FAILED;
3238 lpfc_cmd->timeout = 60;
3239 lpfc_cmd->rdata = rdata;
3240
3241 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3242 task_mgmt_cmd);
3243 if (!status) {
3244 lpfc_release_scsi_buf(phba, lpfc_cmd);
3245 return FAILED;
3246 }
3247
3248 iocbq = &lpfc_cmd->cur_iocbq;
3249 iocbqrsp = lpfc_sli_get_iocbq(phba);
3250 if (iocbqrsp == NULL) {
3251 lpfc_release_scsi_buf(phba, lpfc_cmd);
3252 return FAILED;
3253 }
3254
3255 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3256 "0702 Issue %s to TGT %d LUN %d "
3257 "rpi x%x nlp_flag x%x\n",
3258 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
3259 rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
3260
3261 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3262 iocbq, iocbqrsp, lpfc_cmd->timeout);
3263 if (status != IOCB_SUCCESS) {
3264 if (status == IOCB_TIMEDOUT) {
3265 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3266 ret = TIMEOUT_ERROR;
3267 } else
3268 ret = FAILED;
3269 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3270 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3271 "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
3272 lpfc_taskmgmt_name(task_mgmt_cmd),
3273 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
3274 iocbqrsp->iocb.un.ulpWord[4]);
James Smart2a9bf3d2010-06-07 15:24:45 -04003275 } else if (status == IOCB_BUSY)
3276 ret = FAILED;
3277 else
James Smartbbb9d182009-06-10 17:23:16 -04003278 ret = SUCCESS;
3279
3280 lpfc_sli_release_iocbq(phba, iocbqrsp);
3281
3282 if (ret != TIMEOUT_ERROR)
3283 lpfc_release_scsi_buf(phba, lpfc_cmd);
3284
3285 return ret;
3286}
3287
3288/**
3289 * lpfc_chk_tgt_mapped -
3290 * @vport: The virtual port to check on
3291 * @cmnd: Pointer to scsi_cmnd data structure.
3292 *
3293 * This routine delays until the scsi target (aka rport) for the
3294 * command exists (is present and logged in) or we declare it non-existent.
3295 *
3296 * Return code :
3297 * 0x2003 - Error
3298 * 0x2002 - Success
3299 **/
3300static int
3301lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3302{
3303 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003304 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003305 unsigned long later;
3306
James Smart1c6f4ef52009-11-18 15:40:49 -05003307 if (!rdata) {
3308 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3309 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
3310 return FAILED;
3311 }
3312 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003313 /*
3314 * If target is not in a MAPPED state, delay until
3315 * target is rediscovered or devloss timeout expires.
3316 */
3317 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3318 while (time_after(later, jiffies)) {
3319 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3320 return FAILED;
3321 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3322 return SUCCESS;
3323 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3324 rdata = cmnd->device->hostdata;
3325 if (!rdata)
3326 return FAILED;
3327 pnode = rdata->pnode;
3328 }
3329 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3330 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3331 return FAILED;
3332 return SUCCESS;
3333}
3334
3335/**
3336 * lpfc_reset_flush_io_context -
3337 * @vport: The virtual port (scsi_host) for the flush context
3338 * @tgt_id: If aborting by Target contect - specifies the target id
3339 * @lun_id: If aborting by Lun context - specifies the lun id
3340 * @context: specifies the context level to flush at.
3341 *
3342 * After a reset condition via TMF, we need to flush orphaned i/o
3343 * contexts from the adapter. This routine aborts any contexts
3344 * outstanding, then waits for their completions. The wait is
3345 * bounded by devloss_tmo though.
3346 *
3347 * Return code :
3348 * 0x2003 - Error
3349 * 0x2002 - Success
3350 **/
3351static int
3352lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3353 uint64_t lun_id, lpfc_ctx_cmd context)
3354{
3355 struct lpfc_hba *phba = vport->phba;
3356 unsigned long later;
3357 int cnt;
3358
3359 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3360 if (cnt)
3361 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3362 tgt_id, lun_id, context);
3363 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3364 while (time_after(later, jiffies) && cnt) {
3365 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3366 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3367 }
3368 if (cnt) {
3369 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3370 "0724 I/O flush failure for context %s : cnt x%x\n",
3371 ((context == LPFC_CTX_LUN) ? "LUN" :
3372 ((context == LPFC_CTX_TGT) ? "TGT" :
3373 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3374 cnt);
3375 return FAILED;
3376 }
3377 return SUCCESS;
3378}
3379
James Smart9bad7672008-12-04 22:39:02 -05003380/**
James Smart3621a712009-04-06 18:47:14 -04003381 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05003382 * @cmnd: Pointer to scsi_cmnd data structure.
3383 *
James Smartbbb9d182009-06-10 17:23:16 -04003384 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05003385 * command.
3386 *
3387 * Return code :
3388 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04003389 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003390 **/
dea31012005-04-17 16:05:31 -05003391static int
James Smart7054a602007-04-25 09:52:34 -04003392lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003393{
James Smart2e0fef82007-06-17 19:56:36 -05003394 struct Scsi_Host *shost = cmnd->device->host;
3395 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003396 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003397 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003398 unsigned tgt_id = cmnd->device->id;
3399 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04003400 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003401 int status;
dea31012005-04-17 16:05:31 -05003402
James Smart1c6f4ef52009-11-18 15:40:49 -05003403 if (!rdata) {
3404 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3405 "0798 Device Reset rport failure: rdata x%p\n", rdata);
3406 return FAILED;
3407 }
3408 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003409 status = fc_block_scsi_eh(cmnd);
3410 if (status)
3411 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003412
3413 status = lpfc_chk_tgt_mapped(vport, cmnd);
3414 if (status == FAILED) {
3415 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3416 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3417 return FAILED;
3418 }
3419
3420 scsi_event.event_type = FC_REG_SCSI_EVENT;
3421 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3422 scsi_event.lun = lun_id;
3423 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3424 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3425
3426 fc_host_post_vendor_event(shost, fc_get_event_number(),
3427 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3428
3429 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3430 FCP_LUN_RESET);
3431
3432 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3433 "0713 SCSI layer issued Device Reset (%d, %d) "
3434 "return x%x\n", tgt_id, lun_id, status);
3435
dea31012005-04-17 16:05:31 -05003436 /*
James Smartbbb9d182009-06-10 17:23:16 -04003437 * We have to clean up i/o as : they may be orphaned by the TMF;
3438 * or if the TMF failed, they may be in an indeterminate state.
3439 * So, continue on.
3440 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05003441 */
James Smartbbb9d182009-06-10 17:23:16 -04003442 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3443 LPFC_CTX_LUN);
3444 return status;
3445}
3446
3447/**
3448 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3449 * @cmnd: Pointer to scsi_cmnd data structure.
3450 *
3451 * This routine does a target reset by sending a TARGET_RESET task management
3452 * command.
3453 *
3454 * Return code :
3455 * 0x2003 - Error
3456 * 0x2002 - Success
3457 **/
3458static int
3459lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3460{
3461 struct Scsi_Host *shost = cmnd->device->host;
3462 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3463 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003464 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003465 unsigned tgt_id = cmnd->device->id;
3466 unsigned int lun_id = cmnd->device->lun;
3467 struct lpfc_scsi_event_header scsi_event;
3468 int status;
3469
James Smart1c6f4ef52009-11-18 15:40:49 -05003470 if (!rdata) {
3471 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3472 "0799 Target Reset rport failure: rdata x%p\n", rdata);
3473 return FAILED;
3474 }
3475 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003476 status = fc_block_scsi_eh(cmnd);
3477 if (status)
3478 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003479
3480 status = lpfc_chk_tgt_mapped(vport, cmnd);
3481 if (status == FAILED) {
3482 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3483 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3484 return FAILED;
dea31012005-04-17 16:05:31 -05003485 }
James Smartea2151b2008-09-07 11:52:10 -04003486
3487 scsi_event.event_type = FC_REG_SCSI_EVENT;
3488 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3489 scsi_event.lun = 0;
3490 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3491 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3492
James Smartbbb9d182009-06-10 17:23:16 -04003493 fc_host_post_vendor_event(shost, fc_get_event_number(),
3494 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04003495
James Smartbbb9d182009-06-10 17:23:16 -04003496 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3497 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05003498
James Smarte8b62012007-08-02 11:10:09 -04003499 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04003500 "0723 SCSI layer issued Target Reset (%d, %d) "
3501 "return x%x\n", tgt_id, lun_id, status);
3502
3503 /*
3504 * We have to clean up i/o as : they may be orphaned by the TMF;
3505 * or if the TMF failed, they may be in an indeterminate state.
3506 * So, continue on.
3507 * We will report success if all the i/o aborts successfully.
3508 */
3509 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3510 LPFC_CTX_TGT);
3511 return status;
dea31012005-04-17 16:05:31 -05003512}
3513
James Smart9bad7672008-12-04 22:39:02 -05003514/**
James Smart3621a712009-04-06 18:47:14 -04003515 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003516 * @cmnd: Pointer to scsi_cmnd data structure.
3517 *
James Smartbbb9d182009-06-10 17:23:16 -04003518 * This routine does target reset to all targets on @cmnd->device->host.
3519 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05003520 *
James Smartbbb9d182009-06-10 17:23:16 -04003521 * Return code :
3522 * 0x2003 - Error
3523 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003524 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003525static int
James Smart7054a602007-04-25 09:52:34 -04003526lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003527{
James Smart2e0fef82007-06-17 19:56:36 -05003528 struct Scsi_Host *shost = cmnd->device->host;
3529 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003530 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04003531 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003532 int match;
3533 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04003534
3535 scsi_event.event_type = FC_REG_SCSI_EVENT;
3536 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3537 scsi_event.lun = 0;
3538 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3539 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3540
James Smartbbb9d182009-06-10 17:23:16 -04003541 fc_host_post_vendor_event(shost, fc_get_event_number(),
3542 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05003543
James Smart589a52d2010-07-14 15:30:54 -04003544 ret = fc_block_scsi_eh(cmnd);
3545 if (ret)
3546 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04003547
dea31012005-04-17 16:05:31 -05003548 /*
3549 * Since the driver manages a single bus device, reset all
3550 * targets known to the driver. Should any target reset
3551 * fail, this routine returns failure to the midlayer.
3552 */
James Smarte17da182006-07-06 15:49:25 -04003553 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04003554 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05003555 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003556 spin_lock_irq(shost->host_lock);
3557 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003558 if (!NLP_CHK_NODE_ACT(ndlp))
3559 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003560 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04003561 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04003562 ndlp->rport) {
dea31012005-04-17 16:05:31 -05003563 match = 1;
3564 break;
3565 }
3566 }
James Smart2e0fef82007-06-17 19:56:36 -05003567 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003568 if (!match)
3569 continue;
James Smartbbb9d182009-06-10 17:23:16 -04003570
3571 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3572 i, 0, FCP_TARGET_RESET);
3573
3574 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04003575 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3576 "0700 Bus Reset on target %d failed\n",
3577 i);
James Smart915caaa2008-06-14 22:52:38 -04003578 ret = FAILED;
dea31012005-04-17 16:05:31 -05003579 }
3580 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003581 /*
James Smartbbb9d182009-06-10 17:23:16 -04003582 * We have to clean up i/o as : they may be orphaned by the TMFs
3583 * above; or if any of the TMFs failed, they may be in an
3584 * indeterminate state.
3585 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003586 */
James Smartbbb9d182009-06-10 17:23:16 -04003587
3588 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3589 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003590 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04003591
James Smarte8b62012007-08-02 11:10:09 -04003592 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3593 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05003594 return ret;
3595}
3596
James Smart9bad7672008-12-04 22:39:02 -05003597/**
James Smart3621a712009-04-06 18:47:14 -04003598 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05003599 * @sdev: Pointer to scsi_device.
3600 *
3601 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3602 * globally available list of scsi buffers. This routine also makes sure scsi
3603 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3604 * of scsi buffer exists for the lifetime of the driver.
3605 *
3606 * Return codes:
3607 * non-0 - Error
3608 * 0 - Success
3609 **/
dea31012005-04-17 16:05:31 -05003610static int
dea31012005-04-17 16:05:31 -05003611lpfc_slave_alloc(struct scsi_device *sdev)
3612{
James Smart2e0fef82007-06-17 19:56:36 -05003613 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3614 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003615 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04003616 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05003617 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04003618 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04003619 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05003620
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003621 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05003622 return -ENXIO;
3623
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003624 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04003625 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05003626
3627 /*
3628 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3629 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04003630 * HBA limit conveyed to the midlayer via the host structure. The
3631 * formula accounts for the lun_queue_depth + error handlers + 1
3632 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003633 */
3634 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003635 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003636
James Smartd7c47992010-06-08 18:31:54 -04003637 /* If allocated buffers are enough do nothing */
3638 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
3639 return 0;
3640
James Smart92d7f7b2007-06-17 19:56:38 -05003641 /* Allow some exchanges to be available always to complete discovery */
3642 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003643 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3644 "0704 At limitation of %d preallocated "
3645 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003646 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003647 /* Allow some exchanges to be available always to complete discovery */
3648 } else if (total + num_to_alloc >
3649 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003650 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3651 "0705 Allocation request of %d "
3652 "command buffers will exceed max of %d. "
3653 "Reducing allocation request to %d.\n",
3654 num_to_alloc, phba->cfg_hba_queue_depth,
3655 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003656 num_to_alloc = phba->cfg_hba_queue_depth - total;
3657 }
James Smart3772a992009-05-22 14:50:54 -04003658 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3659 if (num_to_alloc != num_allocated) {
3660 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3661 "0708 Allocation request of %d "
3662 "command buffers did not succeed. "
3663 "Allocated %d buffers.\n",
3664 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05003665 }
James Smart1c6f4ef52009-11-18 15:40:49 -05003666 if (num_allocated > 0)
3667 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05003668 return 0;
3669}
3670
James Smart9bad7672008-12-04 22:39:02 -05003671/**
James Smart3621a712009-04-06 18:47:14 -04003672 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05003673 * @sdev: Pointer to scsi_device.
3674 *
3675 * This routine configures following items
3676 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05003677 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3678 *
3679 * Return codes:
3680 * 0 - Success
3681 **/
dea31012005-04-17 16:05:31 -05003682static int
3683lpfc_slave_configure(struct scsi_device *sdev)
3684{
James Smart2e0fef82007-06-17 19:56:36 -05003685 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3686 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003687
3688 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04003689 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003690 else
James Smart3de2a652007-08-02 11:09:59 -04003691 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003692
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003693 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003694 lpfc_sli_handle_fast_ring_event(phba,
3695 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003696 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3697 lpfc_poll_rearm_timer(phba);
3698 }
3699
dea31012005-04-17 16:05:31 -05003700 return 0;
3701}
3702
James Smart9bad7672008-12-04 22:39:02 -05003703/**
James Smart3621a712009-04-06 18:47:14 -04003704 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05003705 * @sdev: Pointer to scsi_device.
3706 *
3707 * This routine sets @sdev hostatdata filed to null.
3708 **/
dea31012005-04-17 16:05:31 -05003709static void
3710lpfc_slave_destroy(struct scsi_device *sdev)
3711{
James Smartd7c47992010-06-08 18:31:54 -04003712 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3713 struct lpfc_hba *phba = vport->phba;
3714 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05003715 sdev->hostdata = NULL;
3716 return;
3717}
3718
James Smart92d7f7b2007-06-17 19:56:38 -05003719
dea31012005-04-17 16:05:31 -05003720struct scsi_host_template lpfc_template = {
3721 .module = THIS_MODULE,
3722 .name = LPFC_DRIVER_NAME,
3723 .info = lpfc_info,
3724 .queuecommand = lpfc_queuecommand,
3725 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003726 .eh_device_reset_handler = lpfc_device_reset_handler,
3727 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04003728 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05003729 .slave_alloc = lpfc_slave_alloc,
3730 .slave_configure = lpfc_slave_configure,
3731 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04003732 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05003733 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003734 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05003735 .cmd_per_lun = LPFC_CMD_PER_LUN,
3736 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05003737 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04003738 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04003739 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05003740 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05003741};
James Smart3de2a652007-08-02 11:09:59 -04003742
3743struct scsi_host_template lpfc_vport_template = {
3744 .module = THIS_MODULE,
3745 .name = LPFC_DRIVER_NAME,
3746 .info = lpfc_info,
3747 .queuecommand = lpfc_queuecommand,
3748 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003749 .eh_device_reset_handler = lpfc_device_reset_handler,
3750 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04003751 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3752 .slave_alloc = lpfc_slave_alloc,
3753 .slave_configure = lpfc_slave_configure,
3754 .slave_destroy = lpfc_slave_destroy,
3755 .scan_finished = lpfc_scan_finished,
3756 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003757 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04003758 .cmd_per_lun = LPFC_CMD_PER_LUN,
3759 .use_clustering = ENABLE_CLUSTERING,
3760 .shost_attrs = lpfc_vport_attrs,
3761 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05003762 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04003763};