tcm_loop.c 36 KB

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