tcm_loop.c 41 KB

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