target_core_ua.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * Filename: target_core_ua.c
  4. *
  5. * This file contains logic for SPC-3 Unit Attention emulation
  6. *
  7. * (c) Copyright 2009-2013 Datera, Inc.
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. ******************************************************************************/
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <scsi/scsi_proto.h>
  15. #include <target/target_core_base.h>
  16. #include <target/target_core_fabric.h>
  17. #include "target_core_internal.h"
  18. #include "target_core_alua.h"
  19. #include "target_core_pr.h"
  20. #include "target_core_ua.h"
  21. sense_reason_t
  22. target_scsi3_ua_check(struct se_cmd *cmd)
  23. {
  24. struct se_dev_entry *deve;
  25. struct se_session *sess = cmd->se_sess;
  26. struct se_node_acl *nacl;
  27. if (!sess)
  28. return 0;
  29. nacl = sess->se_node_acl;
  30. if (!nacl)
  31. return 0;
  32. rcu_read_lock();
  33. deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
  34. if (!deve) {
  35. rcu_read_unlock();
  36. return 0;
  37. }
  38. if (list_empty_careful(&deve->ua_list)) {
  39. rcu_read_unlock();
  40. return 0;
  41. }
  42. rcu_read_unlock();
  43. /*
  44. * From sam4r14, section 5.14 Unit attention condition:
  45. *
  46. * a) if an INQUIRY command enters the enabled command state, the
  47. * device server shall process the INQUIRY command and shall neither
  48. * report nor clear any unit attention condition;
  49. * b) if a REPORT LUNS command enters the enabled command state, the
  50. * device server shall process the REPORT LUNS command and shall not
  51. * report any unit attention condition;
  52. * e) if a REQUEST SENSE command enters the enabled command state while
  53. * a unit attention condition exists for the SCSI initiator port
  54. * associated with the I_T nexus on which the REQUEST SENSE command
  55. * was received, then the device server shall process the command
  56. * and either:
  57. */
  58. switch (cmd->t_task_cdb[0]) {
  59. case INQUIRY:
  60. case REPORT_LUNS:
  61. case REQUEST_SENSE:
  62. return 0;
  63. default:
  64. return TCM_CHECK_CONDITION_UNIT_ATTENTION;
  65. }
  66. }
  67. int core_scsi3_ua_allocate(
  68. struct se_dev_entry *deve,
  69. u8 asc,
  70. u8 ascq)
  71. {
  72. struct se_ua *ua, *ua_p, *ua_tmp;
  73. ua = kmem_cache_zalloc(se_ua_cache, GFP_ATOMIC);
  74. if (!ua) {
  75. pr_err("Unable to allocate struct se_ua\n");
  76. return -ENOMEM;
  77. }
  78. INIT_LIST_HEAD(&ua->ua_nacl_list);
  79. ua->ua_asc = asc;
  80. ua->ua_ascq = ascq;
  81. spin_lock(&deve->ua_lock);
  82. list_for_each_entry_safe(ua_p, ua_tmp, &deve->ua_list, ua_nacl_list) {
  83. /*
  84. * Do not report the same UNIT ATTENTION twice..
  85. */
  86. if ((ua_p->ua_asc == asc) && (ua_p->ua_ascq == ascq)) {
  87. spin_unlock(&deve->ua_lock);
  88. kmem_cache_free(se_ua_cache, ua);
  89. return 0;
  90. }
  91. /*
  92. * Attach the highest priority Unit Attention to
  93. * the head of the list following sam4r14,
  94. * Section 5.14 Unit Attention Condition:
  95. *
  96. * POWER ON, RESET, OR BUS DEVICE RESET OCCURRED highest
  97. * POWER ON OCCURRED or
  98. * DEVICE INTERNAL RESET
  99. * SCSI BUS RESET OCCURRED or
  100. * MICROCODE HAS BEEN CHANGED or
  101. * protocol specific
  102. * BUS DEVICE RESET FUNCTION OCCURRED
  103. * I_T NEXUS LOSS OCCURRED
  104. * COMMANDS CLEARED BY POWER LOSS NOTIFICATION
  105. * all others Lowest
  106. *
  107. * Each of the ASCQ codes listed above are defined in
  108. * the 29h ASC family, see spc4r17 Table D.1
  109. */
  110. if (ua_p->ua_asc == 0x29) {
  111. if ((asc == 0x29) && (ascq > ua_p->ua_ascq))
  112. list_add(&ua->ua_nacl_list,
  113. &deve->ua_list);
  114. else
  115. list_add_tail(&ua->ua_nacl_list,
  116. &deve->ua_list);
  117. } else if (ua_p->ua_asc == 0x2a) {
  118. /*
  119. * Incoming Family 29h ASCQ codes will override
  120. * Family 2AHh ASCQ codes for Unit Attention condition.
  121. */
  122. if ((asc == 0x29) || (ascq > ua_p->ua_asc))
  123. list_add(&ua->ua_nacl_list,
  124. &deve->ua_list);
  125. else
  126. list_add_tail(&ua->ua_nacl_list,
  127. &deve->ua_list);
  128. } else
  129. list_add_tail(&ua->ua_nacl_list,
  130. &deve->ua_list);
  131. spin_unlock(&deve->ua_lock);
  132. return 0;
  133. }
  134. list_add_tail(&ua->ua_nacl_list, &deve->ua_list);
  135. spin_unlock(&deve->ua_lock);
  136. pr_debug("Allocated UNIT ATTENTION, mapped LUN: %llu, ASC:"
  137. " 0x%02x, ASCQ: 0x%02x\n", deve->mapped_lun,
  138. asc, ascq);
  139. return 0;
  140. }
  141. void target_ua_allocate_lun(struct se_node_acl *nacl,
  142. u32 unpacked_lun, u8 asc, u8 ascq)
  143. {
  144. struct se_dev_entry *deve;
  145. if (!nacl)
  146. return;
  147. rcu_read_lock();
  148. deve = target_nacl_find_deve(nacl, unpacked_lun);
  149. if (!deve) {
  150. rcu_read_unlock();
  151. return;
  152. }
  153. core_scsi3_ua_allocate(deve, asc, ascq);
  154. rcu_read_unlock();
  155. }
  156. void core_scsi3_ua_release_all(
  157. struct se_dev_entry *deve)
  158. {
  159. struct se_ua *ua, *ua_p;
  160. spin_lock(&deve->ua_lock);
  161. list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) {
  162. list_del(&ua->ua_nacl_list);
  163. kmem_cache_free(se_ua_cache, ua);
  164. }
  165. spin_unlock(&deve->ua_lock);
  166. }
  167. /*
  168. * Dequeue a unit attention from the unit attention list. This function
  169. * returns true if the dequeuing succeeded and if *@key, *@asc and *@ascq have
  170. * been set.
  171. */
  172. bool core_scsi3_ua_for_check_condition(struct se_cmd *cmd, u8 *key, u8 *asc,
  173. u8 *ascq)
  174. {
  175. struct se_device *dev = cmd->se_dev;
  176. struct se_dev_entry *deve;
  177. struct se_session *sess = cmd->se_sess;
  178. struct se_node_acl *nacl;
  179. struct se_ua *ua = NULL, *ua_p;
  180. int head = 1;
  181. bool dev_ua_intlck_clear = (dev->dev_attrib.emulate_ua_intlck_ctrl
  182. == TARGET_UA_INTLCK_CTRL_CLEAR);
  183. if (WARN_ON_ONCE(!sess))
  184. return false;
  185. nacl = sess->se_node_acl;
  186. if (WARN_ON_ONCE(!nacl))
  187. return false;
  188. rcu_read_lock();
  189. deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
  190. if (!deve) {
  191. rcu_read_unlock();
  192. *key = ILLEGAL_REQUEST;
  193. *asc = 0x25; /* LOGICAL UNIT NOT SUPPORTED */
  194. *ascq = 0;
  195. return true;
  196. }
  197. *key = UNIT_ATTENTION;
  198. /*
  199. * The highest priority Unit Attentions are placed at the head of the
  200. * struct se_dev_entry->ua_list, and will be returned in CHECK_CONDITION +
  201. * sense data for the received CDB.
  202. */
  203. spin_lock(&deve->ua_lock);
  204. list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) {
  205. /*
  206. * For ua_intlck_ctrl code not equal to 00b, only report the
  207. * highest priority UNIT_ATTENTION and ASC/ASCQ without
  208. * clearing it.
  209. */
  210. if (!dev_ua_intlck_clear) {
  211. *asc = ua->ua_asc;
  212. *ascq = ua->ua_ascq;
  213. break;
  214. }
  215. /*
  216. * Otherwise for the default 00b, release the UNIT ATTENTION
  217. * condition. Return the ASC/ASCQ of the highest priority UA
  218. * (head of the list) in the outgoing CHECK_CONDITION + sense.
  219. */
  220. if (head) {
  221. *asc = ua->ua_asc;
  222. *ascq = ua->ua_ascq;
  223. head = 0;
  224. }
  225. list_del(&ua->ua_nacl_list);
  226. kmem_cache_free(se_ua_cache, ua);
  227. }
  228. spin_unlock(&deve->ua_lock);
  229. rcu_read_unlock();
  230. pr_debug("[%s]: %s UNIT ATTENTION condition with"
  231. " INTLCK_CTRL: %d, mapped LUN: %llu, got CDB: 0x%02x"
  232. " reported ASC: 0x%02x, ASCQ: 0x%02x\n",
  233. nacl->se_tpg->se_tpg_tfo->fabric_name,
  234. dev_ua_intlck_clear ? "Releasing" : "Reporting",
  235. dev->dev_attrib.emulate_ua_intlck_ctrl,
  236. cmd->orig_fe_lun, cmd->t_task_cdb[0], *asc, *ascq);
  237. return head == 0;
  238. }
  239. int core_scsi3_ua_clear_for_request_sense(
  240. struct se_cmd *cmd,
  241. u8 *asc,
  242. u8 *ascq)
  243. {
  244. struct se_dev_entry *deve;
  245. struct se_session *sess = cmd->se_sess;
  246. struct se_node_acl *nacl;
  247. struct se_ua *ua = NULL, *ua_p;
  248. int head = 1;
  249. if (!sess)
  250. return -EINVAL;
  251. nacl = sess->se_node_acl;
  252. if (!nacl)
  253. return -EINVAL;
  254. rcu_read_lock();
  255. deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
  256. if (!deve) {
  257. rcu_read_unlock();
  258. return -EINVAL;
  259. }
  260. if (list_empty_careful(&deve->ua_list)) {
  261. rcu_read_unlock();
  262. return -EPERM;
  263. }
  264. /*
  265. * The highest priority Unit Attentions are placed at the head of the
  266. * struct se_dev_entry->ua_list. The First (and hence highest priority)
  267. * ASC/ASCQ will be returned in REQUEST_SENSE payload data for the
  268. * matching struct se_lun.
  269. *
  270. * Once the returning ASC/ASCQ values are set, we go ahead and
  271. * release all of the Unit Attention conditions for the associated
  272. * struct se_lun.
  273. */
  274. spin_lock(&deve->ua_lock);
  275. list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) {
  276. if (head) {
  277. *asc = ua->ua_asc;
  278. *ascq = ua->ua_ascq;
  279. head = 0;
  280. }
  281. list_del(&ua->ua_nacl_list);
  282. kmem_cache_free(se_ua_cache, ua);
  283. }
  284. spin_unlock(&deve->ua_lock);
  285. rcu_read_unlock();
  286. pr_debug("[%s]: Released UNIT ATTENTION condition, mapped"
  287. " LUN: %llu, got REQUEST_SENSE reported ASC: 0x%02x,"
  288. " ASCQ: 0x%02x\n", nacl->se_tpg->se_tpg_tfo->fabric_name,
  289. cmd->orig_fe_lun, *asc, *ascq);
  290. return (head) ? -EPERM : 0;
  291. }