tcm_loop.c 41 KB

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