tcm_loop.c 40 KB

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