tcm_loop.c 37 KB

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