tcm_loop.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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 u16 tcm_loop_set_fabric_sense_len(struct se_cmd *se_cmd, u32 sense_length)
  739. {
  740. return 0;
  741. }
  742. static u16 tcm_loop_get_fabric_sense_len(void)
  743. {
  744. return 0;
  745. }
  746. static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
  747. {
  748. switch (tl_hba->tl_proto_id) {
  749. case SCSI_PROTOCOL_SAS:
  750. return "SAS";
  751. case SCSI_PROTOCOL_FCP:
  752. return "FCP";
  753. case SCSI_PROTOCOL_ISCSI:
  754. return "iSCSI";
  755. default:
  756. break;
  757. }
  758. return "Unknown";
  759. }
  760. /* Start items for tcm_loop_port_cit */
  761. static int tcm_loop_port_link(
  762. struct se_portal_group *se_tpg,
  763. struct se_lun *lun)
  764. {
  765. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  766. struct tcm_loop_tpg, tl_se_tpg);
  767. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  768. atomic_inc(&tl_tpg->tl_tpg_port_count);
  769. smp_mb__after_atomic_inc();
  770. /*
  771. * Add Linux/SCSI struct scsi_device by HCTL
  772. */
  773. scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
  774. pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
  775. return 0;
  776. }
  777. static void tcm_loop_port_unlink(
  778. struct se_portal_group *se_tpg,
  779. struct se_lun *se_lun)
  780. {
  781. struct scsi_device *sd;
  782. struct tcm_loop_hba *tl_hba;
  783. struct tcm_loop_tpg *tl_tpg;
  784. tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
  785. tl_hba = tl_tpg->tl_hba;
  786. sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
  787. se_lun->unpacked_lun);
  788. if (!sd) {
  789. pr_err("Unable to locate struct scsi_device for %d:%d:"
  790. "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
  791. return;
  792. }
  793. /*
  794. * Remove Linux/SCSI struct scsi_device by HCTL
  795. */
  796. scsi_remove_device(sd);
  797. scsi_device_put(sd);
  798. atomic_dec(&tl_tpg->tl_tpg_port_count);
  799. smp_mb__after_atomic_dec();
  800. pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
  801. }
  802. /* End items for tcm_loop_port_cit */
  803. /* Start items for tcm_loop_nexus_cit */
  804. static int tcm_loop_make_nexus(
  805. struct tcm_loop_tpg *tl_tpg,
  806. const char *name)
  807. {
  808. struct se_portal_group *se_tpg;
  809. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  810. struct tcm_loop_nexus *tl_nexus;
  811. int ret = -ENOMEM;
  812. if (tl_tpg->tl_hba->tl_nexus) {
  813. pr_debug("tl_tpg->tl_hba->tl_nexus already exists\n");
  814. return -EEXIST;
  815. }
  816. se_tpg = &tl_tpg->tl_se_tpg;
  817. tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
  818. if (!tl_nexus) {
  819. pr_err("Unable to allocate struct tcm_loop_nexus\n");
  820. return -ENOMEM;
  821. }
  822. /*
  823. * Initialize the struct se_session pointer
  824. */
  825. tl_nexus->se_sess = transport_init_session();
  826. if (IS_ERR(tl_nexus->se_sess)) {
  827. ret = PTR_ERR(tl_nexus->se_sess);
  828. goto out;
  829. }
  830. /*
  831. * Since we are running in 'demo mode' this call with generate a
  832. * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
  833. * Initiator port name of the passed configfs group 'name'.
  834. */
  835. tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  836. se_tpg, (unsigned char *)name);
  837. if (!tl_nexus->se_sess->se_node_acl) {
  838. transport_free_session(tl_nexus->se_sess);
  839. goto out;
  840. }
  841. /*
  842. * Now, register the SAS I_T Nexus as active with the call to
  843. * transport_register_session()
  844. */
  845. __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
  846. tl_nexus->se_sess, tl_nexus);
  847. tl_tpg->tl_hba->tl_nexus = tl_nexus;
  848. pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
  849. " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
  850. name);
  851. return 0;
  852. out:
  853. kfree(tl_nexus);
  854. return ret;
  855. }
  856. static int tcm_loop_drop_nexus(
  857. struct tcm_loop_tpg *tpg)
  858. {
  859. struct se_session *se_sess;
  860. struct tcm_loop_nexus *tl_nexus;
  861. struct tcm_loop_hba *tl_hba = tpg->tl_hba;
  862. tl_nexus = tpg->tl_hba->tl_nexus;
  863. if (!tl_nexus)
  864. return -ENODEV;
  865. se_sess = tl_nexus->se_sess;
  866. if (!se_sess)
  867. return -ENODEV;
  868. if (atomic_read(&tpg->tl_tpg_port_count)) {
  869. pr_err("Unable to remove TCM_Loop I_T Nexus with"
  870. " active TPG port count: %d\n",
  871. atomic_read(&tpg->tl_tpg_port_count));
  872. return -EPERM;
  873. }
  874. pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
  875. " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
  876. tl_nexus->se_sess->se_node_acl->initiatorname);
  877. /*
  878. * Release the SCSI I_T Nexus to the emulated SAS Target Port
  879. */
  880. transport_deregister_session(tl_nexus->se_sess);
  881. tpg->tl_hba->tl_nexus = NULL;
  882. kfree(tl_nexus);
  883. return 0;
  884. }
  885. /* End items for tcm_loop_nexus_cit */
  886. static ssize_t tcm_loop_tpg_show_nexus(
  887. struct se_portal_group *se_tpg,
  888. char *page)
  889. {
  890. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  891. struct tcm_loop_tpg, tl_se_tpg);
  892. struct tcm_loop_nexus *tl_nexus;
  893. ssize_t ret;
  894. tl_nexus = tl_tpg->tl_hba->tl_nexus;
  895. if (!tl_nexus)
  896. return -ENODEV;
  897. ret = snprintf(page, PAGE_SIZE, "%s\n",
  898. tl_nexus->se_sess->se_node_acl->initiatorname);
  899. return ret;
  900. }
  901. static ssize_t tcm_loop_tpg_store_nexus(
  902. struct se_portal_group *se_tpg,
  903. const char *page,
  904. size_t count)
  905. {
  906. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  907. struct tcm_loop_tpg, tl_se_tpg);
  908. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  909. unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
  910. int ret;
  911. /*
  912. * Shutdown the active I_T nexus if 'NULL' is passed..
  913. */
  914. if (!strncmp(page, "NULL", 4)) {
  915. ret = tcm_loop_drop_nexus(tl_tpg);
  916. return (!ret) ? count : ret;
  917. }
  918. /*
  919. * Otherwise make sure the passed virtual Initiator port WWN matches
  920. * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
  921. * tcm_loop_make_nexus()
  922. */
  923. if (strlen(page) >= TL_WWN_ADDR_LEN) {
  924. pr_err("Emulated NAA Sas Address: %s, exceeds"
  925. " max: %d\n", page, TL_WWN_ADDR_LEN);
  926. return -EINVAL;
  927. }
  928. snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
  929. ptr = strstr(i_port, "naa.");
  930. if (ptr) {
  931. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
  932. pr_err("Passed SAS Initiator Port %s does not"
  933. " match target port protoid: %s\n", i_port,
  934. tcm_loop_dump_proto_id(tl_hba));
  935. return -EINVAL;
  936. }
  937. port_ptr = &i_port[0];
  938. goto check_newline;
  939. }
  940. ptr = strstr(i_port, "fc.");
  941. if (ptr) {
  942. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
  943. pr_err("Passed FCP Initiator Port %s does not"
  944. " match target port protoid: %s\n", i_port,
  945. tcm_loop_dump_proto_id(tl_hba));
  946. return -EINVAL;
  947. }
  948. port_ptr = &i_port[3]; /* Skip over "fc." */
  949. goto check_newline;
  950. }
  951. ptr = strstr(i_port, "iqn.");
  952. if (ptr) {
  953. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
  954. pr_err("Passed iSCSI Initiator Port %s does not"
  955. " match target port protoid: %s\n", i_port,
  956. tcm_loop_dump_proto_id(tl_hba));
  957. return -EINVAL;
  958. }
  959. port_ptr = &i_port[0];
  960. goto check_newline;
  961. }
  962. pr_err("Unable to locate prefix for emulated Initiator Port:"
  963. " %s\n", i_port);
  964. return -EINVAL;
  965. /*
  966. * Clear any trailing newline for the NAA WWN
  967. */
  968. check_newline:
  969. if (i_port[strlen(i_port)-1] == '\n')
  970. i_port[strlen(i_port)-1] = '\0';
  971. ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
  972. if (ret < 0)
  973. return ret;
  974. return count;
  975. }
  976. TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
  977. static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
  978. &tcm_loop_tpg_nexus.attr,
  979. NULL,
  980. };
  981. /* Start items for tcm_loop_naa_cit */
  982. struct se_portal_group *tcm_loop_make_naa_tpg(
  983. struct se_wwn *wwn,
  984. struct config_group *group,
  985. const char *name)
  986. {
  987. struct tcm_loop_hba *tl_hba = container_of(wwn,
  988. struct tcm_loop_hba, tl_hba_wwn);
  989. struct tcm_loop_tpg *tl_tpg;
  990. char *tpgt_str, *end_ptr;
  991. int ret;
  992. unsigned short int tpgt;
  993. tpgt_str = strstr(name, "tpgt_");
  994. if (!tpgt_str) {
  995. pr_err("Unable to locate \"tpgt_#\" directory"
  996. " group\n");
  997. return ERR_PTR(-EINVAL);
  998. }
  999. tpgt_str += 5; /* Skip ahead of "tpgt_" */
  1000. tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
  1001. if (tpgt >= TL_TPGS_PER_HBA) {
  1002. pr_err("Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
  1003. " %u\n", tpgt, TL_TPGS_PER_HBA);
  1004. return ERR_PTR(-EINVAL);
  1005. }
  1006. tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
  1007. tl_tpg->tl_hba = tl_hba;
  1008. tl_tpg->tl_tpgt = tpgt;
  1009. /*
  1010. * Register the tl_tpg as a emulated SAS TCM Target Endpoint
  1011. */
  1012. ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
  1013. wwn, &tl_tpg->tl_se_tpg, tl_tpg,
  1014. TRANSPORT_TPG_TYPE_NORMAL);
  1015. if (ret < 0)
  1016. return ERR_PTR(-ENOMEM);
  1017. pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
  1018. " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
  1019. config_item_name(&wwn->wwn_group.cg_item), tpgt);
  1020. return &tl_tpg->tl_se_tpg;
  1021. }
  1022. void tcm_loop_drop_naa_tpg(
  1023. struct se_portal_group *se_tpg)
  1024. {
  1025. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  1026. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  1027. struct tcm_loop_tpg, tl_se_tpg);
  1028. struct tcm_loop_hba *tl_hba;
  1029. unsigned short tpgt;
  1030. tl_hba = tl_tpg->tl_hba;
  1031. tpgt = tl_tpg->tl_tpgt;
  1032. /*
  1033. * Release the I_T Nexus for the Virtual SAS link if present
  1034. */
  1035. tcm_loop_drop_nexus(tl_tpg);
  1036. /*
  1037. * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
  1038. */
  1039. core_tpg_deregister(se_tpg);
  1040. tl_tpg->tl_hba = NULL;
  1041. tl_tpg->tl_tpgt = 0;
  1042. pr_debug("TCM_Loop_ConfigFS: Deallocated 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. }
  1046. /* End items for tcm_loop_naa_cit */
  1047. /* Start items for tcm_loop_cit */
  1048. struct se_wwn *tcm_loop_make_scsi_hba(
  1049. struct target_fabric_configfs *tf,
  1050. struct config_group *group,
  1051. const char *name)
  1052. {
  1053. struct tcm_loop_hba *tl_hba;
  1054. struct Scsi_Host *sh;
  1055. char *ptr;
  1056. int ret, off = 0;
  1057. tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
  1058. if (!tl_hba) {
  1059. pr_err("Unable to allocate struct tcm_loop_hba\n");
  1060. return ERR_PTR(-ENOMEM);
  1061. }
  1062. /*
  1063. * Determine the emulated Protocol Identifier and Target Port Name
  1064. * based on the incoming configfs directory name.
  1065. */
  1066. ptr = strstr(name, "naa.");
  1067. if (ptr) {
  1068. tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
  1069. goto check_len;
  1070. }
  1071. ptr = strstr(name, "fc.");
  1072. if (ptr) {
  1073. tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
  1074. off = 3; /* Skip over "fc." */
  1075. goto check_len;
  1076. }
  1077. ptr = strstr(name, "iqn.");
  1078. if (!ptr) {
  1079. pr_err("Unable to locate prefix for emulated Target "
  1080. "Port: %s\n", name);
  1081. ret = -EINVAL;
  1082. goto out;
  1083. }
  1084. tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
  1085. check_len:
  1086. if (strlen(name) >= TL_WWN_ADDR_LEN) {
  1087. pr_err("Emulated NAA %s Address: %s, exceeds"
  1088. " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
  1089. TL_WWN_ADDR_LEN);
  1090. ret = -EINVAL;
  1091. goto out;
  1092. }
  1093. snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
  1094. /*
  1095. * Call device_register(tl_hba->dev) to register the emulated
  1096. * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
  1097. * device_register() callbacks in tcm_loop_driver_probe()
  1098. */
  1099. ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
  1100. if (ret)
  1101. goto out;
  1102. sh = tl_hba->sh;
  1103. tcm_loop_hba_no_cnt++;
  1104. pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
  1105. " %s Address: %s at Linux/SCSI Host ID: %d\n",
  1106. tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
  1107. return &tl_hba->tl_hba_wwn;
  1108. out:
  1109. kfree(tl_hba);
  1110. return ERR_PTR(ret);
  1111. }
  1112. void tcm_loop_drop_scsi_hba(
  1113. struct se_wwn *wwn)
  1114. {
  1115. struct tcm_loop_hba *tl_hba = container_of(wwn,
  1116. struct tcm_loop_hba, tl_hba_wwn);
  1117. pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
  1118. " SAS Address: %s at Linux/SCSI Host ID: %d\n",
  1119. tl_hba->tl_wwn_address, tl_hba->sh->host_no);
  1120. /*
  1121. * Call device_unregister() on the original tl_hba->dev.
  1122. * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
  1123. * release *tl_hba;
  1124. */
  1125. device_unregister(&tl_hba->dev);
  1126. }
  1127. /* Start items for tcm_loop_cit */
  1128. static ssize_t tcm_loop_wwn_show_attr_version(
  1129. struct target_fabric_configfs *tf,
  1130. char *page)
  1131. {
  1132. return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
  1133. }
  1134. TF_WWN_ATTR_RO(tcm_loop, version);
  1135. static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
  1136. &tcm_loop_wwn_version.attr,
  1137. NULL,
  1138. };
  1139. /* End items for tcm_loop_cit */
  1140. static int tcm_loop_register_configfs(void)
  1141. {
  1142. struct target_fabric_configfs *fabric;
  1143. int ret;
  1144. /*
  1145. * Set the TCM Loop HBA counter to zero
  1146. */
  1147. tcm_loop_hba_no_cnt = 0;
  1148. /*
  1149. * Register the top level struct config_item_type with TCM core
  1150. */
  1151. fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
  1152. if (IS_ERR(fabric)) {
  1153. pr_err("tcm_loop_register_configfs() failed!\n");
  1154. return PTR_ERR(fabric);
  1155. }
  1156. /*
  1157. * Setup the fabric API of function pointers used by target_core_mod
  1158. */
  1159. fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
  1160. fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
  1161. fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
  1162. fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
  1163. fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
  1164. fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
  1165. fabric->tf_ops.tpg_get_pr_transport_id_len =
  1166. &tcm_loop_get_pr_transport_id_len;
  1167. fabric->tf_ops.tpg_parse_pr_out_transport_id =
  1168. &tcm_loop_parse_pr_out_transport_id;
  1169. fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
  1170. fabric->tf_ops.tpg_check_demo_mode_cache =
  1171. &tcm_loop_check_demo_mode_cache;
  1172. fabric->tf_ops.tpg_check_demo_mode_write_protect =
  1173. &tcm_loop_check_demo_mode_write_protect;
  1174. fabric->tf_ops.tpg_check_prod_mode_write_protect =
  1175. &tcm_loop_check_prod_mode_write_protect;
  1176. /*
  1177. * The TCM loopback fabric module runs in demo-mode to a local
  1178. * virtual SCSI device, so fabric dependent initator ACLs are
  1179. * not required.
  1180. */
  1181. fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
  1182. fabric->tf_ops.tpg_release_fabric_acl =
  1183. &tcm_loop_tpg_release_fabric_acl;
  1184. fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
  1185. /*
  1186. * Used for setting up remaining TCM resources in process context
  1187. */
  1188. fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
  1189. fabric->tf_ops.release_cmd = &tcm_loop_release_cmd;
  1190. fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
  1191. fabric->tf_ops.close_session = &tcm_loop_close_session;
  1192. fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
  1193. fabric->tf_ops.sess_get_initiator_sid = NULL;
  1194. fabric->tf_ops.write_pending = &tcm_loop_write_pending;
  1195. fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
  1196. /*
  1197. * Not used for TCM loopback
  1198. */
  1199. fabric->tf_ops.set_default_node_attributes =
  1200. &tcm_loop_set_default_node_attributes;
  1201. fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
  1202. fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
  1203. fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
  1204. fabric->tf_ops.queue_status = &tcm_loop_queue_status;
  1205. fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
  1206. fabric->tf_ops.set_fabric_sense_len = &tcm_loop_set_fabric_sense_len;
  1207. fabric->tf_ops.get_fabric_sense_len = &tcm_loop_get_fabric_sense_len;
  1208. /*
  1209. * Setup function pointers for generic logic in target_core_fabric_configfs.c
  1210. */
  1211. fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
  1212. fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
  1213. fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
  1214. fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
  1215. /*
  1216. * fabric_post_link() and fabric_pre_unlink() are used for
  1217. * registration and release of TCM Loop Virtual SCSI LUNs.
  1218. */
  1219. fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
  1220. fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
  1221. fabric->tf_ops.fabric_make_np = NULL;
  1222. fabric->tf_ops.fabric_drop_np = NULL;
  1223. /*
  1224. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1225. */
  1226. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
  1227. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
  1228. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1229. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1230. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1231. /*
  1232. * Once fabric->tf_ops has been setup, now register the fabric for
  1233. * use within TCM
  1234. */
  1235. ret = target_fabric_configfs_register(fabric);
  1236. if (ret < 0) {
  1237. pr_err("target_fabric_configfs_register() for"
  1238. " TCM_Loop failed!\n");
  1239. target_fabric_configfs_free(fabric);
  1240. return -1;
  1241. }
  1242. /*
  1243. * Setup our local pointer to *fabric.
  1244. */
  1245. tcm_loop_fabric_configfs = fabric;
  1246. pr_debug("TCM_LOOP[0] - Set fabric ->"
  1247. " tcm_loop_fabric_configfs\n");
  1248. return 0;
  1249. }
  1250. static void tcm_loop_deregister_configfs(void)
  1251. {
  1252. if (!tcm_loop_fabric_configfs)
  1253. return;
  1254. target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
  1255. tcm_loop_fabric_configfs = NULL;
  1256. pr_debug("TCM_LOOP[0] - Cleared"
  1257. " tcm_loop_fabric_configfs\n");
  1258. }
  1259. static int __init tcm_loop_fabric_init(void)
  1260. {
  1261. int ret = -ENOMEM;
  1262. tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
  1263. if (!tcm_loop_workqueue)
  1264. goto out;
  1265. tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
  1266. sizeof(struct tcm_loop_cmd),
  1267. __alignof__(struct tcm_loop_cmd),
  1268. 0, NULL);
  1269. if (!tcm_loop_cmd_cache) {
  1270. pr_debug("kmem_cache_create() for"
  1271. " tcm_loop_cmd_cache failed\n");
  1272. goto out_destroy_workqueue;
  1273. }
  1274. ret = tcm_loop_alloc_core_bus();
  1275. if (ret)
  1276. goto out_destroy_cache;
  1277. ret = tcm_loop_register_configfs();
  1278. if (ret)
  1279. goto out_release_core_bus;
  1280. return 0;
  1281. out_release_core_bus:
  1282. tcm_loop_release_core_bus();
  1283. out_destroy_cache:
  1284. kmem_cache_destroy(tcm_loop_cmd_cache);
  1285. out_destroy_workqueue:
  1286. destroy_workqueue(tcm_loop_workqueue);
  1287. out:
  1288. return ret;
  1289. }
  1290. static void __exit tcm_loop_fabric_exit(void)
  1291. {
  1292. tcm_loop_deregister_configfs();
  1293. tcm_loop_release_core_bus();
  1294. kmem_cache_destroy(tcm_loop_cmd_cache);
  1295. destroy_workqueue(tcm_loop_workqueue);
  1296. }
  1297. MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
  1298. MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
  1299. MODULE_LICENSE("GPL");
  1300. module_init(tcm_loop_fabric_init);
  1301. module_exit(tcm_loop_fabric_exit);