target_core_device.c 47 KB

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