target_core_tmr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. if (!list_empty(&cmd->execute_list))
  262. __target_remove_from_execute_list(cmd);
  263. }
  264. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  265. while (!list_empty(&drain_task_list)) {
  266. cmd = list_entry(drain_task_list.next, struct se_cmd, state_list);
  267. list_del(&cmd->state_list);
  268. pr_debug("LUN_RESET: %s cmd: %p"
  269. " ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state: %d"
  270. "cdb: 0x%02x\n",
  271. (preempt_and_abort_list) ? "Preempt" : "", cmd,
  272. cmd->se_tfo->get_task_tag(cmd), 0,
  273. cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
  274. cmd->t_task_cdb[0]);
  275. pr_debug("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
  276. " -- CMD_T_ACTIVE: %d"
  277. " CMD_T_STOP: %d CMD_T_SENT: %d\n",
  278. cmd->se_tfo->get_task_tag(cmd), cmd->pr_res_key,
  279. (cmd->transport_state & CMD_T_ACTIVE) != 0,
  280. (cmd->transport_state & CMD_T_STOP) != 0,
  281. (cmd->transport_state & CMD_T_SENT) != 0);
  282. /*
  283. * If the command may be queued onto a workqueue cancel it now.
  284. *
  285. * This is equivalent to removal from the execute queue in the
  286. * loop above, but we do it down here given that
  287. * cancel_work_sync may block.
  288. */
  289. if (cmd->t_state == TRANSPORT_COMPLETE)
  290. cancel_work_sync(&cmd->work);
  291. spin_lock_irqsave(&cmd->t_state_lock, flags);
  292. target_stop_cmd(cmd, &flags);
  293. fe_count = atomic_read(&cmd->t_fe_count);
  294. if (!(cmd->transport_state & CMD_T_ACTIVE)) {
  295. pr_debug("LUN_RESET: got CMD_T_ACTIVE for"
  296. " cdb: %p, t_fe_count: %d dev: %p\n", cmd,
  297. fe_count, dev);
  298. cmd->transport_state |= CMD_T_ABORTED;
  299. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  300. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
  301. continue;
  302. }
  303. pr_debug("LUN_RESET: Got !CMD_T_ACTIVE for cdb: %p,"
  304. " t_fe_count: %d dev: %p\n", cmd, fe_count, dev);
  305. cmd->transport_state |= CMD_T_ABORTED;
  306. spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  307. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
  308. }
  309. }
  310. static void core_tmr_drain_cmd_list(
  311. struct se_device *dev,
  312. struct se_cmd *prout_cmd,
  313. struct se_node_acl *tmr_nacl,
  314. int tas,
  315. struct list_head *preempt_and_abort_list)
  316. {
  317. LIST_HEAD(drain_cmd_list);
  318. struct se_queue_obj *qobj = &dev->dev_queue_obj;
  319. struct se_cmd *cmd, *tcmd;
  320. unsigned long flags;
  321. /*
  322. * Release all commands remaining in the per-device command queue.
  323. *
  324. * This follows the same logic as above for the state list.
  325. */
  326. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  327. list_for_each_entry_safe(cmd, tcmd, &qobj->qobj_list, se_queue_node) {
  328. /*
  329. * For PREEMPT_AND_ABORT usage, only process commands
  330. * with a matching reservation key.
  331. */
  332. if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
  333. continue;
  334. /*
  335. * Not aborting PROUT PREEMPT_AND_ABORT CDB..
  336. */
  337. if (prout_cmd == cmd)
  338. continue;
  339. cmd->transport_state &= ~CMD_T_QUEUED;
  340. atomic_dec(&qobj->queue_cnt);
  341. list_move_tail(&cmd->se_queue_node, &drain_cmd_list);
  342. }
  343. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  344. while (!list_empty(&drain_cmd_list)) {
  345. cmd = list_entry(drain_cmd_list.next, struct se_cmd, se_queue_node);
  346. list_del_init(&cmd->se_queue_node);
  347. pr_debug("LUN_RESET: %s from Device Queue: cmd: %p t_state:"
  348. " %d t_fe_count: %d\n", (preempt_and_abort_list) ?
  349. "Preempt" : "", cmd, cmd->t_state,
  350. atomic_read(&cmd->t_fe_count));
  351. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas,
  352. atomic_read(&cmd->t_fe_count));
  353. }
  354. }
  355. int core_tmr_lun_reset(
  356. struct se_device *dev,
  357. struct se_tmr_req *tmr,
  358. struct list_head *preempt_and_abort_list,
  359. struct se_cmd *prout_cmd)
  360. {
  361. struct se_node_acl *tmr_nacl = NULL;
  362. struct se_portal_group *tmr_tpg = NULL;
  363. int tas;
  364. /*
  365. * TASK_ABORTED status bit, this is configurable via ConfigFS
  366. * struct se_device attributes. spc4r17 section 7.4.6 Control mode page
  367. *
  368. * A task aborted status (TAS) bit set to zero specifies that aborted
  369. * tasks shall be terminated by the device server without any response
  370. * to the application client. A TAS bit set to one specifies that tasks
  371. * aborted by the actions of an I_T nexus other than the I_T nexus on
  372. * which the command was received shall be completed with TASK ABORTED
  373. * status (see SAM-4).
  374. */
  375. tas = dev->se_sub_dev->se_dev_attrib.emulate_tas;
  376. /*
  377. * Determine if this se_tmr is coming from a $FABRIC_MOD
  378. * or struct se_device passthrough..
  379. */
  380. if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
  381. tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
  382. tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
  383. if (tmr_nacl && tmr_tpg) {
  384. pr_debug("LUN_RESET: TMR caller fabric: %s"
  385. " initiator port %s\n",
  386. tmr_tpg->se_tpg_tfo->get_fabric_name(),
  387. tmr_nacl->initiatorname);
  388. }
  389. }
  390. pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
  391. (preempt_and_abort_list) ? "Preempt" : "TMR",
  392. dev->transport->name, tas);
  393. core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
  394. core_tmr_drain_state_list(dev, prout_cmd, tmr_nacl, tas,
  395. preempt_and_abort_list);
  396. core_tmr_drain_cmd_list(dev, prout_cmd, tmr_nacl, tas,
  397. preempt_and_abort_list);
  398. /*
  399. * Clear any legacy SPC-2 reservation when called during
  400. * LOGICAL UNIT RESET
  401. */
  402. if (!preempt_and_abort_list &&
  403. (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
  404. spin_lock(&dev->dev_reservation_lock);
  405. dev->dev_reserved_node_acl = NULL;
  406. dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
  407. spin_unlock(&dev->dev_reservation_lock);
  408. pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
  409. }
  410. spin_lock_irq(&dev->stats_lock);
  411. dev->num_resets++;
  412. spin_unlock_irq(&dev->stats_lock);
  413. pr_debug("LUN_RESET: %s for [%s] Complete\n",
  414. (preempt_and_abort_list) ? "Preempt" : "TMR",
  415. dev->transport->name);
  416. return 0;
  417. }