tcm_loop.c 41 KB

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