target_core_ua.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*******************************************************************************
  2. * Filename: target_core_ua.c
  3. *
  4. * This file contains logic for SPC-3 Unit Attention emulation
  5. *
  6. * (c) Copyright 2009-2012 RisingTide Systems LLC.
  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_dev_list);
  93. INIT_LIST_HEAD(&ua->ua_nacl_list);
  94. ua->ua_nacl = nacl;
  95. ua->ua_asc = asc;
  96. ua->ua_ascq = ascq;
  97. spin_lock_irq(&nacl->device_list_lock);
  98. deve = nacl->device_list[unpacked_lun];
  99. spin_lock(&deve->ua_lock);
  100. list_for_each_entry_safe(ua_p, ua_tmp, &deve->ua_list, ua_nacl_list) {
  101. /*
  102. * Do not report the same UNIT ATTENTION twice..
  103. */
  104. if ((ua_p->ua_asc == asc) && (ua_p->ua_ascq == ascq)) {
  105. spin_unlock(&deve->ua_lock);
  106. spin_unlock_irq(&nacl->device_list_lock);
  107. kmem_cache_free(se_ua_cache, ua);
  108. return 0;
  109. }
  110. /*
  111. * Attach the highest priority Unit Attention to
  112. * the head of the list following sam4r14,
  113. * Section 5.14 Unit Attention Condition:
  114. *
  115. * POWER ON, RESET, OR BUS DEVICE RESET OCCURRED highest
  116. * POWER ON OCCURRED or
  117. * DEVICE INTERNAL RESET
  118. * SCSI BUS RESET OCCURRED or
  119. * MICROCODE HAS BEEN CHANGED or
  120. * protocol specific
  121. * BUS DEVICE RESET FUNCTION OCCURRED
  122. * I_T NEXUS LOSS OCCURRED
  123. * COMMANDS CLEARED BY POWER LOSS NOTIFICATION
  124. * all others Lowest
  125. *
  126. * Each of the ASCQ codes listed above are defined in
  127. * the 29h ASC family, see spc4r17 Table D.1
  128. */
  129. if (ua_p->ua_asc == 0x29) {
  130. if ((asc == 0x29) && (ascq > ua_p->ua_ascq))
  131. list_add(&ua->ua_nacl_list,
  132. &deve->ua_list);
  133. else
  134. list_add_tail(&ua->ua_nacl_list,
  135. &deve->ua_list);
  136. } else if (ua_p->ua_asc == 0x2a) {
  137. /*
  138. * Incoming Family 29h ASCQ codes will override
  139. * Family 2AHh ASCQ codes for Unit Attention condition.
  140. */
  141. if ((asc == 0x29) || (ascq > ua_p->ua_asc))
  142. list_add(&ua->ua_nacl_list,
  143. &deve->ua_list);
  144. else
  145. list_add_tail(&ua->ua_nacl_list,
  146. &deve->ua_list);
  147. } else
  148. list_add_tail(&ua->ua_nacl_list,
  149. &deve->ua_list);
  150. spin_unlock(&deve->ua_lock);
  151. spin_unlock_irq(&nacl->device_list_lock);
  152. atomic_inc(&deve->ua_count);
  153. smp_mb__after_atomic_inc();
  154. return 0;
  155. }
  156. list_add_tail(&ua->ua_nacl_list, &deve->ua_list);
  157. spin_unlock(&deve->ua_lock);
  158. spin_unlock_irq(&nacl->device_list_lock);
  159. pr_debug("[%s]: Allocated UNIT ATTENTION, mapped LUN: %u, ASC:"
  160. " 0x%02x, ASCQ: 0x%02x\n",
  161. nacl->se_tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
  162. asc, ascq);
  163. atomic_inc(&deve->ua_count);
  164. smp_mb__after_atomic_inc();
  165. return 0;
  166. }
  167. void core_scsi3_ua_release_all(
  168. struct se_dev_entry *deve)
  169. {
  170. struct se_ua *ua, *ua_p;
  171. spin_lock(&deve->ua_lock);
  172. list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) {
  173. list_del(&ua->ua_nacl_list);
  174. kmem_cache_free(se_ua_cache, ua);
  175. atomic_dec(&deve->ua_count);
  176. smp_mb__after_atomic_dec();
  177. }
  178. spin_unlock(&deve->ua_lock);
  179. }
  180. void core_scsi3_ua_for_check_condition(
  181. struct se_cmd *cmd,
  182. u8 *asc,
  183. u8 *ascq)
  184. {
  185. struct se_device *dev = cmd->se_dev;
  186. struct se_dev_entry *deve;
  187. struct se_session *sess = cmd->se_sess;
  188. struct se_node_acl *nacl;
  189. struct se_ua *ua = NULL, *ua_p;
  190. int head = 1;
  191. if (!sess)
  192. return;
  193. nacl = sess->se_node_acl;
  194. if (!nacl)
  195. return;
  196. spin_lock_irq(&nacl->device_list_lock);
  197. deve = nacl->device_list[cmd->orig_fe_lun];
  198. if (!atomic_read(&deve->ua_count)) {
  199. spin_unlock_irq(&nacl->device_list_lock);
  200. return;
  201. }
  202. /*
  203. * The highest priority Unit Attentions are placed at the head of the
  204. * struct se_dev_entry->ua_list, and will be returned in CHECK_CONDITION +
  205. * sense data for the received CDB.
  206. */
  207. spin_lock(&deve->ua_lock);
  208. list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) {
  209. /*
  210. * For ua_intlck_ctrl code not equal to 00b, only report the
  211. * highest priority UNIT_ATTENTION and ASC/ASCQ without
  212. * clearing it.
  213. */
  214. if (dev->dev_attrib.emulate_ua_intlck_ctrl != 0) {
  215. *asc = ua->ua_asc;
  216. *ascq = ua->ua_ascq;
  217. break;
  218. }
  219. /*
  220. * Otherwise for the default 00b, release the UNIT ATTENTION
  221. * condition. Return the ASC/ASCQ of the highest priority UA
  222. * (head of the list) in the outgoing CHECK_CONDITION + sense.
  223. */
  224. if (head) {
  225. *asc = ua->ua_asc;
  226. *ascq = ua->ua_ascq;
  227. head = 0;
  228. }
  229. list_del(&ua->ua_nacl_list);
  230. kmem_cache_free(se_ua_cache, ua);
  231. atomic_dec(&deve->ua_count);
  232. smp_mb__after_atomic_dec();
  233. }
  234. spin_unlock(&deve->ua_lock);
  235. spin_unlock_irq(&nacl->device_list_lock);
  236. pr_debug("[%s]: %s UNIT ATTENTION condition with"
  237. " INTLCK_CTRL: %d, mapped LUN: %u, got CDB: 0x%02x"
  238. " reported ASC: 0x%02x, ASCQ: 0x%02x\n",
  239. nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  240. (dev->dev_attrib.emulate_ua_intlck_ctrl != 0) ? "Reporting" :
  241. "Releasing", dev->dev_attrib.emulate_ua_intlck_ctrl,
  242. cmd->orig_fe_lun, cmd->t_task_cdb[0], *asc, *ascq);
  243. }
  244. int core_scsi3_ua_clear_for_request_sense(
  245. struct se_cmd *cmd,
  246. u8 *asc,
  247. u8 *ascq)
  248. {
  249. struct se_dev_entry *deve;
  250. struct se_session *sess = cmd->se_sess;
  251. struct se_node_acl *nacl;
  252. struct se_ua *ua = NULL, *ua_p;
  253. int head = 1;
  254. if (!sess)
  255. return -EINVAL;
  256. nacl = sess->se_node_acl;
  257. if (!nacl)
  258. return -EINVAL;
  259. spin_lock_irq(&nacl->device_list_lock);
  260. deve = nacl->device_list[cmd->orig_fe_lun];
  261. if (!atomic_read(&deve->ua_count)) {
  262. spin_unlock_irq(&nacl->device_list_lock);
  263. return -EPERM;
  264. }
  265. /*
  266. * The highest priority Unit Attentions are placed at the head of the
  267. * struct se_dev_entry->ua_list. The First (and hence highest priority)
  268. * ASC/ASCQ will be returned in REQUEST_SENSE payload data for the
  269. * matching struct se_lun.
  270. *
  271. * Once the returning ASC/ASCQ values are set, we go ahead and
  272. * release all of the Unit Attention conditions for the associated
  273. * struct se_lun.
  274. */
  275. spin_lock(&deve->ua_lock);
  276. list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) {
  277. if (head) {
  278. *asc = ua->ua_asc;
  279. *ascq = ua->ua_ascq;
  280. head = 0;
  281. }
  282. list_del(&ua->ua_nacl_list);
  283. kmem_cache_free(se_ua_cache, ua);
  284. atomic_dec(&deve->ua_count);
  285. smp_mb__after_atomic_dec();
  286. }
  287. spin_unlock(&deve->ua_lock);
  288. spin_unlock_irq(&nacl->device_list_lock);
  289. pr_debug("[%s]: Released UNIT ATTENTION condition, mapped"
  290. " LUN: %u, got REQUEST_SENSE reported ASC: 0x%02x,"
  291. " ASCQ: 0x%02x\n", nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  292. cmd->orig_fe_lun, *asc, *ascq);
  293. return (head) ? -EPERM : 0;
  294. }