tcm_loop.c 41 KB

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