tcm_loop.c 40 KB

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