target_core_tmr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. struct se_tmr_req *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 = kmem_cache_zalloc(se_tmr_req_cache, gfp_flags);
  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. unsigned long flags;
  63. if (!dev) {
  64. kmem_cache_free(se_tmr_req_cache, tmr);
  65. return;
  66. }
  67. spin_lock_irqsave(&dev->se_tmr_lock, flags);
  68. list_del(&tmr->tmr_list);
  69. spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
  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. static int target_check_cdb_and_preempt(struct list_head *list,
  91. struct se_cmd *cmd)
  92. {
  93. struct t10_pr_registration *reg;
  94. if (!list)
  95. return 0;
  96. list_for_each_entry(reg, list, pr_reg_abort_list) {
  97. if (reg->pr_res_key == cmd->pr_res_key)
  98. return 0;
  99. }
  100. return 1;
  101. }
  102. static void core_tmr_drain_tmr_list(
  103. struct se_device *dev,
  104. struct se_tmr_req *tmr,
  105. struct list_head *preempt_and_abort_list)
  106. {
  107. LIST_HEAD(drain_tmr_list);
  108. struct se_tmr_req *tmr_p, *tmr_pp;
  109. struct se_cmd *cmd;
  110. unsigned long flags;
  111. /*
  112. * Release all pending and outgoing TMRs aside from the received
  113. * LUN_RESET tmr..
  114. */
  115. spin_lock_irqsave(&dev->se_tmr_lock, flags);
  116. list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
  117. /*
  118. * Allow the received TMR to return with FUNCTION_COMPLETE.
  119. */
  120. if (tmr_p == tmr)
  121. continue;
  122. cmd = tmr_p->task_cmd;
  123. if (!cmd) {
  124. pr_err("Unable to locate struct se_cmd for TMR\n");
  125. continue;
  126. }
  127. /*
  128. * If this function was called with a valid pr_res_key
  129. * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
  130. * skip non regisration key matching TMRs.
  131. */
  132. if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
  133. continue;
  134. spin_lock(&cmd->t_state_lock);
  135. if (!atomic_read(&cmd->t_transport_active)) {
  136. spin_unlock(&cmd->t_state_lock);
  137. continue;
  138. }
  139. if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
  140. spin_unlock(&cmd->t_state_lock);
  141. continue;
  142. }
  143. spin_unlock(&cmd->t_state_lock);
  144. list_move_tail(&tmr_p->tmr_list, &drain_tmr_list);
  145. }
  146. spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
  147. list_for_each_entry_safe(tmr_p, tmr_pp, &drain_tmr_list, tmr_list) {
  148. list_del_init(&tmr_p->tmr_list);
  149. cmd = tmr_p->task_cmd;
  150. pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
  151. " Response: 0x%02x, t_state: %d\n",
  152. (preempt_and_abort_list) ? "Preempt" : "", tmr_p,
  153. tmr_p->function, tmr_p->response, cmd->t_state);
  154. transport_cmd_finish_abort(cmd, 1);
  155. }
  156. }
  157. static void core_tmr_drain_task_list(
  158. struct se_device *dev,
  159. struct se_cmd *prout_cmd,
  160. struct se_node_acl *tmr_nacl,
  161. int tas,
  162. struct list_head *preempt_and_abort_list)
  163. {
  164. LIST_HEAD(drain_task_list);
  165. struct se_cmd *cmd;
  166. struct se_task *task, *task_tmp;
  167. unsigned long flags;
  168. int fe_count;
  169. /*
  170. * Complete outstanding struct se_task CDBs with TASK_ABORTED SAM status.
  171. * This is following sam4r17, section 5.6 Aborting commands, Table 38
  172. * for TMR LUN_RESET:
  173. *
  174. * a) "Yes" indicates that each command that is aborted on an I_T nexus
  175. * other than the one that caused the SCSI device condition is
  176. * completed with TASK ABORTED status, if the TAS bit is set to one in
  177. * the Control mode page (see SPC-4). "No" indicates that no status is
  178. * returned for aborted commands.
  179. *
  180. * d) If the logical unit reset is caused by a particular I_T nexus
  181. * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
  182. * (TASK_ABORTED status) applies.
  183. *
  184. * Otherwise (e.g., if triggered by a hard reset), "no"
  185. * (no TASK_ABORTED SAM status) applies.
  186. *
  187. * Note that this seems to be independent of TAS (Task Aborted Status)
  188. * in the Control Mode Page.
  189. */
  190. spin_lock_irqsave(&dev->execute_task_lock, flags);
  191. list_for_each_entry_safe(task, task_tmp, &dev->state_task_list,
  192. t_state_list) {
  193. if (!task->task_se_cmd) {
  194. pr_err("task->task_se_cmd is NULL!\n");
  195. continue;
  196. }
  197. cmd = task->task_se_cmd;
  198. /*
  199. * For PREEMPT_AND_ABORT usage, only process commands
  200. * with a matching reservation key.
  201. */
  202. if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
  203. continue;
  204. /*
  205. * Not aborting PROUT PREEMPT_AND_ABORT CDB..
  206. */
  207. if (prout_cmd == cmd)
  208. continue;
  209. list_move_tail(&task->t_state_list, &drain_task_list);
  210. task->t_state_active = false;
  211. /*
  212. * Remove from task execute list before processing drain_task_list
  213. */
  214. if (!list_empty(&task->t_execute_list))
  215. __transport_remove_task_from_execute_queue(task, dev);
  216. }
  217. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  218. while (!list_empty(&drain_task_list)) {
  219. task = list_entry(drain_task_list.next, struct se_task, t_state_list);
  220. list_del(&task->t_state_list);
  221. cmd = task->task_se_cmd;
  222. pr_debug("LUN_RESET: %s cmd: %p task: %p"
  223. " ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state: %d"
  224. "cdb: 0x%02x\n",
  225. (preempt_and_abort_list) ? "Preempt" : "", cmd, task,
  226. cmd->se_tfo->get_task_tag(cmd), 0,
  227. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
  228. cmd->t_task_cdb[0]);
  229. pr_debug("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
  230. " t_task_cdbs: %d t_task_cdbs_left: %d"
  231. " t_task_cdbs_sent: %d -- t_transport_active: %d"
  232. " t_transport_stop: %d t_transport_sent: %d\n",
  233. cmd->se_tfo->get_task_tag(cmd), cmd->pr_res_key,
  234. cmd->t_task_list_num,
  235. atomic_read(&cmd->t_task_cdbs_left),
  236. atomic_read(&cmd->t_task_cdbs_sent),
  237. atomic_read(&cmd->t_transport_active),
  238. atomic_read(&cmd->t_transport_stop),
  239. atomic_read(&cmd->t_transport_sent));
  240. /*
  241. * If the command may be queued onto a workqueue cancel it now.
  242. *
  243. * This is equivalent to removal from the execute queue in the
  244. * loop above, but we do it down here given that
  245. * cancel_work_sync may block.
  246. */
  247. if (cmd->t_state == TRANSPORT_COMPLETE)
  248. cancel_work_sync(&cmd->work);
  249. spin_lock_irqsave(&cmd->t_state_lock, flags);
  250. target_stop_task(task, &flags);
  251. if (!atomic_dec_and_test(&cmd->t_task_cdbs_ex_left)) {
  252. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  253. pr_debug("LUN_RESET: Skipping task: %p, dev: %p for"
  254. " t_task_cdbs_ex_left: %d\n", task, dev,
  255. atomic_read(&cmd->t_task_cdbs_ex_left));
  256. continue;
  257. }
  258. fe_count = atomic_read(&cmd->t_fe_count);
  259. if (atomic_read(&cmd->t_transport_active)) {
  260. pr_debug("LUN_RESET: got t_transport_active = 1 for"
  261. " task: %p, t_fe_count: %d dev: %p\n", task,
  262. fe_count, dev);
  263. atomic_set(&cmd->t_transport_aborted, 1);
  264. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  265. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
  266. continue;
  267. }
  268. pr_debug("LUN_RESET: Got t_transport_active = 0 for task: %p,"
  269. " t_fe_count: %d dev: %p\n", task, fe_count, dev);
  270. atomic_set(&cmd->t_transport_aborted, 1);
  271. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  272. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
  273. }
  274. }
  275. static void core_tmr_drain_cmd_list(
  276. struct se_device *dev,
  277. struct se_cmd *prout_cmd,
  278. struct se_node_acl *tmr_nacl,
  279. int tas,
  280. struct list_head *preempt_and_abort_list)
  281. {
  282. LIST_HEAD(drain_cmd_list);
  283. struct se_queue_obj *qobj = &dev->dev_queue_obj;
  284. struct se_cmd *cmd, *tcmd;
  285. unsigned long flags;
  286. /*
  287. * Release all commands remaining in the struct se_device cmd queue.
  288. *
  289. * This follows the same logic as above for the struct se_device
  290. * struct se_task state list, where commands are returned with
  291. * TASK_ABORTED status, if there is an outstanding $FABRIC_MOD
  292. * reference, otherwise the struct se_cmd is released.
  293. */
  294. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  295. list_for_each_entry_safe(cmd, tcmd, &qobj->qobj_list, se_queue_node) {
  296. /*
  297. * For PREEMPT_AND_ABORT usage, only process commands
  298. * with a matching reservation key.
  299. */
  300. if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
  301. continue;
  302. /*
  303. * Not aborting PROUT PREEMPT_AND_ABORT CDB..
  304. */
  305. if (prout_cmd == cmd)
  306. continue;
  307. atomic_set(&cmd->t_transport_queue_active, 0);
  308. atomic_dec(&qobj->queue_cnt);
  309. list_move_tail(&cmd->se_queue_node, &drain_cmd_list);
  310. }
  311. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  312. while (!list_empty(&drain_cmd_list)) {
  313. cmd = list_entry(drain_cmd_list.next, struct se_cmd, se_queue_node);
  314. list_del_init(&cmd->se_queue_node);
  315. pr_debug("LUN_RESET: %s from Device Queue: cmd: %p t_state:"
  316. " %d t_fe_count: %d\n", (preempt_and_abort_list) ?
  317. "Preempt" : "", cmd, cmd->t_state,
  318. atomic_read(&cmd->t_fe_count));
  319. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas,
  320. atomic_read(&cmd->t_fe_count));
  321. }
  322. }
  323. int core_tmr_lun_reset(
  324. struct se_device *dev,
  325. struct se_tmr_req *tmr,
  326. struct list_head *preempt_and_abort_list,
  327. struct se_cmd *prout_cmd)
  328. {
  329. struct se_node_acl *tmr_nacl = NULL;
  330. struct se_portal_group *tmr_tpg = NULL;
  331. int tas;
  332. /*
  333. * TASK_ABORTED status bit, this is configurable via ConfigFS
  334. * struct se_device attributes. spc4r17 section 7.4.6 Control mode page
  335. *
  336. * A task aborted status (TAS) bit set to zero specifies that aborted
  337. * tasks shall be terminated by the device server without any response
  338. * to the application client. A TAS bit set to one specifies that tasks
  339. * aborted by the actions of an I_T nexus other than the I_T nexus on
  340. * which the command was received shall be completed with TASK ABORTED
  341. * status (see SAM-4).
  342. */
  343. tas = dev->se_sub_dev->se_dev_attrib.emulate_tas;
  344. /*
  345. * Determine if this se_tmr is coming from a $FABRIC_MOD
  346. * or struct se_device passthrough..
  347. */
  348. if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
  349. tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
  350. tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
  351. if (tmr_nacl && tmr_tpg) {
  352. pr_debug("LUN_RESET: TMR caller fabric: %s"
  353. " initiator port %s\n",
  354. tmr_tpg->se_tpg_tfo->get_fabric_name(),
  355. tmr_nacl->initiatorname);
  356. }
  357. }
  358. pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
  359. (preempt_and_abort_list) ? "Preempt" : "TMR",
  360. dev->transport->name, tas);
  361. core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
  362. core_tmr_drain_task_list(dev, prout_cmd, tmr_nacl, tas,
  363. preempt_and_abort_list);
  364. core_tmr_drain_cmd_list(dev, prout_cmd, tmr_nacl, tas,
  365. preempt_and_abort_list);
  366. /*
  367. * Clear any legacy SPC-2 reservation when called during
  368. * LOGICAL UNIT RESET
  369. */
  370. if (!preempt_and_abort_list &&
  371. (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
  372. spin_lock(&dev->dev_reservation_lock);
  373. dev->dev_reserved_node_acl = NULL;
  374. dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
  375. spin_unlock(&dev->dev_reservation_lock);
  376. pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
  377. }
  378. spin_lock_irq(&dev->stats_lock);
  379. dev->num_resets++;
  380. spin_unlock_irq(&dev->stats_lock);
  381. pr_debug("LUN_RESET: %s for [%s] Complete\n",
  382. (preempt_and_abort_list) ? "Preempt" : "TMR",
  383. dev->transport->name);
  384. return 0;
  385. }