tcm_loop.c 39 KB

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