target_core_device.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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 <linux/t10-pi.h>
  36. #include <asm/unaligned.h>
  37. #include <net/sock.h>
  38. #include <net/tcp.h>
  39. #include <scsi/scsi_common.h>
  40. #include <scsi/scsi_proto.h>
  41. #include <target/target_core_base.h>
  42. #include <target/target_core_backend.h>
  43. #include <target/target_core_fabric.h>
  44. #include "target_core_internal.h"
  45. #include "target_core_alua.h"
  46. #include "target_core_pr.h"
  47. #include "target_core_ua.h"
  48. static DEFINE_MUTEX(device_mutex);
  49. static LIST_HEAD(device_list);
  50. static DEFINE_IDR(devices_idr);
  51. static struct se_hba *lun0_hba;
  52. /* not static, needed by tpg.c */
  53. struct se_device *g_lun0_dev;
  54. sense_reason_t
  55. transport_lookup_cmd_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
  56. {
  57. struct se_lun *se_lun = NULL;
  58. struct se_session *se_sess = se_cmd->se_sess;
  59. struct se_node_acl *nacl = se_sess->se_node_acl;
  60. struct se_dev_entry *deve;
  61. sense_reason_t ret = TCM_NO_SENSE;
  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. atomic_long_add(se_cmd->data_length,
  68. &deve->write_bytes);
  69. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  70. atomic_long_add(se_cmd->data_length,
  71. &deve->read_bytes);
  72. se_lun = rcu_dereference(deve->se_lun);
  73. if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
  74. se_lun = NULL;
  75. goto out_unlock;
  76. }
  77. se_cmd->se_lun = rcu_dereference(deve->se_lun);
  78. se_cmd->pr_res_key = deve->pr_res_key;
  79. se_cmd->orig_fe_lun = unpacked_lun;
  80. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  81. se_cmd->lun_ref_active = true;
  82. if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
  83. deve->lun_access_ro) {
  84. pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
  85. " Access for 0x%08llx\n",
  86. se_cmd->se_tfo->get_fabric_name(),
  87. unpacked_lun);
  88. rcu_read_unlock();
  89. ret = TCM_WRITE_PROTECTED;
  90. goto ref_dev;
  91. }
  92. }
  93. out_unlock:
  94. rcu_read_unlock();
  95. if (!se_lun) {
  96. /*
  97. * Use the se_portal_group->tpg_virt_lun0 to allow for
  98. * REPORT_LUNS, et al to be returned when no active
  99. * MappedLUN=0 exists for this Initiator Port.
  100. */
  101. if (unpacked_lun != 0) {
  102. pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  103. " Access for 0x%08llx\n",
  104. se_cmd->se_tfo->get_fabric_name(),
  105. unpacked_lun);
  106. return TCM_NON_EXISTENT_LUN;
  107. }
  108. se_lun = se_sess->se_tpg->tpg_virt_lun0;
  109. se_cmd->se_lun = se_sess->se_tpg->tpg_virt_lun0;
  110. se_cmd->orig_fe_lun = 0;
  111. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  112. percpu_ref_get(&se_lun->lun_ref);
  113. se_cmd->lun_ref_active = true;
  114. /*
  115. * Force WRITE PROTECT for virtual LUN 0
  116. */
  117. if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
  118. (se_cmd->data_direction != DMA_NONE)) {
  119. ret = TCM_WRITE_PROTECTED;
  120. goto ref_dev;
  121. }
  122. }
  123. /*
  124. * RCU reference protected by percpu se_lun->lun_ref taken above that
  125. * must drop to zero (including initial reference) before this se_lun
  126. * pointer can be kfree_rcu() by the final se_lun->lun_group put via
  127. * target_core_fabric_configfs.c:target_fabric_port_release
  128. */
  129. ref_dev:
  130. se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  131. atomic_long_inc(&se_cmd->se_dev->num_cmds);
  132. if (se_cmd->data_direction == DMA_TO_DEVICE)
  133. atomic_long_add(se_cmd->data_length,
  134. &se_cmd->se_dev->write_bytes);
  135. else if (se_cmd->data_direction == DMA_FROM_DEVICE)
  136. atomic_long_add(se_cmd->data_length,
  137. &se_cmd->se_dev->read_bytes);
  138. return ret;
  139. }
  140. EXPORT_SYMBOL(transport_lookup_cmd_lun);
  141. int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
  142. {
  143. struct se_dev_entry *deve;
  144. struct se_lun *se_lun = NULL;
  145. struct se_session *se_sess = se_cmd->se_sess;
  146. struct se_node_acl *nacl = se_sess->se_node_acl;
  147. struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
  148. unsigned long flags;
  149. rcu_read_lock();
  150. deve = target_nacl_find_deve(nacl, unpacked_lun);
  151. if (deve) {
  152. se_lun = rcu_dereference(deve->se_lun);
  153. if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
  154. se_lun = NULL;
  155. goto out_unlock;
  156. }
  157. se_cmd->se_lun = rcu_dereference(deve->se_lun);
  158. se_cmd->pr_res_key = deve->pr_res_key;
  159. se_cmd->orig_fe_lun = unpacked_lun;
  160. se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
  161. se_cmd->lun_ref_active = true;
  162. }
  163. out_unlock:
  164. rcu_read_unlock();
  165. if (!se_lun) {
  166. pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
  167. " Access for 0x%08llx\n",
  168. se_cmd->se_tfo->get_fabric_name(),
  169. unpacked_lun);
  170. return -ENODEV;
  171. }
  172. se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  173. se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev);
  174. spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
  175. list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
  176. spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
  177. return 0;
  178. }
  179. EXPORT_SYMBOL(transport_lookup_tmr_lun);
  180. bool target_lun_is_rdonly(struct se_cmd *cmd)
  181. {
  182. struct se_session *se_sess = cmd->se_sess;
  183. struct se_dev_entry *deve;
  184. bool ret;
  185. rcu_read_lock();
  186. deve = target_nacl_find_deve(se_sess->se_node_acl, cmd->orig_fe_lun);
  187. ret = deve && deve->lun_access_ro;
  188. rcu_read_unlock();
  189. return ret;
  190. }
  191. EXPORT_SYMBOL(target_lun_is_rdonly);
  192. /*
  193. * This function is called from core_scsi3_emulate_pro_register_and_move()
  194. * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_kref
  195. * when a matching rtpi is found.
  196. */
  197. struct se_dev_entry *core_get_se_deve_from_rtpi(
  198. struct se_node_acl *nacl,
  199. u16 rtpi)
  200. {
  201. struct se_dev_entry *deve;
  202. struct se_lun *lun;
  203. struct se_portal_group *tpg = nacl->se_tpg;
  204. rcu_read_lock();
  205. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  206. lun = rcu_dereference(deve->se_lun);
  207. if (!lun) {
  208. pr_err("%s device entries device pointer is"
  209. " NULL, but Initiator has access.\n",
  210. tpg->se_tpg_tfo->get_fabric_name());
  211. continue;
  212. }
  213. if (lun->lun_rtpi != rtpi)
  214. continue;
  215. kref_get(&deve->pr_kref);
  216. rcu_read_unlock();
  217. return deve;
  218. }
  219. rcu_read_unlock();
  220. return NULL;
  221. }
  222. void core_free_device_list_for_node(
  223. struct se_node_acl *nacl,
  224. struct se_portal_group *tpg)
  225. {
  226. struct se_dev_entry *deve;
  227. mutex_lock(&nacl->lun_entry_mutex);
  228. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  229. struct se_lun *lun = rcu_dereference_check(deve->se_lun,
  230. lockdep_is_held(&nacl->lun_entry_mutex));
  231. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  232. }
  233. mutex_unlock(&nacl->lun_entry_mutex);
  234. }
  235. void core_update_device_list_access(
  236. u64 mapped_lun,
  237. bool lun_access_ro,
  238. struct se_node_acl *nacl)
  239. {
  240. struct se_dev_entry *deve;
  241. mutex_lock(&nacl->lun_entry_mutex);
  242. deve = target_nacl_find_deve(nacl, mapped_lun);
  243. if (deve)
  244. deve->lun_access_ro = lun_access_ro;
  245. mutex_unlock(&nacl->lun_entry_mutex);
  246. }
  247. /*
  248. * Called with rcu_read_lock or nacl->device_list_lock held.
  249. */
  250. struct se_dev_entry *target_nacl_find_deve(struct se_node_acl *nacl, u64 mapped_lun)
  251. {
  252. struct se_dev_entry *deve;
  253. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
  254. if (deve->mapped_lun == mapped_lun)
  255. return deve;
  256. return NULL;
  257. }
  258. EXPORT_SYMBOL(target_nacl_find_deve);
  259. void target_pr_kref_release(struct kref *kref)
  260. {
  261. struct se_dev_entry *deve = container_of(kref, struct se_dev_entry,
  262. pr_kref);
  263. complete(&deve->pr_comp);
  264. }
  265. static void
  266. target_luns_data_has_changed(struct se_node_acl *nacl, struct se_dev_entry *new,
  267. bool skip_new)
  268. {
  269. struct se_dev_entry *tmp;
  270. rcu_read_lock();
  271. hlist_for_each_entry_rcu(tmp, &nacl->lun_entry_hlist, link) {
  272. if (skip_new && tmp == new)
  273. continue;
  274. core_scsi3_ua_allocate(tmp, 0x3F,
  275. ASCQ_3FH_REPORTED_LUNS_DATA_HAS_CHANGED);
  276. }
  277. rcu_read_unlock();
  278. }
  279. int core_enable_device_list_for_node(
  280. struct se_lun *lun,
  281. struct se_lun_acl *lun_acl,
  282. u64 mapped_lun,
  283. bool lun_access_ro,
  284. struct se_node_acl *nacl,
  285. struct se_portal_group *tpg)
  286. {
  287. struct se_dev_entry *orig, *new;
  288. new = kzalloc(sizeof(*new), GFP_KERNEL);
  289. if (!new) {
  290. pr_err("Unable to allocate se_dev_entry memory\n");
  291. return -ENOMEM;
  292. }
  293. atomic_set(&new->ua_count, 0);
  294. spin_lock_init(&new->ua_lock);
  295. INIT_LIST_HEAD(&new->ua_list);
  296. INIT_LIST_HEAD(&new->lun_link);
  297. new->mapped_lun = mapped_lun;
  298. kref_init(&new->pr_kref);
  299. init_completion(&new->pr_comp);
  300. new->lun_access_ro = lun_access_ro;
  301. new->creation_time = get_jiffies_64();
  302. new->attach_count++;
  303. mutex_lock(&nacl->lun_entry_mutex);
  304. orig = target_nacl_find_deve(nacl, mapped_lun);
  305. if (orig && orig->se_lun) {
  306. struct se_lun *orig_lun = rcu_dereference_check(orig->se_lun,
  307. lockdep_is_held(&nacl->lun_entry_mutex));
  308. if (orig_lun != lun) {
  309. pr_err("Existing orig->se_lun doesn't match new lun"
  310. " for dynamic -> explicit NodeACL conversion:"
  311. " %s\n", nacl->initiatorname);
  312. mutex_unlock(&nacl->lun_entry_mutex);
  313. kfree(new);
  314. return -EINVAL;
  315. }
  316. if (orig->se_lun_acl != NULL) {
  317. pr_warn_ratelimited("Detected existing explicit"
  318. " se_lun_acl->se_lun_group reference for %s"
  319. " mapped_lun: %llu, failing\n",
  320. nacl->initiatorname, mapped_lun);
  321. mutex_unlock(&nacl->lun_entry_mutex);
  322. kfree(new);
  323. return -EINVAL;
  324. }
  325. rcu_assign_pointer(new->se_lun, lun);
  326. rcu_assign_pointer(new->se_lun_acl, lun_acl);
  327. hlist_del_rcu(&orig->link);
  328. hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
  329. mutex_unlock(&nacl->lun_entry_mutex);
  330. spin_lock(&lun->lun_deve_lock);
  331. list_del(&orig->lun_link);
  332. list_add_tail(&new->lun_link, &lun->lun_deve_list);
  333. spin_unlock(&lun->lun_deve_lock);
  334. kref_put(&orig->pr_kref, target_pr_kref_release);
  335. wait_for_completion(&orig->pr_comp);
  336. target_luns_data_has_changed(nacl, new, true);
  337. kfree_rcu(orig, rcu_head);
  338. return 0;
  339. }
  340. rcu_assign_pointer(new->se_lun, lun);
  341. rcu_assign_pointer(new->se_lun_acl, lun_acl);
  342. hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
  343. mutex_unlock(&nacl->lun_entry_mutex);
  344. spin_lock(&lun->lun_deve_lock);
  345. list_add_tail(&new->lun_link, &lun->lun_deve_list);
  346. spin_unlock(&lun->lun_deve_lock);
  347. target_luns_data_has_changed(nacl, new, true);
  348. return 0;
  349. }
  350. /*
  351. * Called with se_node_acl->lun_entry_mutex held.
  352. */
  353. void core_disable_device_list_for_node(
  354. struct se_lun *lun,
  355. struct se_dev_entry *orig,
  356. struct se_node_acl *nacl,
  357. struct se_portal_group *tpg)
  358. {
  359. /*
  360. * rcu_dereference_raw protected by se_lun->lun_group symlink
  361. * reference to se_device->dev_group.
  362. */
  363. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  364. /*
  365. * If the MappedLUN entry is being disabled, the entry in
  366. * lun->lun_deve_list must be removed now before clearing the
  367. * struct se_dev_entry pointers below as logic in
  368. * core_alua_do_transition_tg_pt() depends on these being present.
  369. *
  370. * deve->se_lun_acl will be NULL for demo-mode created LUNs
  371. * that have not been explicitly converted to MappedLUNs ->
  372. * struct se_lun_acl, but we remove deve->lun_link from
  373. * lun->lun_deve_list. This also means that active UAs and
  374. * NodeACL context specific PR metadata for demo-mode
  375. * MappedLUN *deve will be released below..
  376. */
  377. spin_lock(&lun->lun_deve_lock);
  378. list_del(&orig->lun_link);
  379. spin_unlock(&lun->lun_deve_lock);
  380. /*
  381. * Disable struct se_dev_entry LUN ACL mapping
  382. */
  383. core_scsi3_ua_release_all(orig);
  384. hlist_del_rcu(&orig->link);
  385. clear_bit(DEF_PR_REG_ACTIVE, &orig->deve_flags);
  386. orig->lun_access_ro = false;
  387. orig->creation_time = 0;
  388. orig->attach_count--;
  389. /*
  390. * Before firing off RCU callback, wait for any in process SPEC_I_PT=1
  391. * or REGISTER_AND_MOVE PR operation to complete.
  392. */
  393. kref_put(&orig->pr_kref, target_pr_kref_release);
  394. wait_for_completion(&orig->pr_comp);
  395. rcu_assign_pointer(orig->se_lun, NULL);
  396. rcu_assign_pointer(orig->se_lun_acl, NULL);
  397. kfree_rcu(orig, rcu_head);
  398. core_scsi3_free_pr_reg_from_nacl(dev, nacl);
  399. target_luns_data_has_changed(nacl, NULL, false);
  400. }
  401. /* core_clear_lun_from_tpg():
  402. *
  403. *
  404. */
  405. void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
  406. {
  407. struct se_node_acl *nacl;
  408. struct se_dev_entry *deve;
  409. mutex_lock(&tpg->acl_node_mutex);
  410. list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
  411. mutex_lock(&nacl->lun_entry_mutex);
  412. hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
  413. struct se_lun *tmp_lun = rcu_dereference_check(deve->se_lun,
  414. lockdep_is_held(&nacl->lun_entry_mutex));
  415. if (lun != tmp_lun)
  416. continue;
  417. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  418. }
  419. mutex_unlock(&nacl->lun_entry_mutex);
  420. }
  421. mutex_unlock(&tpg->acl_node_mutex);
  422. }
  423. int core_alloc_rtpi(struct se_lun *lun, struct se_device *dev)
  424. {
  425. struct se_lun *tmp;
  426. spin_lock(&dev->se_port_lock);
  427. if (dev->export_count == 0x0000ffff) {
  428. pr_warn("Reached dev->dev_port_count =="
  429. " 0x0000ffff\n");
  430. spin_unlock(&dev->se_port_lock);
  431. return -ENOSPC;
  432. }
  433. again:
  434. /*
  435. * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
  436. * Here is the table from spc4r17 section 7.7.3.8.
  437. *
  438. * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
  439. *
  440. * Code Description
  441. * 0h Reserved
  442. * 1h Relative port 1, historically known as port A
  443. * 2h Relative port 2, historically known as port B
  444. * 3h to FFFFh Relative port 3 through 65 535
  445. */
  446. lun->lun_rtpi = dev->dev_rpti_counter++;
  447. if (!lun->lun_rtpi)
  448. goto again;
  449. list_for_each_entry(tmp, &dev->dev_sep_list, lun_dev_link) {
  450. /*
  451. * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
  452. * for 16-bit wrap..
  453. */
  454. if (lun->lun_rtpi == tmp->lun_rtpi)
  455. goto again;
  456. }
  457. spin_unlock(&dev->se_port_lock);
  458. return 0;
  459. }
  460. static void se_release_vpd_for_dev(struct se_device *dev)
  461. {
  462. struct t10_vpd *vpd, *vpd_tmp;
  463. spin_lock(&dev->t10_wwn.t10_vpd_lock);
  464. list_for_each_entry_safe(vpd, vpd_tmp,
  465. &dev->t10_wwn.t10_vpd_list, vpd_list) {
  466. list_del(&vpd->vpd_list);
  467. kfree(vpd);
  468. }
  469. spin_unlock(&dev->t10_wwn.t10_vpd_lock);
  470. }
  471. static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
  472. {
  473. u32 aligned_max_sectors;
  474. u32 alignment;
  475. /*
  476. * Limit max_sectors to a PAGE_SIZE aligned value for modern
  477. * transport_allocate_data_tasks() operation.
  478. */
  479. alignment = max(1ul, PAGE_SIZE / block_size);
  480. aligned_max_sectors = rounddown(max_sectors, alignment);
  481. if (max_sectors != aligned_max_sectors)
  482. pr_info("Rounding down aligned max_sectors from %u to %u\n",
  483. max_sectors, aligned_max_sectors);
  484. return aligned_max_sectors;
  485. }
  486. int core_dev_add_lun(
  487. struct se_portal_group *tpg,
  488. struct se_device *dev,
  489. struct se_lun *lun)
  490. {
  491. int rc;
  492. rc = core_tpg_add_lun(tpg, lun, false, dev);
  493. if (rc < 0)
  494. return rc;
  495. pr_debug("%s_TPG[%u]_LUN[%llu] - Activated %s Logical Unit from"
  496. " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
  497. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  498. tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
  499. /*
  500. * Update LUN maps for dynamically added initiators when
  501. * generate_node_acl is enabled.
  502. */
  503. if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
  504. struct se_node_acl *acl;
  505. mutex_lock(&tpg->acl_node_mutex);
  506. list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
  507. if (acl->dynamic_node_acl &&
  508. (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
  509. !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
  510. core_tpg_add_node_to_devs(acl, tpg, lun);
  511. }
  512. }
  513. mutex_unlock(&tpg->acl_node_mutex);
  514. }
  515. return 0;
  516. }
  517. /* core_dev_del_lun():
  518. *
  519. *
  520. */
  521. void core_dev_del_lun(
  522. struct se_portal_group *tpg,
  523. struct se_lun *lun)
  524. {
  525. pr_debug("%s_TPG[%u]_LUN[%llu] - Deactivating %s Logical Unit from"
  526. " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
  527. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  528. tpg->se_tpg_tfo->get_fabric_name());
  529. core_tpg_remove_lun(tpg, lun);
  530. }
  531. struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
  532. struct se_portal_group *tpg,
  533. struct se_node_acl *nacl,
  534. u64 mapped_lun,
  535. int *ret)
  536. {
  537. struct se_lun_acl *lacl;
  538. if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
  539. pr_err("%s InitiatorName exceeds maximum size.\n",
  540. tpg->se_tpg_tfo->get_fabric_name());
  541. *ret = -EOVERFLOW;
  542. return NULL;
  543. }
  544. lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
  545. if (!lacl) {
  546. pr_err("Unable to allocate memory for struct se_lun_acl.\n");
  547. *ret = -ENOMEM;
  548. return NULL;
  549. }
  550. lacl->mapped_lun = mapped_lun;
  551. lacl->se_lun_nacl = nacl;
  552. return lacl;
  553. }
  554. int core_dev_add_initiator_node_lun_acl(
  555. struct se_portal_group *tpg,
  556. struct se_lun_acl *lacl,
  557. struct se_lun *lun,
  558. bool lun_access_ro)
  559. {
  560. struct se_node_acl *nacl = lacl->se_lun_nacl;
  561. /*
  562. * rcu_dereference_raw protected by se_lun->lun_group symlink
  563. * reference to se_device->dev_group.
  564. */
  565. struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
  566. if (!nacl)
  567. return -EINVAL;
  568. if (lun->lun_access_ro)
  569. lun_access_ro = true;
  570. lacl->se_lun = lun;
  571. if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
  572. lun_access_ro, nacl, tpg) < 0)
  573. return -EINVAL;
  574. pr_debug("%s_TPG[%hu]_LUN[%llu->%llu] - Added %s ACL for "
  575. " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
  576. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, lacl->mapped_lun,
  577. lun_access_ro ? "RO" : "RW",
  578. nacl->initiatorname);
  579. /*
  580. * Check to see if there are any existing persistent reservation APTPL
  581. * pre-registrations that need to be enabled for this LUN ACL..
  582. */
  583. core_scsi3_check_aptpl_registration(dev, tpg, lun, nacl,
  584. lacl->mapped_lun);
  585. return 0;
  586. }
  587. int core_dev_del_initiator_node_lun_acl(
  588. struct se_lun *lun,
  589. struct se_lun_acl *lacl)
  590. {
  591. struct se_portal_group *tpg = lun->lun_tpg;
  592. struct se_node_acl *nacl;
  593. struct se_dev_entry *deve;
  594. nacl = lacl->se_lun_nacl;
  595. if (!nacl)
  596. return -EINVAL;
  597. mutex_lock(&nacl->lun_entry_mutex);
  598. deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
  599. if (deve)
  600. core_disable_device_list_for_node(lun, deve, nacl, tpg);
  601. mutex_unlock(&nacl->lun_entry_mutex);
  602. pr_debug("%s_TPG[%hu]_LUN[%llu] - Removed ACL for"
  603. " InitiatorNode: %s Mapped LUN: %llu\n",
  604. tpg->se_tpg_tfo->get_fabric_name(),
  605. tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
  606. nacl->initiatorname, lacl->mapped_lun);
  607. return 0;
  608. }
  609. void core_dev_free_initiator_node_lun_acl(
  610. struct se_portal_group *tpg,
  611. struct se_lun_acl *lacl)
  612. {
  613. pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
  614. " Mapped LUN: %llu\n", tpg->se_tpg_tfo->get_fabric_name(),
  615. tpg->se_tpg_tfo->tpg_get_tag(tpg),
  616. tpg->se_tpg_tfo->get_fabric_name(),
  617. lacl->se_lun_nacl->initiatorname, lacl->mapped_lun);
  618. kfree(lacl);
  619. }
  620. static void scsi_dump_inquiry(struct se_device *dev)
  621. {
  622. struct t10_wwn *wwn = &dev->t10_wwn;
  623. char buf[17];
  624. int i, device_type;
  625. /*
  626. * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
  627. */
  628. for (i = 0; i < 8; i++)
  629. if (wwn->vendor[i] >= 0x20)
  630. buf[i] = wwn->vendor[i];
  631. else
  632. buf[i] = ' ';
  633. buf[i] = '\0';
  634. pr_debug(" Vendor: %s\n", buf);
  635. for (i = 0; i < 16; i++)
  636. if (wwn->model[i] >= 0x20)
  637. buf[i] = wwn->model[i];
  638. else
  639. buf[i] = ' ';
  640. buf[i] = '\0';
  641. pr_debug(" Model: %s\n", buf);
  642. for (i = 0; i < 4; i++)
  643. if (wwn->revision[i] >= 0x20)
  644. buf[i] = wwn->revision[i];
  645. else
  646. buf[i] = ' ';
  647. buf[i] = '\0';
  648. pr_debug(" Revision: %s\n", buf);
  649. device_type = dev->transport->get_device_type(dev);
  650. pr_debug(" Type: %s ", scsi_device_type(device_type));
  651. }
  652. struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
  653. {
  654. struct se_device *dev;
  655. struct se_lun *xcopy_lun;
  656. dev = hba->backend->ops->alloc_device(hba, name);
  657. if (!dev)
  658. return NULL;
  659. dev->se_hba = hba;
  660. dev->transport = hba->backend->ops;
  661. dev->prot_length = sizeof(struct t10_pi_tuple);
  662. dev->hba_index = hba->hba_index;
  663. INIT_LIST_HEAD(&dev->dev_sep_list);
  664. INIT_LIST_HEAD(&dev->dev_tmr_list);
  665. INIT_LIST_HEAD(&dev->delayed_cmd_list);
  666. INIT_LIST_HEAD(&dev->state_list);
  667. INIT_LIST_HEAD(&dev->qf_cmd_list);
  668. spin_lock_init(&dev->execute_task_lock);
  669. spin_lock_init(&dev->delayed_cmd_lock);
  670. spin_lock_init(&dev->dev_reservation_lock);
  671. spin_lock_init(&dev->se_port_lock);
  672. spin_lock_init(&dev->se_tmr_lock);
  673. spin_lock_init(&dev->qf_cmd_lock);
  674. sema_init(&dev->caw_sem, 1);
  675. INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
  676. spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
  677. INIT_LIST_HEAD(&dev->t10_pr.registration_list);
  678. INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
  679. spin_lock_init(&dev->t10_pr.registration_lock);
  680. spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
  681. INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
  682. spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
  683. INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
  684. spin_lock_init(&dev->t10_alua.lba_map_lock);
  685. dev->t10_wwn.t10_dev = dev;
  686. dev->t10_alua.t10_dev = dev;
  687. dev->dev_attrib.da_dev = dev;
  688. dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
  689. dev->dev_attrib.emulate_dpo = 1;
  690. dev->dev_attrib.emulate_fua_write = 1;
  691. dev->dev_attrib.emulate_fua_read = 1;
  692. dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
  693. dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
  694. dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
  695. dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
  696. dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
  697. dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
  698. dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
  699. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
  700. dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
  701. dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
  702. dev->dev_attrib.is_nonrot = DA_IS_NONROT;
  703. dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
  704. dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
  705. dev->dev_attrib.max_unmap_block_desc_count =
  706. DA_MAX_UNMAP_BLOCK_DESC_COUNT;
  707. dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
  708. dev->dev_attrib.unmap_granularity_alignment =
  709. DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
  710. dev->dev_attrib.unmap_zeroes_data =
  711. DA_UNMAP_ZEROES_DATA_DEFAULT;
  712. dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
  713. xcopy_lun = &dev->xcopy_lun;
  714. rcu_assign_pointer(xcopy_lun->lun_se_dev, dev);
  715. init_completion(&xcopy_lun->lun_ref_comp);
  716. init_completion(&xcopy_lun->lun_shutdown_comp);
  717. INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
  718. INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
  719. mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
  720. xcopy_lun->lun_tpg = &xcopy_pt_tpg;
  721. return dev;
  722. }
  723. /*
  724. * Check if the underlying struct block_device request_queue supports
  725. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  726. * in ATA and we need to set TPE=1
  727. */
  728. bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib,
  729. struct request_queue *q)
  730. {
  731. int block_size = queue_logical_block_size(q);
  732. if (!blk_queue_discard(q))
  733. return false;
  734. attrib->max_unmap_lba_count =
  735. q->limits.max_discard_sectors >> (ilog2(block_size) - 9);
  736. /*
  737. * Currently hardcoded to 1 in Linux/SCSI code..
  738. */
  739. attrib->max_unmap_block_desc_count = 1;
  740. attrib->unmap_granularity = q->limits.discard_granularity / block_size;
  741. attrib->unmap_granularity_alignment = q->limits.discard_alignment /
  742. block_size;
  743. attrib->unmap_zeroes_data = (q->limits.max_write_zeroes_sectors);
  744. return true;
  745. }
  746. EXPORT_SYMBOL(target_configure_unmap_from_queue);
  747. /*
  748. * Convert from blocksize advertised to the initiator to the 512 byte
  749. * units unconditionally used by the Linux block layer.
  750. */
  751. sector_t target_to_linux_sector(struct se_device *dev, sector_t lb)
  752. {
  753. switch (dev->dev_attrib.block_size) {
  754. case 4096:
  755. return lb << 3;
  756. case 2048:
  757. return lb << 2;
  758. case 1024:
  759. return lb << 1;
  760. default:
  761. return lb;
  762. }
  763. }
  764. EXPORT_SYMBOL(target_to_linux_sector);
  765. /**
  766. * target_find_device - find a se_device by its dev_index
  767. * @id: dev_index
  768. * @do_depend: true if caller needs target_depend_item to be done
  769. *
  770. * If do_depend is true, the caller must do a target_undepend_item
  771. * when finished using the device.
  772. *
  773. * If do_depend is false, the caller must be called in a configfs
  774. * callback or during removal.
  775. */
  776. struct se_device *target_find_device(int id, bool do_depend)
  777. {
  778. struct se_device *dev;
  779. mutex_lock(&device_mutex);
  780. dev = idr_find(&devices_idr, id);
  781. if (dev && do_depend && target_depend_item(&dev->dev_group.cg_item))
  782. dev = NULL;
  783. mutex_unlock(&device_mutex);
  784. return dev;
  785. }
  786. EXPORT_SYMBOL(target_find_device);
  787. struct devices_idr_iter {
  788. int (*fn)(struct se_device *dev, void *data);
  789. void *data;
  790. };
  791. static int target_devices_idr_iter(int id, void *p, void *data)
  792. {
  793. struct devices_idr_iter *iter = data;
  794. struct se_device *dev = p;
  795. /*
  796. * We add the device early to the idr, so it can be used
  797. * by backend modules during configuration. We do not want
  798. * to allow other callers to access partially setup devices,
  799. * so we skip them here.
  800. */
  801. if (!(dev->dev_flags & DF_CONFIGURED))
  802. return 0;
  803. return iter->fn(dev, iter->data);
  804. }
  805. /**
  806. * target_for_each_device - iterate over configured devices
  807. * @fn: iterator function
  808. * @data: pointer to data that will be passed to fn
  809. *
  810. * fn must return 0 to continue looping over devices. non-zero will break
  811. * from the loop and return that value to the caller.
  812. */
  813. int target_for_each_device(int (*fn)(struct se_device *dev, void *data),
  814. void *data)
  815. {
  816. struct devices_idr_iter iter;
  817. int ret;
  818. iter.fn = fn;
  819. iter.data = data;
  820. mutex_lock(&device_mutex);
  821. ret = idr_for_each(&devices_idr, target_devices_idr_iter, &iter);
  822. mutex_unlock(&device_mutex);
  823. return ret;
  824. }
  825. int target_configure_device(struct se_device *dev)
  826. {
  827. struct se_hba *hba = dev->se_hba;
  828. int ret, id;
  829. if (dev->dev_flags & DF_CONFIGURED) {
  830. pr_err("se_dev->se_dev_ptr already set for storage"
  831. " object\n");
  832. return -EEXIST;
  833. }
  834. /*
  835. * Add early so modules like tcmu can use during its
  836. * configuration.
  837. */
  838. mutex_lock(&device_mutex);
  839. /*
  840. * Use cyclic to try and avoid collisions with devices
  841. * that were recently removed.
  842. */
  843. id = idr_alloc_cyclic(&devices_idr, dev, 0, INT_MAX, GFP_KERNEL);
  844. mutex_unlock(&device_mutex);
  845. if (id < 0) {
  846. ret = -ENOMEM;
  847. goto out;
  848. }
  849. dev->dev_index = id;
  850. ret = dev->transport->configure_device(dev);
  851. if (ret)
  852. goto out_free_index;
  853. /*
  854. * XXX: there is not much point to have two different values here..
  855. */
  856. dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
  857. dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
  858. /*
  859. * Align max_hw_sectors down to PAGE_SIZE I/O transfers
  860. */
  861. dev->dev_attrib.hw_max_sectors =
  862. se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
  863. dev->dev_attrib.hw_block_size);
  864. dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
  865. dev->creation_time = get_jiffies_64();
  866. ret = core_setup_alua(dev);
  867. if (ret)
  868. goto out_free_index;
  869. /*
  870. * Startup the struct se_device processing thread
  871. */
  872. dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
  873. dev->transport->name);
  874. if (!dev->tmr_wq) {
  875. pr_err("Unable to create tmr workqueue for %s\n",
  876. dev->transport->name);
  877. ret = -ENOMEM;
  878. goto out_free_alua;
  879. }
  880. /*
  881. * Setup work_queue for QUEUE_FULL
  882. */
  883. INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
  884. /*
  885. * Preload the initial INQUIRY const values if we are doing
  886. * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
  887. * passthrough because this is being provided by the backend LLD.
  888. */
  889. if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)) {
  890. strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
  891. strncpy(&dev->t10_wwn.model[0],
  892. dev->transport->inquiry_prod, 16);
  893. strncpy(&dev->t10_wwn.revision[0],
  894. dev->transport->inquiry_rev, 4);
  895. }
  896. scsi_dump_inquiry(dev);
  897. spin_lock(&hba->device_lock);
  898. hba->dev_count++;
  899. spin_unlock(&hba->device_lock);
  900. dev->dev_flags |= DF_CONFIGURED;
  901. return 0;
  902. out_free_alua:
  903. core_alua_free_lu_gp_mem(dev);
  904. out_free_index:
  905. mutex_lock(&device_mutex);
  906. idr_remove(&devices_idr, dev->dev_index);
  907. mutex_unlock(&device_mutex);
  908. out:
  909. se_release_vpd_for_dev(dev);
  910. return ret;
  911. }
  912. void target_free_device(struct se_device *dev)
  913. {
  914. struct se_hba *hba = dev->se_hba;
  915. WARN_ON(!list_empty(&dev->dev_sep_list));
  916. if (dev->dev_flags & DF_CONFIGURED) {
  917. destroy_workqueue(dev->tmr_wq);
  918. dev->transport->destroy_device(dev);
  919. mutex_lock(&device_mutex);
  920. idr_remove(&devices_idr, dev->dev_index);
  921. mutex_unlock(&device_mutex);
  922. spin_lock(&hba->device_lock);
  923. hba->dev_count--;
  924. spin_unlock(&hba->device_lock);
  925. }
  926. core_alua_free_lu_gp_mem(dev);
  927. core_alua_set_lba_map(dev, NULL, 0, 0);
  928. core_scsi3_free_all_registrations(dev);
  929. se_release_vpd_for_dev(dev);
  930. if (dev->transport->free_prot)
  931. dev->transport->free_prot(dev);
  932. dev->transport->free_device(dev);
  933. }
  934. int core_dev_setup_virtual_lun0(void)
  935. {
  936. struct se_hba *hba;
  937. struct se_device *dev;
  938. char buf[] = "rd_pages=8,rd_nullio=1";
  939. int ret;
  940. hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
  941. if (IS_ERR(hba))
  942. return PTR_ERR(hba);
  943. dev = target_alloc_device(hba, "virt_lun0");
  944. if (!dev) {
  945. ret = -ENOMEM;
  946. goto out_free_hba;
  947. }
  948. hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
  949. ret = target_configure_device(dev);
  950. if (ret)
  951. goto out_free_se_dev;
  952. lun0_hba = hba;
  953. g_lun0_dev = dev;
  954. return 0;
  955. out_free_se_dev:
  956. target_free_device(dev);
  957. out_free_hba:
  958. core_delete_hba(hba);
  959. return ret;
  960. }
  961. void core_dev_release_virtual_lun0(void)
  962. {
  963. struct se_hba *hba = lun0_hba;
  964. if (!hba)
  965. return;
  966. if (g_lun0_dev)
  967. target_free_device(g_lun0_dev);
  968. core_delete_hba(hba);
  969. }
  970. /*
  971. * Common CDB parsing for kernel and user passthrough.
  972. */
  973. sense_reason_t
  974. passthrough_parse_cdb(struct se_cmd *cmd,
  975. sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
  976. {
  977. unsigned char *cdb = cmd->t_task_cdb;
  978. struct se_device *dev = cmd->se_dev;
  979. unsigned int size;
  980. /*
  981. * Clear a lun set in the cdb if the initiator talking to use spoke
  982. * and old standards version, as we can't assume the underlying device
  983. * won't choke up on it.
  984. */
  985. switch (cdb[0]) {
  986. case READ_10: /* SBC - RDProtect */
  987. case READ_12: /* SBC - RDProtect */
  988. case READ_16: /* SBC - RDProtect */
  989. case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
  990. case VERIFY: /* SBC - VRProtect */
  991. case VERIFY_16: /* SBC - VRProtect */
  992. case WRITE_VERIFY: /* SBC - VRProtect */
  993. case WRITE_VERIFY_12: /* SBC - VRProtect */
  994. case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
  995. break;
  996. default:
  997. cdb[1] &= 0x1f; /* clear logical unit number */
  998. break;
  999. }
  1000. /*
  1001. * For REPORT LUNS we always need to emulate the response, for everything
  1002. * else, pass it up.
  1003. */
  1004. if (cdb[0] == REPORT_LUNS) {
  1005. cmd->execute_cmd = spc_emulate_report_luns;
  1006. return TCM_NO_SENSE;
  1007. }
  1008. /*
  1009. * For PERSISTENT RESERVE IN/OUT, RELEASE, and RESERVE we need to
  1010. * emulate the response, since tcmu does not have the information
  1011. * required to process these commands.
  1012. */
  1013. if (!(dev->transport->transport_flags &
  1014. TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
  1015. if (cdb[0] == PERSISTENT_RESERVE_IN) {
  1016. cmd->execute_cmd = target_scsi3_emulate_pr_in;
  1017. size = get_unaligned_be16(&cdb[7]);
  1018. return target_cmd_size_check(cmd, size);
  1019. }
  1020. if (cdb[0] == PERSISTENT_RESERVE_OUT) {
  1021. cmd->execute_cmd = target_scsi3_emulate_pr_out;
  1022. size = get_unaligned_be32(&cdb[5]);
  1023. return target_cmd_size_check(cmd, size);
  1024. }
  1025. if (cdb[0] == RELEASE || cdb[0] == RELEASE_10) {
  1026. cmd->execute_cmd = target_scsi2_reservation_release;
  1027. if (cdb[0] == RELEASE_10)
  1028. size = get_unaligned_be16(&cdb[7]);
  1029. else
  1030. size = cmd->data_length;
  1031. return target_cmd_size_check(cmd, size);
  1032. }
  1033. if (cdb[0] == RESERVE || cdb[0] == RESERVE_10) {
  1034. cmd->execute_cmd = target_scsi2_reservation_reserve;
  1035. if (cdb[0] == RESERVE_10)
  1036. size = get_unaligned_be16(&cdb[7]);
  1037. else
  1038. size = cmd->data_length;
  1039. return target_cmd_size_check(cmd, size);
  1040. }
  1041. }
  1042. /* Set DATA_CDB flag for ops that should have it */
  1043. switch (cdb[0]) {
  1044. case READ_6:
  1045. case READ_10:
  1046. case READ_12:
  1047. case READ_16:
  1048. case WRITE_6:
  1049. case WRITE_10:
  1050. case WRITE_12:
  1051. case WRITE_16:
  1052. case WRITE_VERIFY:
  1053. case WRITE_VERIFY_12:
  1054. case WRITE_VERIFY_16:
  1055. case COMPARE_AND_WRITE:
  1056. case XDWRITEREAD_10:
  1057. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  1058. break;
  1059. case VARIABLE_LENGTH_CMD:
  1060. switch (get_unaligned_be16(&cdb[8])) {
  1061. case READ_32:
  1062. case WRITE_32:
  1063. case WRITE_VERIFY_32:
  1064. case XDWRITEREAD_32:
  1065. cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
  1066. break;
  1067. }
  1068. }
  1069. cmd->execute_cmd = exec_cmd;
  1070. return TCM_NO_SENSE;
  1071. }
  1072. EXPORT_SYMBOL(passthrough_parse_cdb);