target_core_ua.c 9.1 KB

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