target_core_device.c 41 KB

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