blob: 853223947d7adab6768e5ce73ed768e231485f8e [file] [log] [blame]
James Smartd85296c2012-03-01 22:38:13 -05001/*******************************************************************
dea31012005-04-17 16:05:31 -05002 * 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 Smartacd68592012-01-18 16:25:09 -05004 * Copyright (C) 2004-2012 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>
Paul Gortmaker09703662011-05-27 09:37:25 -040024#include <linux/export.h>
James Smarta90f5682006-08-17 11:58:04 -040025#include <linux/delay.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050026#include <asm/unaligned.h>
James Smart737d4242013-04-17 20:14:49 -040027#include <linux/crc-t10dif.h>
28#include <net/checksum.h>
dea31012005-04-17 16:05:31 -050029
30#include <scsi/scsi.h>
31#include <scsi/scsi_device.h>
James Smarte2a0a9d2008-12-04 22:40:02 -050032#include <scsi/scsi_eh.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_host.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsi_transport_fc.h>
36
37#include "lpfc_version.h"
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_hw.h"
40#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040041#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040042#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc.h"
James Smart9a6b09c2012-03-01 22:37:42 -050045#include "lpfc_scsi.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc_logmsg.h"
47#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050048#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050049
50#define LPFC_RESET_WAIT 2
51#define LPFC_ABORT_WAIT 2
52
James Smart737d4242013-04-17 20:14:49 -040053int _dump_buf_done = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -050054
55static char *dif_op_str[] = {
James Smart9a6b09c2012-03-01 22:37:42 -050056 "PROT_NORMAL",
57 "PROT_READ_INSERT",
58 "PROT_WRITE_STRIP",
59 "PROT_READ_STRIP",
60 "PROT_WRITE_INSERT",
61 "PROT_READ_PASS",
62 "PROT_WRITE_PASS",
63};
64
James Smartf9bb2da2011-10-10 21:34:11 -040065struct scsi_dif_tuple {
66 __be16 guard_tag; /* Checksum */
67 __be16 app_tag; /* Opaque storage */
68 __be32 ref_tag; /* Target LBA or indirect LBA */
69};
70
James Smartda0436e2009-05-22 14:51:39 -040071static void
72lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart1c6f4ef52009-11-18 15:40:49 -050073static void
74lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
James Smart9c6aa9d2013-05-31 17:03:39 -040075static int
76lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
James Smarte2a0a9d2008-12-04 22:40:02 -050077
78static void
James Smart6a9c52c2009-10-02 15:16:51 -040079lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -050080{
81 void *src, *dst;
82 struct scatterlist *sgde = scsi_sglist(cmnd);
83
84 if (!_dump_buf_data) {
James Smart6a9c52c2009-10-02 15:16:51 -040085 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
86 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -050087 __func__);
88 return;
89 }
90
91
92 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -040093 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
94 "9051 BLKGRD: ERROR: data scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -050095 return;
96 }
97
98 dst = (void *) _dump_buf_data;
99 while (sgde) {
100 src = sg_virt(sgde);
101 memcpy(dst, src, sgde->length);
102 dst += sgde->length;
103 sgde = sg_next(sgde);
104 }
105}
106
107static void
James Smart6a9c52c2009-10-02 15:16:51 -0400108lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
James Smarte2a0a9d2008-12-04 22:40:02 -0500109{
110 void *src, *dst;
111 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
112
113 if (!_dump_buf_dif) {
James Smart6a9c52c2009-10-02 15:16:51 -0400114 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
115 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
James Smarte2a0a9d2008-12-04 22:40:02 -0500116 __func__);
117 return;
118 }
119
120 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -0400121 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
122 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
James Smarte2a0a9d2008-12-04 22:40:02 -0500123 return;
124 }
125
126 dst = _dump_buf_dif;
127 while (sgde) {
128 src = sg_virt(sgde);
129 memcpy(dst, src, sgde->length);
130 dst += sgde->length;
131 sgde = sg_next(sgde);
132 }
133}
134
James Smart9c6aa9d2013-05-31 17:03:39 -0400135static inline unsigned
136lpfc_cmd_blksize(struct scsi_cmnd *sc)
137{
138 return sc->device->sector_size;
139}
140
141#define LPFC_CHECK_PROTECT_GUARD 1
142#define LPFC_CHECK_PROTECT_REF 2
143static inline unsigned
144lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
145{
146 return 1;
147}
148
149static inline unsigned
150lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
151{
152 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
153 return 0;
154 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
155 return 1;
156 return 0;
157}
158
James Smartea2151b2008-09-07 11:52:10 -0400159/**
James Smartf1126682009-06-10 17:22:44 -0400160 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
161 * @phba: Pointer to HBA object.
162 * @lpfc_cmd: lpfc scsi command object pointer.
163 *
164 * This function is called from the lpfc_prep_task_mgmt_cmd function to
165 * set the last bit in the response sge entry.
166 **/
167static void
168lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
169 struct lpfc_scsi_buf *lpfc_cmd)
170{
171 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
172 if (sgl) {
173 sgl += 1;
174 sgl->word2 = le32_to_cpu(sgl->word2);
175 bf_set(lpfc_sli4_sge_last, sgl, 1);
176 sgl->word2 = cpu_to_le32(sgl->word2);
177 }
178}
179
180/**
James Smart3621a712009-04-06 18:47:14 -0400181 * lpfc_update_stats - Update statistical data for the command completion
James Smartea2151b2008-09-07 11:52:10 -0400182 * @phba: Pointer to HBA object.
183 * @lpfc_cmd: lpfc scsi command object pointer.
184 *
185 * This function is called when there is a command completion and this
186 * function updates the statistical data for the command completion.
187 **/
188static void
189lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
190{
191 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
192 struct lpfc_nodelist *pnode = rdata->pnode;
193 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
194 unsigned long flags;
195 struct Scsi_Host *shost = cmd->device->host;
196 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
197 unsigned long latency;
198 int i;
199
200 if (cmd->result)
201 return;
202
James Smart9f1e1b52008-12-04 22:39:40 -0500203 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
204
James Smartea2151b2008-09-07 11:52:10 -0400205 spin_lock_irqsave(shost->host_lock, flags);
206 if (!vport->stat_data_enabled ||
207 vport->stat_data_blocked ||
James Smart5989b8d2010-10-22 11:06:56 -0400208 !pnode ||
James Smartea2151b2008-09-07 11:52:10 -0400209 !pnode->lat_data ||
210 (phba->bucket_type == LPFC_NO_BUCKET)) {
211 spin_unlock_irqrestore(shost->host_lock, flags);
212 return;
213 }
James Smartea2151b2008-09-07 11:52:10 -0400214
215 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
216 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
217 phba->bucket_step;
James Smart9f1e1b52008-12-04 22:39:40 -0500218 /* check array subscript bounds */
219 if (i < 0)
220 i = 0;
221 else if (i >= LPFC_MAX_BUCKET_COUNT)
222 i = LPFC_MAX_BUCKET_COUNT - 1;
James Smartea2151b2008-09-07 11:52:10 -0400223 } else {
224 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
225 if (latency <= (phba->bucket_base +
226 ((1<<i)*phba->bucket_step)))
227 break;
228 }
229
230 pnode->lat_data[i].cmd_count++;
231 spin_unlock_irqrestore(shost->host_lock, flags);
232}
233
James Smartea2151b2008-09-07 11:52:10 -0400234/**
James Smart3621a712009-04-06 18:47:14 -0400235 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
James Smartea2151b2008-09-07 11:52:10 -0400236 * @phba: Pointer to HBA context object.
237 * @vport: Pointer to vport object.
238 * @ndlp: Pointer to FC node associated with the target.
239 * @lun: Lun number of the scsi device.
240 * @old_val: Old value of the queue depth.
241 * @new_val: New value of the queue depth.
242 *
243 * This function sends an event to the mgmt application indicating
244 * there is a change in the scsi device queue depth.
245 **/
246static void
247lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
248 struct lpfc_vport *vport,
249 struct lpfc_nodelist *ndlp,
250 uint32_t lun,
251 uint32_t old_val,
252 uint32_t new_val)
253{
254 struct lpfc_fast_path_event *fast_path_evt;
255 unsigned long flags;
256
257 fast_path_evt = lpfc_alloc_fast_evt(phba);
258 if (!fast_path_evt)
259 return;
260
261 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
262 FC_REG_SCSI_EVENT;
263 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
264 LPFC_EVENT_VARQUEDEPTH;
265
266 /* Report all luns with change in queue depth */
267 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
268 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
269 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
270 &ndlp->nlp_portname, sizeof(struct lpfc_name));
271 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
272 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
273 }
274
275 fast_path_evt->un.queue_depth_evt.oldval = old_val;
276 fast_path_evt->un.queue_depth_evt.newval = new_val;
277 fast_path_evt->vport = vport;
278
279 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
280 spin_lock_irqsave(&phba->hbalock, flags);
281 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
282 spin_unlock_irqrestore(&phba->hbalock, flags);
283 lpfc_worker_wake_up(phba);
284
285 return;
286}
287
James Smart9bad7672008-12-04 22:39:02 -0500288/**
James Smart5ffc2662009-11-18 15:39:44 -0500289 * lpfc_change_queue_depth - Alter scsi device queue depth
290 * @sdev: Pointer the scsi device on which to change the queue depth.
291 * @qdepth: New queue depth to set the sdev to.
292 * @reason: The reason for the queue depth change.
293 *
294 * This function is called by the midlayer and the LLD to alter the queue
295 * depth for a scsi device. This function sets the queue depth to the new
296 * value and sends an event out to log the queue depth change.
297 **/
298int
299lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
300{
301 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
302 struct lpfc_hba *phba = vport->phba;
303 struct lpfc_rport_data *rdata;
304 unsigned long new_queue_depth, old_queue_depth;
305
306 old_queue_depth = sdev->queue_depth;
307 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
308 new_queue_depth = sdev->queue_depth;
309 rdata = sdev->hostdata;
310 if (rdata)
311 lpfc_send_sdev_queuedepth_change_event(phba, vport,
312 rdata->pnode, sdev->lun,
313 old_queue_depth,
314 new_queue_depth);
315 return sdev->queue_depth;
316}
317
318/**
James Smartfe8f7f92013-01-03 15:43:03 -0500319 * lpfc_change_queue_type() - Change a device's scsi tag queuing type
320 * @sdev: Pointer the scsi device whose queue depth is to change
321 * @tag_type: Identifier for queue tag type
322 */
323static int
324lpfc_change_queue_type(struct scsi_device *sdev, int tag_type)
325{
326 if (sdev->tagged_supported) {
327 scsi_set_tag_type(sdev, tag_type);
328 if (tag_type)
329 scsi_activate_tcq(sdev, sdev->queue_depth);
330 else
331 scsi_deactivate_tcq(sdev, sdev->queue_depth);
332 } else
333 tag_type = 0;
334
335 return tag_type;
336}
337
338/**
James Smart3621a712009-04-06 18:47:14 -0400339 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
James Smart9bad7672008-12-04 22:39:02 -0500340 * @phba: The Hba for which this call is being executed.
341 *
342 * This routine is called when there is resource error in driver or firmware.
343 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
344 * posts at most 1 event each second. This routine wakes up worker thread of
345 * @phba to process WORKER_RAM_DOWN_EVENT event.
346 *
347 * This routine should be called with no lock held.
348 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500349void
James Smarteaf15d52008-12-04 22:39:29 -0500350lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
James Smart92d7f7b2007-06-17 19:56:38 -0500351{
352 unsigned long flags;
James Smart5e9d9b82008-06-14 22:52:53 -0400353 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500354
355 spin_lock_irqsave(&phba->hbalock, flags);
356 atomic_inc(&phba->num_rsrc_err);
357 phba->last_rsrc_error_time = jiffies;
358
359 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
360 spin_unlock_irqrestore(&phba->hbalock, flags);
361 return;
362 }
363
364 phba->last_ramp_down_time = jiffies;
365
366 spin_unlock_irqrestore(&phba->hbalock, flags);
367
368 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400369 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
370 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500371 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500372 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
373
James Smart5e9d9b82008-06-14 22:52:53 -0400374 if (!evt_posted)
375 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500376 return;
377}
378
James Smart9bad7672008-12-04 22:39:02 -0500379/**
James Smart3621a712009-04-06 18:47:14 -0400380 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
James Smart9bad7672008-12-04 22:39:02 -0500381 * @phba: The Hba for which this call is being executed.
382 *
383 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
384 * post at most 1 event every 5 minute after last_ramp_up_time or
385 * last_rsrc_error_time. This routine wakes up worker thread of @phba
386 * to process WORKER_RAM_DOWN_EVENT event.
387 *
388 * This routine should be called with no lock held.
389 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500390static inline void
James Smart3de2a652007-08-02 11:09:59 -0400391lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smarta257bf92009-04-06 18:48:10 -0400392 uint32_t queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500393{
394 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -0400395 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -0400396 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -0500397 atomic_inc(&phba->num_cmd_success);
398
James Smarta257bf92009-04-06 18:48:10 -0400399 if (vport->cfg_lun_queue_depth <= queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -0500400 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500401 spin_lock_irqsave(&phba->hbalock, flags);
James Smart5ffc2662009-11-18 15:39:44 -0500402 if (time_before(jiffies,
403 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
404 time_before(jiffies,
405 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
James Smart92d7f7b2007-06-17 19:56:38 -0500406 spin_unlock_irqrestore(&phba->hbalock, flags);
407 return;
408 }
James Smart92d7f7b2007-06-17 19:56:38 -0500409 phba->last_ramp_up_time = jiffies;
410 spin_unlock_irqrestore(&phba->hbalock, flags);
411
412 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400413 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
414 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500415 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500416 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
417
James Smart5e9d9b82008-06-14 22:52:53 -0400418 if (!evt_posted)
419 lpfc_worker_wake_up(phba);
420 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500421}
422
James Smart9bad7672008-12-04 22:39:02 -0500423/**
James Smart3621a712009-04-06 18:47:14 -0400424 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500425 * @phba: The Hba for which this call is being executed.
426 *
427 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
428 * thread.This routine reduces queue depth for all scsi device on each vport
429 * associated with @phba.
430 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500431void
432lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
433{
James Smart549e55c2007-08-02 11:09:51 -0400434 struct lpfc_vport **vports;
435 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500436 struct scsi_device *sdev;
James Smart5ffc2662009-11-18 15:39:44 -0500437 unsigned long new_queue_depth;
James Smart92d7f7b2007-06-17 19:56:38 -0500438 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400439 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500440
441 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
442 num_cmd_success = atomic_read(&phba->num_cmd_success);
443
James Smart75ad83a2012-05-09 21:18:40 -0400444 /*
445 * The error and success command counters are global per
446 * driver instance. If another handler has already
447 * operated on this error event, just exit.
448 */
449 if (num_rsrc_err == 0)
450 return;
451
James Smart549e55c2007-08-02 11:09:51 -0400452 vports = lpfc_create_vport_work_array(phba);
453 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400454 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400455 shost = lpfc_shost_from_vport(vports[i]);
456 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500457 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400458 sdev->queue_depth * num_rsrc_err /
459 (num_rsrc_err + num_cmd_success);
460 if (!new_queue_depth)
461 new_queue_depth = sdev->queue_depth - 1;
462 else
463 new_queue_depth = sdev->queue_depth -
464 new_queue_depth;
James Smart5ffc2662009-11-18 15:39:44 -0500465 lpfc_change_queue_depth(sdev, new_queue_depth,
466 SCSI_QDEPTH_DEFAULT);
James Smart549e55c2007-08-02 11:09:51 -0400467 }
James Smart92d7f7b2007-06-17 19:56:38 -0500468 }
James Smart09372822008-01-11 01:52:54 -0500469 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500470 atomic_set(&phba->num_rsrc_err, 0);
471 atomic_set(&phba->num_cmd_success, 0);
472}
473
James Smart9bad7672008-12-04 22:39:02 -0500474/**
James Smart3621a712009-04-06 18:47:14 -0400475 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
James Smart9bad7672008-12-04 22:39:02 -0500476 * @phba: The Hba for which this call is being executed.
477 *
478 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
479 * thread.This routine increases queue depth for all scsi device on each vport
480 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
481 * num_cmd_success to zero.
482 **/
James Smart92d7f7b2007-06-17 19:56:38 -0500483void
484lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
485{
James Smart549e55c2007-08-02 11:09:51 -0400486 struct lpfc_vport **vports;
487 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500488 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400489 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500490
James Smart549e55c2007-08-02 11:09:51 -0400491 vports = lpfc_create_vport_work_array(phba);
492 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400493 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400494 shost = lpfc_shost_from_vport(vports[i]);
495 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400496 if (vports[i]->cfg_lun_queue_depth <=
497 sdev->queue_depth)
498 continue;
James Smart5ffc2662009-11-18 15:39:44 -0500499 lpfc_change_queue_depth(sdev,
500 sdev->queue_depth+1,
501 SCSI_QDEPTH_RAMP_UP);
James Smart549e55c2007-08-02 11:09:51 -0400502 }
James Smart92d7f7b2007-06-17 19:56:38 -0500503 }
James Smart09372822008-01-11 01:52:54 -0500504 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500505 atomic_set(&phba->num_rsrc_err, 0);
506 atomic_set(&phba->num_cmd_success, 0);
507}
508
James Smarta8e497d2008-08-24 21:50:11 -0400509/**
James Smart3621a712009-04-06 18:47:14 -0400510 * lpfc_scsi_dev_block - set all scsi hosts to block state
James Smarta8e497d2008-08-24 21:50:11 -0400511 * @phba: Pointer to HBA context object.
512 *
513 * This function walks vport list and set each SCSI host to block state
514 * by invoking fc_remote_port_delete() routine. This function is invoked
515 * with EEH when device's PCI slot has been permanently disabled.
516 **/
517void
518lpfc_scsi_dev_block(struct lpfc_hba *phba)
519{
520 struct lpfc_vport **vports;
521 struct Scsi_Host *shost;
522 struct scsi_device *sdev;
523 struct fc_rport *rport;
524 int i;
525
526 vports = lpfc_create_vport_work_array(phba);
527 if (vports != NULL)
James Smart21e9a0a2009-05-22 14:53:21 -0400528 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smarta8e497d2008-08-24 21:50:11 -0400529 shost = lpfc_shost_from_vport(vports[i]);
530 shost_for_each_device(sdev, shost) {
531 rport = starget_to_rport(scsi_target(sdev));
532 fc_remote_port_delete(rport);
533 }
534 }
535 lpfc_destroy_vport_work_array(phba, vports);
536}
537
James Smart9bad7672008-12-04 22:39:02 -0500538/**
James Smart3772a992009-05-22 14:50:54 -0400539 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -0500540 * @vport: The virtual port for which this call being executed.
James Smart3772a992009-05-22 14:50:54 -0400541 * @num_to_allocate: The requested number of buffers to allocate.
James Smart9bad7672008-12-04 22:39:02 -0500542 *
James Smart3772a992009-05-22 14:50:54 -0400543 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
544 * the scsi buffer contains all the necessary information needed to initiate
545 * a SCSI I/O. The non-DMAable buffer region contains information to build
546 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
547 * and the initial BPL. In addition to allocating memory, the FCP CMND and
548 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
James Smart9bad7672008-12-04 22:39:02 -0500549 *
550 * Return codes:
James Smart3772a992009-05-22 14:50:54 -0400551 * int - number of scsi buffers that were allocated.
552 * 0 = failure, less than num_to_alloc is a partial failure.
James Smart9bad7672008-12-04 22:39:02 -0500553 **/
James Smart3772a992009-05-22 14:50:54 -0400554static int
555lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
dea31012005-04-17 16:05:31 -0500556{
James Smart2e0fef82007-06-17 19:56:36 -0500557 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500558 struct lpfc_scsi_buf *psb;
559 struct ulp_bde64 *bpl;
560 IOCB_t *iocb;
James Smart34b02dc2008-08-24 21:49:55 -0400561 dma_addr_t pdma_phys_fcp_cmd;
562 dma_addr_t pdma_phys_fcp_rsp;
563 dma_addr_t pdma_phys_bpl;
James Bottomley604a3e32005-10-29 10:28:33 -0500564 uint16_t iotag;
James Smart96f70772013-04-17 20:16:15 -0400565 int bcnt, bpl_size;
566
567 bpl_size = phba->cfg_sg_dma_buf_size -
568 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
569
570 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
571 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
572 num_to_alloc, phba->cfg_sg_dma_buf_size,
573 (int)sizeof(struct fcp_cmnd),
574 (int)sizeof(struct fcp_rsp), bpl_size);
dea31012005-04-17 16:05:31 -0500575
James Smart3772a992009-05-22 14:50:54 -0400576 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
577 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
578 if (!psb)
579 break;
dea31012005-04-17 16:05:31 -0500580
James Smart3772a992009-05-22 14:50:54 -0400581 /*
582 * Get memory from the pci pool to map the virt space to pci
583 * bus space for an I/O. The DMA buffer includes space for the
584 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
585 * necessary to support the sg_tablesize.
586 */
587 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
588 GFP_KERNEL, &psb->dma_handle);
589 if (!psb->data) {
590 kfree(psb);
591 break;
592 }
dea31012005-04-17 16:05:31 -0500593
James Smart3772a992009-05-22 14:50:54 -0400594 /* Initialize virtual ptrs to dma_buf region. */
595 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
dea31012005-04-17 16:05:31 -0500596
James Smart3772a992009-05-22 14:50:54 -0400597 /* Allocate iotag for psb->cur_iocbq. */
598 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
599 if (iotag == 0) {
600 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
601 psb->data, psb->dma_handle);
602 kfree(psb);
603 break;
604 }
605 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500606
James Smart3772a992009-05-22 14:50:54 -0400607 psb->fcp_cmnd = psb->data;
608 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
609 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
James Smart34b02dc2008-08-24 21:49:55 -0400610 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500611
James Smart3772a992009-05-22 14:50:54 -0400612 /* Initialize local short-hand pointers. */
613 bpl = psb->fcp_bpl;
614 pdma_phys_fcp_cmd = psb->dma_handle;
615 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
616 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
617 sizeof(struct fcp_rsp);
dea31012005-04-17 16:05:31 -0500618
James Smart3772a992009-05-22 14:50:54 -0400619 /*
620 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
621 * are sg list bdes. Initialize the first two and leave the
622 * rest for queuecommand.
623 */
624 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
625 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
626 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
627 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
628 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
dea31012005-04-17 16:05:31 -0500629
James Smart3772a992009-05-22 14:50:54 -0400630 /* Setup the physical region for the FCP RSP */
631 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
632 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
633 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
634 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
635 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
636
637 /*
638 * Since the IOCB for the FCP I/O is built into this
639 * lpfc_scsi_buf, initialize it with all known data now.
640 */
641 iocb = &psb->cur_iocbq.iocb;
642 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
643 if ((phba->sli_rev == 3) &&
644 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
645 /* fill in immediate fcp command BDE */
646 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
647 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
648 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
649 unsli3.fcp_ext.icd);
650 iocb->un.fcpi64.bdl.addrHigh = 0;
651 iocb->ulpBdeCount = 0;
652 iocb->ulpLe = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300653 /* fill in response BDE */
James Smart3772a992009-05-22 14:50:54 -0400654 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
655 BUFF_TYPE_BDE_64;
656 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
657 sizeof(struct fcp_rsp);
658 iocb->unsli3.fcp_ext.rbde.addrLow =
659 putPaddrLow(pdma_phys_fcp_rsp);
660 iocb->unsli3.fcp_ext.rbde.addrHigh =
661 putPaddrHigh(pdma_phys_fcp_rsp);
662 } else {
663 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
664 iocb->un.fcpi64.bdl.bdeSize =
665 (2 * sizeof(struct ulp_bde64));
666 iocb->un.fcpi64.bdl.addrLow =
667 putPaddrLow(pdma_phys_bpl);
668 iocb->un.fcpi64.bdl.addrHigh =
669 putPaddrHigh(pdma_phys_bpl);
670 iocb->ulpBdeCount = 1;
671 iocb->ulpLe = 1;
672 }
673 iocb->ulpClass = CLASS3;
674 psb->status = IOSTAT_SUCCESS;
James Smartda0436e2009-05-22 14:51:39 -0400675 /* Put it back into the SCSI buffer list */
James Smarteee88772010-09-29 11:19:08 -0400676 psb->cur_iocbq.context1 = psb;
James Smart1c6f4ef52009-11-18 15:40:49 -0500677 lpfc_release_scsi_buf_s3(phba, psb);
James Smart3772a992009-05-22 14:50:54 -0400678
James Smart34b02dc2008-08-24 21:49:55 -0400679 }
dea31012005-04-17 16:05:31 -0500680
James Smart3772a992009-05-22 14:50:54 -0400681 return bcnt;
dea31012005-04-17 16:05:31 -0500682}
683
James Smart9bad7672008-12-04 22:39:02 -0500684/**
James Smart1151e3e2011-02-16 12:39:35 -0500685 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
686 * @vport: pointer to lpfc vport data structure.
687 *
688 * This routine is invoked by the vport cleanup for deletions and the cleanup
689 * for an ndlp on removal.
690 **/
691void
692lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
693{
694 struct lpfc_hba *phba = vport->phba;
695 struct lpfc_scsi_buf *psb, *next_psb;
696 unsigned long iflag = 0;
697
698 spin_lock_irqsave(&phba->hbalock, iflag);
699 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
700 list_for_each_entry_safe(psb, next_psb,
701 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
702 if (psb->rdata && psb->rdata->pnode
703 && psb->rdata->pnode->vport == vport)
704 psb->rdata = NULL;
705 }
706 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
707 spin_unlock_irqrestore(&phba->hbalock, iflag);
708}
709
710/**
James Smartda0436e2009-05-22 14:51:39 -0400711 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
712 * @phba: pointer to lpfc hba data structure.
713 * @axri: pointer to the fcp xri abort wcqe structure.
714 *
715 * This routine is invoked by the worker thread to process a SLI4 fast-path
716 * FCP aborted xri.
717 **/
718void
719lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
720 struct sli4_wcqe_xri_aborted *axri)
721{
722 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
James Smart19ca7602010-11-20 23:11:55 -0500723 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
James Smartda0436e2009-05-22 14:51:39 -0400724 struct lpfc_scsi_buf *psb, *next_psb;
725 unsigned long iflag = 0;
James Smart0f65ff62010-02-26 14:14:23 -0500726 struct lpfc_iocbq *iocbq;
727 int i;
James Smart19ca7602010-11-20 23:11:55 -0500728 struct lpfc_nodelist *ndlp;
729 int rrq_empty = 0;
James Smart589a52d2010-07-14 15:30:54 -0400730 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smartda0436e2009-05-22 14:51:39 -0400731
James Smart0f65ff62010-02-26 14:14:23 -0500732 spin_lock_irqsave(&phba->hbalock, iflag);
733 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400734 list_for_each_entry_safe(psb, next_psb,
735 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
736 if (psb->cur_iocbq.sli4_xritag == xri) {
737 list_del(&psb->list);
James Smart341af102010-01-26 23:07:37 -0500738 psb->exch_busy = 0;
James Smartda0436e2009-05-22 14:51:39 -0400739 psb->status = IOSTAT_SUCCESS;
James Smart0f65ff62010-02-26 14:14:23 -0500740 spin_unlock(
741 &phba->sli4_hba.abts_scsi_buf_list_lock);
James Smart1151e3e2011-02-16 12:39:35 -0500742 if (psb->rdata && psb->rdata->pnode)
743 ndlp = psb->rdata->pnode;
744 else
745 ndlp = NULL;
746
James Smart19ca7602010-11-20 23:11:55 -0500747 rrq_empty = list_empty(&phba->active_rrq_list);
James Smart0f65ff62010-02-26 14:14:23 -0500748 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartcb69f7d2011-12-13 13:21:57 -0500749 if (ndlp) {
James Smartee0f4fe2012-05-09 21:19:14 -0400750 lpfc_set_rrq_active(phba, ndlp,
751 psb->cur_iocbq.sli4_lxritag, rxid, 1);
James Smartcb69f7d2011-12-13 13:21:57 -0500752 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
753 }
James Smartda0436e2009-05-22 14:51:39 -0400754 lpfc_release_scsi_buf_s4(phba, psb);
James Smart19ca7602010-11-20 23:11:55 -0500755 if (rrq_empty)
756 lpfc_worker_wake_up(phba);
James Smartda0436e2009-05-22 14:51:39 -0400757 return;
758 }
759 }
James Smart0f65ff62010-02-26 14:14:23 -0500760 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
761 for (i = 1; i <= phba->sli.last_iotag; i++) {
762 iocbq = phba->sli.iocbq_lookup[i];
763
764 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
765 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
766 continue;
767 if (iocbq->sli4_xritag != xri)
768 continue;
769 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
770 psb->exch_busy = 0;
771 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart0e9bb8d2013-03-01 16:35:12 -0500772 if (!list_empty(&pring->txq))
James Smart589a52d2010-07-14 15:30:54 -0400773 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -0500774 return;
775
776 }
777 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartda0436e2009-05-22 14:51:39 -0400778}
779
780/**
James Smart8a9d2e82012-05-09 21:16:12 -0400781 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
782 * @phba: pointer to lpfc hba data structure.
783 * @post_sblist: pointer to the scsi buffer list.
784 *
785 * This routine walks a list of scsi buffers that was passed in. It attempts
786 * to construct blocks of scsi buffer sgls which contains contiguous xris and
787 * uses the non-embedded SGL block post mailbox commands to post to the port.
788 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
789 * embedded SGL post mailbox command for posting. The @post_sblist passed in
790 * must be local list, thus no lock is needed when manipulate the list.
791 *
792 * Returns: 0 = failure, non-zero number of successfully posted buffers.
793 **/
794int
795lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
796 struct list_head *post_sblist, int sb_count)
797{
798 struct lpfc_scsi_buf *psb, *psb_next;
James Smart96f70772013-04-17 20:16:15 -0400799 int status, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400800 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
801 dma_addr_t pdma_phys_bpl1;
802 int last_xritag = NO_XRI;
803 LIST_HEAD(prep_sblist);
804 LIST_HEAD(blck_sblist);
805 LIST_HEAD(scsi_sblist);
806
807 /* sanity check */
808 if (sb_count <= 0)
809 return -EINVAL;
810
James Smart96f70772013-04-17 20:16:15 -0400811 sgl_size = phba->cfg_sg_dma_buf_size -
812 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
813
James Smart8a9d2e82012-05-09 21:16:12 -0400814 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
815 list_del_init(&psb->list);
816 block_cnt++;
817 if ((last_xritag != NO_XRI) &&
818 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
819 /* a hole in xri block, form a sgl posting block */
820 list_splice_init(&prep_sblist, &blck_sblist);
821 post_cnt = block_cnt - 1;
822 /* prepare list for next posting block */
823 list_add_tail(&psb->list, &prep_sblist);
824 block_cnt = 1;
825 } else {
826 /* prepare list for next posting block */
827 list_add_tail(&psb->list, &prep_sblist);
828 /* enough sgls for non-embed sgl mbox command */
829 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
830 list_splice_init(&prep_sblist, &blck_sblist);
831 post_cnt = block_cnt;
832 block_cnt = 0;
833 }
834 }
835 num_posting++;
836 last_xritag = psb->cur_iocbq.sli4_xritag;
837
838 /* end of repost sgl list condition for SCSI buffers */
839 if (num_posting == sb_count) {
840 if (post_cnt == 0) {
841 /* last sgl posting block */
842 list_splice_init(&prep_sblist, &blck_sblist);
843 post_cnt = block_cnt;
844 } else if (block_cnt == 1) {
845 /* last single sgl with non-contiguous xri */
James Smart96f70772013-04-17 20:16:15 -0400846 if (sgl_size > SGL_PAGE_SIZE)
James Smart8a9d2e82012-05-09 21:16:12 -0400847 pdma_phys_bpl1 = psb->dma_phys_bpl +
848 SGL_PAGE_SIZE;
849 else
850 pdma_phys_bpl1 = 0;
851 status = lpfc_sli4_post_sgl(phba,
852 psb->dma_phys_bpl,
853 pdma_phys_bpl1,
854 psb->cur_iocbq.sli4_xritag);
855 if (status) {
856 /* failure, put on abort scsi list */
857 psb->exch_busy = 1;
858 } else {
859 /* success, put on SCSI buffer list */
860 psb->exch_busy = 0;
861 psb->status = IOSTAT_SUCCESS;
862 num_posted++;
863 }
864 /* success, put on SCSI buffer sgl list */
865 list_add_tail(&psb->list, &scsi_sblist);
866 }
867 }
868
869 /* continue until a nembed page worth of sgls */
870 if (post_cnt == 0)
871 continue;
872
873 /* post block of SCSI buffer list sgls */
874 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
875 post_cnt);
876
877 /* don't reset xirtag due to hole in xri block */
878 if (block_cnt == 0)
879 last_xritag = NO_XRI;
880
881 /* reset SCSI buffer post count for next round of posting */
882 post_cnt = 0;
883
884 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
885 while (!list_empty(&blck_sblist)) {
886 list_remove_head(&blck_sblist, psb,
887 struct lpfc_scsi_buf, list);
888 if (status) {
889 /* failure, put on abort scsi list */
890 psb->exch_busy = 1;
891 } else {
892 /* success, put on SCSI buffer list */
893 psb->exch_busy = 0;
894 psb->status = IOSTAT_SUCCESS;
895 num_posted++;
896 }
897 list_add_tail(&psb->list, &scsi_sblist);
898 }
899 }
900 /* Push SCSI buffers with sgl posted to the availble list */
901 while (!list_empty(&scsi_sblist)) {
902 list_remove_head(&scsi_sblist, psb,
903 struct lpfc_scsi_buf, list);
904 lpfc_release_scsi_buf_s4(phba, psb);
905 }
906 return num_posted;
907}
908
909/**
910 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
James Smartda0436e2009-05-22 14:51:39 -0400911 * @phba: pointer to lpfc hba data structure.
912 *
913 * This routine walks the list of scsi buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -0400914 * repost them to the port by using SGL block post. This is needed after a
James Smartda0436e2009-05-22 14:51:39 -0400915 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
916 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
917 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
918 *
919 * Returns: 0 = success, non-zero failure.
920 **/
921int
922lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
923{
James Smart8a9d2e82012-05-09 21:16:12 -0400924 LIST_HEAD(post_sblist);
925 int num_posted, rc = 0;
James Smartda0436e2009-05-22 14:51:39 -0400926
James Smart8a9d2e82012-05-09 21:16:12 -0400927 /* get all SCSI buffers need to repost to a local list */
James Smarta40fc5f2013-04-17 20:17:40 -0400928 spin_lock_irq(&phba->scsi_buf_list_get_lock);
929 spin_lock_irq(&phba->scsi_buf_list_put_lock);
930 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
931 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
932 spin_unlock_irq(&phba->scsi_buf_list_put_lock);
933 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -0400934
James Smart8a9d2e82012-05-09 21:16:12 -0400935 /* post the list of scsi buffer sgls to port if available */
936 if (!list_empty(&post_sblist)) {
937 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
938 phba->sli4_hba.scsi_xri_cnt);
939 /* failed to post any scsi buffer, return error */
940 if (num_posted == 0)
941 rc = -EIO;
James Smartda0436e2009-05-22 14:51:39 -0400942 }
943 return rc;
944}
945
946/**
947 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
948 * @vport: The virtual port for which this call being executed.
949 * @num_to_allocate: The requested number of buffers to allocate.
950 *
James Smart8a9d2e82012-05-09 21:16:12 -0400951 * This routine allocates scsi buffers for device with SLI-4 interface spec,
James Smartda0436e2009-05-22 14:51:39 -0400952 * the scsi buffer contains all the necessary information needed to initiate
James Smart8a9d2e82012-05-09 21:16:12 -0400953 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
954 * them on a list, it post them to the port by using SGL block post.
James Smartda0436e2009-05-22 14:51:39 -0400955 *
956 * Return codes:
James Smart8a9d2e82012-05-09 21:16:12 -0400957 * int - number of scsi buffers that were allocated and posted.
James Smartda0436e2009-05-22 14:51:39 -0400958 * 0 = failure, less than num_to_alloc is a partial failure.
959 **/
960static int
961lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
962{
963 struct lpfc_hba *phba = vport->phba;
964 struct lpfc_scsi_buf *psb;
965 struct sli4_sge *sgl;
966 IOCB_t *iocb;
967 dma_addr_t pdma_phys_fcp_cmd;
968 dma_addr_t pdma_phys_fcp_rsp;
James Smart96f70772013-04-17 20:16:15 -0400969 dma_addr_t pdma_phys_bpl;
James Smart8a9d2e82012-05-09 21:16:12 -0400970 uint16_t iotag, lxri = 0;
James Smart96f70772013-04-17 20:16:15 -0400971 int bcnt, num_posted, sgl_size;
James Smart8a9d2e82012-05-09 21:16:12 -0400972 LIST_HEAD(prep_sblist);
973 LIST_HEAD(post_sblist);
974 LIST_HEAD(scsi_sblist);
James Smartda0436e2009-05-22 14:51:39 -0400975
James Smart96f70772013-04-17 20:16:15 -0400976 sgl_size = phba->cfg_sg_dma_buf_size -
977 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
978
979 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
980 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
981 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
982 (int)sizeof(struct fcp_cmnd),
983 (int)sizeof(struct fcp_rsp));
984
James Smartda0436e2009-05-22 14:51:39 -0400985 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
986 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
987 if (!psb)
988 break;
James Smartda0436e2009-05-22 14:51:39 -0400989 /*
James Smart8a9d2e82012-05-09 21:16:12 -0400990 * Get memory from the pci pool to map the virt space to
991 * pci bus space for an I/O. The DMA buffer includes space
992 * for the struct fcp_cmnd, struct fcp_rsp and the number
993 * of bde's necessary to support the sg_tablesize.
James Smartda0436e2009-05-22 14:51:39 -0400994 */
995 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
996 GFP_KERNEL, &psb->dma_handle);
997 if (!psb->data) {
998 kfree(psb);
999 break;
1000 }
James Smartda0436e2009-05-22 14:51:39 -04001001 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
1002
James Smart96f70772013-04-17 20:16:15 -04001003 /* Page alignment is CRITICAL, double check to be sure */
1004 if (((unsigned long)(psb->data) &
1005 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0) {
1006 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1007 psb->data, psb->dma_handle);
1008 kfree(psb);
1009 break;
1010 }
1011
James Smartda0436e2009-05-22 14:51:39 -04001012 /* Allocate iotag for psb->cur_iocbq. */
1013 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
1014 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -04001015 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1016 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -04001017 kfree(psb);
1018 break;
1019 }
1020
James Smart6d368e52011-05-24 11:44:12 -04001021 lxri = lpfc_sli4_next_xritag(phba);
1022 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -04001023 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1024 psb->data, psb->dma_handle);
1025 kfree(psb);
1026 break;
1027 }
James Smart6d368e52011-05-24 11:44:12 -04001028 psb->cur_iocbq.sli4_lxritag = lxri;
1029 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -04001030 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -04001031 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -04001032 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001033 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
1034 sizeof(struct fcp_cmnd));
1035
1036 /* Initialize local short-hand pointers. */
1037 sgl = (struct sli4_sge *)psb->fcp_bpl;
1038 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -04001039 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001040 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
1041
1042 /*
James Smart8a9d2e82012-05-09 21:16:12 -04001043 * The first two bdes are the FCP_CMD and FCP_RSP.
1044 * The balance are sg list bdes. Initialize the
1045 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -04001046 */
1047 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
1048 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -04001049 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001050 bf_set(lpfc_sli4_sge_last, sgl, 0);
1051 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001052 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -04001053 sgl++;
1054
1055 /* Setup the physical region for the FCP RSP */
1056 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1057 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -04001058 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001059 bf_set(lpfc_sli4_sge_last, sgl, 1);
1060 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001061 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -04001062
1063 /*
1064 * Since the IOCB for the FCP I/O is built into this
1065 * lpfc_scsi_buf, initialize it with all known data now.
1066 */
1067 iocb = &psb->cur_iocbq.iocb;
1068 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1069 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1070 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1071 * We are setting the bpl to point to out sgl. An sgl's
1072 * entries are 16 bytes, a bpl entries are 12 bytes.
1073 */
1074 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1075 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1076 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1077 iocb->ulpBdeCount = 1;
1078 iocb->ulpLe = 1;
1079 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001080 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001081 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001082
James Smart8a9d2e82012-05-09 21:16:12 -04001083 /* add the scsi buffer to a post list */
1084 list_add_tail(&psb->list, &post_sblist);
James Smarta40fc5f2013-04-17 20:17:40 -04001085 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04001086 phba->sli4_hba.scsi_xri_cnt++;
James Smarta40fc5f2013-04-17 20:17:40 -04001087 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -04001088 }
James Smart8a9d2e82012-05-09 21:16:12 -04001089 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1090 "3021 Allocate %d out of %d requested new SCSI "
1091 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001092
James Smart8a9d2e82012-05-09 21:16:12 -04001093 /* post the list of scsi buffer sgls to port if available */
1094 if (!list_empty(&post_sblist))
1095 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1096 &post_sblist, bcnt);
1097 else
1098 num_posted = 0;
1099
1100 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001101}
1102
1103/**
James Smart3772a992009-05-22 14:50:54 -04001104 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1105 * @vport: The virtual port for which this call being executed.
1106 * @num_to_allocate: The requested number of buffers to allocate.
1107 *
1108 * This routine wraps the actual SCSI buffer allocator function pointer from
1109 * the lpfc_hba struct.
1110 *
1111 * Return codes:
1112 * int - number of scsi buffers that were allocated.
1113 * 0 = failure, less than num_to_alloc is a partial failure.
1114 **/
1115static inline int
1116lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1117{
1118 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1119}
1120
1121/**
James Smart19ca7602010-11-20 23:11:55 -05001122 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001123 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001124 *
1125 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1126 * and returns to caller.
1127 *
1128 * Return codes:
1129 * NULL - Error
1130 * Pointer to lpfc_scsi_buf - Success
1131 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001132static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001133lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001134{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001135 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001136 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
1137 unsigned long gflag = 0;
1138 unsigned long pflag = 0;
dea31012005-04-17 16:05:31 -05001139
James Smarta40fc5f2013-04-17 20:17:40 -04001140 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, gflag);
1141 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
1142 list);
1143 if (!lpfc_cmd) {
1144 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, pflag);
1145 list_splice(&phba->lpfc_scsi_buf_list_put,
1146 &phba->lpfc_scsi_buf_list_get);
1147 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1148 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1149 struct lpfc_scsi_buf, list);
1150 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, pflag);
James Smart1dcb58e2007-04-25 09:51:30 -04001151 }
James Smarta40fc5f2013-04-17 20:17:40 -04001152 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, gflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001153 return lpfc_cmd;
1154}
James Smart19ca7602010-11-20 23:11:55 -05001155/**
1156 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1157 * @phba: The HBA for which this call is being executed.
1158 *
1159 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1160 * and returns to caller.
1161 *
1162 * Return codes:
1163 * NULL - Error
1164 * Pointer to lpfc_scsi_buf - Success
1165 **/
1166static struct lpfc_scsi_buf*
1167lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1168{
James Smart1151e3e2011-02-16 12:39:35 -05001169 struct lpfc_scsi_buf *lpfc_cmd ;
James Smarta40fc5f2013-04-17 20:17:40 -04001170 unsigned long gflag = 0;
1171 unsigned long pflag = 0;
James Smart19ca7602010-11-20 23:11:55 -05001172 int found = 0;
1173
James Smarta40fc5f2013-04-17 20:17:40 -04001174 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, gflag);
1175 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -05001176 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001177 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001178 continue;
1179 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001180 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -05001181 break;
James Smart19ca7602010-11-20 23:11:55 -05001182 }
James Smarta40fc5f2013-04-17 20:17:40 -04001183 if (!found) {
1184 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, pflag);
1185 list_splice(&phba->lpfc_scsi_buf_list_put,
1186 &phba->lpfc_scsi_buf_list_get);
1187 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1188 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, pflag);
1189 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list_get,
1190 list) {
1191 if (lpfc_test_rrq_active(
1192 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1193 continue;
1194 list_del(&lpfc_cmd->list);
1195 found = 1;
1196 break;
1197 }
1198 }
1199 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, gflag);
James Smart1151e3e2011-02-16 12:39:35 -05001200 if (!found)
1201 return NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001202 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001203}
1204/**
1205 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1206 * @phba: The HBA for which this call is being executed.
1207 *
1208 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1209 * and returns to caller.
1210 *
1211 * Return codes:
1212 * NULL - Error
1213 * Pointer to lpfc_scsi_buf - Success
1214 **/
1215static struct lpfc_scsi_buf*
1216lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1217{
1218 return phba->lpfc_get_scsi_buf(phba, ndlp);
1219}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001220
James Smart9bad7672008-12-04 22:39:02 -05001221/**
James Smart3772a992009-05-22 14:50:54 -04001222 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001223 * @phba: The Hba for which this call is being executed.
1224 * @psb: The scsi buffer which is being released.
1225 *
1226 * This routine releases @psb scsi buffer by adding it to tail of @phba
1227 * lpfc_scsi_buf_list list.
1228 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001229static void
James Smart3772a992009-05-22 14:50:54 -04001230lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001231{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001232 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001233
James Smarta40fc5f2013-04-17 20:17:40 -04001234 psb->seg_cnt = 0;
1235 psb->nonsg_phys = 0;
1236 psb->prot_seg_cnt = 0;
1237
1238 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001239 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001240 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001241 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1242 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001243}
1244
James Smart9bad7672008-12-04 22:39:02 -05001245/**
James Smartda0436e2009-05-22 14:51:39 -04001246 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1247 * @phba: The Hba for which this call is being executed.
1248 * @psb: The scsi buffer which is being released.
1249 *
1250 * This routine releases @psb scsi buffer by adding it to tail of @phba
1251 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1252 * and cannot be reused for at least RA_TOV amount of time if it was
1253 * aborted.
1254 **/
1255static void
1256lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1257{
1258 unsigned long iflag = 0;
1259
James Smarta40fc5f2013-04-17 20:17:40 -04001260 psb->seg_cnt = 0;
1261 psb->nonsg_phys = 0;
1262 psb->prot_seg_cnt = 0;
1263
James Smart341af102010-01-26 23:07:37 -05001264 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001265 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1266 iflag);
1267 psb->pCmd = NULL;
1268 list_add_tail(&psb->list,
1269 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1270 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1271 iflag);
1272 } else {
James Smartda0436e2009-05-22 14:51:39 -04001273 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001274 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001275 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1276 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1277 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001278 }
1279}
1280
1281/**
James Smart3772a992009-05-22 14:50:54 -04001282 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1283 * @phba: The Hba for which this call is being executed.
1284 * @psb: The scsi buffer which is being released.
1285 *
1286 * This routine releases @psb scsi buffer by adding it to tail of @phba
1287 * lpfc_scsi_buf_list list.
1288 **/
1289static void
1290lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1291{
1292
1293 phba->lpfc_release_scsi_buf(phba, psb);
1294}
1295
1296/**
1297 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001298 * @phba: The Hba for which this call is being executed.
1299 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1300 *
1301 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001302 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1303 * through sg elements and format the bdea. This routine also initializes all
1304 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001305 *
1306 * Return codes:
1307 * 1 - Error
1308 * 0 - Success
1309 **/
dea31012005-04-17 16:05:31 -05001310static int
James Smart3772a992009-05-22 14:50:54 -04001311lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001312{
1313 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1314 struct scatterlist *sgel = NULL;
1315 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1316 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001317 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001318 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001319 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001320 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001321 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001322 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001323
1324 /*
1325 * There are three possibilities here - use scatter-gather segment, use
1326 * the single mapping, or neither. Start the lpfc command prep by
1327 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1328 * data bde entry.
1329 */
1330 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001331 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001332 /*
1333 * The driver stores the segment count returned from pci_map_sg
1334 * because this a count of dma-mappings used to map the use_sg
1335 * pages. They are not guaranteed to be the same for those
1336 * architectures that implement an IOMMU.
1337 */
dea31012005-04-17 16:05:31 -05001338
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001339 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1340 scsi_sg_count(scsi_cmnd), datadir);
1341 if (unlikely(!nseg))
1342 return 1;
1343
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001344 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001345 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001346 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1347 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001348 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001349 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001350 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001351 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001352 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001353 return 1;
1354 }
1355
1356 /*
1357 * The driver established a maximum scatter-gather segment count
1358 * during probe that limits the number of sg elements in any
1359 * single scsi command. Just run through the seg_cnt and format
1360 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001361 * When using SLI-3 the driver will try to fit all the BDEs into
1362 * the IOCB. If it can't then the BDEs get added to a BPL as it
1363 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001364 */
James Smart34b02dc2008-08-24 21:49:55 -04001365 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001366 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001367 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001368 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001369 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001370 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1371 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1372 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1373 data_bde->addrLow = putPaddrLow(physaddr);
1374 data_bde->addrHigh = putPaddrHigh(physaddr);
1375 data_bde++;
1376 } else {
1377 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1378 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1379 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1380 bpl->addrLow =
1381 le32_to_cpu(putPaddrLow(physaddr));
1382 bpl->addrHigh =
1383 le32_to_cpu(putPaddrHigh(physaddr));
1384 bpl++;
1385 }
dea31012005-04-17 16:05:31 -05001386 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001387 }
dea31012005-04-17 16:05:31 -05001388
1389 /*
1390 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001391 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1392 * explicitly reinitialized and for SLI-3 the extended bde count is
1393 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001394 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001395 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001396 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1397 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001398 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1399 /*
1400 * The extended IOCB format can only fit 3 BDE or a BPL.
1401 * This I/O has more than 3 BDE so the 1st data bde will
1402 * be a BPL that is filled in here.
1403 */
1404 physaddr = lpfc_cmd->dma_handle;
1405 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1406 data_bde->tus.f.bdeSize = (num_bde *
1407 sizeof(struct ulp_bde64));
1408 physaddr += (sizeof(struct fcp_cmnd) +
1409 sizeof(struct fcp_rsp) +
1410 (2 * sizeof(struct ulp_bde64)));
1411 data_bde->addrHigh = putPaddrHigh(physaddr);
1412 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001413 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001414 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1415 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001416 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001417 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1418 }
1419 } else {
1420 iocb_cmd->un.fcpi64.bdl.bdeSize =
1421 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001422 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001423 }
James Smart09372822008-01-11 01:52:54 -05001424 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001425
1426 /*
1427 * Due to difference in data length between DIF/non-DIF paths,
1428 * we need to set word 4 of IOCB here
1429 */
James Smarta257bf92009-04-06 18:48:10 -04001430 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001431 return 0;
1432}
1433
James Smartf9bb2da2011-10-10 21:34:11 -04001434#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001435
James Smart9a6b09c2012-03-01 22:37:42 -05001436/* Return if if error injection is detected by Initiator */
1437#define BG_ERR_INIT 0x1
1438/* Return if if error injection is detected by Target */
1439#define BG_ERR_TGT 0x2
1440/* Return if if swapping CSUM<-->CRC is required for error injection */
1441#define BG_ERR_SWAP 0x10
1442/* Return if disabling Guard/Ref/App checking is required for error injection */
1443#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001444
1445/**
1446 * lpfc_bg_err_inject - Determine if we should inject an error
1447 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001448 * @sc: The SCSI command to examine
1449 * @reftag: (out) BlockGuard reference tag for transmitted data
1450 * @apptag: (out) BlockGuard application tag for transmitted data
1451 * @new_guard (in) Value to replace CRC with if needed
1452 *
James Smart9a6b09c2012-03-01 22:37:42 -05001453 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001454 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001455static int
1456lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1457 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1458{
1459 struct scatterlist *sgpe; /* s/g prot entry */
1460 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001461 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001462 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001463 struct lpfc_nodelist *ndlp;
1464 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001465 uint32_t op = scsi_get_prot_op(sc);
1466 uint32_t blksize;
1467 uint32_t numblks;
1468 sector_t lba;
1469 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001470 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001471
1472 if (op == SCSI_PROT_NORMAL)
1473 return 0;
1474
James Smartacd68592012-01-18 16:25:09 -05001475 sgpe = scsi_prot_sglist(sc);
1476 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001477 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001478
1479 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001480 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1481 blksize = lpfc_cmd_blksize(sc);
1482 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1483
1484 /* Make sure we have the right LBA if one is specified */
1485 if ((phba->lpfc_injerr_lba < lba) ||
1486 (phba->lpfc_injerr_lba >= (lba + numblks)))
1487 return 0;
James Smartacd68592012-01-18 16:25:09 -05001488 if (sgpe) {
1489 blockoff = phba->lpfc_injerr_lba - lba;
1490 numblks = sg_dma_len(sgpe) /
1491 sizeof(struct scsi_dif_tuple);
1492 if (numblks < blockoff)
1493 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001494 }
James Smartf9bb2da2011-10-10 21:34:11 -04001495 }
1496
James Smart4ac9b222012-03-01 22:38:29 -05001497 /* Next check if we need to match the remote NPortID or WWPN */
1498 rdata = sc->device->hostdata;
1499 if (rdata && rdata->pnode) {
1500 ndlp = rdata->pnode;
1501
1502 /* Make sure we have the right NPortID if one is specified */
1503 if (phba->lpfc_injerr_nportid &&
1504 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1505 return 0;
1506
1507 /*
1508 * Make sure we have the right WWPN if one is specified.
1509 * wwn[0] should be a non-zero NAA in a good WWPN.
1510 */
1511 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1512 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1513 sizeof(struct lpfc_name)) != 0))
1514 return 0;
1515 }
1516
1517 /* Setup a ptr to the protection data if the SCSI host provides it */
1518 if (sgpe) {
1519 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1520 src += blockoff;
1521 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1522 }
1523
James Smartf9bb2da2011-10-10 21:34:11 -04001524 /* Should we change the Reference Tag */
1525 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001526 if (phba->lpfc_injerr_wref_cnt) {
1527 switch (op) {
1528 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001529 if (src) {
1530 /*
1531 * For WRITE_PASS, force the error
1532 * to be sent on the wire. It should
1533 * be detected by the Target.
1534 * If blockoff != 0 error will be
1535 * inserted in middle of the IO.
1536 */
James Smartf9bb2da2011-10-10 21:34:11 -04001537
James Smartacd68592012-01-18 16:25:09 -05001538 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1539 "9076 BLKGRD: Injecting reftag error: "
1540 "write lba x%lx + x%x oldrefTag x%x\n",
1541 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001542 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001543
1544 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001545 * Save the old ref_tag so we can
1546 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001547 */
James Smart9a6b09c2012-03-01 22:37:42 -05001548 if (lpfc_cmd) {
1549 lpfc_cmd->prot_data_type =
1550 LPFC_INJERR_REFTAG;
1551 lpfc_cmd->prot_data_segment =
1552 src;
1553 lpfc_cmd->prot_data =
1554 src->ref_tag;
1555 }
1556 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001557 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001558 if (phba->lpfc_injerr_wref_cnt == 0) {
1559 phba->lpfc_injerr_nportid = 0;
1560 phba->lpfc_injerr_lba =
1561 LPFC_INJERR_LBA_OFF;
1562 memset(&phba->lpfc_injerr_wwpn,
1563 0, sizeof(struct lpfc_name));
1564 }
James Smart9a6b09c2012-03-01 22:37:42 -05001565 rc = BG_ERR_TGT | BG_ERR_CHECK;
1566
James Smartacd68592012-01-18 16:25:09 -05001567 break;
1568 }
1569 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001570 case SCSI_PROT_WRITE_INSERT:
1571 /*
1572 * For WRITE_INSERT, force the error
1573 * to be sent on the wire. It should be
1574 * detected by the Target.
1575 */
1576 /* DEADBEEF will be the reftag on the wire */
1577 *reftag = 0xDEADBEEF;
1578 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001579 if (phba->lpfc_injerr_wref_cnt == 0) {
1580 phba->lpfc_injerr_nportid = 0;
1581 phba->lpfc_injerr_lba =
1582 LPFC_INJERR_LBA_OFF;
1583 memset(&phba->lpfc_injerr_wwpn,
1584 0, sizeof(struct lpfc_name));
1585 }
James Smart9a6b09c2012-03-01 22:37:42 -05001586 rc = BG_ERR_TGT | BG_ERR_CHECK;
1587
1588 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1589 "9078 BLKGRD: Injecting reftag error: "
1590 "write lba x%lx\n", (unsigned long)lba);
1591 break;
James Smartacd68592012-01-18 16:25:09 -05001592 case SCSI_PROT_WRITE_STRIP:
1593 /*
1594 * For WRITE_STRIP and WRITE_PASS,
1595 * force the error on data
1596 * being copied from SLI-Host to SLI-Port.
1597 */
1598 *reftag = 0xDEADBEEF;
1599 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001600 if (phba->lpfc_injerr_wref_cnt == 0) {
1601 phba->lpfc_injerr_nportid = 0;
1602 phba->lpfc_injerr_lba =
1603 LPFC_INJERR_LBA_OFF;
1604 memset(&phba->lpfc_injerr_wwpn,
1605 0, sizeof(struct lpfc_name));
1606 }
James Smartacd68592012-01-18 16:25:09 -05001607 rc = BG_ERR_INIT;
1608
1609 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1610 "9077 BLKGRD: Injecting reftag error: "
1611 "write lba x%lx\n", (unsigned long)lba);
1612 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001613 }
James Smartacd68592012-01-18 16:25:09 -05001614 }
1615 if (phba->lpfc_injerr_rref_cnt) {
1616 switch (op) {
1617 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001618 case SCSI_PROT_READ_STRIP:
1619 case SCSI_PROT_READ_PASS:
1620 /*
1621 * For READ_STRIP and READ_PASS, force the
1622 * error on data being read off the wire. It
1623 * should force an IO error to the driver.
1624 */
James Smartf9bb2da2011-10-10 21:34:11 -04001625 *reftag = 0xDEADBEEF;
1626 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001627 if (phba->lpfc_injerr_rref_cnt == 0) {
1628 phba->lpfc_injerr_nportid = 0;
1629 phba->lpfc_injerr_lba =
1630 LPFC_INJERR_LBA_OFF;
1631 memset(&phba->lpfc_injerr_wwpn,
1632 0, sizeof(struct lpfc_name));
1633 }
James Smartacd68592012-01-18 16:25:09 -05001634 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001635
1636 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001637 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001638 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001639 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001640 }
1641 }
1642 }
1643
1644 /* Should we change the Application Tag */
1645 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001646 if (phba->lpfc_injerr_wapp_cnt) {
1647 switch (op) {
1648 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001649 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001650 /*
1651 * For WRITE_PASS, force the error
1652 * to be sent on the wire. It should
1653 * be detected by the Target.
1654 * If blockoff != 0 error will be
1655 * inserted in middle of the IO.
1656 */
1657
James Smartacd68592012-01-18 16:25:09 -05001658 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1659 "9080 BLKGRD: Injecting apptag error: "
1660 "write lba x%lx + x%x oldappTag x%x\n",
1661 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001662 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001663
1664 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001665 * Save the old app_tag so we can
1666 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001667 */
James Smart9a6b09c2012-03-01 22:37:42 -05001668 if (lpfc_cmd) {
1669 lpfc_cmd->prot_data_type =
1670 LPFC_INJERR_APPTAG;
1671 lpfc_cmd->prot_data_segment =
1672 src;
1673 lpfc_cmd->prot_data =
1674 src->app_tag;
1675 }
1676 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001677 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001678 if (phba->lpfc_injerr_wapp_cnt == 0) {
1679 phba->lpfc_injerr_nportid = 0;
1680 phba->lpfc_injerr_lba =
1681 LPFC_INJERR_LBA_OFF;
1682 memset(&phba->lpfc_injerr_wwpn,
1683 0, sizeof(struct lpfc_name));
1684 }
James Smart9a6b09c2012-03-01 22:37:42 -05001685 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001686 break;
1687 }
1688 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001689 case SCSI_PROT_WRITE_INSERT:
1690 /*
1691 * For WRITE_INSERT, force the
1692 * error to be sent on the wire. It should be
1693 * detected by the Target.
1694 */
1695 /* DEAD will be the apptag on the wire */
1696 *apptag = 0xDEAD;
1697 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001698 if (phba->lpfc_injerr_wapp_cnt == 0) {
1699 phba->lpfc_injerr_nportid = 0;
1700 phba->lpfc_injerr_lba =
1701 LPFC_INJERR_LBA_OFF;
1702 memset(&phba->lpfc_injerr_wwpn,
1703 0, sizeof(struct lpfc_name));
1704 }
James Smart9a6b09c2012-03-01 22:37:42 -05001705 rc = BG_ERR_TGT | BG_ERR_CHECK;
1706
1707 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1708 "0813 BLKGRD: Injecting apptag error: "
1709 "write lba x%lx\n", (unsigned long)lba);
1710 break;
James Smartacd68592012-01-18 16:25:09 -05001711 case SCSI_PROT_WRITE_STRIP:
1712 /*
1713 * For WRITE_STRIP and WRITE_PASS,
1714 * force the error on data
1715 * being copied from SLI-Host to SLI-Port.
1716 */
1717 *apptag = 0xDEAD;
1718 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001719 if (phba->lpfc_injerr_wapp_cnt == 0) {
1720 phba->lpfc_injerr_nportid = 0;
1721 phba->lpfc_injerr_lba =
1722 LPFC_INJERR_LBA_OFF;
1723 memset(&phba->lpfc_injerr_wwpn,
1724 0, sizeof(struct lpfc_name));
1725 }
James Smartacd68592012-01-18 16:25:09 -05001726 rc = BG_ERR_INIT;
1727
1728 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1729 "0812 BLKGRD: Injecting apptag error: "
1730 "write lba x%lx\n", (unsigned long)lba);
1731 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001732 }
James Smartacd68592012-01-18 16:25:09 -05001733 }
1734 if (phba->lpfc_injerr_rapp_cnt) {
1735 switch (op) {
1736 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001737 case SCSI_PROT_READ_STRIP:
1738 case SCSI_PROT_READ_PASS:
1739 /*
1740 * For READ_STRIP and READ_PASS, force the
1741 * error on data being read off the wire. It
1742 * should force an IO error to the driver.
1743 */
James Smartf9bb2da2011-10-10 21:34:11 -04001744 *apptag = 0xDEAD;
1745 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001746 if (phba->lpfc_injerr_rapp_cnt == 0) {
1747 phba->lpfc_injerr_nportid = 0;
1748 phba->lpfc_injerr_lba =
1749 LPFC_INJERR_LBA_OFF;
1750 memset(&phba->lpfc_injerr_wwpn,
1751 0, sizeof(struct lpfc_name));
1752 }
James Smartacd68592012-01-18 16:25:09 -05001753 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001754
1755 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001756 "0814 BLKGRD: Injecting apptag error: "
1757 "read lba x%lx\n", (unsigned long)lba);
1758 break;
1759 }
1760 }
1761 }
1762
1763
1764 /* Should we change the Guard Tag */
1765 if (new_guard) {
1766 if (phba->lpfc_injerr_wgrd_cnt) {
1767 switch (op) {
1768 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001769 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001770 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001771
James Smartacd68592012-01-18 16:25:09 -05001772 case SCSI_PROT_WRITE_INSERT:
1773 /*
1774 * For WRITE_INSERT, force the
1775 * error to be sent on the wire. It should be
1776 * detected by the Target.
1777 */
1778 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001779 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1780 phba->lpfc_injerr_nportid = 0;
1781 phba->lpfc_injerr_lba =
1782 LPFC_INJERR_LBA_OFF;
1783 memset(&phba->lpfc_injerr_wwpn,
1784 0, sizeof(struct lpfc_name));
1785 }
James Smartacd68592012-01-18 16:25:09 -05001786
James Smart9a6b09c2012-03-01 22:37:42 -05001787 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001788 /* Signals the caller to swap CRC->CSUM */
1789
1790 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1791 "0817 BLKGRD: Injecting guard error: "
1792 "write lba x%lx\n", (unsigned long)lba);
1793 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001794 case SCSI_PROT_WRITE_STRIP:
1795 /*
1796 * For WRITE_STRIP and WRITE_PASS,
1797 * force the error on data
1798 * being copied from SLI-Host to SLI-Port.
1799 */
1800 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001801 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1802 phba->lpfc_injerr_nportid = 0;
1803 phba->lpfc_injerr_lba =
1804 LPFC_INJERR_LBA_OFF;
1805 memset(&phba->lpfc_injerr_wwpn,
1806 0, sizeof(struct lpfc_name));
1807 }
James Smart9a6b09c2012-03-01 22:37:42 -05001808
1809 rc = BG_ERR_INIT | BG_ERR_SWAP;
1810 /* Signals the caller to swap CRC->CSUM */
1811
1812 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1813 "0816 BLKGRD: Injecting guard error: "
1814 "write lba x%lx\n", (unsigned long)lba);
1815 break;
James Smartacd68592012-01-18 16:25:09 -05001816 }
1817 }
1818 if (phba->lpfc_injerr_rgrd_cnt) {
1819 switch (op) {
1820 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001821 case SCSI_PROT_READ_STRIP:
1822 case SCSI_PROT_READ_PASS:
1823 /*
1824 * For READ_STRIP and READ_PASS, force the
1825 * error on data being read off the wire. It
1826 * should force an IO error to the driver.
1827 */
James Smartacd68592012-01-18 16:25:09 -05001828 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001829 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1830 phba->lpfc_injerr_nportid = 0;
1831 phba->lpfc_injerr_lba =
1832 LPFC_INJERR_LBA_OFF;
1833 memset(&phba->lpfc_injerr_wwpn,
1834 0, sizeof(struct lpfc_name));
1835 }
James Smartacd68592012-01-18 16:25:09 -05001836
James Smart9a6b09c2012-03-01 22:37:42 -05001837 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001838 /* Signals the caller to swap CRC->CSUM */
1839
1840 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1841 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001842 "read lba x%lx\n", (unsigned long)lba);
1843 }
1844 }
1845 }
1846
James Smartf9bb2da2011-10-10 21:34:11 -04001847 return rc;
1848}
1849#endif
1850
James Smartacd68592012-01-18 16:25:09 -05001851/**
1852 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1853 * the specified SCSI command.
1854 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001855 * @sc: The SCSI command to examine
1856 * @txopt: (out) BlockGuard operation for transmitted data
1857 * @rxopt: (out) BlockGuard operation for received data
1858 *
1859 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1860 *
James Smartacd68592012-01-18 16:25:09 -05001861 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001862static int
James Smart6c8eea52010-04-06 14:49:53 -04001863lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1864 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001865{
James Smart6c8eea52010-04-06 14:49:53 -04001866 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001867
James Smart9c6aa9d2013-05-31 17:03:39 -04001868 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001869 switch (scsi_get_prot_op(sc)) {
1870 case SCSI_PROT_READ_INSERT:
1871 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001872 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001873 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001874 break;
1875
1876 case SCSI_PROT_READ_STRIP:
1877 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001878 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001879 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001880 break;
1881
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001882 case SCSI_PROT_READ_PASS:
1883 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001884 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001885 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001886 break;
1887
James Smarte2a0a9d2008-12-04 22:40:02 -05001888 case SCSI_PROT_NORMAL:
1889 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001890 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001891 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1892 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001893 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001894 break;
1895
1896 }
James Smart7c56b9f2011-07-22 18:36:25 -04001897 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001898 switch (scsi_get_prot_op(sc)) {
1899 case SCSI_PROT_READ_STRIP:
1900 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001901 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001902 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001903 break;
1904
1905 case SCSI_PROT_READ_PASS:
1906 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001907 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001908 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001909 break;
1910
James Smarte2a0a9d2008-12-04 22:40:02 -05001911 case SCSI_PROT_READ_INSERT:
1912 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001913 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001914 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001915 break;
1916
James Smarte2a0a9d2008-12-04 22:40:02 -05001917 case SCSI_PROT_NORMAL:
1918 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001919 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001920 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1921 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001922 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001923 break;
1924 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001925 }
1926
James Smart6c8eea52010-04-06 14:49:53 -04001927 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001928}
1929
James Smartacd68592012-01-18 16:25:09 -05001930#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1931/**
1932 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1933 * the specified SCSI command in order to force a guard tag error.
1934 * @phba: The Hba for which this call is being executed.
1935 * @sc: The SCSI command to examine
1936 * @txopt: (out) BlockGuard operation for transmitted data
1937 * @rxopt: (out) BlockGuard operation for received data
1938 *
1939 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1940 *
1941 **/
1942static int
1943lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1944 uint8_t *txop, uint8_t *rxop)
1945{
James Smartacd68592012-01-18 16:25:09 -05001946 uint8_t ret = 0;
1947
James Smart9c6aa9d2013-05-31 17:03:39 -04001948 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001949 switch (scsi_get_prot_op(sc)) {
1950 case SCSI_PROT_READ_INSERT:
1951 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001952 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001953 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001954 break;
1955
1956 case SCSI_PROT_READ_STRIP:
1957 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001958 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001959 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001960 break;
1961
1962 case SCSI_PROT_READ_PASS:
1963 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001964 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001965 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001966 break;
1967
1968 case SCSI_PROT_NORMAL:
1969 default:
1970 break;
1971
1972 }
1973 } else {
1974 switch (scsi_get_prot_op(sc)) {
1975 case SCSI_PROT_READ_STRIP:
1976 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001977 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001978 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001979 break;
1980
1981 case SCSI_PROT_READ_PASS:
1982 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001983 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001984 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001985 break;
1986
1987 case SCSI_PROT_READ_INSERT:
1988 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001989 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001990 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001991 break;
1992
1993 case SCSI_PROT_NORMAL:
1994 default:
1995 break;
1996 }
1997 }
1998
1999 return ret;
2000}
2001#endif
2002
2003/**
2004 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
2005 * @phba: The Hba for which this call is being executed.
2006 * @sc: pointer to scsi command we're working on
2007 * @bpl: pointer to buffer list for protection groups
2008 * @datacnt: number of segments of data that have been dma mapped
2009 *
2010 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05002011 * type LPFC_PG_TYPE_NO_DIF
2012 *
2013 * This is usually used when the HBA is instructed to generate
2014 * DIFs and insert them into data stream (or strip DIF from
2015 * incoming data stream)
2016 *
2017 * The buffer list consists of just one protection group described
2018 * below:
2019 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002020 * start of prot group --> | PDE_5 |
2021 * +-------------------------+
2022 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002023 * +-------------------------+
2024 * | Data BDE |
2025 * +-------------------------+
2026 * |more Data BDE's ... (opt)|
2027 * +-------------------------+
2028 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002029 *
2030 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05002031 *
2032 * Returns the number of BDEs added to the BPL.
2033 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002034static int
2035lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2036 struct ulp_bde64 *bpl, int datasegcnt)
2037{
2038 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04002039 struct lpfc_pde5 *pde5 = NULL;
2040 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002041 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04002042 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002043 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04002044#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002045 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002046#endif
James Smartacd68592012-01-18 16:25:09 -05002047 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002048 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04002049 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04002050 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002051
James Smart6c8eea52010-04-06 14:49:53 -04002052 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2053 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002054 goto out;
2055
James Smart6c8eea52010-04-06 14:49:53 -04002056 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05002057 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002058 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002059
James Smartf9bb2da2011-10-10 21:34:11 -04002060#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002061 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002062 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002063 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002064 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002065 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002066 checking = 0;
2067 }
James Smartf9bb2da2011-10-10 21:34:11 -04002068#endif
2069
James Smart6c8eea52010-04-06 14:49:53 -04002070 /* setup PDE5 with what we have */
2071 pde5 = (struct lpfc_pde5 *) bpl;
2072 memset(pde5, 0, sizeof(struct lpfc_pde5));
2073 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002074
James Smartbc739052010-08-04 16:11:18 -04002075 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002076 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002077 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002078
James Smart6c8eea52010-04-06 14:49:53 -04002079 /* advance bpl and increment bde count */
2080 num_bde++;
2081 bpl++;
2082 pde6 = (struct lpfc_pde6 *) bpl;
2083
2084 /* setup PDE6 with the rest of the info */
2085 memset(pde6, 0, sizeof(struct lpfc_pde6));
2086 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2087 bf_set(pde6_optx, pde6, txop);
2088 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002089
2090 /*
2091 * We only need to check the data on READs, for WRITEs
2092 * protection data is automatically generated, not checked.
2093 */
James Smart6c8eea52010-04-06 14:49:53 -04002094 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002095 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002096 bf_set(pde6_ce, pde6, checking);
2097 else
2098 bf_set(pde6_ce, pde6, 0);
2099
James Smart9c6aa9d2013-05-31 17:03:39 -04002100 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002101 bf_set(pde6_re, pde6, checking);
2102 else
2103 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002104 }
2105 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002106 bf_set(pde6_ae, pde6, 0);
2107 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002108
James Smartbc739052010-08-04 16:11:18 -04002109 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002110 pde6->word0 = cpu_to_le32(pde6->word0);
2111 pde6->word1 = cpu_to_le32(pde6->word1);
2112 pde6->word2 = cpu_to_le32(pde6->word2);
2113
James Smart6c8eea52010-04-06 14:49:53 -04002114 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002115 num_bde++;
2116 bpl++;
2117
2118 /* assumption: caller has already run dma_map_sg on command data */
2119 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2120 physaddr = sg_dma_address(sgde);
2121 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2122 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2123 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2124 if (datadir == DMA_TO_DEVICE)
2125 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2126 else
2127 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2128 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2129 bpl++;
2130 num_bde++;
2131 }
2132
2133out:
2134 return num_bde;
2135}
2136
James Smartacd68592012-01-18 16:25:09 -05002137/**
2138 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2139 * @phba: The Hba for which this call is being executed.
2140 * @sc: pointer to scsi command we're working on
2141 * @bpl: pointer to buffer list for protection groups
2142 * @datacnt: number of segments of data that have been dma mapped
2143 * @protcnt: number of segment of protection data that have been dma mapped
2144 *
2145 * This function sets up BPL buffer list for protection groups of
2146 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002147 *
2148 * This is usually used when DIFs are in their own buffers,
2149 * separate from the data. The HBA can then by instructed
2150 * to place the DIFs in the outgoing stream. For read operations,
2151 * The HBA could extract the DIFs and place it in DIF buffers.
2152 *
2153 * The buffer list for this type consists of one or more of the
2154 * protection groups described below:
2155 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002156 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002157 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002158 * | PDE_6 |
2159 * +-------------------------+
2160 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002161 * +-------------------------+
2162 * | Data BDE |
2163 * +-------------------------+
2164 * |more Data BDE's ... (opt)|
2165 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002166 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002167 * +-------------------------+
2168 * | ... |
2169 * +-------------------------+
2170 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002171 * Note: It is assumed that both data and protection s/g buffers have been
2172 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002173 *
2174 * Returns the number of BDEs added to the BPL.
2175 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002176static int
2177lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2178 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2179{
2180 struct scatterlist *sgde = NULL; /* s/g data entry */
2181 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002182 struct lpfc_pde5 *pde5 = NULL;
2183 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002184 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002185 dma_addr_t dataphysaddr, protphysaddr;
2186 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002187 unsigned int split_offset;
2188 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002189 unsigned int protgrp_blks, protgrp_bytes;
2190 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002191 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002192 int datadir = sc->sc_data_direction;
2193 unsigned char pgdone = 0, alldone = 0;
2194 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002195#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002196 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002197#endif
James Smartacd68592012-01-18 16:25:09 -05002198 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002199 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002200 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002201 int num_bde = 0;
2202
2203 sgpe = scsi_prot_sglist(sc);
2204 sgde = scsi_sglist(sc);
2205
2206 if (!sgpe || !sgde) {
2207 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2208 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2209 sgpe, sgde);
2210 return 0;
2211 }
2212
James Smart6c8eea52010-04-06 14:49:53 -04002213 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2214 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002215 goto out;
2216
James Smart6c8eea52010-04-06 14:49:53 -04002217 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002218 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002219 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002220
James Smartf9bb2da2011-10-10 21:34:11 -04002221#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002222 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002223 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002224 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002225 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002226 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002227 checking = 0;
2228 }
James Smartf9bb2da2011-10-10 21:34:11 -04002229#endif
2230
James Smarte2a0a9d2008-12-04 22:40:02 -05002231 split_offset = 0;
2232 do {
James Smart96f70772013-04-17 20:16:15 -04002233 /* Check to see if we ran out of space */
2234 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2235 return num_bde + 3;
2236
James Smart6c8eea52010-04-06 14:49:53 -04002237 /* setup PDE5 with what we have */
2238 pde5 = (struct lpfc_pde5 *) bpl;
2239 memset(pde5, 0, sizeof(struct lpfc_pde5));
2240 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002241
James Smartbc739052010-08-04 16:11:18 -04002242 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002243 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002244 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002245
James Smart6c8eea52010-04-06 14:49:53 -04002246 /* advance bpl and increment bde count */
2247 num_bde++;
2248 bpl++;
2249 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002250
James Smart6c8eea52010-04-06 14:49:53 -04002251 /* setup PDE6 with the rest of the info */
2252 memset(pde6, 0, sizeof(struct lpfc_pde6));
2253 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2254 bf_set(pde6_optx, pde6, txop);
2255 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002256
James Smart9c6aa9d2013-05-31 17:03:39 -04002257 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002258 bf_set(pde6_ce, pde6, checking);
2259 else
2260 bf_set(pde6_ce, pde6, 0);
2261
James Smart9c6aa9d2013-05-31 17:03:39 -04002262 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002263 bf_set(pde6_re, pde6, checking);
2264 else
2265 bf_set(pde6_re, pde6, 0);
2266
James Smart6c8eea52010-04-06 14:49:53 -04002267 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002268 bf_set(pde6_ae, pde6, 0);
2269 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002270
James Smartbc739052010-08-04 16:11:18 -04002271 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002272 pde6->word0 = cpu_to_le32(pde6->word0);
2273 pde6->word1 = cpu_to_le32(pde6->word1);
2274 pde6->word2 = cpu_to_le32(pde6->word2);
2275
James Smart6c8eea52010-04-06 14:49:53 -04002276 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002277 num_bde++;
2278 bpl++;
2279
2280 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002281 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2282 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002283
James Smarte2a0a9d2008-12-04 22:40:02 -05002284 /* must be integer multiple of the DIF block length */
2285 BUG_ON(protgroup_len % 8);
2286
James Smart7f860592011-03-11 16:05:52 -05002287 pde7 = (struct lpfc_pde7 *) bpl;
2288 memset(pde7, 0, sizeof(struct lpfc_pde7));
2289 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2290
James Smart7c56b9f2011-07-22 18:36:25 -04002291 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2292 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002293
James Smarte2a0a9d2008-12-04 22:40:02 -05002294 protgrp_blks = protgroup_len / 8;
2295 protgrp_bytes = protgrp_blks * blksize;
2296
James Smart7f860592011-03-11 16:05:52 -05002297 /* check if this pde is crossing the 4K boundary; if so split */
2298 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2299 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2300 protgroup_offset += protgroup_remainder;
2301 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002302 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002303 } else {
2304 protgroup_offset = 0;
2305 curr_prot++;
2306 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002307
James Smarte2a0a9d2008-12-04 22:40:02 -05002308 num_bde++;
2309
2310 /* setup BDE's for data blocks associated with DIF data */
2311 pgdone = 0;
2312 subtotal = 0; /* total bytes processed for current prot grp */
2313 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002314 /* Check to see if we ran out of space */
2315 if (num_bde >= phba->cfg_total_seg_cnt)
2316 return num_bde + 1;
2317
James Smarte2a0a9d2008-12-04 22:40:02 -05002318 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002319 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2320 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002321 __func__);
2322 return 0;
2323 }
2324 bpl++;
2325 dataphysaddr = sg_dma_address(sgde) + split_offset;
2326 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2327 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2328
2329 remainder = sg_dma_len(sgde) - split_offset;
2330
2331 if ((subtotal + remainder) <= protgrp_bytes) {
2332 /* we can use this whole buffer */
2333 bpl->tus.f.bdeSize = remainder;
2334 split_offset = 0;
2335
2336 if ((subtotal + remainder) == protgrp_bytes)
2337 pgdone = 1;
2338 } else {
2339 /* must split this buffer with next prot grp */
2340 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2341 split_offset += bpl->tus.f.bdeSize;
2342 }
2343
2344 subtotal += bpl->tus.f.bdeSize;
2345
2346 if (datadir == DMA_TO_DEVICE)
2347 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2348 else
2349 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2350 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2351
2352 num_bde++;
2353 curr_data++;
2354
2355 if (split_offset)
2356 break;
2357
2358 /* Move to the next s/g segment if possible */
2359 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002360
James Smarte2a0a9d2008-12-04 22:40:02 -05002361 }
2362
James Smart7f860592011-03-11 16:05:52 -05002363 if (protgroup_offset) {
2364 /* update the reference tag */
2365 reftag += protgrp_blks;
2366 bpl++;
2367 continue;
2368 }
2369
James Smarte2a0a9d2008-12-04 22:40:02 -05002370 /* are we done ? */
2371 if (curr_prot == protcnt) {
2372 alldone = 1;
2373 } else if (curr_prot < protcnt) {
2374 /* advance to next prot buffer */
2375 sgpe = sg_next(sgpe);
2376 bpl++;
2377
2378 /* update the reference tag */
2379 reftag += protgrp_blks;
2380 } else {
2381 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002382 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2383 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002384 }
2385
2386 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002387out:
2388
James Smarte2a0a9d2008-12-04 22:40:02 -05002389 return num_bde;
2390}
James Smart7f860592011-03-11 16:05:52 -05002391
James Smartacd68592012-01-18 16:25:09 -05002392/**
2393 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2394 * @phba: The Hba for which this call is being executed.
2395 * @sc: pointer to scsi command we're working on
2396 * @sgl: pointer to buffer list for protection groups
2397 * @datacnt: number of segments of data that have been dma mapped
2398 *
2399 * This function sets up SGL buffer list for protection groups of
2400 * type LPFC_PG_TYPE_NO_DIF
2401 *
2402 * This is usually used when the HBA is instructed to generate
2403 * DIFs and insert them into data stream (or strip DIF from
2404 * incoming data stream)
2405 *
2406 * The buffer list consists of just one protection group described
2407 * below:
2408 * +-------------------------+
2409 * start of prot group --> | DI_SEED |
2410 * +-------------------------+
2411 * | Data SGE |
2412 * +-------------------------+
2413 * |more Data SGE's ... (opt)|
2414 * +-------------------------+
2415 *
2416 *
2417 * Note: Data s/g buffers have been dma mapped
2418 *
2419 * Returns the number of SGEs added to the SGL.
2420 **/
2421static int
2422lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2423 struct sli4_sge *sgl, int datasegcnt)
2424{
2425 struct scatterlist *sgde = NULL; /* s/g data entry */
2426 struct sli4_sge_diseed *diseed = NULL;
2427 dma_addr_t physaddr;
2428 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002429 uint32_t reftag;
2430 unsigned blksize;
2431 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002432#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002433 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002434#endif
James Smartacd68592012-01-18 16:25:09 -05002435 uint32_t checking = 1;
2436 uint32_t dma_len;
2437 uint32_t dma_offset = 0;
2438
2439 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2440 if (status)
2441 goto out;
2442
2443 /* extract some info from the scsi command for pde*/
2444 blksize = lpfc_cmd_blksize(sc);
2445 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2446
2447#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002448 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002449 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002450 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002451 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002452 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002453 checking = 0;
2454 }
2455#endif
2456
2457 /* setup DISEED with what we have */
2458 diseed = (struct sli4_sge_diseed *) sgl;
2459 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2460 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2461
2462 /* Endianness conversion if necessary */
2463 diseed->ref_tag = cpu_to_le32(reftag);
2464 diseed->ref_tag_tran = diseed->ref_tag;
2465
James Smarta6887e22013-04-17 20:18:07 -04002466 /*
2467 * We only need to check the data on READs, for WRITEs
2468 * protection data is automatically generated, not checked.
2469 */
2470 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002471 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002472 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2473 else
2474 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2475
James Smart9c6aa9d2013-05-31 17:03:39 -04002476 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002477 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2478 else
2479 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2480 }
2481
James Smartacd68592012-01-18 16:25:09 -05002482 /* setup DISEED with the rest of the info */
2483 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2484 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002485
James Smartacd68592012-01-18 16:25:09 -05002486 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2487 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2488
2489 /* Endianness conversion if necessary for DISEED */
2490 diseed->word2 = cpu_to_le32(diseed->word2);
2491 diseed->word3 = cpu_to_le32(diseed->word3);
2492
2493 /* advance bpl and increment sge count */
2494 num_sge++;
2495 sgl++;
2496
2497 /* assumption: caller has already run dma_map_sg on command data */
2498 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2499 physaddr = sg_dma_address(sgde);
2500 dma_len = sg_dma_len(sgde);
2501 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2502 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2503 if ((i + 1) == datasegcnt)
2504 bf_set(lpfc_sli4_sge_last, sgl, 1);
2505 else
2506 bf_set(lpfc_sli4_sge_last, sgl, 0);
2507 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2508 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2509
2510 sgl->sge_len = cpu_to_le32(dma_len);
2511 dma_offset += dma_len;
2512
2513 sgl++;
2514 num_sge++;
2515 }
2516
2517out:
2518 return num_sge;
2519}
2520
2521/**
2522 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2523 * @phba: The Hba for which this call is being executed.
2524 * @sc: pointer to scsi command we're working on
2525 * @sgl: pointer to buffer list for protection groups
2526 * @datacnt: number of segments of data that have been dma mapped
2527 * @protcnt: number of segment of protection data that have been dma mapped
2528 *
2529 * This function sets up SGL buffer list for protection groups of
2530 * type LPFC_PG_TYPE_DIF
2531 *
2532 * This is usually used when DIFs are in their own buffers,
2533 * separate from the data. The HBA can then by instructed
2534 * to place the DIFs in the outgoing stream. For read operations,
2535 * The HBA could extract the DIFs and place it in DIF buffers.
2536 *
2537 * The buffer list for this type consists of one or more of the
2538 * protection groups described below:
2539 * +-------------------------+
2540 * start of first prot group --> | DISEED |
2541 * +-------------------------+
2542 * | DIF (Prot SGE) |
2543 * +-------------------------+
2544 * | Data SGE |
2545 * +-------------------------+
2546 * |more Data SGE's ... (opt)|
2547 * +-------------------------+
2548 * start of new prot group --> | DISEED |
2549 * +-------------------------+
2550 * | ... |
2551 * +-------------------------+
2552 *
2553 * Note: It is assumed that both data and protection s/g buffers have been
2554 * mapped for DMA
2555 *
2556 * Returns the number of SGEs added to the SGL.
2557 **/
2558static int
2559lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2560 struct sli4_sge *sgl, int datacnt, int protcnt)
2561{
2562 struct scatterlist *sgde = NULL; /* s/g data entry */
2563 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2564 struct sli4_sge_diseed *diseed = NULL;
2565 dma_addr_t dataphysaddr, protphysaddr;
2566 unsigned short curr_data = 0, curr_prot = 0;
2567 unsigned int split_offset;
2568 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2569 unsigned int protgrp_blks, protgrp_bytes;
2570 unsigned int remainder, subtotal;
2571 int status;
2572 unsigned char pgdone = 0, alldone = 0;
2573 unsigned blksize;
2574 uint32_t reftag;
2575 uint8_t txop, rxop;
2576 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002577#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002578 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002579#endif
James Smartacd68592012-01-18 16:25:09 -05002580 uint32_t checking = 1;
2581 uint32_t dma_offset = 0;
2582 int num_sge = 0;
2583
2584 sgpe = scsi_prot_sglist(sc);
2585 sgde = scsi_sglist(sc);
2586
2587 if (!sgpe || !sgde) {
2588 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2589 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2590 sgpe, sgde);
2591 return 0;
2592 }
2593
2594 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2595 if (status)
2596 goto out;
2597
2598 /* extract some info from the scsi command */
2599 blksize = lpfc_cmd_blksize(sc);
2600 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2601
2602#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002603 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002604 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002605 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002606 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002607 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002608 checking = 0;
2609 }
2610#endif
2611
2612 split_offset = 0;
2613 do {
James Smart96f70772013-04-17 20:16:15 -04002614 /* Check to see if we ran out of space */
2615 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2616 return num_sge + 3;
2617
James Smartacd68592012-01-18 16:25:09 -05002618 /* setup DISEED with what we have */
2619 diseed = (struct sli4_sge_diseed *) sgl;
2620 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2621 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2622
2623 /* Endianness conversion if necessary */
2624 diseed->ref_tag = cpu_to_le32(reftag);
2625 diseed->ref_tag_tran = diseed->ref_tag;
2626
James Smart9c6aa9d2013-05-31 17:03:39 -04002627 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002628 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2629
2630 } else {
2631 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2632 /*
2633 * When in this mode, the hardware will replace
2634 * the guard tag from the host with a
2635 * newly generated good CRC for the wire.
2636 * Switch to raw mode here to avoid this
2637 * behavior. What the host sends gets put on the wire.
2638 */
2639 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2640 txop = BG_OP_RAW_MODE;
2641 rxop = BG_OP_RAW_MODE;
2642 }
2643 }
2644
2645
James Smart9c6aa9d2013-05-31 17:03:39 -04002646 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002647 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2648 else
2649 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2650
James Smartacd68592012-01-18 16:25:09 -05002651 /* setup DISEED with the rest of the info */
2652 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2653 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002654
James Smartacd68592012-01-18 16:25:09 -05002655 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2656 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2657
2658 /* Endianness conversion if necessary for DISEED */
2659 diseed->word2 = cpu_to_le32(diseed->word2);
2660 diseed->word3 = cpu_to_le32(diseed->word3);
2661
2662 /* advance sgl and increment bde count */
2663 num_sge++;
2664 sgl++;
2665
2666 /* setup the first BDE that points to protection buffer */
2667 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2668 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2669
2670 /* must be integer multiple of the DIF block length */
2671 BUG_ON(protgroup_len % 8);
2672
2673 /* Now setup DIF SGE */
2674 sgl->word2 = 0;
2675 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2676 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2677 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2678 sgl->word2 = cpu_to_le32(sgl->word2);
2679
2680 protgrp_blks = protgroup_len / 8;
2681 protgrp_bytes = protgrp_blks * blksize;
2682
2683 /* check if DIF SGE is crossing the 4K boundary; if so split */
2684 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2685 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2686 protgroup_offset += protgroup_remainder;
2687 protgrp_blks = protgroup_remainder / 8;
2688 protgrp_bytes = protgrp_blks * blksize;
2689 } else {
2690 protgroup_offset = 0;
2691 curr_prot++;
2692 }
2693
2694 num_sge++;
2695
2696 /* setup SGE's for data blocks associated with DIF data */
2697 pgdone = 0;
2698 subtotal = 0; /* total bytes processed for current prot grp */
2699 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002700 /* Check to see if we ran out of space */
2701 if (num_sge >= phba->cfg_total_seg_cnt)
2702 return num_sge + 1;
2703
James Smartacd68592012-01-18 16:25:09 -05002704 if (!sgde) {
2705 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2706 "9086 BLKGRD:%s Invalid data segment\n",
2707 __func__);
2708 return 0;
2709 }
2710 sgl++;
2711 dataphysaddr = sg_dma_address(sgde) + split_offset;
2712
2713 remainder = sg_dma_len(sgde) - split_offset;
2714
2715 if ((subtotal + remainder) <= protgrp_bytes) {
2716 /* we can use this whole buffer */
2717 dma_len = remainder;
2718 split_offset = 0;
2719
2720 if ((subtotal + remainder) == protgrp_bytes)
2721 pgdone = 1;
2722 } else {
2723 /* must split this buffer with next prot grp */
2724 dma_len = protgrp_bytes - subtotal;
2725 split_offset += dma_len;
2726 }
2727
2728 subtotal += dma_len;
2729
2730 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2731 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2732 bf_set(lpfc_sli4_sge_last, sgl, 0);
2733 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2734 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2735
2736 sgl->sge_len = cpu_to_le32(dma_len);
2737 dma_offset += dma_len;
2738
2739 num_sge++;
2740 curr_data++;
2741
2742 if (split_offset)
2743 break;
2744
2745 /* Move to the next s/g segment if possible */
2746 sgde = sg_next(sgde);
2747 }
2748
2749 if (protgroup_offset) {
2750 /* update the reference tag */
2751 reftag += protgrp_blks;
2752 sgl++;
2753 continue;
2754 }
2755
2756 /* are we done ? */
2757 if (curr_prot == protcnt) {
2758 bf_set(lpfc_sli4_sge_last, sgl, 1);
2759 alldone = 1;
2760 } else if (curr_prot < protcnt) {
2761 /* advance to next prot buffer */
2762 sgpe = sg_next(sgpe);
2763 sgl++;
2764
2765 /* update the reference tag */
2766 reftag += protgrp_blks;
2767 } else {
2768 /* if we're here, we have a bug */
2769 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2770 "9085 BLKGRD: bug in %s\n", __func__);
2771 }
2772
2773 } while (!alldone);
2774
2775out:
2776
2777 return num_sge;
2778}
2779
2780/**
2781 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2782 * @phba: The Hba for which this call is being executed.
2783 * @sc: pointer to scsi command we're working on
2784 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002785 * Given a SCSI command that supports DIF, determine composition of protection
2786 * groups involved in setting up buffer lists
2787 *
James Smartacd68592012-01-18 16:25:09 -05002788 * Returns: Protection group type (with or without DIF)
2789 *
2790 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002791static int
2792lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2793{
2794 int ret = LPFC_PG_TYPE_INVALID;
2795 unsigned char op = scsi_get_prot_op(sc);
2796
2797 switch (op) {
2798 case SCSI_PROT_READ_STRIP:
2799 case SCSI_PROT_WRITE_INSERT:
2800 ret = LPFC_PG_TYPE_NO_DIF;
2801 break;
2802 case SCSI_PROT_READ_INSERT:
2803 case SCSI_PROT_WRITE_STRIP:
2804 case SCSI_PROT_READ_PASS:
2805 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002806 ret = LPFC_PG_TYPE_DIF_BUF;
2807 break;
2808 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002809 if (phba)
2810 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2811 "9021 Unsupported protection op:%d\n",
2812 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002813 break;
2814 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002815 return ret;
2816}
2817
James Smartacd68592012-01-18 16:25:09 -05002818/**
James Smarta6887e22013-04-17 20:18:07 -04002819 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2820 * @phba: The Hba for which this call is being executed.
2821 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2822 *
2823 * Adjust the data length to account for how much data
2824 * is actually on the wire.
2825 *
2826 * returns the adjusted data length
2827 **/
2828static int
2829lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2830 struct lpfc_scsi_buf *lpfc_cmd)
2831{
2832 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2833 int fcpdl;
2834
2835 fcpdl = scsi_bufflen(sc);
2836
2837 /* Check if there is protection data on the wire */
2838 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002839 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002840 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2841 return fcpdl;
2842
2843 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002844 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002845 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2846 return fcpdl;
2847 }
2848
2849 /*
2850 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002851 * DIF (trailer) attached to it. Must ajust FCP data length
2852 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002853 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002854 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002855
2856 return fcpdl;
2857}
2858
2859/**
James Smartacd68592012-01-18 16:25:09 -05002860 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2861 * @phba: The Hba for which this call is being executed.
2862 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2863 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002864 * This is the protection/DIF aware version of
2865 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2866 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002867 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002868static int
James Smartacd68592012-01-18 16:25:09 -05002869lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002870 struct lpfc_scsi_buf *lpfc_cmd)
2871{
2872 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2873 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2874 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2875 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2876 uint32_t num_bde = 0;
2877 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2878 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002879 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002880
2881 /*
2882 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2883 * fcp_rsp regions to the first data bde entry
2884 */
2885 bpl += 2;
2886 if (scsi_sg_count(scsi_cmnd)) {
2887 /*
2888 * The driver stores the segment count returned from pci_map_sg
2889 * because this a count of dma-mappings used to map the use_sg
2890 * pages. They are not guaranteed to be the same for those
2891 * architectures that implement an IOMMU.
2892 */
2893 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2894 scsi_sglist(scsi_cmnd),
2895 scsi_sg_count(scsi_cmnd), datadir);
2896 if (unlikely(!datasegcnt))
2897 return 1;
2898
2899 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002900
2901 /* First check if data segment count from SCSI Layer is good */
2902 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2903 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002904
2905 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2906
2907 switch (prot_group_type) {
2908 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002909
2910 /* Here we need to add a PDE5 and PDE6 to the count */
2911 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2912 goto err;
2913
James Smarte2a0a9d2008-12-04 22:40:02 -05002914 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2915 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002916 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002917 if (num_bde < 2)
2918 goto err;
2919 break;
James Smart96f70772013-04-17 20:16:15 -04002920
2921 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002922 /*
2923 * This type indicates that protection buffers are
2924 * passed to the driver, so that needs to be prepared
2925 * for DMA
2926 */
2927 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2928 scsi_prot_sglist(scsi_cmnd),
2929 scsi_prot_sg_count(scsi_cmnd), datadir);
2930 if (unlikely(!protsegcnt)) {
2931 scsi_dma_unmap(scsi_cmnd);
2932 return 1;
2933 }
2934
2935 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002936
2937 /*
2938 * There is a minimun of 4 BPLs used for every
2939 * protection data segment.
2940 */
2941 if ((lpfc_cmd->prot_seg_cnt * 4) >
2942 (phba->cfg_total_seg_cnt - 2))
2943 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002944
2945 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2946 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002947 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002948 if ((num_bde < 3) ||
2949 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002950 goto err;
2951 break;
James Smart96f70772013-04-17 20:16:15 -04002952
James Smarte2a0a9d2008-12-04 22:40:02 -05002953 case LPFC_PG_TYPE_INVALID:
2954 default:
James Smart96f70772013-04-17 20:16:15 -04002955 scsi_dma_unmap(scsi_cmnd);
2956 lpfc_cmd->seg_cnt = 0;
2957
James Smarte2a0a9d2008-12-04 22:40:02 -05002958 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2959 "9022 Unexpected protection group %i\n",
2960 prot_group_type);
2961 return 1;
2962 }
2963 }
2964
2965 /*
2966 * Finish initializing those IOCB fields that are dependent on the
2967 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2968 * reinitialized since all iocb memory resources are used many times
2969 * for transmit, receive, and continuation bpl's.
2970 */
2971 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2972 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2973 iocb_cmd->ulpBdeCount = 1;
2974 iocb_cmd->ulpLe = 1;
2975
James Smarta6887e22013-04-17 20:18:07 -04002976 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002977 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2978
2979 /*
2980 * Due to difference in data length between DIF/non-DIF paths,
2981 * we need to set word 4 of IOCB here
2982 */
2983 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2984
2985 return 0;
2986err:
James Smart96f70772013-04-17 20:16:15 -04002987 if (lpfc_cmd->seg_cnt)
2988 scsi_dma_unmap(scsi_cmnd);
2989 if (lpfc_cmd->prot_seg_cnt)
2990 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2991 scsi_prot_sg_count(scsi_cmnd),
2992 scsi_cmnd->sc_data_direction);
2993
James Smarte2a0a9d2008-12-04 22:40:02 -05002994 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002995 "9023 Cannot setup S/G List for HBA"
2996 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2997 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2998 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05002999 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04003000
3001 lpfc_cmd->seg_cnt = 0;
3002 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05003003 return 1;
3004}
3005
3006/*
James Smart737d4242013-04-17 20:14:49 -04003007 * This function calcuates the T10 DIF guard tag
3008 * on the specified data using a CRC algorithmn
3009 * using crc_t10dif.
3010 */
3011uint16_t
3012lpfc_bg_crc(uint8_t *data, int count)
3013{
3014 uint16_t crc = 0;
3015 uint16_t x;
3016
3017 crc = crc_t10dif(data, count);
3018 x = cpu_to_be16(crc);
3019 return x;
3020}
3021
3022/*
3023 * This function calcuates the T10 DIF guard tag
3024 * on the specified data using a CSUM algorithmn
3025 * using ip_compute_csum.
3026 */
3027uint16_t
3028lpfc_bg_csum(uint8_t *data, int count)
3029{
3030 uint16_t ret;
3031
3032 ret = ip_compute_csum(data, count);
3033 return ret;
3034}
3035
3036/*
3037 * This function examines the protection data to try to determine
3038 * what type of T10-DIF error occurred.
3039 */
3040void
3041lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3042{
3043 struct scatterlist *sgpe; /* s/g prot entry */
3044 struct scatterlist *sgde; /* s/g data entry */
3045 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3046 struct scsi_dif_tuple *src = NULL;
3047 uint8_t *data_src = NULL;
3048 uint16_t guard_tag, guard_type;
3049 uint16_t start_app_tag, app_tag;
3050 uint32_t start_ref_tag, ref_tag;
3051 int prot, protsegcnt;
3052 int err_type, len, data_len;
3053 int chk_ref, chk_app, chk_guard;
3054 uint16_t sum;
3055 unsigned blksize;
3056
3057 err_type = BGS_GUARD_ERR_MASK;
3058 sum = 0;
3059 guard_tag = 0;
3060
3061 /* First check to see if there is protection data to examine */
3062 prot = scsi_get_prot_op(cmd);
3063 if ((prot == SCSI_PROT_READ_STRIP) ||
3064 (prot == SCSI_PROT_WRITE_INSERT) ||
3065 (prot == SCSI_PROT_NORMAL))
3066 goto out;
3067
3068 /* Currently the driver just supports ref_tag and guard_tag checking */
3069 chk_ref = 1;
3070 chk_app = 0;
3071 chk_guard = 0;
3072
3073 /* Setup a ptr to the protection data provided by the SCSI host */
3074 sgpe = scsi_prot_sglist(cmd);
3075 protsegcnt = lpfc_cmd->prot_seg_cnt;
3076
3077 if (sgpe && protsegcnt) {
3078
3079 /*
3080 * We will only try to verify guard tag if the segment
3081 * data length is a multiple of the blksize.
3082 */
3083 sgde = scsi_sglist(cmd);
3084 blksize = lpfc_cmd_blksize(cmd);
3085 data_src = (uint8_t *)sg_virt(sgde);
3086 data_len = sgde->length;
3087 if ((data_len & (blksize - 1)) == 0)
3088 chk_guard = 1;
3089 guard_type = scsi_host_get_guard(cmd->device->host);
3090
James Smarte85d8f92013-05-31 17:04:10 -04003091 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04003092 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04003093 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04003094 len = sgpe->length;
3095 while (src && protsegcnt) {
3096 while (len) {
3097
3098 /*
3099 * First check to see if a protection data
3100 * check is valid
3101 */
3102 if ((src->ref_tag == 0xffffffff) ||
3103 (src->app_tag == 0xffff)) {
3104 start_ref_tag++;
3105 goto skipit;
3106 }
3107
James Smart9c6aa9d2013-05-31 17:03:39 -04003108 /* First Guard Tag checking */
3109 if (chk_guard) {
3110 guard_tag = src->guard_tag;
3111 if (lpfc_cmd_guard_csum(cmd))
3112 sum = lpfc_bg_csum(data_src,
3113 blksize);
3114 else
3115 sum = lpfc_bg_crc(data_src,
3116 blksize);
3117 if ((guard_tag != sum)) {
3118 err_type = BGS_GUARD_ERR_MASK;
3119 goto out;
3120 }
James Smart737d4242013-04-17 20:14:49 -04003121 }
3122
3123 /* Reference Tag checking */
3124 ref_tag = be32_to_cpu(src->ref_tag);
3125 if (chk_ref && (ref_tag != start_ref_tag)) {
3126 err_type = BGS_REFTAG_ERR_MASK;
3127 goto out;
3128 }
3129 start_ref_tag++;
3130
James Smart9c6aa9d2013-05-31 17:03:39 -04003131 /* App Tag checking */
3132 app_tag = src->app_tag;
3133 if (chk_app && (app_tag != start_app_tag)) {
3134 err_type = BGS_APPTAG_ERR_MASK;
3135 goto out;
James Smart737d4242013-04-17 20:14:49 -04003136 }
3137skipit:
3138 len -= sizeof(struct scsi_dif_tuple);
3139 if (len < 0)
3140 len = 0;
3141 src++;
3142
3143 data_src += blksize;
3144 data_len -= blksize;
3145
3146 /*
3147 * Are we at the end of the Data segment?
3148 * The data segment is only used for Guard
3149 * tag checking.
3150 */
3151 if (chk_guard && (data_len == 0)) {
3152 chk_guard = 0;
3153 sgde = sg_next(sgde);
3154 if (!sgde)
3155 goto out;
3156
3157 data_src = (uint8_t *)sg_virt(sgde);
3158 data_len = sgde->length;
3159 if ((data_len & (blksize - 1)) == 0)
3160 chk_guard = 1;
3161 }
3162 }
3163
3164 /* Goto the next Protection data segment */
3165 sgpe = sg_next(sgpe);
3166 if (sgpe) {
3167 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3168 len = sgpe->length;
3169 } else {
3170 src = NULL;
3171 }
3172 protsegcnt--;
3173 }
3174 }
3175out:
3176 if (err_type == BGS_GUARD_ERR_MASK) {
3177 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3178 0x10, 0x1);
3179 cmd->result = DRIVER_SENSE << 24
3180 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3181 phba->bg_guard_err_cnt++;
3182 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3183 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3184 (unsigned long)scsi_get_lba(cmd),
3185 sum, guard_tag);
3186
3187 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3188 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3189 0x10, 0x3);
3190 cmd->result = DRIVER_SENSE << 24
3191 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3192
3193 phba->bg_reftag_err_cnt++;
3194 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3195 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3196 (unsigned long)scsi_get_lba(cmd),
3197 ref_tag, start_ref_tag);
3198
3199 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3200 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3201 0x10, 0x2);
3202 cmd->result = DRIVER_SENSE << 24
3203 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3204
3205 phba->bg_apptag_err_cnt++;
3206 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3207 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3208 (unsigned long)scsi_get_lba(cmd),
3209 app_tag, start_app_tag);
3210 }
3211}
3212
3213
3214/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003215 * This function checks for BlockGuard errors detected by
3216 * the HBA. In case of errors, the ASC/ASCQ fields in the
3217 * sense buffer will be set accordingly, paired with
3218 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3219 * detected corruption.
3220 *
3221 * Returns:
3222 * 0 - No error found
3223 * 1 - BlockGuard error found
3224 * -1 - Internal error (bad profile, ...etc)
3225 */
3226static int
3227lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3228 struct lpfc_iocbq *pIocbOut)
3229{
3230 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3231 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3232 int ret = 0;
3233 uint32_t bghm = bgf->bghm;
3234 uint32_t bgstat = bgf->bgstat;
3235 uint64_t failing_sector = 0;
3236
James Smarte2a0a9d2008-12-04 22:40:02 -05003237 spin_lock(&_dump_buf_lock);
3238 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003239 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3240 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003241 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003242 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003243
3244 /* If we have a prot sgl, save the DIF buffer */
3245 if (lpfc_prot_group_type(phba, cmd) ==
3246 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003247 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3248 "Saving DIF for %u blocks to debugfs\n",
3249 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3250 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003251 }
3252
3253 _dump_buf_done = 1;
3254 }
3255 spin_unlock(&_dump_buf_lock);
3256
3257 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3258 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003259 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3260 "9072 BLKGRD: Invalid BG Profile in cmd"
3261 " 0x%x lba 0x%llx blk cnt 0x%x "
3262 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3263 (unsigned long long)scsi_get_lba(cmd),
3264 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003265 ret = (-1);
3266 goto out;
3267 }
3268
3269 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3270 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003271 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3272 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3273 " 0x%x lba 0x%llx blk cnt 0x%x "
3274 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3275 (unsigned long long)scsi_get_lba(cmd),
3276 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003277 ret = (-1);
3278 goto out;
3279 }
3280
3281 if (lpfc_bgs_get_guard_err(bgstat)) {
3282 ret = 1;
3283
3284 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3285 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003286 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003287 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3288 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003289 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3290 "9055 BLKGRD: Guard Tag error in cmd"
3291 " 0x%x lba 0x%llx blk cnt 0x%x "
3292 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3293 (unsigned long long)scsi_get_lba(cmd),
3294 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003295 }
3296
3297 if (lpfc_bgs_get_reftag_err(bgstat)) {
3298 ret = 1;
3299
3300 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3301 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003302 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003303 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3304
3305 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003306 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3307 "9056 BLKGRD: Ref Tag error in cmd"
3308 " 0x%x lba 0x%llx blk cnt 0x%x "
3309 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3310 (unsigned long long)scsi_get_lba(cmd),
3311 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003312 }
3313
3314 if (lpfc_bgs_get_apptag_err(bgstat)) {
3315 ret = 1;
3316
3317 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3318 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003319 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003320 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3321
3322 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003323 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3324 "9061 BLKGRD: App Tag error in cmd"
3325 " 0x%x lba 0x%llx blk cnt 0x%x "
3326 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3327 (unsigned long long)scsi_get_lba(cmd),
3328 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003329 }
3330
3331 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3332 /*
3333 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003334 * field, and put the failing LBA in it.
3335 * This code assumes there was also a guard/app/ref tag error
3336 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003337 */
James Smart7c56b9f2011-07-22 18:36:25 -04003338 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3339 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3340 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3341 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003342
3343 /* bghm is a "on the wire" FC frame based count */
3344 switch (scsi_get_prot_op(cmd)) {
3345 case SCSI_PROT_READ_INSERT:
3346 case SCSI_PROT_WRITE_STRIP:
3347 bghm /= cmd->device->sector_size;
3348 break;
3349 case SCSI_PROT_READ_STRIP:
3350 case SCSI_PROT_WRITE_INSERT:
3351 case SCSI_PROT_READ_PASS:
3352 case SCSI_PROT_WRITE_PASS:
3353 bghm /= (cmd->device->sector_size +
3354 sizeof(struct scsi_dif_tuple));
3355 break;
3356 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003357
3358 failing_sector = scsi_get_lba(cmd);
3359 failing_sector += bghm;
3360
James Smart7c56b9f2011-07-22 18:36:25 -04003361 /* Descriptor Information */
3362 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003363 }
3364
3365 if (!ret) {
3366 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003367 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3368 "9057 BLKGRD: Unknown error in cmd"
3369 " 0x%x lba 0x%llx blk cnt 0x%x "
3370 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3371 (unsigned long long)scsi_get_lba(cmd),
3372 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003373
James Smart737d4242013-04-17 20:14:49 -04003374 /* Calcuate what type of error it was */
3375 lpfc_calc_bg_err(phba, lpfc_cmd);
3376 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003377out:
3378 return ret;
3379}
3380
James Smartea2151b2008-09-07 11:52:10 -04003381/**
James Smartda0436e2009-05-22 14:51:39 -04003382 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3383 * @phba: The Hba for which this call is being executed.
3384 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3385 *
3386 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3387 * field of @lpfc_cmd for device with SLI-4 interface spec.
3388 *
3389 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003390 * 1 - Error
3391 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003392 **/
3393static int
3394lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3395{
3396 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3397 struct scatterlist *sgel = NULL;
3398 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3399 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003400 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003401 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3402 dma_addr_t physaddr;
3403 uint32_t num_bde = 0;
3404 uint32_t dma_len;
3405 uint32_t dma_offset = 0;
3406 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003407 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003408
3409 /*
3410 * There are three possibilities here - use scatter-gather segment, use
3411 * the single mapping, or neither. Start the lpfc command prep by
3412 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3413 * data bde entry.
3414 */
3415 if (scsi_sg_count(scsi_cmnd)) {
3416 /*
3417 * The driver stores the segment count returned from pci_map_sg
3418 * because this a count of dma-mappings used to map the use_sg
3419 * pages. They are not guaranteed to be the same for those
3420 * architectures that implement an IOMMU.
3421 */
3422
3423 nseg = scsi_dma_map(scsi_cmnd);
3424 if (unlikely(!nseg))
3425 return 1;
3426 sgl += 1;
3427 /* clear the last flag in the fcp_rsp map entry */
3428 sgl->word2 = le32_to_cpu(sgl->word2);
3429 bf_set(lpfc_sli4_sge_last, sgl, 0);
3430 sgl->word2 = cpu_to_le32(sgl->word2);
3431 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003432 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003433 lpfc_cmd->seg_cnt = nseg;
3434 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003435 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3436 " %s: Too many sg segments from "
3437 "dma_map_sg. Config %d, seg_cnt %d\n",
3438 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003439 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003440 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003441 scsi_dma_unmap(scsi_cmnd);
3442 return 1;
3443 }
3444
3445 /*
3446 * The driver established a maximum scatter-gather segment count
3447 * during probe that limits the number of sg elements in any
3448 * single scsi command. Just run through the seg_cnt and format
3449 * the sge's.
3450 * When using SLI-3 the driver will try to fit all the BDEs into
3451 * the IOCB. If it can't then the BDEs get added to a BPL as it
3452 * does for SLI-2 mode.
3453 */
3454 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3455 physaddr = sg_dma_address(sgel);
3456 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003457 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3458 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003459 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003460 if ((num_bde + 1) == nseg)
3461 bf_set(lpfc_sli4_sge_last, sgl, 1);
3462 else
3463 bf_set(lpfc_sli4_sge_last, sgl, 0);
3464 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003465 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003466 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003467 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003468 dma_offset += dma_len;
3469 sgl++;
3470 }
James Smartfedd3b72011-02-16 12:39:24 -05003471 /* setup the performance hint (first data BDE) if enabled */
3472 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3473 bde = (struct ulp_bde64 *)
3474 &(iocb_cmd->unsli3.sli3Words[5]);
3475 bde->addrLow = first_data_sgl->addr_lo;
3476 bde->addrHigh = first_data_sgl->addr_hi;
3477 bde->tus.f.bdeSize =
3478 le32_to_cpu(first_data_sgl->sge_len);
3479 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3480 bde->tus.w = cpu_to_le32(bde->tus.w);
3481 }
James Smartda0436e2009-05-22 14:51:39 -04003482 } else {
3483 sgl += 1;
3484 /* clear the last flag in the fcp_rsp map entry */
3485 sgl->word2 = le32_to_cpu(sgl->word2);
3486 bf_set(lpfc_sli4_sge_last, sgl, 1);
3487 sgl->word2 = cpu_to_le32(sgl->word2);
3488 }
3489
3490 /*
3491 * Finish initializing those IOCB fields that are dependent on the
3492 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3493 * explicitly reinitialized.
3494 * all iocb memory resources are reused.
3495 */
3496 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3497
3498 /*
3499 * Due to difference in data length between DIF/non-DIF paths,
3500 * we need to set word 4 of IOCB here
3501 */
3502 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3503 return 0;
3504}
3505
3506/**
James Smartacd68592012-01-18 16:25:09 -05003507 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3508 * @phba: The Hba for which this call is being executed.
3509 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3510 *
3511 * This is the protection/DIF aware version of
3512 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3513 * two functions eventually, but for now, it's here
3514 **/
3515static int
3516lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3517 struct lpfc_scsi_buf *lpfc_cmd)
3518{
3519 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3520 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3521 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3522 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003523 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003524 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3525 int prot_group_type = 0;
3526 int fcpdl;
3527
3528 /*
3529 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003530 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003531 */
3532 if (scsi_sg_count(scsi_cmnd)) {
3533 /*
3534 * The driver stores the segment count returned from pci_map_sg
3535 * because this a count of dma-mappings used to map the use_sg
3536 * pages. They are not guaranteed to be the same for those
3537 * architectures that implement an IOMMU.
3538 */
3539 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3540 scsi_sglist(scsi_cmnd),
3541 scsi_sg_count(scsi_cmnd), datadir);
3542 if (unlikely(!datasegcnt))
3543 return 1;
3544
3545 sgl += 1;
3546 /* clear the last flag in the fcp_rsp map entry */
3547 sgl->word2 = le32_to_cpu(sgl->word2);
3548 bf_set(lpfc_sli4_sge_last, sgl, 0);
3549 sgl->word2 = cpu_to_le32(sgl->word2);
3550
3551 sgl += 1;
3552 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003553
3554 /* First check if data segment count from SCSI Layer is good */
3555 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3556 goto err;
James Smartacd68592012-01-18 16:25:09 -05003557
3558 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3559
3560 switch (prot_group_type) {
3561 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003562 /* Here we need to add a DISEED to the count */
3563 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3564 goto err;
3565
3566 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003567 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003568
James Smartacd68592012-01-18 16:25:09 -05003569 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003570 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003571 goto err;
3572 break;
James Smart96f70772013-04-17 20:16:15 -04003573
3574 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003575 /*
3576 * This type indicates that protection buffers are
3577 * passed to the driver, so that needs to be prepared
3578 * for DMA
3579 */
3580 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3581 scsi_prot_sglist(scsi_cmnd),
3582 scsi_prot_sg_count(scsi_cmnd), datadir);
3583 if (unlikely(!protsegcnt)) {
3584 scsi_dma_unmap(scsi_cmnd);
3585 return 1;
3586 }
3587
3588 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003589 /*
3590 * There is a minimun of 3 SGEs used for every
3591 * protection data segment.
3592 */
3593 if ((lpfc_cmd->prot_seg_cnt * 3) >
3594 (phba->cfg_total_seg_cnt - 2))
3595 goto err;
James Smartacd68592012-01-18 16:25:09 -05003596
James Smart96f70772013-04-17 20:16:15 -04003597 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003598 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003599
James Smartacd68592012-01-18 16:25:09 -05003600 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003601 if ((num_sge < 3) ||
3602 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003603 goto err;
3604 break;
James Smart96f70772013-04-17 20:16:15 -04003605
James Smartacd68592012-01-18 16:25:09 -05003606 case LPFC_PG_TYPE_INVALID:
3607 default:
James Smart96f70772013-04-17 20:16:15 -04003608 scsi_dma_unmap(scsi_cmnd);
3609 lpfc_cmd->seg_cnt = 0;
3610
James Smartacd68592012-01-18 16:25:09 -05003611 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3612 "9083 Unexpected protection group %i\n",
3613 prot_group_type);
3614 return 1;
3615 }
3616 }
3617
James Smart8012cc32012-10-31 14:44:49 -04003618 switch (scsi_get_prot_op(scsi_cmnd)) {
3619 case SCSI_PROT_WRITE_STRIP:
3620 case SCSI_PROT_READ_STRIP:
3621 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3622 break;
3623 case SCSI_PROT_WRITE_INSERT:
3624 case SCSI_PROT_READ_INSERT:
3625 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3626 break;
3627 case SCSI_PROT_WRITE_PASS:
3628 case SCSI_PROT_READ_PASS:
3629 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3630 break;
3631 }
3632
James Smartacd68592012-01-18 16:25:09 -05003633 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003634 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3635
3636 /*
3637 * Due to difference in data length between DIF/non-DIF paths,
3638 * we need to set word 4 of IOCB here
3639 */
3640 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003641
3642 return 0;
3643err:
James Smart96f70772013-04-17 20:16:15 -04003644 if (lpfc_cmd->seg_cnt)
3645 scsi_dma_unmap(scsi_cmnd);
3646 if (lpfc_cmd->prot_seg_cnt)
3647 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3648 scsi_prot_sg_count(scsi_cmnd),
3649 scsi_cmnd->sc_data_direction);
3650
James Smartacd68592012-01-18 16:25:09 -05003651 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003652 "9084 Cannot setup S/G List for HBA"
3653 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3654 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3655 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3656 prot_group_type, num_sge);
3657
3658 lpfc_cmd->seg_cnt = 0;
3659 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003660 return 1;
3661}
3662
3663/**
James Smart3772a992009-05-22 14:50:54 -04003664 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3665 * @phba: The Hba for which this call is being executed.
3666 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3667 *
3668 * This routine wraps the actual DMA mapping function pointer from the
3669 * lpfc_hba struct.
3670 *
3671 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003672 * 1 - Error
3673 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003674 **/
3675static inline int
3676lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3677{
3678 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3679}
3680
3681/**
James Smartacd68592012-01-18 16:25:09 -05003682 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3683 * using BlockGuard.
3684 * @phba: The Hba for which this call is being executed.
3685 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3686 *
3687 * This routine wraps the actual DMA mapping function pointer from the
3688 * lpfc_hba struct.
3689 *
3690 * Return codes:
3691 * 1 - Error
3692 * 0 - Success
3693 **/
3694static inline int
3695lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3696{
3697 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3698}
3699
3700/**
James Smart3621a712009-04-06 18:47:14 -04003701 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003702 * @phba: Pointer to hba context object.
3703 * @vport: Pointer to vport object.
3704 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3705 * @rsp_iocb: Pointer to response iocb object which reported error.
3706 *
3707 * This function posts an event when there is a SCSI command reporting
3708 * error from the scsi device.
3709 **/
3710static void
3711lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3712 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3713 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3714 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3715 uint32_t resp_info = fcprsp->rspStatus2;
3716 uint32_t scsi_status = fcprsp->rspStatus3;
3717 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3718 struct lpfc_fast_path_event *fast_path_evt = NULL;
3719 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3720 unsigned long flags;
3721
James Smart5989b8d2010-10-22 11:06:56 -04003722 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3723 return;
3724
James Smartea2151b2008-09-07 11:52:10 -04003725 /* If there is queuefull or busy condition send a scsi event */
3726 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3727 (cmnd->result == SAM_STAT_BUSY)) {
3728 fast_path_evt = lpfc_alloc_fast_evt(phba);
3729 if (!fast_path_evt)
3730 return;
3731 fast_path_evt->un.scsi_evt.event_type =
3732 FC_REG_SCSI_EVENT;
3733 fast_path_evt->un.scsi_evt.subcategory =
3734 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3735 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3736 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3737 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3738 &pnode->nlp_portname, sizeof(struct lpfc_name));
3739 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3740 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3741 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3742 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3743 fast_path_evt = lpfc_alloc_fast_evt(phba);
3744 if (!fast_path_evt)
3745 return;
3746 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3747 FC_REG_SCSI_EVENT;
3748 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3749 LPFC_EVENT_CHECK_COND;
3750 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3751 cmnd->device->lun;
3752 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3753 &pnode->nlp_portname, sizeof(struct lpfc_name));
3754 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3755 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3756 fast_path_evt->un.check_cond_evt.sense_key =
3757 cmnd->sense_buffer[2] & 0xf;
3758 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3759 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3760 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3761 fcpi_parm &&
3762 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3763 ((scsi_status == SAM_STAT_GOOD) &&
3764 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3765 /*
3766 * If status is good or resid does not match with fcp_param and
3767 * there is valid fcpi_parm, then there is a read_check error
3768 */
3769 fast_path_evt = lpfc_alloc_fast_evt(phba);
3770 if (!fast_path_evt)
3771 return;
3772 fast_path_evt->un.read_check_error.header.event_type =
3773 FC_REG_FABRIC_EVENT;
3774 fast_path_evt->un.read_check_error.header.subcategory =
3775 LPFC_EVENT_FCPRDCHKERR;
3776 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3777 &pnode->nlp_portname, sizeof(struct lpfc_name));
3778 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3779 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3780 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3781 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3782 fast_path_evt->un.read_check_error.fcpiparam =
3783 fcpi_parm;
3784 } else
3785 return;
3786
3787 fast_path_evt->vport = vport;
3788 spin_lock_irqsave(&phba->hbalock, flags);
3789 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3790 spin_unlock_irqrestore(&phba->hbalock, flags);
3791 lpfc_worker_wake_up(phba);
3792 return;
3793}
James Smart9bad7672008-12-04 22:39:02 -05003794
3795/**
James Smartf1126682009-06-10 17:22:44 -04003796 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003797 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003798 * @psb: The scsi buffer which is going to be un-mapped.
3799 *
3800 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003801 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003802 **/
dea31012005-04-17 16:05:31 -05003803static void
James Smartf1126682009-06-10 17:22:44 -04003804lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003805{
3806 /*
3807 * There are only two special cases to consider. (1) the scsi command
3808 * requested scatter-gather usage or (2) the scsi command allocated
3809 * a request buffer, but did not request use_sg. There is a third
3810 * case, but it does not require resource deallocation.
3811 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003812 if (psb->seg_cnt > 0)
3813 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003814 if (psb->prot_seg_cnt > 0)
3815 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3816 scsi_prot_sg_count(psb->pCmd),
3817 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003818}
3819
James Smart9bad7672008-12-04 22:39:02 -05003820/**
James Smart3621a712009-04-06 18:47:14 -04003821 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003822 * @vport: The virtual port for which this call is being executed.
3823 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3824 * @rsp_iocb: The response IOCB which contains FCP error.
3825 *
3826 * This routine is called to process response IOCB with status field
3827 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3828 * based upon SCSI and FCP error.
3829 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003830static void
James Smart2e0fef82007-06-17 19:56:36 -05003831lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3832 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003833{
3834 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3835 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3836 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003837 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003838 uint32_t resp_info = fcprsp->rspStatus2;
3839 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003840 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003841 uint32_t host_status = DID_OK;
3842 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003843 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003844
James Smartea2151b2008-09-07 11:52:10 -04003845
dea31012005-04-17 16:05:31 -05003846 /*
3847 * If this is a task management command, there is no
3848 * scsi packet associated with this lpfc_cmd. The driver
3849 * consumes it.
3850 */
3851 if (fcpcmd->fcpCntl2) {
3852 scsi_status = 0;
3853 goto out;
3854 }
3855
James Smart6a9c52c2009-10-02 15:16:51 -04003856 if (resp_info & RSP_LEN_VALID) {
3857 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003858 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003859 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3860 "2719 Invalid response length: "
3861 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3862 cmnd->device->id,
3863 cmnd->device->lun, cmnd->cmnd[0],
3864 rsplen);
3865 host_status = DID_ERROR;
3866 goto out;
3867 }
James Smarte40a02c2010-02-26 14:13:54 -05003868 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3869 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3870 "2757 Protocol failure detected during "
3871 "processing of FCP I/O op: "
3872 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3873 cmnd->device->id,
3874 cmnd->device->lun, cmnd->cmnd[0],
3875 fcprsp->rspInfo3);
3876 host_status = DID_ERROR;
3877 goto out;
3878 }
James Smart6a9c52c2009-10-02 15:16:51 -04003879 }
3880
James Smartc7743952006-12-02 13:34:42 -05003881 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3882 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3883 if (snslen > SCSI_SENSE_BUFFERSIZE)
3884 snslen = SCSI_SENSE_BUFFERSIZE;
3885
3886 if (resp_info & RSP_LEN_VALID)
3887 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3888 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3889 }
3890 lp = (uint32_t *)cmnd->sense_buffer;
3891
James Smartaa1c7ee2012-08-14 14:26:06 -04003892 /* special handling for under run conditions */
3893 if (!scsi_status && (resp_info & RESID_UNDER)) {
3894 /* don't log under runs if fcp set... */
3895 if (vport->cfg_log_verbose & LOG_FCP)
3896 logit = LOG_FCP_ERROR;
3897 /* unless operator says so */
3898 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3899 logit = LOG_FCP_UNDER;
3900 }
James Smartc7743952006-12-02 13:34:42 -05003901
James Smarte8b62012007-08-02 11:10:09 -04003902 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003903 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003904 "Data: x%x x%x x%x x%x x%x\n",
3905 cmnd->cmnd[0], scsi_status,
3906 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3907 be32_to_cpu(fcprsp->rspResId),
3908 be32_to_cpu(fcprsp->rspSnsLen),
3909 be32_to_cpu(fcprsp->rspRspLen),
3910 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003911
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003912 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003913 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003914 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003915
James Smart73d91e52011-10-10 21:32:10 -04003916 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003917 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003918 "residual %d Data: x%x x%x x%x\n",
3919 be32_to_cpu(fcpcmd->fcpDl),
3920 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3921 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003922
3923 /*
James Smart7054a602007-04-25 09:52:34 -04003924 * If there is an under run check if under run reported by
3925 * storage array is same as the under run reported by HBA.
3926 * If this is not same, there is a dropped frame.
3927 */
3928 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3929 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003930 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003931 lpfc_printf_vlog(vport, KERN_WARNING,
3932 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003933 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003934 "and Underrun Data: x%x x%x x%x x%x\n",
3935 be32_to_cpu(fcpcmd->fcpDl),
3936 scsi_get_resid(cmnd), fcpi_parm,
3937 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003938 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003939 host_status = DID_ERROR;
3940 }
3941 /*
dea31012005-04-17 16:05:31 -05003942 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003943 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003944 * is not present, make sure the actual amount transferred is at
3945 * least the underflow value or fail.
3946 */
3947 if (!(resp_info & SNS_LEN_VALID) &&
3948 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003949 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3950 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003951 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003952 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003953 "underrun converted to error "
3954 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003955 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003956 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003957 host_status = DID_ERROR;
3958 }
3959 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003960 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003961 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003962 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003963 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003964 host_status = DID_ERROR;
3965
3966 /*
3967 * Check SLI validation that all the transfer was actually done
James Smart582dd792012-08-03 12:34:44 -04003968 * (fcpi_parm should be zero).
dea31012005-04-17 16:05:31 -05003969 */
James Smart582dd792012-08-03 12:34:44 -04003970 } else if (fcpi_parm) {
James Smarte8b62012007-08-02 11:10:09 -04003971 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart582dd792012-08-03 12:34:44 -04003972 "9029 FCP Data Transfer Check Error: "
James Smarteee88772010-09-29 11:19:08 -04003973 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003974 be32_to_cpu(fcpcmd->fcpDl),
3975 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003976 fcpi_parm, cmnd->cmnd[0], scsi_status);
3977 switch (scsi_status) {
3978 case SAM_STAT_GOOD:
3979 case SAM_STAT_CHECK_CONDITION:
3980 /* Fabric dropped a data frame. Fail any successful
3981 * command in which we detected dropped frames.
3982 * A status of good or some check conditions could
3983 * be considered a successful command.
3984 */
3985 host_status = DID_ERROR;
3986 break;
3987 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003988 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003989 }
3990
3991 out:
3992 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003993 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003994}
3995
James Smart9bad7672008-12-04 22:39:02 -05003996/**
James Smart3621a712009-04-06 18:47:14 -04003997 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05003998 * @phba: The Hba for which this call is being executed.
3999 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04004000 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05004001 *
4002 * This routine assigns scsi command result by looking into response IOCB
4003 * status field appropriately. This routine handles QUEUE FULL condition as
4004 * well by ramping down device queue depth.
4005 **/
dea31012005-04-17 16:05:31 -05004006static void
4007lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4008 struct lpfc_iocbq *pIocbOut)
4009{
4010 struct lpfc_scsi_buf *lpfc_cmd =
4011 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05004012 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05004013 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4014 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04004015 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004016 int result;
James Smarta257bf92009-04-06 18:48:10 -04004017 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05004018 int depth;
James Smartfa61a542008-01-11 01:52:42 -05004019 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04004020 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04004021 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04004022 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04004023 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05004024
James Smart75baf692010-06-08 18:31:21 -04004025 /* Sanity check on return of outstanding command */
4026 if (!(lpfc_cmd->pCmd))
4027 return;
4028 cmd = lpfc_cmd->pCmd;
4029 shost = cmd->device->host;
4030
James Smarte3d2b802012-08-14 14:25:43 -04004031 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05004032 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05004033 /* pick up SLI4 exhange busy status from HBA */
4034 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
4035
James Smart9a6b09c2012-03-01 22:37:42 -05004036#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4037 if (lpfc_cmd->prot_data_type) {
4038 struct scsi_dif_tuple *src = NULL;
4039
4040 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4041 /*
4042 * Used to restore any changes to protection
4043 * data for error injection.
4044 */
4045 switch (lpfc_cmd->prot_data_type) {
4046 case LPFC_INJERR_REFTAG:
4047 src->ref_tag =
4048 lpfc_cmd->prot_data;
4049 break;
4050 case LPFC_INJERR_APPTAG:
4051 src->app_tag =
4052 (uint16_t)lpfc_cmd->prot_data;
4053 break;
4054 case LPFC_INJERR_GUARD:
4055 src->guard_tag =
4056 (uint16_t)lpfc_cmd->prot_data;
4057 break;
4058 default:
4059 break;
4060 }
4061
4062 lpfc_cmd->prot_data = 0;
4063 lpfc_cmd->prot_data_type = 0;
4064 lpfc_cmd->prot_data_segment = NULL;
4065 }
4066#endif
James Smart109f6ed2008-12-04 22:39:08 -05004067 if (pnode && NLP_CHK_NODE_ACT(pnode))
4068 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05004069
4070 if (lpfc_cmd->status) {
4071 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4072 (lpfc_cmd->result & IOERR_DRVR_MASK))
4073 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4074 else if (lpfc_cmd->status >= IOSTAT_CNT)
4075 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04004076 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4077 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4078 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4079 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04004080 logit = 0;
4081 else
4082 logit = LOG_FCP | LOG_FCP_UNDER;
4083 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4084 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04004085 "status: x%x result: x%x "
4086 "sid: x%x did: x%x oxid: x%x "
4087 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04004088 cmd->cmnd[0],
4089 cmd->device ? cmd->device->id : 0xffff,
4090 cmd->device ? cmd->device->lun : 0xffff,
4091 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04004092 vport->fc_myDID,
4093 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04004094 phba->sli_rev == LPFC_SLI_REV4 ?
4095 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004096 pIocbOut->iocb.ulpContext,
4097 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004098
4099 switch (lpfc_cmd->status) {
4100 case IOSTAT_FCP_RSP_ERROR:
4101 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004102 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004103 break;
4104 case IOSTAT_NPORT_BSY:
4105 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04004106 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04004107 fast_path_evt = lpfc_alloc_fast_evt(phba);
4108 if (!fast_path_evt)
4109 break;
4110 fast_path_evt->un.fabric_evt.event_type =
4111 FC_REG_FABRIC_EVENT;
4112 fast_path_evt->un.fabric_evt.subcategory =
4113 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4114 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4115 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4116 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4117 &pnode->nlp_portname,
4118 sizeof(struct lpfc_name));
4119 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4120 &pnode->nlp_nodename,
4121 sizeof(struct lpfc_name));
4122 }
4123 fast_path_evt->vport = vport;
4124 fast_path_evt->work_evt.evt =
4125 LPFC_EVT_FASTPATH_MGMT_EVT;
4126 spin_lock_irqsave(&phba->hbalock, flags);
4127 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4128 &phba->work_list);
4129 spin_unlock_irqrestore(&phba->hbalock, flags);
4130 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004131 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004132 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004133 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004134 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4135 lpfc_cmd->result ==
4136 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4137 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4138 lpfc_cmd->result ==
4139 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4140 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4141 break;
4142 }
James Smartd7c255b2008-08-24 21:50:00 -04004143 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004144 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004145 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4146 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004147 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004148 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004149 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004150 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4151 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4152 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4153 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4154 /*
4155 * This is a response for a BG enabled
4156 * cmd. Parse BG error
4157 */
4158 lpfc_parse_bg_err(phba, lpfc_cmd,
4159 pIocbOut);
4160 break;
4161 } else {
4162 lpfc_printf_vlog(vport, KERN_WARNING,
4163 LOG_BG,
4164 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004165 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004166 }
4167 }
James Smart1151e3e2011-02-16 12:39:35 -05004168 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4169 && (phba->sli_rev == LPFC_SLI_REV4)
4170 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4171 /* This IO was aborted by the target, we don't
4172 * know the rxid and because we did not send the
4173 * ABTS we cannot generate and RRQ.
4174 */
4175 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004176 lpfc_cmd->cur_iocbq.sli4_lxritag,
4177 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004178 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004179 /* else: fall through */
dea31012005-04-17 16:05:31 -05004180 default:
4181 cmd->result = ScsiResult(DID_ERROR, 0);
4182 break;
4183 }
4184
James Smart58da1ff2008-04-07 10:15:56 -04004185 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004186 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004187 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4188 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004189 } else
dea31012005-04-17 16:05:31 -05004190 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004191
4192 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4193 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4194
James Smarte8b62012007-08-02 11:10:09 -04004195 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4196 "0710 Iodone <%d/%d> cmd %p, error "
4197 "x%x SNS x%x x%x Data: x%x x%x\n",
4198 cmd->device->id, cmd->device->lun, cmd,
4199 cmd->result, *lp, *(lp + 3), cmd->retries,
4200 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004201 }
4202
James Smartea2151b2008-09-07 11:52:10 -04004203 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004204 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04004205 if (vport->cfg_max_scsicmpl_time &&
4206 time_after(jiffies, lpfc_cmd->start_time +
4207 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004208 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004209 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4210 if (pnode->cmd_qdepth >
4211 atomic_read(&pnode->cmd_pending) &&
4212 (atomic_read(&pnode->cmd_pending) >
4213 LPFC_MIN_TGT_QDEPTH) &&
4214 ((cmd->cmnd[0] == READ_10) ||
4215 (cmd->cmnd[0] == WRITE_10)))
4216 pnode->cmd_qdepth =
4217 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004218
James Smart109f6ed2008-12-04 22:39:08 -05004219 pnode->last_change_time = jiffies;
4220 }
James Smarta257bf92009-04-06 18:48:10 -04004221 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004222 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004223 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004224 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004225 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004226 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004227 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4228 / 100;
4229 depth = depth ? depth : 1;
4230 pnode->cmd_qdepth += depth;
4231 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4232 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004233 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004234 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004235 }
James Smart977b5a02008-09-07 11:52:04 -04004236 }
4237
James Smart1dcb58e2007-04-25 09:51:30 -04004238 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004239
4240 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4241 queue_depth = cmd->device->queue_depth;
4242 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05004243 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004244
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004245 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004246 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004247 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004248 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004249
James Smartfa61a542008-01-11 01:52:42 -05004250 /*
4251 * If there is a thread waiting for command completion
4252 * wake up the thread.
4253 */
James Smarta257bf92009-04-06 18:48:10 -04004254 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004255 if (lpfc_cmd->waitq)
4256 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004257 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004258 lpfc_release_scsi_buf(phba, lpfc_cmd);
4259 return;
4260 }
4261
James Smart92d7f7b2007-06-17 19:56:38 -05004262 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04004263 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05004264
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004265 /*
4266 * Check for queue full. If the lun is reporting queue full, then
4267 * back off the lun queue depth to prevent target overloads.
4268 */
James Smart58da1ff2008-04-07 10:15:56 -04004269 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4270 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04004271 shost_for_each_device(tmp_sdev, shost) {
4272 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004273 continue;
4274 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05004275 tmp_sdev->queue_depth-1);
4276 if (depth <= 0)
4277 continue;
James Smarte8b62012007-08-02 11:10:09 -04004278 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4279 "0711 detected queue full - lun queue "
4280 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04004281 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05004282 pnode,
4283 tmp_sdev->lun,
4284 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004285 }
4286 }
4287
James Smart876dd7d2012-09-29 11:31:28 -04004288 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004289 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004290 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004291
James Smartfa61a542008-01-11 01:52:42 -05004292 /*
4293 * If there is a thread waiting for command completion
4294 * wake up the thread.
4295 */
James Smarta257bf92009-04-06 18:48:10 -04004296 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004297 if (lpfc_cmd->waitq)
4298 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004299 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004300
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004301 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004302}
4303
James Smart34b02dc2008-08-24 21:49:55 -04004304/**
James Smart3621a712009-04-06 18:47:14 -04004305 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04004306 * @data: A pointer to the immediate command data portion of the IOCB.
4307 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4308 *
4309 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4310 * byte swapping the data to big endian format for transmission on the wire.
4311 **/
4312static void
4313lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4314{
4315 int i, j;
4316 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4317 i += sizeof(uint32_t), j++) {
4318 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4319 }
4320}
4321
James Smart9bad7672008-12-04 22:39:02 -05004322/**
James Smartf1126682009-06-10 17:22:44 -04004323 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004324 * @vport: The virtual port for which this call is being executed.
4325 * @lpfc_cmd: The scsi command which needs to send.
4326 * @pnode: Pointer to lpfc_nodelist.
4327 *
4328 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004329 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004330 **/
dea31012005-04-17 16:05:31 -05004331static void
James Smartf1126682009-06-10 17:22:44 -04004332lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004333 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004334{
James Smart2e0fef82007-06-17 19:56:36 -05004335 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004336 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4337 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4338 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4339 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4340 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04004341 char tag[2];
James Smart027140e2012-08-03 12:35:44 -04004342 uint8_t *ptr;
4343 bool sli4;
dea31012005-04-17 16:05:31 -05004344
James Smart58da1ff2008-04-07 10:15:56 -04004345 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4346 return;
4347
dea31012005-04-17 16:05:31 -05004348 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004349 /* clear task management bits */
4350 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004351
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004352 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4353 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004354
James Smart027140e2012-08-03 12:35:44 -04004355 ptr = &fcp_cmnd->fcpCdb[0];
4356 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4357 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4358 ptr += scsi_cmnd->cmd_len;
4359 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4360 }
4361
James Smart7e2b19f2007-10-29 11:00:39 -04004362 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4363 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05004364 case HEAD_OF_QUEUE_TAG:
4365 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4366 break;
4367 case ORDERED_QUEUE_TAG:
4368 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4369 break;
4370 default:
4371 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4372 break;
4373 }
4374 } else
James Smartfe8f7f92013-01-03 15:43:03 -05004375 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004376
James Smart027140e2012-08-03 12:35:44 -04004377 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4378
dea31012005-04-17 16:05:31 -05004379 /*
4380 * There are three possibilities here - use scatter-gather segment, use
4381 * the single mapping, or neither. Start the lpfc command prep by
4382 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4383 * data bde entry.
4384 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004385 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004386 if (datadir == DMA_TO_DEVICE) {
4387 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart027140e2012-08-03 12:35:44 -04004388 if (sli4)
4389 iocb_cmd->ulpPU = PARM_READ_CHECK;
4390 else {
James Smart3772a992009-05-22 14:50:54 -04004391 iocb_cmd->un.fcpi.fcpi_parm = 0;
4392 iocb_cmd->ulpPU = 0;
James Smart027140e2012-08-03 12:35:44 -04004393 }
dea31012005-04-17 16:05:31 -05004394 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4395 phba->fc4OutputRequests++;
4396 } else {
4397 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4398 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004399 fcp_cmnd->fcpCntl3 = READ_DATA;
4400 phba->fc4InputRequests++;
4401 }
4402 } else {
4403 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4404 iocb_cmd->un.fcpi.fcpi_parm = 0;
4405 iocb_cmd->ulpPU = 0;
4406 fcp_cmnd->fcpCntl3 = 0;
4407 phba->fc4ControlRequests++;
4408 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004409 if (phba->sli_rev == 3 &&
4410 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004411 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004412 /*
4413 * Finish initializing those IOCB fields that are independent
4414 * of the scsi_cmnd request_buffer
4415 */
4416 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004417 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004418 piocbq->iocb.ulpContext =
4419 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004420 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4421 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004422 else
4423 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004424
4425 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4426 piocbq->context1 = lpfc_cmd;
4427 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4428 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004429 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004430}
4431
James Smart9bad7672008-12-04 22:39:02 -05004432/**
James Smart6d368e52011-05-24 11:44:12 -04004433 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004434 * @vport: The virtual port for which this call is being executed.
4435 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4436 * @lun: Logical unit number.
4437 * @task_mgmt_cmd: SCSI task management command.
4438 *
James Smart3772a992009-05-22 14:50:54 -04004439 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4440 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004441 *
4442 * Return codes:
4443 * 0 - Error
4444 * 1 - Success
4445 **/
dea31012005-04-17 16:05:31 -05004446static int
James Smartf1126682009-06-10 17:22:44 -04004447lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004448 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004449 unsigned int lun,
dea31012005-04-17 16:05:31 -05004450 uint8_t task_mgmt_cmd)
4451{
dea31012005-04-17 16:05:31 -05004452 struct lpfc_iocbq *piocbq;
4453 IOCB_t *piocb;
4454 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004455 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004456 struct lpfc_nodelist *ndlp = rdata->pnode;
4457
James Smart58da1ff2008-04-07 10:15:56 -04004458 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4459 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004460 return 0;
dea31012005-04-17 16:05:31 -05004461
dea31012005-04-17 16:05:31 -05004462 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004463 piocbq->vport = vport;
4464
dea31012005-04-17 16:05:31 -05004465 piocb = &piocbq->iocb;
4466
4467 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004468 /* Clear out any old data in the FCP command area */
4469 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4470 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004471 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004472 if (vport->phba->sli_rev == 3 &&
4473 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004474 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004475 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004476 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004477 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4478 piocb->ulpContext =
4479 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4480 }
dea31012005-04-17 16:05:31 -05004481 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4482 piocb->ulpFCP2Rcvy = 1;
4483 }
4484 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4485
4486 /* ulpTimeout is only one byte */
4487 if (lpfc_cmd->timeout > 0xff) {
4488 /*
4489 * Do not timeout the command at the firmware level.
4490 * The driver will provide the timeout mechanism.
4491 */
4492 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004493 } else
dea31012005-04-17 16:05:31 -05004494 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004495
4496 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4497 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004498
James Smart2e0fef82007-06-17 19:56:36 -05004499 return 1;
dea31012005-04-17 16:05:31 -05004500}
4501
James Smart9bad7672008-12-04 22:39:02 -05004502/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004503 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004504 * @phba: The hba struct for which this call is being executed.
4505 * @dev_grp: The HBA PCI-Device group number.
4506 *
4507 * This routine sets up the SCSI interface API function jump table in @phba
4508 * struct.
4509 * Returns: 0 - success, -ENODEV - failure.
4510 **/
4511int
4512lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4513{
4514
James Smartf1126682009-06-10 17:22:44 -04004515 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4516 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004517
James Smart3772a992009-05-22 14:50:54 -04004518 switch (dev_grp) {
4519 case LPFC_PCI_DEV_LP:
4520 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4521 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004522 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004523 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004524 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004525 break;
James Smartda0436e2009-05-22 14:51:39 -04004526 case LPFC_PCI_DEV_OC:
4527 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4528 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004529 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004530 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004531 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004532 break;
James Smart3772a992009-05-22 14:50:54 -04004533 default:
4534 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4535 "1418 Invalid HBA PCI-device group: 0x%x\n",
4536 dev_grp);
4537 return -ENODEV;
4538 break;
4539 }
James Smart3772a992009-05-22 14:50:54 -04004540 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004541 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004542 return 0;
4543}
4544
4545/**
James Smart3621a712009-04-06 18:47:14 -04004546 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004547 * @phba: The Hba for which this call is being executed.
4548 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4549 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4550 *
4551 * This routine is IOCB completion routine for device reset and target reset
4552 * routine. This routine release scsi buffer associated with lpfc_cmd.
4553 **/
James Smart7054a602007-04-25 09:52:34 -04004554static void
4555lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4556 struct lpfc_iocbq *cmdiocbq,
4557 struct lpfc_iocbq *rspiocbq)
4558{
4559 struct lpfc_scsi_buf *lpfc_cmd =
4560 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4561 if (lpfc_cmd)
4562 lpfc_release_scsi_buf(phba, lpfc_cmd);
4563 return;
4564}
4565
James Smart9bad7672008-12-04 22:39:02 -05004566/**
James Smart3621a712009-04-06 18:47:14 -04004567 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004568 * @host: The scsi host for which this call is being executed.
4569 *
4570 * This routine provides module information about hba.
4571 *
4572 * Reutrn code:
4573 * Pointer to char - Success.
4574 **/
dea31012005-04-17 16:05:31 -05004575const char *
4576lpfc_info(struct Scsi_Host *host)
4577{
James Smart2e0fef82007-06-17 19:56:36 -05004578 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4579 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004580 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004581 static char lpfcinfobuf[384];
4582
4583 memset(lpfcinfobuf,0,384);
4584 if (phba && phba->pcidev){
4585 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4586 len = strlen(lpfcinfobuf);
4587 snprintf(lpfcinfobuf + len,
4588 384-len,
4589 " on PCI bus %02x device %02x irq %d",
4590 phba->pcidev->bus->number,
4591 phba->pcidev->devfn,
4592 phba->pcidev->irq);
4593 len = strlen(lpfcinfobuf);
4594 if (phba->Port[0]) {
4595 snprintf(lpfcinfobuf + len,
4596 384-len,
4597 " port %s",
4598 phba->Port);
4599 }
James Smart65467b62010-01-26 23:08:29 -05004600 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004601 if (phba->sli_rev <= LPFC_SLI_REV3) {
4602 link_speed = lpfc_sli_port_speed_get(phba);
4603 } else {
4604 if (phba->sli4_hba.link_state.logical_speed)
4605 link_speed =
4606 phba->sli4_hba.link_state.logical_speed;
4607 else
4608 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004609 }
James Smart8b68cd52012-09-29 11:32:37 -04004610 if (link_speed != 0)
4611 snprintf(lpfcinfobuf + len, 384-len,
4612 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004613 }
4614 return lpfcinfobuf;
4615}
4616
James Smart9bad7672008-12-04 22:39:02 -05004617/**
James Smart3621a712009-04-06 18:47:14 -04004618 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004619 * @phba: The Hba for which this call is being executed.
4620 *
4621 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4622 * The default value of cfg_poll_tmo is 10 milliseconds.
4623 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004624static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4625{
4626 unsigned long poll_tmo_expires =
4627 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4628
James Smart0e9bb8d2013-03-01 16:35:12 -05004629 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004630 mod_timer(&phba->fcp_poll_timer,
4631 poll_tmo_expires);
4632}
4633
James Smart9bad7672008-12-04 22:39:02 -05004634/**
James Smart3621a712009-04-06 18:47:14 -04004635 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004636 * @phba: The Hba for which this call is being executed.
4637 *
4638 * This routine starts the fcp_poll_timer of @phba.
4639 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004640void lpfc_poll_start_timer(struct lpfc_hba * phba)
4641{
4642 lpfc_poll_rearm_timer(phba);
4643}
4644
James Smart9bad7672008-12-04 22:39:02 -05004645/**
James Smart3621a712009-04-06 18:47:14 -04004646 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004647 * @ptr: Map to lpfc_hba data structure pointer.
4648 *
4649 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4650 * and FCP Ring interrupt is disable.
4651 **/
4652
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004653void lpfc_poll_timeout(unsigned long ptr)
4654{
James Smart2e0fef82007-06-17 19:56:36 -05004655 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004656
4657 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004658 lpfc_sli_handle_fast_ring_event(phba,
4659 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4660
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004661 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4662 lpfc_poll_rearm_timer(phba);
4663 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004664}
4665
James Smart9bad7672008-12-04 22:39:02 -05004666/**
James Smart3621a712009-04-06 18:47:14 -04004667 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004668 * @cmnd: Pointer to scsi_cmnd data structure.
4669 * @done: Pointer to done routine.
4670 *
4671 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4672 * This routine prepares an IOCB from scsi command and provides to firmware.
4673 * The @done callback is invoked after driver finished processing the command.
4674 *
4675 * Return value :
4676 * 0 - Success
4677 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4678 **/
dea31012005-04-17 16:05:31 -05004679static int
James Smartb9a7c632012-08-03 12:35:24 -04004680lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004681{
James Smart2e0fef82007-06-17 19:56:36 -05004682 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4683 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004684 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004685 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004686 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004687 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004688 int err;
dea31012005-04-17 16:05:31 -05004689
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004690 err = fc_remote_port_chkready(rport);
4691 if (err) {
4692 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004693 goto out_fail_command;
4694 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004695 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004696
James Smartbf086112011-08-21 21:48:13 -04004697 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004698 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004699
James Smart6a9c52c2009-10-02 15:16:51 -04004700 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4701 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4702 " op:%02x str=%s without registering for"
4703 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004704 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4705 dif_op_str[scsi_get_prot_op(cmnd)]);
4706 goto out_fail_command;
4707 }
4708
dea31012005-04-17 16:05:31 -05004709 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004710 * Catch race where our node has transitioned, but the
4711 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004712 */
James Smart6b415f52012-06-12 13:54:59 -04004713 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4714 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004715 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004716 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004717
James Smart19ca7602010-11-20 23:11:55 -05004718 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004719 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004720 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004721
James Smarte8b62012007-08-02 11:10:09 -04004722 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4723 "0707 driver's buffer pool is empty, "
4724 "IO busied\n");
dea31012005-04-17 16:05:31 -05004725 goto out_host_busy;
4726 }
4727
4728 /*
4729 * Store the midlayer's command structure for the completion phase
4730 * and complete the command initialization.
4731 */
4732 lpfc_cmd->pCmd = cmnd;
4733 lpfc_cmd->rdata = rdata;
4734 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004735 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004736 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004737
James Smarte2a0a9d2008-12-04 22:40:02 -05004738 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004739 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004740 lpfc_printf_vlog(vport,
4741 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004742 "9033 BLKGRD: rcvd %s cmd:x%x "
4743 "sector x%llx cnt %u pt %x\n",
4744 dif_op_str[scsi_get_prot_op(cmnd)],
4745 cmnd->cmnd[0],
4746 (unsigned long long)scsi_get_lba(cmnd),
4747 blk_rq_sectors(cmnd->request),
4748 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004749 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004750 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4751 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004752 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004753 lpfc_printf_vlog(vport,
4754 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004755 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4756 "x%x sector x%llx cnt %u pt %x\n",
4757 cmnd->cmnd[0],
4758 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004759 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004760 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004761 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004762 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4763 }
4764
dea31012005-04-17 16:05:31 -05004765 if (err)
4766 goto out_host_busy_free_buf;
4767
James Smart2e0fef82007-06-17 19:56:36 -05004768 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004769
James Smart977b5a02008-09-07 11:52:04 -04004770 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004771 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004772 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004773 if (err) {
4774 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004775 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004776 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004777 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004778 lpfc_sli_handle_fast_ring_event(phba,
4779 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4780
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004781 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4782 lpfc_poll_rearm_timer(phba);
4783 }
4784
dea31012005-04-17 16:05:31 -05004785 return 0;
4786
4787 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004788 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004789 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004790 out_host_busy:
4791 return SCSI_MLQUEUE_HOST_BUSY;
4792
Mike Christie34963432011-02-25 14:04:28 -06004793 out_tgt_busy:
4794 return SCSI_MLQUEUE_TARGET_BUSY;
4795
dea31012005-04-17 16:05:31 -05004796 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004797 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004798 return 0;
4799}
4800
Jeff Garzikf2812332010-11-16 02:10:29 -05004801
James Smart9bad7672008-12-04 22:39:02 -05004802/**
James Smart3621a712009-04-06 18:47:14 -04004803 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004804 * @cmnd: Pointer to scsi_cmnd data structure.
4805 *
4806 * This routine aborts @cmnd pending in base driver.
4807 *
4808 * Return code :
4809 * 0x2003 - Error
4810 * 0x2002 - Success
4811 **/
dea31012005-04-17 16:05:31 -05004812static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004813lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004814{
James Smart2e0fef82007-06-17 19:56:36 -05004815 struct Scsi_Host *shost = cmnd->device->host;
4816 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4817 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004818 struct lpfc_iocbq *iocb;
4819 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004820 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004821 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004822 int ret = SUCCESS, status = 0;
James Smart876dd7d2012-09-29 11:31:28 -04004823 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004824 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004825
James Smart3a707302012-06-12 13:54:42 -04004826 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004827 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004828 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004829
James Smart876dd7d2012-09-29 11:31:28 -04004830 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004831 /* driver queued commands are in process of being flushed */
4832 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004833 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004834 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4835 "3168 SCSI Layer abort requested I/O has been "
4836 "flushed by LLD.\n");
4837 return FAILED;
4838 }
4839
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004840 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004841 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004842 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004843 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4844 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004845 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004846 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004847 return SUCCESS;
4848 }
dea31012005-04-17 16:05:31 -05004849
James Smart4f2e66c2012-05-09 21:17:07 -04004850 iocb = &lpfc_cmd->cur_iocbq;
4851 /* the command is in process of being cancelled */
4852 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004853 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004854 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4855 "3169 SCSI Layer abort requested I/O has been "
4856 "cancelled by LLD.\n");
4857 return FAILED;
4858 }
dea31012005-04-17 16:05:31 -05004859 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004860 * If pCmd field of the corresponding lpfc_scsi_buf structure
4861 * points to a different SCSI command, then the driver has
4862 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004863 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004864 */
James Smart4f2e66c2012-05-09 21:17:07 -04004865 if (lpfc_cmd->pCmd != cmnd) {
4866 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4867 "3170 SCSI Layer abort requested I/O has been "
4868 "completed by LLD.\n");
4869 goto out_unlock;
4870 }
dea31012005-04-17 16:05:31 -05004871
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004872 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004873
James Smart4f2e66c2012-05-09 21:17:07 -04004874 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004875 if (abtsiocb == NULL) {
4876 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004877 goto out_unlock;
dea31012005-04-17 16:05:31 -05004878 }
4879
dea31012005-04-17 16:05:31 -05004880 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004881 * The scsi command can not be in txq and it is in flight because the
4882 * pCmd is still pointig at the SCSI command we have to abort. There
4883 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004884 */
dea31012005-04-17 16:05:31 -05004885
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004886 cmd = &iocb->iocb;
4887 icmd = &abtsiocb->iocb;
4888 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4889 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004890 if (phba->sli_rev == LPFC_SLI_REV4)
4891 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4892 else
4893 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004894
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004895 icmd->ulpLe = 1;
4896 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004897
4898 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4899 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004900 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004901
James Smart2e0fef82007-06-17 19:56:36 -05004902 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004903 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4904 else
4905 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004906
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004907 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004908 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004909 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004910 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004911
James Smart3772a992009-05-22 14:50:54 -04004912 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4913 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004914 lpfc_sli_release_iocbq(phba, abtsiocb);
4915 ret = FAILED;
4916 goto out;
4917 }
4918
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004919 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004920 lpfc_sli_handle_fast_ring_event(phba,
4921 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004922
James Smartfa61a542008-01-11 01:52:42 -05004923 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004924 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004925 wait_event_timeout(waitq,
4926 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004927 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartfa61a542008-01-11 01:52:42 -05004928 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004929
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004930 if (lpfc_cmd->pCmd == cmnd) {
4931 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004932 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4933 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004934 "for abortng I/O (xri:x%x) to complete: "
4935 "ret %#x, ID %d, LUN %d\n",
4936 iocb->sli4_xritag, ret,
4937 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004938 }
James Smart4f2e66c2012-05-09 21:17:07 -04004939 goto out;
dea31012005-04-17 16:05:31 -05004940
James Smart4f2e66c2012-05-09 21:17:07 -04004941out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004942 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004943out:
James Smarte8b62012007-08-02 11:10:09 -04004944 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4945 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004946 "LUN %d\n", ret, cmnd->device->id,
4947 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004948 return ret;
dea31012005-04-17 16:05:31 -05004949}
4950
James Smartbbb9d182009-06-10 17:23:16 -04004951static char *
4952lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4953{
4954 switch (task_mgmt_cmd) {
4955 case FCP_ABORT_TASK_SET:
4956 return "ABORT_TASK_SET";
4957 case FCP_CLEAR_TASK_SET:
4958 return "FCP_CLEAR_TASK_SET";
4959 case FCP_BUS_RESET:
4960 return "FCP_BUS_RESET";
4961 case FCP_LUN_RESET:
4962 return "FCP_LUN_RESET";
4963 case FCP_TARGET_RESET:
4964 return "FCP_TARGET_RESET";
4965 case FCP_CLEAR_ACA:
4966 return "FCP_CLEAR_ACA";
4967 case FCP_TERMINATE_TASK:
4968 return "FCP_TERMINATE_TASK";
4969 default:
4970 return "unknown";
4971 }
4972}
4973
4974/**
4975 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4976 * @vport: The virtual port for which this call is being executed.
4977 * @rdata: Pointer to remote port local data
4978 * @tgt_id: Target ID of remote device.
4979 * @lun_id: Lun number for the TMF
4980 * @task_mgmt_cmd: type of TMF to send
4981 *
4982 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4983 * a remote port.
4984 *
4985 * Return Code:
4986 * 0x2003 - Error
4987 * 0x2002 - Success.
4988 **/
4989static int
4990lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4991 unsigned tgt_id, unsigned int lun_id,
4992 uint8_t task_mgmt_cmd)
4993{
4994 struct lpfc_hba *phba = vport->phba;
4995 struct lpfc_scsi_buf *lpfc_cmd;
4996 struct lpfc_iocbq *iocbq;
4997 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04004998 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04004999 int ret;
5000 int status;
5001
James Smart5989b8d2010-10-22 11:06:56 -04005002 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04005003 return FAILED;
5004
James Smart19ca7602010-11-20 23:11:55 -05005005 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04005006 if (lpfc_cmd == NULL)
5007 return FAILED;
5008 lpfc_cmd->timeout = 60;
5009 lpfc_cmd->rdata = rdata;
5010
5011 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5012 task_mgmt_cmd);
5013 if (!status) {
5014 lpfc_release_scsi_buf(phba, lpfc_cmd);
5015 return FAILED;
5016 }
5017
5018 iocbq = &lpfc_cmd->cur_iocbq;
5019 iocbqrsp = lpfc_sli_get_iocbq(phba);
5020 if (iocbqrsp == NULL) {
5021 lpfc_release_scsi_buf(phba, lpfc_cmd);
5022 return FAILED;
5023 }
5024
5025 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5026 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04005027 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005028 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005029 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5030 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005031
5032 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5033 iocbq, iocbqrsp, lpfc_cmd->timeout);
5034 if (status != IOCB_SUCCESS) {
5035 if (status == IOCB_TIMEDOUT) {
5036 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
5037 ret = TIMEOUT_ERROR;
5038 } else
5039 ret = FAILED;
5040 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5041 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04005042 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
5043 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005044 lpfc_taskmgmt_name(task_mgmt_cmd),
5045 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04005046 iocbqrsp->iocb.un.ulpWord[4],
5047 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04005048 } else if (status == IOCB_BUSY)
5049 ret = FAILED;
5050 else
James Smartbbb9d182009-06-10 17:23:16 -04005051 ret = SUCCESS;
5052
5053 lpfc_sli_release_iocbq(phba, iocbqrsp);
5054
5055 if (ret != TIMEOUT_ERROR)
5056 lpfc_release_scsi_buf(phba, lpfc_cmd);
5057
5058 return ret;
5059}
5060
5061/**
5062 * lpfc_chk_tgt_mapped -
5063 * @vport: The virtual port to check on
5064 * @cmnd: Pointer to scsi_cmnd data structure.
5065 *
5066 * This routine delays until the scsi target (aka rport) for the
5067 * command exists (is present and logged in) or we declare it non-existent.
5068 *
5069 * Return code :
5070 * 0x2003 - Error
5071 * 0x2002 - Success
5072 **/
5073static int
5074lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5075{
5076 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005077 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005078 unsigned long later;
5079
James Smart1c6f4ef52009-11-18 15:40:49 -05005080 if (!rdata) {
5081 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5082 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5083 return FAILED;
5084 }
5085 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005086 /*
5087 * If target is not in a MAPPED state, delay until
5088 * target is rediscovered or devloss timeout expires.
5089 */
5090 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5091 while (time_after(later, jiffies)) {
5092 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5093 return FAILED;
5094 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5095 return SUCCESS;
5096 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5097 rdata = cmnd->device->hostdata;
5098 if (!rdata)
5099 return FAILED;
5100 pnode = rdata->pnode;
5101 }
5102 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5103 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5104 return FAILED;
5105 return SUCCESS;
5106}
5107
5108/**
5109 * lpfc_reset_flush_io_context -
5110 * @vport: The virtual port (scsi_host) for the flush context
5111 * @tgt_id: If aborting by Target contect - specifies the target id
5112 * @lun_id: If aborting by Lun context - specifies the lun id
5113 * @context: specifies the context level to flush at.
5114 *
5115 * After a reset condition via TMF, we need to flush orphaned i/o
5116 * contexts from the adapter. This routine aborts any contexts
5117 * outstanding, then waits for their completions. The wait is
5118 * bounded by devloss_tmo though.
5119 *
5120 * Return code :
5121 * 0x2003 - Error
5122 * 0x2002 - Success
5123 **/
5124static int
5125lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5126 uint64_t lun_id, lpfc_ctx_cmd context)
5127{
5128 struct lpfc_hba *phba = vport->phba;
5129 unsigned long later;
5130 int cnt;
5131
5132 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5133 if (cnt)
5134 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
5135 tgt_id, lun_id, context);
5136 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5137 while (time_after(later, jiffies) && cnt) {
5138 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5139 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5140 }
5141 if (cnt) {
5142 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5143 "0724 I/O flush failure for context %s : cnt x%x\n",
5144 ((context == LPFC_CTX_LUN) ? "LUN" :
5145 ((context == LPFC_CTX_TGT) ? "TGT" :
5146 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5147 cnt);
5148 return FAILED;
5149 }
5150 return SUCCESS;
5151}
5152
James Smart9bad7672008-12-04 22:39:02 -05005153/**
James Smart3621a712009-04-06 18:47:14 -04005154 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005155 * @cmnd: Pointer to scsi_cmnd data structure.
5156 *
James Smartbbb9d182009-06-10 17:23:16 -04005157 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005158 * command.
5159 *
5160 * Return code :
5161 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005162 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005163 **/
dea31012005-04-17 16:05:31 -05005164static int
James Smart7054a602007-04-25 09:52:34 -04005165lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005166{
James Smart2e0fef82007-06-17 19:56:36 -05005167 struct Scsi_Host *shost = cmnd->device->host;
5168 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005169 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005170 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005171 unsigned tgt_id = cmnd->device->id;
5172 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005173 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005174 int status, ret = SUCCESS;
dea31012005-04-17 16:05:31 -05005175
James Smart1c6f4ef52009-11-18 15:40:49 -05005176 if (!rdata) {
5177 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5178 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5179 return FAILED;
5180 }
5181 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005182 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005183 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005184 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005185
5186 status = lpfc_chk_tgt_mapped(vport, cmnd);
5187 if (status == FAILED) {
5188 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5189 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5190 return FAILED;
5191 }
5192
5193 scsi_event.event_type = FC_REG_SCSI_EVENT;
5194 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5195 scsi_event.lun = lun_id;
5196 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5197 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5198
5199 fc_host_post_vendor_event(shost, fc_get_event_number(),
5200 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5201
5202 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5203 FCP_LUN_RESET);
5204
5205 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5206 "0713 SCSI layer issued Device Reset (%d, %d) "
5207 "return x%x\n", tgt_id, lun_id, status);
5208
dea31012005-04-17 16:05:31 -05005209 /*
James Smartbbb9d182009-06-10 17:23:16 -04005210 * We have to clean up i/o as : they may be orphaned by the TMF;
5211 * or if the TMF failed, they may be in an indeterminate state.
5212 * So, continue on.
5213 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005214 */
James Smart3a707302012-06-12 13:54:42 -04005215 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005216 LPFC_CTX_LUN);
James Smart3a707302012-06-12 13:54:42 -04005217 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04005218}
5219
5220/**
5221 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5222 * @cmnd: Pointer to scsi_cmnd data structure.
5223 *
5224 * This routine does a target reset by sending a TARGET_RESET task management
5225 * command.
5226 *
5227 * Return code :
5228 * 0x2003 - Error
5229 * 0x2002 - Success
5230 **/
5231static int
5232lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5233{
5234 struct Scsi_Host *shost = cmnd->device->host;
5235 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5236 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005237 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005238 unsigned tgt_id = cmnd->device->id;
5239 unsigned int lun_id = cmnd->device->lun;
5240 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005241 int status, ret = SUCCESS;
James Smartbbb9d182009-06-10 17:23:16 -04005242
James Smart1c6f4ef52009-11-18 15:40:49 -05005243 if (!rdata) {
5244 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5245 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5246 return FAILED;
5247 }
5248 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005249 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005250 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005251 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005252
5253 status = lpfc_chk_tgt_mapped(vport, cmnd);
5254 if (status == FAILED) {
5255 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5256 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5257 return FAILED;
dea31012005-04-17 16:05:31 -05005258 }
James Smartea2151b2008-09-07 11:52:10 -04005259
5260 scsi_event.event_type = FC_REG_SCSI_EVENT;
5261 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5262 scsi_event.lun = 0;
5263 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5264 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5265
James Smartbbb9d182009-06-10 17:23:16 -04005266 fc_host_post_vendor_event(shost, fc_get_event_number(),
5267 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005268
James Smartbbb9d182009-06-10 17:23:16 -04005269 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5270 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005271
James Smarte8b62012007-08-02 11:10:09 -04005272 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04005273 "0723 SCSI layer issued Target Reset (%d, %d) "
5274 "return x%x\n", tgt_id, lun_id, status);
5275
5276 /*
5277 * We have to clean up i/o as : they may be orphaned by the TMF;
5278 * or if the TMF failed, they may be in an indeterminate state.
5279 * So, continue on.
5280 * We will report success if all the i/o aborts successfully.
5281 */
James Smart3a707302012-06-12 13:54:42 -04005282 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5283 LPFC_CTX_TGT);
5284 return ret;
dea31012005-04-17 16:05:31 -05005285}
5286
James Smart9bad7672008-12-04 22:39:02 -05005287/**
James Smart3621a712009-04-06 18:47:14 -04005288 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005289 * @cmnd: Pointer to scsi_cmnd data structure.
5290 *
James Smartbbb9d182009-06-10 17:23:16 -04005291 * This routine does target reset to all targets on @cmnd->device->host.
5292 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005293 *
James Smartbbb9d182009-06-10 17:23:16 -04005294 * Return code :
5295 * 0x2003 - Error
5296 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005297 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005298static int
James Smart7054a602007-04-25 09:52:34 -04005299lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005300{
James Smart2e0fef82007-06-17 19:56:36 -05005301 struct Scsi_Host *shost = cmnd->device->host;
5302 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005303 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005304 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005305 int match;
5306 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005307
5308 scsi_event.event_type = FC_REG_SCSI_EVENT;
5309 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5310 scsi_event.lun = 0;
5311 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5312 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5313
James Smartbbb9d182009-06-10 17:23:16 -04005314 fc_host_post_vendor_event(shost, fc_get_event_number(),
5315 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005316
James Smartbf086112011-08-21 21:48:13 -04005317 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005318 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005319 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005320
dea31012005-04-17 16:05:31 -05005321 /*
5322 * Since the driver manages a single bus device, reset all
5323 * targets known to the driver. Should any target reset
5324 * fail, this routine returns failure to the midlayer.
5325 */
James Smarte17da182006-07-06 15:49:25 -04005326 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005327 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005328 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005329 spin_lock_irq(shost->host_lock);
5330 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005331 if (!NLP_CHK_NODE_ACT(ndlp))
5332 continue;
James Smarta6571c62012-10-31 14:44:42 -04005333 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5334 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5335 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005336 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005337 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005338 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005339 match = 1;
5340 break;
5341 }
5342 }
James Smart2e0fef82007-06-17 19:56:36 -05005343 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005344 if (!match)
5345 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005346
5347 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5348 i, 0, FCP_TARGET_RESET);
5349
5350 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005351 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5352 "0700 Bus Reset on target %d failed\n",
5353 i);
James Smart915caaa2008-06-14 22:52:38 -04005354 ret = FAILED;
dea31012005-04-17 16:05:31 -05005355 }
5356 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005357 /*
James Smartbbb9d182009-06-10 17:23:16 -04005358 * We have to clean up i/o as : they may be orphaned by the TMFs
5359 * above; or if any of the TMFs failed, they may be in an
5360 * indeterminate state.
5361 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005362 */
James Smartbbb9d182009-06-10 17:23:16 -04005363
5364 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5365 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005366 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005367
James Smarte8b62012007-08-02 11:10:09 -04005368 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5369 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005370 return ret;
5371}
5372
James Smart9bad7672008-12-04 22:39:02 -05005373/**
James Smart27b01b82012-05-09 21:19:44 -04005374 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5375 * @cmnd: Pointer to scsi_cmnd data structure.
5376 *
5377 * This routine does host reset to the adaptor port. It brings the HBA
5378 * offline, performs a board restart, and then brings the board back online.
5379 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5380 * reject all outstanding SCSI commands to the host and error returned
5381 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5382 * of error handling, it will only return error if resetting of the adapter
5383 * is not successful; in all other cases, will return success.
5384 *
5385 * Return code :
5386 * 0x2003 - Error
5387 * 0x2002 - Success
5388 **/
5389static int
5390lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5391{
5392 struct Scsi_Host *shost = cmnd->device->host;
5393 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5394 struct lpfc_hba *phba = vport->phba;
5395 int rc, ret = SUCCESS;
5396
James Smarta88dbb62013-04-17 20:18:39 -04005397 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5398 "3172 SCSI layer issued Host Reset Data:\n");
5399
James Smart618a5232012-06-12 13:54:36 -04005400 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005401 lpfc_offline(phba);
5402 rc = lpfc_sli_brdrestart(phba);
5403 if (rc)
5404 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005405 rc = lpfc_online(phba);
5406 if (rc)
5407 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005408 lpfc_unblock_mgmt_io(phba);
5409
James Smarta88dbb62013-04-17 20:18:39 -04005410 if (ret == FAILED) {
5411 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5412 "3323 Failed host reset, bring it offline\n");
5413 lpfc_sli4_offline_eratt(phba);
5414 }
James Smart27b01b82012-05-09 21:19:44 -04005415 return ret;
5416}
5417
5418/**
James Smart3621a712009-04-06 18:47:14 -04005419 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005420 * @sdev: Pointer to scsi_device.
5421 *
5422 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5423 * globally available list of scsi buffers. This routine also makes sure scsi
5424 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5425 * of scsi buffer exists for the lifetime of the driver.
5426 *
5427 * Return codes:
5428 * non-0 - Error
5429 * 0 - Success
5430 **/
dea31012005-04-17 16:05:31 -05005431static int
dea31012005-04-17 16:05:31 -05005432lpfc_slave_alloc(struct scsi_device *sdev)
5433{
James Smart2e0fef82007-06-17 19:56:36 -05005434 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5435 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005436 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005437 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005438 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005439 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005440 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05005441
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005442 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005443 return -ENXIO;
5444
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005445 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04005446 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005447
5448 /*
5449 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5450 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005451 * HBA limit conveyed to the midlayer via the host structure. The
5452 * formula accounts for the lun_queue_depth + error handlers + 1
5453 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005454 */
5455 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005456 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005457
James Smartd7c47992010-06-08 18:31:54 -04005458 /* If allocated buffers are enough do nothing */
5459 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5460 return 0;
5461
James Smart92d7f7b2007-06-17 19:56:38 -05005462 /* Allow some exchanges to be available always to complete discovery */
5463 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005464 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5465 "0704 At limitation of %d preallocated "
5466 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005467 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005468 /* Allow some exchanges to be available always to complete discovery */
5469 } else if (total + num_to_alloc >
5470 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005471 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5472 "0705 Allocation request of %d "
5473 "command buffers will exceed max of %d. "
5474 "Reducing allocation request to %d.\n",
5475 num_to_alloc, phba->cfg_hba_queue_depth,
5476 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005477 num_to_alloc = phba->cfg_hba_queue_depth - total;
5478 }
James Smart3772a992009-05-22 14:50:54 -04005479 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5480 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005481 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5482 "0708 Allocation request of %d "
5483 "command buffers did not succeed. "
5484 "Allocated %d buffers.\n",
5485 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005486 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005487 if (num_allocated > 0)
5488 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005489 return 0;
5490}
5491
James Smart9bad7672008-12-04 22:39:02 -05005492/**
James Smart3621a712009-04-06 18:47:14 -04005493 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005494 * @sdev: Pointer to scsi_device.
5495 *
5496 * This routine configures following items
5497 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005498 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5499 *
5500 * Return codes:
5501 * 0 - Success
5502 **/
dea31012005-04-17 16:05:31 -05005503static int
5504lpfc_slave_configure(struct scsi_device *sdev)
5505{
James Smart2e0fef82007-06-17 19:56:36 -05005506 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5507 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005508
5509 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005510 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005511 else
James Smart3de2a652007-08-02 11:09:59 -04005512 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005513
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005514 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005515 lpfc_sli_handle_fast_ring_event(phba,
5516 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005517 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5518 lpfc_poll_rearm_timer(phba);
5519 }
5520
dea31012005-04-17 16:05:31 -05005521 return 0;
5522}
5523
James Smart9bad7672008-12-04 22:39:02 -05005524/**
James Smart3621a712009-04-06 18:47:14 -04005525 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005526 * @sdev: Pointer to scsi_device.
5527 *
5528 * This routine sets @sdev hostatdata filed to null.
5529 **/
dea31012005-04-17 16:05:31 -05005530static void
5531lpfc_slave_destroy(struct scsi_device *sdev)
5532{
James Smartd7c47992010-06-08 18:31:54 -04005533 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5534 struct lpfc_hba *phba = vport->phba;
5535 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005536 sdev->hostdata = NULL;
5537 return;
5538}
5539
James Smart92d7f7b2007-06-17 19:56:38 -05005540
dea31012005-04-17 16:05:31 -05005541struct scsi_host_template lpfc_template = {
5542 .module = THIS_MODULE,
5543 .name = LPFC_DRIVER_NAME,
5544 .info = lpfc_info,
5545 .queuecommand = lpfc_queuecommand,
5546 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005547 .eh_device_reset_handler = lpfc_device_reset_handler,
5548 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005549 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005550 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005551 .slave_alloc = lpfc_slave_alloc,
5552 .slave_configure = lpfc_slave_configure,
5553 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005554 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005555 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005556 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005557 .cmd_per_lun = LPFC_CMD_PER_LUN,
5558 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005559 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005560 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005561 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005562 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005563 .change_queue_type = lpfc_change_queue_type,
dea31012005-04-17 16:05:31 -05005564};
James Smart3de2a652007-08-02 11:09:59 -04005565
5566struct scsi_host_template lpfc_vport_template = {
5567 .module = THIS_MODULE,
5568 .name = LPFC_DRIVER_NAME,
5569 .info = lpfc_info,
5570 .queuecommand = lpfc_queuecommand,
5571 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005572 .eh_device_reset_handler = lpfc_device_reset_handler,
5573 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005574 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5575 .slave_alloc = lpfc_slave_alloc,
5576 .slave_configure = lpfc_slave_configure,
5577 .slave_destroy = lpfc_slave_destroy,
5578 .scan_finished = lpfc_scan_finished,
5579 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005580 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005581 .cmd_per_lun = LPFC_CMD_PER_LUN,
5582 .use_clustering = ENABLE_CLUSTERING,
5583 .shost_attrs = lpfc_vport_attrs,
5584 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005585 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005586 .change_queue_type = lpfc_change_queue_type,
James Smart3de2a652007-08-02 11:09:59 -04005587};