target_core_ua.c 9.1 KB

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