tcm_loop.c 41 KB

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