tcm_loop.c 40 KB

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