iwl_trans.c 38 KB

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