target_core_device.c 43 KB

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