target_core_tmr.c 13 KB

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