request.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 task_type {
  77. io_task = 0,
  78. tmf_task = 1
  79. };
  80. enum sci_request_protocol {
  81. SCIC_NO_PROTOCOL,
  82. SCIC_SMP_PROTOCOL,
  83. SCIC_SSP_PROTOCOL,
  84. SCIC_STP_PROTOCOL
  85. }; /* XXX remove me, use sas_task.{dev|task_proto} instead */;
  86. /**
  87. * isci_stp_request - extra request infrastructure to handle pio/atapi protocol
  88. * @pio_len - number of bytes requested at PIO setup
  89. * @status - pio setup ending status value to tell us if we need
  90. * to wait for another fis or if the transfer is complete. Upon
  91. * receipt of a d2h fis this will be the status field of that fis.
  92. * @sgl - track pio transfer progress as we iterate through the sgl
  93. * @device_cdb_len - atapi device advertises it's transfer constraints at setup
  94. */
  95. struct isci_stp_request {
  96. u32 pio_len;
  97. u8 status;
  98. struct isci_stp_pio_sgl {
  99. int index;
  100. u8 set;
  101. u32 offset;
  102. } sgl;
  103. u32 device_cdb_len;
  104. };
  105. struct isci_request {
  106. enum isci_request_status status;
  107. #define IREQ_COMPLETE_IN_TARGET 0
  108. #define IREQ_TERMINATED 1
  109. #define IREQ_TMF 2
  110. #define IREQ_ACTIVE 3
  111. unsigned long flags;
  112. /* XXX kill ttype and ttype_ptr, allocate full sas_task */
  113. enum task_type ttype;
  114. union ttype_ptr_union {
  115. struct sas_task *io_task_ptr; /* When ttype==io_task */
  116. struct isci_tmf *tmf_task_ptr; /* When ttype==tmf_task */
  117. } ttype_ptr;
  118. struct isci_host *isci_host;
  119. /* For use in the requests_to_{complete|abort} lists: */
  120. struct list_head completed_node;
  121. /* For use in the reqs_in_process list: */
  122. struct list_head dev_node;
  123. spinlock_t state_lock;
  124. dma_addr_t request_daddr;
  125. dma_addr_t zero_scatter_daddr;
  126. unsigned int num_sg_entries;
  127. /* Note: "io_request_completion" is completed in two different ways
  128. * depending on whether this is a TMF or regular request.
  129. * - TMF requests are completed in the thread that started them;
  130. * - regular requests are completed in the request completion callback
  131. * function.
  132. * This difference in operation allows the aborter of a TMF request
  133. * to be sure that once the TMF request completes, the I/O that the
  134. * TMF was aborting is guaranteed to have completed.
  135. *
  136. * XXX kill io_request_completion
  137. */
  138. struct completion *io_request_completion;
  139. struct sci_base_state_machine sm;
  140. struct isci_host *owning_controller;
  141. struct isci_remote_device *target_device;
  142. u16 io_tag;
  143. enum sci_request_protocol protocol;
  144. u32 scu_status; /* hardware result */
  145. u32 sci_status; /* upper layer disposition */
  146. u32 post_context;
  147. struct scu_task_context *tc;
  148. /* could be larger with sg chaining */
  149. #define SCU_SGL_SIZE ((SCI_MAX_SCATTER_GATHER_ELEMENTS + 1) / 2)
  150. struct scu_sgl_element_pair sg_table[SCU_SGL_SIZE] __attribute__ ((aligned(32)));
  151. /* This field is a pointer to the stored rx frame data. It is used in
  152. * STP internal requests and SMP response frames. If this field is
  153. * non-NULL the saved frame must be released on IO request completion.
  154. */
  155. u32 saved_rx_frame_index;
  156. union {
  157. struct {
  158. union {
  159. struct ssp_cmd_iu cmd;
  160. struct ssp_task_iu tmf;
  161. };
  162. union {
  163. struct ssp_response_iu rsp;
  164. u8 rsp_buf[SSP_RESP_IU_MAX_SIZE];
  165. };
  166. } ssp;
  167. struct {
  168. struct smp_resp rsp;
  169. } smp;
  170. struct {
  171. struct isci_stp_request req;
  172. struct host_to_dev_fis cmd;
  173. struct dev_to_host_fis rsp;
  174. } stp;
  175. };
  176. };
  177. static inline struct isci_request *to_ireq(struct isci_stp_request *stp_req)
  178. {
  179. struct isci_request *ireq;
  180. ireq = container_of(stp_req, typeof(*ireq), stp.req);
  181. return ireq;
  182. }
  183. /**
  184. * enum sci_base_request_states - This enumeration depicts all the states for
  185. * the common request state machine.
  186. *
  187. *
  188. */
  189. enum sci_base_request_states {
  190. /*
  191. * Simply the initial state for the base request state machine.
  192. */
  193. SCI_REQ_INIT,
  194. /*
  195. * This state indicates that the request has been constructed.
  196. * This state is entered from the INITIAL state.
  197. */
  198. SCI_REQ_CONSTRUCTED,
  199. /*
  200. * This state indicates that the request has been started. This state
  201. * is entered from the CONSTRUCTED state.
  202. */
  203. SCI_REQ_STARTED,
  204. SCI_REQ_STP_UDMA_WAIT_TC_COMP,
  205. SCI_REQ_STP_UDMA_WAIT_D2H,
  206. SCI_REQ_STP_NON_DATA_WAIT_H2D,
  207. SCI_REQ_STP_NON_DATA_WAIT_D2H,
  208. SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED,
  209. SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG,
  210. SCI_REQ_STP_SOFT_RESET_WAIT_D2H,
  211. /*
  212. * While in this state the IO request object is waiting for the TC
  213. * completion notification for the H2D Register FIS
  214. */
  215. SCI_REQ_STP_PIO_WAIT_H2D,
  216. /*
  217. * While in this state the IO request object is waiting for either a
  218. * PIO Setup FIS or a D2H register FIS. The type of frame received is
  219. * based on the result of the prior frame and line conditions.
  220. */
  221. SCI_REQ_STP_PIO_WAIT_FRAME,
  222. /*
  223. * While in this state the IO request object is waiting for a DATA
  224. * frame from the device.
  225. */
  226. SCI_REQ_STP_PIO_DATA_IN,
  227. /*
  228. * While in this state the IO request object is waiting to transmit
  229. * the next data frame to the device.
  230. */
  231. SCI_REQ_STP_PIO_DATA_OUT,
  232. /*
  233. * The AWAIT_TC_COMPLETION sub-state indicates that the started raw
  234. * task management request is waiting for the transmission of the
  235. * initial frame (i.e. command, task, etc.).
  236. */
  237. SCI_REQ_TASK_WAIT_TC_COMP,
  238. /*
  239. * This sub-state indicates that the started task management request
  240. * is waiting for the reception of an unsolicited frame
  241. * (i.e. response IU).
  242. */
  243. SCI_REQ_TASK_WAIT_TC_RESP,
  244. /*
  245. * This sub-state indicates that the started task management request
  246. * is waiting for the reception of an unsolicited frame
  247. * (i.e. response IU).
  248. */
  249. SCI_REQ_SMP_WAIT_RESP,
  250. /*
  251. * The AWAIT_TC_COMPLETION sub-state indicates that the started SMP
  252. * request is waiting for the transmission of the initial frame
  253. * (i.e. command, task, etc.).
  254. */
  255. SCI_REQ_SMP_WAIT_TC_COMP,
  256. /*
  257. * This state indicates that the request has completed.
  258. * This state is entered from the STARTED state. This state is entered
  259. * from the ABORTING state.
  260. */
  261. SCI_REQ_COMPLETED,
  262. /*
  263. * This state indicates that the request is in the process of being
  264. * terminated/aborted.
  265. * This state is entered from the CONSTRUCTED state.
  266. * This state is entered from the STARTED state.
  267. */
  268. SCI_REQ_ABORTING,
  269. /*
  270. * Simply the final state for the base request state machine.
  271. */
  272. SCI_REQ_FINAL,
  273. };
  274. /**
  275. * scic_sds_request_get_controller() -
  276. *
  277. * This macro will return the controller for this io request object
  278. */
  279. #define scic_sds_request_get_controller(ireq) \
  280. ((ireq)->owning_controller)
  281. /**
  282. * scic_sds_request_get_device() -
  283. *
  284. * This macro will return the device for this io request object
  285. */
  286. #define scic_sds_request_get_device(ireq) \
  287. ((ireq)->target_device)
  288. /**
  289. * scic_sds_request_get_port() -
  290. *
  291. * This macro will return the port for this io request object
  292. */
  293. #define scic_sds_request_get_port(ireq) \
  294. scic_sds_remote_device_get_port(scic_sds_request_get_device(ireq))
  295. /**
  296. * scic_sds_request_get_post_context() -
  297. *
  298. * This macro returns the constructed post context result for the io request.
  299. */
  300. #define scic_sds_request_get_post_context(ireq) \
  301. ((ireq)->post_context)
  302. /**
  303. * scic_sds_request_get_task_context() -
  304. *
  305. * This is a helper macro to return the os handle for this request object.
  306. */
  307. #define scic_sds_request_get_task_context(request) \
  308. ((request)->task_context_buffer)
  309. /**
  310. * scic_sds_request_set_status() -
  311. *
  312. * This macro will set the scu hardware status and sci request completion
  313. * status for an io request.
  314. */
  315. #define scic_sds_request_set_status(request, scu_status_code, sci_status_code) \
  316. { \
  317. (request)->scu_status = (scu_status_code); \
  318. (request)->sci_status = (sci_status_code); \
  319. }
  320. enum sci_status scic_sds_request_start(struct isci_request *ireq);
  321. enum sci_status scic_sds_io_request_terminate(struct isci_request *ireq);
  322. enum sci_status
  323. scic_sds_io_request_event_handler(struct isci_request *ireq,
  324. u32 event_code);
  325. enum sci_status
  326. scic_sds_io_request_frame_handler(struct isci_request *ireq,
  327. u32 frame_index);
  328. enum sci_status
  329. scic_sds_task_request_terminate(struct isci_request *ireq);
  330. extern enum sci_status
  331. scic_sds_request_complete(struct isci_request *ireq);
  332. extern enum sci_status
  333. scic_sds_io_request_tc_completion(struct isci_request *ireq, u32 code);
  334. /* XXX open code in caller */
  335. static inline dma_addr_t
  336. scic_io_request_get_dma_addr(struct isci_request *ireq, void *virt_addr)
  337. {
  338. char *requested_addr = (char *)virt_addr;
  339. char *base_addr = (char *)ireq;
  340. BUG_ON(requested_addr < base_addr);
  341. BUG_ON((requested_addr - base_addr) >= sizeof(*ireq));
  342. return ireq->request_daddr + (requested_addr - base_addr);
  343. }
  344. /**
  345. * This function gets the status of the request object.
  346. * @request: This parameter points to the isci_request object
  347. *
  348. * status of the object as a isci_request_status enum.
  349. */
  350. static inline enum isci_request_status
  351. isci_request_get_state(struct isci_request *isci_request)
  352. {
  353. BUG_ON(isci_request == NULL);
  354. /*probably a bad sign... */
  355. if (isci_request->status == unallocated)
  356. dev_warn(&isci_request->isci_host->pdev->dev,
  357. "%s: isci_request->status == unallocated\n",
  358. __func__);
  359. return isci_request->status;
  360. }
  361. /**
  362. * isci_request_change_state() - This function sets the status of the request
  363. * object.
  364. * @request: This parameter points to the isci_request object
  365. * @status: This Parameter is the new status of the object
  366. *
  367. */
  368. static inline enum isci_request_status
  369. isci_request_change_state(struct isci_request *isci_request,
  370. enum isci_request_status status)
  371. {
  372. enum isci_request_status old_state;
  373. unsigned long flags;
  374. dev_dbg(&isci_request->isci_host->pdev->dev,
  375. "%s: isci_request = %p, state = 0x%x\n",
  376. __func__,
  377. isci_request,
  378. status);
  379. BUG_ON(isci_request == NULL);
  380. spin_lock_irqsave(&isci_request->state_lock, flags);
  381. old_state = isci_request->status;
  382. isci_request->status = status;
  383. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  384. return old_state;
  385. }
  386. /**
  387. * isci_request_change_started_to_newstate() - This function sets the status of
  388. * the request object.
  389. * @request: This parameter points to the isci_request object
  390. * @status: This Parameter is the new status of the object
  391. *
  392. * state previous to any change.
  393. */
  394. static inline enum isci_request_status
  395. isci_request_change_started_to_newstate(struct isci_request *isci_request,
  396. struct completion *completion_ptr,
  397. enum isci_request_status newstate)
  398. {
  399. enum isci_request_status old_state;
  400. unsigned long flags;
  401. spin_lock_irqsave(&isci_request->state_lock, flags);
  402. old_state = isci_request->status;
  403. if (old_state == started || old_state == aborting) {
  404. BUG_ON(isci_request->io_request_completion != NULL);
  405. isci_request->io_request_completion = completion_ptr;
  406. isci_request->status = newstate;
  407. }
  408. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  409. dev_dbg(&isci_request->isci_host->pdev->dev,
  410. "%s: isci_request = %p, old_state = 0x%x\n",
  411. __func__,
  412. isci_request,
  413. old_state);
  414. return old_state;
  415. }
  416. /**
  417. * isci_request_change_started_to_aborted() - This function sets the status of
  418. * the request object.
  419. * @request: This parameter points to the isci_request object
  420. * @completion_ptr: This parameter is saved as the kernel completion structure
  421. * signalled when the old request completes.
  422. *
  423. * state previous to any change.
  424. */
  425. static inline enum isci_request_status
  426. isci_request_change_started_to_aborted(struct isci_request *isci_request,
  427. struct completion *completion_ptr)
  428. {
  429. return isci_request_change_started_to_newstate(isci_request,
  430. completion_ptr,
  431. aborted);
  432. }
  433. #define isci_request_access_task(req) ((req)->ttype_ptr.io_task_ptr)
  434. #define isci_request_access_tmf(req) ((req)->ttype_ptr.tmf_task_ptr)
  435. struct isci_request *isci_tmf_request_from_tag(struct isci_host *ihost,
  436. struct isci_tmf *isci_tmf,
  437. u16 tag);
  438. int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
  439. struct sas_task *task, u16 tag);
  440. void isci_terminate_pending_requests(struct isci_host *ihost,
  441. struct isci_remote_device *idev);
  442. enum sci_status
  443. scic_task_request_construct(struct isci_host *ihost,
  444. struct isci_remote_device *idev,
  445. u16 io_tag,
  446. struct isci_request *ireq);
  447. enum sci_status
  448. scic_task_request_construct_ssp(struct isci_request *ireq);
  449. enum sci_status
  450. scic_task_request_construct_sata(struct isci_request *ireq);
  451. void
  452. scic_stp_io_request_set_ncq_tag(struct isci_request *ireq, u16 ncq_tag);
  453. void scic_sds_smp_request_copy_response(struct isci_request *ireq);
  454. static inline int isci_task_is_ncq_recovery(struct sas_task *task)
  455. {
  456. return (sas_protocol_ata(task->task_proto) &&
  457. task->ata_task.fis.command == ATA_CMD_READ_LOG_EXT &&
  458. task->ata_task.fis.lbal == ATA_LOG_SATA_NCQ);
  459. }
  460. #endif /* !defined(_ISCI_REQUEST_H_) */