tcm_loop.c 40 KB

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