tcm_loop.c 31 KB

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