tcm_loop.c 39 KB

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