23 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
29 #include <scsi/scsi.h>
42 #define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev)
50 static int tcm_loop_hba_no_cnt;
57 static int tcm_loop_check_stop_free(
struct se_cmd *
se_cmd)
74 static void tcm_loop_release_cmd(
struct se_cmd *se_cmd)
86 return sprintf(buffer,
"tcm_loop_proc_info()\n");
89 static int tcm_loop_driver_probe(
struct device *);
90 static int tcm_loop_driver_remove(
struct device *);
92 static int pseudo_lld_bus_match(
struct device *
dev,
98 static struct bus_type tcm_loop_lld_bus = {
99 .name =
"tcm_loop_bus",
100 .match = pseudo_lld_bus_match,
101 .probe = tcm_loop_driver_probe,
102 .remove = tcm_loop_driver_remove,
107 .bus = &tcm_loop_lld_bus,
118 static int tcm_loop_change_queue_depth(
144 if (sc->
device->tagged_supported) {
162 struct se_cmd *se_cmd = &tl_cmd->
tl_se_cmd;
168 u32 sgl_bidi_count = 0;
186 " does not exist\n");
190 if (scsi_bidi_cmnd(sc)) {
193 sgl_bidi = sdb->
table.sgl;
194 sgl_bidi_count = sdb->
table.nents;
200 scsi_bufflen(sc), tcm_loop_sam_attr(sc),
202 scsi_sglist(sc), scsi_sg_count(sc),
203 sgl_bidi, sgl_bidi_count);
223 pr_debug(
"tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
224 " scsi_buf_len: %u\n", sc->
device->host->host_no,
226 sc->
cmnd[0], scsi_bufflen(sc));
228 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache,
GFP_ATOMIC);
230 pr_err(
"Unable to allocate struct tcm_loop_cmd\n");
246 static int tcm_loop_device_reset(
struct scsi_cmnd *sc)
248 struct se_cmd *se_cmd =
NULL;
266 pr_err(
"Unable to perform device reset without"
267 " active I_T Nexus\n");
277 tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache,
GFP_KERNEL);
279 pr_err(
"Unable to allocate memory for tl_cmd\n");
285 pr_err(
"Unable to allocate memory for tl_tmr\n");
339 .proc_info = tcm_loop_proc_info,
340 .proc_name =
"tcm_loopback",
341 .name =
"TCM_Loopback",
342 .queuecommand = tcm_loop_queuecommand,
343 .change_queue_depth = tcm_loop_change_queue_depth,
344 .eh_device_reset_handler = tcm_loop_device_reset,
349 .max_sectors = 0xFFFF,
351 .slave_alloc = tcm_loop_slave_alloc,
352 .slave_configure = tcm_loop_slave_configure,
356 static int tcm_loop_driver_probe(
struct device *
dev)
367 pr_err(
"Unable to allocate struct scsi_host\n");
384 error = scsi_add_host(sh, &tl_hba->
dev);
386 pr_err(
"%s: scsi_add_host failed\n", __func__);
393 static int tcm_loop_driver_remove(
struct device *dev)
406 static void tcm_loop_release_adapter(
struct device *dev)
416 static int tcm_loop_setup_hba_bus(
struct tcm_loop_hba *tl_hba,
int tcm_loop_host_id)
420 tl_hba->
dev.bus = &tcm_loop_lld_bus;
422 tl_hba->
dev.release = &tcm_loop_release_adapter;
427 pr_err(
"device_register() failed for"
428 " tl_hba->dev: %d\n", ret);
439 static int tcm_loop_alloc_core_bus(
void)
444 if (IS_ERR(tcm_loop_primary)) {
445 pr_err(
"Unable to allocate tcm_loop_primary\n");
446 return PTR_ERR(tcm_loop_primary);
451 pr_err(
"bus_register() failed for tcm_loop_lld_bus\n");
457 pr_err(
"driver_register() failed for"
458 "tcm_loop_driverfs\n");
462 pr_debug(
"Initialized TCM Loop Core Bus\n");
472 static void tcm_loop_release_core_bus(
void)
478 pr_debug(
"Releasing TCM Loop Core BUS\n");
481 static char *tcm_loop_get_fabric_name(
void)
505 pr_err(
"Unknown tl_proto_id: 0x%02x, using"
519 return &tl_tpg->
tl_hba->tl_wwn_address[0];
537 static u32 tcm_loop_get_pr_transport_id(
558 pr_err(
"Unknown tl_proto_id: 0x%02x, using"
567 static u32 tcm_loop_get_pr_transport_id_len(
587 pr_err(
"Unknown tl_proto_id: 0x%02x, using"
600 static char *tcm_loop_parse_pr_out_transport_id(
604 char **port_nexus_ptr)
620 pr_err(
"Unknown tl_proto_id: 0x%02x, using"
638 static int tcm_loop_check_demo_mode_cache(
struct se_portal_group *se_tpg)
647 static int tcm_loop_check_demo_mode_write_protect(
struct se_portal_group *se_tpg)
657 static int tcm_loop_check_prod_mode_write_protect(
struct se_portal_group *se_tpg)
662 static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
669 pr_err(
"Unable to allocate struct tcm_loop_nacl\n");
676 static void tcm_loop_tpg_release_fabric_acl(
691 static u32 tcm_loop_sess_get_index(
struct se_session *se_sess)
696 static void tcm_loop_set_default_node_attributes(
struct se_node_acl *se_acl)
701 static u32 tcm_loop_get_task_tag(
struct se_cmd *se_cmd)
706 static int tcm_loop_get_cmd_state(
struct se_cmd *se_cmd)
714 static int tcm_loop_shutdown_session(
struct se_session *se_sess)
719 static void tcm_loop_close_session(
struct se_session *se_sess)
724 static int tcm_loop_write_pending(
struct se_cmd *se_cmd)
739 static int tcm_loop_write_pending_status(
struct se_cmd *se_cmd)
744 static int tcm_loop_queue_data_in(
struct se_cmd *se_cmd)
750 pr_debug(
"tcm_loop_queue_data_in() called for scsi_cmnd: %p"
751 " cdb: 0x%02x\n", sc, sc->
cmnd[0]);
754 set_host_byte(sc,
DID_OK);
762 static int tcm_loop_queue_status(
struct se_cmd *se_cmd)
768 pr_debug(
"tcm_loop_queue_status() called for scsi_cmnd: %p"
769 " cdb: 0x%02x\n", sc, sc->
cmnd[0]);
782 set_host_byte(sc,
DID_OK);
790 static int tcm_loop_queue_tm_rsp(
struct se_cmd *se_cmd)
803 static char *tcm_loop_dump_proto_id(
struct tcm_loop_hba *tl_hba)
821 static int tcm_loop_port_link(
836 pr_debug(
"TCM_Loop_ConfigFS: Port Link Successful\n");
840 static void tcm_loop_port_unlink(
854 pr_err(
"Unable to locate struct scsi_device for %d:%d:"
867 pr_debug(
"TCM_Loop_ConfigFS: Port Unlink Successful\n");
874 static int tcm_loop_make_nexus(
883 if (tl_tpg->
tl_hba->tl_nexus) {
884 pr_debug(
"tl_tpg->tl_hba->tl_nexus already exists\n");
891 pr_err(
"Unable to allocate struct tcm_loop_nexus\n");
898 if (IS_ERR(tl_nexus->
se_sess)) {
899 ret = PTR_ERR(tl_nexus->
se_sess);
908 se_tpg, (
unsigned char *)name);
909 if (!tl_nexus->
se_sess->se_node_acl) {
919 tl_tpg->
tl_hba->tl_nexus = tl_nexus;
920 pr_debug(
"TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
921 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
930 static int tcm_loop_drop_nexus(
937 tl_nexus = tpg->
tl_hba->tl_nexus;
946 pr_err(
"Unable to remove TCM_Loop I_T Nexus with"
947 " active TPG port count: %d\n",
952 pr_debug(
"TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
953 " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
954 tl_nexus->
se_sess->se_node_acl->initiatorname);
966 static ssize_t tcm_loop_tpg_show_nexus(
975 tl_nexus = tl_tpg->
tl_hba->tl_nexus;
980 tl_nexus->
se_sess->se_node_acl->initiatorname);
985 static ssize_t tcm_loop_tpg_store_nexus(
998 if (!
strncmp(page,
"NULL", 4)) {
999 ret = tcm_loop_drop_nexus(tl_tpg);
1000 return (!ret) ? count :
ret;
1008 pr_err(
"Emulated NAA Sas Address: %s, exceeds"
1014 ptr =
strstr(i_port,
"naa.");
1017 pr_err(
"Passed SAS Initiator Port %s does not"
1018 " match target port protoid: %s\n", i_port,
1019 tcm_loop_dump_proto_id(tl_hba));
1022 port_ptr = &i_port[0];
1025 ptr =
strstr(i_port,
"fc.");
1028 pr_err(
"Passed FCP Initiator Port %s does not"
1029 " match target port protoid: %s\n", i_port,
1030 tcm_loop_dump_proto_id(tl_hba));
1033 port_ptr = &i_port[3];
1036 ptr =
strstr(i_port,
"iqn.");
1039 pr_err(
"Passed iSCSI Initiator Port %s does not"
1040 " match target port protoid: %s\n", i_port,
1041 tcm_loop_dump_proto_id(tl_hba));
1044 port_ptr = &i_port[0];
1047 pr_err(
"Unable to locate prefix for emulated Initiator Port:"
1054 if (i_port[
strlen(i_port)-1] ==
'\n')
1055 i_port[
strlen(i_port)-1] =
'\0';
1057 ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
1067 &tcm_loop_tpg_nexus.attr,
1081 char *tpgt_str, *end_ptr;
1083 unsigned short int tpgt;
1085 tpgt_str =
strstr(name,
"tpgt_");
1087 pr_err(
"Unable to locate \"tpgt_#\" directory"
1095 pr_err(
"Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
1111 pr_debug(
"TCM_Loop_ConfigFS: Allocated Emulated %s"
1112 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1113 config_item_name(&wwn->
wwn_group.cg_item), tpgt);
1125 unsigned short tpgt;
1132 tcm_loop_drop_nexus(tl_tpg);
1141 pr_debug(
"TCM_Loop_ConfigFS: Deallocated Emulated %s"
1142 " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
1143 config_item_name(&wwn->
wwn_group.cg_item), tpgt);
1162 pr_err(
"Unable to allocate struct tcm_loop_hba\n");
1169 ptr =
strstr(name,
"naa.");
1174 ptr =
strstr(name,
"fc.");
1180 ptr =
strstr(name,
"iqn.");
1182 pr_err(
"Unable to locate prefix for emulated Target "
1183 "Port: %s\n", name);
1191 pr_err(
"Emulated NAA %s Address: %s, exceeds"
1192 " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
1204 ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
1209 tcm_loop_hba_no_cnt++;
1210 pr_debug(
"TCM_Loop_ConfigFS: Allocated emulated Target"
1211 " %s Address: %s at Linux/SCSI Host ID: %d\n",
1212 tcm_loop_dump_proto_id(tl_hba), name, sh->
host_no);
1217 return ERR_PTR(ret);
1226 pr_debug(
"TCM_Loop_ConfigFS: Deallocating emulated Target"
1227 " SAS Address: %s at Linux/SCSI Host ID: %d\n",
1238 static ssize_t tcm_loop_wwn_show_attr_version(
1248 &tcm_loop_wwn_version.attr,
1254 static int tcm_loop_register_configfs(
void)
1261 tcm_loop_hba_no_cnt = 0;
1266 if (IS_ERR(fabric)) {
1267 pr_err(
"tcm_loop_register_configfs() failed!\n");
1268 return PTR_ERR(fabric);
1273 fabric->
tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
1274 fabric->
tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
1275 fabric->
tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
1276 fabric->
tf_ops.tpg_get_tag = &tcm_loop_get_tag;
1277 fabric->
tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
1278 fabric->
tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
1279 fabric->
tf_ops.tpg_get_pr_transport_id_len =
1280 &tcm_loop_get_pr_transport_id_len;
1281 fabric->
tf_ops.tpg_parse_pr_out_transport_id =
1282 &tcm_loop_parse_pr_out_transport_id;
1283 fabric->
tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
1284 fabric->
tf_ops.tpg_check_demo_mode_cache =
1285 &tcm_loop_check_demo_mode_cache;
1286 fabric->
tf_ops.tpg_check_demo_mode_write_protect =
1287 &tcm_loop_check_demo_mode_write_protect;
1288 fabric->
tf_ops.tpg_check_prod_mode_write_protect =
1289 &tcm_loop_check_prod_mode_write_protect;
1295 fabric->
tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
1296 fabric->
tf_ops.tpg_release_fabric_acl =
1297 &tcm_loop_tpg_release_fabric_acl;
1298 fabric->
tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
1302 fabric->
tf_ops.check_stop_free = &tcm_loop_check_stop_free;
1303 fabric->
tf_ops.release_cmd = &tcm_loop_release_cmd;
1304 fabric->
tf_ops.shutdown_session = &tcm_loop_shutdown_session;
1305 fabric->
tf_ops.close_session = &tcm_loop_close_session;
1306 fabric->
tf_ops.sess_get_index = &tcm_loop_sess_get_index;
1307 fabric->
tf_ops.sess_get_initiator_sid =
NULL;
1308 fabric->
tf_ops.write_pending = &tcm_loop_write_pending;
1309 fabric->
tf_ops.write_pending_status = &tcm_loop_write_pending_status;
1313 fabric->
tf_ops.set_default_node_attributes =
1314 &tcm_loop_set_default_node_attributes;
1315 fabric->
tf_ops.get_task_tag = &tcm_loop_get_task_tag;
1316 fabric->
tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
1317 fabric->
tf_ops.queue_data_in = &tcm_loop_queue_data_in;
1318 fabric->
tf_ops.queue_status = &tcm_loop_queue_status;
1319 fabric->
tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
1332 fabric->
tf_ops.fabric_post_link = &tcm_loop_port_link;
1333 fabric->
tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
1339 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
1340 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
1350 pr_err(
"target_fabric_configfs_register() for"
1351 " TCM_Loop failed!\n");
1358 tcm_loop_fabric_configfs = fabric;
1359 pr_debug(
"TCM_LOOP[0] - Set fabric ->"
1360 " tcm_loop_fabric_configfs\n");
1364 static void tcm_loop_deregister_configfs(
void)
1366 if (!tcm_loop_fabric_configfs)
1370 tcm_loop_fabric_configfs =
NULL;
1372 " tcm_loop_fabric_configfs\n");
1375 static int __init tcm_loop_fabric_init(
void)
1380 if (!tcm_loop_workqueue)
1387 if (!tcm_loop_cmd_cache) {
1389 " tcm_loop_cmd_cache failed\n");
1390 goto out_destroy_workqueue;
1393 ret = tcm_loop_alloc_core_bus();
1395 goto out_destroy_cache;
1397 ret = tcm_loop_register_configfs();
1399 goto out_release_core_bus;
1403 out_release_core_bus:
1404 tcm_loop_release_core_bus();
1407 out_destroy_workqueue:
1413 static void __exit tcm_loop_fabric_exit(
void)
1415 tcm_loop_deregister_configfs();
1416 tcm_loop_release_core_bus();