PageRenderTime 384ms CodeModel.GetById 83ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/target/target_core_tmr.c

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