target_core_tmr.c 12 KB

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