target_core_tmr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <linux/export.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_cmnd.h>
  32. #include <target/target_core_base.h>
  33. #include <target/target_core_backend.h>
  34. #include <target/target_core_fabric.h>
  35. #include <target/target_core_configfs.h>
  36. #include "target_core_internal.h"
  37. #include "target_core_alua.h"
  38. #include "target_core_pr.h"
  39. int core_tmr_alloc_req(
  40. struct se_cmd *se_cmd,
  41. void *fabric_tmr_ptr,
  42. u8 function,
  43. gfp_t gfp_flags)
  44. {
  45. struct se_tmr_req *tmr;
  46. tmr = kzalloc(sizeof(struct se_tmr_req), gfp_flags);
  47. if (!tmr) {
  48. pr_err("Unable to allocate struct se_tmr_req\n");
  49. return -ENOMEM;
  50. }
  51. se_cmd->se_cmd_flags |= SCF_SCSI_TMR_CDB;
  52. se_cmd->se_tmr_req = tmr;
  53. tmr->task_cmd = se_cmd;
  54. tmr->fabric_tmr_ptr = fabric_tmr_ptr;
  55. tmr->function = function;
  56. INIT_LIST_HEAD(&tmr->tmr_list);
  57. return 0;
  58. }
  59. EXPORT_SYMBOL(core_tmr_alloc_req);
  60. void core_tmr_release_req(
  61. struct se_tmr_req *tmr)
  62. {
  63. struct se_device *dev = tmr->tmr_dev;
  64. unsigned long flags;
  65. if (!dev) {
  66. kfree(tmr);
  67. return;
  68. }
  69. spin_lock_irqsave(&dev->se_tmr_lock, flags);
  70. list_del(&tmr->tmr_list);
  71. spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
  72. kfree(tmr);
  73. }
  74. static void core_tmr_handle_tas_abort(
  75. struct se_node_acl *tmr_nacl,
  76. struct se_cmd *cmd,
  77. int tas,
  78. int fe_count)
  79. {
  80. if (!fe_count) {
  81. transport_cmd_finish_abort(cmd, 1);
  82. return;
  83. }
  84. /*
  85. * TASK ABORTED status (TAS) bit support
  86. */
  87. if ((tmr_nacl &&
  88. (tmr_nacl == cmd->se_sess->se_node_acl)) || tas)
  89. transport_send_task_abort(cmd);
  90. transport_cmd_finish_abort(cmd, 0);
  91. }
  92. static int target_check_cdb_and_preempt(struct list_head *list,
  93. struct se_cmd *cmd)
  94. {
  95. struct t10_pr_registration *reg;
  96. if (!list)
  97. return 0;
  98. list_for_each_entry(reg, list, pr_reg_abort_list) {
  99. if (reg->pr_res_key == cmd->pr_res_key)
  100. return 0;
  101. }
  102. return 1;
  103. }
  104. void core_tmr_abort_task(
  105. struct se_device *dev,
  106. struct se_tmr_req *tmr,
  107. struct se_session *se_sess)
  108. {
  109. struct se_cmd *se_cmd, *tmp_cmd;
  110. unsigned long flags;
  111. int ref_tag;
  112. spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
  113. list_for_each_entry_safe(se_cmd, tmp_cmd,
  114. &se_sess->sess_cmd_list, se_cmd_list) {
  115. if (dev != se_cmd->se_dev)
  116. continue;
  117. ref_tag = se_cmd->se_tfo->get_task_tag(se_cmd);
  118. if (tmr->ref_task_tag != ref_tag)
  119. continue;
  120. printk("ABORT_TASK: Found referenced %s task_tag: %u\n",
  121. se_cmd->se_tfo->get_fabric_name(), ref_tag);
  122. spin_lock_irq(&se_cmd->t_state_lock);
  123. if (se_cmd->transport_state & CMD_T_COMPLETE) {
  124. printk("ABORT_TASK: ref_tag: %u already complete, skipping\n", ref_tag);
  125. spin_unlock_irq(&se_cmd->t_state_lock);
  126. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  127. goto out;
  128. }
  129. se_cmd->transport_state |= CMD_T_ABORTED;
  130. spin_unlock_irq(&se_cmd->t_state_lock);
  131. list_del_init(&se_cmd->se_cmd_list);
  132. kref_get(&se_cmd->cmd_kref);
  133. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  134. cancel_work_sync(&se_cmd->work);
  135. transport_wait_for_tasks(se_cmd);
  136. /*
  137. * Now send SAM_STAT_TASK_ABORTED status for the referenced
  138. * se_cmd descriptor..
  139. */
  140. transport_send_task_abort(se_cmd);
  141. /*
  142. * Also deal with possible extra acknowledge reference..
  143. */
  144. if (se_cmd->se_cmd_flags & SCF_ACK_KREF)
  145. target_put_sess_cmd(se_sess, se_cmd);
  146. target_put_sess_cmd(se_sess, se_cmd);
  147. printk("ABORT_TASK: Sending TMR_FUNCTION_COMPLETE for"
  148. " ref_tag: %d\n", ref_tag);
  149. tmr->response = TMR_FUNCTION_COMPLETE;
  150. return;
  151. }
  152. spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  153. out:
  154. printk("ABORT_TASK: Sending TMR_TASK_DOES_NOT_EXIST for ref_tag: %d\n",
  155. tmr->ref_task_tag);
  156. tmr->response = TMR_TASK_DOES_NOT_EXIST;
  157. }
  158. static void core_tmr_drain_tmr_list(
  159. struct se_device *dev,
  160. struct se_tmr_req *tmr,
  161. struct list_head *preempt_and_abort_list)
  162. {
  163. LIST_HEAD(drain_tmr_list);
  164. struct se_tmr_req *tmr_p, *tmr_pp;
  165. struct se_cmd *cmd;
  166. unsigned long flags;
  167. /*
  168. * Release all pending and outgoing TMRs aside from the received
  169. * LUN_RESET tmr..
  170. */
  171. spin_lock_irqsave(&dev->se_tmr_lock, flags);
  172. list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
  173. /*
  174. * Allow the received TMR to return with FUNCTION_COMPLETE.
  175. */
  176. if (tmr_p == tmr)
  177. continue;
  178. cmd = tmr_p->task_cmd;
  179. if (!cmd) {
  180. pr_err("Unable to locate struct se_cmd for TMR\n");
  181. continue;
  182. }
  183. /*
  184. * If this function was called with a valid pr_res_key
  185. * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
  186. * skip non regisration key matching TMRs.
  187. */
  188. if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
  189. continue;
  190. spin_lock(&cmd->t_state_lock);
  191. if (!(cmd->transport_state & CMD_T_ACTIVE)) {
  192. spin_unlock(&cmd->t_state_lock);
  193. continue;
  194. }
  195. if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
  196. spin_unlock(&cmd->t_state_lock);
  197. continue;
  198. }
  199. spin_unlock(&cmd->t_state_lock);
  200. list_move_tail(&tmr_p->tmr_list, &drain_tmr_list);
  201. }
  202. spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
  203. list_for_each_entry_safe(tmr_p, tmr_pp, &drain_tmr_list, tmr_list) {
  204. list_del_init(&tmr_p->tmr_list);
  205. cmd = tmr_p->task_cmd;
  206. pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
  207. " Response: 0x%02x, t_state: %d\n",
  208. (preempt_and_abort_list) ? "Preempt" : "", tmr_p,
  209. tmr_p->function, tmr_p->response, cmd->t_state);
  210. transport_cmd_finish_abort(cmd, 1);
  211. }
  212. }
  213. static void core_tmr_drain_state_list(
  214. struct se_device *dev,
  215. struct se_cmd *prout_cmd,
  216. struct se_node_acl *tmr_nacl,
  217. int tas,
  218. struct list_head *preempt_and_abort_list)
  219. {
  220. LIST_HEAD(drain_task_list);
  221. struct se_cmd *cmd, *next;
  222. unsigned long flags;
  223. int fe_count;
  224. /*
  225. * Complete outstanding commands with TASK_ABORTED SAM status.
  226. *
  227. * This is following sam4r17, section 5.6 Aborting commands, Table 38
  228. * for TMR LUN_RESET:
  229. *
  230. * a) "Yes" indicates that each command that is aborted on an I_T nexus
  231. * other than the one that caused the SCSI device condition is
  232. * completed with TASK ABORTED status, if the TAS bit is set to one in
  233. * the Control mode page (see SPC-4). "No" indicates that no status is
  234. * returned for aborted commands.
  235. *
  236. * d) If the logical unit reset is caused by a particular I_T nexus
  237. * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
  238. * (TASK_ABORTED status) applies.
  239. *
  240. * Otherwise (e.g., if triggered by a hard reset), "no"
  241. * (no TASK_ABORTED SAM status) applies.
  242. *
  243. * Note that this seems to be independent of TAS (Task Aborted Status)
  244. * in the Control Mode Page.
  245. */
  246. spin_lock_irqsave(&dev->execute_task_lock, flags);
  247. list_for_each_entry_safe(cmd, next, &dev->state_list, state_list) {
  248. /*
  249. * For PREEMPT_AND_ABORT usage, only process commands
  250. * with a matching reservation key.
  251. */
  252. if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
  253. continue;
  254. /*
  255. * Not aborting PROUT PREEMPT_AND_ABORT CDB..
  256. */
  257. if (prout_cmd == cmd)
  258. continue;
  259. list_move_tail(&cmd->state_list, &drain_task_list);
  260. cmd->state_active = false;
  261. }
  262. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  263. while (!list_empty(&drain_task_list)) {
  264. cmd = list_entry(drain_task_list.next, struct se_cmd, state_list);
  265. list_del(&cmd->state_list);
  266. pr_debug("LUN_RESET: %s cmd: %p"
  267. " ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state: %d"
  268. "cdb: 0x%02x\n",
  269. (preempt_and_abort_list) ? "Preempt" : "", cmd,
  270. cmd->se_tfo->get_task_tag(cmd), 0,
  271. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
  272. cmd->t_task_cdb[0]);
  273. pr_debug("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
  274. " -- CMD_T_ACTIVE: %d"
  275. " CMD_T_STOP: %d CMD_T_SENT: %d\n",
  276. cmd->se_tfo->get_task_tag(cmd), cmd->pr_res_key,
  277. (cmd->transport_state & CMD_T_ACTIVE) != 0,
  278. (cmd->transport_state & CMD_T_STOP) != 0,
  279. (cmd->transport_state & CMD_T_SENT) != 0);
  280. /*
  281. * If the command may be queued onto a workqueue cancel it now.
  282. *
  283. * This is equivalent to removal from the execute queue in the
  284. * loop above, but we do it down here given that
  285. * cancel_work_sync may block.
  286. */
  287. if (cmd->t_state == TRANSPORT_COMPLETE)
  288. cancel_work_sync(&cmd->work);
  289. spin_lock_irqsave(&cmd->t_state_lock, flags);
  290. target_stop_cmd(cmd, &flags);
  291. fe_count = atomic_read(&cmd->t_fe_count);
  292. if (!(cmd->transport_state & CMD_T_ACTIVE)) {
  293. pr_debug("LUN_RESET: got CMD_T_ACTIVE for"
  294. " cdb: %p, t_fe_count: %d dev: %p\n", cmd,
  295. fe_count, dev);
  296. cmd->transport_state |= CMD_T_ABORTED;
  297. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  298. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
  299. continue;
  300. }
  301. pr_debug("LUN_RESET: Got !CMD_T_ACTIVE for cdb: %p,"
  302. " t_fe_count: %d dev: %p\n", cmd, fe_count, dev);
  303. cmd->transport_state |= CMD_T_ABORTED;
  304. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  305. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
  306. }
  307. }
  308. int core_tmr_lun_reset(
  309. struct se_device *dev,
  310. struct se_tmr_req *tmr,
  311. struct list_head *preempt_and_abort_list,
  312. struct se_cmd *prout_cmd)
  313. {
  314. struct se_node_acl *tmr_nacl = NULL;
  315. struct se_portal_group *tmr_tpg = NULL;
  316. int tas;
  317. /*
  318. * TASK_ABORTED status bit, this is configurable via ConfigFS
  319. * struct se_device attributes. spc4r17 section 7.4.6 Control mode page
  320. *
  321. * A task aborted status (TAS) bit set to zero specifies that aborted
  322. * tasks shall be terminated by the device server without any response
  323. * to the application client. A TAS bit set to one specifies that tasks
  324. * aborted by the actions of an I_T nexus other than the I_T nexus on
  325. * which the command was received shall be completed with TASK ABORTED
  326. * status (see SAM-4).
  327. */
  328. tas = dev->se_sub_dev->se_dev_attrib.emulate_tas;
  329. /*
  330. * Determine if this se_tmr is coming from a $FABRIC_MOD
  331. * or struct se_device passthrough..
  332. */
  333. if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
  334. tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
  335. tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
  336. if (tmr_nacl && tmr_tpg) {
  337. pr_debug("LUN_RESET: TMR caller fabric: %s"
  338. " initiator port %s\n",
  339. tmr_tpg->se_tpg_tfo->get_fabric_name(),
  340. tmr_nacl->initiatorname);
  341. }
  342. }
  343. pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
  344. (preempt_and_abort_list) ? "Preempt" : "TMR",
  345. dev->transport->name, tas);
  346. core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
  347. core_tmr_drain_state_list(dev, prout_cmd, tmr_nacl, tas,
  348. preempt_and_abort_list);
  349. /*
  350. * Clear any legacy SPC-2 reservation when called during
  351. * LOGICAL UNIT RESET
  352. */
  353. if (!preempt_and_abort_list &&
  354. (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
  355. spin_lock(&dev->dev_reservation_lock);
  356. dev->dev_reserved_node_acl = NULL;
  357. dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
  358. spin_unlock(&dev->dev_reservation_lock);
  359. pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
  360. }
  361. spin_lock_irq(&dev->stats_lock);
  362. dev->num_resets++;
  363. spin_unlock_irq(&dev->stats_lock);
  364. pr_debug("LUN_RESET: %s for [%s] Complete\n",
  365. (preempt_and_abort_list) ? "Preempt" : "TMR",
  366. dev->transport->name);
  367. return 0;
  368. }