tcm_loop.c 40 KB

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