PageRenderTime 84ms CodeModel.GetById 24ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/scsi/lpfc/lpfc_scsi.c

https://bitbucket.org/abioy/linux
C | 3658 lines | 2408 code | 346 blank | 904 comment | 397 complexity | d91239933dccdd38e2ea663db82a2018 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

   1/*******************************************************************
   2 * This file is part of the Emulex Linux Device Driver for         *
   3 * Fibre Channel Host Bus Adapters.                                *
   4 * Copyright (C) 2004-2009 Emulex.  All rights reserved.           *
   5 * EMULEX and SLI are trademarks of Emulex.                        *
   6 * www.emulex.com                                                  *
   7 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
   8 *                                                                 *
   9 * This program is free software; you can redistribute it and/or   *
  10 * 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.                                     *
  20 *******************************************************************/
  21#include <linux/pci.h>
  22#include <linux/slab.h>
  23#include <linux/interrupt.h>
  24#include <linux/delay.h>
  25#include <asm/unaligned.h>
  26
  27#include <scsi/scsi.h>
  28#include <scsi/scsi_device.h>
  29#include <scsi/scsi_eh.h>
  30#include <scsi/scsi_host.h>
  31#include <scsi/scsi_tcq.h>
  32#include <scsi/scsi_transport_fc.h>
  33
  34#include "lpfc_version.h"
  35#include "lpfc_hw4.h"
  36#include "lpfc_hw.h"
  37#include "lpfc_sli.h"
  38#include "lpfc_sli4.h"
  39#include "lpfc_nl.h"
  40#include "lpfc_disc.h"
  41#include "lpfc_scsi.h"
  42#include "lpfc.h"
  43#include "lpfc_logmsg.h"
  44#include "lpfc_crtn.h"
  45#include "lpfc_vport.h"
  46
  47#define LPFC_RESET_WAIT  2
  48#define LPFC_ABORT_WAIT  2
  49
  50int _dump_buf_done;
  51
  52static char *dif_op_str[] = {
  53	"SCSI_PROT_NORMAL",
  54	"SCSI_PROT_READ_INSERT",
  55	"SCSI_PROT_WRITE_STRIP",
  56	"SCSI_PROT_READ_STRIP",
  57	"SCSI_PROT_WRITE_INSERT",
  58	"SCSI_PROT_READ_PASS",
  59	"SCSI_PROT_WRITE_PASS",
  60};
  61static void
  62lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
  63static void
  64lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
  65
  66static void
  67lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
  68{
  69	void *src, *dst;
  70	struct scatterlist *sgde = scsi_sglist(cmnd);
  71
  72	if (!_dump_buf_data) {
  73		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  74			"9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
  75				__func__);
  76		return;
  77	}
  78
  79
  80	if (!sgde) {
  81		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  82			"9051 BLKGRD: ERROR: data scatterlist is null\n");
  83		return;
  84	}
  85
  86	dst = (void *) _dump_buf_data;
  87	while (sgde) {
  88		src = sg_virt(sgde);
  89		memcpy(dst, src, sgde->length);
  90		dst += sgde->length;
  91		sgde = sg_next(sgde);
  92	}
  93}
  94
  95static void
  96lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
  97{
  98	void *src, *dst;
  99	struct scatterlist *sgde = scsi_prot_sglist(cmnd);
 100
 101	if (!_dump_buf_dif) {
 102		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
 103			"9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
 104				__func__);
 105		return;
 106	}
 107
 108	if (!sgde) {
 109		lpfc_printf_log(phba, KERN_ERR, LOG_BG,
 110			"9053 BLKGRD: ERROR: prot scatterlist is null\n");
 111		return;
 112	}
 113
 114	dst = _dump_buf_dif;
 115	while (sgde) {
 116		src = sg_virt(sgde);
 117		memcpy(dst, src, sgde->length);
 118		dst += sgde->length;
 119		sgde = sg_next(sgde);
 120	}
 121}
 122
 123/**
 124 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
 125 * @phba: Pointer to HBA object.
 126 * @lpfc_cmd: lpfc scsi command object pointer.
 127 *
 128 * This function is called from the lpfc_prep_task_mgmt_cmd function to
 129 * set the last bit in the response sge entry.
 130 **/
 131static void
 132lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
 133				struct lpfc_scsi_buf *lpfc_cmd)
 134{
 135	struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
 136	if (sgl) {
 137		sgl += 1;
 138		sgl->word2 = le32_to_cpu(sgl->word2);
 139		bf_set(lpfc_sli4_sge_last, sgl, 1);
 140		sgl->word2 = cpu_to_le32(sgl->word2);
 141	}
 142}
 143
 144/**
 145 * lpfc_update_stats - Update statistical data for the command completion
 146 * @phba: Pointer to HBA object.
 147 * @lpfc_cmd: lpfc scsi command object pointer.
 148 *
 149 * This function is called when there is a command completion and this
 150 * function updates the statistical data for the command completion.
 151 **/
 152static void
 153lpfc_update_stats(struct lpfc_hba *phba, struct  lpfc_scsi_buf *lpfc_cmd)
 154{
 155	struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
 156	struct lpfc_nodelist *pnode = rdata->pnode;
 157	struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
 158	unsigned long flags;
 159	struct Scsi_Host  *shost = cmd->device->host;
 160	struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
 161	unsigned long latency;
 162	int i;
 163
 164	if (cmd->result)
 165		return;
 166
 167	latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
 168
 169	spin_lock_irqsave(shost->host_lock, flags);
 170	if (!vport->stat_data_enabled ||
 171		vport->stat_data_blocked ||
 172		!pnode->lat_data ||
 173		(phba->bucket_type == LPFC_NO_BUCKET)) {
 174		spin_unlock_irqrestore(shost->host_lock, flags);
 175		return;
 176	}
 177
 178	if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
 179		i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
 180			phba->bucket_step;
 181		/* check array subscript bounds */
 182		if (i < 0)
 183			i = 0;
 184		else if (i >= LPFC_MAX_BUCKET_COUNT)
 185			i = LPFC_MAX_BUCKET_COUNT - 1;
 186	} else {
 187		for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
 188			if (latency <= (phba->bucket_base +
 189				((1<<i)*phba->bucket_step)))
 190				break;
 191	}
 192
 193	pnode->lat_data[i].cmd_count++;
 194	spin_unlock_irqrestore(shost->host_lock, flags);
 195}
 196
 197/**
 198 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
 199 * @phba: Pointer to HBA context object.
 200 * @vport: Pointer to vport object.
 201 * @ndlp: Pointer to FC node associated with the target.
 202 * @lun: Lun number of the scsi device.
 203 * @old_val: Old value of the queue depth.
 204 * @new_val: New value of the queue depth.
 205 *
 206 * This function sends an event to the mgmt application indicating
 207 * there is a change in the scsi device queue depth.
 208 **/
 209static void
 210lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
 211		struct lpfc_vport  *vport,
 212		struct lpfc_nodelist *ndlp,
 213		uint32_t lun,
 214		uint32_t old_val,
 215		uint32_t new_val)
 216{
 217	struct lpfc_fast_path_event *fast_path_evt;
 218	unsigned long flags;
 219
 220	fast_path_evt = lpfc_alloc_fast_evt(phba);
 221	if (!fast_path_evt)
 222		return;
 223
 224	fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
 225		FC_REG_SCSI_EVENT;
 226	fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
 227		LPFC_EVENT_VARQUEDEPTH;
 228
 229	/* Report all luns with change in queue depth */
 230	fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
 231	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
 232		memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
 233			&ndlp->nlp_portname, sizeof(struct lpfc_name));
 234		memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
 235			&ndlp->nlp_nodename, sizeof(struct lpfc_name));
 236	}
 237
 238	fast_path_evt->un.queue_depth_evt.oldval = old_val;
 239	fast_path_evt->un.queue_depth_evt.newval = new_val;
 240	fast_path_evt->vport = vport;
 241
 242	fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
 243	spin_lock_irqsave(&phba->hbalock, flags);
 244	list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
 245	spin_unlock_irqrestore(&phba->hbalock, flags);
 246	lpfc_worker_wake_up(phba);
 247
 248	return;
 249}
 250
 251/**
 252 * lpfc_change_queue_depth - Alter scsi device queue depth
 253 * @sdev: Pointer the scsi device on which to change the queue depth.
 254 * @qdepth: New queue depth to set the sdev to.
 255 * @reason: The reason for the queue depth change.
 256 *
 257 * This function is called by the midlayer and the LLD to alter the queue
 258 * depth for a scsi device. This function sets the queue depth to the new
 259 * value and sends an event out to log the queue depth change.
 260 **/
 261int
 262lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
 263{
 264	struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
 265	struct lpfc_hba   *phba = vport->phba;
 266	struct lpfc_rport_data *rdata;
 267	unsigned long new_queue_depth, old_queue_depth;
 268
 269	old_queue_depth = sdev->queue_depth;
 270	scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
 271	new_queue_depth = sdev->queue_depth;
 272	rdata = sdev->hostdata;
 273	if (rdata)
 274		lpfc_send_sdev_queuedepth_change_event(phba, vport,
 275						       rdata->pnode, sdev->lun,
 276						       old_queue_depth,
 277						       new_queue_depth);
 278	return sdev->queue_depth;
 279}
 280
 281/**
 282 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
 283 * @phba: The Hba for which this call is being executed.
 284 *
 285 * This routine is called when there is resource error in driver or firmware.
 286 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
 287 * posts at most 1 event each second. This routine wakes up worker thread of
 288 * @phba to process WORKER_RAM_DOWN_EVENT event.
 289 *
 290 * This routine should be called with no lock held.
 291 **/
 292void
 293lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
 294{
 295	unsigned long flags;
 296	uint32_t evt_posted;
 297
 298	spin_lock_irqsave(&phba->hbalock, flags);
 299	atomic_inc(&phba->num_rsrc_err);
 300	phba->last_rsrc_error_time = jiffies;
 301
 302	if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
 303		spin_unlock_irqrestore(&phba->hbalock, flags);
 304		return;
 305	}
 306
 307	phba->last_ramp_down_time = jiffies;
 308
 309	spin_unlock_irqrestore(&phba->hbalock, flags);
 310
 311	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
 312	evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
 313	if (!evt_posted)
 314		phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
 315	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
 316
 317	if (!evt_posted)
 318		lpfc_worker_wake_up(phba);
 319	return;
 320}
 321
 322/**
 323 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
 324 * @phba: The Hba for which this call is being executed.
 325 *
 326 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
 327 * post at most 1 event every 5 minute after last_ramp_up_time or
 328 * last_rsrc_error_time.  This routine wakes up worker thread of @phba
 329 * to process WORKER_RAM_DOWN_EVENT event.
 330 *
 331 * This routine should be called with no lock held.
 332 **/
 333static inline void
 334lpfc_rampup_queue_depth(struct lpfc_vport  *vport,
 335			uint32_t queue_depth)
 336{
 337	unsigned long flags;
 338	struct lpfc_hba *phba = vport->phba;
 339	uint32_t evt_posted;
 340	atomic_inc(&phba->num_cmd_success);
 341
 342	if (vport->cfg_lun_queue_depth <= queue_depth)
 343		return;
 344	spin_lock_irqsave(&phba->hbalock, flags);
 345	if (time_before(jiffies,
 346			phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
 347	    time_before(jiffies,
 348			phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
 349		spin_unlock_irqrestore(&phba->hbalock, flags);
 350		return;
 351	}
 352	phba->last_ramp_up_time = jiffies;
 353	spin_unlock_irqrestore(&phba->hbalock, flags);
 354
 355	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
 356	evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
 357	if (!evt_posted)
 358		phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
 359	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
 360
 361	if (!evt_posted)
 362		lpfc_worker_wake_up(phba);
 363	return;
 364}
 365
 366/**
 367 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
 368 * @phba: The Hba for which this call is being executed.
 369 *
 370 * This routine is called to  process WORKER_RAMP_DOWN_QUEUE event for worker
 371 * thread.This routine reduces queue depth for all scsi device on each vport
 372 * associated with @phba.
 373 **/
 374void
 375lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
 376{
 377	struct lpfc_vport **vports;
 378	struct Scsi_Host  *shost;
 379	struct scsi_device *sdev;
 380	unsigned long new_queue_depth;
 381	unsigned long num_rsrc_err, num_cmd_success;
 382	int i;
 383
 384	num_rsrc_err = atomic_read(&phba->num_rsrc_err);
 385	num_cmd_success = atomic_read(&phba->num_cmd_success);
 386
 387	vports = lpfc_create_vport_work_array(phba);
 388	if (vports != NULL)
 389		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
 390			shost = lpfc_shost_from_vport(vports[i]);
 391			shost_for_each_device(sdev, shost) {
 392				new_queue_depth =
 393					sdev->queue_depth * num_rsrc_err /
 394					(num_rsrc_err + num_cmd_success);
 395				if (!new_queue_depth)
 396					new_queue_depth = sdev->queue_depth - 1;
 397				else
 398					new_queue_depth = sdev->queue_depth -
 399								new_queue_depth;
 400				lpfc_change_queue_depth(sdev, new_queue_depth,
 401							SCSI_QDEPTH_DEFAULT);
 402			}
 403		}
 404	lpfc_destroy_vport_work_array(phba, vports);
 405	atomic_set(&phba->num_rsrc_err, 0);
 406	atomic_set(&phba->num_cmd_success, 0);
 407}
 408
 409/**
 410 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
 411 * @phba: The Hba for which this call is being executed.
 412 *
 413 * This routine is called to  process WORKER_RAMP_UP_QUEUE event for worker
 414 * thread.This routine increases queue depth for all scsi device on each vport
 415 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
 416 * num_cmd_success to zero.
 417 **/
 418void
 419lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
 420{
 421	struct lpfc_vport **vports;
 422	struct Scsi_Host  *shost;
 423	struct scsi_device *sdev;
 424	int i;
 425
 426	vports = lpfc_create_vport_work_array(phba);
 427	if (vports != NULL)
 428		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
 429			shost = lpfc_shost_from_vport(vports[i]);
 430			shost_for_each_device(sdev, shost) {
 431				if (vports[i]->cfg_lun_queue_depth <=
 432				    sdev->queue_depth)
 433					continue;
 434				lpfc_change_queue_depth(sdev,
 435							sdev->queue_depth+1,
 436							SCSI_QDEPTH_RAMP_UP);
 437			}
 438		}
 439	lpfc_destroy_vport_work_array(phba, vports);
 440	atomic_set(&phba->num_rsrc_err, 0);
 441	atomic_set(&phba->num_cmd_success, 0);
 442}
 443
 444/**
 445 * lpfc_scsi_dev_block - set all scsi hosts to block state
 446 * @phba: Pointer to HBA context object.
 447 *
 448 * This function walks vport list and set each SCSI host to block state
 449 * by invoking fc_remote_port_delete() routine. This function is invoked
 450 * with EEH when device's PCI slot has been permanently disabled.
 451 **/
 452void
 453lpfc_scsi_dev_block(struct lpfc_hba *phba)
 454{
 455	struct lpfc_vport **vports;
 456	struct Scsi_Host  *shost;
 457	struct scsi_device *sdev;
 458	struct fc_rport *rport;
 459	int i;
 460
 461	vports = lpfc_create_vport_work_array(phba);
 462	if (vports != NULL)
 463		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
 464			shost = lpfc_shost_from_vport(vports[i]);
 465			shost_for_each_device(sdev, shost) {
 466				rport = starget_to_rport(scsi_target(sdev));
 467				fc_remote_port_delete(rport);
 468			}
 469		}
 470	lpfc_destroy_vport_work_array(phba, vports);
 471}
 472
 473/**
 474 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
 475 * @vport: The virtual port for which this call being executed.
 476 * @num_to_allocate: The requested number of buffers to allocate.
 477 *
 478 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
 479 * the scsi buffer contains all the necessary information needed to initiate
 480 * a SCSI I/O. The non-DMAable buffer region contains information to build
 481 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
 482 * and the initial BPL. In addition to allocating memory, the FCP CMND and
 483 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
 484 *
 485 * Return codes:
 486 *   int - number of scsi buffers that were allocated.
 487 *   0 = failure, less than num_to_alloc is a partial failure.
 488 **/
 489static int
 490lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
 491{
 492	struct lpfc_hba *phba = vport->phba;
 493	struct lpfc_scsi_buf *psb;
 494	struct ulp_bde64 *bpl;
 495	IOCB_t *iocb;
 496	dma_addr_t pdma_phys_fcp_cmd;
 497	dma_addr_t pdma_phys_fcp_rsp;
 498	dma_addr_t pdma_phys_bpl;
 499	uint16_t iotag;
 500	int bcnt;
 501
 502	for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
 503		psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
 504		if (!psb)
 505			break;
 506
 507		/*
 508		 * Get memory from the pci pool to map the virt space to pci
 509		 * bus space for an I/O.  The DMA buffer includes space for the
 510		 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
 511		 * necessary to support the sg_tablesize.
 512		 */
 513		psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
 514					GFP_KERNEL, &psb->dma_handle);
 515		if (!psb->data) {
 516			kfree(psb);
 517			break;
 518		}
 519
 520		/* Initialize virtual ptrs to dma_buf region. */
 521		memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
 522
 523		/* Allocate iotag for psb->cur_iocbq. */
 524		iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
 525		if (iotag == 0) {
 526			pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
 527					psb->data, psb->dma_handle);
 528			kfree(psb);
 529			break;
 530		}
 531		psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
 532
 533		psb->fcp_cmnd = psb->data;
 534		psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
 535		psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
 536			sizeof(struct fcp_rsp);
 537
 538		/* Initialize local short-hand pointers. */
 539		bpl = psb->fcp_bpl;
 540		pdma_phys_fcp_cmd = psb->dma_handle;
 541		pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
 542		pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
 543			sizeof(struct fcp_rsp);
 544
 545		/*
 546		 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
 547		 * are sg list bdes.  Initialize the first two and leave the
 548		 * rest for queuecommand.
 549		 */
 550		bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
 551		bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
 552		bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
 553		bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
 554		bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
 555
 556		/* Setup the physical region for the FCP RSP */
 557		bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
 558		bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
 559		bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
 560		bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
 561		bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
 562
 563		/*
 564		 * Since the IOCB for the FCP I/O is built into this
 565		 * lpfc_scsi_buf, initialize it with all known data now.
 566		 */
 567		iocb = &psb->cur_iocbq.iocb;
 568		iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
 569		if ((phba->sli_rev == 3) &&
 570				!(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
 571			/* fill in immediate fcp command BDE */
 572			iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
 573			iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
 574			iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
 575					unsli3.fcp_ext.icd);
 576			iocb->un.fcpi64.bdl.addrHigh = 0;
 577			iocb->ulpBdeCount = 0;
 578			iocb->ulpLe = 0;
 579			/* fill in responce BDE */
 580			iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
 581							BUFF_TYPE_BDE_64;
 582			iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
 583				sizeof(struct fcp_rsp);
 584			iocb->unsli3.fcp_ext.rbde.addrLow =
 585				putPaddrLow(pdma_phys_fcp_rsp);
 586			iocb->unsli3.fcp_ext.rbde.addrHigh =
 587				putPaddrHigh(pdma_phys_fcp_rsp);
 588		} else {
 589			iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
 590			iocb->un.fcpi64.bdl.bdeSize =
 591					(2 * sizeof(struct ulp_bde64));
 592			iocb->un.fcpi64.bdl.addrLow =
 593					putPaddrLow(pdma_phys_bpl);
 594			iocb->un.fcpi64.bdl.addrHigh =
 595					putPaddrHigh(pdma_phys_bpl);
 596			iocb->ulpBdeCount = 1;
 597			iocb->ulpLe = 1;
 598		}
 599		iocb->ulpClass = CLASS3;
 600		psb->status = IOSTAT_SUCCESS;
 601		/* Put it back into the SCSI buffer list */
 602		lpfc_release_scsi_buf_s3(phba, psb);
 603
 604	}
 605
 606	return bcnt;
 607}
 608
 609/**
 610 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
 611 * @phba: pointer to lpfc hba data structure.
 612 * @axri: pointer to the fcp xri abort wcqe structure.
 613 *
 614 * This routine is invoked by the worker thread to process a SLI4 fast-path
 615 * FCP aborted xri.
 616 **/
 617void
 618lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
 619			  struct sli4_wcqe_xri_aborted *axri)
 620{
 621	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
 622	struct lpfc_scsi_buf *psb, *next_psb;
 623	unsigned long iflag = 0;
 624	struct lpfc_iocbq *iocbq;
 625	int i;
 626
 627	spin_lock_irqsave(&phba->hbalock, iflag);
 628	spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
 629	list_for_each_entry_safe(psb, next_psb,
 630		&phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
 631		if (psb->cur_iocbq.sli4_xritag == xri) {
 632			list_del(&psb->list);
 633			psb->exch_busy = 0;
 634			psb->status = IOSTAT_SUCCESS;
 635			spin_unlock(
 636				&phba->sli4_hba.abts_scsi_buf_list_lock);
 637			spin_unlock_irqrestore(&phba->hbalock, iflag);
 638			lpfc_release_scsi_buf_s4(phba, psb);
 639			return;
 640		}
 641	}
 642	spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
 643	for (i = 1; i <= phba->sli.last_iotag; i++) {
 644		iocbq = phba->sli.iocbq_lookup[i];
 645
 646		if (!(iocbq->iocb_flag &  LPFC_IO_FCP) ||
 647			(iocbq->iocb_flag & LPFC_IO_LIBDFC))
 648			continue;
 649		if (iocbq->sli4_xritag != xri)
 650			continue;
 651		psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
 652		psb->exch_busy = 0;
 653		spin_unlock_irqrestore(&phba->hbalock, iflag);
 654		return;
 655
 656	}
 657	spin_unlock_irqrestore(&phba->hbalock, iflag);
 658}
 659
 660/**
 661 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
 662 * @phba: pointer to lpfc hba data structure.
 663 *
 664 * This routine walks the list of scsi buffers that have been allocated and
 665 * repost them to the HBA by using SGL block post. This is needed after a
 666 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
 667 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
 668 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
 669 *
 670 * Returns: 0 = success, non-zero failure.
 671 **/
 672int
 673lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
 674{
 675	struct lpfc_scsi_buf *psb;
 676	int index, status, bcnt = 0, rcnt = 0, rc = 0;
 677	LIST_HEAD(sblist);
 678
 679	for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
 680		psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
 681		if (psb) {
 682			/* Remove from SCSI buffer list */
 683			list_del(&psb->list);
 684			/* Add it to a local SCSI buffer list */
 685			list_add_tail(&psb->list, &sblist);
 686			if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
 687				bcnt = rcnt;
 688				rcnt = 0;
 689			}
 690		} else
 691			/* A hole present in the XRI array, need to skip */
 692			bcnt = rcnt;
 693
 694		if (index == phba->sli4_hba.scsi_xri_cnt - 1)
 695			/* End of XRI array for SCSI buffer, complete */
 696			bcnt = rcnt;
 697
 698		/* Continue until collect up to a nembed page worth of sgls */
 699		if (bcnt == 0)
 700			continue;
 701		/* Now, post the SCSI buffer list sgls as a block */
 702		status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
 703		/* Reset SCSI buffer count for next round of posting */
 704		bcnt = 0;
 705		while (!list_empty(&sblist)) {
 706			list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
 707					 list);
 708			if (status) {
 709				/* Put this back on the abort scsi list */
 710				psb->exch_busy = 1;
 711				rc++;
 712			} else {
 713				psb->exch_busy = 0;
 714				psb->status = IOSTAT_SUCCESS;
 715			}
 716			/* Put it back into the SCSI buffer list */
 717			lpfc_release_scsi_buf_s4(phba, psb);
 718		}
 719	}
 720	return rc;
 721}
 722
 723/**
 724 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
 725 * @vport: The virtual port for which this call being executed.
 726 * @num_to_allocate: The requested number of buffers to allocate.
 727 *
 728 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
 729 * the scsi buffer contains all the necessary information needed to initiate
 730 * a SCSI I/O.
 731 *
 732 * Return codes:
 733 *   int - number of scsi buffers that were allocated.
 734 *   0 = failure, less than num_to_alloc is a partial failure.
 735 **/
 736static int
 737lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
 738{
 739	struct lpfc_hba *phba = vport->phba;
 740	struct lpfc_scsi_buf *psb;
 741	struct sli4_sge *sgl;
 742	IOCB_t *iocb;
 743	dma_addr_t pdma_phys_fcp_cmd;
 744	dma_addr_t pdma_phys_fcp_rsp;
 745	dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
 746	uint16_t iotag, last_xritag = NO_XRI;
 747	int status = 0, index;
 748	int bcnt;
 749	int non_sequential_xri = 0;
 750	int rc = 0;
 751	LIST_HEAD(sblist);
 752
 753	for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
 754		psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
 755		if (!psb)
 756			break;
 757
 758		/*
 759		 * Get memory from the pci pool to map the virt space to pci bus
 760		 * space for an I/O.  The DMA buffer includes space for the
 761		 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
 762		 * necessary to support the sg_tablesize.
 763		 */
 764		psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
 765						GFP_KERNEL, &psb->dma_handle);
 766		if (!psb->data) {
 767			kfree(psb);
 768			break;
 769		}
 770
 771		/* Initialize virtual ptrs to dma_buf region. */
 772		memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
 773
 774		/* Allocate iotag for psb->cur_iocbq. */
 775		iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
 776		if (iotag == 0) {
 777			kfree(psb);
 778			break;
 779		}
 780
 781		psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
 782		if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
 783			pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
 784			      psb->data, psb->dma_handle);
 785			kfree(psb);
 786			break;
 787		}
 788		if (last_xritag != NO_XRI
 789			&& psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
 790			non_sequential_xri = 1;
 791		} else
 792			list_add_tail(&psb->list, &sblist);
 793		last_xritag = psb->cur_iocbq.sli4_xritag;
 794
 795		index = phba->sli4_hba.scsi_xri_cnt++;
 796		psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
 797
 798		psb->fcp_bpl = psb->data;
 799		psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
 800			- (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
 801		psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
 802					sizeof(struct fcp_cmnd));
 803
 804		/* Initialize local short-hand pointers. */
 805		sgl = (struct sli4_sge *)psb->fcp_bpl;
 806		pdma_phys_bpl = psb->dma_handle;
 807		pdma_phys_fcp_cmd =
 808			(psb->dma_handle + phba->cfg_sg_dma_buf_size)
 809			 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
 810		pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
 811
 812		/*
 813		 * The first two bdes are the FCP_CMD and FCP_RSP.  The balance
 814		 * are sg list bdes.  Initialize the first two and leave the
 815		 * rest for queuecommand.
 816		 */
 817		sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
 818		sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
 819		bf_set(lpfc_sli4_sge_last, sgl, 0);
 820		sgl->word2 = cpu_to_le32(sgl->word2);
 821		sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
 822		sgl++;
 823
 824		/* Setup the physical region for the FCP RSP */
 825		sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
 826		sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
 827		bf_set(lpfc_sli4_sge_last, sgl, 1);
 828		sgl->word2 = cpu_to_le32(sgl->word2);
 829		sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
 830
 831		/*
 832		 * Since the IOCB for the FCP I/O is built into this
 833		 * lpfc_scsi_buf, initialize it with all known data now.
 834		 */
 835		iocb = &psb->cur_iocbq.iocb;
 836		iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
 837		iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
 838		/* setting the BLP size to 2 * sizeof BDE may not be correct.
 839		 * We are setting the bpl to point to out sgl. An sgl's
 840		 * entries are 16 bytes, a bpl entries are 12 bytes.
 841		 */
 842		iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
 843		iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
 844		iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
 845		iocb->ulpBdeCount = 1;
 846		iocb->ulpLe = 1;
 847		iocb->ulpClass = CLASS3;
 848		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
 849			pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
 850		else
 851			pdma_phys_bpl1 = 0;
 852		psb->dma_phys_bpl = pdma_phys_bpl;
 853		phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
 854		if (non_sequential_xri) {
 855			status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
 856						pdma_phys_bpl1,
 857						psb->cur_iocbq.sli4_xritag);
 858			if (status) {
 859				/* Put this back on the abort scsi list */
 860				psb->exch_busy = 1;
 861				rc++;
 862			} else {
 863				psb->exch_busy = 0;
 864				psb->status = IOSTAT_SUCCESS;
 865			}
 866			/* Put it back into the SCSI buffer list */
 867			lpfc_release_scsi_buf_s4(phba, psb);
 868			break;
 869		}
 870	}
 871	if (bcnt) {
 872		status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
 873		/* Reset SCSI buffer count for next round of posting */
 874		while (!list_empty(&sblist)) {
 875			list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
 876				 list);
 877			if (status) {
 878				/* Put this back on the abort scsi list */
 879				psb->exch_busy = 1;
 880				rc++;
 881			} else {
 882				psb->exch_busy = 0;
 883				psb->status = IOSTAT_SUCCESS;
 884			}
 885			/* Put it back into the SCSI buffer list */
 886			lpfc_release_scsi_buf_s4(phba, psb);
 887		}
 888	}
 889
 890	return bcnt + non_sequential_xri - rc;
 891}
 892
 893/**
 894 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
 895 * @vport: The virtual port for which this call being executed.
 896 * @num_to_allocate: The requested number of buffers to allocate.
 897 *
 898 * This routine wraps the actual SCSI buffer allocator function pointer from
 899 * the lpfc_hba struct.
 900 *
 901 * Return codes:
 902 *   int - number of scsi buffers that were allocated.
 903 *   0 = failure, less than num_to_alloc is a partial failure.
 904 **/
 905static inline int
 906lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
 907{
 908	return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
 909}
 910
 911/**
 912 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
 913 * @phba: The HBA for which this call is being executed.
 914 *
 915 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
 916 * and returns to caller.
 917 *
 918 * Return codes:
 919 *   NULL - Error
 920 *   Pointer to lpfc_scsi_buf - Success
 921 **/
 922static struct lpfc_scsi_buf*
 923lpfc_get_scsi_buf(struct lpfc_hba * phba)
 924{
 925	struct  lpfc_scsi_buf * lpfc_cmd = NULL;
 926	struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
 927	unsigned long iflag = 0;
 928
 929	spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
 930	list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
 931	if (lpfc_cmd) {
 932		lpfc_cmd->seg_cnt = 0;
 933		lpfc_cmd->nonsg_phys = 0;
 934		lpfc_cmd->prot_seg_cnt = 0;
 935	}
 936	spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
 937	return  lpfc_cmd;
 938}
 939
 940/**
 941 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
 942 * @phba: The Hba for which this call is being executed.
 943 * @psb: The scsi buffer which is being released.
 944 *
 945 * This routine releases @psb scsi buffer by adding it to tail of @phba
 946 * lpfc_scsi_buf_list list.
 947 **/
 948static void
 949lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
 950{
 951	unsigned long iflag = 0;
 952
 953	spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
 954	psb->pCmd = NULL;
 955	list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
 956	spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
 957}
 958
 959/**
 960 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
 961 * @phba: The Hba for which this call is being executed.
 962 * @psb: The scsi buffer which is being released.
 963 *
 964 * This routine releases @psb scsi buffer by adding it to tail of @phba
 965 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
 966 * and cannot be reused for at least RA_TOV amount of time if it was
 967 * aborted.
 968 **/
 969static void
 970lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
 971{
 972	unsigned long iflag = 0;
 973
 974	if (psb->exch_busy) {
 975		spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
 976					iflag);
 977		psb->pCmd = NULL;
 978		list_add_tail(&psb->list,
 979			&phba->sli4_hba.lpfc_abts_scsi_buf_list);
 980		spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
 981					iflag);
 982	} else {
 983
 984		spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
 985		psb->pCmd = NULL;
 986		list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
 987		spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
 988	}
 989}
 990
 991/**
 992 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
 993 * @phba: The Hba for which this call is being executed.
 994 * @psb: The scsi buffer which is being released.
 995 *
 996 * This routine releases @psb scsi buffer by adding it to tail of @phba
 997 * lpfc_scsi_buf_list list.
 998 **/
 999static void
