tcm_loop.c 38 KB

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