blob: 556bc7a54a1f5905e10618a7f723c864546272db [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 Smart92c13f22013-05-31 17:05:45 -04004 * Copyright (C) 2004-2013 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);
James Smart164cecd2013-09-06 12:22:38 -0400929 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400930 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
931 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
James Smart164cecd2013-09-06 12:22:38 -0400932 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -0400933 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 Smart092cb032013-09-06 12:21:50 -04001003 /*
1004 * 4K Page alignment is CRITICAL to BlockGuard, double check
1005 * to be sure.
1006 */
1007 if (phba->cfg_enable_bg && (((unsigned long)(psb->data) &
1008 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
James Smart96f70772013-04-17 20:16:15 -04001009 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1010 psb->data, psb->dma_handle);
1011 kfree(psb);
1012 break;
1013 }
1014
James Smartda0436e2009-05-22 14:51:39 -04001015 /* Allocate iotag for psb->cur_iocbq. */
1016 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
1017 if (iotag == 0) {
James Smartb92938b2010-06-07 15:24:12 -04001018 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1019 psb->data, psb->dma_handle);
James Smartda0436e2009-05-22 14:51:39 -04001020 kfree(psb);
1021 break;
1022 }
1023
James Smart6d368e52011-05-24 11:44:12 -04001024 lxri = lpfc_sli4_next_xritag(phba);
1025 if (lxri == NO_XRI) {
James Smartda0436e2009-05-22 14:51:39 -04001026 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1027 psb->data, psb->dma_handle);
1028 kfree(psb);
1029 break;
1030 }
James Smart6d368e52011-05-24 11:44:12 -04001031 psb->cur_iocbq.sli4_lxritag = lxri;
1032 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
James Smartda0436e2009-05-22 14:51:39 -04001033 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Smartda0436e2009-05-22 14:51:39 -04001034 psb->fcp_bpl = psb->data;
James Smart96f70772013-04-17 20:16:15 -04001035 psb->fcp_cmnd = (psb->data + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001036 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
1037 sizeof(struct fcp_cmnd));
1038
1039 /* Initialize local short-hand pointers. */
1040 sgl = (struct sli4_sge *)psb->fcp_bpl;
1041 pdma_phys_bpl = psb->dma_handle;
James Smart96f70772013-04-17 20:16:15 -04001042 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
James Smartda0436e2009-05-22 14:51:39 -04001043 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
1044
1045 /*
James Smart8a9d2e82012-05-09 21:16:12 -04001046 * The first two bdes are the FCP_CMD and FCP_RSP.
1047 * The balance are sg list bdes. Initialize the
1048 * first two and leave the rest for queuecommand.
James Smartda0436e2009-05-22 14:51:39 -04001049 */
1050 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
1051 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
James Smart05580562011-05-24 11:40:48 -04001052 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001053 bf_set(lpfc_sli4_sge_last, sgl, 0);
1054 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001055 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
James Smartda0436e2009-05-22 14:51:39 -04001056 sgl++;
1057
1058 /* Setup the physical region for the FCP RSP */
1059 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1060 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
James Smart05580562011-05-24 11:40:48 -04001061 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04001062 bf_set(lpfc_sli4_sge_last, sgl, 1);
1063 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05001064 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
James Smartda0436e2009-05-22 14:51:39 -04001065
1066 /*
1067 * Since the IOCB for the FCP I/O is built into this
1068 * lpfc_scsi_buf, initialize it with all known data now.
1069 */
1070 iocb = &psb->cur_iocbq.iocb;
1071 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1072 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1073 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1074 * We are setting the bpl to point to out sgl. An sgl's
1075 * entries are 16 bytes, a bpl entries are 12 bytes.
1076 */
1077 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1078 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1079 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1080 iocb->ulpBdeCount = 1;
1081 iocb->ulpLe = 1;
1082 iocb->ulpClass = CLASS3;
James Smart8a9d2e82012-05-09 21:16:12 -04001083 psb->cur_iocbq.context1 = psb;
James Smartda0436e2009-05-22 14:51:39 -04001084 psb->dma_phys_bpl = pdma_phys_bpl;
James Smart6d368e52011-05-24 11:44:12 -04001085
James Smart8a9d2e82012-05-09 21:16:12 -04001086 /* add the scsi buffer to a post list */
1087 list_add_tail(&psb->list, &post_sblist);
James Smarta40fc5f2013-04-17 20:17:40 -04001088 spin_lock_irq(&phba->scsi_buf_list_get_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04001089 phba->sli4_hba.scsi_xri_cnt++;
James Smarta40fc5f2013-04-17 20:17:40 -04001090 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
James Smartda0436e2009-05-22 14:51:39 -04001091 }
James Smart8a9d2e82012-05-09 21:16:12 -04001092 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1093 "3021 Allocate %d out of %d requested new SCSI "
1094 "buffers\n", bcnt, num_to_alloc);
James Smartda0436e2009-05-22 14:51:39 -04001095
James Smart8a9d2e82012-05-09 21:16:12 -04001096 /* post the list of scsi buffer sgls to port if available */
1097 if (!list_empty(&post_sblist))
1098 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1099 &post_sblist, bcnt);
1100 else
1101 num_posted = 0;
1102
1103 return num_posted;
James Smartda0436e2009-05-22 14:51:39 -04001104}
1105
1106/**
James Smart3772a992009-05-22 14:50:54 -04001107 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1108 * @vport: The virtual port for which this call being executed.
1109 * @num_to_allocate: The requested number of buffers to allocate.
1110 *
1111 * This routine wraps the actual SCSI buffer allocator function pointer from
1112 * the lpfc_hba struct.
1113 *
1114 * Return codes:
1115 * int - number of scsi buffers that were allocated.
1116 * 0 = failure, less than num_to_alloc is a partial failure.
1117 **/
1118static inline int
1119lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1120{
1121 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1122}
1123
1124/**
James Smart19ca7602010-11-20 23:11:55 -05001125 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
James Smart3772a992009-05-22 14:50:54 -04001126 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05001127 *
1128 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1129 * and returns to caller.
1130 *
1131 * Return codes:
1132 * NULL - Error
1133 * Pointer to lpfc_scsi_buf - Success
1134 **/
Adrian Bunk455c53e2006-01-06 20:21:28 +01001135static struct lpfc_scsi_buf*
James Smart19ca7602010-11-20 23:11:55 -05001136lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05001137{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001138 struct lpfc_scsi_buf * lpfc_cmd = NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001139 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
James Smart164cecd2013-09-06 12:22:38 -04001140 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001141
James Smart164cecd2013-09-06 12:22:38 -04001142 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smarta40fc5f2013-04-17 20:17:40 -04001143 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
1144 list);
1145 if (!lpfc_cmd) {
James Smart164cecd2013-09-06 12:22:38 -04001146 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001147 list_splice(&phba->lpfc_scsi_buf_list_put,
1148 &phba->lpfc_scsi_buf_list_get);
1149 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1150 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1151 struct lpfc_scsi_buf, list);
James Smart164cecd2013-09-06 12:22:38 -04001152 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart1dcb58e2007-04-25 09:51:30 -04001153 }
James Smart164cecd2013-09-06 12:22:38 -04001154 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001155 return lpfc_cmd;
1156}
James Smart19ca7602010-11-20 23:11:55 -05001157/**
1158 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1159 * @phba: The HBA for which this call is being executed.
1160 *
1161 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1162 * and returns to caller.
1163 *
1164 * Return codes:
1165 * NULL - Error
1166 * Pointer to lpfc_scsi_buf - Success
1167 **/
1168static struct lpfc_scsi_buf*
1169lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1170{
James Smart3be30e02013-05-31 17:05:17 -04001171 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
James Smart164cecd2013-09-06 12:22:38 -04001172 unsigned long iflag = 0;
James Smart19ca7602010-11-20 23:11:55 -05001173 int found = 0;
1174
James Smart164cecd2013-09-06 12:22:38 -04001175 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
James Smart3be30e02013-05-31 17:05:17 -04001176 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1177 &phba->lpfc_scsi_buf_list_get, list) {
James Smart19ca7602010-11-20 23:11:55 -05001178 if (lpfc_test_rrq_active(phba, ndlp,
James Smartee0f4fe2012-05-09 21:19:14 -04001179 lpfc_cmd->cur_iocbq.sli4_lxritag))
James Smart1151e3e2011-02-16 12:39:35 -05001180 continue;
1181 list_del(&lpfc_cmd->list);
James Smart19ca7602010-11-20 23:11:55 -05001182 found = 1;
James Smart1151e3e2011-02-16 12:39:35 -05001183 break;
James Smart19ca7602010-11-20 23:11:55 -05001184 }
James Smarta40fc5f2013-04-17 20:17:40 -04001185 if (!found) {
James Smart164cecd2013-09-06 12:22:38 -04001186 spin_lock(&phba->scsi_buf_list_put_lock);
James Smarta40fc5f2013-04-17 20:17:40 -04001187 list_splice(&phba->lpfc_scsi_buf_list_put,
1188 &phba->lpfc_scsi_buf_list_get);
1189 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
James Smart164cecd2013-09-06 12:22:38 -04001190 spin_unlock(&phba->scsi_buf_list_put_lock);
James Smart3be30e02013-05-31 17:05:17 -04001191 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1192 &phba->lpfc_scsi_buf_list_get, list) {
James Smarta40fc5f2013-04-17 20:17:40 -04001193 if (lpfc_test_rrq_active(
1194 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1195 continue;
1196 list_del(&lpfc_cmd->list);
1197 found = 1;
1198 break;
1199 }
1200 }
James Smart164cecd2013-09-06 12:22:38 -04001201 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
James Smart1151e3e2011-02-16 12:39:35 -05001202 if (!found)
1203 return NULL;
James Smarta40fc5f2013-04-17 20:17:40 -04001204 return lpfc_cmd;
James Smart19ca7602010-11-20 23:11:55 -05001205}
1206/**
1207 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1208 * @phba: The HBA for which this call is being executed.
1209 *
1210 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1211 * and returns to caller.
1212 *
1213 * Return codes:
1214 * NULL - Error
1215 * Pointer to lpfc_scsi_buf - Success
1216 **/
1217static struct lpfc_scsi_buf*
1218lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1219{
1220 return phba->lpfc_get_scsi_buf(phba, ndlp);
1221}
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001222
James Smart9bad7672008-12-04 22:39:02 -05001223/**
James Smart3772a992009-05-22 14:50:54 -04001224 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
James Smart9bad7672008-12-04 22:39:02 -05001225 * @phba: The Hba for which this call is being executed.
1226 * @psb: The scsi buffer which is being released.
1227 *
1228 * This routine releases @psb scsi buffer by adding it to tail of @phba
1229 * lpfc_scsi_buf_list list.
1230 **/
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001231static void
James Smart3772a992009-05-22 14:50:54 -04001232lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001233{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001234 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -05001235
James Smarta40fc5f2013-04-17 20:17:40 -04001236 psb->seg_cnt = 0;
1237 psb->nonsg_phys = 0;
1238 psb->prot_seg_cnt = 0;
1239
1240 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001241 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001242 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001243 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1244 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
dea31012005-04-17 16:05:31 -05001245}
1246
James Smart9bad7672008-12-04 22:39:02 -05001247/**
James Smartda0436e2009-05-22 14:51:39 -04001248 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1249 * @phba: The Hba for which this call is being executed.
1250 * @psb: The scsi buffer which is being released.
1251 *
1252 * This routine releases @psb scsi buffer by adding it to tail of @phba
1253 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1254 * and cannot be reused for at least RA_TOV amount of time if it was
1255 * aborted.
1256 **/
1257static void
1258lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1259{
1260 unsigned long iflag = 0;
1261
James Smarta40fc5f2013-04-17 20:17:40 -04001262 psb->seg_cnt = 0;
1263 psb->nonsg_phys = 0;
1264 psb->prot_seg_cnt = 0;
1265
James Smart341af102010-01-26 23:07:37 -05001266 if (psb->exch_busy) {
James Smartda0436e2009-05-22 14:51:39 -04001267 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1268 iflag);
1269 psb->pCmd = NULL;
1270 list_add_tail(&psb->list,
1271 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1272 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1273 iflag);
1274 } else {
James Smartda0436e2009-05-22 14:51:39 -04001275 psb->pCmd = NULL;
James Smart6a485eb2013-04-17 20:19:00 -04001276 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
James Smarta40fc5f2013-04-17 20:17:40 -04001277 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1278 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1279 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
James Smartda0436e2009-05-22 14:51:39 -04001280 }
1281}
1282
1283/**
James Smart3772a992009-05-22 14:50:54 -04001284 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1285 * @phba: The Hba for which this call is being executed.
1286 * @psb: The scsi buffer which is being released.
1287 *
1288 * This routine releases @psb scsi buffer by adding it to tail of @phba
1289 * lpfc_scsi_buf_list list.
1290 **/
1291static void
1292lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1293{
1294
1295 phba->lpfc_release_scsi_buf(phba, psb);
1296}
1297
1298/**
1299 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
James Smart9bad7672008-12-04 22:39:02 -05001300 * @phba: The Hba for which this call is being executed.
1301 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1302 *
1303 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
James Smart3772a992009-05-22 14:50:54 -04001304 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1305 * through sg elements and format the bdea. This routine also initializes all
1306 * IOCB fields which are dependent on scsi command request buffer.
James Smart9bad7672008-12-04 22:39:02 -05001307 *
1308 * Return codes:
1309 * 1 - Error
1310 * 0 - Success
1311 **/
dea31012005-04-17 16:05:31 -05001312static int
James Smart3772a992009-05-22 14:50:54 -04001313lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -05001314{
1315 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1316 struct scatterlist *sgel = NULL;
1317 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1318 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
James Smart0f65ff62010-02-26 14:14:23 -05001319 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
dea31012005-04-17 16:05:31 -05001320 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart34b02dc2008-08-24 21:49:55 -04001321 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
dea31012005-04-17 16:05:31 -05001322 dma_addr_t physaddr;
James Smart34b02dc2008-08-24 21:49:55 -04001323 uint32_t num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001324 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -05001325
1326 /*
1327 * There are three possibilities here - use scatter-gather segment, use
1328 * the single mapping, or neither. Start the lpfc command prep by
1329 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1330 * data bde entry.
1331 */
1332 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001333 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05001334 /*
1335 * The driver stores the segment count returned from pci_map_sg
1336 * because this a count of dma-mappings used to map the use_sg
1337 * pages. They are not guaranteed to be the same for those
1338 * architectures that implement an IOMMU.
1339 */
dea31012005-04-17 16:05:31 -05001340
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001341 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1342 scsi_sg_count(scsi_cmnd), datadir);
1343 if (unlikely(!nseg))
1344 return 1;
1345
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001346 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -05001347 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04001348 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1349 "9064 BLKGRD: %s: Too many sg segments from "
James Smarte2a0a9d2008-12-04 22:40:02 -05001350 "dma_map_sg. Config %d, seg_cnt %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001351 __func__, phba->cfg_sg_seg_cnt,
dea31012005-04-17 16:05:31 -05001352 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04001353 lpfc_cmd->seg_cnt = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05001354 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001355 return 1;
1356 }
1357
1358 /*
1359 * The driver established a maximum scatter-gather segment count
1360 * during probe that limits the number of sg elements in any
1361 * single scsi command. Just run through the seg_cnt and format
1362 * the bde's.
James Smart34b02dc2008-08-24 21:49:55 -04001363 * When using SLI-3 the driver will try to fit all the BDEs into
1364 * the IOCB. If it can't then the BDEs get added to a BPL as it
1365 * does for SLI-2 mode.
dea31012005-04-17 16:05:31 -05001366 */
James Smart34b02dc2008-08-24 21:49:55 -04001367 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
dea31012005-04-17 16:05:31 -05001368 physaddr = sg_dma_address(sgel);
James Smart34b02dc2008-08-24 21:49:55 -04001369 if (phba->sli_rev == 3 &&
James Smarte2a0a9d2008-12-04 22:40:02 -05001370 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
James Smart0f65ff62010-02-26 14:14:23 -05001371 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
James Smart34b02dc2008-08-24 21:49:55 -04001372 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1373 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1374 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1375 data_bde->addrLow = putPaddrLow(physaddr);
1376 data_bde->addrHigh = putPaddrHigh(physaddr);
1377 data_bde++;
1378 } else {
1379 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1380 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1381 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1382 bpl->addrLow =
1383 le32_to_cpu(putPaddrLow(physaddr));
1384 bpl->addrHigh =
1385 le32_to_cpu(putPaddrHigh(physaddr));
1386 bpl++;
1387 }
dea31012005-04-17 16:05:31 -05001388 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -07001389 }
dea31012005-04-17 16:05:31 -05001390
1391 /*
1392 * Finish initializing those IOCB fields that are dependent on the
James Smart34b02dc2008-08-24 21:49:55 -04001393 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1394 * explicitly reinitialized and for SLI-3 the extended bde count is
1395 * explicitly reinitialized since all iocb memory resources are reused.
dea31012005-04-17 16:05:31 -05001396 */
James Smarte2a0a9d2008-12-04 22:40:02 -05001397 if (phba->sli_rev == 3 &&
James Smart0f65ff62010-02-26 14:14:23 -05001398 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1399 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
James Smart34b02dc2008-08-24 21:49:55 -04001400 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1401 /*
1402 * The extended IOCB format can only fit 3 BDE or a BPL.
1403 * This I/O has more than 3 BDE so the 1st data bde will
1404 * be a BPL that is filled in here.
1405 */
1406 physaddr = lpfc_cmd->dma_handle;
1407 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1408 data_bde->tus.f.bdeSize = (num_bde *
1409 sizeof(struct ulp_bde64));
1410 physaddr += (sizeof(struct fcp_cmnd) +
1411 sizeof(struct fcp_rsp) +
1412 (2 * sizeof(struct ulp_bde64)));
1413 data_bde->addrHigh = putPaddrHigh(physaddr);
1414 data_bde->addrLow = putPaddrLow(physaddr);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001415 /* ebde count includes the response bde and data bpl */
James Smart34b02dc2008-08-24 21:49:55 -04001416 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1417 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001418 /* ebde count includes the response bde and data bdes */
James Smart34b02dc2008-08-24 21:49:55 -04001419 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1420 }
1421 } else {
1422 iocb_cmd->un.fcpi64.bdl.bdeSize =
1423 ((num_bde + 2) * sizeof(struct ulp_bde64));
James Smart0f65ff62010-02-26 14:14:23 -05001424 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
James Smart34b02dc2008-08-24 21:49:55 -04001425 }
James Smart09372822008-01-11 01:52:54 -05001426 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
James Smarte2a0a9d2008-12-04 22:40:02 -05001427
1428 /*
1429 * Due to difference in data length between DIF/non-DIF paths,
1430 * we need to set word 4 of IOCB here
1431 */
James Smarta257bf92009-04-06 18:48:10 -04001432 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -05001433 return 0;
1434}
1435
James Smartf9bb2da2011-10-10 21:34:11 -04001436#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05001437
James Smart9a6b09c2012-03-01 22:37:42 -05001438/* Return if if error injection is detected by Initiator */
1439#define BG_ERR_INIT 0x1
1440/* Return if if error injection is detected by Target */
1441#define BG_ERR_TGT 0x2
1442/* Return if if swapping CSUM<-->CRC is required for error injection */
1443#define BG_ERR_SWAP 0x10
1444/* Return if disabling Guard/Ref/App checking is required for error injection */
1445#define BG_ERR_CHECK 0x20
James Smartacd68592012-01-18 16:25:09 -05001446
1447/**
1448 * lpfc_bg_err_inject - Determine if we should inject an error
1449 * @phba: The Hba for which this call is being executed.
James Smartf9bb2da2011-10-10 21:34:11 -04001450 * @sc: The SCSI command to examine
1451 * @reftag: (out) BlockGuard reference tag for transmitted data
1452 * @apptag: (out) BlockGuard application tag for transmitted data
1453 * @new_guard (in) Value to replace CRC with if needed
1454 *
James Smart9a6b09c2012-03-01 22:37:42 -05001455 * Returns BG_ERR_* bit mask or 0 if request ignored
James Smartacd68592012-01-18 16:25:09 -05001456 **/
James Smartf9bb2da2011-10-10 21:34:11 -04001457static int
1458lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1459 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1460{
1461 struct scatterlist *sgpe; /* s/g prot entry */
1462 struct scatterlist *sgde; /* s/g data entry */
James Smart9a6b09c2012-03-01 22:37:42 -05001463 struct lpfc_scsi_buf *lpfc_cmd = NULL;
James Smartacd68592012-01-18 16:25:09 -05001464 struct scsi_dif_tuple *src = NULL;
James Smart4ac9b222012-03-01 22:38:29 -05001465 struct lpfc_nodelist *ndlp;
1466 struct lpfc_rport_data *rdata;
James Smartf9bb2da2011-10-10 21:34:11 -04001467 uint32_t op = scsi_get_prot_op(sc);
1468 uint32_t blksize;
1469 uint32_t numblks;
1470 sector_t lba;
1471 int rc = 0;
James Smartacd68592012-01-18 16:25:09 -05001472 int blockoff = 0;
James Smartf9bb2da2011-10-10 21:34:11 -04001473
1474 if (op == SCSI_PROT_NORMAL)
1475 return 0;
1476
James Smartacd68592012-01-18 16:25:09 -05001477 sgpe = scsi_prot_sglist(sc);
1478 sgde = scsi_sglist(sc);
James Smartf9bb2da2011-10-10 21:34:11 -04001479 lba = scsi_get_lba(sc);
James Smart4ac9b222012-03-01 22:38:29 -05001480
1481 /* First check if we need to match the LBA */
James Smartf9bb2da2011-10-10 21:34:11 -04001482 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1483 blksize = lpfc_cmd_blksize(sc);
1484 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1485
1486 /* Make sure we have the right LBA if one is specified */
1487 if ((phba->lpfc_injerr_lba < lba) ||
1488 (phba->lpfc_injerr_lba >= (lba + numblks)))
1489 return 0;
James Smartacd68592012-01-18 16:25:09 -05001490 if (sgpe) {
1491 blockoff = phba->lpfc_injerr_lba - lba;
1492 numblks = sg_dma_len(sgpe) /
1493 sizeof(struct scsi_dif_tuple);
1494 if (numblks < blockoff)
1495 blockoff = numblks;
James Smartacd68592012-01-18 16:25:09 -05001496 }
James Smartf9bb2da2011-10-10 21:34:11 -04001497 }
1498
James Smart4ac9b222012-03-01 22:38:29 -05001499 /* Next check if we need to match the remote NPortID or WWPN */
1500 rdata = sc->device->hostdata;
1501 if (rdata && rdata->pnode) {
1502 ndlp = rdata->pnode;
1503
1504 /* Make sure we have the right NPortID if one is specified */
1505 if (phba->lpfc_injerr_nportid &&
1506 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1507 return 0;
1508
1509 /*
1510 * Make sure we have the right WWPN if one is specified.
1511 * wwn[0] should be a non-zero NAA in a good WWPN.
1512 */
1513 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1514 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1515 sizeof(struct lpfc_name)) != 0))
1516 return 0;
1517 }
1518
1519 /* Setup a ptr to the protection data if the SCSI host provides it */
1520 if (sgpe) {
1521 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1522 src += blockoff;
1523 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1524 }
1525
James Smartf9bb2da2011-10-10 21:34:11 -04001526 /* Should we change the Reference Tag */
1527 if (reftag) {
James Smartacd68592012-01-18 16:25:09 -05001528 if (phba->lpfc_injerr_wref_cnt) {
1529 switch (op) {
1530 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001531 if (src) {
1532 /*
1533 * For WRITE_PASS, force the error
1534 * to be sent on the wire. It should
1535 * be detected by the Target.
1536 * If blockoff != 0 error will be
1537 * inserted in middle of the IO.
1538 */
James Smartf9bb2da2011-10-10 21:34:11 -04001539
James Smartacd68592012-01-18 16:25:09 -05001540 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1541 "9076 BLKGRD: Injecting reftag error: "
1542 "write lba x%lx + x%x oldrefTag x%x\n",
1543 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001544 be32_to_cpu(src->ref_tag));
James Smartacd68592012-01-18 16:25:09 -05001545
1546 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001547 * Save the old ref_tag so we can
1548 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001549 */
James Smart9a6b09c2012-03-01 22:37:42 -05001550 if (lpfc_cmd) {
1551 lpfc_cmd->prot_data_type =
1552 LPFC_INJERR_REFTAG;
1553 lpfc_cmd->prot_data_segment =
1554 src;
1555 lpfc_cmd->prot_data =
1556 src->ref_tag;
1557 }
1558 src->ref_tag = cpu_to_be32(0xDEADBEEF);
James Smartacd68592012-01-18 16:25:09 -05001559 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001560 if (phba->lpfc_injerr_wref_cnt == 0) {
1561 phba->lpfc_injerr_nportid = 0;
1562 phba->lpfc_injerr_lba =
1563 LPFC_INJERR_LBA_OFF;
1564 memset(&phba->lpfc_injerr_wwpn,
1565 0, sizeof(struct lpfc_name));
1566 }
James Smart9a6b09c2012-03-01 22:37:42 -05001567 rc = BG_ERR_TGT | BG_ERR_CHECK;
1568
James Smartacd68592012-01-18 16:25:09 -05001569 break;
1570 }
1571 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001572 case SCSI_PROT_WRITE_INSERT:
1573 /*
1574 * For WRITE_INSERT, force the error
1575 * to be sent on the wire. It should be
1576 * detected by the Target.
1577 */
1578 /* DEADBEEF will be the reftag on the wire */
1579 *reftag = 0xDEADBEEF;
1580 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001581 if (phba->lpfc_injerr_wref_cnt == 0) {
1582 phba->lpfc_injerr_nportid = 0;
1583 phba->lpfc_injerr_lba =
1584 LPFC_INJERR_LBA_OFF;
1585 memset(&phba->lpfc_injerr_wwpn,
1586 0, sizeof(struct lpfc_name));
1587 }
James Smart9a6b09c2012-03-01 22:37:42 -05001588 rc = BG_ERR_TGT | BG_ERR_CHECK;
1589
1590 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1591 "9078 BLKGRD: Injecting reftag error: "
1592 "write lba x%lx\n", (unsigned long)lba);
1593 break;
James Smartacd68592012-01-18 16:25:09 -05001594 case SCSI_PROT_WRITE_STRIP:
1595 /*
1596 * For WRITE_STRIP and WRITE_PASS,
1597 * force the error on data
1598 * being copied from SLI-Host to SLI-Port.
1599 */
1600 *reftag = 0xDEADBEEF;
1601 phba->lpfc_injerr_wref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001602 if (phba->lpfc_injerr_wref_cnt == 0) {
1603 phba->lpfc_injerr_nportid = 0;
1604 phba->lpfc_injerr_lba =
1605 LPFC_INJERR_LBA_OFF;
1606 memset(&phba->lpfc_injerr_wwpn,
1607 0, sizeof(struct lpfc_name));
1608 }
James Smartacd68592012-01-18 16:25:09 -05001609 rc = BG_ERR_INIT;
1610
1611 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1612 "9077 BLKGRD: Injecting reftag error: "
1613 "write lba x%lx\n", (unsigned long)lba);
1614 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001615 }
James Smartacd68592012-01-18 16:25:09 -05001616 }
1617 if (phba->lpfc_injerr_rref_cnt) {
1618 switch (op) {
1619 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001620 case SCSI_PROT_READ_STRIP:
1621 case SCSI_PROT_READ_PASS:
1622 /*
1623 * For READ_STRIP and READ_PASS, force the
1624 * error on data being read off the wire. It
1625 * should force an IO error to the driver.
1626 */
James Smartf9bb2da2011-10-10 21:34:11 -04001627 *reftag = 0xDEADBEEF;
1628 phba->lpfc_injerr_rref_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001629 if (phba->lpfc_injerr_rref_cnt == 0) {
1630 phba->lpfc_injerr_nportid = 0;
1631 phba->lpfc_injerr_lba =
1632 LPFC_INJERR_LBA_OFF;
1633 memset(&phba->lpfc_injerr_wwpn,
1634 0, sizeof(struct lpfc_name));
1635 }
James Smartacd68592012-01-18 16:25:09 -05001636 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001637
1638 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001639 "9079 BLKGRD: Injecting reftag error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001640 "read lba x%lx\n", (unsigned long)lba);
James Smartacd68592012-01-18 16:25:09 -05001641 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001642 }
1643 }
1644 }
1645
1646 /* Should we change the Application Tag */
1647 if (apptag) {
James Smartacd68592012-01-18 16:25:09 -05001648 if (phba->lpfc_injerr_wapp_cnt) {
1649 switch (op) {
1650 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001651 if (src) {
James Smart9a6b09c2012-03-01 22:37:42 -05001652 /*
1653 * For WRITE_PASS, force the error
1654 * to be sent on the wire. It should
1655 * be detected by the Target.
1656 * If blockoff != 0 error will be
1657 * inserted in middle of the IO.
1658 */
1659
James Smartacd68592012-01-18 16:25:09 -05001660 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1661 "9080 BLKGRD: Injecting apptag error: "
1662 "write lba x%lx + x%x oldappTag x%x\n",
1663 (unsigned long)lba, blockoff,
James Smart9a6b09c2012-03-01 22:37:42 -05001664 be16_to_cpu(src->app_tag));
James Smartacd68592012-01-18 16:25:09 -05001665
1666 /*
James Smart9a6b09c2012-03-01 22:37:42 -05001667 * Save the old app_tag so we can
1668 * restore it on completion.
James Smartacd68592012-01-18 16:25:09 -05001669 */
James Smart9a6b09c2012-03-01 22:37:42 -05001670 if (lpfc_cmd) {
1671 lpfc_cmd->prot_data_type =
1672 LPFC_INJERR_APPTAG;
1673 lpfc_cmd->prot_data_segment =
1674 src;
1675 lpfc_cmd->prot_data =
1676 src->app_tag;
1677 }
1678 src->app_tag = cpu_to_be16(0xDEAD);
James Smartacd68592012-01-18 16:25:09 -05001679 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001680 if (phba->lpfc_injerr_wapp_cnt == 0) {
1681 phba->lpfc_injerr_nportid = 0;
1682 phba->lpfc_injerr_lba =
1683 LPFC_INJERR_LBA_OFF;
1684 memset(&phba->lpfc_injerr_wwpn,
1685 0, sizeof(struct lpfc_name));
1686 }
James Smart9a6b09c2012-03-01 22:37:42 -05001687 rc = BG_ERR_TGT | BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001688 break;
1689 }
1690 /* Drop thru */
James Smart9a6b09c2012-03-01 22:37:42 -05001691 case SCSI_PROT_WRITE_INSERT:
1692 /*
1693 * For WRITE_INSERT, force the
1694 * error to be sent on the wire. It should be
1695 * detected by the Target.
1696 */
1697 /* DEAD will be the apptag on the wire */
1698 *apptag = 0xDEAD;
1699 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001700 if (phba->lpfc_injerr_wapp_cnt == 0) {
1701 phba->lpfc_injerr_nportid = 0;
1702 phba->lpfc_injerr_lba =
1703 LPFC_INJERR_LBA_OFF;
1704 memset(&phba->lpfc_injerr_wwpn,
1705 0, sizeof(struct lpfc_name));
1706 }
James Smart9a6b09c2012-03-01 22:37:42 -05001707 rc = BG_ERR_TGT | BG_ERR_CHECK;
1708
1709 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1710 "0813 BLKGRD: Injecting apptag error: "
1711 "write lba x%lx\n", (unsigned long)lba);
1712 break;
James Smartacd68592012-01-18 16:25:09 -05001713 case SCSI_PROT_WRITE_STRIP:
1714 /*
1715 * For WRITE_STRIP and WRITE_PASS,
1716 * force the error on data
1717 * being copied from SLI-Host to SLI-Port.
1718 */
1719 *apptag = 0xDEAD;
1720 phba->lpfc_injerr_wapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001721 if (phba->lpfc_injerr_wapp_cnt == 0) {
1722 phba->lpfc_injerr_nportid = 0;
1723 phba->lpfc_injerr_lba =
1724 LPFC_INJERR_LBA_OFF;
1725 memset(&phba->lpfc_injerr_wwpn,
1726 0, sizeof(struct lpfc_name));
1727 }
James Smartacd68592012-01-18 16:25:09 -05001728 rc = BG_ERR_INIT;
1729
1730 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1731 "0812 BLKGRD: Injecting apptag error: "
1732 "write lba x%lx\n", (unsigned long)lba);
1733 break;
James Smartf9bb2da2011-10-10 21:34:11 -04001734 }
James Smartacd68592012-01-18 16:25:09 -05001735 }
1736 if (phba->lpfc_injerr_rapp_cnt) {
1737 switch (op) {
1738 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001739 case SCSI_PROT_READ_STRIP:
1740 case SCSI_PROT_READ_PASS:
1741 /*
1742 * For READ_STRIP and READ_PASS, force the
1743 * error on data being read off the wire. It
1744 * should force an IO error to the driver.
1745 */
James Smartf9bb2da2011-10-10 21:34:11 -04001746 *apptag = 0xDEAD;
1747 phba->lpfc_injerr_rapp_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001748 if (phba->lpfc_injerr_rapp_cnt == 0) {
1749 phba->lpfc_injerr_nportid = 0;
1750 phba->lpfc_injerr_lba =
1751 LPFC_INJERR_LBA_OFF;
1752 memset(&phba->lpfc_injerr_wwpn,
1753 0, sizeof(struct lpfc_name));
1754 }
James Smartacd68592012-01-18 16:25:09 -05001755 rc = BG_ERR_INIT;
James Smartf9bb2da2011-10-10 21:34:11 -04001756
1757 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smartacd68592012-01-18 16:25:09 -05001758 "0814 BLKGRD: Injecting apptag error: "
1759 "read lba x%lx\n", (unsigned long)lba);
1760 break;
1761 }
1762 }
1763 }
1764
1765
1766 /* Should we change the Guard Tag */
1767 if (new_guard) {
1768 if (phba->lpfc_injerr_wgrd_cnt) {
1769 switch (op) {
1770 case SCSI_PROT_WRITE_PASS:
James Smart9a6b09c2012-03-01 22:37:42 -05001771 rc = BG_ERR_CHECK;
James Smartacd68592012-01-18 16:25:09 -05001772 /* Drop thru */
James Smartacd68592012-01-18 16:25:09 -05001773
James Smartacd68592012-01-18 16:25:09 -05001774 case SCSI_PROT_WRITE_INSERT:
1775 /*
1776 * For WRITE_INSERT, force the
1777 * error to be sent on the wire. It should be
1778 * detected by the Target.
1779 */
1780 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001781 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1782 phba->lpfc_injerr_nportid = 0;
1783 phba->lpfc_injerr_lba =
1784 LPFC_INJERR_LBA_OFF;
1785 memset(&phba->lpfc_injerr_wwpn,
1786 0, sizeof(struct lpfc_name));
1787 }
James Smartacd68592012-01-18 16:25:09 -05001788
James Smart9a6b09c2012-03-01 22:37:42 -05001789 rc |= BG_ERR_TGT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001790 /* Signals the caller to swap CRC->CSUM */
1791
1792 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1793 "0817 BLKGRD: Injecting guard error: "
1794 "write lba x%lx\n", (unsigned long)lba);
1795 break;
James Smart9a6b09c2012-03-01 22:37:42 -05001796 case SCSI_PROT_WRITE_STRIP:
1797 /*
1798 * For WRITE_STRIP and WRITE_PASS,
1799 * force the error on data
1800 * being copied from SLI-Host to SLI-Port.
1801 */
1802 phba->lpfc_injerr_wgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001803 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1804 phba->lpfc_injerr_nportid = 0;
1805 phba->lpfc_injerr_lba =
1806 LPFC_INJERR_LBA_OFF;
1807 memset(&phba->lpfc_injerr_wwpn,
1808 0, sizeof(struct lpfc_name));
1809 }
James Smart9a6b09c2012-03-01 22:37:42 -05001810
1811 rc = BG_ERR_INIT | BG_ERR_SWAP;
1812 /* Signals the caller to swap CRC->CSUM */
1813
1814 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1815 "0816 BLKGRD: Injecting guard error: "
1816 "write lba x%lx\n", (unsigned long)lba);
1817 break;
James Smartacd68592012-01-18 16:25:09 -05001818 }
1819 }
1820 if (phba->lpfc_injerr_rgrd_cnt) {
1821 switch (op) {
1822 case SCSI_PROT_READ_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001823 case SCSI_PROT_READ_STRIP:
1824 case SCSI_PROT_READ_PASS:
1825 /*
1826 * For READ_STRIP and READ_PASS, force the
1827 * error on data being read off the wire. It
1828 * should force an IO error to the driver.
1829 */
James Smartacd68592012-01-18 16:25:09 -05001830 phba->lpfc_injerr_rgrd_cnt--;
James Smart4ac9b222012-03-01 22:38:29 -05001831 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1832 phba->lpfc_injerr_nportid = 0;
1833 phba->lpfc_injerr_lba =
1834 LPFC_INJERR_LBA_OFF;
1835 memset(&phba->lpfc_injerr_wwpn,
1836 0, sizeof(struct lpfc_name));
1837 }
James Smartacd68592012-01-18 16:25:09 -05001838
James Smart9a6b09c2012-03-01 22:37:42 -05001839 rc = BG_ERR_INIT | BG_ERR_SWAP;
James Smartacd68592012-01-18 16:25:09 -05001840 /* Signals the caller to swap CRC->CSUM */
1841
1842 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1843 "0818 BLKGRD: Injecting guard error: "
James Smartf9bb2da2011-10-10 21:34:11 -04001844 "read lba x%lx\n", (unsigned long)lba);
1845 }
1846 }
1847 }
1848
James Smartf9bb2da2011-10-10 21:34:11 -04001849 return rc;
1850}
1851#endif
1852
James Smartacd68592012-01-18 16:25:09 -05001853/**
1854 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1855 * the specified SCSI command.
1856 * @phba: The Hba for which this call is being executed.
James Smart6c8eea52010-04-06 14:49:53 -04001857 * @sc: The SCSI command to examine
1858 * @txopt: (out) BlockGuard operation for transmitted data
1859 * @rxopt: (out) BlockGuard operation for received data
1860 *
1861 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1862 *
James Smartacd68592012-01-18 16:25:09 -05001863 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05001864static int
James Smart6c8eea52010-04-06 14:49:53 -04001865lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1866 uint8_t *txop, uint8_t *rxop)
James Smarte2a0a9d2008-12-04 22:40:02 -05001867{
James Smart6c8eea52010-04-06 14:49:53 -04001868 uint8_t ret = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05001869
James Smart9c6aa9d2013-05-31 17:03:39 -04001870 if (lpfc_cmd_guard_csum(sc)) {
James Smarte2a0a9d2008-12-04 22:40:02 -05001871 switch (scsi_get_prot_op(sc)) {
1872 case SCSI_PROT_READ_INSERT:
1873 case SCSI_PROT_WRITE_STRIP:
James Smart6c8eea52010-04-06 14:49:53 -04001874 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001875 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smarte2a0a9d2008-12-04 22:40:02 -05001876 break;
1877
1878 case SCSI_PROT_READ_STRIP:
1879 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001880 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001881 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001882 break;
1883
Martin K. Petersenc6af4042009-09-18 17:32:59 -04001884 case SCSI_PROT_READ_PASS:
1885 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001886 *rxop = BG_OP_IN_CRC_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001887 *txop = BG_OP_IN_CSUM_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001888 break;
1889
James Smarte2a0a9d2008-12-04 22:40:02 -05001890 case SCSI_PROT_NORMAL:
1891 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001892 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001893 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1894 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001895 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001896 break;
1897
1898 }
James Smart7c56b9f2011-07-22 18:36:25 -04001899 } else {
James Smarte2a0a9d2008-12-04 22:40:02 -05001900 switch (scsi_get_prot_op(sc)) {
1901 case SCSI_PROT_READ_STRIP:
1902 case SCSI_PROT_WRITE_INSERT:
James Smart6c8eea52010-04-06 14:49:53 -04001903 *rxop = BG_OP_IN_CRC_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001904 *txop = BG_OP_IN_NODIF_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001905 break;
1906
1907 case SCSI_PROT_READ_PASS:
1908 case SCSI_PROT_WRITE_PASS:
James Smart6c8eea52010-04-06 14:49:53 -04001909 *rxop = BG_OP_IN_CRC_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001910 *txop = BG_OP_IN_CRC_OUT_CRC;
James Smarte2a0a9d2008-12-04 22:40:02 -05001911 break;
1912
James Smarte2a0a9d2008-12-04 22:40:02 -05001913 case SCSI_PROT_READ_INSERT:
1914 case SCSI_PROT_WRITE_STRIP:
James Smart7c56b9f2011-07-22 18:36:25 -04001915 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001916 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smart7c56b9f2011-07-22 18:36:25 -04001917 break;
1918
James Smarte2a0a9d2008-12-04 22:40:02 -05001919 case SCSI_PROT_NORMAL:
1920 default:
James Smart6a9c52c2009-10-02 15:16:51 -04001921 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
James Smart7c56b9f2011-07-22 18:36:25 -04001922 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1923 scsi_get_prot_op(sc));
James Smart6c8eea52010-04-06 14:49:53 -04001924 ret = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05001925 break;
1926 }
James Smarte2a0a9d2008-12-04 22:40:02 -05001927 }
1928
James Smart6c8eea52010-04-06 14:49:53 -04001929 return ret;
James Smarte2a0a9d2008-12-04 22:40:02 -05001930}
1931
James Smartacd68592012-01-18 16:25:09 -05001932#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1933/**
1934 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1935 * the specified SCSI command in order to force a guard tag error.
1936 * @phba: The Hba for which this call is being executed.
1937 * @sc: The SCSI command to examine
1938 * @txopt: (out) BlockGuard operation for transmitted data
1939 * @rxopt: (out) BlockGuard operation for received data
1940 *
1941 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1942 *
1943 **/
1944static int
1945lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1946 uint8_t *txop, uint8_t *rxop)
1947{
James Smartacd68592012-01-18 16:25:09 -05001948 uint8_t ret = 0;
1949
James Smart9c6aa9d2013-05-31 17:03:39 -04001950 if (lpfc_cmd_guard_csum(sc)) {
James Smartacd68592012-01-18 16:25:09 -05001951 switch (scsi_get_prot_op(sc)) {
1952 case SCSI_PROT_READ_INSERT:
1953 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001954 *rxop = BG_OP_IN_NODIF_OUT_CRC;
James Smart4ac9b222012-03-01 22:38:29 -05001955 *txop = BG_OP_IN_CRC_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001956 break;
1957
1958 case SCSI_PROT_READ_STRIP:
1959 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001960 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001961 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001962 break;
1963
1964 case SCSI_PROT_READ_PASS:
1965 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001966 *rxop = BG_OP_IN_CSUM_OUT_CRC;
James Smart9a6b09c2012-03-01 22:37:42 -05001967 *txop = BG_OP_IN_CRC_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001968 break;
1969
1970 case SCSI_PROT_NORMAL:
1971 default:
1972 break;
1973
1974 }
1975 } else {
1976 switch (scsi_get_prot_op(sc)) {
1977 case SCSI_PROT_READ_STRIP:
1978 case SCSI_PROT_WRITE_INSERT:
James Smartacd68592012-01-18 16:25:09 -05001979 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
James Smart4ac9b222012-03-01 22:38:29 -05001980 *txop = BG_OP_IN_NODIF_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001981 break;
1982
1983 case SCSI_PROT_READ_PASS:
1984 case SCSI_PROT_WRITE_PASS:
James Smart4ac9b222012-03-01 22:38:29 -05001985 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
James Smart9a6b09c2012-03-01 22:37:42 -05001986 *txop = BG_OP_IN_CSUM_OUT_CSUM;
James Smartacd68592012-01-18 16:25:09 -05001987 break;
1988
1989 case SCSI_PROT_READ_INSERT:
1990 case SCSI_PROT_WRITE_STRIP:
James Smartacd68592012-01-18 16:25:09 -05001991 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
James Smart4ac9b222012-03-01 22:38:29 -05001992 *txop = BG_OP_IN_CSUM_OUT_NODIF;
James Smartacd68592012-01-18 16:25:09 -05001993 break;
1994
1995 case SCSI_PROT_NORMAL:
1996 default:
1997 break;
1998 }
1999 }
2000
2001 return ret;
2002}
2003#endif
2004
2005/**
2006 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
2007 * @phba: The Hba for which this call is being executed.
2008 * @sc: pointer to scsi command we're working on
2009 * @bpl: pointer to buffer list for protection groups
2010 * @datacnt: number of segments of data that have been dma mapped
2011 *
2012 * This function sets up BPL buffer list for protection groups of
James Smarte2a0a9d2008-12-04 22:40:02 -05002013 * type LPFC_PG_TYPE_NO_DIF
2014 *
2015 * This is usually used when the HBA is instructed to generate
2016 * DIFs and insert them into data stream (or strip DIF from
2017 * incoming data stream)
2018 *
2019 * The buffer list consists of just one protection group described
2020 * below:
2021 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002022 * start of prot group --> | PDE_5 |
2023 * +-------------------------+
2024 * | PDE_6 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002025 * +-------------------------+
2026 * | Data BDE |
2027 * +-------------------------+
2028 * |more Data BDE's ... (opt)|
2029 * +-------------------------+
2030 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002031 *
2032 * Note: Data s/g buffers have been dma mapped
James Smartacd68592012-01-18 16:25:09 -05002033 *
2034 * Returns the number of BDEs added to the BPL.
2035 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002036static int
2037lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2038 struct ulp_bde64 *bpl, int datasegcnt)
2039{
2040 struct scatterlist *sgde = NULL; /* s/g data entry */
James Smart6c8eea52010-04-06 14:49:53 -04002041 struct lpfc_pde5 *pde5 = NULL;
2042 struct lpfc_pde6 *pde6 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002043 dma_addr_t physaddr;
James Smart6c8eea52010-04-06 14:49:53 -04002044 int i = 0, num_bde = 0, status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002045 int datadir = sc->sc_data_direction;
James Smart0829a192012-05-09 21:18:12 -04002046#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002047 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002048#endif
James Smartacd68592012-01-18 16:25:09 -05002049 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002050 uint32_t reftag;
James Smart7c56b9f2011-07-22 18:36:25 -04002051 unsigned blksize;
James Smart6c8eea52010-04-06 14:49:53 -04002052 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002053
James Smart6c8eea52010-04-06 14:49:53 -04002054 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2055 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002056 goto out;
2057
James Smart6c8eea52010-04-06 14:49:53 -04002058 /* extract some info from the scsi command for pde*/
James Smarte2a0a9d2008-12-04 22:40:02 -05002059 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002060 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002061
James Smartf9bb2da2011-10-10 21:34:11 -04002062#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002063 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002064 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002065 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002066 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002067 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002068 checking = 0;
2069 }
James Smartf9bb2da2011-10-10 21:34:11 -04002070#endif
2071
James Smart6c8eea52010-04-06 14:49:53 -04002072 /* setup PDE5 with what we have */
2073 pde5 = (struct lpfc_pde5 *) bpl;
2074 memset(pde5, 0, sizeof(struct lpfc_pde5));
2075 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002076
James Smartbc739052010-08-04 16:11:18 -04002077 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002078 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002079 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002080
James Smart6c8eea52010-04-06 14:49:53 -04002081 /* advance bpl and increment bde count */
2082 num_bde++;
2083 bpl++;
2084 pde6 = (struct lpfc_pde6 *) bpl;
2085
2086 /* setup PDE6 with the rest of the info */
2087 memset(pde6, 0, sizeof(struct lpfc_pde6));
2088 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2089 bf_set(pde6_optx, pde6, txop);
2090 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002091
2092 /*
2093 * We only need to check the data on READs, for WRITEs
2094 * protection data is automatically generated, not checked.
2095 */
James Smart6c8eea52010-04-06 14:49:53 -04002096 if (datadir == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002097 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002098 bf_set(pde6_ce, pde6, checking);
2099 else
2100 bf_set(pde6_ce, pde6, 0);
2101
James Smart9c6aa9d2013-05-31 17:03:39 -04002102 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002103 bf_set(pde6_re, pde6, checking);
2104 else
2105 bf_set(pde6_re, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002106 }
2107 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002108 bf_set(pde6_ae, pde6, 0);
2109 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002110
James Smartbc739052010-08-04 16:11:18 -04002111 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002112 pde6->word0 = cpu_to_le32(pde6->word0);
2113 pde6->word1 = cpu_to_le32(pde6->word1);
2114 pde6->word2 = cpu_to_le32(pde6->word2);
2115
James Smart6c8eea52010-04-06 14:49:53 -04002116 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002117 num_bde++;
2118 bpl++;
2119
2120 /* assumption: caller has already run dma_map_sg on command data */
2121 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2122 physaddr = sg_dma_address(sgde);
2123 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2124 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2125 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2126 if (datadir == DMA_TO_DEVICE)
2127 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2128 else
2129 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2130 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2131 bpl++;
2132 num_bde++;
2133 }
2134
2135out:
2136 return num_bde;
2137}
2138
James Smartacd68592012-01-18 16:25:09 -05002139/**
2140 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2141 * @phba: The Hba for which this call is being executed.
2142 * @sc: pointer to scsi command we're working on
2143 * @bpl: pointer to buffer list for protection groups
2144 * @datacnt: number of segments of data that have been dma mapped
2145 * @protcnt: number of segment of protection data that have been dma mapped
2146 *
2147 * This function sets up BPL buffer list for protection groups of
2148 * type LPFC_PG_TYPE_DIF
James Smarte2a0a9d2008-12-04 22:40:02 -05002149 *
2150 * This is usually used when DIFs are in their own buffers,
2151 * separate from the data. The HBA can then by instructed
2152 * to place the DIFs in the outgoing stream. For read operations,
2153 * The HBA could extract the DIFs and place it in DIF buffers.
2154 *
2155 * The buffer list for this type consists of one or more of the
2156 * protection groups described below:
2157 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002158 * start of first prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002159 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002160 * | PDE_6 |
2161 * +-------------------------+
2162 * | PDE_7 (Prot BDE) |
James Smarte2a0a9d2008-12-04 22:40:02 -05002163 * +-------------------------+
2164 * | Data BDE |
2165 * +-------------------------+
2166 * |more Data BDE's ... (opt)|
2167 * +-------------------------+
James Smart6c8eea52010-04-06 14:49:53 -04002168 * start of new prot group --> | PDE_5 |
James Smarte2a0a9d2008-12-04 22:40:02 -05002169 * +-------------------------+
2170 * | ... |
2171 * +-------------------------+
2172 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002173 * Note: It is assumed that both data and protection s/g buffers have been
2174 * mapped for DMA
James Smartacd68592012-01-18 16:25:09 -05002175 *
2176 * Returns the number of BDEs added to the BPL.
2177 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002178static int
2179lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2180 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2181{
2182 struct scatterlist *sgde = NULL; /* s/g data entry */
2183 struct scatterlist *sgpe = NULL; /* s/g prot entry */
James Smart6c8eea52010-04-06 14:49:53 -04002184 struct lpfc_pde5 *pde5 = NULL;
2185 struct lpfc_pde6 *pde6 = NULL;
James Smart7f860592011-03-11 16:05:52 -05002186 struct lpfc_pde7 *pde7 = NULL;
James Smarte2a0a9d2008-12-04 22:40:02 -05002187 dma_addr_t dataphysaddr, protphysaddr;
2188 unsigned short curr_data = 0, curr_prot = 0;
James Smart7f860592011-03-11 16:05:52 -05002189 unsigned int split_offset;
2190 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
James Smarte2a0a9d2008-12-04 22:40:02 -05002191 unsigned int protgrp_blks, protgrp_bytes;
2192 unsigned int remainder, subtotal;
James Smart6c8eea52010-04-06 14:49:53 -04002193 int status;
James Smarte2a0a9d2008-12-04 22:40:02 -05002194 int datadir = sc->sc_data_direction;
2195 unsigned char pgdone = 0, alldone = 0;
2196 unsigned blksize;
James Smart0829a192012-05-09 21:18:12 -04002197#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002198 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002199#endif
James Smartacd68592012-01-18 16:25:09 -05002200 uint32_t checking = 1;
James Smarte2a0a9d2008-12-04 22:40:02 -05002201 uint32_t reftag;
James Smart6c8eea52010-04-06 14:49:53 -04002202 uint8_t txop, rxop;
James Smarte2a0a9d2008-12-04 22:40:02 -05002203 int num_bde = 0;
2204
2205 sgpe = scsi_prot_sglist(sc);
2206 sgde = scsi_sglist(sc);
2207
2208 if (!sgpe || !sgde) {
2209 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2210 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2211 sgpe, sgde);
2212 return 0;
2213 }
2214
James Smart6c8eea52010-04-06 14:49:53 -04002215 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2216 if (status)
James Smarte2a0a9d2008-12-04 22:40:02 -05002217 goto out;
2218
James Smart6c8eea52010-04-06 14:49:53 -04002219 /* extract some info from the scsi command */
James Smarte2a0a9d2008-12-04 22:40:02 -05002220 blksize = lpfc_cmd_blksize(sc);
James Smartacd68592012-01-18 16:25:09 -05002221 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
James Smarte2a0a9d2008-12-04 22:40:02 -05002222
James Smartf9bb2da2011-10-10 21:34:11 -04002223#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002224 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002225 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002226 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002227 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002228 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002229 checking = 0;
2230 }
James Smartf9bb2da2011-10-10 21:34:11 -04002231#endif
2232
James Smarte2a0a9d2008-12-04 22:40:02 -05002233 split_offset = 0;
2234 do {
James Smart96f70772013-04-17 20:16:15 -04002235 /* Check to see if we ran out of space */
2236 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2237 return num_bde + 3;
2238
James Smart6c8eea52010-04-06 14:49:53 -04002239 /* setup PDE5 with what we have */
2240 pde5 = (struct lpfc_pde5 *) bpl;
2241 memset(pde5, 0, sizeof(struct lpfc_pde5));
2242 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
James Smarte2a0a9d2008-12-04 22:40:02 -05002243
James Smartbc739052010-08-04 16:11:18 -04002244 /* Endianness conversion if necessary for PDE5 */
James Smart589a52d2010-07-14 15:30:54 -04002245 pde5->word0 = cpu_to_le32(pde5->word0);
James Smart7c56b9f2011-07-22 18:36:25 -04002246 pde5->reftag = cpu_to_le32(reftag);
James Smart589a52d2010-07-14 15:30:54 -04002247
James Smart6c8eea52010-04-06 14:49:53 -04002248 /* advance bpl and increment bde count */
2249 num_bde++;
2250 bpl++;
2251 pde6 = (struct lpfc_pde6 *) bpl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002252
James Smart6c8eea52010-04-06 14:49:53 -04002253 /* setup PDE6 with the rest of the info */
2254 memset(pde6, 0, sizeof(struct lpfc_pde6));
2255 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2256 bf_set(pde6_optx, pde6, txop);
2257 bf_set(pde6_oprx, pde6, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002258
James Smart9c6aa9d2013-05-31 17:03:39 -04002259 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002260 bf_set(pde6_ce, pde6, checking);
2261 else
2262 bf_set(pde6_ce, pde6, 0);
2263
James Smart9c6aa9d2013-05-31 17:03:39 -04002264 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002265 bf_set(pde6_re, pde6, checking);
2266 else
2267 bf_set(pde6_re, pde6, 0);
2268
James Smart6c8eea52010-04-06 14:49:53 -04002269 bf_set(pde6_ai, pde6, 1);
James Smart7c56b9f2011-07-22 18:36:25 -04002270 bf_set(pde6_ae, pde6, 0);
2271 bf_set(pde6_apptagval, pde6, 0);
James Smart6c8eea52010-04-06 14:49:53 -04002272
James Smartbc739052010-08-04 16:11:18 -04002273 /* Endianness conversion if necessary for PDE6 */
James Smart589a52d2010-07-14 15:30:54 -04002274 pde6->word0 = cpu_to_le32(pde6->word0);
2275 pde6->word1 = cpu_to_le32(pde6->word1);
2276 pde6->word2 = cpu_to_le32(pde6->word2);
2277
James Smart6c8eea52010-04-06 14:49:53 -04002278 /* advance bpl and increment bde count */
James Smarte2a0a9d2008-12-04 22:40:02 -05002279 num_bde++;
2280 bpl++;
2281
2282 /* setup the first BDE that points to protection buffer */
James Smart7f860592011-03-11 16:05:52 -05002283 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2284 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
James Smarte2a0a9d2008-12-04 22:40:02 -05002285
James Smarte2a0a9d2008-12-04 22:40:02 -05002286 /* must be integer multiple of the DIF block length */
2287 BUG_ON(protgroup_len % 8);
2288
James Smart7f860592011-03-11 16:05:52 -05002289 pde7 = (struct lpfc_pde7 *) bpl;
2290 memset(pde7, 0, sizeof(struct lpfc_pde7));
2291 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2292
James Smart7c56b9f2011-07-22 18:36:25 -04002293 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2294 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
James Smart7f860592011-03-11 16:05:52 -05002295
James Smarte2a0a9d2008-12-04 22:40:02 -05002296 protgrp_blks = protgroup_len / 8;
2297 protgrp_bytes = protgrp_blks * blksize;
2298
James Smart7f860592011-03-11 16:05:52 -05002299 /* check if this pde is crossing the 4K boundary; if so split */
2300 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2301 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2302 protgroup_offset += protgroup_remainder;
2303 protgrp_blks = protgroup_remainder / 8;
James Smart7c56b9f2011-07-22 18:36:25 -04002304 protgrp_bytes = protgrp_blks * blksize;
James Smart7f860592011-03-11 16:05:52 -05002305 } else {
2306 protgroup_offset = 0;
2307 curr_prot++;
2308 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002309
James Smarte2a0a9d2008-12-04 22:40:02 -05002310 num_bde++;
2311
2312 /* setup BDE's for data blocks associated with DIF data */
2313 pgdone = 0;
2314 subtotal = 0; /* total bytes processed for current prot grp */
2315 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002316 /* Check to see if we ran out of space */
2317 if (num_bde >= phba->cfg_total_seg_cnt)
2318 return num_bde + 1;
2319
James Smarte2a0a9d2008-12-04 22:40:02 -05002320 if (!sgde) {
James Smart6a9c52c2009-10-02 15:16:51 -04002321 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2322 "9065 BLKGRD:%s Invalid data segment\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05002323 __func__);
2324 return 0;
2325 }
2326 bpl++;
2327 dataphysaddr = sg_dma_address(sgde) + split_offset;
2328 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2329 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2330
2331 remainder = sg_dma_len(sgde) - split_offset;
2332
2333 if ((subtotal + remainder) <= protgrp_bytes) {
2334 /* we can use this whole buffer */
2335 bpl->tus.f.bdeSize = remainder;
2336 split_offset = 0;
2337
2338 if ((subtotal + remainder) == protgrp_bytes)
2339 pgdone = 1;
2340 } else {
2341 /* must split this buffer with next prot grp */
2342 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2343 split_offset += bpl->tus.f.bdeSize;
2344 }
2345
2346 subtotal += bpl->tus.f.bdeSize;
2347
2348 if (datadir == DMA_TO_DEVICE)
2349 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2350 else
2351 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2352 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2353
2354 num_bde++;
2355 curr_data++;
2356
2357 if (split_offset)
2358 break;
2359
2360 /* Move to the next s/g segment if possible */
2361 sgde = sg_next(sgde);
James Smart6c8eea52010-04-06 14:49:53 -04002362
James Smarte2a0a9d2008-12-04 22:40:02 -05002363 }
2364
James Smart7f860592011-03-11 16:05:52 -05002365 if (protgroup_offset) {
2366 /* update the reference tag */
2367 reftag += protgrp_blks;
2368 bpl++;
2369 continue;
2370 }
2371
James Smarte2a0a9d2008-12-04 22:40:02 -05002372 /* are we done ? */
2373 if (curr_prot == protcnt) {
2374 alldone = 1;
2375 } else if (curr_prot < protcnt) {
2376 /* advance to next prot buffer */
2377 sgpe = sg_next(sgpe);
2378 bpl++;
2379
2380 /* update the reference tag */
2381 reftag += protgrp_blks;
2382 } else {
2383 /* if we're here, we have a bug */
James Smart6a9c52c2009-10-02 15:16:51 -04002384 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2385 "9054 BLKGRD: bug in %s\n", __func__);
James Smarte2a0a9d2008-12-04 22:40:02 -05002386 }
2387
2388 } while (!alldone);
James Smarte2a0a9d2008-12-04 22:40:02 -05002389out:
2390
James Smarte2a0a9d2008-12-04 22:40:02 -05002391 return num_bde;
2392}
James Smart7f860592011-03-11 16:05:52 -05002393
James Smartacd68592012-01-18 16:25:09 -05002394/**
2395 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2396 * @phba: The Hba for which this call is being executed.
2397 * @sc: pointer to scsi command we're working on
2398 * @sgl: pointer to buffer list for protection groups
2399 * @datacnt: number of segments of data that have been dma mapped
2400 *
2401 * This function sets up SGL buffer list for protection groups of
2402 * type LPFC_PG_TYPE_NO_DIF
2403 *
2404 * This is usually used when the HBA is instructed to generate
2405 * DIFs and insert them into data stream (or strip DIF from
2406 * incoming data stream)
2407 *
2408 * The buffer list consists of just one protection group described
2409 * below:
2410 * +-------------------------+
2411 * start of prot group --> | DI_SEED |
2412 * +-------------------------+
2413 * | Data SGE |
2414 * +-------------------------+
2415 * |more Data SGE's ... (opt)|
2416 * +-------------------------+
2417 *
2418 *
2419 * Note: Data s/g buffers have been dma mapped
2420 *
2421 * Returns the number of SGEs added to the SGL.
2422 **/
2423static int
2424lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2425 struct sli4_sge *sgl, int datasegcnt)
2426{
2427 struct scatterlist *sgde = NULL; /* s/g data entry */
2428 struct sli4_sge_diseed *diseed = NULL;
2429 dma_addr_t physaddr;
2430 int i = 0, num_sge = 0, status;
James Smartacd68592012-01-18 16:25:09 -05002431 uint32_t reftag;
2432 unsigned blksize;
2433 uint8_t txop, rxop;
James Smart0829a192012-05-09 21:18:12 -04002434#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002435 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002436#endif
James Smartacd68592012-01-18 16:25:09 -05002437 uint32_t checking = 1;
2438 uint32_t dma_len;
2439 uint32_t dma_offset = 0;
2440
2441 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2442 if (status)
2443 goto out;
2444
2445 /* extract some info from the scsi command for pde*/
2446 blksize = lpfc_cmd_blksize(sc);
2447 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2448
2449#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002450 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002451 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002452 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002453 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002454 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002455 checking = 0;
2456 }
2457#endif
2458
2459 /* setup DISEED with what we have */
2460 diseed = (struct sli4_sge_diseed *) sgl;
2461 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2462 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2463
2464 /* Endianness conversion if necessary */
2465 diseed->ref_tag = cpu_to_le32(reftag);
2466 diseed->ref_tag_tran = diseed->ref_tag;
2467
James Smarta6887e22013-04-17 20:18:07 -04002468 /*
2469 * We only need to check the data on READs, for WRITEs
2470 * protection data is automatically generated, not checked.
2471 */
2472 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002473 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
James Smarta6887e22013-04-17 20:18:07 -04002474 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2475 else
2476 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2477
James Smart9c6aa9d2013-05-31 17:03:39 -04002478 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002479 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2480 else
2481 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2482 }
2483
James Smartacd68592012-01-18 16:25:09 -05002484 /* setup DISEED with the rest of the info */
2485 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2486 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002487
James Smartacd68592012-01-18 16:25:09 -05002488 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2489 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2490
2491 /* Endianness conversion if necessary for DISEED */
2492 diseed->word2 = cpu_to_le32(diseed->word2);
2493 diseed->word3 = cpu_to_le32(diseed->word3);
2494
2495 /* advance bpl and increment sge count */
2496 num_sge++;
2497 sgl++;
2498
2499 /* assumption: caller has already run dma_map_sg on command data */
2500 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2501 physaddr = sg_dma_address(sgde);
2502 dma_len = sg_dma_len(sgde);
2503 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2504 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2505 if ((i + 1) == datasegcnt)
2506 bf_set(lpfc_sli4_sge_last, sgl, 1);
2507 else
2508 bf_set(lpfc_sli4_sge_last, sgl, 0);
2509 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2510 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2511
2512 sgl->sge_len = cpu_to_le32(dma_len);
2513 dma_offset += dma_len;
2514
2515 sgl++;
2516 num_sge++;
2517 }
2518
2519out:
2520 return num_sge;
2521}
2522
2523/**
2524 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2525 * @phba: The Hba for which this call is being executed.
2526 * @sc: pointer to scsi command we're working on
2527 * @sgl: pointer to buffer list for protection groups
2528 * @datacnt: number of segments of data that have been dma mapped
2529 * @protcnt: number of segment of protection data that have been dma mapped
2530 *
2531 * This function sets up SGL buffer list for protection groups of
2532 * type LPFC_PG_TYPE_DIF
2533 *
2534 * This is usually used when DIFs are in their own buffers,
2535 * separate from the data. The HBA can then by instructed
2536 * to place the DIFs in the outgoing stream. For read operations,
2537 * The HBA could extract the DIFs and place it in DIF buffers.
2538 *
2539 * The buffer list for this type consists of one or more of the
2540 * protection groups described below:
2541 * +-------------------------+
2542 * start of first prot group --> | DISEED |
2543 * +-------------------------+
2544 * | DIF (Prot SGE) |
2545 * +-------------------------+
2546 * | Data SGE |
2547 * +-------------------------+
2548 * |more Data SGE's ... (opt)|
2549 * +-------------------------+
2550 * start of new prot group --> | DISEED |
2551 * +-------------------------+
2552 * | ... |
2553 * +-------------------------+
2554 *
2555 * Note: It is assumed that both data and protection s/g buffers have been
2556 * mapped for DMA
2557 *
2558 * Returns the number of SGEs added to the SGL.
2559 **/
2560static int
2561lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2562 struct sli4_sge *sgl, int datacnt, int protcnt)
2563{
2564 struct scatterlist *sgde = NULL; /* s/g data entry */
2565 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2566 struct sli4_sge_diseed *diseed = NULL;
2567 dma_addr_t dataphysaddr, protphysaddr;
2568 unsigned short curr_data = 0, curr_prot = 0;
2569 unsigned int split_offset;
2570 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2571 unsigned int protgrp_blks, protgrp_bytes;
2572 unsigned int remainder, subtotal;
2573 int status;
2574 unsigned char pgdone = 0, alldone = 0;
2575 unsigned blksize;
2576 uint32_t reftag;
2577 uint8_t txop, rxop;
2578 uint32_t dma_len;
James Smart0829a192012-05-09 21:18:12 -04002579#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smartacd68592012-01-18 16:25:09 -05002580 uint32_t rc;
James Smart0829a192012-05-09 21:18:12 -04002581#endif
James Smartacd68592012-01-18 16:25:09 -05002582 uint32_t checking = 1;
2583 uint32_t dma_offset = 0;
2584 int num_sge = 0;
2585
2586 sgpe = scsi_prot_sglist(sc);
2587 sgde = scsi_sglist(sc);
2588
2589 if (!sgpe || !sgde) {
2590 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2591 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2592 sgpe, sgde);
2593 return 0;
2594 }
2595
2596 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2597 if (status)
2598 goto out;
2599
2600 /* extract some info from the scsi command */
2601 blksize = lpfc_cmd_blksize(sc);
2602 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2603
2604#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
James Smart4ac9b222012-03-01 22:38:29 -05002605 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
James Smartacd68592012-01-18 16:25:09 -05002606 if (rc) {
James Smart9a6b09c2012-03-01 22:37:42 -05002607 if (rc & BG_ERR_SWAP)
James Smartacd68592012-01-18 16:25:09 -05002608 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
James Smart9a6b09c2012-03-01 22:37:42 -05002609 if (rc & BG_ERR_CHECK)
James Smartacd68592012-01-18 16:25:09 -05002610 checking = 0;
2611 }
2612#endif
2613
2614 split_offset = 0;
2615 do {
James Smart96f70772013-04-17 20:16:15 -04002616 /* Check to see if we ran out of space */
2617 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2618 return num_sge + 3;
2619
James Smartacd68592012-01-18 16:25:09 -05002620 /* setup DISEED with what we have */
2621 diseed = (struct sli4_sge_diseed *) sgl;
2622 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2623 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2624
2625 /* Endianness conversion if necessary */
2626 diseed->ref_tag = cpu_to_le32(reftag);
2627 diseed->ref_tag_tran = diseed->ref_tag;
2628
James Smart9c6aa9d2013-05-31 17:03:39 -04002629 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
James Smarta6887e22013-04-17 20:18:07 -04002630 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2631
2632 } else {
2633 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2634 /*
2635 * When in this mode, the hardware will replace
2636 * the guard tag from the host with a
2637 * newly generated good CRC for the wire.
2638 * Switch to raw mode here to avoid this
2639 * behavior. What the host sends gets put on the wire.
2640 */
2641 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2642 txop = BG_OP_RAW_MODE;
2643 rxop = BG_OP_RAW_MODE;
2644 }
2645 }
2646
2647
James Smart9c6aa9d2013-05-31 17:03:39 -04002648 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
James Smarta6887e22013-04-17 20:18:07 -04002649 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2650 else
2651 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2652
James Smartacd68592012-01-18 16:25:09 -05002653 /* setup DISEED with the rest of the info */
2654 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2655 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
James Smarta6887e22013-04-17 20:18:07 -04002656
James Smartacd68592012-01-18 16:25:09 -05002657 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2658 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2659
2660 /* Endianness conversion if necessary for DISEED */
2661 diseed->word2 = cpu_to_le32(diseed->word2);
2662 diseed->word3 = cpu_to_le32(diseed->word3);
2663
2664 /* advance sgl and increment bde count */
2665 num_sge++;
2666 sgl++;
2667
2668 /* setup the first BDE that points to protection buffer */
2669 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2670 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2671
2672 /* must be integer multiple of the DIF block length */
2673 BUG_ON(protgroup_len % 8);
2674
2675 /* Now setup DIF SGE */
2676 sgl->word2 = 0;
2677 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2678 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2679 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2680 sgl->word2 = cpu_to_le32(sgl->word2);
2681
2682 protgrp_blks = protgroup_len / 8;
2683 protgrp_bytes = protgrp_blks * blksize;
2684
2685 /* check if DIF SGE is crossing the 4K boundary; if so split */
2686 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2687 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2688 protgroup_offset += protgroup_remainder;
2689 protgrp_blks = protgroup_remainder / 8;
2690 protgrp_bytes = protgrp_blks * blksize;
2691 } else {
2692 protgroup_offset = 0;
2693 curr_prot++;
2694 }
2695
2696 num_sge++;
2697
2698 /* setup SGE's for data blocks associated with DIF data */
2699 pgdone = 0;
2700 subtotal = 0; /* total bytes processed for current prot grp */
2701 while (!pgdone) {
James Smart96f70772013-04-17 20:16:15 -04002702 /* Check to see if we ran out of space */
2703 if (num_sge >= phba->cfg_total_seg_cnt)
2704 return num_sge + 1;
2705
James Smartacd68592012-01-18 16:25:09 -05002706 if (!sgde) {
2707 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2708 "9086 BLKGRD:%s Invalid data segment\n",
2709 __func__);
2710 return 0;
2711 }
2712 sgl++;
2713 dataphysaddr = sg_dma_address(sgde) + split_offset;
2714
2715 remainder = sg_dma_len(sgde) - split_offset;
2716
2717 if ((subtotal + remainder) <= protgrp_bytes) {
2718 /* we can use this whole buffer */
2719 dma_len = remainder;
2720 split_offset = 0;
2721
2722 if ((subtotal + remainder) == protgrp_bytes)
2723 pgdone = 1;
2724 } else {
2725 /* must split this buffer with next prot grp */
2726 dma_len = protgrp_bytes - subtotal;
2727 split_offset += dma_len;
2728 }
2729
2730 subtotal += dma_len;
2731
2732 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2733 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2734 bf_set(lpfc_sli4_sge_last, sgl, 0);
2735 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2736 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2737
2738 sgl->sge_len = cpu_to_le32(dma_len);
2739 dma_offset += dma_len;
2740
2741 num_sge++;
2742 curr_data++;
2743
2744 if (split_offset)
2745 break;
2746
2747 /* Move to the next s/g segment if possible */
2748 sgde = sg_next(sgde);
2749 }
2750
2751 if (protgroup_offset) {
2752 /* update the reference tag */
2753 reftag += protgrp_blks;
2754 sgl++;
2755 continue;
2756 }
2757
2758 /* are we done ? */
2759 if (curr_prot == protcnt) {
2760 bf_set(lpfc_sli4_sge_last, sgl, 1);
2761 alldone = 1;
2762 } else if (curr_prot < protcnt) {
2763 /* advance to next prot buffer */
2764 sgpe = sg_next(sgpe);
2765 sgl++;
2766
2767 /* update the reference tag */
2768 reftag += protgrp_blks;
2769 } else {
2770 /* if we're here, we have a bug */
2771 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2772 "9085 BLKGRD: bug in %s\n", __func__);
2773 }
2774
2775 } while (!alldone);
2776
2777out:
2778
2779 return num_sge;
2780}
2781
2782/**
2783 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2784 * @phba: The Hba for which this call is being executed.
2785 * @sc: pointer to scsi command we're working on
2786 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002787 * Given a SCSI command that supports DIF, determine composition of protection
2788 * groups involved in setting up buffer lists
2789 *
James Smartacd68592012-01-18 16:25:09 -05002790 * Returns: Protection group type (with or without DIF)
2791 *
2792 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002793static int
2794lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2795{
2796 int ret = LPFC_PG_TYPE_INVALID;
2797 unsigned char op = scsi_get_prot_op(sc);
2798
2799 switch (op) {
2800 case SCSI_PROT_READ_STRIP:
2801 case SCSI_PROT_WRITE_INSERT:
2802 ret = LPFC_PG_TYPE_NO_DIF;
2803 break;
2804 case SCSI_PROT_READ_INSERT:
2805 case SCSI_PROT_WRITE_STRIP:
2806 case SCSI_PROT_READ_PASS:
2807 case SCSI_PROT_WRITE_PASS:
James Smarte2a0a9d2008-12-04 22:40:02 -05002808 ret = LPFC_PG_TYPE_DIF_BUF;
2809 break;
2810 default:
James Smart9c6aa9d2013-05-31 17:03:39 -04002811 if (phba)
2812 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2813 "9021 Unsupported protection op:%d\n",
2814 op);
James Smarte2a0a9d2008-12-04 22:40:02 -05002815 break;
2816 }
James Smarte2a0a9d2008-12-04 22:40:02 -05002817 return ret;
2818}
2819
James Smartacd68592012-01-18 16:25:09 -05002820/**
James Smarta6887e22013-04-17 20:18:07 -04002821 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2822 * @phba: The Hba for which this call is being executed.
2823 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2824 *
2825 * Adjust the data length to account for how much data
2826 * is actually on the wire.
2827 *
2828 * returns the adjusted data length
2829 **/
2830static int
2831lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2832 struct lpfc_scsi_buf *lpfc_cmd)
2833{
2834 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2835 int fcpdl;
2836
2837 fcpdl = scsi_bufflen(sc);
2838
2839 /* Check if there is protection data on the wire */
2840 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
James Smart9c6aa9d2013-05-31 17:03:39 -04002841 /* Read check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002842 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2843 return fcpdl;
2844
2845 } else {
James Smart9c6aa9d2013-05-31 17:03:39 -04002846 /* Write check for protection data */
James Smarta6887e22013-04-17 20:18:07 -04002847 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2848 return fcpdl;
2849 }
2850
2851 /*
2852 * If we are in DIF Type 1 mode every data block has a 8 byte
James Smart9c6aa9d2013-05-31 17:03:39 -04002853 * DIF (trailer) attached to it. Must ajust FCP data length
2854 * to account for the protection data.
James Smarta6887e22013-04-17 20:18:07 -04002855 */
James Smart9c6aa9d2013-05-31 17:03:39 -04002856 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
James Smarta6887e22013-04-17 20:18:07 -04002857
2858 return fcpdl;
2859}
2860
2861/**
James Smartacd68592012-01-18 16:25:09 -05002862 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2863 * @phba: The Hba for which this call is being executed.
2864 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2865 *
James Smarte2a0a9d2008-12-04 22:40:02 -05002866 * This is the protection/DIF aware version of
2867 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2868 * two functions eventually, but for now, it's here
James Smartacd68592012-01-18 16:25:09 -05002869 **/
James Smarte2a0a9d2008-12-04 22:40:02 -05002870static int
James Smartacd68592012-01-18 16:25:09 -05002871lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
James Smarte2a0a9d2008-12-04 22:40:02 -05002872 struct lpfc_scsi_buf *lpfc_cmd)
2873{
2874 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2875 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2876 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2877 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2878 uint32_t num_bde = 0;
2879 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2880 int prot_group_type = 0;
James Smarta6887e22013-04-17 20:18:07 -04002881 int fcpdl;
James Smarte2a0a9d2008-12-04 22:40:02 -05002882
2883 /*
2884 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2885 * fcp_rsp regions to the first data bde entry
2886 */
2887 bpl += 2;
2888 if (scsi_sg_count(scsi_cmnd)) {
2889 /*
2890 * The driver stores the segment count returned from pci_map_sg
2891 * because this a count of dma-mappings used to map the use_sg
2892 * pages. They are not guaranteed to be the same for those
2893 * architectures that implement an IOMMU.
2894 */
2895 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2896 scsi_sglist(scsi_cmnd),
2897 scsi_sg_count(scsi_cmnd), datadir);
2898 if (unlikely(!datasegcnt))
2899 return 1;
2900
2901 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04002902
2903 /* First check if data segment count from SCSI Layer is good */
2904 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2905 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002906
2907 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2908
2909 switch (prot_group_type) {
2910 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04002911
2912 /* Here we need to add a PDE5 and PDE6 to the count */
2913 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2914 goto err;
2915
James Smarte2a0a9d2008-12-04 22:40:02 -05002916 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2917 datasegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002918 /* we should have 2 or more entries in buffer list */
James Smarte2a0a9d2008-12-04 22:40:02 -05002919 if (num_bde < 2)
2920 goto err;
2921 break;
James Smart96f70772013-04-17 20:16:15 -04002922
2923 case LPFC_PG_TYPE_DIF_BUF:
James Smarte2a0a9d2008-12-04 22:40:02 -05002924 /*
2925 * This type indicates that protection buffers are
2926 * passed to the driver, so that needs to be prepared
2927 * for DMA
2928 */
2929 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2930 scsi_prot_sglist(scsi_cmnd),
2931 scsi_prot_sg_count(scsi_cmnd), datadir);
2932 if (unlikely(!protsegcnt)) {
2933 scsi_dma_unmap(scsi_cmnd);
2934 return 1;
2935 }
2936
2937 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04002938
2939 /*
2940 * There is a minimun of 4 BPLs used for every
2941 * protection data segment.
2942 */
2943 if ((lpfc_cmd->prot_seg_cnt * 4) >
2944 (phba->cfg_total_seg_cnt - 2))
2945 goto err;
James Smarte2a0a9d2008-12-04 22:40:02 -05002946
2947 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2948 datasegcnt, protsegcnt);
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002949 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04002950 if ((num_bde < 3) ||
2951 (num_bde > phba->cfg_total_seg_cnt))
James Smarte2a0a9d2008-12-04 22:40:02 -05002952 goto err;
2953 break;
James Smart96f70772013-04-17 20:16:15 -04002954
James Smarte2a0a9d2008-12-04 22:40:02 -05002955 case LPFC_PG_TYPE_INVALID:
2956 default:
James Smart96f70772013-04-17 20:16:15 -04002957 scsi_dma_unmap(scsi_cmnd);
2958 lpfc_cmd->seg_cnt = 0;
2959
James Smarte2a0a9d2008-12-04 22:40:02 -05002960 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2961 "9022 Unexpected protection group %i\n",
2962 prot_group_type);
2963 return 1;
2964 }
2965 }
2966
2967 /*
2968 * Finish initializing those IOCB fields that are dependent on the
2969 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2970 * reinitialized since all iocb memory resources are used many times
2971 * for transmit, receive, and continuation bpl's.
2972 */
2973 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2974 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2975 iocb_cmd->ulpBdeCount = 1;
2976 iocb_cmd->ulpLe = 1;
2977
James Smarta6887e22013-04-17 20:18:07 -04002978 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05002979 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2980
2981 /*
2982 * Due to difference in data length between DIF/non-DIF paths,
2983 * we need to set word 4 of IOCB here
2984 */
2985 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2986
2987 return 0;
2988err:
James Smart96f70772013-04-17 20:16:15 -04002989 if (lpfc_cmd->seg_cnt)
2990 scsi_dma_unmap(scsi_cmnd);
2991 if (lpfc_cmd->prot_seg_cnt)
2992 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2993 scsi_prot_sg_count(scsi_cmnd),
2994 scsi_cmnd->sc_data_direction);
2995
James Smarte2a0a9d2008-12-04 22:40:02 -05002996 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04002997 "9023 Cannot setup S/G List for HBA"
2998 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2999 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3000 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
James Smarte2a0a9d2008-12-04 22:40:02 -05003001 prot_group_type, num_bde);
James Smart96f70772013-04-17 20:16:15 -04003002
3003 lpfc_cmd->seg_cnt = 0;
3004 lpfc_cmd->prot_seg_cnt = 0;
James Smarte2a0a9d2008-12-04 22:40:02 -05003005 return 1;
3006}
3007
3008/*
James Smart737d4242013-04-17 20:14:49 -04003009 * This function calcuates the T10 DIF guard tag
3010 * on the specified data using a CRC algorithmn
3011 * using crc_t10dif.
3012 */
3013uint16_t
3014lpfc_bg_crc(uint8_t *data, int count)
3015{
3016 uint16_t crc = 0;
3017 uint16_t x;
3018
3019 crc = crc_t10dif(data, count);
3020 x = cpu_to_be16(crc);
3021 return x;
3022}
3023
3024/*
3025 * This function calcuates the T10 DIF guard tag
3026 * on the specified data using a CSUM algorithmn
3027 * using ip_compute_csum.
3028 */
3029uint16_t
3030lpfc_bg_csum(uint8_t *data, int count)
3031{
3032 uint16_t ret;
3033
3034 ret = ip_compute_csum(data, count);
3035 return ret;
3036}
3037
3038/*
3039 * This function examines the protection data to try to determine
3040 * what type of T10-DIF error occurred.
3041 */
3042void
3043lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3044{
3045 struct scatterlist *sgpe; /* s/g prot entry */
3046 struct scatterlist *sgde; /* s/g data entry */
3047 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3048 struct scsi_dif_tuple *src = NULL;
3049 uint8_t *data_src = NULL;
3050 uint16_t guard_tag, guard_type;
3051 uint16_t start_app_tag, app_tag;
3052 uint32_t start_ref_tag, ref_tag;
3053 int prot, protsegcnt;
3054 int err_type, len, data_len;
3055 int chk_ref, chk_app, chk_guard;
3056 uint16_t sum;
3057 unsigned blksize;
3058
3059 err_type = BGS_GUARD_ERR_MASK;
3060 sum = 0;
3061 guard_tag = 0;
3062
3063 /* First check to see if there is protection data to examine */
3064 prot = scsi_get_prot_op(cmd);
3065 if ((prot == SCSI_PROT_READ_STRIP) ||
3066 (prot == SCSI_PROT_WRITE_INSERT) ||
3067 (prot == SCSI_PROT_NORMAL))
3068 goto out;
3069
3070 /* Currently the driver just supports ref_tag and guard_tag checking */
3071 chk_ref = 1;
3072 chk_app = 0;
3073 chk_guard = 0;
3074
3075 /* Setup a ptr to the protection data provided by the SCSI host */
3076 sgpe = scsi_prot_sglist(cmd);
3077 protsegcnt = lpfc_cmd->prot_seg_cnt;
3078
3079 if (sgpe && protsegcnt) {
3080
3081 /*
3082 * We will only try to verify guard tag if the segment
3083 * data length is a multiple of the blksize.
3084 */
3085 sgde = scsi_sglist(cmd);
3086 blksize = lpfc_cmd_blksize(cmd);
3087 data_src = (uint8_t *)sg_virt(sgde);
3088 data_len = sgde->length;
3089 if ((data_len & (blksize - 1)) == 0)
3090 chk_guard = 1;
3091 guard_type = scsi_host_get_guard(cmd->device->host);
3092
James Smarte85d8f92013-05-31 17:04:10 -04003093 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
James Smarta6887e22013-04-17 20:18:07 -04003094 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
James Smart737d4242013-04-17 20:14:49 -04003095 start_app_tag = src->app_tag;
James Smart737d4242013-04-17 20:14:49 -04003096 len = sgpe->length;
3097 while (src && protsegcnt) {
3098 while (len) {
3099
3100 /*
3101 * First check to see if a protection data
3102 * check is valid
3103 */
3104 if ((src->ref_tag == 0xffffffff) ||
3105 (src->app_tag == 0xffff)) {
3106 start_ref_tag++;
3107 goto skipit;
3108 }
3109
James Smart9c6aa9d2013-05-31 17:03:39 -04003110 /* First Guard Tag checking */
3111 if (chk_guard) {
3112 guard_tag = src->guard_tag;
3113 if (lpfc_cmd_guard_csum(cmd))
3114 sum = lpfc_bg_csum(data_src,
3115 blksize);
3116 else
3117 sum = lpfc_bg_crc(data_src,
3118 blksize);
3119 if ((guard_tag != sum)) {
3120 err_type = BGS_GUARD_ERR_MASK;
3121 goto out;
3122 }
James Smart737d4242013-04-17 20:14:49 -04003123 }
3124
3125 /* Reference Tag checking */
3126 ref_tag = be32_to_cpu(src->ref_tag);
3127 if (chk_ref && (ref_tag != start_ref_tag)) {
3128 err_type = BGS_REFTAG_ERR_MASK;
3129 goto out;
3130 }
3131 start_ref_tag++;
3132
James Smart9c6aa9d2013-05-31 17:03:39 -04003133 /* App Tag checking */
3134 app_tag = src->app_tag;
3135 if (chk_app && (app_tag != start_app_tag)) {
3136 err_type = BGS_APPTAG_ERR_MASK;
3137 goto out;
James Smart737d4242013-04-17 20:14:49 -04003138 }
3139skipit:
3140 len -= sizeof(struct scsi_dif_tuple);
3141 if (len < 0)
3142 len = 0;
3143 src++;
3144
3145 data_src += blksize;
3146 data_len -= blksize;
3147
3148 /*
3149 * Are we at the end of the Data segment?
3150 * The data segment is only used for Guard
3151 * tag checking.
3152 */
3153 if (chk_guard && (data_len == 0)) {
3154 chk_guard = 0;
3155 sgde = sg_next(sgde);
3156 if (!sgde)
3157 goto out;
3158
3159 data_src = (uint8_t *)sg_virt(sgde);
3160 data_len = sgde->length;
3161 if ((data_len & (blksize - 1)) == 0)
3162 chk_guard = 1;
3163 }
3164 }
3165
3166 /* Goto the next Protection data segment */
3167 sgpe = sg_next(sgpe);
3168 if (sgpe) {
3169 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3170 len = sgpe->length;
3171 } else {
3172 src = NULL;
3173 }
3174 protsegcnt--;
3175 }
3176 }
3177out:
3178 if (err_type == BGS_GUARD_ERR_MASK) {
3179 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3180 0x10, 0x1);
3181 cmd->result = DRIVER_SENSE << 24
3182 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3183 phba->bg_guard_err_cnt++;
3184 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3185 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3186 (unsigned long)scsi_get_lba(cmd),
3187 sum, guard_tag);
3188
3189 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3190 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3191 0x10, 0x3);
3192 cmd->result = DRIVER_SENSE << 24
3193 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3194
3195 phba->bg_reftag_err_cnt++;
3196 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3197 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3198 (unsigned long)scsi_get_lba(cmd),
3199 ref_tag, start_ref_tag);
3200
3201 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3202 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3203 0x10, 0x2);
3204 cmd->result = DRIVER_SENSE << 24
3205 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3206
3207 phba->bg_apptag_err_cnt++;
3208 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3209 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3210 (unsigned long)scsi_get_lba(cmd),
3211 app_tag, start_app_tag);
3212 }
3213}
3214
3215
3216/*
James Smarte2a0a9d2008-12-04 22:40:02 -05003217 * This function checks for BlockGuard errors detected by
3218 * the HBA. In case of errors, the ASC/ASCQ fields in the
3219 * sense buffer will be set accordingly, paired with
3220 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3221 * detected corruption.
3222 *
3223 * Returns:
3224 * 0 - No error found
3225 * 1 - BlockGuard error found
3226 * -1 - Internal error (bad profile, ...etc)
3227 */
3228static int
3229lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3230 struct lpfc_iocbq *pIocbOut)
3231{
3232 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3233 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3234 int ret = 0;
3235 uint32_t bghm = bgf->bghm;
3236 uint32_t bgstat = bgf->bgstat;
3237 uint64_t failing_sector = 0;
3238
James Smarte2a0a9d2008-12-04 22:40:02 -05003239 spin_lock(&_dump_buf_lock);
3240 if (!_dump_buf_done) {
James Smart6a9c52c2009-10-02 15:16:51 -04003241 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3242 " Data for %u blocks to debugfs\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05003243 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
James Smart6a9c52c2009-10-02 15:16:51 -04003244 lpfc_debug_save_data(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003245
3246 /* If we have a prot sgl, save the DIF buffer */
3247 if (lpfc_prot_group_type(phba, cmd) ==
3248 LPFC_PG_TYPE_DIF_BUF) {
James Smart6a9c52c2009-10-02 15:16:51 -04003249 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3250 "Saving DIF for %u blocks to debugfs\n",
3251 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3252 lpfc_debug_save_dif(phba, cmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003253 }
3254
3255 _dump_buf_done = 1;
3256 }
3257 spin_unlock(&_dump_buf_lock);
3258
3259 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3260 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003261 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3262 "9072 BLKGRD: Invalid BG Profile in cmd"
3263 " 0x%x lba 0x%llx blk cnt 0x%x "
3264 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3265 (unsigned long long)scsi_get_lba(cmd),
3266 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003267 ret = (-1);
3268 goto out;
3269 }
3270
3271 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3272 cmd->result = ScsiResult(DID_ERROR, 0);
James Smart737d4242013-04-17 20:14:49 -04003273 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3274 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3275 " 0x%x lba 0x%llx blk cnt 0x%x "
3276 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3277 (unsigned long long)scsi_get_lba(cmd),
3278 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003279 ret = (-1);
3280 goto out;
3281 }
3282
3283 if (lpfc_bgs_get_guard_err(bgstat)) {
3284 ret = 1;
3285
3286 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3287 0x10, 0x1);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003288 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003289 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3290 phba->bg_guard_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003291 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3292 "9055 BLKGRD: Guard Tag error in cmd"
3293 " 0x%x lba 0x%llx blk cnt 0x%x "
3294 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3295 (unsigned long long)scsi_get_lba(cmd),
3296 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003297 }
3298
3299 if (lpfc_bgs_get_reftag_err(bgstat)) {
3300 ret = 1;
3301
3302 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3303 0x10, 0x3);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003304 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003305 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3306
3307 phba->bg_reftag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003308 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3309 "9056 BLKGRD: Ref Tag error in cmd"
3310 " 0x%x lba 0x%llx blk cnt 0x%x "
3311 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3312 (unsigned long long)scsi_get_lba(cmd),
3313 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003314 }
3315
3316 if (lpfc_bgs_get_apptag_err(bgstat)) {
3317 ret = 1;
3318
3319 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3320 0x10, 0x2);
Martin K. Petersen1c9fbaf2009-01-04 03:14:11 -05003321 cmd->result = DRIVER_SENSE << 24
James Smarte2a0a9d2008-12-04 22:40:02 -05003322 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3323
3324 phba->bg_apptag_err_cnt++;
James Smart737d4242013-04-17 20:14:49 -04003325 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3326 "9061 BLKGRD: App Tag error in cmd"
3327 " 0x%x lba 0x%llx blk cnt 0x%x "
3328 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3329 (unsigned long long)scsi_get_lba(cmd),
3330 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003331 }
3332
3333 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3334 /*
3335 * setup sense data descriptor 0 per SPC-4 as an information
James Smart7c56b9f2011-07-22 18:36:25 -04003336 * field, and put the failing LBA in it.
3337 * This code assumes there was also a guard/app/ref tag error
3338 * indication.
James Smarte2a0a9d2008-12-04 22:40:02 -05003339 */
James Smart7c56b9f2011-07-22 18:36:25 -04003340 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3341 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3342 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3343 cmd->sense_buffer[10] = 0x80; /* Validity bit */
James Smartacd68592012-01-18 16:25:09 -05003344
3345 /* bghm is a "on the wire" FC frame based count */
3346 switch (scsi_get_prot_op(cmd)) {
3347 case SCSI_PROT_READ_INSERT:
3348 case SCSI_PROT_WRITE_STRIP:
3349 bghm /= cmd->device->sector_size;
3350 break;
3351 case SCSI_PROT_READ_STRIP:
3352 case SCSI_PROT_WRITE_INSERT:
3353 case SCSI_PROT_READ_PASS:
3354 case SCSI_PROT_WRITE_PASS:
3355 bghm /= (cmd->device->sector_size +
3356 sizeof(struct scsi_dif_tuple));
3357 break;
3358 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003359
3360 failing_sector = scsi_get_lba(cmd);
3361 failing_sector += bghm;
3362
James Smart7c56b9f2011-07-22 18:36:25 -04003363 /* Descriptor Information */
3364 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
James Smarte2a0a9d2008-12-04 22:40:02 -05003365 }
3366
3367 if (!ret) {
3368 /* No error was reported - problem in FW? */
James Smart737d4242013-04-17 20:14:49 -04003369 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3370 "9057 BLKGRD: Unknown error in cmd"
3371 " 0x%x lba 0x%llx blk cnt 0x%x "
3372 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3373 (unsigned long long)scsi_get_lba(cmd),
3374 blk_rq_sectors(cmd->request), bgstat, bghm);
James Smarte2a0a9d2008-12-04 22:40:02 -05003375
James Smart737d4242013-04-17 20:14:49 -04003376 /* Calcuate what type of error it was */
3377 lpfc_calc_bg_err(phba, lpfc_cmd);
3378 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003379out:
3380 return ret;
3381}
3382
James Smartea2151b2008-09-07 11:52:10 -04003383/**
James Smartda0436e2009-05-22 14:51:39 -04003384 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3385 * @phba: The Hba for which this call is being executed.
3386 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3387 *
3388 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3389 * field of @lpfc_cmd for device with SLI-4 interface spec.
3390 *
3391 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003392 * 1 - Error
3393 * 0 - Success
James Smartda0436e2009-05-22 14:51:39 -04003394 **/
3395static int
3396lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3397{
3398 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3399 struct scatterlist *sgel = NULL;
3400 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3401 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
James Smartfedd3b72011-02-16 12:39:24 -05003402 struct sli4_sge *first_data_sgl;
James Smartda0436e2009-05-22 14:51:39 -04003403 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3404 dma_addr_t physaddr;
3405 uint32_t num_bde = 0;
3406 uint32_t dma_len;
3407 uint32_t dma_offset = 0;
3408 int nseg;
James Smartfedd3b72011-02-16 12:39:24 -05003409 struct ulp_bde64 *bde;
James Smartda0436e2009-05-22 14:51:39 -04003410
3411 /*
3412 * There are three possibilities here - use scatter-gather segment, use
3413 * the single mapping, or neither. Start the lpfc command prep by
3414 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3415 * data bde entry.
3416 */
3417 if (scsi_sg_count(scsi_cmnd)) {
3418 /*
3419 * The driver stores the segment count returned from pci_map_sg
3420 * because this a count of dma-mappings used to map the use_sg
3421 * pages. They are not guaranteed to be the same for those
3422 * architectures that implement an IOMMU.
3423 */
3424
3425 nseg = scsi_dma_map(scsi_cmnd);
3426 if (unlikely(!nseg))
3427 return 1;
3428 sgl += 1;
3429 /* clear the last flag in the fcp_rsp map entry */
3430 sgl->word2 = le32_to_cpu(sgl->word2);
3431 bf_set(lpfc_sli4_sge_last, sgl, 0);
3432 sgl->word2 = cpu_to_le32(sgl->word2);
3433 sgl += 1;
James Smartfedd3b72011-02-16 12:39:24 -05003434 first_data_sgl = sgl;
James Smartda0436e2009-05-22 14:51:39 -04003435 lpfc_cmd->seg_cnt = nseg;
3436 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
James Smart6a9c52c2009-10-02 15:16:51 -04003437 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3438 " %s: Too many sg segments from "
3439 "dma_map_sg. Config %d, seg_cnt %d\n",
3440 __func__, phba->cfg_sg_seg_cnt,
James Smartda0436e2009-05-22 14:51:39 -04003441 lpfc_cmd->seg_cnt);
James Smart96f70772013-04-17 20:16:15 -04003442 lpfc_cmd->seg_cnt = 0;
James Smartda0436e2009-05-22 14:51:39 -04003443 scsi_dma_unmap(scsi_cmnd);
3444 return 1;
3445 }
3446
3447 /*
3448 * The driver established a maximum scatter-gather segment count
3449 * during probe that limits the number of sg elements in any
3450 * single scsi command. Just run through the seg_cnt and format
3451 * the sge's.
3452 * When using SLI-3 the driver will try to fit all the BDEs into
3453 * the IOCB. If it can't then the BDEs get added to a BPL as it
3454 * does for SLI-2 mode.
3455 */
3456 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3457 physaddr = sg_dma_address(sgel);
3458 dma_len = sg_dma_len(sgel);
James Smartda0436e2009-05-22 14:51:39 -04003459 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3460 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
James Smart05580562011-05-24 11:40:48 -04003461 sgl->word2 = le32_to_cpu(sgl->word2);
James Smartda0436e2009-05-22 14:51:39 -04003462 if ((num_bde + 1) == nseg)
3463 bf_set(lpfc_sli4_sge_last, sgl, 1);
3464 else
3465 bf_set(lpfc_sli4_sge_last, sgl, 0);
3466 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
James Smartf9bb2da2011-10-10 21:34:11 -04003467 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
James Smartda0436e2009-05-22 14:51:39 -04003468 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05003469 sgl->sge_len = cpu_to_le32(dma_len);
James Smartda0436e2009-05-22 14:51:39 -04003470 dma_offset += dma_len;
3471 sgl++;
3472 }
James Smartfedd3b72011-02-16 12:39:24 -05003473 /* setup the performance hint (first data BDE) if enabled */
3474 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3475 bde = (struct ulp_bde64 *)
3476 &(iocb_cmd->unsli3.sli3Words[5]);
3477 bde->addrLow = first_data_sgl->addr_lo;
3478 bde->addrHigh = first_data_sgl->addr_hi;
3479 bde->tus.f.bdeSize =
3480 le32_to_cpu(first_data_sgl->sge_len);
3481 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3482 bde->tus.w = cpu_to_le32(bde->tus.w);
3483 }
James Smartda0436e2009-05-22 14:51:39 -04003484 } else {
3485 sgl += 1;
3486 /* clear the last flag in the fcp_rsp map entry */
3487 sgl->word2 = le32_to_cpu(sgl->word2);
3488 bf_set(lpfc_sli4_sge_last, sgl, 1);
3489 sgl->word2 = cpu_to_le32(sgl->word2);
3490 }
3491
3492 /*
3493 * Finish initializing those IOCB fields that are dependent on the
3494 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3495 * explicitly reinitialized.
3496 * all iocb memory resources are reused.
3497 */
3498 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3499
3500 /*
3501 * Due to difference in data length between DIF/non-DIF paths,
3502 * we need to set word 4 of IOCB here
3503 */
3504 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3505 return 0;
3506}
3507
3508/**
James Smartacd68592012-01-18 16:25:09 -05003509 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3510 * @phba: The Hba for which this call is being executed.
3511 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3512 *
3513 * This is the protection/DIF aware version of
3514 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3515 * two functions eventually, but for now, it's here
3516 **/
3517static int
3518lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3519 struct lpfc_scsi_buf *lpfc_cmd)
3520{
3521 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3522 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3523 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3524 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart96f70772013-04-17 20:16:15 -04003525 uint32_t num_sge = 0;
James Smartacd68592012-01-18 16:25:09 -05003526 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3527 int prot_group_type = 0;
3528 int fcpdl;
3529
3530 /*
3531 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
James Smart96f70772013-04-17 20:16:15 -04003532 * fcp_rsp regions to the first data sge entry
James Smartacd68592012-01-18 16:25:09 -05003533 */
3534 if (scsi_sg_count(scsi_cmnd)) {
3535 /*
3536 * The driver stores the segment count returned from pci_map_sg
3537 * because this a count of dma-mappings used to map the use_sg
3538 * pages. They are not guaranteed to be the same for those
3539 * architectures that implement an IOMMU.
3540 */
3541 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3542 scsi_sglist(scsi_cmnd),
3543 scsi_sg_count(scsi_cmnd), datadir);
3544 if (unlikely(!datasegcnt))
3545 return 1;
3546
3547 sgl += 1;
3548 /* clear the last flag in the fcp_rsp map entry */
3549 sgl->word2 = le32_to_cpu(sgl->word2);
3550 bf_set(lpfc_sli4_sge_last, sgl, 0);
3551 sgl->word2 = cpu_to_le32(sgl->word2);
3552
3553 sgl += 1;
3554 lpfc_cmd->seg_cnt = datasegcnt;
James Smart96f70772013-04-17 20:16:15 -04003555
3556 /* First check if data segment count from SCSI Layer is good */
3557 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3558 goto err;
James Smartacd68592012-01-18 16:25:09 -05003559
3560 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3561
3562 switch (prot_group_type) {
3563 case LPFC_PG_TYPE_NO_DIF:
James Smart96f70772013-04-17 20:16:15 -04003564 /* Here we need to add a DISEED to the count */
3565 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3566 goto err;
3567
3568 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003569 datasegcnt);
James Smart96f70772013-04-17 20:16:15 -04003570
James Smartacd68592012-01-18 16:25:09 -05003571 /* we should have 2 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003572 if (num_sge < 2)
James Smartacd68592012-01-18 16:25:09 -05003573 goto err;
3574 break;
James Smart96f70772013-04-17 20:16:15 -04003575
3576 case LPFC_PG_TYPE_DIF_BUF:
James Smartacd68592012-01-18 16:25:09 -05003577 /*
3578 * This type indicates that protection buffers are
3579 * passed to the driver, so that needs to be prepared
3580 * for DMA
3581 */
3582 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3583 scsi_prot_sglist(scsi_cmnd),
3584 scsi_prot_sg_count(scsi_cmnd), datadir);
3585 if (unlikely(!protsegcnt)) {
3586 scsi_dma_unmap(scsi_cmnd);
3587 return 1;
3588 }
3589
3590 lpfc_cmd->prot_seg_cnt = protsegcnt;
James Smart96f70772013-04-17 20:16:15 -04003591 /*
3592 * There is a minimun of 3 SGEs used for every
3593 * protection data segment.
3594 */
3595 if ((lpfc_cmd->prot_seg_cnt * 3) >
3596 (phba->cfg_total_seg_cnt - 2))
3597 goto err;
James Smartacd68592012-01-18 16:25:09 -05003598
James Smart96f70772013-04-17 20:16:15 -04003599 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
James Smartacd68592012-01-18 16:25:09 -05003600 datasegcnt, protsegcnt);
James Smart96f70772013-04-17 20:16:15 -04003601
James Smartacd68592012-01-18 16:25:09 -05003602 /* we should have 3 or more entries in buffer list */
James Smart96f70772013-04-17 20:16:15 -04003603 if ((num_sge < 3) ||
3604 (num_sge > phba->cfg_total_seg_cnt))
James Smartacd68592012-01-18 16:25:09 -05003605 goto err;
3606 break;
James Smart96f70772013-04-17 20:16:15 -04003607
James Smartacd68592012-01-18 16:25:09 -05003608 case LPFC_PG_TYPE_INVALID:
3609 default:
James Smart96f70772013-04-17 20:16:15 -04003610 scsi_dma_unmap(scsi_cmnd);
3611 lpfc_cmd->seg_cnt = 0;
3612
James Smartacd68592012-01-18 16:25:09 -05003613 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3614 "9083 Unexpected protection group %i\n",
3615 prot_group_type);
3616 return 1;
3617 }
3618 }
3619
James Smart8012cc32012-10-31 14:44:49 -04003620 switch (scsi_get_prot_op(scsi_cmnd)) {
3621 case SCSI_PROT_WRITE_STRIP:
3622 case SCSI_PROT_READ_STRIP:
3623 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3624 break;
3625 case SCSI_PROT_WRITE_INSERT:
3626 case SCSI_PROT_READ_INSERT:
3627 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3628 break;
3629 case SCSI_PROT_WRITE_PASS:
3630 case SCSI_PROT_READ_PASS:
3631 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3632 break;
3633 }
3634
James Smartacd68592012-01-18 16:25:09 -05003635 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
James Smartacd68592012-01-18 16:25:09 -05003636 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3637
3638 /*
3639 * Due to difference in data length between DIF/non-DIF paths,
3640 * we need to set word 4 of IOCB here
3641 */
3642 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
James Smartacd68592012-01-18 16:25:09 -05003643
3644 return 0;
3645err:
James Smart96f70772013-04-17 20:16:15 -04003646 if (lpfc_cmd->seg_cnt)
3647 scsi_dma_unmap(scsi_cmnd);
3648 if (lpfc_cmd->prot_seg_cnt)
3649 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3650 scsi_prot_sg_count(scsi_cmnd),
3651 scsi_cmnd->sc_data_direction);
3652
James Smartacd68592012-01-18 16:25:09 -05003653 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart96f70772013-04-17 20:16:15 -04003654 "9084 Cannot setup S/G List for HBA"
3655 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3656 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3657 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3658 prot_group_type, num_sge);
3659
3660 lpfc_cmd->seg_cnt = 0;
3661 lpfc_cmd->prot_seg_cnt = 0;
James Smartacd68592012-01-18 16:25:09 -05003662 return 1;
3663}
3664
3665/**
James Smart3772a992009-05-22 14:50:54 -04003666 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3667 * @phba: The Hba for which this call is being executed.
3668 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3669 *
3670 * This routine wraps the actual DMA mapping function pointer from the
3671 * lpfc_hba struct.
3672 *
3673 * Return codes:
James Smart6c8eea52010-04-06 14:49:53 -04003674 * 1 - Error
3675 * 0 - Success
James Smart3772a992009-05-22 14:50:54 -04003676 **/
3677static inline int
3678lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3679{
3680 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3681}
3682
3683/**
James Smartacd68592012-01-18 16:25:09 -05003684 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3685 * using BlockGuard.
3686 * @phba: The Hba for which this call is being executed.
3687 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3688 *
3689 * This routine wraps the actual DMA mapping function pointer from the
3690 * lpfc_hba struct.
3691 *
3692 * Return codes:
3693 * 1 - Error
3694 * 0 - Success
3695 **/
3696static inline int
3697lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3698{
3699 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3700}
3701
3702/**
James Smart3621a712009-04-06 18:47:14 -04003703 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
James Smartea2151b2008-09-07 11:52:10 -04003704 * @phba: Pointer to hba context object.
3705 * @vport: Pointer to vport object.
3706 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3707 * @rsp_iocb: Pointer to response iocb object which reported error.
3708 *
3709 * This function posts an event when there is a SCSI command reporting
3710 * error from the scsi device.
3711 **/
3712static void
3713lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3714 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3715 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3716 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3717 uint32_t resp_info = fcprsp->rspStatus2;
3718 uint32_t scsi_status = fcprsp->rspStatus3;
3719 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3720 struct lpfc_fast_path_event *fast_path_evt = NULL;
3721 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3722 unsigned long flags;
3723
James Smart5989b8d2010-10-22 11:06:56 -04003724 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3725 return;
3726
James Smartea2151b2008-09-07 11:52:10 -04003727 /* If there is queuefull or busy condition send a scsi event */
3728 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3729 (cmnd->result == SAM_STAT_BUSY)) {
3730 fast_path_evt = lpfc_alloc_fast_evt(phba);
3731 if (!fast_path_evt)
3732 return;
3733 fast_path_evt->un.scsi_evt.event_type =
3734 FC_REG_SCSI_EVENT;
3735 fast_path_evt->un.scsi_evt.subcategory =
3736 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3737 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3738 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3739 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3740 &pnode->nlp_portname, sizeof(struct lpfc_name));
3741 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3742 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3743 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3744 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3745 fast_path_evt = lpfc_alloc_fast_evt(phba);
3746 if (!fast_path_evt)
3747 return;
3748 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3749 FC_REG_SCSI_EVENT;
3750 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3751 LPFC_EVENT_CHECK_COND;
3752 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3753 cmnd->device->lun;
3754 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3755 &pnode->nlp_portname, sizeof(struct lpfc_name));
3756 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3757 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3758 fast_path_evt->un.check_cond_evt.sense_key =
3759 cmnd->sense_buffer[2] & 0xf;
3760 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3761 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3762 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3763 fcpi_parm &&
3764 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3765 ((scsi_status == SAM_STAT_GOOD) &&
3766 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3767 /*
3768 * If status is good or resid does not match with fcp_param and
3769 * there is valid fcpi_parm, then there is a read_check error
3770 */
3771 fast_path_evt = lpfc_alloc_fast_evt(phba);
3772 if (!fast_path_evt)
3773 return;
3774 fast_path_evt->un.read_check_error.header.event_type =
3775 FC_REG_FABRIC_EVENT;
3776 fast_path_evt->un.read_check_error.header.subcategory =
3777 LPFC_EVENT_FCPRDCHKERR;
3778 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3779 &pnode->nlp_portname, sizeof(struct lpfc_name));
3780 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3781 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3782 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3783 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3784 fast_path_evt->un.read_check_error.fcpiparam =
3785 fcpi_parm;
3786 } else
3787 return;
3788
3789 fast_path_evt->vport = vport;
3790 spin_lock_irqsave(&phba->hbalock, flags);
3791 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3792 spin_unlock_irqrestore(&phba->hbalock, flags);
3793 lpfc_worker_wake_up(phba);
3794 return;
3795}
James Smart9bad7672008-12-04 22:39:02 -05003796
3797/**
James Smartf1126682009-06-10 17:22:44 -04003798 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
James Smart3772a992009-05-22 14:50:54 -04003799 * @phba: The HBA for which this call is being executed.
James Smart9bad7672008-12-04 22:39:02 -05003800 * @psb: The scsi buffer which is going to be un-mapped.
3801 *
3802 * This routine does DMA un-mapping of scatter gather list of scsi command
James Smart3772a992009-05-22 14:50:54 -04003803 * field of @lpfc_cmd for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05003804 **/
dea31012005-04-17 16:05:31 -05003805static void
James Smartf1126682009-06-10 17:22:44 -04003806lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James Smartbcf4dbf2006-07-06 15:50:08 -04003807{
3808 /*
3809 * There are only two special cases to consider. (1) the scsi command
3810 * requested scatter-gather usage or (2) the scsi command allocated
3811 * a request buffer, but did not request use_sg. There is a third
3812 * case, but it does not require resource deallocation.
3813 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003814 if (psb->seg_cnt > 0)
3815 scsi_dma_unmap(psb->pCmd);
James Smarte2a0a9d2008-12-04 22:40:02 -05003816 if (psb->prot_seg_cnt > 0)
3817 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3818 scsi_prot_sg_count(psb->pCmd),
3819 psb->pCmd->sc_data_direction);
James Smartbcf4dbf2006-07-06 15:50:08 -04003820}
3821
James Smart9bad7672008-12-04 22:39:02 -05003822/**
James Smart3621a712009-04-06 18:47:14 -04003823 * lpfc_handler_fcp_err - FCP response handler
James Smart9bad7672008-12-04 22:39:02 -05003824 * @vport: The virtual port for which this call is being executed.
3825 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3826 * @rsp_iocb: The response IOCB which contains FCP error.
3827 *
3828 * This routine is called to process response IOCB with status field
3829 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3830 * based upon SCSI and FCP error.
3831 **/
James Smartbcf4dbf2006-07-06 15:50:08 -04003832static void
James Smart2e0fef82007-06-17 19:56:36 -05003833lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3834 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -05003835{
3836 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3837 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3838 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -04003839 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -05003840 uint32_t resp_info = fcprsp->rspStatus2;
3841 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -05003842 uint32_t *lp;
dea31012005-04-17 16:05:31 -05003843 uint32_t host_status = DID_OK;
3844 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -05003845 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -05003846
James Smartea2151b2008-09-07 11:52:10 -04003847
dea31012005-04-17 16:05:31 -05003848 /*
3849 * If this is a task management command, there is no
3850 * scsi packet associated with this lpfc_cmd. The driver
3851 * consumes it.
3852 */
3853 if (fcpcmd->fcpCntl2) {
3854 scsi_status = 0;
3855 goto out;
3856 }
3857
James Smart6a9c52c2009-10-02 15:16:51 -04003858 if (resp_info & RSP_LEN_VALID) {
3859 rsplen = be32_to_cpu(fcprsp->rspRspLen);
James Smarte40a02c2010-02-26 14:13:54 -05003860 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
James Smart6a9c52c2009-10-02 15:16:51 -04003861 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3862 "2719 Invalid response length: "
3863 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3864 cmnd->device->id,
3865 cmnd->device->lun, cmnd->cmnd[0],
3866 rsplen);
3867 host_status = DID_ERROR;
3868 goto out;
3869 }
James Smarte40a02c2010-02-26 14:13:54 -05003870 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3871 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3872 "2757 Protocol failure detected during "
3873 "processing of FCP I/O op: "
3874 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3875 cmnd->device->id,
3876 cmnd->device->lun, cmnd->cmnd[0],
3877 fcprsp->rspInfo3);
3878 host_status = DID_ERROR;
3879 goto out;
3880 }
James Smart6a9c52c2009-10-02 15:16:51 -04003881 }
3882
James Smartc7743952006-12-02 13:34:42 -05003883 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3884 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3885 if (snslen > SCSI_SENSE_BUFFERSIZE)
3886 snslen = SCSI_SENSE_BUFFERSIZE;
3887
3888 if (resp_info & RSP_LEN_VALID)
3889 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3890 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3891 }
3892 lp = (uint32_t *)cmnd->sense_buffer;
3893
James Smartaa1c7ee2012-08-14 14:26:06 -04003894 /* special handling for under run conditions */
3895 if (!scsi_status && (resp_info & RESID_UNDER)) {
3896 /* don't log under runs if fcp set... */
3897 if (vport->cfg_log_verbose & LOG_FCP)
3898 logit = LOG_FCP_ERROR;
3899 /* unless operator says so */
3900 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3901 logit = LOG_FCP_UNDER;
3902 }
James Smartc7743952006-12-02 13:34:42 -05003903
James Smarte8b62012007-08-02 11:10:09 -04003904 lpfc_printf_vlog(vport, KERN_WARNING, logit,
James Smarte2a0a9d2008-12-04 22:40:02 -05003905 "9024 FCP command x%x failed: x%x SNS x%x x%x "
James Smarte8b62012007-08-02 11:10:09 -04003906 "Data: x%x x%x x%x x%x x%x\n",
3907 cmnd->cmnd[0], scsi_status,
3908 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3909 be32_to_cpu(fcprsp->rspResId),
3910 be32_to_cpu(fcprsp->rspSnsLen),
3911 be32_to_cpu(fcprsp->rspRspLen),
3912 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -05003913
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003914 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -05003915 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003916 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -05003917
James Smart73d91e52011-10-10 21:32:10 -04003918 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
James Smarte2a0a9d2008-12-04 22:40:02 -05003919 "9025 FCP Read Underrun, expected %d, "
James Smarte8b62012007-08-02 11:10:09 -04003920 "residual %d Data: x%x x%x x%x\n",
3921 be32_to_cpu(fcpcmd->fcpDl),
3922 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3923 cmnd->underflow);
dea31012005-04-17 16:05:31 -05003924
3925 /*
James Smart7054a602007-04-25 09:52:34 -04003926 * If there is an under run check if under run reported by
3927 * storage array is same as the under run reported by HBA.
3928 * If this is not same, there is a dropped frame.
3929 */
3930 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3931 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003932 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -04003933 lpfc_printf_vlog(vport, KERN_WARNING,
3934 LOG_FCP | LOG_FCP_ERROR,
James Smarte2a0a9d2008-12-04 22:40:02 -05003935 "9026 FCP Read Check Error "
James Smarte8b62012007-08-02 11:10:09 -04003936 "and Underrun Data: x%x x%x x%x x%x\n",
3937 be32_to_cpu(fcpcmd->fcpDl),
3938 scsi_get_resid(cmnd), fcpi_parm,
3939 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003940 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -04003941 host_status = DID_ERROR;
3942 }
3943 /*
dea31012005-04-17 16:05:31 -05003944 * The cmnd->underflow is the minimum number of bytes that must
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003945 * be transferred for this command. Provided a sense condition
dea31012005-04-17 16:05:31 -05003946 * is not present, make sure the actual amount transferred is at
3947 * least the underflow value or fail.
3948 */
3949 if (!(resp_info & SNS_LEN_VALID) &&
3950 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003951 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3952 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -04003953 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003954 "9027 FCP command x%x residual "
James Smarte8b62012007-08-02 11:10:09 -04003955 "underrun converted to error "
3956 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -04003957 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -04003958 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -05003959 host_status = DID_ERROR;
3960 }
3961 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -04003962 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
James Smarte2a0a9d2008-12-04 22:40:02 -05003963 "9028 FCP command x%x residual overrun error. "
James Smarte4e74272009-07-19 10:01:38 -04003964 "Data: x%x x%x\n", cmnd->cmnd[0],
James Smarte8b62012007-08-02 11:10:09 -04003965 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -05003966 host_status = DID_ERROR;
3967
3968 /*
3969 * Check SLI validation that all the transfer was actually done
James Smart26373d22013-09-06 12:19:17 -04003970 * (fcpi_parm should be zero). Apply check only to reads.
dea31012005-04-17 16:05:31 -05003971 */
James Smart26373d22013-09-06 12:19:17 -04003972 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -04003973 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart26373d22013-09-06 12:19:17 -04003974 "9029 FCP Read Check Error Data: "
James Smarteee88772010-09-29 11:19:08 -04003975 "x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003976 be32_to_cpu(fcpcmd->fcpDl),
3977 be32_to_cpu(fcprsp->rspResId),
James Smarteee88772010-09-29 11:19:08 -04003978 fcpi_parm, cmnd->cmnd[0], scsi_status);
3979 switch (scsi_status) {
3980 case SAM_STAT_GOOD:
3981 case SAM_STAT_CHECK_CONDITION:
3982 /* Fabric dropped a data frame. Fail any successful
3983 * command in which we detected dropped frames.
3984 * A status of good or some check conditions could
3985 * be considered a successful command.
3986 */
3987 host_status = DID_ERROR;
3988 break;
3989 }
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05003990 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -05003991 }
3992
3993 out:
3994 cmnd->result = ScsiResult(host_status, scsi_status);
James Smartea2151b2008-09-07 11:52:10 -04003995 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
dea31012005-04-17 16:05:31 -05003996}
3997
James Smart9bad7672008-12-04 22:39:02 -05003998/**
James Smart3621a712009-04-06 18:47:14 -04003999 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
James Smart9bad7672008-12-04 22:39:02 -05004000 * @phba: The Hba for which this call is being executed.
4001 * @pIocbIn: The command IOCBQ for the scsi cmnd.
James Smart3772a992009-05-22 14:50:54 -04004002 * @pIocbOut: The response IOCBQ for the scsi cmnd.
James Smart9bad7672008-12-04 22:39:02 -05004003 *
4004 * This routine assigns scsi command result by looking into response IOCB
4005 * status field appropriately. This routine handles QUEUE FULL condition as
4006 * well by ramping down device queue depth.
4007 **/
dea31012005-04-17 16:05:31 -05004008static void
4009lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4010 struct lpfc_iocbq *pIocbOut)
4011{
4012 struct lpfc_scsi_buf *lpfc_cmd =
4013 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -05004014 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -05004015 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4016 struct lpfc_nodelist *pnode = rdata->pnode;
James Smart75baf692010-06-08 18:31:21 -04004017 struct scsi_cmnd *cmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004018 int result;
James Smarta257bf92009-04-06 18:48:10 -04004019 struct scsi_device *tmp_sdev;
James Smart5ffc2662009-11-18 15:39:44 -05004020 int depth;
James Smartfa61a542008-01-11 01:52:42 -05004021 unsigned long flags;
James Smartea2151b2008-09-07 11:52:10 -04004022 struct lpfc_fast_path_event *fast_path_evt;
James Smart75baf692010-06-08 18:31:21 -04004023 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04004024 uint32_t queue_depth, scsi_id;
James Smart73d91e52011-10-10 21:32:10 -04004025 uint32_t logit = LOG_FCP;
dea31012005-04-17 16:05:31 -05004026
James Smart75baf692010-06-08 18:31:21 -04004027 /* Sanity check on return of outstanding command */
4028 if (!(lpfc_cmd->pCmd))
4029 return;
4030 cmd = lpfc_cmd->pCmd;
4031 shost = cmd->device->host;
4032
James Smarte3d2b802012-08-14 14:25:43 -04004033 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
dea31012005-04-17 16:05:31 -05004034 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
James Smart341af102010-01-26 23:07:37 -05004035 /* pick up SLI4 exhange busy status from HBA */
4036 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
4037
James Smart9a6b09c2012-03-01 22:37:42 -05004038#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4039 if (lpfc_cmd->prot_data_type) {
4040 struct scsi_dif_tuple *src = NULL;
4041
4042 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4043 /*
4044 * Used to restore any changes to protection
4045 * data for error injection.
4046 */
4047 switch (lpfc_cmd->prot_data_type) {
4048 case LPFC_INJERR_REFTAG:
4049 src->ref_tag =
4050 lpfc_cmd->prot_data;
4051 break;
4052 case LPFC_INJERR_APPTAG:
4053 src->app_tag =
4054 (uint16_t)lpfc_cmd->prot_data;
4055 break;
4056 case LPFC_INJERR_GUARD:
4057 src->guard_tag =
4058 (uint16_t)lpfc_cmd->prot_data;
4059 break;
4060 default:
4061 break;
4062 }
4063
4064 lpfc_cmd->prot_data = 0;
4065 lpfc_cmd->prot_data_type = 0;
4066 lpfc_cmd->prot_data_segment = NULL;
4067 }
4068#endif
James Smart109f6ed2008-12-04 22:39:08 -05004069 if (pnode && NLP_CHK_NODE_ACT(pnode))
4070 atomic_dec(&pnode->cmd_pending);
dea31012005-04-17 16:05:31 -05004071
4072 if (lpfc_cmd->status) {
4073 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4074 (lpfc_cmd->result & IOERR_DRVR_MASK))
4075 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4076 else if (lpfc_cmd->status >= IOSTAT_CNT)
4077 lpfc_cmd->status = IOSTAT_DEFAULT;
James Smartaa1c7ee2012-08-14 14:26:06 -04004078 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4079 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4080 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4081 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
James Smart73d91e52011-10-10 21:32:10 -04004082 logit = 0;
4083 else
4084 logit = LOG_FCP | LOG_FCP_UNDER;
4085 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4086 "9030 FCP cmd x%x failed <%d/%d> "
James Smart5a0d80f2012-05-09 21:18:20 -04004087 "status: x%x result: x%x "
4088 "sid: x%x did: x%x oxid: x%x "
4089 "Data: x%x x%x\n",
James Smart73d91e52011-10-10 21:32:10 -04004090 cmd->cmnd[0],
4091 cmd->device ? cmd->device->id : 0xffff,
4092 cmd->device ? cmd->device->lun : 0xffff,
4093 lpfc_cmd->status, lpfc_cmd->result,
James Smart3bf41ba2013-05-31 17:03:18 -04004094 vport->fc_myDID,
4095 (pnode) ? pnode->nlp_DID : 0,
James Smart5a0d80f2012-05-09 21:18:20 -04004096 phba->sli_rev == LPFC_SLI_REV4 ?
4097 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
James Smart73d91e52011-10-10 21:32:10 -04004098 pIocbOut->iocb.ulpContext,
4099 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -05004100
4101 switch (lpfc_cmd->status) {
4102 case IOSTAT_FCP_RSP_ERROR:
4103 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -05004104 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -05004105 break;
4106 case IOSTAT_NPORT_BSY:
4107 case IOSTAT_FABRIC_BSY:
James Smart0f1f53a2008-08-24 21:50:18 -04004108 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
James Smartea2151b2008-09-07 11:52:10 -04004109 fast_path_evt = lpfc_alloc_fast_evt(phba);
4110 if (!fast_path_evt)
4111 break;
4112 fast_path_evt->un.fabric_evt.event_type =
4113 FC_REG_FABRIC_EVENT;
4114 fast_path_evt->un.fabric_evt.subcategory =
4115 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4116 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4117 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4118 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4119 &pnode->nlp_portname,
4120 sizeof(struct lpfc_name));
4121 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4122 &pnode->nlp_nodename,
4123 sizeof(struct lpfc_name));
4124 }
4125 fast_path_evt->vport = vport;
4126 fast_path_evt->work_evt.evt =
4127 LPFC_EVT_FASTPATH_MGMT_EVT;
4128 spin_lock_irqsave(&phba->hbalock, flags);
4129 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4130 &phba->work_list);
4131 spin_unlock_irqrestore(&phba->hbalock, flags);
4132 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05004133 break;
James Smart92d7f7b2007-06-17 19:56:38 -05004134 case IOSTAT_LOCAL_REJECT:
James Smart1151e3e2011-02-16 12:39:35 -05004135 case IOSTAT_REMOTE_STOP:
James Smartab56dc22011-02-16 12:39:57 -05004136 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4137 lpfc_cmd->result ==
4138 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4139 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4140 lpfc_cmd->result ==
4141 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4142 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4143 break;
4144 }
James Smartd7c255b2008-08-24 21:50:00 -04004145 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
James Smart92d7f7b2007-06-17 19:56:38 -05004146 lpfc_cmd->result == IOERR_NO_RESOURCES ||
James Smartb92938b2010-06-07 15:24:12 -04004147 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4148 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
James Smart92d7f7b2007-06-17 19:56:38 -05004149 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -04004150 break;
James Smarte2a0a9d2008-12-04 22:40:02 -05004151 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004152 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4153 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4154 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4155 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4156 /*
4157 * This is a response for a BG enabled
4158 * cmd. Parse BG error
4159 */
4160 lpfc_parse_bg_err(phba, lpfc_cmd,
4161 pIocbOut);
4162 break;
4163 } else {
4164 lpfc_printf_vlog(vport, KERN_WARNING,
4165 LOG_BG,
4166 "9031 non-zero BGSTAT "
James Smart6a9c52c2009-10-02 15:16:51 -04004167 "on unprotected cmd\n");
James Smarte2a0a9d2008-12-04 22:40:02 -05004168 }
4169 }
James Smart1151e3e2011-02-16 12:39:35 -05004170 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4171 && (phba->sli_rev == LPFC_SLI_REV4)
4172 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4173 /* This IO was aborted by the target, we don't
4174 * know the rxid and because we did not send the
4175 * ABTS we cannot generate and RRQ.
4176 */
4177 lpfc_set_rrq_active(phba, pnode,
James Smartee0f4fe2012-05-09 21:19:14 -04004178 lpfc_cmd->cur_iocbq.sli4_lxritag,
4179 0, 0);
James Smart1151e3e2011-02-16 12:39:35 -05004180 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004181 /* else: fall through */
dea31012005-04-17 16:05:31 -05004182 default:
4183 cmd->result = ScsiResult(DID_ERROR, 0);
4184 break;
4185 }
4186
James Smart58da1ff2008-04-07 10:15:56 -04004187 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004188 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
James Smart0f1f53a2008-08-24 21:50:18 -04004189 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4190 SAM_STAT_BUSY);
James Smartab56dc22011-02-16 12:39:57 -05004191 } else
dea31012005-04-17 16:05:31 -05004192 cmd->result = ScsiResult(DID_OK, 0);
dea31012005-04-17 16:05:31 -05004193
4194 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4195 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4196
James Smarte8b62012007-08-02 11:10:09 -04004197 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4198 "0710 Iodone <%d/%d> cmd %p, error "
4199 "x%x SNS x%x x%x Data: x%x x%x\n",
4200 cmd->device->id, cmd->device->lun, cmd,
4201 cmd->result, *lp, *(lp + 3), cmd->retries,
4202 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -05004203 }
4204
James Smartea2151b2008-09-07 11:52:10 -04004205 lpfc_update_stats(phba, lpfc_cmd);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004206 result = cmd->result;
James Smart977b5a02008-09-07 11:52:04 -04004207 if (vport->cfg_max_scsicmpl_time &&
4208 time_after(jiffies, lpfc_cmd->start_time +
4209 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
James Smarta257bf92009-04-06 18:48:10 -04004210 spin_lock_irqsave(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004211 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4212 if (pnode->cmd_qdepth >
4213 atomic_read(&pnode->cmd_pending) &&
4214 (atomic_read(&pnode->cmd_pending) >
4215 LPFC_MIN_TGT_QDEPTH) &&
4216 ((cmd->cmnd[0] == READ_10) ||
4217 (cmd->cmnd[0] == WRITE_10)))
4218 pnode->cmd_qdepth =
4219 atomic_read(&pnode->cmd_pending);
James Smart977b5a02008-09-07 11:52:04 -04004220
James Smart109f6ed2008-12-04 22:39:08 -05004221 pnode->last_change_time = jiffies;
4222 }
James Smarta257bf92009-04-06 18:48:10 -04004223 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004224 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
James Smart7dc517d2010-07-14 15:32:10 -04004225 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
James Smart977b5a02008-09-07 11:52:04 -04004226 time_after(jiffies, pnode->last_change_time +
James Smart109f6ed2008-12-04 22:39:08 -05004227 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
James Smarta257bf92009-04-06 18:48:10 -04004228 spin_lock_irqsave(shost->host_lock, flags);
James Smart7dc517d2010-07-14 15:32:10 -04004229 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4230 / 100;
4231 depth = depth ? depth : 1;
4232 pnode->cmd_qdepth += depth;
4233 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4234 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004235 pnode->last_change_time = jiffies;
James Smarta257bf92009-04-06 18:48:10 -04004236 spin_unlock_irqrestore(shost->host_lock, flags);
James Smart109f6ed2008-12-04 22:39:08 -05004237 }
James Smart977b5a02008-09-07 11:52:04 -04004238 }
4239
James Smart1dcb58e2007-04-25 09:51:30 -04004240 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smarta257bf92009-04-06 18:48:10 -04004241
4242 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4243 queue_depth = cmd->device->queue_depth;
4244 scsi_id = cmd->device->id;
dea31012005-04-17 16:05:31 -05004245 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004246
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004247 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart876dd7d2012-09-29 11:31:28 -04004248 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004249 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004250 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004251
James Smartfa61a542008-01-11 01:52:42 -05004252 /*
4253 * If there is a thread waiting for command completion
4254 * wake up the thread.
4255 */
James Smarta257bf92009-04-06 18:48:10 -04004256 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004257 if (lpfc_cmd->waitq)
4258 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004259 spin_unlock_irqrestore(shost->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05004260 lpfc_release_scsi_buf(phba, lpfc_cmd);
4261 return;
4262 }
4263
James Smart92d7f7b2007-06-17 19:56:38 -05004264 if (!result)
James Smarta257bf92009-04-06 18:48:10 -04004265 lpfc_rampup_queue_depth(vport, queue_depth);
James Smart92d7f7b2007-06-17 19:56:38 -05004266
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004267 /*
4268 * Check for queue full. If the lun is reporting queue full, then
4269 * back off the lun queue depth to prevent target overloads.
4270 */
James Smart58da1ff2008-04-07 10:15:56 -04004271 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4272 NLP_CHK_NODE_ACT(pnode)) {
James Smarta257bf92009-04-06 18:48:10 -04004273 shost_for_each_device(tmp_sdev, shost) {
4274 if (tmp_sdev->id != scsi_id)
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004275 continue;
4276 depth = scsi_track_queue_full(tmp_sdev,
James Smart5ffc2662009-11-18 15:39:44 -05004277 tmp_sdev->queue_depth-1);
4278 if (depth <= 0)
4279 continue;
James Smarte8b62012007-08-02 11:10:09 -04004280 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4281 "0711 detected queue full - lun queue "
4282 "depth adjusted to %d.\n", depth);
James Smartea2151b2008-09-07 11:52:10 -04004283 lpfc_send_sdev_queuedepth_change_event(phba, vport,
James Smart5ffc2662009-11-18 15:39:44 -05004284 pnode,
4285 tmp_sdev->lun,
4286 depth+1, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -05004287 }
4288 }
4289
James Smart876dd7d2012-09-29 11:31:28 -04004290 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004291 lpfc_cmd->pCmd = NULL;
James Smart876dd7d2012-09-29 11:31:28 -04004292 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart92e3af62012-08-14 14:26:28 -04004293
James Smartfa61a542008-01-11 01:52:42 -05004294 /*
4295 * If there is a thread waiting for command completion
4296 * wake up the thread.
4297 */
James Smarta257bf92009-04-06 18:48:10 -04004298 spin_lock_irqsave(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004299 if (lpfc_cmd->waitq)
4300 wake_up(lpfc_cmd->waitq);
James Smarta257bf92009-04-06 18:48:10 -04004301 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -05004302
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004303 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004304}
4305
James Smart34b02dc2008-08-24 21:49:55 -04004306/**
James Smart3621a712009-04-06 18:47:14 -04004307 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
James Smart34b02dc2008-08-24 21:49:55 -04004308 * @data: A pointer to the immediate command data portion of the IOCB.
4309 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4310 *
4311 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4312 * byte swapping the data to big endian format for transmission on the wire.
4313 **/
4314static void
4315lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4316{
4317 int i, j;
4318 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4319 i += sizeof(uint32_t), j++) {
4320 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4321 }
4322}
4323
James Smart9bad7672008-12-04 22:39:02 -05004324/**
James Smartf1126682009-06-10 17:22:44 -04004325 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004326 * @vport: The virtual port for which this call is being executed.
4327 * @lpfc_cmd: The scsi command which needs to send.
4328 * @pnode: Pointer to lpfc_nodelist.
4329 *
4330 * This routine initializes fcp_cmnd and iocb data structure from scsi command
James Smart3772a992009-05-22 14:50:54 -04004331 * to transfer for device with SLI3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004332 **/
dea31012005-04-17 16:05:31 -05004333static void
James Smartf1126682009-06-10 17:22:44 -04004334lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
James Smart2e0fef82007-06-17 19:56:36 -05004335 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -05004336{
James Smart2e0fef82007-06-17 19:56:36 -05004337 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004338 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4339 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4340 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4341 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4342 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -04004343 char tag[2];
James Smart027140e2012-08-03 12:35:44 -04004344 uint8_t *ptr;
4345 bool sli4;
James Smart98bbf5f2013-09-06 12:18:45 -04004346 uint32_t fcpdl;
dea31012005-04-17 16:05:31 -05004347
James Smart58da1ff2008-04-07 10:15:56 -04004348 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4349 return;
4350
dea31012005-04-17 16:05:31 -05004351 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -04004352 /* clear task management bits */
4353 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -05004354
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -04004355 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4356 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004357
James Smart027140e2012-08-03 12:35:44 -04004358 ptr = &fcp_cmnd->fcpCdb[0];
4359 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4360 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4361 ptr += scsi_cmnd->cmd_len;
4362 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4363 }
4364
James Smart7e2b19f2007-10-29 11:00:39 -04004365 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4366 switch (tag[0]) {
dea31012005-04-17 16:05:31 -05004367 case HEAD_OF_QUEUE_TAG:
4368 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4369 break;
4370 case ORDERED_QUEUE_TAG:
4371 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4372 break;
4373 default:
4374 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4375 break;
4376 }
4377 } else
James Smartfe8f7f92013-01-03 15:43:03 -05004378 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
dea31012005-04-17 16:05:31 -05004379
James Smart027140e2012-08-03 12:35:44 -04004380 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4381
dea31012005-04-17 16:05:31 -05004382 /*
4383 * There are three possibilities here - use scatter-gather segment, use
4384 * the single mapping, or neither. Start the lpfc command prep by
4385 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4386 * data bde entry.
4387 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -05004388 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -05004389 if (datadir == DMA_TO_DEVICE) {
4390 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
James Smart182ba752013-07-15 18:34:05 -04004391 iocb_cmd->ulpPU = PARM_READ_CHECK;
James Smart3cb01c52013-07-15 18:35:04 -04004392 if (vport->cfg_first_burst_size &&
4393 (pnode->nlp_flag & NLP_FIRSTBURST)) {
James Smart98bbf5f2013-09-06 12:18:45 -04004394 fcpdl = scsi_bufflen(scsi_cmnd);
4395 if (fcpdl < vport->cfg_first_burst_size)
4396 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4397 else
4398 piocbq->iocb.un.fcpi.fcpi_XRdy =
4399 vport->cfg_first_burst_size;
James Smart3cb01c52013-07-15 18:35:04 -04004400 }
dea31012005-04-17 16:05:31 -05004401 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4402 phba->fc4OutputRequests++;
4403 } else {
4404 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4405 iocb_cmd->ulpPU = PARM_READ_CHECK;
dea31012005-04-17 16:05:31 -05004406 fcp_cmnd->fcpCntl3 = READ_DATA;
4407 phba->fc4InputRequests++;
4408 }
4409 } else {
4410 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4411 iocb_cmd->un.fcpi.fcpi_parm = 0;
4412 iocb_cmd->ulpPU = 0;
4413 fcp_cmnd->fcpCntl3 = 0;
4414 phba->fc4ControlRequests++;
4415 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004416 if (phba->sli_rev == 3 &&
4417 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004418 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004419 /*
4420 * Finish initializing those IOCB fields that are independent
4421 * of the scsi_cmnd request_buffer
4422 */
4423 piocbq->iocb.ulpContext = pnode->nlp_rpi;
James Smart027140e2012-08-03 12:35:44 -04004424 if (sli4)
James Smart6d368e52011-05-24 11:44:12 -04004425 piocbq->iocb.ulpContext =
4426 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
dea31012005-04-17 16:05:31 -05004427 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4428 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -05004429 else
4430 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -05004431
4432 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4433 piocbq->context1 = lpfc_cmd;
4434 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4435 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -05004436 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -05004437}
4438
James Smart9bad7672008-12-04 22:39:02 -05004439/**
James Smart6d368e52011-05-24 11:44:12 -04004440 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
James Smart9bad7672008-12-04 22:39:02 -05004441 * @vport: The virtual port for which this call is being executed.
4442 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4443 * @lun: Logical unit number.
4444 * @task_mgmt_cmd: SCSI task management command.
4445 *
James Smart3772a992009-05-22 14:50:54 -04004446 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4447 * for device with SLI-3 interface spec.
James Smart9bad7672008-12-04 22:39:02 -05004448 *
4449 * Return codes:
4450 * 0 - Error
4451 * 1 - Success
4452 **/
dea31012005-04-17 16:05:31 -05004453static int
James Smartf1126682009-06-10 17:22:44 -04004454lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -05004455 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -04004456 unsigned int lun,
dea31012005-04-17 16:05:31 -05004457 uint8_t task_mgmt_cmd)
4458{
dea31012005-04-17 16:05:31 -05004459 struct lpfc_iocbq *piocbq;
4460 IOCB_t *piocb;
4461 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -04004462 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -05004463 struct lpfc_nodelist *ndlp = rdata->pnode;
4464
James Smart58da1ff2008-04-07 10:15:56 -04004465 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4466 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05004467 return 0;
dea31012005-04-17 16:05:31 -05004468
dea31012005-04-17 16:05:31 -05004469 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -05004470 piocbq->vport = vport;
4471
dea31012005-04-17 16:05:31 -05004472 piocb = &piocbq->iocb;
4473
4474 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart34b02dc2008-08-24 21:49:55 -04004475 /* Clear out any old data in the FCP command area */
4476 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4477 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -05004478 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
James Smarte2a0a9d2008-12-04 22:40:02 -05004479 if (vport->phba->sli_rev == 3 &&
4480 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
James Smart34b02dc2008-08-24 21:49:55 -04004481 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
dea31012005-04-17 16:05:31 -05004482 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
dea31012005-04-17 16:05:31 -05004483 piocb->ulpContext = ndlp->nlp_rpi;
James Smart6d368e52011-05-24 11:44:12 -04004484 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4485 piocb->ulpContext =
4486 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4487 }
dea31012005-04-17 16:05:31 -05004488 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4489 piocb->ulpFCP2Rcvy = 1;
4490 }
4491 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4492
4493 /* ulpTimeout is only one byte */
4494 if (lpfc_cmd->timeout > 0xff) {
4495 /*
4496 * Do not timeout the command at the firmware level.
4497 * The driver will provide the timeout mechanism.
4498 */
4499 piocb->ulpTimeout = 0;
James Smartf1126682009-06-10 17:22:44 -04004500 } else
dea31012005-04-17 16:05:31 -05004501 piocb->ulpTimeout = lpfc_cmd->timeout;
James Smartf1126682009-06-10 17:22:44 -04004502
4503 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4504 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004505
James Smart2e0fef82007-06-17 19:56:36 -05004506 return 1;
dea31012005-04-17 16:05:31 -05004507}
4508
James Smart9bad7672008-12-04 22:39:02 -05004509/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004510 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
James Smart3772a992009-05-22 14:50:54 -04004511 * @phba: The hba struct for which this call is being executed.
4512 * @dev_grp: The HBA PCI-Device group number.
4513 *
4514 * This routine sets up the SCSI interface API function jump table in @phba
4515 * struct.
4516 * Returns: 0 - success, -ENODEV - failure.
4517 **/
4518int
4519lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4520{
4521
James Smartf1126682009-06-10 17:22:44 -04004522 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4523 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
James Smartf1126682009-06-10 17:22:44 -04004524
James Smart3772a992009-05-22 14:50:54 -04004525 switch (dev_grp) {
4526 case LPFC_PCI_DEV_LP:
4527 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4528 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
James Smartacd68592012-01-18 16:25:09 -05004529 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004530 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
James Smart19ca7602010-11-20 23:11:55 -05004531 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
James Smart3772a992009-05-22 14:50:54 -04004532 break;
James Smartda0436e2009-05-22 14:51:39 -04004533 case LPFC_PCI_DEV_OC:
4534 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4535 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
James Smartacd68592012-01-18 16:25:09 -05004536 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004537 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
James Smart19ca7602010-11-20 23:11:55 -05004538 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
James Smartda0436e2009-05-22 14:51:39 -04004539 break;
James Smart3772a992009-05-22 14:50:54 -04004540 default:
4541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4542 "1418 Invalid HBA PCI-device group: 0x%x\n",
4543 dev_grp);
4544 return -ENODEV;
4545 break;
4546 }
James Smart3772a992009-05-22 14:50:54 -04004547 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
James Smart84d1b002010-02-12 14:42:33 -05004548 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
James Smart3772a992009-05-22 14:50:54 -04004549 return 0;
4550}
4551
4552/**
James Smart3621a712009-04-06 18:47:14 -04004553 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
James Smart9bad7672008-12-04 22:39:02 -05004554 * @phba: The Hba for which this call is being executed.
4555 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4556 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4557 *
4558 * This routine is IOCB completion routine for device reset and target reset
4559 * routine. This routine release scsi buffer associated with lpfc_cmd.
4560 **/
James Smart7054a602007-04-25 09:52:34 -04004561static void
4562lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4563 struct lpfc_iocbq *cmdiocbq,
4564 struct lpfc_iocbq *rspiocbq)
4565{
4566 struct lpfc_scsi_buf *lpfc_cmd =
4567 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4568 if (lpfc_cmd)
4569 lpfc_release_scsi_buf(phba, lpfc_cmd);
4570 return;
4571}
4572
James Smart9bad7672008-12-04 22:39:02 -05004573/**
James Smart3621a712009-04-06 18:47:14 -04004574 * lpfc_info - Info entry point of scsi_host_template data structure
James Smart9bad7672008-12-04 22:39:02 -05004575 * @host: The scsi host for which this call is being executed.
4576 *
4577 * This routine provides module information about hba.
4578 *
4579 * Reutrn code:
4580 * Pointer to char - Success.
4581 **/
dea31012005-04-17 16:05:31 -05004582const char *
4583lpfc_info(struct Scsi_Host *host)
4584{
James Smart2e0fef82007-06-17 19:56:36 -05004585 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4586 struct lpfc_hba *phba = vport->phba;
James Smart8b68cd52012-09-29 11:32:37 -04004587 int len, link_speed = 0;
dea31012005-04-17 16:05:31 -05004588 static char lpfcinfobuf[384];
4589
4590 memset(lpfcinfobuf,0,384);
4591 if (phba && phba->pcidev){
4592 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4593 len = strlen(lpfcinfobuf);
4594 snprintf(lpfcinfobuf + len,
4595 384-len,
4596 " on PCI bus %02x device %02x irq %d",
4597 phba->pcidev->bus->number,
4598 phba->pcidev->devfn,
4599 phba->pcidev->irq);
4600 len = strlen(lpfcinfobuf);
4601 if (phba->Port[0]) {
4602 snprintf(lpfcinfobuf + len,
4603 384-len,
4604 " port %s",
4605 phba->Port);
4606 }
James Smart65467b62010-01-26 23:08:29 -05004607 len = strlen(lpfcinfobuf);
James Smart8b68cd52012-09-29 11:32:37 -04004608 if (phba->sli_rev <= LPFC_SLI_REV3) {
4609 link_speed = lpfc_sli_port_speed_get(phba);
4610 } else {
4611 if (phba->sli4_hba.link_state.logical_speed)
4612 link_speed =
4613 phba->sli4_hba.link_state.logical_speed;
4614 else
4615 link_speed = phba->sli4_hba.link_state.speed;
James Smart65467b62010-01-26 23:08:29 -05004616 }
James Smart8b68cd52012-09-29 11:32:37 -04004617 if (link_speed != 0)
4618 snprintf(lpfcinfobuf + len, 384-len,
4619 " Logical Link Speed: %d Mbps", link_speed);
dea31012005-04-17 16:05:31 -05004620 }
4621 return lpfcinfobuf;
4622}
4623
James Smart9bad7672008-12-04 22:39:02 -05004624/**
James Smart3621a712009-04-06 18:47:14 -04004625 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
James Smart9bad7672008-12-04 22:39:02 -05004626 * @phba: The Hba for which this call is being executed.
4627 *
4628 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4629 * The default value of cfg_poll_tmo is 10 milliseconds.
4630 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004631static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4632{
4633 unsigned long poll_tmo_expires =
4634 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4635
James Smart0e9bb8d2013-03-01 16:35:12 -05004636 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004637 mod_timer(&phba->fcp_poll_timer,
4638 poll_tmo_expires);
4639}
4640
James Smart9bad7672008-12-04 22:39:02 -05004641/**
James Smart3621a712009-04-06 18:47:14 -04004642 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
James Smart9bad7672008-12-04 22:39:02 -05004643 * @phba: The Hba for which this call is being executed.
4644 *
4645 * This routine starts the fcp_poll_timer of @phba.
4646 **/
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004647void lpfc_poll_start_timer(struct lpfc_hba * phba)
4648{
4649 lpfc_poll_rearm_timer(phba);
4650}
4651
James Smart9bad7672008-12-04 22:39:02 -05004652/**
James Smart3621a712009-04-06 18:47:14 -04004653 * lpfc_poll_timeout - Restart polling timer
James Smart9bad7672008-12-04 22:39:02 -05004654 * @ptr: Map to lpfc_hba data structure pointer.
4655 *
4656 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4657 * and FCP Ring interrupt is disable.
4658 **/
4659
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004660void lpfc_poll_timeout(unsigned long ptr)
4661{
James Smart2e0fef82007-06-17 19:56:36 -05004662 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004663
4664 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004665 lpfc_sli_handle_fast_ring_event(phba,
4666 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4667
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004668 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4669 lpfc_poll_rearm_timer(phba);
4670 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004671}
4672
James Smart9bad7672008-12-04 22:39:02 -05004673/**
James Smart3621a712009-04-06 18:47:14 -04004674 * lpfc_queuecommand - scsi_host_template queuecommand entry point
James Smart9bad7672008-12-04 22:39:02 -05004675 * @cmnd: Pointer to scsi_cmnd data structure.
4676 * @done: Pointer to done routine.
4677 *
4678 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4679 * This routine prepares an IOCB from scsi command and provides to firmware.
4680 * The @done callback is invoked after driver finished processing the command.
4681 *
4682 * Return value :
4683 * 0 - Success
4684 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4685 **/
dea31012005-04-17 16:05:31 -05004686static int
James Smartb9a7c632012-08-03 12:35:24 -04004687lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004688{
James Smart2e0fef82007-06-17 19:56:36 -05004689 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4690 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004691 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05004692 struct lpfc_nodelist *ndlp;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004693 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004694 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004695 int err;
dea31012005-04-17 16:05:31 -05004696
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004697 err = fc_remote_port_chkready(rport);
4698 if (err) {
4699 cmnd->result = err;
dea31012005-04-17 16:05:31 -05004700 goto out_fail_command;
4701 }
James Smart1c6f4ef52009-11-18 15:40:49 -05004702 ndlp = rdata->pnode;
dea31012005-04-17 16:05:31 -05004703
James Smartbf086112011-08-21 21:48:13 -04004704 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
James Smartacd68592012-01-18 16:25:09 -05004705 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
James Smarte2a0a9d2008-12-04 22:40:02 -05004706
James Smart6a9c52c2009-10-02 15:16:51 -04004707 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4708 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4709 " op:%02x str=%s without registering for"
4710 " BlockGuard - Rejecting command\n",
James Smarte2a0a9d2008-12-04 22:40:02 -05004711 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4712 dif_op_str[scsi_get_prot_op(cmnd)]);
4713 goto out_fail_command;
4714 }
4715
dea31012005-04-17 16:05:31 -05004716 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004717 * Catch race where our node has transitioned, but the
4718 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05004719 */
James Smart6b415f52012-06-12 13:54:59 -04004720 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4721 goto out_tgt_busy;
James Smart7dc517d2010-07-14 15:32:10 -04004722 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
Mike Christie34963432011-02-25 14:04:28 -06004723 goto out_tgt_busy;
Mike Christiea93ce022008-08-17 15:24:41 -05004724
James Smart19ca7602010-11-20 23:11:55 -05004725 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
dea31012005-04-17 16:05:31 -05004726 if (lpfc_cmd == NULL) {
James Smarteaf15d52008-12-04 22:39:29 -05004727 lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05004728
James Smarte8b62012007-08-02 11:10:09 -04004729 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4730 "0707 driver's buffer pool is empty, "
4731 "IO busied\n");
dea31012005-04-17 16:05:31 -05004732 goto out_host_busy;
4733 }
4734
4735 /*
4736 * Store the midlayer's command structure for the completion phase
4737 * and complete the command initialization.
4738 */
4739 lpfc_cmd->pCmd = cmnd;
4740 lpfc_cmd->rdata = rdata;
4741 lpfc_cmd->timeout = 0;
James Smart977b5a02008-09-07 11:52:04 -04004742 lpfc_cmd->start_time = jiffies;
dea31012005-04-17 16:05:31 -05004743 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
dea31012005-04-17 16:05:31 -05004744
James Smarte2a0a9d2008-12-04 22:40:02 -05004745 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
James Smart6a9c52c2009-10-02 15:16:51 -04004746 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004747 lpfc_printf_vlog(vport,
4748 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004749 "9033 BLKGRD: rcvd %s cmd:x%x "
4750 "sector x%llx cnt %u pt %x\n",
4751 dif_op_str[scsi_get_prot_op(cmnd)],
4752 cmnd->cmnd[0],
4753 (unsigned long long)scsi_get_lba(cmnd),
4754 blk_rq_sectors(cmnd->request),
4755 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004756 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004757 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4758 } else {
James Smart6a9c52c2009-10-02 15:16:51 -04004759 if (vport->phba->cfg_enable_bg) {
James Smart737d4242013-04-17 20:14:49 -04004760 lpfc_printf_vlog(vport,
4761 KERN_INFO, LOG_SCSI_CMD,
James Smart26134702012-08-14 14:25:50 -04004762 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4763 "x%x sector x%llx cnt %u pt %x\n",
4764 cmnd->cmnd[0],
4765 (unsigned long long)scsi_get_lba(cmnd),
James Smart9a6b09c2012-03-01 22:37:42 -05004766 blk_rq_sectors(cmnd->request),
James Smart26134702012-08-14 14:25:50 -04004767 (cmnd->cmnd[1]>>5));
James Smart6a9c52c2009-10-02 15:16:51 -04004768 }
James Smarte2a0a9d2008-12-04 22:40:02 -05004769 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4770 }
4771
dea31012005-04-17 16:05:31 -05004772 if (err)
4773 goto out_host_busy_free_buf;
4774
James Smart2e0fef82007-06-17 19:56:36 -05004775 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05004776
James Smart977b5a02008-09-07 11:52:04 -04004777 atomic_inc(&ndlp->cmd_pending);
James Smart3772a992009-05-22 14:50:54 -04004778 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
James Smart92d7f7b2007-06-17 19:56:38 -05004779 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
James Smarteaf15d52008-12-04 22:39:29 -05004780 if (err) {
4781 atomic_dec(&ndlp->cmd_pending);
dea31012005-04-17 16:05:31 -05004782 goto out_host_busy_free_buf;
James Smarteaf15d52008-12-04 22:39:29 -05004783 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004784 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04004785 lpfc_sli_handle_fast_ring_event(phba,
4786 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4787
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004788 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4789 lpfc_poll_rearm_timer(phba);
4790 }
4791
dea31012005-04-17 16:05:31 -05004792 return 0;
4793
4794 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04004795 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004796 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05004797 out_host_busy:
4798 return SCSI_MLQUEUE_HOST_BUSY;
4799
Mike Christie34963432011-02-25 14:04:28 -06004800 out_tgt_busy:
4801 return SCSI_MLQUEUE_TARGET_BUSY;
4802
dea31012005-04-17 16:05:31 -05004803 out_fail_command:
James Smartb9a7c632012-08-03 12:35:24 -04004804 cmnd->scsi_done(cmnd);
dea31012005-04-17 16:05:31 -05004805 return 0;
4806}
4807
Jeff Garzikf2812332010-11-16 02:10:29 -05004808
James Smart9bad7672008-12-04 22:39:02 -05004809/**
James Smart3621a712009-04-06 18:47:14 -04004810 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05004811 * @cmnd: Pointer to scsi_cmnd data structure.
4812 *
4813 * This routine aborts @cmnd pending in base driver.
4814 *
4815 * Return code :
4816 * 0x2003 - Error
4817 * 0x2002 - Success
4818 **/
dea31012005-04-17 16:05:31 -05004819static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05004820lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05004821{
James Smart2e0fef82007-06-17 19:56:36 -05004822 struct Scsi_Host *shost = cmnd->device->host;
4823 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4824 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004825 struct lpfc_iocbq *iocb;
4826 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05004827 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05004828 IOCB_t *cmd, *icmd;
James Smart3a707302012-06-12 13:54:42 -04004829 int ret = SUCCESS, status = 0;
James Smart876dd7d2012-09-29 11:31:28 -04004830 unsigned long flags;
James Smartfa61a542008-01-11 01:52:42 -05004831 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004832
James Smart3a707302012-06-12 13:54:42 -04004833 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04004834 if (status != 0 && status != SUCCESS)
James Smart3a707302012-06-12 13:54:42 -04004835 return status;
James Smart4f2e66c2012-05-09 21:17:07 -04004836
James Smart876dd7d2012-09-29 11:31:28 -04004837 spin_lock_irqsave(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004838 /* driver queued commands are in process of being flushed */
4839 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
James Smart876dd7d2012-09-29 11:31:28 -04004840 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004841 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4842 "3168 SCSI Layer abort requested I/O has been "
4843 "flushed by LLD.\n");
4844 return FAILED;
4845 }
4846
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004847 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
James Smart92e3af62012-08-14 14:26:28 -04004848 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
James Smart876dd7d2012-09-29 11:31:28 -04004849 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarteee88772010-09-29 11:19:08 -04004850 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4851 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004852 "x%x ID %d LUN %d\n",
James Smart3a707302012-06-12 13:54:42 -04004853 SUCCESS, cmnd->device->id, cmnd->device->lun);
James Smarteee88772010-09-29 11:19:08 -04004854 return SUCCESS;
4855 }
dea31012005-04-17 16:05:31 -05004856
James Smart4f2e66c2012-05-09 21:17:07 -04004857 iocb = &lpfc_cmd->cur_iocbq;
4858 /* the command is in process of being cancelled */
4859 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
James Smart876dd7d2012-09-29 11:31:28 -04004860 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004861 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4862 "3169 SCSI Layer abort requested I/O has been "
4863 "cancelled by LLD.\n");
4864 return FAILED;
4865 }
dea31012005-04-17 16:05:31 -05004866 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004867 * If pCmd field of the corresponding lpfc_scsi_buf structure
4868 * points to a different SCSI command, then the driver has
4869 * already completed this command, but the midlayer did not
James Smart4f2e66c2012-05-09 21:17:07 -04004870 * see the completion before the eh fired. Just return SUCCESS.
dea31012005-04-17 16:05:31 -05004871 */
James Smart4f2e66c2012-05-09 21:17:07 -04004872 if (lpfc_cmd->pCmd != cmnd) {
4873 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4874 "3170 SCSI Layer abort requested I/O has been "
4875 "completed by LLD.\n");
4876 goto out_unlock;
4877 }
dea31012005-04-17 16:05:31 -05004878
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004879 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05004880
James Smart4f2e66c2012-05-09 21:17:07 -04004881 abtsiocb = __lpfc_sli_get_iocbq(phba);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004882 if (abtsiocb == NULL) {
4883 ret = FAILED;
James Smart4f2e66c2012-05-09 21:17:07 -04004884 goto out_unlock;
dea31012005-04-17 16:05:31 -05004885 }
4886
James Smartafbd8d82013-09-06 12:22:13 -04004887 /* Indicate the IO is being aborted by the driver. */
4888 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4889
dea31012005-04-17 16:05:31 -05004890 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004891 * The scsi command can not be in txq and it is in flight because the
4892 * pCmd is still pointig at the SCSI command we have to abort. There
4893 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05004894 */
dea31012005-04-17 16:05:31 -05004895
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004896 cmd = &iocb->iocb;
4897 icmd = &abtsiocb->iocb;
4898 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4899 icmd->un.acxri.abortContextTag = cmd->ulpContext;
James Smart3772a992009-05-22 14:50:54 -04004900 if (phba->sli_rev == LPFC_SLI_REV4)
4901 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4902 else
4903 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05004904
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004905 icmd->ulpLe = 1;
4906 icmd->ulpClass = cmd->ulpClass;
James Smart5ffc2662009-11-18 15:39:44 -05004907
4908 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4909 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -05004910 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart5ffc2662009-11-18 15:39:44 -05004911
James Smart2e0fef82007-06-17 19:56:36 -05004912 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004913 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4914 else
4915 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05004916
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004917 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05004918 abtsiocb->vport = vport;
James Smart4f2e66c2012-05-09 21:17:07 -04004919 /* no longer need the lock after this point */
James Smart876dd7d2012-09-29 11:31:28 -04004920 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004921
James Smart3772a992009-05-22 14:50:54 -04004922 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4923 IOCB_ERROR) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004924 lpfc_sli_release_iocbq(phba, abtsiocb);
4925 ret = FAILED;
4926 goto out;
4927 }
4928
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004929 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
James Smart45ed1192009-10-02 15:17:02 -04004930 lpfc_sli_handle_fast_ring_event(phba,
4931 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05004932
James Smartfa61a542008-01-11 01:52:42 -05004933 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004934 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05004935 wait_event_timeout(waitq,
4936 (lpfc_cmd->pCmd != cmnd),
James Smart256ec0d2013-04-17 20:14:58 -04004937 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
James Smartfa61a542008-01-11 01:52:42 -05004938 lpfc_cmd->waitq = NULL;
dea31012005-04-17 16:05:31 -05004939
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004940 if (lpfc_cmd->pCmd == cmnd) {
4941 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04004942 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4943 "0748 abort handler timed out waiting "
James Smart247ca942012-08-14 14:26:13 -04004944 "for abortng I/O (xri:x%x) to complete: "
4945 "ret %#x, ID %d, LUN %d\n",
4946 iocb->sli4_xritag, ret,
4947 cmnd->device->id, cmnd->device->lun);
dea31012005-04-17 16:05:31 -05004948 }
James Smart4f2e66c2012-05-09 21:17:07 -04004949 goto out;
dea31012005-04-17 16:05:31 -05004950
James Smart4f2e66c2012-05-09 21:17:07 -04004951out_unlock:
James Smart876dd7d2012-09-29 11:31:28 -04004952 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart4f2e66c2012-05-09 21:17:07 -04004953out:
James Smarte8b62012007-08-02 11:10:09 -04004954 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4955 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04004956 "LUN %d\n", ret, cmnd->device->id,
4957 cmnd->device->lun);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04004958 return ret;
dea31012005-04-17 16:05:31 -05004959}
4960
James Smartbbb9d182009-06-10 17:23:16 -04004961static char *
4962lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4963{
4964 switch (task_mgmt_cmd) {
4965 case FCP_ABORT_TASK_SET:
4966 return "ABORT_TASK_SET";
4967 case FCP_CLEAR_TASK_SET:
4968 return "FCP_CLEAR_TASK_SET";
4969 case FCP_BUS_RESET:
4970 return "FCP_BUS_RESET";
4971 case FCP_LUN_RESET:
4972 return "FCP_LUN_RESET";
4973 case FCP_TARGET_RESET:
4974 return "FCP_TARGET_RESET";
4975 case FCP_CLEAR_ACA:
4976 return "FCP_CLEAR_ACA";
4977 case FCP_TERMINATE_TASK:
4978 return "FCP_TERMINATE_TASK";
4979 default:
4980 return "unknown";
4981 }
4982}
4983
4984/**
4985 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4986 * @vport: The virtual port for which this call is being executed.
4987 * @rdata: Pointer to remote port local data
4988 * @tgt_id: Target ID of remote device.
4989 * @lun_id: Lun number for the TMF
4990 * @task_mgmt_cmd: type of TMF to send
4991 *
4992 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4993 * a remote port.
4994 *
4995 * Return Code:
4996 * 0x2003 - Error
4997 * 0x2002 - Success.
4998 **/
4999static int
5000lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
5001 unsigned tgt_id, unsigned int lun_id,
5002 uint8_t task_mgmt_cmd)
5003{
5004 struct lpfc_hba *phba = vport->phba;
5005 struct lpfc_scsi_buf *lpfc_cmd;
5006 struct lpfc_iocbq *iocbq;
5007 struct lpfc_iocbq *iocbqrsp;
James Smart5989b8d2010-10-22 11:06:56 -04005008 struct lpfc_nodelist *pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005009 int ret;
5010 int status;
5011
James Smart5989b8d2010-10-22 11:06:56 -04005012 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smartbbb9d182009-06-10 17:23:16 -04005013 return FAILED;
5014
James Smart19ca7602010-11-20 23:11:55 -05005015 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
James Smartbbb9d182009-06-10 17:23:16 -04005016 if (lpfc_cmd == NULL)
5017 return FAILED;
5018 lpfc_cmd->timeout = 60;
5019 lpfc_cmd->rdata = rdata;
5020
5021 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5022 task_mgmt_cmd);
5023 if (!status) {
5024 lpfc_release_scsi_buf(phba, lpfc_cmd);
5025 return FAILED;
5026 }
5027
5028 iocbq = &lpfc_cmd->cur_iocbq;
5029 iocbqrsp = lpfc_sli_get_iocbq(phba);
5030 if (iocbqrsp == NULL) {
5031 lpfc_release_scsi_buf(phba, lpfc_cmd);
5032 return FAILED;
5033 }
James Smart5a0916b2013-07-15 18:31:42 -04005034 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smartbbb9d182009-06-10 17:23:16 -04005035
5036 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5037 "0702 Issue %s to TGT %d LUN %d "
James Smart6d368e52011-05-24 11:44:12 -04005038 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005039 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
James Smart6d368e52011-05-24 11:44:12 -04005040 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5041 iocbq->iocb_flag);
James Smartbbb9d182009-06-10 17:23:16 -04005042
5043 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5044 iocbq, iocbqrsp, lpfc_cmd->timeout);
5045 if (status != IOCB_SUCCESS) {
5046 if (status == IOCB_TIMEDOUT) {
James Smartbbb9d182009-06-10 17:23:16 -04005047 ret = TIMEOUT_ERROR;
5048 } else
5049 ret = FAILED;
5050 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5051 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smart6d368e52011-05-24 11:44:12 -04005052 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
5053 "iocb_flag x%x\n",
James Smartbbb9d182009-06-10 17:23:16 -04005054 lpfc_taskmgmt_name(task_mgmt_cmd),
5055 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
James Smart6d368e52011-05-24 11:44:12 -04005056 iocbqrsp->iocb.un.ulpWord[4],
5057 iocbq->iocb_flag);
James Smart2a9bf3d2010-06-07 15:24:45 -04005058 } else if (status == IOCB_BUSY)
5059 ret = FAILED;
5060 else
James Smartbbb9d182009-06-10 17:23:16 -04005061 ret = SUCCESS;
5062
5063 lpfc_sli_release_iocbq(phba, iocbqrsp);
5064
5065 if (ret != TIMEOUT_ERROR)
5066 lpfc_release_scsi_buf(phba, lpfc_cmd);
5067
5068 return ret;
5069}
5070
5071/**
5072 * lpfc_chk_tgt_mapped -
5073 * @vport: The virtual port to check on
5074 * @cmnd: Pointer to scsi_cmnd data structure.
5075 *
5076 * This routine delays until the scsi target (aka rport) for the
5077 * command exists (is present and logged in) or we declare it non-existent.
5078 *
5079 * Return code :
5080 * 0x2003 - Error
5081 * 0x2002 - Success
5082 **/
5083static int
5084lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5085{
5086 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005087 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005088 unsigned long later;
5089
James Smart1c6f4ef52009-11-18 15:40:49 -05005090 if (!rdata) {
5091 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5092 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5093 return FAILED;
5094 }
5095 pnode = rdata->pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005096 /*
5097 * If target is not in a MAPPED state, delay until
5098 * target is rediscovered or devloss timeout expires.
5099 */
5100 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5101 while (time_after(later, jiffies)) {
5102 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5103 return FAILED;
5104 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5105 return SUCCESS;
5106 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5107 rdata = cmnd->device->hostdata;
5108 if (!rdata)
5109 return FAILED;
5110 pnode = rdata->pnode;
5111 }
5112 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5113 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5114 return FAILED;
5115 return SUCCESS;
5116}
5117
5118/**
5119 * lpfc_reset_flush_io_context -
5120 * @vport: The virtual port (scsi_host) for the flush context
5121 * @tgt_id: If aborting by Target contect - specifies the target id
5122 * @lun_id: If aborting by Lun context - specifies the lun id
5123 * @context: specifies the context level to flush at.
5124 *
5125 * After a reset condition via TMF, we need to flush orphaned i/o
5126 * contexts from the adapter. This routine aborts any contexts
5127 * outstanding, then waits for their completions. The wait is
5128 * bounded by devloss_tmo though.
5129 *
5130 * Return code :
5131 * 0x2003 - Error
5132 * 0x2002 - Success
5133 **/
5134static int
5135lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5136 uint64_t lun_id, lpfc_ctx_cmd context)
5137{
5138 struct lpfc_hba *phba = vport->phba;
5139 unsigned long later;
5140 int cnt;
5141
5142 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5143 if (cnt)
5144 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
5145 tgt_id, lun_id, context);
5146 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5147 while (time_after(later, jiffies) && cnt) {
5148 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5149 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5150 }
5151 if (cnt) {
5152 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5153 "0724 I/O flush failure for context %s : cnt x%x\n",
5154 ((context == LPFC_CTX_LUN) ? "LUN" :
5155 ((context == LPFC_CTX_TGT) ? "TGT" :
5156 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5157 cnt);
5158 return FAILED;
5159 }
5160 return SUCCESS;
5161}
5162
James Smart9bad7672008-12-04 22:39:02 -05005163/**
James Smart3621a712009-04-06 18:47:14 -04005164 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
James Smart9bad7672008-12-04 22:39:02 -05005165 * @cmnd: Pointer to scsi_cmnd data structure.
5166 *
James Smartbbb9d182009-06-10 17:23:16 -04005167 * This routine does a device reset by sending a LUN_RESET task management
James Smart9bad7672008-12-04 22:39:02 -05005168 * command.
5169 *
5170 * Return code :
5171 * 0x2003 - Error
James Smart3621a712009-04-06 18:47:14 -04005172 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005173 **/
dea31012005-04-17 16:05:31 -05005174static int
James Smart7054a602007-04-25 09:52:34 -04005175lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005176{
James Smart2e0fef82007-06-17 19:56:36 -05005177 struct Scsi_Host *shost = cmnd->device->host;
5178 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005179 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005180 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005181 unsigned tgt_id = cmnd->device->id;
5182 unsigned int lun_id = cmnd->device->lun;
James Smartea2151b2008-09-07 11:52:10 -04005183 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005184 int status, ret = SUCCESS;
dea31012005-04-17 16:05:31 -05005185
James Smart1c6f4ef52009-11-18 15:40:49 -05005186 if (!rdata) {
5187 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5188 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5189 return FAILED;
5190 }
5191 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005192 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005193 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005194 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005195
5196 status = lpfc_chk_tgt_mapped(vport, cmnd);
5197 if (status == FAILED) {
5198 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5199 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5200 return FAILED;
5201 }
5202
5203 scsi_event.event_type = FC_REG_SCSI_EVENT;
5204 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5205 scsi_event.lun = lun_id;
5206 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5207 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5208
5209 fc_host_post_vendor_event(shost, fc_get_event_number(),
5210 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5211
5212 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5213 FCP_LUN_RESET);
5214
5215 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5216 "0713 SCSI layer issued Device Reset (%d, %d) "
5217 "return x%x\n", tgt_id, lun_id, status);
5218
dea31012005-04-17 16:05:31 -05005219 /*
James Smartbbb9d182009-06-10 17:23:16 -04005220 * We have to clean up i/o as : they may be orphaned by the TMF;
5221 * or if the TMF failed, they may be in an indeterminate state.
5222 * So, continue on.
5223 * We will report success if all the i/o aborts successfully.
dea31012005-04-17 16:05:31 -05005224 */
James Smart3a707302012-06-12 13:54:42 -04005225 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
James Smartbbb9d182009-06-10 17:23:16 -04005226 LPFC_CTX_LUN);
James Smart3a707302012-06-12 13:54:42 -04005227 return ret;
James Smartbbb9d182009-06-10 17:23:16 -04005228}
5229
5230/**
5231 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5232 * @cmnd: Pointer to scsi_cmnd data structure.
5233 *
5234 * This routine does a target reset by sending a TARGET_RESET task management
5235 * command.
5236 *
5237 * Return code :
5238 * 0x2003 - Error
5239 * 0x2002 - Success
5240 **/
5241static int
5242lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5243{
5244 struct Scsi_Host *shost = cmnd->device->host;
5245 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5246 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
James Smart1c6f4ef52009-11-18 15:40:49 -05005247 struct lpfc_nodelist *pnode;
James Smartbbb9d182009-06-10 17:23:16 -04005248 unsigned tgt_id = cmnd->device->id;
5249 unsigned int lun_id = cmnd->device->lun;
5250 struct lpfc_scsi_event_header scsi_event;
James Smart3a707302012-06-12 13:54:42 -04005251 int status, ret = SUCCESS;
James Smartbbb9d182009-06-10 17:23:16 -04005252
James Smart1c6f4ef52009-11-18 15:40:49 -05005253 if (!rdata) {
5254 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5255 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5256 return FAILED;
5257 }
5258 pnode = rdata->pnode;
James Smart589a52d2010-07-14 15:30:54 -04005259 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005260 if (status != 0 && status != SUCCESS)
James Smart589a52d2010-07-14 15:30:54 -04005261 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005262
5263 status = lpfc_chk_tgt_mapped(vport, cmnd);
5264 if (status == FAILED) {
5265 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5266 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5267 return FAILED;
dea31012005-04-17 16:05:31 -05005268 }
James Smartea2151b2008-09-07 11:52:10 -04005269
5270 scsi_event.event_type = FC_REG_SCSI_EVENT;
5271 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5272 scsi_event.lun = 0;
5273 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5274 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5275
James Smartbbb9d182009-06-10 17:23:16 -04005276 fc_host_post_vendor_event(shost, fc_get_event_number(),
5277 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -04005278
James Smartbbb9d182009-06-10 17:23:16 -04005279 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5280 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05005281
James Smarte8b62012007-08-02 11:10:09 -04005282 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
James Smartbbb9d182009-06-10 17:23:16 -04005283 "0723 SCSI layer issued Target Reset (%d, %d) "
5284 "return x%x\n", tgt_id, lun_id, status);
5285
5286 /*
5287 * We have to clean up i/o as : they may be orphaned by the TMF;
5288 * or if the TMF failed, they may be in an indeterminate state.
5289 * So, continue on.
5290 * We will report success if all the i/o aborts successfully.
5291 */
James Smart3a707302012-06-12 13:54:42 -04005292 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5293 LPFC_CTX_TGT);
5294 return ret;
dea31012005-04-17 16:05:31 -05005295}
5296
James Smart9bad7672008-12-04 22:39:02 -05005297/**
James Smart3621a712009-04-06 18:47:14 -04005298 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
James Smart9bad7672008-12-04 22:39:02 -05005299 * @cmnd: Pointer to scsi_cmnd data structure.
5300 *
James Smartbbb9d182009-06-10 17:23:16 -04005301 * This routine does target reset to all targets on @cmnd->device->host.
5302 * This emulates Parallel SCSI Bus Reset Semantics.
James Smart9bad7672008-12-04 22:39:02 -05005303 *
James Smartbbb9d182009-06-10 17:23:16 -04005304 * Return code :
5305 * 0x2003 - Error
5306 * 0x2002 - Success
James Smart9bad7672008-12-04 22:39:02 -05005307 **/
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04005308static int
James Smart7054a602007-04-25 09:52:34 -04005309lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05005310{
James Smart2e0fef82007-06-17 19:56:36 -05005311 struct Scsi_Host *shost = cmnd->device->host;
5312 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
dea31012005-04-17 16:05:31 -05005313 struct lpfc_nodelist *ndlp = NULL;
James Smartea2151b2008-09-07 11:52:10 -04005314 struct lpfc_scsi_event_header scsi_event;
James Smartbbb9d182009-06-10 17:23:16 -04005315 int match;
5316 int ret = SUCCESS, status, i;
James Smartea2151b2008-09-07 11:52:10 -04005317
5318 scsi_event.event_type = FC_REG_SCSI_EVENT;
5319 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5320 scsi_event.lun = 0;
5321 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5322 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5323
James Smartbbb9d182009-06-10 17:23:16 -04005324 fc_host_post_vendor_event(shost, fc_get_event_number(),
5325 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
dea31012005-04-17 16:05:31 -05005326
James Smartbf086112011-08-21 21:48:13 -04005327 status = fc_block_scsi_eh(cmnd);
James Smart908e18e2012-08-03 12:34:54 -04005328 if (status != 0 && status != SUCCESS)
James Smartbf086112011-08-21 21:48:13 -04005329 return status;
James Smartbbb9d182009-06-10 17:23:16 -04005330
dea31012005-04-17 16:05:31 -05005331 /*
5332 * Since the driver manages a single bus device, reset all
5333 * targets known to the driver. Should any target reset
5334 * fail, this routine returns failure to the midlayer.
5335 */
James Smarte17da182006-07-06 15:49:25 -04005336 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04005337 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05005338 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05005339 spin_lock_irq(shost->host_lock);
5340 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005341 if (!NLP_CHK_NODE_ACT(ndlp))
5342 continue;
James Smarta6571c62012-10-31 14:44:42 -04005343 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5344 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5345 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005346 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04005347 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04005348 ndlp->rport) {
dea31012005-04-17 16:05:31 -05005349 match = 1;
5350 break;
5351 }
5352 }
James Smart2e0fef82007-06-17 19:56:36 -05005353 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005354 if (!match)
5355 continue;
James Smartbbb9d182009-06-10 17:23:16 -04005356
5357 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5358 i, 0, FCP_TARGET_RESET);
5359
5360 if (status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04005361 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5362 "0700 Bus Reset on target %d failed\n",
5363 i);
James Smart915caaa2008-06-14 22:52:38 -04005364 ret = FAILED;
dea31012005-04-17 16:05:31 -05005365 }
5366 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005367 /*
James Smartbbb9d182009-06-10 17:23:16 -04005368 * We have to clean up i/o as : they may be orphaned by the TMFs
5369 * above; or if any of the TMFs failed, they may be in an
5370 * indeterminate state.
5371 * We will report success if all the i/o aborts successfully.
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005372 */
James Smartbbb9d182009-06-10 17:23:16 -04005373
5374 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5375 if (status != SUCCESS)
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05005376 ret = FAILED;
James Smartbbb9d182009-06-10 17:23:16 -04005377
James Smarte8b62012007-08-02 11:10:09 -04005378 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5379 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05005380 return ret;
5381}
5382
James Smart9bad7672008-12-04 22:39:02 -05005383/**
James Smart27b01b82012-05-09 21:19:44 -04005384 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5385 * @cmnd: Pointer to scsi_cmnd data structure.
5386 *
5387 * This routine does host reset to the adaptor port. It brings the HBA
5388 * offline, performs a board restart, and then brings the board back online.
5389 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5390 * reject all outstanding SCSI commands to the host and error returned
5391 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5392 * of error handling, it will only return error if resetting of the adapter
5393 * is not successful; in all other cases, will return success.
5394 *
5395 * Return code :
5396 * 0x2003 - Error
5397 * 0x2002 - Success
5398 **/
5399static int
5400lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5401{
5402 struct Scsi_Host *shost = cmnd->device->host;
5403 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5404 struct lpfc_hba *phba = vport->phba;
5405 int rc, ret = SUCCESS;
5406
James Smarta88dbb62013-04-17 20:18:39 -04005407 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5408 "3172 SCSI layer issued Host Reset Data:\n");
5409
James Smart618a5232012-06-12 13:54:36 -04005410 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart27b01b82012-05-09 21:19:44 -04005411 lpfc_offline(phba);
5412 rc = lpfc_sli_brdrestart(phba);
5413 if (rc)
5414 ret = FAILED;
James Smarta88dbb62013-04-17 20:18:39 -04005415 rc = lpfc_online(phba);
5416 if (rc)
5417 ret = FAILED;
James Smart27b01b82012-05-09 21:19:44 -04005418 lpfc_unblock_mgmt_io(phba);
5419
James Smarta88dbb62013-04-17 20:18:39 -04005420 if (ret == FAILED) {
5421 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5422 "3323 Failed host reset, bring it offline\n");
5423 lpfc_sli4_offline_eratt(phba);
5424 }
James Smart27b01b82012-05-09 21:19:44 -04005425 return ret;
5426}
5427
5428/**
James Smart3621a712009-04-06 18:47:14 -04005429 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
James Smart9bad7672008-12-04 22:39:02 -05005430 * @sdev: Pointer to scsi_device.
5431 *
5432 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5433 * globally available list of scsi buffers. This routine also makes sure scsi
5434 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5435 * of scsi buffer exists for the lifetime of the driver.
5436 *
5437 * Return codes:
5438 * non-0 - Error
5439 * 0 - Success
5440 **/
dea31012005-04-17 16:05:31 -05005441static int
dea31012005-04-17 16:05:31 -05005442lpfc_slave_alloc(struct scsi_device *sdev)
5443{
James Smart2e0fef82007-06-17 19:56:36 -05005444 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5445 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005446 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
James Smart3772a992009-05-22 14:50:54 -04005447 uint32_t total = 0;
dea31012005-04-17 16:05:31 -05005448 uint32_t num_to_alloc = 0;
James Smart3772a992009-05-22 14:50:54 -04005449 int num_allocated = 0;
James Smartd7c47992010-06-08 18:31:54 -04005450 uint32_t sdev_cnt;
dea31012005-04-17 16:05:31 -05005451
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005452 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05005453 return -ENXIO;
5454
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005455 sdev->hostdata = rport->dd_data;
James Smartd7c47992010-06-08 18:31:54 -04005456 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005457
5458 /*
5459 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5460 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04005461 * HBA limit conveyed to the midlayer via the host structure. The
5462 * formula accounts for the lun_queue_depth + error handlers + 1
5463 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05005464 */
5465 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04005466 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05005467
James Smartd7c47992010-06-08 18:31:54 -04005468 /* If allocated buffers are enough do nothing */
5469 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5470 return 0;
5471
James Smart92d7f7b2007-06-17 19:56:38 -05005472 /* Allow some exchanges to be available always to complete discovery */
5473 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005474 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5475 "0704 At limitation of %d preallocated "
5476 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05005477 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05005478 /* Allow some exchanges to be available always to complete discovery */
5479 } else if (total + num_to_alloc >
5480 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04005481 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5482 "0705 Allocation request of %d "
5483 "command buffers will exceed max of %d. "
5484 "Reducing allocation request to %d.\n",
5485 num_to_alloc, phba->cfg_hba_queue_depth,
5486 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05005487 num_to_alloc = phba->cfg_hba_queue_depth - total;
5488 }
James Smart3772a992009-05-22 14:50:54 -04005489 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5490 if (num_to_alloc != num_allocated) {
James Smart96f70772013-04-17 20:16:15 -04005491 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5492 "0708 Allocation request of %d "
5493 "command buffers did not succeed. "
5494 "Allocated %d buffers.\n",
5495 num_to_alloc, num_allocated);
dea31012005-04-17 16:05:31 -05005496 }
James Smart1c6f4ef52009-11-18 15:40:49 -05005497 if (num_allocated > 0)
5498 phba->total_scsi_bufs += num_allocated;
dea31012005-04-17 16:05:31 -05005499 return 0;
5500}
5501
James Smart9bad7672008-12-04 22:39:02 -05005502/**
James Smart3621a712009-04-06 18:47:14 -04005503 * lpfc_slave_configure - scsi_host_template slave_configure entry point
James Smart9bad7672008-12-04 22:39:02 -05005504 * @sdev: Pointer to scsi_device.
5505 *
5506 * This routine configures following items
5507 * - Tag command queuing support for @sdev if supported.
James Smart9bad7672008-12-04 22:39:02 -05005508 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5509 *
5510 * Return codes:
5511 * 0 - Success
5512 **/
dea31012005-04-17 16:05:31 -05005513static int
5514lpfc_slave_configure(struct scsi_device *sdev)
5515{
James Smart2e0fef82007-06-17 19:56:36 -05005516 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5517 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005518
5519 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04005520 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005521 else
James Smart3de2a652007-08-02 11:09:59 -04005522 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05005523
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005524 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smart45ed1192009-10-02 15:17:02 -04005525 lpfc_sli_handle_fast_ring_event(phba,
5526 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05005527 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5528 lpfc_poll_rearm_timer(phba);
5529 }
5530
dea31012005-04-17 16:05:31 -05005531 return 0;
5532}
5533
James Smart9bad7672008-12-04 22:39:02 -05005534/**
James Smart3621a712009-04-06 18:47:14 -04005535 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
James Smart9bad7672008-12-04 22:39:02 -05005536 * @sdev: Pointer to scsi_device.
5537 *
5538 * This routine sets @sdev hostatdata filed to null.
5539 **/
dea31012005-04-17 16:05:31 -05005540static void
5541lpfc_slave_destroy(struct scsi_device *sdev)
5542{
James Smartd7c47992010-06-08 18:31:54 -04005543 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5544 struct lpfc_hba *phba = vport->phba;
5545 atomic_dec(&phba->sdev_cnt);
dea31012005-04-17 16:05:31 -05005546 sdev->hostdata = NULL;
5547 return;
5548}
5549
James Smart92d7f7b2007-06-17 19:56:38 -05005550
dea31012005-04-17 16:05:31 -05005551struct scsi_host_template lpfc_template = {
5552 .module = THIS_MODULE,
5553 .name = LPFC_DRIVER_NAME,
5554 .info = lpfc_info,
5555 .queuecommand = lpfc_queuecommand,
5556 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005557 .eh_device_reset_handler = lpfc_device_reset_handler,
5558 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart7054a602007-04-25 09:52:34 -04005559 .eh_bus_reset_handler = lpfc_bus_reset_handler,
James Smart27b01b82012-05-09 21:19:44 -04005560 .eh_host_reset_handler = lpfc_host_reset_handler,
dea31012005-04-17 16:05:31 -05005561 .slave_alloc = lpfc_slave_alloc,
5562 .slave_configure = lpfc_slave_configure,
5563 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04005564 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05005565 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005566 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05005567 .cmd_per_lun = LPFC_CMD_PER_LUN,
5568 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05005569 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04005570 .max_sectors = 0xFFFF,
James Smartf1c3b0f2009-07-19 10:01:32 -04005571 .vendor_id = LPFC_NL_VENDOR_ID,
James Smart5ffc2662009-11-18 15:39:44 -05005572 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005573 .change_queue_type = lpfc_change_queue_type,
dea31012005-04-17 16:05:31 -05005574};
James Smart3de2a652007-08-02 11:09:59 -04005575
5576struct scsi_host_template lpfc_vport_template = {
5577 .module = THIS_MODULE,
5578 .name = LPFC_DRIVER_NAME,
5579 .info = lpfc_info,
5580 .queuecommand = lpfc_queuecommand,
5581 .eh_abort_handler = lpfc_abort_handler,
James Smartbbb9d182009-06-10 17:23:16 -04005582 .eh_device_reset_handler = lpfc_device_reset_handler,
5583 .eh_target_reset_handler = lpfc_target_reset_handler,
James Smart3de2a652007-08-02 11:09:59 -04005584 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5585 .slave_alloc = lpfc_slave_alloc,
5586 .slave_configure = lpfc_slave_configure,
5587 .slave_destroy = lpfc_slave_destroy,
5588 .scan_finished = lpfc_scan_finished,
5589 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05005590 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04005591 .cmd_per_lun = LPFC_CMD_PER_LUN,
5592 .use_clustering = ENABLE_CLUSTERING,
5593 .shost_attrs = lpfc_vport_attrs,
5594 .max_sectors = 0xFFFF,
James Smart5ffc2662009-11-18 15:39:44 -05005595 .change_queue_depth = lpfc_change_queue_depth,
James Smartfe8f7f92013-01-03 15:43:03 -05005596 .change_queue_type = lpfc_change_queue_type,
James Smart3de2a652007-08-02 11:09:59 -04005597};