tcm_loop.c 41 KB

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