1 /*******************************************************************************
2 * Filename: target_core_pscsi.c
4 * This file contains the generic target mode <-> Linux SCSI subsystem plugin.
6 * (c) Copyright 2003-2013 Datera, Inc.
8 * Nicholas A. Bellinger <nab@kernel.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 ******************************************************************************/
26 #include <linux/string.h>
27 #include <linux/parser.h>
28 #include <linux/timer.h>
29 #include <linux/blkdev.h>
30 #include <linux/blk_types.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/genhd.h>
34 #include <linux/cdrom.h>
35 #include <linux/ratelimit.h>
36 #include <linux/module.h>
37 #include <asm/unaligned.h>
39 #include <scsi/scsi.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_host.h>
43 #include <scsi/scsi_tcq.h>
45 #include <target/target_core_base.h>
46 #include <target/target_core_backend.h>
47 #include <target/target_core_backend_configfs.h>
49 #include "target_core_alua.h"
50 #include "target_core_internal.h"
51 #include "target_core_pscsi.h"
53 #define ISPRINT(a) ((a >= ' ') && (a <= '~'))
55 static inline struct pscsi_dev_virt
*PSCSI_DEV(struct se_device
*dev
)
57 return container_of(dev
, struct pscsi_dev_virt
, dev
);
60 static struct se_subsystem_api pscsi_template
;
62 static sense_reason_t
pscsi_execute_cmd(struct se_cmd
*cmd
);
63 static void pscsi_req_done(struct request
*, int);
65 /* pscsi_attach_hba():
67 * pscsi_get_sh() used scsi_host_lookup() to locate struct Scsi_Host.
68 * from the passed SCSI Host ID.
70 static int pscsi_attach_hba(struct se_hba
*hba
, u32 host_id
)
72 struct pscsi_hba_virt
*phv
;
74 phv
= kzalloc(sizeof(struct pscsi_hba_virt
), GFP_KERNEL
);
76 pr_err("Unable to allocate struct pscsi_hba_virt\n");
79 phv
->phv_host_id
= host_id
;
80 phv
->phv_mode
= PHV_VIRTUAL_HOST_ID
;
84 pr_debug("CORE_HBA[%d] - TCM SCSI HBA Driver %s on"
85 " Generic Target Core Stack %s\n", hba
->hba_id
,
86 PSCSI_VERSION
, TARGET_CORE_MOD_VERSION
);
87 pr_debug("CORE_HBA[%d] - Attached SCSI HBA to Generic\n",
93 static void pscsi_detach_hba(struct se_hba
*hba
)
95 struct pscsi_hba_virt
*phv
= hba
->hba_ptr
;
96 struct Scsi_Host
*scsi_host
= phv
->phv_lld_host
;
99 scsi_host_put(scsi_host
);
101 pr_debug("CORE_HBA[%d] - Detached SCSI HBA: %s from"
102 " Generic Target Core\n", hba
->hba_id
,
103 (scsi_host
->hostt
->name
) ? (scsi_host
->hostt
->name
) :
106 pr_debug("CORE_HBA[%d] - Detached Virtual SCSI HBA"
107 " from Generic Target Core\n", hba
->hba_id
);
113 static int pscsi_pmode_enable_hba(struct se_hba
*hba
, unsigned long mode_flag
)
115 struct pscsi_hba_virt
*phv
= hba
->hba_ptr
;
116 struct Scsi_Host
*sh
= phv
->phv_lld_host
;
118 * Release the struct Scsi_Host
124 phv
->phv_lld_host
= NULL
;
125 phv
->phv_mode
= PHV_VIRTUAL_HOST_ID
;
127 pr_debug("CORE_HBA[%d] - Disabled pSCSI HBA Passthrough"
128 " %s\n", hba
->hba_id
, (sh
->hostt
->name
) ?
129 (sh
->hostt
->name
) : "Unknown");
135 * Otherwise, locate struct Scsi_Host from the original passed
136 * pSCSI Host ID and enable for phba mode
138 sh
= scsi_host_lookup(phv
->phv_host_id
);
140 pr_err("pSCSI: Unable to locate SCSI Host for"
141 " phv_host_id: %d\n", phv
->phv_host_id
);
145 phv
->phv_lld_host
= sh
;
146 phv
->phv_mode
= PHV_LLD_SCSI_HOST_NO
;
148 pr_debug("CORE_HBA[%d] - Enabled pSCSI HBA Passthrough %s\n",
149 hba
->hba_id
, (sh
->hostt
->name
) ? (sh
->hostt
->name
) : "Unknown");
154 static void pscsi_tape_read_blocksize(struct se_device
*dev
,
155 struct scsi_device
*sdev
)
157 unsigned char cdb
[MAX_COMMAND_SIZE
], *buf
;
160 buf
= kzalloc(12, GFP_KERNEL
);
164 memset(cdb
, 0, MAX_COMMAND_SIZE
);
166 cdb
[4] = 0x0c; /* 12 bytes */
168 ret
= scsi_execute_req(sdev
, cdb
, DMA_FROM_DEVICE
, buf
, 12, NULL
,
174 * If MODE_SENSE still returns zero, set the default value to 1024.
176 sdev
->sector_size
= (buf
[9] << 16) | (buf
[10] << 8) | (buf
[11]);
177 if (!sdev
->sector_size
)
178 sdev
->sector_size
= 1024;
184 pscsi_set_inquiry_info(struct scsi_device
*sdev
, struct t10_wwn
*wwn
)
188 if (sdev
->inquiry_len
< INQUIRY_LEN
)
195 * Use sdev->inquiry from drivers/scsi/scsi_scan.c:scsi_alloc_sdev()
197 memcpy(&wwn
->vendor
[0], &buf
[8], sizeof(wwn
->vendor
));
198 memcpy(&wwn
->model
[0], &buf
[16], sizeof(wwn
->model
));
199 memcpy(&wwn
->revision
[0], &buf
[32], sizeof(wwn
->revision
));
203 pscsi_get_inquiry_vpd_serial(struct scsi_device
*sdev
, struct t10_wwn
*wwn
)
205 unsigned char cdb
[MAX_COMMAND_SIZE
], *buf
;
208 buf
= kzalloc(INQUIRY_VPD_SERIAL_LEN
, GFP_KERNEL
);
212 memset(cdb
, 0, MAX_COMMAND_SIZE
);
214 cdb
[1] = 0x01; /* Query VPD */
215 cdb
[2] = 0x80; /* Unit Serial Number */
216 cdb
[3] = (INQUIRY_VPD_SERIAL_LEN
>> 8) & 0xff;
217 cdb
[4] = (INQUIRY_VPD_SERIAL_LEN
& 0xff);
219 ret
= scsi_execute_req(sdev
, cdb
, DMA_FROM_DEVICE
, buf
,
220 INQUIRY_VPD_SERIAL_LEN
, NULL
, HZ
, 1, NULL
);
224 snprintf(&wwn
->unit_serial
[0], INQUIRY_VPD_SERIAL_LEN
, "%s", &buf
[4]);
226 wwn
->t10_dev
->dev_flags
|= DF_FIRMWARE_VPD_UNIT_SERIAL
;
237 pscsi_get_inquiry_vpd_device_ident(struct scsi_device
*sdev
,
240 unsigned char cdb
[MAX_COMMAND_SIZE
], *buf
, *page_83
;
241 int ident_len
, page_len
, off
= 4, ret
;
244 buf
= kzalloc(INQUIRY_VPD_SERIAL_LEN
, GFP_KERNEL
);
248 memset(cdb
, 0, MAX_COMMAND_SIZE
);
250 cdb
[1] = 0x01; /* Query VPD */
251 cdb
[2] = 0x83; /* Device Identifier */
252 cdb
[3] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN
>> 8) & 0xff;
253 cdb
[4] = (INQUIRY_VPD_DEVICE_IDENTIFIER_LEN
& 0xff);
255 ret
= scsi_execute_req(sdev
, cdb
, DMA_FROM_DEVICE
, buf
,
256 INQUIRY_VPD_DEVICE_IDENTIFIER_LEN
,
261 page_len
= (buf
[2] << 8) | buf
[3];
262 while (page_len
> 0) {
263 /* Grab a pointer to the Identification descriptor */
265 ident_len
= page_83
[3];
267 pr_err("page_83[3]: identifier"
271 pr_debug("T10 VPD Identifier Length: %d\n", ident_len
);
273 vpd
= kzalloc(sizeof(struct t10_vpd
), GFP_KERNEL
);
275 pr_err("Unable to allocate memory for"
276 " struct t10_vpd\n");
279 INIT_LIST_HEAD(&vpd
->vpd_list
);
281 transport_set_vpd_proto_id(vpd
, page_83
);
282 transport_set_vpd_assoc(vpd
, page_83
);
284 if (transport_set_vpd_ident_type(vpd
, page_83
) < 0) {
285 off
+= (ident_len
+ 4);
286 page_len
-= (ident_len
+ 4);
290 if (transport_set_vpd_ident(vpd
, page_83
) < 0) {
291 off
+= (ident_len
+ 4);
292 page_len
-= (ident_len
+ 4);
297 list_add_tail(&vpd
->vpd_list
, &wwn
->t10_vpd_list
);
298 off
+= (ident_len
+ 4);
299 page_len
-= (ident_len
+ 4);
306 static int pscsi_add_device_to_list(struct se_device
*dev
,
307 struct scsi_device
*sd
)
309 struct pscsi_dev_virt
*pdv
= PSCSI_DEV(dev
);
310 struct request_queue
*q
= sd
->request_queue
;
314 if (!sd
->queue_depth
) {
315 sd
->queue_depth
= PSCSI_DEFAULT_QUEUEDEPTH
;
317 pr_err("Set broken SCSI Device %d:%d:%llu"
318 " queue_depth to %d\n", sd
->channel
, sd
->id
,
319 sd
->lun
, sd
->queue_depth
);
322 dev
->dev_attrib
.hw_block_size
= sd
->sector_size
;
323 dev
->dev_attrib
.hw_max_sectors
=
324 min_t(int, sd
->host
->max_sectors
, queue_max_hw_sectors(q
));
325 dev
->dev_attrib
.hw_queue_depth
= sd
->queue_depth
;
328 * Setup our standard INQUIRY info into se_dev->t10_wwn
330 pscsi_set_inquiry_info(sd
, &dev
->t10_wwn
);
333 * Locate VPD WWN Information used for various purposes within
334 * the Storage Engine.
336 if (!pscsi_get_inquiry_vpd_serial(sd
, &dev
->t10_wwn
)) {
338 * If VPD Unit Serial returned GOOD status, try
339 * VPD Device Identification page (0x83).
341 pscsi_get_inquiry_vpd_device_ident(sd
, &dev
->t10_wwn
);
345 * For TYPE_TAPE, attempt to determine blocksize with MODE_SENSE.
347 if (sd
->type
== TYPE_TAPE
)
348 pscsi_tape_read_blocksize(dev
, sd
);
352 static struct se_device
*pscsi_alloc_device(struct se_hba
*hba
,
355 struct pscsi_dev_virt
*pdv
;
357 pdv
= kzalloc(sizeof(struct pscsi_dev_virt
), GFP_KERNEL
);
359 pr_err("Unable to allocate memory for struct pscsi_dev_virt\n");
363 pr_debug("PSCSI: Allocated pdv: %p for %s\n", pdv
, name
);
368 * Called with struct Scsi_Host->host_lock called.
370 static int pscsi_create_type_disk(struct se_device
*dev
, struct scsi_device
*sd
)
371 __releases(sh
->host_lock
)
373 struct pscsi_hba_virt
*phv
= dev
->se_hba
->hba_ptr
;
374 struct pscsi_dev_virt
*pdv
= PSCSI_DEV(dev
);
375 struct Scsi_Host
*sh
= sd
->host
;
376 struct block_device
*bd
;
379 if (scsi_device_get(sd
)) {
380 pr_err("scsi_device_get() failed for %d:%d:%d:%llu\n",
381 sh
->host_no
, sd
->channel
, sd
->id
, sd
->lun
);
382 spin_unlock_irq(sh
->host_lock
);
385 spin_unlock_irq(sh
->host_lock
);
387 * Claim exclusive struct block_device access to struct scsi_device
388 * for TYPE_DISK using supplied udev_path
390 bd
= blkdev_get_by_path(dev
->udev_path
,
391 FMODE_WRITE
|FMODE_READ
|FMODE_EXCL
, pdv
);
393 pr_err("pSCSI: blkdev_get_by_path() failed\n");
399 ret
= pscsi_add_device_to_list(dev
, sd
);
401 blkdev_put(pdv
->pdv_bd
, FMODE_WRITE
|FMODE_READ
|FMODE_EXCL
);
406 pr_debug("CORE_PSCSI[%d] - Added TYPE_DISK for %d:%d:%d:%llu\n",
407 phv
->phv_host_id
, sh
->host_no
, sd
->channel
, sd
->id
, sd
->lun
);
412 * Called with struct Scsi_Host->host_lock called.
414 static int pscsi_create_type_rom(struct se_device
*dev
, struct scsi_device
*sd
)
415 __releases(sh
->host_lock
)
417 struct pscsi_hba_virt
*phv
= dev
->se_hba
->hba_ptr
;
418 struct Scsi_Host
*sh
= sd
->host
;
421 if (scsi_device_get(sd
)) {
422 pr_err("scsi_device_get() failed for %d:%d:%d:%llu\n",
423 sh
->host_no
, sd
->channel
, sd
->id
, sd
->lun
);
424 spin_unlock_irq(sh
->host_lock
);
427 spin_unlock_irq(sh
->host_lock
);
429 ret
= pscsi_add_device_to_list(dev
, sd
);
434 pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%llu\n",
435 phv
->phv_host_id
, scsi_device_type(sd
->type
), sh
->host_no
,
436 sd
->channel
, sd
->id
, sd
->lun
);
442 * Called with struct Scsi_Host->host_lock called.
444 static int pscsi_create_type_other(struct se_device
*dev
,
445 struct scsi_device
*sd
)
446 __releases(sh
->host_lock
)
448 struct pscsi_hba_virt
*phv
= dev
->se_hba
->hba_ptr
;
449 struct Scsi_Host
*sh
= sd
->host
;
452 spin_unlock_irq(sh
->host_lock
);
453 ret
= pscsi_add_device_to_list(dev
, sd
);
457 pr_debug("CORE_PSCSI[%d] - Added Type: %s for %d:%d:%d:%llu\n",
458 phv
->phv_host_id
, scsi_device_type(sd
->type
), sh
->host_no
,
459 sd
->channel
, sd
->id
, sd
->lun
);
463 static int pscsi_configure_device(struct se_device
*dev
)
465 struct se_hba
*hba
= dev
->se_hba
;
466 struct pscsi_dev_virt
*pdv
= PSCSI_DEV(dev
);
467 struct scsi_device
*sd
;
468 struct pscsi_hba_virt
*phv
= dev
->se_hba
->hba_ptr
;
469 struct Scsi_Host
*sh
= phv
->phv_lld_host
;
470 int legacy_mode_enable
= 0;
473 if (!(pdv
->pdv_flags
& PDF_HAS_CHANNEL_ID
) ||
474 !(pdv
->pdv_flags
& PDF_HAS_TARGET_ID
) ||
475 !(pdv
->pdv_flags
& PDF_HAS_LUN_ID
)) {
476 pr_err("Missing scsi_channel_id=, scsi_target_id= and"
477 " scsi_lun_id= parameters\n");
482 * If not running in PHV_LLD_SCSI_HOST_NO mode, locate the
483 * struct Scsi_Host we will need to bring the TCM/pSCSI object online
486 if (phv
->phv_mode
== PHV_LLD_SCSI_HOST_NO
) {
487 pr_err("pSCSI: Unable to locate struct"
488 " Scsi_Host for PHV_LLD_SCSI_HOST_NO\n");
492 * For the newer PHV_VIRTUAL_HOST_ID struct scsi_device
493 * reference, we enforce that udev_path has been set
495 if (!(dev
->dev_flags
& DF_USING_UDEV_PATH
)) {
496 pr_err("pSCSI: udev_path attribute has not"
497 " been set before ENABLE=1\n");
501 * If no scsi_host_id= was passed for PHV_VIRTUAL_HOST_ID,
502 * use the original TCM hba ID to reference Linux/SCSI Host No
503 * and enable for PHV_LLD_SCSI_HOST_NO mode.
505 if (!(pdv
->pdv_flags
& PDF_HAS_VIRT_HOST_ID
)) {
506 if (hba
->dev_count
) {
507 pr_err("pSCSI: Unable to set hba_mode"
508 " with active devices\n");
512 if (pscsi_pmode_enable_hba(hba
, 1) != 1)
515 legacy_mode_enable
= 1;
516 hba
->hba_flags
|= HBA_FLAGS_PSCSI_MODE
;
517 sh
= phv
->phv_lld_host
;
519 sh
= scsi_host_lookup(pdv
->pdv_host_id
);
521 pr_err("pSCSI: Unable to locate"
522 " pdv_host_id: %d\n", pdv
->pdv_host_id
);
525 pdv
->pdv_lld_host
= sh
;
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
);
608 else if (pdv
->pdv_lld_host
)
609 scsi_host_put(pdv
->pdv_lld_host
);
611 if ((sd
->type
== TYPE_DISK
) || (sd
->type
== TYPE_ROM
))
620 static void pscsi_transport_complete(struct se_cmd
*cmd
, struct scatterlist
*sg
,
621 unsigned char *sense_buffer
)
623 struct pscsi_dev_virt
*pdv
= PSCSI_DEV(cmd
->se_dev
);
624 struct scsi_device
*sd
= pdv
->pdv_sd
;
626 struct pscsi_plugin_task
*pt
= cmd
->priv
;
629 * Special case for REPORT_LUNs handling where pscsi_plugin_task has
630 * not been allocated because TCM is handling the emulation directly.
635 cdb
= &pt
->pscsi_cdb
[0];
636 result
= pt
->pscsi_result
;
638 * Hack to make sure that Write-Protect modepage is set if R/O mode is
641 if (!cmd
->data_length
)
642 goto after_mode_sense
;
644 if (((cdb
[0] == MODE_SENSE
) || (cdb
[0] == MODE_SENSE_10
)) &&
645 (status_byte(result
) << 1) == SAM_STAT_GOOD
) {
646 bool read_only
= target_lun_is_rdonly(cmd
);
651 buf
= transport_kmap_data_sg(cmd
);
653 ; /* XXX: TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE */
655 if (cdb
[0] == MODE_SENSE_10
) {
656 if (!(buf
[3] & 0x80))
659 if (!(buf
[2] & 0x80))
663 transport_kunmap_data_sg(cmd
);
668 if (sd
->type
!= TYPE_TAPE
|| !cmd
->data_length
)
669 goto after_mode_select
;
672 * Hack to correctly obtain the initiator requested blocksize for
673 * TYPE_TAPE. Since this value is dependent upon each tape media,
674 * struct scsi_device->sector_size will not contain the correct value
675 * by default, so we go ahead and set it so
676 * TRANSPORT(dev)->get_blockdev() returns the correct value to the
679 if (((cdb
[0] == MODE_SELECT
) || (cdb
[0] == MODE_SELECT_10
)) &&
680 (status_byte(result
) << 1) == SAM_STAT_GOOD
) {
685 buf
= sg_virt(&sg
[0]);
687 pr_err("Unable to get buf for scatterlist\n");
688 goto after_mode_select
;
691 if (cdb
[0] == MODE_SELECT
)
694 bdl
= (buf
[6] << 8) | (buf
[7]);
697 goto after_mode_select
;
699 if (cdb
[0] == MODE_SELECT
)
700 blocksize
= (buf
[9] << 16) | (buf
[10] << 8) |
703 blocksize
= (buf
[13] << 16) | (buf
[14] << 8) |
706 sd
->sector_size
= blocksize
;
710 if (sense_buffer
&& (status_byte(result
) & CHECK_CONDITION
)) {
711 memcpy(sense_buffer
, pt
->pscsi_sense
, TRANSPORT_SENSE_BUFFER
);
712 cmd
->se_cmd_flags
|= SCF_TRANSPORT_TASK_SENSE
;
717 Opt_scsi_host_id
, Opt_scsi_channel_id
, Opt_scsi_target_id
,
718 Opt_scsi_lun_id
, Opt_err
721 static match_table_t tokens
= {
722 {Opt_scsi_host_id
, "scsi_host_id=%d"},
723 {Opt_scsi_channel_id
, "scsi_channel_id=%d"},
724 {Opt_scsi_target_id
, "scsi_target_id=%d"},
725 {Opt_scsi_lun_id
, "scsi_lun_id=%d"},
729 static ssize_t
pscsi_set_configfs_dev_params(struct se_device
*dev
,
730 const char *page
, ssize_t count
)
732 struct pscsi_dev_virt
*pdv
= PSCSI_DEV(dev
);
733 struct pscsi_hba_virt
*phv
= dev
->se_hba
->hba_ptr
;
734 char *orig
, *ptr
, *opts
;
735 substring_t args
[MAX_OPT_ARGS
];
736 int ret
= 0, arg
, token
;
738 opts
= kstrdup(page
, GFP_KERNEL
);
744 while ((ptr
= strsep(&opts
, ",\n")) != NULL
) {
748 token
= match_token(ptr
, tokens
, args
);
750 case Opt_scsi_host_id
:
751 if (phv
->phv_mode
== PHV_LLD_SCSI_HOST_NO
) {
752 pr_err("PSCSI[%d]: Unable to accept"
753 " scsi_host_id while phv_mode =="
754 " PHV_LLD_SCSI_HOST_NO\n",
759 ret
= match_int(args
, &arg
);
762 pdv
->pdv_host_id
= arg
;
763 pr_debug("PSCSI[%d]: Referencing SCSI Host ID:"
764 " %d\n", phv
->phv_host_id
, pdv
->pdv_host_id
);
765 pdv
->pdv_flags
|= PDF_HAS_VIRT_HOST_ID
;
767 case Opt_scsi_channel_id
:
768 ret
= match_int(args
, &arg
);
771 pdv
->pdv_channel_id
= arg
;
772 pr_debug("PSCSI[%d]: Referencing SCSI Channel"
773 " ID: %d\n", phv
->phv_host_id
,
774 pdv
->pdv_channel_id
);
775 pdv
->pdv_flags
|= PDF_HAS_CHANNEL_ID
;
777 case Opt_scsi_target_id
:
778 ret
= match_int(args
, &arg
);
781 pdv
->pdv_target_id
= arg
;
782 pr_debug("PSCSI[%d]: Referencing SCSI Target"
783 " ID: %d\n", phv
->phv_host_id
,
785 pdv
->pdv_flags
|= PDF_HAS_TARGET_ID
;
787 case Opt_scsi_lun_id
:
788 ret
= match_int(args
, &arg
);
791 pdv
->pdv_lun_id
= arg
;
792 pr_debug("PSCSI[%d]: Referencing SCSI LUN ID:"
793 " %d\n", phv
->phv_host_id
, pdv
->pdv_lun_id
);
794 pdv
->pdv_flags
|= PDF_HAS_LUN_ID
;
803 return (!ret
) ? count
: ret
;
806 static ssize_t
pscsi_show_configfs_dev_params(struct se_device
*dev
, char *b
)
808 struct pscsi_hba_virt
*phv
= dev
->se_hba
->hba_ptr
;
809 struct pscsi_dev_virt
*pdv
= PSCSI_DEV(dev
);
810 struct scsi_device
*sd
= pdv
->pdv_sd
;
811 unsigned char host_id
[16];
815 if (phv
->phv_mode
== PHV_VIRTUAL_HOST_ID
)
816 snprintf(host_id
, 16, "%d", pdv
->pdv_host_id
);
818 snprintf(host_id
, 16, "PHBA Mode");
820 bl
= sprintf(b
, "SCSI Device Bus Location:"
821 " Channel ID: %d Target ID: %d LUN: %d Host ID: %s\n",
822 pdv
->pdv_channel_id
, pdv
->pdv_target_id
, pdv
->pdv_lun_id
,
826 bl
+= sprintf(b
+ bl
, " ");
827 bl
+= sprintf(b
+ bl
, "Vendor: ");
828 for (i
= 0; i
< 8; i
++) {
829 if (ISPRINT(sd
->vendor
[i
])) /* printable character? */
830 bl
+= sprintf(b
+ bl
, "%c", sd
->vendor
[i
]);
832 bl
+= sprintf(b
+ bl
, " ");
834 bl
+= sprintf(b
+ bl
, " Model: ");
835 for (i
= 0; i
< 16; i
++) {
836 if (ISPRINT(sd
->model
[i
])) /* printable character ? */
837 bl
+= sprintf(b
+ bl
, "%c", sd
->model
[i
]);
839 bl
+= sprintf(b
+ bl
, " ");
841 bl
+= sprintf(b
+ bl
, " Rev: ");
842 for (i
= 0; i
< 4; i
++) {
843 if (ISPRINT(sd
->rev
[i
])) /* printable character ? */
844 bl
+= sprintf(b
+ bl
, "%c", sd
->rev
[i
]);
846 bl
+= sprintf(b
+ bl
, " ");
848 bl
+= sprintf(b
+ bl
, "\n");
853 static void pscsi_bi_endio(struct bio
*bio
, int error
)
858 static inline struct bio
*pscsi_get_bio(int nr_vecs
)
862 * Use bio_malloc() following the comment in for bio -> struct request
863 * in block/blk-core.c:blk_make_request()
865 bio
= bio_kmalloc(GFP_KERNEL
, nr_vecs
);
867 pr_err("PSCSI: bio_kmalloc() failed\n");
870 bio
->bi_end_io
= pscsi_bi_endio
;
875 static sense_reason_t
876 pscsi_map_sg(struct se_cmd
*cmd
, struct scatterlist
*sgl
, u32 sgl_nents
,
877 enum dma_data_direction data_direction
, struct bio
**hbio
)
879 struct pscsi_dev_virt
*pdv
= PSCSI_DEV(cmd
->se_dev
);
880 struct bio
*bio
= NULL
, *tbio
= NULL
;
882 struct scatterlist
*sg
;
883 u32 data_len
= cmd
->data_length
, i
, len
, bytes
, off
;
884 int nr_pages
= (cmd
->data_length
+ sgl
[0].offset
+
885 PAGE_SIZE
- 1) >> PAGE_SHIFT
;
887 int rw
= (data_direction
== DMA_TO_DEVICE
);
891 pr_debug("PSCSI: nr_pages: %d\n", nr_pages
);
893 for_each_sg(sgl
, sg
, sgl_nents
, i
) {
898 pr_debug("PSCSI: i: %d page: %p len: %d off: %d\n", i
,
902 * We only have one page of data in each sg element,
903 * we can not cross a page boundary.
905 if (off
+ len
> PAGE_SIZE
)
908 if (len
> 0 && data_len
> 0) {
909 bytes
= min_t(unsigned int, len
, PAGE_SIZE
- off
);
910 bytes
= min(bytes
, data_len
);
913 nr_vecs
= min_t(int, BIO_MAX_PAGES
, nr_pages
);
916 * Calls bio_kmalloc() and sets bio->bi_end_io()
918 bio
= pscsi_get_bio(nr_vecs
);
923 bio
->bi_rw
|= REQ_WRITE
;
925 pr_debug("PSCSI: Allocated bio: %p,"
926 " dir: %s nr_vecs: %d\n", bio
,
927 (rw
) ? "rw" : "r", nr_vecs
);
929 * Set *hbio pointer to handle the case:
930 * nr_pages > BIO_MAX_PAGES, where additional
931 * bios need to be added to complete a given
937 tbio
= tbio
->bi_next
= bio
;
940 pr_debug("PSCSI: Calling bio_add_pc_page() i: %d"
941 " bio: %p page: %p len: %d off: %d\n", i
, bio
,
944 rc
= bio_add_pc_page(pdv
->pdv_sd
->request_queue
,
945 bio
, page
, bytes
, off
);
949 pr_debug("PSCSI: bio->bi_vcnt: %d nr_vecs: %d\n",
950 bio
->bi_vcnt
, nr_vecs
);
952 if (bio
->bi_vcnt
> nr_vecs
) {
953 pr_debug("PSCSI: Reached bio->bi_vcnt max:"
954 " %d i: %d bio: %p, allocating another"
955 " bio\n", bio
->bi_vcnt
, i
, bio
);
957 * Clear the pointer so that another bio will
958 * be allocated with pscsi_get_bio() above, the
959 * current bio has already been set *tbio and
973 *hbio
= (*hbio
)->bi_next
;
974 bio_endio(bio
, 0); /* XXX: should be error */
976 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
979 static sense_reason_t
980 pscsi_parse_cdb(struct se_cmd
*cmd
)
982 if (cmd
->se_cmd_flags
& SCF_BIDI
)
983 return TCM_UNSUPPORTED_SCSI_OPCODE
;
985 return passthrough_parse_cdb(cmd
, pscsi_execute_cmd
);
988 static sense_reason_t
989 pscsi_execute_cmd(struct se_cmd
*cmd
)
991 struct scatterlist
*sgl
= cmd
->t_data_sg
;
992 u32 sgl_nents
= cmd
->t_data_nents
;
993 enum dma_data_direction data_direction
= cmd
->data_direction
;
994 struct pscsi_dev_virt
*pdv
= PSCSI_DEV(cmd
->se_dev
);
995 struct pscsi_plugin_task
*pt
;
1001 * Dynamically alloc cdb space, since it may be larger than
1002 * TCM_MAX_COMMAND_SIZE
1004 pt
= kzalloc(sizeof(*pt
) + scsi_command_size(cmd
->t_task_cdb
), GFP_KERNEL
);
1006 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1010 memcpy(pt
->pscsi_cdb
, cmd
->t_task_cdb
,
1011 scsi_command_size(cmd
->t_task_cdb
));
1014 req
= blk_get_request(pdv
->pdv_sd
->request_queue
,
1015 (data_direction
== DMA_TO_DEVICE
),
1018 pr_err("PSCSI: blk_get_request() failed\n");
1019 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1023 blk_rq_set_block_pc(req
);
1025 BUG_ON(!cmd
->data_length
);
1027 ret
= pscsi_map_sg(cmd
, sgl
, sgl_nents
, data_direction
, &hbio
);
1031 req
= blk_make_request(pdv
->pdv_sd
->request_queue
, hbio
,
1034 pr_err("pSCSI: blk_make_request() failed\n");
1035 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1040 req
->end_io
= pscsi_req_done
;
1041 req
->end_io_data
= cmd
;
1042 req
->cmd_len
= scsi_command_size(pt
->pscsi_cdb
);
1043 req
->cmd
= &pt
->pscsi_cdb
[0];
1044 req
->sense
= &pt
->pscsi_sense
[0];
1046 if (pdv
->pdv_sd
->type
== TYPE_DISK
)
1047 req
->timeout
= PS_TIMEOUT_DISK
;
1049 req
->timeout
= PS_TIMEOUT_OTHER
;
1050 req
->retries
= PS_RETRY
;
1052 blk_execute_rq_nowait(pdv
->pdv_sd
->request_queue
, NULL
, req
,
1053 (cmd
->sam_task_attr
== TCM_HEAD_TAG
),
1060 struct bio
*bio
= hbio
;
1061 hbio
= hbio
->bi_next
;
1062 bio_endio(bio
, 0); /* XXX: should be error */
1064 ret
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1070 /* pscsi_get_device_type():
1074 static u32
pscsi_get_device_type(struct se_device
*dev
)
1076 struct pscsi_dev_virt
*pdv
= PSCSI_DEV(dev
);
1077 struct scsi_device
*sd
= pdv
->pdv_sd
;
1079 return (sd
) ? sd
->type
: TYPE_NO_LUN
;
1082 static sector_t
pscsi_get_blocks(struct se_device
*dev
)
1084 struct pscsi_dev_virt
*pdv
= PSCSI_DEV(dev
);
1086 if (pdv
->pdv_bd
&& pdv
->pdv_bd
->bd_part
)
1087 return pdv
->pdv_bd
->bd_part
->nr_sects
;
1093 static void pscsi_req_done(struct request
*req
, int uptodate
)
1095 struct se_cmd
*cmd
= req
->end_io_data
;
1096 struct pscsi_plugin_task
*pt
= cmd
->priv
;
1098 pt
->pscsi_result
= req
->errors
;
1099 pt
->pscsi_resid
= req
->resid_len
;
1101 cmd
->scsi_status
= status_byte(pt
->pscsi_result
) << 1;
1102 if (cmd
->scsi_status
) {
1103 pr_debug("PSCSI Status Byte exception at cmd: %p CDB:"
1104 " 0x%02x Result: 0x%08x\n", cmd
, pt
->pscsi_cdb
[0],
1108 switch (host_byte(pt
->pscsi_result
)) {
1110 target_complete_cmd(cmd
, cmd
->scsi_status
);
1113 pr_debug("PSCSI Host Byte exception at cmd: %p CDB:"
1114 " 0x%02x Result: 0x%08x\n", cmd
, pt
->pscsi_cdb
[0],
1116 target_complete_cmd(cmd
, SAM_STAT_CHECK_CONDITION
);
1120 __blk_put_request(req
->q
, req
);
1124 DEF_TB_DEV_ATTRIB_RO(pscsi
, hw_pi_prot_type
);
1125 TB_DEV_ATTR_RO(pscsi
, hw_pi_prot_type
);
1127 DEF_TB_DEV_ATTRIB_RO(pscsi
, hw_block_size
);
1128 TB_DEV_ATTR_RO(pscsi
, hw_block_size
);
1130 DEF_TB_DEV_ATTRIB_RO(pscsi
, hw_max_sectors
);
1131 TB_DEV_ATTR_RO(pscsi
, hw_max_sectors
);
1133 DEF_TB_DEV_ATTRIB_RO(pscsi
, hw_queue_depth
);
1134 TB_DEV_ATTR_RO(pscsi
, hw_queue_depth
);
1136 static struct configfs_attribute
*pscsi_backend_dev_attrs
[] = {
1137 &pscsi_dev_attrib_hw_pi_prot_type
.attr
,
1138 &pscsi_dev_attrib_hw_block_size
.attr
,
1139 &pscsi_dev_attrib_hw_max_sectors
.attr
,
1140 &pscsi_dev_attrib_hw_queue_depth
.attr
,
1144 static struct se_subsystem_api pscsi_template
= {
1146 .owner
= THIS_MODULE
,
1147 .transport_flags
= TRANSPORT_FLAG_PASSTHROUGH
,
1148 .attach_hba
= pscsi_attach_hba
,
1149 .detach_hba
= pscsi_detach_hba
,
1150 .pmode_enable_hba
= pscsi_pmode_enable_hba
,
1151 .alloc_device
= pscsi_alloc_device
,
1152 .configure_device
= pscsi_configure_device
,
1153 .free_device
= pscsi_free_device
,
1154 .transport_complete
= pscsi_transport_complete
,
1155 .parse_cdb
= pscsi_parse_cdb
,
1156 .set_configfs_dev_params
= pscsi_set_configfs_dev_params
,
1157 .show_configfs_dev_params
= pscsi_show_configfs_dev_params
,
1158 .get_device_type
= pscsi_get_device_type
,
1159 .get_blocks
= pscsi_get_blocks
,
1162 static int __init
pscsi_module_init(void)
1164 struct target_backend_cits
*tbc
= &pscsi_template
.tb_cits
;
1166 target_core_setup_sub_cits(&pscsi_template
);
1167 tbc
->tb_dev_attrib_cit
.ct_attrs
= pscsi_backend_dev_attrs
;
1169 return transport_subsystem_register(&pscsi_template
);
1172 static void __exit
pscsi_module_exit(void)
1174 transport_subsystem_release(&pscsi_template
);
1177 MODULE_DESCRIPTION("TCM PSCSI subsystem plugin");
1178 MODULE_AUTHOR("nab@Linux-iSCSI.org");
1179 MODULE_LICENSE("GPL");
1181 module_init(pscsi_module_init
);
1182 module_exit(pscsi_module_exit
);