target_core_tmr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*******************************************************************************
  2. * Filename: target_core_tmr.c
  3. *
  4. * This file contains SPC-3 task management infrastructure
  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 <linux/list.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <target/target_core_base.h>
  33. #include <target/target_core_device.h>
  34. #include <target/target_core_tmr.h>
  35. #include <target/target_core_transport.h>
  36. #include <target/target_core_fabric_ops.h>
  37. #include <target/target_core_configfs.h>
  38. #include "target_core_alua.h"
  39. #include "target_core_pr.h"
  40. #define DEBUG_LUN_RESET
  41. #ifdef DEBUG_LUN_RESET
  42. #define DEBUG_LR(x...) printk(KERN_INFO x)
  43. #else
  44. #define DEBUG_LR(x...)
  45. #endif
  46. struct se_tmr_req *core_tmr_alloc_req(
  47. struct se_cmd *se_cmd,
  48. void *fabric_tmr_ptr,
  49. u8 function)
  50. {
  51. struct se_tmr_req *tmr;
  52. tmr = kmem_cache_zalloc(se_tmr_req_cache, (in_interrupt()) ?
  53. GFP_ATOMIC : GFP_KERNEL);
  54. if (!(tmr)) {
  55. printk(KERN_ERR "Unable to allocate struct se_tmr_req\n");
  56. return ERR_PTR(-ENOMEM);
  57. }
  58. tmr->task_cmd = se_cmd;
  59. tmr->fabric_tmr_ptr = fabric_tmr_ptr;
  60. tmr->function = function;
  61. INIT_LIST_HEAD(&tmr->tmr_list);
  62. return tmr;
  63. }
  64. EXPORT_SYMBOL(core_tmr_alloc_req);
  65. void core_tmr_release_req(
  66. struct se_tmr_req *tmr)
  67. {
  68. struct se_device *dev = tmr->tmr_dev;
  69. if (!dev) {
  70. kmem_cache_free(se_tmr_req_cache, tmr);
  71. return;
  72. }
  73. spin_lock(&dev->se_tmr_lock);
  74. list_del(&tmr->tmr_list);
  75. spin_unlock(&dev->se_tmr_lock);
  76. kmem_cache_free(se_tmr_req_cache, tmr);
  77. }
  78. static void core_tmr_handle_tas_abort(
  79. struct se_node_acl *tmr_nacl,
  80. struct se_cmd *cmd,
  81. int tas,
  82. int fe_count)
  83. {
  84. if (!(fe_count)) {
  85. transport_cmd_finish_abort(cmd, 1);
  86. return;
  87. }
  88. /*
  89. * TASK ABORTED status (TAS) bit support
  90. */
  91. if (((tmr_nacl != NULL) &&
  92. (tmr_nacl == cmd->se_sess->se_node_acl)) || tas)
  93. transport_send_task_abort(cmd);
  94. transport_cmd_finish_abort(cmd, 0);
  95. }
  96. int core_tmr_lun_reset(
  97. struct se_device *dev,
  98. struct se_tmr_req *tmr,
  99. struct list_head *preempt_and_abort_list,
  100. struct se_cmd *prout_cmd)
  101. {
  102. struct se_cmd *cmd, *tcmd;
  103. struct se_node_acl *tmr_nacl = NULL;
  104. struct se_portal_group *tmr_tpg = NULL;
  105. struct se_queue_obj *qobj = &dev->dev_queue_obj;
  106. struct se_tmr_req *tmr_p, *tmr_pp;
  107. struct se_task *task, *task_tmp;
  108. unsigned long flags;
  109. int fe_count, tas;
  110. /*
  111. * TASK_ABORTED status bit, this is configurable via ConfigFS
  112. * struct se_device attributes. spc4r17 section 7.4.6 Control mode page
  113. *
  114. * A task aborted status (TAS) bit set to zero specifies that aborted
  115. * tasks shall be terminated by the device server without any response
  116. * to the application client. A TAS bit set to one specifies that tasks
  117. * aborted by the actions of an I_T nexus other than the I_T nexus on
  118. * which the command was received shall be completed with TASK ABORTED
  119. * status (see SAM-4).
  120. */
  121. tas = dev->se_sub_dev->se_dev_attrib.emulate_tas;
  122. /*
  123. * Determine if this se_tmr is coming from a $FABRIC_MOD
  124. * or struct se_device passthrough..
  125. */
  126. if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
  127. tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
  128. tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
  129. if (tmr_nacl && tmr_tpg) {
  130. DEBUG_LR("LUN_RESET: TMR caller fabric: %s"
  131. " initiator port %s\n",
  132. tmr_tpg->se_tpg_tfo->get_fabric_name(),
  133. tmr_nacl->initiatorname);
  134. }
  135. }
  136. DEBUG_LR("LUN_RESET: %s starting for [%s], tas: %d\n",
  137. (preempt_and_abort_list) ? "Preempt" : "TMR",
  138. dev->transport->name, tas);
  139. /*
  140. * Release all pending and outgoing TMRs aside from the received
  141. * LUN_RESET tmr..
  142. */
  143. spin_lock(&dev->se_tmr_lock);
  144. list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
  145. /*
  146. * Allow the received TMR to return with FUNCTION_COMPLETE.
  147. */
  148. if (tmr && (tmr_p == tmr))
  149. continue;
  150. cmd = tmr_p->task_cmd;
  151. if (!(cmd)) {
  152. printk(KERN_ERR "Unable to locate struct se_cmd for TMR\n");
  153. continue;
  154. }
  155. /*
  156. * If this function was called with a valid pr_res_key
  157. * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
  158. * skip non regisration key matching TMRs.
  159. */
  160. if ((preempt_and_abort_list != NULL) &&
  161. (core_scsi3_check_cdb_abort_and_preempt(
  162. preempt_and_abort_list, cmd) != 0))
  163. continue;
  164. spin_unlock(&dev->se_tmr_lock);
  165. spin_lock_irqsave(&cmd->t_state_lock, flags);
  166. if (!(atomic_read(&cmd->t_transport_active))) {
  167. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  168. spin_lock(&dev->se_tmr_lock);
  169. continue;
  170. }
  171. if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
  172. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  173. spin_lock(&dev->se_tmr_lock);
  174. continue;
  175. }
  176. DEBUG_LR("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
  177. " Response: 0x%02x, t_state: %d\n",
  178. (preempt_and_abort_list) ? "Preempt" : "", tmr_p,
  179. tmr_p->function, tmr_p->response, cmd->t_state);
  180. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  181. transport_cmd_finish_abort_tmr(cmd);
  182. spin_lock(&dev->se_tmr_lock);
  183. }
  184. spin_unlock(&dev->se_tmr_lock);
  185. /*
  186. * Complete outstanding struct se_task CDBs with TASK_ABORTED SAM status.
  187. * This is following sam4r17, section 5.6 Aborting commands, Table 38
  188. * for TMR LUN_RESET:
  189. *
  190. * a) "Yes" indicates that each command that is aborted on an I_T nexus
  191. * other than the one that caused the SCSI device condition is
  192. * completed with TASK ABORTED status, if the TAS bit is set to one in
  193. * the Control mode page (see SPC-4). "No" indicates that no status is
  194. * returned for aborted commands.
  195. *
  196. * d) If the logical unit reset is caused by a particular I_T nexus
  197. * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
  198. * (TASK_ABORTED status) applies.
  199. *
  200. * Otherwise (e.g., if triggered by a hard reset), "no"
  201. * (no TASK_ABORTED SAM status) applies.
  202. *
  203. * Note that this seems to be independent of TAS (Task Aborted Status)
  204. * in the Control Mode Page.
  205. */
  206. spin_lock_irqsave(&dev->execute_task_lock, flags);
  207. list_for_each_entry_safe(task, task_tmp, &dev->state_task_list,
  208. t_state_list) {
  209. if (!task->task_se_cmd) {
  210. printk(KERN_ERR "task->task_se_cmd is NULL!\n");
  211. continue;
  212. }
  213. cmd = task->task_se_cmd;
  214. /*
  215. * For PREEMPT_AND_ABORT usage, only process commands
  216. * with a matching reservation key.
  217. */
  218. if ((preempt_and_abort_list != NULL) &&
  219. (core_scsi3_check_cdb_abort_and_preempt(
  220. preempt_and_abort_list, cmd) != 0))
  221. continue;
  222. /*
  223. * Not aborting PROUT PREEMPT_AND_ABORT CDB..
  224. */
  225. if (prout_cmd == cmd)
  226. continue;
  227. list_del(&task->t_state_list);
  228. atomic_set(&task->task_state_active, 0);
  229. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  230. spin_lock_irqsave(&cmd->t_state_lock, flags);
  231. DEBUG_LR("LUN_RESET: %s cmd: %p task: %p"
  232. " ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state/"
  233. "def_t_state: %d/%d cdb: 0x%02x\n",
  234. (preempt_and_abort_list) ? "Preempt" : "", cmd, task,
  235. cmd->se_tfo->get_task_tag(cmd), 0,
  236. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
  237. cmd->deferred_t_state, cmd->t_task_cdb[0]);
  238. DEBUG_LR("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
  239. " t_task_cdbs: %d t_task_cdbs_left: %d"
  240. " t_task_cdbs_sent: %d -- t_transport_active: %d"
  241. " t_transport_stop: %d t_transport_sent: %d\n",
  242. cmd->se_tfo->get_task_tag(cmd), cmd->pr_res_key,
  243. cmd->t_task_list_num,
  244. atomic_read(&cmd->t_task_cdbs_left),
  245. atomic_read(&cmd->t_task_cdbs_sent),
  246. atomic_read(&cmd->t_transport_active),
  247. atomic_read(&cmd->t_transport_stop),
  248. atomic_read(&cmd->t_transport_sent));
  249. if (atomic_read(&task->task_active)) {
  250. atomic_set(&task->task_stop, 1);
  251. spin_unlock_irqrestore(
  252. &cmd->t_state_lock, flags);
  253. DEBUG_LR("LUN_RESET: Waiting for task: %p to shutdown"
  254. " for dev: %p\n", task, dev);
  255. wait_for_completion(&task->task_stop_comp);
  256. DEBUG_LR("LUN_RESET Completed task: %p shutdown for"
  257. " dev: %p\n", task, dev);
  258. spin_lock_irqsave(&cmd->t_state_lock, flags);
  259. atomic_dec(&cmd->t_task_cdbs_left);
  260. atomic_set(&task->task_active, 0);
  261. atomic_set(&task->task_stop, 0);
  262. } else {
  263. if (atomic_read(&task->task_execute_queue) != 0)
  264. transport_remove_task_from_execute_queue(task, dev);
  265. }
  266. __transport_stop_task_timer(task, &flags);
  267. if (!(atomic_dec_and_test(&cmd->t_task_cdbs_ex_left))) {
  268. spin_unlock_irqrestore(
  269. &cmd->t_state_lock, flags);
  270. DEBUG_LR("LUN_RESET: Skipping task: %p, dev: %p for"
  271. " t_task_cdbs_ex_left: %d\n", task, dev,
  272. atomic_read(&cmd->t_task_cdbs_ex_left));
  273. spin_lock_irqsave(&dev->execute_task_lock, flags);
  274. continue;
  275. }
  276. fe_count = atomic_read(&cmd->t_fe_count);
  277. if (atomic_read(&cmd->t_transport_active)) {
  278. DEBUG_LR("LUN_RESET: got t_transport_active = 1 for"
  279. " task: %p, t_fe_count: %d dev: %p\n", task,
  280. fe_count, dev);
  281. atomic_set(&cmd->t_transport_aborted, 1);
  282. spin_unlock_irqrestore(&cmd->t_state_lock,
  283. flags);
  284. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
  285. spin_lock_irqsave(&dev->execute_task_lock, flags);
  286. continue;
  287. }
  288. DEBUG_LR("LUN_RESET: Got t_transport_active = 0 for task: %p,"
  289. " t_fe_count: %d dev: %p\n", task, fe_count, dev);
  290. atomic_set(&cmd->t_transport_aborted, 1);
  291. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  292. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
  293. spin_lock_irqsave(&dev->execute_task_lock, flags);
  294. }
  295. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  296. /*
  297. * Release all commands remaining in the struct se_device cmd queue.
  298. *
  299. * This follows the same logic as above for the struct se_device
  300. * struct se_task state list, where commands are returned with
  301. * TASK_ABORTED status, if there is an outstanding $FABRIC_MOD
  302. * reference, otherwise the struct se_cmd is released.
  303. */
  304. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  305. list_for_each_entry_safe(cmd, tcmd, &qobj->qobj_list, se_queue_node) {
  306. /*
  307. * For PREEMPT_AND_ABORT usage, only process commands
  308. * with a matching reservation key.
  309. */
  310. if ((preempt_and_abort_list != NULL) &&
  311. (core_scsi3_check_cdb_abort_and_preempt(
  312. preempt_and_abort_list, cmd) != 0))
  313. continue;
  314. /*
  315. * Not aborting PROUT PREEMPT_AND_ABORT CDB..
  316. */
  317. if (prout_cmd == cmd)
  318. continue;
  319. atomic_dec(&cmd->t_transport_queue_active);
  320. atomic_dec(&qobj->queue_cnt);
  321. list_del(&cmd->se_queue_node);
  322. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  323. DEBUG_LR("LUN_RESET: %s from Device Queue: cmd: %p t_state:"
  324. " %d t_fe_count: %d\n", (preempt_and_abort_list) ?
  325. "Preempt" : "", cmd, cmd->t_state,
  326. atomic_read(&cmd->t_fe_count));
  327. /*
  328. * Signal that the command has failed via cmd->se_cmd_flags,
  329. */
  330. transport_new_cmd_failure(cmd);
  331. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas,
  332. atomic_read(&cmd->t_fe_count));
  333. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  334. }
  335. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  336. /*
  337. * Clear any legacy SPC-2 reservation when called during
  338. * LOGICAL UNIT RESET
  339. */
  340. if (!(preempt_and_abort_list) &&
  341. (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
  342. spin_lock(&dev->dev_reservation_lock);
  343. dev->dev_reserved_node_acl = NULL;
  344. dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
  345. spin_unlock(&dev->dev_reservation_lock);
  346. printk(KERN_INFO "LUN_RESET: SCSI-2 Released reservation\n");
  347. }
  348. spin_lock_irq(&dev->stats_lock);
  349. dev->num_resets++;
  350. spin_unlock_irq(&dev->stats_lock);
  351. DEBUG_LR("LUN_RESET: %s for [%s] Complete\n",
  352. (preempt_and_abort_list) ? "Preempt" : "TMR",
  353. dev->transport->name);
  354. return 0;
  355. }