iwl-trans.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #ifndef __iwl_trans_h__
  66. #define __iwl_trans_h__
  67. #include <linux/ieee80211.h>
  68. #include <linux/mm.h> /* for page_address */
  69. #include <linux/lockdep.h>
  70. #include "iwl-debug.h"
  71. #include "iwl-config.h"
  72. #include "iwl-fw.h"
  73. #include "iwl-op-mode.h"
  74. /**
  75. * DOC: Transport layer - what is it ?
  76. *
  77. * The transport layer is the layer that deals with the HW directly. It provides
  78. * an abstraction of the underlying HW to the upper layer. The transport layer
  79. * doesn't provide any policy, algorithm or anything of this kind, but only
  80. * mechanisms to make the HW do something. It is not completely stateless but
  81. * close to it.
  82. * We will have an implementation for each different supported bus.
  83. */
  84. /**
  85. * DOC: Life cycle of the transport layer
  86. *
  87. * The transport layer has a very precise life cycle.
  88. *
  89. * 1) A helper function is called during the module initialization and
  90. * registers the bus driver's ops with the transport's alloc function.
  91. * 2) Bus's probe calls to the transport layer's allocation functions.
  92. * Of course this function is bus specific.
  93. * 3) This allocation functions will spawn the upper layer which will
  94. * register mac80211.
  95. *
  96. * 4) At some point (i.e. mac80211's start call), the op_mode will call
  97. * the following sequence:
  98. * start_hw
  99. * start_fw
  100. *
  101. * 5) Then when finished (or reset):
  102. * stop_device
  103. *
  104. * 6) Eventually, the free function will be called.
  105. */
  106. /**
  107. * DOC: Host command section
  108. *
  109. * A host command is a command issued by the upper layer to the fw. There are
  110. * several versions of fw that have several APIs. The transport layer is
  111. * completely agnostic to these differences.
  112. * The transport does provide helper functionality (i.e. SYNC / ASYNC mode),
  113. */
  114. #define SEQ_TO_QUEUE(s) (((s) >> 8) & 0x1f)
  115. #define QUEUE_TO_SEQ(q) (((q) & 0x1f) << 8)
  116. #define SEQ_TO_INDEX(s) ((s) & 0xff)
  117. #define INDEX_TO_SEQ(i) ((i) & 0xff)
  118. #define SEQ_RX_FRAME cpu_to_le16(0x8000)
  119. /**
  120. * struct iwl_cmd_header
  121. *
  122. * This header format appears in the beginning of each command sent from the
  123. * driver, and each response/notification received from uCode.
  124. */
  125. struct iwl_cmd_header {
  126. u8 cmd; /* Command ID: REPLY_RXON, etc. */
  127. u8 flags; /* 0:5 reserved, 6 abort, 7 internal */
  128. /*
  129. * The driver sets up the sequence number to values of its choosing.
  130. * uCode does not use this value, but passes it back to the driver
  131. * when sending the response to each driver-originated command, so
  132. * the driver can match the response to the command. Since the values
  133. * don't get used by uCode, the driver may set up an arbitrary format.
  134. *
  135. * There is one exception: uCode sets bit 15 when it originates
  136. * the response/notification, i.e. when the response/notification
  137. * is not a direct response to a command sent by the driver. For
  138. * example, uCode issues REPLY_RX when it sends a received frame
  139. * to the driver; it is not a direct response to any driver command.
  140. *
  141. * The Linux driver uses the following format:
  142. *
  143. * 0:7 tfd index - position within TX queue
  144. * 8:12 TX queue id
  145. * 13:14 reserved
  146. * 15 unsolicited RX or uCode-originated notification
  147. */
  148. __le16 sequence;
  149. } __packed;
  150. /* iwl_cmd_header flags value */
  151. #define IWL_CMD_FAILED_MSK 0x40
  152. #define FH_RSCSR_FRAME_SIZE_MSK 0x00003FFF /* bits 0-13 */
  153. #define FH_RSCSR_FRAME_INVALID 0x55550000
  154. #define FH_RSCSR_FRAME_ALIGN 0x40
  155. struct iwl_rx_packet {
  156. /*
  157. * The first 4 bytes of the RX frame header contain both the RX frame
  158. * size and some flags.
  159. * Bit fields:
  160. * 31: flag flush RB request
  161. * 30: flag ignore TC (terminal counter) request
  162. * 29: flag fast IRQ request
  163. * 28-14: Reserved
  164. * 13-00: RX frame size
  165. */
  166. __le32 len_n_flags;
  167. struct iwl_cmd_header hdr;
  168. u8 data[];
  169. } __packed;
  170. static inline u32 iwl_rx_packet_len(const struct iwl_rx_packet *pkt)
  171. {
  172. return le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  173. }
  174. static inline u32 iwl_rx_packet_payload_len(const struct iwl_rx_packet *pkt)
  175. {
  176. return iwl_rx_packet_len(pkt) - sizeof(pkt->hdr);
  177. }
  178. /**
  179. * enum CMD_MODE - how to send the host commands ?
  180. *
  181. * @CMD_ASYNC: Return right away and don't wait for the response
  182. * @CMD_WANT_SKB: Not valid with CMD_ASYNC. The caller needs the buffer of
  183. * the response. The caller needs to call iwl_free_resp when done.
  184. * @CMD_HIGH_PRIO: The command is high priority - it goes to the front of the
  185. * command queue, but after other high priority commands. Valid only
  186. * with CMD_ASYNC.
  187. * @CMD_SEND_IN_IDLE: The command should be sent even when the trans is idle.
  188. * @CMD_MAKE_TRANS_IDLE: The command response should mark the trans as idle.
  189. * @CMD_WAKE_UP_TRANS: The command response should wake up the trans
  190. * (i.e. mark it as non-idle).
  191. */
  192. enum CMD_MODE {
  193. CMD_ASYNC = BIT(0),
  194. CMD_WANT_SKB = BIT(1),
  195. CMD_SEND_IN_RFKILL = BIT(2),
  196. CMD_HIGH_PRIO = BIT(3),
  197. CMD_SEND_IN_IDLE = BIT(4),
  198. CMD_MAKE_TRANS_IDLE = BIT(5),
  199. CMD_WAKE_UP_TRANS = BIT(6),
  200. };
  201. #define DEF_CMD_PAYLOAD_SIZE 320
  202. /**
  203. * struct iwl_device_cmd
  204. *
  205. * For allocation of the command and tx queues, this establishes the overall
  206. * size of the largest command we send to uCode, except for commands that
  207. * aren't fully copied and use other TFD space.
  208. */
  209. struct iwl_device_cmd {
  210. struct iwl_cmd_header hdr; /* uCode API */
  211. u8 payload[DEF_CMD_PAYLOAD_SIZE];
  212. } __packed;
  213. #define TFD_MAX_PAYLOAD_SIZE (sizeof(struct iwl_device_cmd))
  214. /*
  215. * number of transfer buffers (fragments) per transmit frame descriptor;
  216. * this is just the driver's idea, the hardware supports 20
  217. */
  218. #define IWL_MAX_CMD_TBS_PER_TFD 2
  219. /**
  220. * struct iwl_hcmd_dataflag - flag for each one of the chunks of the command
  221. *
  222. * @IWL_HCMD_DFL_NOCOPY: By default, the command is copied to the host command's
  223. * ring. The transport layer doesn't map the command's buffer to DMA, but
  224. * rather copies it to a previously allocated DMA buffer. This flag tells
  225. * the transport layer not to copy the command, but to map the existing
  226. * buffer (that is passed in) instead. This saves the memcpy and allows
  227. * commands that are bigger than the fixed buffer to be submitted.
  228. * Note that a TFD entry after a NOCOPY one cannot be a normal copied one.
  229. * @IWL_HCMD_DFL_DUP: Only valid without NOCOPY, duplicate the memory for this
  230. * chunk internally and free it again after the command completes. This
  231. * can (currently) be used only once per command.
  232. * Note that a TFD entry after a DUP one cannot be a normal copied one.
  233. */
  234. enum iwl_hcmd_dataflag {
  235. IWL_HCMD_DFL_NOCOPY = BIT(0),
  236. IWL_HCMD_DFL_DUP = BIT(1),
  237. };
  238. /**
  239. * struct iwl_host_cmd - Host command to the uCode
  240. *
  241. * @data: array of chunks that composes the data of the host command
  242. * @resp_pkt: response packet, if %CMD_WANT_SKB was set
  243. * @_rx_page_order: (internally used to free response packet)
  244. * @_rx_page_addr: (internally used to free response packet)
  245. * @handler_status: return value of the handler of the command
  246. * (put in setup_rx_handlers) - valid for SYNC mode only
  247. * @flags: can be CMD_*
  248. * @len: array of the lengths of the chunks in data
  249. * @dataflags: IWL_HCMD_DFL_*
  250. * @id: id of the host command
  251. */
  252. struct iwl_host_cmd {
  253. const void *data[IWL_MAX_CMD_TBS_PER_TFD];
  254. struct iwl_rx_packet *resp_pkt;
  255. unsigned long _rx_page_addr;
  256. u32 _rx_page_order;
  257. int handler_status;
  258. u32 flags;
  259. u16 len[IWL_MAX_CMD_TBS_PER_TFD];
  260. u8 dataflags[IWL_MAX_CMD_TBS_PER_TFD];
  261. u8 id;
  262. };
  263. static inline void iwl_free_resp(struct iwl_host_cmd *cmd)
  264. {
  265. free_pages(cmd->_rx_page_addr, cmd->_rx_page_order);
  266. }
  267. struct iwl_rx_cmd_buffer {
  268. struct page *_page;
  269. int _offset;
  270. bool _page_stolen;
  271. u32 _rx_page_order;
  272. unsigned int truesize;
  273. };
  274. static inline void *rxb_addr(struct iwl_rx_cmd_buffer *r)
  275. {
  276. return (void *)((unsigned long)page_address(r->_page) + r->_offset);
  277. }
  278. static inline int rxb_offset(struct iwl_rx_cmd_buffer *r)
  279. {
  280. return r->_offset;
  281. }
  282. static inline struct page *rxb_steal_page(struct iwl_rx_cmd_buffer *r)
  283. {
  284. r->_page_stolen = true;
  285. get_page(r->_page);
  286. return r->_page;
  287. }
  288. static inline void iwl_free_rxb(struct iwl_rx_cmd_buffer *r)
  289. {
  290. __free_pages(r->_page, r->_rx_page_order);
  291. }
  292. #define MAX_NO_RECLAIM_CMDS 6
  293. #define IWL_MASK(lo, hi) ((1 << (hi)) | ((1 << (hi)) - (1 << (lo))))
  294. /*
  295. * Maximum number of HW queues the transport layer
  296. * currently supports
  297. */
  298. #define IWL_MAX_HW_QUEUES 32
  299. #define IWL_MAX_TID_COUNT 8
  300. #define IWL_FRAME_LIMIT 64
  301. /**
  302. * enum iwl_wowlan_status - WoWLAN image/device status
  303. * @IWL_D3_STATUS_ALIVE: firmware is still running after resume
  304. * @IWL_D3_STATUS_RESET: device was reset while suspended
  305. */
  306. enum iwl_d3_status {
  307. IWL_D3_STATUS_ALIVE,
  308. IWL_D3_STATUS_RESET,
  309. };
  310. /**
  311. * enum iwl_trans_status: transport status flags
  312. * @STATUS_SYNC_HCMD_ACTIVE: a SYNC command is being processed
  313. * @STATUS_DEVICE_ENABLED: APM is enabled
  314. * @STATUS_TPOWER_PMI: the device might be asleep (need to wake it up)
  315. * @STATUS_INT_ENABLED: interrupts are enabled
  316. * @STATUS_RFKILL: the HW RFkill switch is in KILL position
  317. * @STATUS_FW_ERROR: the fw is in error state
  318. * @STATUS_TRANS_GOING_IDLE: shutting down the trans, only special commands
  319. * are sent
  320. * @STATUS_TRANS_IDLE: the trans is idle - general commands are not to be sent
  321. */
  322. enum iwl_trans_status {
  323. STATUS_SYNC_HCMD_ACTIVE,
  324. STATUS_DEVICE_ENABLED,
  325. STATUS_TPOWER_PMI,
  326. STATUS_INT_ENABLED,
  327. STATUS_RFKILL,
  328. STATUS_FW_ERROR,
  329. STATUS_TRANS_GOING_IDLE,
  330. STATUS_TRANS_IDLE,
  331. };
  332. /**
  333. * struct iwl_trans_config - transport configuration
  334. *
  335. * @op_mode: pointer to the upper layer.
  336. * @cmd_queue: the index of the command queue.
  337. * Must be set before start_fw.
  338. * @cmd_fifo: the fifo for host commands
  339. * @cmd_q_wdg_timeout: the timeout of the watchdog timer for the command queue.
  340. * @no_reclaim_cmds: Some devices erroneously don't set the
  341. * SEQ_RX_FRAME bit on some notifications, this is the
  342. * list of such notifications to filter. Max length is
  343. * %MAX_NO_RECLAIM_CMDS.
  344. * @n_no_reclaim_cmds: # of commands in list
  345. * @rx_buf_size_8k: 8 kB RX buffer size needed for A-MSDUs,
  346. * if unset 4k will be the RX buffer size
  347. * @bc_table_dword: set to true if the BC table expects the byte count to be
  348. * in DWORD (as opposed to bytes)
  349. * @scd_set_active: should the transport configure the SCD for HCMD queue
  350. * @command_names: array of command names, must be 256 entries
  351. * (one for each command); for debugging only
  352. * @sdio_adma_addr: the default address to set for the ADMA in SDIO mode until
  353. * we get the ALIVE from the uCode
  354. */
  355. struct iwl_trans_config {
  356. struct iwl_op_mode *op_mode;
  357. u8 cmd_queue;
  358. u8 cmd_fifo;
  359. unsigned int cmd_q_wdg_timeout;
  360. const u8 *no_reclaim_cmds;
  361. unsigned int n_no_reclaim_cmds;
  362. bool rx_buf_size_8k;
  363. bool bc_table_dword;
  364. bool scd_set_active;
  365. const char *const *command_names;
  366. u32 sdio_adma_addr;
  367. };
  368. struct iwl_trans_dump_data {
  369. u32 len;
  370. u8 data[];
  371. };
  372. struct iwl_trans;
  373. struct iwl_trans_txq_scd_cfg {
  374. u8 fifo;
  375. s8 sta_id;
  376. u8 tid;
  377. bool aggregate;
  378. int frame_limit;
  379. };
  380. /**
  381. * struct iwl_trans_ops - transport specific operations
  382. *
  383. * All the handlers MUST be implemented
  384. *
  385. * @start_hw: starts the HW. If low_power is true, the NIC needs to be taken
  386. * out of a low power state. From that point on, the HW can send
  387. * interrupts. May sleep.
  388. * @op_mode_leave: Turn off the HW RF kill indication if on
  389. * May sleep
  390. * @start_fw: allocates and inits all the resources for the transport
  391. * layer. Also kick a fw image.
  392. * May sleep
  393. * @fw_alive: called when the fw sends alive notification. If the fw provides
  394. * the SCD base address in SRAM, then provide it here, or 0 otherwise.
  395. * May sleep
  396. * @stop_device: stops the whole device (embedded CPU put to reset) and stops
  397. * the HW. If low_power is true, the NIC will be put in low power state.
  398. * From that point on, the HW will be stopped but will still issue an
  399. * interrupt if the HW RF kill switch is triggered.
  400. * This callback must do the right thing and not crash even if %start_hw()
  401. * was called but not &start_fw(). May sleep.
  402. * @d3_suspend: put the device into the correct mode for WoWLAN during
  403. * suspend. This is optional, if not implemented WoWLAN will not be
  404. * supported. This callback may sleep.
  405. * @d3_resume: resume the device after WoWLAN, enabling the opmode to
  406. * talk to the WoWLAN image to get its status. This is optional, if not
  407. * implemented WoWLAN will not be supported. This callback may sleep.
  408. * @send_cmd:send a host command. Must return -ERFKILL if RFkill is asserted.
  409. * If RFkill is asserted in the middle of a SYNC host command, it must
  410. * return -ERFKILL straight away.
  411. * May sleep only if CMD_ASYNC is not set
  412. * @tx: send an skb
  413. * Must be atomic
  414. * @reclaim: free packet until ssn. Returns a list of freed packets.
  415. * Must be atomic
  416. * @txq_enable: setup a queue. To setup an AC queue, use the
  417. * iwl_trans_ac_txq_enable wrapper. fw_alive must have been called before
  418. * this one. The op_mode must not configure the HCMD queue. The scheduler
  419. * configuration may be %NULL, in which case the hardware will not be
  420. * configured. May sleep.
  421. * @txq_disable: de-configure a Tx queue to send AMPDUs
  422. * Must be atomic
  423. * @wait_tx_queue_empty: wait until tx queues are empty. May sleep.
  424. * @freeze_txq_timer: prevents the timer of the queue from firing until the
  425. * queue is set to awake. Must be atomic.
  426. * @dbgfs_register: add the dbgfs files under this directory. Files will be
  427. * automatically deleted.
  428. * @write8: write a u8 to a register at offset ofs from the BAR
  429. * @write32: write a u32 to a register at offset ofs from the BAR
  430. * @read32: read a u32 register at offset ofs from the BAR
  431. * @read_prph: read a DWORD from a periphery register
  432. * @write_prph: write a DWORD to a periphery register
  433. * @read_mem: read device's SRAM in DWORD
  434. * @write_mem: write device's SRAM in DWORD. If %buf is %NULL, then the memory
  435. * will be zeroed.
  436. * @configure: configure parameters required by the transport layer from
  437. * the op_mode. May be called several times before start_fw, can't be
  438. * called after that.
  439. * @set_pmi: set the power pmi state
  440. * @grab_nic_access: wake the NIC to be able to access non-HBUS regs.
  441. * Sleeping is not allowed between grab_nic_access and
  442. * release_nic_access.
  443. * @release_nic_access: let the NIC go to sleep. The "flags" parameter
  444. * must be the same one that was sent before to the grab_nic_access.
  445. * @set_bits_mask - set SRAM register according to value and mask.
  446. * @ref: grab a reference to the transport/FW layers, disallowing
  447. * certain low power states
  448. * @unref: release a reference previously taken with @ref. Note that
  449. * initially the reference count is 1, making an initial @unref
  450. * necessary to allow low power states.
  451. * @dump_data: return a vmalloc'ed buffer with debug data, maybe containing last
  452. * TX'ed commands and similar. The buffer will be vfree'd by the caller.
  453. * Note that the transport must fill in the proper file headers.
  454. */
  455. struct iwl_trans_ops {
  456. int (*start_hw)(struct iwl_trans *iwl_trans, bool low_power);
  457. void (*op_mode_leave)(struct iwl_trans *iwl_trans);
  458. int (*start_fw)(struct iwl_trans *trans, const struct fw_img *fw,
  459. bool run_in_rfkill);
  460. int (*update_sf)(struct iwl_trans *trans,
  461. struct iwl_sf_region *st_fwrd_space);
  462. void (*fw_alive)(struct iwl_trans *trans, u32 scd_addr);
  463. void (*stop_device)(struct iwl_trans *trans, bool low_power);
  464. void (*d3_suspend)(struct iwl_trans *trans, bool test);
  465. int (*d3_resume)(struct iwl_trans *trans, enum iwl_d3_status *status,
  466. bool test);
  467. int (*send_cmd)(struct iwl_trans *trans, struct iwl_host_cmd *cmd);
  468. int (*tx)(struct iwl_trans *trans, struct sk_buff *skb,
  469. struct iwl_device_cmd *dev_cmd, int queue);
  470. void (*reclaim)(struct iwl_trans *trans, int queue, int ssn,
  471. struct sk_buff_head *skbs);
  472. void (*txq_enable)(struct iwl_trans *trans, int queue, u16 ssn,
  473. const struct iwl_trans_txq_scd_cfg *cfg,
  474. unsigned int queue_wdg_timeout);
  475. void (*txq_disable)(struct iwl_trans *trans, int queue,
  476. bool configure_scd);
  477. int (*dbgfs_register)(struct iwl_trans *trans, struct dentry* dir);
  478. int (*wait_tx_queue_empty)(struct iwl_trans *trans, u32 txq_bm);
  479. void (*freeze_txq_timer)(struct iwl_trans *trans, unsigned long txqs,
  480. bool freeze);
  481. void (*write8)(struct iwl_trans *trans, u32 ofs, u8 val);
  482. void (*write32)(struct iwl_trans *trans, u32 ofs, u32 val);
  483. u32 (*read32)(struct iwl_trans *trans, u32 ofs);
  484. u32 (*read_prph)(struct iwl_trans *trans, u32 ofs);
  485. void (*write_prph)(struct iwl_trans *trans, u32 ofs, u32 val);
  486. int (*read_mem)(struct iwl_trans *trans, u32 addr,
  487. void *buf, int dwords);
  488. int (*write_mem)(struct iwl_trans *trans, u32 addr,
  489. const void *buf, int dwords);
  490. void (*configure)(struct iwl_trans *trans,
  491. const struct iwl_trans_config *trans_cfg);
  492. void (*set_pmi)(struct iwl_trans *trans, bool state);
  493. bool (*grab_nic_access)(struct iwl_trans *trans, bool silent,
  494. unsigned long *flags);
  495. void (*release_nic_access)(struct iwl_trans *trans,
  496. unsigned long *flags);
  497. void (*set_bits_mask)(struct iwl_trans *trans, u32 reg, u32 mask,
  498. u32 value);
  499. void (*ref)(struct iwl_trans *trans);
  500. void (*unref)(struct iwl_trans *trans);
  501. void (*suspend)(struct iwl_trans *trans);
  502. void (*resume)(struct iwl_trans *trans);
  503. struct iwl_trans_dump_data *(*dump_data)(struct iwl_trans *trans);
  504. };
  505. /**
  506. * enum iwl_trans_state - state of the transport layer
  507. *
  508. * @IWL_TRANS_NO_FW: no fw has sent an alive response
  509. * @IWL_TRANS_FW_ALIVE: a fw has sent an alive response
  510. */
  511. enum iwl_trans_state {
  512. IWL_TRANS_NO_FW = 0,
  513. IWL_TRANS_FW_ALIVE = 1,
  514. };
  515. /**
  516. * enum iwl_d0i3_mode - d0i3 mode
  517. *
  518. * @IWL_D0I3_MODE_OFF - d0i3 is disabled
  519. * @IWL_D0I3_MODE_ON_IDLE - enter d0i3 when device is idle
  520. * (e.g. no active references)
  521. * @IWL_D0I3_MODE_ON_SUSPEND - enter d0i3 only on suspend
  522. * (in case of 'any' trigger)
  523. */
  524. enum iwl_d0i3_mode {
  525. IWL_D0I3_MODE_OFF = 0,
  526. IWL_D0I3_MODE_ON_IDLE,
  527. IWL_D0I3_MODE_ON_SUSPEND,
  528. };
  529. /**
  530. * struct iwl_trans - transport common data
  531. *
  532. * @ops - pointer to iwl_trans_ops
  533. * @op_mode - pointer to the op_mode
  534. * @cfg - pointer to the configuration
  535. * @status: a bit-mask of transport status flags
  536. * @dev - pointer to struct device * that represents the device
  537. * @hw_id: a u32 with the ID of the device / sub-device.
  538. * Set during transport allocation.
  539. * @hw_id_str: a string with info about HW ID. Set during transport allocation.
  540. * @pm_support: set to true in start_hw if link pm is supported
  541. * @ltr_enabled: set to true if the LTR is enabled
  542. * @dev_cmd_pool: pool for Tx cmd allocation - for internal use only.
  543. * The user should use iwl_trans_{alloc,free}_tx_cmd.
  544. * @dev_cmd_headroom: room needed for the transport's private use before the
  545. * device_cmd for Tx - for internal use only
  546. * The user should use iwl_trans_{alloc,free}_tx_cmd.
  547. * @rx_mpdu_cmd: MPDU RX command ID, must be assigned by opmode before
  548. * starting the firmware, used for tracing
  549. * @rx_mpdu_cmd_hdr_size: used for tracing, amount of data before the
  550. * start of the 802.11 header in the @rx_mpdu_cmd
  551. * @dflt_pwr_limit: default power limit fetched from the platform (ACPI)
  552. * @dbg_dest_tlv: points to the destination TLV for debug
  553. * @dbg_conf_tlv: array of pointers to configuration TLVs for debug
  554. * @dbg_trigger_tlv: array of pointers to triggers TLVs for debug
  555. * @dbg_dest_reg_num: num of reg_ops in %dbg_dest_tlv
  556. */
  557. struct iwl_trans {
  558. const struct iwl_trans_ops *ops;
  559. struct iwl_op_mode *op_mode;
  560. const struct iwl_cfg *cfg;
  561. enum iwl_trans_state state;
  562. unsigned long status;
  563. struct device *dev;
  564. u32 hw_rev;
  565. u32 hw_id;
  566. char hw_id_str[52];
  567. u8 rx_mpdu_cmd, rx_mpdu_cmd_hdr_size;
  568. bool pm_support;
  569. bool ltr_enabled;
  570. /* The following fields are internal only */
  571. struct kmem_cache *dev_cmd_pool;
  572. size_t dev_cmd_headroom;
  573. char dev_cmd_pool_name[50];
  574. struct dentry *dbgfs_dir;
  575. #ifdef CONFIG_LOCKDEP
  576. struct lockdep_map sync_cmd_lockdep_map;
  577. #endif
  578. u64 dflt_pwr_limit;
  579. const struct iwl_fw_dbg_dest_tlv *dbg_dest_tlv;
  580. const struct iwl_fw_dbg_conf_tlv *dbg_conf_tlv[FW_DBG_CONF_MAX];
  581. struct iwl_fw_dbg_trigger_tlv * const *dbg_trigger_tlv;
  582. u8 dbg_dest_reg_num;
  583. enum iwl_d0i3_mode d0i3_mode;
  584. bool wowlan_d0i3;
  585. /* pointer to trans specific struct */
  586. /*Ensure that this pointer will always be aligned to sizeof pointer */
  587. char trans_specific[0] __aligned(sizeof(void *));
  588. };
  589. static inline void iwl_trans_configure(struct iwl_trans *trans,
  590. const struct iwl_trans_config *trans_cfg)
  591. {
  592. trans->op_mode = trans_cfg->op_mode;
  593. trans->ops->configure(trans, trans_cfg);
  594. }
  595. static inline int _iwl_trans_start_hw(struct iwl_trans *trans, bool low_power)
  596. {
  597. might_sleep();
  598. return trans->ops->start_hw(trans, low_power);
  599. }
  600. static inline int iwl_trans_start_hw(struct iwl_trans *trans)
  601. {
  602. return trans->ops->start_hw(trans, true);
  603. }
  604. static inline void iwl_trans_op_mode_leave(struct iwl_trans *trans)
  605. {
  606. might_sleep();
  607. if (trans->ops->op_mode_leave)
  608. trans->ops->op_mode_leave(trans);
  609. trans->op_mode = NULL;
  610. trans->state = IWL_TRANS_NO_FW;
  611. }
  612. static inline void iwl_trans_fw_alive(struct iwl_trans *trans, u32 scd_addr)
  613. {
  614. might_sleep();
  615. trans->state = IWL_TRANS_FW_ALIVE;
  616. trans->ops->fw_alive(trans, scd_addr);
  617. }
  618. static inline int iwl_trans_start_fw(struct iwl_trans *trans,
  619. const struct fw_img *fw,
  620. bool run_in_rfkill)
  621. {
  622. might_sleep();
  623. WARN_ON_ONCE(!trans->rx_mpdu_cmd);
  624. clear_bit(STATUS_FW_ERROR, &trans->status);
  625. return trans->ops->start_fw(trans, fw, run_in_rfkill);
  626. }
  627. static inline int iwl_trans_update_sf(struct iwl_trans *trans,
  628. struct iwl_sf_region *st_fwrd_space)
  629. {
  630. might_sleep();
  631. if (trans->ops->update_sf)
  632. return trans->ops->update_sf(trans, st_fwrd_space);
  633. return 0;
  634. }
  635. static inline void _iwl_trans_stop_device(struct iwl_trans *trans,
  636. bool low_power)
  637. {
  638. might_sleep();
  639. trans->ops->stop_device(trans, low_power);
  640. trans->state = IWL_TRANS_NO_FW;
  641. }
  642. static inline void iwl_trans_stop_device(struct iwl_trans *trans)
  643. {
  644. _iwl_trans_stop_device(trans, true);
  645. }
  646. static inline void iwl_trans_d3_suspend(struct iwl_trans *trans, bool test)
  647. {
  648. might_sleep();
  649. trans->ops->d3_suspend(trans, test);
  650. }
  651. static inline int iwl_trans_d3_resume(struct iwl_trans *trans,
  652. enum iwl_d3_status *status,
  653. bool test)
  654. {
  655. might_sleep();
  656. return trans->ops->d3_resume(trans, status, test);
  657. }
  658. static inline void iwl_trans_ref(struct iwl_trans *trans)
  659. {
  660. if (trans->ops->ref)
  661. trans->ops->ref(trans);
  662. }
  663. static inline void iwl_trans_unref(struct iwl_trans *trans)
  664. {
  665. if (trans->ops->unref)
  666. trans->ops->unref(trans);
  667. }
  668. static inline void iwl_trans_suspend(struct iwl_trans *trans)
  669. {
  670. if (trans->ops->suspend)
  671. trans->ops->suspend(trans);
  672. }
  673. static inline void iwl_trans_resume(struct iwl_trans *trans)
  674. {
  675. if (trans->ops->resume)
  676. trans->ops->resume(trans);
  677. }
  678. static inline struct iwl_trans_dump_data *
  679. iwl_trans_dump_data(struct iwl_trans *trans)
  680. {
  681. if (!trans->ops->dump_data)
  682. return NULL;
  683. return trans->ops->dump_data(trans);
  684. }
  685. static inline int iwl_trans_send_cmd(struct iwl_trans *trans,
  686. struct iwl_host_cmd *cmd)
  687. {
  688. int ret;
  689. if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  690. test_bit(STATUS_RFKILL, &trans->status)))
  691. return -ERFKILL;
  692. if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
  693. return -EIO;
  694. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
  695. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  696. return -EIO;
  697. }
  698. if (!(cmd->flags & CMD_ASYNC))
  699. lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
  700. ret = trans->ops->send_cmd(trans, cmd);
  701. if (!(cmd->flags & CMD_ASYNC))
  702. lock_map_release(&trans->sync_cmd_lockdep_map);
  703. return ret;
  704. }
  705. static inline struct iwl_device_cmd *
  706. iwl_trans_alloc_tx_cmd(struct iwl_trans *trans)
  707. {
  708. u8 *dev_cmd_ptr = kmem_cache_alloc(trans->dev_cmd_pool, GFP_ATOMIC);
  709. if (unlikely(dev_cmd_ptr == NULL))
  710. return NULL;
  711. return (struct iwl_device_cmd *)
  712. (dev_cmd_ptr + trans->dev_cmd_headroom);
  713. }
  714. static inline void iwl_trans_free_tx_cmd(struct iwl_trans *trans,
  715. struct iwl_device_cmd *dev_cmd)
  716. {
  717. u8 *dev_cmd_ptr = (u8 *)dev_cmd - trans->dev_cmd_headroom;
  718. kmem_cache_free(trans->dev_cmd_pool, dev_cmd_ptr);
  719. }
  720. static inline int iwl_trans_tx(struct iwl_trans *trans, struct sk_buff *skb,
  721. struct iwl_device_cmd *dev_cmd, int queue)
  722. {
  723. if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
  724. return -EIO;
  725. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  726. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  727. return trans->ops->tx(trans, skb, dev_cmd, queue);
  728. }
  729. static inline void iwl_trans_reclaim(struct iwl_trans *trans, int queue,
  730. int ssn, struct sk_buff_head *skbs)
  731. {
  732. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  733. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  734. trans->ops->reclaim(trans, queue, ssn, skbs);
  735. }
  736. static inline void iwl_trans_txq_disable(struct iwl_trans *trans, int queue,
  737. bool configure_scd)
  738. {
  739. trans->ops->txq_disable(trans, queue, configure_scd);
  740. }
  741. static inline void
  742. iwl_trans_txq_enable_cfg(struct iwl_trans *trans, int queue, u16 ssn,
  743. const struct iwl_trans_txq_scd_cfg *cfg,
  744. unsigned int queue_wdg_timeout)
  745. {
  746. might_sleep();
  747. if (unlikely((trans->state != IWL_TRANS_FW_ALIVE)))
  748. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  749. trans->ops->txq_enable(trans, queue, ssn, cfg, queue_wdg_timeout);
  750. }
  751. static inline void iwl_trans_txq_enable(struct iwl_trans *trans, int queue,
  752. int fifo, int sta_id, int tid,
  753. int frame_limit, u16 ssn,
  754. unsigned int queue_wdg_timeout)
  755. {
  756. struct iwl_trans_txq_scd_cfg cfg = {
  757. .fifo = fifo,
  758. .sta_id = sta_id,
  759. .tid = tid,
  760. .frame_limit = frame_limit,
  761. .aggregate = sta_id >= 0,
  762. };
  763. iwl_trans_txq_enable_cfg(trans, queue, ssn, &cfg, queue_wdg_timeout);
  764. }
  765. static inline
  766. void iwl_trans_ac_txq_enable(struct iwl_trans *trans, int queue, int fifo,
  767. unsigned int queue_wdg_timeout)
  768. {
  769. struct iwl_trans_txq_scd_cfg cfg = {
  770. .fifo = fifo,
  771. .sta_id = -1,
  772. .tid = IWL_MAX_TID_COUNT,
  773. .frame_limit = IWL_FRAME_LIMIT,
  774. .aggregate = false,
  775. };
  776. iwl_trans_txq_enable_cfg(trans, queue, 0, &cfg, queue_wdg_timeout);
  777. }
  778. static inline void iwl_trans_freeze_txq_timer(struct iwl_trans *trans,
  779. unsigned long txqs,
  780. bool freeze)
  781. {
  782. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  783. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  784. if (trans->ops->freeze_txq_timer)
  785. trans->ops->freeze_txq_timer(trans, txqs, freeze);
  786. }
  787. static inline int iwl_trans_wait_tx_queue_empty(struct iwl_trans *trans,
  788. u32 txqs)
  789. {
  790. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  791. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  792. return trans->ops->wait_tx_queue_empty(trans, txqs);
  793. }
  794. static inline int iwl_trans_dbgfs_register(struct iwl_trans *trans,
  795. struct dentry *dir)
  796. {
  797. return trans->ops->dbgfs_register(trans, dir);
  798. }
  799. static inline void iwl_trans_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  800. {
  801. trans->ops->write8(trans, ofs, val);
  802. }
  803. static inline void iwl_trans_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  804. {
  805. trans->ops->write32(trans, ofs, val);
  806. }
  807. static inline u32 iwl_trans_read32(struct iwl_trans *trans, u32 ofs)
  808. {
  809. return trans->ops->read32(trans, ofs);
  810. }
  811. static inline u32 iwl_trans_read_prph(struct iwl_trans *trans, u32 ofs)
  812. {
  813. return trans->ops->read_prph(trans, ofs);
  814. }
  815. static inline void iwl_trans_write_prph(struct iwl_trans *trans, u32 ofs,
  816. u32 val)
  817. {
  818. return trans->ops->write_prph(trans, ofs, val);
  819. }
  820. static inline int iwl_trans_read_mem(struct iwl_trans *trans, u32 addr,
  821. void *buf, int dwords)
  822. {
  823. return trans->ops->read_mem(trans, addr, buf, dwords);
  824. }
  825. #define iwl_trans_read_mem_bytes(trans, addr, buf, bufsize) \
  826. do { \
  827. if (__builtin_constant_p(bufsize)) \
  828. BUILD_BUG_ON((bufsize) % sizeof(u32)); \
  829. iwl_trans_read_mem(trans, addr, buf, (bufsize) / sizeof(u32));\
  830. } while (0)
  831. static inline u32 iwl_trans_read_mem32(struct iwl_trans *trans, u32 addr)
  832. {
  833. u32 value;
  834. if (WARN_ON(iwl_trans_read_mem(trans, addr, &value, 1)))
  835. return 0xa5a5a5a5;
  836. return value;
  837. }
  838. static inline int iwl_trans_write_mem(struct iwl_trans *trans, u32 addr,
  839. const void *buf, int dwords)
  840. {
  841. return trans->ops->write_mem(trans, addr, buf, dwords);
  842. }
  843. static inline u32 iwl_trans_write_mem32(struct iwl_trans *trans, u32 addr,
  844. u32 val)
  845. {
  846. return iwl_trans_write_mem(trans, addr, &val, 1);
  847. }
  848. static inline void iwl_trans_set_pmi(struct iwl_trans *trans, bool state)
  849. {
  850. if (trans->ops->set_pmi)
  851. trans->ops->set_pmi(trans, state);
  852. }
  853. static inline void
  854. iwl_trans_set_bits_mask(struct iwl_trans *trans, u32 reg, u32 mask, u32 value)
  855. {
  856. trans->ops->set_bits_mask(trans, reg, mask, value);
  857. }
  858. #define iwl_trans_grab_nic_access(trans, silent, flags) \
  859. __cond_lock(nic_access, \
  860. likely((trans)->ops->grab_nic_access(trans, silent, flags)))
  861. static inline void __releases(nic_access)
  862. iwl_trans_release_nic_access(struct iwl_trans *trans, unsigned long *flags)
  863. {
  864. trans->ops->release_nic_access(trans, flags);
  865. __release(nic_access);
  866. }
  867. static inline void iwl_trans_fw_error(struct iwl_trans *trans)
  868. {
  869. if (WARN_ON_ONCE(!trans->op_mode))
  870. return;
  871. /* prevent double restarts due to the same erroneous FW */
  872. if (!test_and_set_bit(STATUS_FW_ERROR, &trans->status))
  873. iwl_op_mode_nic_error(trans->op_mode);
  874. }
  875. /*****************************************************
  876. * transport helper functions
  877. *****************************************************/
  878. struct iwl_trans *iwl_trans_alloc(unsigned int priv_size,
  879. struct device *dev,
  880. const struct iwl_cfg *cfg,
  881. const struct iwl_trans_ops *ops,
  882. size_t dev_cmd_headroom);
  883. void iwl_trans_free(struct iwl_trans *trans);
  884. /*****************************************************
  885. * driver (transport) register/unregister functions
  886. ******************************************************/
  887. int __must_check iwl_pci_register_driver(void);
  888. void iwl_pci_unregister_driver(void);
  889. #endif /* __iwl_trans_h__ */