29 #include <linux/string.h>
34 #include <linux/slab.h>
37 #include <linux/cdrom.h>
39 #include <linux/module.h>
40 #include <asm/unaligned.h>
42 #include <scsi/scsi.h>
54 #define ISPRINT(a) ((a >= ' ') && (a <= '~'))
58 static int pscsi_execute_cmd(
struct se_cmd *
cmd);
59 static void pscsi_req_done(
struct request *,
int);
72 pr_err(
"Unable to allocate struct pscsi_hba_virt\n");
80 pr_debug(
"CORE_HBA[%d] - TCM SCSI HBA Driver %s on"
81 " Generic Target Core Stack %s\n", hba->
hba_id,
83 pr_debug(
"CORE_HBA[%d] - Attached SCSI HBA to Generic\n",
89 static void pscsi_detach_hba(
struct se_hba *hba)
97 pr_debug(
"CORE_HBA[%d] - Detached SCSI HBA: %s from"
98 " Generic Target Core\n", hba->
hba_id,
99 (scsi_host->
hostt->name) ? (scsi_host->
hostt->name) :
102 pr_debug(
"CORE_HBA[%d] - Detached Virtual SCSI HBA"
103 " from Generic Target Core\n", hba->
hba_id);
109 static int pscsi_pmode_enable_hba(
struct se_hba *hba,
unsigned long mode_flag)
123 pr_debug(
"CORE_HBA[%d] - Disabled pSCSI HBA Passthrough"
125 (sh->
hostt->name) :
"Unknown");
136 pr_err(
"pSCSI: Unable to locate SCSI Host for"
144 pr_debug(
"CORE_HBA[%d] - Enabled pSCSI HBA Passthrough %s\n",
150 static void pscsi_tape_read_blocksize(
struct se_device *
dev,
172 sdev->
sector_size = (buf[9] << 16) | (buf[10] << 8) | (buf[11]);
233 pscsi_get_inquiry_vpd_device_ident(
struct scsi_device *sdev,
237 int ident_len, page_len, off = 4,
ret;
257 page_len = (buf[2] << 8) | buf[3];
258 while (page_len > 0) {
261 ident_len = page_83[3];
263 pr_err(
"page_83[3]: identifier"
267 pr_debug(
"T10 VPD Identifier Length: %d\n", ident_len);
271 pr_err(
"Unable to allocate memory for"
272 " struct t10_vpd\n");
281 off += (ident_len + 4);
282 page_len -= (ident_len + 4);
287 off += (ident_len + 4);
288 page_len -= (ident_len + 4);
294 off += (ident_len + 4);
295 page_len -= (ident_len + 4);
306 static struct se_device *pscsi_add_device_to_list(
316 struct queue_limits *limits;
323 pr_err(
"Set broken SCSI Device %d:%d:%d"
324 " queue_depth to %d\n", sd->
channel, sd->
id,
332 limits = &dev_limits.limits;
334 limits->max_hw_sectors =
min_t(
int, sd->
host->max_sectors, queue_max_hw_sectors(q));
335 limits->max_sectors =
min_t(
int, sd->
host->max_sectors, queue_max_sectors(q));
341 pscsi_set_inquiry_info(sd, &se_dev->
t10_wwn);
358 se_dev, dev_flags, pdv,
369 if (!pscsi_get_inquiry_vpd_serial(sd, &se_dev->
t10_wwn)) {
374 pscsi_get_inquiry_vpd_device_ident(sd, &se_dev->
t10_wwn);
381 pscsi_tape_read_blocksize(dev, sd);
385 static void *pscsi_allocate_virtdevice(
struct se_hba *hba,
const char *
name)
391 pr_err(
"Unable to allocate memory for struct pscsi_dev_virt\n");
396 pr_debug(
"PSCSI: Allocated pdv: %p for %s\n", pdv, name);
403 static struct se_device *pscsi_create_type_disk(
417 pr_err(
"scsi_device_get() failed for %d:%d:%d:%d\n",
430 pr_err(
"pSCSI: blkdev_get_by_path() failed\n");
436 dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
442 pr_debug(
"CORE_PSCSI[%d] - Added TYPE_DISK for %d:%d:%d:%d\n",
451 static struct se_device *pscsi_create_type_rom(
464 pr_err(
"scsi_device_get() failed for %d:%d:%d:%d\n",
471 dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
476 pr_debug(
"CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
486 static struct se_device *pscsi_create_type_other(
499 dev = pscsi_add_device_to_list(hba, se_dev, pdv, sd, dev_flags);
503 pr_debug(
"CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%d\n",
510 static struct se_device *pscsi_create_virtdevice(
520 int legacy_mode_enable = 0;
523 pr_err(
"Unable to locate struct pscsi_dev_virt"
533 pr_err(
"pSCSI: Unable to locate struct"
534 " Scsi_Host for PHV_LLD_SCSI_HOST_NO\n");
542 pr_err(
"pSCSI: udev_path attribute has not"
543 " been set before ENABLE=1\n");
554 pr_err(
"pSCSI: Unable to set hba_mode"
555 " with active devices\n");
561 if (pscsi_pmode_enable_hba(hba, 1) != 1)
564 legacy_mode_enable = 1;
570 pr_err(
"pSCSI: Unable to locate"
577 pr_err(
"pSCSI: PHV_VIRTUAL_HOST_ID set while"
578 " struct Scsi_Host exists\n");
596 dev = pscsi_create_type_disk(sd, pdv, se_dev, hba);
599 dev = pscsi_create_type_rom(sd, pdv, se_dev, hba);
602 dev = pscsi_create_type_other(sd, pdv, se_dev, hba);
609 else if (legacy_mode_enable) {
610 pscsi_pmode_enable_hba(hba, 0);
625 else if (legacy_mode_enable) {
626 pscsi_pmode_enable_hba(hba, 0);
637 static void pscsi_free_device(
void *p)
671 unsigned char *sense_buffer)
692 goto after_mode_sense;
700 if (!(buf[3] & 0x80))
703 if (!(buf[2] & 0x80))
713 goto after_mode_select;
729 buf = sg_virt(&sg[0]);
731 pr_err(
"Unable to get buf for scatterlist\n");
732 goto after_mode_select;
738 bdl = (buf[6] << 8) | (buf[7]);
741 goto after_mode_select;
744 blocksize = (buf[9] << 16) | (buf[10] << 8) |
747 blocksize = (buf[13] << 16) | (buf[14] << 8) |
773 static ssize_t pscsi_set_configfs_dev_params(
struct se_hba *hba,
790 while ((ptr =
strsep(&opts,
",\n")) !=
NULL) {
798 pr_err(
"PSCSI[%d]: Unable to accept"
799 " scsi_host_id while phv_mode =="
800 " PHV_LLD_SCSI_HOST_NO\n",
807 pr_debug(
"PSCSI[%d]: Referencing SCSI Host ID:"
814 pr_debug(
"PSCSI[%d]: Referencing SCSI Channel"
822 pr_debug(
"PSCSI[%d]: Referencing SCSI Target"
830 pr_debug(
"PSCSI[%d]: Referencing SCSI LUN ID:"
841 return (!ret) ? count :
ret;
844 static ssize_t pscsi_check_configfs_dev_params(
853 pr_err(
"Missing scsi_channel_id=, scsi_target_id= and"
854 " scsi_lun_id= parameters\n");
861 static ssize_t pscsi_show_configfs_dev_params(
struct se_hba *hba,
868 unsigned char host_id[16];
877 bl =
sprintf(b,
"SCSI Device Bus Location:"
878 " Channel ID: %d Target ID: %d LUN: %d Host ID: %s\n",
884 bl +=
sprintf(b + bl,
"Vendor: ");
885 for (i = 0; i < 8; i++) {
891 bl +=
sprintf(b + bl,
" Model: ");
892 for (i = 0; i < 16; i++) {
898 bl +=
sprintf(b + bl,
" Rev: ");
899 for (i = 0; i < 4; i++) {
910 static void pscsi_bi_endio(
struct bio *bio,
int error)
915 static inline struct bio *pscsi_get_bio(
int sg_num)
924 pr_err(
"PSCSI: bio_kmalloc() failed\n");
927 bio->bi_end_io = pscsi_bi_endio;
937 struct bio *bio =
NULL, *tbio =
NULL;
948 pr_debug(
"PSCSI: nr_pages: %d\n", nr_pages);
955 pr_debug(
"PSCSI: i: %d page: %p len: %d off: %d\n", i,
958 while (len > 0 && data_len > 0) {
960 bytes =
min(bytes, data_len);
963 nr_vecs =
min_t(
int, BIO_MAX_PAGES, nr_pages);
968 bio = pscsi_get_bio(nr_vecs);
975 pr_debug(
"PSCSI: Allocated bio: %p,"
976 " dir: %s nr_vecs: %d\n", bio,
977 (rw) ?
"rw" :
"r", nr_vecs);
987 tbio = tbio->bi_next = bio;
990 pr_debug(
"PSCSI: Calling bio_add_pc_page() i: %d"
991 " bio: %p page: %p len: %d off: %d\n", i, bio,
995 bio, page, bytes, off);
999 pr_debug(
"PSCSI: bio->bi_vcnt: %d nr_vecs: %d\n",
1000 bio->bi_vcnt, nr_vecs);
1002 if (bio->bi_vcnt > nr_vecs) {
1003 pr_debug(
"PSCSI: Reached bio->bi_vcnt max:"
1004 " %d i: %d bio: %p, allocating another"
1005 " bio\n", bio->bi_vcnt, i, bio);
1026 *hbio = (*hbio)->bi_next;
1027 bio->bi_next =
NULL;
1039 static inline void pscsi_clear_cdb_lun(
unsigned char *cdb)
1058 static int pscsi_parse_cdb(
struct se_cmd *cmd)
1061 unsigned int dummy_size;
1070 pscsi_clear_cdb_lun(cdb);
1102 static int pscsi_execute_cmd(
struct se_cmd *cmd)
1131 if (!req || IS_ERR(req)) {
1132 pr_err(
"PSCSI: blk_get_request() failed: %ld\n",
1133 req ? IS_ERR(req) : -
ENOMEM);
1141 ret = pscsi_map_sg(cmd, sgl, sgl_nents, data_direction, &hbio);
1151 pr_err(
"pSCSI: blk_make_request() failed\n");
1156 req->cmd_type = REQ_TYPE_BLOCK_PC;
1157 req->end_io = pscsi_req_done;
1158 req->end_io_data =
cmd;
1159 req->cmd_len = scsi_command_size(pt->
pscsi_cdb);
1177 struct bio *bio = hbio;
1178 hbio = hbio->bi_next;
1179 bio->bi_next =
NULL;
1204 static u32 pscsi_get_device_type(
struct se_device *dev)
1217 return pdv->
pdv_bd->bd_part->nr_sects;
1223 static void pscsi_req_done(
struct request *req,
int uptodate)
1225 struct se_cmd *cmd = req->end_io_data;
1233 pr_debug(
"PSCSI Status Byte exception at cmd: %p CDB:"
1234 " 0x%02x Result: 0x%08x\n", cmd, pt->
pscsi_cdb[0],
1243 pr_debug(
"PSCSI Host Byte exception at cmd: %p CDB:"
1244 " 0x%02x Result: 0x%08x\n", cmd, pt->
pscsi_cdb[0],
1259 .attach_hba = pscsi_attach_hba,
1260 .detach_hba = pscsi_detach_hba,
1261 .pmode_enable_hba = pscsi_pmode_enable_hba,
1262 .allocate_virtdevice = pscsi_allocate_virtdevice,
1263 .create_virtdevice = pscsi_create_virtdevice,
1264 .free_device = pscsi_free_device,
1265 .transport_complete = pscsi_transport_complete,
1266 .parse_cdb = pscsi_parse_cdb,
1267 .check_configfs_dev_params = pscsi_check_configfs_dev_params,
1268 .set_configfs_dev_params = pscsi_set_configfs_dev_params,
1269 .show_configfs_dev_params = pscsi_show_configfs_dev_params,
1270 .get_device_rev = pscsi_get_device_rev,
1271 .get_device_type = pscsi_get_device_type,
1272 .get_blocks = pscsi_get_blocks,
1275 static int __init pscsi_module_init(
void)
1280 static void pscsi_module_exit(
void)