request.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #ifndef _ISCI_REQUEST_H_
  56. #define _ISCI_REQUEST_H_
  57. #include "isci.h"
  58. #include "host.h"
  59. #include "scu_task_context.h"
  60. /**
  61. * struct isci_request_status - This enum defines the possible states of an I/O
  62. * request.
  63. *
  64. *
  65. */
  66. enum isci_request_status {
  67. unallocated = 0x00,
  68. allocated = 0x01,
  69. started = 0x02,
  70. completed = 0x03,
  71. aborting = 0x04,
  72. aborted = 0x05,
  73. terminating = 0x06,
  74. dead = 0x07
  75. };
  76. enum sci_request_protocol {
  77. SCIC_NO_PROTOCOL,
  78. SCIC_SMP_PROTOCOL,
  79. SCIC_SSP_PROTOCOL,
  80. SCIC_STP_PROTOCOL
  81. }; /* XXX remove me, use sas_task.{dev|task_proto} instead */;
  82. /**
  83. * isci_stp_request - extra request infrastructure to handle pio/atapi protocol
  84. * @pio_len - number of bytes requested at PIO setup
  85. * @status - pio setup ending status value to tell us if we need
  86. * to wait for another fis or if the transfer is complete. Upon
  87. * receipt of a d2h fis this will be the status field of that fis.
  88. * @sgl - track pio transfer progress as we iterate through the sgl
  89. */
  90. struct isci_stp_request {
  91. u32 pio_len;
  92. u8 status;
  93. struct isci_stp_pio_sgl {
  94. int index;
  95. u8 set;
  96. u32 offset;
  97. } sgl;
  98. };
  99. struct isci_request {
  100. enum isci_request_status status;
  101. #define IREQ_COMPLETE_IN_TARGET 0
  102. #define IREQ_TERMINATED 1
  103. #define IREQ_TMF 2
  104. #define IREQ_ACTIVE 3
  105. unsigned long flags;
  106. /* XXX kill ttype and ttype_ptr, allocate full sas_task */
  107. union ttype_ptr_union {
  108. struct sas_task *io_task_ptr; /* When ttype==io_task */
  109. struct isci_tmf *tmf_task_ptr; /* When ttype==tmf_task */
  110. } ttype_ptr;
  111. struct isci_host *isci_host;
  112. /* For use in the requests_to_{complete|abort} lists: */
  113. struct list_head completed_node;
  114. /* For use in the reqs_in_process list: */
  115. struct list_head dev_node;
  116. spinlock_t state_lock;
  117. dma_addr_t request_daddr;
  118. dma_addr_t zero_scatter_daddr;
  119. unsigned int num_sg_entries;
  120. /* Note: "io_request_completion" is completed in two different ways
  121. * depending on whether this is a TMF or regular request.
  122. * - TMF requests are completed in the thread that started them;
  123. * - regular requests are completed in the request completion callback
  124. * function.
  125. * This difference in operation allows the aborter of a TMF request
  126. * to be sure that once the TMF request completes, the I/O that the
  127. * TMF was aborting is guaranteed to have completed.
  128. *
  129. * XXX kill io_request_completion
  130. */
  131. struct completion *io_request_completion;
  132. struct sci_base_state_machine sm;
  133. struct isci_host *owning_controller;
  134. struct isci_remote_device *target_device;
  135. u16 io_tag;
  136. enum sci_request_protocol protocol;
  137. u32 scu_status; /* hardware result */
  138. u32 sci_status; /* upper layer disposition */
  139. u32 post_context;
  140. struct scu_task_context *tc;
  141. /* could be larger with sg chaining */
  142. #define SCU_SGL_SIZE ((SCI_MAX_SCATTER_GATHER_ELEMENTS + 1) / 2)
  143. struct scu_sgl_element_pair sg_table[SCU_SGL_SIZE] __attribute__ ((aligned(32)));
  144. /* This field is a pointer to the stored rx frame data. It is used in
  145. * STP internal requests and SMP response frames. If this field is
  146. * non-NULL the saved frame must be released on IO request completion.
  147. */
  148. u32 saved_rx_frame_index;
  149. union {
  150. struct {
  151. union {
  152. struct ssp_cmd_iu cmd;
  153. struct ssp_task_iu tmf;
  154. };
  155. union {
  156. struct ssp_response_iu rsp;
  157. u8 rsp_buf[SSP_RESP_IU_MAX_SIZE];
  158. };
  159. } ssp;
  160. struct {
  161. struct isci_stp_request req;
  162. struct host_to_dev_fis cmd;
  163. struct dev_to_host_fis rsp;
  164. } stp;
  165. };
  166. };
  167. static inline struct isci_request *to_ireq(struct isci_stp_request *stp_req)
  168. {
  169. struct isci_request *ireq;
  170. ireq = container_of(stp_req, typeof(*ireq), stp.req);
  171. return ireq;
  172. }
  173. /**
  174. * enum sci_base_request_states - This enumeration depicts all the states for
  175. * the common request state machine.
  176. *
  177. *
  178. */
  179. enum sci_base_request_states {
  180. /*
  181. * Simply the initial state for the base request state machine.
  182. */
  183. SCI_REQ_INIT,
  184. /*
  185. * This state indicates that the request has been constructed.
  186. * This state is entered from the INITIAL state.
  187. */
  188. SCI_REQ_CONSTRUCTED,
  189. /*
  190. * This state indicates that the request has been started. This state
  191. * is entered from the CONSTRUCTED state.
  192. */
  193. SCI_REQ_STARTED,
  194. SCI_REQ_STP_UDMA_WAIT_TC_COMP,
  195. SCI_REQ_STP_UDMA_WAIT_D2H,
  196. SCI_REQ_STP_NON_DATA_WAIT_H2D,
  197. SCI_REQ_STP_NON_DATA_WAIT_D2H,
  198. SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED,
  199. SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG,
  200. SCI_REQ_STP_SOFT_RESET_WAIT_D2H,
  201. /*
  202. * While in this state the IO request object is waiting for the TC
  203. * completion notification for the H2D Register FIS
  204. */
  205. SCI_REQ_STP_PIO_WAIT_H2D,
  206. /*
  207. * While in this state the IO request object is waiting for either a
  208. * PIO Setup FIS or a D2H register FIS. The type of frame received is
  209. * based on the result of the prior frame and line conditions.
  210. */
  211. SCI_REQ_STP_PIO_WAIT_FRAME,
  212. /*
  213. * While in this state the IO request object is waiting for a DATA
  214. * frame from the device.
  215. */
  216. SCI_REQ_STP_PIO_DATA_IN,
  217. /*
  218. * While in this state the IO request object is waiting to transmit
  219. * the next data frame to the device.
  220. */
  221. SCI_REQ_STP_PIO_DATA_OUT,
  222. /*
  223. * While in this state the IO request object is waiting for the TC
  224. * completion notification for the H2D Register FIS
  225. */
  226. SCI_REQ_ATAPI_WAIT_H2D,
  227. /*
  228. * While in this state the IO request object is waiting for either a
  229. * PIO Setup.
  230. */
  231. SCI_REQ_ATAPI_WAIT_PIO_SETUP,
  232. /*
  233. * The non-data IO transit to this state in this state after receiving
  234. * TC completion. While in this state IO request object is waiting for
  235. * D2H status frame as UF.
  236. */
  237. SCI_REQ_ATAPI_WAIT_D2H,
  238. /*
  239. * When transmitting raw frames hardware reports task context completion
  240. * after every frame submission, so in the non-accelerated case we need
  241. * to expect the completion for the "cdb" frame.
  242. */
  243. SCI_REQ_ATAPI_WAIT_TC_COMP,
  244. /*
  245. * The AWAIT_TC_COMPLETION sub-state indicates that the started raw
  246. * task management request is waiting for the transmission of the
  247. * initial frame (i.e. command, task, etc.).
  248. */
  249. SCI_REQ_TASK_WAIT_TC_COMP,
  250. /*
  251. * This sub-state indicates that the started task management request
  252. * is waiting for the reception of an unsolicited frame
  253. * (i.e. response IU).
  254. */
  255. SCI_REQ_TASK_WAIT_TC_RESP,
  256. /*
  257. * This sub-state indicates that the started task management request
  258. * is waiting for the reception of an unsolicited frame
  259. * (i.e. response IU).
  260. */
  261. SCI_REQ_SMP_WAIT_RESP,
  262. /*
  263. * The AWAIT_TC_COMPLETION sub-state indicates that the started SMP
  264. * request is waiting for the transmission of the initial frame
  265. * (i.e. command, task, etc.).
  266. */
  267. SCI_REQ_SMP_WAIT_TC_COMP,
  268. /*
  269. * This state indicates that the request has completed.
  270. * This state is entered from the STARTED state. This state is entered
  271. * from the ABORTING state.
  272. */
  273. SCI_REQ_COMPLETED,
  274. /*
  275. * This state indicates that the request is in the process of being
  276. * terminated/aborted.
  277. * This state is entered from the CONSTRUCTED state.
  278. * This state is entered from the STARTED state.
  279. */
  280. SCI_REQ_ABORTING,
  281. /*
  282. * Simply the final state for the base request state machine.
  283. */
  284. SCI_REQ_FINAL,
  285. };
  286. enum sci_status sci_request_start(struct isci_request *ireq);
  287. enum sci_status sci_io_request_terminate(struct isci_request *ireq);
  288. enum sci_status
  289. sci_io_request_event_handler(struct isci_request *ireq,
  290. u32 event_code);
  291. enum sci_status
  292. sci_io_request_frame_handler(struct isci_request *ireq,
  293. u32 frame_index);
  294. enum sci_status
  295. sci_task_request_terminate(struct isci_request *ireq);
  296. extern enum sci_status
  297. sci_request_complete(struct isci_request *ireq);
  298. extern enum sci_status
  299. sci_io_request_tc_completion(struct isci_request *ireq, u32 code);
  300. /* XXX open code in caller */
  301. static inline dma_addr_t
  302. sci_io_request_get_dma_addr(struct isci_request *ireq, void *virt_addr)
  303. {
  304. char *requested_addr = (char *)virt_addr;
  305. char *base_addr = (char *)ireq;
  306. BUG_ON(requested_addr < base_addr);
  307. BUG_ON((requested_addr - base_addr) >= sizeof(*ireq));
  308. return ireq->request_daddr + (requested_addr - base_addr);
  309. }
  310. /**
  311. * isci_request_change_state() - This function sets the status of the request
  312. * object.
  313. * @request: This parameter points to the isci_request object
  314. * @status: This Parameter is the new status of the object
  315. *
  316. */
  317. static inline enum isci_request_status
  318. isci_request_change_state(struct isci_request *isci_request,
  319. enum isci_request_status status)
  320. {
  321. enum isci_request_status old_state;
  322. unsigned long flags;
  323. dev_dbg(&isci_request->isci_host->pdev->dev,
  324. "%s: isci_request = %p, state = 0x%x\n",
  325. __func__,
  326. isci_request,
  327. status);
  328. BUG_ON(isci_request == NULL);
  329. spin_lock_irqsave(&isci_request->state_lock, flags);
  330. old_state = isci_request->status;
  331. isci_request->status = status;
  332. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  333. return old_state;
  334. }
  335. /**
  336. * isci_request_change_started_to_newstate() - This function sets the status of
  337. * the request object.
  338. * @request: This parameter points to the isci_request object
  339. * @status: This Parameter is the new status of the object
  340. *
  341. * state previous to any change.
  342. */
  343. static inline enum isci_request_status
  344. isci_request_change_started_to_newstate(struct isci_request *isci_request,
  345. struct completion *completion_ptr,
  346. enum isci_request_status newstate)
  347. {
  348. enum isci_request_status old_state;
  349. unsigned long flags;
  350. spin_lock_irqsave(&isci_request->state_lock, flags);
  351. old_state = isci_request->status;
  352. if (old_state == started || old_state == aborting) {
  353. BUG_ON(isci_request->io_request_completion != NULL);
  354. isci_request->io_request_completion = completion_ptr;
  355. isci_request->status = newstate;
  356. }
  357. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  358. dev_dbg(&isci_request->isci_host->pdev->dev,
  359. "%s: isci_request = %p, old_state = 0x%x\n",
  360. __func__,
  361. isci_request,
  362. old_state);
  363. return old_state;
  364. }
  365. /**
  366. * isci_request_change_started_to_aborted() - This function sets the status of
  367. * the request object.
  368. * @request: This parameter points to the isci_request object
  369. * @completion_ptr: This parameter is saved as the kernel completion structure
  370. * signalled when the old request completes.
  371. *
  372. * state previous to any change.
  373. */
  374. static inline enum isci_request_status
  375. isci_request_change_started_to_aborted(struct isci_request *isci_request,
  376. struct completion *completion_ptr)
  377. {
  378. return isci_request_change_started_to_newstate(isci_request,
  379. completion_ptr,
  380. aborted);
  381. }
  382. #define isci_request_access_task(req) ((req)->ttype_ptr.io_task_ptr)
  383. #define isci_request_access_tmf(req) ((req)->ttype_ptr.tmf_task_ptr)
  384. struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
  385. struct isci_tmf *isci_tmf,
  386. u16 tag);
  387. int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
  388. struct sas_task *task, u16 tag);
  389. void isci_terminate_pending_requests(struct isci_host *ihost,
  390. struct isci_remote_device *idev);
  391. enum sci_status
  392. sci_task_request_construct(struct isci_host *ihost,
  393. struct isci_remote_device *idev,
  394. u16 io_tag,
  395. struct isci_request *ireq);
  396. enum sci_status
  397. sci_task_request_construct_ssp(struct isci_request *ireq);
  398. enum sci_status
  399. sci_task_request_construct_sata(struct isci_request *ireq);
  400. void sci_smp_request_copy_response(struct isci_request *ireq);
  401. static inline int isci_task_is_ncq_recovery(struct sas_task *task)
  402. {
  403. return (sas_protocol_ata(task->task_proto) &&
  404. task->ata_task.fis.command == ATA_CMD_READ_LOG_EXT &&
  405. task->ata_task.fis.lbal == ATA_LOG_SATA_NCQ);
  406. }
  407. #endif /* !defined(_ISCI_REQUEST_H_) */