tcm_loop.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /*******************************************************************************
  2. *
  3. * This file contains the Linux/SCSI LLD virtual SCSI initiator driver
  4. * for emulated SAS initiator ports
  5. *
  6. * © Copyright 2011 RisingTide Systems LLC.
  7. *
  8. * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
  9. *
  10. * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. ****************************************************************************/
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/types.h>
  27. #include <linux/configfs.h>
  28. #include <scsi/scsi.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <target/target_core_base.h>
  34. #include <target/target_core_fabric.h>
  35. #include <target/target_core_fabric_configfs.h>
  36. #include <target/target_core_configfs.h>
  37. #include "tcm_loop.h"
  38. #define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev)
  39. /* Local pointer to allocated TCM configfs fabric module */
  40. static struct target_fabric_configfs *tcm_loop_fabric_configfs;
  41. static struct workqueue_struct *tcm_loop_workqueue;
  42. static struct kmem_cache *tcm_loop_cmd_cache;
  43. static int tcm_loop_hba_no_cnt;
  44. static int tcm_loop_queue_status(struct se_cmd *se_cmd);
  45. /*
  46. * Called from struct target_core_fabric_ops->check_stop_free()
  47. */
  48. static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
  49. {
  50. /*
  51. * Do not release struct se_cmd's containing a valid TMR
  52. * pointer. These will be released directly in tcm_loop_device_reset()
  53. * with transport_generic_free_cmd().
  54. */
  55. if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  56. return 0;
  57. /*
  58. * Release the struct se_cmd, which will make a callback to release
  59. * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
  60. */
  61. transport_generic_free_cmd(se_cmd, 0);
  62. return 1;
  63. }
  64. static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
  65. {
  66. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  67. struct tcm_loop_cmd, tl_se_cmd);
  68. kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
  69. }
  70. static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host)
  71. {
  72. seq_printf(m, "tcm_loop_proc_info()\n");
  73. return 0;
  74. }
  75. static int tcm_loop_driver_probe(struct device *);
  76. static int tcm_loop_driver_remove(struct device *);
  77. static int pseudo_lld_bus_match(struct device *dev,
  78. struct device_driver *dev_driver)
  79. {
  80. return 1;
  81. }
  82. static struct bus_type tcm_loop_lld_bus = {
  83. .name = "tcm_loop_bus",
  84. .match = pseudo_lld_bus_match,
  85. .probe = tcm_loop_driver_probe,
  86. .remove = tcm_loop_driver_remove,
  87. };
  88. static struct device_driver tcm_loop_driverfs = {
  89. .name = "tcm_loop",
  90. .bus = &tcm_loop_lld_bus,
  91. };
  92. /*
  93. * Used with root_device_register() in tcm_loop_alloc_core_bus() below
  94. */
  95. struct device *tcm_loop_primary;
  96. /*
  97. * Copied from drivers/scsi/libfc/fc_fcp.c:fc_change_queue_depth() and
  98. * drivers/scsi/libiscsi.c:iscsi_change_queue_depth()
  99. */
  100. static int tcm_loop_change_queue_depth(
  101. struct scsi_device *sdev,
  102. int depth,
  103. int reason)
  104. {
  105. switch (reason) {
  106. case SCSI_QDEPTH_DEFAULT:
  107. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  108. break;
  109. case SCSI_QDEPTH_QFULL:
  110. scsi_track_queue_full(sdev, depth);
  111. break;
  112. case SCSI_QDEPTH_RAMP_UP:
  113. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  114. break;
  115. default:
  116. return -EOPNOTSUPP;
  117. }
  118. return sdev->queue_depth;
  119. }
  120. /*
  121. * Locate the SAM Task Attr from struct scsi_cmnd *
  122. */
  123. static int tcm_loop_sam_attr(struct scsi_cmnd *sc)
  124. {
  125. if (sc->device->tagged_supported) {
  126. switch (sc->tag) {
  127. case HEAD_OF_QUEUE_TAG:
  128. return MSG_HEAD_TAG;
  129. case ORDERED_QUEUE_TAG:
  130. return MSG_ORDERED_TAG;
  131. default:
  132. break;
  133. }
  134. }
  135. return MSG_SIMPLE_TAG;
  136. }
  137. static void tcm_loop_submission_work(struct work_struct *work)
  138. {
  139. struct tcm_loop_cmd *tl_cmd =
  140. container_of(work, struct tcm_loop_cmd, work);
  141. struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
  142. struct scsi_cmnd *sc = tl_cmd->sc;
  143. struct tcm_loop_nexus *tl_nexus;
  144. struct tcm_loop_hba *tl_hba;
  145. struct tcm_loop_tpg *tl_tpg;
  146. struct scatterlist *sgl_bidi = NULL;
  147. u32 sgl_bidi_count = 0;
  148. int rc;
  149. tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
  150. tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
  151. /*
  152. * Ensure that this tl_tpg reference from the incoming sc->device->id
  153. * has already been configured via tcm_loop_make_naa_tpg().
  154. */
  155. if (!tl_tpg->tl_hba) {
  156. set_host_byte(sc, DID_NO_CONNECT);
  157. goto out_done;
  158. }
  159. tl_nexus = tl_hba->tl_nexus;
  160. if (!tl_nexus) {
  161. scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
  162. " does not exist\n");
  163. set_host_byte(sc, DID_ERROR);
  164. goto out_done;
  165. }
  166. if (scsi_bidi_cmnd(sc)) {
  167. struct scsi_data_buffer *sdb = scsi_in(sc);
  168. sgl_bidi = sdb->table.sgl;
  169. sgl_bidi_count = sdb->table.nents;
  170. se_cmd->se_cmd_flags |= SCF_BIDI;
  171. }
  172. rc = target_submit_cmd_map_sgls(se_cmd, tl_nexus->se_sess, sc->cmnd,
  173. &tl_cmd->tl_sense_buf[0], tl_cmd->sc->device->lun,
  174. scsi_bufflen(sc), tcm_loop_sam_attr(sc),
  175. sc->sc_data_direction, 0,
  176. scsi_sglist(sc), scsi_sg_count(sc),
  177. sgl_bidi, sgl_bidi_count);
  178. if (rc < 0) {
  179. set_host_byte(sc, DID_NO_CONNECT);
  180. goto out_done;
  181. }
  182. return;
  183. out_done:
  184. sc->scsi_done(sc);
  185. return;
  186. }
  187. /*
  188. * ->queuecommand can be and usually is called from interrupt context, so
  189. * defer the actual submission to a workqueue.
  190. */
  191. static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
  192. {
  193. struct tcm_loop_cmd *tl_cmd;
  194. pr_debug("tcm_loop_queuecommand() %d:%d:%d:%d got CDB: 0x%02x"
  195. " scsi_buf_len: %u\n", sc->device->host->host_no,
  196. sc->device->id, sc->device->channel, sc->device->lun,
  197. sc->cmnd[0], scsi_bufflen(sc));
  198. tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
  199. if (!tl_cmd) {
  200. pr_err("Unable to allocate struct tcm_loop_cmd\n");
  201. set_host_byte(sc, DID_ERROR);
  202. sc->scsi_done(sc);
  203. return 0;
  204. }
  205. tl_cmd->sc = sc;
  206. INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
  207. queue_work(tcm_loop_workqueue, &tl_cmd->work);
  208. return 0;
  209. }
  210. /*
  211. * Called from SCSI EH process context to issue a LUN_RESET TMR
  212. * to struct scsi_device
  213. */
  214. static int tcm_loop_device_reset(struct scsi_cmnd *sc)
  215. {
  216. struct se_cmd *se_cmd = NULL;
  217. struct se_portal_group *se_tpg;
  218. struct se_session *se_sess;
  219. struct tcm_loop_cmd *tl_cmd = NULL;
  220. struct tcm_loop_hba *tl_hba;
  221. struct tcm_loop_nexus *tl_nexus;
  222. struct tcm_loop_tmr *tl_tmr = NULL;
  223. struct tcm_loop_tpg *tl_tpg;
  224. int ret = FAILED, rc;
  225. /*
  226. * Locate the tcm_loop_hba_t pointer
  227. */
  228. tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
  229. /*
  230. * Locate the tl_nexus and se_sess pointers
  231. */
  232. tl_nexus = tl_hba->tl_nexus;
  233. if (!tl_nexus) {
  234. pr_err("Unable to perform device reset without"
  235. " active I_T Nexus\n");
  236. return FAILED;
  237. }
  238. se_sess = tl_nexus->se_sess;
  239. /*
  240. * Locate the tl_tpg and se_tpg pointers from TargetID in sc->device->id
  241. */
  242. tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
  243. se_tpg = &tl_tpg->tl_se_tpg;
  244. tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
  245. if (!tl_cmd) {
  246. pr_err("Unable to allocate memory for tl_cmd\n");
  247. return FAILED;
  248. }
  249. tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
  250. if (!tl_tmr) {
  251. pr_err("Unable to allocate memory for tl_tmr\n");
  252. goto release;
  253. }
  254. init_waitqueue_head(&tl_tmr->tl_tmr_wait);
  255. se_cmd = &tl_cmd->tl_se_cmd;
  256. /*
  257. * Initialize struct se_cmd descriptor from target_core_mod infrastructure
  258. */
  259. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
  260. DMA_NONE, MSG_SIMPLE_TAG,
  261. &tl_cmd->tl_sense_buf[0]);
  262. rc = core_tmr_alloc_req(se_cmd, tl_tmr, TMR_LUN_RESET, GFP_KERNEL);
  263. if (rc < 0)
  264. goto release;
  265. /*
  266. * Locate the underlying TCM struct se_lun from sc->device->lun
  267. */
  268. if (transport_lookup_tmr_lun(se_cmd, sc->device->lun) < 0)
  269. goto release;
  270. /*
  271. * Queue the TMR to TCM Core and sleep waiting for tcm_loop_queue_tm_rsp()
  272. * to wake us up.
  273. */
  274. transport_generic_handle_tmr(se_cmd);
  275. wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
  276. /*
  277. * The TMR LUN_RESET has completed, check the response status and
  278. * then release allocations.
  279. */
  280. ret = (se_cmd->se_tmr_req->response == TMR_FUNCTION_COMPLETE) ?
  281. SUCCESS : FAILED;
  282. release:
  283. if (se_cmd)
  284. transport_generic_free_cmd(se_cmd, 1);
  285. else
  286. kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
  287. kfree(tl_tmr);
  288. return ret;
  289. }
  290. static int tcm_loop_slave_alloc(struct scsi_device *sd)
  291. {
  292. set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
  293. return 0;
  294. }
  295. static int tcm_loop_slave_configure(struct scsi_device *sd)
  296. {
  297. return 0;
  298. }
  299. static struct scsi_host_template tcm_loop_driver_template = {
  300. .show_info = tcm_loop_show_info,
  301. .proc_name = "tcm_loopback",
  302. .name = "TCM_Loopback",
  303. .queuecommand = tcm_loop_queuecommand,
  304. .change_queue_depth = tcm_loop_change_queue_depth,
  305. .eh_device_reset_handler = tcm_loop_device_reset,
  306. .can_queue = 1024,
  307. .this_id = -1,
  308. .sg_tablesize = 256,
  309. .cmd_per_lun = 1024,
  310. .max_sectors = 0xFFFF,
  311. .use_clustering = DISABLE_CLUSTERING,
  312. .slave_alloc = tcm_loop_slave_alloc,
  313. .slave_configure = tcm_loop_slave_configure,
  314. .module = THIS_MODULE,
  315. };
  316. static int tcm_loop_driver_probe(struct device *dev)
  317. {
  318. struct tcm_loop_hba *tl_hba;
  319. struct Scsi_Host *sh;
  320. int error;
  321. tl_hba = to_tcm_loop_hba(dev);
  322. sh = scsi_host_alloc(&tcm_loop_driver_template,
  323. sizeof(struct tcm_loop_hba));
  324. if (!sh) {
  325. pr_err("Unable to allocate struct scsi_host\n");
  326. return -ENODEV;
  327. }
  328. tl_hba->sh = sh;
  329. /*
  330. * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
  331. */
  332. *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
  333. /*
  334. * Setup single ID, Channel and LUN for now..
  335. */
  336. sh->max_id = 2;
  337. sh->max_lun = 0;
  338. sh->max_channel = 0;
  339. sh->max_cmd_len = TL_SCSI_MAX_CMD_LEN;
  340. error = scsi_add_host(sh, &tl_hba->dev);
  341. if (error) {
  342. pr_err("%s: scsi_add_host failed\n", __func__);
  343. scsi_host_put(sh);
  344. return -ENODEV;
  345. }
  346. return 0;
  347. }
  348. static int tcm_loop_driver_remove(struct device *dev)
  349. {
  350. struct tcm_loop_hba *tl_hba;
  351. struct Scsi_Host *sh;
  352. tl_hba = to_tcm_loop_hba(dev);
  353. sh = tl_hba->sh;
  354. scsi_remove_host(sh);
  355. scsi_host_put(sh);
  356. return 0;
  357. }
  358. static void tcm_loop_release_adapter(struct device *dev)
  359. {
  360. struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
  361. kfree(tl_hba);
  362. }
  363. /*
  364. * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
  365. */
  366. static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
  367. {
  368. int ret;
  369. tl_hba->dev.bus = &tcm_loop_lld_bus;
  370. tl_hba->dev.parent = tcm_loop_primary;
  371. tl_hba->dev.release = &tcm_loop_release_adapter;
  372. dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
  373. ret = device_register(&tl_hba->dev);
  374. if (ret) {
  375. pr_err("device_register() failed for"
  376. " tl_hba->dev: %d\n", ret);
  377. return -ENODEV;
  378. }
  379. return 0;
  380. }
  381. /*
  382. * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
  383. * tcm_loop SCSI bus.
  384. */
  385. static int tcm_loop_alloc_core_bus(void)
  386. {
  387. int ret;
  388. tcm_loop_primary = root_device_register("tcm_loop_0");
  389. if (IS_ERR(tcm_loop_primary)) {
  390. pr_err("Unable to allocate tcm_loop_primary\n");
  391. return PTR_ERR(tcm_loop_primary);
  392. }
  393. ret = bus_register(&tcm_loop_lld_bus);
  394. if (ret) {
  395. pr_err("bus_register() failed for tcm_loop_lld_bus\n");
  396. goto dev_unreg;
  397. }
  398. ret = driver_register(&tcm_loop_driverfs);
  399. if (ret) {
  400. pr_err("driver_register() failed for"
  401. "tcm_loop_driverfs\n");
  402. goto bus_unreg;
  403. }
  404. pr_debug("Initialized TCM Loop Core Bus\n");
  405. return ret;
  406. bus_unreg:
  407. bus_unregister(&tcm_loop_lld_bus);
  408. dev_unreg:
  409. root_device_unregister(tcm_loop_primary);
  410. return ret;
  411. }
  412. static void tcm_loop_release_core_bus(void)
  413. {
  414. driver_unregister(&tcm_loop_driverfs);
  415. bus_unregister(&tcm_loop_lld_bus);
  416. root_device_unregister(tcm_loop_primary);
  417. pr_debug("Releasing TCM Loop Core BUS\n");
  418. }
  419. static char *tcm_loop_get_fabric_name(void)
  420. {
  421. return "loopback";
  422. }
  423. static u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  424. {
  425. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  426. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  427. /*
  428. * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
  429. * time based on the protocol dependent prefix of the passed configfs group.
  430. *
  431. * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
  432. * ProtocolID using target_core_fabric_lib.c symbols.
  433. */
  434. switch (tl_hba->tl_proto_id) {
  435. case SCSI_PROTOCOL_SAS:
  436. return sas_get_fabric_proto_ident(se_tpg);
  437. case SCSI_PROTOCOL_FCP:
  438. return fc_get_fabric_proto_ident(se_tpg);
  439. case SCSI_PROTOCOL_ISCSI:
  440. return iscsi_get_fabric_proto_ident(se_tpg);
  441. default:
  442. pr_err("Unknown tl_proto_id: 0x%02x, using"
  443. " SAS emulation\n", tl_hba->tl_proto_id);
  444. break;
  445. }
  446. return sas_get_fabric_proto_ident(se_tpg);
  447. }
  448. static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
  449. {
  450. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  451. /*
  452. * Return the passed NAA identifier for the SAS Target Port
  453. */
  454. return &tl_tpg->tl_hba->tl_wwn_address[0];
  455. }
  456. static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
  457. {
  458. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  459. /*
  460. * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
  461. * to represent the SCSI Target Port.
  462. */
  463. return tl_tpg->tl_tpgt;
  464. }
  465. static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
  466. {
  467. return 1;
  468. }
  469. static u32 tcm_loop_get_pr_transport_id(
  470. struct se_portal_group *se_tpg,
  471. struct se_node_acl *se_nacl,
  472. struct t10_pr_registration *pr_reg,
  473. int *format_code,
  474. unsigned char *buf)
  475. {
  476. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  477. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  478. switch (tl_hba->tl_proto_id) {
  479. case SCSI_PROTOCOL_SAS:
  480. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  481. format_code, buf);
  482. case SCSI_PROTOCOL_FCP:
  483. return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  484. format_code, buf);
  485. case SCSI_PROTOCOL_ISCSI:
  486. return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  487. format_code, buf);
  488. default:
  489. pr_err("Unknown tl_proto_id: 0x%02x, using"
  490. " SAS emulation\n", tl_hba->tl_proto_id);
  491. break;
  492. }
  493. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  494. format_code, buf);
  495. }
  496. static u32 tcm_loop_get_pr_transport_id_len(
  497. struct se_portal_group *se_tpg,
  498. struct se_node_acl *se_nacl,
  499. struct t10_pr_registration *pr_reg,
  500. int *format_code)
  501. {
  502. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  503. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  504. switch (tl_hba->tl_proto_id) {
  505. case SCSI_PROTOCOL_SAS:
  506. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  507. format_code);
  508. case SCSI_PROTOCOL_FCP:
  509. return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  510. format_code);
  511. case SCSI_PROTOCOL_ISCSI:
  512. return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  513. format_code);
  514. default:
  515. pr_err("Unknown tl_proto_id: 0x%02x, using"
  516. " SAS emulation\n", tl_hba->tl_proto_id);
  517. break;
  518. }
  519. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  520. format_code);
  521. }
  522. /*
  523. * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
  524. * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
  525. */
  526. static char *tcm_loop_parse_pr_out_transport_id(
  527. struct se_portal_group *se_tpg,
  528. const char *buf,
  529. u32 *out_tid_len,
  530. char **port_nexus_ptr)
  531. {
  532. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  533. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  534. switch (tl_hba->tl_proto_id) {
  535. case SCSI_PROTOCOL_SAS:
  536. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  537. port_nexus_ptr);
  538. case SCSI_PROTOCOL_FCP:
  539. return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  540. port_nexus_ptr);
  541. case SCSI_PROTOCOL_ISCSI:
  542. return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  543. port_nexus_ptr);
  544. default:
  545. pr_err("Unknown tl_proto_id: 0x%02x, using"
  546. " SAS emulation\n", tl_hba->tl_proto_id);
  547. break;
  548. }
  549. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  550. port_nexus_ptr);
  551. }
  552. /*
  553. * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
  554. * based upon the incoming fabric dependent SCSI Initiator Port
  555. */
  556. static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
  557. {
  558. return 1;
  559. }
  560. static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
  561. {
  562. return 0;
  563. }
  564. /*
  565. * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
  566. * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
  567. */
  568. static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
  569. {
  570. return 0;
  571. }
  572. /*
  573. * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
  574. * never be called for TCM_Loop by target_core_fabric_configfs.c code.
  575. * It has been added here as a nop for target_fabric_tf_ops_check()
  576. */
  577. static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
  578. {
  579. return 0;
  580. }
  581. static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
  582. struct se_portal_group *se_tpg)
  583. {
  584. struct tcm_loop_nacl *tl_nacl;
  585. tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
  586. if (!tl_nacl) {
  587. pr_err("Unable to allocate struct tcm_loop_nacl\n");
  588. return NULL;
  589. }
  590. return &tl_nacl->se_node_acl;
  591. }
  592. static void tcm_loop_tpg_release_fabric_acl(
  593. struct se_portal_group *se_tpg,
  594. struct se_node_acl *se_nacl)
  595. {
  596. struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
  597. struct tcm_loop_nacl, se_node_acl);
  598. kfree(tl_nacl);
  599. }
  600. static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
  601. {
  602. return 1;
  603. }
  604. static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
  605. {
  606. return 1;
  607. }
  608. static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
  609. {
  610. return;
  611. }
  612. static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
  613. {
  614. return 1;
  615. }
  616. static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
  617. {
  618. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  619. struct tcm_loop_cmd, tl_se_cmd);
  620. return tl_cmd->sc_cmd_state;
  621. }
  622. static int tcm_loop_shutdown_session(struct se_session *se_sess)
  623. {
  624. return 0;
  625. }
  626. static void tcm_loop_close_session(struct se_session *se_sess)
  627. {
  628. return;
  629. };
  630. static int tcm_loop_write_pending(struct se_cmd *se_cmd)
  631. {
  632. /*
  633. * Since Linux/SCSI has already sent down a struct scsi_cmnd
  634. * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
  635. * memory, and memory has already been mapped to struct se_cmd->t_mem_list
  636. * format with transport_generic_map_mem_to_cmd().
  637. *
  638. * We now tell TCM to add this WRITE CDB directly into the TCM storage
  639. * object execution queue.
  640. */
  641. target_execute_cmd(se_cmd);
  642. return 0;
  643. }
  644. static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
  645. {
  646. return 0;
  647. }
  648. static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
  649. {
  650. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  651. struct tcm_loop_cmd, tl_se_cmd);
  652. struct scsi_cmnd *sc = tl_cmd->sc;
  653. pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
  654. " cdb: 0x%02x\n", sc, sc->cmnd[0]);
  655. sc->result = SAM_STAT_GOOD;
  656. set_host_byte(sc, DID_OK);
  657. if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
  658. (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
  659. scsi_set_resid(sc, se_cmd->residual_count);
  660. sc->scsi_done(sc);
  661. return 0;
  662. }
  663. static int tcm_loop_queue_status(struct se_cmd *se_cmd)
  664. {
  665. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  666. struct tcm_loop_cmd, tl_se_cmd);
  667. struct scsi_cmnd *sc = tl_cmd->sc;
  668. pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
  669. " cdb: 0x%02x\n", sc, sc->cmnd[0]);
  670. if (se_cmd->sense_buffer &&
  671. ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
  672. (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
  673. memcpy(sc->sense_buffer, se_cmd->sense_buffer,
  674. SCSI_SENSE_BUFFERSIZE);
  675. sc->result = SAM_STAT_CHECK_CONDITION;
  676. set_driver_byte(sc, DRIVER_SENSE);
  677. } else
  678. sc->result = se_cmd->scsi_status;
  679. set_host_byte(sc, DID_OK);
  680. if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
  681. (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
  682. scsi_set_resid(sc, se_cmd->residual_count);
  683. sc->scsi_done(sc);
  684. return 0;
  685. }
  686. static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
  687. {
  688. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  689. struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
  690. /*
  691. * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
  692. * and wake up the wait_queue_head_t in tcm_loop_device_reset()
  693. */
  694. atomic_set(&tl_tmr->tmr_complete, 1);
  695. wake_up(&tl_tmr->tl_tmr_wait);
  696. }
  697. static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
  698. {
  699. switch (tl_hba->tl_proto_id) {
  700. case SCSI_PROTOCOL_SAS:
  701. return "SAS";
  702. case SCSI_PROTOCOL_FCP:
  703. return "FCP";
  704. case SCSI_PROTOCOL_ISCSI:
  705. return "iSCSI";
  706. default:
  707. break;
  708. }
  709. return "Unknown";
  710. }
  711. /* Start items for tcm_loop_port_cit */
  712. static int tcm_loop_port_link(
  713. struct se_portal_group *se_tpg,
  714. struct se_lun *lun)
  715. {
  716. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  717. struct tcm_loop_tpg, tl_se_tpg);
  718. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  719. atomic_inc(&tl_tpg->tl_tpg_port_count);
  720. smp_mb__after_atomic_inc();
  721. /*
  722. * Add Linux/SCSI struct scsi_device by HCTL
  723. */
  724. scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
  725. pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
  726. return 0;
  727. }
  728. static void tcm_loop_port_unlink(
  729. struct se_portal_group *se_tpg,
  730. struct se_lun *se_lun)
  731. {
  732. struct scsi_device *sd;
  733. struct tcm_loop_hba *tl_hba;
  734. struct tcm_loop_tpg *tl_tpg;
  735. tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
  736. tl_hba = tl_tpg->tl_hba;
  737. sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
  738. se_lun->unpacked_lun);
  739. if (!sd) {
  740. pr_err("Unable to locate struct scsi_device for %d:%d:"
  741. "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
  742. return;
  743. }
  744. /*
  745. * Remove Linux/SCSI struct scsi_device by HCTL
  746. */
  747. scsi_remove_device(sd);
  748. scsi_device_put(sd);
  749. atomic_dec(&tl_tpg->tl_tpg_port_count);
  750. smp_mb__after_atomic_dec();
  751. pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
  752. }
  753. /* End items for tcm_loop_port_cit */
  754. /* Start items for tcm_loop_nexus_cit */
  755. static int tcm_loop_make_nexus(
  756. struct tcm_loop_tpg *tl_tpg,
  757. const char *name)
  758. {
  759. struct se_portal_group *se_tpg;
  760. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  761. struct tcm_loop_nexus *tl_nexus;
  762. int ret = -ENOMEM;
  763. if (tl_tpg->tl_hba->tl_nexus) {
  764. pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
  765. return -EEXIST;
  766. }
  767. se_tpg = &tl_tpg->tl_se_tpg;
  768. tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
  769. if (!tl_nexus) {
  770. pr_err("Unable to allocate struct tcm_loop_nexus\n");
  771. return -ENOMEM;
  772. }
  773. /*
  774. * Initialize the struct se_session pointer
  775. */
  776. tl_nexus->se_sess = transport_init_session();
  777. if (IS_ERR(tl_nexus->se_sess)) {
  778. ret = PTR_ERR(tl_nexus->se_sess);
  779. goto out;
  780. }
  781. /*
  782. * Since we are running in 'demo mode' this call with generate a
  783. * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
  784. * Initiator port name of the passed configfs group 'name'.
  785. */
  786. tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  787. se_tpg, (unsigned char *)name);
  788. if (!tl_nexus->se_sess->se_node_acl) {
  789. transport_free_session(tl_nexus->se_sess);
  790. goto out;
  791. }
  792. /*
  793. * Now, register the SAS I_T Nexus as active with the call to
  794. * transport_register_session()
  795. */
  796. __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
  797. tl_nexus->se_sess, tl_nexus);
  798. tl_tpg->tl_hba->tl_nexus = tl_nexus;
  799. pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
  800. " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
  801. name);
  802. return 0;
  803. out:
  804. kfree(tl_nexus);
  805. return ret;
  806. }
  807. static int tcm_loop_drop_nexus(
  808. struct tcm_loop_tpg *tpg)
  809. {
  810. struct se_session *se_sess;
  811. struct tcm_loop_nexus *tl_nexus;
  812. struct tcm_loop_hba *tl_hba = tpg->tl_hba;
  813. tl_nexus = tpg->tl_hba->tl_nexus;
  814. if (!tl_nexus)
  815. return -ENODEV;
  816. se_sess = tl_nexus->se_sess;
  817. if (!se_sess)
  818. return -ENODEV;
  819. if (atomic_read(&tpg->tl_tpg_port_count)) {
  820. pr_err("Unable to remove TCM_Loop I_T Nexus with"
  821. " active TPG port count: %d\n",
  822. atomic_read(&tpg->tl_tpg_port_count));
  823. return -EPERM;
  824. }
  825. pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
  826. " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
  827. tl_nexus->se_sess->se_node_acl->initiatorname);
  828. /*
  829. * Release the SCSI I_T Nexus to the emulated SAS Target Port
  830. */
  831. transport_deregister_session(tl_nexus->se_sess);
  832. tpg->tl_hba->tl_nexus = NULL;
  833. kfree(tl_nexus);
  834. return 0;
  835. }
  836. /* End items for tcm_loop_nexus_cit */
  837. static ssize_t tcm_loop_tpg_show_nexus(
  838. struct se_portal_group *se_tpg,
  839. char *page)
  840. {
  841. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  842. struct tcm_loop_tpg, tl_se_tpg);
  843. struct tcm_loop_nexus *tl_nexus;
  844. ssize_t ret;
  845. tl_nexus = tl_tpg->tl_hba->tl_nexus;
  846. if (!tl_nexus)
  847. return -ENODEV;
  848. ret = snprintf(page, PAGE_SIZE, "%s\n",
  849. tl_nexus->se_sess->se_node_acl->initiatorname);
  850. return ret;
  851. }
  852. static ssize_t tcm_loop_tpg_store_nexus(
  853. struct se_portal_group *se_tpg,
  854. const char *page,
  855. size_t count)
  856. {
  857. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  858. struct tcm_loop_tpg, tl_se_tpg);
  859. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  860. unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
  861. int ret;
  862. /*
  863. * Shutdown the active I_T nexus if 'NULL' is passed..
  864. */
  865. if (!strncmp(page, "NULL", 4)) {
  866. ret = tcm_loop_drop_nexus(tl_tpg);
  867. return (!ret) ? count : ret;
  868. }
  869. /*
  870. * Otherwise make sure the passed virtual Initiator port WWN matches
  871. * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
  872. * tcm_loop_make_nexus()
  873. */
  874. if (strlen(page) >= TL_WWN_ADDR_LEN) {
  875. pr_err("Emulated NAA Sas Address: %s, exceeds"
  876. " max: %d\n", page, TL_WWN_ADDR_LEN);
  877. return -EINVAL;
  878. }
  879. snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
  880. ptr = strstr(i_port, "naa.");
  881. if (ptr) {
  882. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
  883. pr_err("Passed SAS Initiator Port %s does not"
  884. " match target port protoid: %s\n", i_port,
  885. tcm_loop_dump_proto_id(tl_hba));
  886. return -EINVAL;
  887. }
  888. port_ptr = &i_port[0];
  889. goto check_newline;
  890. }
  891. ptr = strstr(i_port, "fc.");
  892. if (ptr) {
  893. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
  894. pr_err("Passed FCP Initiator Port %s does not"
  895. " match target port protoid: %s\n", i_port,
  896. tcm_loop_dump_proto_id(tl_hba));
  897. return -EINVAL;
  898. }
  899. port_ptr = &i_port[3]; /* Skip over "fc." */
  900. goto check_newline;
  901. }
  902. ptr = strstr(i_port, "iqn.");
  903. if (ptr) {
  904. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
  905. pr_err("Passed iSCSI Initiator Port %s does not"
  906. " match target port protoid: %s\n", i_port,
  907. tcm_loop_dump_proto_id(tl_hba));
  908. return -EINVAL;
  909. }
  910. port_ptr = &i_port[0];
  911. goto check_newline;
  912. }
  913. pr_err("Unable to locate prefix for emulated Initiator Port:"
  914. " %s\n", i_port);
  915. return -EINVAL;
  916. /*
  917. * Clear any trailing newline for the NAA WWN
  918. */
  919. check_newline:
  920. if (i_port[strlen(i_port)-1] == '\n')
  921. i_port[strlen(i_port)-1] = '\0';
  922. ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
  923. if (ret < 0)
  924. return ret;
  925. return count;
  926. }
  927. TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
  928. static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
  929. &tcm_loop_tpg_nexus.attr,
  930. NULL,
  931. };
  932. /* Start items for tcm_loop_naa_cit */
  933. struct se_portal_group *tcm_loop_make_naa_tpg(
  934. struct se_wwn *wwn,
  935. struct config_group *group,
  936. const char *name)
  937. {
  938. struct tcm_loop_hba *tl_hba = container_of(wwn,
  939. struct tcm_loop_hba, tl_hba_wwn);
  940. struct tcm_loop_tpg *tl_tpg;
  941. char *tpgt_str, *end_ptr;
  942. int ret;
  943. unsigned short int tpgt;
  944. tpgt_str = strstr(name, "tpgt_");
  945. if (!tpgt_str) {
  946. pr_err("Unable to locate \"tpgt_#\" directory"
  947. " group\n");
  948. return ERR_PTR(-EINVAL);
  949. }
  950. tpgt_str += 5; /* Skip ahead of "tpgt_" */
  951. tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
  952. if (tpgt >= TL_TPGS_PER_HBA) {
  953. pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
  954. " %u\n", tpgt, TL_TPGS_PER_HBA);
  955. return ERR_PTR(-EINVAL);
  956. }
  957. tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
  958. tl_tpg->tl_hba = tl_hba;
  959. tl_tpg->tl_tpgt = tpgt;
  960. /*
  961. * Register the tl_tpg as a emulated SAS TCM Target Endpoint
  962. */
  963. ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
  964. wwn, &tl_tpg->tl_se_tpg, tl_tpg,
  965. TRANSPORT_TPG_TYPE_NORMAL);
  966. if (ret < 0)
  967. return ERR_PTR(-ENOMEM);
  968. pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
  969. " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
  970. config_item_name(&wwn->wwn_group.cg_item), tpgt);
  971. return &tl_tpg->tl_se_tpg;
  972. }
  973. void tcm_loop_drop_naa_tpg(
  974. struct se_portal_group *se_tpg)
  975. {
  976. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  977. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  978. struct tcm_loop_tpg, tl_se_tpg);
  979. struct tcm_loop_hba *tl_hba;
  980. unsigned short tpgt;
  981. tl_hba = tl_tpg->tl_hba;
  982. tpgt = tl_tpg->tl_tpgt;
  983. /*
  984. * Release the I_T Nexus for the Virtual SAS link if present
  985. */
  986. tcm_loop_drop_nexus(tl_tpg);
  987. /*
  988. * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
  989. */
  990. core_tpg_deregister(se_tpg);
  991. tl_tpg->tl_hba = NULL;
  992. tl_tpg->tl_tpgt = 0;
  993. pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
  994. " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
  995. config_item_name(&wwn->wwn_group.cg_item), tpgt);
  996. }
  997. /* End items for tcm_loop_naa_cit */
  998. /* Start items for tcm_loop_cit */
  999. struct se_wwn *tcm_loop_make_scsi_hba(
  1000. struct target_fabric_configfs *tf,
  1001. struct config_group *group,
  1002. const char *name)
  1003. {
  1004. struct tcm_loop_hba *tl_hba;
  1005. struct Scsi_Host *sh;
  1006. char *ptr;
  1007. int ret, off = 0;
  1008. tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
  1009. if (!tl_hba) {
  1010. pr_err("Unable to allocate struct tcm_loop_hba\n");
  1011. return ERR_PTR(-ENOMEM);
  1012. }
  1013. /*
  1014. * Determine the emulated Protocol Identifier and Target Port Name
  1015. * based on the incoming configfs directory name.
  1016. */
  1017. ptr = strstr(name, "naa.");
  1018. if (ptr) {
  1019. tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
  1020. goto check_len;
  1021. }
  1022. ptr = strstr(name, "fc.");
  1023. if (ptr) {
  1024. tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
  1025. off = 3; /* Skip over "fc." */
  1026. goto check_len;
  1027. }
  1028. ptr = strstr(name, "iqn.");
  1029. if (!ptr) {
  1030. pr_err("Unable to locate prefix for emulated Target "
  1031. "Port: %s\n", name);
  1032. ret = -EINVAL;
  1033. goto out;
  1034. }
  1035. tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
  1036. check_len:
  1037. if (strlen(name) >= TL_WWN_ADDR_LEN) {
  1038. pr_err("Emulated NAA %s Address: %s, exceeds"
  1039. " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
  1040. TL_WWN_ADDR_LEN);
  1041. ret = -EINVAL;
  1042. goto out;
  1043. }
  1044. snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
  1045. /*
  1046. * Call device_register(tl_hba->dev) to register the emulated
  1047. * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
  1048. * device_register() callbacks in tcm_loop_driver_probe()
  1049. */
  1050. ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
  1051. if (ret)
  1052. goto out;
  1053. sh = tl_hba->sh;
  1054. tcm_loop_hba_no_cnt++;
  1055. pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
  1056. " %s Address: %s at Linux/SCSI Host ID: %d\n",
  1057. tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
  1058. return &tl_hba->tl_hba_wwn;
  1059. out:
  1060. kfree(tl_hba);
  1061. return ERR_PTR(ret);
  1062. }
  1063. void tcm_loop_drop_scsi_hba(
  1064. struct se_wwn *wwn)
  1065. {
  1066. struct tcm_loop_hba *tl_hba = container_of(wwn,
  1067. struct tcm_loop_hba, tl_hba_wwn);
  1068. pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
  1069. " SAS Address: %s at Linux/SCSI Host ID: %d\n",
  1070. tl_hba->tl_wwn_address, tl_hba->sh->host_no);
  1071. /*
  1072. * Call device_unregister() on the original tl_hba->dev.
  1073. * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
  1074. * release *tl_hba;
  1075. */
  1076. device_unregister(&tl_hba->dev);
  1077. }
  1078. /* Start items for tcm_loop_cit */
  1079. static ssize_t tcm_loop_wwn_show_attr_version(
  1080. struct target_fabric_configfs *tf,
  1081. char *page)
  1082. {
  1083. return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
  1084. }
  1085. TF_WWN_ATTR_RO(tcm_loop, version);
  1086. static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
  1087. &tcm_loop_wwn_version.attr,
  1088. NULL,
  1089. };
  1090. /* End items for tcm_loop_cit */
  1091. static int tcm_loop_register_configfs(void)
  1092. {
  1093. struct target_fabric_configfs *fabric;
  1094. int ret;
  1095. /*
  1096. * Set the TCM Loop HBA counter to zero
  1097. */
  1098. tcm_loop_hba_no_cnt = 0;
  1099. /*
  1100. * Register the top level struct config_item_type with TCM core
  1101. */
  1102. fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
  1103. if (IS_ERR(fabric)) {
  1104. pr_err("tcm_loop_register_configfs() failed!\n");
  1105. return PTR_ERR(fabric);
  1106. }
  1107. /*
  1108. * Setup the fabric API of function pointers used by target_core_mod
  1109. */
  1110. fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
  1111. fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
  1112. fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
  1113. fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
  1114. fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
  1115. fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
  1116. fabric->tf_ops.tpg_get_pr_transport_id_len =
  1117. &tcm_loop_get_pr_transport_id_len;
  1118. fabric->tf_ops.tpg_parse_pr_out_transport_id =
  1119. &tcm_loop_parse_pr_out_transport_id;
  1120. fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
  1121. fabric->tf_ops.tpg_check_demo_mode_cache =
  1122. &tcm_loop_check_demo_mode_cache;
  1123. fabric->tf_ops.tpg_check_demo_mode_write_protect =
  1124. &tcm_loop_check_demo_mode_write_protect;
  1125. fabric->tf_ops.tpg_check_prod_mode_write_protect =
  1126. &tcm_loop_check_prod_mode_write_protect;
  1127. /*
  1128. * The TCM loopback fabric module runs in demo-mode to a local
  1129. * virtual SCSI device, so fabric dependent initator ACLs are
  1130. * not required.
  1131. */
  1132. fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
  1133. fabric->tf_ops.tpg_release_fabric_acl =
  1134. &tcm_loop_tpg_release_fabric_acl;
  1135. fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
  1136. /*
  1137. * Used for setting up remaining TCM resources in process context
  1138. */
  1139. fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
  1140. fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
  1141. fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
  1142. fabric->tf_ops.close_session = &tcm_loop_close_session;
  1143. fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
  1144. fabric->tf_ops.sess_get_initiator_sid = NULL;
  1145. fabric->tf_ops.write_pending = &tcm_loop_write_pending;
  1146. fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
  1147. /*
  1148. * Not used for TCM loopback
  1149. */
  1150. fabric->tf_ops.set_default_node_attributes =
  1151. &tcm_loop_set_default_node_attributes;
  1152. fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
  1153. fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
  1154. fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
  1155. fabric->tf_ops.queue_status = &tcm_loop_queue_status;
  1156. fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
  1157. /*
  1158. * Setup function pointers for generic logic in target_core_fabric_configfs.c
  1159. */
  1160. fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
  1161. fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
  1162. fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
  1163. fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
  1164. /*
  1165. * fabric_post_link() and fabric_pre_unlink() are used for
  1166. * registration and release of TCM Loop Virtual SCSI LUNs.
  1167. */
  1168. fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
  1169. fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
  1170. fabric->tf_ops.fabric_make_np = NULL;
  1171. fabric->tf_ops.fabric_drop_np = NULL;
  1172. /*
  1173. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1174. */
  1175. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
  1176. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
  1177. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1178. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1179. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1180. /*
  1181. * Once fabric->tf_ops has been setup, now register the fabric for
  1182. * use within TCM
  1183. */
  1184. ret = target_fabric_configfs_register(fabric);
  1185. if (ret < 0) {
  1186. pr_err("target_fabric_configfs_register() for"
  1187. " TCM_Loop failed!\n");
  1188. target_fabric_configfs_free(fabric);
  1189. return -1;
  1190. }
  1191. /*
  1192. * Setup our local pointer to *fabric.
  1193. */
  1194. tcm_loop_fabric_configfs = fabric;
  1195. pr_debug("TCM_LOOP[0] - Set fabric ->"
  1196. " tcm_loop_fabric_configfs\n");
  1197. return 0;
  1198. }
  1199. static void tcm_loop_deregister_configfs(void)
  1200. {
  1201. if (!tcm_loop_fabric_configfs)
  1202. return;
  1203. target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
  1204. tcm_loop_fabric_configfs = NULL;
  1205. pr_debug("TCM_LOOP[0] - Cleared"
  1206. " tcm_loop_fabric_configfs\n");
  1207. }
  1208. static int __init tcm_loop_fabric_init(void)
  1209. {
  1210. int ret = -ENOMEM;
  1211. tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
  1212. if (!tcm_loop_workqueue)
  1213. goto out;
  1214. tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
  1215. sizeof(struct tcm_loop_cmd),
  1216. __alignof__(struct tcm_loop_cmd),
  1217. 0, NULL);
  1218. if (!tcm_loop_cmd_cache) {
  1219. pr_debug("kmem_cache_create() for"
  1220. " tcm_loop_cmd_cache failed\n");
  1221. goto out_destroy_workqueue;
  1222. }
  1223. ret = tcm_loop_alloc_core_bus();
  1224. if (ret)
  1225. goto out_destroy_cache;
  1226. ret = tcm_loop_register_configfs();
  1227. if (ret)
  1228. goto out_release_core_bus;
  1229. return 0;
  1230. out_release_core_bus:
  1231. tcm_loop_release_core_bus();
  1232. out_destroy_cache:
  1233. kmem_cache_destroy(tcm_loop_cmd_cache);
  1234. out_destroy_workqueue:
  1235. destroy_workqueue(tcm_loop_workqueue);
  1236. out:
  1237. return ret;
  1238. }
  1239. static void __exit tcm_loop_fabric_exit(void)
  1240. {
  1241. tcm_loop_deregister_configfs();
  1242. tcm_loop_release_core_bus();
  1243. kmem_cache_destroy(tcm_loop_cmd_cache);
  1244. destroy_workqueue(tcm_loop_workqueue);
  1245. }
  1246. MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
  1247. MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
  1248. MODULE_LICENSE("GPL");
  1249. module_init(tcm_loop_fabric_init);
  1250. module_exit(tcm_loop_fabric_exit);