iwl-trans.h 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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_trigger_tlv: array of pointers to triggers TLVs for debug
  549. * @dbg_dest_reg_num: num of reg_ops in %dbg_dest_tlv
  550. */
  551. struct iwl_trans {
  552. const struct iwl_trans_ops *ops;
  553. struct iwl_op_mode *op_mode;
  554. const struct iwl_cfg *cfg;
  555. enum iwl_trans_state state;
  556. unsigned long status;
  557. struct device *dev;
  558. u32 hw_rev;
  559. u32 hw_id;
  560. char hw_id_str[52];
  561. u8 rx_mpdu_cmd, rx_mpdu_cmd_hdr_size;
  562. bool pm_support;
  563. bool ltr_enabled;
  564. /* The following fields are internal only */
  565. struct kmem_cache *dev_cmd_pool;
  566. size_t dev_cmd_headroom;
  567. char dev_cmd_pool_name[50];
  568. struct dentry *dbgfs_dir;
  569. #ifdef CONFIG_LOCKDEP
  570. struct lockdep_map sync_cmd_lockdep_map;
  571. #endif
  572. u64 dflt_pwr_limit;
  573. const struct iwl_fw_dbg_dest_tlv *dbg_dest_tlv;
  574. const struct iwl_fw_dbg_conf_tlv *dbg_conf_tlv[FW_DBG_CONF_MAX];
  575. struct iwl_fw_dbg_trigger_tlv * const *dbg_trigger_tlv;
  576. u8 dbg_dest_reg_num;
  577. enum iwl_d0i3_mode d0i3_mode;
  578. /* pointer to trans specific struct */
  579. /*Ensure that this pointer will always be aligned to sizeof pointer */
  580. char trans_specific[0] __aligned(sizeof(void *));
  581. };
  582. static inline void iwl_trans_configure(struct iwl_trans *trans,
  583. const struct iwl_trans_config *trans_cfg)
  584. {
  585. trans->op_mode = trans_cfg->op_mode;
  586. trans->ops->configure(trans, trans_cfg);
  587. }
  588. static inline int iwl_trans_start_hw(struct iwl_trans *trans)
  589. {
  590. might_sleep();
  591. return trans->ops->start_hw(trans);
  592. }
  593. static inline void iwl_trans_op_mode_leave(struct iwl_trans *trans)
  594. {
  595. might_sleep();
  596. if (trans->ops->op_mode_leave)
  597. trans->ops->op_mode_leave(trans);
  598. trans->op_mode = NULL;
  599. trans->state = IWL_TRANS_NO_FW;
  600. }
  601. static inline void iwl_trans_fw_alive(struct iwl_trans *trans, u32 scd_addr)
  602. {
  603. might_sleep();
  604. trans->state = IWL_TRANS_FW_ALIVE;
  605. trans->ops->fw_alive(trans, scd_addr);
  606. }
  607. static inline int iwl_trans_start_fw(struct iwl_trans *trans,
  608. const struct fw_img *fw,
  609. bool run_in_rfkill)
  610. {
  611. might_sleep();
  612. WARN_ON_ONCE(!trans->rx_mpdu_cmd);
  613. clear_bit(STATUS_FW_ERROR, &trans->status);
  614. return trans->ops->start_fw(trans, fw, run_in_rfkill);
  615. }
  616. static inline int iwl_trans_update_sf(struct iwl_trans *trans,
  617. struct iwl_sf_region *st_fwrd_space)
  618. {
  619. might_sleep();
  620. if (trans->ops->update_sf)
  621. return trans->ops->update_sf(trans, st_fwrd_space);
  622. return 0;
  623. }
  624. static inline void iwl_trans_stop_device(struct iwl_trans *trans)
  625. {
  626. might_sleep();
  627. trans->ops->stop_device(trans);
  628. trans->state = IWL_TRANS_NO_FW;
  629. }
  630. static inline void iwl_trans_d3_suspend(struct iwl_trans *trans, bool test)
  631. {
  632. might_sleep();
  633. trans->ops->d3_suspend(trans, test);
  634. }
  635. static inline int iwl_trans_d3_resume(struct iwl_trans *trans,
  636. enum iwl_d3_status *status,
  637. bool test)
  638. {
  639. might_sleep();
  640. return trans->ops->d3_resume(trans, status, test);
  641. }
  642. static inline void iwl_trans_ref(struct iwl_trans *trans)
  643. {
  644. if (trans->ops->ref)
  645. trans->ops->ref(trans);
  646. }
  647. static inline void iwl_trans_unref(struct iwl_trans *trans)
  648. {
  649. if (trans->ops->unref)
  650. trans->ops->unref(trans);
  651. }
  652. static inline void iwl_trans_suspend(struct iwl_trans *trans)
  653. {
  654. if (trans->ops->suspend)
  655. trans->ops->suspend(trans);
  656. }
  657. static inline void iwl_trans_resume(struct iwl_trans *trans)
  658. {
  659. if (trans->ops->resume)
  660. trans->ops->resume(trans);
  661. }
  662. static inline struct iwl_trans_dump_data *
  663. iwl_trans_dump_data(struct iwl_trans *trans)
  664. {
  665. if (!trans->ops->dump_data)
  666. return NULL;
  667. return trans->ops->dump_data(trans);
  668. }
  669. static inline int iwl_trans_send_cmd(struct iwl_trans *trans,
  670. struct iwl_host_cmd *cmd)
  671. {
  672. int ret;
  673. if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  674. test_bit(STATUS_RFKILL, &trans->status)))
  675. return -ERFKILL;
  676. if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
  677. return -EIO;
  678. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
  679. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  680. return -EIO;
  681. }
  682. if (!(cmd->flags & CMD_ASYNC))
  683. lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
  684. ret = trans->ops->send_cmd(trans, cmd);
  685. if (!(cmd->flags & CMD_ASYNC))
  686. lock_map_release(&trans->sync_cmd_lockdep_map);
  687. return ret;
  688. }
  689. static inline struct iwl_device_cmd *
  690. iwl_trans_alloc_tx_cmd(struct iwl_trans *trans)
  691. {
  692. u8 *dev_cmd_ptr = kmem_cache_alloc(trans->dev_cmd_pool, GFP_ATOMIC);
  693. if (unlikely(dev_cmd_ptr == NULL))
  694. return NULL;
  695. return (struct iwl_device_cmd *)
  696. (dev_cmd_ptr + trans->dev_cmd_headroom);
  697. }
  698. static inline void iwl_trans_free_tx_cmd(struct iwl_trans *trans,
  699. struct iwl_device_cmd *dev_cmd)
  700. {
  701. u8 *dev_cmd_ptr = (u8 *)dev_cmd - trans->dev_cmd_headroom;
  702. kmem_cache_free(trans->dev_cmd_pool, dev_cmd_ptr);
  703. }
  704. static inline int iwl_trans_tx(struct iwl_trans *trans, struct sk_buff *skb,
  705. struct iwl_device_cmd *dev_cmd, int queue)
  706. {
  707. if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
  708. return -EIO;
  709. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  710. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  711. return trans->ops->tx(trans, skb, dev_cmd, queue);
  712. }
  713. static inline void iwl_trans_reclaim(struct iwl_trans *trans, int queue,
  714. int ssn, struct sk_buff_head *skbs)
  715. {
  716. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  717. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  718. trans->ops->reclaim(trans, queue, ssn, skbs);
  719. }
  720. static inline void iwl_trans_txq_disable(struct iwl_trans *trans, int queue,
  721. bool configure_scd)
  722. {
  723. trans->ops->txq_disable(trans, queue, configure_scd);
  724. }
  725. static inline void
  726. iwl_trans_txq_enable_cfg(struct iwl_trans *trans, int queue, u16 ssn,
  727. const struct iwl_trans_txq_scd_cfg *cfg,
  728. unsigned int queue_wdg_timeout)
  729. {
  730. might_sleep();
  731. if (unlikely((trans->state != IWL_TRANS_FW_ALIVE)))
  732. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  733. trans->ops->txq_enable(trans, queue, ssn, cfg, queue_wdg_timeout);
  734. }
  735. static inline void iwl_trans_txq_enable(struct iwl_trans *trans, int queue,
  736. int fifo, int sta_id, int tid,
  737. int frame_limit, u16 ssn,
  738. unsigned int queue_wdg_timeout)
  739. {
  740. struct iwl_trans_txq_scd_cfg cfg = {
  741. .fifo = fifo,
  742. .sta_id = sta_id,
  743. .tid = tid,
  744. .frame_limit = frame_limit,
  745. .aggregate = sta_id >= 0,
  746. };
  747. iwl_trans_txq_enable_cfg(trans, queue, ssn, &cfg, queue_wdg_timeout);
  748. }
  749. static inline
  750. void iwl_trans_ac_txq_enable(struct iwl_trans *trans, int queue, int fifo,
  751. unsigned int queue_wdg_timeout)
  752. {
  753. struct iwl_trans_txq_scd_cfg cfg = {
  754. .fifo = fifo,
  755. .sta_id = -1,
  756. .tid = IWL_MAX_TID_COUNT,
  757. .frame_limit = IWL_FRAME_LIMIT,
  758. .aggregate = false,
  759. };
  760. iwl_trans_txq_enable_cfg(trans, queue, 0, &cfg, queue_wdg_timeout);
  761. }
  762. static inline int iwl_trans_wait_tx_queue_empty(struct iwl_trans *trans,
  763. u32 txqs)
  764. {
  765. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  766. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  767. return trans->ops->wait_tx_queue_empty(trans, txqs);
  768. }
  769. static inline int iwl_trans_dbgfs_register(struct iwl_trans *trans,
  770. struct dentry *dir)
  771. {
  772. return trans->ops->dbgfs_register(trans, dir);
  773. }
  774. static inline void iwl_trans_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  775. {
  776. trans->ops->write8(trans, ofs, val);
  777. }
  778. static inline void iwl_trans_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  779. {
  780. trans->ops->write32(trans, ofs, val);
  781. }
  782. static inline u32 iwl_trans_read32(struct iwl_trans *trans, u32 ofs)
  783. {
  784. return trans->ops->read32(trans, ofs);
  785. }
  786. static inline u32 iwl_trans_read_prph(struct iwl_trans *trans, u32 ofs)
  787. {
  788. return trans->ops->read_prph(trans, ofs);
  789. }
  790. static inline void iwl_trans_write_prph(struct iwl_trans *trans, u32 ofs,
  791. u32 val)
  792. {
  793. return trans->ops->write_prph(trans, ofs, val);
  794. }
  795. static inline int iwl_trans_read_mem(struct iwl_trans *trans, u32 addr,
  796. void *buf, int dwords)
  797. {
  798. return trans->ops->read_mem(trans, addr, buf, dwords);
  799. }
  800. #define iwl_trans_read_mem_bytes(trans, addr, buf, bufsize) \
  801. do { \
  802. if (__builtin_constant_p(bufsize)) \
  803. BUILD_BUG_ON((bufsize) % sizeof(u32)); \
  804. iwl_trans_read_mem(trans, addr, buf, (bufsize) / sizeof(u32));\
  805. } while (0)
  806. static inline u32 iwl_trans_read_mem32(struct iwl_trans *trans, u32 addr)
  807. {
  808. u32 value;
  809. if (WARN_ON(iwl_trans_read_mem(trans, addr, &value, 1)))
  810. return 0xa5a5a5a5;
  811. return value;
  812. }
  813. static inline int iwl_trans_write_mem(struct iwl_trans *trans, u32 addr,
  814. const void *buf, int dwords)
  815. {
  816. return trans->ops->write_mem(trans, addr, buf, dwords);
  817. }
  818. static inline u32 iwl_trans_write_mem32(struct iwl_trans *trans, u32 addr,
  819. u32 val)
  820. {
  821. return iwl_trans_write_mem(trans, addr, &val, 1);
  822. }
  823. static inline void iwl_trans_set_pmi(struct iwl_trans *trans, bool state)
  824. {
  825. if (trans->ops->set_pmi)
  826. trans->ops->set_pmi(trans, state);
  827. }
  828. static inline void
  829. iwl_trans_set_bits_mask(struct iwl_trans *trans, u32 reg, u32 mask, u32 value)
  830. {
  831. trans->ops->set_bits_mask(trans, reg, mask, value);
  832. }
  833. #define iwl_trans_grab_nic_access(trans, silent, flags) \
  834. __cond_lock(nic_access, \
  835. likely((trans)->ops->grab_nic_access(trans, silent, flags)))
  836. static inline void __releases(nic_access)
  837. iwl_trans_release_nic_access(struct iwl_trans *trans, unsigned long *flags)
  838. {
  839. trans->ops->release_nic_access(trans, flags);
  840. __release(nic_access);
  841. }
  842. static inline void iwl_trans_fw_error(struct iwl_trans *trans)
  843. {
  844. if (WARN_ON_ONCE(!trans->op_mode))
  845. return;
  846. /* prevent double restarts due to the same erroneous FW */
  847. if (!test_and_set_bit(STATUS_FW_ERROR, &trans->status))
  848. iwl_op_mode_nic_error(trans->op_mode);
  849. }
  850. /*****************************************************
  851. * driver (transport) register/unregister functions
  852. ******************************************************/
  853. int __must_check iwl_pci_register_driver(void);
  854. void iwl_pci_unregister_driver(void);
  855. static inline void trans_lockdep_init(struct iwl_trans *trans)
  856. {
  857. #ifdef CONFIG_LOCKDEP
  858. static struct lock_class_key __key;
  859. lockdep_init_map(&trans->sync_cmd_lockdep_map, "sync_cmd_lockdep_map",
  860. &__key, 0);
  861. #endif
  862. }
  863. #endif /* __iwl_trans_h__ */