PageRenderTime 73ms CodeModel.GetById 20ms app.highlight 43ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/target/target_core_pscsi.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 1473 lines | 1010 code | 183 blank | 280 comment | 156 complexity | 42c87c8ac743be62415c9985daab7aa4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
   1/*******************************************************************************
   2 * Filename:  target_core_pscsi.c
   3 *
   4 * This file contains the generic target mode <-> Linux SCSI subsystem plugin.
   5 *
   6 * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc.
   7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
   8 * Copyright (c) 2007-2010 Rising Tide Systems
   9 * Copyright (c) 2008-2010 Linux-iSCSI.org
  10 *
  11 * Nicholas A. Bellinger <nab@kernel.org>
  12 *
  13 * This program is free software; you can redistribute it and/or modify
  14 * it under the terms of the GNU General Public License as published by
  15 * the Free Software Foundation; either version 2 of the License, or
  16 * (at your option) any later version.
  17 *
  18 * This program is distributed in the hope that it will be useful,
  19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21 * GNU General Public License for more details.
  22 *
  23 * You should have received a copy of the GNU General Public License
  24 * along with this program; if not, write to the Free Software
  25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26 *
  27 ******************************************************************************/
  28
  29#include <linux/version.h>
  30#include <linux/string.h>
  31#include <linux/parser.h>
  32#include <linux/timer.h>
  33#include <linux/blkdev.h>
  34#include <linux/blk_types.h>
  35#include <linux/slab.h>
  36#include <linux/spinlock.h>
  37#include <linux/genhd.h>
  38#include <linux/cdrom.h>
  39#include <linux/file.h>
  40#include <scsi/scsi.h>
  41#include <scsi/scsi_device.h>
  42#include <scsi/scsi_cmnd.h>
  43#include <scsi/scsi_host.h>
  44#include <scsi/scsi_tcq.h>
  45
  46#include <target/target_core_base.h>
  47#include <target/target_core_device.h>
  48#include <target/target_core_transport.h>
  49
  50#include "target_core_pscsi.h"
  51
  52#define ISPRINT(a)  ((a >= ' ') && (a <= '~'))
  53
  54static struct se_subsystem_api pscsi_template;
  55
  56static void pscsi_req_done(struct request *, int);
  57
  58/*	pscsi_get_sh():
  59 *
  60 *
  61 */
  62static struct Scsi_Host *pscsi_get_sh(u32 host_no)
  63{
  64	struct Scsi_Host *sh = NULL;
  65
  66	sh = scsi_host_lookup(host_no);
  67	if (IS_ERR(sh)) {
  68		printk(KERN_ERR "Unable to locate SCSI HBA with Host ID:"
  69				" %u\n", host_no);
  70		return NULL;
  71	}
  72
  73	return sh;
  74}
  75
  76/*	pscsi_attach_hba():
  77 *
  78 * 	pscsi_get_sh() used scsi_host_lookup() to locate struct Scsi_Host.
  79 *	from the passed SCSI Host ID.
  80 */
  81static int pscsi_attach_hba(struct se_hba *hba, u32 host_id)
  82{
  83	int hba_depth;
  84	struct pscsi_hba_virt *phv;
  85
  86	phv = kzalloc(sizeof(struct pscsi_hba_virt), GFP_KERNEL);
  87	if (!(phv)) {
  88		printk(KERN_ERR "Unable to allocate struct pscsi_hba_virt\n");
  89		return -1;
  90	}
  91	phv->phv_host_id = host_id;
  92	phv->phv_mode = PHV_VIRUTAL_HOST_ID;
  93	hba_depth = PSCSI_VIRTUAL_HBA_DEPTH;
  94	atomic_set(&hba->left_queue_depth, hba_depth);
  95	atomic_set(&hba->max_queue_depth, hba_depth);
  96
  97	hba->hba_ptr = (void *)phv;
  98
  99	printk(KERN_INFO "CORE_HBA[%d] - TCM SCSI HBA Driver %s on"
 100		" Generic Target Core Stack %s\n", hba->hba_id,
 101		PSCSI_VERSION, TARGET_CORE_MOD_VERSION);
 102	printk(KERN_INFO "CORE_HBA[%d] - Attached SCSI HBA to Generic"
 103		" Target Core with TCQ Depth: %d\n", hba->hba_id,
 104		atomic_read(&hba->max_queue_depth));
 105
 106	return 0;
 107}
 108
 109static void pscsi_detach_hba(struct se_hba *hba)
 110{
 111	struct pscsi_hba_virt *phv = hba->hba_ptr;
 112	struct Scsi_Host *scsi_host = phv->phv_lld_host;
 113
 114	if (scsi_host) {
 115		scsi_host_put(scsi_host);
 116
 117		printk(KERN_INFO "CORE_HBA[%d] - Detached SCSI HBA: %s from"
 118			" Generic Target Core\n", hba->hba_id,
 119			(scsi_host->hostt->name) ? (scsi_host->hostt->name) :
 120			"Unknown");
 121	} else
 122		printk(KERN_INFO "CORE_HBA[%d] - Detached Virtual SCSI HBA"
 123			" from Generic Target Core\n", hba->hba_id);
 124
 125	kfree(phv);
 126	hba->hba_ptr = NULL;
 127}
 128
 129static int pscsi_pmode_enable_hba(struct se_hba *hba, unsigned long mode_flag)
 130{
 131	struct pscsi_hba_virt *phv = (struct pscsi_hba_virt *)hba->hba_ptr;
 132	struct Scsi_Host *sh = phv->phv_lld_host;
 133	int hba_depth = PSCSI_VIRTUAL_HBA_DEPTH;
 134	/*
 135	 * Release the struct Scsi_Host
 136	 */
 137	if (!(mode_flag)) {
 138		if (!(sh))
 139			return 0;
 140
 141		phv->phv_lld_host = NULL;
 142		phv->phv_mode = PHV_VIRUTAL_HOST_ID;
 143		atomic_set(&hba->left_queue_depth, hba_depth);
 144		atomic_set(&hba->max_queue_depth, hba_depth);
 145
 146		printk(KERN_INFO "CORE_HBA[%d] - Disabled pSCSI HBA Passthrough"
 147			" %s\n", hba->hba_id, (sh->hostt->name) ?
 148			(sh->hostt->name) : "Unknown");
 149
 150		scsi_host_put(sh);
 151		return 0;
 152	}
 153	/*
 154	 * Otherwise, locate struct Scsi_Host from the original passed
 155	 * pSCSI Host ID and enable for phba mode
 156	 */
 157	sh = pscsi_get_sh(phv->phv_host_id);
 158	if (!(sh)) {
 159		printk(KERN_ERR "pSCSI: Unable to locate SCSI Host for"
 160			" phv_host_id: %d\n", phv->phv_host_id);
 161		return -1;
 162	}
 163	/*
 164	 * Usually the SCSI LLD will use the hostt->can_queue value to define
 165	 * its HBA TCQ depth.  Some other drivers (like 2.6 megaraid) don't set
 166	 * this at all and set sh->can_queue at runtime.
 167	 */
 168	hba_depth = (sh->hostt->can_queue > sh->can_queue) ?
 169		sh->hostt->can_queue : sh->can_queue;
 170
 171	atomic_set(&hba->left_queue_depth, hba_depth);
 172	atomic_set(&hba->max_queue_depth, hba_depth);
 173
 174	phv->phv_lld_host = sh;
 175	phv->phv_mode = PHV_LLD_SCSI_HOST_NO;
 176
 177	printk(KERN_INFO "CORE_HBA[%d] - Enabled pSCSI HBA Passthrough %s\n",
 178		hba->hba_id, (sh->hostt->name) ? (sh->hostt->name) : "Unknown");
 179
 180	return 1;
 181}
 182
 183static void pscsi_tape_read_blocksize(struct se_device *dev,
 184		struct scsi_device *sdev)
 185{
 186	unsigned char cdb[MAX_COMMAND_SIZE], *buf;
 187	int ret;
 188
 189	buf = kzalloc(12, GFP_KERNEL);
 190	if (!buf)
 191		return;
 192
 193	memset(cdb, 0, MAX_COMMAND_SIZE);
 194	cdb[0] = MODE_SENSE;
 195	cdb[4] = 0x0c; /* 12 bytes */
 196
 197	ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf, 12, NULL,
 198			HZ, 1, NULL);
 199	if (ret)
 200		goto out_free;
 201
 202	/*
 203	 * If MODE_SENSE still returns zero, set the default value to 1024.
 204	 */
 205	sdev->sector_size = (buf[9] << 16) | (buf[10] << 8) | (buf[11]);
 206	if (!sdev->sector_size)
 207		sdev->sector_size = 1024;
 208out_free:
 209	kfree(buf);
 210}
 211
 212static void
 213pscsi_set_inquiry_info(struct scsi_device *sdev, struct t10_wwn *wwn)
 214{
 215	unsigned char *buf;
 216
 217	if (sdev->inquiry_len < INQUIRY_LEN)
 218		return;
 219
 220	buf = sdev->inquiry;
 221	if (!buf)
 222		return;
 223	/*
 224	 * Use sdev->inquiry from drivers/scsi/scsi_scan.c:scsi_alloc_sdev()
 225	 */
 226	memcpy(&wwn->vendor[0], &buf[8], sizeof(wwn->vendor));
 227	memcpy(&wwn->model[0], &buf[16], sizeof(wwn->model));
 228	memcpy(&wwn->revision[0], &buf[32], sizeof(wwn->revision));
 229}
 230
 231static int
 232pscsi_get_inquiry_vpd_serial(struct scsi_device *sdev, struct t10_wwn *wwn)
 233{
 234	unsigned char cdb[MAX_COMMAND_SIZE], *buf;
 235	int ret;
 236
 237	buf = kzalloc(INQUIRY_VPD_SERIAL_LEN, GFP_KERNEL);
 238	if (!buf)
 239		return -1;
 240
 241	memset(cdb, 0, MAX_COMMAND_SIZE);
 242	cdb[0] = INQUIRY;
 243	cdb[1] = 0x01; /* Query VPD */
 244	cdb[2] = 0x80; /* Unit Serial Number */
 245	cdb[3] = (INQUIRY_VPD_SERIAL_LEN >> 8) & 0xff;
 246	cdb[4] = (INQUIRY_VPD_SERIAL_LEN & 0xff);
 247
 248	ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
 249			      INQUIRY_VPD_SERIAL_LEN, NULL, HZ, 1, NULL);
 250	if (ret)
 251		goto out_free;
 252
 253	snprintf(&wwn->unit_serial[0], INQUIRY_VPD_SERIAL_LEN, "%s", &buf[4]);
 254
 255	wwn->t10_sub_dev->su_dev_flags |= SDF_FIRMWARE_VPD_UNIT_SERIAL;
 256
 257	kfree(buf);
 258	return 0;
 259
 260out_free:
 261	kfree(buf);
 262	return -1;
 263}
 264
 265static void
 266pscsi_get_inquiry_vpd_device_ident(struct scsi_device *sdev,
 267		struct t10_wwn *wwn)
 268{
 269	unsigned char cdb[MAX_COMMAND_SIZE], *buf, *page_83;
 270	int ident_len, page_len, off = 4, ret;
 271	struct t10_vpd *vpd;
 272
 273	buf = kzalloc(INQUIRY_VPD_SERIAL_LEN, GFP_KERNEL);
 274	if (!buf)
 275		return;
 276
 277	memset(cdb, 0, MAX_COMMAND_SIZE);
 278	cdb[0] = INQUIRY;
 279	cdb[1] = 0x01; /* Query VPD */
 280	cdb[2] = 0x83; /* Device Identifier */
 281	cdb[3] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN >> 8) & 0xff;
 282	cdb[4] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN & 0xff);
 283
 284	ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
 285			      INQUIRY_VPD_DEVICE_IDENTIFIER_LEN,
 286			      NULL, HZ, 1, NULL);
 287	if (ret)
 288		goto out;
 289
 290	page_len = (buf[2] << 8) | buf[3];
 291	while (page_len > 0) {
 292		/* Grab a pointer to the Identification descriptor */
 293		page_83 = &buf[off];
 294		ident_len = page_83[3];
 295		if (!ident_len) {
 296			printk(KERN_ERR "page_83[3]: identifier"
 297					" length zero!\n");
 298			break;
 299		}
 300		printk(KERN_INFO "T10 VPD Identifer Length: %d\n", ident_len);
 301
 302		vpd = kzalloc(sizeof(struct t10_vpd), GFP_KERNEL);
 303		if (!vpd) {
 304			printk(KERN_ERR "Unable to allocate memory for"
 305					" struct t10_vpd\n");
 306			goto out;
 307		}
 308		INIT_LIST_HEAD(&vpd->vpd_list);
 309
 310		transport_set_vpd_proto_id(vpd, page_83);
 311		transport_set_vpd_assoc(vpd, page_83);
 312
 313		if (transport_set_vpd_ident_type(vpd, page_83) < 0) {
 314			off += (ident_len + 4);
 315			page_len -= (ident_len + 4);
 316			kfree(vpd);
 317			continue;
 318		}
 319		if (transport_set_vpd_ident(vpd, page_83) < 0) {
 320			off += (ident_len + 4);
 321			page_len -= (ident_len + 4);
 322			kfree(vpd);
 323			continue;
 324		}
 325
 326		list_add_tail(&vpd->vpd_list, &wwn->t10_vpd_list);
 327		off += (ident_len + 4);
 328		page_len -= (ident_len + 4);
 329	}
 330
 331out:
 332	kfree(buf);
 333}
 334
 335/*	pscsi_add_device_to_list():
 336 *
 337 *
 338 */
 339static struct se_device *pscsi_add_device_to_list(
 340	struct se_hba *hba,
 341	struct se_subsystem_dev *se_dev,
 342	struct pscsi_dev_virt *pdv,
 343	struct scsi_device *sd,
 344	int dev_flags)
 345{
 346	struct se_device *dev;
 347	struct se_dev_limits dev_limits;
 348	struct request_queue *q;
 349	struct queue_limits *limits;
 350
 351	memset(&dev_limits, 0, sizeof(struct se_dev_limits));
 352
 353	if (!sd->queue_depth) {
 354		sd->queue_depth = PSCSI_DEFAULT_QUEUEDEPTH;
 355
 356		printk(KERN_ERR "Set broken SCSI Device %d:%d:%d"
 357			" queue_depth to %d\n", sd->channel, sd->id,
 358				sd->lun, sd->queue_depth);
 359	}
 360	/*
 361	 * Setup the local scope queue_limits from struct request_queue->limits
 362	 * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
 363	 */
 364	q = sd->request_queue;
 365	limits = &dev_limits.limits;
 366	limits->logical_block_size = sd->sector_size;
 367	limits->max_hw_sectors = (sd->host->max_sectors > queue_max_hw_sectors(q)) ?
 368				  queue_max_hw_sectors(q) : sd->host->max_sectors;
 369	limits->max_sectors = (sd->host->max_sectors > queue_max_sectors(q)) ?
 370				  queue_max_sectors(q) : sd->host->max_sectors;
 371	dev_limits.hw_queue_depth = sd->queue_depth;
 372	dev_limits.queue_depth = sd->queue_depth;
 373	/*
 374	 * Setup our standard INQUIRY info into se_dev->t10_wwn
 375	 */
 376	pscsi_set_inquiry_info(sd, &se_dev->t10_wwn);
 377
 378	/*
 379	 * Set the pointer pdv->pdv_sd to from passed struct scsi_device,
 380	 * which has already been referenced with Linux SCSI code with
 381	 * scsi_device_get() in this file's pscsi_create_virtdevice().
 382	 *
 383	 * The passthrough operations called by the transport_add_device_*
 384	 * function below will require this pointer to be set for passthroug
 385	 *  ops.
 386	 *
 387	 * For the shutdown case in pscsi_free_device(), this struct
 388	 * scsi_device  reference is released with Linux SCSI code
 389	 * scsi_device_put() and the pdv->pdv_sd cleared.
 390	 */
 391	pdv->pdv_sd = sd;
 392
 393	dev = transport_add_device_to_core_hba(hba, &pscsi_template,
 394				se_dev, dev_flags, (void *)pdv,
 395				&dev_limits, NULL, NULL);
 396	if (!(dev)) {
 397		pdv->pdv_sd = NULL;
 398		return NULL;
 399	}
 400
 401	/*
 402	 * Locate VPD WWN Information used for various purposes within
 403	 * the Storage Engine.
 404	 */
 405	if (!pscsi_get_inquiry_vpd_serial(sd, &se_dev->t10_wwn)) {
 406		/*
 407		 * If VPD Unit Serial returned GOOD status, try
 408		 * VPD Device Identification page (0x83).
 409		 */
 410		pscsi_get_inquiry_vpd_device_ident(sd, &se_dev->t10_wwn);
 411	}
 412
 413	/*
 414	 * For TYPE_TAPE, attempt to determine blocksize with MODE_SENSE.
 415	 */
 416	if (sd->type == TYPE_TAPE)
 417		pscsi_tape_read_blocksize(dev, sd);
 418	return dev;
 419}
 420
 421static void *pscsi_allocate_virtdevice(struct se_hba *hba, const char *name)
 422{
 423	struct pscsi_dev_virt *pdv;
 424
 425	pdv = kzalloc(sizeof(struct pscsi_dev_virt), GFP_KERNEL);
 426	if (!(pdv)) {
 427		printk(KERN_ERR "Unable to allocate memory for struct pscsi_dev_virt\n");
 428		return NULL;
 429	}
 430	pdv->pdv_se_hba = hba;
 431
 432	printk(KERN_INFO "PSCSI: Allocated pdv: %p for %s\n", pdv, name);
 433	return (void *)pdv;
 434}
 435
 436/*
 437 * Called with struct Scsi_Host->host_lock called.
 438 */
 439static struct se_device *pscsi_create_type_disk(
 440	struct scsi_device *sd,
 441	struct pscsi_dev_virt *pdv,
 442	struct se_subsystem_dev *se_dev,
 443	struct se_hba *hba)
 444	__releases(sh->host_lock)
 445{
 446	struct se_device *dev;
 447	struct pscsi_hba_virt *phv = (struct pscsi_hba_virt *)pdv->pdv_se_hba->hba_ptr;
 448	struct Scsi_Host *sh = sd->host;
 449	struct block_device *bd;
 450	u32 dev_flags = 0;
 451
 452	if (scsi_device_get(sd)) {
 453		printk(KERN_ERR "scsi_device_get() failed for %d:%d:%d:%d\n",
 454			sh->host_no, sd->channel, sd->id, sd->lun);
 455		spin_unlock_irq(sh->host_lock);
 456		return NULL;
 457	}
 458	spin_unlock_irq(sh->host_lock);
 459	/*
 460	 * Claim exclusive struct block_device access to struct scsi_device
 461	 * for TYPE_DISK using supplied udev_path
 462	 */
 463	bd = blkdev_get_by_path(se_dev->se_dev_udev_path,
 464				FMODE_WRITE|FMODE_READ|FMODE_EXCL, pdv);
 465	if (IS_ERR(bd)) {
 466		printk(KERN_ERR "pSCSI: blkdev_get_by_path() failed\n");
 467		scsi_device_put(sd);
 468		return NULL;
 469	}
 470	pdv->pdv_bd = bd;
 471
 472	dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
 473	if (!(dev)) {
 474		blkdev_put(pdv->pdv_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
 475		scsi_device_put(sd);
 476		return NULL;
 477	}
 478	printk(KERN_INFO "CORE_PSCSI[%d] - Added TYPE_DISK for %d:%d:%d:%d\n",
 479		phv->phv_host_id, sh->host_no, sd->channel, sd->id, sd->lun);
 480
 481	return dev;
 482}
 483
 484/*
 485 * Called with struct Scsi_Host->host_lock called.
 486 */
 487static struct se_device *pscsi_create_type_rom(
 488	struct scsi_device *sd,
 489	struct pscsi_dev_virt *pdv,
 490	struct se_subsystem_dev *se_dev,
 491	struct se_hba *hba)
 492	__releases(sh->host_lock)
 493{
 494	struct se_device *dev;
 495	struct pscsi_hba_virt *phv = (struct pscsi_hba_virt *)pdv->pdv_se_hba->hba_ptr;
 496	struct Scsi_Host *sh = sd->host;
 497	u32 dev_flags = 0;
 498
 499	if (scsi_device_get(sd)) {
 500		printk(KERN_ERR "scsi_device_get() failed for %d:%d:%d:%d\n",
 501			sh->host_no, sd->channel, sd->id, sd->lun);
 502		spin_unlock_irq(sh->host_lock);
 503		return NULL;
 504	}
 505	spin_unlock_irq(sh->host_lock);
 506
 507	dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
 508	if (!(dev)) {
 509		scsi_device_put(sd);
 510		return NULL;
 511	}
 512	printk(KERN_INFO "CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
 513		phv->phv_host_id, scsi_device_type(sd->type), sh->host_no,
 514		sd->channel, sd->id, sd->lun);
 515
 516	return dev;
 517}
 518
 519/*
 520 *Called with struct Scsi_Host->host_lock called.
 521 */
 522static struct se_device *pscsi_create_type_other(
 523	struct scsi_device *sd,
 524	struct pscsi_dev_virt *pdv,
 525	struct se_subsystem_dev *se_dev,
 526	struct se_hba *hba)
 527	__releases(sh->host_lock)
 528{
 529	struct se_device *dev;
 530	struct pscsi_hba_virt *phv = (struct pscsi_hba_virt *)pdv->pdv_se_hba->hba_ptr;
 531	struct Scsi_Host *sh = sd->host;
 532	u32 dev_flags = 0;
 533
 534	spin_unlock_irq(sh->host_lock);
 535	dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
 536	if (!(dev))
 537		return NULL;
 538
 539	printk(KERN_INFO "CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
 540		phv->phv_host_id, scsi_device_type(sd->type), sh->host_no,
 541		sd->channel, sd->id, sd->lun);
 542
 543	return dev;
 544}
 545
 546static struct se_device *pscsi_create_virtdevice(
 547	struct se_hba *hba,
 548	struct se_subsystem_dev *se_dev,
 549	void *p)
 550{
 551	struct pscsi_dev_virt *pdv = (struct pscsi_dev_virt *)p;
 552	struct se_device *dev;
 553	struct scsi_device *sd;
 554	struct pscsi_hba_virt *phv = (struct pscsi_hba_virt *)hba->hba_ptr;
 555	struct Scsi_Host *sh = phv->phv_lld_host;
 556	int legacy_mode_enable = 0;
 557
 558	if (!(pdv)) {
 559		printk(KERN_ERR "Unable to locate struct pscsi_dev_virt"
 560				" parameter\n");
 561		return ERR_PTR(-EINVAL);
 562	}
 563	/*
 564	 * If not running in PHV_LLD_SCSI_HOST_NO mode, locate the
 565	 * struct Scsi_Host we will need to bring the TCM/pSCSI object online
 566	 */
 567	if (!(sh)) {
 568		if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
 569			printk(KERN_ERR "pSCSI: Unable to locate struct"
 570				" Scsi_Host for PHV_LLD_SCSI_HOST_NO\n");
 571			return ERR_PTR(-ENODEV);
 572		}
 573		/*
 574		 * For the newer PHV_VIRUTAL_HOST_ID struct scsi_device
 575		 * reference, we enforce that udev_path has been set
 576		 */
 577		if (!(se_dev->su_dev_flags & SDF_USING_UDEV_PATH)) {
 578			printk(KERN_ERR "pSCSI: udev_path attribute has not"
 579				" been set before ENABLE=1\n");
 580			return ERR_PTR(-EINVAL);
 581		}
 582		/*
 583		 * If no scsi_host_id= was passed for PHV_VIRUTAL_HOST_ID,
 584		 * use the original TCM hba ID to reference Linux/SCSI Host No
 585		 * and enable for PHV_LLD_SCSI_HOST_NO mode.
 586		 */
 587		if (!(pdv->pdv_flags & PDF_HAS_VIRT_HOST_ID)) {
 588			spin_lock(&hba->device_lock);
 589			if (!(list_empty(&hba->hba_dev_list))) {
 590				printk(KERN_ERR "pSCSI: Unable to set hba_mode"
 591					" with active devices\n");
 592				spin_unlock(&hba->device_lock);
 593				return ERR_PTR(-EEXIST);
 594			}
 595			spin_unlock(&hba->device_lock);
 596
 597			if (pscsi_pmode_enable_hba(hba, 1) != 1)
 598				return ERR_PTR(-ENODEV);
 599
 600			legacy_mode_enable = 1;
 601			hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
 602			sh = phv->phv_lld_host;
 603		} else {
 604			sh = pscsi_get_sh(pdv->pdv_host_id);
 605			if (!(sh)) {
 606				printk(KERN_ERR "pSCSI: Unable to locate"
 607					" pdv_host_id: %d\n", pdv->pdv_host_id);
 608				return ERR_PTR(-ENODEV);
 609			}
 610		}
 611	} else {
 612		if (phv->phv_mode == PHV_VIRUTAL_HOST_ID) {
 613			printk(KERN_ERR "pSCSI: PHV_VIRUTAL_HOST_ID set while"
 614				" struct Scsi_Host exists\n");
 615			return ERR_PTR(-EEXIST);
 616		}
 617	}
 618
 619	spin_lock_irq(sh->host_lock);
 620	list_for_each_entry(sd, &sh->__devices, siblings) {
 621		if ((pdv->pdv_channel_id != sd->channel) ||
 622		    (pdv->pdv_target_id != sd->id) ||
 623		    (pdv->pdv_lun_id != sd->lun))
 624			continue;
 625		/*
 626		 * Functions will release the held struct scsi_host->host_lock
 627		 * before calling calling pscsi_add_device_to_list() to register
 628		 * struct scsi_device with target_core_mod.
 629		 */
 630		switch (sd->type) {
 631		case TYPE_DISK:
 632			dev = pscsi_create_type_disk(sd, pdv, se_dev, hba);
 633			break;
 634		case TYPE_ROM:
 635			dev = pscsi_create_type_rom(sd, pdv, se_dev, hba);
 636			break;
 637		default:
 638			dev = pscsi_create_type_other(sd, pdv, se_dev, hba);
 639			break;
 640		}
 641
 642		if (!(dev)) {
 643			if (phv->phv_mode == PHV_VIRUTAL_HOST_ID)
 644				scsi_host_put(sh);
 645			else if (legacy_mode_enable) {
 646				pscsi_pmode_enable_hba(hba, 0);
 647				hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
 648			}
 649			pdv->pdv_sd = NULL;
 650			return ERR_PTR(-ENODEV);
 651		}
 652		return dev;
 653	}
 654	spin_unlock_irq(sh->host_lock);
 655
 656	printk(KERN_ERR "pSCSI: Unable to locate %d:%d:%d:%d\n", sh->host_no,
 657		pdv->pdv_channel_id,  pdv->pdv_target_id, pdv->pdv_lun_id);
 658
 659	if (phv->phv_mode == PHV_VIRUTAL_HOST_ID)
 660		scsi_host_put(sh);
 661	else if (legacy_mode_enable) {
 662		pscsi_pmode_enable_hba(hba, 0);
 663		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
 664	}
 665
 666	return ERR_PTR(-ENODEV);
 667}
 668
 669/*	pscsi_free_device(): (Part of se_subsystem_api_t template)
 670 *
 671 *
 672 */
 673static void pscsi_free_device(void *p)
 674{
 675	struct pscsi_dev_virt *pdv = p;
 676	struct pscsi_hba_virt *phv = pdv->pdv_se_hba->hba_ptr;
 677	struct scsi_device *sd = pdv->pdv_sd;
 678
 679	if (sd) {
 680		/*
 681		 * Release exclusive pSCSI internal struct block_device claim for
 682		 * struct scsi_device with TYPE_DISK from pscsi_create_type_disk()
 683		 */
 684		if ((sd->type == TYPE_DISK) && pdv->pdv_bd) {
 685			blkdev_put(pdv->pdv_bd,
 686				   FMODE_WRITE|FMODE_READ|FMODE_EXCL);
 687			pdv->pdv_bd = NULL;
 688		}
 689		/*
 690		 * For HBA mode PHV_LLD_SCSI_HOST_NO, release the reference
 691		 * to struct Scsi_Host now.
 692		 */
 693		if ((phv->phv_mode == PHV_LLD_SCSI_HOST_NO) &&
 694		    (phv->phv_lld_host != NULL))
 695			scsi_host_put(phv->phv_lld_host);
 696
 697		if ((sd->type == TYPE_DISK) || (sd->type == TYPE_ROM))
 698			scsi_device_put(sd);
 699
 700		pdv->pdv_sd = NULL;
 701	}
 702
 703	kfree(pdv);
 704}
 705
 706static inline struct pscsi_plugin_task *PSCSI_TASK(struct se_task *task)
 707{
 708	return container_of(task, struct pscsi_plugin_task, pscsi_task);
 709}
 710
 711
 712/*	pscsi_transport_complete():
 713 *
 714 *
 715 */
 716static int pscsi_transport_complete(struct se_task *task)
 717{
 718	struct pscsi_dev_virt *pdv = task->se_dev->dev_ptr;
 719	struct scsi_device *sd = pdv->pdv_sd;
 720	int result;
 721	struct pscsi_plugin_task *pt = PSCSI_TASK(task);
 722	unsigned char *cdb = &pt->pscsi_cdb[0];
 723
 724	result = pt->pscsi_result;
 725	/*
 726	 * Hack to make sure that Write-Protect modepage is set if R/O mode is
 727	 * forced.
 728	 */
 729	if (((cdb[0] == MODE_SENSE) || (cdb[0] == MODE_SENSE_10)) &&
 730	     (status_byte(result) << 1) == SAM_STAT_GOOD) {
 731		if (!TASK_CMD(task)->se_deve)
 732			goto after_mode_sense;
 733
 734		if (TASK_CMD(task)->se_deve->lun_flags &
 735				TRANSPORT_LUNFLAGS_READ_ONLY) {
 736			unsigned char *buf = (unsigned char *)
 737				T_TASK(task->task_se_cmd)->t_task_buf;
 738
 739			if (cdb[0] == MODE_SENSE_10) {
 740				if (!(buf[3] & 0x80))
 741					buf[3] |= 0x80;
 742			} else {
 743				if (!(buf[2] & 0x80))
 744					buf[2] |= 0x80;
 745			}
 746		}
 747	}
 748after_mode_sense:
 749
 750	if (sd->type != TYPE_TAPE)
 751		goto after_mode_select;
 752
 753	/*
 754	 * Hack to correctly obtain the initiator requested blocksize for
 755	 * TYPE_TAPE.  Since this value is dependent upon each tape media,
 756	 * struct scsi_device->sector_size will not contain the correct value
 757	 * by default, so we go ahead and set it so
 758	 * TRANSPORT(dev)->get_blockdev() returns the correct value to the
 759	 * storage engine.
 760	 */
 761	if (((cdb[0] == MODE_SELECT) || (cdb[0] == MODE_SELECT_10)) &&
 762	      (status_byte(result) << 1) == SAM_STAT_GOOD) {
 763		unsigned char *buf;
 764		struct scatterlist *sg = task->task_sg;
 765		u16 bdl;
 766		u32 blocksize;
 767
 768		buf = sg_virt(&sg[0]);
 769		if (!(buf)) {
 770			printk(KERN_ERR "Unable to get buf for scatterlist\n");
 771			goto after_mode_select;
 772		}
 773
 774		if (cdb[0] == MODE_SELECT)
 775			bdl = (buf[3]);
 776		else
 777			bdl = (buf[6] << 8) | (buf[7]);
 778
 779		if (!bdl)
 780			goto after_mode_select;
 781
 782		if (cdb[0] == MODE_SELECT)
 783			blocksize = (buf[9] << 16) | (buf[10] << 8) |
 784					(buf[11]);
 785		else
 786			blocksize = (buf[13] << 16) | (buf[14] << 8) |
 787					(buf[15]);
 788
 789		sd->sector_size = blocksize;
 790	}
 791after_mode_select:
 792
 793	if (status_byte(result) & CHECK_CONDITION)
 794		return 1;
 795
 796	return 0;
 797}
 798
 799static struct se_task *
 800pscsi_alloc_task(struct se_cmd *cmd)
 801{
 802	struct pscsi_plugin_task *pt;
 803	unsigned char *cdb = T_TASK(cmd)->t_task_cdb;
 804
 805	pt = kzalloc(sizeof(struct pscsi_plugin_task), GFP_KERNEL);
 806	if (!pt) {
 807		printk(KERN_ERR "Unable to allocate struct pscsi_plugin_task\n");
 808		return NULL;
 809	}
 810
 811	/*
 812	 * If TCM Core is signaling a > TCM_MAX_COMMAND_SIZE allocation,
 813	 * allocate the extended CDB buffer for per struct se_task context
 814	 * pt->pscsi_cdb now.
 815	 */
 816	if (T_TASK(cmd)->t_task_cdb != T_TASK(cmd)->__t_task_cdb) {
 817
 818		pt->pscsi_cdb = kzalloc(scsi_command_size(cdb), GFP_KERNEL);
 819		if (!(pt->pscsi_cdb)) {
 820			printk(KERN_ERR "pSCSI: Unable to allocate extended"
 821					" pt->pscsi_cdb\n");
 822			kfree(pt);
 823			return NULL;
 824		}
 825	} else
 826		pt->pscsi_cdb = &pt->__pscsi_cdb[0];
 827
 828	return &pt->pscsi_task;
 829}
 830
 831static inline void pscsi_blk_init_request(
 832	struct se_task *task,
 833	struct pscsi_plugin_task *pt,
 834	struct request *req,
 835	int bidi_read)
 836{
 837	/*
 838	 * Defined as "scsi command" in include/linux/blkdev.h.
 839	 */
 840	req->cmd_type = REQ_TYPE_BLOCK_PC;
 841	/*
 842	 * For the extra BIDI-COMMAND READ struct request we do not
 843	 * need to setup the remaining structure members
 844	 */
 845	if (bidi_read)
 846		return;
 847	/*
 848	 * Setup the done function pointer for struct request,
 849	 * also set the end_io_data pointer.to struct se_task.
 850	 */
 851	req->end_io = pscsi_req_done;
 852	req->end_io_data = (void *)task;
 853	/*
 854	 * Load the referenced struct se_task's SCSI CDB into
 855	 * include/linux/blkdev.h:struct request->cmd
 856	 */
 857	req->cmd_len = scsi_command_size(pt->pscsi_cdb);
 858	req->cmd = &pt->pscsi_cdb[0];
 859	/*
 860	 * Setup pointer for outgoing sense data.
 861	 */
 862	req->sense = (void *)&pt->pscsi_sense[0];
 863	req->sense_len = 0;
 864}
 865
 866/*
 867 * Used for pSCSI data payloads for all *NON* SCF_SCSI_DATA_SG_IO_CDB
 868*/
 869static int pscsi_blk_get_request(struct se_task *task)
 870{
 871	struct pscsi_plugin_task *pt = PSCSI_TASK(task);
 872	struct pscsi_dev_virt *pdv = task->se_dev->dev_ptr;
 873
 874	pt->pscsi_req = blk_get_request(pdv->pdv_sd->request_queue,
 875			(task->task_data_direction == DMA_TO_DEVICE),
 876			GFP_KERNEL);
 877	if (!(pt->pscsi_req) || IS_ERR(pt->pscsi_req)) {
 878		printk(KERN_ERR "PSCSI: blk_get_request() failed: %ld\n",
 879				IS_ERR(pt->pscsi_req));
 880		return PYX_TRANSPORT_LU_COMM_FAILURE;
 881	}
 882	/*
 883	 * Setup the newly allocated struct request for REQ_TYPE_BLOCK_PC,
 884	 * and setup rq callback, CDB and sense.
 885	 */
 886	pscsi_blk_init_request(task, pt, pt->pscsi_req, 0);
 887	return 0;
 888}
 889
 890/*      pscsi_do_task(): (Part of se_subsystem_api_t template)
 891 *
 892 *
 893 */
 894static int pscsi_do_task(struct se_task *task)
 895{
 896	struct pscsi_plugin_task *pt = PSCSI_TASK(task);
 897	struct pscsi_dev_virt *pdv = task->se_dev->dev_ptr;
 898	/*
 899	 * Set the struct request->timeout value based on peripheral
 900	 * device type from SCSI.
 901	 */
 902	if (pdv->pdv_sd->type == TYPE_DISK)
 903		pt->pscsi_req->timeout = PS_TIMEOUT_DISK;
 904	else
 905		pt->pscsi_req->timeout = PS_TIMEOUT_OTHER;
 906
 907	pt->pscsi_req->retries = PS_RETRY;
 908	/*
 909	 * Queue the struct request into the struct scsi_device->request_queue.
 910	 * Also check for HEAD_OF_QUEUE SAM TASK attr from received se_cmd
 911	 * descriptor
 912	 */
 913	blk_execute_rq_nowait(pdv->pdv_sd->request_queue, NULL, pt->pscsi_req,
 914			(task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG),
 915			pscsi_req_done);
 916
 917	return PYX_TRANSPORT_SENT_TO_TRANSPORT;
 918}
 919
 920static void pscsi_free_task(struct se_task *task)
 921{
 922	struct pscsi_plugin_task *pt = PSCSI_TASK(task);
 923	struct se_cmd *cmd = task->task_se_cmd;
 924
 925	/*
 926	 * Release the extended CDB allocation from pscsi_alloc_task()
 927	 * if one exists.
 928	 */
 929	if (T_TASK(cmd)->t_task_cdb != T_TASK(cmd)->__t_task_cdb)
 930		kfree(pt->pscsi_cdb);
 931	/*
 932	 * We do not release the bio(s) here associated with this task, as
 933	 * this is handled by bio_put() and pscsi_bi_endio().
 934	 */
 935	kfree(pt);
 936}
 937
 938enum {
 939	Opt_scsi_host_id, Opt_scsi_channel_id, Opt_scsi_target_id,
 940	Opt_scsi_lun_id, Opt_err
 941};
 942
 943static match_table_t tokens = {
 944	{Opt_scsi_host_id, "scsi_host_id=%d"},
 945	{Opt_scsi_channel_id, "scsi_channel_id=%d"},
 946	{Opt_scsi_target_id, "scsi_target_id=%d"},
 947	{Opt_scsi_lun_id, "scsi_lun_id=%d"},
 948	{Opt_err, NULL}
 949};
 950
 951static ssize_t pscsi_set_configfs_dev_params(struct se_hba *hba,
 952	struct se_subsystem_dev *se_dev,
 953	const char *page,
 954	ssize_t count)
 955{
 956	struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
 957	struct pscsi_hba_virt *phv = hba->hba_ptr;
 958	char *orig, *ptr, *opts;
 959	substring_t args[MAX_OPT_ARGS];
 960	int ret = 0, arg, token;
 961
 962	opts = kstrdup(page, GFP_KERNEL);
 963	if (!opts)
 964		return -ENOMEM;
 965
 966	orig = opts;
 967
 968	while ((ptr = strsep(&opts, ",")) != NULL) {
 969		if (!*ptr)
 970			continue;
 971
 972		token = match_token(ptr, tokens, args);
 973		switch (token) {
 974		case Opt_scsi_host_id:
 975			if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
 976				printk(KERN_ERR "PSCSI[%d]: Unable to accept"
 977					" scsi_host_id while phv_mode =="
 978					" PHV_LLD_SCSI_HOST_NO\n",
 979					phv->phv_host_id);
 980				ret = -EINVAL;
 981				goto out;
 982			}
 983			match_int(args, &arg);
 984			pdv->pdv_host_id = arg;
 985			printk(KERN_INFO "PSCSI[%d]: Referencing SCSI Host ID:"
 986				" %d\n", phv->phv_host_id, pdv->pdv_host_id);
 987			pdv->pdv_flags |= PDF_HAS_VIRT_HOST_ID;
 988			break;
 989		case Opt_scsi_channel_id:
 990			match_int(args, &arg);
 991			pdv->pdv_channel_id = arg;
 992			printk(KERN_INFO "PSCSI[%d]: Referencing SCSI Channel"
 993				" ID: %d\n",  phv->phv_host_id,
 994				pdv->pdv_channel_id);
 995			pdv->pdv_flags |= PDF_HAS_CHANNEL_ID;
 996			break;
 997		case Opt_scsi_target_id:
 998			match_int(args, &arg);
 999			pdv->pdv_target_id = arg;
1000			printk(KERN_INFO "PSCSI[%d]: Referencing SCSI Target"
1001				" ID: %d\n", phv->phv_host_id,
1002				pdv->pdv_target_id);
1003			pdv->pdv_flags |= PDF_HAS_TARGET_ID;
1004			break;
1005		case Opt_scsi_lun_id:
1006			match_int(args, &arg);
1007			pdv->pdv_lun_id = arg;
1008			printk(KERN_INFO "PSCSI[%d]: Referencing SCSI LUN ID:"
1009				" %d\n", phv->phv_host_id, pdv->pdv_lun_id);
1010			pdv->pdv_flags |= PDF_HAS_LUN_ID;
1011			break;
1012		default:
1013			break;
1014		}
1015	}
1016
1017out:
1018	kfree(orig);
1019	return (!ret) ? count : ret;
1020}
1021
1022static ssize_t pscsi_check_configfs_dev_params(
1023	struct se_hba *hba,
1024	struct se_subsystem_dev *se_dev)
1025{
1026	struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
1027
1028	if (!(pdv->pdv_flags & PDF_HAS_CHANNEL_ID) ||
1029	    !(pdv->pdv_flags & PDF_HAS_TARGET_ID) ||
1030	    !(pdv->pdv_flags & PDF_HAS_LUN_ID)) {
1031		printk(KERN_ERR "Missing scsi_channel_id=, scsi_target_id= and"
1032			" scsi_lun_id= parameters\n");
1033		return -1;
1034	}
1035
1036	return 0;
1037}
1038
1039static ssize_t pscsi_show_configfs_dev_params(struct se_hba *hba,
1040					      struct se_subsystem_dev *se_dev,
1041					      char *b)
1042{
1043	struct pscsi_hba_virt *phv = hba->hba_ptr;
1044        struct pscsi_dev_virt *pdv = se_dev->se_dev_su_ptr;
1045	struct scsi_device *sd = pdv->pdv_sd;
1046	unsigned char host_id[16];
1047	ssize_t bl;
1048	int i;
1049
1050	if (phv->phv_mode == PHV_VIRUTAL_HOST_ID)
1051		snprintf(host_id, 16, "%d", pdv->pdv_host_id);
1052	else
1053		snprintf(host_id, 16, "PHBA Mode");
1054
1055	bl = sprintf(b, "SCSI Device Bus Location:"
1056		" Channel ID: %d Target ID: %d LUN: %d Host ID: %s\n",
1057		pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id,
1058		host_id);
1059
1060	if (sd) {
1061		bl += sprintf(b + bl, "        ");
1062		bl += sprintf(b + bl, "Vendor: ");
1063		for (i = 0; i < 8; i++) {
1064			if (ISPRINT(sd->vendor[i]))   /* printable character? */
1065				bl += sprintf(b + bl, "%c", sd->vendor[i]);
1066			else
1067				bl += sprintf(b + bl, " ");
1068		}
1069		bl += sprintf(b + bl, " Model: ");
1070		for (i = 0; i < 16; i++) {
1071			if (ISPRINT(sd->model[i]))   /* printable character ? */
1072				bl += sprintf(b + bl, "%c", sd->model[i]);
1073			else
1074				bl += sprintf(b + bl, " ");
1075		}
1076		bl += sprintf(b + bl, " Rev: ");
1077		for (i = 0; i < 4; i++) {
1078			if (ISPRINT(sd->rev[i]))   /* printable character ? */
1079				bl += sprintf(b + bl, "%c", sd->rev[i]);
1080			else
1081				bl += sprintf(b + bl, " ");
1082		}
1083		bl += sprintf(b + bl, "\n");
1084	}
1085	return bl;
1086}
1087
1088static void pscsi_bi_endio(struct bio *bio, int error)
1089{
1090	bio_put(bio);
1091}
1092
1093static inline struct bio *pscsi_get_bio(struct pscsi_dev_virt *pdv, int sg_num)
1094{
1095	struct bio *bio;
1096	/*
1097	 * Use bio_malloc() following the comment in for bio -> struct request
1098	 * in block/blk-core.c:blk_make_request()
1099	 */
1100	bio = bio_kmalloc(GFP_KERNEL, sg_num);
1101	if (!(bio)) {
1102		printk(KERN_ERR "PSCSI: bio_kmalloc() failed\n");
1103		return NULL;
1104	}
1105	bio->bi_end_io = pscsi_bi_endio;
1106
1107	return bio;
1108}
1109
1110#if 0
1111#define DEBUG_PSCSI(x...) printk(x)
1112#else
1113#define DEBUG_PSCSI(x...)
1114#endif
1115
1116static int __pscsi_map_task_SG(
1117	struct se_task *task,
1118	struct scatterlist *task_sg,
1119	u32 task_sg_num,
1120	int bidi_read)
1121{
1122	struct pscsi_plugin_task *pt = PSCSI_TASK(task);
1123	struct pscsi_dev_virt *pdv = task->se_dev->dev_ptr;
1124	struct bio *bio = NULL, *hbio = NULL, *tbio = NULL;
1125	struct page *page;
1126	struct scatterlist *sg;
1127	u32 data_len = task->task_size, i, len, bytes, off;
1128	int nr_pages = (task->task_size + task_sg[0].offset +
1129			PAGE_SIZE - 1) >> PAGE_SHIFT;
1130	int nr_vecs = 0, rc, ret = PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
1131	int rw = (task->task_data_direction == DMA_TO_DEVICE);
1132
1133	if (!task->task_size)
1134		return 0;
1135	/*
1136	 * For SCF_SCSI_DATA_SG_IO_CDB, Use fs/bio.c:bio_add_page() to setup
1137	 * the bio_vec maplist from TC< struct se_mem -> task->task_sg ->
1138	 * struct scatterlist memory.  The struct se_task->task_sg[] currently needs
1139	 * to be attached to struct bios for submission to Linux/SCSI using
1140	 * struct request to struct scsi_device->request_queue.
1141	 *
1142	 * Note that this will be changing post v2.6.28 as Target_Core_Mod/pSCSI
1143	 * is ported to upstream SCSI passthrough functionality that accepts
1144	 * struct scatterlist->page_link or struct page as a paraemeter.
1145	 */
1146	DEBUG_PSCSI("PSCSI: nr_pages: %d\n", nr_pages);
1147
1148	for_each_sg(task_sg, sg, task_sg_num, i) {
1149		page = sg_page(sg);
1150		off = sg->offset;
1151		len = sg->length;
1152
1153		DEBUG_PSCSI("PSCSI: i: %d page: %p len: %d off: %d\n", i,
1154			page, len, off);
1155
1156		while (len > 0 && data_len > 0) {
1157			bytes = min_t(unsigned int, len, PAGE_SIZE - off);
1158			bytes = min(bytes, data_len);
1159
1160			if (!(bio)) {
1161				nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
1162				nr_pages -= nr_vecs;
1163				/*
1164				 * Calls bio_kmalloc() and sets bio->bi_end_io()
1165				 */
1166				bio = pscsi_get_bio(pdv, nr_vecs);
1167				if (!(bio))
1168					goto fail;
1169
1170				if (rw)
1171					bio->bi_rw |= REQ_WRITE;
1172
1173				DEBUG_PSCSI("PSCSI: Allocated bio: %p,"
1174					" dir: %s nr_vecs: %d\n", bio,
1175					(rw) ? "rw" : "r", nr_vecs);
1176				/*
1177				 * Set *hbio pointer to handle the case:
1178				 * nr_pages > BIO_MAX_PAGES, where additional
1179				 * bios need to be added to complete a given
1180				 * struct se_task
1181				 */
1182				if (!hbio)
1183					hbio = tbio = bio;
1184				else
1185					tbio = tbio->bi_next = bio;
1186			}
1187
1188			DEBUG_PSCSI("PSCSI: Calling bio_add_pc_page() i: %d"
1189				" bio: %p page: %p len: %d off: %d\n", i, bio,
1190				page, len, off);
1191
1192			rc = bio_add_pc_page(pdv->pdv_sd->request_queue,
1193					bio, page, bytes, off);
1194			if (rc != bytes)
1195				goto fail;
1196
1197			DEBUG_PSCSI("PSCSI: bio->bi_vcnt: %d nr_vecs: %d\n",
1198				bio->bi_vcnt, nr_vecs);
1199
1200			if (bio->bi_vcnt > nr_vecs) {
1201				DEBUG_PSCSI("PSCSI: Reached bio->bi_vcnt max:"
1202					" %d i: %d bio: %p, allocating another"
1203					" bio\n", bio->bi_vcnt, i, bio);
1204				/*
1205				 * Clear the pointer so that another bio will
1206				 * be allocated with pscsi_get_bio() above, the
1207				 * current bio has already been set *tbio and
1208				 * bio->bi_next.
1209				 */
1210				bio = NULL;
1211			}
1212
1213			page++;
1214			len -= bytes;
1215			data_len -= bytes;
1216			off = 0;
1217		}
1218	}
1219	/*
1220	 * Setup the primary pt->pscsi_req used for non BIDI and BIDI-COMMAND
1221	 * primary SCSI WRITE poayload mapped for struct se_task->task_sg[]
1222	 */
1223	if (!(bidi_read)) {
1224		/*
1225		 * Starting with v2.6.31, call blk_make_request() passing in *hbio to
1226		 * allocate the pSCSI task a struct request.
1227		 */
1228		pt->pscsi_req = blk_make_request(pdv->pdv_sd->request_queue,
1229					hbio, GFP_KERNEL);
1230		if (!(pt->pscsi_req)) {
1231			printk(KERN_ERR "pSCSI: blk_make_request() failed\n");
1232			goto fail;
1233		}
1234		/*
1235		 * Setup the newly allocated struct request for REQ_TYPE_BLOCK_PC,
1236		 * and setup rq callback, CDB and sense.
1237		 */
1238		pscsi_blk_init_request(task, pt, pt->pscsi_req, 0);
1239
1240		return task->task_sg_num;
1241	}
1242	/*
1243	 * Setup the secondary pt->pscsi_req->next_rq used for the extra BIDI-COMMAND
1244	 * SCSI READ paylaod mapped for struct se_task->task_sg_bidi[]
1245	 */
1246	pt->pscsi_req->next_rq = blk_make_request(pdv->pdv_sd->request_queue,
1247					hbio, GFP_KERNEL);
1248	if (!(pt->pscsi_req->next_rq)) {
1249		printk(KERN_ERR "pSCSI: blk_make_request() failed for BIDI\n");
1250		goto fail;
1251	}
1252	pscsi_blk_init_request(task, pt, pt->pscsi_req->next_rq, 1);
1253
1254	return task->task_sg_num;
1255fail:
1256	while (hbio) {
1257		bio = hbio;
1258		hbio = hbio->bi_next;
1259		bio->bi_next = NULL;
1260		bio_endio(bio, 0);
1261	}
1262	return ret;
1263}
1264
1265static int pscsi_map_task_SG(struct se_task *task)
1266{
1267	int ret;
1268
1269	/*
1270	 * Setup the main struct request for the task->task_sg[] payload
1271	 */
1272
1273	ret = __pscsi_map_task_SG(task, task->task_sg, task->task_sg_num, 0);
1274	if (ret >= 0 && task->task_sg_bidi) {
1275		/*
1276		 * If present, set up the extra BIDI-COMMAND SCSI READ
1277		 * struct request and payload.
1278		 */
1279		ret = __pscsi_map_task_SG(task, task->task_sg_bidi,
1280					task->task_sg_num, 1);
1281	}
1282
1283	if (ret < 0)
1284		return PYX_TRANSPORT_LU_COMM_FAILURE;
1285	return 0;
1286}
1287
1288/*	pscsi_map_task_non_SG():
1289 *
1290 *
1291 */
1292static int pscsi_map_task_non_SG(struct se_task *task)
1293{
1294	struct se_cmd *cmd = TASK_CMD(task);
1295	struct pscsi_plugin_task *pt = PSCSI_TASK(task);
1296	struct pscsi_dev_virt *pdv = task->se_dev->dev_ptr;
1297	int ret = 0;
1298
1299	if (pscsi_blk_get_request(task) < 0)
1300		return PYX_TRANSPORT_LU_COMM_FAILURE;
1301
1302	if (!task->task_size)
1303		return 0;
1304
1305	ret = blk_rq_map_kern(pdv->pdv_sd->request_queue,
1306			pt->pscsi_req, T_TASK(cmd)->t_task_buf,
1307			task->task_size, GFP_KERNEL);
1308	if (ret < 0) {
1309		printk(KERN_ERR "PSCSI: blk_rq_map_kern() failed: %d\n", ret);
1310		return PYX_TRANSPORT_LU_COMM_FAILURE;
1311	}
1312	return 0;
1313}
1314
1315static int pscsi_CDB_none(struct se_task *task)
1316{
1317	return pscsi_blk_get_request(task);
1318}
1319
1320/*	pscsi_get_cdb():
1321 *
1322 *
1323 */
1324static unsigned char *pscsi_get_cdb(struct se_task *task)
1325{
1326	struct pscsi_plugin_task *pt = PSCSI_TASK(task);
1327
1328	return pt->pscsi_cdb;
1329}
1330
1331/*	pscsi_get_sense_buffer():
1332 *
1333 *
1334 */
1335static unsigned char *pscsi_get_sense_buffer(struct se_task *task)
1336{
1337	struct pscsi_plugin_task *pt = PSCSI_TASK(task);
1338
1339	return (unsigned char *)&pt->pscsi_sense[0];
1340}
1341
1342/*	pscsi_get_device_rev():
1343 *
1344 *
1345 */
1346static u32 pscsi_get_device_rev(struct se_device *dev)
1347{
1348	struct pscsi_dev_virt *pdv = dev->dev_ptr;
1349	struct scsi_device *sd = pdv->pdv_sd;
1350
1351	return (sd->scsi_level - 1) ? sd->scsi_level - 1 : 1;
1352}
1353
1354/*	pscsi_get_device_type():
1355 *
1356 *
1357 */
1358static u32 pscsi_get_device_type(struct se_device *dev)
1359{
1360	struct pscsi_dev_virt *pdv = dev->dev_ptr;
1361	struct scsi_device *sd = pdv->pdv_sd;
1362
1363	return sd->type;
1364}
1365
1366static sector_t pscsi_get_blocks(struct se_device *dev)
1367{
1368	struct pscsi_dev_virt *pdv = dev->dev_ptr;
1369
1370	if (pdv->pdv_bd && pdv->pdv_bd->bd_part)
1371		return pdv->pdv_bd->bd_part->nr_sects;
1372
1373	dump_stack();
1374	return 0;
1375}
1376
1377/*	pscsi_handle_SAM_STATUS_failures():
1378 *
1379 *
1380 */
1381static inline void pscsi_process_SAM_status(
1382	struct se_task *task,
1383	struct pscsi_plugin_task *pt)
1384{
1385	task->task_scsi_status = status_byte(pt->pscsi_result);
1386	if ((task->task_scsi_status)) {
1387		task->task_scsi_status <<= 1;
1388		printk(KERN_INFO "PSCSI Status Byte exception at task: %p CDB:"
1389			" 0x%02x Result: 0x%08x\n", task, pt->pscsi_cdb[0],
1390			pt->pscsi_result);
1391	}
1392
1393	switch (host_byte(pt->pscsi_result)) {
1394	case DID_OK:
1395		transport_complete_task(task, (!task->task_scsi_status));
1396		break;
1397	default:
1398		printk(KERN_INFO "PSCSI Host Byte exception at task: %p CDB:"
1399			" 0x%02x Result: 0x%08x\n", task, pt->pscsi_cdb[0],
1400			pt->pscsi_result);
1401		task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
1402		task->task_error_status = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1403		TASK_CMD(task)->transport_error_status =
1404					PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1405		transport_complete_task(task, 0);
1406		break;
1407	}
1408
1409	return;
1410}
1411
1412static void pscsi_req_done(struct request *req, int uptodate)
1413{
1414	struct se_task *task = req->end_io_data;
1415	struct pscsi_plugin_task *pt = PSCSI_TASK(task);
1416
1417	pt->pscsi_result = req->errors;
1418	pt->pscsi_resid = req->resid_len;
1419
1420	pscsi_process_SAM_status(task, pt);
1421	/*
1422	 * Release BIDI-READ if present
1423	 */
1424	if (req->next_rq != NULL)
1425		__blk_put_request(req->q, req->next_rq);
1426
1427	__blk_put_request(req->q, req);
1428	pt->pscsi_req = NULL;
1429}
1430
1431static struct se_subsystem_api pscsi_template = {
1432	.name			= "pscsi",
1433	.owner			= THIS_MODULE,
1434	.transport_type		= TRANSPORT_PLUGIN_PHBA_PDEV,
1435	.cdb_none		= pscsi_CDB_none,
1436	.map_task_non_SG	= pscsi_map_task_non_SG,
1437	.map_task_SG		= pscsi_map_task_SG,
1438	.attach_hba		= pscsi_attach_hba,
1439	.detach_hba		= pscsi_detach_hba,
1440	.pmode_enable_hba	= pscsi_pmode_enable_hba,
1441	.allocate_virtdevice	= pscsi_allocate_virtdevice,
1442	.create_virtdevice	= pscsi_create_virtdevice,
1443	.free_device		= pscsi_free_device,
1444	.transport_complete	= pscsi_transport_complete,
1445	.alloc_task		= pscsi_alloc_task,
1446	.do_task		= pscsi_do_task,
1447	.free_task		= pscsi_free_task,
1448	.check_configfs_dev_params = pscsi_check_configfs_dev_params,
1449	.set_configfs_dev_params = pscsi_set_configfs_dev_params,
1450	.show_configfs_dev_params = pscsi_show_configfs_dev_params,
1451	.get_cdb		= pscsi_get_cdb,
1452	.get_sense_buffer	= pscsi_get_sense_buffer,
1453	.get_device_rev		= pscsi_get_device_rev,
1454	.get_device_type	= pscsi_get_device_type,
1455	.get_blocks		= pscsi_get_blocks,
1456};
1457
1458static int __init pscsi_module_init(void)
1459{
1460	return transport_subsystem_register(&pscsi_template);
1461}
1462
1463static void pscsi_module_exit(void)
1464{
1465	transport_subsystem_release(&pscsi_template);
1466}
1467
1468MODULE_DESCRIPTION("TCM PSCSI subsystem plugin");
1469MODULE_AUTHOR("nab@Linux-iSCSI.org");
1470MODULE_LICENSE("GPL");
1471
1472module_init(pscsi_module_init);
1473module_exit(pscsi_module_exit);