target_core_device.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  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_task *se_task)
  566. {
  567. struct se_cmd *se_cmd = se_task->task_se_cmd;
  568. struct se_dev_entry *deve;
  569. struct se_session *se_sess = se_cmd->se_sess;
  570. unsigned char *buf;
  571. u32 lun_count = 0, offset = 8, i;
  572. buf = transport_kmap_data_sg(se_cmd);
  573. if (!buf)
  574. return -ENOMEM;
  575. /*
  576. * If no struct se_session pointer is present, this struct se_cmd is
  577. * coming via a target_core_mod PASSTHROUGH op, and not through
  578. * a $FABRIC_MOD. In that case, report LUN=0 only.
  579. */
  580. if (!se_sess) {
  581. int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
  582. lun_count = 1;
  583. goto done;
  584. }
  585. spin_lock_irq(&se_sess->se_node_acl->device_list_lock);
  586. for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
  587. deve = se_sess->se_node_acl->device_list[i];
  588. if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
  589. continue;
  590. /*
  591. * We determine the correct LUN LIST LENGTH even once we
  592. * have reached the initial allocation length.
  593. * See SPC2-R20 7.19.
  594. */
  595. lun_count++;
  596. if ((offset + 8) > se_cmd->data_length)
  597. continue;
  598. int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
  599. offset += 8;
  600. }
  601. spin_unlock_irq(&se_sess->se_node_acl->device_list_lock);
  602. /*
  603. * See SPC3 r07, page 159.
  604. */
  605. done:
  606. lun_count *= 8;
  607. buf[0] = ((lun_count >> 24) & 0xff);
  608. buf[1] = ((lun_count >> 16) & 0xff);
  609. buf[2] = ((lun_count >> 8) & 0xff);
  610. buf[3] = (lun_count & 0xff);
  611. transport_kunmap_data_sg(se_cmd);
  612. se_task->task_scsi_status = GOOD;
  613. transport_complete_task(se_task, 1);
  614. return 0;
  615. }
  616. /* se_release_device_for_hba():
  617. *
  618. *
  619. */
  620. void se_release_device_for_hba(struct se_device *dev)
  621. {
  622. struct se_hba *hba = dev->se_hba;
  623. if ((dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) ||
  624. (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED) ||
  625. (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) ||
  626. (dev->dev_status & TRANSPORT_DEVICE_OFFLINE_ACTIVATED) ||
  627. (dev->dev_status & TRANSPORT_DEVICE_OFFLINE_DEACTIVATED))
  628. se_dev_stop(dev);
  629. if (dev->dev_ptr) {
  630. kthread_stop(dev->process_thread);
  631. if (dev->transport->free_device)
  632. dev->transport->free_device(dev->dev_ptr);
  633. }
  634. spin_lock(&hba->device_lock);
  635. list_del(&dev->dev_list);
  636. hba->dev_count--;
  637. spin_unlock(&hba->device_lock);
  638. core_scsi3_free_all_registrations(dev);
  639. se_release_vpd_for_dev(dev);
  640. kfree(dev);
  641. }
  642. void se_release_vpd_for_dev(struct se_device *dev)
  643. {
  644. struct t10_vpd *vpd, *vpd_tmp;
  645. spin_lock(&dev->se_sub_dev->t10_wwn.t10_vpd_lock);
  646. list_for_each_entry_safe(vpd, vpd_tmp,
  647. &dev->se_sub_dev->t10_wwn.t10_vpd_list, vpd_list) {
  648. list_del(&vpd->vpd_list);
  649. kfree(vpd);
  650. }
  651. spin_unlock(&dev->se_sub_dev->t10_wwn.t10_vpd_lock);
  652. }
  653. /* se_free_virtual_device():
  654. *
  655. * Used for IBLOCK, RAMDISK, and FILEIO Transport Drivers.
  656. */
  657. int se_free_virtual_device(struct se_device *dev, struct se_hba *hba)
  658. {
  659. if (!list_empty(&dev->dev_sep_list))
  660. dump_stack();
  661. core_alua_free_lu_gp_mem(dev);
  662. se_release_device_for_hba(dev);
  663. return 0;
  664. }
  665. static void se_dev_start(struct se_device *dev)
  666. {
  667. struct se_hba *hba = dev->se_hba;
  668. spin_lock(&hba->device_lock);
  669. atomic_inc(&dev->dev_obj.obj_access_count);
  670. if (atomic_read(&dev->dev_obj.obj_access_count) == 1) {
  671. if (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED) {
  672. dev->dev_status &= ~TRANSPORT_DEVICE_DEACTIVATED;
  673. dev->dev_status |= TRANSPORT_DEVICE_ACTIVATED;
  674. } else if (dev->dev_status &
  675. TRANSPORT_DEVICE_OFFLINE_DEACTIVATED) {
  676. dev->dev_status &=
  677. ~TRANSPORT_DEVICE_OFFLINE_DEACTIVATED;
  678. dev->dev_status |= TRANSPORT_DEVICE_OFFLINE_ACTIVATED;
  679. }
  680. }
  681. spin_unlock(&hba->device_lock);
  682. }
  683. static void se_dev_stop(struct se_device *dev)
  684. {
  685. struct se_hba *hba = dev->se_hba;
  686. spin_lock(&hba->device_lock);
  687. atomic_dec(&dev->dev_obj.obj_access_count);
  688. if (atomic_read(&dev->dev_obj.obj_access_count) == 0) {
  689. if (dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) {
  690. dev->dev_status &= ~TRANSPORT_DEVICE_ACTIVATED;
  691. dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
  692. } else if (dev->dev_status &
  693. TRANSPORT_DEVICE_OFFLINE_ACTIVATED) {
  694. dev->dev_status &= ~TRANSPORT_DEVICE_OFFLINE_ACTIVATED;
  695. dev->dev_status |= TRANSPORT_DEVICE_OFFLINE_DEACTIVATED;
  696. }
  697. }
  698. spin_unlock(&hba->device_lock);
  699. }
  700. int se_dev_check_online(struct se_device *dev)
  701. {
  702. unsigned long flags;
  703. int ret;
  704. spin_lock_irqsave(&dev->dev_status_lock, flags);
  705. ret = ((dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) ||
  706. (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED)) ? 0 : 1;
  707. spin_unlock_irqrestore(&dev->dev_status_lock, flags);
  708. return ret;
  709. }
  710. int se_dev_check_shutdown(struct se_device *dev)
  711. {
  712. int ret;
  713. spin_lock_irq(&dev->dev_status_lock);
  714. ret = (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN);
  715. spin_unlock_irq(&dev->dev_status_lock);
  716. return ret;
  717. }
  718. u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
  719. {
  720. u32 aligned_max_sectors;
  721. u32 alignment;
  722. /*
  723. * Limit max_sectors to a PAGE_SIZE aligned value for modern
  724. * transport_allocate_data_tasks() operation.
  725. */
  726. alignment = max(1ul, PAGE_SIZE / block_size);
  727. aligned_max_sectors = rounddown(max_sectors, alignment);
  728. if (max_sectors != aligned_max_sectors)
  729. pr_info("Rounding down aligned max_sectors from %u to %u\n",
  730. max_sectors, aligned_max_sectors);
  731. return aligned_max_sectors;
  732. }
  733. void se_dev_set_default_attribs(
  734. struct se_device *dev,
  735. struct se_dev_limits *dev_limits)
  736. {
  737. struct queue_limits *limits = &dev_limits->limits;
  738. dev->se_sub_dev->se_dev_attrib.emulate_dpo = DA_EMULATE_DPO;
  739. dev->se_sub_dev->se_dev_attrib.emulate_fua_write = DA_EMULATE_FUA_WRITE;
  740. dev->se_sub_dev->se_dev_attrib.emulate_fua_read = DA_EMULATE_FUA_READ;
  741. dev->se_sub_dev->se_dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
  742. dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
  743. dev->se_sub_dev->se_dev_attrib.emulate_tas = DA_EMULATE_TAS;
  744. dev->se_sub_dev->se_dev_attrib.emulate_tpu = DA_EMULATE_TPU;
  745. dev->se_sub_dev->se_dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
  746. dev->se_sub_dev->se_dev_attrib.emulate_reservations = DA_EMULATE_RESERVATIONS;
  747. dev->se_sub_dev->se_dev_attrib.emulate_alua = DA_EMULATE_ALUA;
  748. dev->se_sub_dev->se_dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
  749. dev->se_sub_dev->se_dev_attrib.is_nonrot = DA_IS_NONROT;
  750. dev->se_sub_dev->se_dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
  751. /*
  752. * The TPU=1 and TPWS=1 settings will be set in TCM/IBLOCK
  753. * iblock_create_virtdevice() from struct queue_limits values
  754. * if blk_queue_discard()==1
  755. */
  756. dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
  757. dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count =
  758. DA_MAX_UNMAP_BLOCK_DESC_COUNT;
  759. dev->se_sub_dev->se_dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
  760. dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment =
  761. DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
  762. /*
  763. * block_size is based on subsystem plugin dependent requirements.
  764. */
  765. dev->se_sub_dev->se_dev_attrib.hw_block_size = limits->logical_block_size;
  766. dev->se_sub_dev->se_dev_attrib.block_size = limits->logical_block_size;
  767. /*
  768. * max_sectors is based on subsystem plugin dependent requirements.
  769. */
  770. dev->se_sub_dev->se_dev_attrib.hw_max_sectors = limits->max_hw_sectors;
  771. /*
  772. * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
  773. */
  774. limits->max_sectors = se_dev_align_max_sectors(limits->max_sectors,
  775. limits->logical_block_size);
  776. dev->se_sub_dev->se_dev_attrib.max_sectors = limits->max_sectors;
  777. /*
  778. * Set fabric_max_sectors, which is reported in block limits
  779. * VPD page (B0h).
  780. */
  781. dev->se_sub_dev->se_dev_attrib.fabric_max_sectors = DA_FABRIC_MAX_SECTORS;
  782. /*
  783. * Set optimal_sectors from fabric_max_sectors, which can be
  784. * lowered via configfs.
  785. */
  786. dev->se_sub_dev->se_dev_attrib.optimal_sectors = DA_FABRIC_MAX_SECTORS;
  787. /*
  788. * queue_depth is based on subsystem plugin dependent requirements.
  789. */
  790. dev->se_sub_dev->se_dev_attrib.hw_queue_depth = dev_limits->hw_queue_depth;
  791. dev->se_sub_dev->se_dev_attrib.queue_depth = dev_limits->queue_depth;
  792. }
  793. int se_dev_set_max_unmap_lba_count(
  794. struct se_device *dev,
  795. u32 max_unmap_lba_count)
  796. {
  797. dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
  798. pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
  799. dev, dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count);
  800. return 0;
  801. }
  802. int se_dev_set_max_unmap_block_desc_count(
  803. struct se_device *dev,
  804. u32 max_unmap_block_desc_count)
  805. {
  806. dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count =
  807. max_unmap_block_desc_count;
  808. pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
  809. dev, dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count);
  810. return 0;
  811. }
  812. int se_dev_set_unmap_granularity(
  813. struct se_device *dev,
  814. u32 unmap_granularity)
  815. {
  816. dev->se_sub_dev->se_dev_attrib.unmap_granularity = unmap_granularity;
  817. pr_debug("dev[%p]: Set unmap_granularity: %u\n",
  818. dev, dev->se_sub_dev->se_dev_attrib.unmap_granularity);
  819. return 0;
  820. }
  821. int se_dev_set_unmap_granularity_alignment(
  822. struct se_device *dev,
  823. u32 unmap_granularity_alignment)
  824. {
  825. dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
  826. pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
  827. dev, dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment);
  828. return 0;
  829. }
  830. int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
  831. {
  832. if (flag != 0 && flag != 1) {
  833. pr_err("Illegal value %d\n", flag);
  834. return -EINVAL;
  835. }
  836. if (flag) {
  837. pr_err("dpo_emulated not supported\n");
  838. return -EINVAL;
  839. }
  840. return 0;
  841. }
  842. int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
  843. {
  844. if (flag != 0 && flag != 1) {
  845. pr_err("Illegal value %d\n", flag);
  846. return -EINVAL;
  847. }
  848. if (flag && dev->transport->fua_write_emulated == 0) {
  849. pr_err("fua_write_emulated not supported\n");
  850. return -EINVAL;
  851. }
  852. dev->se_sub_dev->se_dev_attrib.emulate_fua_write = flag;
  853. pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
  854. dev, dev->se_sub_dev->se_dev_attrib.emulate_fua_write);
  855. return 0;
  856. }
  857. int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
  858. {
  859. if (flag != 0 && flag != 1) {
  860. pr_err("Illegal value %d\n", flag);
  861. return -EINVAL;
  862. }
  863. if (flag) {
  864. pr_err("ua read emulated not supported\n");
  865. return -EINVAL;
  866. }
  867. return 0;
  868. }
  869. int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
  870. {
  871. if (flag != 0 && flag != 1) {
  872. pr_err("Illegal value %d\n", flag);
  873. return -EINVAL;
  874. }
  875. if (flag && dev->transport->write_cache_emulated == 0) {
  876. pr_err("write_cache_emulated not supported\n");
  877. return -EINVAL;
  878. }
  879. dev->se_sub_dev->se_dev_attrib.emulate_write_cache = flag;
  880. pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
  881. dev, dev->se_sub_dev->se_dev_attrib.emulate_write_cache);
  882. return 0;
  883. }
  884. int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
  885. {
  886. if ((flag != 0) && (flag != 1) && (flag != 2)) {
  887. pr_err("Illegal value %d\n", flag);
  888. return -EINVAL;
  889. }
  890. if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
  891. pr_err("dev[%p]: Unable to change SE Device"
  892. " UA_INTRLCK_CTRL while dev_export_obj: %d count"
  893. " exists\n", dev,
  894. atomic_read(&dev->dev_export_obj.obj_access_count));
  895. return -EINVAL;
  896. }
  897. dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl = flag;
  898. pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
  899. dev, dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl);
  900. return 0;
  901. }
  902. int se_dev_set_emulate_tas(struct se_device *dev, int flag)
  903. {
  904. if ((flag != 0) && (flag != 1)) {
  905. pr_err("Illegal value %d\n", flag);
  906. return -EINVAL;
  907. }
  908. if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
  909. pr_err("dev[%p]: Unable to change SE Device TAS while"
  910. " dev_export_obj: %d count exists\n", dev,
  911. atomic_read(&dev->dev_export_obj.obj_access_count));
  912. return -EINVAL;
  913. }
  914. dev->se_sub_dev->se_dev_attrib.emulate_tas = flag;
  915. pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
  916. dev, (dev->se_sub_dev->se_dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
  917. return 0;
  918. }
  919. int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
  920. {
  921. if ((flag != 0) && (flag != 1)) {
  922. pr_err("Illegal value %d\n", flag);
  923. return -EINVAL;
  924. }
  925. /*
  926. * We expect this value to be non-zero when generic Block Layer
  927. * Discard supported is detected iblock_create_virtdevice().
  928. */
  929. if (flag && !dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count) {
  930. pr_err("Generic Block Discard not supported\n");
  931. return -ENOSYS;
  932. }
  933. dev->se_sub_dev->se_dev_attrib.emulate_tpu = flag;
  934. pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
  935. dev, flag);
  936. return 0;
  937. }
  938. int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
  939. {
  940. if ((flag != 0) && (flag != 1)) {
  941. pr_err("Illegal value %d\n", flag);
  942. return -EINVAL;
  943. }
  944. /*
  945. * We expect this value to be non-zero when generic Block Layer
  946. * Discard supported is detected iblock_create_virtdevice().
  947. */
  948. if (flag && !dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count) {
  949. pr_err("Generic Block Discard not supported\n");
  950. return -ENOSYS;
  951. }
  952. dev->se_sub_dev->se_dev_attrib.emulate_tpws = flag;
  953. pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
  954. dev, flag);
  955. return 0;
  956. }
  957. int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
  958. {
  959. if ((flag != 0) && (flag != 1)) {
  960. pr_err("Illegal value %d\n", flag);
  961. return -EINVAL;
  962. }
  963. dev->se_sub_dev->se_dev_attrib.enforce_pr_isids = flag;
  964. pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
  965. (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
  966. return 0;
  967. }
  968. int se_dev_set_is_nonrot(struct se_device *dev, int flag)
  969. {
  970. if ((flag != 0) && (flag != 1)) {
  971. printk(KERN_ERR "Illegal value %d\n", flag);
  972. return -EINVAL;
  973. }
  974. dev->se_sub_dev->se_dev_attrib.is_nonrot = flag;
  975. pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
  976. dev, flag);
  977. return 0;
  978. }
  979. int se_dev_set_emulate_rest_reord(struct se_device *dev, int flag)
  980. {
  981. if (flag != 0) {
  982. printk(KERN_ERR "dev[%p]: SE Device emulatation of restricted"
  983. " reordering not implemented\n", dev);
  984. return -ENOSYS;
  985. }
  986. dev->se_sub_dev->se_dev_attrib.emulate_rest_reord = flag;
  987. pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
  988. return 0;
  989. }
  990. /*
  991. * Note, this can only be called on unexported SE Device Object.
  992. */
  993. int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
  994. {
  995. if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
  996. pr_err("dev[%p]: Unable to change SE Device TCQ while"
  997. " dev_export_obj: %d count exists\n", dev,
  998. atomic_read(&dev->dev_export_obj.obj_access_count));
  999. return -EINVAL;
  1000. }
  1001. if (!queue_depth) {
  1002. pr_err("dev[%p]: Illegal ZERO value for queue"
  1003. "_depth\n", dev);
  1004. return -EINVAL;
  1005. }
  1006. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  1007. if (queue_depth > dev->se_sub_dev->se_dev_attrib.hw_queue_depth) {
  1008. pr_err("dev[%p]: Passed queue_depth: %u"
  1009. " exceeds TCM/SE_Device TCQ: %u\n",
  1010. dev, queue_depth,
  1011. dev->se_sub_dev->se_dev_attrib.hw_queue_depth);
  1012. return -EINVAL;
  1013. }
  1014. } else {
  1015. if (queue_depth > dev->se_sub_dev->se_dev_attrib.queue_depth) {
  1016. if (queue_depth > dev->se_sub_dev->se_dev_attrib.hw_queue_depth) {
  1017. pr_err("dev[%p]: Passed queue_depth:"
  1018. " %u exceeds TCM/SE_Device MAX"
  1019. " TCQ: %u\n", dev, queue_depth,
  1020. dev->se_sub_dev->se_dev_attrib.hw_queue_depth);
  1021. return -EINVAL;
  1022. }
  1023. }
  1024. }
  1025. dev->se_sub_dev->se_dev_attrib.queue_depth = dev->queue_depth = queue_depth;
  1026. pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
  1027. dev, queue_depth);
  1028. return 0;
  1029. }
  1030. int se_dev_set_max_sectors(struct se_device *dev, u32 max_sectors)
  1031. {
  1032. int force = 0; /* Force setting for VDEVS */
  1033. if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
  1034. pr_err("dev[%p]: Unable to change SE Device"
  1035. " max_sectors while dev_export_obj: %d count exists\n",
  1036. dev, atomic_read(&dev->dev_export_obj.obj_access_count));
  1037. return -EINVAL;
  1038. }
  1039. if (!max_sectors) {
  1040. pr_err("dev[%p]: Illegal ZERO value for"
  1041. " max_sectors\n", dev);
  1042. return -EINVAL;
  1043. }
  1044. if (max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
  1045. pr_err("dev[%p]: Passed max_sectors: %u less than"
  1046. " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, max_sectors,
  1047. DA_STATUS_MAX_SECTORS_MIN);
  1048. return -EINVAL;
  1049. }
  1050. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  1051. if (max_sectors > dev->se_sub_dev->se_dev_attrib.hw_max_sectors) {
  1052. pr_err("dev[%p]: Passed max_sectors: %u"
  1053. " greater than TCM/SE_Device max_sectors:"
  1054. " %u\n", dev, max_sectors,
  1055. dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
  1056. return -EINVAL;
  1057. }
  1058. } else {
  1059. if (!force && (max_sectors >
  1060. dev->se_sub_dev->se_dev_attrib.hw_max_sectors)) {
  1061. pr_err("dev[%p]: Passed max_sectors: %u"
  1062. " greater than TCM/SE_Device max_sectors"
  1063. ": %u, use force=1 to override.\n", dev,
  1064. max_sectors, dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
  1065. return -EINVAL;
  1066. }
  1067. if (max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
  1068. pr_err("dev[%p]: Passed max_sectors: %u"
  1069. " greater than DA_STATUS_MAX_SECTORS_MAX:"
  1070. " %u\n", dev, max_sectors,
  1071. DA_STATUS_MAX_SECTORS_MAX);
  1072. return -EINVAL;
  1073. }
  1074. }
  1075. /*
  1076. * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
  1077. */
  1078. max_sectors = se_dev_align_max_sectors(max_sectors,
  1079. dev->se_sub_dev->se_dev_attrib.block_size);
  1080. dev->se_sub_dev->se_dev_attrib.max_sectors = max_sectors;
  1081. pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
  1082. dev, max_sectors);
  1083. return 0;
  1084. }
  1085. int se_dev_set_fabric_max_sectors(struct se_device *dev, u32 fabric_max_sectors)
  1086. {
  1087. int block_size = dev->se_sub_dev->se_dev_attrib.block_size;
  1088. if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
  1089. pr_err("dev[%p]: Unable to change SE Device"
  1090. " fabric_max_sectors while dev_export_obj: %d count exists\n",
  1091. dev, atomic_read(&dev->dev_export_obj.obj_access_count));
  1092. return -EINVAL;
  1093. }
  1094. if (!fabric_max_sectors) {
  1095. pr_err("dev[%p]: Illegal ZERO value for"
  1096. " fabric_max_sectors\n", dev);
  1097. return -EINVAL;
  1098. }
  1099. if (fabric_max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
  1100. pr_err("dev[%p]: Passed fabric_max_sectors: %u less than"
  1101. " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, fabric_max_sectors,
  1102. DA_STATUS_MAX_SECTORS_MIN);
  1103. return -EINVAL;
  1104. }
  1105. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  1106. if (fabric_max_sectors > dev->se_sub_dev->se_dev_attrib.hw_max_sectors) {
  1107. pr_err("dev[%p]: Passed fabric_max_sectors: %u"
  1108. " greater than TCM/SE_Device max_sectors:"
  1109. " %u\n", dev, fabric_max_sectors,
  1110. dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
  1111. return -EINVAL;
  1112. }
  1113. } else {
  1114. if (fabric_max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
  1115. pr_err("dev[%p]: Passed fabric_max_sectors: %u"
  1116. " greater than DA_STATUS_MAX_SECTORS_MAX:"
  1117. " %u\n", dev, fabric_max_sectors,
  1118. DA_STATUS_MAX_SECTORS_MAX);
  1119. return -EINVAL;
  1120. }
  1121. }
  1122. /*
  1123. * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
  1124. */
  1125. if (!block_size) {
  1126. block_size = 512;
  1127. pr_warn("Defaulting to 512 for zero block_size\n");
  1128. }
  1129. fabric_max_sectors = se_dev_align_max_sectors(fabric_max_sectors,
  1130. block_size);
  1131. dev->se_sub_dev->se_dev_attrib.fabric_max_sectors = fabric_max_sectors;
  1132. pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
  1133. dev, fabric_max_sectors);
  1134. return 0;
  1135. }
  1136. int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
  1137. {
  1138. if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
  1139. pr_err("dev[%p]: Unable to change SE Device"
  1140. " optimal_sectors while dev_export_obj: %d count exists\n",
  1141. dev, atomic_read(&dev->dev_export_obj.obj_access_count));
  1142. return -EINVAL;
  1143. }
  1144. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  1145. pr_err("dev[%p]: Passed optimal_sectors cannot be"
  1146. " changed for TCM/pSCSI\n", dev);
  1147. return -EINVAL;
  1148. }
  1149. if (optimal_sectors > dev->se_sub_dev->se_dev_attrib.fabric_max_sectors) {
  1150. pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
  1151. " greater than fabric_max_sectors: %u\n", dev,
  1152. optimal_sectors, dev->se_sub_dev->se_dev_attrib.fabric_max_sectors);
  1153. return -EINVAL;
  1154. }
  1155. dev->se_sub_dev->se_dev_attrib.optimal_sectors = optimal_sectors;
  1156. pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
  1157. dev, optimal_sectors);
  1158. return 0;
  1159. }
  1160. int se_dev_set_block_size(struct se_device *dev, u32 block_size)
  1161. {
  1162. if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
  1163. pr_err("dev[%p]: Unable to change SE Device block_size"
  1164. " while dev_export_obj: %d count exists\n", dev,
  1165. atomic_read(&dev->dev_export_obj.obj_access_count));
  1166. return -EINVAL;
  1167. }
  1168. if ((block_size != 512) &&
  1169. (block_size != 1024) &&
  1170. (block_size != 2048) &&
  1171. (block_size != 4096)) {
  1172. pr_err("dev[%p]: Illegal value for block_device: %u"
  1173. " for SE device, must be 512, 1024, 2048 or 4096\n",
  1174. dev, block_size);
  1175. return -EINVAL;
  1176. }
  1177. if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
  1178. pr_err("dev[%p]: Not allowed to change block_size for"
  1179. " Physical Device, use for Linux/SCSI to change"
  1180. " block_size for underlying hardware\n", dev);
  1181. return -EINVAL;
  1182. }
  1183. dev->se_sub_dev->se_dev_attrib.block_size = block_size;
  1184. pr_debug("dev[%p]: SE Device block_size changed to %u\n",
  1185. dev, block_size);
  1186. return 0;
  1187. }
  1188. struct se_lun *core_dev_add_lun(
  1189. struct se_portal_group *tpg,
  1190. struct se_hba *hba,
  1191. struct se_device *dev,
  1192. u32 lun)
  1193. {
  1194. struct se_lun *lun_p;
  1195. u32 lun_access = 0;
  1196. int rc;
  1197. if (atomic_read(&dev->dev_access_obj.obj_access_count) != 0) {
  1198. pr_err("Unable to export struct se_device while dev_access_obj: %d\n",
  1199. atomic_read(&dev->dev_access_obj.obj_access_count));
  1200. return ERR_PTR(-EACCES);
  1201. }
  1202. lun_p = core_tpg_pre_addlun(tpg, lun);
  1203. if (IS_ERR(lun_p))
  1204. return lun_p;
  1205. if (dev->dev_flags & DF_READ_ONLY)
  1206. lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  1207. else
  1208. lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
  1209. rc = core_tpg_post_addlun(tpg, lun_p, lun_access, dev);
  1210. if (rc < 0)
  1211. return ERR_PTR(rc);
  1212. pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
  1213. " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  1214. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun_p->unpacked_lun,
  1215. tpg->se_tpg_tfo->get_fabric_name(), hba->hba_id);
  1216. /*
  1217. * Update LUN maps for dynamically added initiators when
  1218. * generate_node_acl is enabled.
  1219. */
  1220. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  1221. struct se_node_acl *acl;
  1222. spin_lock_irq(&tpg->acl_node_lock);
  1223. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  1224. if (acl->dynamic_node_acl &&
  1225. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  1226. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  1227. spin_unlock_irq(&tpg->acl_node_lock);
  1228. core_tpg_add_node_to_devs(acl, tpg);
  1229. spin_lock_irq(&tpg->acl_node_lock);
  1230. }
  1231. }
  1232. spin_unlock_irq(&tpg->acl_node_lock);
  1233. }
  1234. return lun_p;
  1235. }
  1236. /* core_dev_del_lun():
  1237. *
  1238. *
  1239. */
  1240. int core_dev_del_lun(
  1241. struct se_portal_group *tpg,
  1242. u32 unpacked_lun)
  1243. {
  1244. struct se_lun *lun;
  1245. lun = core_tpg_pre_dellun(tpg, unpacked_lun);
  1246. if (IS_ERR(lun))
  1247. return PTR_ERR(lun);
  1248. core_tpg_post_dellun(tpg, lun);
  1249. pr_debug("%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from"
  1250. " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
  1251. tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun,
  1252. tpg->se_tpg_tfo->get_fabric_name());
  1253. return 0;
  1254. }
  1255. struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
  1256. {
  1257. struct se_lun *lun;
  1258. spin_lock(&tpg->tpg_lun_lock);
  1259. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  1260. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
  1261. "_PER_TPG-1: %u for Target Portal Group: %hu\n",
  1262. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1263. TRANSPORT_MAX_LUNS_PER_TPG-1,
  1264. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1265. spin_unlock(&tpg->tpg_lun_lock);
  1266. return NULL;
  1267. }
  1268. lun = tpg->tpg_lun_list[unpacked_lun];
  1269. if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
  1270. pr_err("%s Logical Unit Number: %u is not free on"
  1271. " Target Portal Group: %hu, ignoring request.\n",
  1272. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1273. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1274. spin_unlock(&tpg->tpg_lun_lock);
  1275. return NULL;
  1276. }
  1277. spin_unlock(&tpg->tpg_lun_lock);
  1278. return lun;
  1279. }
  1280. /* core_dev_get_lun():
  1281. *
  1282. *
  1283. */
  1284. static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
  1285. {
  1286. struct se_lun *lun;
  1287. spin_lock(&tpg->tpg_lun_lock);
  1288. if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
  1289. pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
  1290. "_TPG-1: %u for Target Portal Group: %hu\n",
  1291. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1292. TRANSPORT_MAX_LUNS_PER_TPG-1,
  1293. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1294. spin_unlock(&tpg->tpg_lun_lock);
  1295. return NULL;
  1296. }
  1297. lun = tpg->tpg_lun_list[unpacked_lun];
  1298. if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
  1299. pr_err("%s Logical Unit Number: %u is not active on"
  1300. " Target Portal Group: %hu, ignoring request.\n",
  1301. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1302. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1303. spin_unlock(&tpg->tpg_lun_lock);
  1304. return NULL;
  1305. }
  1306. spin_unlock(&tpg->tpg_lun_lock);
  1307. return lun;
  1308. }
  1309. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  1310. struct se_portal_group *tpg,
  1311. struct se_node_acl *nacl,
  1312. u32 mapped_lun,
  1313. int *ret)
  1314. {
  1315. struct se_lun_acl *lacl;
  1316. if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
  1317. pr_err("%s InitiatorName exceeds maximum size.\n",
  1318. tpg->se_tpg_tfo->get_fabric_name());
  1319. *ret = -EOVERFLOW;
  1320. return NULL;
  1321. }
  1322. lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
  1323. if (!lacl) {
  1324. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  1325. *ret = -ENOMEM;
  1326. return NULL;
  1327. }
  1328. INIT_LIST_HEAD(&lacl->lacl_list);
  1329. lacl->mapped_lun = mapped_lun;
  1330. lacl->se_lun_nacl = nacl;
  1331. snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s",
  1332. nacl->initiatorname);
  1333. return lacl;
  1334. }
  1335. int core_dev_add_initiator_node_lun_acl(
  1336. struct se_portal_group *tpg,
  1337. struct se_lun_acl *lacl,
  1338. u32 unpacked_lun,
  1339. u32 lun_access)
  1340. {
  1341. struct se_lun *lun;
  1342. struct se_node_acl *nacl;
  1343. lun = core_dev_get_lun(tpg, unpacked_lun);
  1344. if (!lun) {
  1345. pr_err("%s Logical Unit Number: %u is not active on"
  1346. " Target Portal Group: %hu, ignoring request.\n",
  1347. tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  1348. tpg->se_tpg_tfo->tpg_get_tag(tpg));
  1349. return -EINVAL;
  1350. }
  1351. nacl = lacl->se_lun_nacl;
  1352. if (!nacl)
  1353. return -EINVAL;
  1354. if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
  1355. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
  1356. lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  1357. lacl->se_lun = lun;
  1358. if (core_update_device_list_for_node(lun, lacl, lacl->mapped_lun,
  1359. lun_access, nacl, tpg, 1) < 0)
  1360. return -EINVAL;
  1361. spin_lock(&lun->lun_acl_lock);
  1362. list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
  1363. atomic_inc(&lun->lun_acl_count);
  1364. smp_mb__after_atomic_inc();
  1365. spin_unlock(&lun->lun_acl_lock);
  1366. pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
  1367. " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  1368. tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
  1369. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
  1370. lacl->initiatorname);
  1371. /*
  1372. * Check to see if there are any existing persistent reservation APTPL
  1373. * pre-registrations that need to be enabled for this LUN ACL..
  1374. */
  1375. core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, lacl);
  1376. return 0;
  1377. }
  1378. /* core_dev_del_initiator_node_lun_acl():
  1379. *
  1380. *
  1381. */
  1382. int core_dev_del_initiator_node_lun_acl(
  1383. struct se_portal_group *tpg,
  1384. struct se_lun *lun,
  1385. struct se_lun_acl *lacl)
  1386. {
  1387. struct se_node_acl *nacl;
  1388. nacl = lacl->se_lun_nacl;
  1389. if (!nacl)
  1390. return -EINVAL;
  1391. spin_lock(&lun->lun_acl_lock);
  1392. list_del(&lacl->lacl_list);
  1393. atomic_dec(&lun->lun_acl_count);
  1394. smp_mb__after_atomic_dec();
  1395. spin_unlock(&lun->lun_acl_lock);
  1396. core_update_device_list_for_node(lun, NULL, lacl->mapped_lun,
  1397. TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg, 0);
  1398. lacl->se_lun = NULL;
  1399. pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
  1400. " InitiatorNode: %s Mapped LUN: %u\n",
  1401. tpg->se_tpg_tfo->get_fabric_name(),
  1402. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  1403. lacl->initiatorname, lacl->mapped_lun);
  1404. return 0;
  1405. }
  1406. void core_dev_free_initiator_node_lun_acl(
  1407. struct se_portal_group *tpg,
  1408. struct se_lun_acl *lacl)
  1409. {
  1410. pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
  1411. " Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  1412. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  1413. tpg->se_tpg_tfo->get_fabric_name(),
  1414. lacl->initiatorname, lacl->mapped_lun);
  1415. kfree(lacl);
  1416. }
  1417. int core_dev_setup_virtual_lun0(void)
  1418. {
  1419. struct se_hba *hba;
  1420. struct se_device *dev;
  1421. struct se_subsystem_dev *se_dev = NULL;
  1422. struct se_subsystem_api *t;
  1423. char buf[16];
  1424. int ret;
  1425. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  1426. if (IS_ERR(hba))
  1427. return PTR_ERR(hba);
  1428. lun0_hba = hba;
  1429. t = hba->transport;
  1430. se_dev = kzalloc(sizeof(struct se_subsystem_dev), GFP_KERNEL);
  1431. if (!se_dev) {
  1432. pr_err("Unable to allocate memory for"
  1433. " struct se_subsystem_dev\n");
  1434. ret = -ENOMEM;
  1435. goto out;
  1436. }
  1437. INIT_LIST_HEAD(&se_dev->t10_wwn.t10_vpd_list);
  1438. spin_lock_init(&se_dev->t10_wwn.t10_vpd_lock);
  1439. INIT_LIST_HEAD(&se_dev->t10_pr.registration_list);
  1440. INIT_LIST_HEAD(&se_dev->t10_pr.aptpl_reg_list);
  1441. spin_lock_init(&se_dev->t10_pr.registration_lock);
  1442. spin_lock_init(&se_dev->t10_pr.aptpl_reg_lock);
  1443. INIT_LIST_HEAD(&se_dev->t10_alua.tg_pt_gps_list);
  1444. spin_lock_init(&se_dev->t10_alua.tg_pt_gps_lock);
  1445. spin_lock_init(&se_dev->se_dev_lock);
  1446. se_dev->t10_pr.pr_aptpl_buf_len = PR_APTPL_BUF_LEN;
  1447. se_dev->t10_wwn.t10_sub_dev = se_dev;
  1448. se_dev->t10_alua.t10_sub_dev = se_dev;
  1449. se_dev->se_dev_attrib.da_sub_dev = se_dev;
  1450. se_dev->se_dev_hba = hba;
  1451. se_dev->se_dev_su_ptr = t->allocate_virtdevice(hba, "virt_lun0");
  1452. if (!se_dev->se_dev_su_ptr) {
  1453. pr_err("Unable to locate subsystem dependent pointer"
  1454. " from allocate_virtdevice()\n");
  1455. ret = -ENOMEM;
  1456. goto out;
  1457. }
  1458. lun0_su_dev = se_dev;
  1459. memset(buf, 0, 16);
  1460. sprintf(buf, "rd_pages=8");
  1461. t->set_configfs_dev_params(hba, se_dev, buf, sizeof(buf));
  1462. dev = t->create_virtdevice(hba, se_dev, se_dev->se_dev_su_ptr);
  1463. if (IS_ERR(dev)) {
  1464. ret = PTR_ERR(dev);
  1465. goto out;
  1466. }
  1467. dev->dev_link_magic = SE_DEV_LINK_MAGIC;
  1468. se_dev->se_dev_ptr = dev;
  1469. g_lun0_dev = dev;
  1470. return 0;
  1471. out:
  1472. lun0_su_dev = NULL;
  1473. kfree(se_dev);
  1474. if (lun0_hba) {
  1475. core_delete_hba(lun0_hba);
  1476. lun0_hba = NULL;
  1477. }
  1478. return ret;
  1479. }
  1480. void core_dev_release_virtual_lun0(void)
  1481. {
  1482. struct se_hba *hba = lun0_hba;
  1483. struct se_subsystem_dev *su_dev = lun0_su_dev;
  1484. if (!hba)
  1485. return;
  1486. if (g_lun0_dev)
  1487. se_free_virtual_device(g_lun0_dev, hba);
  1488. kfree(su_dev);
  1489. core_delete_hba(hba);
  1490. }