iwl-trans.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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 - 2014 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 - 2014 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 tranport 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 commaned 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 functionnality (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- from that point on, the HW can send interrupts
  386. * May sleep
  387. * @op_mode_leave: Turn off the HW RF kill indication if on
  388. * May sleep
  389. * @start_fw: allocates and inits all the resources for the transport
  390. * layer. Also kick a fw image.
  391. * May sleep
  392. * @fw_alive: called when the fw sends alive notification. If the fw provides
  393. * the SCD base address in SRAM, then provide it here, or 0 otherwise.
  394. * May sleep
  395. * @stop_device: stops the whole device (embedded CPU put to reset) and stops
  396. * the HW. From that point on, the HW will be in low power but will still
  397. * issue interrupt if the HW RF kill is triggered. This callback must do
  398. * the right thing and not crash even if start_hw() was called but not
  399. * start_fw(). May sleep
  400. * @d3_suspend: put the device into the correct mode for WoWLAN during
  401. * suspend. This is optional, if not implemented WoWLAN will not be
  402. * supported. This callback may sleep.
  403. * @d3_resume: resume the device after WoWLAN, enabling the opmode to
  404. * talk to the WoWLAN image to get its status. This is optional, if not
  405. * implemented WoWLAN will not be supported. This callback may sleep.
  406. * @send_cmd:send a host command. Must return -ERFKILL if RFkill is asserted.
  407. * If RFkill is asserted in the middle of a SYNC host command, it must
  408. * return -ERFKILL straight away.
  409. * May sleep only if CMD_ASYNC is not set
  410. * @tx: send an skb
  411. * Must be atomic
  412. * @reclaim: free packet until ssn. Returns a list of freed packets.
  413. * Must be atomic
  414. * @txq_enable: setup a queue. To setup an AC queue, use the
  415. * iwl_trans_ac_txq_enable wrapper. fw_alive must have been called before
  416. * this one. The op_mode must not configure the HCMD queue. The scheduler
  417. * configuration may be %NULL, in which case the hardware will not be
  418. * configured. May sleep.
  419. * @txq_disable: de-configure a Tx queue to send AMPDUs
  420. * Must be atomic
  421. * @wait_tx_queue_empty: wait until tx queues are empty. May sleep.
  422. * @dbgfs_register: add the dbgfs files under this directory. Files will be
  423. * automatically deleted.
  424. * @write8: write a u8 to a register at offset ofs from the BAR
  425. * @write32: write a u32 to a register at offset ofs from the BAR
  426. * @read32: read a u32 register at offset ofs from the BAR
  427. * @read_prph: read a DWORD from a periphery register
  428. * @write_prph: write a DWORD to a periphery register
  429. * @read_mem: read device's SRAM in DWORD
  430. * @write_mem: write device's SRAM in DWORD. If %buf is %NULL, then the memory
  431. * will be zeroed.
  432. * @configure: configure parameters required by the transport layer from
  433. * the op_mode. May be called several times before start_fw, can't be
  434. * called after that.
  435. * @set_pmi: set the power pmi state
  436. * @grab_nic_access: wake the NIC to be able to access non-HBUS regs.
  437. * Sleeping is not allowed between grab_nic_access and
  438. * release_nic_access.
  439. * @release_nic_access: let the NIC go to sleep. The "flags" parameter
  440. * must be the same one that was sent before to the grab_nic_access.
  441. * @set_bits_mask - set SRAM register according to value and mask.
  442. * @ref: grab a reference to the transport/FW layers, disallowing
  443. * certain low power states
  444. * @unref: release a reference previously taken with @ref. Note that
  445. * initially the reference count is 1, making an initial @unref
  446. * necessary to allow low power states.
  447. * @dump_data: return a vmalloc'ed buffer with debug data, maybe containing last
  448. * TX'ed commands and similar. The buffer will be vfree'd by the caller.
  449. * Note that the transport must fill in the proper file headers.
  450. */
  451. struct iwl_trans_ops {
  452. int (*start_hw)(struct iwl_trans *iwl_trans);
  453. void (*op_mode_leave)(struct iwl_trans *iwl_trans);
  454. int (*start_fw)(struct iwl_trans *trans, const struct fw_img *fw,
  455. bool run_in_rfkill);
  456. int (*update_sf)(struct iwl_trans *trans,
  457. struct iwl_sf_region *st_fwrd_space);
  458. void (*fw_alive)(struct iwl_trans *trans, u32 scd_addr);
  459. void (*stop_device)(struct iwl_trans *trans);
  460. void (*d3_suspend)(struct iwl_trans *trans, bool test);
  461. int (*d3_resume)(struct iwl_trans *trans, enum iwl_d3_status *status,
  462. bool test);
  463. int (*send_cmd)(struct iwl_trans *trans, struct iwl_host_cmd *cmd);
  464. int (*tx)(struct iwl_trans *trans, struct sk_buff *skb,
  465. struct iwl_device_cmd *dev_cmd, int queue);
  466. void (*reclaim)(struct iwl_trans *trans, int queue, int ssn,
  467. struct sk_buff_head *skbs);
  468. void (*txq_enable)(struct iwl_trans *trans, int queue, u16 ssn,
  469. const struct iwl_trans_txq_scd_cfg *cfg,
  470. unsigned int queue_wdg_timeout);
  471. void (*txq_disable)(struct iwl_trans *trans, int queue,
  472. bool configure_scd);
  473. int (*dbgfs_register)(struct iwl_trans *trans, struct dentry* dir);
  474. int (*wait_tx_queue_empty)(struct iwl_trans *trans, u32 txq_bm);
  475. void (*write8)(struct iwl_trans *trans, u32 ofs, u8 val);
  476. void (*write32)(struct iwl_trans *trans, u32 ofs, u32 val);
  477. u32 (*read32)(struct iwl_trans *trans, u32 ofs);
  478. u32 (*read_prph)(struct iwl_trans *trans, u32 ofs);
  479. void (*write_prph)(struct iwl_trans *trans, u32 ofs, u32 val);
  480. int (*read_mem)(struct iwl_trans *trans, u32 addr,
  481. void *buf, int dwords);
  482. int (*write_mem)(struct iwl_trans *trans, u32 addr,
  483. const void *buf, int dwords);
  484. void (*configure)(struct iwl_trans *trans,
  485. const struct iwl_trans_config *trans_cfg);
  486. void (*set_pmi)(struct iwl_trans *trans, bool state);
  487. bool (*grab_nic_access)(struct iwl_trans *trans, bool silent,
  488. unsigned long *flags);
  489. void (*release_nic_access)(struct iwl_trans *trans,
  490. unsigned long *flags);
  491. void (*set_bits_mask)(struct iwl_trans *trans, u32 reg, u32 mask,
  492. u32 value);
  493. void (*ref)(struct iwl_trans *trans);
  494. void (*unref)(struct iwl_trans *trans);
  495. void (*suspend)(struct iwl_trans *trans);
  496. void (*resume)(struct iwl_trans *trans);
  497. struct iwl_trans_dump_data *(*dump_data)(struct iwl_trans *trans);
  498. };
  499. /**
  500. * enum iwl_trans_state - state of the transport layer
  501. *
  502. * @IWL_TRANS_NO_FW: no fw has sent an alive response
  503. * @IWL_TRANS_FW_ALIVE: a fw has sent an alive response
  504. */
  505. enum iwl_trans_state {
  506. IWL_TRANS_NO_FW = 0,
  507. IWL_TRANS_FW_ALIVE = 1,
  508. };
  509. /**
  510. * enum iwl_d0i3_mode - d0i3 mode
  511. *
  512. * @IWL_D0I3_MODE_OFF - d0i3 is disabled
  513. * @IWL_D0I3_MODE_ON_IDLE - enter d0i3 when device is idle
  514. * (e.g. no active references)
  515. * @IWL_D0I3_MODE_ON_SUSPEND - enter d0i3 only on suspend
  516. * (in case of 'any' trigger)
  517. */
  518. enum iwl_d0i3_mode {
  519. IWL_D0I3_MODE_OFF = 0,
  520. IWL_D0I3_MODE_ON_IDLE,
  521. IWL_D0I3_MODE_ON_SUSPEND,
  522. };
  523. /**
  524. * struct iwl_trans - transport common data
  525. *
  526. * @ops - pointer to iwl_trans_ops
  527. * @op_mode - pointer to the op_mode
  528. * @cfg - pointer to the configuration
  529. * @status: a bit-mask of transport status flags
  530. * @dev - pointer to struct device * that represents the device
  531. * @hw_id: a u32 with the ID of the device / subdevice.
  532. * Set during transport allocation.
  533. * @hw_id_str: a string with info about HW ID. Set during transport allocation.
  534. * @pm_support: set to true in start_hw if link pm is supported
  535. * @ltr_enabled: set to true if the LTR is enabled
  536. * @dev_cmd_pool: pool for Tx cmd allocation - for internal use only.
  537. * The user should use iwl_trans_{alloc,free}_tx_cmd.
  538. * @dev_cmd_headroom: room needed for the transport's private use before the
  539. * device_cmd for Tx - for internal use only
  540. * The user should use iwl_trans_{alloc,free}_tx_cmd.
  541. * @rx_mpdu_cmd: MPDU RX command ID, must be assigned by opmode before
  542. * starting the firmware, used for tracing
  543. * @rx_mpdu_cmd_hdr_size: used for tracing, amount of data before the
  544. * start of the 802.11 header in the @rx_mpdu_cmd
  545. * @dflt_pwr_limit: default power limit fetched from the platform (ACPI)
  546. * @dbg_dest_tlv: points to the destination TLV for debug
  547. * @dbg_conf_tlv: array of pointers to configuration TLVs for debug
  548. * @dbg_dest_reg_num: num of reg_ops in %dbg_dest_tlv
  549. */
  550. struct iwl_trans {
  551. const struct iwl_trans_ops *ops;
  552. struct iwl_op_mode *op_mode;
  553. const struct iwl_cfg *cfg;
  554. enum iwl_trans_state state;
  555. unsigned long status;
  556. struct device *dev;
  557. u32 hw_rev;
  558. u32 hw_id;
  559. char hw_id_str[52];
  560. u8 rx_mpdu_cmd, rx_mpdu_cmd_hdr_size;
  561. bool pm_support;
  562. bool ltr_enabled;
  563. /* The following fields are internal only */
  564. struct kmem_cache *dev_cmd_pool;
  565. size_t dev_cmd_headroom;
  566. char dev_cmd_pool_name[50];
  567. struct dentry *dbgfs_dir;
  568. #ifdef CONFIG_LOCKDEP
  569. struct lockdep_map sync_cmd_lockdep_map;
  570. #endif
  571. u64 dflt_pwr_limit;
  572. const struct iwl_fw_dbg_dest_tlv *dbg_dest_tlv;
  573. const struct iwl_fw_dbg_conf_tlv *dbg_conf_tlv[FW_DBG_MAX];
  574. u8 dbg_dest_reg_num;
  575. enum iwl_d0i3_mode d0i3_mode;
  576. /* pointer to trans specific struct */
  577. /*Ensure that this pointer will always be aligned to sizeof pointer */
  578. char trans_specific[0] __aligned(sizeof(void *));
  579. };
  580. static inline void iwl_trans_configure(struct iwl_trans *trans,
  581. const struct iwl_trans_config *trans_cfg)
  582. {
  583. trans->op_mode = trans_cfg->op_mode;
  584. trans->ops->configure(trans, trans_cfg);
  585. }
  586. static inline int iwl_trans_start_hw(struct iwl_trans *trans)
  587. {
  588. might_sleep();
  589. return trans->ops->start_hw(trans);
  590. }
  591. static inline void iwl_trans_op_mode_leave(struct iwl_trans *trans)
  592. {
  593. might_sleep();
  594. if (trans->ops->op_mode_leave)
  595. trans->ops->op_mode_leave(trans);
  596. trans->op_mode = NULL;
  597. trans->state = IWL_TRANS_NO_FW;
  598. }
  599. static inline void iwl_trans_fw_alive(struct iwl_trans *trans, u32 scd_addr)
  600. {
  601. might_sleep();
  602. trans->state = IWL_TRANS_FW_ALIVE;
  603. trans->ops->fw_alive(trans, scd_addr);
  604. }
  605. static inline int iwl_trans_start_fw(struct iwl_trans *trans,
  606. const struct fw_img *fw,
  607. bool run_in_rfkill)
  608. {
  609. might_sleep();
  610. WARN_ON_ONCE(!trans->rx_mpdu_cmd);
  611. clear_bit(STATUS_FW_ERROR, &trans->status);
  612. return trans->ops->start_fw(trans, fw, run_in_rfkill);
  613. }
  614. static inline int iwl_trans_update_sf(struct iwl_trans *trans,
  615. struct iwl_sf_region *st_fwrd_space)
  616. {
  617. might_sleep();
  618. if (trans->ops->update_sf)
  619. return trans->ops->update_sf(trans, st_fwrd_space);
  620. return 0;
  621. }
  622. static inline void iwl_trans_stop_device(struct iwl_trans *trans)
  623. {
  624. might_sleep();
  625. trans->ops->stop_device(trans);
  626. trans->state = IWL_TRANS_NO_FW;
  627. }
  628. static inline void iwl_trans_d3_suspend(struct iwl_trans *trans, bool test)
  629. {
  630. might_sleep();
  631. trans->ops->d3_suspend(trans, test);
  632. }
  633. static inline int iwl_trans_d3_resume(struct iwl_trans *trans,
  634. enum iwl_d3_status *status,
  635. bool test)
  636. {
  637. might_sleep();
  638. return trans->ops->d3_resume(trans, status, test);
  639. }
  640. static inline void iwl_trans_ref(struct iwl_trans *trans)
  641. {
  642. if (trans->ops->ref)
  643. trans->ops->ref(trans);
  644. }
  645. static inline void iwl_trans_unref(struct iwl_trans *trans)
  646. {
  647. if (trans->ops->unref)
  648. trans->ops->unref(trans);
  649. }
  650. static inline void iwl_trans_suspend(struct iwl_trans *trans)
  651. {
  652. if (trans->ops->suspend)
  653. trans->ops->suspend(trans);
  654. }
  655. static inline void iwl_trans_resume(struct iwl_trans *trans)
  656. {
  657. if (trans->ops->resume)
  658. trans->ops->resume(trans);
  659. }
  660. static inline struct iwl_trans_dump_data *
  661. iwl_trans_dump_data(struct iwl_trans *trans)
  662. {
  663. if (!trans->ops->dump_data)
  664. return NULL;
  665. return trans->ops->dump_data(trans);
  666. }
  667. static inline int iwl_trans_send_cmd(struct iwl_trans *trans,
  668. struct iwl_host_cmd *cmd)
  669. {
  670. int ret;
  671. if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  672. test_bit(STATUS_RFKILL, &trans->status)))
  673. return -ERFKILL;
  674. if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
  675. return -EIO;
  676. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
  677. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  678. return -EIO;
  679. }
  680. if (!(cmd->flags & CMD_ASYNC))
  681. lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
  682. ret = trans->ops->send_cmd(trans, cmd);
  683. if (!(cmd->flags & CMD_ASYNC))
  684. lock_map_release(&trans->sync_cmd_lockdep_map);
  685. return ret;
  686. }
  687. static inline struct iwl_device_cmd *
  688. iwl_trans_alloc_tx_cmd(struct iwl_trans *trans)
  689. {
  690. u8 *dev_cmd_ptr = kmem_cache_alloc(trans->dev_cmd_pool, GFP_ATOMIC);
  691. if (unlikely(dev_cmd_ptr == NULL))
  692. return NULL;
  693. return (struct iwl_device_cmd *)
  694. (dev_cmd_ptr + trans->dev_cmd_headroom);
  695. }
  696. static inline void iwl_trans_free_tx_cmd(struct iwl_trans *trans,
  697. struct iwl_device_cmd *dev_cmd)
  698. {
  699. u8 *dev_cmd_ptr = (u8 *)dev_cmd - trans->dev_cmd_headroom;
  700. kmem_cache_free(trans->dev_cmd_pool, dev_cmd_ptr);
  701. }
  702. static inline int iwl_trans_tx(struct iwl_trans *trans, struct sk_buff *skb,
  703. struct iwl_device_cmd *dev_cmd, int queue)
  704. {
  705. if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
  706. return -EIO;
  707. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  708. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  709. return trans->ops->tx(trans, skb, dev_cmd, queue);
  710. }
  711. static inline void iwl_trans_reclaim(struct iwl_trans *trans, int queue,
  712. int ssn, struct sk_buff_head *skbs)
  713. {
  714. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  715. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  716. trans->ops->reclaim(trans, queue, ssn, skbs);
  717. }
  718. static inline void iwl_trans_txq_disable(struct iwl_trans *trans, int queue,
  719. bool configure_scd)
  720. {
  721. trans->ops->txq_disable(trans, queue, configure_scd);
  722. }
  723. static inline void
  724. iwl_trans_txq_enable_cfg(struct iwl_trans *trans, int queue, u16 ssn,
  725. const struct iwl_trans_txq_scd_cfg *cfg,
  726. unsigned int queue_wdg_timeout)
  727. {
  728. might_sleep();
  729. if (unlikely((trans->state != IWL_TRANS_FW_ALIVE)))
  730. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  731. trans->ops->txq_enable(trans, queue, ssn, cfg, queue_wdg_timeout);
  732. }
  733. static inline void iwl_trans_txq_enable(struct iwl_trans *trans, int queue,
  734. int fifo, int sta_id, int tid,
  735. int frame_limit, u16 ssn,
  736. unsigned int queue_wdg_timeout)
  737. {
  738. struct iwl_trans_txq_scd_cfg cfg = {
  739. .fifo = fifo,
  740. .sta_id = sta_id,
  741. .tid = tid,
  742. .frame_limit = frame_limit,
  743. .aggregate = sta_id >= 0,
  744. };
  745. iwl_trans_txq_enable_cfg(trans, queue, ssn, &cfg, queue_wdg_timeout);
  746. }
  747. static inline
  748. void iwl_trans_ac_txq_enable(struct iwl_trans *trans, int queue, int fifo,
  749. unsigned int queue_wdg_timeout)
  750. {
  751. struct iwl_trans_txq_scd_cfg cfg = {
  752. .fifo = fifo,
  753. .sta_id = -1,
  754. .tid = IWL_MAX_TID_COUNT,
  755. .frame_limit = IWL_FRAME_LIMIT,
  756. .aggregate = false,
  757. };
  758. iwl_trans_txq_enable_cfg(trans, queue, 0, &cfg, queue_wdg_timeout);
  759. }
  760. static inline int iwl_trans_wait_tx_queue_empty(struct iwl_trans *trans,
  761. u32 txqs)
  762. {
  763. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  764. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  765. return trans->ops->wait_tx_queue_empty(trans, txqs);
  766. }
  767. static inline int iwl_trans_dbgfs_register(struct iwl_trans *trans,
  768. struct dentry *dir)
  769. {
  770. return trans->ops->dbgfs_register(trans, dir);
  771. }
  772. static inline void iwl_trans_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  773. {
  774. trans->ops->write8(trans, ofs, val);
  775. }
  776. static inline void iwl_trans_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  777. {
  778. trans->ops->write32(trans, ofs, val);
  779. }
  780. static inline u32 iwl_trans_read32(struct iwl_trans *trans, u32 ofs)
  781. {
  782. return trans->ops->read32(trans, ofs);
  783. }
  784. static inline u32 iwl_trans_read_prph(struct iwl_trans *trans, u32 ofs)
  785. {
  786. return trans->ops->read_prph(trans, ofs);
  787. }
  788. static inline void iwl_trans_write_prph(struct iwl_trans *trans, u32 ofs,
  789. u32 val)
  790. {
  791. return trans->ops->write_prph(trans, ofs, val);
  792. }
  793. static inline int iwl_trans_read_mem(struct iwl_trans *trans, u32 addr,
  794. void *buf, int dwords)
  795. {
  796. return trans->ops->read_mem(trans, addr, buf, dwords);
  797. }
  798. #define iwl_trans_read_mem_bytes(trans, addr, buf, bufsize) \
  799. do { \
  800. if (__builtin_constant_p(bufsize)) \
  801. BUILD_BUG_ON((bufsize) % sizeof(u32)); \
  802. iwl_trans_read_mem(trans, addr, buf, (bufsize) / sizeof(u32));\
  803. } while (0)
  804. static inline u32 iwl_trans_read_mem32(struct iwl_trans *trans, u32 addr)
  805. {
  806. u32 value;
  807. if (WARN_ON(iwl_trans_read_mem(trans, addr, &value, 1)))
  808. return 0xa5a5a5a5;
  809. return value;
  810. }
  811. static inline int iwl_trans_write_mem(struct iwl_trans *trans, u32 addr,
  812. const void *buf, int dwords)
  813. {
  814. return trans->ops->write_mem(trans, addr, buf, dwords);
  815. }
  816. static inline u32 iwl_trans_write_mem32(struct iwl_trans *trans, u32 addr,
  817. u32 val)
  818. {
  819. return iwl_trans_write_mem(trans, addr, &val, 1);
  820. }
  821. static inline void iwl_trans_set_pmi(struct iwl_trans *trans, bool state)
  822. {
  823. if (trans->ops->set_pmi)
  824. trans->ops->set_pmi(trans, state);
  825. }
  826. static inline void
  827. iwl_trans_set_bits_mask(struct iwl_trans *trans, u32 reg, u32 mask, u32 value)
  828. {
  829. trans->ops->set_bits_mask(trans, reg, mask, value);
  830. }
  831. #define iwl_trans_grab_nic_access(trans, silent, flags) \
  832. __cond_lock(nic_access, \
  833. likely((trans)->ops->grab_nic_access(trans, silent, flags)))
  834. static inline void __releases(nic_access)
  835. iwl_trans_release_nic_access(struct iwl_trans *trans, unsigned long *flags)
  836. {
  837. trans->ops->release_nic_access(trans, flags);
  838. __release(nic_access);
  839. }
  840. static inline void iwl_trans_fw_error(struct iwl_trans *trans)
  841. {
  842. if (WARN_ON_ONCE(!trans->op_mode))
  843. return;
  844. /* prevent double restarts due to the same erroneous FW */
  845. if (!test_and_set_bit(STATUS_FW_ERROR, &trans->status))
  846. iwl_op_mode_nic_error(trans->op_mode);
  847. }
  848. /*****************************************************
  849. * driver (transport) register/unregister functions
  850. ******************************************************/
  851. int __must_check iwl_pci_register_driver(void);
  852. void iwl_pci_unregister_driver(void);
  853. static inline void trans_lockdep_init(struct iwl_trans *trans)
  854. {
  855. #ifdef CONFIG_LOCKDEP
  856. static struct lock_class_key __key;
  857. lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map",
  858. &__key, 0);
  859. #endif
  860. }
  861. #endif /* __iwl_trans_h__ */