iwl-trans.h 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. * those functions retrieve specific information from
  121. * the id field in the iwl_host_cmd struct which contains
  122. * the command id, the group id and the version of the command
  123. * and vice versa
  124. */
  125. static inline u8 iwl_cmd_opcode(u32 cmdid)
  126. {
  127. return cmdid & 0xFF;
  128. }
  129. static inline u8 iwl_cmd_groupid(u32 cmdid)
  130. {
  131. return ((cmdid & 0xFF00) >> 8);
  132. }
  133. static inline u8 iwl_cmd_version(u32 cmdid)
  134. {
  135. return ((cmdid & 0xFF0000) >> 16);
  136. }
  137. static inline u32 iwl_cmd_id(u8 opcode, u8 groupid, u8 version)
  138. {
  139. return opcode + (groupid << 8) + (version << 16);
  140. }
  141. /* make u16 wide id out of u8 group and opcode */
  142. #define WIDE_ID(grp, opcode) ((grp << 8) | opcode)
  143. /* due to the conversion, this group is special; new groups
  144. * should be defined in the appropriate fw-api header files
  145. */
  146. #define IWL_ALWAYS_LONG_GROUP 1
  147. /**
  148. * struct iwl_cmd_header
  149. *
  150. * This header format appears in the beginning of each command sent from the
  151. * driver, and each response/notification received from uCode.
  152. */
  153. struct iwl_cmd_header {
  154. u8 cmd; /* Command ID: REPLY_RXON, etc. */
  155. u8 group_id;
  156. /*
  157. * The driver sets up the sequence number to values of its choosing.
  158. * uCode does not use this value, but passes it back to the driver
  159. * when sending the response to each driver-originated command, so
  160. * the driver can match the response to the command. Since the values
  161. * don't get used by uCode, the driver may set up an arbitrary format.
  162. *
  163. * There is one exception: uCode sets bit 15 when it originates
  164. * the response/notification, i.e. when the response/notification
  165. * is not a direct response to a command sent by the driver. For
  166. * example, uCode issues REPLY_RX when it sends a received frame
  167. * to the driver; it is not a direct response to any driver command.
  168. *
  169. * The Linux driver uses the following format:
  170. *
  171. * 0:7 tfd index - position within TX queue
  172. * 8:12 TX queue id
  173. * 13:14 reserved
  174. * 15 unsolicited RX or uCode-originated notification
  175. */
  176. __le16 sequence;
  177. } __packed;
  178. /**
  179. * struct iwl_cmd_header_wide
  180. *
  181. * This header format appears in the beginning of each command sent from the
  182. * driver, and each response/notification received from uCode.
  183. * this is the wide version that contains more information about the command
  184. * like length, version and command type
  185. */
  186. struct iwl_cmd_header_wide {
  187. u8 cmd;
  188. u8 group_id;
  189. __le16 sequence;
  190. __le16 length;
  191. u8 reserved;
  192. u8 version;
  193. } __packed;
  194. #define FH_RSCSR_FRAME_SIZE_MSK 0x00003FFF /* bits 0-13 */
  195. #define FH_RSCSR_FRAME_INVALID 0x55550000
  196. #define FH_RSCSR_FRAME_ALIGN 0x40
  197. struct iwl_rx_packet {
  198. /*
  199. * The first 4 bytes of the RX frame header contain both the RX frame
  200. * size and some flags.
  201. * Bit fields:
  202. * 31: flag flush RB request
  203. * 30: flag ignore TC (terminal counter) request
  204. * 29: flag fast IRQ request
  205. * 28-14: Reserved
  206. * 13-00: RX frame size
  207. */
  208. __le32 len_n_flags;
  209. struct iwl_cmd_header hdr;
  210. u8 data[];
  211. } __packed;
  212. static inline u32 iwl_rx_packet_len(const struct iwl_rx_packet *pkt)
  213. {
  214. return le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  215. }
  216. static inline u32 iwl_rx_packet_payload_len(const struct iwl_rx_packet *pkt)
  217. {
  218. return iwl_rx_packet_len(pkt) - sizeof(pkt->hdr);
  219. }
  220. /**
  221. * enum CMD_MODE - how to send the host commands ?
  222. *
  223. * @CMD_ASYNC: Return right away and don't wait for the response
  224. * @CMD_WANT_SKB: Not valid with CMD_ASYNC. The caller needs the buffer of
  225. * the response. The caller needs to call iwl_free_resp when done.
  226. * @CMD_HIGH_PRIO: The command is high priority - it goes to the front of the
  227. * command queue, but after other high priority commands. Valid only
  228. * with CMD_ASYNC.
  229. * @CMD_SEND_IN_IDLE: The command should be sent even when the trans is idle.
  230. * @CMD_MAKE_TRANS_IDLE: The command response should mark the trans as idle.
  231. * @CMD_WAKE_UP_TRANS: The command response should wake up the trans
  232. * (i.e. mark it as non-idle).
  233. * @CMD_TB_BITMAP_POS: Position of the first bit for the TB bitmap. We need to
  234. * check that we leave enough room for the TBs bitmap which needs 20 bits.
  235. */
  236. enum CMD_MODE {
  237. CMD_ASYNC = BIT(0),
  238. CMD_WANT_SKB = BIT(1),
  239. CMD_SEND_IN_RFKILL = BIT(2),
  240. CMD_HIGH_PRIO = BIT(3),
  241. CMD_SEND_IN_IDLE = BIT(4),
  242. CMD_MAKE_TRANS_IDLE = BIT(5),
  243. CMD_WAKE_UP_TRANS = BIT(6),
  244. CMD_TB_BITMAP_POS = 11,
  245. };
  246. #define DEF_CMD_PAYLOAD_SIZE 320
  247. /**
  248. * struct iwl_device_cmd
  249. *
  250. * For allocation of the command and tx queues, this establishes the overall
  251. * size of the largest command we send to uCode, except for commands that
  252. * aren't fully copied and use other TFD space.
  253. */
  254. struct iwl_device_cmd {
  255. union {
  256. struct {
  257. struct iwl_cmd_header hdr; /* uCode API */
  258. u8 payload[DEF_CMD_PAYLOAD_SIZE];
  259. };
  260. struct {
  261. struct iwl_cmd_header_wide hdr_wide;
  262. u8 payload_wide[DEF_CMD_PAYLOAD_SIZE -
  263. sizeof(struct iwl_cmd_header_wide) +
  264. sizeof(struct iwl_cmd_header)];
  265. };
  266. };
  267. } __packed;
  268. #define TFD_MAX_PAYLOAD_SIZE (sizeof(struct iwl_device_cmd))
  269. /*
  270. * number of transfer buffers (fragments) per transmit frame descriptor;
  271. * this is just the driver's idea, the hardware supports 20
  272. */
  273. #define IWL_MAX_CMD_TBS_PER_TFD 2
  274. /**
  275. * struct iwl_hcmd_dataflag - flag for each one of the chunks of the command
  276. *
  277. * @IWL_HCMD_DFL_NOCOPY: By default, the command is copied to the host command's
  278. * ring. The transport layer doesn't map the command's buffer to DMA, but
  279. * rather copies it to a previously allocated DMA buffer. This flag tells
  280. * the transport layer not to copy the command, but to map the existing
  281. * buffer (that is passed in) instead. This saves the memcpy and allows
  282. * commands that are bigger than the fixed buffer to be submitted.
  283. * Note that a TFD entry after a NOCOPY one cannot be a normal copied one.
  284. * @IWL_HCMD_DFL_DUP: Only valid without NOCOPY, duplicate the memory for this
  285. * chunk internally and free it again after the command completes. This
  286. * can (currently) be used only once per command.
  287. * Note that a TFD entry after a DUP one cannot be a normal copied one.
  288. */
  289. enum iwl_hcmd_dataflag {
  290. IWL_HCMD_DFL_NOCOPY = BIT(0),
  291. IWL_HCMD_DFL_DUP = BIT(1),
  292. };
  293. /**
  294. * struct iwl_host_cmd - Host command to the uCode
  295. *
  296. * @data: array of chunks that composes the data of the host command
  297. * @resp_pkt: response packet, if %CMD_WANT_SKB was set
  298. * @_rx_page_order: (internally used to free response packet)
  299. * @_rx_page_addr: (internally used to free response packet)
  300. * @flags: can be CMD_*
  301. * @len: array of the lengths of the chunks in data
  302. * @dataflags: IWL_HCMD_DFL_*
  303. * @id: command id of the host command, for wide commands encoding the
  304. * version and group as well
  305. */
  306. struct iwl_host_cmd {
  307. const void *data[IWL_MAX_CMD_TBS_PER_TFD];
  308. struct iwl_rx_packet *resp_pkt;
  309. unsigned long _rx_page_addr;
  310. u32 _rx_page_order;
  311. u32 flags;
  312. u32 id;
  313. u16 len[IWL_MAX_CMD_TBS_PER_TFD];
  314. u8 dataflags[IWL_MAX_CMD_TBS_PER_TFD];
  315. };
  316. static inline void iwl_free_resp(struct iwl_host_cmd *cmd)
  317. {
  318. free_pages(cmd->_rx_page_addr, cmd->_rx_page_order);
  319. }
  320. struct iwl_rx_cmd_buffer {
  321. struct page *_page;
  322. int _offset;
  323. bool _page_stolen;
  324. u32 _rx_page_order;
  325. unsigned int truesize;
  326. };
  327. static inline void *rxb_addr(struct iwl_rx_cmd_buffer *r)
  328. {
  329. return (void *)((unsigned long)page_address(r->_page) + r->_offset);
  330. }
  331. static inline int rxb_offset(struct iwl_rx_cmd_buffer *r)
  332. {
  333. return r->_offset;
  334. }
  335. static inline struct page *rxb_steal_page(struct iwl_rx_cmd_buffer *r)
  336. {
  337. r->_page_stolen = true;
  338. get_page(r->_page);
  339. return r->_page;
  340. }
  341. static inline void iwl_free_rxb(struct iwl_rx_cmd_buffer *r)
  342. {
  343. __free_pages(r->_page, r->_rx_page_order);
  344. }
  345. #define MAX_NO_RECLAIM_CMDS 6
  346. #define IWL_MASK(lo, hi) ((1 << (hi)) | ((1 << (hi)) - (1 << (lo))))
  347. /*
  348. * Maximum number of HW queues the transport layer
  349. * currently supports
  350. */
  351. #define IWL_MAX_HW_QUEUES 32
  352. #define IWL_MAX_TID_COUNT 8
  353. #define IWL_FRAME_LIMIT 64
  354. /**
  355. * enum iwl_wowlan_status - WoWLAN image/device status
  356. * @IWL_D3_STATUS_ALIVE: firmware is still running after resume
  357. * @IWL_D3_STATUS_RESET: device was reset while suspended
  358. */
  359. enum iwl_d3_status {
  360. IWL_D3_STATUS_ALIVE,
  361. IWL_D3_STATUS_RESET,
  362. };
  363. /**
  364. * enum iwl_trans_status: transport status flags
  365. * @STATUS_SYNC_HCMD_ACTIVE: a SYNC command is being processed
  366. * @STATUS_DEVICE_ENABLED: APM is enabled
  367. * @STATUS_TPOWER_PMI: the device might be asleep (need to wake it up)
  368. * @STATUS_INT_ENABLED: interrupts are enabled
  369. * @STATUS_RFKILL: the HW RFkill switch is in KILL position
  370. * @STATUS_FW_ERROR: the fw is in error state
  371. * @STATUS_TRANS_GOING_IDLE: shutting down the trans, only special commands
  372. * are sent
  373. * @STATUS_TRANS_IDLE: the trans is idle - general commands are not to be sent
  374. */
  375. enum iwl_trans_status {
  376. STATUS_SYNC_HCMD_ACTIVE,
  377. STATUS_DEVICE_ENABLED,
  378. STATUS_TPOWER_PMI,
  379. STATUS_INT_ENABLED,
  380. STATUS_RFKILL,
  381. STATUS_FW_ERROR,
  382. STATUS_TRANS_GOING_IDLE,
  383. STATUS_TRANS_IDLE,
  384. };
  385. /**
  386. * struct iwl_trans_config - transport configuration
  387. *
  388. * @op_mode: pointer to the upper layer.
  389. * @cmd_queue: the index of the command queue.
  390. * Must be set before start_fw.
  391. * @cmd_fifo: the fifo for host commands
  392. * @cmd_q_wdg_timeout: the timeout of the watchdog timer for the command queue.
  393. * @no_reclaim_cmds: Some devices erroneously don't set the
  394. * SEQ_RX_FRAME bit on some notifications, this is the
  395. * list of such notifications to filter. Max length is
  396. * %MAX_NO_RECLAIM_CMDS.
  397. * @n_no_reclaim_cmds: # of commands in list
  398. * @rx_buf_size_8k: 8 kB RX buffer size needed for A-MSDUs,
  399. * if unset 4k will be the RX buffer size
  400. * @bc_table_dword: set to true if the BC table expects the byte count to be
  401. * in DWORD (as opposed to bytes)
  402. * @scd_set_active: should the transport configure the SCD for HCMD queue
  403. * @wide_cmd_header: firmware supports wide host command header
  404. * @command_names: array of command names, must be 256 entries
  405. * (one for each command); for debugging only
  406. * @sdio_adma_addr: the default address to set for the ADMA in SDIO mode until
  407. * we get the ALIVE from the uCode
  408. */
  409. struct iwl_trans_config {
  410. struct iwl_op_mode *op_mode;
  411. u8 cmd_queue;
  412. u8 cmd_fifo;
  413. unsigned int cmd_q_wdg_timeout;
  414. const u8 *no_reclaim_cmds;
  415. unsigned int n_no_reclaim_cmds;
  416. bool rx_buf_size_8k;
  417. bool bc_table_dword;
  418. bool scd_set_active;
  419. bool wide_cmd_header;
  420. const char *const *command_names;
  421. u32 sdio_adma_addr;
  422. };
  423. struct iwl_trans_dump_data {
  424. u32 len;
  425. u8 data[];
  426. };
  427. struct iwl_trans;
  428. struct iwl_trans_txq_scd_cfg {
  429. u8 fifo;
  430. s8 sta_id;
  431. u8 tid;
  432. bool aggregate;
  433. int frame_limit;
  434. };
  435. /**
  436. * struct iwl_trans_ops - transport specific operations
  437. *
  438. * All the handlers MUST be implemented
  439. *
  440. * @start_hw: starts the HW. If low_power is true, the NIC needs to be taken
  441. * out of a low power state. From that point on, the HW can send
  442. * interrupts. May sleep.
  443. * @op_mode_leave: Turn off the HW RF kill indication if on
  444. * May sleep
  445. * @start_fw: allocates and inits all the resources for the transport
  446. * layer. Also kick a fw image.
  447. * May sleep
  448. * @fw_alive: called when the fw sends alive notification. If the fw provides
  449. * the SCD base address in SRAM, then provide it here, or 0 otherwise.
  450. * May sleep
  451. * @stop_device: stops the whole device (embedded CPU put to reset) and stops
  452. * the HW. If low_power is true, the NIC will be put in low power state.
  453. * From that point on, the HW will be stopped but will still issue an
  454. * interrupt if the HW RF kill switch is triggered.
  455. * This callback must do the right thing and not crash even if %start_hw()
  456. * was called but not &start_fw(). May sleep.
  457. * @d3_suspend: put the device into the correct mode for WoWLAN during
  458. * suspend. This is optional, if not implemented WoWLAN will not be
  459. * supported. This callback may sleep.
  460. * @d3_resume: resume the device after WoWLAN, enabling the opmode to
  461. * talk to the WoWLAN image to get its status. This is optional, if not
  462. * implemented WoWLAN will not be supported. This callback may sleep.
  463. * @send_cmd:send a host command. Must return -ERFKILL if RFkill is asserted.
  464. * If RFkill is asserted in the middle of a SYNC host command, it must
  465. * return -ERFKILL straight away.
  466. * May sleep only if CMD_ASYNC is not set
  467. * @tx: send an skb
  468. * Must be atomic
  469. * @reclaim: free packet until ssn. Returns a list of freed packets.
  470. * Must be atomic
  471. * @txq_enable: setup a queue. To setup an AC queue, use the
  472. * iwl_trans_ac_txq_enable wrapper. fw_alive must have been called before
  473. * this one. The op_mode must not configure the HCMD queue. The scheduler
  474. * configuration may be %NULL, in which case the hardware will not be
  475. * configured. May sleep.
  476. * @txq_disable: de-configure a Tx queue to send AMPDUs
  477. * Must be atomic
  478. * @wait_tx_queue_empty: wait until tx queues are empty. May sleep.
  479. * @freeze_txq_timer: prevents the timer of the queue from firing until the
  480. * queue is set to awake. Must be atomic.
  481. * @dbgfs_register: add the dbgfs files under this directory. Files will be
  482. * automatically deleted.
  483. * @write8: write a u8 to a register at offset ofs from the BAR
  484. * @write32: write a u32 to a register at offset ofs from the BAR
  485. * @read32: read a u32 register at offset ofs from the BAR
  486. * @read_prph: read a DWORD from a periphery register
  487. * @write_prph: write a DWORD to a periphery register
  488. * @read_mem: read device's SRAM in DWORD
  489. * @write_mem: write device's SRAM in DWORD. If %buf is %NULL, then the memory
  490. * will be zeroed.
  491. * @configure: configure parameters required by the transport layer from
  492. * the op_mode. May be called several times before start_fw, can't be
  493. * called after that.
  494. * @set_pmi: set the power pmi state
  495. * @grab_nic_access: wake the NIC to be able to access non-HBUS regs.
  496. * Sleeping is not allowed between grab_nic_access and
  497. * release_nic_access.
  498. * @release_nic_access: let the NIC go to sleep. The "flags" parameter
  499. * must be the same one that was sent before to the grab_nic_access.
  500. * @set_bits_mask - set SRAM register according to value and mask.
  501. * @ref: grab a reference to the transport/FW layers, disallowing
  502. * certain low power states
  503. * @unref: release a reference previously taken with @ref. Note that
  504. * initially the reference count is 1, making an initial @unref
  505. * necessary to allow low power states.
  506. * @dump_data: return a vmalloc'ed buffer with debug data, maybe containing last
  507. * TX'ed commands and similar. The buffer will be vfree'd by the caller.
  508. * Note that the transport must fill in the proper file headers.
  509. */
  510. struct iwl_trans_ops {
  511. int (*start_hw)(struct iwl_trans *iwl_trans, bool low_power);
  512. void (*op_mode_leave)(struct iwl_trans *iwl_trans);
  513. int (*start_fw)(struct iwl_trans *trans, const struct fw_img *fw,
  514. bool run_in_rfkill);
  515. int (*update_sf)(struct iwl_trans *trans,
  516. struct iwl_sf_region *st_fwrd_space);
  517. void (*fw_alive)(struct iwl_trans *trans, u32 scd_addr);
  518. void (*stop_device)(struct iwl_trans *trans, bool low_power);
  519. void (*d3_suspend)(struct iwl_trans *trans, bool test);
  520. int (*d3_resume)(struct iwl_trans *trans, enum iwl_d3_status *status,
  521. bool test);
  522. int (*send_cmd)(struct iwl_trans *trans, struct iwl_host_cmd *cmd);
  523. int (*tx)(struct iwl_trans *trans, struct sk_buff *skb,
  524. struct iwl_device_cmd *dev_cmd, int queue);
  525. void (*reclaim)(struct iwl_trans *trans, int queue, int ssn,
  526. struct sk_buff_head *skbs);
  527. void (*txq_enable)(struct iwl_trans *trans, int queue, u16 ssn,
  528. const struct iwl_trans_txq_scd_cfg *cfg,
  529. unsigned int queue_wdg_timeout);
  530. void (*txq_disable)(struct iwl_trans *trans, int queue,
  531. bool configure_scd);
  532. int (*dbgfs_register)(struct iwl_trans *trans, struct dentry* dir);
  533. int (*wait_tx_queue_empty)(struct iwl_trans *trans, u32 txq_bm);
  534. void (*freeze_txq_timer)(struct iwl_trans *trans, unsigned long txqs,
  535. bool freeze);
  536. void (*write8)(struct iwl_trans *trans, u32 ofs, u8 val);
  537. void (*write32)(struct iwl_trans *trans, u32 ofs, u32 val);
  538. u32 (*read32)(struct iwl_trans *trans, u32 ofs);
  539. u32 (*read_prph)(struct iwl_trans *trans, u32 ofs);
  540. void (*write_prph)(struct iwl_trans *trans, u32 ofs, u32 val);
  541. int (*read_mem)(struct iwl_trans *trans, u32 addr,
  542. void *buf, int dwords);
  543. int (*write_mem)(struct iwl_trans *trans, u32 addr,
  544. const void *buf, int dwords);
  545. void (*configure)(struct iwl_trans *trans,
  546. const struct iwl_trans_config *trans_cfg);
  547. void (*set_pmi)(struct iwl_trans *trans, bool state);
  548. bool (*grab_nic_access)(struct iwl_trans *trans, bool silent,
  549. unsigned long *flags);
  550. void (*release_nic_access)(struct iwl_trans *trans,
  551. unsigned long *flags);
  552. void (*set_bits_mask)(struct iwl_trans *trans, u32 reg, u32 mask,
  553. u32 value);
  554. void (*ref)(struct iwl_trans *trans);
  555. void (*unref)(struct iwl_trans *trans);
  556. int (*suspend)(struct iwl_trans *trans);
  557. void (*resume)(struct iwl_trans *trans);
  558. struct iwl_trans_dump_data *(*dump_data)(struct iwl_trans *trans,
  559. struct iwl_fw_dbg_trigger_tlv
  560. *trigger);
  561. };
  562. /**
  563. * enum iwl_trans_state - state of the transport layer
  564. *
  565. * @IWL_TRANS_NO_FW: no fw has sent an alive response
  566. * @IWL_TRANS_FW_ALIVE: a fw has sent an alive response
  567. */
  568. enum iwl_trans_state {
  569. IWL_TRANS_NO_FW = 0,
  570. IWL_TRANS_FW_ALIVE = 1,
  571. };
  572. /**
  573. * enum iwl_d0i3_mode - d0i3 mode
  574. *
  575. * @IWL_D0I3_MODE_OFF - d0i3 is disabled
  576. * @IWL_D0I3_MODE_ON_IDLE - enter d0i3 when device is idle
  577. * (e.g. no active references)
  578. * @IWL_D0I3_MODE_ON_SUSPEND - enter d0i3 only on suspend
  579. * (in case of 'any' trigger)
  580. */
  581. enum iwl_d0i3_mode {
  582. IWL_D0I3_MODE_OFF = 0,
  583. IWL_D0I3_MODE_ON_IDLE,
  584. IWL_D0I3_MODE_ON_SUSPEND,
  585. };
  586. /**
  587. * struct iwl_trans - transport common data
  588. *
  589. * @ops - pointer to iwl_trans_ops
  590. * @op_mode - pointer to the op_mode
  591. * @cfg - pointer to the configuration
  592. * @status: a bit-mask of transport status flags
  593. * @dev - pointer to struct device * that represents the device
  594. * @max_skb_frags: maximum number of fragments an SKB can have when transmitted.
  595. * 0 indicates that frag SKBs (NETIF_F_SG) aren't supported.
  596. * @hw_id: a u32 with the ID of the device / sub-device.
  597. * Set during transport allocation.
  598. * @hw_id_str: a string with info about HW ID. Set during transport allocation.
  599. * @pm_support: set to true in start_hw if link pm is supported
  600. * @ltr_enabled: set to true if the LTR is enabled
  601. * @dev_cmd_pool: pool for Tx cmd allocation - for internal use only.
  602. * The user should use iwl_trans_{alloc,free}_tx_cmd.
  603. * @dev_cmd_headroom: room needed for the transport's private use before the
  604. * device_cmd for Tx - for internal use only
  605. * The user should use iwl_trans_{alloc,free}_tx_cmd.
  606. * @rx_mpdu_cmd: MPDU RX command ID, must be assigned by opmode before
  607. * starting the firmware, used for tracing
  608. * @rx_mpdu_cmd_hdr_size: used for tracing, amount of data before the
  609. * start of the 802.11 header in the @rx_mpdu_cmd
  610. * @dflt_pwr_limit: default power limit fetched from the platform (ACPI)
  611. * @dbg_dest_tlv: points to the destination TLV for debug
  612. * @dbg_conf_tlv: array of pointers to configuration TLVs for debug
  613. * @dbg_trigger_tlv: array of pointers to triggers TLVs for debug
  614. * @dbg_dest_reg_num: num of reg_ops in %dbg_dest_tlv
  615. * @paging_req_addr: The location were the FW will upload / download the pages
  616. * from. The address is set by the opmode
  617. * @paging_db: Pointer to the opmode paging data base, the pointer is set by
  618. * the opmode.
  619. * @paging_download_buf: Buffer used for copying all of the pages before
  620. * downloading them to the FW. The buffer is allocated in the opmode
  621. */
  622. struct iwl_trans {
  623. const struct iwl_trans_ops *ops;
  624. struct iwl_op_mode *op_mode;
  625. const struct iwl_cfg *cfg;
  626. enum iwl_trans_state state;
  627. unsigned long status;
  628. struct device *dev;
  629. u32 max_skb_frags;
  630. u32 hw_rev;
  631. u32 hw_id;
  632. char hw_id_str[52];
  633. u8 rx_mpdu_cmd, rx_mpdu_cmd_hdr_size;
  634. bool pm_support;
  635. bool ltr_enabled;
  636. /* The following fields are internal only */
  637. struct kmem_cache *dev_cmd_pool;
  638. size_t dev_cmd_headroom;
  639. char dev_cmd_pool_name[50];
  640. struct dentry *dbgfs_dir;
  641. #ifdef CONFIG_LOCKDEP
  642. struct lockdep_map sync_cmd_lockdep_map;
  643. #endif
  644. u64 dflt_pwr_limit;
  645. const struct iwl_fw_dbg_dest_tlv *dbg_dest_tlv;
  646. const struct iwl_fw_dbg_conf_tlv *dbg_conf_tlv[FW_DBG_CONF_MAX];
  647. struct iwl_fw_dbg_trigger_tlv * const *dbg_trigger_tlv;
  648. u8 dbg_dest_reg_num;
  649. /*
  650. * Paging parameters - All of the parameters should be set by the
  651. * opmode when paging is enabled
  652. */
  653. u32 paging_req_addr;
  654. struct iwl_fw_paging *paging_db;
  655. void *paging_download_buf;
  656. enum iwl_d0i3_mode d0i3_mode;
  657. bool wowlan_d0i3;
  658. /* pointer to trans specific struct */
  659. /*Ensure that this pointer will always be aligned to sizeof pointer */
  660. char trans_specific[0] __aligned(sizeof(void *));
  661. };
  662. static inline void iwl_trans_configure(struct iwl_trans *trans,
  663. const struct iwl_trans_config *trans_cfg)
  664. {
  665. trans->op_mode = trans_cfg->op_mode;
  666. trans->ops->configure(trans, trans_cfg);
  667. }
  668. static inline int _iwl_trans_start_hw(struct iwl_trans *trans, bool low_power)
  669. {
  670. might_sleep();
  671. return trans->ops->start_hw(trans, low_power);
  672. }
  673. static inline int iwl_trans_start_hw(struct iwl_trans *trans)
  674. {
  675. return trans->ops->start_hw(trans, true);
  676. }
  677. static inline void iwl_trans_op_mode_leave(struct iwl_trans *trans)
  678. {
  679. might_sleep();
  680. if (trans->ops->op_mode_leave)
  681. trans->ops->op_mode_leave(trans);
  682. trans->op_mode = NULL;
  683. trans->state = IWL_TRANS_NO_FW;
  684. }
  685. static inline void iwl_trans_fw_alive(struct iwl_trans *trans, u32 scd_addr)
  686. {
  687. might_sleep();
  688. trans->state = IWL_TRANS_FW_ALIVE;
  689. trans->ops->fw_alive(trans, scd_addr);
  690. }
  691. static inline int iwl_trans_start_fw(struct iwl_trans *trans,
  692. const struct fw_img *fw,
  693. bool run_in_rfkill)
  694. {
  695. might_sleep();
  696. WARN_ON_ONCE(!trans->rx_mpdu_cmd);
  697. clear_bit(STATUS_FW_ERROR, &trans->status);
  698. return trans->ops->start_fw(trans, fw, run_in_rfkill);
  699. }
  700. static inline int iwl_trans_update_sf(struct iwl_trans *trans,
  701. struct iwl_sf_region *st_fwrd_space)
  702. {
  703. might_sleep();
  704. if (trans->ops->update_sf)
  705. return trans->ops->update_sf(trans, st_fwrd_space);
  706. return 0;
  707. }
  708. static inline void _iwl_trans_stop_device(struct iwl_trans *trans,
  709. bool low_power)
  710. {
  711. might_sleep();
  712. trans->ops->stop_device(trans, low_power);
  713. trans->state = IWL_TRANS_NO_FW;
  714. }
  715. static inline void iwl_trans_stop_device(struct iwl_trans *trans)
  716. {
  717. _iwl_trans_stop_device(trans, true);
  718. }
  719. static inline void iwl_trans_d3_suspend(struct iwl_trans *trans, bool test)
  720. {
  721. might_sleep();
  722. if (trans->ops->d3_suspend)
  723. trans->ops->d3_suspend(trans, test);
  724. }
  725. static inline int iwl_trans_d3_resume(struct iwl_trans *trans,
  726. enum iwl_d3_status *status,
  727. bool test)
  728. {
  729. might_sleep();
  730. if (!trans->ops->d3_resume)
  731. return 0;
  732. return trans->ops->d3_resume(trans, status, test);
  733. }
  734. static inline void iwl_trans_ref(struct iwl_trans *trans)
  735. {
  736. if (trans->ops->ref)
  737. trans->ops->ref(trans);
  738. }
  739. static inline void iwl_trans_unref(struct iwl_trans *trans)
  740. {
  741. if (trans->ops->unref)
  742. trans->ops->unref(trans);
  743. }
  744. static inline int iwl_trans_suspend(struct iwl_trans *trans)
  745. {
  746. if (!trans->ops->suspend)
  747. return 0;
  748. return trans->ops->suspend(trans);
  749. }
  750. static inline void iwl_trans_resume(struct iwl_trans *trans)
  751. {
  752. if (trans->ops->resume)
  753. trans->ops->resume(trans);
  754. }
  755. static inline struct iwl_trans_dump_data *
  756. iwl_trans_dump_data(struct iwl_trans *trans,
  757. struct iwl_fw_dbg_trigger_tlv *trigger)
  758. {
  759. if (!trans->ops->dump_data)
  760. return NULL;
  761. return trans->ops->dump_data(trans, trigger);
  762. }
  763. static inline int iwl_trans_send_cmd(struct iwl_trans *trans,
  764. struct iwl_host_cmd *cmd)
  765. {
  766. int ret;
  767. if (unlikely(!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  768. test_bit(STATUS_RFKILL, &trans->status)))
  769. return -ERFKILL;
  770. if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
  771. return -EIO;
  772. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE)) {
  773. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  774. return -EIO;
  775. }
  776. if (!(cmd->flags & CMD_ASYNC))
  777. lock_map_acquire_read(&trans->sync_cmd_lockdep_map);
  778. ret = trans->ops->send_cmd(trans, cmd);
  779. if (!(cmd->flags & CMD_ASYNC))
  780. lock_map_release(&trans->sync_cmd_lockdep_map);
  781. return ret;
  782. }
  783. static inline struct iwl_device_cmd *
  784. iwl_trans_alloc_tx_cmd(struct iwl_trans *trans)
  785. {
  786. u8 *dev_cmd_ptr = kmem_cache_alloc(trans->dev_cmd_pool, GFP_ATOMIC);
  787. if (unlikely(dev_cmd_ptr == NULL))
  788. return NULL;
  789. return (struct iwl_device_cmd *)
  790. (dev_cmd_ptr + trans->dev_cmd_headroom);
  791. }
  792. static inline void iwl_trans_free_tx_cmd(struct iwl_trans *trans,
  793. struct iwl_device_cmd *dev_cmd)
  794. {
  795. u8 *dev_cmd_ptr = (u8 *)dev_cmd - trans->dev_cmd_headroom;
  796. kmem_cache_free(trans->dev_cmd_pool, dev_cmd_ptr);
  797. }
  798. static inline int iwl_trans_tx(struct iwl_trans *trans, struct sk_buff *skb,
  799. struct iwl_device_cmd *dev_cmd, int queue)
  800. {
  801. if (unlikely(test_bit(STATUS_FW_ERROR, &trans->status)))
  802. return -EIO;
  803. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  804. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  805. return trans->ops->tx(trans, skb, dev_cmd, queue);
  806. }
  807. static inline void iwl_trans_reclaim(struct iwl_trans *trans, int queue,
  808. int ssn, struct sk_buff_head *skbs)
  809. {
  810. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  811. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  812. trans->ops->reclaim(trans, queue, ssn, skbs);
  813. }
  814. static inline void iwl_trans_txq_disable(struct iwl_trans *trans, int queue,
  815. bool configure_scd)
  816. {
  817. trans->ops->txq_disable(trans, queue, configure_scd);
  818. }
  819. static inline void
  820. iwl_trans_txq_enable_cfg(struct iwl_trans *trans, int queue, u16 ssn,
  821. const struct iwl_trans_txq_scd_cfg *cfg,
  822. unsigned int queue_wdg_timeout)
  823. {
  824. might_sleep();
  825. if (unlikely((trans->state != IWL_TRANS_FW_ALIVE)))
  826. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  827. trans->ops->txq_enable(trans, queue, ssn, cfg, queue_wdg_timeout);
  828. }
  829. static inline void iwl_trans_txq_enable(struct iwl_trans *trans, int queue,
  830. int fifo, int sta_id, int tid,
  831. int frame_limit, u16 ssn,
  832. unsigned int queue_wdg_timeout)
  833. {
  834. struct iwl_trans_txq_scd_cfg cfg = {
  835. .fifo = fifo,
  836. .sta_id = sta_id,
  837. .tid = tid,
  838. .frame_limit = frame_limit,
  839. .aggregate = sta_id >= 0,
  840. };
  841. iwl_trans_txq_enable_cfg(trans, queue, ssn, &cfg, queue_wdg_timeout);
  842. }
  843. static inline
  844. void iwl_trans_ac_txq_enable(struct iwl_trans *trans, int queue, int fifo,
  845. unsigned int queue_wdg_timeout)
  846. {
  847. struct iwl_trans_txq_scd_cfg cfg = {
  848. .fifo = fifo,
  849. .sta_id = -1,
  850. .tid = IWL_MAX_TID_COUNT,
  851. .frame_limit = IWL_FRAME_LIMIT,
  852. .aggregate = false,
  853. };
  854. iwl_trans_txq_enable_cfg(trans, queue, 0, &cfg, queue_wdg_timeout);
  855. }
  856. static inline void iwl_trans_freeze_txq_timer(struct iwl_trans *trans,
  857. unsigned long txqs,
  858. bool freeze)
  859. {
  860. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  861. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  862. if (trans->ops->freeze_txq_timer)
  863. trans->ops->freeze_txq_timer(trans, txqs, freeze);
  864. }
  865. static inline int iwl_trans_wait_tx_queue_empty(struct iwl_trans *trans,
  866. u32 txqs)
  867. {
  868. if (unlikely(trans->state != IWL_TRANS_FW_ALIVE))
  869. IWL_ERR(trans, "%s bad state = %d\n", __func__, trans->state);
  870. return trans->ops->wait_tx_queue_empty(trans, txqs);
  871. }
  872. static inline int iwl_trans_dbgfs_register(struct iwl_trans *trans,
  873. struct dentry *dir)
  874. {
  875. return trans->ops->dbgfs_register(trans, dir);
  876. }
  877. static inline void iwl_trans_write8(struct iwl_trans *trans, u32 ofs, u8 val)
  878. {
  879. trans->ops->write8(trans, ofs, val);
  880. }
  881. static inline void iwl_trans_write32(struct iwl_trans *trans, u32 ofs, u32 val)
  882. {
  883. trans->ops->write32(trans, ofs, val);
  884. }
  885. static inline u32 iwl_trans_read32(struct iwl_trans *trans, u32 ofs)
  886. {
  887. return trans->ops->read32(trans, ofs);
  888. }
  889. static inline u32 iwl_trans_read_prph(struct iwl_trans *trans, u32 ofs)
  890. {
  891. return trans->ops->read_prph(trans, ofs);
  892. }
  893. static inline void iwl_trans_write_prph(struct iwl_trans *trans, u32 ofs,
  894. u32 val)
  895. {
  896. return trans->ops->write_prph(trans, ofs, val);
  897. }
  898. static inline int iwl_trans_read_mem(struct iwl_trans *trans, u32 addr,
  899. void *buf, int dwords)
  900. {
  901. return trans->ops->read_mem(trans, addr, buf, dwords);
  902. }
  903. #define iwl_trans_read_mem_bytes(trans, addr, buf, bufsize) \
  904. do { \
  905. if (__builtin_constant_p(bufsize)) \
  906. BUILD_BUG_ON((bufsize) % sizeof(u32)); \
  907. iwl_trans_read_mem(trans, addr, buf, (bufsize) / sizeof(u32));\
  908. } while (0)
  909. static inline u32 iwl_trans_read_mem32(struct iwl_trans *trans, u32 addr)
  910. {
  911. u32 value;
  912. if (WARN_ON(iwl_trans_read_mem(trans, addr, &value, 1)))
  913. return 0xa5a5a5a5;
  914. return value;
  915. }
  916. static inline int iwl_trans_write_mem(struct iwl_trans *trans, u32 addr,
  917. const void *buf, int dwords)
  918. {
  919. return trans->ops->write_mem(trans, addr, buf, dwords);
  920. }
  921. static inline u32 iwl_trans_write_mem32(struct iwl_trans *trans, u32 addr,
  922. u32 val)
  923. {
  924. return iwl_trans_write_mem(trans, addr, &val, 1);
  925. }
  926. static inline void iwl_trans_set_pmi(struct iwl_trans *trans, bool state)
  927. {
  928. if (trans->ops->set_pmi)
  929. trans->ops->set_pmi(trans, state);
  930. }
  931. static inline void
  932. iwl_trans_set_bits_mask(struct iwl_trans *trans, u32 reg, u32 mask, u32 value)
  933. {
  934. trans->ops->set_bits_mask(trans, reg, mask, value);
  935. }
  936. #define iwl_trans_grab_nic_access(trans, silent, flags) \
  937. __cond_lock(nic_access, \
  938. likely((trans)->ops->grab_nic_access(trans, silent, flags)))
  939. static inline void __releases(nic_access)
  940. iwl_trans_release_nic_access(struct iwl_trans *trans, unsigned long *flags)
  941. {
  942. trans->ops->release_nic_access(trans, flags);
  943. __release(nic_access);
  944. }
  945. static inline void iwl_trans_fw_error(struct iwl_trans *trans)
  946. {
  947. if (WARN_ON_ONCE(!trans->op_mode))
  948. return;
  949. /* prevent double restarts due to the same erroneous FW */
  950. if (!test_and_set_bit(STATUS_FW_ERROR, &trans->status))
  951. iwl_op_mode_nic_error(trans->op_mode);
  952. }
  953. /*****************************************************
  954. * transport helper functions
  955. *****************************************************/
  956. struct iwl_trans *iwl_trans_alloc(unsigned int priv_size,
  957. struct device *dev,
  958. const struct iwl_cfg *cfg,
  959. const struct iwl_trans_ops *ops,
  960. size_t dev_cmd_headroom);
  961. void iwl_trans_free(struct iwl_trans *trans);
  962. /*****************************************************
  963. * driver (transport) register/unregister functions
  964. ******************************************************/
  965. int __must_check iwl_pci_register_driver(void);
  966. void iwl_pci_unregister_driver(void);
  967. #endif /* __iwl_trans_h__ */