iwl_trans.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /* Intel 7265D/3165 PCIe transport: see iwl_trans.h.
  2. *
  3. * Register sequences follow Linux iwlwifi (pcie/trans.c, pcie/rx.c,
  4. * pcie/tx.c) for the 7000 device family: no MSI-X, no ICT, legacy single
  5. * RX queue (256 x 4 KB RBDs), gen1 128-byte TFDs, byte-count table in
  6. * dwords. Everything is polled; CSR_INT_MASK stays zero and PCI INTx is
  7. * disabled, so the card never raises an interrupt.
  8. *
  9. * The NIC is kept awake (MAC_ACCESS_REQ held) the whole time it is up: the
  10. * firmware is configured without power save, and holding the request is
  11. * what Linux does while host commands are in flight (apmg_wake_up_wa). */
  12. #include "iwl_trans.h"
  13. #include "mm.h"
  14. #include "paging.h"
  15. #include "pci.h"
  16. #include "process.h"
  17. #include "ramfs.h"
  18. #include "serial.h"
  19. #define FW_PATH "firmware/iwlwifi-7265D-29.ucode"
  20. /* ---- CSR / HBUS / FH / PRPH registers (iwl-csr.h, iwl-fh.h, iwl-prph.h) */
  21. #define CSR_HW_IF_CONFIG_REG 0x000
  22. #define CSR_INT_COALESCING 0x004
  23. #define CSR_INT 0x008
  24. #define CSR_INT_MASK 0x00c
  25. #define CSR_FH_INT_STATUS 0x010
  26. #define CSR_RESET 0x020
  27. #define CSR_GP_CNTRL 0x024
  28. #define CSR_HW_REV 0x028
  29. #define CSR_GIO_REG 0x03c
  30. #define CSR_UCODE_DRV_GP1_CLR 0x05c
  31. #define CSR_MBOX_SET_REG 0x088
  32. #define CSR_MAC_SHADOW_REG_CTRL 0x0a8
  33. #define CSR_GIO_CHICKEN_BITS 0x100
  34. #define CSR_DBG_HPET_MEM_REG 0x240
  35. #define CSR_DBG_LINK_PWR_MGMT_REG 0x250
  36. #define HW_IF_NIC_READY 0x00400000u
  37. #define HW_IF_PREPARE 0x08000000u
  38. #define HW_IF_HAP_WAKE_L1A 0x00080000u
  39. #define INT_FH_RX (1u << 31)
  40. #define INT_HW_ERR (1u << 29)
  41. #define INT_FH_TX (1u << 27)
  42. #define INT_SW_ERR (1u << 25)
  43. #define INT_SW_RX (1u << 3)
  44. #define INT_ALIVE (1u << 0)
  45. #define RESET_SW_RESET 0x00000080u
  46. #define RESET_MASTER_DISABLED 0x00000100u
  47. #define RESET_STOP_MASTER 0x00000200u
  48. #define GP_MAC_CLOCK_READY 0x00000001u
  49. #define GP_INIT_DONE 0x00000004u
  50. #define GP_MAC_ACCESS_REQ 0x00000008u
  51. #define GP_GOING_TO_SLEEP 0x00000010u
  52. #define GP_HW_RF_KILL_SW 0x08000000u
  53. #define UCODE_SW_BIT_RFKILL 0x00000002u
  54. #define UCODE_CMD_BLOCKED 0x00000004u
  55. #define MBOX_OS_ALIVE (1u << 5)
  56. #define GIO_L0S_DISABLED 0x00000002u
  57. #define GIO_CHICKEN_L1A_NO_L0S_RX 0x00800000u
  58. #define GIO_CHICKEN_DIS_L0S_TIMER 0x20000000u
  59. #define LINK_PWR_MGMT_DISABLED 0x80000000u
  60. #define HBUS_TARG_MEM_WADDR 0x410
  61. #define HBUS_TARG_MEM_WDAT 0x418
  62. #define HBUS_TARG_PRPH_WADDR 0x444
  63. #define HBUS_TARG_PRPH_RADDR 0x448
  64. #define HBUS_TARG_PRPH_WDAT 0x44c
  65. #define HBUS_TARG_PRPH_RDAT 0x450
  66. #define HBUS_TARG_WRPTR 0x460
  67. #define FH_KW_MEM_ADDR_REG 0x197c
  68. #define FH_RSCSR_STTS_WPTR 0x1bc0
  69. #define FH_RSCSR_RBDCB_BASE 0x1bc4
  70. #define FH_RSCSR_RBDCB_WPTR 0x1bc8
  71. #define FH_RSCSR_RDPTR 0x1bcc
  72. #define FH_RCSR_CONFIG 0x1c00
  73. #define FH_RCSR_RBDCB_WPTR 0x1c08
  74. #define FH_RCSR_FLUSH_RB_REQ 0x1c10
  75. #define FH_TFDIB_CTRL0(c) (0x1900 + 8 * (c))
  76. #define FH_TFDIB_CTRL1(c) (0x1904 + 8 * (c))
  77. #define FH_TCSR_CONFIG(c) (0x1d00 + 0x20 * (c))
  78. #define FH_TCSR_BUF_STS(c) (0x1d08 + 0x20 * (c))
  79. #define FH_SRVC_CHNL 9
  80. #define FH_SRVC_SRAM_ADDR 0x19c8
  81. #define FH_TX_CHICKEN_BITS 0x1e98
  82. #define FH_TCSR_DMA_ENABLE 0x80000000u
  83. #define FH_TCSR_CREDIT_ENABLE 0x00000008u
  84. #define FH_TCSR_HOST_ENDTFD 0x00100000u
  85. #define FH_MEM_TB_MAX_LENGTH 0x20000u
  86. #define APMG_CLK_EN_REG 0x3004
  87. #define APMG_PS_CTRL_REG 0x300c
  88. #define APMG_PCIDEV_STT_REG 0x3010
  89. #define APMG_RTC_INT_STT_REG 0x301c
  90. #define APMG_CLK_DMA_RQT 0x00000200u
  91. #define APMG_PS_PWR_SRC_MSK 0x03000000u
  92. #define APMG_PCIDEV_L1_ACT_DIS 0x00000800u
  93. #define APMG_PCIDEV_WAKE_ME 0x00004000u
  94. #define APMG_RTC_INT_RFKILL 0x10000000u
  95. #define LMPM_CHICK 0xa01ff8
  96. #define LMPM_EXTENDED_ADDR 0x1u
  97. #define FW_MEM_EXT_START 0x40000u
  98. #define FW_MEM_EXT_END 0x57fffu
  99. #define SCD_BASE 0xa02c00
  100. #define SCD_SRAM_BASE_ADDR (SCD_BASE + 0x0)
  101. #define SCD_DRAM_BASE_ADDR (SCD_BASE + 0x8)
  102. #define SCD_TXFACT (SCD_BASE + 0x10)
  103. #define SCD_QUEUECHAIN_SEL (SCD_BASE + 0xe8)
  104. #define SCD_AGGR_SEL (SCD_BASE + 0x248)
  105. #define SCD_GP_CTRL (SCD_BASE + 0x1a8)
  106. #define SCD_EN_CTRL (SCD_BASE + 0x254)
  107. #define SCD_GP_AUTO_ACTIVE (1u << 18)
  108. #define SCD_GP_31_QUEUES (1u << 0)
  109. #define SCD_CONTEXT_MEM 0x600
  110. #define SCD_TRANS_TBL_MEM 0x7e0
  111. #define SCD_STTS_ACTIVE 3
  112. #define SCD_STTS_TXF 0
  113. #define SCD_STTS_WSL 4
  114. #define SCD_STTS_ACT_EN 19
  115. #define SCD_STTS_MSK 0x017f0000u
  116. static unsigned int scd_queue_rdptr(unsigned int q)
  117. {
  118. return q < 20 ? SCD_BASE + 0x68 + q * 4 : SCD_BASE + 0x2b4 + q * 4;
  119. }
  120. static unsigned int scd_queue_status(unsigned int q)
  121. {
  122. return q < 20 ? SCD_BASE + 0x10c + q * 4 : SCD_BASE + 0x334 + (q - 20) * 4;
  123. }
  124. static unsigned int fh_cbbc(unsigned int q)
  125. {
  126. if (q < 16)
  127. return 0x19d0 + 4 * q;
  128. if (q < 20)
  129. return 0x1bf0 + 4 * (q - 16);
  130. return 0x1b20 + 4 * (q - 20);
  131. }
  132. /* ---- device state ------------------------------------------------------ */
  133. #define RX_RING 256
  134. #define TFD_SIZE 128
  135. #define TFD_RING 256
  136. #define BC_ENTRIES 320 /* 256 + 64 duplicate */
  137. #define CMD_SLOTS 32
  138. #define CMD_BUF 4096 /* one page per command slot */
  139. #define DATA_SLOTS 32
  140. #define DATA_BUF 2048
  141. #define FIRST_TB 20 /* IWL_FIRST_TB_SIZE */
  142. struct txq {
  143. unsigned char *tfds; /* 256 x 128-byte TFDs */
  144. unsigned char *bufs; /* slots x bufsize */
  145. unsigned int nslots, bufsize;
  146. unsigned int wptr, rptr; /* 0..255 */
  147. int active;
  148. };
  149. struct iwl_state {
  150. volatile unsigned char *mmio;
  151. unsigned int bar, bus, dev, func, devid, hw_rev;
  152. const char *chip;
  153. unsigned int *rbd; /* RX descriptors (page) */
  154. unsigned char *rb[RX_RING]; /* RX buffers (4 KB pages) */
  155. volatile unsigned short *rb_stts;
  156. unsigned int rx_read;
  157. unsigned short *bc; /* byte-count tables, IWL_NUM_Q x 320 */
  158. unsigned char *kw; /* keep-warm page */
  159. unsigned char *fwbuf; /* 128 KB firmware chunk bounce buffer */
  160. struct txq q[IWL_NUM_Q];
  161. unsigned int scd_base;
  162. iwl_rx_handler handler;
  163. /* synchronous command bookkeeping */
  164. int waiting; /* a command is waiting for its response */
  165. unsigned short wait_seq;
  166. void *resp;
  167. unsigned int resp_max;
  168. int resp_len, resp_done;
  169. unsigned char wait_notif;
  170. int notif_seen;
  171. int fatal;
  172. struct iwl_fw_info fw;
  173. const unsigned char *fw_data;
  174. unsigned int fw_size;
  175. };
  176. static struct iwl_state *S; /* page-allocated on probe */
  177. typedef char iwl_state_fits_page[sizeof(struct iwl_state) <= 4096 ? 1 : -1];
  178. void iwl_log(const char *s)
  179. {
  180. serial_puts(SERIAL_COM1, "iwl: ");
  181. serial_puts(SERIAL_COM1, s);
  182. serial_puts(SERIAL_COM1, "\n");
  183. }
  184. void iwl_loghex(const char *s, unsigned int v)
  185. {
  186. serial_puts(SERIAL_COM1, "iwl: ");
  187. serial_puts(SERIAL_COM1, s);
  188. serial_puts(SERIAL_COM1, " ");
  189. serial_puthex(SERIAL_COM1, v);
  190. serial_puts(SERIAL_COM1, "\n");
  191. }
  192. /* Byte helpers: the kernel has no libc, and `rep` string ops keep gcc from
  193. * turning a loop back into a memcpy/memset call. */
  194. void iwl_memcpy(void *dst, const void *src, unsigned int n)
  195. {
  196. __asm__ volatile ("cld; rep movsb"
  197. : "+D"(dst), "+S"(src), "+c"(n) : : "memory");
  198. }
  199. void iwl_memzero(void *dst, unsigned int n)
  200. {
  201. __asm__ volatile ("cld; rep stosb"
  202. : "+D"(dst), "+c"(n) : "a"(0) : "memory");
  203. }
  204. /* ~1 us per port-0x80 write on every chipset; works with interrupts off
  205. * (some network syscalls run with IF=0, which freezes get_ticks_ms()). */
  206. void iwl_udelay(unsigned int us)
  207. {
  208. while (us--)
  209. port_outb(0x80, 0);
  210. }
  211. unsigned int iwl_rd(unsigned int reg)
  212. {
  213. return *(volatile unsigned int *)(S->mmio + reg);
  214. }
  215. void iwl_wr(unsigned int reg, unsigned int val)
  216. {
  217. *(volatile unsigned int *)(S->mmio + reg) = val;
  218. }
  219. static void iwl_set(unsigned int reg, unsigned int bits)
  220. {
  221. iwl_wr(reg, iwl_rd(reg) | bits);
  222. }
  223. static void iwl_clr(unsigned int reg, unsigned int bits)
  224. {
  225. iwl_wr(reg, iwl_rd(reg) & ~bits);
  226. }
  227. /* Poll until (reg & mask) == (bits & mask); timeout in microseconds. */
  228. static int iwl_poll(unsigned int reg, unsigned int bits, unsigned int mask,
  229. unsigned int us)
  230. {
  231. unsigned int t;
  232. for (t = 0; t < us; t += 10) {
  233. if ((iwl_rd(reg) & mask) == (bits & mask))
  234. return 0;
  235. iwl_udelay(10);
  236. }
  237. return -1;
  238. }
  239. unsigned int iwl_rd_prph(unsigned int addr)
  240. {
  241. iwl_wr(HBUS_TARG_PRPH_RADDR, (addr & 0x000fffffu) | (3u << 24));
  242. return iwl_rd(HBUS_TARG_PRPH_RDAT);
  243. }
  244. void iwl_wr_prph(unsigned int addr, unsigned int val)
  245. {
  246. iwl_wr(HBUS_TARG_PRPH_WADDR, (addr & 0x000fffffu) | (3u << 24));
  247. iwl_wr(HBUS_TARG_PRPH_WDAT, val);
  248. }
  249. static void prph_set(unsigned int addr, unsigned int bits)
  250. {
  251. iwl_wr_prph(addr, iwl_rd_prph(addr) | bits);
  252. }
  253. static void prph_clr(unsigned int addr, unsigned int bits)
  254. {
  255. iwl_wr_prph(addr, iwl_rd_prph(addr) & ~bits);
  256. }
  257. unsigned int iwl_mem_rd32(unsigned int addr)
  258. {
  259. iwl_wr(0x40c /* HBUS_TARG_MEM_RADDR */, addr);
  260. return iwl_rd(0x41c /* HBUS_TARG_MEM_RDAT */);
  261. }
  262. static void mem_wr32(unsigned int addr, unsigned int val)
  263. {
  264. iwl_wr(HBUS_TARG_MEM_WADDR, addr);
  265. iwl_wr(HBUS_TARG_MEM_WDAT, val);
  266. }
  267. static void mem_zero(unsigned int addr, unsigned int dwords)
  268. {
  269. iwl_wr(HBUS_TARG_MEM_WADDR, addr);
  270. while (dwords--)
  271. iwl_wr(HBUS_TARG_MEM_WDAT, 0);
  272. }
  273. /* Wake the MAC and keep it awake (MAC_ACCESS_REQ stays set). */
  274. static int grab_nic(void)
  275. {
  276. iwl_set(CSR_GP_CNTRL, GP_MAC_ACCESS_REQ);
  277. if (iwl_poll(CSR_GP_CNTRL, GP_MAC_CLOCK_READY,
  278. GP_MAC_CLOCK_READY | GP_GOING_TO_SLEEP, 15000) < 0) {
  279. iwl_loghex("nic access timeout, GP_CNTRL", iwl_rd(CSR_GP_CNTRL));
  280. return -1;
  281. }
  282. return 0;
  283. }
  284. /* ---- probe --------------------------------------------------------------- */
  285. static const char *chip_for(unsigned int devid)
  286. {
  287. switch (devid) {
  288. case 0x095a: case 0x095b: return "Intel Wireless 7265";
  289. case 0x3165: case 0x3166: return "Intel Wireless 3165";
  290. }
  291. return 0;
  292. }
  293. int iwl_present(void)
  294. {
  295. return S != 0;
  296. }
  297. const char *iwl_chip_name(void)
  298. {
  299. return S ? S->chip : "none";
  300. }
  301. unsigned int iwl_hw_rev(void)
  302. {
  303. return S ? S->hw_rev : 0;
  304. }
  305. const struct iwl_fw_info *iwl_fw(void)
  306. {
  307. return S ? &S->fw : 0;
  308. }
  309. const unsigned char *iwl_fw_data(unsigned int *size)
  310. {
  311. if (!S)
  312. return 0;
  313. *size = S->fw_size;
  314. return S->fw_data;
  315. }
  316. /* Put the function in D0 (PCI PM capability), as pci_enable_device()
  317. * does; firmware can leave an unused WiFi card in D3hot. */
  318. static void pci_d0(unsigned int bus, unsigned int dev, unsigned int func)
  319. {
  320. unsigned int cap, n;
  321. if (!((pci_cfg_read32(bus, dev, func, 0x04) >> 16) & 0x10))
  322. return;
  323. cap = pci_cfg_read32(bus, dev, func, 0x34) & 0xfcu;
  324. for (n = 0; cap && n < 48; n++) {
  325. unsigned int hdr = pci_cfg_read32(bus, dev, func, cap);
  326. if ((hdr & 0xff) == 0x01) {
  327. unsigned int pmcsr = pci_cfg_read32(bus, dev, func, cap + 4);
  328. if (pmcsr & 3u) {
  329. pci_cfg_write32(bus, dev, func, cap + 4,
  330. pmcsr & ~3u);
  331. iwl_udelay(10000);
  332. iwl_log("PCI D3 -> D0");
  333. }
  334. return;
  335. }
  336. cap = (hdr >> 8) & 0xfcu;
  337. }
  338. }
  339. int iwl_probe(void)
  340. {
  341. unsigned int bus, dev, func;
  342. if (S)
  343. return 0;
  344. for (bus = 0; bus < 256; bus++) {
  345. for (dev = 0; dev < 32; dev++) {
  346. unsigned int id = pci_cfg_read32(bus, dev, 0, 0);
  347. unsigned int nfunc;
  348. if (id == 0xffffffffu || id == 0)
  349. continue;
  350. nfunc = (pci_cfg_read32(bus, dev, 0, 0x0c) & 0x800000u) ? 8 : 1;
  351. for (func = 0; func < nfunc; func++) {
  352. unsigned int idf = pci_cfg_read32(bus, dev, func, 0);
  353. unsigned int bar, cmd, page;
  354. const char *name;
  355. if ((idf & 0xffffu) != 0x8086)
  356. continue;
  357. name = chip_for(idf >> 16);
  358. if (!name)
  359. continue;
  360. bar = pci_cfg_read32(bus, dev, func, 0x10);
  361. if (bar & 1u)
  362. continue;
  363. bar &= 0xfffffff0u;
  364. if (!bar) {
  365. iwl_log("BAR0 unassigned");
  366. continue;
  367. }
  368. page = mm_alloc_page();
  369. if (!page)
  370. return -1;
  371. S = (struct iwl_state *)page;
  372. iwl_memzero(S, 4096);
  373. S->bar = bar;
  374. S->bus = bus;
  375. S->dev = dev;
  376. S->func = func;
  377. S->devid = idf >> 16;
  378. S->chip = name;
  379. pci_d0(bus, dev, func);
  380. /* Memory + bus master, INTx off (polled). */
  381. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  382. pci_cfg_write32(bus, dev, func, 0x04,
  383. (cmd & 0xffffu) | 0x0006u | 0x0400u);
  384. /* PCI_CFG_RETRY_TIMEOUT (0x41) = 0, like Linux. */
  385. pci_cfg_write8(bus, dev, func, 0x41, 0);
  386. /* Map the 8 KB CSR window in every address space
  387. * (probe may run after processes exist). */
  388. process_map_mmio_all(bar, 0x2000);
  389. S->mmio = (volatile unsigned char *)bar;
  390. iwl_wr(CSR_INT_MASK, 0);
  391. S->hw_rev = iwl_rd(CSR_HW_REV);
  392. serial_puts(SERIAL_COM1, "iwl: ");
  393. serial_puts(SERIAL_COM1, name);
  394. serial_puts(SERIAL_COM1, " dev ");
  395. serial_puthex(SERIAL_COM1, S->devid);
  396. serial_puts(SERIAL_COM1, " hw_rev ");
  397. serial_puthex(SERIAL_COM1, S->hw_rev);
  398. serial_puts(SERIAL_COM1, " bar ");
  399. serial_puthex(SERIAL_COM1, bar);
  400. serial_puts(SERIAL_COM1, "\n");
  401. if (S->hw_rev == 0xffffffffu) {
  402. iwl_log("HW_REV reads ~0: PCI issue");
  403. S = 0;
  404. return -1;
  405. }
  406. /* 0x095a/b: only rev-D silicon runs the 7265D
  407. * firmware (Linux: CSR_HW_REV_TYPE_7265D). */
  408. if (S->devid != 0x3165 && S->devid != 0x3166 &&
  409. (S->hw_rev & 0xfff0u) != 0x210u)
  410. iwl_log("warning: not a 7265D; the 7265D firmware may fail");
  411. return 0;
  412. }
  413. }
  414. }
  415. return -1;
  416. }
  417. /* ---- firmware file ------------------------------------------------------- */
  418. #define TLV_INST 1
  419. #define TLV_FLAGS 18
  420. #define TLV_SEC_RT 19
  421. #define TLV_SEC_INIT 20
  422. #define TLV_DEF_CALIB 22
  423. #define TLV_PHY_SKU 23
  424. #define TLV_API_CHANGES 29
  425. #define TLV_ENABLED_CAPA 30
  426. static unsigned int le32(const unsigned char *p)
  427. {
  428. return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
  429. }
  430. int iwl_fw_parse(void)
  431. {
  432. const unsigned char *d;
  433. unsigned int size, off, i;
  434. if (!S)
  435. return -1;
  436. if (S->fw.present)
  437. return 0;
  438. d = ramfs_lookup(FW_PATH, &size);
  439. if (!d || size < 88 || le32(d) != 0 || le32(d + 4) != 0x0a4c5749u) {
  440. iwl_log("firmware " FW_PATH " missing or not a TLV image");
  441. return -1;
  442. }
  443. S->fw_data = d;
  444. S->fw_size = size;
  445. for (i = 0; i < sizeof(S->fw.human) - 1 && d[8 + i]; i++)
  446. S->fw.human[i] = (char)d[8 + i];
  447. S->fw.ver = le32(d + 72);
  448. off = 88;
  449. while (off + 8 <= size) {
  450. unsigned int t = le32(d + off), l = le32(d + off + 4);
  451. const unsigned char *p = d + off + 8;
  452. if (off + 8 + l > size)
  453. break;
  454. if (t == TLV_API_CHANGES && l >= 8 && le32(p) < 4)
  455. S->fw.api[le32(p)] = le32(p + 4);
  456. else if (t == TLV_ENABLED_CAPA && l >= 8 && le32(p) < 4)
  457. S->fw.capa[le32(p)] = le32(p + 4);
  458. else if (t == TLV_PHY_SKU && l >= 4)
  459. S->fw.phy_sku = le32(p);
  460. else if (t == TLV_DEF_CALIB && l >= 12) {
  461. unsigned int type = le32(p);
  462. /* enum iwl_ucode_type: 0 REGULAR, 1 INIT */
  463. if (type < 2) {
  464. S->fw.flow_trigger[type] = le32(p + 4);
  465. S->fw.event_trigger[type] = le32(p + 8);
  466. }
  467. }
  468. off += 8 + ((l + 3) & ~3u);
  469. }
  470. S->fw.present = 1;
  471. iwl_log(S->fw.human);
  472. return 0;
  473. }
  474. /* ---- hardware bring-up ------------------------------------------------- */
  475. int iwl_rfkill(void)
  476. {
  477. return S && !(iwl_rd(CSR_GP_CNTRL) & GP_HW_RF_KILL_SW);
  478. }
  479. static int set_hw_ready(void)
  480. {
  481. iwl_set(CSR_HW_IF_CONFIG_REG, HW_IF_NIC_READY);
  482. if (iwl_poll(CSR_HW_IF_CONFIG_REG, HW_IF_NIC_READY, HW_IF_NIC_READY, 50) < 0)
  483. return -1;
  484. iwl_set(CSR_MBOX_SET_REG, MBOX_OS_ALIVE);
  485. return 0;
  486. }
  487. static int prepare_card(void)
  488. {
  489. int iter, t;
  490. if (set_hw_ready() == 0)
  491. return 0;
  492. iwl_set(CSR_DBG_LINK_PWR_MGMT_REG, LINK_PWR_MGMT_DISABLED);
  493. iwl_udelay(1000);
  494. for (iter = 0; iter < 10; iter++) {
  495. iwl_set(CSR_HW_IF_CONFIG_REG, HW_IF_PREPARE);
  496. for (t = 0; t < 150000; t += 200) {
  497. if (set_hw_ready() == 0)
  498. return 0;
  499. iwl_udelay(200);
  500. }
  501. iwl_udelay(25000);
  502. }
  503. iwl_log("card not ready (owned by ME/AMT?)");
  504. return -1;
  505. }
  506. static int apm_init(void)
  507. {
  508. iwl_set(CSR_GIO_CHICKEN_BITS, GIO_CHICKEN_DIS_L0S_TIMER);
  509. iwl_set(CSR_GIO_CHICKEN_BITS, GIO_CHICKEN_L1A_NO_L0S_RX);
  510. iwl_set(CSR_DBG_HPET_MEM_REG, 0xffff0000u);
  511. iwl_set(CSR_HW_IF_CONFIG_REG, HW_IF_HAP_WAKE_L1A);
  512. iwl_set(CSR_GIO_REG, GIO_L0S_DISABLED);
  513. /* iwl_finish_nic_init(): INIT_DONE, then wait for the MAC clock. */
  514. iwl_set(CSR_GP_CNTRL, GP_INIT_DONE);
  515. if (iwl_poll(CSR_GP_CNTRL, GP_MAC_CLOCK_READY, GP_MAC_CLOCK_READY,
  516. 25000) < 0) {
  517. iwl_log("MAC clock not ready");
  518. return -1;
  519. }
  520. if (grab_nic() < 0)
  521. return -1;
  522. iwl_wr_prph(APMG_CLK_EN_REG, APMG_CLK_DMA_RQT);
  523. iwl_udelay(20);
  524. prph_set(APMG_PCIDEV_STT_REG, APMG_PCIDEV_L1_ACT_DIS);
  525. iwl_wr_prph(APMG_RTC_INT_STT_REG, APMG_RTC_INT_RFKILL);
  526. return 0;
  527. }
  528. static void stop_master(void)
  529. {
  530. iwl_set(CSR_RESET, RESET_STOP_MASTER);
  531. if (iwl_poll(CSR_RESET, RESET_MASTER_DISABLED, RESET_MASTER_DISABLED,
  532. 100) < 0)
  533. iwl_log("master disable timed out");
  534. }
  535. int iwl_start_hw(void)
  536. {
  537. if (!S)
  538. return -1;
  539. if (prepare_card() < 0)
  540. return -1;
  541. iwl_set(CSR_RESET, RESET_SW_RESET);
  542. iwl_udelay(6000);
  543. if (prepare_card() < 0)
  544. return -1;
  545. if (apm_init() < 0)
  546. return -1;
  547. iwl_wr(CSR_INT, 0xffffffffu);
  548. iwl_wr(CSR_FH_INT_STATUS, 0xffffffffu);
  549. return 0;
  550. }
  551. static void rx_stop(void)
  552. {
  553. iwl_wr(FH_RCSR_CONFIG, 0);
  554. iwl_poll(0x1c44 /* FH_MEM_RSSR_RX_STATUS_REG */, 0x01000000u,
  555. 0x01000000u, 1000);
  556. }
  557. void iwl_stop_device(void)
  558. {
  559. unsigned int c;
  560. if (!S || !S->mmio)
  561. return;
  562. iwl_wr(CSR_INT_MASK, 0);
  563. if (iwl_rd(CSR_GP_CNTRL) & GP_MAC_CLOCK_READY) {
  564. /* Stop the scheduler, every TX DMA channel and RX DMA. */
  565. iwl_wr_prph(SCD_TXFACT, 0);
  566. for (c = 0; c < 8; c++)
  567. iwl_wr(FH_TCSR_CONFIG(c), 0);
  568. rx_stop();
  569. iwl_wr_prph(APMG_CLK_EN_REG, APMG_CLK_DMA_RQT);
  570. /* Inform ME that we are leaving (7000 family). */
  571. prph_set(APMG_PCIDEV_STT_REG, APMG_PCIDEV_WAKE_ME);
  572. iwl_udelay(5000);
  573. }
  574. stop_master();
  575. iwl_set(CSR_RESET, RESET_SW_RESET);
  576. iwl_udelay(6000);
  577. iwl_clr(CSR_GP_CNTRL, GP_INIT_DONE | GP_MAC_ACCESS_REQ);
  578. iwl_wr(CSR_INT, 0xffffffffu);
  579. iwl_wr(CSR_FH_INT_STATUS, 0xffffffffu);
  580. for (c = 0; c < IWL_NUM_Q; c++)
  581. S->q[c].active = 0;
  582. }
  583. /* ---- ring allocation ---------------------------------------------------- */
  584. static void *alloc_zero(unsigned int pages)
  585. {
  586. unsigned int p = mm_alloc_pages(pages);
  587. if (p)
  588. iwl_memzero((void *)p, pages * 4096);
  589. return (void *)p;
  590. }
  591. static int alloc_txq(int qn, unsigned int slots, unsigned int bufsize)
  592. {
  593. struct txq *q = &S->q[qn];
  594. if (!q->tfds) {
  595. q->tfds = alloc_zero(TFD_RING * TFD_SIZE / 4096);
  596. q->bufs = alloc_zero((slots * bufsize + 4095) / 4096);
  597. if (!q->tfds || !q->bufs)
  598. return -1;
  599. q->nslots = slots;
  600. q->bufsize = bufsize;
  601. }
  602. iwl_memzero(q->tfds, TFD_RING * TFD_SIZE);
  603. q->wptr = q->rptr = 0;
  604. q->active = 0;
  605. return 0;
  606. }
  607. static int alloc_rings(void)
  608. {
  609. unsigned int i;
  610. if (!S->rbd) {
  611. S->rbd = alloc_zero(1);
  612. S->rb_stts = alloc_zero(1);
  613. S->bc = alloc_zero((IWL_NUM_Q * BC_ENTRIES * 2 + 4095) / 4096);
  614. S->kw = alloc_zero(1);
  615. S->fwbuf = alloc_zero(FH_MEM_TB_MAX_LENGTH / 4096);
  616. if (!S->rbd || !S->rb_stts || !S->bc || !S->kw || !S->fwbuf)
  617. return -1;
  618. for (i = 0; i < RX_RING; i++) {
  619. S->rb[i] = (unsigned char *)mm_alloc_page();
  620. if (!S->rb[i])
  621. return -1;
  622. }
  623. }
  624. if (alloc_txq(IWL_Q_CMD, CMD_SLOTS, CMD_BUF) < 0 ||
  625. alloc_txq(IWL_Q_AUX, 8, DATA_BUF) < 0 ||
  626. alloc_txq(IWL_Q_BSS, DATA_SLOTS, DATA_BUF) < 0)
  627. return -1;
  628. return 0;
  629. }
  630. static void rx_init(void)
  631. {
  632. unsigned int i;
  633. for (i = 0; i < RX_RING; i++) {
  634. *(volatile unsigned int *)S->rb[i] = 0x55550000u; /* FRAME_INVALID */
  635. S->rbd[i] = (unsigned int)S->rb[i] >> 8;
  636. }
  637. iwl_memzero((void *)S->rb_stts, 16);
  638. S->rx_read = 0;
  639. iwl_wr(FH_RCSR_CONFIG, 0);
  640. iwl_wr(FH_RCSR_RBDCB_WPTR, 0);
  641. iwl_wr(FH_RCSR_FLUSH_RB_REQ, 0);
  642. iwl_wr(FH_RSCSR_RDPTR, 0);
  643. iwl_wr(FH_RSCSR_RBDCB_WPTR, 0);
  644. iwl_wr(FH_RSCSR_RBDCB_BASE, (unsigned int)S->rbd >> 8);
  645. iwl_wr(FH_RSCSR_STTS_WPTR, (unsigned int)S->rb_stts >> 4);
  646. /* enable | ignore RXF empty | irq to host | 4K RBs | RB timeout 0x11 |
  647. * 256 RBDs (log 8). */
  648. iwl_wr(FH_RCSR_CONFIG, 0x80000000u | 0x4u | 0x1000u | 0 |
  649. (0x11u << 4) | (8u << 20));
  650. *(volatile unsigned char *)(S->mmio + CSR_INT_COALESCING) = 0x40;
  651. /* Hand every buffer but the last 8 to the hardware (write pointer
  652. * moves in multiples of 8). */
  653. iwl_wr(FH_RSCSR_RBDCB_WPTR, (RX_RING - 8) & ~7u);
  654. }
  655. static void tx_init(void)
  656. {
  657. unsigned int q;
  658. iwl_wr_prph(SCD_TXFACT, 0);
  659. iwl_wr(FH_KW_MEM_ADDR_REG, (unsigned int)S->kw >> 4);
  660. for (q = 0; q < IWL_NUM_Q; q++)
  661. if (S->q[q].tfds)
  662. iwl_wr(fh_cbbc(q), (unsigned int)S->q[q].tfds >> 8);
  663. prph_set(SCD_GP_CTRL, SCD_GP_AUTO_ACTIVE | SCD_GP_31_QUEUES);
  664. }
  665. /* ---- firmware load ------------------------------------------------------ */
  666. static int load_chunk(unsigned int dst, const unsigned char *src,
  667. unsigned int n)
  668. {
  669. int ext = dst >= FW_MEM_EXT_START && dst <= FW_MEM_EXT_END;
  670. unsigned int t;
  671. iwl_memcpy(S->fwbuf, src, n);
  672. if (ext)
  673. prph_set(LMPM_CHICK, LMPM_EXTENDED_ADDR);
  674. iwl_wr(CSR_INT, INT_FH_TX);
  675. iwl_wr(CSR_FH_INT_STATUS, 0xffffffffu);
  676. iwl_wr(FH_TCSR_CONFIG(FH_SRVC_CHNL), 0);
  677. iwl_wr(FH_SRVC_SRAM_ADDR, dst);
  678. iwl_wr(FH_TFDIB_CTRL0(FH_SRVC_CHNL), (unsigned int)S->fwbuf);
  679. iwl_wr(FH_TFDIB_CTRL1(FH_SRVC_CHNL), n);
  680. iwl_wr(FH_TCSR_BUF_STS(FH_SRVC_CHNL), (1u << 20) | (1u << 12) | 3u);
  681. iwl_wr(FH_TCSR_CONFIG(FH_SRVC_CHNL),
  682. FH_TCSR_DMA_ENABLE | FH_TCSR_HOST_ENDTFD);
  683. for (t = 0; t < 5000000; t += 10) {
  684. if (iwl_rd(CSR_INT) & INT_FH_TX)
  685. break;
  686. iwl_udelay(10);
  687. }
  688. iwl_wr(CSR_INT, INT_FH_TX);
  689. iwl_wr(CSR_FH_INT_STATUS, 0xffffffffu);
  690. if (ext)
  691. prph_clr(LMPM_CHICK, LMPM_EXTENDED_ADDR);
  692. if (t >= 5000000) {
  693. iwl_loghex("firmware chunk timeout at", dst);
  694. return -1;
  695. }
  696. return 0;
  697. }
  698. static int load_sections(int img)
  699. {
  700. unsigned int want = img == IWL_IMG_INIT ? TLV_SEC_INIT : TLV_SEC_RT;
  701. const unsigned char *d = S->fw_data;
  702. unsigned int off = 88, nsec = 0;
  703. while (off + 8 <= S->fw_size) {
  704. unsigned int t = le32(d + off), l = le32(d + off + 4);
  705. const unsigned char *p = d + off + 8;
  706. if (t == want && l > 4) {
  707. unsigned int dst = le32(p), len = l - 4, o;
  708. /* CPU1_CPU2 / paging separators end the CPU1 list
  709. * (single-CPU image: nothing follows that we load). */
  710. if (dst == 0xffffccccu || dst == 0xaaaabbbbu)
  711. break;
  712. for (o = 0; o < len; o += FH_MEM_TB_MAX_LENGTH) {
  713. unsigned int n = len - o;
  714. if (n > FH_MEM_TB_MAX_LENGTH)
  715. n = FH_MEM_TB_MAX_LENGTH;
  716. if (load_chunk(dst + o, p + 4 + o, n) < 0)
  717. return -1;
  718. }
  719. nsec++;
  720. }
  721. off += 8 + ((l + 3) & ~3u);
  722. }
  723. return nsec ? 0 : -1;
  724. }
  725. int iwl_load_fw(int img)
  726. {
  727. if (!S || !S->fw.present)
  728. return -1;
  729. if (prepare_card() < 0)
  730. return -1;
  731. iwl_wr(CSR_INT, 0xffffffffu);
  732. iwl_wr(CSR_UCODE_DRV_GP1_CLR, UCODE_SW_BIT_RFKILL);
  733. iwl_wr(CSR_UCODE_DRV_GP1_CLR, UCODE_CMD_BLOCKED);
  734. iwl_wr(CSR_INT, 0xffffffffu);
  735. /* iwl_pcie_nic_init(): APM, power source VMAIN, NIC config, rings. */
  736. if (apm_init() < 0)
  737. return -1;
  738. iwl_wr_prph(APMG_PS_CTRL_REG,
  739. iwl_rd_prph(APMG_PS_CTRL_REG) & ~APMG_PS_PWR_SRC_MSK);
  740. if (alloc_rings() < 0) {
  741. iwl_log("out of memory for rings");
  742. return -1;
  743. }
  744. S->fatal = 0;
  745. S->waiting = 0;
  746. rx_init();
  747. tx_init();
  748. iwl_wr(CSR_MAC_SHADOW_REG_CTRL,
  749. iwl_rd(CSR_MAC_SHADOW_REG_CTRL) | 0x800fffffu);
  750. iwl_wr(CSR_UCODE_DRV_GP1_CLR, UCODE_SW_BIT_RFKILL);
  751. if (load_sections(img) < 0) {
  752. iwl_log("firmware section load failed");
  753. return -1;
  754. }
  755. iwl_wr(CSR_INT, 0xffffffffu);
  756. iwl_wr(CSR_RESET, 0); /* release the CPU */
  757. return 0;
  758. }
  759. /* After ALIVE: scheduler context, byte-count tables, cmd queue on FIFO 7,
  760. * TX DMA channels (iwl_pcie_tx_start()). */
  761. void iwl_fw_alive(unsigned int scd_base)
  762. {
  763. struct txq *q = &S->q[IWL_Q_CMD];
  764. unsigned int c;
  765. S->scd_base = iwl_rd_prph(SCD_SRAM_BASE_ADDR);
  766. if (scd_base && scd_base != S->scd_base)
  767. iwl_loghex("scd base mismatch, alive says", scd_base);
  768. mem_zero(S->scd_base + SCD_CONTEXT_MEM,
  769. (((SCD_TRANS_TBL_MEM + IWL_NUM_Q * 2) & 0xfffcu) -
  770. SCD_CONTEXT_MEM) / 4);
  771. iwl_wr_prph(SCD_DRAM_BASE_ADDR, (unsigned int)S->bc >> 10);
  772. /* iwl_trans_ac_txq_enable(cmd queue, FIFO 7). */
  773. iwl_wr_prph(SCD_EN_CTRL, 0);
  774. iwl_wr_prph(scd_queue_status(IWL_Q_CMD), 1u << SCD_STTS_ACT_EN);
  775. prph_clr(SCD_AGGR_SEL, 1u << IWL_Q_CMD);
  776. q->wptr = q->rptr = 0;
  777. iwl_wr(HBUS_TARG_WRPTR, 0 | (IWL_Q_CMD << 8));
  778. iwl_wr_prph(scd_queue_rdptr(IWL_Q_CMD), 0);
  779. mem_wr32(S->scd_base + SCD_CONTEXT_MEM + IWL_Q_CMD * 8, 0);
  780. mem_wr32(S->scd_base + SCD_CONTEXT_MEM + IWL_Q_CMD * 8 + 4,
  781. (64u & 0x7f) | ((64u << 16) & 0x7f0000u));
  782. iwl_wr_prph(scd_queue_status(IWL_Q_CMD),
  783. (1u << SCD_STTS_ACTIVE) | (7u << SCD_STTS_TXF) |
  784. (1u << SCD_STTS_WSL) | SCD_STTS_MSK);
  785. iwl_wr_prph(SCD_EN_CTRL, 1u << IWL_Q_CMD);
  786. q->active = 1;
  787. iwl_wr_prph(SCD_TXFACT, 0xffu); /* FIFOs 0..7 */
  788. for (c = 0; c < 8; c++)
  789. iwl_wr(FH_TCSR_CONFIG(c),
  790. FH_TCSR_DMA_ENABLE | FH_TCSR_CREDIT_ENABLE);
  791. iwl_wr(FH_TX_CHICKEN_BITS, iwl_rd(FH_TX_CHICKEN_BITS) | 0x2u);
  792. prph_clr(APMG_PCIDEV_STT_REG, APMG_PCIDEV_L1_ACT_DIS);
  793. }
  794. /* DQA data/mgmt queue (iwl_trans_pcie_txq_enable() with cfg == NULL: the
  795. * firmware's SCD_QUEUE_CFG command configures the scheduler itself). */
  796. void iwl_txq_enable(int qn, unsigned short ssn)
  797. {
  798. struct txq *q = &S->q[qn];
  799. /* SCD write-pointer quirk: never move by a multiple of 0x40. */
  800. if (!((ssn - q->wptr) & 0x3f) && ssn != q->wptr)
  801. ssn++;
  802. q->rptr = q->wptr = ssn & 0xff;
  803. iwl_wr(HBUS_TARG_WRPTR, (ssn & 0xff) | ((unsigned int)qn << 8));
  804. q->active = 1;
  805. }
  806. unsigned int iwl_txq_wptr(int q)
  807. {
  808. return S->q[q].wptr;
  809. }
  810. /* ---- TX ---------------------------------------------------------------- */
  811. static void tfd_set(unsigned char *tfd, unsigned int tb, unsigned int addr,
  812. unsigned int len)
  813. {
  814. unsigned char *e = tfd + 4 + tb * 6;
  815. e[0] = addr; e[1] = addr >> 8; e[2] = addr >> 16; e[3] = addr >> 24;
  816. e[4] = (len << 4) & 0xf0;
  817. e[5] = len >> 4;
  818. tfd[3] = (unsigned char)(tb + 1);
  819. }
  820. static void bc_set(int qn, unsigned int idx, unsigned int bytes,
  821. unsigned int sta)
  822. {
  823. unsigned short *t = S->bc + qn * BC_ENTRIES;
  824. unsigned short v;
  825. /* + CRC + delimiter, in dwords (bc_table_dword on 7000). */
  826. v = (unsigned short)((((bytes + 8) + 3) / 4) | (sta << 12));
  827. t[idx] = v;
  828. if (idx < BC_ENTRIES - TFD_RING)
  829. t[TFD_RING + idx] = v;
  830. }
  831. static int txq_space(struct txq *q)
  832. {
  833. return ((q->wptr - q->rptr) & 0xff) < q->nslots - 1;
  834. }
  835. static unsigned char *slot_buf(struct txq *q, unsigned int idx)
  836. {
  837. return q->bufs + (idx & (q->nslots - 1)) * q->bufsize;
  838. }
  839. static void ring_doorbell(int qn)
  840. {
  841. struct txq *q = &S->q[qn];
  842. q->wptr = (q->wptr + 1) & 0xff;
  843. iwl_wr(HBUS_TARG_WRPTR, q->wptr | ((unsigned int)qn << 8));
  844. }
  845. static int enqueue_cmd(unsigned int id, const void *data, unsigned int len,
  846. unsigned short *seq_out)
  847. {
  848. struct txq *q = &S->q[IWL_Q_CMD];
  849. /* mvm sets wide_cmd_header: legacy (group 0) commands are sent as
  850. * group 1 with the 8-byte header (iwl_trans_send_cmd(), DEF_ID). */
  851. unsigned char group = (id >> 8) & 0xff ? (id >> 8) & 0xff : 1;
  852. unsigned char *buf, *tfd;
  853. unsigned int hdr = 8, total = hdr + len, idx;
  854. unsigned short seq;
  855. if (!q->active || total > CMD_BUF)
  856. return -1;
  857. if (!txq_space(q)) {
  858. /* Command responses reclaim; every one we sent was synchronous,
  859. * so the ring can simply be considered drained. */
  860. q->rptr = q->wptr;
  861. }
  862. idx = q->wptr;
  863. buf = slot_buf(q, idx);
  864. seq = (unsigned short)((IWL_Q_CMD << 8) | idx);
  865. buf[0] = id & 0xff;
  866. buf[1] = group;
  867. buf[2] = seq & 0xff;
  868. buf[3] = seq >> 8;
  869. buf[4] = len & 0xff;
  870. buf[5] = len >> 8;
  871. buf[6] = 0;
  872. buf[7] = (id >> 16) & 0xff;
  873. iwl_memcpy(buf + hdr, data, len);
  874. tfd = q->tfds + idx * TFD_SIZE;
  875. iwl_memzero(tfd, TFD_SIZE);
  876. if (total <= FIRST_TB) {
  877. tfd_set(tfd, 0, (unsigned int)buf, total);
  878. } else {
  879. tfd_set(tfd, 0, (unsigned int)buf, FIRST_TB);
  880. tfd_set(tfd, 1, (unsigned int)buf + FIRST_TB, total - FIRST_TB);
  881. }
  882. *seq_out = seq;
  883. ring_doorbell(IWL_Q_CMD);
  884. return 0;
  885. }
  886. int iwl_tx(int qn, const void *txcmd, unsigned int txlen,
  887. const void *frame, unsigned int flen, unsigned char sta_id,
  888. unsigned int mic_len)
  889. {
  890. struct txq *q = &S->q[qn];
  891. unsigned char *buf, *tfd;
  892. unsigned int idx, total, t;
  893. if (!q->active || 4 + txlen + flen > q->bufsize)
  894. return -1;
  895. for (t = 0; !txq_space(q); t++) {
  896. if (t > 2000 || iwl_rx_poll() < 0)
  897. return -1;
  898. iwl_udelay(50);
  899. }
  900. idx = q->wptr;
  901. buf = slot_buf(q, idx);
  902. /* struct iwl_device_tx_cmd: 4-byte header (TX_CMD, seq) + tx_cmd. */
  903. buf[0] = 0x1c; /* TX_CMD */
  904. buf[1] = 0;
  905. buf[2] = idx;
  906. buf[3] = (unsigned char)qn;
  907. iwl_memcpy(buf + 4, txcmd, txlen);
  908. iwl_memcpy(buf + 4 + txlen, frame, flen);
  909. /* dram_lsb_ptr/msb: where the firmware writes the tx_cmd scratch
  910. * (offset 8 of struct iwl_tx_cmd), as iwl_pcie_tx() does. */
  911. if (txlen >= 56) {
  912. unsigned int scratch = (unsigned int)buf + 4 + 8;
  913. buf[4 + 44] = scratch;
  914. buf[4 + 45] = scratch >> 8;
  915. buf[4 + 46] = scratch >> 16;
  916. buf[4 + 47] = scratch >> 24;
  917. buf[4 + 48] = 0;
  918. }
  919. total = 4 + txlen + flen;
  920. tfd = q->tfds + idx * TFD_SIZE;
  921. iwl_memzero(tfd, TFD_SIZE);
  922. tfd_set(tfd, 0, (unsigned int)buf, FIRST_TB);
  923. tfd_set(tfd, 1, (unsigned int)buf + FIRST_TB, total - FIRST_TB);
  924. /* Byte count: the over-the-air MPDU (tx_cmd->len) plus the MIC the
  925. * firmware appends for a hardware-encrypted frame. */
  926. bc_set(qn, idx, flen + mic_len, sta_id);
  927. ring_doorbell(qn);
  928. return 0;
  929. }
  930. void iwl_tx_reclaim(int qn, unsigned int idx)
  931. {
  932. if (qn >= 0 && qn < IWL_NUM_Q)
  933. S->q[qn].rptr = (idx + 1) & 0xff;
  934. }
  935. /* ---- RX ---------------------------------------------------------------- */
  936. void iwl_set_rx_handler(iwl_rx_handler h)
  937. {
  938. S->handler = h;
  939. }
  940. static void handle_pkt(const struct iwl_pkt *pkt)
  941. {
  942. if (S->waiting && !(pkt->seq & 0x8000u) && pkt->seq == S->wait_seq) {
  943. unsigned int n = iwl_pkt_payload_len(pkt);
  944. if (n > S->resp_max)
  945. n = S->resp_max;
  946. if (S->resp && n)
  947. iwl_memcpy(S->resp, pkt->data, n);
  948. S->resp_len = (int)iwl_pkt_payload_len(pkt);
  949. S->resp_done = 1;
  950. return;
  951. }
  952. if (S->wait_notif && pkt->group == 0 && pkt->cmd == S->wait_notif)
  953. S->notif_seen = 1;
  954. if (S->handler)
  955. S->handler(pkt);
  956. }
  957. int iwl_rx_poll(void)
  958. {
  959. unsigned int r, n = 0, ints;
  960. if (!S || !S->rbd)
  961. return 0;
  962. ints = iwl_rd(CSR_INT);
  963. if (ints & (INT_SW_ERR | INT_HW_ERR)) {
  964. if (!S->fatal) {
  965. iwl_loghex("firmware/hardware error, CSR_INT", ints);
  966. iwl_loghex("GP_CNTRL", iwl_rd(CSR_GP_CNTRL));
  967. }
  968. S->fatal = 1;
  969. iwl_wr(CSR_INT, ints);
  970. return -1;
  971. }
  972. if (ints)
  973. iwl_wr(CSR_INT, ints);
  974. if (iwl_rd(CSR_FH_INT_STATUS))
  975. iwl_wr(CSR_FH_INT_STATUS, 0xffffffffu);
  976. r = S->rb_stts[0] & 0x0fff;
  977. r &= RX_RING - 1;
  978. while (S->rx_read != r) {
  979. unsigned char *page = S->rb[S->rx_read];
  980. unsigned int off = 0;
  981. while (off + 12 < 4096) {
  982. const struct iwl_pkt *pkt =
  983. (const struct iwl_pkt *)(page + off);
  984. unsigned int len;
  985. if (pkt->len_n_flags == 0x55550000u)
  986. break;
  987. len = (pkt->len_n_flags & 0x3fffu) + 4;
  988. if (len < 8 || off + len > 4096)
  989. break;
  990. handle_pkt(pkt);
  991. off += (len + 63) & ~63u;
  992. }
  993. *(volatile unsigned int *)page = 0x55550000u;
  994. S->rx_read = (S->rx_read + 1) & (RX_RING - 1);
  995. n++;
  996. /* Give the hardware the slot 8 behind us (multiple of 8). */
  997. if ((S->rx_read & 7) == 0)
  998. iwl_wr(FH_RSCSR_RBDCB_WPTR,
  999. (S->rx_read + RX_RING - 8) & (RX_RING - 1));
  1000. }
  1001. return S->fatal ? -1 : (int)n;
  1002. }
  1003. int iwl_wait_notif(unsigned char cmd, unsigned int ms)
  1004. {
  1005. unsigned int t;
  1006. S->wait_notif = cmd;
  1007. S->notif_seen = 0;
  1008. for (t = 0; t < ms * 10; t++) {
  1009. if (iwl_rx_poll() < 0)
  1010. break;
  1011. if (S->notif_seen)
  1012. break;
  1013. iwl_udelay(100);
  1014. }
  1015. S->wait_notif = 0;
  1016. return S->notif_seen ? 0 : -1;
  1017. }
  1018. int iwl_send_cmd(unsigned int id, const void *data, unsigned int len,
  1019. void *resp, unsigned int resp_max)
  1020. {
  1021. unsigned short seq;
  1022. unsigned int t;
  1023. if (!S || S->fatal)
  1024. return -1;
  1025. S->resp = resp;
  1026. S->resp_max = resp_max;
  1027. S->resp_done = 0;
  1028. S->resp_len = -1;
  1029. if (enqueue_cmd(id, data, len, &seq) < 0)
  1030. return -1;
  1031. S->wait_seq = seq;
  1032. S->waiting = 1;
  1033. for (t = 0; t < 20000 && !S->resp_done; t++) { /* 2 s */
  1034. if (iwl_rx_poll() < 0)
  1035. break;
  1036. if (!S->resp_done)
  1037. iwl_udelay(100);
  1038. }
  1039. S->waiting = 0;
  1040. S->q[IWL_Q_CMD].rptr = S->q[IWL_Q_CMD].wptr;
  1041. if (!S->resp_done) {
  1042. iwl_loghex("command timeout, id", id);
  1043. return -1;
  1044. }
  1045. return S->resp_len;
  1046. }
  1047. int iwl_send_cmd_async(unsigned int id, const void *data, unsigned int len)
  1048. {
  1049. unsigned short seq;
  1050. if (!S || S->fatal)
  1051. return -1;
  1052. return enqueue_cmd(id, data, len, &seq);
  1053. }