target_core_tmr.c 12 KB

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