iwl-trans.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #ifndef __iwl_trans_h__
  64. #define __iwl_trans_h__
  65. #include <linux/ieee80211.h>
  66. #include "iwl-shared.h"
  67. #include "iwl-debug.h"
  68. /**
  69. * DOC: Transport layer - what is it ?
  70. *
  71. * The tranport layer is the layer that deals with the HW directly. It provides
  72. * an abstraction of the underlying HW to the upper layer. The transport layer
  73. * doesn't provide any policy, algorithm or anything of this kind, but only
  74. * mechanisms to make the HW do something.It is not completely stateless but
  75. * close to it.
  76. * We will have an implementation for each different supported bus.
  77. */
  78. /**
  79. * DOC: Life cycle of the transport layer
  80. *
  81. * The transport layer has a very precise life cycle.
  82. *
  83. * 1) A helper function is called during the module initialization and
  84. * registers the bus driver's ops with the transport's alloc function.
  85. * 2) Bus's probe calls to the transport layer's allocation functions.
  86. * Of course this function is bus specific.
  87. * 3) This allocation functions will spawn the upper layer which will
  88. * register mac80211.
  89. *
  90. * 4) At some point (i.e. mac80211's start call), the op_mode will call
  91. * the following sequence:
  92. * start_hw
  93. * start_fw
  94. *
  95. * 5) Then when finished (or reset):
  96. * stop_fw (a.k.a. stop device for the moment)
  97. * stop_hw
  98. *
  99. * 6) Eventually, the free function will be called.
  100. */
  101. struct iwl_priv;
  102. struct iwl_shared;
  103. struct iwl_op_mode;
  104. struct fw_img;
  105. struct sk_buff;
  106. struct dentry;
  107. /**
  108. * DOC: Host command section
  109. *
  110. * A host command is a commaned issued by the upper layer to the fw. There are
  111. * several versions of fw that have several APIs. The transport layer is
  112. * completely agnostic to these differences.
  113. * The transport does provide helper functionnality (i.e. SYNC / ASYNC mode),
  114. */
  115. #define SEQ_TO_SN(seq) (((seq) & IEEE80211_SCTL_SEQ) >> 4)
  116. #define SN_TO_SEQ(ssn) (((ssn) << 4) & IEEE80211_SCTL_SEQ)
  117. #define MAX_SN ((IEEE80211_SCTL_SEQ) >> 4)
  118. /**
  119. * enum CMD_MODE - how to send the host commands ?
  120. *
  121. * @CMD_SYNC: The caller will be stalled until the fw responds to the command
  122. * @CMD_ASYNC: Return right away and don't want for the response
  123. * @CMD_WANT_SKB: valid only with CMD_SYNC. The caller needs the buffer of the
  124. * response.
  125. * @CMD_ON_DEMAND: This command is sent by the test mode pipe.
  126. */
  127. enum CMD_MODE {
  128. CMD_SYNC = 0,
  129. CMD_ASYNC = BIT(0),
  130. CMD_WANT_SKB = BIT(1),
  131. CMD_ON_DEMAND = BIT(2),
  132. };
  133. #define DEF_CMD_PAYLOAD_SIZE 320
  134. /**
  135. * struct iwl_device_cmd
  136. *
  137. * For allocation of the command and tx queues, this establishes the overall
  138. * size of the largest command we send to uCode, except for commands that
  139. * aren't fully copied and use other TFD space.
  140. */
  141. struct iwl_device_cmd {
  142. struct iwl_cmd_header hdr; /* uCode API */
  143. u8 payload[DEF_CMD_PAYLOAD_SIZE];
  144. } __packed;
  145. #define TFD_MAX_PAYLOAD_SIZE (sizeof(struct iwl_device_cmd))
  146. #define IWL_MAX_CMD_TFDS 2
  147. /**
  148. * struct iwl_hcmd_dataflag - flag for each one of the chunks of the command
  149. *
  150. * IWL_HCMD_DFL_NOCOPY: By default, the command is copied to the host command's
  151. * ring. The transport layer doesn't map the command's buffer to DMA, but
  152. * rather copies it to an previously allocated DMA buffer. This flag tells
  153. * the transport layer not to copy the command, but to map the existing
  154. * buffer. This can save memcpy and is worth with very big comamnds.
  155. */
  156. enum iwl_hcmd_dataflag {
  157. IWL_HCMD_DFL_NOCOPY = BIT(0),
  158. };
  159. /**
  160. * struct iwl_host_cmd - Host command to the uCode
  161. *
  162. * @data: array of chunks that composes the data of the host command
  163. * @reply_page: pointer to the page that holds the response to the host command
  164. * @handler_status: return value of the handler of the command
  165. * (put in setup_rx_handlers) - valid for SYNC mode only
  166. * @flags: can be CMD_*
  167. * @len: array of the lenths of the chunks in data
  168. * @dataflags: IWL_HCMD_DFL_*
  169. * @id: id of the host command
  170. */
  171. struct iwl_host_cmd {
  172. const void *data[IWL_MAX_CMD_TFDS];
  173. unsigned long reply_page;
  174. int handler_status;
  175. u32 flags;
  176. u16 len[IWL_MAX_CMD_TFDS];
  177. u8 dataflags[IWL_MAX_CMD_TFDS];
  178. u8 id;
  179. };
  180. /**
  181. * struct iwl_trans_ops - transport specific operations
  182. *
  183. * All the handlers MUST be implemented
  184. *
  185. * @start_hw: starts the HW- from that point on, the HW can send interrupts
  186. * May sleep
  187. * @stop_hw: stops the HW- from that point on, the HW will be in low power but
  188. * will still issue interrupt if the HW RF kill is triggered.
  189. * May sleep
  190. * @start_fw: allocates and inits all the resources for the transport
  191. * layer. Also kick a fw image.
  192. * May sleep
  193. * @fw_alive: called when the fw sends alive notification
  194. * May sleep
  195. * @wake_any_queue: wake all the queues of a specfic context IWL_RXON_CTX_*
  196. * @stop_device:stops the whole device (embedded CPU put to reset)
  197. * May sleep
  198. * @send_cmd:send a host command
  199. * May sleep only if CMD_SYNC is set
  200. * @tx: send an skb
  201. * Must be atomic
  202. * @reclaim: free packet until ssn. Returns a list of freed packets.
  203. * Must be atomic
  204. * @tx_agg_alloc: allocate resources for a TX BA session
  205. * May sleep
  206. * @tx_agg_setup: setup a tx queue for AMPDU - will be called once the HW is
  207. * ready and a successful ADDBA response has been received.
  208. * May sleep
  209. * @tx_agg_disable: de-configure a Tx queue to send AMPDUs
  210. * May sleep
  211. * @free: release all the ressource for the transport layer itself such as
  212. * irq, tasklet etc... From this point on, the device may not issue
  213. * any interrupt (incl. RFKILL).
  214. * May sleep
  215. * @stop_queue: stop a specific queue
  216. * @check_stuck_queue: check if a specific queue is stuck
  217. * @wait_tx_queue_empty: wait until all tx queues are empty
  218. * May sleep
  219. * @dbgfs_register: add the dbgfs files under this directory. Files will be
  220. * automatically deleted.
  221. * @suspend: stop the device unless WoWLAN is configured
  222. * @resume: resume activity of the device
  223. * @write8: write a u8 to a register at offset ofs from the BAR
  224. * @write32: write a u32 to a register at offset ofs from the BAR
  225. * @read32: read a u32 register at offset ofs from the BAR
  226. */
  227. struct iwl_trans_ops {
  228. int (*start_hw)(struct iwl_trans *iwl_trans);
  229. void (*stop_hw)(struct iwl_trans *iwl_trans);
  230. int (*start_fw)(struct iwl_trans *trans, struct fw_img *fw);
  231. void (*fw_alive)(struct iwl_trans *trans);
  232. void (*stop_device)(struct iwl_trans *trans);
  233. void (*wake_any_queue)(struct iwl_trans *trans,
  234. enum iwl_rxon_context_id ctx,
  235. const char *msg);
  236. int (*send_cmd)(struct iwl_trans *trans, struct iwl_host_cmd *cmd);
  237. int (*tx)(struct iwl_trans *trans, struct sk_buff *skb,
  238. struct iwl_device_cmd *dev_cmd, enum iwl_rxon_context_id ctx,
  239. u8 sta_id, u8 tid);
  240. int (*reclaim)(struct iwl_trans *trans, int sta_id, int tid,
  241. int txq_id, int ssn, u32 status,
  242. struct sk_buff_head *skbs);
  243. int (*tx_agg_disable)(struct iwl_trans *trans,
  244. int sta_id, int tid);
  245. int (*tx_agg_alloc)(struct iwl_trans *trans,
  246. int sta_id, int tid);
  247. void (*tx_agg_setup)(struct iwl_trans *trans,
  248. enum iwl_rxon_context_id ctx, int sta_id, int tid,
  249. int frame_limit, u16 ssn);
  250. void (*free)(struct iwl_trans *trans);
  251. void (*stop_queue)(struct iwl_trans *trans, int q, const char *msg);
  252. int (*dbgfs_register)(struct iwl_trans *trans, struct dentry* dir);
  253. int (*check_stuck_queue)(struct iwl_trans *trans, int q);
  254. int (*wait_tx_queue_empty)(struct iwl_trans *trans);
  255. #ifdef CONFIG_PM_SLEEP
  256. int (*suspend)(struct iwl_trans *trans);
  257. int (*resume)(struct iwl_trans *trans);
  258. #endif
  259. void (*write8)(struct iwl_trans *trans, u32 ofs, u8 val);
  260. void (*write32)(struct iwl_trans *trans, u32 ofs, u32 val);
  261. u32 (*read32)(struct iwl_trans *trans, u32 ofs);
  262. };
  263. /* Opaque calibration results */
  264. struct iwl_calib_result {
  265. struct list_head list;
  266. size_t cmd_len;
  267. struct iwl_calib_hdr hdr;
  268. /* data follows */
  269. };
  270. /**
  271. * enum iwl_trans_state - state of the transport layer
  272. *
  273. * @IWL_TRANS_NO_FW: no fw has sent an alive response
  274. * @IWL_TRANS_FW_ALIVE: a fw has sent an alive response
  275. */
  276. enum iwl_trans_state {
  277. IWL_TRANS_NO_FW = 0,
  278. IWL_TRANS_FW_ALIVE = 1,
  279. };
  280. /**
  281. * struct iwl_trans - transport common data
  282. *
  283. * @ops - pointer to iwl_trans_ops
  284. * @op_mode - pointer to the op_mode
  285. * @shrd - pointer to iwl_shared which holds shared data from the upper layer
  286. * @hcmd_lock: protects HCMD
  287. * @reg_lock - protect hw register access
  288. * @dev - pointer to struct device * that represents the device
  289. * @irq - the irq number for the device
  290. * @hw_id: a u32 with the ID of the device / subdevice.
  291. * Set during transport allocation.
  292. * @hw_id_str: a string with info about HW ID. Set during transport allocation.
  293. * @ucode_write_complete: indicates that the ucode has been copied.
  294. * @nvm_device_type: indicates OTP or eeprom
  295. * @pm_support: set to true in start_hw if link pm is supported
  296. * @calib_results: list head for init calibration results
  297. */
  298. struct iwl_trans {
  299. const struct iwl_trans_ops *ops;
  300. struct iwl_op_mode *op_mode;
  301. struct iwl_shared *shrd;
  302. enum iwl_trans_state state;
  303. spinlock_t hcmd_lock;
  304. spinlock_t reg_lock;
  305. struct device *dev;
  306. unsigned int irq;
  307. u32 hw_rev;
  308. u32 hw_id;
  309. char hw_id_str[52];
  310. u8 ucode_write_complete;
  311. int nvm_device_type;
  312. bool pm_support;
  313. struct list_head calib_results;
  314. /* pointer to trans specific struct */
  315. /*Ensure that this pointer will always be aligned to sizeof pointer */
  316. char trans_specific[0] __aligned(sizeof(void *));
  317. };
  318. static inline void iwl_trans_configure(struct iwl_trans *trans,
  319. struct iwl_op_mode *op_mode)
  320. {
  321. /*
  322. * only set the op_mode for the moment. Later on, this function will do
  323. * more
  324. */
  325. trans->op_mode = op_mode;
  326. }
  327. static inline int iwl_trans_start_hw(struct iwl_trans *trans)
  328. {
  329. might_sleep();
  330. return trans->ops->start_hw(trans);
  331. }
  332. static inline void iwl_trans_stop_hw(struct iwl_trans *trans)
  333. {
  334. might_sleep();
  335. trans->ops->stop_hw(trans);
  336. trans->state = IWL_TRANS_NO_FW;
  337. }
  338. static inline void iwl_trans_fw_alive(struct iwl_trans *trans)
  339. {
  340. might_sleep();
  341. trans->ops->fw_alive(trans);
  342. trans->state = IWL_TRANS_FW_ALIVE;
  343. }
  344. static inline int iwl_trans_start_fw(struct iwl_trans *trans, struct fw_img *fw)
  345. {
  346. might_sleep();
  347. return trans->ops->start_fw(trans, fw);
  348. }
  349. static inline void iwl_trans_stop_device(struct iwl_trans *trans)
  350. {
  351. might_sleep();
  352. trans->ops->stop_device(trans);
  353. trans->state = IWL_TRANS_NO_FW;
  354. }
  355. static inline void iwl_trans_wake_any_queue(struct iwl_trans *trans,
  356. enum iwl_rxon_context_id ctx,
  357. const char *msg)
  358. {
  359. if (trans->state != IWL_TRANS_FW_ALIVE)
  360. IWL_ERR(trans, "%s bad state = %d", __func__, trans->state);
  361. trans->ops->wake_any_queue(trans, ctx, msg);
  362. }
  363. static inline int iwl_trans_send_cmd(struct iwl_trans *trans,
  364. struct iwl_host_cmd *cmd)
  365. {
  366. if (trans->state != IWL_TRANS_FW_ALIVE)
  367. IWL_ERR(trans, "%s bad state = %d", __func__, trans->state);
  368. return trans->ops->send_cmd(trans, cmd);
  369. }
  370. int iwl_trans_send_cmd_pdu(struct iwl_trans *trans, u8 id,
  371. u32 flags, u16 len, const void *data);
  372. static inline int iwl_trans_tx(struct iwl_trans *trans, struct sk_buff *skb,
  373. struct iwl_device_cmd *dev_cmd, enum iwl_rxon_context_id ctx,
  374. u8 sta_id, u8 tid)
  375. {
  376. if (trans->state != IWL_TRANS_FW_ALIVE)
  377. IWL_ERR(trans, "%s bad state = %d", __func__, trans->state);
  378. return trans->ops->tx(trans, skb, dev_cmd, ctx, sta_id, tid);
  379. }
  380. static inline int iwl_trans_reclaim(struct iwl_trans *trans, int sta_id,
  381. int tid, int txq_id, int ssn, u32 status,
  382. struct sk_buff_head *skbs)
  383. {
  384. if (trans->state != IWL_TRANS_FW_ALIVE)
  385. IWL_ERR(trans, "%s bad state = %d", __func__, trans->state);
  386. return trans->ops->reclaim(trans, sta_id, tid, txq_id, ssn,
  387. status, skbs);
  388. }
  389. static inline int iwl_trans_tx_agg_disable(struct iwl_trans *trans,
  390. int sta_id, int tid)
  391. {
  392. might_sleep();
  393. if (trans->state != IWL_TRANS_FW_ALIVE)
  394. IWL_ERR(trans, "%s bad state = %d", __func__, trans->state);
  395. return trans->ops->tx_agg_disable(trans, sta_id, tid);
  396. }
  397. static inline int iwl_trans_tx_agg_alloc(struct iwl_trans *trans,
  398. int sta_id, int tid)
  399. {
  400. might_sleep();
  401. if (trans->state != IWL_TRANS_FW_ALIVE)
  402. IWL_ERR(trans, "%s bad state = %d", __func__, trans->state);
  403. return trans->ops->tx_agg_alloc(trans, sta_id, tid);
  404. }
  405. static inline void iwl_trans_tx_agg_setup(struct iwl_trans *trans,
  406. enum iwl_rxon_context_id ctx,
  407. int sta_id, int tid,
  408. int frame_limit, u16 ssn)
  409. {
  410. might_sleep();
  411. if (trans->state != IWL_TRANS_FW_ALIVE)
  412. IWL_ERR(trans, "%s bad state = %d", __func__, trans->state);
  413. trans->ops->tx_agg_setup(trans, ctx, sta_id, tid, frame_limit, ssn);
  414. }
  415. static inline void iwl_trans_free(struct iwl_trans *trans)
  416. {
  417. trans->ops->free(trans);
  418. }
  419. static inline void iwl_trans_stop_queue(struct iwl_trans *trans, int q,
  420. const char *msg)
  421. {
  422. if (trans->state != IWL_TRANS_FW_ALIVE)
  423. IWL_ERR(trans, "%s bad state = %d", __func__, trans->state);
  424. trans->ops->stop_queue(trans, q, msg);
  425. }
  426. static inline int iwl_trans_wait_tx_queue_empty(struct iwl_trans *trans)
  427. {
  428. if (trans->state != IWL_TRANS_FW_ALIVE)
  429. IWL_ERR(trans, "%s bad state = %d", __func__, trans->state);
  430. return trans->ops->wait_tx_queue_empty(trans);
  431. }
  432. static inline int iwl_trans_check_stuck_queue(struct iwl_trans *trans, int q)
  433. {
  434. if (trans->state != IWL_TRANS_FW_ALIVE)
  435. IWL_ERR(trans, "%s bad state = %d", __func__, trans->state);
  436. return trans->ops->check_stuck_queue(trans, q);
  437. }
  438. static inline int iwl_trans_dbgfs_register(struct iwl_trans *trans,
  439. struct dentry *dir)
  440. {
  441. return trans->ops->dbgfs_register(trans, dir);
  442. }
  443. #ifdef CONFIG_PM_SLEEP
  444. static inline int iwl_trans_suspend(struct iwl_trans *trans)
  445. {
  446. return trans->ops->suspend(trans);
  447. }
  448. static inline int iwl_trans_resume(struct iwl_trans *trans)
  449. {
  450. return trans->ops->resume(trans);
  451. }
  452. #endif
  453. static inline void iwl_trans_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  454. {
  455. trans->ops->write8(trans, ofs, val);
  456. }
  457. static inline void iwl_trans_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  458. {
  459. trans->ops->write32(trans, ofs, val);
  460. }
  461. static inline u32 iwl_trans_read32(struct iwl_trans *trans, u32 ofs)
  462. {
  463. return trans->ops->read32(trans, ofs);
  464. }
  465. /*****************************************************
  466. * Utils functions
  467. ******************************************************/
  468. int iwl_send_calib_results(struct iwl_trans *trans);
  469. int iwl_calib_set(struct iwl_trans *trans,
  470. const struct iwl_calib_hdr *cmd, int len);
  471. void iwl_calib_free_results(struct iwl_trans *trans);
  472. /*****************************************************
  473. * Transport layers implementations + their allocation function
  474. ******************************************************/
  475. struct pci_dev;
  476. struct pci_device_id;
  477. extern const struct iwl_trans_ops trans_ops_pcie;
  478. struct iwl_trans *iwl_trans_pcie_alloc(struct iwl_shared *shrd,
  479. struct pci_dev *pdev,
  480. const struct pci_device_id *ent);
  481. int __must_check iwl_pci_register_driver(void);
  482. void iwl_pci_unregister_driver(void);
  483. extern const struct iwl_trans_ops trans_ops_idi;
  484. struct iwl_trans *iwl_trans_idi_alloc(struct iwl_shared *shrd,
  485. void *pdev_void,
  486. const void *ent_void);
  487. #endif /* __iwl_trans_h__ */