tcm_loop.c 32 KB

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