tcm_loop.c 42 KB

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