target_core_ua.c 8.8 KB

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