blob: 581837b3c71a9369c12f31d3b547f555bfca576d [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
dea31012005-04-17 16:05:31 -050021#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050023#include <linux/interrupt.h>
James Smarta90f5682006-08-17 11:58:04 -040024#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050025#include <asm/unaligned.h>
dea31012005-04-17 16:05:31 -050026
27#include <scsi/scsi.h>
28#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050029#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050030#include <scsi/scsi_host.h>
31#include <scsi/scsi_tcq.h>
32#include <scsi/scsi_transport_fc.h>
33
34#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040035#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050036#include "lpfc_hw.h"
37#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040039#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_disc.h"
41#include "lpfc_scsi.h"
42#include "lpfc.h"
43#include "lpfc_logmsg.h"
44#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050045#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050046
47#define LPFC_RESET_WAIT 2
48#define LPFC_ABORT_WAIT 2
49
James Smarte2a0a9d2008-12-04 22:40:02 -050050int _dump_buf_done;
51
52static char *dif_op_str[] = {
53 "SCSI_PROT_NORMAL",
54 "SCSI_PROT_READ_INSERT",
55 "SCSI_PROT_WRITE_STRIP",
56 "SCSI_PROT_READ_STRIP",
57 "SCSI_PROT_WRITE_INSERT",
58 "SCSI_PROT_READ_PASS",
59 "SCSI_PROT_WRITE_PASS",
James Smarte2a0a9d2008-12-04 22:40:02 -050060};
James Smartda0436e2009-05-22 14:51:39 -040061static void
62lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050063static void
64lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smarte2a0a9d2008-12-04 22:40:02 -050065
66static void
James Smart6a9c52c2009-10-02 15:16:51 -040067lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050068{
69 void *src, *dst;
70 struct scatterlist *sgde = scsi_sglist(cmnd);
71
72 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040073 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
74 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050075 __func__);
76 return;
77 }
78
79
80 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040081 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
82 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050083 return;
84 }
85
86 dst = (void *) _dump_buf_data;
87 while (sgde) {
88 src = sg_virt(sgde);
89 memcpy(dst, src, sgde->length);
90 dst += sgde->length;
91 sgde = sg_next(sgde);
92 }
93}
94
95static void
James Smart6a9c52c2009-10-02 15:16:51 -040096lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050097{
98 void *src, *dst;
99 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
100
101 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400102 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
103 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500104 __func__);
105 return;
106 }
107
108 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400109 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
110 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500111 return;
112 }
113
114 dst = _dump_buf_dif;
115 while (sgde) {
116 src = sg_virt(sgde);
117 memcpy(dst, src, sgde->length);
118 dst += sgde->length;
119 sgde = sg_next(sgde);
120 }
121}
122
James Smartea2151b2008-09-07 11:52:10 -0400123/**
James Smartf1126682009-06-10 17:22:44 -0400124 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
125 * @phba: Pointer to HBA object.
126 * @lpfc_cmd: lpfc scsi command object pointer.
127 *
128 * This function is called from the lpfc_prep_task_mgmt_cmd function to
129 * set the last bit in the response sge entry.
130 **/
131static void
132lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
133 struct lpfc_scsi_buf *lpfc_cmd)
134{
135 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
136 if (sgl) {
137 sgl += 1;
138 sgl->word2 = le32_to_cpu(sgl->word2);
139 bf_set(lpfc_sli4_sge_last, sgl, 1);
140 sgl->word2 = cpu_to_le32(sgl->word2);
141 }
142}
143
144/**
James Smart3621a712009-04-06 18:47:14 -0400145 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400146 * @phba: Pointer to HBA object.
147 * @lpfc_cmd: lpfc scsi command object pointer.
148 *
149 * This function is called when there is a command completion and this
150 * function updates the statistical data for the command completion.
151 **/
152static void
153lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
154{
155 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
156 struct lpfc_nodelist *pnode = rdata->pnode;
157 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
158 unsigned long flags;
159 struct Scsi_Host *shost = cmd->device->host;
160 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
161 unsigned long latency;
162 int i;
163
164 if (cmd->result)
165 return;
166
James Smart9f1e1b52008-12-04 22:39:40 -0500167 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
168
James Smartea2151b2008-09-07 11:52:10 -0400169 spin_lock_irqsave(shost->host_lock, flags);
170 if (!vport->stat_data_enabled ||
171 vport->stat_data_blocked ||
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;
580 /* fill in responce BDE */
581 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 Smartda0436e2009-05-22 14:51:39 -0400612 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
613 * @phba: pointer to lpfc hba data structure.
614 * @axri: pointer to the fcp xri abort wcqe structure.
615 *
616 * This routine is invoked by the worker thread to process a SLI4 fast-path
617 * FCP aborted xri.
618 **/
619void
620lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
621 struct sli4_wcqe_xri_aborted *axri)
622{
623 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
624 struct lpfc_scsi_buf *psb, *next_psb;
625 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500626 struct lpfc_iocbq *iocbq;
627 int i;
James Smart589a52d2010-07-14 15:30:54 -0400628 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400629
James Smart0f65ff62010-02-26 14:14:23 -0500630 spin_lock_irqsave(&phba->hbalock, iflag);
631 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400632 list_for_each_entry_safe(psb, next_psb,
633 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
634 if (psb->cur_iocbq.sli4_xritag == xri) {
635 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500636 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400637 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500638 spin_unlock(
639 &phba->sli4_hba.abts_scsi_buf_list_lock);
640 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400641 lpfc_release_scsi_buf_s4(phba, psb);
642 return;
643 }
644 }
James Smart0f65ff62010-02-26 14:14:23 -0500645 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
646 for (i = 1; i <= phba->sli.last_iotag; i++) {
647 iocbq = phba->sli.iocbq_lookup[i];
648
649 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
650 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
651 continue;
652 if (iocbq->sli4_xritag != xri)
653 continue;
654 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
655 psb->exch_busy = 0;
656 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart589a52d2010-07-14 15:30:54 -0400657 if (pring->txq_cnt)
658 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500659 return;
660
661 }
662 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400663}
664
665/**
666 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
667 * @phba: pointer to lpfc hba data structure.
668 *
669 * This routine walks the list of scsi buffers that have been allocated and
670 * repost them to the HBA by using SGL block post. This is needed after a
671 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
672 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
673 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
674 *
675 * Returns: 0 = success, non-zero failure.
676 **/
677int
678lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
679{
680 struct lpfc_scsi_buf *psb;
681 int index, status, bcnt = 0, rcnt = 0, rc = 0;
682 LIST_HEAD(sblist);
683
684 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
685 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
686 if (psb) {
687 /* Remove from SCSI buffer list */
688 list_del(&psb->list);
689 /* Add it to a local SCSI buffer list */
690 list_add_tail(&psb->list, &sblist);
691 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
692 bcnt = rcnt;
693 rcnt = 0;
694 }
695 } else
696 /* A hole present in the XRI array, need to skip */
697 bcnt = rcnt;
698
699 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
700 /* End of XRI array for SCSI buffer, complete */
701 bcnt = rcnt;
702
703 /* Continue until collect up to a nembed page worth of sgls */
704 if (bcnt == 0)
705 continue;
706 /* Now, post the SCSI buffer list sgls as a block */
707 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
708 /* Reset SCSI buffer count for next round of posting */
709 bcnt = 0;
710 while (!list_empty(&sblist)) {
711 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
712 list);
713 if (status) {
714 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500715 psb->exch_busy = 1;
James Smartda0436e2009-05-22 14:51:39 -0400716 rc++;
James Smart341af102010-01-26 23:07:37 -0500717 } else {
718 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400719 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500720 }
James Smartda0436e2009-05-22 14:51:39 -0400721 /* Put it back into the SCSI buffer list */
722 lpfc_release_scsi_buf_s4(phba, psb);
723 }
724 }
725 return rc;
726}
727
728/**
729 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
730 * @vport: The virtual port for which this call being executed.
731 * @num_to_allocate: The requested number of buffers to allocate.
732 *
733 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
734 * the scsi buffer contains all the necessary information needed to initiate
735 * a SCSI I/O.
736 *
737 * Return codes:
738 * int - number of scsi buffers that were allocated.
739 * 0 = failure, less than num_to_alloc is a partial failure.
740 **/
741static int
742lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
743{
744 struct lpfc_hba *phba = vport->phba;
745 struct lpfc_scsi_buf *psb;
746 struct sli4_sge *sgl;
747 IOCB_t *iocb;
748 dma_addr_t pdma_phys_fcp_cmd;
749 dma_addr_t pdma_phys_fcp_rsp;
750 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
751 uint16_t iotag, last_xritag = NO_XRI;
752 int status = 0, index;
753 int bcnt;
754 int non_sequential_xri = 0;
James Smartda0436e2009-05-22 14:51:39 -0400755 LIST_HEAD(sblist);
756
757 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
758 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
759 if (!psb)
760 break;
761
762 /*
763 * Get memory from the pci pool to map the virt space to pci bus
764 * space for an I/O. The DMA buffer includes space for the
765 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
766 * necessary to support the sg_tablesize.
767 */
768 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
769 GFP_KERNEL, &psb->dma_handle);
770 if (!psb->data) {
771 kfree(psb);
772 break;
773 }
774
775 /* Initialize virtual ptrs to dma_buf region. */
776 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
777
778 /* Allocate iotag for psb->cur_iocbq. */
779 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
780 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -0400781 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
782 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -0400783 kfree(psb);
784 break;
785 }
786
787 psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
788 if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
789 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
790 psb->data, psb->dma_handle);
791 kfree(psb);
792 break;
793 }
794 if (last_xritag != NO_XRI
795 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
796 non_sequential_xri = 1;
797 } else
798 list_add_tail(&psb->list, &sblist);
799 last_xritag = psb->cur_iocbq.sli4_xritag;
800
801 index = phba->sli4_hba.scsi_xri_cnt++;
802 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
803
804 psb->fcp_bpl = psb->data;
805 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
806 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
807 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
808 sizeof(struct fcp_cmnd));
809
810 /* Initialize local short-hand pointers. */
811 sgl = (struct sli4_sge *)psb->fcp_bpl;
812 pdma_phys_bpl = psb->dma_handle;
813 pdma_phys_fcp_cmd =
814 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
815 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
816 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
817
818 /*
819 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
820 * are sg list bdes. Initialize the first two and leave the
821 * rest for queuecommand.
822 */
823 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
824 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smartda0436e2009-05-22 14:51:39 -0400825 bf_set(lpfc_sli4_sge_last, sgl, 0);
826 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500827 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -0400828 sgl++;
829
830 /* Setup the physical region for the FCP RSP */
831 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
832 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400833 bf_set(lpfc_sli4_sge_last, sgl, 1);
834 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -0500835 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -0400836
837 /*
838 * Since the IOCB for the FCP I/O is built into this
839 * lpfc_scsi_buf, initialize it with all known data now.
840 */
841 iocb = &psb->cur_iocbq.iocb;
842 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
843 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
844 /* setting the BLP size to 2 * sizeof BDE may not be correct.
845 * We are setting the bpl to point to out sgl. An sgl's
846 * entries are 16 bytes, a bpl entries are 12 bytes.
847 */
848 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
849 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
850 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
851 iocb->ulpBdeCount = 1;
852 iocb->ulpLe = 1;
853 iocb->ulpClass = CLASS3;
James Smarteee88772010-09-29 11:19:08 -0400854 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -0400855 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
856 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
857 else
858 pdma_phys_bpl1 = 0;
859 psb->dma_phys_bpl = pdma_phys_bpl;
860 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
861 if (non_sequential_xri) {
862 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
863 pdma_phys_bpl1,
864 psb->cur_iocbq.sli4_xritag);
865 if (status) {
866 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500867 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500868 } else {
869 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400870 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500871 }
James Smartda0436e2009-05-22 14:51:39 -0400872 /* Put it back into the SCSI buffer list */
873 lpfc_release_scsi_buf_s4(phba, psb);
874 break;
875 }
876 }
877 if (bcnt) {
878 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
879 /* Reset SCSI buffer count for next round of posting */
880 while (!list_empty(&sblist)) {
881 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
882 list);
883 if (status) {
884 /* Put this back on the abort scsi list */
James Smart341af102010-01-26 23:07:37 -0500885 psb->exch_busy = 1;
James Smart341af102010-01-26 23:07:37 -0500886 } else {
887 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400888 psb->status = IOSTAT_SUCCESS;
James Smart341af102010-01-26 23:07:37 -0500889 }
James Smartda0436e2009-05-22 14:51:39 -0400890 /* Put it back into the SCSI buffer list */
891 lpfc_release_scsi_buf_s4(phba, psb);
892 }
893 }
894
James Smartd7c47992010-06-08 18:31:54 -0400895 return bcnt + non_sequential_xri;
James Smartda0436e2009-05-22 14:51:39 -0400896}
897
898/**
James Smart3772a992009-05-22 14:50:54 -0400899 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
900 * @vport: The virtual port for which this call being executed.
901 * @num_to_allocate: The requested number of buffers to allocate.
902 *
903 * This routine wraps the actual SCSI buffer allocator function pointer from
904 * the lpfc_hba struct.
905 *
906 * Return codes:
907 * int - number of scsi buffers that were allocated.
908 * 0 = failure, less than num_to_alloc is a partial failure.
909 **/
910static inline int
911lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
912{
913 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
914}
915
916/**
917 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
918 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -0500919 *
920 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
921 * and returns to caller.
922 *
923 * Return codes:
924 * NULL - Error
925 * Pointer to lpfc_scsi_buf - Success
926 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +0100927static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500928lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500929{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400930 struct lpfc_scsi_buf * lpfc_cmd = NULL;
931 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500932 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500933
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500934 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400935 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400936 if (lpfc_cmd) {
937 lpfc_cmd->seg_cnt = 0;
938 lpfc_cmd->nonsg_phys = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -0500939 lpfc_cmd->prot_seg_cnt = 0;
James Smart1dcb58e2007-04-25 09:51:30 -0400940 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500941 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400942 return lpfc_cmd;
943}
944
James Smart9bad7672008-12-04 22:39:02 -0500945/**
James Smart3772a992009-05-22 14:50:54 -0400946 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -0500947 * @phba: The Hba for which this call is being executed.
948 * @psb: The scsi buffer which is being released.
949 *
950 * This routine releases @psb scsi buffer by adding it to tail of @phba
951 * lpfc_scsi_buf_list list.
952 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400953static void
James Smart3772a992009-05-22 14:50:54 -0400954lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400955{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500956 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500957
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500958 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400959 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500960 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500961 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500962}
963
James Smart9bad7672008-12-04 22:39:02 -0500964/**
James Smartda0436e2009-05-22 14:51:39 -0400965 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
966 * @phba: The Hba for which this call is being executed.
967 * @psb: The scsi buffer which is being released.
968 *
969 * This routine releases @psb scsi buffer by adding it to tail of @phba
970 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
971 * and cannot be reused for at least RA_TOV amount of time if it was
972 * aborted.
973 **/
974static void
975lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
976{
977 unsigned long iflag = 0;
978
James Smart341af102010-01-26 23:07:37 -0500979 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -0400980 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
981 iflag);
982 psb->pCmd = NULL;
983 list_add_tail(&psb->list,
984 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
985 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
986 iflag);
987 } else {
988
989 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
990 psb->pCmd = NULL;
991 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
992 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
993 }
994}
995
996/**
James Smart3772a992009-05-22 14:50:54 -0400997 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
998 * @phba: The Hba for which this call is being executed.
999 * @psb: The scsi buffer which is being released.
1000 *
1001 * This routine releases @psb scsi buffer by adding it to tail of @phba
1002 * lpfc_scsi_buf_list list.
1003 **/
1004static void
1005lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1006{
1007
1008 phba->lpfc_release_scsi_buf(phba, psb);
1009}
1010
1011/**
1012 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001013 * @phba: The Hba for which this call is being executed.
1014 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1015 *
1016 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001017 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1018 * through sg elements and format the bdea. This routine also initializes all
1019 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001020 *
1021 * Return codes:
1022 * 1 - Error
1023 * 0 - Success
1024 **/
dea31012005-04-17 16:05:31 -05001025static int
James Smart3772a992009-05-22 14:50:54 -04001026lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001027{
1028 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1029 struct scatterlist *sgel = NULL;
1030 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1031 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001032 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001033 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001034 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001035 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001036 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001037 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001038
1039 /*
1040 * There are three possibilities here - use scatter-gather segment, use
1041 * the single mapping, or neither. Start the lpfc command prep by
1042 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1043 * data bde entry.
1044 */
1045 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001046 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001047 /*
1048 * The driver stores the segment count returned from pci_map_sg
1049 * because this a count of dma-mappings used to map the use_sg
1050 * pages. They are not guaranteed to be the same for those
1051 * architectures that implement an IOMMU.
1052 */
dea31012005-04-17 16:05:31 -05001053
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001054 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1055 scsi_sg_count(scsi_cmnd), datadir);
1056 if (unlikely(!nseg))
1057 return 1;
1058
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001059 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001060 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001061 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1062 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001063 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001064 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001065 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001066 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001067 return 1;
1068 }
1069
1070 /*
1071 * The driver established a maximum scatter-gather segment count
1072 * during probe that limits the number of sg elements in any
1073 * single scsi command. Just run through the seg_cnt and format
1074 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001075 * When using SLI-3 the driver will try to fit all the BDEs into
1076 * the IOCB. If it can't then the BDEs get added to a BPL as it
1077 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001078 */
James Smart34b02dc2008-08-24 21:49:55 -04001079 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001080 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001081 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001082 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001083 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001084 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1085 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1086 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1087 data_bde->addrLow = putPaddrLow(physaddr);
1088 data_bde->addrHigh = putPaddrHigh(physaddr);
1089 data_bde++;
1090 } else {
1091 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1092 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1093 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1094 bpl->addrLow =
1095 le32_to_cpu(putPaddrLow(physaddr));
1096 bpl->addrHigh =
1097 le32_to_cpu(putPaddrHigh(physaddr));
1098 bpl++;
1099 }
dea31012005-04-17 16:05:31 -05001100 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001101 }
dea31012005-04-17 16:05:31 -05001102
1103 /*
1104 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001105 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1106 * explicitly reinitialized and for SLI-3 the extended bde count is
1107 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001108 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001109 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001110 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1111 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001112 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1113 /*
1114 * The extended IOCB format can only fit 3 BDE or a BPL.
1115 * This I/O has more than 3 BDE so the 1st data bde will
1116 * be a BPL that is filled in here.
1117 */
1118 physaddr = lpfc_cmd->dma_handle;
1119 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1120 data_bde->tus.f.bdeSize = (num_bde *
1121 sizeof(struct ulp_bde64));
1122 physaddr += (sizeof(struct fcp_cmnd) +
1123 sizeof(struct fcp_rsp) +
1124 (2 * sizeof(struct ulp_bde64)));
1125 data_bde->addrHigh = putPaddrHigh(physaddr);
1126 data_bde->addrLow = putPaddrLow(physaddr);
1127 /* ebde count includes the responce bde and data bpl */
1128 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1129 } else {
1130 /* ebde count includes the responce bde and data bdes */
1131 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1132 }
1133 } else {
1134 iocb_cmd->un.fcpi64.bdl.bdeSize =
1135 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001136 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001137 }
James Smart09372822008-01-11 01:52:54 -05001138 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001139
1140 /*
1141 * Due to difference in data length between DIF/non-DIF paths,
1142 * we need to set word 4 of IOCB here
1143 */
James Smarta257bf92009-04-06 18:48:10 -04001144 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001145 return 0;
1146}
1147
James Smarte2a0a9d2008-12-04 22:40:02 -05001148/*
James Smart6c8eea52010-04-06 14:49:53 -04001149 * Given a scsi cmnd, determine the BlockGuard opcodes to be used with it
1150 * @sc: The SCSI command to examine
1151 * @txopt: (out) BlockGuard operation for transmitted data
1152 * @rxopt: (out) BlockGuard operation for received data
1153 *
1154 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1155 *
James Smarte2a0a9d2008-12-04 22:40:02 -05001156 */
1157static int
James Smart6c8eea52010-04-06 14:49:53 -04001158lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1159 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001160{
1161 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
James Smart6c8eea52010-04-06 14:49:53 -04001162 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001163
1164 if (guard_type == SHOST_DIX_GUARD_IP) {
1165 switch (scsi_get_prot_op(sc)) {
1166 case SCSI_PROT_READ_INSERT:
1167 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001168 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1169 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001170 break;
1171
1172 case SCSI_PROT_READ_STRIP:
1173 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001174 *txop = BG_OP_IN_NODIF_OUT_CRC;
1175 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001176 break;
1177
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001178 case SCSI_PROT_READ_PASS:
1179 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001180 *txop = BG_OP_IN_CSUM_OUT_CRC;
1181 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smarte2a0a9d2008-12-04 22:40:02 -05001182 break;
1183
James Smarte2a0a9d2008-12-04 22:40:02 -05001184 case SCSI_PROT_NORMAL:
1185 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001186 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart6c8eea52010-04-06 14:49:53 -04001187 "9063 BLKGRD: Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001188 scsi_get_prot_op(sc), guard_type);
James Smart6c8eea52010-04-06 14:49:53 -04001189 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001190 break;
1191
1192 }
1193 } else if (guard_type == SHOST_DIX_GUARD_CRC) {
1194 switch (scsi_get_prot_op(sc)) {
1195 case SCSI_PROT_READ_STRIP:
1196 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001197 *txop = BG_OP_IN_NODIF_OUT_CRC;
1198 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001199 break;
1200
1201 case SCSI_PROT_READ_PASS:
1202 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001203 *txop = BG_OP_IN_CRC_OUT_CRC;
1204 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001205 break;
1206
James Smarte2a0a9d2008-12-04 22:40:02 -05001207 case SCSI_PROT_READ_INSERT:
1208 case SCSI_PROT_WRITE_STRIP:
1209 case SCSI_PROT_NORMAL:
1210 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001211 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1212 "9075 BLKGRD: Bad op/guard:%d/%d combination\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001213 scsi_get_prot_op(sc), guard_type);
James Smart6c8eea52010-04-06 14:49:53 -04001214 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001215 break;
1216 }
1217 } else {
1218 /* unsupported format */
1219 BUG();
1220 }
1221
James Smart6c8eea52010-04-06 14:49:53 -04001222 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001223}
1224
1225struct scsi_dif_tuple {
1226 __be16 guard_tag; /* Checksum */
1227 __be16 app_tag; /* Opaque storage */
1228 __be32 ref_tag; /* Target LBA or indirect LBA */
1229};
1230
1231static inline unsigned
1232lpfc_cmd_blksize(struct scsi_cmnd *sc)
1233{
1234 return sc->device->sector_size;
1235}
1236
1237/**
1238 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1239 * @sc: in: SCSI command
James Smart3621a712009-04-06 18:47:14 -04001240 * @apptagmask: out: app tag mask
1241 * @apptagval: out: app tag value
1242 * @reftag: out: ref tag (reference tag)
James Smarte2a0a9d2008-12-04 22:40:02 -05001243 *
1244 * Description:
Martin Olsson98a17082009-04-22 18:21:29 +02001245 * Extract DIF parameters from the command if possible. Otherwise,
1246 * use default parameters.
James Smarte2a0a9d2008-12-04 22:40:02 -05001247 *
1248 **/
1249static inline void
1250lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1251 uint16_t *apptagval, uint32_t *reftag)
1252{
1253 struct scsi_dif_tuple *spt;
1254 unsigned char op = scsi_get_prot_op(sc);
1255 unsigned int protcnt = scsi_prot_sg_count(sc);
1256 static int cnt;
1257
1258 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001259 op == SCSI_PROT_WRITE_PASS)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001260
1261 cnt++;
1262 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1263 scsi_prot_sglist(sc)[0].offset;
1264 *apptagmask = 0;
1265 *apptagval = 0;
1266 *reftag = cpu_to_be32(spt->ref_tag);
1267
1268 } else {
1269 /* SBC defines ref tag to be lower 32bits of LBA */
1270 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1271 *apptagmask = 0;
1272 *apptagval = 0;
1273 }
1274}
1275
1276/*
1277 * This function sets up buffer list for protection groups of
1278 * type LPFC_PG_TYPE_NO_DIF
1279 *
1280 * This is usually used when the HBA is instructed to generate
1281 * DIFs and insert them into data stream (or strip DIF from
1282 * incoming data stream)
1283 *
1284 * The buffer list consists of just one protection group described
1285 * below:
1286 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001287 * start of prot group --> | PDE_5 |
1288 * +-------------------------+
1289 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001290 * +-------------------------+
1291 * | Data BDE |
1292 * +-------------------------+
1293 * |more Data BDE's ... (opt)|
1294 * +-------------------------+
1295 *
1296 * @sc: pointer to scsi command we're working on
1297 * @bpl: pointer to buffer list for protection groups
1298 * @datacnt: number of segments of data that have been dma mapped
1299 *
1300 * Note: Data s/g buffers have been dma mapped
1301 */
1302static int
1303lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1304 struct ulp_bde64 *bpl, int datasegcnt)
1305{
1306 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04001307 struct lpfc_pde5 *pde5 = NULL;
1308 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001309 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04001310 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001311 int datadir = sc->sc_data_direction;
James Smarte2a0a9d2008-12-04 22:40:02 -05001312 unsigned blksize;
1313 uint32_t reftag;
1314 uint16_t apptagmask, apptagval;
James Smart6c8eea52010-04-06 14:49:53 -04001315 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001316
James Smart6c8eea52010-04-06 14:49:53 -04001317 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1318 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001319 goto out;
1320
James Smart6c8eea52010-04-06 14:49:53 -04001321 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05001322 blksize = lpfc_cmd_blksize(sc);
1323 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1324
James Smart6c8eea52010-04-06 14:49:53 -04001325 /* setup PDE5 with what we have */
1326 pde5 = (struct lpfc_pde5 *) bpl;
1327 memset(pde5, 0, sizeof(struct lpfc_pde5));
1328 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1329 pde5->reftag = reftag;
James Smarte2a0a9d2008-12-04 22:40:02 -05001330
James Smartbc739052010-08-04 16:11:18 -04001331 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001332 pde5->word0 = cpu_to_le32(pde5->word0);
1333 pde5->reftag = cpu_to_le32(pde5->reftag);
1334
James Smart6c8eea52010-04-06 14:49:53 -04001335 /* advance bpl and increment bde count */
1336 num_bde++;
1337 bpl++;
1338 pde6 = (struct lpfc_pde6 *) bpl;
1339
1340 /* setup PDE6 with the rest of the info */
1341 memset(pde6, 0, sizeof(struct lpfc_pde6));
1342 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1343 bf_set(pde6_optx, pde6, txop);
1344 bf_set(pde6_oprx, pde6, rxop);
1345 if (datadir == DMA_FROM_DEVICE) {
1346 bf_set(pde6_ce, pde6, 1);
1347 bf_set(pde6_re, pde6, 1);
1348 bf_set(pde6_ae, pde6, 1);
1349 }
1350 bf_set(pde6_ai, pde6, 1);
1351 bf_set(pde6_apptagval, pde6, apptagval);
1352
James Smartbc739052010-08-04 16:11:18 -04001353 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001354 pde6->word0 = cpu_to_le32(pde6->word0);
1355 pde6->word1 = cpu_to_le32(pde6->word1);
1356 pde6->word2 = cpu_to_le32(pde6->word2);
1357
James Smart6c8eea52010-04-06 14:49:53 -04001358 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001359 num_bde++;
1360 bpl++;
1361
1362 /* assumption: caller has already run dma_map_sg on command data */
1363 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1364 physaddr = sg_dma_address(sgde);
1365 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1366 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1367 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1368 if (datadir == DMA_TO_DEVICE)
1369 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1370 else
1371 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1372 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1373 bpl++;
1374 num_bde++;
1375 }
1376
1377out:
1378 return num_bde;
1379}
1380
1381/*
1382 * This function sets up buffer list for protection groups of
1383 * type LPFC_PG_TYPE_DIF_BUF
1384 *
1385 * This is usually used when DIFs are in their own buffers,
1386 * separate from the data. The HBA can then by instructed
1387 * to place the DIFs in the outgoing stream. For read operations,
1388 * The HBA could extract the DIFs and place it in DIF buffers.
1389 *
1390 * The buffer list for this type consists of one or more of the
1391 * protection groups described below:
1392 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001393 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001394 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001395 * | PDE_6 |
1396 * +-------------------------+
1397 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05001398 * +-------------------------+
1399 * | Data BDE |
1400 * +-------------------------+
1401 * |more Data BDE's ... (opt)|
1402 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04001403 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05001404 * +-------------------------+
1405 * | ... |
1406 * +-------------------------+
1407 *
1408 * @sc: pointer to scsi command we're working on
1409 * @bpl: pointer to buffer list for protection groups
1410 * @datacnt: number of segments of data that have been dma mapped
1411 * @protcnt: number of segment of protection data that have been dma mapped
1412 *
1413 * Note: It is assumed that both data and protection s/g buffers have been
1414 * mapped for DMA
1415 */
1416static int
1417lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1418 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1419{
1420 struct scatterlist *sgde = NULL; /* s/g data entry */
1421 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04001422 struct lpfc_pde5 *pde5 = NULL;
1423 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05001424 struct ulp_bde64 *prot_bde = NULL;
1425 dma_addr_t dataphysaddr, protphysaddr;
1426 unsigned short curr_data = 0, curr_prot = 0;
1427 unsigned int split_offset, protgroup_len;
1428 unsigned int protgrp_blks, protgrp_bytes;
1429 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04001430 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05001431 int datadir = sc->sc_data_direction;
1432 unsigned char pgdone = 0, alldone = 0;
1433 unsigned blksize;
1434 uint32_t reftag;
1435 uint16_t apptagmask, apptagval;
James Smart6c8eea52010-04-06 14:49:53 -04001436 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05001437 int num_bde = 0;
1438
1439 sgpe = scsi_prot_sglist(sc);
1440 sgde = scsi_sglist(sc);
1441
1442 if (!sgpe || !sgde) {
1443 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1444 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1445 sgpe, sgde);
1446 return 0;
1447 }
1448
James Smart6c8eea52010-04-06 14:49:53 -04001449 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1450 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05001451 goto out;
1452
James Smart6c8eea52010-04-06 14:49:53 -04001453 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05001454 blksize = lpfc_cmd_blksize(sc);
1455 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1456
1457 split_offset = 0;
1458 do {
James Smart6c8eea52010-04-06 14:49:53 -04001459 /* setup PDE5 with what we have */
1460 pde5 = (struct lpfc_pde5 *) bpl;
1461 memset(pde5, 0, sizeof(struct lpfc_pde5));
1462 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1463 pde5->reftag = reftag;
James Smarte2a0a9d2008-12-04 22:40:02 -05001464
James Smartbc739052010-08-04 16:11:18 -04001465 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04001466 pde5->word0 = cpu_to_le32(pde5->word0);
1467 pde5->reftag = cpu_to_le32(pde5->reftag);
1468
James Smart6c8eea52010-04-06 14:49:53 -04001469 /* advance bpl and increment bde count */
1470 num_bde++;
1471 bpl++;
1472 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05001473
James Smart6c8eea52010-04-06 14:49:53 -04001474 /* setup PDE6 with the rest of the info */
1475 memset(pde6, 0, sizeof(struct lpfc_pde6));
1476 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1477 bf_set(pde6_optx, pde6, txop);
1478 bf_set(pde6_oprx, pde6, rxop);
1479 bf_set(pde6_ce, pde6, 1);
1480 bf_set(pde6_re, pde6, 1);
1481 bf_set(pde6_ae, pde6, 1);
1482 bf_set(pde6_ai, pde6, 1);
1483 bf_set(pde6_apptagval, pde6, apptagval);
1484
James Smartbc739052010-08-04 16:11:18 -04001485 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04001486 pde6->word0 = cpu_to_le32(pde6->word0);
1487 pde6->word1 = cpu_to_le32(pde6->word1);
1488 pde6->word2 = cpu_to_le32(pde6->word2);
1489
James Smart6c8eea52010-04-06 14:49:53 -04001490 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05001491 num_bde++;
1492 bpl++;
1493
1494 /* setup the first BDE that points to protection buffer */
1495 prot_bde = (struct ulp_bde64 *) bpl;
1496 protphysaddr = sg_dma_address(sgpe);
James Smart6c8eea52010-04-06 14:49:53 -04001497 prot_bde->addrHigh = le32_to_cpu(putPaddrLow(protphysaddr));
1498 prot_bde->addrLow = le32_to_cpu(putPaddrHigh(protphysaddr));
James Smarte2a0a9d2008-12-04 22:40:02 -05001499 protgroup_len = sg_dma_len(sgpe);
1500
James Smarte2a0a9d2008-12-04 22:40:02 -05001501 /* must be integer multiple of the DIF block length */
1502 BUG_ON(protgroup_len % 8);
1503
1504 protgrp_blks = protgroup_len / 8;
1505 protgrp_bytes = protgrp_blks * blksize;
1506
1507 prot_bde->tus.f.bdeSize = protgroup_len;
James Smart6c8eea52010-04-06 14:49:53 -04001508 prot_bde->tus.f.bdeFlags = LPFC_PDE7_DESCRIPTOR;
James Smarte2a0a9d2008-12-04 22:40:02 -05001509 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1510
1511 curr_prot++;
1512 num_bde++;
1513
1514 /* setup BDE's for data blocks associated with DIF data */
1515 pgdone = 0;
1516 subtotal = 0; /* total bytes processed for current prot grp */
1517 while (!pgdone) {
1518 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04001519 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1520 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001521 __func__);
1522 return 0;
1523 }
1524 bpl++;
1525 dataphysaddr = sg_dma_address(sgde) + split_offset;
1526 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1527 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1528
1529 remainder = sg_dma_len(sgde) - split_offset;
1530
1531 if ((subtotal + remainder) <= protgrp_bytes) {
1532 /* we can use this whole buffer */
1533 bpl->tus.f.bdeSize = remainder;
1534 split_offset = 0;
1535
1536 if ((subtotal + remainder) == protgrp_bytes)
1537 pgdone = 1;
1538 } else {
1539 /* must split this buffer with next prot grp */
1540 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1541 split_offset += bpl->tus.f.bdeSize;
1542 }
1543
1544 subtotal += bpl->tus.f.bdeSize;
1545
1546 if (datadir == DMA_TO_DEVICE)
1547 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1548 else
1549 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1550 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1551
1552 num_bde++;
1553 curr_data++;
1554
1555 if (split_offset)
1556 break;
1557
1558 /* Move to the next s/g segment if possible */
1559 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04001560
James Smarte2a0a9d2008-12-04 22:40:02 -05001561 }
1562
1563 /* are we done ? */
1564 if (curr_prot == protcnt) {
1565 alldone = 1;
1566 } else if (curr_prot < protcnt) {
1567 /* advance to next prot buffer */
1568 sgpe = sg_next(sgpe);
1569 bpl++;
1570
1571 /* update the reference tag */
1572 reftag += protgrp_blks;
1573 } else {
1574 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04001575 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1576 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05001577 }
1578
1579 } while (!alldone);
1580
1581out:
1582
James Smarte2a0a9d2008-12-04 22:40:02 -05001583 return num_bde;
1584}
1585/*
1586 * Given a SCSI command that supports DIF, determine composition of protection
1587 * groups involved in setting up buffer lists
1588 *
1589 * Returns:
1590 * for DIF (for both read and write)
1591 * */
1592static int
1593lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1594{
1595 int ret = LPFC_PG_TYPE_INVALID;
1596 unsigned char op = scsi_get_prot_op(sc);
1597
1598 switch (op) {
1599 case SCSI_PROT_READ_STRIP:
1600 case SCSI_PROT_WRITE_INSERT:
1601 ret = LPFC_PG_TYPE_NO_DIF;
1602 break;
1603 case SCSI_PROT_READ_INSERT:
1604 case SCSI_PROT_WRITE_STRIP:
1605 case SCSI_PROT_READ_PASS:
1606 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05001607 ret = LPFC_PG_TYPE_DIF_BUF;
1608 break;
1609 default:
1610 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1611 "9021 Unsupported protection op:%d\n", op);
1612 break;
1613 }
1614
1615 return ret;
1616}
1617
1618/*
1619 * This is the protection/DIF aware version of
1620 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1621 * two functions eventually, but for now, it's here
1622 */
1623static int
1624lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1625 struct lpfc_scsi_buf *lpfc_cmd)
1626{
1627 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1628 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1629 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1630 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1631 uint32_t num_bde = 0;
1632 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1633 int prot_group_type = 0;
1634 int diflen, fcpdl;
1635 unsigned blksize;
1636
1637 /*
1638 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1639 * fcp_rsp regions to the first data bde entry
1640 */
1641 bpl += 2;
1642 if (scsi_sg_count(scsi_cmnd)) {
1643 /*
1644 * The driver stores the segment count returned from pci_map_sg
1645 * because this a count of dma-mappings used to map the use_sg
1646 * pages. They are not guaranteed to be the same for those
1647 * architectures that implement an IOMMU.
1648 */
1649 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1650 scsi_sglist(scsi_cmnd),
1651 scsi_sg_count(scsi_cmnd), datadir);
1652 if (unlikely(!datasegcnt))
1653 return 1;
1654
1655 lpfc_cmd->seg_cnt = datasegcnt;
1656 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001657 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1658 "9067 BLKGRD: %s: Too many sg segments"
1659 " from dma_map_sg. Config %d, seg_cnt"
1660 " %d\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001661 __func__, phba->cfg_sg_seg_cnt,
1662 lpfc_cmd->seg_cnt);
1663 scsi_dma_unmap(scsi_cmnd);
1664 return 1;
1665 }
1666
1667 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1668
1669 switch (prot_group_type) {
1670 case LPFC_PG_TYPE_NO_DIF:
1671 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1672 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001673 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001674 if (num_bde < 2)
1675 goto err;
1676 break;
1677 case LPFC_PG_TYPE_DIF_BUF:{
1678 /*
1679 * This type indicates that protection buffers are
1680 * passed to the driver, so that needs to be prepared
1681 * for DMA
1682 */
1683 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1684 scsi_prot_sglist(scsi_cmnd),
1685 scsi_prot_sg_count(scsi_cmnd), datadir);
1686 if (unlikely(!protsegcnt)) {
1687 scsi_dma_unmap(scsi_cmnd);
1688 return 1;
1689 }
1690
1691 lpfc_cmd->prot_seg_cnt = protsegcnt;
1692 if (lpfc_cmd->prot_seg_cnt
1693 > phba->cfg_prot_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001694 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1695 "9068 BLKGRD: %s: Too many prot sg "
1696 "segments from dma_map_sg. Config %d,"
James Smarte2a0a9d2008-12-04 22:40:02 -05001697 "prot_seg_cnt %d\n", __func__,
1698 phba->cfg_prot_sg_seg_cnt,
1699 lpfc_cmd->prot_seg_cnt);
1700 dma_unmap_sg(&phba->pcidev->dev,
1701 scsi_prot_sglist(scsi_cmnd),
1702 scsi_prot_sg_count(scsi_cmnd),
1703 datadir);
1704 scsi_dma_unmap(scsi_cmnd);
1705 return 1;
1706 }
1707
1708 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1709 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05001710 /* we should have 3 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05001711 if (num_bde < 3)
1712 goto err;
1713 break;
1714 }
1715 case LPFC_PG_TYPE_INVALID:
1716 default:
1717 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1718 "9022 Unexpected protection group %i\n",
1719 prot_group_type);
1720 return 1;
1721 }
1722 }
1723
1724 /*
1725 * Finish initializing those IOCB fields that are dependent on the
1726 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1727 * reinitialized since all iocb memory resources are used many times
1728 * for transmit, receive, and continuation bpl's.
1729 */
1730 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1731 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1732 iocb_cmd->ulpBdeCount = 1;
1733 iocb_cmd->ulpLe = 1;
1734
1735 fcpdl = scsi_bufflen(scsi_cmnd);
1736
1737 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1738 /*
1739 * We are in DIF Type 1 mode
1740 * Every data block has a 8 byte DIF (trailer)
1741 * attached to it. Must ajust FCP data length
1742 */
1743 blksize = lpfc_cmd_blksize(scsi_cmnd);
1744 diflen = (fcpdl / blksize) * 8;
1745 fcpdl += diflen;
1746 }
1747 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1748
1749 /*
1750 * Due to difference in data length between DIF/non-DIF paths,
1751 * we need to set word 4 of IOCB here
1752 */
1753 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1754
1755 return 0;
1756err:
1757 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1758 "9023 Could not setup all needed BDE's"
1759 "prot_group_type=%d, num_bde=%d\n",
1760 prot_group_type, num_bde);
1761 return 1;
1762}
1763
1764/*
1765 * This function checks for BlockGuard errors detected by
1766 * the HBA. In case of errors, the ASC/ASCQ fields in the
1767 * sense buffer will be set accordingly, paired with
1768 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1769 * detected corruption.
1770 *
1771 * Returns:
1772 * 0 - No error found
1773 * 1 - BlockGuard error found
1774 * -1 - Internal error (bad profile, ...etc)
1775 */
1776static int
1777lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1778 struct lpfc_iocbq *pIocbOut)
1779{
1780 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1781 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1782 int ret = 0;
1783 uint32_t bghm = bgf->bghm;
1784 uint32_t bgstat = bgf->bgstat;
1785 uint64_t failing_sector = 0;
1786
James Smart6a9c52c2009-10-02 15:16:51 -04001787 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
1788 " 0x%x lba 0x%llx blk cnt 0x%x "
James Smarte2a0a9d2008-12-04 22:40:02 -05001789 "bgstat=0x%x bghm=0x%x\n",
James Smart87b5c322008-12-16 10:34:09 -05001790 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
Tejun Heo83096eb2009-05-07 22:24:39 +09001791 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05001792
1793 spin_lock(&_dump_buf_lock);
1794 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04001795 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
1796 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001797 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04001798 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001799
1800 /* If we have a prot sgl, save the DIF buffer */
1801 if (lpfc_prot_group_type(phba, cmd) ==
1802 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04001803 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
1804 "Saving DIF for %u blocks to debugfs\n",
1805 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1806 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05001807 }
1808
1809 _dump_buf_done = 1;
1810 }
1811 spin_unlock(&_dump_buf_lock);
1812
1813 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1814 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001815 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
1816 " BlockGuard profile. bgstat:0x%x\n",
1817 bgstat);
James Smarte2a0a9d2008-12-04 22:40:02 -05001818 ret = (-1);
1819 goto out;
1820 }
1821
1822 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1823 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001824 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
1825 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05001826 bgstat);
1827 ret = (-1);
1828 goto out;
1829 }
1830
1831 if (lpfc_bgs_get_guard_err(bgstat)) {
1832 ret = 1;
1833
1834 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1835 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001836 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001837 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1838 phba->bg_guard_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001839 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1840 "9055 BLKGRD: guard_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001841 }
1842
1843 if (lpfc_bgs_get_reftag_err(bgstat)) {
1844 ret = 1;
1845
1846 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1847 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001848 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001849 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1850
1851 phba->bg_reftag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001852 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1853 "9056 BLKGRD: ref_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001854 }
1855
1856 if (lpfc_bgs_get_apptag_err(bgstat)) {
1857 ret = 1;
1858
1859 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1860 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05001861 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05001862 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1863
1864 phba->bg_apptag_err_cnt++;
James Smart6a9c52c2009-10-02 15:16:51 -04001865 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1866 "9061 BLKGRD: app_tag error\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001867 }
1868
1869 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1870 /*
1871 * setup sense data descriptor 0 per SPC-4 as an information
1872 * field, and put the failing LBA in it
1873 */
1874 cmd->sense_buffer[8] = 0; /* Information */
1875 cmd->sense_buffer[9] = 0xa; /* Add. length */
David Howells2344b5b2009-04-14 17:08:34 +01001876 bghm /= cmd->device->sector_size;
James Smarte2a0a9d2008-12-04 22:40:02 -05001877
1878 failing_sector = scsi_get_lba(cmd);
1879 failing_sector += bghm;
1880
1881 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1882 }
1883
1884 if (!ret) {
1885 /* No error was reported - problem in FW? */
1886 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart6a9c52c2009-10-02 15:16:51 -04001887 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1888 "9057 BLKGRD: no errors reported!\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05001889 }
1890
1891out:
1892 return ret;
1893}
1894
James Smartea2151b2008-09-07 11:52:10 -04001895/**
James Smartda0436e2009-05-22 14:51:39 -04001896 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1897 * @phba: The Hba for which this call is being executed.
1898 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1899 *
1900 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1901 * field of @lpfc_cmd for device with SLI-4 interface spec.
1902 *
1903 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04001904 * 1 - Error
1905 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04001906 **/
1907static int
1908lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1909{
1910 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1911 struct scatterlist *sgel = NULL;
1912 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1913 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
1914 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1915 dma_addr_t physaddr;
1916 uint32_t num_bde = 0;
1917 uint32_t dma_len;
1918 uint32_t dma_offset = 0;
1919 int nseg;
1920
1921 /*
1922 * There are three possibilities here - use scatter-gather segment, use
1923 * the single mapping, or neither. Start the lpfc command prep by
1924 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1925 * data bde entry.
1926 */
1927 if (scsi_sg_count(scsi_cmnd)) {
1928 /*
1929 * The driver stores the segment count returned from pci_map_sg
1930 * because this a count of dma-mappings used to map the use_sg
1931 * pages. They are not guaranteed to be the same for those
1932 * architectures that implement an IOMMU.
1933 */
1934
1935 nseg = scsi_dma_map(scsi_cmnd);
1936 if (unlikely(!nseg))
1937 return 1;
1938 sgl += 1;
1939 /* clear the last flag in the fcp_rsp map entry */
1940 sgl->word2 = le32_to_cpu(sgl->word2);
1941 bf_set(lpfc_sli4_sge_last, sgl, 0);
1942 sgl->word2 = cpu_to_le32(sgl->word2);
1943 sgl += 1;
1944
1945 lpfc_cmd->seg_cnt = nseg;
1946 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001947 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
1948 " %s: Too many sg segments from "
1949 "dma_map_sg. Config %d, seg_cnt %d\n",
1950 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04001951 lpfc_cmd->seg_cnt);
1952 scsi_dma_unmap(scsi_cmnd);
1953 return 1;
1954 }
1955
1956 /*
1957 * The driver established a maximum scatter-gather segment count
1958 * during probe that limits the number of sg elements in any
1959 * single scsi command. Just run through the seg_cnt and format
1960 * the sge's.
1961 * When using SLI-3 the driver will try to fit all the BDEs into
1962 * the IOCB. If it can't then the BDEs get added to a BPL as it
1963 * does for SLI-2 mode.
1964 */
1965 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1966 physaddr = sg_dma_address(sgel);
1967 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04001968 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1969 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1970 if ((num_bde + 1) == nseg)
1971 bf_set(lpfc_sli4_sge_last, sgl, 1);
1972 else
1973 bf_set(lpfc_sli4_sge_last, sgl, 0);
1974 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1975 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001976 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04001977 dma_offset += dma_len;
1978 sgl++;
1979 }
1980 } else {
1981 sgl += 1;
1982 /* clear the last flag in the fcp_rsp map entry */
1983 sgl->word2 = le32_to_cpu(sgl->word2);
1984 bf_set(lpfc_sli4_sge_last, sgl, 1);
1985 sgl->word2 = cpu_to_le32(sgl->word2);
1986 }
1987
1988 /*
1989 * Finish initializing those IOCB fields that are dependent on the
1990 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1991 * explicitly reinitialized.
1992 * all iocb memory resources are reused.
1993 */
1994 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1995
1996 /*
1997 * Due to difference in data length between DIF/non-DIF paths,
1998 * we need to set word 4 of IOCB here
1999 */
2000 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
2001 return 0;
2002}
2003
2004/**
James Smart3772a992009-05-22 14:50:54 -04002005 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
2006 * @phba: The Hba for which this call is being executed.
2007 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2008 *
2009 * This routine wraps the actual DMA mapping function pointer from the
2010 * lpfc_hba struct.
2011 *
2012 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04002013 * 1 - Error
2014 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04002015 **/
2016static inline int
2017lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2018{
2019 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2020}
2021
2022/**
James Smart3621a712009-04-06 18:47:14 -04002023 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04002024 * @phba: Pointer to hba context object.
2025 * @vport: Pointer to vport object.
2026 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
2027 * @rsp_iocb: Pointer to response iocb object which reported error.
2028 *
2029 * This function posts an event when there is a SCSI command reporting
2030 * error from the scsi device.
2031 **/
2032static void
2033lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
2034 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
2035 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2036 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2037 uint32_t resp_info = fcprsp->rspStatus2;
2038 uint32_t scsi_status = fcprsp->rspStatus3;
2039 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2040 struct lpfc_fast_path_event *fast_path_evt = NULL;
2041 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
2042 unsigned long flags;
2043
James Smart5989b8d2010-10-22 11:06:56 -04002044 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2045 return;
2046
James Smartea2151b2008-09-07 11:52:10 -04002047 /* If there is queuefull or busy condition send a scsi event */
2048 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
2049 (cmnd->result == SAM_STAT_BUSY)) {
2050 fast_path_evt = lpfc_alloc_fast_evt(phba);
2051 if (!fast_path_evt)
2052 return;
2053 fast_path_evt->un.scsi_evt.event_type =
2054 FC_REG_SCSI_EVENT;
2055 fast_path_evt->un.scsi_evt.subcategory =
2056 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
2057 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
2058 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
2059 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
2060 &pnode->nlp_portname, sizeof(struct lpfc_name));
2061 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
2062 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2063 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
2064 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
2065 fast_path_evt = lpfc_alloc_fast_evt(phba);
2066 if (!fast_path_evt)
2067 return;
2068 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
2069 FC_REG_SCSI_EVENT;
2070 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
2071 LPFC_EVENT_CHECK_COND;
2072 fast_path_evt->un.check_cond_evt.scsi_event.lun =
2073 cmnd->device->lun;
2074 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
2075 &pnode->nlp_portname, sizeof(struct lpfc_name));
2076 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
2077 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2078 fast_path_evt->un.check_cond_evt.sense_key =
2079 cmnd->sense_buffer[2] & 0xf;
2080 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
2081 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
2082 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2083 fcpi_parm &&
2084 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
2085 ((scsi_status == SAM_STAT_GOOD) &&
2086 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
2087 /*
2088 * If status is good or resid does not match with fcp_param and
2089 * there is valid fcpi_parm, then there is a read_check error
2090 */
2091 fast_path_evt = lpfc_alloc_fast_evt(phba);
2092 if (!fast_path_evt)
2093 return;
2094 fast_path_evt->un.read_check_error.header.event_type =
2095 FC_REG_FABRIC_EVENT;
2096 fast_path_evt->un.read_check_error.header.subcategory =
2097 LPFC_EVENT_FCPRDCHKERR;
2098 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
2099 &pnode->nlp_portname, sizeof(struct lpfc_name));
2100 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2101 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2102 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2103 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2104 fast_path_evt->un.read_check_error.fcpiparam =
2105 fcpi_parm;
2106 } else
2107 return;
2108
2109 fast_path_evt->vport = vport;
2110 spin_lock_irqsave(&phba->hbalock, flags);
2111 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2112 spin_unlock_irqrestore(&phba->hbalock, flags);
2113 lpfc_worker_wake_up(phba);
2114 return;
2115}
James Smart9bad7672008-12-04 22:39:02 -05002116
2117/**
James Smartf1126682009-06-10 17:22:44 -04002118 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04002119 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05002120 * @psb: The scsi buffer which is going to be un-mapped.
2121 *
2122 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04002123 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002124 **/
dea31012005-04-17 16:05:31 -05002125static void
James Smartf1126682009-06-10 17:22:44 -04002126lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04002127{
2128 /*
2129 * There are only two special cases to consider. (1) the scsi command
2130 * requested scatter-gather usage or (2) the scsi command allocated
2131 * a request buffer, but did not request use_sg. There is a third
2132 * case, but it does not require resource deallocation.
2133 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002134 if (psb->seg_cnt > 0)
2135 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002136 if (psb->prot_seg_cnt > 0)
2137 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2138 scsi_prot_sg_count(psb->pCmd),
2139 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04002140}
2141
James Smart9bad7672008-12-04 22:39:02 -05002142/**
James Smart3621a712009-04-06 18:47:14 -04002143 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05002144 * @vport: The virtual port for which this call is being executed.
2145 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2146 * @rsp_iocb: The response IOCB which contains FCP error.
2147 *
2148 * This routine is called to process response IOCB with status field
2149 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2150 * based upon SCSI and FCP error.
2151 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04002152static void
James Smart2e0fef82007-06-17 19:56:36 -05002153lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2154 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05002155{
2156 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2157 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2158 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04002159 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05002160 uint32_t resp_info = fcprsp->rspStatus2;
2161 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05002162 uint32_t *lp;
dea31012005-04-17 16:05:31 -05002163 uint32_t host_status = DID_OK;
2164 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05002165 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05002166
James Smartea2151b2008-09-07 11:52:10 -04002167
dea31012005-04-17 16:05:31 -05002168 /*
2169 * If this is a task management command, there is no
2170 * scsi packet associated with this lpfc_cmd. The driver
2171 * consumes it.
2172 */
2173 if (fcpcmd->fcpCntl2) {
2174 scsi_status = 0;
2175 goto out;
2176 }
2177
James Smart6a9c52c2009-10-02 15:16:51 -04002178 if (resp_info & RSP_LEN_VALID) {
2179 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05002180 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04002181 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2182 "2719 Invalid response length: "
2183 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2184 cmnd->device->id,
2185 cmnd->device->lun, cmnd->cmnd[0],
2186 rsplen);
2187 host_status = DID_ERROR;
2188 goto out;
2189 }
James Smarte40a02c2010-02-26 14:13:54 -05002190 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
2191 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2192 "2757 Protocol failure detected during "
2193 "processing of FCP I/O op: "
2194 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
2195 cmnd->device->id,
2196 cmnd->device->lun, cmnd->cmnd[0],
2197 fcprsp->rspInfo3);
2198 host_status = DID_ERROR;
2199 goto out;
2200 }
James Smart6a9c52c2009-10-02 15:16:51 -04002201 }
2202
James Smartc7743952006-12-02 13:34:42 -05002203 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2204 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2205 if (snslen > SCSI_SENSE_BUFFERSIZE)
2206 snslen = SCSI_SENSE_BUFFERSIZE;
2207
2208 if (resp_info & RSP_LEN_VALID)
2209 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2210 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2211 }
2212 lp = (uint32_t *)cmnd->sense_buffer;
2213
2214 if (!scsi_status && (resp_info & RESID_UNDER))
2215 logit = LOG_FCP;
2216
James Smarte8b62012007-08-02 11:10:09 -04002217 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05002218 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04002219 "Data: x%x x%x x%x x%x x%x\n",
2220 cmnd->cmnd[0], scsi_status,
2221 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2222 be32_to_cpu(fcprsp->rspResId),
2223 be32_to_cpu(fcprsp->rspSnsLen),
2224 be32_to_cpu(fcprsp->rspRspLen),
2225 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05002226
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002227 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05002228 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002229 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05002230
James Smarte8b62012007-08-02 11:10:09 -04002231 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002232 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04002233 "residual %d Data: x%x x%x x%x\n",
2234 be32_to_cpu(fcpcmd->fcpDl),
2235 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2236 cmnd->underflow);
dea31012005-04-17 16:05:31 -05002237
2238 /*
James Smart7054a602007-04-25 09:52:34 -04002239 * If there is an under run check if under run reported by
2240 * storage array is same as the under run reported by HBA.
2241 * If this is not same, there is a dropped frame.
2242 */
2243 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2244 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002245 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04002246 lpfc_printf_vlog(vport, KERN_WARNING,
2247 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002248 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04002249 "and Underrun Data: x%x x%x x%x x%x\n",
2250 be32_to_cpu(fcpcmd->fcpDl),
2251 scsi_get_resid(cmnd), fcpi_parm,
2252 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002253 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04002254 host_status = DID_ERROR;
2255 }
2256 /*
dea31012005-04-17 16:05:31 -05002257 * The cmnd->underflow is the minimum number of bytes that must
2258 * be transfered for this command. Provided a sense condition
2259 * is not present, make sure the actual amount transferred is at
2260 * least the underflow value or fail.
2261 */
2262 if (!(resp_info & SNS_LEN_VALID) &&
2263 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002264 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2265 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04002266 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002267 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04002268 "underrun converted to error "
2269 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04002270 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04002271 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05002272 host_status = DID_ERROR;
2273 }
2274 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04002275 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002276 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04002277 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04002278 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05002279 host_status = DID_ERROR;
2280
2281 /*
2282 * Check SLI validation that all the transfer was actually done
2283 * (fcpi_parm should be zero). Apply check only to reads.
2284 */
James Smarteee88772010-09-29 11:19:08 -04002285 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04002286 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05002287 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04002288 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002289 be32_to_cpu(fcpcmd->fcpDl),
2290 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04002291 fcpi_parm, cmnd->cmnd[0], scsi_status);
2292 switch (scsi_status) {
2293 case SAM_STAT_GOOD:
2294 case SAM_STAT_CHECK_CONDITION:
2295 /* Fabric dropped a data frame. Fail any successful
2296 * command in which we detected dropped frames.
2297 * A status of good or some check conditions could
2298 * be considered a successful command.
2299 */
2300 host_status = DID_ERROR;
2301 break;
2302 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002303 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05002304 }
2305
2306 out:
2307 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04002308 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05002309}
2310
James Smart9bad7672008-12-04 22:39:02 -05002311/**
James Smart3621a712009-04-06 18:47:14 -04002312 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05002313 * @phba: The Hba for which this call is being executed.
2314 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04002315 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05002316 *
2317 * This routine assigns scsi command result by looking into response IOCB
2318 * status field appropriately. This routine handles QUEUE FULL condition as
2319 * well by ramping down device queue depth.
2320 **/
dea31012005-04-17 16:05:31 -05002321static void
2322lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2323 struct lpfc_iocbq *pIocbOut)
2324{
2325 struct lpfc_scsi_buf *lpfc_cmd =
2326 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002327 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05002328 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2329 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04002330 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002331 int result;
James Smarta257bf92009-04-06 18:48:10 -04002332 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05002333 int depth;
James Smartfa61a542008-01-11 01:52:42 -05002334 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04002335 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04002336 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04002337 uint32_t queue_depth, scsi_id;
dea31012005-04-17 16:05:31 -05002338
James Smart75baf692010-06-08 18:31:21 -04002339 /* Sanity check on return of outstanding command */
2340 if (!(lpfc_cmd->pCmd))
2341 return;
2342 cmd = lpfc_cmd->pCmd;
2343 shost = cmd->device->host;
2344
dea31012005-04-17 16:05:31 -05002345 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2346 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05002347 /* pick up SLI4 exhange busy status from HBA */
2348 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
2349
James Smart109f6ed2008-12-04 22:39:08 -05002350 if (pnode && NLP_CHK_NODE_ACT(pnode))
2351 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05002352
2353 if (lpfc_cmd->status) {
2354 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2355 (lpfc_cmd->result & IOERR_DRVR_MASK))
2356 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2357 else if (lpfc_cmd->status >= IOSTAT_CNT)
2358 lpfc_cmd->status = IOSTAT_DEFAULT;
2359
James Smarte8b62012007-08-02 11:10:09 -04002360 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05002361 "9030 FCP cmd x%x failed <%d/%d> "
James Smarte8b62012007-08-02 11:10:09 -04002362 "status: x%x result: x%x Data: x%x x%x\n",
2363 cmd->cmnd[0],
2364 cmd->device ? cmd->device->id : 0xffff,
2365 cmd->device ? cmd->device->lun : 0xffff,
2366 lpfc_cmd->status, lpfc_cmd->result,
2367 pIocbOut->iocb.ulpContext,
2368 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05002369
2370 switch (lpfc_cmd->status) {
2371 case IOSTAT_FCP_RSP_ERROR:
2372 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05002373 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05002374 break;
2375 case IOSTAT_NPORT_BSY:
2376 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04002377 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04002378 fast_path_evt = lpfc_alloc_fast_evt(phba);
2379 if (!fast_path_evt)
2380 break;
2381 fast_path_evt->un.fabric_evt.event_type =
2382 FC_REG_FABRIC_EVENT;
2383 fast_path_evt->un.fabric_evt.subcategory =
2384 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2385 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2386 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2387 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2388 &pnode->nlp_portname,
2389 sizeof(struct lpfc_name));
2390 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2391 &pnode->nlp_nodename,
2392 sizeof(struct lpfc_name));
2393 }
2394 fast_path_evt->vport = vport;
2395 fast_path_evt->work_evt.evt =
2396 LPFC_EVT_FASTPATH_MGMT_EVT;
2397 spin_lock_irqsave(&phba->hbalock, flags);
2398 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2399 &phba->work_list);
2400 spin_unlock_irqrestore(&phba->hbalock, flags);
2401 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05002402 break;
James Smart92d7f7b2007-06-17 19:56:38 -05002403 case IOSTAT_LOCAL_REJECT:
James Smartd7c255b2008-08-24 21:50:00 -04002404 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05002405 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04002406 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
2407 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05002408 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04002409 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05002410 }
2411
2412 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2413 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2414 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2415 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2416 /*
2417 * This is a response for a BG enabled
2418 * cmd. Parse BG error
2419 */
2420 lpfc_parse_bg_err(phba, lpfc_cmd,
2421 pIocbOut);
2422 break;
2423 } else {
2424 lpfc_printf_vlog(vport, KERN_WARNING,
2425 LOG_BG,
2426 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04002427 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05002428 }
2429 }
2430
2431 /* else: fall through */
dea31012005-04-17 16:05:31 -05002432 default:
2433 cmd->result = ScsiResult(DID_ERROR, 0);
2434 break;
2435 }
2436
James Smart58da1ff2008-04-07 10:15:56 -04002437 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002438 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04002439 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2440 SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -05002441 } else {
2442 cmd->result = ScsiResult(DID_OK, 0);
2443 }
2444
2445 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2446 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2447
James Smarte8b62012007-08-02 11:10:09 -04002448 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2449 "0710 Iodone <%d/%d> cmd %p, error "
2450 "x%x SNS x%x x%x Data: x%x x%x\n",
2451 cmd->device->id, cmd->device->lun, cmd,
2452 cmd->result, *lp, *(lp + 3), cmd->retries,
2453 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05002454 }
2455
James Smartea2151b2008-09-07 11:52:10 -04002456 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002457 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04002458 if (vport->cfg_max_scsicmpl_time &&
2459 time_after(jiffies, lpfc_cmd->start_time +
2460 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04002461 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002462 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2463 if (pnode->cmd_qdepth >
2464 atomic_read(&pnode->cmd_pending) &&
2465 (atomic_read(&pnode->cmd_pending) >
2466 LPFC_MIN_TGT_QDEPTH) &&
2467 ((cmd->cmnd[0] == READ_10) ||
2468 (cmd->cmnd[0] == WRITE_10)))
2469 pnode->cmd_qdepth =
2470 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04002471
James Smart109f6ed2008-12-04 22:39:08 -05002472 pnode->last_change_time = jiffies;
2473 }
James Smarta257bf92009-04-06 18:48:10 -04002474 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002475 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04002476 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04002477 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05002478 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04002479 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04002480 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
2481 / 100;
2482 depth = depth ? depth : 1;
2483 pnode->cmd_qdepth += depth;
2484 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
2485 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05002486 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04002487 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05002488 }
James Smart977b5a02008-09-07 11:52:04 -04002489 }
2490
James Smart1dcb58e2007-04-25 09:51:30 -04002491 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04002492
2493 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2494 queue_depth = cmd->device->queue_depth;
2495 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05002496 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002497
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002498 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -05002499 /*
2500 * If there is a thread waiting for command completion
2501 * wake up the thread.
2502 */
James Smarta257bf92009-04-06 18:48:10 -04002503 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002504 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002505 if (lpfc_cmd->waitq)
2506 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002507 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002508 lpfc_release_scsi_buf(phba, lpfc_cmd);
2509 return;
2510 }
2511
James Smart92d7f7b2007-06-17 19:56:38 -05002512 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04002513 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05002514
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002515 /*
2516 * Check for queue full. If the lun is reporting queue full, then
2517 * back off the lun queue depth to prevent target overloads.
2518 */
James Smart58da1ff2008-04-07 10:15:56 -04002519 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2520 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04002521 shost_for_each_device(tmp_sdev, shost) {
2522 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002523 continue;
2524 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05002525 tmp_sdev->queue_depth-1);
2526 if (depth <= 0)
2527 continue;
James Smarte8b62012007-08-02 11:10:09 -04002528 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2529 "0711 detected queue full - lun queue "
2530 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04002531 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05002532 pnode,
2533 tmp_sdev->lun,
2534 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05002535 }
2536 }
2537
James Smartfa61a542008-01-11 01:52:42 -05002538 /*
2539 * If there is a thread waiting for command completion
2540 * wake up the thread.
2541 */
James Smarta257bf92009-04-06 18:48:10 -04002542 spin_lock_irqsave(shost->host_lock, flags);
James Smart495a7142008-06-14 22:52:59 -04002543 lpfc_cmd->pCmd = NULL;
James Smartfa61a542008-01-11 01:52:42 -05002544 if (lpfc_cmd->waitq)
2545 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04002546 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05002547
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002548 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002549}
2550
James Smart34b02dc2008-08-24 21:49:55 -04002551/**
James Smart3621a712009-04-06 18:47:14 -04002552 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04002553 * @data: A pointer to the immediate command data portion of the IOCB.
2554 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2555 *
2556 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2557 * byte swapping the data to big endian format for transmission on the wire.
2558 **/
2559static void
2560lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2561{
2562 int i, j;
2563 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2564 i += sizeof(uint32_t), j++) {
2565 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2566 }
2567}
2568
James Smart9bad7672008-12-04 22:39:02 -05002569/**
James Smartf1126682009-06-10 17:22:44 -04002570 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002571 * @vport: The virtual port for which this call is being executed.
2572 * @lpfc_cmd: The scsi command which needs to send.
2573 * @pnode: Pointer to lpfc_nodelist.
2574 *
2575 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04002576 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002577 **/
dea31012005-04-17 16:05:31 -05002578static void
James Smartf1126682009-06-10 17:22:44 -04002579lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05002580 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05002581{
James Smart2e0fef82007-06-17 19:56:36 -05002582 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002583 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2584 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2585 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2586 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2587 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04002588 char tag[2];
dea31012005-04-17 16:05:31 -05002589
James Smart58da1ff2008-04-07 10:15:56 -04002590 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2591 return;
2592
dea31012005-04-17 16:05:31 -05002593 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04002594 /* clear task management bits */
2595 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05002596
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04002597 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2598 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002599
2600 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2601
James Smart7e2b19f2007-10-29 11:00:39 -04002602 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2603 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05002604 case HEAD_OF_QUEUE_TAG:
2605 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2606 break;
2607 case ORDERED_QUEUE_TAG:
2608 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2609 break;
2610 default:
2611 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2612 break;
2613 }
2614 } else
2615 fcp_cmnd->fcpCntl1 = 0;
2616
2617 /*
2618 * There are three possibilities here - use scatter-gather segment, use
2619 * the single mapping, or neither. Start the lpfc command prep by
2620 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2621 * data bde entry.
2622 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05002623 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05002624 if (datadir == DMA_TO_DEVICE) {
2625 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart3772a992009-05-22 14:50:54 -04002626 if (phba->sli_rev < LPFC_SLI_REV4) {
2627 iocb_cmd->un.fcpi.fcpi_parm = 0;
2628 iocb_cmd->ulpPU = 0;
2629 } else
2630 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002631 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2632 phba->fc4OutputRequests++;
2633 } else {
2634 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2635 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05002636 fcp_cmnd->fcpCntl3 = READ_DATA;
2637 phba->fc4InputRequests++;
2638 }
2639 } else {
2640 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2641 iocb_cmd->un.fcpi.fcpi_parm = 0;
2642 iocb_cmd->ulpPU = 0;
2643 fcp_cmnd->fcpCntl3 = 0;
2644 phba->fc4ControlRequests++;
2645 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002646 if (phba->sli_rev == 3 &&
2647 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002648 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002649 /*
2650 * Finish initializing those IOCB fields that are independent
2651 * of the scsi_cmnd request_buffer
2652 */
2653 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2654 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2655 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05002656 else
2657 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05002658
2659 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2660 piocbq->context1 = lpfc_cmd;
2661 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2662 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05002663 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05002664}
2665
James Smart9bad7672008-12-04 22:39:02 -05002666/**
James Smartf1126682009-06-10 17:22:44 -04002667 * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05002668 * @vport: The virtual port for which this call is being executed.
2669 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2670 * @lun: Logical unit number.
2671 * @task_mgmt_cmd: SCSI task management command.
2672 *
James Smart3772a992009-05-22 14:50:54 -04002673 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2674 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05002675 *
2676 * Return codes:
2677 * 0 - Error
2678 * 1 - Success
2679 **/
dea31012005-04-17 16:05:31 -05002680static int
James Smartf1126682009-06-10 17:22:44 -04002681lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05002682 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04002683 unsigned int lun,
dea31012005-04-17 16:05:31 -05002684 uint8_t task_mgmt_cmd)
2685{
dea31012005-04-17 16:05:31 -05002686 struct lpfc_iocbq *piocbq;
2687 IOCB_t *piocb;
2688 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04002689 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05002690 struct lpfc_nodelist *ndlp = rdata->pnode;
2691
James Smart58da1ff2008-04-07 10:15:56 -04002692 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2693 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05002694 return 0;
dea31012005-04-17 16:05:31 -05002695
dea31012005-04-17 16:05:31 -05002696 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002697 piocbq->vport = vport;
2698
dea31012005-04-17 16:05:31 -05002699 piocb = &piocbq->iocb;
2700
2701 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04002702 /* Clear out any old data in the FCP command area */
2703 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2704 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05002705 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05002706 if (vport->phba->sli_rev == 3 &&
2707 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04002708 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05002709 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05002710 piocb->ulpContext = ndlp->nlp_rpi;
2711 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2712 piocb->ulpFCP2Rcvy = 1;
2713 }
2714 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2715
2716 /* ulpTimeout is only one byte */
2717 if (lpfc_cmd->timeout > 0xff) {
2718 /*
2719 * Do not timeout the command at the firmware level.
2720 * The driver will provide the timeout mechanism.
2721 */
2722 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04002723 } else
dea31012005-04-17 16:05:31 -05002724 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04002725
2726 if (vport->phba->sli_rev == LPFC_SLI_REV4)
2727 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05002728
James Smart2e0fef82007-06-17 19:56:36 -05002729 return 1;
dea31012005-04-17 16:05:31 -05002730}
2731
James Smart9bad7672008-12-04 22:39:02 -05002732/**
James Smart3772a992009-05-22 14:50:54 -04002733 * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2734 * @phba: The hba struct for which this call is being executed.
2735 * @dev_grp: The HBA PCI-Device group number.
2736 *
2737 * This routine sets up the SCSI interface API function jump table in @phba
2738 * struct.
2739 * Returns: 0 - success, -ENODEV - failure.
2740 **/
2741int
2742lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2743{
2744
James Smartf1126682009-06-10 17:22:44 -04002745 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2746 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2747 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2748
James Smart3772a992009-05-22 14:50:54 -04002749 switch (dev_grp) {
2750 case LPFC_PCI_DEV_LP:
2751 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2752 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04002753 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2754 break;
James Smartda0436e2009-05-22 14:51:39 -04002755 case LPFC_PCI_DEV_OC:
2756 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2757 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04002758 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2759 break;
James Smart3772a992009-05-22 14:50:54 -04002760 default:
2761 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2762 "1418 Invalid HBA PCI-device group: 0x%x\n",
2763 dev_grp);
2764 return -ENODEV;
2765 break;
2766 }
2767 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2768 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05002769 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04002770 return 0;
2771}
2772
2773/**
James Smart3621a712009-04-06 18:47:14 -04002774 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05002775 * @phba: The Hba for which this call is being executed.
2776 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2777 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2778 *
2779 * This routine is IOCB completion routine for device reset and target reset
2780 * routine. This routine release scsi buffer associated with lpfc_cmd.
2781 **/
James Smart7054a602007-04-25 09:52:34 -04002782static void
2783lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2784 struct lpfc_iocbq *cmdiocbq,
2785 struct lpfc_iocbq *rspiocbq)
2786{
2787 struct lpfc_scsi_buf *lpfc_cmd =
2788 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2789 if (lpfc_cmd)
2790 lpfc_release_scsi_buf(phba, lpfc_cmd);
2791 return;
2792}
2793
James Smart9bad7672008-12-04 22:39:02 -05002794/**
James Smart3621a712009-04-06 18:47:14 -04002795 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05002796 * @host: The scsi host for which this call is being executed.
2797 *
2798 * This routine provides module information about hba.
2799 *
2800 * Reutrn code:
2801 * Pointer to char - Success.
2802 **/
dea31012005-04-17 16:05:31 -05002803const char *
2804lpfc_info(struct Scsi_Host *host)
2805{
James Smart2e0fef82007-06-17 19:56:36 -05002806 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2807 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002808 int len;
2809 static char lpfcinfobuf[384];
2810
2811 memset(lpfcinfobuf,0,384);
2812 if (phba && phba->pcidev){
2813 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2814 len = strlen(lpfcinfobuf);
2815 snprintf(lpfcinfobuf + len,
2816 384-len,
2817 " on PCI bus %02x device %02x irq %d",
2818 phba->pcidev->bus->number,
2819 phba->pcidev->devfn,
2820 phba->pcidev->irq);
2821 len = strlen(lpfcinfobuf);
2822 if (phba->Port[0]) {
2823 snprintf(lpfcinfobuf + len,
2824 384-len,
2825 " port %s",
2826 phba->Port);
2827 }
James Smart65467b62010-01-26 23:08:29 -05002828 len = strlen(lpfcinfobuf);
2829 if (phba->sli4_hba.link_state.logical_speed) {
2830 snprintf(lpfcinfobuf + len,
2831 384-len,
2832 " Logical Link Speed: %d Mbps",
2833 phba->sli4_hba.link_state.logical_speed * 10);
2834 }
dea31012005-04-17 16:05:31 -05002835 }
2836 return lpfcinfobuf;
2837}
2838
James Smart9bad7672008-12-04 22:39:02 -05002839/**
James Smart3621a712009-04-06 18:47:14 -04002840 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05002841 * @phba: The Hba for which this call is being executed.
2842 *
2843 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2844 * The default value of cfg_poll_tmo is 10 milliseconds.
2845 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002846static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2847{
2848 unsigned long poll_tmo_expires =
2849 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2850
2851 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2852 mod_timer(&phba->fcp_poll_timer,
2853 poll_tmo_expires);
2854}
2855
James Smart9bad7672008-12-04 22:39:02 -05002856/**
James Smart3621a712009-04-06 18:47:14 -04002857 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05002858 * @phba: The Hba for which this call is being executed.
2859 *
2860 * This routine starts the fcp_poll_timer of @phba.
2861 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002862void lpfc_poll_start_timer(struct lpfc_hba * phba)
2863{
2864 lpfc_poll_rearm_timer(phba);
2865}
2866
James Smart9bad7672008-12-04 22:39:02 -05002867/**
James Smart3621a712009-04-06 18:47:14 -04002868 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05002869 * @ptr: Map to lpfc_hba data structure pointer.
2870 *
2871 * This routine restarts fcp_poll timer, when FCP ring polling is enable
2872 * and FCP Ring interrupt is disable.
2873 **/
2874
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002875void lpfc_poll_timeout(unsigned long ptr)
2876{
James Smart2e0fef82007-06-17 19:56:36 -05002877 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002878
2879 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04002880 lpfc_sli_handle_fast_ring_event(phba,
2881 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
2882
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002883 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2884 lpfc_poll_rearm_timer(phba);
2885 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002886}
2887
James Smart9bad7672008-12-04 22:39:02 -05002888/**
James Smart3621a712009-04-06 18:47:14 -04002889 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05002890 * @cmnd: Pointer to scsi_cmnd data structure.
2891 * @done: Pointer to done routine.
2892 *
2893 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2894 * This routine prepares an IOCB from scsi command and provides to firmware.
2895 * The @done callback is invoked after driver finished processing the command.
2896 *
2897 * Return value :
2898 * 0 - Success
2899 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2900 **/
dea31012005-04-17 16:05:31 -05002901static int
Jeff Garzikf2812332010-11-16 02:10:29 -05002902lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
dea31012005-04-17 16:05:31 -05002903{
James Smart2e0fef82007-06-17 19:56:36 -05002904 struct Scsi_Host *shost = cmnd->device->host;
2905 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2906 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05002907 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05002908 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04002909 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002910 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002911 int err;
dea31012005-04-17 16:05:31 -05002912
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002913 err = fc_remote_port_chkready(rport);
2914 if (err) {
2915 cmnd->result = err;
dea31012005-04-17 16:05:31 -05002916 goto out_fail_command;
2917 }
James Smart1c6f4ef52009-11-18 15:40:49 -05002918 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05002919
James Smarte2a0a9d2008-12-04 22:40:02 -05002920 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2921 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2922
James Smart6a9c52c2009-10-02 15:16:51 -04002923 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2924 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
2925 " op:%02x str=%s without registering for"
2926 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002927 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2928 dif_op_str[scsi_get_prot_op(cmnd)]);
2929 goto out_fail_command;
2930 }
2931
dea31012005-04-17 16:05:31 -05002932 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04002933 * Catch race where our node has transitioned, but the
2934 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05002935 */
James Smartb522d7d2008-09-07 11:51:56 -04002936 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2937 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2938 goto out_fail_command;
2939 }
James Smart7dc517d2010-07-14 15:32:10 -04002940 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
James Smart977b5a02008-09-07 11:52:04 -04002941 goto out_host_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05002942
James Smarted957682007-06-17 19:56:37 -05002943 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05002944 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05002945 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002946
James Smarte8b62012007-08-02 11:10:09 -04002947 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2948 "0707 driver's buffer pool is empty, "
2949 "IO busied\n");
dea31012005-04-17 16:05:31 -05002950 goto out_host_busy;
2951 }
2952
2953 /*
2954 * Store the midlayer's command structure for the completion phase
2955 * and complete the command initialization.
2956 */
2957 lpfc_cmd->pCmd = cmnd;
2958 lpfc_cmd->rdata = rdata;
2959 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04002960 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05002961 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
2962 cmnd->scsi_done = done;
2963
James Smarte2a0a9d2008-12-04 22:40:02 -05002964 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04002965 if (vport->phba->cfg_enable_bg) {
2966 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002967 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
2968 "str=%s\n",
2969 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2970 dif_op_str[scsi_get_prot_op(cmnd)]);
James Smart6a9c52c2009-10-02 15:16:51 -04002971 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002972 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
James Smarte4e74272009-07-19 10:01:38 -04002973 "%02x %02x %02x %02x %02x\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002974 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2975 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2976 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2977 cmnd->cmnd[9]);
James Smart6a9c52c2009-10-02 15:16:51 -04002978 if (cmnd->cmnd[0] == READ_10)
2979 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002980 "9035 BLKGRD: READ @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002981 "count %u\n",
2982 (unsigned long long)scsi_get_lba(cmnd),
2983 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04002984 else if (cmnd->cmnd[0] == WRITE_10)
2985 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05002986 "9036 BLKGRD: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09002987 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05002988 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09002989 blk_rq_sectors(cmnd->request),
James Smarte2a0a9d2008-12-04 22:40:02 -05002990 cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04002991 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002992
2993 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
2994 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04002995 if (vport->phba->cfg_enable_bg) {
James Smarte2a0a9d2008-12-04 22:40:02 -05002996 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smart6a9c52c2009-10-02 15:16:51 -04002997 "9038 BLKGRD: rcvd unprotected cmd:"
2998 "%02x op:%02x str=%s\n",
2999 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3000 dif_op_str[scsi_get_prot_op(cmnd)]);
3001 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3002 "9039 BLKGRD: CDB: %02x %02x %02x "
3003 "%02x %02x %02x %02x %02x %02x %02x\n",
3004 cmnd->cmnd[0], cmnd->cmnd[1],
3005 cmnd->cmnd[2], cmnd->cmnd[3],
3006 cmnd->cmnd[4], cmnd->cmnd[5],
3007 cmnd->cmnd[6], cmnd->cmnd[7],
3008 cmnd->cmnd[8], cmnd->cmnd[9]);
3009 if (cmnd->cmnd[0] == READ_10)
3010 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3011 "9040 dbg: READ @ sector %llu, "
3012 "count %u\n",
3013 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003014 blk_rq_sectors(cmnd->request));
James Smart6a9c52c2009-10-02 15:16:51 -04003015 else if (cmnd->cmnd[0] == WRITE_10)
3016 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003017 "9041 dbg: WRITE @ sector %llu, "
Tejun Heo83096eb2009-05-07 22:24:39 +09003018 "count %u cmd=%p\n",
James Smart87b5c322008-12-16 10:34:09 -05003019 (unsigned long long)scsi_get_lba(cmnd),
Tejun Heo83096eb2009-05-07 22:24:39 +09003020 blk_rq_sectors(cmnd->request), cmnd);
James Smart6a9c52c2009-10-02 15:16:51 -04003021 else
3022 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
James Smarte2a0a9d2008-12-04 22:40:02 -05003023 "9042 dbg: parser not implemented\n");
James Smart6a9c52c2009-10-02 15:16:51 -04003024 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003025 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3026 }
3027
dea31012005-04-17 16:05:31 -05003028 if (err)
3029 goto out_host_busy_free_buf;
3030
James Smart2e0fef82007-06-17 19:56:36 -05003031 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05003032
James Smart977b5a02008-09-07 11:52:04 -04003033 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04003034 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05003035 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05003036 if (err) {
3037 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05003038 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05003039 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003040 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003041 spin_unlock(shost->host_lock);
3042 lpfc_sli_handle_fast_ring_event(phba,
3043 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3044
3045 spin_lock(shost->host_lock);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003046 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3047 lpfc_poll_rearm_timer(phba);
3048 }
3049
dea31012005-04-17 16:05:31 -05003050 return 0;
3051
3052 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04003053 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003054 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05003055 out_host_busy:
3056 return SCSI_MLQUEUE_HOST_BUSY;
3057
3058 out_fail_command:
3059 done(cmnd);
3060 return 0;
3061}
3062
Jeff Garzikf2812332010-11-16 02:10:29 -05003063static DEF_SCSI_QCMD(lpfc_queuecommand)
3064
James Smart9bad7672008-12-04 22:39:02 -05003065/**
James Smart3621a712009-04-06 18:47:14 -04003066 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003067 * @cmnd: Pointer to scsi_cmnd data structure.
3068 *
3069 * This routine aborts @cmnd pending in base driver.
3070 *
3071 * Return code :
3072 * 0x2003 - Error
3073 * 0x2002 - Success
3074 **/
dea31012005-04-17 16:05:31 -05003075static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05003076lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003077{
James Smart2e0fef82007-06-17 19:56:36 -05003078 struct Scsi_Host *shost = cmnd->device->host;
3079 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3080 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003081 struct lpfc_iocbq *iocb;
3082 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05003083 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05003084 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003085 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05003086 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003087
James Smart589a52d2010-07-14 15:30:54 -04003088 ret = fc_block_scsi_eh(cmnd);
3089 if (ret)
3090 return ret;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003091 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smarteee88772010-09-29 11:19:08 -04003092 if (!lpfc_cmd) {
3093 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3094 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
3095 "x%x ID %d "
3096 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3097 cmnd->device->lun, cmnd->serial_number);
3098 return SUCCESS;
3099 }
dea31012005-04-17 16:05:31 -05003100
3101 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003102 * If pCmd field of the corresponding lpfc_scsi_buf structure
3103 * points to a different SCSI command, then the driver has
3104 * already completed this command, but the midlayer did not
3105 * see the completion before the eh fired. Just return
3106 * SUCCESS.
dea31012005-04-17 16:05:31 -05003107 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003108 iocb = &lpfc_cmd->cur_iocbq;
3109 if (lpfc_cmd->pCmd != cmnd)
3110 goto out;
dea31012005-04-17 16:05:31 -05003111
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003112 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05003113
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003114 abtsiocb = lpfc_sli_get_iocbq(phba);
3115 if (abtsiocb == NULL) {
3116 ret = FAILED;
dea31012005-04-17 16:05:31 -05003117 goto out;
3118 }
3119
dea31012005-04-17 16:05:31 -05003120 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003121 * The scsi command can not be in txq and it is in flight because the
3122 * pCmd is still pointig at the SCSI command we have to abort. There
3123 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05003124 */
dea31012005-04-17 16:05:31 -05003125
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003126 cmd = &iocb->iocb;
3127 icmd = &abtsiocb->iocb;
3128 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3129 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04003130 if (phba->sli_rev == LPFC_SLI_REV4)
3131 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3132 else
3133 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05003134
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003135 icmd->ulpLe = 1;
3136 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05003137
3138 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
3139 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05003140 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05003141
James Smart2e0fef82007-06-17 19:56:36 -05003142 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003143 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3144 else
3145 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05003146
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003147 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05003148 abtsiocb->vport = vport;
James Smart3772a992009-05-22 14:50:54 -04003149 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3150 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003151 lpfc_sli_release_iocbq(phba, abtsiocb);
3152 ret = FAILED;
3153 goto out;
3154 }
3155
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003156 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04003157 lpfc_sli_handle_fast_ring_event(phba,
3158 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003159
James Smartfa61a542008-01-11 01:52:42 -05003160 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003161 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05003162 wait_event_timeout(waitq,
3163 (lpfc_cmd->pCmd != cmnd),
3164 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003165
James Smartfa61a542008-01-11 01:52:42 -05003166 spin_lock_irq(shost->host_lock);
3167 lpfc_cmd->waitq = NULL;
3168 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003169
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003170 if (lpfc_cmd->pCmd == cmnd) {
3171 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04003172 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3173 "0748 abort handler timed out waiting "
3174 "for abort to complete: ret %#x, ID %d, "
3175 "LUN %d, snum %#lx\n",
3176 ret, cmnd->device->id, cmnd->device->lun,
3177 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05003178 }
3179
3180 out:
James Smarte8b62012007-08-02 11:10:09 -04003181 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3182 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3183 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3184 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04003185 return ret;
dea31012005-04-17 16:05:31 -05003186}
3187
James Smartbbb9d182009-06-10 17:23:16 -04003188static char *
3189lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3190{
3191 switch (task_mgmt_cmd) {
3192 case FCP_ABORT_TASK_SET:
3193 return "ABORT_TASK_SET";
3194 case FCP_CLEAR_TASK_SET:
3195 return "FCP_CLEAR_TASK_SET";
3196 case FCP_BUS_RESET:
3197 return "FCP_BUS_RESET";
3198 case FCP_LUN_RESET:
3199 return "FCP_LUN_RESET";
3200 case FCP_TARGET_RESET:
3201 return "FCP_TARGET_RESET";
3202 case FCP_CLEAR_ACA:
3203 return "FCP_CLEAR_ACA";
3204 case FCP_TERMINATE_TASK:
3205 return "FCP_TERMINATE_TASK";
3206 default:
3207 return "unknown";
3208 }
3209}
3210
3211/**
3212 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3213 * @vport: The virtual port for which this call is being executed.
3214 * @rdata: Pointer to remote port local data
3215 * @tgt_id: Target ID of remote device.
3216 * @lun_id: Lun number for the TMF
3217 * @task_mgmt_cmd: type of TMF to send
3218 *
3219 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3220 * a remote port.
3221 *
3222 * Return Code:
3223 * 0x2003 - Error
3224 * 0x2002 - Success.
3225 **/
3226static int
3227lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3228 unsigned tgt_id, unsigned int lun_id,
3229 uint8_t task_mgmt_cmd)
3230{
3231 struct lpfc_hba *phba = vport->phba;
3232 struct lpfc_scsi_buf *lpfc_cmd;
3233 struct lpfc_iocbq *iocbq;
3234 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04003235 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003236 int ret;
3237 int status;
3238
James Smart5989b8d2010-10-22 11:06:56 -04003239 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04003240 return FAILED;
3241
3242 lpfc_cmd = lpfc_get_scsi_buf(phba);
3243 if (lpfc_cmd == NULL)
3244 return FAILED;
3245 lpfc_cmd->timeout = 60;
3246 lpfc_cmd->rdata = rdata;
3247
3248 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3249 task_mgmt_cmd);
3250 if (!status) {
3251 lpfc_release_scsi_buf(phba, lpfc_cmd);
3252 return FAILED;
3253 }
3254
3255 iocbq = &lpfc_cmd->cur_iocbq;
3256 iocbqrsp = lpfc_sli_get_iocbq(phba);
3257 if (iocbqrsp == NULL) {
3258 lpfc_release_scsi_buf(phba, lpfc_cmd);
3259 return FAILED;
3260 }
3261
3262 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3263 "0702 Issue %s to TGT %d LUN %d "
3264 "rpi x%x nlp_flag x%x\n",
3265 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart5989b8d2010-10-22 11:06:56 -04003266 pnode->nlp_rpi, pnode->nlp_flag);
James Smartbbb9d182009-06-10 17:23:16 -04003267
3268 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3269 iocbq, iocbqrsp, lpfc_cmd->timeout);
3270 if (status != IOCB_SUCCESS) {
3271 if (status == IOCB_TIMEDOUT) {
3272 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3273 ret = TIMEOUT_ERROR;
3274 } else
3275 ret = FAILED;
3276 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3277 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3278 "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
3279 lpfc_taskmgmt_name(task_mgmt_cmd),
3280 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
3281 iocbqrsp->iocb.un.ulpWord[4]);
James Smart2a9bf3d2010-06-07 15:24:45 -04003282 } else if (status == IOCB_BUSY)
3283 ret = FAILED;
3284 else
James Smartbbb9d182009-06-10 17:23:16 -04003285 ret = SUCCESS;
3286
3287 lpfc_sli_release_iocbq(phba, iocbqrsp);
3288
3289 if (ret != TIMEOUT_ERROR)
3290 lpfc_release_scsi_buf(phba, lpfc_cmd);
3291
3292 return ret;
3293}
3294
3295/**
3296 * lpfc_chk_tgt_mapped -
3297 * @vport: The virtual port to check on
3298 * @cmnd: Pointer to scsi_cmnd data structure.
3299 *
3300 * This routine delays until the scsi target (aka rport) for the
3301 * command exists (is present and logged in) or we declare it non-existent.
3302 *
3303 * Return code :
3304 * 0x2003 - Error
3305 * 0x2002 - Success
3306 **/
3307static int
3308lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3309{
3310 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003311 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003312 unsigned long later;
3313
James Smart1c6f4ef52009-11-18 15:40:49 -05003314 if (!rdata) {
3315 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3316 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
3317 return FAILED;
3318 }
3319 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003320 /*
3321 * If target is not in a MAPPED state, delay until
3322 * target is rediscovered or devloss timeout expires.
3323 */
3324 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3325 while (time_after(later, jiffies)) {
3326 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3327 return FAILED;
3328 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3329 return SUCCESS;
3330 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3331 rdata = cmnd->device->hostdata;
3332 if (!rdata)
3333 return FAILED;
3334 pnode = rdata->pnode;
3335 }
3336 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3337 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3338 return FAILED;
3339 return SUCCESS;
3340}
3341
3342/**
3343 * lpfc_reset_flush_io_context -
3344 * @vport: The virtual port (scsi_host) for the flush context
3345 * @tgt_id: If aborting by Target contect - specifies the target id
3346 * @lun_id: If aborting by Lun context - specifies the lun id
3347 * @context: specifies the context level to flush at.
3348 *
3349 * After a reset condition via TMF, we need to flush orphaned i/o
3350 * contexts from the adapter. This routine aborts any contexts
3351 * outstanding, then waits for their completions. The wait is
3352 * bounded by devloss_tmo though.
3353 *
3354 * Return code :
3355 * 0x2003 - Error
3356 * 0x2002 - Success
3357 **/
3358static int
3359lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3360 uint64_t lun_id, lpfc_ctx_cmd context)
3361{
3362 struct lpfc_hba *phba = vport->phba;
3363 unsigned long later;
3364 int cnt;
3365
3366 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3367 if (cnt)
3368 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3369 tgt_id, lun_id, context);
3370 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3371 while (time_after(later, jiffies) && cnt) {
3372 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3373 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3374 }
3375 if (cnt) {
3376 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3377 "0724 I/O flush failure for context %s : cnt x%x\n",
3378 ((context == LPFC_CTX_LUN) ? "LUN" :
3379 ((context == LPFC_CTX_TGT) ? "TGT" :
3380 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3381 cnt);
3382 return FAILED;
3383 }
3384 return SUCCESS;
3385}
3386
James Smart9bad7672008-12-04 22:39:02 -05003387/**
James Smart3621a712009-04-06 18:47:14 -04003388 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05003389 * @cmnd: Pointer to scsi_cmnd data structure.
3390 *
James Smartbbb9d182009-06-10 17:23:16 -04003391 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05003392 * command.
3393 *
3394 * Return code :
3395 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04003396 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003397 **/
dea31012005-04-17 16:05:31 -05003398static int
James Smart7054a602007-04-25 09:52:34 -04003399lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003400{
James Smart2e0fef82007-06-17 19:56:36 -05003401 struct Scsi_Host *shost = cmnd->device->host;
3402 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003403 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003404 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003405 unsigned tgt_id = cmnd->device->id;
3406 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04003407 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003408 int status;
dea31012005-04-17 16:05:31 -05003409
James Smart1c6f4ef52009-11-18 15:40:49 -05003410 if (!rdata) {
3411 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3412 "0798 Device Reset rport failure: rdata x%p\n", rdata);
3413 return FAILED;
3414 }
3415 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003416 status = fc_block_scsi_eh(cmnd);
3417 if (status)
3418 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003419
3420 status = lpfc_chk_tgt_mapped(vport, cmnd);
3421 if (status == FAILED) {
3422 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3423 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3424 return FAILED;
3425 }
3426
3427 scsi_event.event_type = FC_REG_SCSI_EVENT;
3428 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3429 scsi_event.lun = lun_id;
3430 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3431 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3432
3433 fc_host_post_vendor_event(shost, fc_get_event_number(),
3434 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3435
3436 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3437 FCP_LUN_RESET);
3438
3439 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3440 "0713 SCSI layer issued Device Reset (%d, %d) "
3441 "return x%x\n", tgt_id, lun_id, status);
3442
dea31012005-04-17 16:05:31 -05003443 /*
James Smartbbb9d182009-06-10 17:23:16 -04003444 * We have to clean up i/o as : they may be orphaned by the TMF;
3445 * or if the TMF failed, they may be in an indeterminate state.
3446 * So, continue on.
3447 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05003448 */
James Smartbbb9d182009-06-10 17:23:16 -04003449 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3450 LPFC_CTX_LUN);
3451 return status;
3452}
3453
3454/**
3455 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3456 * @cmnd: Pointer to scsi_cmnd data structure.
3457 *
3458 * This routine does a target reset by sending a TARGET_RESET task management
3459 * command.
3460 *
3461 * Return code :
3462 * 0x2003 - Error
3463 * 0x2002 - Success
3464 **/
3465static int
3466lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3467{
3468 struct Scsi_Host *shost = cmnd->device->host;
3469 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3470 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05003471 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04003472 unsigned tgt_id = cmnd->device->id;
3473 unsigned int lun_id = cmnd->device->lun;
3474 struct lpfc_scsi_event_header scsi_event;
3475 int status;
3476
James Smart1c6f4ef52009-11-18 15:40:49 -05003477 if (!rdata) {
3478 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3479 "0799 Target Reset rport failure: rdata x%p\n", rdata);
3480 return FAILED;
3481 }
3482 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04003483 status = fc_block_scsi_eh(cmnd);
3484 if (status)
3485 return status;
James Smartbbb9d182009-06-10 17:23:16 -04003486
3487 status = lpfc_chk_tgt_mapped(vport, cmnd);
3488 if (status == FAILED) {
3489 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3490 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3491 return FAILED;
dea31012005-04-17 16:05:31 -05003492 }
James Smartea2151b2008-09-07 11:52:10 -04003493
3494 scsi_event.event_type = FC_REG_SCSI_EVENT;
3495 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3496 scsi_event.lun = 0;
3497 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3498 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3499
James Smartbbb9d182009-06-10 17:23:16 -04003500 fc_host_post_vendor_event(shost, fc_get_event_number(),
3501 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04003502
James Smartbbb9d182009-06-10 17:23:16 -04003503 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3504 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05003505
James Smarte8b62012007-08-02 11:10:09 -04003506 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04003507 "0723 SCSI layer issued Target Reset (%d, %d) "
3508 "return x%x\n", tgt_id, lun_id, status);
3509
3510 /*
3511 * We have to clean up i/o as : they may be orphaned by the TMF;
3512 * or if the TMF failed, they may be in an indeterminate state.
3513 * So, continue on.
3514 * We will report success if all the i/o aborts successfully.
3515 */
3516 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3517 LPFC_CTX_TGT);
3518 return status;
dea31012005-04-17 16:05:31 -05003519}
3520
James Smart9bad7672008-12-04 22:39:02 -05003521/**
James Smart3621a712009-04-06 18:47:14 -04003522 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05003523 * @cmnd: Pointer to scsi_cmnd data structure.
3524 *
James Smartbbb9d182009-06-10 17:23:16 -04003525 * This routine does target reset to all targets on @cmnd->device->host.
3526 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05003527 *
James Smartbbb9d182009-06-10 17:23:16 -04003528 * Return code :
3529 * 0x2003 - Error
3530 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05003531 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04003532static int
James Smart7054a602007-04-25 09:52:34 -04003533lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05003534{
James Smart2e0fef82007-06-17 19:56:36 -05003535 struct Scsi_Host *shost = cmnd->device->host;
3536 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05003537 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04003538 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04003539 int match;
3540 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04003541
3542 scsi_event.event_type = FC_REG_SCSI_EVENT;
3543 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3544 scsi_event.lun = 0;
3545 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3546 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3547
James Smartbbb9d182009-06-10 17:23:16 -04003548 fc_host_post_vendor_event(shost, fc_get_event_number(),
3549 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05003550
James Smart589a52d2010-07-14 15:30:54 -04003551 ret = fc_block_scsi_eh(cmnd);
3552 if (ret)
3553 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04003554
dea31012005-04-17 16:05:31 -05003555 /*
3556 * Since the driver manages a single bus device, reset all
3557 * targets known to the driver. Should any target reset
3558 * fail, this routine returns failure to the midlayer.
3559 */
James Smarte17da182006-07-06 15:49:25 -04003560 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04003561 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05003562 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003563 spin_lock_irq(shost->host_lock);
3564 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05003565 if (!NLP_CHK_NODE_ACT(ndlp))
3566 continue;
James Smart685f0bf2007-04-25 09:53:08 -04003567 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04003568 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04003569 ndlp->rport) {
dea31012005-04-17 16:05:31 -05003570 match = 1;
3571 break;
3572 }
3573 }
James Smart2e0fef82007-06-17 19:56:36 -05003574 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05003575 if (!match)
3576 continue;
James Smartbbb9d182009-06-10 17:23:16 -04003577
3578 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3579 i, 0, FCP_TARGET_RESET);
3580
3581 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04003582 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3583 "0700 Bus Reset on target %d failed\n",
3584 i);
James Smart915caaa2008-06-14 22:52:38 -04003585 ret = FAILED;
dea31012005-04-17 16:05:31 -05003586 }
3587 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003588 /*
James Smartbbb9d182009-06-10 17:23:16 -04003589 * We have to clean up i/o as : they may be orphaned by the TMFs
3590 * above; or if any of the TMFs failed, they may be in an
3591 * indeterminate state.
3592 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003593 */
James Smartbbb9d182009-06-10 17:23:16 -04003594
3595 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3596 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05003597 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04003598
James Smarte8b62012007-08-02 11:10:09 -04003599 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3600 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05003601 return ret;
3602}
3603
James Smart9bad7672008-12-04 22:39:02 -05003604/**
James Smart3621a712009-04-06 18:47:14 -04003605 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05003606 * @sdev: Pointer to scsi_device.
3607 *
3608 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3609 * globally available list of scsi buffers. This routine also makes sure scsi
3610 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3611 * of scsi buffer exists for the lifetime of the driver.
3612 *
3613 * Return codes:
3614 * non-0 - Error
3615 * 0 - Success
3616 **/
dea31012005-04-17 16:05:31 -05003617static int
dea31012005-04-17 16:05:31 -05003618lpfc_slave_alloc(struct scsi_device *sdev)
3619{
James Smart2e0fef82007-06-17 19:56:36 -05003620 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3621 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003622 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04003623 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05003624 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04003625 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04003626 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05003627
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003628 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05003629 return -ENXIO;
3630
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003631 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04003632 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05003633
3634 /*
3635 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3636 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04003637 * HBA limit conveyed to the midlayer via the host structure. The
3638 * formula accounts for the lun_queue_depth + error handlers + 1
3639 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05003640 */
3641 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04003642 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05003643
James Smartd7c47992010-06-08 18:31:54 -04003644 /* If allocated buffers are enough do nothing */
3645 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
3646 return 0;
3647
James Smart92d7f7b2007-06-17 19:56:38 -05003648 /* Allow some exchanges to be available always to complete discovery */
3649 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003650 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3651 "0704 At limitation of %d preallocated "
3652 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05003653 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003654 /* Allow some exchanges to be available always to complete discovery */
3655 } else if (total + num_to_alloc >
3656 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04003657 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3658 "0705 Allocation request of %d "
3659 "command buffers will exceed max of %d. "
3660 "Reducing allocation request to %d.\n",
3661 num_to_alloc, phba->cfg_hba_queue_depth,
3662 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05003663 num_to_alloc = phba->cfg_hba_queue_depth - total;
3664 }
James Smart3772a992009-05-22 14:50:54 -04003665 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3666 if (num_to_alloc != num_allocated) {
3667 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3668 "0708 Allocation request of %d "
3669 "command buffers did not succeed. "
3670 "Allocated %d buffers.\n",
3671 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05003672 }
James Smart1c6f4ef52009-11-18 15:40:49 -05003673 if (num_allocated > 0)
3674 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05003675 return 0;
3676}
3677
James Smart9bad7672008-12-04 22:39:02 -05003678/**
James Smart3621a712009-04-06 18:47:14 -04003679 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05003680 * @sdev: Pointer to scsi_device.
3681 *
3682 * This routine configures following items
3683 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05003684 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3685 *
3686 * Return codes:
3687 * 0 - Success
3688 **/
dea31012005-04-17 16:05:31 -05003689static int
3690lpfc_slave_configure(struct scsi_device *sdev)
3691{
James Smart2e0fef82007-06-17 19:56:36 -05003692 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3693 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003694
3695 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04003696 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003697 else
James Smart3de2a652007-08-02 11:09:59 -04003698 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05003699
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003700 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04003701 lpfc_sli_handle_fast_ring_event(phba,
3702 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003703 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3704 lpfc_poll_rearm_timer(phba);
3705 }
3706
dea31012005-04-17 16:05:31 -05003707 return 0;
3708}
3709
James Smart9bad7672008-12-04 22:39:02 -05003710/**
James Smart3621a712009-04-06 18:47:14 -04003711 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05003712 * @sdev: Pointer to scsi_device.
3713 *
3714 * This routine sets @sdev hostatdata filed to null.
3715 **/
dea31012005-04-17 16:05:31 -05003716static void
3717lpfc_slave_destroy(struct scsi_device *sdev)
3718{
James Smartd7c47992010-06-08 18:31:54 -04003719 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3720 struct lpfc_hba *phba = vport->phba;
3721 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05003722 sdev->hostdata = NULL;
3723 return;
3724}
3725
James Smart92d7f7b2007-06-17 19:56:38 -05003726
dea31012005-04-17 16:05:31 -05003727struct scsi_host_template lpfc_template = {
3728 .module = THIS_MODULE,
3729 .name = LPFC_DRIVER_NAME,
3730 .info = lpfc_info,
3731 .queuecommand = lpfc_queuecommand,
3732 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003733 .eh_device_reset_handler = lpfc_device_reset_handler,
3734 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04003735 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05003736 .slave_alloc = lpfc_slave_alloc,
3737 .slave_configure = lpfc_slave_configure,
3738 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04003739 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05003740 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003741 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05003742 .cmd_per_lun = LPFC_CMD_PER_LUN,
3743 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05003744 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04003745 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04003746 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05003747 .change_queue_depth = lpfc_change_queue_depth,
dea31012005-04-17 16:05:31 -05003748};
James Smart3de2a652007-08-02 11:09:59 -04003749
3750struct scsi_host_template lpfc_vport_template = {
3751 .module = THIS_MODULE,
3752 .name = LPFC_DRIVER_NAME,
3753 .info = lpfc_info,
3754 .queuecommand = lpfc_queuecommand,
3755 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04003756 .eh_device_reset_handler = lpfc_device_reset_handler,
3757 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04003758 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3759 .slave_alloc = lpfc_slave_alloc,
3760 .slave_configure = lpfc_slave_configure,
3761 .slave_destroy = lpfc_slave_destroy,
3762 .scan_finished = lpfc_scan_finished,
3763 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05003764 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04003765 .cmd_per_lun = LPFC_CMD_PER_LUN,
3766 .use_clustering = ENABLE_CLUSTERING,
3767 .shost_attrs = lpfc_vport_attrs,
3768 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05003769 .change_queue_depth = lpfc_change_queue_depth,
James Smart3de2a652007-08-02 11:09:59 -04003770};