target_core_device.c 46 KB

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