tcm_loop.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  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/libsas.h> /* For TASK_ATTR_* */
  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 = TASK_ATTR_HOQ;
  89. break;
  90. case ORDERED_QUEUE_TAG:
  91. sam_task_attr = TASK_ATTR_ORDERED;
  92. break;
  93. default:
  94. sam_task_attr = TASK_ATTR_SIMPLE;
  95. break;
  96. }
  97. } else
  98. sam_task_attr = TASK_ATTR_SIMPLE;
  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, TASK_ATTR_SIMPLE,
  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 (!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 u64 tcm_loop_pack_lun(unsigned int lun)
  836. {
  837. u64 result;
  838. /* LSB of lun into byte 1 big-endian */
  839. result = ((lun & 0xff) << 8);
  840. /* use flat space addressing method */
  841. result |= 0x40 | ((lun >> 8) & 0x3f);
  842. return cpu_to_le64(result);
  843. }
  844. static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
  845. {
  846. switch (tl_hba->tl_proto_id) {
  847. case SCSI_PROTOCOL_SAS:
  848. return "SAS";
  849. case SCSI_PROTOCOL_FCP:
  850. return "FCP";
  851. case SCSI_PROTOCOL_ISCSI:
  852. return "iSCSI";
  853. default:
  854. break;
  855. }
  856. return "Unknown";
  857. }
  858. /* Start items for tcm_loop_port_cit */
  859. static int tcm_loop_port_link(
  860. struct se_portal_group *se_tpg,
  861. struct se_lun *lun)
  862. {
  863. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  864. struct tcm_loop_tpg, tl_se_tpg);
  865. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  866. atomic_inc(&tl_tpg->tl_tpg_port_count);
  867. smp_mb__after_atomic_inc();
  868. /*
  869. * Add Linux/SCSI struct scsi_device by HCTL
  870. */
  871. scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
  872. printk(KERN_INFO "TCM_Loop_ConfigFS: Port Link Successful\n");
  873. return 0;
  874. }
  875. static void tcm_loop_port_unlink(
  876. struct se_portal_group *se_tpg,
  877. struct se_lun *se_lun)
  878. {
  879. struct scsi_device *sd;
  880. struct tcm_loop_hba *tl_hba;
  881. struct tcm_loop_tpg *tl_tpg;
  882. tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
  883. tl_hba = tl_tpg->tl_hba;
  884. sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
  885. se_lun->unpacked_lun);
  886. if (!sd) {
  887. printk(KERN_ERR "Unable to locate struct scsi_device for %d:%d:"
  888. "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
  889. return;
  890. }
  891. /*
  892. * Remove Linux/SCSI struct scsi_device by HCTL
  893. */
  894. scsi_remove_device(sd);
  895. scsi_device_put(sd);
  896. atomic_dec(&tl_tpg->tl_tpg_port_count);
  897. smp_mb__after_atomic_dec();
  898. printk(KERN_INFO "TCM_Loop_ConfigFS: Port Unlink Successful\n");
  899. }
  900. /* End items for tcm_loop_port_cit */
  901. /* Start items for tcm_loop_nexus_cit */
  902. static int tcm_loop_make_nexus(
  903. struct tcm_loop_tpg *tl_tpg,
  904. const char *name)
  905. {
  906. struct se_portal_group *se_tpg;
  907. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  908. struct tcm_loop_nexus *tl_nexus;
  909. if (tl_tpg->tl_hba->tl_nexus) {
  910. printk(KERN_INFO "tl_tpg->tl_hba->tl_nexus already exists\n");
  911. return -EEXIST;
  912. }
  913. se_tpg = &tl_tpg->tl_se_tpg;
  914. tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
  915. if (!tl_nexus) {
  916. printk(KERN_ERR "Unable to allocate struct tcm_loop_nexus\n");
  917. return -ENOMEM;
  918. }
  919. /*
  920. * Initialize the struct se_session pointer
  921. */
  922. tl_nexus->se_sess = transport_init_session();
  923. if (!tl_nexus->se_sess)
  924. goto out;
  925. /*
  926. * Since we are running in 'demo mode' this call with generate a
  927. * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
  928. * Initiator port name of the passed configfs group 'name'.
  929. */
  930. tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  931. se_tpg, (unsigned char *)name);
  932. if (!tl_nexus->se_sess->se_node_acl) {
  933. transport_free_session(tl_nexus->se_sess);
  934. goto out;
  935. }
  936. /*
  937. * Now, register the SAS I_T Nexus as active with the call to
  938. * transport_register_session()
  939. */
  940. __transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
  941. tl_nexus->se_sess, (void *)tl_nexus);
  942. tl_tpg->tl_hba->tl_nexus = tl_nexus;
  943. printk(KERN_INFO "TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
  944. " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
  945. name);
  946. return 0;
  947. out:
  948. kfree(tl_nexus);
  949. return -ENOMEM;
  950. }
  951. static int tcm_loop_drop_nexus(
  952. struct tcm_loop_tpg *tpg)
  953. {
  954. struct se_session *se_sess;
  955. struct tcm_loop_nexus *tl_nexus;
  956. struct tcm_loop_hba *tl_hba = tpg->tl_hba;
  957. tl_nexus = tpg->tl_hba->tl_nexus;
  958. if (!tl_nexus)
  959. return -ENODEV;
  960. se_sess = tl_nexus->se_sess;
  961. if (!se_sess)
  962. return -ENODEV;
  963. if (atomic_read(&tpg->tl_tpg_port_count)) {
  964. printk(KERN_ERR "Unable to remove TCM_Loop I_T Nexus with"
  965. " active TPG port count: %d\n",
  966. atomic_read(&tpg->tl_tpg_port_count));
  967. return -EPERM;
  968. }
  969. printk(KERN_INFO "TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
  970. " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
  971. tl_nexus->se_sess->se_node_acl->initiatorname);
  972. /*
  973. * Release the SCSI I_T Nexus to the emulated SAS Target Port
  974. */
  975. transport_deregister_session(tl_nexus->se_sess);
  976. tpg->tl_hba->tl_nexus = NULL;
  977. kfree(tl_nexus);
  978. return 0;
  979. }
  980. /* End items for tcm_loop_nexus_cit */
  981. static ssize_t tcm_loop_tpg_show_nexus(
  982. struct se_portal_group *se_tpg,
  983. char *page)
  984. {
  985. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  986. struct tcm_loop_tpg, tl_se_tpg);
  987. struct tcm_loop_nexus *tl_nexus;
  988. ssize_t ret;
  989. tl_nexus = tl_tpg->tl_hba->tl_nexus;
  990. if (!tl_nexus)
  991. return -ENODEV;
  992. ret = snprintf(page, PAGE_SIZE, "%s\n",
  993. tl_nexus->se_sess->se_node_acl->initiatorname);
  994. return ret;
  995. }
  996. static ssize_t tcm_loop_tpg_store_nexus(
  997. struct se_portal_group *se_tpg,
  998. const char *page,
  999. size_t count)
  1000. {
  1001. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  1002. struct tcm_loop_tpg, tl_se_tpg);
  1003. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  1004. unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
  1005. int ret;
  1006. /*
  1007. * Shutdown the active I_T nexus if 'NULL' is passed..
  1008. */
  1009. if (!strncmp(page, "NULL", 4)) {
  1010. ret = tcm_loop_drop_nexus(tl_tpg);
  1011. return (!ret) ? count : ret;
  1012. }
  1013. /*
  1014. * Otherwise make sure the passed virtual Initiator port WWN matches
  1015. * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
  1016. * tcm_loop_make_nexus()
  1017. */
  1018. if (strlen(page) > TL_WWN_ADDR_LEN) {
  1019. printk(KERN_ERR "Emulated NAA Sas Address: %s, exceeds"
  1020. " max: %d\n", page, TL_WWN_ADDR_LEN);
  1021. return -EINVAL;
  1022. }
  1023. snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
  1024. ptr = strstr(i_port, "naa.");
  1025. if (ptr) {
  1026. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
  1027. printk(KERN_ERR "Passed SAS Initiator Port %s does not"
  1028. " match target port protoid: %s\n", i_port,
  1029. tcm_loop_dump_proto_id(tl_hba));
  1030. return -EINVAL;
  1031. }
  1032. port_ptr = &i_port[0];
  1033. goto check_newline;
  1034. }
  1035. ptr = strstr(i_port, "fc.");
  1036. if (ptr) {
  1037. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
  1038. printk(KERN_ERR "Passed FCP Initiator Port %s does not"
  1039. " match target port protoid: %s\n", i_port,
  1040. tcm_loop_dump_proto_id(tl_hba));
  1041. return -EINVAL;
  1042. }
  1043. port_ptr = &i_port[3]; /* Skip over "fc." */
  1044. goto check_newline;
  1045. }
  1046. ptr = strstr(i_port, "iqn.");
  1047. if (ptr) {
  1048. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
  1049. printk(KERN_ERR "Passed iSCSI Initiator Port %s does not"
  1050. " match target port protoid: %s\n", i_port,
  1051. tcm_loop_dump_proto_id(tl_hba));
  1052. return -EINVAL;
  1053. }
  1054. port_ptr = &i_port[0];
  1055. goto check_newline;
  1056. }
  1057. printk(KERN_ERR "Unable to locate prefix for emulated Initiator Port:"
  1058. " %s\n", i_port);
  1059. return -EINVAL;
  1060. /*
  1061. * Clear any trailing newline for the NAA WWN
  1062. */
  1063. check_newline:
  1064. if (i_port[strlen(i_port)-1] == '\n')
  1065. i_port[strlen(i_port)-1] = '\0';
  1066. ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
  1067. if (ret < 0)
  1068. return ret;
  1069. return count;
  1070. }
  1071. TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
  1072. static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
  1073. &tcm_loop_tpg_nexus.attr,
  1074. NULL,
  1075. };
  1076. /* Start items for tcm_loop_naa_cit */
  1077. struct se_portal_group *tcm_loop_make_naa_tpg(
  1078. struct se_wwn *wwn,
  1079. struct config_group *group,
  1080. const char *name)
  1081. {
  1082. struct tcm_loop_hba *tl_hba = container_of(wwn,
  1083. struct tcm_loop_hba, tl_hba_wwn);
  1084. struct tcm_loop_tpg *tl_tpg;
  1085. char *tpgt_str, *end_ptr;
  1086. int ret;
  1087. unsigned short int tpgt;
  1088. tpgt_str = strstr(name, "tpgt_");
  1089. if (!tpgt_str) {
  1090. printk(KERN_ERR "Unable to locate \"tpgt_#\" directory"
  1091. " group\n");
  1092. return ERR_PTR(-EINVAL);
  1093. }
  1094. tpgt_str += 5; /* Skip ahead of "tpgt_" */
  1095. tpgt = (unsigned short int) simple_strtoul(tpgt_str, &end_ptr, 0);
  1096. if (tpgt > TL_TPGS_PER_HBA) {
  1097. printk(KERN_ERR "Passed tpgt: %hu exceeds TL_TPGS_PER_HBA:"
  1098. " %u\n", tpgt, TL_TPGS_PER_HBA);
  1099. return ERR_PTR(-EINVAL);
  1100. }
  1101. tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
  1102. tl_tpg->tl_hba = tl_hba;
  1103. tl_tpg->tl_tpgt = tpgt;
  1104. /*
  1105. * Register the tl_tpg as a emulated SAS TCM Target Endpoint
  1106. */
  1107. ret = core_tpg_register(&tcm_loop_fabric_configfs->tf_ops,
  1108. wwn, &tl_tpg->tl_se_tpg, (void *)tl_tpg,
  1109. TRANSPORT_TPG_TYPE_NORMAL);
  1110. if (ret < 0)
  1111. return ERR_PTR(-ENOMEM);
  1112. printk(KERN_INFO "TCM_Loop_ConfigFS: Allocated Emulated %s"
  1113. " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
  1114. config_item_name(&wwn->wwn_group.cg_item), tpgt);
  1115. return &tl_tpg->tl_se_tpg;
  1116. }
  1117. void tcm_loop_drop_naa_tpg(
  1118. struct se_portal_group *se_tpg)
  1119. {
  1120. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  1121. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  1122. struct tcm_loop_tpg, tl_se_tpg);
  1123. struct tcm_loop_hba *tl_hba;
  1124. unsigned short tpgt;
  1125. tl_hba = tl_tpg->tl_hba;
  1126. tpgt = tl_tpg->tl_tpgt;
  1127. /*
  1128. * Release the I_T Nexus for the Virtual SAS link if present
  1129. */
  1130. tcm_loop_drop_nexus(tl_tpg);
  1131. /*
  1132. * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
  1133. */
  1134. core_tpg_deregister(se_tpg);
  1135. printk(KERN_INFO "TCM_Loop_ConfigFS: Deallocated Emulated %s"
  1136. " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
  1137. config_item_name(&wwn->wwn_group.cg_item), tpgt);
  1138. }
  1139. /* End items for tcm_loop_naa_cit */
  1140. /* Start items for tcm_loop_cit */
  1141. struct se_wwn *tcm_loop_make_scsi_hba(
  1142. struct target_fabric_configfs *tf,
  1143. struct config_group *group,
  1144. const char *name)
  1145. {
  1146. struct tcm_loop_hba *tl_hba;
  1147. struct Scsi_Host *sh;
  1148. char *ptr;
  1149. int ret, off = 0;
  1150. tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
  1151. if (!tl_hba) {
  1152. printk(KERN_ERR "Unable to allocate struct tcm_loop_hba\n");
  1153. return ERR_PTR(-ENOMEM);
  1154. }
  1155. /*
  1156. * Determine the emulated Protocol Identifier and Target Port Name
  1157. * based on the incoming configfs directory name.
  1158. */
  1159. ptr = strstr(name, "naa.");
  1160. if (ptr) {
  1161. tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
  1162. goto check_len;
  1163. }
  1164. ptr = strstr(name, "fc.");
  1165. if (ptr) {
  1166. tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
  1167. off = 3; /* Skip over "fc." */
  1168. goto check_len;
  1169. }
  1170. ptr = strstr(name, "iqn.");
  1171. if (ptr) {
  1172. tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
  1173. goto check_len;
  1174. }
  1175. printk(KERN_ERR "Unable to locate prefix for emulated Target Port:"
  1176. " %s\n", name);
  1177. return ERR_PTR(-EINVAL);
  1178. check_len:
  1179. if (strlen(name) > TL_WWN_ADDR_LEN) {
  1180. printk(KERN_ERR "Emulated NAA %s Address: %s, exceeds"
  1181. " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
  1182. TL_WWN_ADDR_LEN);
  1183. kfree(tl_hba);
  1184. return ERR_PTR(-EINVAL);
  1185. }
  1186. snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
  1187. /*
  1188. * Call device_register(tl_hba->dev) to register the emulated
  1189. * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
  1190. * device_register() callbacks in tcm_loop_driver_probe()
  1191. */
  1192. ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
  1193. if (ret)
  1194. goto out;
  1195. sh = tl_hba->sh;
  1196. tcm_loop_hba_no_cnt++;
  1197. printk(KERN_INFO "TCM_Loop_ConfigFS: Allocated emulated Target"
  1198. " %s Address: %s at Linux/SCSI Host ID: %d\n",
  1199. tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
  1200. return &tl_hba->tl_hba_wwn;
  1201. out:
  1202. kfree(tl_hba);
  1203. return ERR_PTR(ret);
  1204. }
  1205. void tcm_loop_drop_scsi_hba(
  1206. struct se_wwn *wwn)
  1207. {
  1208. struct tcm_loop_hba *tl_hba = container_of(wwn,
  1209. struct tcm_loop_hba, tl_hba_wwn);
  1210. int host_no = tl_hba->sh->host_no;
  1211. /*
  1212. * Call device_unregister() on the original tl_hba->dev.
  1213. * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
  1214. * release *tl_hba;
  1215. */
  1216. device_unregister(&tl_hba->dev);
  1217. printk(KERN_INFO "TCM_Loop_ConfigFS: Deallocated emulated Target"
  1218. " SAS Address: %s at Linux/SCSI Host ID: %d\n",
  1219. config_item_name(&wwn->wwn_group.cg_item), host_no);
  1220. }
  1221. /* Start items for tcm_loop_cit */
  1222. static ssize_t tcm_loop_wwn_show_attr_version(
  1223. struct target_fabric_configfs *tf,
  1224. char *page)
  1225. {
  1226. return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
  1227. }
  1228. TF_WWN_ATTR_RO(tcm_loop, version);
  1229. static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
  1230. &tcm_loop_wwn_version.attr,
  1231. NULL,
  1232. };
  1233. /* End items for tcm_loop_cit */
  1234. static int tcm_loop_register_configfs(void)
  1235. {
  1236. struct target_fabric_configfs *fabric;
  1237. struct config_group *tf_cg;
  1238. int ret;
  1239. /*
  1240. * Set the TCM Loop HBA counter to zero
  1241. */
  1242. tcm_loop_hba_no_cnt = 0;
  1243. /*
  1244. * Register the top level struct config_item_type with TCM core
  1245. */
  1246. fabric = target_fabric_configfs_init(THIS_MODULE, "loopback");
  1247. if (!fabric) {
  1248. printk(KERN_ERR "tcm_loop_register_configfs() failed!\n");
  1249. return -1;
  1250. }
  1251. /*
  1252. * Setup the fabric API of function pointers used by target_core_mod
  1253. */
  1254. fabric->tf_ops.get_fabric_name = &tcm_loop_get_fabric_name;
  1255. fabric->tf_ops.get_fabric_proto_ident = &tcm_loop_get_fabric_proto_ident;
  1256. fabric->tf_ops.tpg_get_wwn = &tcm_loop_get_endpoint_wwn;
  1257. fabric->tf_ops.tpg_get_tag = &tcm_loop_get_tag;
  1258. fabric->tf_ops.tpg_get_default_depth = &tcm_loop_get_default_depth;
  1259. fabric->tf_ops.tpg_get_pr_transport_id = &tcm_loop_get_pr_transport_id;
  1260. fabric->tf_ops.tpg_get_pr_transport_id_len =
  1261. &tcm_loop_get_pr_transport_id_len;
  1262. fabric->tf_ops.tpg_parse_pr_out_transport_id =
  1263. &tcm_loop_parse_pr_out_transport_id;
  1264. fabric->tf_ops.tpg_check_demo_mode = &tcm_loop_check_demo_mode;
  1265. fabric->tf_ops.tpg_check_demo_mode_cache =
  1266. &tcm_loop_check_demo_mode_cache;
  1267. fabric->tf_ops.tpg_check_demo_mode_write_protect =
  1268. &tcm_loop_check_demo_mode_write_protect;
  1269. fabric->tf_ops.tpg_check_prod_mode_write_protect =
  1270. &tcm_loop_check_prod_mode_write_protect;
  1271. /*
  1272. * The TCM loopback fabric module runs in demo-mode to a local
  1273. * virtual SCSI device, so fabric dependent initator ACLs are
  1274. * not required.
  1275. */
  1276. fabric->tf_ops.tpg_alloc_fabric_acl = &tcm_loop_tpg_alloc_fabric_acl;
  1277. fabric->tf_ops.tpg_release_fabric_acl =
  1278. &tcm_loop_tpg_release_fabric_acl;
  1279. fabric->tf_ops.tpg_get_inst_index = &tcm_loop_get_inst_index;
  1280. /*
  1281. * Since tcm_loop is mapping physical memory from Linux/SCSI
  1282. * struct scatterlist arrays for each struct scsi_cmnd I/O,
  1283. * we do not need TCM to allocate a iovec array for
  1284. * virtual memory address mappings
  1285. */
  1286. fabric->tf_ops.alloc_cmd_iovecs = NULL;
  1287. /*
  1288. * Used for setting up remaining TCM resources in process context
  1289. */
  1290. fabric->tf_ops.new_cmd_map = &tcm_loop_new_cmd_map;
  1291. fabric->tf_ops.check_stop_free = &tcm_loop_check_stop_free;
  1292. fabric->tf_ops.release_cmd_to_pool = &tcm_loop_deallocate_core_cmd;
  1293. fabric->tf_ops.release_cmd_direct = &tcm_loop_deallocate_core_cmd;
  1294. fabric->tf_ops.shutdown_session = &tcm_loop_shutdown_session;
  1295. fabric->tf_ops.close_session = &tcm_loop_close_session;
  1296. fabric->tf_ops.stop_session = &tcm_loop_stop_session;
  1297. fabric->tf_ops.fall_back_to_erl0 = &tcm_loop_fall_back_to_erl0;
  1298. fabric->tf_ops.sess_logged_in = &tcm_loop_sess_logged_in;
  1299. fabric->tf_ops.sess_get_index = &tcm_loop_sess_get_index;
  1300. fabric->tf_ops.sess_get_initiator_sid = NULL;
  1301. fabric->tf_ops.write_pending = &tcm_loop_write_pending;
  1302. fabric->tf_ops.write_pending_status = &tcm_loop_write_pending_status;
  1303. /*
  1304. * Not used for TCM loopback
  1305. */
  1306. fabric->tf_ops.set_default_node_attributes =
  1307. &tcm_loop_set_default_node_attributes;
  1308. fabric->tf_ops.get_task_tag = &tcm_loop_get_task_tag;
  1309. fabric->tf_ops.get_cmd_state = &tcm_loop_get_cmd_state;
  1310. fabric->tf_ops.new_cmd_failure = &tcm_loop_new_cmd_failure;
  1311. fabric->tf_ops.queue_data_in = &tcm_loop_queue_data_in;
  1312. fabric->tf_ops.queue_status = &tcm_loop_queue_status;
  1313. fabric->tf_ops.queue_tm_rsp = &tcm_loop_queue_tm_rsp;
  1314. fabric->tf_ops.set_fabric_sense_len = &tcm_loop_set_fabric_sense_len;
  1315. fabric->tf_ops.get_fabric_sense_len = &tcm_loop_get_fabric_sense_len;
  1316. fabric->tf_ops.is_state_remove = &tcm_loop_is_state_remove;
  1317. fabric->tf_ops.pack_lun = &tcm_loop_pack_lun;
  1318. tf_cg = &fabric->tf_group;
  1319. /*
  1320. * Setup function pointers for generic logic in target_core_fabric_configfs.c
  1321. */
  1322. fabric->tf_ops.fabric_make_wwn = &tcm_loop_make_scsi_hba;
  1323. fabric->tf_ops.fabric_drop_wwn = &tcm_loop_drop_scsi_hba;
  1324. fabric->tf_ops.fabric_make_tpg = &tcm_loop_make_naa_tpg;
  1325. fabric->tf_ops.fabric_drop_tpg = &tcm_loop_drop_naa_tpg;
  1326. /*
  1327. * fabric_post_link() and fabric_pre_unlink() are used for
  1328. * registration and release of TCM Loop Virtual SCSI LUNs.
  1329. */
  1330. fabric->tf_ops.fabric_post_link = &tcm_loop_port_link;
  1331. fabric->tf_ops.fabric_pre_unlink = &tcm_loop_port_unlink;
  1332. fabric->tf_ops.fabric_make_np = NULL;
  1333. fabric->tf_ops.fabric_drop_np = NULL;
  1334. /*
  1335. * Setup default attribute lists for various fabric->tf_cit_tmpl
  1336. */
  1337. TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_loop_wwn_attrs;
  1338. TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_loop_tpg_attrs;
  1339. TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
  1340. TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
  1341. TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
  1342. /*
  1343. * Once fabric->tf_ops has been setup, now register the fabric for
  1344. * use within TCM
  1345. */
  1346. ret = target_fabric_configfs_register(fabric);
  1347. if (ret < 0) {
  1348. printk(KERN_ERR "target_fabric_configfs_register() for"
  1349. " TCM_Loop failed!\n");
  1350. target_fabric_configfs_free(fabric);
  1351. return -1;
  1352. }
  1353. /*
  1354. * Setup our local pointer to *fabric.
  1355. */
  1356. tcm_loop_fabric_configfs = fabric;
  1357. printk(KERN_INFO "TCM_LOOP[0] - Set fabric ->"
  1358. " tcm_loop_fabric_configfs\n");
  1359. return 0;
  1360. }
  1361. static void tcm_loop_deregister_configfs(void)
  1362. {
  1363. if (!tcm_loop_fabric_configfs)
  1364. return;
  1365. target_fabric_configfs_deregister(tcm_loop_fabric_configfs);
  1366. tcm_loop_fabric_configfs = NULL;
  1367. printk(KERN_INFO "TCM_LOOP[0] - Cleared"
  1368. " tcm_loop_fabric_configfs\n");
  1369. }
  1370. static int __init tcm_loop_fabric_init(void)
  1371. {
  1372. int ret;
  1373. tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
  1374. sizeof(struct tcm_loop_cmd),
  1375. __alignof__(struct tcm_loop_cmd),
  1376. 0, NULL);
  1377. if (!tcm_loop_cmd_cache) {
  1378. printk(KERN_ERR "kmem_cache_create() for"
  1379. " tcm_loop_cmd_cache failed\n");
  1380. return -ENOMEM;
  1381. }
  1382. ret = tcm_loop_alloc_core_bus();
  1383. if (ret)
  1384. return ret;
  1385. ret = tcm_loop_register_configfs();
  1386. if (ret) {
  1387. tcm_loop_release_core_bus();
  1388. return ret;
  1389. }
  1390. return 0;
  1391. }
  1392. static void __exit tcm_loop_fabric_exit(void)
  1393. {
  1394. tcm_loop_deregister_configfs();
  1395. tcm_loop_release_core_bus();
  1396. kmem_cache_destroy(tcm_loop_cmd_cache);
  1397. }
  1398. MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
  1399. MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
  1400. MODULE_LICENSE("GPL");
  1401. module_init(tcm_loop_fabric_init);
  1402. module_exit(tcm_loop_fabric_exit);