target_core_device.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709
  1. /*******************************************************************************
  2. * Filename: target_core_device.c (based on iscsi_target_device.c)
  3. *
  4. * This file contains the TCM Virtual Device and Disk Transport
  5. * agnostic related functions.
  6. *
  7. * (c) Copyright 2003-2013 Datera, Inc.
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. *
  25. ******************************************************************************/
  26. #include <linux/net.h>
  27. #include <linux/string.h>
  28. #include <linux/delay.h>
  29. #include <linux/timer.h>
  30. #include <linux/slab.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/kthread.h>
  33. #include <linux/in.h>
  34. #include <linux/export.h>
  35. #include <net/sock.h>
  36. #include <net/tcp.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_device.h>
  39. #include <target/target_core_base.h>
  40. #include <target/target_core_backend.h>
  41. #include <target/target_core_fabric.h>
  42. #include "target_core_internal.h"
  43. #include "target_core_alua.h"
  44. #include "target_core_pr.h"
  45. #include "target_core_ua.h"
  46. DEFINE_MUTEX(g_device_mutex);
  47. LIST_HEAD(g_device_list);
  48. static struct se_hba *lun0_hba;
  49. /* not static, needed by tpg.c */
  50. struct se_device *g_lun0_dev;
  51. sense_reason_t
  52. transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
  53. {
  54. struct se_lun *se_lun = NULL;
  55. struct se_session *se_sess = se_cmd->se_sess;
  56. struct se_device *dev;
  57. unsigned long flags;
  58. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  59. return TCM_NON_EXISTENT_LUN;
  60. spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
  61. se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
  62. if (se_cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  63. struct se_dev_entry *deve = se_cmd->se_deve;
  64. deve->total_cmds++;
  65. if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
  66. (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)) {
  67. pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
  68. " Access for 0x%08x\n",
  69. se_cmd->se_tfo->get_fabric_name(),
  70. unpacked_lun);
  71. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  72. return TCM_WRITE_PROTECTED;
  73. }
  74. if (se_cmd->data_direction == DMA_TO_DEVICE)
  75. deve->write_bytes += se_cmd->data_length;
  76. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  77. deve->read_bytes += se_cmd->data_length;
  78. se_lun = deve->se_lun;
  79. se_cmd->se_lun = deve->se_lun;
  80. se_cmd->pr_res_key = deve->pr_res_key;
  81. se_cmd->orig_fe_lun = unpacked_lun;
  82. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  83. percpu_ref_get(&se_lun->lun_ref);
  84. se_cmd->lun_ref_active = true;
  85. }
  86. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  87. if (!se_lun) {
  88. /*
  89. * Use the se_portal_group->tpg_virt_lun0 to allow for
  90. * REPORT_LUNS, et al to be returned when no active
  91. * MappedLUN=0 exists for this Initiator Port.
  92. */
  93. if (unpacked_lun != 0) {
  94. pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  95. " Access for 0x%08x\n",
  96. se_cmd->se_tfo->get_fabric_name(),
  97. unpacked_lun);
  98. return TCM_NON_EXISTENT_LUN;
  99. }
  100. /*
  101. * Force WRITE PROTECT for virtual LUN 0
  102. */
  103. if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
  104. (se_cmd->data_direction != DMA_NONE))
  105. return TCM_WRITE_PROTECTED;
  106. se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  107. se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  108. se_cmd->orig_fe_lun = 0;
  109. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  110. percpu_ref_get(&se_lun->lun_ref);
  111. se_cmd->lun_ref_active = true;
  112. }
  113. /* Directly associate cmd with se_dev */
  114. se_cmd->se_dev = se_lun->lun_se_dev;
  115. dev = se_lun->lun_se_dev;
  116. atomic_long_inc(&dev->num_cmds);
  117. if (se_cmd->data_direction == DMA_TO_DEVICE)
  118. atomic_long_add(se_cmd->data_length, &dev->write_bytes);
  119. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  120. atomic_long_add(se_cmd->data_length, &dev->read_bytes);
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(transport_lookup_cmd_lun);
  124. int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
  125. {
  126. struct se_dev_entry *deve;
  127. struct se_lun *se_lun = NULL;
  128. struct se_session *se_sess = se_cmd->se_sess;
  129. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  130. unsigned long flags;
  131. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  132. return -ENODEV;
  133. spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
  134. se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
  135. deve = se_cmd->se_deve;
  136. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  137. se_tmr->tmr_lun = deve->se_lun;
  138. se_cmd->se_lun = deve->se_lun;
  139. se_lun = deve->se_lun;
  140. se_cmd->pr_res_key = deve->pr_res_key;
  141. se_cmd->orig_fe_lun = unpacked_lun;
  142. }
  143. spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
  144. if (!se_lun) {
  145. pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  146. " Access for 0x%08x\n",
  147. se_cmd->se_tfo->get_fabric_name(),
  148. unpacked_lun);
  149. return -ENODEV;
  150. }
  151. /* Directly associate cmd with se_dev */
  152. se_cmd->se_dev = se_lun->lun_se_dev;
  153. se_tmr->tmr_dev = se_lun->lun_se_dev;
  154. spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
  155. list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
  156. spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(transport_lookup_tmr_lun);
  160. /*
  161. * This function is called from core_scsi3_emulate_pro_register_and_move()
  162. * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_ref_count
  163. * when a matching rtpi is found.
  164. */
  165. struct se_dev_entry *core_get_se_deve_from_rtpi(
  166. struct se_node_acl *nacl,
  167. u16 rtpi)
  168. {
  169. struct se_dev_entry *deve;
  170. struct se_lun *lun;
  171. struct se_port *port;
  172. struct se_portal_group *tpg = nacl->se_tpg;
  173. u32 i;
  174. spin_lock_irq(&nacl->device_list_lock);
  175. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  176. deve = nacl->device_list[i];
  177. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  178. continue;
  179. lun = deve->se_lun;
  180. if (!lun) {
  181. pr_err("%s device entries device pointer is"
  182. " NULL, but Initiator has access.\n",
  183. tpg->se_tpg_tfo->get_fabric_name());
  184. continue;
  185. }
  186. port = lun->lun_sep;
  187. if (!port) {
  188. pr_err("%s device entries device pointer is"
  189. " NULL, but Initiator has access.\n",
  190. tpg->se_tpg_tfo->get_fabric_name());
  191. continue;
  192. }
  193. if (port->sep_rtpi != rtpi)
  194. continue;
  195. atomic_inc_mb(&deve->pr_ref_count);
  196. spin_unlock_irq(&nacl->device_list_lock);
  197. return deve;
  198. }
  199. spin_unlock_irq(&nacl->device_list_lock);
  200. return NULL;
  201. }
  202. int core_free_device_list_for_node(
  203. struct se_node_acl *nacl,
  204. struct se_portal_group *tpg)
  205. {
  206. struct se_dev_entry *deve;
  207. struct se_lun *lun;
  208. u32 i;
  209. if (!nacl->device_list)
  210. return 0;
  211. spin_lock_irq(&nacl->device_list_lock);
  212. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  213. deve = nacl->device_list[i];
  214. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  215. continue;
  216. if (!deve->se_lun) {
  217. pr_err("%s device entries device pointer is"
  218. " NULL, but Initiator has access.\n",
  219. tpg->se_tpg_tfo->get_fabric_name());
  220. continue;
  221. }
  222. lun = deve->se_lun;
  223. spin_unlock_irq(&nacl->device_list_lock);
  224. core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
  225. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  226. spin_lock_irq(&nacl->device_list_lock);
  227. }
  228. spin_unlock_irq(&nacl->device_list_lock);
  229. array_free(nacl->device_list, TRANSPORT_MAX_LUNS_PER_TPG);
  230. nacl->device_list = NULL;
  231. return 0;
  232. }
  233. void core_update_device_list_access(
  234. u32 mapped_lun,
  235. u32 lun_access,
  236. struct se_node_acl *nacl)
  237. {
  238. struct se_dev_entry *deve;
  239. spin_lock_irq(&nacl->device_list_lock);
  240. deve = nacl->device_list[mapped_lun];
  241. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  242. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  243. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  244. } else {
  245. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  246. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  247. }
  248. spin_unlock_irq(&nacl->device_list_lock);
  249. }
  250. /* core_enable_device_list_for_node():
  251. *
  252. *
  253. */
  254. int core_enable_device_list_for_node(
  255. struct se_lun *lun,
  256. struct se_lun_acl *lun_acl,
  257. u32 mapped_lun,
  258. u32 lun_access,
  259. struct se_node_acl *nacl,
  260. struct se_portal_group *tpg)
  261. {
  262. struct se_port *port = lun->lun_sep;
  263. struct se_dev_entry *deve;
  264. spin_lock_irq(&nacl->device_list_lock);
  265. deve = nacl->device_list[mapped_lun];
  266. /*
  267. * Check if the call is handling demo mode -> explicit LUN ACL
  268. * transition. This transition must be for the same struct se_lun
  269. * + mapped_lun that was setup in demo mode..
  270. */
  271. if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
  272. if (deve->se_lun_acl != NULL) {
  273. pr_err("struct se_dev_entry->se_lun_acl"
  274. " already set for demo mode -> explicit"
  275. " LUN ACL transition\n");
  276. spin_unlock_irq(&nacl->device_list_lock);
  277. return -EINVAL;
  278. }
  279. if (deve->se_lun != lun) {
  280. pr_err("struct se_dev_entry->se_lun does"
  281. " match passed struct se_lun for demo mode"
  282. " -> explicit LUN ACL transition\n");
  283. spin_unlock_irq(&nacl->device_list_lock);
  284. return -EINVAL;
  285. }
  286. deve->se_lun_acl = lun_acl;
  287. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  288. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  289. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  290. } else {
  291. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  292. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  293. }
  294. spin_unlock_irq(&nacl->device_list_lock);
  295. return 0;
  296. }
  297. deve->se_lun = lun;
  298. deve->se_lun_acl = lun_acl;
  299. deve->mapped_lun = mapped_lun;
  300. deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS;
  301. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  302. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  303. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  304. } else {
  305. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  306. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  307. }
  308. deve->creation_time = get_jiffies_64();
  309. deve->attach_count++;
  310. spin_unlock_irq(&nacl->device_list_lock);
  311. spin_lock_bh(&port->sep_alua_lock);
  312. list_add_tail(&deve->alua_port_list, &port->sep_alua_list);
  313. spin_unlock_bh(&port->sep_alua_lock);
  314. return 0;
  315. }
  316. /* core_disable_device_list_for_node():
  317. *
  318. *
  319. */
  320. int core_disable_device_list_for_node(
  321. struct se_lun *lun,
  322. struct se_lun_acl *lun_acl,
  323. u32 mapped_lun,
  324. u32 lun_access,
  325. struct se_node_acl *nacl,
  326. struct se_portal_group *tpg)
  327. {
  328. struct se_port *port = lun->lun_sep;
  329. struct se_dev_entry *deve = nacl->device_list[mapped_lun];
  330. /*
  331. * If the MappedLUN entry is being disabled, the entry in
  332. * port->sep_alua_list must be removed now before clearing the
  333. * struct se_dev_entry pointers below as logic in
  334. * core_alua_do_transition_tg_pt() depends on these being present.
  335. *
  336. * deve->se_lun_acl will be NULL for demo-mode created LUNs
  337. * that have not been explicitly converted to MappedLUNs ->
  338. * struct se_lun_acl, but we remove deve->alua_port_list from
  339. * port->sep_alua_list. This also means that active UAs and
  340. * NodeACL context specific PR metadata for demo-mode
  341. * MappedLUN *deve will be released below..
  342. */
  343. spin_lock_bh(&port->sep_alua_lock);
  344. list_del(&deve->alua_port_list);
  345. spin_unlock_bh(&port->sep_alua_lock);
  346. /*
  347. * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
  348. * PR operation to complete.
  349. */
  350. while (atomic_read(&deve->pr_ref_count) != 0)
  351. cpu_relax();
  352. spin_lock_irq(&nacl->device_list_lock);
  353. /*
  354. * Disable struct se_dev_entry LUN ACL mapping
  355. */
  356. core_scsi3_ua_release_all(deve);
  357. deve->se_lun = NULL;
  358. deve->se_lun_acl = NULL;
  359. deve->lun_flags = 0;
  360. deve->creation_time = 0;
  361. deve->attach_count--;
  362. spin_unlock_irq(&nacl->device_list_lock);
  363. core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
  364. return 0;
  365. }
  366. /* core_clear_lun_from_tpg():
  367. *
  368. *
  369. */
  370. void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
  371. {
  372. struct se_node_acl *nacl;
  373. struct se_dev_entry *deve;
  374. u32 i;
  375. spin_lock_irq(&tpg->acl_node_lock);
  376. list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
  377. spin_unlock_irq(&tpg->acl_node_lock);
  378. spin_lock_irq(&nacl->device_list_lock);
  379. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  380. deve = nacl->device_list[i];
  381. if (lun != deve->se_lun)
  382. continue;
  383. spin_unlock_irq(&nacl->device_list_lock);
  384. core_disable_device_list_for_node(lun, NULL,
  385. deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
  386. nacl, tpg);
  387. spin_lock_irq(&nacl->device_list_lock);
  388. }
  389. spin_unlock_irq(&nacl->device_list_lock);
  390. spin_lock_irq(&tpg->acl_node_lock);
  391. }
  392. spin_unlock_irq(&tpg->acl_node_lock);
  393. }
  394. static struct se_port *core_alloc_port(struct se_device *dev)
  395. {
  396. struct se_port *port, *port_tmp;
  397. port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
  398. if (!port) {
  399. pr_err("Unable to allocate struct se_port\n");
  400. return ERR_PTR(-ENOMEM);
  401. }
  402. INIT_LIST_HEAD(&port->sep_alua_list);
  403. INIT_LIST_HEAD(&port->sep_list);
  404. atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  405. spin_lock_init(&port->sep_alua_lock);
  406. mutex_init(&port->sep_tg_pt_md_mutex);
  407. spin_lock(&dev->se_port_lock);
  408. if (dev->dev_port_count == 0x0000ffff) {
  409. pr_warn("Reached dev->dev_port_count =="
  410. " 0x0000ffff\n");
  411. spin_unlock(&dev->se_port_lock);
  412. return ERR_PTR(-ENOSPC);
  413. }
  414. again:
  415. /*
  416. * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
  417. * Here is the table from spc4r17 section 7.7.3.8.
  418. *
  419. * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
  420. *
  421. * Code Description
  422. * 0h Reserved
  423. * 1h Relative port 1, historically known as port A
  424. * 2h Relative port 2, historically known as port B
  425. * 3h to FFFFh Relative port 3 through 65 535
  426. */
  427. port->sep_rtpi = dev->dev_rpti_counter++;
  428. if (!port->sep_rtpi)
  429. goto again;
  430. list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
  431. /*
  432. * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
  433. * for 16-bit wrap..
  434. */
  435. if (port->sep_rtpi == port_tmp->sep_rtpi)
  436. goto again;
  437. }
  438. spin_unlock(&dev->se_port_lock);
  439. return port;
  440. }
  441. static void core_export_port(
  442. struct se_device *dev,
  443. struct se_portal_group *tpg,
  444. struct se_port *port,
  445. struct se_lun *lun)
  446. {
  447. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
  448. spin_lock(&dev->se_port_lock);
  449. spin_lock(&lun->lun_sep_lock);
  450. port->sep_tpg = tpg;
  451. port->sep_lun = lun;
  452. lun->lun_sep = port;
  453. spin_unlock(&lun->lun_sep_lock);
  454. list_add_tail(&port->sep_list, &dev->dev_sep_list);
  455. spin_unlock(&dev->se_port_lock);
  456. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
  457. !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
  458. tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
  459. if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
  460. pr_err("Unable to allocate t10_alua_tg_pt"
  461. "_gp_member_t\n");
  462. return;
  463. }
  464. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  465. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  466. dev->t10_alua.default_tg_pt_gp);
  467. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  468. pr_debug("%s/%s: Adding to default ALUA Target Port"
  469. " Group: alua/default_tg_pt_gp\n",
  470. dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
  471. }
  472. dev->dev_port_count++;
  473. port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
  474. }
  475. /*
  476. * Called with struct se_device->se_port_lock spinlock held.
  477. */
  478. static void core_release_port(struct se_device *dev, struct se_port *port)
  479. __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
  480. {
  481. /*
  482. * Wait for any port reference for PR ALL_TG_PT=1 operation
  483. * to complete in __core_scsi3_alloc_registration()
  484. */
  485. spin_unlock(&dev->se_port_lock);
  486. if (atomic_read(&port->sep_tg_pt_ref_cnt))
  487. cpu_relax();
  488. spin_lock(&dev->se_port_lock);
  489. core_alua_free_tg_pt_gp_mem(port);
  490. list_del(&port->sep_list);
  491. dev->dev_port_count--;
  492. kfree(port);
  493. }
  494. int core_dev_export(
  495. struct se_device *dev,
  496. struct se_portal_group *tpg,
  497. struct se_lun *lun)
  498. {
  499. struct se_hba *hba = dev->se_hba;
  500. struct se_port *port;
  501. port = core_alloc_port(dev);
  502. if (IS_ERR(port))
  503. return PTR_ERR(port);
  504. lun->lun_se_dev = dev;
  505. spin_lock(&hba->device_lock);
  506. dev->export_count++;
  507. spin_unlock(&hba->device_lock);
  508. core_export_port(dev, tpg, port, lun);
  509. return 0;
  510. }
  511. void core_dev_unexport(
  512. struct se_device *dev,
  513. struct se_portal_group *tpg,
  514. struct se_lun *lun)
  515. {
  516. struct se_hba *hba = dev->se_hba;
  517. struct se_port *port = lun->lun_sep;
  518. spin_lock(&lun->lun_sep_lock);
  519. if (lun->lun_se_dev == NULL) {
  520. spin_unlock(&lun->lun_sep_lock);
  521. return;
  522. }
  523. spin_unlock(&lun->lun_sep_lock);
  524. spin_lock(&dev->se_port_lock);
  525. core_release_port(dev, port);
  526. spin_unlock(&dev->se_port_lock);
  527. spin_lock(&hba->device_lock);
  528. dev->export_count--;
  529. spin_unlock(&hba->device_lock);
  530. lun->lun_sep = NULL;
  531. lun->lun_se_dev = NULL;
  532. }
  533. static void se_release_vpd_for_dev(struct se_device *dev)
  534. {
  535. struct t10_vpd *vpd, *vpd_tmp;
  536. spin_lock(&dev->t10_wwn.t10_vpd_lock);
  537. list_for_each_entry_safe(vpd, vpd_tmp,
  538. &dev->t10_wwn.t10_vpd_list, vpd_list) {
  539. list_del(&vpd->vpd_list);
  540. kfree(vpd);
  541. }
  542. spin_unlock(&dev->t10_wwn.t10_vpd_lock);
  543. }
  544. static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
  545. {
  546. u32 aligned_max_sectors;
  547. u32 alignment;
  548. /*
  549. * Limit max_sectors to a PAGE_SIZE aligned value for modern
  550. * transport_allocate_data_tasks() operation.
  551. */
  552. alignment = max(1ul, PAGE_SIZE / block_size);
  553. aligned_max_sectors = rounddown(max_sectors, alignment);
  554. if (max_sectors != aligned_max_sectors)
  555. pr_info("Rounding down aligned max_sectors from %u to %u\n",
  556. max_sectors, aligned_max_sectors);
  557. return aligned_max_sectors;
  558. }
  559. bool se_dev_check_wce(struct se_device *dev)
  560. {
  561. bool wce = false;
  562. if (dev->transport->get_write_cache)
  563. wce = dev->transport->get_write_cache(dev);
  564. else if (dev->dev_attrib.emulate_write_cache > 0)
  565. wce = true;
  566. return wce;
  567. }
  568. int se_dev_set_max_unmap_lba_count(
  569. struct se_device *dev,
  570. u32 max_unmap_lba_count)
  571. {
  572. dev->dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
  573. pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
  574. dev, dev->dev_attrib.max_unmap_lba_count);
  575. return 0;
  576. }
  577. EXPORT_SYMBOL(se_dev_set_max_unmap_lba_count);
  578. int se_dev_set_max_unmap_block_desc_count(
  579. struct se_device *dev,
  580. u32 max_unmap_block_desc_count)
  581. {
  582. dev->dev_attrib.max_unmap_block_desc_count =
  583. max_unmap_block_desc_count;
  584. pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
  585. dev, dev->dev_attrib.max_unmap_block_desc_count);
  586. return 0;
  587. }
  588. EXPORT_SYMBOL(se_dev_set_max_unmap_block_desc_count);
  589. int se_dev_set_unmap_granularity(
  590. struct se_device *dev,
  591. u32 unmap_granularity)
  592. {
  593. dev->dev_attrib.unmap_granularity = unmap_granularity;
  594. pr_debug("dev[%p]: Set unmap_granularity: %u\n",
  595. dev, dev->dev_attrib.unmap_granularity);
  596. return 0;
  597. }
  598. EXPORT_SYMBOL(se_dev_set_unmap_granularity);
  599. int se_dev_set_unmap_granularity_alignment(
  600. struct se_device *dev,
  601. u32 unmap_granularity_alignment)
  602. {
  603. dev->dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
  604. pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
  605. dev, dev->dev_attrib.unmap_granularity_alignment);
  606. return 0;
  607. }
  608. EXPORT_SYMBOL(se_dev_set_unmap_granularity_alignment);
  609. int se_dev_set_max_write_same_len(
  610. struct se_device *dev,
  611. u32 max_write_same_len)
  612. {
  613. dev->dev_attrib.max_write_same_len = max_write_same_len;
  614. pr_debug("dev[%p]: Set max_write_same_len: %u\n",
  615. dev, dev->dev_attrib.max_write_same_len);
  616. return 0;
  617. }
  618. EXPORT_SYMBOL(se_dev_set_max_write_same_len);
  619. static void dev_set_t10_wwn_model_alias(struct se_device *dev)
  620. {
  621. const char *configname;
  622. configname = config_item_name(&dev->dev_group.cg_item);
  623. if (strlen(configname) >= 16) {
  624. pr_warn("dev[%p]: Backstore name '%s' is too long for "
  625. "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
  626. configname);
  627. }
  628. snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
  629. }
  630. int se_dev_set_emulate_model_alias(struct se_device *dev, int flag)
  631. {
  632. if (dev->export_count) {
  633. pr_err("dev[%p]: Unable to change model alias"
  634. " while export_count is %d\n",
  635. dev, dev->export_count);
  636. return -EINVAL;
  637. }
  638. if (flag != 0 && flag != 1) {
  639. pr_err("Illegal value %d\n", flag);
  640. return -EINVAL;
  641. }
  642. if (flag) {
  643. dev_set_t10_wwn_model_alias(dev);
  644. } else {
  645. strncpy(&dev->t10_wwn.model[0],
  646. dev->transport->inquiry_prod, 16);
  647. }
  648. dev->dev_attrib.emulate_model_alias = flag;
  649. return 0;
  650. }
  651. EXPORT_SYMBOL(se_dev_set_emulate_model_alias);
  652. int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
  653. {
  654. if (flag != 0 && flag != 1) {
  655. pr_err("Illegal value %d\n", flag);
  656. return -EINVAL;
  657. }
  658. if (flag) {
  659. pr_err("dpo_emulated not supported\n");
  660. return -EINVAL;
  661. }
  662. return 0;
  663. }
  664. EXPORT_SYMBOL(se_dev_set_emulate_dpo);
  665. int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
  666. {
  667. if (flag != 0 && flag != 1) {
  668. pr_err("Illegal value %d\n", flag);
  669. return -EINVAL;
  670. }
  671. if (flag &&
  672. dev->transport->get_write_cache) {
  673. pr_warn("emulate_fua_write not supported for this device, ignoring\n");
  674. return 0;
  675. }
  676. if (dev->export_count) {
  677. pr_err("emulate_fua_write cannot be changed with active"
  678. " exports: %d\n", dev->export_count);
  679. return -EINVAL;
  680. }
  681. dev->dev_attrib.emulate_fua_write = flag;
  682. pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
  683. dev, dev->dev_attrib.emulate_fua_write);
  684. return 0;
  685. }
  686. EXPORT_SYMBOL(se_dev_set_emulate_fua_write);
  687. int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
  688. {
  689. if (flag != 0 && flag != 1) {
  690. pr_err("Illegal value %d\n", flag);
  691. return -EINVAL;
  692. }
  693. if (flag) {
  694. pr_err("ua read emulated not supported\n");
  695. return -EINVAL;
  696. }
  697. return 0;
  698. }
  699. EXPORT_SYMBOL(se_dev_set_emulate_fua_read);
  700. int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
  701. {
  702. if (flag != 0 && flag != 1) {
  703. pr_err("Illegal value %d\n", flag);
  704. return -EINVAL;
  705. }
  706. if (flag &&
  707. dev->transport->get_write_cache) {
  708. pr_err("emulate_write_cache not supported for this device\n");
  709. return -EINVAL;
  710. }
  711. if (dev->export_count) {
  712. pr_err("emulate_write_cache cannot be changed with active"
  713. " exports: %d\n", dev->export_count);
  714. return -EINVAL;
  715. }
  716. dev->dev_attrib.emulate_write_cache = flag;
  717. pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
  718. dev, dev->dev_attrib.emulate_write_cache);
  719. return 0;
  720. }
  721. EXPORT_SYMBOL(se_dev_set_emulate_write_cache);
  722. int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
  723. {
  724. if ((flag != 0) && (flag != 1) && (flag != 2)) {
  725. pr_err("Illegal value %d\n", flag);
  726. return -EINVAL;
  727. }
  728. if (dev->export_count) {
  729. pr_err("dev[%p]: Unable to change SE Device"
  730. " UA_INTRLCK_CTRL while export_count is %d\n",
  731. dev, dev->export_count);
  732. return -EINVAL;
  733. }
  734. dev->dev_attrib.emulate_ua_intlck_ctrl = flag;
  735. pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
  736. dev, dev->dev_attrib.emulate_ua_intlck_ctrl);
  737. return 0;
  738. }
  739. EXPORT_SYMBOL(se_dev_set_emulate_ua_intlck_ctrl);
  740. int se_dev_set_emulate_tas(struct se_device *dev, int flag)
  741. {
  742. if ((flag != 0) && (flag != 1)) {
  743. pr_err("Illegal value %d\n", flag);
  744. return -EINVAL;
  745. }
  746. if (dev->export_count) {
  747. pr_err("dev[%p]: Unable to change SE Device TAS while"
  748. " export_count is %d\n",
  749. dev, dev->export_count);
  750. return -EINVAL;
  751. }
  752. dev->dev_attrib.emulate_tas = flag;
  753. pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
  754. dev, (dev->dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
  755. return 0;
  756. }
  757. EXPORT_SYMBOL(se_dev_set_emulate_tas);
  758. int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
  759. {
  760. if ((flag != 0) && (flag != 1)) {
  761. pr_err("Illegal value %d\n", flag);
  762. return -EINVAL;
  763. }
  764. /*
  765. * We expect this value to be non-zero when generic Block Layer
  766. * Discard supported is detected iblock_create_virtdevice().
  767. */
  768. if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
  769. pr_err("Generic Block Discard not supported\n");
  770. return -ENOSYS;
  771. }
  772. dev->dev_attrib.emulate_tpu = flag;
  773. pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
  774. dev, flag);
  775. return 0;
  776. }
  777. EXPORT_SYMBOL(se_dev_set_emulate_tpu);
  778. int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
  779. {
  780. if ((flag != 0) && (flag != 1)) {
  781. pr_err("Illegal value %d\n", flag);
  782. return -EINVAL;
  783. }
  784. /*
  785. * We expect this value to be non-zero when generic Block Layer
  786. * Discard supported is detected iblock_create_virtdevice().
  787. */
  788. if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
  789. pr_err("Generic Block Discard not supported\n");
  790. return -ENOSYS;
  791. }
  792. dev->dev_attrib.emulate_tpws = flag;
  793. pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
  794. dev, flag);
  795. return 0;
  796. }
  797. EXPORT_SYMBOL(se_dev_set_emulate_tpws);
  798. int se_dev_set_emulate_caw(struct se_device *dev, int flag)
  799. {
  800. if (flag != 0 && flag != 1) {
  801. pr_err("Illegal value %d\n", flag);
  802. return -EINVAL;
  803. }
  804. dev->dev_attrib.emulate_caw = flag;
  805. pr_debug("dev[%p]: SE Device CompareAndWrite (AtomicTestandSet): %d\n",
  806. dev, flag);
  807. return 0;
  808. }
  809. EXPORT_SYMBOL(se_dev_set_emulate_caw);
  810. int se_dev_set_emulate_3pc(struct se_device *dev, int flag)
  811. {
  812. if (flag != 0 && flag != 1) {
  813. pr_err("Illegal value %d\n", flag);
  814. return -EINVAL;
  815. }
  816. dev->dev_attrib.emulate_3pc = flag;
  817. pr_debug("dev[%p]: SE Device 3rd Party Copy (EXTENDED_COPY): %d\n",
  818. dev, flag);
  819. return 0;
  820. }
  821. EXPORT_SYMBOL(se_dev_set_emulate_3pc);
  822. int se_dev_set_pi_prot_type(struct se_device *dev, int flag)
  823. {
  824. int rc, old_prot = dev->dev_attrib.pi_prot_type;
  825. if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
  826. pr_err("Illegal value %d for pi_prot_type\n", flag);
  827. return -EINVAL;
  828. }
  829. if (flag == 2) {
  830. pr_err("DIF TYPE2 protection currently not supported\n");
  831. return -ENOSYS;
  832. }
  833. if (dev->dev_attrib.hw_pi_prot_type) {
  834. pr_warn("DIF protection enabled on underlying hardware,"
  835. " ignoring\n");
  836. return 0;
  837. }
  838. if (!dev->transport->init_prot || !dev->transport->free_prot) {
  839. /* 0 is only allowed value for non-supporting backends */
  840. if (flag == 0)
  841. return 0;
  842. pr_err("DIF protection not supported by backend: %s\n",
  843. dev->transport->name);
  844. return -ENOSYS;
  845. }
  846. if (!(dev->dev_flags & DF_CONFIGURED)) {
  847. pr_err("DIF protection requires device to be configured\n");
  848. return -ENODEV;
  849. }
  850. if (dev->export_count) {
  851. pr_err("dev[%p]: Unable to change SE Device PROT type while"
  852. " export_count is %d\n", dev, dev->export_count);
  853. return -EINVAL;
  854. }
  855. dev->dev_attrib.pi_prot_type = flag;
  856. if (flag && !old_prot) {
  857. rc = dev->transport->init_prot(dev);
  858. if (rc) {
  859. dev->dev_attrib.pi_prot_type = old_prot;
  860. return rc;
  861. }
  862. } else if (!flag && old_prot) {
  863. dev->transport->free_prot(dev);
  864. }
  865. pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
  866. return 0;
  867. }
  868. EXPORT_SYMBOL(se_dev_set_pi_prot_type);
  869. int se_dev_set_pi_prot_format(struct se_device *dev, int flag)
  870. {
  871. int rc;
  872. if (!flag)
  873. return 0;
  874. if (flag != 1) {
  875. pr_err("Illegal value %d for pi_prot_format\n", flag);
  876. return -EINVAL;
  877. }
  878. if (!dev->transport->format_prot) {
  879. pr_err("DIF protection format not supported by backend %s\n",
  880. dev->transport->name);
  881. return -ENOSYS;
  882. }
  883. if (!(dev->dev_flags & DF_CONFIGURED)) {
  884. pr_err("DIF protection format requires device to be configured\n");
  885. return -ENODEV;
  886. }
  887. if (dev->export_count) {
  888. pr_err("dev[%p]: Unable to format SE Device PROT type while"
  889. " export_count is %d\n", dev, dev->export_count);
  890. return -EINVAL;
  891. }
  892. rc = dev->transport->format_prot(dev);
  893. if (rc)
  894. return rc;
  895. pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
  896. return 0;
  897. }
  898. EXPORT_SYMBOL(se_dev_set_pi_prot_format);
  899. int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
  900. {
  901. if ((flag != 0) && (flag != 1)) {
  902. pr_err("Illegal value %d\n", flag);
  903. return -EINVAL;
  904. }
  905. dev->dev_attrib.enforce_pr_isids = flag;
  906. pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
  907. (dev->dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
  908. return 0;
  909. }
  910. EXPORT_SYMBOL(se_dev_set_enforce_pr_isids);
  911. int se_dev_set_force_pr_aptpl(struct se_device *dev, int flag)
  912. {
  913. if ((flag != 0) && (flag != 1)) {
  914. printk(KERN_ERR "Illegal value %d\n", flag);
  915. return -EINVAL;
  916. }
  917. if (dev->export_count) {
  918. pr_err("dev[%p]: Unable to set force_pr_aptpl while"
  919. " export_count is %d\n", dev, dev->export_count);
  920. return -EINVAL;
  921. }
  922. dev->dev_attrib.force_pr_aptpl = flag;
  923. pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", dev, flag);
  924. return 0;
  925. }
  926. EXPORT_SYMBOL(se_dev_set_force_pr_aptpl);
  927. int se_dev_set_is_nonrot(struct se_device *dev, int flag)
  928. {
  929. if ((flag != 0) && (flag != 1)) {
  930. printk(KERN_ERR "Illegal value %d\n", flag);
  931. return -EINVAL;
  932. }
  933. dev->dev_attrib.is_nonrot = flag;
  934. pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
  935. dev, flag);
  936. return 0;
  937. }
  938. EXPORT_SYMBOL(se_dev_set_is_nonrot);
  939. int se_dev_set_emulate_rest_reord(struct se_device *dev, int flag)
  940. {
  941. if (flag != 0) {
  942. printk(KERN_ERR "dev[%p]: SE Device emulatation of restricted"
  943. " reordering not implemented\n", dev);
  944. return -ENOSYS;
  945. }
  946. dev->dev_attrib.emulate_rest_reord = flag;
  947. pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
  948. return 0;
  949. }
  950. EXPORT_SYMBOL(se_dev_set_emulate_rest_reord);
  951. /*
  952. * Note, this can only be called on unexported SE Device Object.
  953. */
  954. int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
  955. {
  956. if (dev->export_count) {
  957. pr_err("dev[%p]: Unable to change SE Device TCQ while"
  958. " export_count is %d\n",
  959. dev, dev->export_count);
  960. return -EINVAL;
  961. }
  962. if (!queue_depth) {
  963. pr_err("dev[%p]: Illegal ZERO value for queue"
  964. "_depth\n", dev);
  965. return -EINVAL;
  966. }
  967. if (queue_depth > dev->dev_attrib.queue_depth) {
  968. if (queue_depth > dev->dev_attrib.hw_queue_depth) {
  969. pr_err("dev[%p]: Passed queue_depth:"
  970. " %u exceeds TCM/SE_Device MAX"
  971. " TCQ: %u\n", dev, queue_depth,
  972. dev->dev_attrib.hw_queue_depth);
  973. return -EINVAL;
  974. }
  975. }
  976. dev->dev_attrib.queue_depth = dev->queue_depth = queue_depth;
  977. pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
  978. dev, queue_depth);
  979. return 0;
  980. }
  981. EXPORT_SYMBOL(se_dev_set_queue_depth);
  982. int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
  983. {
  984. if (dev->export_count) {
  985. pr_err("dev[%p]: Unable to change SE Device"
  986. " optimal_sectors while export_count is %d\n",
  987. dev, dev->export_count);
  988. return -EINVAL;
  989. }
  990. if (optimal_sectors > dev->dev_attrib.hw_max_sectors) {
  991. pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
  992. " greater than hw_max_sectors: %u\n", dev,
  993. optimal_sectors, dev->dev_attrib.hw_max_sectors);
  994. return -EINVAL;
  995. }
  996. dev->dev_attrib.optimal_sectors = optimal_sectors;
  997. pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
  998. dev, optimal_sectors);
  999. return 0;
  1000. }
  1001. EXPORT_SYMBOL(se_dev_set_optimal_sectors);
  1002. int se_dev_set_block_size(struct se_device *dev, u32 block_size)
  1003. {
  1004. if (dev->export_count) {
  1005. pr_err("dev[%p]: Unable to change SE Device block_size"
  1006. " while export_count is %d\n",
  1007. dev, dev->export_count);
  1008. return -EINVAL;
  1009. }
  1010. if ((block_size != 512) &&
  1011. (block_size != 1024) &&
  1012. (block_size != 2048) &&
  1013. (block_size != 4096)) {
  1014. pr_err("dev[%p]: Illegal value for block_device: %u"
  1015. " for SE device, must be 512, 1024, 2048 or 4096\n",
  1016. dev, block_size);
  1017. return -EINVAL;
  1018. }
  1019. dev->dev_attrib.block_size = block_size;
  1020. pr_debug("dev[%p]: SE Device block_size changed to %u\n",
  1021. dev, block_size);
  1022. if (dev->dev_attrib.max_bytes_per_io)
  1023. dev->dev_attrib.hw_max_sectors =
  1024. dev->dev_attrib.max_bytes_per_io / block_size;
  1025. return 0;
  1026. }
  1027. EXPORT_SYMBOL(se_dev_set_block_size);
  1028. struct se_lun *core_dev_add_lun(
  1029. struct se_portal_group *tpg,
  1030. struct se_device *dev,
  1031. u32 unpacked_lun)
  1032. {
  1033. struct se_lun *lun;
  1034. int rc;
  1035. lun = core_tpg_alloc_lun(tpg, unpacked_lun);
  1036. if (IS_ERR(lun))
  1037. return lun;
  1038. rc = core_tpg_add_lun(tpg, lun,
  1039. TRANSPORT_LUNFLAGS_READ_WRITE, dev);
  1040. if (rc < 0)
  1041. return ERR_PTR(rc);
  1042. pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
  1043. " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  1044. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  1045. tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
  1046. /*
  1047. * Update LUN maps for dynamically added initiators when
  1048. * generate_node_acl is enabled.
  1049. */
  1050. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  1051. struct se_node_acl *acl;
  1052. spin_lock_irq(&tpg->acl_node_lock);
  1053. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  1054. if (acl->dynamic_node_acl &&
  1055. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  1056. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  1057. spin_unlock_irq(&tpg->acl_node_lock);
  1058. core_tpg_add_node_to_devs(acl, tpg);
  1059. spin_lock_irq(&tpg->acl_node_lock);
  1060. }
  1061. }
  1062. spin_unlock_irq(&tpg->acl_node_lock);
  1063. }
  1064. return lun;
  1065. }
  1066. /* core_dev_del_lun():
  1067. *
  1068. *
  1069. */
  1070. void core_dev_del_lun(
  1071. struct se_portal_group *tpg,
  1072. struct se_lun *lun)
  1073. {
  1074. pr_debug("%s_TPG[%u]_LUN[%u] - Deactivating %s Logical Unit from"
  1075. " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
  1076. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  1077. tpg->se_tpg_tfo->get_fabric_name());
  1078. core_tpg_remove_lun(tpg, lun);
  1079. }
  1080. struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
  1081. {
  1082. struct se_lun *lun;
  1083. spin_lock(&tpg->tpg_lun_lock);
  1084. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  1085. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
  1086. "_PER_TPG-1: %u for Target Portal Group: %hu\n",
  1087. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1088. TRANSPORT_MAX_LUNS_PER_TPG-1,
  1089. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1090. spin_unlock(&tpg->tpg_lun_lock);
  1091. return NULL;
  1092. }
  1093. lun = tpg->tpg_lun_list[unpacked_lun];
  1094. if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
  1095. pr_err("%s Logical Unit Number: %u is not free on"
  1096. " Target Portal Group: %hu, ignoring request.\n",
  1097. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1098. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1099. spin_unlock(&tpg->tpg_lun_lock);
  1100. return NULL;
  1101. }
  1102. spin_unlock(&tpg->tpg_lun_lock);
  1103. return lun;
  1104. }
  1105. /* core_dev_get_lun():
  1106. *
  1107. *
  1108. */
  1109. static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
  1110. {
  1111. struct se_lun *lun;
  1112. spin_lock(&tpg->tpg_lun_lock);
  1113. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  1114. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
  1115. "_TPG-1: %u for Target Portal Group: %hu\n",
  1116. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1117. TRANSPORT_MAX_LUNS_PER_TPG-1,
  1118. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1119. spin_unlock(&tpg->tpg_lun_lock);
  1120. return NULL;
  1121. }
  1122. lun = tpg->tpg_lun_list[unpacked_lun];
  1123. if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
  1124. pr_err("%s Logical Unit Number: %u is not active on"
  1125. " Target Portal Group: %hu, ignoring request.\n",
  1126. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1127. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1128. spin_unlock(&tpg->tpg_lun_lock);
  1129. return NULL;
  1130. }
  1131. spin_unlock(&tpg->tpg_lun_lock);
  1132. return lun;
  1133. }
  1134. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  1135. struct se_portal_group *tpg,
  1136. struct se_node_acl *nacl,
  1137. u32 mapped_lun,
  1138. int *ret)
  1139. {
  1140. struct se_lun_acl *lacl;
  1141. if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
  1142. pr_err("%s InitiatorName exceeds maximum size.\n",
  1143. tpg->se_tpg_tfo->get_fabric_name());
  1144. *ret = -EOVERFLOW;
  1145. return NULL;
  1146. }
  1147. lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
  1148. if (!lacl) {
  1149. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  1150. *ret = -ENOMEM;
  1151. return NULL;
  1152. }
  1153. INIT_LIST_HEAD(&lacl->lacl_list);
  1154. lacl->mapped_lun = mapped_lun;
  1155. lacl->se_lun_nacl = nacl;
  1156. snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s",
  1157. nacl->initiatorname);
  1158. return lacl;
  1159. }
  1160. int core_dev_add_initiator_node_lun_acl(
  1161. struct se_portal_group *tpg,
  1162. struct se_lun_acl *lacl,
  1163. u32 unpacked_lun,
  1164. u32 lun_access)
  1165. {
  1166. struct se_lun *lun;
  1167. struct se_node_acl *nacl;
  1168. lun = core_dev_get_lun(tpg, unpacked_lun);
  1169. if (!lun) {
  1170. pr_err("%s Logical Unit Number: %u is not active on"
  1171. " Target Portal Group: %hu, ignoring request.\n",
  1172. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1173. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1174. return -EINVAL;
  1175. }
  1176. nacl = lacl->se_lun_nacl;
  1177. if (!nacl)
  1178. return -EINVAL;
  1179. if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
  1180. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
  1181. lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  1182. lacl->se_lun = lun;
  1183. if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
  1184. lun_access, nacl, tpg) < 0)
  1185. return -EINVAL;
  1186. spin_lock(&lun->lun_acl_lock);
  1187. list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
  1188. atomic_inc_mb(&lun->lun_acl_count);
  1189. spin_unlock(&lun->lun_acl_lock);
  1190. pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
  1191. " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  1192. tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
  1193. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
  1194. lacl->initiatorname);
  1195. /*
  1196. * Check to see if there are any existing persistent reservation APTPL
  1197. * pre-registrations that need to be enabled for this LUN ACL..
  1198. */
  1199. core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, nacl,
  1200. lacl->mapped_lun);
  1201. return 0;
  1202. }
  1203. /* core_dev_del_initiator_node_lun_acl():
  1204. *
  1205. *
  1206. */
  1207. int core_dev_del_initiator_node_lun_acl(
  1208. struct se_portal_group *tpg,
  1209. struct se_lun *lun,
  1210. struct se_lun_acl *lacl)
  1211. {
  1212. struct se_node_acl *nacl;
  1213. nacl = lacl->se_lun_nacl;
  1214. if (!nacl)
  1215. return -EINVAL;
  1216. spin_lock(&lun->lun_acl_lock);
  1217. list_del(&lacl->lacl_list);
  1218. atomic_dec_mb(&lun->lun_acl_count);
  1219. spin_unlock(&lun->lun_acl_lock);
  1220. core_disable_device_list_for_node(lun, NULL, lacl->mapped_lun,
  1221. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
  1222. lacl->se_lun = NULL;
  1223. pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
  1224. " InitiatorNode: %s Mapped LUN: %u\n",
  1225. tpg->se_tpg_tfo->get_fabric_name(),
  1226. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  1227. lacl->initiatorname, lacl->mapped_lun);
  1228. return 0;
  1229. }
  1230. void core_dev_free_initiator_node_lun_acl(
  1231. struct se_portal_group *tpg,
  1232. struct se_lun_acl *lacl)
  1233. {
  1234. pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
  1235. " Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  1236. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1237. tpg->se_tpg_tfo->get_fabric_name(),
  1238. lacl->initiatorname, lacl->mapped_lun);
  1239. kfree(lacl);
  1240. }
  1241. static void scsi_dump_inquiry(struct se_device *dev)
  1242. {
  1243. struct t10_wwn *wwn = &dev->t10_wwn;
  1244. char buf[17];
  1245. int i, device_type;
  1246. /*
  1247. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  1248. */
  1249. for (i = 0; i < 8; i++)
  1250. if (wwn->vendor[i] >= 0x20)
  1251. buf[i] = wwn->vendor[i];
  1252. else
  1253. buf[i] = ' ';
  1254. buf[i] = '\0';
  1255. pr_debug(" Vendor: %s\n", buf);
  1256. for (i = 0; i < 16; i++)
  1257. if (wwn->model[i] >= 0x20)
  1258. buf[i] = wwn->model[i];
  1259. else
  1260. buf[i] = ' ';
  1261. buf[i] = '\0';
  1262. pr_debug(" Model: %s\n", buf);
  1263. for (i = 0; i < 4; i++)
  1264. if (wwn->revision[i] >= 0x20)
  1265. buf[i] = wwn->revision[i];
  1266. else
  1267. buf[i] = ' ';
  1268. buf[i] = '\0';
  1269. pr_debug(" Revision: %s\n", buf);
  1270. device_type = dev->transport->get_device_type(dev);
  1271. pr_debug(" Type: %s ", scsi_device_type(device_type));
  1272. }
  1273. struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
  1274. {
  1275. struct se_device *dev;
  1276. struct se_lun *xcopy_lun;
  1277. dev = hba->transport->alloc_device(hba, name);
  1278. if (!dev)
  1279. return NULL;
  1280. dev->dev_link_magic = SE_DEV_LINK_MAGIC;
  1281. dev->se_hba = hba;
  1282. dev->transport = hba->transport;
  1283. dev->prot_length = sizeof(struct se_dif_v1_tuple);
  1284. INIT_LIST_HEAD(&dev->dev_list);
  1285. INIT_LIST_HEAD(&dev->dev_sep_list);
  1286. INIT_LIST_HEAD(&dev->dev_tmr_list);
  1287. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  1288. INIT_LIST_HEAD(&dev->state_list);
  1289. INIT_LIST_HEAD(&dev->qf_cmd_list);
  1290. INIT_LIST_HEAD(&dev->g_dev_node);
  1291. spin_lock_init(&dev->execute_task_lock);
  1292. spin_lock_init(&dev->delayed_cmd_lock);
  1293. spin_lock_init(&dev->dev_reservation_lock);
  1294. spin_lock_init(&dev->se_port_lock);
  1295. spin_lock_init(&dev->se_tmr_lock);
  1296. spin_lock_init(&dev->qf_cmd_lock);
  1297. sema_init(&dev->caw_sem, 1);
  1298. atomic_set(&dev->dev_ordered_id, 0);
  1299. INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
  1300. spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
  1301. INIT_LIST_HEAD(&dev->t10_pr.registration_list);
  1302. INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
  1303. spin_lock_init(&dev->t10_pr.registration_lock);
  1304. spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
  1305. INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
  1306. spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
  1307. INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
  1308. spin_lock_init(&dev->t10_alua.lba_map_lock);
  1309. dev->t10_wwn.t10_dev = dev;
  1310. dev->t10_alua.t10_dev = dev;
  1311. dev->dev_attrib.da_dev = dev;
  1312. dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
  1313. dev->dev_attrib.emulate_dpo = DA_EMULATE_DPO;
  1314. dev->dev_attrib.emulate_fua_write = DA_EMULATE_FUA_WRITE;
  1315. dev->dev_attrib.emulate_fua_read = DA_EMULATE_FUA_READ;
  1316. dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
  1317. dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
  1318. dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
  1319. dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
  1320. dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
  1321. dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
  1322. dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
  1323. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
  1324. dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
  1325. dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
  1326. dev->dev_attrib.is_nonrot = DA_IS_NONROT;
  1327. dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
  1328. dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
  1329. dev->dev_attrib.max_unmap_block_desc_count =
  1330. DA_MAX_UNMAP_BLOCK_DESC_COUNT;
  1331. dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
  1332. dev->dev_attrib.unmap_granularity_alignment =
  1333. DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
  1334. dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
  1335. xcopy_lun = &dev->xcopy_lun;
  1336. xcopy_lun->lun_se_dev = dev;
  1337. init_completion(&xcopy_lun->lun_shutdown_comp);
  1338. INIT_LIST_HEAD(&xcopy_lun->lun_acl_list);
  1339. spin_lock_init(&xcopy_lun->lun_acl_lock);
  1340. spin_lock_init(&xcopy_lun->lun_sep_lock);
  1341. init_completion(&xcopy_lun->lun_ref_comp);
  1342. return dev;
  1343. }
  1344. int target_configure_device(struct se_device *dev)
  1345. {
  1346. struct se_hba *hba = dev->se_hba;
  1347. int ret;
  1348. if (dev->dev_flags & DF_CONFIGURED) {
  1349. pr_err("se_dev->se_dev_ptr already set for storage"
  1350. " object\n");
  1351. return -EEXIST;
  1352. }
  1353. ret = dev->transport->configure_device(dev);
  1354. if (ret)
  1355. goto out;
  1356. /*
  1357. * XXX: there is not much point to have two different values here..
  1358. */
  1359. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  1360. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  1361. /*
  1362. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  1363. */
  1364. dev->dev_attrib.hw_max_sectors =
  1365. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  1366. dev->dev_attrib.hw_block_size);
  1367. dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
  1368. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  1369. dev->creation_time = get_jiffies_64();
  1370. ret = core_setup_alua(dev);
  1371. if (ret)
  1372. goto out;
  1373. /*
  1374. * Startup the struct se_device processing thread
  1375. */
  1376. dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
  1377. dev->transport->name);
  1378. if (!dev->tmr_wq) {
  1379. pr_err("Unable to create tmr workqueue for %s\n",
  1380. dev->transport->name);
  1381. ret = -ENOMEM;
  1382. goto out_free_alua;
  1383. }
  1384. /*
  1385. * Setup work_queue for QUEUE_FULL
  1386. */
  1387. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  1388. /*
  1389. * Preload the initial INQUIRY const values if we are doing
  1390. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  1391. * passthrough because this is being provided by the backend LLD.
  1392. */
  1393. if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
  1394. strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  1395. strncpy(&dev->t10_wwn.model[0],
  1396. dev->transport->inquiry_prod, 16);
  1397. strncpy(&dev->t10_wwn.revision[0],
  1398. dev->transport->inquiry_rev, 4);
  1399. }
  1400. scsi_dump_inquiry(dev);
  1401. spin_lock(&hba->device_lock);
  1402. hba->dev_count++;
  1403. spin_unlock(&hba->device_lock);
  1404. mutex_lock(&g_device_mutex);
  1405. list_add_tail(&dev->g_dev_node, &g_device_list);
  1406. mutex_unlock(&g_device_mutex);
  1407. dev->dev_flags |= DF_CONFIGURED;
  1408. return 0;
  1409. out_free_alua:
  1410. core_alua_free_lu_gp_mem(dev);
  1411. out:
  1412. se_release_vpd_for_dev(dev);
  1413. return ret;
  1414. }
  1415. void target_free_device(struct se_device *dev)
  1416. {
  1417. struct se_hba *hba = dev->se_hba;
  1418. WARN_ON(!list_empty(&dev->dev_sep_list));
  1419. if (dev->dev_flags & DF_CONFIGURED) {
  1420. destroy_workqueue(dev->tmr_wq);
  1421. mutex_lock(&g_device_mutex);
  1422. list_del(&dev->g_dev_node);
  1423. mutex_unlock(&g_device_mutex);
  1424. spin_lock(&hba->device_lock);
  1425. hba->dev_count--;
  1426. spin_unlock(&hba->device_lock);
  1427. }
  1428. core_alua_free_lu_gp_mem(dev);
  1429. core_alua_set_lba_map(dev, NULL, 0, 0);
  1430. core_scsi3_free_all_registrations(dev);
  1431. se_release_vpd_for_dev(dev);
  1432. if (dev->transport->free_prot)
  1433. dev->transport->free_prot(dev);
  1434. dev->transport->free_device(dev);
  1435. }
  1436. int core_dev_setup_virtual_lun0(void)
  1437. {
  1438. struct se_hba *hba;
  1439. struct se_device *dev;
  1440. char buf[] = "rd_pages=8,rd_nullio=1";
  1441. int ret;
  1442. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  1443. if (IS_ERR(hba))
  1444. return PTR_ERR(hba);
  1445. dev = target_alloc_device(hba, "virt_lun0");
  1446. if (!dev) {
  1447. ret = -ENOMEM;
  1448. goto out_free_hba;
  1449. }
  1450. hba->transport->set_configfs_dev_params(dev, buf, sizeof(buf));
  1451. ret = target_configure_device(dev);
  1452. if (ret)
  1453. goto out_free_se_dev;
  1454. lun0_hba = hba;
  1455. g_lun0_dev = dev;
  1456. return 0;
  1457. out_free_se_dev:
  1458. target_free_device(dev);
  1459. out_free_hba:
  1460. core_delete_hba(hba);
  1461. return ret;
  1462. }
  1463. void core_dev_release_virtual_lun0(void)
  1464. {
  1465. struct se_hba *hba = lun0_hba;
  1466. if (!hba)
  1467. return;
  1468. if (g_lun0_dev)
  1469. target_free_device(g_lun0_dev);
  1470. core_delete_hba(hba);
  1471. }