tcm_loop.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /*******************************************************************************
  2. *
  3. * This file contains the Linux/SCSI LLD virtual SCSI initiator driver
  4. * for emulated SAS initiator ports
  5. *
  6. * © Copyright 2011-2013 Datera, Inc.
  7. *
  8. * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
  9. *
  10. * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. ****************************************************************************/
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/types.h>
  27. #include <linux/configfs.h>
  28. #include <scsi/scsi.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_cmnd.h>
  33. #include <target/target_core_base.h>
  34. #include <target/target_core_fabric.h>
  35. #include "tcm_loop.h"
  36. #define to_tcm_loop_hba(hba) container_of(hba, struct tcm_loop_hba, dev)
  37. static struct workqueue_struct *tcm_loop_workqueue;
  38. static struct kmem_cache *tcm_loop_cmd_cache;
  39. static int tcm_loop_hba_no_cnt;
  40. static int tcm_loop_queue_status(struct se_cmd *se_cmd);
  41. /*
  42. * Called from struct target_core_fabric_ops->check_stop_free()
  43. */
  44. static int tcm_loop_check_stop_free(struct se_cmd *se_cmd)
  45. {
  46. /*
  47. * Do not release struct se_cmd's containing a valid TMR
  48. * pointer. These will be released directly in tcm_loop_device_reset()
  49. * with transport_generic_free_cmd().
  50. */
  51. if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  52. return 0;
  53. /*
  54. * Release the struct se_cmd, which will make a callback to release
  55. * struct tcm_loop_cmd * in tcm_loop_deallocate_core_cmd()
  56. */
  57. transport_generic_free_cmd(se_cmd, 0);
  58. return 1;
  59. }
  60. static void tcm_loop_release_cmd(struct se_cmd *se_cmd)
  61. {
  62. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  63. struct tcm_loop_cmd, tl_se_cmd);
  64. kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
  65. }
  66. static int tcm_loop_show_info(struct seq_file *m, struct Scsi_Host *host)
  67. {
  68. seq_printf(m, "tcm_loop_proc_info()\n");
  69. return 0;
  70. }
  71. static int tcm_loop_driver_probe(struct device *);
  72. static int tcm_loop_driver_remove(struct device *);
  73. static int pseudo_lld_bus_match(struct device *dev,
  74. struct device_driver *dev_driver)
  75. {
  76. return 1;
  77. }
  78. static struct bus_type tcm_loop_lld_bus = {
  79. .name = "tcm_loop_bus",
  80. .match = pseudo_lld_bus_match,
  81. .probe = tcm_loop_driver_probe,
  82. .remove = tcm_loop_driver_remove,
  83. };
  84. static struct device_driver tcm_loop_driverfs = {
  85. .name = "tcm_loop",
  86. .bus = &tcm_loop_lld_bus,
  87. };
  88. /*
  89. * Used with root_device_register() in tcm_loop_alloc_core_bus() below
  90. */
  91. static struct device *tcm_loop_primary;
  92. static void tcm_loop_submission_work(struct work_struct *work)
  93. {
  94. struct tcm_loop_cmd *tl_cmd =
  95. container_of(work, struct tcm_loop_cmd, work);
  96. struct se_cmd *se_cmd = &tl_cmd->tl_se_cmd;
  97. struct scsi_cmnd *sc = tl_cmd->sc;
  98. struct tcm_loop_nexus *tl_nexus;
  99. struct tcm_loop_hba *tl_hba;
  100. struct tcm_loop_tpg *tl_tpg;
  101. struct scatterlist *sgl_bidi = NULL;
  102. u32 sgl_bidi_count = 0, transfer_length;
  103. int rc;
  104. tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
  105. tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
  106. /*
  107. * Ensure that this tl_tpg reference from the incoming sc->device->id
  108. * has already been configured via tcm_loop_make_naa_tpg().
  109. */
  110. if (!tl_tpg->tl_hba) {
  111. set_host_byte(sc, DID_NO_CONNECT);
  112. goto out_done;
  113. }
  114. if (tl_tpg->tl_transport_status == TCM_TRANSPORT_OFFLINE) {
  115. set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
  116. goto out_done;
  117. }
  118. tl_nexus = tl_tpg->tl_nexus;
  119. if (!tl_nexus) {
  120. scmd_printk(KERN_ERR, sc, "TCM_Loop I_T Nexus"
  121. " does not exist\n");
  122. set_host_byte(sc, DID_ERROR);
  123. goto out_done;
  124. }
  125. if (scsi_bidi_cmnd(sc)) {
  126. struct scsi_data_buffer *sdb = scsi_in(sc);
  127. sgl_bidi = sdb->table.sgl;
  128. sgl_bidi_count = sdb->table.nents;
  129. se_cmd->se_cmd_flags |= SCF_BIDI;
  130. }
  131. transfer_length = scsi_transfer_length(sc);
  132. if (!scsi_prot_sg_count(sc) &&
  133. scsi_get_prot_op(sc) != SCSI_PROT_NORMAL) {
  134. se_cmd->prot_pto = true;
  135. /*
  136. * loopback transport doesn't support
  137. * WRITE_GENERATE, READ_STRIP protection
  138. * information operations, go ahead unprotected.
  139. */
  140. transfer_length = scsi_bufflen(sc);
  141. }
  142. se_cmd->tag = tl_cmd->sc_cmd_tag;
  143. rc = target_submit_cmd_map_sgls(se_cmd, tl_nexus->se_sess, sc->cmnd,
  144. &tl_cmd->tl_sense_buf[0], tl_cmd->sc->device->lun,
  145. transfer_length, TCM_SIMPLE_TAG,
  146. sc->sc_data_direction, 0,
  147. scsi_sglist(sc), scsi_sg_count(sc),
  148. sgl_bidi, sgl_bidi_count,
  149. scsi_prot_sglist(sc), scsi_prot_sg_count(sc));
  150. if (rc < 0) {
  151. set_host_byte(sc, DID_NO_CONNECT);
  152. goto out_done;
  153. }
  154. return;
  155. out_done:
  156. kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
  157. sc->scsi_done(sc);
  158. return;
  159. }
  160. /*
  161. * ->queuecommand can be and usually is called from interrupt context, so
  162. * defer the actual submission to a workqueue.
  163. */
  164. static int tcm_loop_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
  165. {
  166. struct tcm_loop_cmd *tl_cmd;
  167. pr_debug("tcm_loop_queuecommand() %d:%d:%d:%llu got CDB: 0x%02x"
  168. " scsi_buf_len: %u\n", sc->device->host->host_no,
  169. sc->device->id, sc->device->channel, sc->device->lun,
  170. sc->cmnd[0], scsi_bufflen(sc));
  171. tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_ATOMIC);
  172. if (!tl_cmd) {
  173. pr_err("Unable to allocate struct tcm_loop_cmd\n");
  174. set_host_byte(sc, DID_ERROR);
  175. sc->scsi_done(sc);
  176. return 0;
  177. }
  178. tl_cmd->sc = sc;
  179. tl_cmd->sc_cmd_tag = sc->request->tag;
  180. INIT_WORK(&tl_cmd->work, tcm_loop_submission_work);
  181. queue_work(tcm_loop_workqueue, &tl_cmd->work);
  182. return 0;
  183. }
  184. /*
  185. * Called from SCSI EH process context to issue a LUN_RESET TMR
  186. * to struct scsi_device
  187. */
  188. static int tcm_loop_issue_tmr(struct tcm_loop_tpg *tl_tpg,
  189. u64 lun, int task, enum tcm_tmreq_table tmr)
  190. {
  191. struct se_cmd *se_cmd = NULL;
  192. struct se_session *se_sess;
  193. struct se_portal_group *se_tpg;
  194. struct tcm_loop_nexus *tl_nexus;
  195. struct tcm_loop_cmd *tl_cmd = NULL;
  196. struct tcm_loop_tmr *tl_tmr = NULL;
  197. int ret = TMR_FUNCTION_FAILED, rc;
  198. /*
  199. * Locate the tl_nexus and se_sess pointers
  200. */
  201. tl_nexus = tl_tpg->tl_nexus;
  202. if (!tl_nexus) {
  203. pr_err("Unable to perform device reset without"
  204. " active I_T Nexus\n");
  205. return ret;
  206. }
  207. tl_cmd = kmem_cache_zalloc(tcm_loop_cmd_cache, GFP_KERNEL);
  208. if (!tl_cmd) {
  209. pr_err("Unable to allocate memory for tl_cmd\n");
  210. return ret;
  211. }
  212. tl_tmr = kzalloc(sizeof(struct tcm_loop_tmr), GFP_KERNEL);
  213. if (!tl_tmr) {
  214. pr_err("Unable to allocate memory for tl_tmr\n");
  215. goto release;
  216. }
  217. init_waitqueue_head(&tl_tmr->tl_tmr_wait);
  218. se_cmd = &tl_cmd->tl_se_cmd;
  219. se_tpg = &tl_tpg->tl_se_tpg;
  220. se_sess = tl_tpg->tl_nexus->se_sess;
  221. /*
  222. * Initialize struct se_cmd descriptor from target_core_mod infrastructure
  223. */
  224. transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 0,
  225. DMA_NONE, TCM_SIMPLE_TAG,
  226. &tl_cmd->tl_sense_buf[0]);
  227. rc = core_tmr_alloc_req(se_cmd, tl_tmr, tmr, GFP_KERNEL);
  228. if (rc < 0)
  229. goto release;
  230. if (tmr == TMR_ABORT_TASK)
  231. se_cmd->se_tmr_req->ref_task_tag = task;
  232. /*
  233. * Locate the underlying TCM struct se_lun
  234. */
  235. if (transport_lookup_tmr_lun(se_cmd, lun) < 0) {
  236. ret = TMR_LUN_DOES_NOT_EXIST;
  237. goto release;
  238. }
  239. /*
  240. * Queue the TMR to TCM Core and sleep waiting for
  241. * tcm_loop_queue_tm_rsp() to wake us up.
  242. */
  243. transport_generic_handle_tmr(se_cmd);
  244. wait_event(tl_tmr->tl_tmr_wait, atomic_read(&tl_tmr->tmr_complete));
  245. /*
  246. * The TMR LUN_RESET has completed, check the response status and
  247. * then release allocations.
  248. */
  249. ret = se_cmd->se_tmr_req->response;
  250. release:
  251. if (se_cmd)
  252. transport_generic_free_cmd(se_cmd, 1);
  253. else
  254. kmem_cache_free(tcm_loop_cmd_cache, tl_cmd);
  255. kfree(tl_tmr);
  256. return ret;
  257. }
  258. static int tcm_loop_abort_task(struct scsi_cmnd *sc)
  259. {
  260. struct tcm_loop_hba *tl_hba;
  261. struct tcm_loop_tpg *tl_tpg;
  262. int ret = FAILED;
  263. /*
  264. * Locate the tcm_loop_hba_t pointer
  265. */
  266. tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
  267. tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
  268. ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
  269. sc->request->tag, TMR_ABORT_TASK);
  270. return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
  271. }
  272. /*
  273. * Called from SCSI EH process context to issue a LUN_RESET TMR
  274. * to struct scsi_device
  275. */
  276. static int tcm_loop_device_reset(struct scsi_cmnd *sc)
  277. {
  278. struct tcm_loop_hba *tl_hba;
  279. struct tcm_loop_tpg *tl_tpg;
  280. int ret = FAILED;
  281. /*
  282. * Locate the tcm_loop_hba_t pointer
  283. */
  284. tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
  285. tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
  286. ret = tcm_loop_issue_tmr(tl_tpg, sc->device->lun,
  287. 0, TMR_LUN_RESET);
  288. return (ret == TMR_FUNCTION_COMPLETE) ? SUCCESS : FAILED;
  289. }
  290. static int tcm_loop_target_reset(struct scsi_cmnd *sc)
  291. {
  292. struct tcm_loop_hba *tl_hba;
  293. struct tcm_loop_tpg *tl_tpg;
  294. /*
  295. * Locate the tcm_loop_hba_t pointer
  296. */
  297. tl_hba = *(struct tcm_loop_hba **)shost_priv(sc->device->host);
  298. if (!tl_hba) {
  299. pr_err("Unable to perform device reset without"
  300. " active I_T Nexus\n");
  301. return FAILED;
  302. }
  303. /*
  304. * Locate the tl_tpg pointer from TargetID in sc->device->id
  305. */
  306. tl_tpg = &tl_hba->tl_hba_tpgs[sc->device->id];
  307. if (tl_tpg) {
  308. tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
  309. return SUCCESS;
  310. }
  311. return FAILED;
  312. }
  313. static int tcm_loop_slave_alloc(struct scsi_device *sd)
  314. {
  315. set_bit(QUEUE_FLAG_BIDI, &sd->request_queue->queue_flags);
  316. return 0;
  317. }
  318. static struct scsi_host_template tcm_loop_driver_template = {
  319. .show_info = tcm_loop_show_info,
  320. .proc_name = "tcm_loopback",
  321. .name = "TCM_Loopback",
  322. .queuecommand = tcm_loop_queuecommand,
  323. .change_queue_depth = scsi_change_queue_depth,
  324. .eh_abort_handler = tcm_loop_abort_task,
  325. .eh_device_reset_handler = tcm_loop_device_reset,
  326. .eh_target_reset_handler = tcm_loop_target_reset,
  327. .can_queue = 1024,
  328. .this_id = -1,
  329. .sg_tablesize = 256,
  330. .cmd_per_lun = 1024,
  331. .max_sectors = 0xFFFF,
  332. .use_clustering = DISABLE_CLUSTERING,
  333. .slave_alloc = tcm_loop_slave_alloc,
  334. .module = THIS_MODULE,
  335. .track_queue_depth = 1,
  336. };
  337. static int tcm_loop_driver_probe(struct device *dev)
  338. {
  339. struct tcm_loop_hba *tl_hba;
  340. struct Scsi_Host *sh;
  341. int error, host_prot;
  342. tl_hba = to_tcm_loop_hba(dev);
  343. sh = scsi_host_alloc(&tcm_loop_driver_template,
  344. sizeof(struct tcm_loop_hba));
  345. if (!sh) {
  346. pr_err("Unable to allocate struct scsi_host\n");
  347. return -ENODEV;
  348. }
  349. tl_hba->sh = sh;
  350. /*
  351. * Assign the struct tcm_loop_hba pointer to struct Scsi_Host->hostdata
  352. */
  353. *((struct tcm_loop_hba **)sh->hostdata) = tl_hba;
  354. /*
  355. * Setup single ID, Channel and LUN for now..
  356. */
  357. sh->max_id = 2;
  358. sh->max_lun = 0;
  359. sh->max_channel = 0;
  360. sh->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
  361. host_prot = SHOST_DIF_TYPE1_PROTECTION | SHOST_DIF_TYPE2_PROTECTION |
  362. SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE1_PROTECTION |
  363. SHOST_DIX_TYPE2_PROTECTION | SHOST_DIX_TYPE3_PROTECTION;
  364. scsi_host_set_prot(sh, host_prot);
  365. scsi_host_set_guard(sh, SHOST_DIX_GUARD_CRC);
  366. error = scsi_add_host(sh, &tl_hba->dev);
  367. if (error) {
  368. pr_err("%s: scsi_add_host failed\n", __func__);
  369. scsi_host_put(sh);
  370. return -ENODEV;
  371. }
  372. return 0;
  373. }
  374. static int tcm_loop_driver_remove(struct device *dev)
  375. {
  376. struct tcm_loop_hba *tl_hba;
  377. struct Scsi_Host *sh;
  378. tl_hba = to_tcm_loop_hba(dev);
  379. sh = tl_hba->sh;
  380. scsi_remove_host(sh);
  381. scsi_host_put(sh);
  382. return 0;
  383. }
  384. static void tcm_loop_release_adapter(struct device *dev)
  385. {
  386. struct tcm_loop_hba *tl_hba = to_tcm_loop_hba(dev);
  387. kfree(tl_hba);
  388. }
  389. /*
  390. * Called from tcm_loop_make_scsi_hba() in tcm_loop_configfs.c
  391. */
  392. static int tcm_loop_setup_hba_bus(struct tcm_loop_hba *tl_hba, int tcm_loop_host_id)
  393. {
  394. int ret;
  395. tl_hba->dev.bus = &tcm_loop_lld_bus;
  396. tl_hba->dev.parent = tcm_loop_primary;
  397. tl_hba->dev.release = &tcm_loop_release_adapter;
  398. dev_set_name(&tl_hba->dev, "tcm_loop_adapter_%d", tcm_loop_host_id);
  399. ret = device_register(&tl_hba->dev);
  400. if (ret) {
  401. pr_err("device_register() failed for"
  402. " tl_hba->dev: %d\n", ret);
  403. return -ENODEV;
  404. }
  405. return 0;
  406. }
  407. /*
  408. * Called from tcm_loop_fabric_init() in tcl_loop_fabric.c to load the emulated
  409. * tcm_loop SCSI bus.
  410. */
  411. static int tcm_loop_alloc_core_bus(void)
  412. {
  413. int ret;
  414. tcm_loop_primary = root_device_register("tcm_loop_0");
  415. if (IS_ERR(tcm_loop_primary)) {
  416. pr_err("Unable to allocate tcm_loop_primary\n");
  417. return PTR_ERR(tcm_loop_primary);
  418. }
  419. ret = bus_register(&tcm_loop_lld_bus);
  420. if (ret) {
  421. pr_err("bus_register() failed for tcm_loop_lld_bus\n");
  422. goto dev_unreg;
  423. }
  424. ret = driver_register(&tcm_loop_driverfs);
  425. if (ret) {
  426. pr_err("driver_register() failed for"
  427. "tcm_loop_driverfs\n");
  428. goto bus_unreg;
  429. }
  430. pr_debug("Initialized TCM Loop Core Bus\n");
  431. return ret;
  432. bus_unreg:
  433. bus_unregister(&tcm_loop_lld_bus);
  434. dev_unreg:
  435. root_device_unregister(tcm_loop_primary);
  436. return ret;
  437. }
  438. static void tcm_loop_release_core_bus(void)
  439. {
  440. driver_unregister(&tcm_loop_driverfs);
  441. bus_unregister(&tcm_loop_lld_bus);
  442. root_device_unregister(tcm_loop_primary);
  443. pr_debug("Releasing TCM Loop Core BUS\n");
  444. }
  445. static char *tcm_loop_get_fabric_name(void)
  446. {
  447. return "loopback";
  448. }
  449. static inline struct tcm_loop_tpg *tl_tpg(struct se_portal_group *se_tpg)
  450. {
  451. return container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
  452. }
  453. static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
  454. {
  455. /*
  456. * Return the passed NAA identifier for the Target Port
  457. */
  458. return &tl_tpg(se_tpg)->tl_hba->tl_wwn_address[0];
  459. }
  460. static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
  461. {
  462. /*
  463. * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
  464. * to represent the SCSI Target Port.
  465. */
  466. return tl_tpg(se_tpg)->tl_tpgt;
  467. }
  468. /*
  469. * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
  470. * based upon the incoming fabric dependent SCSI Initiator Port
  471. */
  472. static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
  473. {
  474. return 1;
  475. }
  476. static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
  477. {
  478. return 0;
  479. }
  480. /*
  481. * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
  482. * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
  483. */
  484. static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
  485. {
  486. return 0;
  487. }
  488. /*
  489. * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
  490. * never be called for TCM_Loop by target_core_fabric_configfs.c code.
  491. * It has been added here as a nop for target_fabric_tf_ops_check()
  492. */
  493. static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
  494. {
  495. return 0;
  496. }
  497. static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg)
  498. {
  499. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
  500. tl_se_tpg);
  501. return tl_tpg->tl_fabric_prot_type;
  502. }
  503. static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
  504. {
  505. return 1;
  506. }
  507. static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
  508. {
  509. return 1;
  510. }
  511. static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
  512. {
  513. return;
  514. }
  515. static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
  516. {
  517. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  518. struct tcm_loop_cmd, tl_se_cmd);
  519. return tl_cmd->sc_cmd_state;
  520. }
  521. static int tcm_loop_shutdown_session(struct se_session *se_sess)
  522. {
  523. return 0;
  524. }
  525. static void tcm_loop_close_session(struct se_session *se_sess)
  526. {
  527. return;
  528. };
  529. static int tcm_loop_write_pending(struct se_cmd *se_cmd)
  530. {
  531. /*
  532. * Since Linux/SCSI has already sent down a struct scsi_cmnd
  533. * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
  534. * memory, and memory has already been mapped to struct se_cmd->t_mem_list
  535. * format with transport_generic_map_mem_to_cmd().
  536. *
  537. * We now tell TCM to add this WRITE CDB directly into the TCM storage
  538. * object execution queue.
  539. */
  540. target_execute_cmd(se_cmd);
  541. return 0;
  542. }
  543. static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
  544. {
  545. return 0;
  546. }
  547. static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
  548. {
  549. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  550. struct tcm_loop_cmd, tl_se_cmd);
  551. struct scsi_cmnd *sc = tl_cmd->sc;
  552. pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
  553. " cdb: 0x%02x\n", sc, sc->cmnd[0]);
  554. sc->result = SAM_STAT_GOOD;
  555. set_host_byte(sc, DID_OK);
  556. if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
  557. (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
  558. scsi_set_resid(sc, se_cmd->residual_count);
  559. sc->scsi_done(sc);
  560. return 0;
  561. }
  562. static int tcm_loop_queue_status(struct se_cmd *se_cmd)
  563. {
  564. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  565. struct tcm_loop_cmd, tl_se_cmd);
  566. struct scsi_cmnd *sc = tl_cmd->sc;
  567. pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
  568. " cdb: 0x%02x\n", sc, sc->cmnd[0]);
  569. if (se_cmd->sense_buffer &&
  570. ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
  571. (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
  572. memcpy(sc->sense_buffer, se_cmd->sense_buffer,
  573. SCSI_SENSE_BUFFERSIZE);
  574. sc->result = SAM_STAT_CHECK_CONDITION;
  575. set_driver_byte(sc, DRIVER_SENSE);
  576. } else
  577. sc->result = se_cmd->scsi_status;
  578. set_host_byte(sc, DID_OK);
  579. if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
  580. (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
  581. scsi_set_resid(sc, se_cmd->residual_count);
  582. sc->scsi_done(sc);
  583. return 0;
  584. }
  585. static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
  586. {
  587. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  588. struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
  589. /*
  590. * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
  591. * and wake up the wait_queue_head_t in tcm_loop_device_reset()
  592. */
  593. atomic_set(&tl_tmr->tmr_complete, 1);
  594. wake_up(&tl_tmr->tl_tmr_wait);
  595. }
  596. static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
  597. {
  598. return;
  599. }
  600. static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
  601. {
  602. switch (tl_hba->tl_proto_id) {
  603. case SCSI_PROTOCOL_SAS:
  604. return "SAS";
  605. case SCSI_PROTOCOL_FCP:
  606. return "FCP";
  607. case SCSI_PROTOCOL_ISCSI:
  608. return "iSCSI";
  609. default:
  610. break;
  611. }
  612. return "Unknown";
  613. }
  614. /* Start items for tcm_loop_port_cit */
  615. static int tcm_loop_port_link(
  616. struct se_portal_group *se_tpg,
  617. struct se_lun *lun)
  618. {
  619. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  620. struct tcm_loop_tpg, tl_se_tpg);
  621. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  622. atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
  623. /*
  624. * Add Linux/SCSI struct scsi_device by HCTL
  625. */
  626. scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
  627. pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
  628. return 0;
  629. }
  630. static void tcm_loop_port_unlink(
  631. struct se_portal_group *se_tpg,
  632. struct se_lun *se_lun)
  633. {
  634. struct scsi_device *sd;
  635. struct tcm_loop_hba *tl_hba;
  636. struct tcm_loop_tpg *tl_tpg;
  637. tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
  638. tl_hba = tl_tpg->tl_hba;
  639. sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
  640. se_lun->unpacked_lun);
  641. if (!sd) {
  642. pr_err("Unable to locate struct scsi_device for %d:%d:"
  643. "%llu\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
  644. return;
  645. }
  646. /*
  647. * Remove Linux/SCSI struct scsi_device by HCTL
  648. */
  649. scsi_remove_device(sd);
  650. scsi_device_put(sd);
  651. atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
  652. pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
  653. }
  654. /* End items for tcm_loop_port_cit */
  655. static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_show(
  656. struct config_item *item, char *page)
  657. {
  658. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  659. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
  660. tl_se_tpg);
  661. return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type);
  662. }
  663. static ssize_t tcm_loop_tpg_attrib_fabric_prot_type_store(
  664. struct config_item *item, const char *page, size_t count)
  665. {
  666. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  667. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
  668. tl_se_tpg);
  669. unsigned long val;
  670. int ret = kstrtoul(page, 0, &val);
  671. if (ret) {
  672. pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
  673. return ret;
  674. }
  675. if (val != 0 && val != 1 && val != 3) {
  676. pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
  677. return -EINVAL;
  678. }
  679. tl_tpg->tl_fabric_prot_type = val;
  680. return count;
  681. }
  682. CONFIGFS_ATTR(tcm_loop_tpg_attrib_, fabric_prot_type);
  683. static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = {
  684. &tcm_loop_tpg_attrib_attr_fabric_prot_type,
  685. NULL,
  686. };
  687. /* Start items for tcm_loop_nexus_cit */
  688. static int tcm_loop_make_nexus(
  689. struct tcm_loop_tpg *tl_tpg,
  690. const char *name)
  691. {
  692. struct se_portal_group *se_tpg;
  693. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  694. struct tcm_loop_nexus *tl_nexus;
  695. int ret = -ENOMEM;
  696. if (tl_tpg->tl_nexus) {
  697. pr_debug("tl_tpg->tl_nexus already exists\n");
  698. return -EEXIST;
  699. }
  700. se_tpg = &tl_tpg->tl_se_tpg;
  701. tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
  702. if (!tl_nexus) {
  703. pr_err("Unable to allocate struct tcm_loop_nexus\n");
  704. return -ENOMEM;
  705. }
  706. /*
  707. * Initialize the struct se_session pointer
  708. */
  709. tl_nexus->se_sess = transport_init_session(
  710. TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
  711. if (IS_ERR(tl_nexus->se_sess)) {
  712. ret = PTR_ERR(tl_nexus->se_sess);
  713. goto out;
  714. }
  715. /*
  716. * Since we are running in 'demo mode' this call with generate a
  717. * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
  718. * Initiator port name of the passed configfs group 'name'.
  719. */
  720. tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  721. se_tpg, (unsigned char *)name);
  722. if (!tl_nexus->se_sess->se_node_acl) {
  723. transport_free_session(tl_nexus->se_sess);
  724. goto out;
  725. }
  726. /* Now, register the I_T Nexus as active. */
  727. transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
  728. tl_nexus->se_sess, tl_nexus);
  729. tl_tpg->tl_nexus = tl_nexus;
  730. pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
  731. " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
  732. name);
  733. return 0;
  734. out:
  735. kfree(tl_nexus);
  736. return ret;
  737. }
  738. static int tcm_loop_drop_nexus(
  739. struct tcm_loop_tpg *tpg)
  740. {
  741. struct se_session *se_sess;
  742. struct tcm_loop_nexus *tl_nexus;
  743. tl_nexus = tpg->tl_nexus;
  744. if (!tl_nexus)
  745. return -ENODEV;
  746. se_sess = tl_nexus->se_sess;
  747. if (!se_sess)
  748. return -ENODEV;
  749. if (atomic_read(&tpg->tl_tpg_port_count)) {
  750. pr_err("Unable to remove TCM_Loop I_T Nexus with"
  751. " active TPG port count: %d\n",
  752. atomic_read(&tpg->tl_tpg_port_count));
  753. return -EPERM;
  754. }
  755. pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
  756. " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tpg->tl_hba),
  757. tl_nexus->se_sess->se_node_acl->initiatorname);
  758. /*
  759. * Release the SCSI I_T Nexus to the emulated Target Port
  760. */
  761. transport_deregister_session(tl_nexus->se_sess);
  762. tpg->tl_nexus = NULL;
  763. kfree(tl_nexus);
  764. return 0;
  765. }
  766. /* End items for tcm_loop_nexus_cit */
  767. static ssize_t tcm_loop_tpg_nexus_show(struct config_item *item, char *page)
  768. {
  769. struct se_portal_group *se_tpg = to_tpg(item);
  770. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  771. struct tcm_loop_tpg, tl_se_tpg);
  772. struct tcm_loop_nexus *tl_nexus;
  773. ssize_t ret;
  774. tl_nexus = tl_tpg->tl_nexus;
  775. if (!tl_nexus)
  776. return -ENODEV;
  777. ret = snprintf(page, PAGE_SIZE, "%s\n",
  778. tl_nexus->se_sess->se_node_acl->initiatorname);
  779. return ret;
  780. }
  781. static ssize_t tcm_loop_tpg_nexus_store(struct config_item *item,
  782. const char *page, size_t count)
  783. {
  784. struct se_portal_group *se_tpg = to_tpg(item);
  785. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  786. struct tcm_loop_tpg, tl_se_tpg);
  787. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  788. unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
  789. int ret;
  790. /*
  791. * Shutdown the active I_T nexus if 'NULL' is passed..
  792. */
  793. if (!strncmp(page, "NULL", 4)) {
  794. ret = tcm_loop_drop_nexus(tl_tpg);
  795. return (!ret) ? count : ret;
  796. }
  797. /*
  798. * Otherwise make sure the passed virtual Initiator port WWN matches
  799. * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
  800. * tcm_loop_make_nexus()
  801. */
  802. if (strlen(page) >= TL_WWN_ADDR_LEN) {
  803. pr_err("Emulated NAA Sas Address: %s, exceeds"
  804. " max: %d\n", page, TL_WWN_ADDR_LEN);
  805. return -EINVAL;
  806. }
  807. snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
  808. ptr = strstr(i_port, "naa.");
  809. if (ptr) {
  810. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
  811. pr_err("Passed SAS Initiator Port %s does not"
  812. " match target port protoid: %s\n", i_port,
  813. tcm_loop_dump_proto_id(tl_hba));
  814. return -EINVAL;
  815. }
  816. port_ptr = &i_port[0];
  817. goto check_newline;
  818. }
  819. ptr = strstr(i_port, "fc.");
  820. if (ptr) {
  821. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
  822. pr_err("Passed FCP Initiator Port %s does not"
  823. " match target port protoid: %s\n", i_port,
  824. tcm_loop_dump_proto_id(tl_hba));
  825. return -EINVAL;
  826. }
  827. port_ptr = &i_port[3]; /* Skip over "fc." */
  828. goto check_newline;
  829. }
  830. ptr = strstr(i_port, "iqn.");
  831. if (ptr) {
  832. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
  833. pr_err("Passed iSCSI Initiator Port %s does not"
  834. " match target port protoid: %s\n", i_port,
  835. tcm_loop_dump_proto_id(tl_hba));
  836. return -EINVAL;
  837. }
  838. port_ptr = &i_port[0];
  839. goto check_newline;
  840. }
  841. pr_err("Unable to locate prefix for emulated Initiator Port:"
  842. " %s\n", i_port);
  843. return -EINVAL;
  844. /*
  845. * Clear any trailing newline for the NAA WWN
  846. */
  847. check_newline:
  848. if (i_port[strlen(i_port)-1] == '\n')
  849. i_port[strlen(i_port)-1] = '\0';
  850. ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
  851. if (ret < 0)
  852. return ret;
  853. return count;
  854. }
  855. static ssize_t tcm_loop_tpg_transport_status_show(struct config_item *item,
  856. char *page)
  857. {
  858. struct se_portal_group *se_tpg = to_tpg(item);
  859. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  860. struct tcm_loop_tpg, tl_se_tpg);
  861. const char *status = NULL;
  862. ssize_t ret = -EINVAL;
  863. switch (tl_tpg->tl_transport_status) {
  864. case TCM_TRANSPORT_ONLINE:
  865. status = "online";
  866. break;
  867. case TCM_TRANSPORT_OFFLINE:
  868. status = "offline";
  869. break;
  870. default:
  871. break;
  872. }
  873. if (status)
  874. ret = snprintf(page, PAGE_SIZE, "%s\n", status);
  875. return ret;
  876. }
  877. static ssize_t tcm_loop_tpg_transport_status_store(struct config_item *item,
  878. const char *page, size_t count)
  879. {
  880. struct se_portal_group *se_tpg = to_tpg(item);
  881. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  882. struct tcm_loop_tpg, tl_se_tpg);
  883. if (!strncmp(page, "online", 6)) {
  884. tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
  885. return count;
  886. }
  887. if (!strncmp(page, "offline", 7)) {
  888. tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
  889. if (tl_tpg->tl_nexus) {
  890. struct se_session *tl_sess = tl_tpg->tl_nexus->se_sess;
  891. core_allocate_nexus_loss_ua(tl_sess->se_node_acl);
  892. }
  893. return count;
  894. }
  895. return -EINVAL;
  896. }
  897. CONFIGFS_ATTR(tcm_loop_tpg_, nexus);
  898. CONFIGFS_ATTR(tcm_loop_tpg_, transport_status);
  899. static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
  900. &tcm_loop_tpg_attr_nexus,
  901. &tcm_loop_tpg_attr_transport_status,
  902. NULL,
  903. };
  904. /* Start items for tcm_loop_naa_cit */
  905. static struct se_portal_group *tcm_loop_make_naa_tpg(
  906. struct se_wwn *wwn,
  907. struct config_group *group,
  908. const char *name)
  909. {
  910. struct tcm_loop_hba *tl_hba = container_of(wwn,
  911. struct tcm_loop_hba, tl_hba_wwn);
  912. struct tcm_loop_tpg *tl_tpg;
  913. int ret;
  914. unsigned long tpgt;
  915. if (strstr(name, "tpgt_") != name) {
  916. pr_err("Unable to locate \"tpgt_#\" directory"
  917. " group\n");
  918. return ERR_PTR(-EINVAL);
  919. }
  920. if (kstrtoul(name+5, 10, &tpgt))
  921. return ERR_PTR(-EINVAL);
  922. if (tpgt >= TL_TPGS_PER_HBA) {
  923. pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA:"
  924. " %u\n", tpgt, TL_TPGS_PER_HBA);
  925. return ERR_PTR(-EINVAL);
  926. }
  927. tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
  928. tl_tpg->tl_hba = tl_hba;
  929. tl_tpg->tl_tpgt = tpgt;
  930. /*
  931. * Register the tl_tpg as a emulated TCM Target Endpoint
  932. */
  933. ret = core_tpg_register(wwn, &tl_tpg->tl_se_tpg, tl_hba->tl_proto_id);
  934. if (ret < 0)
  935. return ERR_PTR(-ENOMEM);
  936. pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
  937. " Target Port %s,t,0x%04lx\n", tcm_loop_dump_proto_id(tl_hba),
  938. config_item_name(&wwn->wwn_group.cg_item), tpgt);
  939. return &tl_tpg->tl_se_tpg;
  940. }
  941. static void tcm_loop_drop_naa_tpg(
  942. struct se_portal_group *se_tpg)
  943. {
  944. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  945. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  946. struct tcm_loop_tpg, tl_se_tpg);
  947. struct tcm_loop_hba *tl_hba;
  948. unsigned short tpgt;
  949. tl_hba = tl_tpg->tl_hba;
  950. tpgt = tl_tpg->tl_tpgt;
  951. /*
  952. * Release the I_T Nexus for the Virtual target link if present
  953. */
  954. tcm_loop_drop_nexus(tl_tpg);
  955. /*
  956. * Deregister the tl_tpg as a emulated TCM Target Endpoint
  957. */
  958. core_tpg_deregister(se_tpg);
  959. tl_tpg->tl_hba = NULL;
  960. tl_tpg->tl_tpgt = 0;
  961. pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
  962. " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
  963. config_item_name(&wwn->wwn_group.cg_item), tpgt);
  964. }
  965. /* End items for tcm_loop_naa_cit */
  966. /* Start items for tcm_loop_cit */
  967. static struct se_wwn *tcm_loop_make_scsi_hba(
  968. struct target_fabric_configfs *tf,
  969. struct config_group *group,
  970. const char *name)
  971. {
  972. struct tcm_loop_hba *tl_hba;
  973. struct Scsi_Host *sh;
  974. char *ptr;
  975. int ret, off = 0;
  976. tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
  977. if (!tl_hba) {
  978. pr_err("Unable to allocate struct tcm_loop_hba\n");
  979. return ERR_PTR(-ENOMEM);
  980. }
  981. /*
  982. * Determine the emulated Protocol Identifier and Target Port Name
  983. * based on the incoming configfs directory name.
  984. */
  985. ptr = strstr(name, "naa.");
  986. if (ptr) {
  987. tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
  988. goto check_len;
  989. }
  990. ptr = strstr(name, "fc.");
  991. if (ptr) {
  992. tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
  993. off = 3; /* Skip over "fc." */
  994. goto check_len;
  995. }
  996. ptr = strstr(name, "iqn.");
  997. if (!ptr) {
  998. pr_err("Unable to locate prefix for emulated Target "
  999. "Port: %s\n", name);
  1000. ret = -EINVAL;
  1001. goto out;
  1002. }
  1003. tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
  1004. check_len:
  1005. if (strlen(name) >= TL_WWN_ADDR_LEN) {
  1006. pr_err("Emulated NAA %s Address: %s, exceeds"
  1007. " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
  1008. TL_WWN_ADDR_LEN);
  1009. ret = -EINVAL;
  1010. goto out;
  1011. }
  1012. snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
  1013. /*
  1014. * Call device_register(tl_hba->dev) to register the emulated
  1015. * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
  1016. * device_register() callbacks in tcm_loop_driver_probe()
  1017. */
  1018. ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
  1019. if (ret)
  1020. goto out;
  1021. sh = tl_hba->sh;
  1022. tcm_loop_hba_no_cnt++;
  1023. pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
  1024. " %s Address: %s at Linux/SCSI Host ID: %d\n",
  1025. tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
  1026. return &tl_hba->tl_hba_wwn;
  1027. out:
  1028. kfree(tl_hba);
  1029. return ERR_PTR(ret);
  1030. }
  1031. static void tcm_loop_drop_scsi_hba(
  1032. struct se_wwn *wwn)
  1033. {
  1034. struct tcm_loop_hba *tl_hba = container_of(wwn,
  1035. struct tcm_loop_hba, tl_hba_wwn);
  1036. pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
  1037. " %s Address: %s at Linux/SCSI Host ID: %d\n",
  1038. tcm_loop_dump_proto_id(tl_hba), tl_hba->tl_wwn_address,
  1039. tl_hba->sh->host_no);
  1040. /*
  1041. * Call device_unregister() on the original tl_hba->dev.
  1042. * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
  1043. * release *tl_hba;
  1044. */
  1045. device_unregister(&tl_hba->dev);
  1046. }
  1047. /* Start items for tcm_loop_cit */
  1048. static ssize_t tcm_loop_wwn_version_show(struct config_item *item, char *page)
  1049. {
  1050. return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
  1051. }
  1052. CONFIGFS_ATTR_RO(tcm_loop_wwn_, version);
  1053. static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
  1054. &tcm_loop_wwn_attr_version,
  1055. NULL,
  1056. };
  1057. /* End items for tcm_loop_cit */
  1058. static const struct target_core_fabric_ops loop_ops = {
  1059. .module = THIS_MODULE,
  1060. .name = "loopback",
  1061. .get_fabric_name = tcm_loop_get_fabric_name,
  1062. .tpg_get_wwn = tcm_loop_get_endpoint_wwn,
  1063. .tpg_get_tag = tcm_loop_get_tag,
  1064. .tpg_check_demo_mode = tcm_loop_check_demo_mode,
  1065. .tpg_check_demo_mode_cache = tcm_loop_check_demo_mode_cache,
  1066. .tpg_check_demo_mode_write_protect =
  1067. tcm_loop_check_demo_mode_write_protect,
  1068. .tpg_check_prod_mode_write_protect =
  1069. tcm_loop_check_prod_mode_write_protect,
  1070. .tpg_check_prot_fabric_only = tcm_loop_check_prot_fabric_only,
  1071. .tpg_get_inst_index = tcm_loop_get_inst_index,
  1072. .check_stop_free = tcm_loop_check_stop_free,
  1073. .release_cmd = tcm_loop_release_cmd,
  1074. .shutdown_session = tcm_loop_shutdown_session,
  1075. .close_session = tcm_loop_close_session,
  1076. .sess_get_index = tcm_loop_sess_get_index,
  1077. .write_pending = tcm_loop_write_pending,
  1078. .write_pending_status = tcm_loop_write_pending_status,
  1079. .set_default_node_attributes = tcm_loop_set_default_node_attributes,
  1080. .get_cmd_state = tcm_loop_get_cmd_state,
  1081. .queue_data_in = tcm_loop_queue_data_in,
  1082. .queue_status = tcm_loop_queue_status,
  1083. .queue_tm_rsp = tcm_loop_queue_tm_rsp,
  1084. .aborted_task = tcm_loop_aborted_task,
  1085. .fabric_make_wwn = tcm_loop_make_scsi_hba,
  1086. .fabric_drop_wwn = tcm_loop_drop_scsi_hba,
  1087. .fabric_make_tpg = tcm_loop_make_naa_tpg,
  1088. .fabric_drop_tpg = tcm_loop_drop_naa_tpg,
  1089. .fabric_post_link = tcm_loop_port_link,
  1090. .fabric_pre_unlink = tcm_loop_port_unlink,
  1091. .tfc_wwn_attrs = tcm_loop_wwn_attrs,
  1092. .tfc_tpg_base_attrs = tcm_loop_tpg_attrs,
  1093. .tfc_tpg_attrib_attrs = tcm_loop_tpg_attrib_attrs,
  1094. };
  1095. static int __init tcm_loop_fabric_init(void)
  1096. {
  1097. int ret = -ENOMEM;
  1098. tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
  1099. if (!tcm_loop_workqueue)
  1100. goto out;
  1101. tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
  1102. sizeof(struct tcm_loop_cmd),
  1103. __alignof__(struct tcm_loop_cmd),
  1104. 0, NULL);
  1105. if (!tcm_loop_cmd_cache) {
  1106. pr_debug("kmem_cache_create() for"
  1107. " tcm_loop_cmd_cache failed\n");
  1108. goto out_destroy_workqueue;
  1109. }
  1110. ret = tcm_loop_alloc_core_bus();
  1111. if (ret)
  1112. goto out_destroy_cache;
  1113. ret = target_register_template(&loop_ops);
  1114. if (ret)
  1115. goto out_release_core_bus;
  1116. return 0;
  1117. out_release_core_bus:
  1118. tcm_loop_release_core_bus();
  1119. out_destroy_cache:
  1120. kmem_cache_destroy(tcm_loop_cmd_cache);
  1121. out_destroy_workqueue:
  1122. destroy_workqueue(tcm_loop_workqueue);
  1123. out:
  1124. return ret;
  1125. }
  1126. static void __exit tcm_loop_fabric_exit(void)
  1127. {
  1128. target_unregister_template(&loop_ops);
  1129. tcm_loop_release_core_bus();
  1130. kmem_cache_destroy(tcm_loop_cmd_cache);
  1131. destroy_workqueue(tcm_loop_workqueue);
  1132. }
  1133. MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
  1134. MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
  1135. MODULE_LICENSE("GPL");
  1136. module_init(tcm_loop_fabric_init);
  1137. module_exit(tcm_loop_fabric_exit);