1000lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1001{
1002
1003	phba->lpfc_release_scsi_buf(phba, psb);
1004}
1005
1006/**
1007 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
1008 * @phba: The Hba for which this call is being executed.
1009 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1010 *
1011 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1012 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1013 * through sg elements and format the bdea. This routine also initializes all
1014 * IOCB fields which are dependent on scsi command request buffer.
1015 *
1016 * Return codes:
1017 *   1 - Error
1018 *   0 - Success
1019 **/
1020static int
1021lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1022{
1023	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1024	struct scatterlist *sgel = NULL;
1025	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1026	struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1027	struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
1028	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1029	struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
1030	dma_addr_t physaddr;
1031	uint32_t num_bde = 0;
1032	int nseg, datadir = scsi_cmnd->sc_data_direction;
1033
1034	/*
1035	 * There are three possibilities here - use scatter-gather segment, use
1036	 * the single mapping, or neither.  Start the lpfc command prep by
1037	 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1038	 * data bde entry.
1039	 */
1040	bpl += 2;
1041	if (scsi_sg_count(scsi_cmnd)) {
1042		/*
1043		 * The driver stores the segment count returned from pci_map_sg
1044		 * because this a count of dma-mappings used to map the use_sg
1045		 * pages.  They are not guaranteed to be the same for those
1046		 * architectures that implement an IOMMU.
1047		 */
1048
1049		nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1050				  scsi_sg_count(scsi_cmnd), datadir);
1051		if (unlikely(!nseg))
1052			return 1;
1053
1054		lpfc_cmd->seg_cnt = nseg;
1055		if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1056			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1057				"9064 BLKGRD: %s: Too many sg segments from "
1058			       "dma_map_sg.  Config %d, seg_cnt %d\n",
1059			       __func__, phba->cfg_sg_seg_cnt,
1060			       lpfc_cmd->seg_cnt);
1061			scsi_dma_unmap(scsi_cmnd);
1062			return 1;
1063		}
1064
1065		/*
1066		 * The driver established a maximum scatter-gather segment count
1067		 * during probe that limits the number of sg elements in any
1068		 * single scsi command.  Just run through the seg_cnt and format
1069		 * the bde's.
1070		 * When using SLI-3 the driver will try to fit all the BDEs into
1071		 * the IOCB. If it can't then the BDEs get added to a BPL as it
1072		 * does for SLI-2 mode.
1073		 */
1074		scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1075			physaddr = sg_dma_address(sgel);
1076			if (phba->sli_rev == 3 &&
1077			    !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1078			    !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
1079			    nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1080				data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1081				data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1082				data_bde->addrLow = putPaddrLow(physaddr);
1083				data_bde->addrHigh = putPaddrHigh(physaddr);
1084				data_bde++;
1085			} else {
1086				bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1087				bpl->tus.f.bdeSize = sg_dma_len(sgel);
1088				bpl->tus.w = le32_to_cpu(bpl->tus.w);
1089				bpl->addrLow =
1090					le32_to_cpu(putPaddrLow(physaddr));
1091				bpl->addrHigh =
1092					le32_to_cpu(putPaddrHigh(physaddr));
1093				bpl++;
1094			}
1095		}
1096	}
1097
1098	/*
1099	 * Finish initializing those IOCB fields that are dependent on the
1100	 * scsi_cmnd request_buffer.  Note that for SLI-2 the bdeSize is
1101	 * explicitly reinitialized and for SLI-3 the extended bde count is
1102	 * explicitly reinitialized since all iocb memory resources are reused.
1103	 */
1104	if (phba->sli_rev == 3 &&
1105	    !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1106	    !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
1107		if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1108			/*
1109			 * The extended IOCB format can only fit 3 BDE or a BPL.
1110			 * This I/O has more than 3 BDE so the 1st data bde will
1111			 * be a BPL that is filled in here.
1112			 */
1113			physaddr = lpfc_cmd->dma_handle;
1114			data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1115			data_bde->tus.f.bdeSize = (num_bde *
1116						   sizeof(struct ulp_bde64));
1117			physaddr += (sizeof(struct fcp_cmnd) +
1118				     sizeof(struct fcp_rsp) +
1119				     (2 * sizeof(struct ulp_bde64)));
1120			data_bde->addrHigh = putPaddrHigh(physaddr);
1121			data_bde->addrLow = putPaddrLow(physaddr);
1122			/* ebde count includes the responce bde and data bpl */
1123			iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1124		} else {
1125			/* ebde count includes the responce bde and data bdes */
1126			iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1127		}
1128	} else {
1129		iocb_cmd->un.fcpi64.bdl.bdeSize =
1130			((num_bde + 2) * sizeof(struct ulp_bde64));
1131		iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1132	}
1133	fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1134
1135	/*
1136	 * Due to difference in data length between DIF/non-DIF paths,
1137	 * we need to set word 4 of IOCB here
1138	 */
1139	iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1140	return 0;
1141}
1142
1143/*
1144 * Given a scsi cmnd, determine the BlockGuard profile to be used
1145 * with the cmd
1146 */
1147static int
1148lpfc_sc_to_sli_prof(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1149{
1150	uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1151	uint8_t ret_prof = LPFC_PROF_INVALID;
1152
1153	if (guard_type == SHOST_DIX_GUARD_IP) {
1154		switch (scsi_get_prot_op(sc)) {
1155		case SCSI_PROT_READ_INSERT:
1156		case SCSI_PROT_WRITE_STRIP:
1157			ret_prof = LPFC_PROF_AST2;
1158			break;
1159
1160		case SCSI_PROT_READ_STRIP:
1161		case SCSI_PROT_WRITE_INSERT:
1162			ret_prof = LPFC_PROF_A1;
1163			break;
1164
1165		case SCSI_PROT_READ_PASS:
1166		case SCSI_PROT_WRITE_PASS:
1167			ret_prof = LPFC_PROF_AST1;
1168			break;
1169
1170		case SCSI_PROT_NORMAL:
1171		default:
1172			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1173				"9063 BLKGRD:Bad op/guard:%d/%d combination\n",
1174					scsi_get_prot_op(sc), guard_type);
1175			break;
1176
1177		}
1178	} else if (guard_type == SHOST_DIX_GUARD_CRC) {
1179		switch (scsi_get_prot_op(sc)) {
1180		case SCSI_PROT_READ_STRIP:
1181		case SCSI_PROT_WRITE_INSERT:
1182			ret_prof = LPFC_PROF_A1;
1183			break;
1184
1185		case SCSI_PROT_READ_PASS:
1186		case SCSI_PROT_WRITE_PASS:
1187			ret_prof = LPFC_PROF_C1;
1188			break;
1189
1190		case SCSI_PROT_READ_INSERT:
1191		case SCSI_PROT_WRITE_STRIP:
1192		case SCSI_PROT_NORMAL:
1193		default:
1194			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1195				"9075 BLKGRD: Bad op/guard:%d/%d combination\n",
1196					scsi_get_prot_op(sc), guard_type);
1197			break;
1198		}
1199	} else {
1200		/* unsupported format */
1201		BUG();
1202	}
1203
1204	return ret_prof;
1205}
1206
1207struct scsi_dif_tuple {
1208	__be16 guard_tag;       /* Checksum */
1209	__be16 app_tag;         /* Opaque storage */
1210	__be32 ref_tag;         /* Target LBA or indirect LBA */
1211};
1212
1213static inline unsigned
1214lpfc_cmd_blksize(struct scsi_cmnd *sc)
1215{
1216	return sc->device->sector_size;
1217}
1218
1219/**
1220 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1221 * @sc:             in: SCSI command
1222 * @apptagmask:     out: app tag mask
1223 * @apptagval:      out: app tag value
1224 * @reftag:         out: ref tag (reference tag)
1225 *
1226 * Description:
1227 *   Extract DIF parameters from the command if possible.  Otherwise,
1228 *   use default parameters.
1229 *
1230 **/
1231static inline void
1232lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1233		uint16_t *apptagval, uint32_t *reftag)
1234{
1235	struct  scsi_dif_tuple *spt;
1236	unsigned char op = scsi_get_prot_op(sc);
1237	unsigned int protcnt = scsi_prot_sg_count(sc);
1238	static int cnt;
1239
1240	if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
1241				op == SCSI_PROT_WRITE_PASS)) {
1242
1243		cnt++;
1244		spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1245			scsi_prot_sglist(sc)[0].offset;
1246		*apptagmask = 0;
1247		*apptagval = 0;
1248		*reftag = cpu_to_be32(spt->ref_tag);
1249
1250	} else {
1251		/* SBC defines ref tag to be lower 32bits of LBA */
1252		*reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1253		*apptagmask = 0;
1254		*apptagval = 0;
1255	}
1256}
1257
1258/*
1259 * This function sets up buffer list for protection groups of
1260 * type LPFC_PG_TYPE_NO_DIF
1261 *
1262 * This is usually used when the HBA is instructed to generate
1263 * DIFs and insert them into data stream (or strip DIF from
1264 * incoming data stream)
1265 *
1266 * The buffer list consists of just one protection group described
1267 * below:
1268 *                                +-------------------------+
1269 *   start of prot group  -->     |          PDE_1          |
1270 *                                +-------------------------+
1271 *                                |         Data BDE        |
1272 *                                +-------------------------+
1273 *                                |more Data BDE's ... (opt)|
1274 *                                +-------------------------+
1275 *
1276 * @sc: pointer to scsi command we're working on
1277 * @bpl: pointer to buffer list for protection groups
1278 * @datacnt: number of segments of data that have been dma mapped
1279 *
1280 * Note: Data s/g buffers have been dma mapped
1281 */
1282static int
1283lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1284		struct ulp_bde64 *bpl, int datasegcnt)
1285{
1286	struct scatterlist *sgde = NULL; /* s/g data entry */
1287	struct lpfc_pde *pde1 = NULL;
1288	dma_addr_t physaddr;
1289	int i = 0, num_bde = 0;
1290	int datadir = sc->sc_data_direction;
1291	int prof = LPFC_PROF_INVALID;
1292	unsigned blksize;
1293	uint32_t reftag;
1294	uint16_t apptagmask, apptagval;
1295
1296	pde1 = (struct lpfc_pde *) bpl;
1297	prof = lpfc_sc_to_sli_prof(phba, sc);
1298
1299	if (prof == LPFC_PROF_INVALID)
1300		goto out;
1301
1302	/* extract some info from the scsi command for PDE1*/
1303	blksize = lpfc_cmd_blksize(sc);
1304	lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1305
1306	/* setup PDE1 with what we have */
1307	lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1308			BG_EC_STOP_ERR);
1309	lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1310
1311	num_bde++;
1312	bpl++;
1313
1314	/* assumption: caller has already run dma_map_sg on command data */
1315	scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1316		physaddr = sg_dma_address(sgde);
1317		bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1318		bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1319		bpl->tus.f.bdeSize = sg_dma_len(sgde);
1320		if (datadir == DMA_TO_DEVICE)
1321			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1322		else
1323			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1324		bpl->tus.w = le32_to_cpu(bpl->tus.w);
1325		bpl++;
1326		num_bde++;
1327	}
1328
1329out:
1330	return num_bde;
1331}
1332
1333/*
1334 * This function sets up buffer list for protection groups of
1335 * type LPFC_PG_TYPE_DIF_BUF
1336 *
1337 * This is usually used when DIFs are in their own buffers,
1338 * separate from the data. The HBA can then by instructed
1339 * to place the DIFs in the outgoing stream.  For read operations,
1340 * The HBA could extract the DIFs and place it in DIF buffers.
1341 *
1342 * The buffer list for this type consists of one or more of the
1343 * protection groups described below:
1344 *                                    +-------------------------+
1345 *   start of first prot group  -->   |          PDE_1          |
1346 *                                    +-------------------------+
1347 *                                    |      PDE_3 (Prot BDE)   |
1348 *                                    +-------------------------+
1349 *                                    |        Data BDE         |
1350 *                                    +-------------------------+
1351 *                                    |more Data BDE's ... (opt)|
1352 *                                    +-------------------------+
1353 *   start of new  prot group  -->    |          PDE_1          |
1354 *                                    +-------------------------+
1355 *                                    |          ...            |
1356 *                                    +-------------------------+
1357 *
1358 * @sc: pointer to scsi command we're working on
1359 * @bpl: pointer to buffer list for protection groups
1360 * @datacnt: number of segments of data that have been dma mapped
1361 * @protcnt: number of segment of protection data that have been dma mapped
1362 *
1363 * Note: It is assumed that both data and protection s/g buffers have been
1364 *       mapped for DMA
1365 */
1366static int
1367lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1368		struct ulp_bde64 *bpl, int datacnt, int protcnt)
1369{
1370	struct scatterlist *sgde = NULL; /* s/g data entry */
1371	struct scatterlist *sgpe = NULL; /* s/g prot entry */
1372	struct lpfc_pde *pde1 = NULL;
1373	struct ulp_bde64 *prot_bde = NULL;
1374	dma_addr_t dataphysaddr, protphysaddr;
1375	unsigned short curr_data = 0, curr_prot = 0;
1376	unsigned int split_offset, protgroup_len;
1377	unsigned int protgrp_blks, protgrp_bytes;
1378	unsigned int remainder, subtotal;
1379	int prof = LPFC_PROF_INVALID;
1380	int datadir = sc->sc_data_direction;
1381	unsigned char pgdone = 0, alldone = 0;
1382	unsigned blksize;
1383	uint32_t reftag;
1384	uint16_t apptagmask, apptagval;
1385	int num_bde = 0;
1386
1387	sgpe = scsi_prot_sglist(sc);
1388	sgde = scsi_sglist(sc);
1389
1390	if (!sgpe || !sgde) {
1391		lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1392				"9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1393				sgpe, sgde);
1394		return 0;
1395	}
1396
1397	prof = lpfc_sc_to_sli_prof(phba, sc);
1398	if (prof == LPFC_PROF_INVALID)
1399		goto out;
1400
1401	/* extract some info from the scsi command for PDE1*/
1402	blksize = lpfc_cmd_blksize(sc);
1403	lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1404
1405	split_offset = 0;
1406	do {
1407		/* setup the first PDE_1 */
1408		pde1 = (struct lpfc_pde *) bpl;
1409
1410		lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1411				BG_EC_STOP_ERR);
1412		lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1413
1414		num_bde++;
1415		bpl++;
1416
1417		/* setup the first BDE that points to protection buffer */
1418		prot_bde = (struct ulp_bde64 *) bpl;
1419		protphysaddr = sg_dma_address(sgpe);
1420		prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1421		prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1422		protgroup_len = sg_dma_len(sgpe);
1423
1424
1425		/* must be integer multiple of the DIF block length */
1426		BUG_ON(protgroup_len % 8);
1427
1428		protgrp_blks = protgroup_len / 8;
1429		protgrp_bytes = protgrp_blks * blksize;
1430
1431		prot_bde->tus.f.bdeSize = protgroup_len;
1432		if (datadir == DMA_TO_DEVICE)
1433			prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1434		else
1435			prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1436		prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1437
1438		curr_prot++;
1439		num_bde++;
1440
1441		/* setup BDE's for data blocks associated with DIF data */
1442		pgdone = 0;
1443		subtotal = 0; /* total bytes processed for current prot grp */
1444		while (!pgdone) {
1445			if (!sgde) {
1446				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1447					"9065 BLKGRD:%s Invalid data segment\n",
1448						__func__);
1449				return 0;
1450			}
1451			bpl++;
1452			dataphysaddr = sg_dma_address(sgde) + split_offset;
1453			bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1454			bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1455
1456			remainder = sg_dma_len(sgde) - split_offset;
1457
1458			if ((subtotal + remainder) <= protgrp_bytes) {
1459				/* we can use this whole buffer */
1460				bpl->tus.f.bdeSize = remainder;
1461				split_offset = 0;
1462
1463				if ((subtotal + remainder) == protgrp_bytes)
1464					pgdone = 1;
1465			} else {
1466				/* must split this buffer with next prot grp */
1467				bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1468				split_offset += bpl->tus.f.bdeSize;
1469			}
1470
1471			subtotal += bpl->tus.f.bdeSize;
1472
1473			if (datadir == DMA_TO_DEVICE)
1474				bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1475			else
1476				bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1477			bpl->tus.w = le32_to_cpu(bpl->tus.w);
1478
1479			num_bde++;
1480			curr_data++;
1481
1482			if (split_offset)
1483				break;
1484
1485			/* Move to the next s/g segment if possible */
1486			sgde = sg_next(sgde);
1487		}
1488
1489		/* are we done ? */
1490		if (curr_prot == protcnt) {
1491			alldone = 1;
1492		} else if (curr_prot < protcnt) {
1493			/* advance to next prot buffer */
1494			sgpe = sg_next(sgpe);
1495			bpl++;
1496
1497			/* update the reference tag */
1498			reftag += protgrp_blks;
1499		} else {
1500			/* if we're here, we have a bug */
1501			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1502				"9054 BLKGRD: bug in %s\n", __func__);
1503		}
1504
1505	} while (!alldone);
1506
1507out:
1508
1509
1510	return num_bde;
1511}
1512/*
1513 * Given a SCSI command that supports DIF, determine composition of protection
1514 * groups involved in setting up buffer lists
1515 *
1516 * Returns:
1517 *			      for DIF (for both read and write)
1518 * */
1519static int
1520lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1521{
1522	int ret = LPFC_PG_TYPE_INVALID;
1523	unsigned char op = scsi_get_prot_op(sc);
1524
1525	switch (op) {
1526	case SCSI_PROT_READ_STRIP:
1527	case SCSI_PROT_WRITE_INSERT:
1528		ret = LPFC_PG_TYPE_NO_DIF;
1529		break;
1530	case SCSI_PROT_READ_INSERT:
1531	case SCSI_PROT_WRITE_STRIP:
1532	case SCSI_PROT_READ_PASS:
1533	case SCSI_PROT_WRITE_PASS:
1534		ret = LPFC_PG_TYPE_DIF_BUF;
1535		break;
1536	default:
1537		lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1538				"9021 Unsupported protection op:%d\n", op);
1539		break;
1540	}
1541
1542	return ret;
1543}
1544
1545/*
1546 * This is the protection/DIF aware version of
1547 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1548 * two functions eventually, but for now, it's here
1549 */
1550static int
1551lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1552		struct lpfc_scsi_buf *lpfc_cmd)
1553{
1554	struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1555	struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1556	struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1557	IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1558	uint32_t num_bde = 0;
1559	int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1560	int prot_group_type = 0;
1561	int diflen, fcpdl;
1562	unsigned blksize;
1563
1564	/*
1565	 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1566	 *  fcp_rsp regions to the first data bde entry
1567	 */
1568	bpl += 2;
1569	if (scsi_sg_count(scsi_cmnd)) {
1570		/*
1571		 * The driver stores the segment count returned from pci_map_sg
1572		 * because this a count of dma-mappings used to map the use_sg
1573		 * pages.  They are not guaranteed to be the same for those
1574		 * architectures that implement an IOMMU.
1575		 */
1576		datasegcnt = dma_map_sg(&phba->pcidev->dev,
1577					scsi_sglist(scsi_cmnd),
1578					scsi_sg_count(scsi_cmnd), datadir);
1579		if (unlikely(!datasegcnt))
1580			return 1;
1581
1582		lpfc_cmd->seg_cnt = datasegcnt;
1583		if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1584			lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1585					"9067 BLKGRD: %s: Too many sg segments"
1586					" from dma_map_sg.  Config %d, seg_cnt"
1587					" %d\n",
1588					__func__, phba->cfg_sg_seg_cnt,
1589					lpfc_cmd->seg_cnt);
1590			scsi_dma_unmap(scsi_cmnd);
1591			return 1;
1592		}
1593
1594		prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1595
1596		switch (prot_group_type) {
1597		case LPFC_PG_TYPE_NO_DIF:
1598			num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1599					datasegcnt);
1600			/* we should have 2 or more entries in buffer list */
1601			if (num_bde < 2)
1602				goto err;
1603			break;
1604		case LPFC_PG_TYPE_DIF_BUF:{
1605			/*
1606			 * This type indicates that protection buffers are
1607			 * passed to the driver, so that needs to be prepared
1608			 * for DMA
1609			 */
1610			protsegcnt = dma_map_sg(&phba->pcidev->dev,
1611					scsi_prot_sglist(scsi_cmnd),
1612					scsi_prot_sg_count(scsi_cmnd), datadir);
1613			if (unlikely(!protsegcnt)) {
1614				scsi_dma_unmap(scsi_cmnd);
1615				return 1;
1616			}
1617
1618			lpfc_cmd->prot_seg_cnt = protsegcnt;
1619			if (lpfc_cmd->prot_seg_cnt
1620			    > phba->cfg_prot_sg_seg_cnt) {
1621				lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1622					"9068 BLKGRD: %s: Too many prot sg "
1623					"segments from dma_map_sg.  Config %d,"
1624						"prot_seg_cnt %d\n", __func__,
1625						

Large files files are truncated, but you can click here to view the full file