tcm_loop.c 41 KB

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