iwl_trans.c 40 KB

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