target_core_ua.c 8.9 KB

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