target_core_tmr.c 13 KB

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