tcm_loop.c 32 KB

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