target_core_device.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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_node_acl *nacl = se_sess->se_node_acl;
  58. struct se_device *dev;
  59. struct se_dev_entry *deve;
  60. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  61. return TCM_NON_EXISTENT_LUN;
  62. rcu_read_lock();
  63. deve = target_nacl_find_deve(nacl, unpacked_lun);
  64. if (deve) {
  65. atomic_long_inc(&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. rcu_read_unlock();
  73. return TCM_WRITE_PROTECTED;
  74. }
  75. if (se_cmd->data_direction == DMA_TO_DEVICE)
  76. atomic_long_add(se_cmd->data_length,
  77. &deve->write_bytes);
  78. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  79. atomic_long_add(se_cmd->data_length,
  80. &deve->read_bytes);
  81. se_lun = rcu_dereference(deve->se_lun);
  82. se_cmd->se_lun = rcu_dereference(deve->se_lun);
  83. se_cmd->pr_res_key = deve->pr_res_key;
  84. se_cmd->orig_fe_lun = unpacked_lun;
  85. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  86. percpu_ref_get(&se_lun->lun_ref);
  87. se_cmd->lun_ref_active = true;
  88. }
  89. rcu_read_unlock();
  90. if (!se_lun) {
  91. /*
  92. * Use the se_portal_group->tpg_virt_lun0 to allow for
  93. * REPORT_LUNS, et al to be returned when no active
  94. * MappedLUN=0 exists for this Initiator Port.
  95. */
  96. if (unpacked_lun != 0) {
  97. pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  98. " Access for 0x%08x\n",
  99. se_cmd->se_tfo->get_fabric_name(),
  100. unpacked_lun);
  101. return TCM_NON_EXISTENT_LUN;
  102. }
  103. /*
  104. * Force WRITE PROTECT for virtual LUN 0
  105. */
  106. if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
  107. (se_cmd->data_direction != DMA_NONE))
  108. return TCM_WRITE_PROTECTED;
  109. se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  110. se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0;
  111. se_cmd->orig_fe_lun = 0;
  112. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  113. percpu_ref_get(&se_lun->lun_ref);
  114. se_cmd->lun_ref_active = true;
  115. }
  116. /* Directly associate cmd with se_dev */
  117. se_cmd->se_dev = se_lun->lun_se_dev;
  118. dev = se_lun->lun_se_dev;
  119. atomic_long_inc(&dev->num_cmds);
  120. if (se_cmd->data_direction == DMA_TO_DEVICE)
  121. atomic_long_add(se_cmd->data_length, &dev->write_bytes);
  122. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  123. atomic_long_add(se_cmd->data_length, &dev->read_bytes);
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(transport_lookup_cmd_lun);
  127. int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
  128. {
  129. struct se_dev_entry *deve;
  130. struct se_lun *se_lun = NULL;
  131. struct se_session *se_sess = se_cmd->se_sess;
  132. struct se_node_acl *nacl = se_sess->se_node_acl;
  133. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  134. unsigned long flags;
  135. if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
  136. return -ENODEV;
  137. rcu_read_lock();
  138. deve = target_nacl_find_deve(nacl, unpacked_lun);
  139. if (deve) {
  140. se_tmr->tmr_lun = rcu_dereference(deve->se_lun);
  141. se_cmd->se_lun = rcu_dereference(deve->se_lun);
  142. se_lun = rcu_dereference(deve->se_lun);
  143. se_cmd->pr_res_key = deve->pr_res_key;
  144. se_cmd->orig_fe_lun = unpacked_lun;
  145. }
  146. rcu_read_unlock();
  147. if (!se_lun) {
  148. pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  149. " Access for 0x%08x\n",
  150. se_cmd->se_tfo->get_fabric_name(),
  151. unpacked_lun);
  152. return -ENODEV;
  153. }
  154. /* Directly associate cmd with se_dev */
  155. se_cmd->se_dev = se_lun->lun_se_dev;
  156. se_tmr->tmr_dev = se_lun->lun_se_dev;
  157. spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
  158. list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
  159. spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
  160. return 0;
  161. }
  162. EXPORT_SYMBOL(transport_lookup_tmr_lun);
  163. bool target_lun_is_rdonly(struct se_cmd *cmd)
  164. {
  165. struct se_session *se_sess = cmd->se_sess;
  166. struct se_dev_entry *deve;
  167. bool ret;
  168. if (cmd->se_lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY)
  169. return true;
  170. rcu_read_lock();
  171. deve = target_nacl_find_deve(se_sess->se_node_acl, cmd->orig_fe_lun);
  172. ret = (deve && deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY);
  173. rcu_read_unlock();
  174. return ret;
  175. }
  176. EXPORT_SYMBOL(target_lun_is_rdonly);
  177. /*
  178. * This function is called from core_scsi3_emulate_pro_register_and_move()
  179. * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_kref
  180. * when a matching rtpi is found.
  181. */
  182. struct se_dev_entry *core_get_se_deve_from_rtpi(
  183. struct se_node_acl *nacl,
  184. u16 rtpi)
  185. {
  186. struct se_dev_entry *deve;
  187. struct se_lun *lun;
  188. struct se_portal_group *tpg = nacl->se_tpg;
  189. rcu_read_lock();
  190. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  191. lun = rcu_dereference(deve->se_lun);
  192. if (!lun) {
  193. pr_err("%s device entries device pointer is"
  194. " NULL, but Initiator has access.\n",
  195. tpg->se_tpg_tfo->get_fabric_name());
  196. continue;
  197. }
  198. if (lun->lun_rtpi != rtpi)
  199. continue;
  200. kref_get(&deve->pr_kref);
  201. rcu_read_unlock();
  202. return deve;
  203. }
  204. rcu_read_unlock();
  205. return NULL;
  206. }
  207. void core_free_device_list_for_node(
  208. struct se_node_acl *nacl,
  209. struct se_portal_group *tpg)
  210. {
  211. struct se_dev_entry *deve;
  212. mutex_lock(&nacl->lun_entry_mutex);
  213. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  214. struct se_lun *lun = rcu_dereference_check(deve->se_lun,
  215. lockdep_is_held(&nacl->lun_entry_mutex));
  216. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  217. }
  218. mutex_unlock(&nacl->lun_entry_mutex);
  219. }
  220. void core_update_device_list_access(
  221. u32 mapped_lun,
  222. u32 lun_access,
  223. struct se_node_acl *nacl)
  224. {
  225. struct se_dev_entry *deve;
  226. mutex_lock(&nacl->lun_entry_mutex);
  227. deve = target_nacl_find_deve(nacl, mapped_lun);
  228. if (deve) {
  229. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
  230. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
  231. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  232. } else {
  233. deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
  234. deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  235. }
  236. }
  237. mutex_unlock(&nacl->lun_entry_mutex);
  238. }
  239. /*
  240. * Called with rcu_read_lock or nacl->device_list_lock held.
  241. */
  242. struct se_dev_entry *target_nacl_find_deve(struct se_node_acl *nacl, u32 mapped_lun)
  243. {
  244. struct se_dev_entry *deve;
  245. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
  246. if (deve->mapped_lun == mapped_lun)
  247. return deve;
  248. return NULL;
  249. }
  250. EXPORT_SYMBOL(target_nacl_find_deve);
  251. void target_pr_kref_release(struct kref *kref)
  252. {
  253. struct se_dev_entry *deve = container_of(kref, struct se_dev_entry,
  254. pr_kref);
  255. complete(&deve->pr_comp);
  256. }
  257. /* core_enable_device_list_for_node():
  258. *
  259. *
  260. */
  261. int core_enable_device_list_for_node(
  262. struct se_lun *lun,
  263. struct se_lun_acl *lun_acl,
  264. u32 mapped_lun,
  265. u32 lun_access,
  266. struct se_node_acl *nacl,
  267. struct se_portal_group *tpg)
  268. {
  269. struct se_port *port = lun->lun_sep;
  270. struct se_dev_entry *orig, *new;
  271. new = kzalloc(sizeof(*new), GFP_KERNEL);
  272. if (!new) {
  273. pr_err("Unable to allocate se_dev_entry memory\n");
  274. return -ENOMEM;
  275. }
  276. atomic_set(&new->ua_count, 0);
  277. spin_lock_init(&new->ua_lock);
  278. INIT_LIST_HEAD(&new->alua_port_list);
  279. INIT_LIST_HEAD(&new->ua_list);
  280. new->mapped_lun = mapped_lun;
  281. kref_init(&new->pr_kref);
  282. init_completion(&new->pr_comp);
  283. if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE)
  284. new->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
  285. else
  286. new->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
  287. new->creation_time = get_jiffies_64();
  288. new->attach_count++;
  289. mutex_lock(&nacl->lun_entry_mutex);
  290. orig = target_nacl_find_deve(nacl, mapped_lun);
  291. if (orig && orig->se_lun) {
  292. struct se_lun *orig_lun = rcu_dereference_check(orig->se_lun,
  293. lockdep_is_held(&nacl->lun_entry_mutex));
  294. if (orig_lun != lun) {
  295. pr_err("Existing orig->se_lun doesn't match new lun"
  296. " for dynamic -> explicit NodeACL conversion:"
  297. " %s\n", nacl->initiatorname);
  298. mutex_unlock(&nacl->lun_entry_mutex);
  299. kfree(new);
  300. return -EINVAL;
  301. }
  302. BUG_ON(orig->se_lun_acl != NULL);
  303. rcu_assign_pointer(new->se_lun, lun);
  304. rcu_assign_pointer(new->se_lun_acl, lun_acl);
  305. hlist_del_rcu(&orig->link);
  306. hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
  307. mutex_unlock(&nacl->lun_entry_mutex);
  308. spin_lock_bh(&port->sep_alua_lock);
  309. list_del(&orig->alua_port_list);
  310. list_add_tail(&new->alua_port_list, &port->sep_alua_list);
  311. spin_unlock_bh(&port->sep_alua_lock);
  312. kref_put(&orig->pr_kref, target_pr_kref_release);
  313. wait_for_completion(&orig->pr_comp);
  314. kfree_rcu(orig, rcu_head);
  315. return 0;
  316. }
  317. rcu_assign_pointer(new->se_lun, lun);
  318. rcu_assign_pointer(new->se_lun_acl, lun_acl);
  319. hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
  320. mutex_unlock(&nacl->lun_entry_mutex);
  321. spin_lock_bh(&port->sep_alua_lock);
  322. list_add_tail(&new->alua_port_list, &port->sep_alua_list);
  323. spin_unlock_bh(&port->sep_alua_lock);
  324. return 0;
  325. }
  326. /*
  327. * Called with se_node_acl->lun_entry_mutex held.
  328. */
  329. void core_disable_device_list_for_node(
  330. struct se_lun *lun,
  331. struct se_dev_entry *orig,
  332. struct se_node_acl *nacl,
  333. struct se_portal_group *tpg)
  334. {
  335. struct se_port *port = lun->lun_sep;
  336. /*
  337. * If the MappedLUN entry is being disabled, the entry in
  338. * port->sep_alua_list must be removed now before clearing the
  339. * struct se_dev_entry pointers below as logic in
  340. * core_alua_do_transition_tg_pt() depends on these being present.
  341. *
  342. * deve->se_lun_acl will be NULL for demo-mode created LUNs
  343. * that have not been explicitly converted to MappedLUNs ->
  344. * struct se_lun_acl, but we remove deve->alua_port_list from
  345. * port->sep_alua_list. This also means that active UAs and
  346. * NodeACL context specific PR metadata for demo-mode
  347. * MappedLUN *deve will be released below..
  348. */
  349. spin_lock_bh(&port->sep_alua_lock);
  350. list_del(&orig->alua_port_list);
  351. spin_unlock_bh(&port->sep_alua_lock);
  352. /*
  353. * Disable struct se_dev_entry LUN ACL mapping
  354. */
  355. core_scsi3_ua_release_all(orig);
  356. hlist_del_rcu(&orig->link);
  357. clear_bit(DEF_PR_REG_ACTIVE, &orig->deve_flags);
  358. rcu_assign_pointer(orig->se_lun, NULL);
  359. rcu_assign_pointer(orig->se_lun_acl, NULL);
  360. orig->lun_flags = 0;
  361. orig->creation_time = 0;
  362. orig->attach_count--;
  363. /*
  364. * Before firing off RCU callback, wait for any in process SPEC_I_PT=1
  365. * or REGISTER_AND_MOVE PR operation to complete.
  366. */
  367. kref_put(&orig->pr_kref, target_pr_kref_release);
  368. wait_for_completion(&orig->pr_comp);
  369. kfree_rcu(orig, rcu_head);
  370. core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
  371. }
  372. /* core_clear_lun_from_tpg():
  373. *
  374. *
  375. */
  376. void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
  377. {
  378. struct se_node_acl *nacl;
  379. struct se_dev_entry *deve;
  380. mutex_lock(&tpg->acl_node_mutex);
  381. list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
  382. mutex_lock(&nacl->lun_entry_mutex);
  383. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  384. struct se_lun *tmp_lun = rcu_dereference_check(deve->se_lun,
  385. lockdep_is_held(&nacl->lun_entry_mutex));
  386. if (lun != tmp_lun)
  387. continue;
  388. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  389. }
  390. mutex_unlock(&nacl->lun_entry_mutex);
  391. }
  392. mutex_unlock(&tpg->acl_node_mutex);
  393. }
  394. static struct se_port *core_alloc_port(struct se_device *dev)
  395. {
  396. struct se_port *port, *port_tmp;
  397. port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
  398. if (!port) {
  399. pr_err("Unable to allocate struct se_port\n");
  400. return ERR_PTR(-ENOMEM);
  401. }
  402. INIT_LIST_HEAD(&port->sep_alua_list);
  403. INIT_LIST_HEAD(&port->sep_list);
  404. atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  405. spin_lock_init(&port->sep_alua_lock);
  406. mutex_init(&port->sep_tg_pt_md_mutex);
  407. spin_lock(&dev->se_port_lock);
  408. if (dev->dev_port_count == 0x0000ffff) {
  409. pr_warn("Reached dev->dev_port_count =="
  410. " 0x0000ffff\n");
  411. spin_unlock(&dev->se_port_lock);
  412. return ERR_PTR(-ENOSPC);
  413. }
  414. again:
  415. /*
  416. * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
  417. * Here is the table from spc4r17 section 7.7.3.8.
  418. *
  419. * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
  420. *
  421. * Code Description
  422. * 0h Reserved
  423. * 1h Relative port 1, historically known as port A
  424. * 2h Relative port 2, historically known as port B
  425. * 3h to FFFFh Relative port 3 through 65 535
  426. */
  427. port->sep_rtpi = dev->dev_rpti_counter++;
  428. if (!port->sep_rtpi)
  429. goto again;
  430. list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
  431. /*
  432. * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
  433. * for 16-bit wrap..
  434. */
  435. if (port->sep_rtpi == port_tmp->sep_rtpi)
  436. goto again;
  437. }
  438. spin_unlock(&dev->se_port_lock);
  439. return port;
  440. }
  441. static void core_export_port(
  442. struct se_device *dev,
  443. struct se_portal_group *tpg,
  444. struct se_port *port,
  445. struct se_lun *lun)
  446. {
  447. struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
  448. spin_lock(&dev->se_port_lock);
  449. spin_lock(&lun->lun_sep_lock);
  450. port->sep_tpg = tpg;
  451. port->sep_lun = lun;
  452. lun->lun_sep = port;
  453. spin_unlock(&lun->lun_sep_lock);
  454. list_add_tail(&port->sep_list, &dev->dev_sep_list);
  455. spin_unlock(&dev->se_port_lock);
  456. if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) &&
  457. !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
  458. tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
  459. if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
  460. pr_err("Unable to allocate t10_alua_tg_pt"
  461. "_gp_member_t\n");
  462. return;
  463. }
  464. spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  465. __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
  466. dev->t10_alua.default_tg_pt_gp);
  467. spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  468. pr_debug("%s/%s: Adding to default ALUA Target Port"
  469. " Group: alua/default_tg_pt_gp\n",
  470. dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
  471. }
  472. dev->dev_port_count++;
  473. port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
  474. }
  475. /*
  476. * Called with struct se_device->se_port_lock spinlock held.
  477. */
  478. static void core_release_port(struct se_device *dev, struct se_port *port)
  479. __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
  480. {
  481. /*
  482. * Wait for any port reference for PR ALL_TG_PT=1 operation
  483. * to complete in __core_scsi3_alloc_registration()
  484. */
  485. spin_unlock(&dev->se_port_lock);
  486. if (atomic_read(&port->sep_tg_pt_ref_cnt))
  487. cpu_relax();
  488. spin_lock(&dev->se_port_lock);
  489. core_alua_free_tg_pt_gp_mem(port);
  490. list_del(&port->sep_list);
  491. dev->dev_port_count--;
  492. kfree(port);
  493. }
  494. int core_dev_export(
  495. struct se_device *dev,
  496. struct se_portal_group *tpg,
  497. struct se_lun *lun)
  498. {
  499. struct se_hba *hba = dev->se_hba;
  500. struct se_port *port;
  501. port = core_alloc_port(dev);
  502. if (IS_ERR(port))
  503. return PTR_ERR(port);
  504. lun->lun_index = dev->dev_index;
  505. lun->lun_se_dev = dev;
  506. lun->lun_rtpi = port->sep_rtpi;
  507. spin_lock(&hba->device_lock);
  508. dev->export_count++;
  509. spin_unlock(&hba->device_lock);
  510. core_export_port(dev, tpg, port, lun);
  511. return 0;
  512. }
  513. void core_dev_unexport(
  514. struct se_device *dev,
  515. struct se_portal_group *tpg,
  516. struct se_lun *lun)
  517. {
  518. struct se_hba *hba = dev->se_hba;
  519. struct se_port *port = lun->lun_sep;
  520. spin_lock(&lun->lun_sep_lock);
  521. if (lun->lun_se_dev == NULL) {
  522. spin_unlock(&lun->lun_sep_lock);
  523. return;
  524. }
  525. spin_unlock(&lun->lun_sep_lock);
  526. spin_lock(&dev->se_port_lock);
  527. core_release_port(dev, port);
  528. spin_unlock(&dev->se_port_lock);
  529. spin_lock(&hba->device_lock);
  530. dev->export_count--;
  531. spin_unlock(&hba->device_lock);
  532. lun->lun_sep = NULL;
  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. bool se_dev_check_wce(struct se_device *dev)
  562. {
  563. bool wce = false;
  564. if (dev->transport->get_write_cache)
  565. wce = dev->transport->get_write_cache(dev);
  566. else if (dev->dev_attrib.emulate_write_cache > 0)
  567. wce = true;
  568. return wce;
  569. }
  570. int core_dev_add_lun(
  571. struct se_portal_group *tpg,
  572. struct se_device *dev,
  573. struct se_lun *lun)
  574. {
  575. int rc;
  576. rc = core_tpg_add_lun(tpg, lun,
  577. TRANSPORT_LUNFLAGS_READ_WRITE, dev);
  578. if (rc < 0)
  579. return rc;
  580. pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
  581. " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  582. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  583. tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
  584. /*
  585. * Update LUN maps for dynamically added initiators when
  586. * generate_node_acl is enabled.
  587. */
  588. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  589. struct se_node_acl *acl;
  590. mutex_lock(&tpg->acl_node_mutex);
  591. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  592. if (acl->dynamic_node_acl &&
  593. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  594. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  595. core_tpg_add_node_to_devs(acl, tpg, lun);
  596. }
  597. }
  598. mutex_unlock(&tpg->acl_node_mutex);
  599. }
  600. return 0;
  601. }
  602. /* core_dev_del_lun():
  603. *
  604. *
  605. */
  606. void core_dev_del_lun(
  607. struct se_portal_group *tpg,
  608. struct se_lun *lun)
  609. {
  610. pr_debug("%s_TPG[%u]_LUN[%u] - Deactivating %s Logical Unit from"
  611. " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
  612. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  613. tpg->se_tpg_tfo->get_fabric_name());
  614. core_tpg_remove_lun(tpg, lun);
  615. }
  616. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  617. struct se_portal_group *tpg,
  618. struct se_node_acl *nacl,
  619. u32 mapped_lun,
  620. int *ret)
  621. {
  622. struct se_lun_acl *lacl;
  623. if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
  624. pr_err("%s InitiatorName exceeds maximum size.\n",
  625. tpg->se_tpg_tfo->get_fabric_name());
  626. *ret = -EOVERFLOW;
  627. return NULL;
  628. }
  629. lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
  630. if (!lacl) {
  631. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  632. *ret = -ENOMEM;
  633. return NULL;
  634. }
  635. lacl->mapped_lun = mapped_lun;
  636. lacl->se_lun_nacl = nacl;
  637. snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s",
  638. nacl->initiatorname);
  639. return lacl;
  640. }
  641. int core_dev_add_initiator_node_lun_acl(
  642. struct se_portal_group *tpg,
  643. struct se_lun_acl *lacl,
  644. struct se_lun *lun,
  645. u32 lun_access)
  646. {
  647. struct se_node_acl *nacl = lacl->se_lun_nacl;
  648. if (!nacl)
  649. return -EINVAL;
  650. if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
  651. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
  652. lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
  653. lacl->se_lun = lun;
  654. if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
  655. lun_access, nacl, tpg) < 0)
  656. return -EINVAL;
  657. pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
  658. " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  659. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, lacl->mapped_lun,
  660. (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
  661. lacl->initiatorname);
  662. /*
  663. * Check to see if there are any existing persistent reservation APTPL
  664. * pre-registrations that need to be enabled for this LUN ACL..
  665. */
  666. core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, nacl,
  667. lacl->mapped_lun);
  668. return 0;
  669. }
  670. int core_dev_del_initiator_node_lun_acl(
  671. struct se_portal_group *tpg,
  672. struct se_lun *lun,
  673. struct se_lun_acl *lacl)
  674. {
  675. struct se_node_acl *nacl;
  676. struct se_dev_entry *deve;
  677. nacl = lacl->se_lun_nacl;
  678. if (!nacl)
  679. return -EINVAL;
  680. mutex_lock(&nacl->lun_entry_mutex);
  681. deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
  682. if (deve)
  683. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  684. mutex_unlock(&nacl->lun_entry_mutex);
  685. pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
  686. " InitiatorNode: %s Mapped LUN: %u\n",
  687. tpg->se_tpg_tfo->get_fabric_name(),
  688. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  689. lacl->initiatorname, lacl->mapped_lun);
  690. return 0;
  691. }
  692. void core_dev_free_initiator_node_lun_acl(
  693. struct se_portal_group *tpg,
  694. struct se_lun_acl *lacl)
  695. {
  696. pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
  697. " Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  698. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  699. tpg->se_tpg_tfo->get_fabric_name(),
  700. lacl->initiatorname, lacl->mapped_lun);
  701. kfree(lacl);
  702. }
  703. static void scsi_dump_inquiry(struct se_device *dev)
  704. {
  705. struct t10_wwn *wwn = &dev->t10_wwn;
  706. char buf[17];
  707. int i, device_type;
  708. /*
  709. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  710. */
  711. for (i = 0; i < 8; i++)
  712. if (wwn->vendor[i] >= 0x20)
  713. buf[i] = wwn->vendor[i];
  714. else
  715. buf[i] = ' ';
  716. buf[i] = '\0';
  717. pr_debug(" Vendor: %s\n", buf);
  718. for (i = 0; i < 16; i++)
  719. if (wwn->model[i] >= 0x20)
  720. buf[i] = wwn->model[i];
  721. else
  722. buf[i] = ' ';
  723. buf[i] = '\0';
  724. pr_debug(" Model: %s\n", buf);
  725. for (i = 0; i < 4; i++)
  726. if (wwn->revision[i] >= 0x20)
  727. buf[i] = wwn->revision[i];
  728. else
  729. buf[i] = ' ';
  730. buf[i] = '\0';
  731. pr_debug(" Revision: %s\n", buf);
  732. device_type = dev->transport->get_device_type(dev);
  733. pr_debug(" Type: %s ", scsi_device_type(device_type));
  734. }
  735. struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
  736. {
  737. struct se_device *dev;
  738. struct se_lun *xcopy_lun;
  739. dev = hba->backend->ops->alloc_device(hba, name);
  740. if (!dev)
  741. return NULL;
  742. dev->dev_link_magic = SE_DEV_LINK_MAGIC;
  743. dev->se_hba = hba;
  744. dev->transport = hba->backend->ops;
  745. dev->prot_length = sizeof(struct se_dif_v1_tuple);
  746. INIT_LIST_HEAD(&dev->dev_list);
  747. INIT_LIST_HEAD(&dev->dev_sep_list);
  748. INIT_LIST_HEAD(&dev->dev_tmr_list);
  749. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  750. INIT_LIST_HEAD(&dev->state_list);
  751. INIT_LIST_HEAD(&dev->qf_cmd_list);
  752. INIT_LIST_HEAD(&dev->g_dev_node);
  753. spin_lock_init(&dev->execute_task_lock);
  754. spin_lock_init(&dev->delayed_cmd_lock);
  755. spin_lock_init(&dev->dev_reservation_lock);
  756. spin_lock_init(&dev->se_port_lock);
  757. spin_lock_init(&dev->se_tmr_lock);
  758. spin_lock_init(&dev->qf_cmd_lock);
  759. sema_init(&dev->caw_sem, 1);
  760. atomic_set(&dev->dev_ordered_id, 0);
  761. INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
  762. spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
  763. INIT_LIST_HEAD(&dev->t10_pr.registration_list);
  764. INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
  765. spin_lock_init(&dev->t10_pr.registration_lock);
  766. spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
  767. INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
  768. spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
  769. INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
  770. spin_lock_init(&dev->t10_alua.lba_map_lock);
  771. dev->t10_wwn.t10_dev = dev;
  772. dev->t10_alua.t10_dev = dev;
  773. dev->dev_attrib.da_dev = dev;
  774. dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
  775. dev->dev_attrib.emulate_dpo = 1;
  776. dev->dev_attrib.emulate_fua_write = 1;
  777. dev->dev_attrib.emulate_fua_read = 1;
  778. dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
  779. dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
  780. dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
  781. dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
  782. dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
  783. dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
  784. dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
  785. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
  786. dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
  787. dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
  788. dev->dev_attrib.is_nonrot = DA_IS_NONROT;
  789. dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
  790. dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
  791. dev->dev_attrib.max_unmap_block_desc_count =
  792. DA_MAX_UNMAP_BLOCK_DESC_COUNT;
  793. dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
  794. dev->dev_attrib.unmap_granularity_alignment =
  795. DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
  796. dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
  797. xcopy_lun = &dev->xcopy_lun;
  798. xcopy_lun->lun_se_dev = dev;
  799. init_completion(&xcopy_lun->lun_shutdown_comp);
  800. spin_lock_init(&xcopy_lun->lun_sep_lock);
  801. init_completion(&xcopy_lun->lun_ref_comp);
  802. return dev;
  803. }
  804. int target_configure_device(struct se_device *dev)
  805. {
  806. struct se_hba *hba = dev->se_hba;
  807. int ret;
  808. if (dev->dev_flags & DF_CONFIGURED) {
  809. pr_err("se_dev->se_dev_ptr already set for storage"
  810. " object\n");
  811. return -EEXIST;
  812. }
  813. ret = dev->transport->configure_device(dev);
  814. if (ret)
  815. goto out;
  816. /*
  817. * XXX: there is not much point to have two different values here..
  818. */
  819. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  820. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  821. /*
  822. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  823. */
  824. dev->dev_attrib.hw_max_sectors =
  825. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  826. dev->dev_attrib.hw_block_size);
  827. dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
  828. dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
  829. dev->creation_time = get_jiffies_64();
  830. ret = core_setup_alua(dev);
  831. if (ret)
  832. goto out;
  833. /*
  834. * Startup the struct se_device processing thread
  835. */
  836. dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
  837. dev->transport->name);
  838. if (!dev->tmr_wq) {
  839. pr_err("Unable to create tmr workqueue for %s\n",
  840. dev->transport->name);
  841. ret = -ENOMEM;
  842. goto out_free_alua;
  843. }
  844. /*
  845. * Setup work_queue for QUEUE_FULL
  846. */
  847. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  848. /*
  849. * Preload the initial INQUIRY const values if we are doing
  850. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  851. * passthrough because this is being provided by the backend LLD.
  852. */
  853. if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)) {
  854. strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  855. strncpy(&dev->t10_wwn.model[0],
  856. dev->transport->inquiry_prod, 16);
  857. strncpy(&dev->t10_wwn.revision[0],
  858. dev->transport->inquiry_rev, 4);
  859. }
  860. scsi_dump_inquiry(dev);
  861. spin_lock(&hba->device_lock);
  862. hba->dev_count++;
  863. spin_unlock(&hba->device_lock);
  864. mutex_lock(&g_device_mutex);
  865. list_add_tail(&dev->g_dev_node, &g_device_list);
  866. mutex_unlock(&g_device_mutex);
  867. dev->dev_flags |= DF_CONFIGURED;
  868. return 0;
  869. out_free_alua:
  870. core_alua_free_lu_gp_mem(dev);
  871. out:
  872. se_release_vpd_for_dev(dev);
  873. return ret;
  874. }
  875. void target_free_device(struct se_device *dev)
  876. {
  877. struct se_hba *hba = dev->se_hba;
  878. WARN_ON(!list_empty(&dev->dev_sep_list));
  879. if (dev->dev_flags & DF_CONFIGURED) {
  880. destroy_workqueue(dev->tmr_wq);
  881. mutex_lock(&g_device_mutex);
  882. list_del(&dev->g_dev_node);
  883. mutex_unlock(&g_device_mutex);
  884. spin_lock(&hba->device_lock);
  885. hba->dev_count--;
  886. spin_unlock(&hba->device_lock);
  887. }
  888. core_alua_free_lu_gp_mem(dev);
  889. core_alua_set_lba_map(dev, NULL, 0, 0);
  890. core_scsi3_free_all_registrations(dev);
  891. se_release_vpd_for_dev(dev);
  892. if (dev->transport->free_prot)
  893. dev->transport->free_prot(dev);
  894. dev->transport->free_device(dev);
  895. }
  896. int core_dev_setup_virtual_lun0(void)
  897. {
  898. struct se_hba *hba;
  899. struct se_device *dev;
  900. char buf[] = "rd_pages=8,rd_nullio=1";
  901. int ret;
  902. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  903. if (IS_ERR(hba))
  904. return PTR_ERR(hba);
  905. dev = target_alloc_device(hba, "virt_lun0");
  906. if (!dev) {
  907. ret = -ENOMEM;
  908. goto out_free_hba;
  909. }
  910. hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
  911. ret = target_configure_device(dev);
  912. if (ret)
  913. goto out_free_se_dev;
  914. lun0_hba = hba;
  915. g_lun0_dev = dev;
  916. return 0;
  917. out_free_se_dev:
  918. target_free_device(dev);
  919. out_free_hba:
  920. core_delete_hba(hba);
  921. return ret;
  922. }
  923. void core_dev_release_virtual_lun0(void)
  924. {
  925. struct se_hba *hba = lun0_hba;
  926. if (!hba)
  927. return;
  928. if (g_lun0_dev)
  929. target_free_device(g_lun0_dev);
  930. core_delete_hba(hba);
  931. }
  932. /*
  933. * Common CDB parsing for kernel and user passthrough.
  934. */
  935. sense_reason_t
  936. passthrough_parse_cdb(struct se_cmd *cmd,
  937. sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
  938. {
  939. unsigned char *cdb = cmd->t_task_cdb;
  940. /*
  941. * Clear a lun set in the cdb if the initiator talking to use spoke
  942. * and old standards version, as we can't assume the underlying device
  943. * won't choke up on it.
  944. */
  945. switch (cdb[0]) {
  946. case READ_10: /* SBC - RDProtect */
  947. case READ_12: /* SBC - RDProtect */
  948. case READ_16: /* SBC - RDProtect */
  949. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  950. case VERIFY: /* SBC - VRProtect */
  951. case VERIFY_16: /* SBC - VRProtect */
  952. case WRITE_VERIFY: /* SBC - VRProtect */
  953. case WRITE_VERIFY_12: /* SBC - VRProtect */
  954. case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
  955. break;
  956. default:
  957. cdb[1] &= 0x1f; /* clear logical unit number */
  958. break;
  959. }
  960. /*
  961. * For REPORT LUNS we always need to emulate the response, for everything
  962. * else, pass it up.
  963. */
  964. if (cdb[0] == REPORT_LUNS) {
  965. cmd->execute_cmd = spc_emulate_report_luns;
  966. return TCM_NO_SENSE;
  967. }
  968. /* Set DATA_CDB flag for ops that should have it */
  969. switch (cdb[0]) {
  970. case READ_6:
  971. case READ_10:
  972. case READ_12:
  973. case READ_16:
  974. case WRITE_6:
  975. case WRITE_10:
  976. case WRITE_12:
  977. case WRITE_16:
  978. case WRITE_VERIFY:
  979. case WRITE_VERIFY_12:
  980. case 0x8e: /* WRITE_VERIFY_16 */
  981. case COMPARE_AND_WRITE:
  982. case XDWRITEREAD_10:
  983. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  984. break;
  985. case VARIABLE_LENGTH_CMD:
  986. switch (get_unaligned_be16(&cdb[8])) {
  987. case READ_32:
  988. case WRITE_32:
  989. case 0x0c: /* WRITE_VERIFY_32 */
  990. case XDWRITEREAD_32:
  991. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  992. break;
  993. }
  994. }
  995. cmd->execute_cmd = exec_cmd;
  996. return TCM_NO_SENSE;
  997. }
  998. EXPORT_SYMBOL(passthrough_parse_cdb);