tcm_loop.c 41 KB

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