target_core_tmr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/version.h>
  27. #include <linux/slab.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/list.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. #define DEBUG_LUN_RESET
  41. #ifdef DEBUG_LUN_RESET
  42. #define DEBUG_LR(x...) printk(KERN_INFO x)
  43. #else
  44. #define DEBUG_LR(x...)
  45. #endif
  46. struct se_tmr_req *core_tmr_alloc_req(
  47. struct se_cmd *se_cmd,
  48. void *fabric_tmr_ptr,
  49. u8 function)
  50. {
  51. struct se_tmr_req *tmr;
  52. tmr = kmem_cache_zalloc(se_tmr_req_cache, (in_interrupt()) ?
  53. GFP_ATOMIC : GFP_KERNEL);
  54. if (!(tmr)) {
  55. printk(KERN_ERR "Unable to allocate struct se_tmr_req\n");
  56. return ERR_PTR(-ENOMEM);
  57. }
  58. tmr->task_cmd = se_cmd;
  59. tmr->fabric_tmr_ptr = fabric_tmr_ptr;
  60. tmr->function = function;
  61. INIT_LIST_HEAD(&tmr->tmr_list);
  62. return tmr;
  63. }
  64. EXPORT_SYMBOL(core_tmr_alloc_req);
  65. void core_tmr_release_req(
  66. struct se_tmr_req *tmr)
  67. {
  68. struct se_device *dev = tmr->tmr_dev;
  69. spin_lock(&dev->se_tmr_lock);
  70. list_del(&tmr->tmr_list);
  71. kmem_cache_free(se_tmr_req_cache, tmr);
  72. spin_unlock(&dev->se_tmr_lock);
  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 != NULL) &&
  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. int core_tmr_lun_reset(
  93. struct se_device *dev,
  94. struct se_tmr_req *tmr,
  95. struct list_head *preempt_and_abort_list,
  96. struct se_cmd *prout_cmd)
  97. {
  98. struct se_cmd *cmd;
  99. struct se_queue_req *qr, *qr_tmp;
  100. struct se_node_acl *tmr_nacl = NULL;
  101. struct se_portal_group *tmr_tpg = NULL;
  102. struct se_queue_obj *qobj = dev->dev_queue_obj;
  103. struct se_tmr_req *tmr_p, *tmr_pp;
  104. struct se_task *task, *task_tmp;
  105. unsigned long flags;
  106. int fe_count, state, tas;
  107. /*
  108. * TASK_ABORTED status bit, this is configurable via ConfigFS
  109. * struct se_device attributes. spc4r17 section 7.4.6 Control mode page
  110. *
  111. * A task aborted status (TAS) bit set to zero specifies that aborted
  112. * tasks shall be terminated by the device server without any response
  113. * to the application client. A TAS bit set to one specifies that tasks
  114. * aborted by the actions of an I_T nexus other than the I_T nexus on
  115. * which the command was received shall be completed with TASK ABORTED
  116. * status (see SAM-4).
  117. */
  118. tas = DEV_ATTRIB(dev)->emulate_tas;
  119. /*
  120. * Determine if this se_tmr is coming from a $FABRIC_MOD
  121. * or struct se_device passthrough..
  122. */
  123. if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
  124. tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
  125. tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
  126. if (tmr_nacl && tmr_tpg) {
  127. DEBUG_LR("LUN_RESET: TMR caller fabric: %s"
  128. " initiator port %s\n",
  129. TPG_TFO(tmr_tpg)->get_fabric_name(),
  130. tmr_nacl->initiatorname);
  131. }
  132. }
  133. DEBUG_LR("LUN_RESET: %s starting for [%s], tas: %d\n",
  134. (preempt_and_abort_list) ? "Preempt" : "TMR",
  135. TRANSPORT(dev)->name, tas);
  136. /*
  137. * Release all pending and outgoing TMRs aside from the received
  138. * LUN_RESET tmr..
  139. */
  140. spin_lock(&dev->se_tmr_lock);
  141. list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
  142. /*
  143. * Allow the received TMR to return with FUNCTION_COMPLETE.
  144. */
  145. if (tmr && (tmr_p == tmr))
  146. continue;
  147. cmd = tmr_p->task_cmd;
  148. if (!(cmd)) {
  149. printk(KERN_ERR "Unable to locate struct se_cmd for TMR\n");
  150. continue;
  151. }
  152. /*
  153. * If this function was called with a valid pr_res_key
  154. * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
  155. * skip non regisration key matching TMRs.
  156. */
  157. if ((preempt_and_abort_list != NULL) &&
  158. (core_scsi3_check_cdb_abort_and_preempt(
  159. preempt_and_abort_list, cmd) != 0))
  160. continue;
  161. spin_unlock(&dev->se_tmr_lock);
  162. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  163. if (!(atomic_read(&T_TASK(cmd)->t_transport_active))) {
  164. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  165. spin_lock(&dev->se_tmr_lock);
  166. continue;
  167. }
  168. if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
  169. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  170. spin_lock(&dev->se_tmr_lock);
  171. continue;
  172. }
  173. DEBUG_LR("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
  174. " Response: 0x%02x, t_state: %d\n",
  175. (preempt_and_abort_list) ? "Preempt" : "", tmr_p,
  176. tmr_p->function, tmr_p->response, cmd->t_state);
  177. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  178. transport_cmd_finish_abort_tmr(cmd);
  179. spin_lock(&dev->se_tmr_lock);
  180. }
  181. spin_unlock(&dev->se_tmr_lock);
  182. /*
  183. * Complete outstanding struct se_task CDBs with TASK_ABORTED SAM status.
  184. * This is following sam4r17, section 5.6 Aborting commands, Table 38
  185. * for TMR LUN_RESET:
  186. *
  187. * a) "Yes" indicates that each command that is aborted on an I_T nexus
  188. * other than the one that caused the SCSI device condition is
  189. * completed with TASK ABORTED status, if the TAS bit is set to one in
  190. * the Control mode page (see SPC-4). "No" indicates that no status is
  191. * returned for aborted commands.
  192. *
  193. * d) If the logical unit reset is caused by a particular I_T nexus
  194. * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
  195. * (TASK_ABORTED status) applies.
  196. *
  197. * Otherwise (e.g., if triggered by a hard reset), "no"
  198. * (no TASK_ABORTED SAM status) applies.
  199. *
  200. * Note that this seems to be independent of TAS (Task Aborted Status)
  201. * in the Control Mode Page.
  202. */
  203. spin_lock_irqsave(&dev->execute_task_lock, flags);
  204. list_for_each_entry_safe(task, task_tmp, &dev->state_task_list,
  205. t_state_list) {
  206. if (!(TASK_CMD(task))) {
  207. printk(KERN_ERR "TASK_CMD(task) is NULL!\n");
  208. continue;
  209. }
  210. cmd = TASK_CMD(task);
  211. if (!T_TASK(cmd)) {
  212. printk(KERN_ERR "T_TASK(cmd) is NULL for task: %p cmd:"
  213. " %p ITT: 0x%08x\n", task, cmd,
  214. CMD_TFO(cmd)->get_task_tag(cmd));
  215. continue;
  216. }
  217. /*
  218. * For PREEMPT_AND_ABORT usage, only process commands
  219. * with a matching reservation key.
  220. */
  221. if ((preempt_and_abort_list != NULL) &&
  222. (core_scsi3_check_cdb_abort_and_preempt(
  223. preempt_and_abort_list, cmd) != 0))
  224. continue;
  225. /*
  226. * Not aborting PROUT PREEMPT_AND_ABORT CDB..
  227. */
  228. if (prout_cmd == cmd)
  229. continue;
  230. list_del(&task->t_state_list);
  231. atomic_set(&task->task_state_active, 0);
  232. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  233. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  234. DEBUG_LR("LUN_RESET: %s cmd: %p task: %p"
  235. " ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state/"
  236. "def_t_state: %d/%d cdb: 0x%02x\n",
  237. (preempt_and_abort_list) ? "Preempt" : "", cmd, task,
  238. CMD_TFO(cmd)->get_task_tag(cmd), 0,
  239. CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state,
  240. cmd->deferred_t_state, T_TASK(cmd)->t_task_cdb[0]);
  241. DEBUG_LR("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
  242. " t_task_cdbs: %d t_task_cdbs_left: %d"
  243. " t_task_cdbs_sent: %d -- t_transport_active: %d"
  244. " t_transport_stop: %d t_transport_sent: %d\n",
  245. CMD_TFO(cmd)->get_task_tag(cmd), cmd->pr_res_key,
  246. T_TASK(cmd)->t_task_cdbs,
  247. atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
  248. atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
  249. atomic_read(&T_TASK(cmd)->t_transport_active),
  250. atomic_read(&T_TASK(cmd)->t_transport_stop),
  251. atomic_read(&T_TASK(cmd)->t_transport_sent));
  252. if (atomic_read(&task->task_active)) {
  253. atomic_set(&task->task_stop, 1);
  254. spin_unlock_irqrestore(
  255. &T_TASK(cmd)->t_state_lock, flags);
  256. DEBUG_LR("LUN_RESET: Waiting for task: %p to shutdown"
  257. " for dev: %p\n", task, dev);
  258. wait_for_completion(&task->task_stop_comp);
  259. DEBUG_LR("LUN_RESET Completed task: %p shutdown for"
  260. " dev: %p\n", task, dev);
  261. spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
  262. atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
  263. atomic_set(&task->task_active, 0);
  264. atomic_set(&task->task_stop, 0);
  265. } else {
  266. if (atomic_read(&task->task_execute_queue) != 0)
  267. transport_remove_task_from_execute_queue(task, dev);
  268. }
  269. __transport_stop_task_timer(task, &flags);
  270. if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_ex_left))) {
  271. spin_unlock_irqrestore(
  272. &T_TASK(cmd)->t_state_lock, flags);
  273. DEBUG_LR("LUN_RESET: Skipping task: %p, dev: %p for"
  274. " t_task_cdbs_ex_left: %d\n", task, dev,
  275. atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left));
  276. spin_lock_irqsave(&dev->execute_task_lock, flags);
  277. continue;
  278. }
  279. fe_count = atomic_read(&T_TASK(cmd)->t_fe_count);
  280. if (atomic_read(&T_TASK(cmd)->t_transport_active)) {
  281. DEBUG_LR("LUN_RESET: got t_transport_active = 1 for"
  282. " task: %p, t_fe_count: %d dev: %p\n", task,
  283. fe_count, dev);
  284. atomic_set(&T_TASK(cmd)->t_transport_aborted, 1);
  285. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
  286. flags);
  287. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
  288. spin_lock_irqsave(&dev->execute_task_lock, flags);
  289. continue;
  290. }
  291. DEBUG_LR("LUN_RESET: Got t_transport_active = 0 for task: %p,"
  292. " t_fe_count: %d dev: %p\n", task, fe_count, dev);
  293. atomic_set(&T_TASK(cmd)->t_transport_aborted, 1);
  294. spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
  295. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
  296. spin_lock_irqsave(&dev->execute_task_lock, flags);
  297. }
  298. spin_unlock_irqrestore(&dev->execute_task_lock, flags);
  299. /*
  300. * Release all commands remaining in the struct se_device cmd queue.
  301. *
  302. * This follows the same logic as above for the struct se_device
  303. * struct se_task state list, where commands are returned with
  304. * TASK_ABORTED status, if there is an outstanding $FABRIC_MOD
  305. * reference, otherwise the struct se_cmd is released.
  306. */
  307. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  308. list_for_each_entry_safe(qr, qr_tmp, &qobj->qobj_list, qr_list) {
  309. cmd = (struct se_cmd *)qr->cmd;
  310. if (!(cmd)) {
  311. /*
  312. * Skip these for non PREEMPT_AND_ABORT usage..
  313. */
  314. if (preempt_and_abort_list != NULL)
  315. continue;
  316. atomic_dec(&qobj->queue_cnt);
  317. list_del(&qr->qr_list);
  318. kfree(qr);
  319. continue;
  320. }
  321. /*
  322. * For PREEMPT_AND_ABORT usage, only process commands
  323. * with a matching reservation key.
  324. */
  325. if ((preempt_and_abort_list != NULL) &&
  326. (core_scsi3_check_cdb_abort_and_preempt(
  327. preempt_and_abort_list, cmd) != 0))
  328. continue;
  329. /*
  330. * Not aborting PROUT PREEMPT_AND_ABORT CDB..
  331. */
  332. if (prout_cmd == cmd)
  333. continue;
  334. atomic_dec(&T_TASK(cmd)->t_transport_queue_active);
  335. atomic_dec(&qobj->queue_cnt);
  336. list_del(&qr->qr_list);
  337. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  338. state = qr->state;
  339. kfree(qr);
  340. DEBUG_LR("LUN_RESET: %s from Device Queue: cmd: %p t_state:"
  341. " %d t_fe_count: %d\n", (preempt_and_abort_list) ?
  342. "Preempt" : "", cmd, state,
  343. atomic_read(&T_TASK(cmd)->t_fe_count));
  344. /*
  345. * Signal that the command has failed via cmd->se_cmd_flags,
  346. * and call TFO->new_cmd_failure() to wakeup any fabric
  347. * dependent code used to wait for unsolicited data out
  348. * allocation to complete. The fabric module is expected
  349. * to dump any remaining unsolicited data out for the aborted
  350. * command at this point.
  351. */
  352. transport_new_cmd_failure(cmd);
  353. core_tmr_handle_tas_abort(tmr_nacl, cmd, tas,
  354. atomic_read(&T_TASK(cmd)->t_fe_count));
  355. spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
  356. }
  357. spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
  358. /*
  359. * Clear any legacy SPC-2 reservation when called during
  360. * LOGICAL UNIT RESET
  361. */
  362. if (!(preempt_and_abort_list) &&
  363. (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
  364. spin_lock(&dev->dev_reservation_lock);
  365. dev->dev_reserved_node_acl = NULL;
  366. dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
  367. spin_unlock(&dev->dev_reservation_lock);
  368. printk(KERN_INFO "LUN_RESET: SCSI-2 Released reservation\n");
  369. }
  370. spin_lock_irq(&dev->stats_lock);
  371. dev->num_resets++;
  372. spin_unlock_irq(&dev->stats_lock);
  373. DEBUG_LR("LUN_RESET: %s for [%s] Complete\n",
  374. (preempt_and_abort_list) ? "Preempt" : "TMR",
  375. TRANSPORT(dev)->name);
  376. return 0;
  377. }