tcm_loop.c 37 KB

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