target_core_tmr.c 14 KB

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