target_core_device.c 46 KB

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