tcm_loop.c 40 KB

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