tcm_loop.c 37 KB

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