target_core_tmr.c 13 KB

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