target_core_ua.c 9.0 KB

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