1 /*******************************************************************************
2 * Filename: target_core_pscsi.c
4 * This file contains the generic target mode <-> Linux SCSI subsystem plugin.
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
11 * Nicholas A. Bellinger <nab@kernel.org>
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.
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.
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.
27 ******************************************************************************/
29 #include <linux/string.h>
30 #include <linux/parser.h>
31 #include <linux/timer.h>
32 #include <linux/blkdev.h>
33 #include <linux/blk_types.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/genhd.h>
37 #include <linux/cdrom.h>
38 #include <linux/ratelimit.h>
39 #include <linux/module.h>
40 #include <asm/unaligned.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_device.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_host.h>
46 #include <scsi/scsi_tcq.h>
48 #include <target/target_core_base.h>
49 #include <target/target_core_backend.h>
51 #include "target_core_alua.h"
52 #include "target_core_pscsi.h"
54 #define ISPRINT(a) ((a >= ' ') && (a <= '~'))
56 static inline struct pscsi_dev_virt *PSCSI_DEV(struct se_device *dev)
58 return container_of(dev, struct pscsi_dev_virt, dev);
61 static struct se_subsystem_api pscsi_template;
63 static int pscsi_execute_cmd(struct se_cmd *cmd);
64 static void pscsi_req_done(struct request *, int);
66 /* pscsi_attach_hba():
68 * pscsi_get_sh() used scsi_host_lookup() to locate struct Scsi_Host.
69 * from the passed SCSI Host ID.
71 static int pscsi_attach_hba(struct se_hba *hba, u32 host_id)
73 struct pscsi_hba_virt *phv;
75 phv = kzalloc(sizeof(struct pscsi_hba_virt), GFP_KERNEL);
77 pr_err("Unable to allocate struct pscsi_hba_virt\n");
80 phv->phv_host_id = host_id;
81 phv->phv_mode = PHV_VIRTUAL_HOST_ID;
85 pr_debug("CORE_HBA[%d] - TCM SCSI HBA Driver %s on"
86 " Generic Target Core Stack %s\n", hba->hba_id,
87 PSCSI_VERSION, TARGET_CORE_MOD_VERSION);
88 pr_debug("CORE_HBA[%d] - Attached SCSI HBA to Generic\n",
94 static void pscsi_detach_hba(struct se_hba *hba)
96 struct pscsi_hba_virt *phv = hba->hba_ptr;
97 struct Scsi_Host *scsi_host = phv->phv_lld_host;
100 scsi_host_put(scsi_host);
102 pr_debug("CORE_HBA[%d] - Detached SCSI HBA: %s from"
103 " Generic Target Core\n", hba->hba_id,
104 (scsi_host->hostt->name) ? (scsi_host->hostt->name) :
107 pr_debug("CORE_HBA[%d] - Detached Virtual SCSI HBA"
108 " from Generic Target Core\n", hba->hba_id);
114 static int pscsi_pmode_enable_hba(struct se_hba *hba, unsigned long mode_flag)
116 struct pscsi_hba_virt *phv = hba->hba_ptr;
117 struct Scsi_Host *sh = phv->phv_lld_host;
119 * Release the struct Scsi_Host
125 phv->phv_lld_host = NULL;
126 phv->phv_mode = PHV_VIRTUAL_HOST_ID;
128 pr_debug("CORE_HBA[%d] - Disabled pSCSI HBA Passthrough"
129 " %s\n", hba->hba_id, (sh->hostt->name) ?
130 (sh->hostt->name) : "Unknown");
136 * Otherwise, locate struct Scsi_Host from the original passed
137 * pSCSI Host ID and enable for phba mode
139 sh = scsi_host_lookup(phv->phv_host_id);
141 pr_err("pSCSI: Unable to locate SCSI Host for"
142 " phv_host_id: %d\n", phv->phv_host_id);
146 phv->phv_lld_host = sh;
147 phv->phv_mode = PHV_LLD_SCSI_HOST_NO;
149 pr_debug("CORE_HBA[%d] - Enabled pSCSI HBA Passthrough %s\n",
150 hba->hba_id, (sh->hostt->name) ? (sh->hostt->name) : "Unknown");
155 static void pscsi_tape_read_blocksize(struct se_device *dev,
156 struct scsi_device *sdev)
158 unsigned char cdb[MAX_COMMAND_SIZE], *buf;
161 buf = kzalloc(12, GFP_KERNEL);
165 memset(cdb, 0, MAX_COMMAND_SIZE);
167 cdb[4] = 0x0c; /* 12 bytes */
169 ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf, 12, NULL,
175 * If MODE_SENSE still returns zero, set the default value to 1024.
177 sdev->sector_size = (buf[9] << 16) | (buf[10] << 8) | (buf[11]);
178 if (!sdev->sector_size)
179 sdev->sector_size = 1024;
185 pscsi_set_inquiry_info(struct scsi_device *sdev, struct t10_wwn *wwn)
189 if (sdev->inquiry_len < INQUIRY_LEN)
196 * Use sdev->inquiry from drivers/scsi/scsi_scan.c:scsi_alloc_sdev()
198 memcpy(&wwn->vendor[0], &buf[8], sizeof(wwn->vendor));
199 memcpy(&wwn->model[0], &buf[16], sizeof(wwn->model));
200 memcpy(&wwn->revision[0], &buf[32], sizeof(wwn->revision));
204 pscsi_get_inquiry_vpd_serial(struct scsi_device *sdev, struct t10_wwn *wwn)
206 unsigned char cdb[MAX_COMMAND_SIZE], *buf;
209 buf = kzalloc(INQUIRY_VPD_SERIAL_LEN, GFP_KERNEL);
213 memset(cdb, 0, MAX_COMMAND_SIZE);
215 cdb[1] = 0x01; /* Query VPD */
216 cdb[2] = 0x80; /* Unit Serial Number */
217 cdb[3] = (INQUIRY_VPD_SERIAL_LEN >> 8) & 0xff;
218 cdb[4] = (INQUIRY_VPD_SERIAL_LEN & 0xff);
220 ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
221 INQUIRY_VPD_SERIAL_LEN, NULL, HZ, 1, NULL);
225 snprintf(&wwn->unit_serial[0], INQUIRY_VPD_SERIAL_LEN, "%s", &buf[4]);
227 wwn->t10_dev->dev_flags |= DF_FIRMWARE_VPD_UNIT_SERIAL;
238 pscsi_get_inquiry_vpd_device_ident(struct scsi_device *sdev,
241 unsigned char cdb[MAX_COMMAND_SIZE], *buf, *page_83;
242 int ident_len, page_len, off = 4, ret;
245 buf = kzalloc(INQUIRY_VPD_SERIAL_LEN, GFP_KERNEL);
249 memset(cdb, 0, MAX_COMMAND_SIZE);
251 cdb[1] = 0x01; /* Query VPD */
252 cdb[2] = 0x83; /* Device Identifier */
253 cdb[3] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN >> 8) & 0xff;
254 cdb[4] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN & 0xff);
256 ret = scsi_execute_req(sdev, cdb, DMA_FROM_DEVICE, buf,
257 INQUIRY_VPD_DEVICE_IDENTIFIER_LEN,
262 page_len = (buf[2] << 8) | buf[3];
263 while (page_len > 0) {
264 /* Grab a pointer to the Identification descriptor */
266 ident_len = page_83[3];
268 pr_err("page_83[3]: identifier"
272 pr_debug("T10 VPD Identifier Length: %d\n", ident_len);
274 vpd = kzalloc(sizeof(struct t10_vpd), GFP_KERNEL);
276 pr_err("Unable to allocate memory for"
277 " struct t10_vpd\n");
280 INIT_LIST_HEAD(&vpd->vpd_list);
282 transport_set_vpd_proto_id(vpd, page_83);
283 transport_set_vpd_assoc(vpd, page_83);
285 if (transport_set_vpd_ident_type(vpd, page_83) < 0) {
286 off += (ident_len + 4);
287 page_len -= (ident_len + 4);
291 if (transport_set_vpd_ident(vpd, page_83) < 0) {
292 off += (ident_len + 4);
293 page_len -= (ident_len + 4);
298 list_add_tail(&vpd->vpd_list, &wwn->t10_vpd_list);
299 off += (ident_len + 4);
300 page_len -= (ident_len + 4);
307 static int pscsi_add_device_to_list(struct se_device *dev,
308 struct scsi_device *sd)
310 struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
311 struct request_queue *q = sd->request_queue;
315 if (!sd->queue_depth) {
316 sd->queue_depth = PSCSI_DEFAULT_QUEUEDEPTH;
318 pr_err("Set broken SCSI Device %d:%d:%d"
319 " queue_depth to %d\n", sd->channel, sd->id,
320 sd->lun, sd->queue_depth);
323 dev->dev_attrib.hw_block_size = sd->sector_size;
324 dev->dev_attrib.hw_max_sectors =
325 min_t(int, sd->host->max_sectors, queue_max_hw_sectors(q));
326 dev->dev_attrib.hw_queue_depth = sd->queue_depth;
329 * Setup our standard INQUIRY info into se_dev->t10_wwn
331 pscsi_set_inquiry_info(sd, &dev->t10_wwn);
334 * Locate VPD WWN Information used for various purposes within
335 * the Storage Engine.
337 if (!pscsi_get_inquiry_vpd_serial(sd, &dev->t10_wwn)) {
339 * If VPD Unit Serial returned GOOD status, try
340 * VPD Device Identification page (0x83).
342 pscsi_get_inquiry_vpd_device_ident(sd, &dev->t10_wwn);
346 * For TYPE_TAPE, attempt to determine blocksize with MODE_SENSE.
348 if (sd->type == TYPE_TAPE)
349 pscsi_tape_read_blocksize(dev, sd);
353 static struct se_device *pscsi_alloc_device(struct se_hba *hba,
356 struct pscsi_dev_virt *pdv;
358 pdv = kzalloc(sizeof(struct pscsi_dev_virt), GFP_KERNEL);
360 pr_err("Unable to allocate memory for struct pscsi_dev_virt\n");
364 pr_debug("PSCSI: Allocated pdv: %p for %s\n", pdv, name);
369 * Called with struct Scsi_Host->host_lock called.
371 static int pscsi_create_type_disk(struct se_device *dev, struct scsi_device *sd)
372 __releases(sh->host_lock)
374 struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
375 struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
376 struct Scsi_Host *sh = sd->host;
377 struct block_device *bd;
380 if (scsi_device_get(sd)) {
381 pr_err("scsi_device_get() failed for %d:%d:%d:%d\n",
382 sh->host_no, sd->channel, sd->id, sd->lun);
383 spin_unlock_irq(sh->host_lock);
386 spin_unlock_irq(sh->host_lock);
388 * Claim exclusive struct block_device access to struct scsi_device
389 * for TYPE_DISK using supplied udev_path
391 bd = blkdev_get_by_path(dev->udev_path,
392 FMODE_WRITE|FMODE_READ|FMODE_EXCL, pdv);
394 pr_err("pSCSI: blkdev_get_by_path() failed\n");
400 ret = pscsi_add_device_to_list(dev, sd);
402 blkdev_put(pdv->pdv_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
407 pr_debug("CORE_PSCSI[%d] - Added TYPE_DISK for %d:%d:%d:%d\n",
408 phv->phv_host_id, sh->host_no, sd->channel, sd->id, sd->lun);
413 * Called with struct Scsi_Host->host_lock called.
415 static int pscsi_create_type_rom(struct se_device *dev, struct scsi_device *sd)
416 __releases(sh->host_lock)
418 struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
419 struct Scsi_Host *sh = sd->host;
422 if (scsi_device_get(sd)) {
423 pr_err("scsi_device_get() failed for %d:%d:%d:%d\n",
424 sh->host_no, sd->channel, sd->id, sd->lun);
425 spin_unlock_irq(sh->host_lock);
428 spin_unlock_irq(sh->host_lock);
430 ret = pscsi_add_device_to_list(dev, sd);
435 pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
436 phv->phv_host_id, scsi_device_type(sd->type), sh->host_no,
437 sd->channel, sd->id, sd->lun);
443 * Called with struct Scsi_Host->host_lock called.
445 static int pscsi_create_type_other(struct se_device *dev,
446 struct scsi_device *sd)
447 __releases(sh->host_lock)
449 struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
450 struct Scsi_Host *sh = sd->host;
453 spin_unlock_irq(sh->host_lock);
454 ret = pscsi_add_device_to_list(dev, sd);
458 pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
459 phv->phv_host_id, scsi_device_type(sd->type), sh->host_no,
460 sd->channel, sd->id, sd->lun);
464 int pscsi_configure_device(struct se_device *dev)
466 struct se_hba *hba = dev->se_hba;
467 struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
468 struct scsi_device *sd;
469 struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
470 struct Scsi_Host *sh = phv->phv_lld_host;
471 int legacy_mode_enable = 0;
474 if (!(pdv->pdv_flags & PDF_HAS_CHANNEL_ID) ||
475 !(pdv->pdv_flags & PDF_HAS_TARGET_ID) ||
476 !(pdv->pdv_flags & PDF_HAS_LUN_ID)) {
477 pr_err("Missing scsi_channel_id=, scsi_target_id= and"
478 " scsi_lun_id= parameters\n");
483 * If not running in PHV_LLD_SCSI_HOST_NO mode, locate the
484 * struct Scsi_Host we will need to bring the TCM/pSCSI object online
487 if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
488 pr_err("pSCSI: Unable to locate struct"
489 " Scsi_Host for PHV_LLD_SCSI_HOST_NO\n");
493 * For the newer PHV_VIRTUAL_HOST_ID struct scsi_device
494 * reference, we enforce that udev_path has been set
496 if (!(dev->dev_flags & DF_USING_UDEV_PATH)) {
497 pr_err("pSCSI: udev_path attribute has not"
498 " been set before ENABLE=1\n");
502 * If no scsi_host_id= was passed for PHV_VIRTUAL_HOST_ID,
503 * use the original TCM hba ID to reference Linux/SCSI Host No
504 * and enable for PHV_LLD_SCSI_HOST_NO mode.
506 if (!(pdv->pdv_flags & PDF_HAS_VIRT_HOST_ID)) {
507 if (hba->dev_count) {
508 pr_err("pSCSI: Unable to set hba_mode"
509 " with active devices\n");
513 if (pscsi_pmode_enable_hba(hba, 1) != 1)
516 legacy_mode_enable = 1;
517 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
518 sh = phv->phv_lld_host;
520 sh = scsi_host_lookup(pdv->pdv_host_id);
522 pr_err("pSCSI: Unable to locate"
523 " pdv_host_id: %d\n", pdv->pdv_host_id);
528 if (phv->phv_mode == PHV_VIRTUAL_HOST_ID) {
529 pr_err("pSCSI: PHV_VIRTUAL_HOST_ID set while"
530 " struct Scsi_Host exists\n");
535 spin_lock_irq(sh->host_lock);
536 list_for_each_entry(sd, &sh->__devices, siblings) {
537 if ((pdv->pdv_channel_id != sd->channel) ||
538 (pdv->pdv_target_id != sd->id) ||
539 (pdv->pdv_lun_id != sd->lun))
542 * Functions will release the held struct scsi_host->host_lock
543 * before calling calling pscsi_add_device_to_list() to register
544 * struct scsi_device with target_core_mod.
548 ret = pscsi_create_type_disk(dev, sd);
551 ret = pscsi_create_type_rom(dev, sd);
554 ret = pscsi_create_type_other(dev, sd);
559 if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
561 else if (legacy_mode_enable) {
562 pscsi_pmode_enable_hba(hba, 0);
563 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
570 spin_unlock_irq(sh->host_lock);
572 pr_err("pSCSI: Unable to locate %d:%d:%d:%d\n", sh->host_no,
573 pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id);
575 if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
577 else if (legacy_mode_enable) {
578 pscsi_pmode_enable_hba(hba, 0);
579 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
585 static void pscsi_free_device(struct se_device *dev)
587 struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
588 struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
589 struct scsi_device *sd = pdv->pdv_sd;
593 * Release exclusive pSCSI internal struct block_device claim for
594 * struct scsi_device with TYPE_DISK from pscsi_create_type_disk()
596 if ((sd->type == TYPE_DISK) && pdv->pdv_bd) {
597 blkdev_put(pdv->pdv_bd,
598 FMODE_WRITE|FMODE_READ|FMODE_EXCL);
602 * For HBA mode PHV_LLD_SCSI_HOST_NO, release the reference
603 * to struct Scsi_Host now.
605 if ((phv->phv_mode == PHV_LLD_SCSI_HOST_NO) &&
606 (phv->phv_lld_host != NULL))
607 scsi_host_put(phv->phv_lld_host);
609 if ((sd->type == TYPE_DISK) || (sd->type == TYPE_ROM))
618 static void pscsi_transport_complete(struct se_cmd *cmd, struct scatterlist *sg,
619 unsigned char *sense_buffer)
621 struct pscsi_dev_virt *pdv = PSCSI_DEV(cmd->se_dev);
622 struct scsi_device *sd = pdv->pdv_sd;
624 struct pscsi_plugin_task *pt = cmd->priv;
627 * Special case for REPORT_LUNs handling where pscsi_plugin_task has
628 * not been allocated because TCM is handling the emulation directly.
633 cdb = &pt->pscsi_cdb[0];
634 result = pt->pscsi_result;
636 * Hack to make sure that Write-Protect modepage is set if R/O mode is
639 if (!cmd->se_deve || !cmd->data_length)
640 goto after_mode_sense;
642 if (((cdb[0] == MODE_SENSE) || (cdb[0] == MODE_SENSE_10)) &&
643 (status_byte(result) << 1) == SAM_STAT_GOOD) {
644 if (cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) {
645 unsigned char *buf = transport_kmap_data_sg(cmd);
647 if (cdb[0] == MODE_SENSE_10) {
648 if (!(buf[3] & 0x80))
651 if (!(buf[2] & 0x80))
655 transport_kunmap_data_sg(cmd);
660 if (sd->type != TYPE_TAPE || !cmd->data_length)
661 goto after_mode_select;
664 * Hack to correctly obtain the initiator requested blocksize for
665 * TYPE_TAPE. Since this value is dependent upon each tape media,
666 * struct scsi_device->sector_size will not contain the correct value
667 * by default, so we go ahead and set it so
668 * TRANSPORT(dev)->get_blockdev() returns the correct value to the
671 if (((cdb[0] == MODE_SELECT) || (cdb[0] == MODE_SELECT_10)) &&
672 (status_byte(result) << 1) == SAM_STAT_GOOD) {
677 buf = sg_virt(&sg[0]);
679 pr_err("Unable to get buf for scatterlist\n");
680 goto after_mode_select;
683 if (cdb[0] == MODE_SELECT)
686 bdl = (buf[6] << 8) | (buf[7]);
689 goto after_mode_select;
691 if (cdb[0] == MODE_SELECT)
692 blocksize = (buf[9] << 16) | (buf[10] << 8) |
695 blocksize = (buf[13] << 16) | (buf[14] << 8) |
698 sd->sector_size = blocksize;
702 if (sense_buffer && (status_byte(result) & CHECK_CONDITION)) {
703 memcpy(sense_buffer, pt->pscsi_sense, TRANSPORT_SENSE_BUFFER);
704 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
709 Opt_scsi_host_id, Opt_scsi_channel_id, Opt_scsi_target_id,
710 Opt_scsi_lun_id, Opt_err
713 static match_table_t tokens = {
714 {Opt_scsi_host_id, "scsi_host_id=%d"},
715 {Opt_scsi_channel_id, "scsi_channel_id=%d"},
716 {Opt_scsi_target_id, "scsi_target_id=%d"},
717 {Opt_scsi_lun_id, "scsi_lun_id=%d"},
721 static ssize_t pscsi_set_configfs_dev_params(struct se_device *dev,
722 const char *page, ssize_t count)
724 struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
725 struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
726 char *orig, *ptr, *opts;
727 substring_t args[MAX_OPT_ARGS];
728 int ret = 0, arg, token;
730 opts = kstrdup(page, GFP_KERNEL);
736 while ((ptr = strsep(&opts, ",\n")) != NULL) {
740 token = match_token(ptr, tokens, args);
742 case Opt_scsi_host_id:
743 if (phv->phv_mode == PHV_LLD_SCSI_HOST_NO) {
744 pr_err("PSCSI[%d]: Unable to accept"
745 " scsi_host_id while phv_mode =="
746 " PHV_LLD_SCSI_HOST_NO\n",
751 match_int(args, &arg);
752 pdv->pdv_host_id = arg;
753 pr_debug("PSCSI[%d]: Referencing SCSI Host ID:"
754 " %d\n", phv->phv_host_id, pdv->pdv_host_id);
755 pdv->pdv_flags |= PDF_HAS_VIRT_HOST_ID;
757 case Opt_scsi_channel_id:
758 match_int(args, &arg);
759 pdv->pdv_channel_id = arg;
760 pr_debug("PSCSI[%d]: Referencing SCSI Channel"
761 " ID: %d\n", phv->phv_host_id,
762 pdv->pdv_channel_id);
763 pdv->pdv_flags |= PDF_HAS_CHANNEL_ID;
765 case Opt_scsi_target_id:
766 match_int(args, &arg);
767 pdv->pdv_target_id = arg;
768 pr_debug("PSCSI[%d]: Referencing SCSI Target"
769 " ID: %d\n", phv->phv_host_id,
771 pdv->pdv_flags |= PDF_HAS_TARGET_ID;
773 case Opt_scsi_lun_id:
774 match_int(args, &arg);
775 pdv->pdv_lun_id = arg;
776 pr_debug("PSCSI[%d]: Referencing SCSI LUN ID:"
777 " %d\n", phv->phv_host_id, pdv->pdv_lun_id);
778 pdv->pdv_flags |= PDF_HAS_LUN_ID;
787 return (!ret) ? count : ret;
790 static ssize_t pscsi_show_configfs_dev_params(struct se_device *dev, char *b)
792 struct pscsi_hba_virt *phv = dev->se_hba->hba_ptr;
793 struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
794 struct scsi_device *sd = pdv->pdv_sd;
795 unsigned char host_id[16];
799 if (phv->phv_mode == PHV_VIRTUAL_HOST_ID)
800 snprintf(host_id, 16, "%d", pdv->pdv_host_id);
802 snprintf(host_id, 16, "PHBA Mode");
804 bl = sprintf(b, "SCSI Device Bus Location:"
805 " Channel ID: %d Target ID: %d LUN: %d Host ID: %s\n",
806 pdv->pdv_channel_id, pdv->pdv_target_id, pdv->pdv_lun_id,
810 bl += sprintf(b + bl, " ");
811 bl += sprintf(b + bl, "Vendor: ");
812 for (i = 0; i < 8; i++) {
813 if (ISPRINT(sd->vendor[i])) /* printable character? */
814 bl += sprintf(b + bl, "%c", sd->vendor[i]);
816 bl += sprintf(b + bl, " ");
818 bl += sprintf(b + bl, " Model: ");
819 for (i = 0; i < 16; i++) {
820 if (ISPRINT(sd->model[i])) /* printable character ? */
821 bl += sprintf(b + bl, "%c", sd->model[i]);
823 bl += sprintf(b + bl, " ");
825 bl += sprintf(b + bl, " Rev: ");
826 for (i = 0; i < 4; i++) {
827 if (ISPRINT(sd->rev[i])) /* printable character ? */
828 bl += sprintf(b + bl, "%c", sd->rev[i]);
830 bl += sprintf(b + bl, " ");
832 bl += sprintf(b + bl, "\n");
837 static void pscsi_bi_endio(struct bio *bio, int error)
842 static inline struct bio *pscsi_get_bio(int sg_num)
846 * Use bio_malloc() following the comment in for bio -> struct request
847 * in block/blk-core.c:blk_make_request()
849 bio = bio_kmalloc(GFP_KERNEL, sg_num);
851 pr_err("PSCSI: bio_kmalloc() failed\n");
854 bio->bi_end_io = pscsi_bi_endio;
859 static int pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl,
860 u32 sgl_nents, enum dma_data_direction data_direction,
863 struct pscsi_dev_virt *pdv = PSCSI_DEV(cmd->se_dev);
864 struct bio *bio = NULL, *tbio = NULL;
866 struct scatterlist *sg;
867 u32 data_len = cmd->data_length, i, len, bytes, off;
868 int nr_pages = (cmd->data_length + sgl[0].offset +
869 PAGE_SIZE - 1) >> PAGE_SHIFT;
871 int rw = (data_direction == DMA_TO_DEVICE);
875 pr_debug("PSCSI: nr_pages: %d\n", nr_pages);
877 for_each_sg(sgl, sg, sgl_nents, i) {
882 pr_debug("PSCSI: i: %d page: %p len: %d off: %d\n", i,
885 while (len > 0 && data_len > 0) {
886 bytes = min_t(unsigned int, len, PAGE_SIZE - off);
887 bytes = min(bytes, data_len);
890 nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
893 * Calls bio_kmalloc() and sets bio->bi_end_io()
895 bio = pscsi_get_bio(nr_vecs);
900 bio->bi_rw |= REQ_WRITE;
902 pr_debug("PSCSI: Allocated bio: %p,"
903 " dir: %s nr_vecs: %d\n", bio,
904 (rw) ? "rw" : "r", nr_vecs);
906 * Set *hbio pointer to handle the case:
907 * nr_pages > BIO_MAX_PAGES, where additional
908 * bios need to be added to complete a given
914 tbio = tbio->bi_next = bio;
917 pr_debug("PSCSI: Calling bio_add_pc_page() i: %d"
918 " bio: %p page: %p len: %d off: %d\n", i, bio,
921 rc = bio_add_pc_page(pdv->pdv_sd->request_queue,
922 bio, page, bytes, off);
926 pr_debug("PSCSI: bio->bi_vcnt: %d nr_vecs: %d\n",
927 bio->bi_vcnt, nr_vecs);
929 if (bio->bi_vcnt > nr_vecs) {
930 pr_debug("PSCSI: Reached bio->bi_vcnt max:"
931 " %d i: %d bio: %p, allocating another"
932 " bio\n", bio->bi_vcnt, i, bio);
934 * Clear the pointer so that another bio will
935 * be allocated with pscsi_get_bio() above, the
936 * current bio has already been set *tbio and
953 *hbio = (*hbio)->bi_next;
955 bio_endio(bio, 0); /* XXX: should be error */
957 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
962 * Clear a lun set in the cdb if the initiator talking to use spoke
963 * and old standards version, as we can't assume the underlying device
964 * won't choke up on it.
966 static inline void pscsi_clear_cdb_lun(unsigned char *cdb)
969 case READ_10: /* SBC - RDProtect */
970 case READ_12: /* SBC - RDProtect */
971 case READ_16: /* SBC - RDProtect */
972 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
973 case VERIFY: /* SBC - VRProtect */
974 case VERIFY_16: /* SBC - VRProtect */
975 case WRITE_VERIFY: /* SBC - VRProtect */
976 case WRITE_VERIFY_12: /* SBC - VRProtect */
977 case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
980 cdb[1] &= 0x1f; /* clear logical unit number */
985 static int pscsi_parse_cdb(struct se_cmd *cmd)
987 unsigned char *cdb = cmd->t_task_cdb;
988 unsigned int dummy_size;
991 if (cmd->se_cmd_flags & SCF_BIDI) {
992 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
993 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
997 pscsi_clear_cdb_lun(cdb);
1000 * For REPORT LUNS we always need to emulate the response, for everything
1001 * else the default for pSCSI is to pass the command to the underlying
1002 * LLD / physical hardware.
1006 ret = spc_parse_cdb(cmd, &dummy_size);
1019 cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
1022 cmd->execute_cmd = pscsi_execute_cmd;
1029 static int pscsi_execute_cmd(struct se_cmd *cmd)
1031 struct scatterlist *sgl = cmd->t_data_sg;
1032 u32 sgl_nents = cmd->t_data_nents;
1033 enum dma_data_direction data_direction = cmd->data_direction;
1034 struct pscsi_dev_virt *pdv = PSCSI_DEV(cmd->se_dev);
1035 struct pscsi_plugin_task *pt;
1036 struct request *req;
1041 * Dynamically alloc cdb space, since it may be larger than
1042 * TCM_MAX_COMMAND_SIZE
1044 pt = kzalloc(sizeof(*pt) + scsi_command_size(cmd->t_task_cdb), GFP_KERNEL);
1046 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1051 memcpy(pt->pscsi_cdb, cmd->t_task_cdb,
1052 scsi_command_size(cmd->t_task_cdb));
1055 req = blk_get_request(pdv->pdv_sd->request_queue,
1056 (data_direction == DMA_TO_DEVICE),
1058 if (!req || IS_ERR(req)) {
1059 pr_err("PSCSI: blk_get_request() failed: %ld\n",
1060 req ? IS_ERR(req) : -ENOMEM);
1061 cmd->scsi_sense_reason =
1062 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1066 BUG_ON(!cmd->data_length);
1068 ret = pscsi_map_sg(cmd, sgl, sgl_nents, data_direction, &hbio);
1070 cmd->scsi_sense_reason =
1071 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1075 req = blk_make_request(pdv->pdv_sd->request_queue, hbio,
1078 pr_err("pSCSI: blk_make_request() failed\n");
1083 req->cmd_type = REQ_TYPE_BLOCK_PC;
1084 req->end_io = pscsi_req_done;
1085 req->end_io_data = cmd;
1086 req->cmd_len = scsi_command_size(pt->pscsi_cdb);
1087 req->cmd = &pt->pscsi_cdb[0];
1088 req->sense = &pt->pscsi_sense[0];
1090 if (pdv->pdv_sd->type == TYPE_DISK)
1091 req->timeout = PS_TIMEOUT_DISK;
1093 req->timeout = PS_TIMEOUT_OTHER;
1094 req->retries = PS_RETRY;
1096 blk_execute_rq_nowait(pdv->pdv_sd->request_queue, NULL, req,
1097 (cmd->sam_task_attr == MSG_HEAD_TAG),
1104 struct bio *bio = hbio;
1105 hbio = hbio->bi_next;
1106 bio->bi_next = NULL;
1107 bio_endio(bio, 0); /* XXX: should be error */
1109 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1115 /* pscsi_get_device_rev():
1119 static u32 pscsi_get_device_rev(struct se_device *dev)
1121 struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
1122 struct scsi_device *sd = pdv->pdv_sd;
1124 return (sd->scsi_level - 1) ? sd->scsi_level - 1 : 1;
1127 /* pscsi_get_device_type():
1131 static u32 pscsi_get_device_type(struct se_device *dev)
1133 struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
1134 struct scsi_device *sd = pdv->pdv_sd;
1139 static sector_t pscsi_get_blocks(struct se_device *dev)
1141 struct pscsi_dev_virt *pdv = PSCSI_DEV(dev);
1143 if (pdv->pdv_bd && pdv->pdv_bd->bd_part)
1144 return pdv->pdv_bd->bd_part->nr_sects;
1150 static void pscsi_req_done(struct request *req, int uptodate)
1152 struct se_cmd *cmd = req->end_io_data;
1153 struct pscsi_plugin_task *pt = cmd->priv;
1155 pt->pscsi_result = req->errors;
1156 pt->pscsi_resid = req->resid_len;
1158 cmd->scsi_status = status_byte(pt->pscsi_result) << 1;
1159 if (cmd->scsi_status) {
1160 pr_debug("PSCSI Status Byte exception at cmd: %p CDB:"
1161 " 0x%02x Result: 0x%08x\n", cmd, pt->pscsi_cdb[0],
1165 switch (host_byte(pt->pscsi_result)) {
1167 target_complete_cmd(cmd, cmd->scsi_status);
1170 pr_debug("PSCSI Host Byte exception at cmd: %p CDB:"
1171 " 0x%02x Result: 0x%08x\n", cmd, pt->pscsi_cdb[0],
1173 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1174 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
1178 __blk_put_request(req->q, req);
1182 static struct se_subsystem_api pscsi_template = {
1184 .owner = THIS_MODULE,
1185 .transport_type = TRANSPORT_PLUGIN_PHBA_PDEV,
1186 .attach_hba = pscsi_attach_hba,
1187 .detach_hba = pscsi_detach_hba,
1188 .pmode_enable_hba = pscsi_pmode_enable_hba,
1189 .alloc_device = pscsi_alloc_device,
1190 .configure_device = pscsi_configure_device,
1191 .free_device = pscsi_free_device,
1192 .transport_complete = pscsi_transport_complete,
1193 .parse_cdb = pscsi_parse_cdb,
1194 .set_configfs_dev_params = pscsi_set_configfs_dev_params,
1195 .show_configfs_dev_params = pscsi_show_configfs_dev_params,
1196 .get_device_rev = pscsi_get_device_rev,
1197 .get_device_type = pscsi_get_device_type,
1198 .get_blocks = pscsi_get_blocks,
1201 static int __init pscsi_module_init(void)
1203 return transport_subsystem_register(&pscsi_template);
1206 static void pscsi_module_exit(void)
1208 transport_subsystem_release(&pscsi_template);
1211 MODULE_DESCRIPTION("TCM PSCSI subsystem plugin");
1212 MODULE_AUTHOR("nab@Linux-iSCSI.org");
1213 MODULE_LICENSE("GPL");
1215 module_init(pscsi_module_init);
1216 module_exit(pscsi_module_exit);