tcm_loop.c 39 KB

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