tcm_loop.c 40 KB

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