request.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. struct scic_sds_stp_request {
  87. union {
  88. u32 ncq;
  89. u32 udma;
  90. struct scic_sds_stp_pio_request {
  91. /*
  92. * Total transfer for the entire PIO request recorded
  93. * at request constuction time.
  94. *
  95. * @todo Should we just decrement this value for each
  96. * byte of data transitted or received to elemenate
  97. * the current_transfer_bytes field?
  98. */
  99. u32 total_transfer_bytes;
  100. /*
  101. * Total number of bytes received/transmitted in data
  102. * frames since the start of the IO request. At the
  103. * end of the IO request this should equal the
  104. * total_transfer_bytes.
  105. */
  106. u32 current_transfer_bytes;
  107. /*
  108. * The number of bytes requested in the in the PIO
  109. * setup.
  110. */
  111. u32 pio_transfer_bytes;
  112. /*
  113. * PIO Setup ending status value to tell us if we need
  114. * to wait for another FIS or if the transfer is
  115. * complete. On the receipt of a D2H FIS this will be
  116. * the status field of that FIS.
  117. */
  118. u8 ending_status;
  119. /*
  120. * On receipt of a D2H FIS this will be the ending
  121. * error field if the ending_status has the
  122. * SATA_STATUS_ERR bit set.
  123. */
  124. u8 ending_error;
  125. struct scic_sds_request_pio_sgl {
  126. int sgl_index;
  127. u8 sgl_set;
  128. u32 sgl_offset;
  129. } request_current;
  130. } pio;
  131. struct {
  132. /*
  133. * The number of bytes requested in the PIO setup
  134. * before CDB data frame.
  135. */
  136. u32 device_preferred_cdb_length;
  137. } packet;
  138. } type;
  139. };
  140. struct scic_sds_request {
  141. /*
  142. * This field contains the information for the base request state
  143. * machine.
  144. */
  145. struct sci_base_state_machine sm;
  146. /*
  147. * This field simply points to the controller to which this IO request
  148. * is associated.
  149. */
  150. struct scic_sds_controller *owning_controller;
  151. /*
  152. * This field simply points to the remote device to which this IO
  153. * request is associated.
  154. */
  155. struct scic_sds_remote_device *target_device;
  156. /*
  157. * This field indicates the IO tag for this request. The IO tag is
  158. * comprised of the task_index and a sequence count. The sequence count
  159. * is utilized to help identify tasks from one life to another.
  160. */
  161. u16 io_tag;
  162. /*
  163. * This field specifies the protocol being utilized for this
  164. * IO request.
  165. */
  166. enum sci_request_protocol protocol;
  167. /*
  168. * This field indicates the completion status taken from the SCUs
  169. * completion code. It indicates the completion result for the SCU
  170. * hardware.
  171. */
  172. u32 scu_status;
  173. /*
  174. * This field indicates the completion status returned to the SCI user.
  175. * It indicates the users view of the io request completion.
  176. */
  177. u32 sci_status;
  178. /*
  179. * This field contains the value to be utilized when posting
  180. * (e.g. Post_TC, * Post_TC_Abort) this request to the silicon.
  181. */
  182. u32 post_context;
  183. struct scu_task_context *tc;
  184. /* could be larger with sg chaining */
  185. #define SCU_SGL_SIZE ((SCI_MAX_SCATTER_GATHER_ELEMENTS + 1) / 2)
  186. struct scu_sgl_element_pair sg_table[SCU_SGL_SIZE] __attribute__ ((aligned(32)));
  187. /*
  188. * This field is a pointer to the stored rx frame data. It is used in
  189. * STP internal requests and SMP response frames. If this field is
  190. * non-NULL the saved frame must be released on IO request completion.
  191. *
  192. * @todo In the future do we want to keep a list of RX frame buffers?
  193. */
  194. u32 saved_rx_frame_index;
  195. union {
  196. struct {
  197. union {
  198. struct ssp_cmd_iu cmd;
  199. struct ssp_task_iu tmf;
  200. };
  201. union {
  202. struct ssp_response_iu rsp;
  203. u8 rsp_buf[SSP_RESP_IU_MAX_SIZE];
  204. };
  205. } ssp;
  206. struct {
  207. struct smp_resp rsp;
  208. } smp;
  209. struct {
  210. struct scic_sds_stp_request req;
  211. struct host_to_dev_fis cmd;
  212. struct dev_to_host_fis rsp;
  213. } stp;
  214. };
  215. };
  216. static inline struct scic_sds_request *to_sci_req(struct scic_sds_stp_request *stp_req)
  217. {
  218. struct scic_sds_request *sci_req;
  219. sci_req = container_of(stp_req, typeof(*sci_req), stp.req);
  220. return sci_req;
  221. }
  222. struct isci_request {
  223. enum isci_request_status status;
  224. enum task_type ttype;
  225. unsigned short io_tag;
  226. #define IREQ_COMPLETE_IN_TARGET 0
  227. #define IREQ_TERMINATED 1
  228. #define IREQ_TMF 2
  229. unsigned long flags;
  230. union ttype_ptr_union {
  231. struct sas_task *io_task_ptr; /* When ttype==io_task */
  232. struct isci_tmf *tmf_task_ptr; /* When ttype==tmf_task */
  233. } ttype_ptr;
  234. struct isci_host *isci_host;
  235. /* For use in the requests_to_{complete|abort} lists: */
  236. struct list_head completed_node;
  237. /* For use in the reqs_in_process list: */
  238. struct list_head dev_node;
  239. spinlock_t state_lock;
  240. dma_addr_t request_daddr;
  241. dma_addr_t zero_scatter_daddr;
  242. unsigned int num_sg_entries; /* returned by pci_alloc_sg */
  243. /** Note: "io_request_completion" is completed in two different ways
  244. * depending on whether this is a TMF or regular request.
  245. * - TMF requests are completed in the thread that started them;
  246. * - regular requests are completed in the request completion callback
  247. * function.
  248. * This difference in operation allows the aborter of a TMF request
  249. * to be sure that once the TMF request completes, the I/O that the
  250. * TMF was aborting is guaranteed to have completed.
  251. */
  252. struct completion *io_request_completion;
  253. struct scic_sds_request sci;
  254. };
  255. static inline struct isci_request *sci_req_to_ireq(struct scic_sds_request *sci_req)
  256. {
  257. struct isci_request *ireq = container_of(sci_req, typeof(*ireq), sci);
  258. return ireq;
  259. }
  260. /**
  261. * enum sci_base_request_states - This enumeration depicts all the states for
  262. * the common request state machine.
  263. *
  264. *
  265. */
  266. enum sci_base_request_states {
  267. /*
  268. * Simply the initial state for the base request state machine.
  269. */
  270. SCI_REQ_INIT,
  271. /*
  272. * This state indicates that the request has been constructed.
  273. * This state is entered from the INITIAL state.
  274. */
  275. SCI_REQ_CONSTRUCTED,
  276. /*
  277. * This state indicates that the request has been started. This state
  278. * is entered from the CONSTRUCTED state.
  279. */
  280. SCI_REQ_STARTED,
  281. SCI_REQ_STP_UDMA_WAIT_TC_COMP,
  282. SCI_REQ_STP_UDMA_WAIT_D2H,
  283. SCI_REQ_STP_NON_DATA_WAIT_H2D,
  284. SCI_REQ_STP_NON_DATA_WAIT_D2H,
  285. SCI_REQ_STP_SOFT_RESET_WAIT_H2D_ASSERTED,
  286. SCI_REQ_STP_SOFT_RESET_WAIT_H2D_DIAG,
  287. SCI_REQ_STP_SOFT_RESET_WAIT_D2H,
  288. /*
  289. * While in this state the IO request object is waiting for the TC
  290. * completion notification for the H2D Register FIS
  291. */
  292. SCI_REQ_STP_PIO_WAIT_H2D,
  293. /*
  294. * While in this state the IO request object is waiting for either a
  295. * PIO Setup FIS or a D2H register FIS. The type of frame received is
  296. * based on the result of the prior frame and line conditions.
  297. */
  298. SCI_REQ_STP_PIO_WAIT_FRAME,
  299. /*
  300. * While in this state the IO request object is waiting for a DATA
  301. * frame from the device.
  302. */
  303. SCI_REQ_STP_PIO_DATA_IN,
  304. /*
  305. * While in this state the IO request object is waiting to transmit
  306. * the next data frame to the device.
  307. */
  308. SCI_REQ_STP_PIO_DATA_OUT,
  309. /*
  310. * The AWAIT_TC_COMPLETION sub-state indicates that the started raw
  311. * task management request is waiting for the transmission of the
  312. * initial frame (i.e. command, task, etc.).
  313. */
  314. SCI_REQ_TASK_WAIT_TC_COMP,
  315. /*
  316. * This sub-state indicates that the started task management request
  317. * is waiting for the reception of an unsolicited frame
  318. * (i.e. response IU).
  319. */
  320. SCI_REQ_TASK_WAIT_TC_RESP,
  321. /*
  322. * This sub-state indicates that the started task management request
  323. * is waiting for the reception of an unsolicited frame
  324. * (i.e. response IU).
  325. */
  326. SCI_REQ_SMP_WAIT_RESP,
  327. /*
  328. * The AWAIT_TC_COMPLETION sub-state indicates that the started SMP
  329. * request is waiting for the transmission of the initial frame
  330. * (i.e. command, task, etc.).
  331. */
  332. SCI_REQ_SMP_WAIT_TC_COMP,
  333. /*
  334. * This state indicates that the request has completed.
  335. * This state is entered from the STARTED state. This state is entered
  336. * from the ABORTING state.
  337. */
  338. SCI_REQ_COMPLETED,
  339. /*
  340. * This state indicates that the request is in the process of being
  341. * terminated/aborted.
  342. * This state is entered from the CONSTRUCTED state.
  343. * This state is entered from the STARTED state.
  344. */
  345. SCI_REQ_ABORTING,
  346. /*
  347. * Simply the final state for the base request state machine.
  348. */
  349. SCI_REQ_FINAL,
  350. };
  351. /**
  352. * scic_sds_request_get_controller() -
  353. *
  354. * This macro will return the controller for this io request object
  355. */
  356. #define scic_sds_request_get_controller(sci_req) \
  357. ((sci_req)->owning_controller)
  358. /**
  359. * scic_sds_request_get_device() -
  360. *
  361. * This macro will return the device for this io request object
  362. */
  363. #define scic_sds_request_get_device(sci_req) \
  364. ((sci_req)->target_device)
  365. /**
  366. * scic_sds_request_get_port() -
  367. *
  368. * This macro will return the port for this io request object
  369. */
  370. #define scic_sds_request_get_port(sci_req) \
  371. scic_sds_remote_device_get_port(scic_sds_request_get_device(sci_req))
  372. /**
  373. * scic_sds_request_get_post_context() -
  374. *
  375. * This macro returns the constructed post context result for the io request.
  376. */
  377. #define scic_sds_request_get_post_context(sci_req) \
  378. ((sci_req)->post_context)
  379. /**
  380. * scic_sds_request_get_task_context() -
  381. *
  382. * This is a helper macro to return the os handle for this request object.
  383. */
  384. #define scic_sds_request_get_task_context(request) \
  385. ((request)->task_context_buffer)
  386. /**
  387. * scic_sds_request_set_status() -
  388. *
  389. * This macro will set the scu hardware status and sci request completion
  390. * status for an io request.
  391. */
  392. #define scic_sds_request_set_status(request, scu_status_code, sci_status_code) \
  393. { \
  394. (request)->scu_status = (scu_status_code); \
  395. (request)->sci_status = (sci_status_code); \
  396. }
  397. enum sci_status scic_sds_request_start(struct scic_sds_request *sci_req);
  398. enum sci_status scic_sds_io_request_terminate(struct scic_sds_request *sci_req);
  399. enum sci_status
  400. scic_sds_io_request_event_handler(struct scic_sds_request *sci_req,
  401. u32 event_code);
  402. enum sci_status
  403. scic_sds_io_request_frame_handler(struct scic_sds_request *sci_req,
  404. u32 frame_index);
  405. enum sci_status
  406. scic_sds_task_request_terminate(struct scic_sds_request *sci_req);
  407. extern enum sci_status
  408. scic_sds_request_complete(struct scic_sds_request *sci_req);
  409. extern enum sci_status
  410. scic_sds_io_request_tc_completion(struct scic_sds_request *sci_req, u32 code);
  411. /* XXX open code in caller */
  412. static inline dma_addr_t
  413. scic_io_request_get_dma_addr(struct scic_sds_request *sci_req, void *virt_addr)
  414. {
  415. struct isci_request *ireq = sci_req_to_ireq(sci_req);
  416. char *requested_addr = (char *)virt_addr;
  417. char *base_addr = (char *)ireq;
  418. BUG_ON(requested_addr < base_addr);
  419. BUG_ON((requested_addr - base_addr) >= sizeof(*ireq));
  420. return ireq->request_daddr + (requested_addr - base_addr);
  421. }
  422. /**
  423. * This function gets the status of the request object.
  424. * @request: This parameter points to the isci_request object
  425. *
  426. * status of the object as a isci_request_status enum.
  427. */
  428. static inline enum isci_request_status
  429. isci_request_get_state(struct isci_request *isci_request)
  430. {
  431. BUG_ON(isci_request == NULL);
  432. /*probably a bad sign... */
  433. if (isci_request->status == unallocated)
  434. dev_warn(&isci_request->isci_host->pdev->dev,
  435. "%s: isci_request->status == unallocated\n",
  436. __func__);
  437. return isci_request->status;
  438. }
  439. /**
  440. * isci_request_change_state() - This function sets the status of the request
  441. * object.
  442. * @request: This parameter points to the isci_request object
  443. * @status: This Parameter is the new status of the object
  444. *
  445. */
  446. static inline enum isci_request_status
  447. isci_request_change_state(struct isci_request *isci_request,
  448. enum isci_request_status status)
  449. {
  450. enum isci_request_status old_state;
  451. unsigned long flags;
  452. dev_dbg(&isci_request->isci_host->pdev->dev,
  453. "%s: isci_request = %p, state = 0x%x\n",
  454. __func__,
  455. isci_request,
  456. status);
  457. BUG_ON(isci_request == NULL);
  458. spin_lock_irqsave(&isci_request->state_lock, flags);
  459. old_state = isci_request->status;
  460. isci_request->status = status;
  461. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  462. return old_state;
  463. }
  464. /**
  465. * isci_request_change_started_to_newstate() - This function sets the status of
  466. * the request object.
  467. * @request: This parameter points to the isci_request object
  468. * @status: This Parameter is the new status of the object
  469. *
  470. * state previous to any change.
  471. */
  472. static inline enum isci_request_status
  473. isci_request_change_started_to_newstate(struct isci_request *isci_request,
  474. struct completion *completion_ptr,
  475. enum isci_request_status newstate)
  476. {
  477. enum isci_request_status old_state;
  478. unsigned long flags;
  479. spin_lock_irqsave(&isci_request->state_lock, flags);
  480. old_state = isci_request->status;
  481. if (old_state == started || old_state == aborting) {
  482. BUG_ON(isci_request->io_request_completion != NULL);
  483. isci_request->io_request_completion = completion_ptr;
  484. isci_request->status = newstate;
  485. }
  486. spin_unlock_irqrestore(&isci_request->state_lock, flags);
  487. dev_dbg(&isci_request->isci_host->pdev->dev,
  488. "%s: isci_request = %p, old_state = 0x%x\n",
  489. __func__,
  490. isci_request,
  491. old_state);
  492. return old_state;
  493. }
  494. /**
  495. * isci_request_change_started_to_aborted() - This function sets the status of
  496. * the request object.
  497. * @request: This parameter points to the isci_request object
  498. * @completion_ptr: This parameter is saved as the kernel completion structure
  499. * signalled when the old request completes.
  500. *
  501. * state previous to any change.
  502. */
  503. static inline enum isci_request_status
  504. isci_request_change_started_to_aborted(struct isci_request *isci_request,
  505. struct completion *completion_ptr)
  506. {
  507. return isci_request_change_started_to_newstate(isci_request,
  508. completion_ptr,
  509. aborted);
  510. }
  511. /**
  512. * isci_request_free() - This function frees the request object.
  513. * @isci_host: This parameter specifies the ISCI host object
  514. * @isci_request: This parameter points to the isci_request object
  515. *
  516. */
  517. static inline void isci_request_free(struct isci_host *isci_host,
  518. struct isci_request *isci_request)
  519. {
  520. if (!isci_request)
  521. return;
  522. /* release the dma memory if we fail. */
  523. dma_pool_free(isci_host->dma_pool,
  524. isci_request,
  525. isci_request->request_daddr);
  526. }
  527. #define isci_request_access_task(req) ((req)->ttype_ptr.io_task_ptr)
  528. #define isci_request_access_tmf(req) ((req)->ttype_ptr.tmf_task_ptr)
  529. struct isci_request *isci_request_alloc_tmf(struct isci_host *ihost,
  530. struct isci_tmf *isci_tmf,
  531. gfp_t gfp_flags);
  532. int isci_request_execute(struct isci_host *ihost, struct isci_remote_device *idev,
  533. struct sas_task *task, u16 tag, gfp_t gfp_flags);
  534. void isci_terminate_pending_requests(struct isci_host *ihost,
  535. struct isci_remote_device *idev);
  536. enum sci_status
  537. scic_task_request_construct(struct scic_sds_controller *scic,
  538. struct scic_sds_remote_device *sci_dev,
  539. u16 io_tag,
  540. struct scic_sds_request *sci_req);
  541. enum sci_status
  542. scic_task_request_construct_ssp(struct scic_sds_request *sci_req);
  543. enum sci_status
  544. scic_task_request_construct_sata(struct scic_sds_request *sci_req);
  545. void
  546. scic_stp_io_request_set_ncq_tag(struct scic_sds_request *sci_req, u16 ncq_tag);
  547. void scic_sds_smp_request_copy_response(struct scic_sds_request *sci_req);
  548. static inline int isci_task_is_ncq_recovery(struct sas_task *task)
  549. {
  550. return (sas_protocol_ata(task->task_proto) &&
  551. task->ata_task.fis.command == ATA_CMD_READ_LOG_EXT &&
  552. task->ata_task.fis.lbal == ATA_LOG_SATA_NCQ);
  553. }
  554. #endif /* !defined(_ISCI_REQUEST_H_) */