target_core_ua.c 9.0 KB

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