tcm_loop.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  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 = TL_SCSI_MAX_CMD_LEN;
  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 u8 tcm_loop_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  453. {
  454. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  455. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  456. /*
  457. * tl_proto_id is set at tcm_loop_configfs.c:tcm_loop_make_scsi_hba()
  458. * time based on the protocol dependent prefix of the passed configfs group.
  459. *
  460. * Based upon tl_proto_id, TCM_Loop emulates the requested fabric
  461. * ProtocolID using target_core_fabric_lib.c symbols.
  462. */
  463. switch (tl_hba->tl_proto_id) {
  464. case SCSI_PROTOCOL_SAS:
  465. return sas_get_fabric_proto_ident(se_tpg);
  466. case SCSI_PROTOCOL_FCP:
  467. return fc_get_fabric_proto_ident(se_tpg);
  468. case SCSI_PROTOCOL_ISCSI:
  469. return iscsi_get_fabric_proto_ident(se_tpg);
  470. default:
  471. pr_err("Unknown tl_proto_id: 0x%02x, using"
  472. " SAS emulation\n", tl_hba->tl_proto_id);
  473. break;
  474. }
  475. return sas_get_fabric_proto_ident(se_tpg);
  476. }
  477. static char *tcm_loop_get_endpoint_wwn(struct se_portal_group *se_tpg)
  478. {
  479. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  480. /*
  481. * Return the passed NAA identifier for the SAS Target Port
  482. */
  483. return &tl_tpg->tl_hba->tl_wwn_address[0];
  484. }
  485. static u16 tcm_loop_get_tag(struct se_portal_group *se_tpg)
  486. {
  487. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  488. /*
  489. * This Tag is used when forming SCSI Name identifier in EVPD=1 0x83
  490. * to represent the SCSI Target Port.
  491. */
  492. return tl_tpg->tl_tpgt;
  493. }
  494. static u32 tcm_loop_get_default_depth(struct se_portal_group *se_tpg)
  495. {
  496. return 1;
  497. }
  498. static u32 tcm_loop_get_pr_transport_id(
  499. struct se_portal_group *se_tpg,
  500. struct se_node_acl *se_nacl,
  501. struct t10_pr_registration *pr_reg,
  502. int *format_code,
  503. unsigned char *buf)
  504. {
  505. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  506. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  507. switch (tl_hba->tl_proto_id) {
  508. case SCSI_PROTOCOL_SAS:
  509. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  510. format_code, buf);
  511. case SCSI_PROTOCOL_FCP:
  512. return fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  513. format_code, buf);
  514. case SCSI_PROTOCOL_ISCSI:
  515. return iscsi_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  516. format_code, buf);
  517. default:
  518. pr_err("Unknown tl_proto_id: 0x%02x, using"
  519. " SAS emulation\n", tl_hba->tl_proto_id);
  520. break;
  521. }
  522. return sas_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
  523. format_code, buf);
  524. }
  525. static u32 tcm_loop_get_pr_transport_id_len(
  526. struct se_portal_group *se_tpg,
  527. struct se_node_acl *se_nacl,
  528. struct t10_pr_registration *pr_reg,
  529. int *format_code)
  530. {
  531. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  532. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  533. switch (tl_hba->tl_proto_id) {
  534. case SCSI_PROTOCOL_SAS:
  535. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  536. format_code);
  537. case SCSI_PROTOCOL_FCP:
  538. return fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  539. format_code);
  540. case SCSI_PROTOCOL_ISCSI:
  541. return iscsi_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  542. format_code);
  543. default:
  544. pr_err("Unknown tl_proto_id: 0x%02x, using"
  545. " SAS emulation\n", tl_hba->tl_proto_id);
  546. break;
  547. }
  548. return sas_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
  549. format_code);
  550. }
  551. /*
  552. * Used for handling SCSI fabric dependent TransportIDs in SPC-3 and above
  553. * Persistent Reservation SPEC_I_PT=1 and PROUT REGISTER_AND_MOVE operations.
  554. */
  555. static char *tcm_loop_parse_pr_out_transport_id(
  556. struct se_portal_group *se_tpg,
  557. const char *buf,
  558. u32 *out_tid_len,
  559. char **port_nexus_ptr)
  560. {
  561. struct tcm_loop_tpg *tl_tpg = se_tpg->se_tpg_fabric_ptr;
  562. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  563. switch (tl_hba->tl_proto_id) {
  564. case SCSI_PROTOCOL_SAS:
  565. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  566. port_nexus_ptr);
  567. case SCSI_PROTOCOL_FCP:
  568. return fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  569. port_nexus_ptr);
  570. case SCSI_PROTOCOL_ISCSI:
  571. return iscsi_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  572. port_nexus_ptr);
  573. default:
  574. pr_err("Unknown tl_proto_id: 0x%02x, using"
  575. " SAS emulation\n", tl_hba->tl_proto_id);
  576. break;
  577. }
  578. return sas_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
  579. port_nexus_ptr);
  580. }
  581. /*
  582. * Returning (1) here allows for target_core_mod struct se_node_acl to be generated
  583. * based upon the incoming fabric dependent SCSI Initiator Port
  584. */
  585. static int tcm_loop_check_demo_mode(struct se_portal_group *se_tpg)
  586. {
  587. return 1;
  588. }
  589. static int tcm_loop_check_demo_mode_cache(struct se_portal_group *se_tpg)
  590. {
  591. return 0;
  592. }
  593. /*
  594. * Allow I_T Nexus full READ-WRITE access without explict Initiator Node ACLs for
  595. * local virtual Linux/SCSI LLD passthrough into VM hypervisor guest
  596. */
  597. static int tcm_loop_check_demo_mode_write_protect(struct se_portal_group *se_tpg)
  598. {
  599. return 0;
  600. }
  601. /*
  602. * Because TCM_Loop does not use explict ACLs and MappedLUNs, this will
  603. * never be called for TCM_Loop by target_core_fabric_configfs.c code.
  604. * It has been added here as a nop for target_fabric_tf_ops_check()
  605. */
  606. static int tcm_loop_check_prod_mode_write_protect(struct se_portal_group *se_tpg)
  607. {
  608. return 0;
  609. }
  610. static int tcm_loop_check_prot_fabric_only(struct se_portal_group *se_tpg)
  611. {
  612. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
  613. tl_se_tpg);
  614. return tl_tpg->tl_fabric_prot_type;
  615. }
  616. static struct se_node_acl *tcm_loop_tpg_alloc_fabric_acl(
  617. struct se_portal_group *se_tpg)
  618. {
  619. struct tcm_loop_nacl *tl_nacl;
  620. tl_nacl = kzalloc(sizeof(struct tcm_loop_nacl), GFP_KERNEL);
  621. if (!tl_nacl) {
  622. pr_err("Unable to allocate struct tcm_loop_nacl\n");
  623. return NULL;
  624. }
  625. return &tl_nacl->se_node_acl;
  626. }
  627. static void tcm_loop_tpg_release_fabric_acl(
  628. struct se_portal_group *se_tpg,
  629. struct se_node_acl *se_nacl)
  630. {
  631. struct tcm_loop_nacl *tl_nacl = container_of(se_nacl,
  632. struct tcm_loop_nacl, se_node_acl);
  633. kfree(tl_nacl);
  634. }
  635. static u32 tcm_loop_get_inst_index(struct se_portal_group *se_tpg)
  636. {
  637. return 1;
  638. }
  639. static u32 tcm_loop_sess_get_index(struct se_session *se_sess)
  640. {
  641. return 1;
  642. }
  643. static void tcm_loop_set_default_node_attributes(struct se_node_acl *se_acl)
  644. {
  645. return;
  646. }
  647. static u32 tcm_loop_get_task_tag(struct se_cmd *se_cmd)
  648. {
  649. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  650. struct tcm_loop_cmd, tl_se_cmd);
  651. return tl_cmd->sc_cmd_tag;
  652. }
  653. static int tcm_loop_get_cmd_state(struct se_cmd *se_cmd)
  654. {
  655. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  656. struct tcm_loop_cmd, tl_se_cmd);
  657. return tl_cmd->sc_cmd_state;
  658. }
  659. static int tcm_loop_shutdown_session(struct se_session *se_sess)
  660. {
  661. return 0;
  662. }
  663. static void tcm_loop_close_session(struct se_session *se_sess)
  664. {
  665. return;
  666. };
  667. static int tcm_loop_write_pending(struct se_cmd *se_cmd)
  668. {
  669. /*
  670. * Since Linux/SCSI has already sent down a struct scsi_cmnd
  671. * sc->sc_data_direction of DMA_TO_DEVICE with struct scatterlist array
  672. * memory, and memory has already been mapped to struct se_cmd->t_mem_list
  673. * format with transport_generic_map_mem_to_cmd().
  674. *
  675. * We now tell TCM to add this WRITE CDB directly into the TCM storage
  676. * object execution queue.
  677. */
  678. target_execute_cmd(se_cmd);
  679. return 0;
  680. }
  681. static int tcm_loop_write_pending_status(struct se_cmd *se_cmd)
  682. {
  683. return 0;
  684. }
  685. static int tcm_loop_queue_data_in(struct se_cmd *se_cmd)
  686. {
  687. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  688. struct tcm_loop_cmd, tl_se_cmd);
  689. struct scsi_cmnd *sc = tl_cmd->sc;
  690. pr_debug("tcm_loop_queue_data_in() called for scsi_cmnd: %p"
  691. " cdb: 0x%02x\n", sc, sc->cmnd[0]);
  692. sc->result = SAM_STAT_GOOD;
  693. set_host_byte(sc, DID_OK);
  694. if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
  695. (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
  696. scsi_set_resid(sc, se_cmd->residual_count);
  697. sc->scsi_done(sc);
  698. return 0;
  699. }
  700. static int tcm_loop_queue_status(struct se_cmd *se_cmd)
  701. {
  702. struct tcm_loop_cmd *tl_cmd = container_of(se_cmd,
  703. struct tcm_loop_cmd, tl_se_cmd);
  704. struct scsi_cmnd *sc = tl_cmd->sc;
  705. pr_debug("tcm_loop_queue_status() called for scsi_cmnd: %p"
  706. " cdb: 0x%02x\n", sc, sc->cmnd[0]);
  707. if (se_cmd->sense_buffer &&
  708. ((se_cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
  709. (se_cmd->se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
  710. memcpy(sc->sense_buffer, se_cmd->sense_buffer,
  711. SCSI_SENSE_BUFFERSIZE);
  712. sc->result = SAM_STAT_CHECK_CONDITION;
  713. set_driver_byte(sc, DRIVER_SENSE);
  714. } else
  715. sc->result = se_cmd->scsi_status;
  716. set_host_byte(sc, DID_OK);
  717. if ((se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) ||
  718. (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT))
  719. scsi_set_resid(sc, se_cmd->residual_count);
  720. sc->scsi_done(sc);
  721. return 0;
  722. }
  723. static void tcm_loop_queue_tm_rsp(struct se_cmd *se_cmd)
  724. {
  725. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  726. struct tcm_loop_tmr *tl_tmr = se_tmr->fabric_tmr_ptr;
  727. /*
  728. * The SCSI EH thread will be sleeping on se_tmr->tl_tmr_wait, go ahead
  729. * and wake up the wait_queue_head_t in tcm_loop_device_reset()
  730. */
  731. atomic_set(&tl_tmr->tmr_complete, 1);
  732. wake_up(&tl_tmr->tl_tmr_wait);
  733. }
  734. static void tcm_loop_aborted_task(struct se_cmd *se_cmd)
  735. {
  736. return;
  737. }
  738. static char *tcm_loop_dump_proto_id(struct tcm_loop_hba *tl_hba)
  739. {
  740. switch (tl_hba->tl_proto_id) {
  741. case SCSI_PROTOCOL_SAS:
  742. return "SAS";
  743. case SCSI_PROTOCOL_FCP:
  744. return "FCP";
  745. case SCSI_PROTOCOL_ISCSI:
  746. return "iSCSI";
  747. default:
  748. break;
  749. }
  750. return "Unknown";
  751. }
  752. /* Start items for tcm_loop_port_cit */
  753. static int tcm_loop_port_link(
  754. struct se_portal_group *se_tpg,
  755. struct se_lun *lun)
  756. {
  757. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  758. struct tcm_loop_tpg, tl_se_tpg);
  759. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  760. atomic_inc_mb(&tl_tpg->tl_tpg_port_count);
  761. /*
  762. * Add Linux/SCSI struct scsi_device by HCTL
  763. */
  764. scsi_add_device(tl_hba->sh, 0, tl_tpg->tl_tpgt, lun->unpacked_lun);
  765. pr_debug("TCM_Loop_ConfigFS: Port Link Successful\n");
  766. return 0;
  767. }
  768. static void tcm_loop_port_unlink(
  769. struct se_portal_group *se_tpg,
  770. struct se_lun *se_lun)
  771. {
  772. struct scsi_device *sd;
  773. struct tcm_loop_hba *tl_hba;
  774. struct tcm_loop_tpg *tl_tpg;
  775. tl_tpg = container_of(se_tpg, struct tcm_loop_tpg, tl_se_tpg);
  776. tl_hba = tl_tpg->tl_hba;
  777. sd = scsi_device_lookup(tl_hba->sh, 0, tl_tpg->tl_tpgt,
  778. se_lun->unpacked_lun);
  779. if (!sd) {
  780. pr_err("Unable to locate struct scsi_device for %d:%d:"
  781. "%d\n", 0, tl_tpg->tl_tpgt, se_lun->unpacked_lun);
  782. return;
  783. }
  784. /*
  785. * Remove Linux/SCSI struct scsi_device by HCTL
  786. */
  787. scsi_remove_device(sd);
  788. scsi_device_put(sd);
  789. atomic_dec_mb(&tl_tpg->tl_tpg_port_count);
  790. pr_debug("TCM_Loop_ConfigFS: Port Unlink Successful\n");
  791. }
  792. /* End items for tcm_loop_port_cit */
  793. static ssize_t tcm_loop_tpg_attrib_show_fabric_prot_type(
  794. struct se_portal_group *se_tpg,
  795. char *page)
  796. {
  797. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
  798. tl_se_tpg);
  799. return sprintf(page, "%d\n", tl_tpg->tl_fabric_prot_type);
  800. }
  801. static ssize_t tcm_loop_tpg_attrib_store_fabric_prot_type(
  802. struct se_portal_group *se_tpg,
  803. const char *page,
  804. size_t count)
  805. {
  806. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg, struct tcm_loop_tpg,
  807. tl_se_tpg);
  808. unsigned long val;
  809. int ret = kstrtoul(page, 0, &val);
  810. if (ret) {
  811. pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
  812. return ret;
  813. }
  814. if (val != 0 && val != 1 && val != 3) {
  815. pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
  816. return -EINVAL;
  817. }
  818. tl_tpg->tl_fabric_prot_type = val;
  819. return count;
  820. }
  821. TF_TPG_ATTRIB_ATTR(tcm_loop, fabric_prot_type, S_IRUGO | S_IWUSR);
  822. static struct configfs_attribute *tcm_loop_tpg_attrib_attrs[] = {
  823. &tcm_loop_tpg_attrib_fabric_prot_type.attr,
  824. NULL,
  825. };
  826. /* Start items for tcm_loop_nexus_cit */
  827. static int tcm_loop_make_nexus(
  828. struct tcm_loop_tpg *tl_tpg,
  829. const char *name)
  830. {
  831. struct se_portal_group *se_tpg;
  832. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  833. struct tcm_loop_nexus *tl_nexus;
  834. int ret = -ENOMEM;
  835. if (tl_tpg->tl_nexus) {
  836. pr_debug("tl_tpg->tl_nexus already exists\n");
  837. return -EEXIST;
  838. }
  839. se_tpg = &tl_tpg->tl_se_tpg;
  840. tl_nexus = kzalloc(sizeof(struct tcm_loop_nexus), GFP_KERNEL);
  841. if (!tl_nexus) {
  842. pr_err("Unable to allocate struct tcm_loop_nexus\n");
  843. return -ENOMEM;
  844. }
  845. /*
  846. * Initialize the struct se_session pointer
  847. */
  848. tl_nexus->se_sess = transport_init_session(
  849. TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS);
  850. if (IS_ERR(tl_nexus->se_sess)) {
  851. ret = PTR_ERR(tl_nexus->se_sess);
  852. goto out;
  853. }
  854. /*
  855. * Since we are running in 'demo mode' this call with generate a
  856. * struct se_node_acl for the tcm_loop struct se_portal_group with the SCSI
  857. * Initiator port name of the passed configfs group 'name'.
  858. */
  859. tl_nexus->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
  860. se_tpg, (unsigned char *)name);
  861. if (!tl_nexus->se_sess->se_node_acl) {
  862. transport_free_session(tl_nexus->se_sess);
  863. goto out;
  864. }
  865. /* Now, register the SAS I_T Nexus as active. */
  866. transport_register_session(se_tpg, tl_nexus->se_sess->se_node_acl,
  867. tl_nexus->se_sess, tl_nexus);
  868. tl_tpg->tl_nexus = tl_nexus;
  869. pr_debug("TCM_Loop_ConfigFS: Established I_T Nexus to emulated"
  870. " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tl_hba),
  871. name);
  872. return 0;
  873. out:
  874. kfree(tl_nexus);
  875. return ret;
  876. }
  877. static int tcm_loop_drop_nexus(
  878. struct tcm_loop_tpg *tpg)
  879. {
  880. struct se_session *se_sess;
  881. struct tcm_loop_nexus *tl_nexus;
  882. tl_nexus = tpg->tl_nexus;
  883. if (!tl_nexus)
  884. return -ENODEV;
  885. se_sess = tl_nexus->se_sess;
  886. if (!se_sess)
  887. return -ENODEV;
  888. if (atomic_read(&tpg->tl_tpg_port_count)) {
  889. pr_err("Unable to remove TCM_Loop I_T Nexus with"
  890. " active TPG port count: %d\n",
  891. atomic_read(&tpg->tl_tpg_port_count));
  892. return -EPERM;
  893. }
  894. pr_debug("TCM_Loop_ConfigFS: Removing I_T Nexus to emulated"
  895. " %s Initiator Port: %s\n", tcm_loop_dump_proto_id(tpg->tl_hba),
  896. tl_nexus->se_sess->se_node_acl->initiatorname);
  897. /*
  898. * Release the SCSI I_T Nexus to the emulated SAS Target Port
  899. */
  900. transport_deregister_session(tl_nexus->se_sess);
  901. tpg->tl_nexus = NULL;
  902. kfree(tl_nexus);
  903. return 0;
  904. }
  905. /* End items for tcm_loop_nexus_cit */
  906. static ssize_t tcm_loop_tpg_show_nexus(
  907. struct se_portal_group *se_tpg,
  908. char *page)
  909. {
  910. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  911. struct tcm_loop_tpg, tl_se_tpg);
  912. struct tcm_loop_nexus *tl_nexus;
  913. ssize_t ret;
  914. tl_nexus = tl_tpg->tl_nexus;
  915. if (!tl_nexus)
  916. return -ENODEV;
  917. ret = snprintf(page, PAGE_SIZE, "%s\n",
  918. tl_nexus->se_sess->se_node_acl->initiatorname);
  919. return ret;
  920. }
  921. static ssize_t tcm_loop_tpg_store_nexus(
  922. struct se_portal_group *se_tpg,
  923. const char *page,
  924. size_t count)
  925. {
  926. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  927. struct tcm_loop_tpg, tl_se_tpg);
  928. struct tcm_loop_hba *tl_hba = tl_tpg->tl_hba;
  929. unsigned char i_port[TL_WWN_ADDR_LEN], *ptr, *port_ptr;
  930. int ret;
  931. /*
  932. * Shutdown the active I_T nexus if 'NULL' is passed..
  933. */
  934. if (!strncmp(page, "NULL", 4)) {
  935. ret = tcm_loop_drop_nexus(tl_tpg);
  936. return (!ret) ? count : ret;
  937. }
  938. /*
  939. * Otherwise make sure the passed virtual Initiator port WWN matches
  940. * the fabric protocol_id set in tcm_loop_make_scsi_hba(), and call
  941. * tcm_loop_make_nexus()
  942. */
  943. if (strlen(page) >= TL_WWN_ADDR_LEN) {
  944. pr_err("Emulated NAA Sas Address: %s, exceeds"
  945. " max: %d\n", page, TL_WWN_ADDR_LEN);
  946. return -EINVAL;
  947. }
  948. snprintf(&i_port[0], TL_WWN_ADDR_LEN, "%s", page);
  949. ptr = strstr(i_port, "naa.");
  950. if (ptr) {
  951. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_SAS) {
  952. pr_err("Passed SAS Initiator Port %s does not"
  953. " match target port protoid: %s\n", i_port,
  954. tcm_loop_dump_proto_id(tl_hba));
  955. return -EINVAL;
  956. }
  957. port_ptr = &i_port[0];
  958. goto check_newline;
  959. }
  960. ptr = strstr(i_port, "fc.");
  961. if (ptr) {
  962. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_FCP) {
  963. pr_err("Passed FCP Initiator Port %s does not"
  964. " match target port protoid: %s\n", i_port,
  965. tcm_loop_dump_proto_id(tl_hba));
  966. return -EINVAL;
  967. }
  968. port_ptr = &i_port[3]; /* Skip over "fc." */
  969. goto check_newline;
  970. }
  971. ptr = strstr(i_port, "iqn.");
  972. if (ptr) {
  973. if (tl_hba->tl_proto_id != SCSI_PROTOCOL_ISCSI) {
  974. pr_err("Passed iSCSI Initiator Port %s does not"
  975. " match target port protoid: %s\n", i_port,
  976. tcm_loop_dump_proto_id(tl_hba));
  977. return -EINVAL;
  978. }
  979. port_ptr = &i_port[0];
  980. goto check_newline;
  981. }
  982. pr_err("Unable to locate prefix for emulated Initiator Port:"
  983. " %s\n", i_port);
  984. return -EINVAL;
  985. /*
  986. * Clear any trailing newline for the NAA WWN
  987. */
  988. check_newline:
  989. if (i_port[strlen(i_port)-1] == '\n')
  990. i_port[strlen(i_port)-1] = '\0';
  991. ret = tcm_loop_make_nexus(tl_tpg, port_ptr);
  992. if (ret < 0)
  993. return ret;
  994. return count;
  995. }
  996. TF_TPG_BASE_ATTR(tcm_loop, nexus, S_IRUGO | S_IWUSR);
  997. static ssize_t tcm_loop_tpg_show_transport_status(
  998. struct se_portal_group *se_tpg,
  999. char *page)
  1000. {
  1001. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  1002. struct tcm_loop_tpg, tl_se_tpg);
  1003. const char *status = NULL;
  1004. ssize_t ret = -EINVAL;
  1005. switch (tl_tpg->tl_transport_status) {
  1006. case TCM_TRANSPORT_ONLINE:
  1007. status = "online";
  1008. break;
  1009. case TCM_TRANSPORT_OFFLINE:
  1010. status = "offline";
  1011. break;
  1012. default:
  1013. break;
  1014. }
  1015. if (status)
  1016. ret = snprintf(page, PAGE_SIZE, "%s\n", status);
  1017. return ret;
  1018. }
  1019. static ssize_t tcm_loop_tpg_store_transport_status(
  1020. struct se_portal_group *se_tpg,
  1021. const char *page,
  1022. size_t count)
  1023. {
  1024. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  1025. struct tcm_loop_tpg, tl_se_tpg);
  1026. if (!strncmp(page, "online", 6)) {
  1027. tl_tpg->tl_transport_status = TCM_TRANSPORT_ONLINE;
  1028. return count;
  1029. }
  1030. if (!strncmp(page, "offline", 7)) {
  1031. tl_tpg->tl_transport_status = TCM_TRANSPORT_OFFLINE;
  1032. return count;
  1033. }
  1034. return -EINVAL;
  1035. }
  1036. TF_TPG_BASE_ATTR(tcm_loop, transport_status, S_IRUGO | S_IWUSR);
  1037. static struct configfs_attribute *tcm_loop_tpg_attrs[] = {
  1038. &tcm_loop_tpg_nexus.attr,
  1039. &tcm_loop_tpg_transport_status.attr,
  1040. NULL,
  1041. };
  1042. /* Start items for tcm_loop_naa_cit */
  1043. static struct se_portal_group *tcm_loop_make_naa_tpg(
  1044. struct se_wwn *wwn,
  1045. struct config_group *group,
  1046. const char *name)
  1047. {
  1048. struct tcm_loop_hba *tl_hba = container_of(wwn,
  1049. struct tcm_loop_hba, tl_hba_wwn);
  1050. struct tcm_loop_tpg *tl_tpg;
  1051. int ret;
  1052. unsigned long tpgt;
  1053. if (strstr(name, "tpgt_") != name) {
  1054. pr_err("Unable to locate \"tpgt_#\" directory"
  1055. " group\n");
  1056. return ERR_PTR(-EINVAL);
  1057. }
  1058. if (kstrtoul(name+5, 10, &tpgt))
  1059. return ERR_PTR(-EINVAL);
  1060. if (tpgt >= TL_TPGS_PER_HBA) {
  1061. pr_err("Passed tpgt: %lu exceeds TL_TPGS_PER_HBA:"
  1062. " %u\n", tpgt, TL_TPGS_PER_HBA);
  1063. return ERR_PTR(-EINVAL);
  1064. }
  1065. tl_tpg = &tl_hba->tl_hba_tpgs[tpgt];
  1066. tl_tpg->tl_hba = tl_hba;
  1067. tl_tpg->tl_tpgt = tpgt;
  1068. /*
  1069. * Register the tl_tpg as a emulated SAS TCM Target Endpoint
  1070. */
  1071. ret = core_tpg_register(&loop_ops, wwn, &tl_tpg->tl_se_tpg, tl_tpg,
  1072. TRANSPORT_TPG_TYPE_NORMAL);
  1073. if (ret < 0)
  1074. return ERR_PTR(-ENOMEM);
  1075. pr_debug("TCM_Loop_ConfigFS: Allocated Emulated %s"
  1076. " Target Port %s,t,0x%04lx\n", tcm_loop_dump_proto_id(tl_hba),
  1077. config_item_name(&wwn->wwn_group.cg_item), tpgt);
  1078. return &tl_tpg->tl_se_tpg;
  1079. }
  1080. static void tcm_loop_drop_naa_tpg(
  1081. struct se_portal_group *se_tpg)
  1082. {
  1083. struct se_wwn *wwn = se_tpg->se_tpg_wwn;
  1084. struct tcm_loop_tpg *tl_tpg = container_of(se_tpg,
  1085. struct tcm_loop_tpg, tl_se_tpg);
  1086. struct tcm_loop_hba *tl_hba;
  1087. unsigned short tpgt;
  1088. tl_hba = tl_tpg->tl_hba;
  1089. tpgt = tl_tpg->tl_tpgt;
  1090. /*
  1091. * Release the I_T Nexus for the Virtual SAS link if present
  1092. */
  1093. tcm_loop_drop_nexus(tl_tpg);
  1094. /*
  1095. * Deregister the tl_tpg as a emulated SAS TCM Target Endpoint
  1096. */
  1097. core_tpg_deregister(se_tpg);
  1098. tl_tpg->tl_hba = NULL;
  1099. tl_tpg->tl_tpgt = 0;
  1100. pr_debug("TCM_Loop_ConfigFS: Deallocated Emulated %s"
  1101. " Target Port %s,t,0x%04x\n", tcm_loop_dump_proto_id(tl_hba),
  1102. config_item_name(&wwn->wwn_group.cg_item), tpgt);
  1103. }
  1104. /* End items for tcm_loop_naa_cit */
  1105. /* Start items for tcm_loop_cit */
  1106. static struct se_wwn *tcm_loop_make_scsi_hba(
  1107. struct target_fabric_configfs *tf,
  1108. struct config_group *group,
  1109. const char *name)
  1110. {
  1111. struct tcm_loop_hba *tl_hba;
  1112. struct Scsi_Host *sh;
  1113. char *ptr;
  1114. int ret, off = 0;
  1115. tl_hba = kzalloc(sizeof(struct tcm_loop_hba), GFP_KERNEL);
  1116. if (!tl_hba) {
  1117. pr_err("Unable to allocate struct tcm_loop_hba\n");
  1118. return ERR_PTR(-ENOMEM);
  1119. }
  1120. /*
  1121. * Determine the emulated Protocol Identifier and Target Port Name
  1122. * based on the incoming configfs directory name.
  1123. */
  1124. ptr = strstr(name, "naa.");
  1125. if (ptr) {
  1126. tl_hba->tl_proto_id = SCSI_PROTOCOL_SAS;
  1127. goto check_len;
  1128. }
  1129. ptr = strstr(name, "fc.");
  1130. if (ptr) {
  1131. tl_hba->tl_proto_id = SCSI_PROTOCOL_FCP;
  1132. off = 3; /* Skip over "fc." */
  1133. goto check_len;
  1134. }
  1135. ptr = strstr(name, "iqn.");
  1136. if (!ptr) {
  1137. pr_err("Unable to locate prefix for emulated Target "
  1138. "Port: %s\n", name);
  1139. ret = -EINVAL;
  1140. goto out;
  1141. }
  1142. tl_hba->tl_proto_id = SCSI_PROTOCOL_ISCSI;
  1143. check_len:
  1144. if (strlen(name) >= TL_WWN_ADDR_LEN) {
  1145. pr_err("Emulated NAA %s Address: %s, exceeds"
  1146. " max: %d\n", name, tcm_loop_dump_proto_id(tl_hba),
  1147. TL_WWN_ADDR_LEN);
  1148. ret = -EINVAL;
  1149. goto out;
  1150. }
  1151. snprintf(&tl_hba->tl_wwn_address[0], TL_WWN_ADDR_LEN, "%s", &name[off]);
  1152. /*
  1153. * Call device_register(tl_hba->dev) to register the emulated
  1154. * Linux/SCSI LLD of type struct Scsi_Host at tl_hba->sh after
  1155. * device_register() callbacks in tcm_loop_driver_probe()
  1156. */
  1157. ret = tcm_loop_setup_hba_bus(tl_hba, tcm_loop_hba_no_cnt);
  1158. if (ret)
  1159. goto out;
  1160. sh = tl_hba->sh;
  1161. tcm_loop_hba_no_cnt++;
  1162. pr_debug("TCM_Loop_ConfigFS: Allocated emulated Target"
  1163. " %s Address: %s at Linux/SCSI Host ID: %d\n",
  1164. tcm_loop_dump_proto_id(tl_hba), name, sh->host_no);
  1165. return &tl_hba->tl_hba_wwn;
  1166. out:
  1167. kfree(tl_hba);
  1168. return ERR_PTR(ret);
  1169. }
  1170. static void tcm_loop_drop_scsi_hba(
  1171. struct se_wwn *wwn)
  1172. {
  1173. struct tcm_loop_hba *tl_hba = container_of(wwn,
  1174. struct tcm_loop_hba, tl_hba_wwn);
  1175. pr_debug("TCM_Loop_ConfigFS: Deallocating emulated Target"
  1176. " SAS Address: %s at Linux/SCSI Host ID: %d\n",
  1177. tl_hba->tl_wwn_address, tl_hba->sh->host_no);
  1178. /*
  1179. * Call device_unregister() on the original tl_hba->dev.
  1180. * tcm_loop_fabric_scsi.c:tcm_loop_release_adapter() will
  1181. * release *tl_hba;
  1182. */
  1183. device_unregister(&tl_hba->dev);
  1184. }
  1185. /* Start items for tcm_loop_cit */
  1186. static ssize_t tcm_loop_wwn_show_attr_version(
  1187. struct target_fabric_configfs *tf,
  1188. char *page)
  1189. {
  1190. return sprintf(page, "TCM Loopback Fabric module %s\n", TCM_LOOP_VERSION);
  1191. }
  1192. TF_WWN_ATTR_RO(tcm_loop, version);
  1193. static struct configfs_attribute *tcm_loop_wwn_attrs[] = {
  1194. &tcm_loop_wwn_version.attr,
  1195. NULL,
  1196. };
  1197. /* End items for tcm_loop_cit */
  1198. static const struct target_core_fabric_ops loop_ops = {
  1199. .module = THIS_MODULE,
  1200. .name = "loopback",
  1201. .get_fabric_name = tcm_loop_get_fabric_name,
  1202. .get_fabric_proto_ident = tcm_loop_get_fabric_proto_ident,
  1203. .tpg_get_wwn = tcm_loop_get_endpoint_wwn,
  1204. .tpg_get_tag = tcm_loop_get_tag,
  1205. .tpg_get_default_depth = tcm_loop_get_default_depth,
  1206. .tpg_get_pr_transport_id = tcm_loop_get_pr_transport_id,
  1207. .tpg_get_pr_transport_id_len = tcm_loop_get_pr_transport_id_len,
  1208. .tpg_parse_pr_out_transport_id = tcm_loop_parse_pr_out_transport_id,
  1209. .tpg_check_demo_mode = tcm_loop_check_demo_mode,
  1210. .tpg_check_demo_mode_cache = tcm_loop_check_demo_mode_cache,
  1211. .tpg_check_demo_mode_write_protect =
  1212. tcm_loop_check_demo_mode_write_protect,
  1213. .tpg_check_prod_mode_write_protect =
  1214. tcm_loop_check_prod_mode_write_protect,
  1215. .tpg_check_prot_fabric_only = tcm_loop_check_prot_fabric_only,
  1216. .tpg_alloc_fabric_acl = tcm_loop_tpg_alloc_fabric_acl,
  1217. .tpg_release_fabric_acl = tcm_loop_tpg_release_fabric_acl,
  1218. .tpg_get_inst_index = tcm_loop_get_inst_index,
  1219. .check_stop_free = tcm_loop_check_stop_free,
  1220. .release_cmd = tcm_loop_release_cmd,
  1221. .shutdown_session = tcm_loop_shutdown_session,
  1222. .close_session = tcm_loop_close_session,
  1223. .sess_get_index = tcm_loop_sess_get_index,
  1224. .write_pending = tcm_loop_write_pending,
  1225. .write_pending_status = tcm_loop_write_pending_status,
  1226. .set_default_node_attributes = tcm_loop_set_default_node_attributes,
  1227. .get_task_tag = tcm_loop_get_task_tag,
  1228. .get_cmd_state = tcm_loop_get_cmd_state,
  1229. .queue_data_in = tcm_loop_queue_data_in,
  1230. .queue_status = tcm_loop_queue_status,
  1231. .queue_tm_rsp = tcm_loop_queue_tm_rsp,
  1232. .aborted_task = tcm_loop_aborted_task,
  1233. .fabric_make_wwn = tcm_loop_make_scsi_hba,
  1234. .fabric_drop_wwn = tcm_loop_drop_scsi_hba,
  1235. .fabric_make_tpg = tcm_loop_make_naa_tpg,
  1236. .fabric_drop_tpg = tcm_loop_drop_naa_tpg,
  1237. .fabric_post_link = tcm_loop_port_link,
  1238. .fabric_pre_unlink = tcm_loop_port_unlink,
  1239. .tfc_wwn_attrs = tcm_loop_wwn_attrs,
  1240. .tfc_tpg_base_attrs = tcm_loop_tpg_attrs,
  1241. .tfc_tpg_attrib_attrs = tcm_loop_tpg_attrib_attrs,
  1242. };
  1243. static int __init tcm_loop_fabric_init(void)
  1244. {
  1245. int ret = -ENOMEM;
  1246. tcm_loop_workqueue = alloc_workqueue("tcm_loop", 0, 0);
  1247. if (!tcm_loop_workqueue)
  1248. goto out;
  1249. tcm_loop_cmd_cache = kmem_cache_create("tcm_loop_cmd_cache",
  1250. sizeof(struct tcm_loop_cmd),
  1251. __alignof__(struct tcm_loop_cmd),
  1252. 0, NULL);
  1253. if (!tcm_loop_cmd_cache) {
  1254. pr_debug("kmem_cache_create() for"
  1255. " tcm_loop_cmd_cache failed\n");
  1256. goto out_destroy_workqueue;
  1257. }
  1258. ret = tcm_loop_alloc_core_bus();
  1259. if (ret)
  1260. goto out_destroy_cache;
  1261. ret = target_register_template(&loop_ops);
  1262. if (ret)
  1263. goto out_release_core_bus;
  1264. return 0;
  1265. out_release_core_bus:
  1266. tcm_loop_release_core_bus();
  1267. out_destroy_cache:
  1268. kmem_cache_destroy(tcm_loop_cmd_cache);
  1269. out_destroy_workqueue:
  1270. destroy_workqueue(tcm_loop_workqueue);
  1271. out:
  1272. return ret;
  1273. }
  1274. static void __exit tcm_loop_fabric_exit(void)
  1275. {
  1276. target_unregister_template(&loop_ops);
  1277. tcm_loop_release_core_bus();
  1278. kmem_cache_destroy(tcm_loop_cmd_cache);
  1279. destroy_workqueue(tcm_loop_workqueue);
  1280. }
  1281. MODULE_DESCRIPTION("TCM loopback virtual Linux/SCSI fabric module");
  1282. MODULE_AUTHOR("Nicholas A. Bellinger <nab@risingtidesystems.com>");
  1283. MODULE_LICENSE("GPL");
  1284. module_init(tcm_loop_fabric_init);
  1285. module_exit(tcm_loop_fabric_exit);