tcm_loop.c 39 KB

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