target_core_tmr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * Filename: target_core_tmr.c
  4. *
  5. * This file contains SPC-3 task management infrastructure
  6. *
  7. * (c) Copyright 2009-2013 Datera, Inc.
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. ******************************************************************************/
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/list.h>
  15. #include <linux/export.h>
  16. #include <target/target_core_base.h>
  17. #include <target/target_core_backend.h>
  18. #include <target/target_core_fabric.h>
  19. #include "target_core_internal.h"
  20. #include "target_core_alua.h"
  21. #include "target_core_pr.h"
  22. int core_tmr_alloc_req(
  23. struct se_cmd *se_cmd,
  24. void *fabric_tmr_ptr,
  25. u8 function,
  26. gfp_t gfp_flags)
  27. {
  28. struct se_tmr_req *tmr;
  29. tmr = kzalloc(sizeof(struct se_tmr_req), gfp_flags);
  30. if (!tmr) {
  31. pr_err("Unable to allocate struct se_tmr_req\n");
  32. return -ENOMEM;
  33. }
  34. se_cmd->se_cmd_flags |= SCF_SCSI_TMR_CDB;
  35. se_cmd->se_tmr_req = tmr;
  36. tmr->task_cmd = se_cmd;
  37. tmr->fabric_tmr_ptr = fabric_tmr_ptr;
  38. tmr->function = function;
  39. INIT_LIST_HEAD(&tmr->tmr_list);
  40. return 0;
  41. }
  42. EXPORT_SYMBOL(core_tmr_alloc_req);
  43. void core_tmr_release_req(struct se_tmr_req *tmr)
  44. {
  45. kfree(tmr);
  46. }
  47. static int target_check_cdb_and_preempt(struct list_head *list,
  48. struct se_cmd *cmd)
  49. {
  50. struct t10_pr_registration *reg;
  51. if (!list)
  52. return 0;
  53. list_for_each_entry(reg, list, pr_reg_abort_list) {
  54. if (reg->pr_res_key == cmd->pr_res_key)
  55. return 0;
  56. }
  57. return 1;
  58. }
  59. static bool __target_check_io_state(struct se_cmd *se_cmd,
  60. struct se_session *tmr_sess, bool tas)
  61. {
  62. struct se_session *sess = se_cmd->se_sess;
  63. lockdep_assert_held(&sess->sess_cmd_lock);
  64. /*
  65. * If command already reached CMD_T_COMPLETE state within
  66. * target_complete_cmd() or CMD_T_FABRIC_STOP due to shutdown,
  67. * this se_cmd has been passed to fabric driver and will
  68. * not be aborted.
  69. *
  70. * Otherwise, obtain a local se_cmd->cmd_kref now for TMR
  71. * ABORT_TASK + LUN_RESET for CMD_T_ABORTED processing as
  72. * long as se_cmd->cmd_kref is still active unless zero.
  73. */
  74. spin_lock(&se_cmd->t_state_lock);
  75. if (se_cmd->transport_state & (CMD_T_COMPLETE | CMD_T_FABRIC_STOP)) {
  76. pr_debug("Attempted to abort io tag: %llu already complete or"
  77. " fabric stop, skipping\n", se_cmd->tag);
  78. spin_unlock(&se_cmd->t_state_lock);
  79. return false;
  80. }
  81. se_cmd->transport_state |= CMD_T_ABORTED;
  82. if ((tmr_sess != se_cmd->se_sess) && tas)
  83. se_cmd->transport_state |= CMD_T_TAS;
  84. spin_unlock(&se_cmd->t_state_lock);
  85. return kref_get_unless_zero(&se_cmd->cmd_kref);
  86. }
  87. void core_tmr_abort_task(
  88. struct se_device *dev,
  89. struct se_tmr_req *tmr,
  90. struct se_session *se_sess)
  91. {
  92. LIST_HEAD(aborted_list);
  93. struct se_cmd *se_cmd, *next;
  94. unsigned long flags;
  95. bool rc;
  96. u64 ref_tag;
  97. int i;
  98. for (i = 0; i < dev->queue_cnt; i++) {
  99. flush_work(&dev->queues[i].sq.work);
  100. spin_lock_irqsave(&dev->queues[i].lock, flags);
  101. list_for_each_entry_safe(se_cmd, next, &dev->queues[i].state_list,
  102. state_list) {
  103. if (se_sess != se_cmd->se_sess)
  104. continue;
  105. /*
  106. * skip task management functions, including
  107. * tmr->task_cmd
  108. */
  109. if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
  110. continue;
  111. ref_tag = se_cmd->tag;
  112. if (tmr->ref_task_tag != ref_tag)
  113. continue;
  114. pr_err("ABORT_TASK: Found referenced %s task_tag: %llu\n",
  115. se_cmd->se_tfo->fabric_name, ref_tag);
  116. spin_lock(&se_sess->sess_cmd_lock);
  117. rc = __target_check_io_state(se_cmd, se_sess, 0);
  118. spin_unlock(&se_sess->sess_cmd_lock);
  119. if (!rc)
  120. continue;
  121. list_move_tail(&se_cmd->state_list, &aborted_list);
  122. se_cmd->state_active = false;
  123. spin_unlock_irqrestore(&dev->queues[i].lock, flags);
  124. if (dev->transport->tmr_notify)
  125. dev->transport->tmr_notify(dev, TMR_ABORT_TASK,
  126. &aborted_list);
  127. list_del_init(&se_cmd->state_list);
  128. target_put_cmd_and_wait(se_cmd);
  129. pr_err("ABORT_TASK: Sending TMR_FUNCTION_COMPLETE for ref_tag: %llu\n",
  130. ref_tag);
  131. tmr->response = TMR_FUNCTION_COMPLETE;
  132. atomic_long_inc(&dev->aborts_complete);
  133. return;
  134. }
  135. spin_unlock_irqrestore(&dev->queues[i].lock, flags);
  136. }
  137. if (dev->transport->tmr_notify)
  138. dev->transport->tmr_notify(dev, TMR_ABORT_TASK, &aborted_list);
  139. printk("ABORT_TASK: Sending TMR_TASK_DOES_NOT_EXIST for ref_tag: %lld\n",
  140. tmr->ref_task_tag);
  141. tmr->response = TMR_TASK_DOES_NOT_EXIST;
  142. atomic_long_inc(&dev->aborts_no_task);
  143. }
  144. static void core_tmr_drain_tmr_list(
  145. struct se_device *dev,
  146. struct se_tmr_req *tmr,
  147. struct list_head *preempt_and_abort_list)
  148. {
  149. LIST_HEAD(drain_tmr_list);
  150. struct se_session *sess;
  151. struct se_tmr_req *tmr_p, *tmr_pp;
  152. struct se_cmd *cmd;
  153. unsigned long flags;
  154. bool rc;
  155. /*
  156. * Release all pending and outgoing TMRs aside from the received
  157. * LUN_RESET tmr..
  158. */
  159. spin_lock_irqsave(&dev->se_tmr_lock, flags);
  160. list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
  161. if (tmr_p == tmr)
  162. continue;
  163. cmd = tmr_p->task_cmd;
  164. if (!cmd) {
  165. pr_err("Unable to locate struct se_cmd for TMR\n");
  166. continue;
  167. }
  168. /*
  169. * We only execute one LUN_RESET at a time so we can't wait
  170. * on them below.
  171. */
  172. if (tmr_p->function == TMR_LUN_RESET)
  173. continue;
  174. /*
  175. * If this function was called with a valid pr_res_key
  176. * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
  177. * skip non registration key matching TMRs.
  178. */
  179. if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
  180. continue;
  181. sess = cmd->se_sess;
  182. if (WARN_ON_ONCE(!sess))
  183. continue;
  184. spin_lock(&sess->sess_cmd_lock);
  185. rc = __target_check_io_state(cmd, sess, 0);
  186. spin_unlock(&sess->sess_cmd_lock);
  187. if (!rc) {
  188. printk("LUN_RESET TMR: non-zero kref_get_unless_zero\n");
  189. continue;
  190. }
  191. list_move_tail(&tmr_p->tmr_list, &drain_tmr_list);
  192. tmr_p->tmr_dev = NULL;
  193. }
  194. spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
  195. list_for_each_entry_safe(tmr_p, tmr_pp, &drain_tmr_list, tmr_list) {
  196. list_del_init(&tmr_p->tmr_list);
  197. cmd = tmr_p->task_cmd;
  198. pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
  199. " Response: 0x%02x, t_state: %d\n",
  200. (preempt_and_abort_list) ? "Preempt" : "", tmr_p,
  201. tmr_p->function, tmr_p->response, cmd->t_state);
  202. target_put_cmd_and_wait(cmd);
  203. }
  204. }
  205. /**
  206. * core_tmr_drain_state_list() - abort SCSI commands associated with a device
  207. *
  208. * @dev: Device for which to abort outstanding SCSI commands.
  209. * @prout_cmd: Pointer to the SCSI PREEMPT AND ABORT if this function is called
  210. * to realize the PREEMPT AND ABORT functionality.
  211. * @tmr_sess: Session through which the LUN RESET has been received.
  212. * @tas: Task Aborted Status (TAS) bit from the SCSI control mode page.
  213. * A quote from SPC-4, paragraph "7.5.10 Control mode page":
  214. * "A task aborted status (TAS) bit set to zero specifies that
  215. * aborted commands shall be terminated by the device server
  216. * without any response to the application client. A TAS bit set
  217. * to one specifies that commands aborted by the actions of an I_T
  218. * nexus other than the I_T nexus on which the command was
  219. * received shall be completed with TASK ABORTED status."
  220. * @preempt_and_abort_list: For the PREEMPT AND ABORT functionality, a list
  221. * with registrations that will be preempted.
  222. */
  223. static void core_tmr_drain_state_list(
  224. struct se_device *dev,
  225. struct se_cmd *prout_cmd,
  226. struct se_session *tmr_sess,
  227. bool tas,
  228. struct list_head *preempt_and_abort_list)
  229. {
  230. LIST_HEAD(drain_task_list);
  231. struct se_session *sess;
  232. struct se_cmd *cmd, *next;
  233. unsigned long flags;
  234. int rc, i;
  235. /*
  236. * Complete outstanding commands with TASK_ABORTED SAM status.
  237. *
  238. * This is following sam4r17, section 5.6 Aborting commands, Table 38
  239. * for TMR LUN_RESET:
  240. *
  241. * a) "Yes" indicates that each command that is aborted on an I_T nexus
  242. * other than the one that caused the SCSI device condition is
  243. * completed with TASK ABORTED status, if the TAS bit is set to one in
  244. * the Control mode page (see SPC-4). "No" indicates that no status is
  245. * returned for aborted commands.
  246. *
  247. * d) If the logical unit reset is caused by a particular I_T nexus
  248. * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
  249. * (TASK_ABORTED status) applies.
  250. *
  251. * Otherwise (e.g., if triggered by a hard reset), "no"
  252. * (no TASK_ABORTED SAM status) applies.
  253. *
  254. * Note that this seems to be independent of TAS (Task Aborted Status)
  255. * in the Control Mode Page.
  256. */
  257. for (i = 0; i < dev->queue_cnt; i++) {
  258. flush_work(&dev->queues[i].sq.work);
  259. spin_lock_irqsave(&dev->queues[i].lock, flags);
  260. list_for_each_entry_safe(cmd, next, &dev->queues[i].state_list,
  261. state_list) {
  262. /*
  263. * For PREEMPT_AND_ABORT usage, only process commands
  264. * with a matching reservation key.
  265. */
  266. if (target_check_cdb_and_preempt(preempt_and_abort_list,
  267. cmd))
  268. continue;
  269. /*
  270. * Not aborting PROUT PREEMPT_AND_ABORT CDB..
  271. */
  272. if (prout_cmd == cmd)
  273. continue;
  274. sess = cmd->se_sess;
  275. if (WARN_ON_ONCE(!sess))
  276. continue;
  277. spin_lock(&sess->sess_cmd_lock);
  278. rc = __target_check_io_state(cmd, tmr_sess, tas);
  279. spin_unlock(&sess->sess_cmd_lock);
  280. if (!rc)
  281. continue;
  282. list_move_tail(&cmd->state_list, &drain_task_list);
  283. cmd->state_active = false;
  284. }
  285. spin_unlock_irqrestore(&dev->queues[i].lock, flags);
  286. }
  287. if (dev->transport->tmr_notify)
  288. dev->transport->tmr_notify(dev, preempt_and_abort_list ?
  289. TMR_LUN_RESET_PRO : TMR_LUN_RESET,
  290. &drain_task_list);
  291. while (!list_empty(&drain_task_list)) {
  292. cmd = list_entry(drain_task_list.next, struct se_cmd, state_list);
  293. list_del_init(&cmd->state_list);
  294. target_show_cmd("LUN_RESET: ", cmd);
  295. pr_debug("LUN_RESET: ITT[0x%08llx] - %s pr_res_key: 0x%016Lx\n",
  296. cmd->tag, (preempt_and_abort_list) ? "preempt" : "",
  297. cmd->pr_res_key);
  298. target_put_cmd_and_wait(cmd);
  299. }
  300. }
  301. int core_tmr_lun_reset(
  302. struct se_device *dev,
  303. struct se_tmr_req *tmr,
  304. struct list_head *preempt_and_abort_list,
  305. struct se_cmd *prout_cmd)
  306. {
  307. struct se_node_acl *tmr_nacl = NULL;
  308. struct se_portal_group *tmr_tpg = NULL;
  309. struct se_session *tmr_sess = NULL;
  310. bool tas;
  311. /*
  312. * TASK_ABORTED status bit, this is configurable via ConfigFS
  313. * struct se_device attributes. spc4r17 section 7.4.6 Control mode page
  314. *
  315. * A task aborted status (TAS) bit set to zero specifies that aborted
  316. * tasks shall be terminated by the device server without any response
  317. * to the application client. A TAS bit set to one specifies that tasks
  318. * aborted by the actions of an I_T nexus other than the I_T nexus on
  319. * which the command was received shall be completed with TASK ABORTED
  320. * status (see SAM-4).
  321. */
  322. tas = dev->dev_attrib.emulate_tas;
  323. /*
  324. * Determine if this se_tmr is coming from a $FABRIC_MOD
  325. * or struct se_device passthrough..
  326. */
  327. if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
  328. tmr_sess = tmr->task_cmd->se_sess;
  329. tmr_nacl = tmr_sess->se_node_acl;
  330. tmr_tpg = tmr_sess->se_tpg;
  331. if (tmr_nacl && tmr_tpg) {
  332. pr_debug("LUN_RESET: TMR caller fabric: %s"
  333. " initiator port %s\n",
  334. tmr_tpg->se_tpg_tfo->fabric_name,
  335. tmr_nacl->initiatorname);
  336. }
  337. }
  338. /*
  339. * We only allow one reset or preempt and abort to execute at a time
  340. * to prevent one call from claiming all the cmds causing a second
  341. * call from returning while cmds it should have waited on are still
  342. * running.
  343. */
  344. mutex_lock(&dev->lun_reset_mutex);
  345. pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
  346. (preempt_and_abort_list) ? "Preempt" : "TMR",
  347. dev->transport->name, tas);
  348. core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
  349. core_tmr_drain_state_list(dev, prout_cmd, tmr_sess, tas,
  350. preempt_and_abort_list);
  351. mutex_unlock(&dev->lun_reset_mutex);
  352. /*
  353. * Clear any legacy SPC-2 reservation when called during
  354. * LOGICAL UNIT RESET
  355. */
  356. if (!preempt_and_abort_list &&
  357. (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)) {
  358. spin_lock(&dev->dev_reservation_lock);
  359. dev->reservation_holder = NULL;
  360. dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
  361. spin_unlock(&dev->dev_reservation_lock);
  362. pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
  363. }
  364. atomic_long_inc(&dev->num_resets);
  365. pr_debug("LUN_RESET: %s for [%s] Complete\n",
  366. (preempt_and_abort_list) ? "Preempt" : "TMR",
  367. dev->transport->name);
  368. return 0;
  369. }