blob: eadd241eeff113cffc7a893412c43662ba695828 [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 Smart792581d2011-03-11 16:06:44 -05004 * Copyright (C) 2004-2011 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 ||
James Smart5989b8d2010-10-22 11:06:56 -0400172 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400173 !pnode->lat_data ||
174 (phba->bucket_type == LPFC_NO_BUCKET)) {
175 spin_unlock_irqrestore(shost->host_lock, flags);
176 return;
177 }
James Smartea2151b2008-09-07 11:52:10 -0400178
179 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
180 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
181 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500182 /* check array subscript bounds */
183 if (i < 0)
184 i = 0;
185 else if (i >= LPFC_MAX_BUCKET_COUNT)
186 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400187 } else {
188 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
189 if (latency <= (phba->bucket_base +
190 ((1<<i)*phba->bucket_step)))
191 break;
192 }
193
194 pnode->lat_data[i].cmd_count++;
195 spin_unlock_irqrestore(shost->host_lock, flags);
196}
197
James Smartea2151b2008-09-07 11:52:10 -0400198/**
James Smart3621a712009-04-06 18:47:14 -0400199 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400200 * @phba: Pointer to HBA context object.
201 * @vport: Pointer to vport object.
202 * @ndlp: Pointer to FC node associated with the target.
203 * @lun: Lun number of the scsi device.
204 * @old_val: Old value of the queue depth.
205 * @new_val: New value of the queue depth.
206 *
207 * This function sends an event to the mgmt application indicating
208 * there is a change in the scsi device queue depth.
209 **/
210static void
211lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
212 struct lpfc_vport *vport,
213 struct lpfc_nodelist *ndlp,
214 uint32_t lun,
215 uint32_t old_val,
216 uint32_t new_val)
217{
218 struct lpfc_fast_path_event *fast_path_evt;
219 unsigned long flags;
220
221 fast_path_evt = lpfc_alloc_fast_evt(phba);
222 if (!fast_path_evt)
223 return;
224
225 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
226 FC_REG_SCSI_EVENT;
227 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
228 LPFC_EVENT_VARQUEDEPTH;
229
230 /* Report all luns with change in queue depth */
231 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
232 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
233 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
234 &ndlp->nlp_portname, sizeof(struct lpfc_name));
235 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
236 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
237 }
238
239 fast_path_evt->un.queue_depth_evt.oldval = old_val;
240 fast_path_evt->un.queue_depth_evt.newval = new_val;
241 fast_path_evt->vport = vport;
242
243 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
244 spin_lock_irqsave(&phba->hbalock, flags);
245 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
246 spin_unlock_irqrestore(&phba->hbalock, flags);
247 lpfc_worker_wake_up(phba);
248
249 return;
250}
251
James Smart9bad7672008-12-04 22:39:02 -0500252/**
James Smart5ffc2662009-11-18 15:39:44 -0500253 * lpfc_change_queue_depth - Alter scsi device queue depth
254 * @sdev: Pointer the scsi device on which to change the queue depth.
255 * @qdepth: New queue depth to set the sdev to.
256 * @reason: The reason for the queue depth change.
257 *
258 * This function is called by the midlayer and the LLD to alter the queue
259 * depth for a scsi device. This function sets the queue depth to the new
260 * value and sends an event out to log the queue depth change.
261 **/
262int
263lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
264{
265 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
266 struct lpfc_hba *phba = vport->phba;
267 struct lpfc_rport_data *rdata;
268 unsigned long new_queue_depth, old_queue_depth;
269
270 old_queue_depth = sdev->queue_depth;
271 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
272 new_queue_depth = sdev->queue_depth;
273 rdata = sdev->hostdata;
274 if (rdata)
275 lpfc_send_sdev_queuedepth_change_event(phba, vport,
276 rdata->pnode, sdev->lun,
277 old_queue_depth,
278 new_queue_depth);
279 return sdev->queue_depth;
280}
281
282/**
James Smart3621a712009-04-06 18:47:14 -0400283 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500284 * @phba: The Hba for which this call is being executed.
285 *
286 * This routine is called when there is resource error in driver or firmware.
287 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
288 * posts at most 1 event each second. This routine wakes up worker thread of
289 * @phba to process WORKER_RAM_DOWN_EVENT event.
290 *
291 * This routine should be called with no lock held.
292 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500293void
James Smarteaf15d52008-12-04 22:39:29 -0500294lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500295{
296 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400297 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500298
299 spin_lock_irqsave(&phba->hbalock, flags);
300 atomic_inc(&phba->num_rsrc_err);
301 phba->last_rsrc_error_time = jiffies;
302
303 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
304 spin_unlock_irqrestore(&phba->hbalock, flags);
305 return;
306 }
307
308 phba->last_ramp_down_time = jiffies;
309
310 spin_unlock_irqrestore(&phba->hbalock, flags);
311
312 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400313 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
314 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500315 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500316 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
317
James Smart5e9d9b82008-06-14 22:52:53 -0400318 if (!evt_posted)
319 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500320 return;
321}
322
James Smart9bad7672008-12-04 22:39:02 -0500323/**
James Smart3621a712009-04-06 18:47:14 -0400324 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500325 * @phba: The Hba for which this call is being executed.
326 *
327 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
328 * post at most 1 event every 5 minute after last_ramp_up_time or
329 * last_rsrc_error_time. This routine wakes up worker thread of @phba
330 * to process WORKER_RAM_DOWN_EVENT event.
331 *
332 * This routine should be called with no lock held.
333 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500334static inline void
James Smart3de2a652007-08-02 11:09:59 -0400335lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400336 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500337{
338 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400339 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400340 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500341 atomic_inc(&phba->num_cmd_success);
342
James Smarta257bf92009-04-06 18:48:10 -0400343 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500344 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500345 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500346 if (time_before(jiffies,
347 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
348 time_before(jiffies,
349 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500350 spin_unlock_irqrestore(&phba->hbalock, flags);
351 return;
352 }
James Smart92d7f7b2007-06-17 19:56:38 -0500353 phba->last_ramp_up_time = jiffies;
354 spin_unlock_irqrestore(&phba->hbalock, flags);
355
356 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400357 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
358 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500359 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500360 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
361
James Smart5e9d9b82008-06-14 22:52:53 -0400362 if (!evt_posted)
363 lpfc_worker_wake_up(phba);
364 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500365}
366
James Smart9bad7672008-12-04 22:39:02 -0500367/**
James Smart3621a712009-04-06 18:47:14 -0400368 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500369 * @phba: The Hba for which this call is being executed.
370 *
371 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
372 * thread.This routine reduces queue depth for all scsi device on each vport
373 * associated with @phba.
374 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500375void
376lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
377{
James Smart549e55c2007-08-02 11:09:51 -0400378 struct lpfc_vport **vports;
379 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500380 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500381 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500382 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400383 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500384
385 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
386 num_cmd_success = atomic_read(&phba->num_cmd_success);
387
James Smart549e55c2007-08-02 11:09:51 -0400388 vports = lpfc_create_vport_work_array(phba);
389 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400390 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400391 shost = lpfc_shost_from_vport(vports[i]);
392 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500393 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400394 sdev->queue_depth * num_rsrc_err /
395 (num_rsrc_err + num_cmd_success);
396 if (!new_queue_depth)
397 new_queue_depth = sdev->queue_depth - 1;
398 else
399 new_queue_depth = sdev->queue_depth -
400 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500401 lpfc_change_queue_depth(sdev, new_queue_depth,
402 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400403 }
James Smart92d7f7b2007-06-17 19:56:38 -0500404 }
James Smart09372822008-01-11 01:52:54 -0500405 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500406 atomic_set(&phba->num_rsrc_err, 0);
407 atomic_set(&phba->num_cmd_success, 0);
408}
409
James Smart9bad7672008-12-04 22:39:02 -0500410/**
James Smart3621a712009-04-06 18:47:14 -0400411 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500412 * @phba: The Hba for which this call is being executed.
413 *
414 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
415 * thread.This routine increases queue depth for all scsi device on each vport
416 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
417 * num_cmd_success to zero.
418 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500419void
420lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
421{
James Smart549e55c2007-08-02 11:09:51 -0400422 struct lpfc_vport **vports;
423 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500424 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400425 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500426
James Smart549e55c2007-08-02 11:09:51 -0400427 vports = lpfc_create_vport_work_array(phba);
428 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400429 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400430 shost = lpfc_shost_from_vport(vports[i]);
431 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400432 if (vports[i]->cfg_lun_queue_depth <=
433 sdev->queue_depth)
434 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500435 lpfc_change_queue_depth(sdev,
436 sdev->queue_depth+1,
437 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400438 }
James Smart92d7f7b2007-06-17 19:56:38 -0500439 }
James Smart09372822008-01-11 01:52:54 -0500440 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500441 atomic_set(&phba->num_rsrc_err, 0);
442 atomic_set(&phba->num_cmd_success, 0);
443}
444
James Smarta8e497d2008-08-24 21:50:11 -0400445/**
James Smart3621a712009-04-06 18:47:14 -0400446 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400447 * @phba: Pointer to HBA context object.
448 *
449 * This function walks vport list and set each SCSI host to block state
450 * by invoking fc_remote_port_delete() routine. This function is invoked
451 * with EEH when device's PCI slot has been permanently disabled.
452 **/
453void
454lpfc_scsi_dev_block(struct lpfc_hba *phba)
455{
456 struct lpfc_vport **vports;
457 struct Scsi_Host *shost;
458 struct scsi_device *sdev;
459 struct fc_rport *rport;
460 int i;
461
462 vports = lpfc_create_vport_work_array(phba);
463 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400464 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400465 shost = lpfc_shost_from_vport(vports[i]);
466 shost_for_each_device(sdev, shost) {
467 rport = starget_to_rport(scsi_target(sdev));
468 fc_remote_port_delete(rport);
469 }
470 }
471 lpfc_destroy_vport_work_array(phba, vports);
472}
473
James Smart9bad7672008-12-04 22:39:02 -0500474/**
James Smart3772a992009-05-22 14:50:54 -0400475 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500476 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400477 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500478 *
James Smart3772a992009-05-22 14:50:54 -0400479 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
480 * the scsi buffer contains all the necessary information needed to initiate
481 * a SCSI I/O. The non-DMAable buffer region contains information to build
482 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
483 * and the initial BPL. In addition to allocating memory, the FCP CMND and
484 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500485 *
486 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400487 * int - number of scsi buffers that were allocated.
488 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500489 **/
James Smart3772a992009-05-22 14:50:54 -0400490static int
491lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500492{
James Smart2e0fef82007-06-17 19:56:36 -0500493 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500494 struct lpfc_scsi_buf *psb;
495 struct ulp_bde64 *bpl;
496 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400497 dma_addr_t pdma_phys_fcp_cmd;
498 dma_addr_t pdma_phys_fcp_rsp;
499 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500500 uint16_t iotag;
James Smart3772a992009-05-22 14:50:54 -0400501 int bcnt;
dea31012005-04-17 16:05:31 -0500502
James Smart3772a992009-05-22 14:50:54 -0400503 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
504 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
505 if (!psb)
506 break;
dea31012005-04-17 16:05:31 -0500507
James Smart3772a992009-05-22 14:50:54 -0400508 /*
509 * Get memory from the pci pool to map the virt space to pci
510 * bus space for an I/O. The DMA buffer includes space for the
511 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
512 * necessary to support the sg_tablesize.
513 */
514 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
515 GFP_KERNEL, &psb->dma_handle);
516 if (!psb->data) {
517 kfree(psb);
518 break;
519 }
dea31012005-04-17 16:05:31 -0500520
James Smart3772a992009-05-22 14:50:54 -0400521 /* Initialize virtual ptrs to dma_buf region. */
522 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500523
James Smart3772a992009-05-22 14:50:54 -0400524 /* Allocate iotag for psb->cur_iocbq. */
525 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
526 if (iotag == 0) {
527 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
528 psb->data, psb->dma_handle);
529 kfree(psb);
530 break;
531 }
532 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500533
James Smart3772a992009-05-22 14:50:54 -0400534 psb->fcp_cmnd = psb->data;
535 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
536 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400537 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500538
James Smart3772a992009-05-22 14:50:54 -0400539 /* Initialize local short-hand pointers. */
540 bpl = psb->fcp_bpl;
541 pdma_phys_fcp_cmd = psb->dma_handle;
542 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
543 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
544 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500545
James Smart3772a992009-05-22 14:50:54 -0400546 /*
547 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
548 * are sg list bdes. Initialize the first two and leave the
549 * rest for queuecommand.
550 */
551 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
552 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
553 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
554 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
555 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500556
James Smart3772a992009-05-22 14:50:54 -0400557 /* Setup the physical region for the FCP RSP */
558 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
559 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
560 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
561 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
562 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
563
564 /*
565 * Since the IOCB for the FCP I/O is built into this
566 * lpfc_scsi_buf, initialize it with all known data now.
567 */
568 iocb = &psb->cur_iocbq.iocb;
569 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
570 if ((phba->sli_rev == 3) &&
571 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
572 /* fill in immediate fcp command BDE */
573 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
574 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
575 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
576 unsli3.fcp_ext.icd);
577 iocb->un.fcpi64.bdl.addrHigh = 0;
578 iocb->ulpBdeCount = 0;
579 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300580 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400581 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
582 BUFF_TYPE_BDE_64;
583 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
584 sizeof(struct fcp_rsp);
585 iocb->unsli3.fcp_ext.rbde.addrLow =
586 putPaddrLow(pdma_phys_fcp_rsp);
587 iocb->unsli3.fcp_ext.rbde.addrHigh =
588 putPaddrHigh(pdma_phys_fcp_rsp);
589 } else {
590 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
591 iocb->un.fcpi64.bdl.bdeSize =
592 (2 * sizeof(struct ulp_bde64));
593 iocb->un.fcpi64.bdl.addrLow =
594 putPaddrLow(pdma_phys_bpl);
595 iocb->un.fcpi64.bdl.addrHigh =
596 putPaddrHigh(pdma_phys_bpl);
597 iocb->ulpBdeCount = 1;
598 iocb->ulpLe = 1;
599 }
600 iocb->ulpClass = CLASS3;
601 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400602 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400603 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500604 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400605
James Smart34b02dc2008-08-24 21:49:55 -0400606 }
dea31012005-04-17 16:05:31 -0500607
James Smart3772a992009-05-22 14:50:54 -0400608 return bcnt;
dea31012005-04-17 16:05:31 -0500609}
610
James Smart9bad7672008-12-04 22:39:02 -0500611/**
James Smart1151e3e2011-02-16 12:39:35 -0500612 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
613 * @vport: pointer to lpfc vport data structure.
614 *
615 * This routine is invoked by the vport cleanup for deletions and the cleanup
616 * for an ndlp on removal.
617 **/
618void
619lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
620{
621 struct lpfc_hba *phba = vport->phba;
622 struct lpfc_scsi_buf *psb, *next_psb;
623 unsigned long iflag = 0;
624
625 spin_lock_irqsave(&phba->hbalock, iflag);
626 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
627 list_for_each_entry_safe(psb, next_psb,
628 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
629 if (psb->rdata && psb->rdata->pnode
630 && psb->rdata->pnode->vport == vport)
631 psb->rdata = NULL;
632 }
633 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
634 spin_unlock_irqrestore(&phba->hbalock, iflag);
635}
636
637/**
James Smartda0436e2009-05-22 14:51:39 -0400638 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
639 * @phba: pointer to lpfc hba data structure.
640 * @axri: pointer to the fcp xri abort wcqe structure.
641 *
642 * This routine is invoked by the worker thread to process a SLI4 fast-path
643 * FCP aborted xri.
644 **/
645void
646lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
647 struct sli4_wcqe_xri_aborted *axri)
648{
649 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500650 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400651 struct lpfc_scsi_buf *psb, *next_psb;
652 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500653 struct lpfc_iocbq *iocbq;
654 int i;
James Smart19ca7602010-11-20 23:11:55 -0500655 struct lpfc_nodelist *ndlp;
656 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400657 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400658
James Smart0f65ff62010-02-26 14:14:23 -0500659 spin_lock_irqsave(&phba->hbalock, iflag);
660 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400661 list_for_each_entry_safe(psb, next_psb,
662 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
663 if (psb->cur_iocbq.sli4_xritag == xri) {
664 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500665 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400666 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500667 spin_unlock(
668 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500669 if (psb->rdata && psb->rdata->pnode)
670 ndlp = psb->rdata->pnode;
671 else
672 ndlp = NULL;
673
James Smart19ca7602010-11-20 23:11:55 -0500674 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500675 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart19ca7602010-11-20 23:11:55 -0500676 if (ndlp)
677 lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
James Smartda0436e2009-05-22 14:51:39 -0400678 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500679 if (rrq_empty)
680 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400681 return;
682 }
683 }
James Smart0f65ff62010-02-26 14:14:23 -0500684 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
685 for (i = 1; i <= phba->sli.last_iotag; i++) {
686 iocbq = phba->sli.iocbq_lookup[i];
687
688 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
689 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
690 continue;
691 if (iocbq->sli4_xritag != xri)
692 continue;
693 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
694 psb->exch_busy = 0;
695 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart589a52d2010-07-14 15:30:54 -0400696 if (pring->txq_cnt)
697 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500698 return;
699
700 }
701 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400702}
703
704/**
705 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
706 * @phba: pointer to lpfc hba data structure.
707 *
708 * This routine walks the list of scsi buffers that have been allocated and
709 * repost them to the HBA by using SGL block post. This is needed after a
710 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
711 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
712 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
713 *
714 * Returns: 0 = success, non-zero failure.
715 **/
716int
717lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
718{
719 struct lpfc_scsi_buf *psb;
720 int index, status, bcnt = 0, rcnt = 0, rc = 0;
721 LIST_HEAD(sblist);
722
723 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
724 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
725 if (psb) {
726 /* Remove from SCSI buffer list */
727 list_del(&psb->list);
728 /* Add it to a local SCSI buffer list */
729 list_add_tail(&psb->list, &sblist);
730 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
731 bcnt = rcnt;
732 rcnt = 0;
733 }
734 } else
735 /* A hole present in the XRI array, need to skip */
736 bcnt = rcnt;
737
738 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
739 /* End of XRI array for SCSI buffer, complete */
740 bcnt = rcnt;
741
742 /* Continue until collect up to a nembed page worth of sgls */
743 if (bcnt == 0)
744 continue;
745 /* Now, post the SCSI buffer list sgls as a block */
James Smart6d368e52011-05-24 11:44:12 -0400746 if (!phba->sli4_hba.extents_in_use)
747 status = lpfc_sli4_post_scsi_sgl_block(phba,
748 &sblist,
749 bcnt);
750 else
751 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
752 &sblist,
753 bcnt);
James Smartda0436e2009-05-22 14:51:39 -0400754 /* Reset SCSI buffer count for next round of posting */
755 bcnt = 0;
756 while (!list_empty(&sblist)) {
757 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
758 list);
759 if (status) {
760 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500761 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400762 rc++;
James Smart341af102010-01-26 23:07:37 -0500763 } else {
764 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400765 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500766 }
James Smartda0436e2009-05-22 14:51:39 -0400767 /* Put it back into the SCSI buffer list */
768 lpfc_release_scsi_buf_s4(phba, psb);
769 }
770 }
771 return rc;
772}
773
774/**
775 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
776 * @vport: The virtual port for which this call being executed.
777 * @num_to_allocate: The requested number of buffers to allocate.
778 *
779 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
780 * the scsi buffer contains all the necessary information needed to initiate
781 * a SCSI I/O.
782 *
783 * Return codes:
784 * int - number of scsi buffers that were allocated.
785 * 0 = failure, less than num_to_alloc is a partial failure.
786 **/
787static int
788lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
789{
790 struct lpfc_hba *phba = vport->phba;
791 struct lpfc_scsi_buf *psb;
792 struct sli4_sge *sgl;
793 IOCB_t *iocb;
794 dma_addr_t pdma_phys_fcp_cmd;
795 dma_addr_t pdma_phys_fcp_rsp;
796 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
James Smart6d368e52011-05-24 11:44:12 -0400797 uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400798 int status = 0, index;
799 int bcnt;
800 int non_sequential_xri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400801 LIST_HEAD(sblist);
802
803 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
804 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
805 if (!psb)
806 break;
807
808 /*
809 * Get memory from the pci pool to map the virt space to pci bus
810 * space for an I/O. The DMA buffer includes space for the
811 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
812 * necessary to support the sg_tablesize.
813 */
814 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
815 GFP_KERNEL, &psb->dma_handle);
816 if (!psb->data) {
817 kfree(psb);
818 break;
819 }
820
821 /* Initialize virtual ptrs to dma_buf region. */
822 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
823
824 /* Allocate iotag for psb->cur_iocbq. */
825 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
826 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400827 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
828 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400829 kfree(psb);
830 break;
831 }
832
James Smart6d368e52011-05-24 11:44:12 -0400833 lxri = lpfc_sli4_next_xritag(phba);
834 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -0400835 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
836 psb->data, psb->dma_handle);
837 kfree(psb);
838 break;
839 }
James Smart6d368e52011-05-24 11:44:12 -0400840 psb->cur_iocbq.sli4_lxritag = lxri;
841 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -0400842 if (last_xritag != NO_XRI
843 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
844 non_sequential_xri = 1;
845 } else
846 list_add_tail(&psb->list, &sblist);
847 last_xritag = psb->cur_iocbq.sli4_xritag;
848
849 index = phba->sli4_hba.scsi_xri_cnt++;
850 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
851
852 psb->fcp_bpl = psb->data;
853 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
854 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
855 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
856 sizeof(struct fcp_cmnd));
857
858 /* Initialize local short-hand pointers. */
859 sgl = (struct sli4_sge *)psb->fcp_bpl;
860 pdma_phys_bpl = psb->dma_handle;
861 pdma_phys_fcp_cmd =
862 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
863 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
864 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
865
866 /*
867 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
868 * are sg list bdes. Initialize the first two and leave the
869 * rest for queuecommand.
870 */
871 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
872 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -0400873 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400874 bf_set(lpfc_sli4_sge_last, sgl, 0);
875 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500876 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400877 sgl++;
878
879 /* Setup the physical region for the FCP RSP */
880 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
881 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -0400882 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -0400883 bf_set(lpfc_sli4_sge_last, sgl, 1);
884 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500885 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400886
887 /*
888 * Since the IOCB for the FCP I/O is built into this
889 * lpfc_scsi_buf, initialize it with all known data now.
890 */
891 iocb = &psb->cur_iocbq.iocb;
892 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
893 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
894 /* setting the BLP size to 2 * sizeof BDE may not be correct.
895 * We are setting the bpl to point to out sgl. An sgl's
896 * entries are 16 bytes, a bpl entries are 12 bytes.
897 */
898 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
899 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
900 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
901 iocb->ulpBdeCount = 1;
902 iocb->ulpLe = 1;
903 iocb->ulpClass = CLASS3;
James Smarteee88772010-09-29 11:19:08 -0400904 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -0400905 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
906 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
907 else
908 pdma_phys_bpl1 = 0;
909 psb->dma_phys_bpl = pdma_phys_bpl;
910 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
911 if (non_sequential_xri) {
912 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
913 pdma_phys_bpl1,
914 psb->cur_iocbq.sli4_xritag);
915 if (status) {
916 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500917 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500918 } else {
919 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400920 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500921 }
James Smartda0436e2009-05-22 14:51:39 -0400922 /* Put it back into the SCSI buffer list */
923 lpfc_release_scsi_buf_s4(phba, psb);
924 break;
925 }
926 }
927 if (bcnt) {
James Smart6d368e52011-05-24 11:44:12 -0400928 if (!phba->sli4_hba.extents_in_use)
929 status = lpfc_sli4_post_scsi_sgl_block(phba,
930 &sblist,
931 bcnt);
932 else
933 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
934 &sblist,
935 bcnt);
936
937 if (status) {
938 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
939 "3021 SCSI SGL post error %d\n",
940 status);
941 bcnt = 0;
942 }
James Smartda0436e2009-05-22 14:51:39 -0400943 /* Reset SCSI buffer count for next round of posting */
944 while (!list_empty(&sblist)) {
945 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
946 list);
947 if (status) {
948 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500949 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500950 } else {
951 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400952 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500953 }
James Smartda0436e2009-05-22 14:51:39 -0400954 /* Put it back into the SCSI buffer list */
955 lpfc_release_scsi_buf_s4(phba, psb);
956 }
957 }
958
James Smartd7c47992010-06-08 18:31:54 -0400959 return bcnt + non_sequential_xri;
James Smartda0436e2009-05-22 14:51:39 -0400960}
961
962/**
James Smart3772a992009-05-22 14:50:54 -0400963 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
964 * @vport: The virtual port for which this call being executed.
965 * @num_to_allocate: The requested number of buffers to allocate.
966 *
967 * This routine wraps the actual SCSI buffer allocator function pointer from
968 * the lpfc_hba struct.
969 *
970 * Return codes:
971 * int - number of scsi buffers that were allocated.
972 * 0 = failure, less than num_to_alloc is a partial failure.
973 **/
974static inline int
975lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
976{
977 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
978}
979
980/**
James Smart19ca7602010-11-20 23:11:55 -0500981 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -0400982 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500983 *
984 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
985 * and returns to caller.
986 *
987 * Return codes:
988 * NULL - Error
989 * Pointer to lpfc_scsi_buf - Success
990 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100991static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -0500992lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -0500993{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400994 struct lpfc_scsi_buf * lpfc_cmd = NULL;
995 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500996 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500997
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500998 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400999 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -04001000 if (lpfc_cmd) {
1001 lpfc_cmd->seg_cnt = 0;
1002 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001003 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -04001004 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001005 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001006 return lpfc_cmd;
1007}
James Smart19ca7602010-11-20 23:11:55 -05001008/**
1009 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1010 * @phba: The HBA for which this call is being executed.
1011 *
1012 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1013 * and returns to caller.
1014 *
1015 * Return codes:
1016 * NULL - Error
1017 * Pointer to lpfc_scsi_buf - Success
1018 **/
1019static struct lpfc_scsi_buf*
1020lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1021{
James Smart1151e3e2011-02-16 12:39:35 -05001022 struct lpfc_scsi_buf *lpfc_cmd ;
James Smart19ca7602010-11-20 23:11:55 -05001023 unsigned long iflag = 0;
1024 int found = 0;
1025
1026 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001027 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1028 list) {
James Smart19ca7602010-11-20 23:11:55 -05001029 if (lpfc_test_rrq_active(phba, ndlp,
James Smart1151e3e2011-02-16 12:39:35 -05001030 lpfc_cmd->cur_iocbq.sli4_xritag))
1031 continue;
1032 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001033 found = 1;
1034 lpfc_cmd->seg_cnt = 0;
1035 lpfc_cmd->nonsg_phys = 0;
1036 lpfc_cmd->prot_seg_cnt = 0;
James Smart1151e3e2011-02-16 12:39:35 -05001037 break;
James Smart19ca7602010-11-20 23:11:55 -05001038 }
James Smart1151e3e2011-02-16 12:39:35 -05001039 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1040 iflag);
1041 if (!found)
1042 return NULL;
1043 else
1044 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001045}
1046/**
1047 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1048 * @phba: The HBA for which this call is being executed.
1049 *
1050 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1051 * and returns to caller.
1052 *
1053 * Return codes:
1054 * NULL - Error
1055 * Pointer to lpfc_scsi_buf - Success
1056 **/
1057static struct lpfc_scsi_buf*
1058lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1059{
1060 return phba->lpfc_get_scsi_buf(phba, ndlp);
1061}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001062
James Smart9bad7672008-12-04 22:39:02 -05001063/**
James Smart3772a992009-05-22 14:50:54 -04001064 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001065 * @phba: The Hba for which this call is being executed.
1066 * @psb: The scsi buffer which is being released.
1067 *
1068 * This routine releases @psb scsi buffer by adding it to tail of @phba
1069 * lpfc_scsi_buf_list list.
1070 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001071static void
James Smart3772a992009-05-22 14:50:54 -04001072lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001073{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001074 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001075
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001076 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001077 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -05001078 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001079 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -05001080}
1081
James Smart9bad7672008-12-04 22:39:02 -05001082/**
James Smartda0436e2009-05-22 14:51:39 -04001083 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1084 * @phba: The Hba for which this call is being executed.
1085 * @psb: The scsi buffer which is being released.
1086 *
1087 * This routine releases @psb scsi buffer by adding it to tail of @phba
1088 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1089 * and cannot be reused for at least RA_TOV amount of time if it was
1090 * aborted.
1091 **/
1092static void
1093lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1094{
1095 unsigned long iflag = 0;
1096
James Smart341af102010-01-26 23:07:37 -05001097 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001098 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1099 iflag);
1100 psb->pCmd = NULL;
1101 list_add_tail(&psb->list,
1102 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1103 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1104 iflag);
1105 } else {
1106
1107 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1108 psb->pCmd = NULL;
1109 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1110 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1111 }
1112}
1113
1114/**
James Smart3772a992009-05-22 14:50:54 -04001115 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1116 * @phba: The Hba for which this call is being executed.
1117 * @psb: The scsi buffer which is being released.
1118 *
1119 * This routine releases @psb scsi buffer by adding it to tail of @phba
1120 * lpfc_scsi_buf_list list.
1121 **/
1122static void
1123lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1124{
1125
1126 phba->lpfc_release_scsi_buf(phba, psb);
1127}
1128
1129/**
1130 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001131 * @phba: The Hba for which this call is being executed.
1132 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1133 *
1134 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001135 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1136 * through sg elements and format the bdea. This routine also initializes all
1137 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001138 *
1139 * Return codes:
1140 * 1 - Error
1141 * 0 - Success
1142 **/
dea31012005-04-17 16:05:31 -05001143static int
James Smart3772a992009-05-22 14:50:54 -04001144lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001145{
1146 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1147 struct scatterlist *sgel = NULL;
1148 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1149 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001150 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001151 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001152 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001153 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001154 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001155 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001156
1157 /*
1158 * There are three possibilities here - use scatter-gather segment, use
1159 * the single mapping, or neither. Start the lpfc command prep by
1160 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1161 * data bde entry.
1162 */
1163 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001164 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001165 /*
1166 * The driver stores the segment count returned from pci_map_sg
1167 * because this a count of dma-mappings used to map the use_sg
1168 * pages. They are not guaranteed to be the same for those
1169 * architectures that implement an IOMMU.
1170 */
dea31012005-04-17 16:05:31 -05001171
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001172 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1173 scsi_sg_count(scsi_cmnd), datadir);
1174 if (unlikely(!nseg))
1175 return 1;
1176
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001177 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001178 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001179 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1180 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001181 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001182 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001183 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001184 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001185 return 1;
1186 }
1187
1188 /*
1189 * The driver established a maximum scatter-gather segment count
1190 * during probe that limits the number of sg elements in any
1191 * single scsi command. Just run through the seg_cnt and format
1192 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001193 * When using SLI-3 the driver will try to fit all the BDEs into
1194 * the IOCB. If it can't then the BDEs get added to a BPL as it
1195 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001196 */
James Smart34b02dc2008-08-24 21:49:55 -04001197 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001198 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001199 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001200 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001201 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001202 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1203 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1204 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1205 data_bde->addrLow = putPaddrLow(physaddr);
1206 data_bde->addrHigh = putPaddrHigh(physaddr);
1207 data_bde++;
1208 } else {
1209 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1210 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1211 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1212 bpl->addrLow =
1213 le32_to_cpu(putPaddrLow(physaddr));
1214 bpl->addrHigh =
1215 le32_to_cpu(putPaddrHigh(physaddr));
1216 bpl++;
1217 }
dea31012005-04-17 16:05:31 -05001218 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001219 }
dea31012005-04-17 16:05:31 -05001220
1221 /*
1222 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001223 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1224 * explicitly reinitialized and for SLI-3 the extended bde count is
1225 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001226 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001227 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001228 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1229 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001230 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1231 /*
1232 * The extended IOCB format can only fit 3 BDE or a BPL.
1233 * This I/O has more than 3 BDE so the 1st data bde will
1234 * be a BPL that is filled in here.
1235 */
1236 physaddr = lpfc_cmd->dma_handle;
1237 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1238 data_bde->tus.f.bdeSize = (num_bde *
1239 sizeof(struct ulp_bde64));
1240 physaddr += (sizeof(struct fcp_cmnd) +
1241 sizeof(struct fcp_rsp) +
1242 (2 * sizeof(struct ulp_bde64)));
1243 data_bde->addrHigh = putPaddrHigh(physaddr);
1244 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001245 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001246 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1247 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001248 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001249 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1250 }
1251 } else {
1252 iocb_cmd->un.fcpi64.bdl.bdeSize =
1253 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001254 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001255 }
James Smart09372822008-01-11 01:52:54 -05001256 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001257
1258 /*
1259 * Due to difference in data length between DIF/non-DIF paths,
1260 * we need to set word 4 of IOCB here
1261 */
James Smarta257bf92009-04-06 18:48:10 -04001262 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001263 return 0;
1264}
1265
James Smarte2a0a9d2008-12-04 22:40:02 -05001266/*
James Smart6c8eea52010-04-06 14:49:53 -04001267 * Given a scsi cmnd, determine the BlockGuard opcodes to be used with it
1268 * @sc: The SCSI command to examine
1269 * @txopt: (out) BlockGuard operation for transmitted data
1270 * @rxopt: (out) BlockGuard operation for received data
1271 *
1272 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1273 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001274 */
1275static int
James Smart6c8eea52010-04-06 14:49:53 -04001276lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1277 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001278{
1279 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001280 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001281
1282 if (guard_type == SHOST_DIX_GUARD_IP) {
1283 switch (scsi_get_prot_op(sc)) {
1284 case SCSI_PROT_READ_INSERT:
1285 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001286 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1287 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001288 break;
1289
1290 case SCSI_PROT_READ_STRIP:
1291 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001292 *txop = BG_OP_IN_NODIF_OUT_CRC;
1293 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001294 break;
1295
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001296 case SCSI_PROT_READ_PASS:
1297 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001298 *txop = BG_OP_IN_CSUM_OUT_CRC;
1299 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001300 break;
1301
James Smarte2a0a9d2008-12-04 22:40:02 -05001302 case SCSI_PROT_NORMAL:
1303 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001304 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001305 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1306 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001307 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001308 break;
1309
1310 }
James Smart7c56b9f2011-07-22 18:36:25 -04001311 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001312 switch (scsi_get_prot_op(sc)) {
1313 case SCSI_PROT_READ_STRIP:
1314 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001315 *txop = BG_OP_IN_NODIF_OUT_CRC;
1316 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001317 break;
1318
1319 case SCSI_PROT_READ_PASS:
1320 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001321 *txop = BG_OP_IN_CRC_OUT_CRC;
1322 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001323 break;
1324
James Smarte2a0a9d2008-12-04 22:40:02 -05001325 case SCSI_PROT_READ_INSERT:
1326 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001327 *txop = BG_OP_IN_CRC_OUT_NODIF;
1328 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1329 break;
1330
James Smarte2a0a9d2008-12-04 22:40:02 -05001331 case SCSI_PROT_NORMAL:
1332 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001333 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001334 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1335 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001336 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001337 break;
1338 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001339 }
1340
James Smart6c8eea52010-04-06 14:49:53 -04001341 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001342}
1343
1344struct scsi_dif_tuple {
1345 __be16 guard_tag; /* Checksum */
1346 __be16 app_tag; /* Opaque storage */
1347 __be32 ref_tag; /* Target LBA or indirect LBA */
1348};
1349
1350static inline unsigned
1351lpfc_cmd_blksize(struct scsi_cmnd *sc)
1352{
1353 return sc->device->sector_size;
1354}
1355
James Smarte2a0a9d2008-12-04 22:40:02 -05001356/*
1357 * This function sets up buffer list for protection groups of
1358 * type LPFC_PG_TYPE_NO_DIF
1359 *
1360 * This is usually used when the HBA is instructed to generate
1361 * DIFs and insert them into data stream (or strip DIF from
1362 * incoming data stream)
1363 *
1364 * The buffer list consists of just one protection group described
1365 * below:
1366 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001367 * start of prot group --> | PDE_5 |
1368 * +-------------------------+
1369 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001370 * +-------------------------+
1371 * | Data BDE |
1372 * +-------------------------+
1373 * |more Data BDE's ... (opt)|
1374 * +-------------------------+
1375 *
1376 * @sc: pointer to scsi command we're working on
1377 * @bpl: pointer to buffer list for protection groups
1378 * @datacnt: number of segments of data that have been dma mapped
1379 *
1380 * Note: Data s/g buffers have been dma mapped
1381 */
1382static int
1383lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1384 struct ulp_bde64 *bpl, int datasegcnt)
1385{
1386 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001387 struct lpfc_pde5 *pde5 = NULL;
1388 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001389 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001390 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001391 int datadir = sc->sc_data_direction;
James Smarte2a0a9d2008-12-04 22:40:02 -05001392 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04001393 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04001394 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001395
James Smart6c8eea52010-04-06 14:49:53 -04001396 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1397 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001398 goto out;
1399
James Smart6c8eea52010-04-06 14:49:53 -04001400 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001401 blksize = lpfc_cmd_blksize(sc);
James Smart7c56b9f2011-07-22 18:36:25 -04001402 reftag = scsi_get_lba(sc) & 0xffffffff;
James Smarte2a0a9d2008-12-04 22:40:02 -05001403
James Smart6c8eea52010-04-06 14:49:53 -04001404 /* setup PDE5 with what we have */
1405 pde5 = (struct lpfc_pde5 *) bpl;
1406 memset(pde5, 0, sizeof(struct lpfc_pde5));
1407 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001408
James Smartbc739052010-08-04 16:11:18 -04001409 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001410 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001411 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001412
James Smart6c8eea52010-04-06 14:49:53 -04001413 /* advance bpl and increment bde count */
1414 num_bde++;
1415 bpl++;
1416 pde6 = (struct lpfc_pde6 *) bpl;
1417
1418 /* setup PDE6 with the rest of the info */
1419 memset(pde6, 0, sizeof(struct lpfc_pde6));
1420 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1421 bf_set(pde6_optx, pde6, txop);
1422 bf_set(pde6_oprx, pde6, rxop);
1423 if (datadir == DMA_FROM_DEVICE) {
1424 bf_set(pde6_ce, pde6, 1);
1425 bf_set(pde6_re, pde6, 1);
James Smart6c8eea52010-04-06 14:49:53 -04001426 }
1427 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001428 bf_set(pde6_ae, pde6, 0);
1429 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001430
James Smartbc739052010-08-04 16:11:18 -04001431 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001432 pde6->word0 = cpu_to_le32(pde6->word0);
1433 pde6->word1 = cpu_to_le32(pde6->word1);
1434 pde6->word2 = cpu_to_le32(pde6->word2);
1435
James Smart6c8eea52010-04-06 14:49:53 -04001436 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001437 num_bde++;
1438 bpl++;
1439
1440 /* assumption: caller has already run dma_map_sg on command data */
1441 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1442 physaddr = sg_dma_address(sgde);
1443 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1444 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1445 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1446 if (datadir == DMA_TO_DEVICE)
1447 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1448 else
1449 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1450 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1451 bpl++;
1452 num_bde++;
1453 }
1454
1455out:
1456 return num_bde;
1457}
1458
1459/*
1460 * This function sets up buffer list for protection groups of
1461 * type LPFC_PG_TYPE_DIF_BUF
1462 *
1463 * This is usually used when DIFs are in their own buffers,
1464 * separate from the data. The HBA can then by instructed
1465 * to place the DIFs in the outgoing stream. For read operations,
1466 * The HBA could extract the DIFs and place it in DIF buffers.
1467 *
1468 * The buffer list for this type consists of one or more of the
1469 * protection groups described below:
1470 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001471 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001472 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001473 * | PDE_6 |
1474 * +-------------------------+
1475 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001476 * +-------------------------+
1477 * | Data BDE |
1478 * +-------------------------+
1479 * |more Data BDE's ... (opt)|
1480 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001481 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001482 * +-------------------------+
1483 * | ... |
1484 * +-------------------------+
1485 *
1486 * @sc: pointer to scsi command we're working on
1487 * @bpl: pointer to buffer list for protection groups
1488 * @datacnt: number of segments of data that have been dma mapped
1489 * @protcnt: number of segment of protection data that have been dma mapped
1490 *
1491 * Note: It is assumed that both data and protection s/g buffers have been
1492 * mapped for DMA
1493 */
1494static int
1495lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1496 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1497{
1498 struct scatterlist *sgde = NULL; /* s/g data entry */
1499 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001500 struct lpfc_pde5 *pde5 = NULL;
1501 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05001502 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001503 dma_addr_t dataphysaddr, protphysaddr;
1504 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05001505 unsigned int split_offset;
1506 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05001507 unsigned int protgrp_blks, protgrp_bytes;
1508 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001509 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001510 int datadir = sc->sc_data_direction;
1511 unsigned char pgdone = 0, alldone = 0;
1512 unsigned blksize;
1513 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04001514 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001515 int num_bde = 0;
1516
1517 sgpe = scsi_prot_sglist(sc);
1518 sgde = scsi_sglist(sc);
1519
1520 if (!sgpe || !sgde) {
1521 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1522 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1523 sgpe, sgde);
1524 return 0;
1525 }
1526
James Smart6c8eea52010-04-06 14:49:53 -04001527 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1528 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001529 goto out;
1530
James Smart6c8eea52010-04-06 14:49:53 -04001531 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001532 blksize = lpfc_cmd_blksize(sc);
James Smart7c56b9f2011-07-22 18:36:25 -04001533 reftag = scsi_get_lba(sc) & 0xffffffff;
James Smarte2a0a9d2008-12-04 22:40:02 -05001534
1535 split_offset = 0;
1536 do {
James Smart6c8eea52010-04-06 14:49:53 -04001537 /* setup PDE5 with what we have */
1538 pde5 = (struct lpfc_pde5 *) bpl;
1539 memset(pde5, 0, sizeof(struct lpfc_pde5));
1540 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05001541
James Smartbc739052010-08-04 16:11:18 -04001542 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001543 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04001544 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04001545
James Smart6c8eea52010-04-06 14:49:53 -04001546 /* advance bpl and increment bde count */
1547 num_bde++;
1548 bpl++;
1549 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001550
James Smart6c8eea52010-04-06 14:49:53 -04001551 /* setup PDE6 with the rest of the info */
1552 memset(pde6, 0, sizeof(struct lpfc_pde6));
1553 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1554 bf_set(pde6_optx, pde6, txop);
1555 bf_set(pde6_oprx, pde6, rxop);
1556 bf_set(pde6_ce, pde6, 1);
1557 bf_set(pde6_re, pde6, 1);
James Smart6c8eea52010-04-06 14:49:53 -04001558 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04001559 bf_set(pde6_ae, pde6, 0);
1560 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04001561
James Smartbc739052010-08-04 16:11:18 -04001562 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001563 pde6->word0 = cpu_to_le32(pde6->word0);
1564 pde6->word1 = cpu_to_le32(pde6->word1);
1565 pde6->word2 = cpu_to_le32(pde6->word2);
1566
James Smart6c8eea52010-04-06 14:49:53 -04001567 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001568 num_bde++;
1569 bpl++;
1570
1571 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05001572 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1573 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05001574
James Smarte2a0a9d2008-12-04 22:40:02 -05001575 /* must be integer multiple of the DIF block length */
1576 BUG_ON(protgroup_len % 8);
1577
James Smart7f860592011-03-11 16:05:52 -05001578 pde7 = (struct lpfc_pde7 *) bpl;
1579 memset(pde7, 0, sizeof(struct lpfc_pde7));
1580 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1581
James Smart7c56b9f2011-07-22 18:36:25 -04001582 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1583 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05001584
James Smarte2a0a9d2008-12-04 22:40:02 -05001585 protgrp_blks = protgroup_len / 8;
1586 protgrp_bytes = protgrp_blks * blksize;
1587
James Smart7f860592011-03-11 16:05:52 -05001588 /* check if this pde is crossing the 4K boundary; if so split */
1589 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1590 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1591 protgroup_offset += protgroup_remainder;
1592 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04001593 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05001594 } else {
1595 protgroup_offset = 0;
1596 curr_prot++;
1597 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001598
James Smarte2a0a9d2008-12-04 22:40:02 -05001599 num_bde++;
1600
1601 /* setup BDE's for data blocks associated with DIF data */
1602 pgdone = 0;
1603 subtotal = 0; /* total bytes processed for current prot grp */
1604 while (!pgdone) {
1605 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001606 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1607 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001608 __func__);
1609 return 0;
1610 }
1611 bpl++;
1612 dataphysaddr = sg_dma_address(sgde) + split_offset;
1613 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1614 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1615
1616 remainder = sg_dma_len(sgde) - split_offset;
1617
1618 if ((subtotal + remainder) <= protgrp_bytes) {
1619 /* we can use this whole buffer */
1620 bpl->tus.f.bdeSize = remainder;
1621 split_offset = 0;
1622
1623 if ((subtotal + remainder) == protgrp_bytes)
1624 pgdone = 1;
1625 } else {
1626 /* must split this buffer with next prot grp */
1627 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1628 split_offset += bpl->tus.f.bdeSize;
1629 }
1630
1631 subtotal += bpl->tus.f.bdeSize;
1632
1633 if (datadir == DMA_TO_DEVICE)
1634 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1635 else
1636 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1637 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1638
1639 num_bde++;
1640 curr_data++;
1641
1642 if (split_offset)
1643 break;
1644
1645 /* Move to the next s/g segment if possible */
1646 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001647
James Smarte2a0a9d2008-12-04 22:40:02 -05001648 }
1649
James Smart7f860592011-03-11 16:05:52 -05001650 if (protgroup_offset) {
1651 /* update the reference tag */
1652 reftag += protgrp_blks;
1653 bpl++;
1654 continue;
1655 }
1656
James Smarte2a0a9d2008-12-04 22:40:02 -05001657 /* are we done ? */
1658 if (curr_prot == protcnt) {
1659 alldone = 1;
1660 } else if (curr_prot < protcnt) {
1661 /* advance to next prot buffer */
1662 sgpe = sg_next(sgpe);
1663 bpl++;
1664
1665 /* update the reference tag */
1666 reftag += protgrp_blks;
1667 } else {
1668 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001669 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1670 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001671 }
1672
1673 } while (!alldone);
1674
1675out:
1676
James Smarte2a0a9d2008-12-04 22:40:02 -05001677 return num_bde;
1678}
James Smart7f860592011-03-11 16:05:52 -05001679
James Smarte2a0a9d2008-12-04 22:40:02 -05001680/*
1681 * Given a SCSI command that supports DIF, determine composition of protection
1682 * groups involved in setting up buffer lists
1683 *
1684 * Returns:
1685 * for DIF (for both read and write)
1686 * */
1687static int
1688lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1689{
1690 int ret = LPFC_PG_TYPE_INVALID;
1691 unsigned char op = scsi_get_prot_op(sc);
1692
1693 switch (op) {
1694 case SCSI_PROT_READ_STRIP:
1695 case SCSI_PROT_WRITE_INSERT:
1696 ret = LPFC_PG_TYPE_NO_DIF;
1697 break;
1698 case SCSI_PROT_READ_INSERT:
1699 case SCSI_PROT_WRITE_STRIP:
1700 case SCSI_PROT_READ_PASS:
1701 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001702 ret = LPFC_PG_TYPE_DIF_BUF;
1703 break;
1704 default:
1705 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1706 "9021 Unsupported protection op:%d\n", op);
1707 break;
1708 }
1709
1710 return ret;
1711}
1712
1713/*
1714 * This is the protection/DIF aware version of
1715 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1716 * two functions eventually, but for now, it's here
1717 */
1718static int
1719lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1720 struct lpfc_scsi_buf *lpfc_cmd)
1721{
1722 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1723 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1724 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1725 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1726 uint32_t num_bde = 0;
1727 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1728 int prot_group_type = 0;
1729 int diflen, fcpdl;
1730 unsigned blksize;
1731
1732 /*
1733 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1734 * fcp_rsp regions to the first data bde entry
1735 */
1736 bpl += 2;
1737 if (scsi_sg_count(scsi_cmnd)) {
1738 /*
1739 * The driver stores the segment count returned from pci_map_sg
1740 * because this a count of dma-mappings used to map the use_sg
1741 * pages. They are not guaranteed to be the same for those
1742 * architectures that implement an IOMMU.
1743 */
1744 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1745 scsi_sglist(scsi_cmnd),
1746 scsi_sg_count(scsi_cmnd), datadir);
1747 if (unlikely(!datasegcnt))
1748 return 1;
1749
1750 lpfc_cmd->seg_cnt = datasegcnt;
1751 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001752 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1753 "9067 BLKGRD: %s: Too many sg segments"
1754 " from dma_map_sg. Config %d, seg_cnt"
1755 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001756 __func__, phba->cfg_sg_seg_cnt,
1757 lpfc_cmd->seg_cnt);
1758 scsi_dma_unmap(scsi_cmnd);
1759 return 1;
1760 }
1761
1762 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1763
1764 switch (prot_group_type) {
1765 case LPFC_PG_TYPE_NO_DIF:
1766 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1767 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001768 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001769 if (num_bde < 2)
1770 goto err;
1771 break;
1772 case LPFC_PG_TYPE_DIF_BUF:{
1773 /*
1774 * This type indicates that protection buffers are
1775 * passed to the driver, so that needs to be prepared
1776 * for DMA
1777 */
1778 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1779 scsi_prot_sglist(scsi_cmnd),
1780 scsi_prot_sg_count(scsi_cmnd), datadir);
1781 if (unlikely(!protsegcnt)) {
1782 scsi_dma_unmap(scsi_cmnd);
1783 return 1;
1784 }
1785
1786 lpfc_cmd->prot_seg_cnt = protsegcnt;
1787 if (lpfc_cmd->prot_seg_cnt
1788 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001789 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1790 "9068 BLKGRD: %s: Too many prot sg "
1791 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05001792 "prot_seg_cnt %d\n", __func__,
1793 phba->cfg_prot_sg_seg_cnt,
1794 lpfc_cmd->prot_seg_cnt);
1795 dma_unmap_sg(&phba->pcidev->dev,
1796 scsi_prot_sglist(scsi_cmnd),
1797 scsi_prot_sg_count(scsi_cmnd),
1798 datadir);
1799 scsi_dma_unmap(scsi_cmnd);
1800 return 1;
1801 }
1802
1803 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1804 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001805 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001806 if (num_bde < 3)
1807 goto err;
1808 break;
1809 }
1810 case LPFC_PG_TYPE_INVALID:
1811 default:
1812 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1813 "9022 Unexpected protection group %i\n",
1814 prot_group_type);
1815 return 1;
1816 }
1817 }
1818
1819 /*
1820 * Finish initializing those IOCB fields that are dependent on the
1821 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1822 * reinitialized since all iocb memory resources are used many times
1823 * for transmit, receive, and continuation bpl's.
1824 */
1825 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1826 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1827 iocb_cmd->ulpBdeCount = 1;
1828 iocb_cmd->ulpLe = 1;
1829
1830 fcpdl = scsi_bufflen(scsi_cmnd);
1831
1832 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1833 /*
1834 * We are in DIF Type 1 mode
1835 * Every data block has a 8 byte DIF (trailer)
1836 * attached to it. Must ajust FCP data length
1837 */
1838 blksize = lpfc_cmd_blksize(scsi_cmnd);
1839 diflen = (fcpdl / blksize) * 8;
1840 fcpdl += diflen;
1841 }
1842 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1843
1844 /*
1845 * Due to difference in data length between DIF/non-DIF paths,
1846 * we need to set word 4 of IOCB here
1847 */
1848 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1849
1850 return 0;
1851err:
1852 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1853 "9023 Could not setup all needed BDE's"
1854 "prot_group_type=%d, num_bde=%d\n",
1855 prot_group_type, num_bde);
1856 return 1;
1857}
1858
1859/*
1860 * This function checks for BlockGuard errors detected by
1861 * the HBA. In case of errors, the ASC/ASCQ fields in the
1862 * sense buffer will be set accordingly, paired with
1863 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1864 * detected corruption.
1865 *
1866 * Returns:
1867 * 0 - No error found
1868 * 1 - BlockGuard error found
1869 * -1 - Internal error (bad profile, ...etc)
1870 */
1871static int
1872lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1873 struct lpfc_iocbq *pIocbOut)
1874{
1875 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1876 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1877 int ret = 0;
1878 uint32_t bghm = bgf->bghm;
1879 uint32_t bgstat = bgf->bgstat;
1880 uint64_t failing_sector = 0;
1881
James Smart6a9c52c2009-10-02 15:16:51 -04001882 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
1883 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05001884 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05001885 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09001886 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05001887
1888 spin_lock(&_dump_buf_lock);
1889 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04001890 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
1891 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001892 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04001893 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001894
1895 /* If we have a prot sgl, save the DIF buffer */
1896 if (lpfc_prot_group_type(phba, cmd) ==
1897 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04001898 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
1899 "Saving DIF for %u blocks to debugfs\n",
1900 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1901 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001902 }
1903
1904 _dump_buf_done = 1;
1905 }
1906 spin_unlock(&_dump_buf_lock);
1907
1908 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1909 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001910 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
1911 " BlockGuard profile. bgstat:0x%x\n",
1912 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05001913 ret = (-1);
1914 goto out;
1915 }
1916
1917 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1918 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001919 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
1920 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001921 bgstat);
1922 ret = (-1);
1923 goto out;
1924 }
1925
1926 if (lpfc_bgs_get_guard_err(bgstat)) {
1927 ret = 1;
1928
1929 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1930 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001931 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001932 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1933 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001934 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1935 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001936 }
1937
1938 if (lpfc_bgs_get_reftag_err(bgstat)) {
1939 ret = 1;
1940
1941 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1942 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001943 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001944 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1945
1946 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001947 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1948 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001949 }
1950
1951 if (lpfc_bgs_get_apptag_err(bgstat)) {
1952 ret = 1;
1953
1954 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1955 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001956 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001957 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1958
1959 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001960 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1961 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001962 }
1963
1964 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1965 /*
1966 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04001967 * field, and put the failing LBA in it.
1968 * This code assumes there was also a guard/app/ref tag error
1969 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05001970 */
James Smart7c56b9f2011-07-22 18:36:25 -04001971 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
1972 cmd->sense_buffer[8] = 0; /* Information descriptor type */
1973 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
1974 cmd->sense_buffer[10] = 0x80; /* Validity bit */
David Howells2344b5b2009-04-14 17:08:34 +01001975 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05001976
1977 failing_sector = scsi_get_lba(cmd);
1978 failing_sector += bghm;
1979
James Smart7c56b9f2011-07-22 18:36:25 -04001980 /* Descriptor Information */
1981 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05001982 }
1983
1984 if (!ret) {
1985 /* No error was reported - problem in FW? */
1986 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001987 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1988 "9057 BLKGRD: no errors reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001989 }
1990
1991out:
1992 return ret;
1993}
1994
James Smartea2151b2008-09-07 11:52:10 -04001995/**
James Smartda0436e2009-05-22 14:51:39 -04001996 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1997 * @phba: The Hba for which this call is being executed.
1998 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1999 *
2000 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2001 * field of @lpfc_cmd for device with SLI-4 interface spec.
2002 *
2003 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002004 * 1 - Error
2005 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04002006 **/
2007static int
2008lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2009{
2010 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2011 struct scatterlist *sgel = NULL;
2012 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2013 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05002014 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04002015 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2016 dma_addr_t physaddr;
2017 uint32_t num_bde = 0;
2018 uint32_t dma_len;
2019 uint32_t dma_offset = 0;
2020 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05002021 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04002022
2023 /*
2024 * There are three possibilities here - use scatter-gather segment, use
2025 * the single mapping, or neither. Start the lpfc command prep by
2026 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2027 * data bde entry.
2028 */
2029 if (scsi_sg_count(scsi_cmnd)) {
2030 /*
2031 * The driver stores the segment count returned from pci_map_sg
2032 * because this a count of dma-mappings used to map the use_sg
2033 * pages. They are not guaranteed to be the same for those
2034 * architectures that implement an IOMMU.
2035 */
2036
2037 nseg = scsi_dma_map(scsi_cmnd);
2038 if (unlikely(!nseg))
2039 return 1;
2040 sgl += 1;
2041 /* clear the last flag in the fcp_rsp map entry */
2042 sgl->word2 = le32_to_cpu(sgl->word2);
2043 bf_set(lpfc_sli4_sge_last, sgl, 0);
2044 sgl->word2 = cpu_to_le32(sgl->word2);
2045 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05002046 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04002047 lpfc_cmd->seg_cnt = nseg;
2048 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04002049 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
2050 " %s: Too many sg segments from "
2051 "dma_map_sg. Config %d, seg_cnt %d\n",
2052 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04002053 lpfc_cmd->seg_cnt);
2054 scsi_dma_unmap(scsi_cmnd);
2055 return 1;
2056 }
2057
2058 /*
2059 * The driver established a maximum scatter-gather segment count
2060 * during probe that limits the number of sg elements in any
2061 * single scsi command. Just run through the seg_cnt and format
2062 * the sge's.
2063 * When using SLI-3 the driver will try to fit all the BDEs into
2064 * the IOCB. If it can't then the BDEs get added to a BPL as it
2065 * does for SLI-2 mode.
2066 */
2067 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
2068 physaddr = sg_dma_address(sgel);
2069 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04002070 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2071 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04002072 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04002073 if ((num_bde + 1) == nseg)
2074 bf_set(lpfc_sli4_sge_last, sgl, 1);
2075 else
2076 bf_set(lpfc_sli4_sge_last, sgl, 0);
2077 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2078 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05002079 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04002080 dma_offset += dma_len;
2081 sgl++;
2082 }
James Smartfedd3b72011-02-16 12:39:24 -05002083 /* setup the performance hint (first data BDE) if enabled */
2084 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
2085 bde = (struct ulp_bde64 *)
2086 &(iocb_cmd->unsli3.sli3Words[5]);
2087 bde->addrLow = first_data_sgl->addr_lo;
2088 bde->addrHigh = first_data_sgl->addr_hi;
2089 bde->tus.f.bdeSize =
2090 le32_to_cpu(first_data_sgl->sge_len);
2091 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2092 bde->tus.w = cpu_to_le32(bde->tus.w);
2093 }
James Smartda0436e2009-05-22 14:51:39 -04002094 } else {
2095 sgl += 1;
2096 /* clear the last flag in the fcp_rsp map entry */
2097 sgl->word2 = le32_to_cpu(sgl->word2);
2098 bf_set(lpfc_sli4_sge_last, sgl, 1);
2099 sgl->word2 = cpu_to_le32(sgl->word2);
2100 }
2101
2102 /*
2103 * Finish initializing those IOCB fields that are dependent on the
2104 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
2105 * explicitly reinitialized.
2106 * all iocb memory resources are reused.
2107 */
2108 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
2109
2110 /*
2111 * Due to difference in data length between DIF/non-DIF paths,
2112 * we need to set word 4 of IOCB here
2113 */
2114 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
2115 return 0;
2116}
2117
2118/**
James Smart3772a992009-05-22 14:50:54 -04002119 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
2120 * @phba: The Hba for which this call is being executed.
2121 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2122 *
2123 * This routine wraps the actual DMA mapping function pointer from the
2124 * lpfc_hba struct.
2125 *
2126 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002127 * 1 - Error
2128 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04002129 **/
2130static inline int
2131lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2132{
2133 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2134}
2135
2136/**
James Smart3621a712009-04-06 18:47:14 -04002137 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04002138 * @phba: Pointer to hba context object.
2139 * @vport: Pointer to vport object.
2140 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
2141 * @rsp_iocb: Pointer to response iocb object which reported error.
2142 *
2143 * This function posts an event when there is a SCSI command reporting
2144 * error from the scsi device.
2145 **/
2146static void
2147lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
2148 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
2149 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2150 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2151 uint32_t resp_info = fcprsp->rspStatus2;
2152 uint32_t scsi_status = fcprsp->rspStatus3;
2153 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2154 struct lpfc_fast_path_event *fast_path_evt = NULL;
2155 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
2156 unsigned long flags;
2157
James Smart5989b8d2010-10-22 11:06:56 -04002158 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2159 return;
2160
James Smartea2151b2008-09-07 11:52:10 -04002161 /* If there is queuefull or busy condition send a scsi event */
2162 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
2163 (cmnd->result == SAM_STAT_BUSY)) {
2164 fast_path_evt = lpfc_alloc_fast_evt(phba);
2165 if (!fast_path_evt)
2166 return;
2167 fast_path_evt->un.scsi_evt.event_type =
2168 FC_REG_SCSI_EVENT;
2169 fast_path_evt->un.scsi_evt.subcategory =
2170 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
2171 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
2172 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
2173 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
2174 &pnode->nlp_portname, sizeof(struct lpfc_name));
2175 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
2176 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2177 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
2178 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
2179 fast_path_evt = lpfc_alloc_fast_evt(phba);
2180 if (!fast_path_evt)
2181 return;
2182 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
2183 FC_REG_SCSI_EVENT;
2184 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
2185 LPFC_EVENT_CHECK_COND;
2186 fast_path_evt->un.check_cond_evt.scsi_event.lun =
2187 cmnd->device->lun;
2188 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
2189 &pnode->nlp_portname, sizeof(struct lpfc_name));
2190 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
2191 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2192 fast_path_evt->un.check_cond_evt.sense_key =
2193 cmnd->sense_buffer[2] & 0xf;
2194 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
2195 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
2196 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2197 fcpi_parm &&
2198 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
2199 ((scsi_status == SAM_STAT_GOOD) &&
2200 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
2201 /*
2202 * If status is good or resid does not match with fcp_param and
2203 * there is valid fcpi_parm, then there is a read_check error
2204 */
2205 fast_path_evt = lpfc_alloc_fast_evt(phba);
2206 if (!fast_path_evt)
2207 return;
2208 fast_path_evt->un.read_check_error.header.event_type =
2209 FC_REG_FABRIC_EVENT;
2210 fast_path_evt->un.read_check_error.header.subcategory =
2211 LPFC_EVENT_FCPRDCHKERR;
2212 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
2213 &pnode->nlp_portname, sizeof(struct lpfc_name));
2214 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2215 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2216 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2217 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2218 fast_path_evt->un.read_check_error.fcpiparam =
2219 fcpi_parm;
2220 } else
2221 return;
2222
2223 fast_path_evt->vport = vport;
2224 spin_lock_irqsave(&phba->hbalock, flags);
2225 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2226 spin_unlock_irqrestore(&phba->hbalock, flags);
2227 lpfc_worker_wake_up(phba);
2228 return;
2229}
James Smart9bad7672008-12-04 22:39:02 -05002230
2231/**
James Smartf1126682009-06-10 17:22:44 -04002232 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04002233 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05002234 * @psb: The scsi buffer which is going to be un-mapped.
2235 *
2236 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04002237 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002238 **/
dea31012005-04-17 16:05:31 -05002239static void
James Smartf1126682009-06-10 17:22:44 -04002240lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04002241{
2242 /*
2243 * There are only two special cases to consider. (1) the scsi command
2244 * requested scatter-gather usage or (2) the scsi command allocated
2245 * a request buffer, but did not request use_sg. There is a third
2246 * case, but it does not require resource deallocation.
2247 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002248 if (psb->seg_cnt > 0)
2249 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002250 if (psb->prot_seg_cnt > 0)
2251 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2252 scsi_prot_sg_count(psb->pCmd),
2253 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04002254}
2255
James Smart9bad7672008-12-04 22:39:02 -05002256/**
James Smart3621a712009-04-06 18:47:14 -04002257 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05002258 * @vport: The virtual port for which this call is being executed.
2259 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2260 * @rsp_iocb: The response IOCB which contains FCP error.
2261 *
2262 * This routine is called to process response IOCB with status field
2263 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2264 * based upon SCSI and FCP error.
2265 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04002266static void
James Smart2e0fef82007-06-17 19:56:36 -05002267lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2268 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05002269{
2270 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2271 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2272 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04002273 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05002274 uint32_t resp_info = fcprsp->rspStatus2;
2275 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05002276 uint32_t *lp;
dea31012005-04-17 16:05:31 -05002277 uint32_t host_status = DID_OK;
2278 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05002279 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05002280
James Smartea2151b2008-09-07 11:52:10 -04002281
dea31012005-04-17 16:05:31 -05002282 /*
2283 * If this is a task management command, there is no
2284 * scsi packet associated with this lpfc_cmd. The driver
2285 * consumes it.
2286 */
2287 if (fcpcmd->fcpCntl2) {
2288 scsi_status = 0;
2289 goto out;
2290 }
2291
James Smart6a9c52c2009-10-02 15:16:51 -04002292 if (resp_info & RSP_LEN_VALID) {
2293 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05002294 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04002295 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2296 "2719 Invalid response length: "
2297 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2298 cmnd->device->id,
2299 cmnd->device->lun, cmnd->cmnd[0],
2300 rsplen);
2301 host_status = DID_ERROR;
2302 goto out;
2303 }
James Smarte40a02c2010-02-26 14:13:54 -05002304 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
2305 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2306 "2757 Protocol failure detected during "
2307 "processing of FCP I/O op: "
2308 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
2309 cmnd->device->id,
2310 cmnd->device->lun, cmnd->cmnd[0],
2311 fcprsp->rspInfo3);
2312 host_status = DID_ERROR;
2313 goto out;
2314 }
James Smart6a9c52c2009-10-02 15:16:51 -04002315 }
2316
James Smartc7743952006-12-02 13:34:42 -05002317 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2318 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2319 if (snslen > SCSI_SENSE_BUFFERSIZE)
2320 snslen = SCSI_SENSE_BUFFERSIZE;
2321
2322 if (resp_info & RSP_LEN_VALID)
2323 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2324 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2325 }
2326 lp = (uint32_t *)cmnd->sense_buffer;
2327
2328 if (!scsi_status && (resp_info & RESID_UNDER))
2329 logit = LOG_FCP;
2330
James Smarte8b62012007-08-02 11:10:09 -04002331 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05002332 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04002333 "Data: x%x x%x x%x x%x x%x\n",
2334 cmnd->cmnd[0], scsi_status,
2335 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2336 be32_to_cpu(fcprsp->rspResId),
2337 be32_to_cpu(fcprsp->rspSnsLen),
2338 be32_to_cpu(fcprsp->rspRspLen),
2339 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05002340
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002341 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05002342 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002343 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05002344
James Smarte8b62012007-08-02 11:10:09 -04002345 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002346 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04002347 "residual %d Data: x%x x%x x%x\n",
2348 be32_to_cpu(fcpcmd->fcpDl),
2349 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2350 cmnd->underflow);
dea31012005-04-17 16:05:31 -05002351
2352 /*
James Smart7054a602007-04-25 09:52:34 -04002353 * If there is an under run check if under run reported by
2354 * storage array is same as the under run reported by HBA.
2355 * If this is not same, there is a dropped frame.
2356 */
2357 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2358 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002359 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04002360 lpfc_printf_vlog(vport, KERN_WARNING,
2361 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002362 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04002363 "and Underrun Data: x%x x%x x%x x%x\n",
2364 be32_to_cpu(fcpcmd->fcpDl),
2365 scsi_get_resid(cmnd), fcpi_parm,
2366 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002367 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04002368 host_status = DID_ERROR;
2369 }
2370 /*
dea31012005-04-17 16:05:31 -05002371 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002372 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05002373 * is not present, make sure the actual amount transferred is at
2374 * least the underflow value or fail.
2375 */
2376 if (!(resp_info & SNS_LEN_VALID) &&
2377 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002378 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2379 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04002380 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002381 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04002382 "underrun converted to error "
2383 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04002384 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04002385 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05002386 host_status = DID_ERROR;
2387 }
2388 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04002389 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002390 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04002391 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04002392 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05002393 host_status = DID_ERROR;
2394
2395 /*
2396 * Check SLI validation that all the transfer was actually done
2397 * (fcpi_parm should be zero). Apply check only to reads.
2398 */
James Smarteee88772010-09-29 11:19:08 -04002399 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04002400 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002401 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04002402 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002403 be32_to_cpu(fcpcmd->fcpDl),
2404 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04002405 fcpi_parm, cmnd->cmnd[0], scsi_status);
2406 switch (scsi_status) {
2407 case SAM_STAT_GOOD:
2408 case SAM_STAT_CHECK_CONDITION:
2409 /* Fabric dropped a data frame. Fail any successful
2410 * command in which we detected dropped frames.
2411 * A status of good or some check conditions could
2412 * be considered a successful command.
2413 */
2414 host_status = DID_ERROR;
2415 break;
2416 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002417 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05002418 }
2419
2420 out:
2421 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04002422 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05002423}
2424
James Smart9bad7672008-12-04 22:39:02 -05002425/**
James Smart3621a712009-04-06 18:47:14 -04002426 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05002427 * @phba: The Hba for which this call is being executed.
2428 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04002429 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05002430 *
2431 * This routine assigns scsi command result by looking into response IOCB
2432 * status field appropriately. This routine handles QUEUE FULL condition as
2433 * well by ramping down device queue depth.
2434 **/
dea31012005-04-17 16:05:31 -05002435static void
2436lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2437 struct lpfc_iocbq *pIocbOut)
2438{
2439 struct lpfc_scsi_buf *lpfc_cmd =
2440 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002441 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05002442 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2443 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04002444 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002445 int result;
James Smarta257bf92009-04-06 18:48:10 -04002446 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05002447 int depth;
James Smartfa61a542008-01-11 01:52:42 -05002448 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04002449 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04002450 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04002451 uint32_t queue_depth, scsi_id;
dea31012005-04-17 16:05:31 -05002452
James Smart75baf692010-06-08 18:31:21 -04002453 /* Sanity check on return of outstanding command */
2454 if (!(lpfc_cmd->pCmd))
2455 return;
2456 cmd = lpfc_cmd->pCmd;
2457 shost = cmd->device->host;
2458
dea31012005-04-17 16:05:31 -05002459 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2460 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05002461 /* pick up SLI4 exhange busy status from HBA */
2462 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
2463
James Smart109f6ed2008-12-04 22:39:08 -05002464 if (pnode && NLP_CHK_NODE_ACT(pnode))
2465 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05002466
2467 if (lpfc_cmd->status) {
2468 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2469 (lpfc_cmd->result & IOERR_DRVR_MASK))
2470 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2471 else if (lpfc_cmd->status >= IOSTAT_CNT)
2472 lpfc_cmd->status = IOSTAT_DEFAULT;
2473
James Smarte8b62012007-08-02 11:10:09 -04002474 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002475 "9030 FCP cmd x%x failed <%d/%d> "
James Smarte8b62012007-08-02 11:10:09 -04002476 "status: x%x result: x%x Data: x%x x%x\n",
2477 cmd->cmnd[0],
2478 cmd->device ? cmd->device->id : 0xffff,
2479 cmd->device ? cmd->device->lun : 0xffff,
2480 lpfc_cmd->status, lpfc_cmd->result,
2481 pIocbOut->iocb.ulpContext,
2482 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05002483
2484 switch (lpfc_cmd->status) {
2485 case IOSTAT_FCP_RSP_ERROR:
2486 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05002487 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05002488 break;
2489 case IOSTAT_NPORT_BSY:
2490 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04002491 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04002492 fast_path_evt = lpfc_alloc_fast_evt(phba);
2493 if (!fast_path_evt)
2494 break;
2495 fast_path_evt->un.fabric_evt.event_type =
2496 FC_REG_FABRIC_EVENT;
2497 fast_path_evt->un.fabric_evt.subcategory =
2498 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2499 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2500 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2501 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2502 &pnode->nlp_portname,
2503 sizeof(struct lpfc_name));
2504 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2505 &pnode->nlp_nodename,
2506 sizeof(struct lpfc_name));
2507 }
2508 fast_path_evt->vport = vport;
2509 fast_path_evt->work_evt.evt =
2510 LPFC_EVT_FASTPATH_MGMT_EVT;
2511 spin_lock_irqsave(&phba->hbalock, flags);
2512 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2513 &phba->work_list);
2514 spin_unlock_irqrestore(&phba->hbalock, flags);
2515 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002516 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002517 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05002518 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05002519 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
2520 lpfc_cmd->result ==
2521 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
2522 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
2523 lpfc_cmd->result ==
2524 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
2525 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
2526 break;
2527 }
James Smartd7c255b2008-08-24 21:50:00 -04002528 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05002529 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04002530 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
2531 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05002532 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04002533 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05002534 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002535 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2536 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2537 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2538 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2539 /*
2540 * This is a response for a BG enabled
2541 * cmd. Parse BG error
2542 */
2543 lpfc_parse_bg_err(phba, lpfc_cmd,
2544 pIocbOut);
2545 break;
2546 } else {
2547 lpfc_printf_vlog(vport, KERN_WARNING,
2548 LOG_BG,
2549 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04002550 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002551 }
2552 }
James Smart1151e3e2011-02-16 12:39:35 -05002553 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
2554 && (phba->sli_rev == LPFC_SLI_REV4)
2555 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
2556 /* This IO was aborted by the target, we don't
2557 * know the rxid and because we did not send the
2558 * ABTS we cannot generate and RRQ.
2559 */
2560 lpfc_set_rrq_active(phba, pnode,
2561 lpfc_cmd->cur_iocbq.sli4_xritag,
2562 0, 0);
2563 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002564 /* else: fall through */
dea31012005-04-17 16:05:31 -05002565 default:
2566 cmd->result = ScsiResult(DID_ERROR, 0);
2567 break;
2568 }
2569
James Smart58da1ff2008-04-07 10:15:56 -04002570 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002571 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04002572 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2573 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05002574 } else
dea31012005-04-17 16:05:31 -05002575 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05002576
2577 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2578 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2579
James Smarte8b62012007-08-02 11:10:09 -04002580 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2581 "0710 Iodone <%d/%d> cmd %p, error "
2582 "x%x SNS x%x x%x Data: x%x x%x\n",
2583 cmd->device->id, cmd->device->lun, cmd,
2584 cmd->result, *lp, *(lp + 3), cmd->retries,
2585 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05002586 }
2587
James Smartea2151b2008-09-07 11:52:10 -04002588 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002589 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04002590 if (vport->cfg_max_scsicmpl_time &&
2591 time_after(jiffies, lpfc_cmd->start_time +
2592 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04002593 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002594 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2595 if (pnode->cmd_qdepth >
2596 atomic_read(&pnode->cmd_pending) &&
2597 (atomic_read(&pnode->cmd_pending) >
2598 LPFC_MIN_TGT_QDEPTH) &&
2599 ((cmd->cmnd[0] == READ_10) ||
2600 (cmd->cmnd[0] == WRITE_10)))
2601 pnode->cmd_qdepth =
2602 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04002603
James Smart109f6ed2008-12-04 22:39:08 -05002604 pnode->last_change_time = jiffies;
2605 }
James Smarta257bf92009-04-06 18:48:10 -04002606 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002607 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04002608 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04002609 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05002610 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04002611 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04002612 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
2613 / 100;
2614 depth = depth ? depth : 1;
2615 pnode->cmd_qdepth += depth;
2616 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
2617 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05002618 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04002619 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002620 }
James Smart977b5a02008-09-07 11:52:04 -04002621 }
2622
James Smart1dcb58e2007-04-25 09:51:30 -04002623 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04002624
2625 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2626 queue_depth = cmd->device->queue_depth;
2627 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05002628 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002629
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002630 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05002631 /*
2632 * If there is a thread waiting for command completion
2633 * wake up the thread.
2634 */
James Smarta257bf92009-04-06 18:48:10 -04002635 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002636 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002637 if (lpfc_cmd->waitq)
2638 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002639 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002640 lpfc_release_scsi_buf(phba, lpfc_cmd);
2641 return;
2642 }
2643
James Smart92d7f7b2007-06-17 19:56:38 -05002644 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04002645 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05002646
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002647 /*
2648 * Check for queue full. If the lun is reporting queue full, then
2649 * back off the lun queue depth to prevent target overloads.
2650 */
James Smart58da1ff2008-04-07 10:15:56 -04002651 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2652 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04002653 shost_for_each_device(tmp_sdev, shost) {
2654 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002655 continue;
2656 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05002657 tmp_sdev->queue_depth-1);
2658 if (depth <= 0)
2659 continue;
James Smarte8b62012007-08-02 11:10:09 -04002660 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2661 "0711 detected queue full - lun queue "
2662 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04002663 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05002664 pnode,
2665 tmp_sdev->lun,
2666 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002667 }
2668 }
2669
James Smartfa61a542008-01-11 01:52:42 -05002670 /*
2671 * If there is a thread waiting for command completion
2672 * wake up the thread.
2673 */
James Smarta257bf92009-04-06 18:48:10 -04002674 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002675 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002676 if (lpfc_cmd->waitq)
2677 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002678 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002679
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002680 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002681}
2682
James Smart34b02dc2008-08-24 21:49:55 -04002683/**
James Smart3621a712009-04-06 18:47:14 -04002684 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002685 * @data: A pointer to the immediate command data portion of the IOCB.
2686 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2687 *
2688 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2689 * byte swapping the data to big endian format for transmission on the wire.
2690 **/
2691static void
2692lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2693{
2694 int i, j;
2695 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2696 i += sizeof(uint32_t), j++) {
2697 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2698 }
2699}
2700
James Smart9bad7672008-12-04 22:39:02 -05002701/**
James Smartf1126682009-06-10 17:22:44 -04002702 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002703 * @vport: The virtual port for which this call is being executed.
2704 * @lpfc_cmd: The scsi command which needs to send.
2705 * @pnode: Pointer to lpfc_nodelist.
2706 *
2707 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002708 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002709 **/
dea31012005-04-17 16:05:31 -05002710static void
James Smartf1126682009-06-10 17:22:44 -04002711lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002712 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002713{
James Smart2e0fef82007-06-17 19:56:36 -05002714 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002715 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2716 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2717 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2718 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2719 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002720 char tag[2];
dea31012005-04-17 16:05:31 -05002721
James Smart58da1ff2008-04-07 10:15:56 -04002722 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2723 return;
2724
dea31012005-04-17 16:05:31 -05002725 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002726 /* clear task management bits */
2727 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002728
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002729 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2730 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002731
2732 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2733
James Smart7e2b19f2007-10-29 11:00:39 -04002734 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2735 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002736 case HEAD_OF_QUEUE_TAG:
2737 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2738 break;
2739 case ORDERED_QUEUE_TAG:
2740 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2741 break;
2742 default:
2743 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2744 break;
2745 }
2746 } else
2747 fcp_cmnd->fcpCntl1 = 0;
2748
2749 /*
2750 * There are three possibilities here - use scatter-gather segment, use
2751 * the single mapping, or neither. Start the lpfc command prep by
2752 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2753 * data bde entry.
2754 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002755 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002756 if (datadir == DMA_TO_DEVICE) {
2757 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002758 if (phba->sli_rev < LPFC_SLI_REV4) {
2759 iocb_cmd->un.fcpi.fcpi_parm = 0;
2760 iocb_cmd->ulpPU = 0;
2761 } else
2762 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002763 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2764 phba->fc4OutputRequests++;
2765 } else {
2766 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2767 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002768 fcp_cmnd->fcpCntl3 = READ_DATA;
2769 phba->fc4InputRequests++;
2770 }
2771 } else {
2772 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2773 iocb_cmd->un.fcpi.fcpi_parm = 0;
2774 iocb_cmd->ulpPU = 0;
2775 fcp_cmnd->fcpCntl3 = 0;
2776 phba->fc4ControlRequests++;
2777 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002778 if (phba->sli_rev == 3 &&
2779 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002780 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002781 /*
2782 * Finish initializing those IOCB fields that are independent
2783 * of the scsi_cmnd request_buffer
2784 */
2785 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04002786 if (phba->sli_rev == LPFC_SLI_REV4)
2787 piocbq->iocb.ulpContext =
2788 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05002789 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2790 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002791 else
2792 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002793
2794 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2795 piocbq->context1 = lpfc_cmd;
2796 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2797 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002798 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002799}
2800
James Smart9bad7672008-12-04 22:39:02 -05002801/**
James Smart6d368e52011-05-24 11:44:12 -04002802 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002803 * @vport: The virtual port for which this call is being executed.
2804 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2805 * @lun: Logical unit number.
2806 * @task_mgmt_cmd: SCSI task management command.
2807 *
James Smart3772a992009-05-22 14:50:54 -04002808 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2809 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002810 *
2811 * Return codes:
2812 * 0 - Error
2813 * 1 - Success
2814 **/
dea31012005-04-17 16:05:31 -05002815static int
James Smartf1126682009-06-10 17:22:44 -04002816lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05002817 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04002818 unsigned int lun,
dea31012005-04-17 16:05:31 -05002819 uint8_t task_mgmt_cmd)
2820{
dea31012005-04-17 16:05:31 -05002821 struct lpfc_iocbq *piocbq;
2822 IOCB_t *piocb;
2823 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04002824 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05002825 struct lpfc_nodelist *ndlp = rdata->pnode;
2826
James Smart58da1ff2008-04-07 10:15:56 -04002827 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2828 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05002829 return 0;
dea31012005-04-17 16:05:31 -05002830
dea31012005-04-17 16:05:31 -05002831 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002832 piocbq->vport = vport;
2833
dea31012005-04-17 16:05:31 -05002834 piocb = &piocbq->iocb;
2835
2836 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04002837 /* Clear out any old data in the FCP command area */
2838 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2839 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002840 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05002841 if (vport->phba->sli_rev == 3 &&
2842 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002843 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002844 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05002845 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04002846 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
2847 piocb->ulpContext =
2848 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
2849 }
dea31012005-04-17 16:05:31 -05002850 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2851 piocb->ulpFCP2Rcvy = 1;
2852 }
2853 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2854
2855 /* ulpTimeout is only one byte */
2856 if (lpfc_cmd->timeout > 0xff) {
2857 /*
2858 * Do not timeout the command at the firmware level.
2859 * The driver will provide the timeout mechanism.
2860 */
2861 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04002862 } else
dea31012005-04-17 16:05:31 -05002863 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04002864
2865 if (vport->phba->sli_rev == LPFC_SLI_REV4)
2866 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002867
James Smart2e0fef82007-06-17 19:56:36 -05002868 return 1;
dea31012005-04-17 16:05:31 -05002869}
2870
James Smart9bad7672008-12-04 22:39:02 -05002871/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002872 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04002873 * @phba: The hba struct for which this call is being executed.
2874 * @dev_grp: The HBA PCI-Device group number.
2875 *
2876 * This routine sets up the SCSI interface API function jump table in @phba
2877 * struct.
2878 * Returns: 0 - success, -ENODEV - failure.
2879 **/
2880int
2881lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2882{
2883
James Smartf1126682009-06-10 17:22:44 -04002884 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2885 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04002886
James Smart3772a992009-05-22 14:50:54 -04002887 switch (dev_grp) {
2888 case LPFC_PCI_DEV_LP:
2889 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2890 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04002891 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05002892 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04002893 break;
James Smartda0436e2009-05-22 14:51:39 -04002894 case LPFC_PCI_DEV_OC:
2895 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2896 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04002897 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05002898 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04002899 break;
James Smart3772a992009-05-22 14:50:54 -04002900 default:
2901 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2902 "1418 Invalid HBA PCI-device group: 0x%x\n",
2903 dev_grp);
2904 return -ENODEV;
2905 break;
2906 }
James Smart3772a992009-05-22 14:50:54 -04002907 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05002908 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04002909 return 0;
2910}
2911
2912/**
James Smart3621a712009-04-06 18:47:14 -04002913 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05002914 * @phba: The Hba for which this call is being executed.
2915 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2916 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2917 *
2918 * This routine is IOCB completion routine for device reset and target reset
2919 * routine. This routine release scsi buffer associated with lpfc_cmd.
2920 **/
James Smart7054a602007-04-25 09:52:34 -04002921static void
2922lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2923 struct lpfc_iocbq *cmdiocbq,
2924 struct lpfc_iocbq *rspiocbq)
2925{
2926 struct lpfc_scsi_buf *lpfc_cmd =
2927 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2928 if (lpfc_cmd)
2929 lpfc_release_scsi_buf(phba, lpfc_cmd);
2930 return;
2931}
2932
James Smart9bad7672008-12-04 22:39:02 -05002933/**
James Smart3621a712009-04-06 18:47:14 -04002934 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05002935 * @host: The scsi host for which this call is being executed.
2936 *
2937 * This routine provides module information about hba.
2938 *
2939 * Reutrn code:
2940 * Pointer to char - Success.
2941 **/
dea31012005-04-17 16:05:31 -05002942const char *
2943lpfc_info(struct Scsi_Host *host)
2944{
James Smart2e0fef82007-06-17 19:56:36 -05002945 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2946 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002947 int len;
2948 static char lpfcinfobuf[384];
2949
2950 memset(lpfcinfobuf,0,384);
2951 if (phba && phba->pcidev){
2952 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2953 len = strlen(lpfcinfobuf);
2954 snprintf(lpfcinfobuf + len,
2955 384-len,
2956 " on PCI bus %02x device %02x irq %d",
2957 phba->pcidev->bus->number,
2958 phba->pcidev->devfn,
2959 phba->pcidev->irq);
2960 len = strlen(lpfcinfobuf);
2961 if (phba->Port[0]) {
2962 snprintf(lpfcinfobuf + len,
2963 384-len,
2964 " port %s",
2965 phba->Port);
2966 }
James Smart65467b62010-01-26 23:08:29 -05002967 len = strlen(lpfcinfobuf);
2968 if (phba->sli4_hba.link_state.logical_speed) {
2969 snprintf(lpfcinfobuf + len,
2970 384-len,
2971 " Logical Link Speed: %d Mbps",
2972 phba->sli4_hba.link_state.logical_speed * 10);
2973 }
dea31012005-04-17 16:05:31 -05002974 }
2975 return lpfcinfobuf;
2976}
2977
James Smart9bad7672008-12-04 22:39:02 -05002978/**
James Smart3621a712009-04-06 18:47:14 -04002979 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05002980 * @phba: The Hba for which this call is being executed.
2981 *
2982 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2983 * The default value of cfg_poll_tmo is 10 milliseconds.
2984 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002985static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2986{
2987 unsigned long poll_tmo_expires =
2988 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2989
2990 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2991 mod_timer(&phba->fcp_poll_timer,
2992 poll_tmo_expires);
2993}
2994
James Smart9bad7672008-12-04 22:39:02 -05002995/**
James Smart3621a712009-04-06 18:47:14 -04002996 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05002997 * @phba: The Hba for which this call is being executed.
2998 *
2999 * This routine starts the fcp_poll_timer of @phba.
3000 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003001void lpfc_poll_start_timer(struct lpfc_hba * phba)
3002{
3003 lpfc_poll_rearm_timer(phba);
3004}
3005
James Smart9bad7672008-12-04 22:39:02 -05003006/**
James Smart3621a712009-04-06 18:47:14 -04003007 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05003008 * @ptr: Map to lpfc_hba data structure pointer.
3009 *
3010 * This routine restarts fcp_poll timer, when FCP ring polling is enable
3011 * and FCP Ring interrupt is disable.
3012 **/
3013
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003014void lpfc_poll_timeout(unsigned long ptr)
3015{
James Smart2e0fef82007-06-17 19:56:36 -05003016 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003017
3018 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003019 lpfc_sli_handle_fast_ring_event(phba,
3020 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3021
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003022 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3023 lpfc_poll_rearm_timer(phba);
3024 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003025}
3026
James Smart9bad7672008-12-04 22:39:02 -05003027/**
James Smart3621a712009-04-06 18:47:14 -04003028 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05003029 * @cmnd: Pointer to scsi_cmnd data structure.
3030 * @done: Pointer to done routine.
3031 *
3032 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
3033 * This routine prepares an IOCB from scsi command and provides to firmware.
3034 * The @done callback is invoked after driver finished processing the command.
3035 *
3036 * Return value :
3037 * 0 - Success
3038 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
3039 **/
dea31012005-04-17 16:05:31 -05003040static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003041lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
dea31012005-04-17 16:05:31 -05003042{
James Smart2e0fef82007-06-17 19:56:36 -05003043 struct Scsi_Host *shost = cmnd->device->host;
3044 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3045 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003046 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003047 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003048 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003049 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003050 int err;
dea31012005-04-17 16:05:31 -05003051
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003052 err = fc_remote_port_chkready(rport);
3053 if (err) {
3054 cmnd->result = err;
dea31012005-04-17 16:05:31 -05003055 goto out_fail_command;
3056 }
James Smart1c6f4ef52009-11-18 15:40:49 -05003057 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05003058
James Smarte2a0a9d2008-12-04 22:40:02 -05003059 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
3060 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
3061
James Smart6a9c52c2009-10-02 15:16:51 -04003062 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3063 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
3064 " op:%02x str=%s without registering for"
3065 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003066 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3067 dif_op_str[scsi_get_prot_op(cmnd)]);
3068 goto out_fail_command;
3069 }
3070
dea31012005-04-17 16:05:31 -05003071 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003072 * Catch race where our node has transitioned, but the
3073 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05003074 */
James Smartb522d7d2008-09-07 11:51:56 -04003075 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
Mike Christief55ca84d2011-02-25 14:04:27 -06003076 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
James Smartb522d7d2008-09-07 11:51:56 -04003077 goto out_fail_command;
3078 }
James Smart7dc517d2010-07-14 15:32:10 -04003079 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06003080 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05003081
James Smart19ca7602010-11-20 23:11:55 -05003082 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05003083 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05003084 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003085
James Smarte8b62012007-08-02 11:10:09 -04003086 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3087 "0707 driver's buffer pool is empty, "
3088 "IO busied\n");
dea31012005-04-17 16:05:31 -05003089 goto out_host_busy;
3090 }
3091
3092 /*
3093 * Store the midlayer's command structure for the completion phase
3094 * and complete the command initialization.
3095 */
3096 lpfc_cmd->pCmd = cmnd;
3097 lpfc_cmd->rdata = rdata;
3098 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04003099 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05003100 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
3101 cmnd->scsi_done = done;
3102
James Smarte2a0a9d2008-12-04 22:40:02 -05003103 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04003104 if (vport->phba->cfg_enable_bg) {
3105 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003106 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
3107 "str=%s\n",
3108 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3109 dif_op_str[scsi_get_prot_op(cmnd)]);
James Smart6a9c52c2009-10-02 15:16:51 -04003110 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003111 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04003112 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003113 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
3114 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
3115 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
3116 cmnd->cmnd[9]);
James Smart6a9c52c2009-10-02 15:16:51 -04003117 if (cmnd->cmnd[0] == READ_10)
3118 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003119 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003120 "count %u\n",
3121 (unsigned long long)scsi_get_lba(cmnd),
3122 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04003123 else if (cmnd->cmnd[0] == WRITE_10)
3124 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003125 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003126 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05003127 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003128 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05003129 cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04003130 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003131
3132 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3133 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04003134 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05003135 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart6a9c52c2009-10-02 15:16:51 -04003136 "9038 BLKGRD: rcvd unprotected cmd:"
3137 "%02x op:%02x str=%s\n",
3138 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3139 dif_op_str[scsi_get_prot_op(cmnd)]);
3140 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3141 "9039 BLKGRD: CDB: %02x %02x %02x "
3142 "%02x %02x %02x %02x %02x %02x %02x\n",
3143 cmnd->cmnd[0], cmnd->cmnd[1],
3144 cmnd->cmnd[2], cmnd->cmnd[3],
3145 cmnd->cmnd[4], cmnd->cmnd[5],
3146 cmnd->cmnd[6], cmnd->cmnd[7],
3147 cmnd->cmnd[8], cmnd->cmnd[9]);
3148 if (cmnd->cmnd[0] == READ_10)
3149 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3150 "9040 dbg: READ @ sector %llu, "
3151 "count %u\n",
3152 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003153 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04003154 else if (cmnd->cmnd[0] == WRITE_10)
3155 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003156 "9041 dbg: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003157 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05003158 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003159 blk_rq_sectors(cmnd->request), cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04003160 else
3161 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003162 "9042 dbg: parser not implemented\n");
James Smart6a9c52c2009-10-02 15:16:51 -04003163 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003164 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3165 }
3166
dea31012005-04-17 16:05:31 -05003167 if (err)
3168 goto out_host_busy_free_buf;
3169
James Smart2e0fef82007-06-17 19:56:36 -05003170 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05003171
James Smart977b5a02008-09-07 11:52:04 -04003172 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04003173 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05003174 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05003175 if (err) {
3176 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05003177 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05003178 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003179 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003180 spin_unlock(shost->host_lock);
3181 lpfc_sli_handle_fast_ring_event(phba,
3182 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3183
3184 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003185 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3186 lpfc_poll_rearm_timer(phba);
3187 }
3188
dea31012005-04-17 16:05:31 -05003189 return 0;
3190
3191 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04003192 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003193 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003194 out_host_busy:
3195 return SCSI_MLQUEUE_HOST_BUSY;
3196
Mike Christie34963432011-02-25 14:04:28 -06003197 out_tgt_busy:
3198 return SCSI_MLQUEUE_TARGET_BUSY;
3199
dea31012005-04-17 16:05:31 -05003200 out_fail_command:
3201 done(cmnd);
3202 return 0;
3203}
3204
Jeff Garzikf2812332010-11-16 02:10:29 -05003205static DEF_SCSI_QCMD(lpfc_queuecommand)
3206
James Smart9bad7672008-12-04 22:39:02 -05003207/**
James Smart3621a712009-04-06 18:47:14 -04003208 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003209 * @cmnd: Pointer to scsi_cmnd data structure.
3210 *
3211 * This routine aborts @cmnd pending in base driver.
3212 *
3213 * Return code :
3214 * 0x2003 - Error
3215 * 0x2002 - Success
3216 **/
dea31012005-04-17 16:05:31 -05003217static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05003218lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003219{
James Smart2e0fef82007-06-17 19:56:36 -05003220 struct Scsi_Host *shost = cmnd->device->host;
3221 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3222 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003223 struct lpfc_iocbq *iocb;
3224 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05003225 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05003226 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003227 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05003228 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003229
James Smart589a52d2010-07-14 15:30:54 -04003230 ret = fc_block_scsi_eh(cmnd);
3231 if (ret)
3232 return ret;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003233 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04003234 if (!lpfc_cmd) {
3235 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3236 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003237 "x%x ID %d LUN %d\n",
3238 ret, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04003239 return SUCCESS;
3240 }
dea31012005-04-17 16:05:31 -05003241
3242 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003243 * If pCmd field of the corresponding lpfc_scsi_buf structure
3244 * points to a different SCSI command, then the driver has
3245 * already completed this command, but the midlayer did not
3246 * see the completion before the eh fired. Just return
3247 * SUCCESS.
dea31012005-04-17 16:05:31 -05003248 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003249 iocb = &lpfc_cmd->cur_iocbq;
3250 if (lpfc_cmd->pCmd != cmnd)
3251 goto out;
dea31012005-04-17 16:05:31 -05003252
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003253 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05003254
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003255 abtsiocb = lpfc_sli_get_iocbq(phba);
3256 if (abtsiocb == NULL) {
3257 ret = FAILED;
dea31012005-04-17 16:05:31 -05003258 goto out;
3259 }
3260
dea31012005-04-17 16:05:31 -05003261 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003262 * The scsi command can not be in txq and it is in flight because the
3263 * pCmd is still pointig at the SCSI command we have to abort. There
3264 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05003265 */
dea31012005-04-17 16:05:31 -05003266
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003267 cmd = &iocb->iocb;
3268 icmd = &abtsiocb->iocb;
3269 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3270 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04003271 if (phba->sli_rev == LPFC_SLI_REV4)
3272 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3273 else
3274 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003275
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003276 icmd->ulpLe = 1;
3277 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05003278
3279 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
3280 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05003281 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05003282
James Smart2e0fef82007-06-17 19:56:36 -05003283 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003284 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3285 else
3286 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05003287
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003288 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05003289 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04003290 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3291 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003292 lpfc_sli_release_iocbq(phba, abtsiocb);
3293 ret = FAILED;
3294 goto out;
3295 }
3296
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003297 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04003298 lpfc_sli_handle_fast_ring_event(phba,
3299 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003300
James Smartfa61a542008-01-11 01:52:42 -05003301 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003302 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05003303 wait_event_timeout(waitq,
3304 (lpfc_cmd->pCmd != cmnd),
3305 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003306
James Smartfa61a542008-01-11 01:52:42 -05003307 spin_lock_irq(shost->host_lock);
3308 lpfc_cmd->waitq = NULL;
3309 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003310
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003311 if (lpfc_cmd->pCmd == cmnd) {
3312 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04003313 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3314 "0748 abort handler timed out waiting "
3315 "for abort to complete: ret %#x, ID %d, "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003316 "LUN %d\n",
3317 ret, cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05003318 }
3319
3320 out:
James Smarte8b62012007-08-02 11:10:09 -04003321 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3322 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003323 "LUN %d\n", ret, cmnd->device->id,
3324 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003325 return ret;
dea31012005-04-17 16:05:31 -05003326}
3327
James Smartbbb9d182009-06-10 17:23:16 -04003328static char *
3329lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3330{
3331 switch (task_mgmt_cmd) {
3332 case FCP_ABORT_TASK_SET:
3333 return "ABORT_TASK_SET";
3334 case FCP_CLEAR_TASK_SET:
3335 return "FCP_CLEAR_TASK_SET";
3336 case FCP_BUS_RESET:
3337 return "FCP_BUS_RESET";
3338 case FCP_LUN_RESET:
3339 return "FCP_LUN_RESET";
3340 case FCP_TARGET_RESET:
3341 return "FCP_TARGET_RESET";
3342 case FCP_CLEAR_ACA:
3343 return "FCP_CLEAR_ACA";
3344 case FCP_TERMINATE_TASK:
3345 return "FCP_TERMINATE_TASK";
3346 default:
3347 return "unknown";
3348 }
3349}
3350
3351/**
3352 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3353 * @vport: The virtual port for which this call is being executed.
3354 * @rdata: Pointer to remote port local data
3355 * @tgt_id: Target ID of remote device.
3356 * @lun_id: Lun number for the TMF
3357 * @task_mgmt_cmd: type of TMF to send
3358 *
3359 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3360 * a remote port.
3361 *
3362 * Return Code:
3363 * 0x2003 - Error
3364 * 0x2002 - Success.
3365 **/
3366static int
3367lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3368 unsigned tgt_id, unsigned int lun_id,
3369 uint8_t task_mgmt_cmd)
3370{
3371 struct lpfc_hba *phba = vport->phba;
3372 struct lpfc_scsi_buf *lpfc_cmd;
3373 struct lpfc_iocbq *iocbq;
3374 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04003375 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003376 int ret;
3377 int status;
3378
James Smart5989b8d2010-10-22 11:06:56 -04003379 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04003380 return FAILED;
3381
James Smart19ca7602010-11-20 23:11:55 -05003382 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04003383 if (lpfc_cmd == NULL)
3384 return FAILED;
3385 lpfc_cmd->timeout = 60;
3386 lpfc_cmd->rdata = rdata;
3387
3388 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3389 task_mgmt_cmd);
3390 if (!status) {
3391 lpfc_release_scsi_buf(phba, lpfc_cmd);
3392 return FAILED;
3393 }
3394
3395 iocbq = &lpfc_cmd->cur_iocbq;
3396 iocbqrsp = lpfc_sli_get_iocbq(phba);
3397 if (iocbqrsp == NULL) {
3398 lpfc_release_scsi_buf(phba, lpfc_cmd);
3399 return FAILED;
3400 }
3401
3402 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3403 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04003404 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04003405 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04003406 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
3407 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04003408
3409 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3410 iocbq, iocbqrsp, lpfc_cmd->timeout);
3411 if (status != IOCB_SUCCESS) {
3412 if (status == IOCB_TIMEDOUT) {
3413 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3414 ret = TIMEOUT_ERROR;
3415 } else
3416 ret = FAILED;
3417 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3418 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04003419 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
3420 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04003421 lpfc_taskmgmt_name(task_mgmt_cmd),
3422 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04003423 iocbqrsp->iocb.un.ulpWord[4],
3424 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04003425 } else if (status == IOCB_BUSY)
3426 ret = FAILED;
3427 else
James Smartbbb9d182009-06-10 17:23:16 -04003428 ret = SUCCESS;
3429
3430 lpfc_sli_release_iocbq(phba, iocbqrsp);
3431
3432 if (ret != TIMEOUT_ERROR)
3433 lpfc_release_scsi_buf(phba, lpfc_cmd);
3434
3435 return ret;
3436}
3437
3438/**
3439 * lpfc_chk_tgt_mapped -
3440 * @vport: The virtual port to check on
3441 * @cmnd: Pointer to scsi_cmnd data structure.
3442 *
3443 * This routine delays until the scsi target (aka rport) for the
3444 * command exists (is present and logged in) or we declare it non-existent.
3445 *
3446 * Return code :
3447 * 0x2003 - Error
3448 * 0x2002 - Success
3449 **/
3450static int
3451lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3452{
3453 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003454 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003455 unsigned long later;
3456
James Smart1c6f4ef52009-11-18 15:40:49 -05003457 if (!rdata) {
3458 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3459 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
3460 return FAILED;
3461 }
3462 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003463 /*
3464 * If target is not in a MAPPED state, delay until
3465 * target is rediscovered or devloss timeout expires.
3466 */
3467 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3468 while (time_after(later, jiffies)) {
3469 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3470 return FAILED;
3471 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3472 return SUCCESS;
3473 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3474 rdata = cmnd->device->hostdata;
3475 if (!rdata)
3476 return FAILED;
3477 pnode = rdata->pnode;
3478 }
3479 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3480 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3481 return FAILED;
3482 return SUCCESS;
3483}
3484
3485/**
3486 * lpfc_reset_flush_io_context -
3487 * @vport: The virtual port (scsi_host) for the flush context
3488 * @tgt_id: If aborting by Target contect - specifies the target id
3489 * @lun_id: If aborting by Lun context - specifies the lun id
3490 * @context: specifies the context level to flush at.
3491 *
3492 * After a reset condition via TMF, we need to flush orphaned i/o
3493 * contexts from the adapter. This routine aborts any contexts
3494 * outstanding, then waits for their completions. The wait is
3495 * bounded by devloss_tmo though.
3496 *
3497 * Return code :
3498 * 0x2003 - Error
3499 * 0x2002 - Success
3500 **/
3501static int
3502lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3503 uint64_t lun_id, lpfc_ctx_cmd context)
3504{
3505 struct lpfc_hba *phba = vport->phba;
3506 unsigned long later;
3507 int cnt;
3508
3509 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3510 if (cnt)
3511 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3512 tgt_id, lun_id, context);
3513 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3514 while (time_after(later, jiffies) && cnt) {
3515 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3516 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3517 }
3518 if (cnt) {
3519 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3520 "0724 I/O flush failure for context %s : cnt x%x\n",
3521 ((context == LPFC_CTX_LUN) ? "LUN" :
3522 ((context == LPFC_CTX_TGT) ? "TGT" :
3523 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3524 cnt);
3525 return FAILED;
3526 }
3527 return SUCCESS;
3528}
3529
James Smart9bad7672008-12-04 22:39:02 -05003530/**
James Smart3621a712009-04-06 18:47:14 -04003531 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05003532 * @cmnd: Pointer to scsi_cmnd data structure.
3533 *
James Smartbbb9d182009-06-10 17:23:16 -04003534 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05003535 * command.
3536 *
3537 * Return code :
3538 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04003539 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003540 **/
dea31012005-04-17 16:05:31 -05003541static int
James Smart7054a602007-04-25 09:52:34 -04003542lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003543{
James Smart2e0fef82007-06-17 19:56:36 -05003544 struct Scsi_Host *shost = cmnd->device->host;
3545 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003546 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003547 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003548 unsigned tgt_id = cmnd->device->id;
3549 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04003550 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003551 int status;
dea31012005-04-17 16:05:31 -05003552
James Smart1c6f4ef52009-11-18 15:40:49 -05003553 if (!rdata) {
3554 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3555 "0798 Device Reset rport failure: rdata x%p\n", rdata);
3556 return FAILED;
3557 }
3558 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003559 status = fc_block_scsi_eh(cmnd);
3560 if (status)
3561 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003562
3563 status = lpfc_chk_tgt_mapped(vport, cmnd);
3564 if (status == FAILED) {
3565 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3566 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3567 return FAILED;
3568 }
3569
3570 scsi_event.event_type = FC_REG_SCSI_EVENT;
3571 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3572 scsi_event.lun = lun_id;
3573 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3574 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3575
3576 fc_host_post_vendor_event(shost, fc_get_event_number(),
3577 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3578
3579 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3580 FCP_LUN_RESET);
3581
3582 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3583 "0713 SCSI layer issued Device Reset (%d, %d) "
3584 "return x%x\n", tgt_id, lun_id, status);
3585
dea31012005-04-17 16:05:31 -05003586 /*
James Smartbbb9d182009-06-10 17:23:16 -04003587 * We have to clean up i/o as : they may be orphaned by the TMF;
3588 * or if the TMF failed, they may be in an indeterminate state.
3589 * So, continue on.
3590 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05003591 */
James Smartbbb9d182009-06-10 17:23:16 -04003592 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3593 LPFC_CTX_LUN);
3594 return status;
3595}
3596
3597/**
3598 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3599 * @cmnd: Pointer to scsi_cmnd data structure.
3600 *
3601 * This routine does a target reset by sending a TARGET_RESET task management
3602 * command.
3603 *
3604 * Return code :
3605 * 0x2003 - Error
3606 * 0x2002 - Success
3607 **/
3608static int
3609lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3610{
3611 struct Scsi_Host *shost = cmnd->device->host;
3612 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3613 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003614 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003615 unsigned tgt_id = cmnd->device->id;
3616 unsigned int lun_id = cmnd->device->lun;
3617 struct lpfc_scsi_event_header scsi_event;
3618 int status;
3619
James Smart1c6f4ef52009-11-18 15:40:49 -05003620 if (!rdata) {
3621 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3622 "0799 Target Reset rport failure: rdata x%p\n", rdata);
3623 return FAILED;
3624 }
3625 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003626 status = fc_block_scsi_eh(cmnd);
3627 if (status)
3628 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003629
3630 status = lpfc_chk_tgt_mapped(vport, cmnd);
3631 if (status == FAILED) {
3632 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3633 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3634 return FAILED;
dea31012005-04-17 16:05:31 -05003635 }
James Smartea2151b2008-09-07 11:52:10 -04003636
3637 scsi_event.event_type = FC_REG_SCSI_EVENT;
3638 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3639 scsi_event.lun = 0;
3640 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3641 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3642
James Smartbbb9d182009-06-10 17:23:16 -04003643 fc_host_post_vendor_event(shost, fc_get_event_number(),
3644 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04003645
James Smartbbb9d182009-06-10 17:23:16 -04003646 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3647 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05003648
James Smarte8b62012007-08-02 11:10:09 -04003649 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04003650 "0723 SCSI layer issued Target Reset (%d, %d) "
3651 "return x%x\n", tgt_id, lun_id, status);
3652
3653 /*
3654 * We have to clean up i/o as : they may be orphaned by the TMF;
3655 * or if the TMF failed, they may be in an indeterminate state.
3656 * So, continue on.
3657 * We will report success if all the i/o aborts successfully.
3658 */
3659 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3660 LPFC_CTX_TGT);
3661 return status;
dea31012005-04-17 16:05:31 -05003662}
3663
James Smart9bad7672008-12-04 22:39:02 -05003664/**
James Smart3621a712009-04-06 18:47:14 -04003665 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003666 * @cmnd: Pointer to scsi_cmnd data structure.
3667 *
James Smartbbb9d182009-06-10 17:23:16 -04003668 * This routine does target reset to all targets on @cmnd->device->host.
3669 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05003670 *
James Smartbbb9d182009-06-10 17:23:16 -04003671 * Return code :
3672 * 0x2003 - Error
3673 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003674 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003675static int
James Smart7054a602007-04-25 09:52:34 -04003676lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003677{
James Smart2e0fef82007-06-17 19:56:36 -05003678 struct Scsi_Host *shost = cmnd->device->host;
3679 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003680 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04003681 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003682 int match;
3683 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04003684
3685 scsi_event.event_type = FC_REG_SCSI_EVENT;
3686 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3687 scsi_event.lun = 0;
3688 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3689 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3690
James Smartbbb9d182009-06-10 17:23:16 -04003691 fc_host_post_vendor_event(shost, fc_get_event_number(),
3692 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05003693
James Smart589a52d2010-07-14 15:30:54 -04003694 ret = fc_block_scsi_eh(cmnd);
3695 if (ret)
3696 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04003697
dea31012005-04-17 16:05:31 -05003698 /*
3699 * Since the driver manages a single bus device, reset all
3700 * targets known to the driver. Should any target reset
3701 * fail, this routine returns failure to the midlayer.
3702 */
James Smarte17da182006-07-06 15:49:25 -04003703 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04003704 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05003705 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003706 spin_lock_irq(shost->host_lock);
3707 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003708 if (!NLP_CHK_NODE_ACT(ndlp))
3709 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003710 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04003711 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04003712 ndlp->rport) {
dea31012005-04-17 16:05:31 -05003713 match = 1;
3714 break;
3715 }
3716 }
James Smart2e0fef82007-06-17 19:56:36 -05003717 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003718 if (!match)
3719 continue;
James Smartbbb9d182009-06-10 17:23:16 -04003720
3721 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3722 i, 0, FCP_TARGET_RESET);
3723
3724 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04003725 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3726 "0700 Bus Reset on target %d failed\n",
3727 i);
James Smart915caaa2008-06-14 22:52:38 -04003728 ret = FAILED;
dea31012005-04-17 16:05:31 -05003729 }
3730 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003731 /*
James Smartbbb9d182009-06-10 17:23:16 -04003732 * We have to clean up i/o as : they may be orphaned by the TMFs
3733 * above; or if any of the TMFs failed, they may be in an
3734 * indeterminate state.
3735 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003736 */
James Smartbbb9d182009-06-10 17:23:16 -04003737
3738 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3739 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003740 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04003741
James Smarte8b62012007-08-02 11:10:09 -04003742 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3743 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05003744 return ret;
3745}
3746
James Smart9bad7672008-12-04 22:39:02 -05003747/**
James Smart3621a712009-04-06 18:47:14 -04003748 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05003749 * @sdev: Pointer to scsi_device.
3750 *
3751 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3752 * globally available list of scsi buffers. This routine also makes sure scsi
3753 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3754 * of scsi buffer exists for the lifetime of the driver.
3755 *
3756 * Return codes:
3757 * non-0 - Error
3758 * 0 - Success
3759 **/
dea31012005-04-17 16:05:31 -05003760static int
dea31012005-04-17 16:05:31 -05003761lpfc_slave_alloc(struct scsi_device *sdev)
3762{
James Smart2e0fef82007-06-17 19:56:36 -05003763 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3764 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003765 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04003766 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05003767 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04003768 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04003769 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05003770
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003771 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05003772 return -ENXIO;
3773
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003774 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04003775 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05003776
3777 /*
3778 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3779 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04003780 * HBA limit conveyed to the midlayer via the host structure. The
3781 * formula accounts for the lun_queue_depth + error handlers + 1
3782 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003783 */
3784 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003785 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003786
James Smartd7c47992010-06-08 18:31:54 -04003787 /* If allocated buffers are enough do nothing */
3788 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
3789 return 0;
3790
James Smart92d7f7b2007-06-17 19:56:38 -05003791 /* Allow some exchanges to be available always to complete discovery */
3792 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003793 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3794 "0704 At limitation of %d preallocated "
3795 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003796 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003797 /* Allow some exchanges to be available always to complete discovery */
3798 } else if (total + num_to_alloc >
3799 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003800 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3801 "0705 Allocation request of %d "
3802 "command buffers will exceed max of %d. "
3803 "Reducing allocation request to %d.\n",
3804 num_to_alloc, phba->cfg_hba_queue_depth,
3805 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003806 num_to_alloc = phba->cfg_hba_queue_depth - total;
3807 }
James Smart3772a992009-05-22 14:50:54 -04003808 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3809 if (num_to_alloc != num_allocated) {
3810 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3811 "0708 Allocation request of %d "
3812 "command buffers did not succeed. "
3813 "Allocated %d buffers.\n",
3814 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05003815 }
James Smart1c6f4ef52009-11-18 15:40:49 -05003816 if (num_allocated > 0)
3817 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05003818 return 0;
3819}
3820
James Smart9bad7672008-12-04 22:39:02 -05003821/**
James Smart3621a712009-04-06 18:47:14 -04003822 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05003823 * @sdev: Pointer to scsi_device.
3824 *
3825 * This routine configures following items
3826 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05003827 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3828 *
3829 * Return codes:
3830 * 0 - Success
3831 **/
dea31012005-04-17 16:05:31 -05003832static int
3833lpfc_slave_configure(struct scsi_device *sdev)
3834{
James Smart2e0fef82007-06-17 19:56:36 -05003835 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3836 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003837
3838 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04003839 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003840 else
James Smart3de2a652007-08-02 11:09:59 -04003841 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003842
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003843 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003844 lpfc_sli_handle_fast_ring_event(phba,
3845 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003846 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3847 lpfc_poll_rearm_timer(phba);
3848 }
3849
dea31012005-04-17 16:05:31 -05003850 return 0;
3851}
3852
James Smart9bad7672008-12-04 22:39:02 -05003853/**
James Smart3621a712009-04-06 18:47:14 -04003854 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05003855 * @sdev: Pointer to scsi_device.
3856 *
3857 * This routine sets @sdev hostatdata filed to null.
3858 **/
dea31012005-04-17 16:05:31 -05003859static void
3860lpfc_slave_destroy(struct scsi_device *sdev)
3861{
James Smartd7c47992010-06-08 18:31:54 -04003862 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3863 struct lpfc_hba *phba = vport->phba;
3864 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05003865 sdev->hostdata = NULL;
3866 return;
3867}
3868
James Smart92d7f7b2007-06-17 19:56:38 -05003869
dea31012005-04-17 16:05:31 -05003870struct scsi_host_template lpfc_template = {
3871 .module = THIS_MODULE,
3872 .name = LPFC_DRIVER_NAME,
3873 .info = lpfc_info,
3874 .queuecommand = lpfc_queuecommand,
3875 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003876 .eh_device_reset_handler = lpfc_device_reset_handler,
3877 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04003878 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05003879 .slave_alloc = lpfc_slave_alloc,
3880 .slave_configure = lpfc_slave_configure,
3881 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04003882 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05003883 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003884 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05003885 .cmd_per_lun = LPFC_CMD_PER_LUN,
3886 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05003887 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04003888 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04003889 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05003890 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05003891};
James Smart3de2a652007-08-02 11:09:59 -04003892
3893struct scsi_host_template lpfc_vport_template = {
3894 .module = THIS_MODULE,
3895 .name = LPFC_DRIVER_NAME,
3896 .info = lpfc_info,
3897 .queuecommand = lpfc_queuecommand,
3898 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003899 .eh_device_reset_handler = lpfc_device_reset_handler,
3900 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04003901 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3902 .slave_alloc = lpfc_slave_alloc,
3903 .slave_configure = lpfc_slave_configure,
3904 .slave_destroy = lpfc_slave_destroy,
3905 .scan_finished = lpfc_scan_finished,
3906 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003907 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04003908 .cmd_per_lun = LPFC_CMD_PER_LUN,
3909 .use_clustering = ENABLE_CLUSTERING,
3910 .shost_attrs = lpfc_vport_attrs,
3911 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05003912 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04003913};