sdio.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (c) 2004-2011 Atheros Communications Inc.
  4. * Copyright (c) 2011-2012,2017 Qualcomm Atheros, Inc.
  5. * Copyright (c) 2016-2017 Erik Stromdahl <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/mmc/card.h>
  9. #include <linux/mmc/mmc.h>
  10. #include <linux/mmc/host.h>
  11. #include <linux/mmc/sdio_func.h>
  12. #include <linux/mmc/sdio_ids.h>
  13. #include <linux/mmc/sdio.h>
  14. #include <linux/mmc/sd.h>
  15. #include <linux/bitfield.h>
  16. #include "core.h"
  17. #include "bmi.h"
  18. #include "debug.h"
  19. #include "hif.h"
  20. #include "htc.h"
  21. #include "mac.h"
  22. #include "targaddrs.h"
  23. #include "trace.h"
  24. #include "sdio.h"
  25. #include "coredump.h"
  26. void ath10k_sdio_fw_crashed_dump(struct ath10k *ar);
  27. #define ATH10K_SDIO_VSG_BUF_SIZE (64 * 1024)
  28. /* inlined helper functions */
  29. static inline int ath10k_sdio_calc_txrx_padded_len(struct ath10k_sdio *ar_sdio,
  30. size_t len)
  31. {
  32. return __ALIGN_MASK((len), ar_sdio->mbox_info.block_mask);
  33. }
  34. static inline enum ath10k_htc_ep_id pipe_id_to_eid(u8 pipe_id)
  35. {
  36. return (enum ath10k_htc_ep_id)pipe_id;
  37. }
  38. static inline void ath10k_sdio_mbox_free_rx_pkt(struct ath10k_sdio_rx_data *pkt)
  39. {
  40. dev_kfree_skb(pkt->skb);
  41. pkt->skb = NULL;
  42. pkt->alloc_len = 0;
  43. pkt->act_len = 0;
  44. pkt->trailer_only = false;
  45. }
  46. static inline int ath10k_sdio_mbox_alloc_rx_pkt(struct ath10k_sdio_rx_data *pkt,
  47. size_t act_len, size_t full_len,
  48. bool part_of_bundle,
  49. bool last_in_bundle)
  50. {
  51. pkt->skb = dev_alloc_skb(full_len);
  52. if (!pkt->skb)
  53. return -ENOMEM;
  54. pkt->act_len = act_len;
  55. pkt->alloc_len = full_len;
  56. pkt->part_of_bundle = part_of_bundle;
  57. pkt->last_in_bundle = last_in_bundle;
  58. pkt->trailer_only = false;
  59. return 0;
  60. }
  61. static inline bool is_trailer_only_msg(struct ath10k_sdio_rx_data *pkt)
  62. {
  63. bool trailer_only = false;
  64. struct ath10k_htc_hdr *htc_hdr =
  65. (struct ath10k_htc_hdr *)pkt->skb->data;
  66. u16 len = __le16_to_cpu(htc_hdr->len);
  67. if (len == htc_hdr->trailer_len)
  68. trailer_only = true;
  69. return trailer_only;
  70. }
  71. /* sdio/mmc functions */
  72. static inline void ath10k_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
  73. unsigned int address,
  74. unsigned char val)
  75. {
  76. *arg = FIELD_PREP(BIT(31), write) |
  77. FIELD_PREP(BIT(27), raw) |
  78. FIELD_PREP(BIT(26), 1) |
  79. FIELD_PREP(GENMASK(25, 9), address) |
  80. FIELD_PREP(BIT(8), 1) |
  81. FIELD_PREP(GENMASK(7, 0), val);
  82. }
  83. static int ath10k_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
  84. unsigned int address,
  85. unsigned char byte)
  86. {
  87. struct mmc_command io_cmd;
  88. memset(&io_cmd, 0, sizeof(io_cmd));
  89. ath10k_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
  90. io_cmd.opcode = SD_IO_RW_DIRECT;
  91. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  92. return mmc_wait_for_cmd(card->host, &io_cmd, 0);
  93. }
  94. static int ath10k_sdio_func0_cmd52_rd_byte(struct mmc_card *card,
  95. unsigned int address,
  96. unsigned char *byte)
  97. {
  98. struct mmc_command io_cmd;
  99. int ret;
  100. memset(&io_cmd, 0, sizeof(io_cmd));
  101. ath10k_sdio_set_cmd52_arg(&io_cmd.arg, 0, 0, address, 0);
  102. io_cmd.opcode = SD_IO_RW_DIRECT;
  103. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  104. ret = mmc_wait_for_cmd(card->host, &io_cmd, 0);
  105. if (!ret)
  106. *byte = io_cmd.resp[0];
  107. return ret;
  108. }
  109. static int ath10k_sdio_config(struct ath10k *ar)
  110. {
  111. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  112. struct sdio_func *func = ar_sdio->func;
  113. unsigned char byte, asyncintdelay = 2;
  114. int ret;
  115. ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio configuration\n");
  116. sdio_claim_host(func);
  117. byte = 0;
  118. ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
  119. SDIO_CCCR_DRIVE_STRENGTH,
  120. &byte);
  121. byte &= ~ATH10K_SDIO_DRIVE_DTSX_MASK;
  122. byte |= FIELD_PREP(ATH10K_SDIO_DRIVE_DTSX_MASK,
  123. ATH10K_SDIO_DRIVE_DTSX_TYPE_D);
  124. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  125. SDIO_CCCR_DRIVE_STRENGTH,
  126. byte);
  127. byte = 0;
  128. ret = ath10k_sdio_func0_cmd52_rd_byte(
  129. func->card,
  130. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_ADDR,
  131. &byte);
  132. byte |= (CCCR_SDIO_DRIVER_STRENGTH_ENABLE_A |
  133. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_C |
  134. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_D);
  135. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  136. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_ADDR,
  137. byte);
  138. if (ret) {
  139. ath10k_warn(ar, "failed to enable driver strength: %d\n", ret);
  140. goto out;
  141. }
  142. byte = 0;
  143. ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
  144. CCCR_SDIO_IRQ_MODE_REG_SDIO3,
  145. &byte);
  146. byte |= SDIO_IRQ_MODE_ASYNC_4BIT_IRQ_SDIO3;
  147. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  148. CCCR_SDIO_IRQ_MODE_REG_SDIO3,
  149. byte);
  150. if (ret) {
  151. ath10k_warn(ar, "failed to enable 4-bit async irq mode: %d\n",
  152. ret);
  153. goto out;
  154. }
  155. byte = 0;
  156. ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
  157. CCCR_SDIO_ASYNC_INT_DELAY_ADDRESS,
  158. &byte);
  159. byte &= ~CCCR_SDIO_ASYNC_INT_DELAY_MASK;
  160. byte |= FIELD_PREP(CCCR_SDIO_ASYNC_INT_DELAY_MASK, asyncintdelay);
  161. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  162. CCCR_SDIO_ASYNC_INT_DELAY_ADDRESS,
  163. byte);
  164. /* give us some time to enable, in ms */
  165. func->enable_timeout = 100;
  166. ret = sdio_set_block_size(func, ar_sdio->mbox_info.block_size);
  167. if (ret) {
  168. ath10k_warn(ar, "failed to set sdio block size to %d: %d\n",
  169. ar_sdio->mbox_info.block_size, ret);
  170. goto out;
  171. }
  172. out:
  173. sdio_release_host(func);
  174. return ret;
  175. }
  176. static int ath10k_sdio_write32(struct ath10k *ar, u32 addr, u32 val)
  177. {
  178. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  179. struct sdio_func *func = ar_sdio->func;
  180. int ret;
  181. sdio_claim_host(func);
  182. sdio_writel(func, val, addr, &ret);
  183. if (ret) {
  184. ath10k_warn(ar, "failed to write 0x%x to address 0x%x: %d\n",
  185. val, addr, ret);
  186. goto out;
  187. }
  188. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio write32 addr 0x%x val 0x%x\n",
  189. addr, val);
  190. out:
  191. sdio_release_host(func);
  192. return ret;
  193. }
  194. static int ath10k_sdio_writesb32(struct ath10k *ar, u32 addr, u32 val)
  195. {
  196. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  197. struct sdio_func *func = ar_sdio->func;
  198. __le32 *buf;
  199. int ret;
  200. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  201. if (!buf)
  202. return -ENOMEM;
  203. *buf = cpu_to_le32(val);
  204. sdio_claim_host(func);
  205. ret = sdio_writesb(func, addr, buf, sizeof(*buf));
  206. if (ret) {
  207. ath10k_warn(ar, "failed to write value 0x%x to fixed sb address 0x%x: %d\n",
  208. val, addr, ret);
  209. goto out;
  210. }
  211. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio writesb32 addr 0x%x val 0x%x\n",
  212. addr, val);
  213. out:
  214. sdio_release_host(func);
  215. kfree(buf);
  216. return ret;
  217. }
  218. static int ath10k_sdio_read32(struct ath10k *ar, u32 addr, u32 *val)
  219. {
  220. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  221. struct sdio_func *func = ar_sdio->func;
  222. int ret;
  223. sdio_claim_host(func);
  224. *val = sdio_readl(func, addr, &ret);
  225. if (ret) {
  226. ath10k_warn(ar, "failed to read from address 0x%x: %d\n",
  227. addr, ret);
  228. goto out;
  229. }
  230. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read32 addr 0x%x val 0x%x\n",
  231. addr, *val);
  232. out:
  233. sdio_release_host(func);
  234. return ret;
  235. }
  236. static int ath10k_sdio_read(struct ath10k *ar, u32 addr, void *buf, size_t len)
  237. {
  238. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  239. struct sdio_func *func = ar_sdio->func;
  240. int ret;
  241. sdio_claim_host(func);
  242. ret = sdio_memcpy_fromio(func, buf, addr, len);
  243. if (ret) {
  244. ath10k_warn(ar, "failed to read from address 0x%x: %d\n",
  245. addr, ret);
  246. goto out;
  247. }
  248. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read addr 0x%x buf 0x%p len %zu\n",
  249. addr, buf, len);
  250. ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio read ", buf, len);
  251. out:
  252. sdio_release_host(func);
  253. return ret;
  254. }
  255. static int ath10k_sdio_write(struct ath10k *ar, u32 addr, const void *buf, size_t len)
  256. {
  257. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  258. struct sdio_func *func = ar_sdio->func;
  259. int ret;
  260. sdio_claim_host(func);
  261. /* For some reason toio() doesn't have const for the buffer, need
  262. * an ugly hack to workaround that.
  263. */
  264. ret = sdio_memcpy_toio(func, addr, (void *)buf, len);
  265. if (ret) {
  266. ath10k_warn(ar, "failed to write to address 0x%x: %d\n",
  267. addr, ret);
  268. goto out;
  269. }
  270. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio write addr 0x%x buf 0x%p len %zu\n",
  271. addr, buf, len);
  272. ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio write ", buf, len);
  273. out:
  274. sdio_release_host(func);
  275. return ret;
  276. }
  277. static int ath10k_sdio_readsb(struct ath10k *ar, u32 addr, void *buf, size_t len)
  278. {
  279. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  280. struct sdio_func *func = ar_sdio->func;
  281. int ret;
  282. sdio_claim_host(func);
  283. len = round_down(len, ar_sdio->mbox_info.block_size);
  284. ret = sdio_readsb(func, buf, addr, len);
  285. if (ret) {
  286. ath10k_warn(ar, "failed to read from fixed (sb) address 0x%x: %d\n",
  287. addr, ret);
  288. goto out;
  289. }
  290. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio readsb addr 0x%x buf 0x%p len %zu\n",
  291. addr, buf, len);
  292. ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio readsb ", buf, len);
  293. out:
  294. sdio_release_host(func);
  295. return ret;
  296. }
  297. /* HIF mbox functions */
  298. static int ath10k_sdio_mbox_rx_process_packet(struct ath10k *ar,
  299. struct ath10k_sdio_rx_data *pkt,
  300. u32 *lookaheads,
  301. int *n_lookaheads)
  302. {
  303. struct ath10k_htc *htc = &ar->htc;
  304. struct sk_buff *skb = pkt->skb;
  305. struct ath10k_htc_hdr *htc_hdr = (struct ath10k_htc_hdr *)skb->data;
  306. bool trailer_present = htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
  307. enum ath10k_htc_ep_id eid;
  308. u8 *trailer;
  309. int ret;
  310. if (trailer_present) {
  311. trailer = skb->data + skb->len - htc_hdr->trailer_len;
  312. eid = pipe_id_to_eid(htc_hdr->eid);
  313. ret = ath10k_htc_process_trailer(htc,
  314. trailer,
  315. htc_hdr->trailer_len,
  316. eid,
  317. lookaheads,
  318. n_lookaheads);
  319. if (ret)
  320. return ret;
  321. if (is_trailer_only_msg(pkt))
  322. pkt->trailer_only = true;
  323. skb_trim(skb, skb->len - htc_hdr->trailer_len);
  324. }
  325. skb_pull(skb, sizeof(*htc_hdr));
  326. return 0;
  327. }
  328. static int ath10k_sdio_mbox_rx_process_packets(struct ath10k *ar,
  329. u32 lookaheads[],
  330. int *n_lookahead)
  331. {
  332. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  333. struct ath10k_htc *htc = &ar->htc;
  334. struct ath10k_sdio_rx_data *pkt;
  335. struct ath10k_htc_ep *ep;
  336. struct ath10k_skb_rxcb *cb;
  337. enum ath10k_htc_ep_id id;
  338. int ret, i, *n_lookahead_local;
  339. u32 *lookaheads_local;
  340. int lookahead_idx = 0;
  341. for (i = 0; i < ar_sdio->n_rx_pkts; i++) {
  342. lookaheads_local = lookaheads;
  343. n_lookahead_local = n_lookahead;
  344. id = ((struct ath10k_htc_hdr *)
  345. &lookaheads[lookahead_idx++])->eid;
  346. if (id >= ATH10K_HTC_EP_COUNT) {
  347. ath10k_warn(ar, "invalid endpoint in look-ahead: %d\n",
  348. id);
  349. ret = -ENOMEM;
  350. goto out;
  351. }
  352. ep = &htc->endpoint[id];
  353. if (ep->service_id == 0) {
  354. ath10k_warn(ar, "ep %d is not connected\n", id);
  355. ret = -ENOMEM;
  356. goto out;
  357. }
  358. pkt = &ar_sdio->rx_pkts[i];
  359. if (pkt->part_of_bundle && !pkt->last_in_bundle) {
  360. /* Only read lookahead's from RX trailers
  361. * for the last packet in a bundle.
  362. */
  363. lookahead_idx--;
  364. lookaheads_local = NULL;
  365. n_lookahead_local = NULL;
  366. }
  367. ret = ath10k_sdio_mbox_rx_process_packet(ar,
  368. pkt,
  369. lookaheads_local,
  370. n_lookahead_local);
  371. if (ret)
  372. goto out;
  373. if (!pkt->trailer_only) {
  374. cb = ATH10K_SKB_RXCB(pkt->skb);
  375. cb->eid = id;
  376. skb_queue_tail(&ar_sdio->rx_head, pkt->skb);
  377. queue_work(ar->workqueue_aux,
  378. &ar_sdio->async_work_rx);
  379. } else {
  380. kfree_skb(pkt->skb);
  381. }
  382. /* The RX complete handler now owns the skb...*/
  383. pkt->skb = NULL;
  384. pkt->alloc_len = 0;
  385. }
  386. ret = 0;
  387. out:
  388. /* Free all packets that was not passed on to the RX completion
  389. * handler...
  390. */
  391. for (; i < ar_sdio->n_rx_pkts; i++)
  392. ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
  393. return ret;
  394. }
  395. static int ath10k_sdio_mbox_alloc_bundle(struct ath10k *ar,
  396. struct ath10k_sdio_rx_data *rx_pkts,
  397. struct ath10k_htc_hdr *htc_hdr,
  398. size_t full_len, size_t act_len,
  399. size_t *bndl_cnt)
  400. {
  401. int ret, i;
  402. u8 max_msgs = ar->htc.max_msgs_per_htc_bundle;
  403. *bndl_cnt = ath10k_htc_get_bundle_count(max_msgs, htc_hdr->flags);
  404. if (*bndl_cnt > max_msgs) {
  405. ath10k_warn(ar,
  406. "HTC bundle length %u exceeds maximum %u\n",
  407. le16_to_cpu(htc_hdr->len),
  408. max_msgs);
  409. return -ENOMEM;
  410. }
  411. /* Allocate bndl_cnt extra skb's for the bundle.
  412. * The package containing the
  413. * ATH10K_HTC_FLAG_BUNDLE_MASK flag is not included
  414. * in bndl_cnt. The skb for that packet will be
  415. * allocated separately.
  416. */
  417. for (i = 0; i < *bndl_cnt; i++) {
  418. ret = ath10k_sdio_mbox_alloc_rx_pkt(&rx_pkts[i],
  419. act_len,
  420. full_len,
  421. true,
  422. false);
  423. if (ret)
  424. return ret;
  425. }
  426. return 0;
  427. }
  428. static int ath10k_sdio_mbox_rx_alloc(struct ath10k *ar,
  429. u32 lookaheads[], int n_lookaheads)
  430. {
  431. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  432. struct ath10k_htc_hdr *htc_hdr;
  433. size_t full_len, act_len;
  434. bool last_in_bundle;
  435. int ret, i;
  436. int pkt_cnt = 0;
  437. if (n_lookaheads > ATH10K_SDIO_MAX_RX_MSGS) {
  438. ath10k_warn(ar, "the total number of pkts to be fetched (%u) exceeds maximum %u\n",
  439. n_lookaheads, ATH10K_SDIO_MAX_RX_MSGS);
  440. ret = -ENOMEM;
  441. goto err;
  442. }
  443. for (i = 0; i < n_lookaheads; i++) {
  444. htc_hdr = (struct ath10k_htc_hdr *)&lookaheads[i];
  445. last_in_bundle = false;
  446. if (le16_to_cpu(htc_hdr->len) > ATH10K_HTC_MBOX_MAX_PAYLOAD_LENGTH) {
  447. ath10k_warn(ar, "payload length %d exceeds max htc length: %zu\n",
  448. le16_to_cpu(htc_hdr->len),
  449. ATH10K_HTC_MBOX_MAX_PAYLOAD_LENGTH);
  450. ret = -ENOMEM;
  451. ath10k_core_start_recovery(ar);
  452. ath10k_warn(ar, "exceeds length, start recovery\n");
  453. goto err;
  454. }
  455. act_len = le16_to_cpu(htc_hdr->len) + sizeof(*htc_hdr);
  456. full_len = ath10k_sdio_calc_txrx_padded_len(ar_sdio, act_len);
  457. if (full_len > ATH10K_SDIO_MAX_BUFFER_SIZE) {
  458. ath10k_warn(ar, "rx buffer requested with invalid htc_hdr length (%d, 0x%x): %d\n",
  459. htc_hdr->eid, htc_hdr->flags,
  460. le16_to_cpu(htc_hdr->len));
  461. ret = -EINVAL;
  462. goto err;
  463. }
  464. if (ath10k_htc_get_bundle_count(
  465. ar->htc.max_msgs_per_htc_bundle, htc_hdr->flags)) {
  466. /* HTC header indicates that every packet to follow
  467. * has the same padded length so that it can be
  468. * optimally fetched as a full bundle.
  469. */
  470. size_t bndl_cnt;
  471. ret = ath10k_sdio_mbox_alloc_bundle(ar,
  472. &ar_sdio->rx_pkts[pkt_cnt],
  473. htc_hdr,
  474. full_len,
  475. act_len,
  476. &bndl_cnt);
  477. if (ret) {
  478. ath10k_warn(ar, "failed to allocate a bundle: %d\n",
  479. ret);
  480. goto err;
  481. }
  482. pkt_cnt += bndl_cnt;
  483. /* next buffer will be the last in the bundle */
  484. last_in_bundle = true;
  485. }
  486. /* Allocate skb for packet. If the packet had the
  487. * ATH10K_HTC_FLAG_BUNDLE_MASK flag set, all bundled
  488. * packet skb's have been allocated in the previous step.
  489. */
  490. if (htc_hdr->flags & ATH10K_HTC_FLAGS_RECV_1MORE_BLOCK)
  491. full_len += ATH10K_HIF_MBOX_BLOCK_SIZE;
  492. ret = ath10k_sdio_mbox_alloc_rx_pkt(&ar_sdio->rx_pkts[pkt_cnt],
  493. act_len,
  494. full_len,
  495. last_in_bundle,
  496. last_in_bundle);
  497. if (ret) {
  498. ath10k_warn(ar, "alloc_rx_pkt error %d\n", ret);
  499. goto err;
  500. }
  501. pkt_cnt++;
  502. }
  503. ar_sdio->n_rx_pkts = pkt_cnt;
  504. return 0;
  505. err:
  506. for (i = 0; i < ATH10K_SDIO_MAX_RX_MSGS; i++) {
  507. if (!ar_sdio->rx_pkts[i].alloc_len)
  508. break;
  509. ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
  510. }
  511. return ret;
  512. }
  513. static int ath10k_sdio_mbox_rx_fetch(struct ath10k *ar)
  514. {
  515. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  516. struct ath10k_sdio_rx_data *pkt = &ar_sdio->rx_pkts[0];
  517. struct sk_buff *skb = pkt->skb;
  518. struct ath10k_htc_hdr *htc_hdr;
  519. int ret;
  520. ret = ath10k_sdio_readsb(ar, ar_sdio->mbox_info.htc_addr,
  521. skb->data, pkt->alloc_len);
  522. if (ret)
  523. goto err;
  524. htc_hdr = (struct ath10k_htc_hdr *)skb->data;
  525. pkt->act_len = le16_to_cpu(htc_hdr->len) + sizeof(*htc_hdr);
  526. if (pkt->act_len > pkt->alloc_len) {
  527. ret = -EINVAL;
  528. goto err;
  529. }
  530. skb_put(skb, pkt->act_len);
  531. return 0;
  532. err:
  533. ar_sdio->n_rx_pkts = 0;
  534. ath10k_sdio_mbox_free_rx_pkt(pkt);
  535. return ret;
  536. }
  537. static int ath10k_sdio_mbox_rx_fetch_bundle(struct ath10k *ar)
  538. {
  539. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  540. struct ath10k_sdio_rx_data *pkt;
  541. struct ath10k_htc_hdr *htc_hdr;
  542. int ret, i;
  543. u32 pkt_offset, virt_pkt_len;
  544. virt_pkt_len = 0;
  545. for (i = 0; i < ar_sdio->n_rx_pkts; i++)
  546. virt_pkt_len += ar_sdio->rx_pkts[i].alloc_len;
  547. if (virt_pkt_len > ATH10K_SDIO_VSG_BUF_SIZE) {
  548. ath10k_warn(ar, "sdio vsg buffer size limit: %d\n", virt_pkt_len);
  549. ret = -E2BIG;
  550. goto err;
  551. }
  552. ret = ath10k_sdio_readsb(ar, ar_sdio->mbox_info.htc_addr,
  553. ar_sdio->vsg_buffer, virt_pkt_len);
  554. if (ret) {
  555. ath10k_warn(ar, "failed to read bundle packets: %d", ret);
  556. goto err;
  557. }
  558. pkt_offset = 0;
  559. for (i = 0; i < ar_sdio->n_rx_pkts; i++) {
  560. pkt = &ar_sdio->rx_pkts[i];
  561. htc_hdr = (struct ath10k_htc_hdr *)(ar_sdio->vsg_buffer + pkt_offset);
  562. pkt->act_len = le16_to_cpu(htc_hdr->len) + sizeof(*htc_hdr);
  563. if (pkt->act_len > pkt->alloc_len) {
  564. ret = -EINVAL;
  565. goto err;
  566. }
  567. skb_put_data(pkt->skb, htc_hdr, pkt->act_len);
  568. pkt_offset += pkt->alloc_len;
  569. }
  570. return 0;
  571. err:
  572. /* Free all packets that was not successfully fetched. */
  573. for (i = 0; i < ar_sdio->n_rx_pkts; i++)
  574. ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
  575. ar_sdio->n_rx_pkts = 0;
  576. return ret;
  577. }
  578. /* This is the timeout for mailbox processing done in the sdio irq
  579. * handler. The timeout is deliberately set quite high since SDIO dump logs
  580. * over serial port can/will add a substantial overhead to the processing
  581. * (if enabled).
  582. */
  583. #define SDIO_MBOX_PROCESSING_TIMEOUT_HZ (20 * HZ)
  584. static int ath10k_sdio_mbox_rxmsg_pending_handler(struct ath10k *ar,
  585. u32 msg_lookahead, bool *done)
  586. {
  587. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  588. u32 lookaheads[ATH10K_SDIO_MAX_RX_MSGS];
  589. int n_lookaheads = 1;
  590. unsigned long timeout;
  591. int ret;
  592. *done = true;
  593. /* Copy the lookahead obtained from the HTC register table into our
  594. * temp array as a start value.
  595. */
  596. lookaheads[0] = msg_lookahead;
  597. timeout = jiffies + SDIO_MBOX_PROCESSING_TIMEOUT_HZ;
  598. do {
  599. /* Try to allocate as many HTC RX packets indicated by
  600. * n_lookaheads.
  601. */
  602. ret = ath10k_sdio_mbox_rx_alloc(ar, lookaheads,
  603. n_lookaheads);
  604. if (ret)
  605. break;
  606. if (ar_sdio->n_rx_pkts >= 2)
  607. /* A recv bundle was detected, force IRQ status
  608. * re-check again.
  609. */
  610. *done = false;
  611. if (ar_sdio->n_rx_pkts > 1)
  612. ret = ath10k_sdio_mbox_rx_fetch_bundle(ar);
  613. else
  614. ret = ath10k_sdio_mbox_rx_fetch(ar);
  615. /* Process fetched packets. This will potentially update
  616. * n_lookaheads depending on if the packets contain lookahead
  617. * reports.
  618. */
  619. n_lookaheads = 0;
  620. ret = ath10k_sdio_mbox_rx_process_packets(ar,
  621. lookaheads,
  622. &n_lookaheads);
  623. if (!n_lookaheads || ret)
  624. break;
  625. /* For SYNCH processing, if we get here, we are running
  626. * through the loop again due to updated lookaheads. Set
  627. * flag that we should re-check IRQ status registers again
  628. * before leaving IRQ processing, this can net better
  629. * performance in high throughput situations.
  630. */
  631. *done = false;
  632. } while (time_before(jiffies, timeout));
  633. if (ret && (ret != -ECANCELED))
  634. ath10k_warn(ar, "failed to get pending recv messages: %d\n",
  635. ret);
  636. return ret;
  637. }
  638. static int ath10k_sdio_mbox_proc_dbg_intr(struct ath10k *ar)
  639. {
  640. u32 val;
  641. int ret;
  642. /* TODO: Add firmware crash handling */
  643. ath10k_warn(ar, "firmware crashed\n");
  644. /* read counter to clear the interrupt, the debug error interrupt is
  645. * counter 0.
  646. */
  647. ret = ath10k_sdio_read32(ar, MBOX_COUNT_DEC_ADDRESS, &val);
  648. if (ret)
  649. ath10k_warn(ar, "failed to clear debug interrupt: %d\n", ret);
  650. return ret;
  651. }
  652. static int ath10k_sdio_mbox_proc_counter_intr(struct ath10k *ar)
  653. {
  654. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  655. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  656. u8 counter_int_status;
  657. int ret;
  658. mutex_lock(&irq_data->mtx);
  659. counter_int_status = irq_data->irq_proc_reg->counter_int_status &
  660. irq_data->irq_en_reg->cntr_int_status_en;
  661. /* NOTE: other modules like GMBOX may use the counter interrupt for
  662. * credit flow control on other counters, we only need to check for
  663. * the debug assertion counter interrupt.
  664. */
  665. if (counter_int_status & ATH10K_SDIO_TARGET_DEBUG_INTR_MASK)
  666. ret = ath10k_sdio_mbox_proc_dbg_intr(ar);
  667. else
  668. ret = 0;
  669. mutex_unlock(&irq_data->mtx);
  670. return ret;
  671. }
  672. static int ath10k_sdio_mbox_proc_err_intr(struct ath10k *ar)
  673. {
  674. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  675. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  676. u8 error_int_status;
  677. int ret;
  678. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio error interrupt\n");
  679. error_int_status = irq_data->irq_proc_reg->error_int_status & 0x0F;
  680. if (!error_int_status) {
  681. ath10k_warn(ar, "invalid error interrupt status: 0x%x\n",
  682. error_int_status);
  683. return -EIO;
  684. }
  685. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  686. "sdio error_int_status 0x%x\n", error_int_status);
  687. if (FIELD_GET(MBOX_ERROR_INT_STATUS_WAKEUP_MASK,
  688. error_int_status))
  689. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio interrupt error wakeup\n");
  690. if (FIELD_GET(MBOX_ERROR_INT_STATUS_RX_UNDERFLOW_MASK,
  691. error_int_status))
  692. ath10k_warn(ar, "rx underflow interrupt error\n");
  693. if (FIELD_GET(MBOX_ERROR_INT_STATUS_TX_OVERFLOW_MASK,
  694. error_int_status))
  695. ath10k_warn(ar, "tx overflow interrupt error\n");
  696. /* Clear the interrupt */
  697. irq_data->irq_proc_reg->error_int_status &= ~error_int_status;
  698. /* set W1C value to clear the interrupt, this hits the register first */
  699. ret = ath10k_sdio_writesb32(ar, MBOX_ERROR_INT_STATUS_ADDRESS,
  700. error_int_status);
  701. if (ret) {
  702. ath10k_warn(ar, "unable to write to error int status address: %d\n",
  703. ret);
  704. return ret;
  705. }
  706. return 0;
  707. }
  708. static int ath10k_sdio_mbox_proc_cpu_intr(struct ath10k *ar)
  709. {
  710. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  711. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  712. u8 cpu_int_status;
  713. int ret;
  714. mutex_lock(&irq_data->mtx);
  715. cpu_int_status = irq_data->irq_proc_reg->cpu_int_status &
  716. irq_data->irq_en_reg->cpu_int_status_en;
  717. if (!cpu_int_status) {
  718. ath10k_warn(ar, "CPU interrupt status is zero\n");
  719. ret = -EIO;
  720. goto out;
  721. }
  722. /* Clear the interrupt */
  723. irq_data->irq_proc_reg->cpu_int_status &= ~cpu_int_status;
  724. /* Set up the register transfer buffer to hit the register 4 times,
  725. * this is done to make the access 4-byte aligned to mitigate issues
  726. * with host bus interconnects that restrict bus transfer lengths to
  727. * be a multiple of 4-bytes.
  728. *
  729. * Set W1C value to clear the interrupt, this hits the register first.
  730. */
  731. ret = ath10k_sdio_writesb32(ar, MBOX_CPU_INT_STATUS_ADDRESS,
  732. cpu_int_status);
  733. if (ret) {
  734. ath10k_warn(ar, "unable to write to cpu interrupt status address: %d\n",
  735. ret);
  736. goto out;
  737. }
  738. out:
  739. mutex_unlock(&irq_data->mtx);
  740. if (cpu_int_status & MBOX_CPU_STATUS_ENABLE_ASSERT_MASK)
  741. ath10k_sdio_fw_crashed_dump(ar);
  742. return ret;
  743. }
  744. static int ath10k_sdio_mbox_read_int_status(struct ath10k *ar,
  745. u8 *host_int_status,
  746. u32 *lookahead)
  747. {
  748. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  749. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  750. struct ath10k_sdio_irq_proc_regs *irq_proc_reg = irq_data->irq_proc_reg;
  751. struct ath10k_sdio_irq_enable_regs *irq_en_reg = irq_data->irq_en_reg;
  752. u8 htc_mbox = FIELD_PREP(ATH10K_HTC_MAILBOX_MASK, 1);
  753. int ret;
  754. mutex_lock(&irq_data->mtx);
  755. *lookahead = 0;
  756. *host_int_status = 0;
  757. /* int_status_en is supposed to be non zero, otherwise interrupts
  758. * shouldn't be enabled. There is however a short time frame during
  759. * initialization between the irq register and int_status_en init
  760. * where this can happen.
  761. * We silently ignore this condition.
  762. */
  763. if (!irq_en_reg->int_status_en) {
  764. ret = 0;
  765. goto out;
  766. }
  767. /* Read the first sizeof(struct ath10k_irq_proc_registers)
  768. * bytes of the HTC register table. This
  769. * will yield us the value of different int status
  770. * registers and the lookahead registers.
  771. */
  772. ret = ath10k_sdio_read(ar, MBOX_HOST_INT_STATUS_ADDRESS,
  773. irq_proc_reg, sizeof(*irq_proc_reg));
  774. if (ret) {
  775. ath10k_core_start_recovery(ar);
  776. ath10k_warn(ar, "read int status fail, start recovery\n");
  777. goto out;
  778. }
  779. /* Update only those registers that are enabled */
  780. *host_int_status = irq_proc_reg->host_int_status &
  781. irq_en_reg->int_status_en;
  782. /* Look at mbox status */
  783. if (!(*host_int_status & htc_mbox)) {
  784. *lookahead = 0;
  785. ret = 0;
  786. goto out;
  787. }
  788. /* Mask out pending mbox value, we use look ahead as
  789. * the real flag for mbox processing.
  790. */
  791. *host_int_status &= ~htc_mbox;
  792. if (irq_proc_reg->rx_lookahead_valid & htc_mbox) {
  793. *lookahead = le32_to_cpu(
  794. irq_proc_reg->rx_lookahead[ATH10K_HTC_MAILBOX]);
  795. if (!*lookahead)
  796. ath10k_warn(ar, "sdio mbox lookahead is zero\n");
  797. }
  798. out:
  799. mutex_unlock(&irq_data->mtx);
  800. return ret;
  801. }
  802. static int ath10k_sdio_mbox_proc_pending_irqs(struct ath10k *ar,
  803. bool *done)
  804. {
  805. u8 host_int_status;
  806. u32 lookahead;
  807. int ret;
  808. /* NOTE: HIF implementation guarantees that the context of this
  809. * call allows us to perform SYNCHRONOUS I/O, that is we can block,
  810. * sleep or call any API that can block or switch thread/task
  811. * contexts. This is a fully schedulable context.
  812. */
  813. ret = ath10k_sdio_mbox_read_int_status(ar,
  814. &host_int_status,
  815. &lookahead);
  816. if (ret) {
  817. *done = true;
  818. goto out;
  819. }
  820. if (!host_int_status && !lookahead) {
  821. ret = 0;
  822. *done = true;
  823. goto out;
  824. }
  825. if (lookahead) {
  826. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  827. "sdio pending mailbox msg lookahead 0x%08x\n",
  828. lookahead);
  829. ret = ath10k_sdio_mbox_rxmsg_pending_handler(ar,
  830. lookahead,
  831. done);
  832. if (ret)
  833. goto out;
  834. }
  835. /* now, handle the rest of the interrupts */
  836. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  837. "sdio host_int_status 0x%x\n", host_int_status);
  838. if (FIELD_GET(MBOX_HOST_INT_STATUS_CPU_MASK, host_int_status)) {
  839. /* CPU Interrupt */
  840. ret = ath10k_sdio_mbox_proc_cpu_intr(ar);
  841. if (ret)
  842. goto out;
  843. }
  844. if (FIELD_GET(MBOX_HOST_INT_STATUS_ERROR_MASK, host_int_status)) {
  845. /* Error Interrupt */
  846. ret = ath10k_sdio_mbox_proc_err_intr(ar);
  847. if (ret)
  848. goto out;
  849. }
  850. if (FIELD_GET(MBOX_HOST_INT_STATUS_COUNTER_MASK, host_int_status))
  851. /* Counter Interrupt */
  852. ret = ath10k_sdio_mbox_proc_counter_intr(ar);
  853. ret = 0;
  854. out:
  855. /* An optimization to bypass reading the IRQ status registers
  856. * unnecessarily which can re-wake the target, if upper layers
  857. * determine that we are in a low-throughput mode, we can rely on
  858. * taking another interrupt rather than re-checking the status
  859. * registers which can re-wake the target.
  860. *
  861. * NOTE : for host interfaces that makes use of detecting pending
  862. * mbox messages at hif can not use this optimization due to
  863. * possible side effects, SPI requires the host to drain all
  864. * messages from the mailbox before exiting the ISR routine.
  865. */
  866. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  867. "sdio pending irqs done %d status %d",
  868. *done, ret);
  869. return ret;
  870. }
  871. static void ath10k_sdio_set_mbox_info(struct ath10k *ar)
  872. {
  873. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  874. struct ath10k_mbox_info *mbox_info = &ar_sdio->mbox_info;
  875. u16 device = ar_sdio->func->device, dev_id_base, dev_id_chiprev;
  876. mbox_info->htc_addr = ATH10K_HIF_MBOX_BASE_ADDR;
  877. mbox_info->block_size = ATH10K_HIF_MBOX_BLOCK_SIZE;
  878. mbox_info->block_mask = ATH10K_HIF_MBOX_BLOCK_SIZE - 1;
  879. mbox_info->gmbox_addr = ATH10K_HIF_GMBOX_BASE_ADDR;
  880. mbox_info->gmbox_sz = ATH10K_HIF_GMBOX_WIDTH;
  881. mbox_info->ext_info[0].htc_ext_addr = ATH10K_HIF_MBOX0_EXT_BASE_ADDR;
  882. dev_id_base = (device & 0x0F00);
  883. dev_id_chiprev = (device & 0x00FF);
  884. switch (dev_id_base) {
  885. case (SDIO_DEVICE_ID_ATHEROS_AR6005 & 0x0F00):
  886. if (dev_id_chiprev < 4)
  887. mbox_info->ext_info[0].htc_ext_sz =
  888. ATH10K_HIF_MBOX0_EXT_WIDTH;
  889. else
  890. /* from QCA6174 2.0(0x504), the width has been extended
  891. * to 56K
  892. */
  893. mbox_info->ext_info[0].htc_ext_sz =
  894. ATH10K_HIF_MBOX0_EXT_WIDTH_ROME_2_0;
  895. break;
  896. case (SDIO_DEVICE_ID_ATHEROS_QCA9377 & 0x0F00):
  897. mbox_info->ext_info[0].htc_ext_sz =
  898. ATH10K_HIF_MBOX0_EXT_WIDTH_ROME_2_0;
  899. break;
  900. default:
  901. mbox_info->ext_info[0].htc_ext_sz =
  902. ATH10K_HIF_MBOX0_EXT_WIDTH;
  903. }
  904. mbox_info->ext_info[1].htc_ext_addr =
  905. mbox_info->ext_info[0].htc_ext_addr +
  906. mbox_info->ext_info[0].htc_ext_sz +
  907. ATH10K_HIF_MBOX_DUMMY_SPACE_SIZE;
  908. mbox_info->ext_info[1].htc_ext_sz = ATH10K_HIF_MBOX1_EXT_WIDTH;
  909. }
  910. /* BMI functions */
  911. static int ath10k_sdio_bmi_credits(struct ath10k *ar)
  912. {
  913. u32 addr, cmd_credits;
  914. unsigned long timeout;
  915. int ret;
  916. /* Read the counter register to get the command credits */
  917. addr = MBOX_COUNT_DEC_ADDRESS + ATH10K_HIF_MBOX_NUM_MAX * 4;
  918. timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
  919. cmd_credits = 0;
  920. while (time_before(jiffies, timeout) && !cmd_credits) {
  921. /* Hit the credit counter with a 4-byte access, the first byte
  922. * read will hit the counter and cause a decrement, while the
  923. * remaining 3 bytes has no effect. The rationale behind this
  924. * is to make all HIF accesses 4-byte aligned.
  925. */
  926. ret = ath10k_sdio_read32(ar, addr, &cmd_credits);
  927. if (ret) {
  928. ath10k_warn(ar,
  929. "unable to decrement the command credit count register: %d\n",
  930. ret);
  931. return ret;
  932. }
  933. /* The counter is only 8 bits.
  934. * Ignore anything in the upper 3 bytes
  935. */
  936. cmd_credits &= 0xFF;
  937. }
  938. if (!cmd_credits) {
  939. ath10k_warn(ar, "bmi communication timeout\n");
  940. return -ETIMEDOUT;
  941. }
  942. return 0;
  943. }
  944. static int ath10k_sdio_bmi_get_rx_lookahead(struct ath10k *ar)
  945. {
  946. unsigned long timeout;
  947. u32 rx_word;
  948. int ret;
  949. timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
  950. rx_word = 0;
  951. while ((time_before(jiffies, timeout)) && !rx_word) {
  952. ret = ath10k_sdio_read32(ar,
  953. MBOX_HOST_INT_STATUS_ADDRESS,
  954. &rx_word);
  955. if (ret) {
  956. ath10k_warn(ar, "unable to read RX_LOOKAHEAD_VALID: %d\n", ret);
  957. return ret;
  958. }
  959. /* all we really want is one bit */
  960. rx_word &= 1;
  961. }
  962. if (!rx_word) {
  963. ath10k_warn(ar, "bmi_recv_buf FIFO empty\n");
  964. return -EINVAL;
  965. }
  966. return ret;
  967. }
  968. static int ath10k_sdio_bmi_exchange_msg(struct ath10k *ar,
  969. void *req, u32 req_len,
  970. void *resp, u32 *resp_len)
  971. {
  972. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  973. u32 addr;
  974. int ret;
  975. if (req) {
  976. ret = ath10k_sdio_bmi_credits(ar);
  977. if (ret)
  978. return ret;
  979. addr = ar_sdio->mbox_info.htc_addr;
  980. memcpy(ar_sdio->bmi_buf, req, req_len);
  981. ret = ath10k_sdio_write(ar, addr, ar_sdio->bmi_buf, req_len);
  982. if (ret) {
  983. ath10k_warn(ar,
  984. "unable to send the bmi data to the device: %d\n",
  985. ret);
  986. return ret;
  987. }
  988. }
  989. if (!resp || !resp_len)
  990. /* No response expected */
  991. return 0;
  992. /* During normal bootup, small reads may be required.
  993. * Rather than issue an HIF Read and then wait as the Target
  994. * adds successive bytes to the FIFO, we wait here until
  995. * we know that response data is available.
  996. *
  997. * This allows us to cleanly timeout on an unexpected
  998. * Target failure rather than risk problems at the HIF level.
  999. * In particular, this avoids SDIO timeouts and possibly garbage
  1000. * data on some host controllers. And on an interconnect
  1001. * such as Compact Flash (as well as some SDIO masters) which
  1002. * does not provide any indication on data timeout, it avoids
  1003. * a potential hang or garbage response.
  1004. *
  1005. * Synchronization is more difficult for reads larger than the
  1006. * size of the MBOX FIFO (128B), because the Target is unable
  1007. * to push the 129th byte of data until AFTER the Host posts an
  1008. * HIF Read and removes some FIFO data. So for large reads the
  1009. * Host proceeds to post an HIF Read BEFORE all the data is
  1010. * actually available to read. Fortunately, large BMI reads do
  1011. * not occur in practice -- they're supported for debug/development.
  1012. *
  1013. * So Host/Target BMI synchronization is divided into these cases:
  1014. * CASE 1: length < 4
  1015. * Should not happen
  1016. *
  1017. * CASE 2: 4 <= length <= 128
  1018. * Wait for first 4 bytes to be in FIFO
  1019. * If CONSERVATIVE_BMI_READ is enabled, also wait for
  1020. * a BMI command credit, which indicates that the ENTIRE
  1021. * response is available in the FIFO
  1022. *
  1023. * CASE 3: length > 128
  1024. * Wait for the first 4 bytes to be in FIFO
  1025. *
  1026. * For most uses, a small timeout should be sufficient and we will
  1027. * usually see a response quickly; but there may be some unusual
  1028. * (debug) cases of BMI_EXECUTE where we want an larger timeout.
  1029. * For now, we use an unbounded busy loop while waiting for
  1030. * BMI_EXECUTE.
  1031. *
  1032. * If BMI_EXECUTE ever needs to support longer-latency execution,
  1033. * especially in production, this code needs to be enhanced to sleep
  1034. * and yield. Also note that BMI_COMMUNICATION_TIMEOUT is currently
  1035. * a function of Host processor speed.
  1036. */
  1037. ret = ath10k_sdio_bmi_get_rx_lookahead(ar);
  1038. if (ret)
  1039. return ret;
  1040. /* We always read from the start of the mbox address */
  1041. addr = ar_sdio->mbox_info.htc_addr;
  1042. ret = ath10k_sdio_read(ar, addr, ar_sdio->bmi_buf, *resp_len);
  1043. if (ret) {
  1044. ath10k_warn(ar,
  1045. "unable to read the bmi data from the device: %d\n",
  1046. ret);
  1047. return ret;
  1048. }
  1049. memcpy(resp, ar_sdio->bmi_buf, *resp_len);
  1050. return 0;
  1051. }
  1052. /* sdio async handling functions */
  1053. static struct ath10k_sdio_bus_request
  1054. *ath10k_sdio_alloc_busreq(struct ath10k *ar)
  1055. {
  1056. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1057. struct ath10k_sdio_bus_request *bus_req;
  1058. spin_lock_bh(&ar_sdio->lock);
  1059. if (list_empty(&ar_sdio->bus_req_freeq)) {
  1060. bus_req = NULL;
  1061. goto out;
  1062. }
  1063. bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
  1064. struct ath10k_sdio_bus_request, list);
  1065. list_del(&bus_req->list);
  1066. out:
  1067. spin_unlock_bh(&ar_sdio->lock);
  1068. return bus_req;
  1069. }
  1070. static void ath10k_sdio_free_bus_req(struct ath10k *ar,
  1071. struct ath10k_sdio_bus_request *bus_req)
  1072. {
  1073. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1074. memset(bus_req, 0, sizeof(*bus_req));
  1075. spin_lock_bh(&ar_sdio->lock);
  1076. list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
  1077. spin_unlock_bh(&ar_sdio->lock);
  1078. }
  1079. static void __ath10k_sdio_write_async(struct ath10k *ar,
  1080. struct ath10k_sdio_bus_request *req)
  1081. {
  1082. struct ath10k_htc_ep *ep;
  1083. struct sk_buff *skb;
  1084. int ret;
  1085. skb = req->skb;
  1086. ret = ath10k_sdio_write(ar, req->address, skb->data, skb->len);
  1087. if (ret)
  1088. ath10k_warn(ar, "failed to write skb to 0x%x asynchronously: %d",
  1089. req->address, ret);
  1090. if (req->htc_msg) {
  1091. ep = &ar->htc.endpoint[req->eid];
  1092. ath10k_htc_notify_tx_completion(ep, skb);
  1093. } else if (req->comp) {
  1094. complete(req->comp);
  1095. }
  1096. ath10k_sdio_free_bus_req(ar, req);
  1097. }
  1098. /* To improve throughput use workqueue to deliver packets to HTC layer,
  1099. * this way SDIO bus is utilised much better.
  1100. */
  1101. static void ath10k_rx_indication_async_work(struct work_struct *work)
  1102. {
  1103. struct ath10k_sdio *ar_sdio = container_of(work, struct ath10k_sdio,
  1104. async_work_rx);
  1105. struct ath10k *ar = ar_sdio->ar;
  1106. struct ath10k_htc_ep *ep;
  1107. struct ath10k_skb_rxcb *cb;
  1108. struct sk_buff *skb;
  1109. while (true) {
  1110. skb = skb_dequeue(&ar_sdio->rx_head);
  1111. if (!skb)
  1112. break;
  1113. cb = ATH10K_SKB_RXCB(skb);
  1114. ep = &ar->htc.endpoint[cb->eid];
  1115. ep->ep_ops.ep_rx_complete(ar, skb);
  1116. }
  1117. if (test_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags)) {
  1118. local_bh_disable();
  1119. napi_schedule(&ar->napi);
  1120. local_bh_enable();
  1121. }
  1122. }
  1123. static int ath10k_sdio_read_rtc_state(struct ath10k_sdio *ar_sdio, unsigned char *state)
  1124. {
  1125. struct ath10k *ar = ar_sdio->ar;
  1126. unsigned char rtc_state = 0;
  1127. int ret = 0;
  1128. rtc_state = sdio_f0_readb(ar_sdio->func, ATH10K_CIS_RTC_STATE_ADDR, &ret);
  1129. if (ret) {
  1130. ath10k_warn(ar, "failed to read rtc state: %d\n", ret);
  1131. return ret;
  1132. }
  1133. *state = rtc_state & 0x3;
  1134. return ret;
  1135. }
  1136. static int ath10k_sdio_set_mbox_sleep(struct ath10k *ar, bool enable_sleep)
  1137. {
  1138. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1139. u32 val;
  1140. int retry = ATH10K_CIS_READ_RETRY, ret = 0;
  1141. unsigned char rtc_state = 0;
  1142. sdio_claim_host(ar_sdio->func);
  1143. ret = ath10k_sdio_read32(ar, ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL, &val);
  1144. if (ret) {
  1145. ath10k_warn(ar, "failed to read fifo/chip control register: %d\n",
  1146. ret);
  1147. goto release;
  1148. }
  1149. if (enable_sleep) {
  1150. val &= ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL_DISABLE_SLEEP_OFF;
  1151. ar_sdio->mbox_state = SDIO_MBOX_SLEEP_STATE;
  1152. } else {
  1153. val |= ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL_DISABLE_SLEEP_ON;
  1154. ar_sdio->mbox_state = SDIO_MBOX_AWAKE_STATE;
  1155. }
  1156. ret = ath10k_sdio_write32(ar, ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL, val);
  1157. if (ret) {
  1158. ath10k_warn(ar, "failed to write to FIFO_TIMEOUT_AND_CHIP_CONTROL: %d",
  1159. ret);
  1160. }
  1161. if (!enable_sleep) {
  1162. do {
  1163. udelay(ATH10K_CIS_READ_WAIT_4_RTC_CYCLE_IN_US);
  1164. ret = ath10k_sdio_read_rtc_state(ar_sdio, &rtc_state);
  1165. if (ret) {
  1166. ath10k_warn(ar, "failed to disable mbox sleep: %d", ret);
  1167. break;
  1168. }
  1169. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read rtc state: %d\n",
  1170. rtc_state);
  1171. if (rtc_state == ATH10K_CIS_RTC_STATE_ON)
  1172. break;
  1173. udelay(ATH10K_CIS_XTAL_SETTLE_DURATION_IN_US);
  1174. retry--;
  1175. } while (retry > 0);
  1176. }
  1177. release:
  1178. sdio_release_host(ar_sdio->func);
  1179. return ret;
  1180. }
  1181. static void ath10k_sdio_sleep_timer_handler(struct timer_list *t)
  1182. {
  1183. struct ath10k_sdio *ar_sdio = from_timer(ar_sdio, t, sleep_timer);
  1184. ar_sdio->mbox_state = SDIO_MBOX_REQUEST_TO_SLEEP_STATE;
  1185. queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
  1186. }
  1187. static void ath10k_sdio_write_async_work(struct work_struct *work)
  1188. {
  1189. struct ath10k_sdio *ar_sdio = container_of(work, struct ath10k_sdio,
  1190. wr_async_work);
  1191. struct ath10k *ar = ar_sdio->ar;
  1192. struct ath10k_sdio_bus_request *req, *tmp_req;
  1193. struct ath10k_mbox_info *mbox_info = &ar_sdio->mbox_info;
  1194. spin_lock_bh(&ar_sdio->wr_async_lock);
  1195. list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
  1196. list_del(&req->list);
  1197. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1198. if (req->address >= mbox_info->htc_addr &&
  1199. ar_sdio->mbox_state == SDIO_MBOX_SLEEP_STATE) {
  1200. ath10k_sdio_set_mbox_sleep(ar, false);
  1201. mod_timer(&ar_sdio->sleep_timer, jiffies +
  1202. msecs_to_jiffies(ATH10K_MIN_SLEEP_INACTIVITY_TIME_MS));
  1203. }
  1204. __ath10k_sdio_write_async(ar, req);
  1205. spin_lock_bh(&ar_sdio->wr_async_lock);
  1206. }
  1207. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1208. if (ar_sdio->mbox_state == SDIO_MBOX_REQUEST_TO_SLEEP_STATE)
  1209. ath10k_sdio_set_mbox_sleep(ar, true);
  1210. }
  1211. static int ath10k_sdio_prep_async_req(struct ath10k *ar, u32 addr,
  1212. struct sk_buff *skb,
  1213. struct completion *comp,
  1214. bool htc_msg, enum ath10k_htc_ep_id eid)
  1215. {
  1216. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1217. struct ath10k_sdio_bus_request *bus_req;
  1218. /* Allocate a bus request for the message and queue it on the
  1219. * SDIO workqueue.
  1220. */
  1221. bus_req = ath10k_sdio_alloc_busreq(ar);
  1222. if (!bus_req) {
  1223. ath10k_warn(ar,
  1224. "unable to allocate bus request for async request\n");
  1225. return -ENOMEM;
  1226. }
  1227. bus_req->skb = skb;
  1228. bus_req->eid = eid;
  1229. bus_req->address = addr;
  1230. bus_req->htc_msg = htc_msg;
  1231. bus_req->comp = comp;
  1232. spin_lock_bh(&ar_sdio->wr_async_lock);
  1233. list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
  1234. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1235. return 0;
  1236. }
  1237. /* IRQ handler */
  1238. static void ath10k_sdio_irq_handler(struct sdio_func *func)
  1239. {
  1240. struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
  1241. struct ath10k *ar = ar_sdio->ar;
  1242. unsigned long timeout;
  1243. bool done = false;
  1244. int ret;
  1245. /* Release the host during interrupts so we can pick it back up when
  1246. * we process commands.
  1247. */
  1248. sdio_release_host(ar_sdio->func);
  1249. timeout = jiffies + ATH10K_SDIO_HIF_COMMUNICATION_TIMEOUT_HZ;
  1250. do {
  1251. ret = ath10k_sdio_mbox_proc_pending_irqs(ar, &done);
  1252. if (ret)
  1253. break;
  1254. } while (time_before(jiffies, timeout) && !done);
  1255. ath10k_mac_tx_push_pending(ar);
  1256. sdio_claim_host(ar_sdio->func);
  1257. if (ret && ret != -ECANCELED)
  1258. ath10k_warn(ar, "failed to process pending SDIO interrupts: %d\n",
  1259. ret);
  1260. }
  1261. /* sdio HIF functions */
  1262. static int ath10k_sdio_disable_intrs(struct ath10k *ar)
  1263. {
  1264. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1265. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  1266. struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
  1267. int ret;
  1268. mutex_lock(&irq_data->mtx);
  1269. memset(regs, 0, sizeof(*regs));
  1270. ret = ath10k_sdio_write(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
  1271. &regs->int_status_en, sizeof(*regs));
  1272. if (ret)
  1273. ath10k_warn(ar, "unable to disable sdio interrupts: %d\n", ret);
  1274. mutex_unlock(&irq_data->mtx);
  1275. return ret;
  1276. }
  1277. static int ath10k_sdio_hif_power_up(struct ath10k *ar,
  1278. enum ath10k_firmware_mode fw_mode)
  1279. {
  1280. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1281. struct sdio_func *func = ar_sdio->func;
  1282. int ret;
  1283. if (!ar_sdio->is_disabled)
  1284. return 0;
  1285. ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio power on\n");
  1286. ret = ath10k_sdio_config(ar);
  1287. if (ret) {
  1288. ath10k_err(ar, "failed to config sdio: %d\n", ret);
  1289. return ret;
  1290. }
  1291. sdio_claim_host(func);
  1292. ret = sdio_enable_func(func);
  1293. if (ret) {
  1294. ath10k_warn(ar, "unable to enable sdio function: %d)\n", ret);
  1295. sdio_release_host(func);
  1296. return ret;
  1297. }
  1298. sdio_release_host(func);
  1299. /* Wait for hardware to initialise. It should take a lot less than
  1300. * 20 ms but let's be conservative here.
  1301. */
  1302. msleep(20);
  1303. ar_sdio->is_disabled = false;
  1304. ret = ath10k_sdio_disable_intrs(ar);
  1305. if (ret)
  1306. return ret;
  1307. return 0;
  1308. }
  1309. static void ath10k_sdio_hif_power_down(struct ath10k *ar)
  1310. {
  1311. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1312. int ret;
  1313. if (ar_sdio->is_disabled)
  1314. return;
  1315. ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio power off\n");
  1316. del_timer_sync(&ar_sdio->sleep_timer);
  1317. ath10k_sdio_set_mbox_sleep(ar, true);
  1318. /* Disable the card */
  1319. sdio_claim_host(ar_sdio->func);
  1320. ret = sdio_disable_func(ar_sdio->func);
  1321. if (ret) {
  1322. ath10k_warn(ar, "unable to disable sdio function: %d\n", ret);
  1323. sdio_release_host(ar_sdio->func);
  1324. return;
  1325. }
  1326. ret = mmc_hw_reset(ar_sdio->func->card);
  1327. if (ret)
  1328. ath10k_warn(ar, "unable to reset sdio: %d\n", ret);
  1329. sdio_release_host(ar_sdio->func);
  1330. ar_sdio->is_disabled = true;
  1331. }
  1332. static int ath10k_sdio_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
  1333. struct ath10k_hif_sg_item *items, int n_items)
  1334. {
  1335. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1336. enum ath10k_htc_ep_id eid;
  1337. struct sk_buff *skb;
  1338. int ret, i;
  1339. eid = pipe_id_to_eid(pipe_id);
  1340. for (i = 0; i < n_items; i++) {
  1341. size_t padded_len;
  1342. u32 address;
  1343. skb = items[i].transfer_context;
  1344. padded_len = ath10k_sdio_calc_txrx_padded_len(ar_sdio,
  1345. skb->len);
  1346. skb_trim(skb, padded_len);
  1347. /* Write TX data to the end of the mbox address space */
  1348. address = ar_sdio->mbox_addr[eid] + ar_sdio->mbox_size[eid] -
  1349. skb->len;
  1350. ret = ath10k_sdio_prep_async_req(ar, address, skb,
  1351. NULL, true, eid);
  1352. if (ret)
  1353. return ret;
  1354. }
  1355. queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
  1356. return 0;
  1357. }
  1358. static int ath10k_sdio_enable_intrs(struct ath10k *ar)
  1359. {
  1360. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1361. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  1362. struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
  1363. int ret;
  1364. mutex_lock(&irq_data->mtx);
  1365. /* Enable all but CPU interrupts */
  1366. regs->int_status_en = FIELD_PREP(MBOX_INT_STATUS_ENABLE_ERROR_MASK, 1) |
  1367. FIELD_PREP(MBOX_INT_STATUS_ENABLE_CPU_MASK, 1) |
  1368. FIELD_PREP(MBOX_INT_STATUS_ENABLE_COUNTER_MASK, 1);
  1369. /* NOTE: There are some cases where HIF can do detection of
  1370. * pending mbox messages which is disabled now.
  1371. */
  1372. regs->int_status_en |=
  1373. FIELD_PREP(MBOX_INT_STATUS_ENABLE_MBOX_DATA_MASK, 1);
  1374. /* Set up the CPU Interrupt Status Register, enable CPU sourced interrupt #0
  1375. * #0 is used for report assertion from target
  1376. */
  1377. regs->cpu_int_status_en = FIELD_PREP(MBOX_CPU_STATUS_ENABLE_ASSERT_MASK, 1);
  1378. /* Set up the Error Interrupt status Register */
  1379. regs->err_int_status_en =
  1380. FIELD_PREP(MBOX_ERROR_STATUS_ENABLE_RX_UNDERFLOW_MASK, 1) |
  1381. FIELD_PREP(MBOX_ERROR_STATUS_ENABLE_TX_OVERFLOW_MASK, 1);
  1382. /* Enable Counter interrupt status register to get fatal errors for
  1383. * debugging.
  1384. */
  1385. regs->cntr_int_status_en =
  1386. FIELD_PREP(MBOX_COUNTER_INT_STATUS_ENABLE_BIT_MASK,
  1387. ATH10K_SDIO_TARGET_DEBUG_INTR_MASK);
  1388. ret = ath10k_sdio_write(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
  1389. &regs->int_status_en, sizeof(*regs));
  1390. if (ret)
  1391. ath10k_warn(ar,
  1392. "failed to update mbox interrupt status register : %d\n",
  1393. ret);
  1394. mutex_unlock(&irq_data->mtx);
  1395. return ret;
  1396. }
  1397. /* HIF diagnostics */
  1398. static int ath10k_sdio_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
  1399. size_t buf_len)
  1400. {
  1401. int ret;
  1402. void *mem;
  1403. mem = kzalloc(buf_len, GFP_KERNEL);
  1404. if (!mem)
  1405. return -ENOMEM;
  1406. /* set window register to start read cycle */
  1407. ret = ath10k_sdio_write32(ar, MBOX_WINDOW_READ_ADDR_ADDRESS, address);
  1408. if (ret) {
  1409. ath10k_warn(ar, "failed to set mbox window read address: %d", ret);
  1410. goto out;
  1411. }
  1412. /* read the data */
  1413. ret = ath10k_sdio_read(ar, MBOX_WINDOW_DATA_ADDRESS, mem, buf_len);
  1414. if (ret) {
  1415. ath10k_warn(ar, "failed to read from mbox window data address: %d\n",
  1416. ret);
  1417. goto out;
  1418. }
  1419. memcpy(buf, mem, buf_len);
  1420. out:
  1421. kfree(mem);
  1422. return ret;
  1423. }
  1424. static int ath10k_sdio_diag_read32(struct ath10k *ar, u32 address,
  1425. u32 *value)
  1426. {
  1427. __le32 *val;
  1428. int ret;
  1429. val = kzalloc(sizeof(*val), GFP_KERNEL);
  1430. if (!val)
  1431. return -ENOMEM;
  1432. ret = ath10k_sdio_hif_diag_read(ar, address, val, sizeof(*val));
  1433. if (ret)
  1434. goto out;
  1435. *value = __le32_to_cpu(*val);
  1436. out:
  1437. kfree(val);
  1438. return ret;
  1439. }
  1440. static int ath10k_sdio_hif_diag_write_mem(struct ath10k *ar, u32 address,
  1441. const void *data, int nbytes)
  1442. {
  1443. int ret;
  1444. /* set write data */
  1445. ret = ath10k_sdio_write(ar, MBOX_WINDOW_DATA_ADDRESS, data, nbytes);
  1446. if (ret) {
  1447. ath10k_warn(ar,
  1448. "failed to write 0x%p to mbox window data address: %d\n",
  1449. data, ret);
  1450. return ret;
  1451. }
  1452. /* set window register, which starts the write cycle */
  1453. ret = ath10k_sdio_write32(ar, MBOX_WINDOW_WRITE_ADDR_ADDRESS, address);
  1454. if (ret) {
  1455. ath10k_warn(ar, "failed to set mbox window write address: %d", ret);
  1456. return ret;
  1457. }
  1458. return 0;
  1459. }
  1460. static int ath10k_sdio_hif_start_post(struct ath10k *ar)
  1461. {
  1462. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1463. u32 addr, val;
  1464. int ret = 0;
  1465. addr = host_interest_item_address(HI_ITEM(hi_acs_flags));
  1466. ret = ath10k_sdio_diag_read32(ar, addr, &val);
  1467. if (ret) {
  1468. ath10k_warn(ar, "unable to read hi_acs_flags : %d\n", ret);
  1469. return ret;
  1470. }
  1471. if (val & HI_ACS_FLAGS_SDIO_SWAP_MAILBOX_FW_ACK) {
  1472. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1473. "sdio mailbox swap service enabled\n");
  1474. ar_sdio->swap_mbox = true;
  1475. } else {
  1476. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1477. "sdio mailbox swap service disabled\n");
  1478. ar_sdio->swap_mbox = false;
  1479. }
  1480. ath10k_sdio_set_mbox_sleep(ar, true);
  1481. return 0;
  1482. }
  1483. static int ath10k_sdio_get_htt_tx_complete(struct ath10k *ar)
  1484. {
  1485. u32 addr, val;
  1486. int ret;
  1487. addr = host_interest_item_address(HI_ITEM(hi_acs_flags));
  1488. ret = ath10k_sdio_diag_read32(ar, addr, &val);
  1489. if (ret) {
  1490. ath10k_warn(ar,
  1491. "unable to read hi_acs_flags for htt tx comple : %d\n", ret);
  1492. return ret;
  1493. }
  1494. ret = (val & HI_ACS_FLAGS_SDIO_REDUCE_TX_COMPL_FW_ACK);
  1495. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio reduce tx complete fw%sack\n",
  1496. ret ? " " : " not ");
  1497. return ret;
  1498. }
  1499. /* HIF start/stop */
  1500. static int ath10k_sdio_hif_start(struct ath10k *ar)
  1501. {
  1502. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1503. int ret;
  1504. ath10k_core_napi_enable(ar);
  1505. /* Sleep 20 ms before HIF interrupts are disabled.
  1506. * This will give target plenty of time to process the BMI done
  1507. * request before interrupts are disabled.
  1508. */
  1509. msleep(20);
  1510. ret = ath10k_sdio_disable_intrs(ar);
  1511. if (ret)
  1512. return ret;
  1513. /* eid 0 always uses the lower part of the extended mailbox address
  1514. * space (ext_info[0].htc_ext_addr).
  1515. */
  1516. ar_sdio->mbox_addr[0] = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
  1517. ar_sdio->mbox_size[0] = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
  1518. sdio_claim_host(ar_sdio->func);
  1519. /* Register the isr */
  1520. ret = sdio_claim_irq(ar_sdio->func, ath10k_sdio_irq_handler);
  1521. if (ret) {
  1522. ath10k_warn(ar, "failed to claim sdio interrupt: %d\n", ret);
  1523. sdio_release_host(ar_sdio->func);
  1524. return ret;
  1525. }
  1526. sdio_release_host(ar_sdio->func);
  1527. ret = ath10k_sdio_enable_intrs(ar);
  1528. if (ret)
  1529. ath10k_warn(ar, "failed to enable sdio interrupts: %d\n", ret);
  1530. /* Enable sleep and then disable it again */
  1531. ret = ath10k_sdio_set_mbox_sleep(ar, true);
  1532. if (ret)
  1533. return ret;
  1534. /* Wait for 20ms for the written value to take effect */
  1535. msleep(20);
  1536. ret = ath10k_sdio_set_mbox_sleep(ar, false);
  1537. if (ret)
  1538. return ret;
  1539. return 0;
  1540. }
  1541. #define SDIO_IRQ_DISABLE_TIMEOUT_HZ (3 * HZ)
  1542. static void ath10k_sdio_irq_disable(struct ath10k *ar)
  1543. {
  1544. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1545. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  1546. struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
  1547. struct sk_buff *skb;
  1548. struct completion irqs_disabled_comp;
  1549. int ret;
  1550. skb = dev_alloc_skb(sizeof(*regs));
  1551. if (!skb)
  1552. return;
  1553. mutex_lock(&irq_data->mtx);
  1554. memset(regs, 0, sizeof(*regs)); /* disable all interrupts */
  1555. memcpy(skb->data, regs, sizeof(*regs));
  1556. skb_put(skb, sizeof(*regs));
  1557. mutex_unlock(&irq_data->mtx);
  1558. init_completion(&irqs_disabled_comp);
  1559. ret = ath10k_sdio_prep_async_req(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
  1560. skb, &irqs_disabled_comp, false, 0);
  1561. if (ret)
  1562. goto out;
  1563. queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
  1564. /* Wait for the completion of the IRQ disable request.
  1565. * If there is a timeout we will try to disable irq's anyway.
  1566. */
  1567. ret = wait_for_completion_timeout(&irqs_disabled_comp,
  1568. SDIO_IRQ_DISABLE_TIMEOUT_HZ);
  1569. if (!ret)
  1570. ath10k_warn(ar, "sdio irq disable request timed out\n");
  1571. sdio_claim_host(ar_sdio->func);
  1572. ret = sdio_release_irq(ar_sdio->func);
  1573. if (ret)
  1574. ath10k_warn(ar, "failed to release sdio interrupt: %d\n", ret);
  1575. sdio_release_host(ar_sdio->func);
  1576. out:
  1577. kfree_skb(skb);
  1578. }
  1579. static void ath10k_sdio_hif_stop(struct ath10k *ar)
  1580. {
  1581. struct ath10k_sdio_bus_request *req, *tmp_req;
  1582. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1583. struct sk_buff *skb;
  1584. ath10k_sdio_irq_disable(ar);
  1585. cancel_work_sync(&ar_sdio->async_work_rx);
  1586. while ((skb = skb_dequeue(&ar_sdio->rx_head)))
  1587. dev_kfree_skb_any(skb);
  1588. cancel_work_sync(&ar_sdio->wr_async_work);
  1589. spin_lock_bh(&ar_sdio->wr_async_lock);
  1590. /* Free all bus requests that have not been handled */
  1591. list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
  1592. struct ath10k_htc_ep *ep;
  1593. list_del(&req->list);
  1594. if (req->htc_msg) {
  1595. ep = &ar->htc.endpoint[req->eid];
  1596. ath10k_htc_notify_tx_completion(ep, req->skb);
  1597. } else if (req->skb) {
  1598. kfree_skb(req->skb);
  1599. }
  1600. ath10k_sdio_free_bus_req(ar, req);
  1601. }
  1602. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1603. ath10k_core_napi_sync_disable(ar);
  1604. }
  1605. #ifdef CONFIG_PM
  1606. static int ath10k_sdio_hif_suspend(struct ath10k *ar)
  1607. {
  1608. return 0;
  1609. }
  1610. static int ath10k_sdio_hif_resume(struct ath10k *ar)
  1611. {
  1612. switch (ar->state) {
  1613. case ATH10K_STATE_OFF:
  1614. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1615. "sdio resume configuring sdio\n");
  1616. /* need to set sdio settings after power is cut from sdio */
  1617. ath10k_sdio_config(ar);
  1618. break;
  1619. case ATH10K_STATE_ON:
  1620. default:
  1621. break;
  1622. }
  1623. return 0;
  1624. }
  1625. #endif
  1626. static int ath10k_sdio_hif_map_service_to_pipe(struct ath10k *ar,
  1627. u16 service_id,
  1628. u8 *ul_pipe, u8 *dl_pipe)
  1629. {
  1630. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1631. struct ath10k_htc *htc = &ar->htc;
  1632. u32 htt_addr, wmi_addr, htt_mbox_size, wmi_mbox_size;
  1633. enum ath10k_htc_ep_id eid;
  1634. bool ep_found = false;
  1635. int i;
  1636. /* For sdio, we are interested in the mapping between eid
  1637. * and pipeid rather than service_id to pipe_id.
  1638. * First we find out which eid has been allocated to the
  1639. * service...
  1640. */
  1641. for (i = 0; i < ATH10K_HTC_EP_COUNT; i++) {
  1642. if (htc->endpoint[i].service_id == service_id) {
  1643. eid = htc->endpoint[i].eid;
  1644. ep_found = true;
  1645. break;
  1646. }
  1647. }
  1648. if (!ep_found)
  1649. return -EINVAL;
  1650. /* Then we create the simplest mapping possible between pipeid
  1651. * and eid
  1652. */
  1653. *ul_pipe = *dl_pipe = (u8)eid;
  1654. /* Normally, HTT will use the upper part of the extended
  1655. * mailbox address space (ext_info[1].htc_ext_addr) and WMI ctrl
  1656. * the lower part (ext_info[0].htc_ext_addr).
  1657. * If fw wants swapping of mailbox addresses, the opposite is true.
  1658. */
  1659. if (ar_sdio->swap_mbox) {
  1660. htt_addr = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
  1661. wmi_addr = ar_sdio->mbox_info.ext_info[1].htc_ext_addr;
  1662. htt_mbox_size = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
  1663. wmi_mbox_size = ar_sdio->mbox_info.ext_info[1].htc_ext_sz;
  1664. } else {
  1665. htt_addr = ar_sdio->mbox_info.ext_info[1].htc_ext_addr;
  1666. wmi_addr = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
  1667. htt_mbox_size = ar_sdio->mbox_info.ext_info[1].htc_ext_sz;
  1668. wmi_mbox_size = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
  1669. }
  1670. switch (service_id) {
  1671. case ATH10K_HTC_SVC_ID_RSVD_CTRL:
  1672. /* HTC ctrl ep mbox address has already been setup in
  1673. * ath10k_sdio_hif_start
  1674. */
  1675. break;
  1676. case ATH10K_HTC_SVC_ID_WMI_CONTROL:
  1677. ar_sdio->mbox_addr[eid] = wmi_addr;
  1678. ar_sdio->mbox_size[eid] = wmi_mbox_size;
  1679. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1680. "sdio wmi ctrl mbox_addr 0x%x mbox_size %d\n",
  1681. ar_sdio->mbox_addr[eid], ar_sdio->mbox_size[eid]);
  1682. break;
  1683. case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
  1684. ar_sdio->mbox_addr[eid] = htt_addr;
  1685. ar_sdio->mbox_size[eid] = htt_mbox_size;
  1686. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1687. "sdio htt data mbox_addr 0x%x mbox_size %d\n",
  1688. ar_sdio->mbox_addr[eid], ar_sdio->mbox_size[eid]);
  1689. break;
  1690. default:
  1691. ath10k_warn(ar, "unsupported HTC service id: %d\n",
  1692. service_id);
  1693. return -EINVAL;
  1694. }
  1695. return 0;
  1696. }
  1697. static void ath10k_sdio_hif_get_default_pipe(struct ath10k *ar,
  1698. u8 *ul_pipe, u8 *dl_pipe)
  1699. {
  1700. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio hif get default pipe\n");
  1701. /* HTC ctrl ep (SVC id 1) always has eid (and pipe_id in our
  1702. * case) == 0
  1703. */
  1704. *ul_pipe = 0;
  1705. *dl_pipe = 0;
  1706. }
  1707. static const struct ath10k_hif_ops ath10k_sdio_hif_ops = {
  1708. .tx_sg = ath10k_sdio_hif_tx_sg,
  1709. .diag_read = ath10k_sdio_hif_diag_read,
  1710. .diag_write = ath10k_sdio_hif_diag_write_mem,
  1711. .exchange_bmi_msg = ath10k_sdio_bmi_exchange_msg,
  1712. .start = ath10k_sdio_hif_start,
  1713. .stop = ath10k_sdio_hif_stop,
  1714. .start_post = ath10k_sdio_hif_start_post,
  1715. .get_htt_tx_complete = ath10k_sdio_get_htt_tx_complete,
  1716. .map_service_to_pipe = ath10k_sdio_hif_map_service_to_pipe,
  1717. .get_default_pipe = ath10k_sdio_hif_get_default_pipe,
  1718. .power_up = ath10k_sdio_hif_power_up,
  1719. .power_down = ath10k_sdio_hif_power_down,
  1720. #ifdef CONFIG_PM
  1721. .suspend = ath10k_sdio_hif_suspend,
  1722. .resume = ath10k_sdio_hif_resume,
  1723. #endif
  1724. };
  1725. #ifdef CONFIG_PM_SLEEP
  1726. /* Empty handlers so that mmc subsystem doesn't remove us entirely during
  1727. * suspend. We instead follow cfg80211 suspend/resume handlers.
  1728. */
  1729. static int ath10k_sdio_pm_suspend(struct device *device)
  1730. {
  1731. struct sdio_func *func = dev_to_sdio_func(device);
  1732. struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
  1733. struct ath10k *ar = ar_sdio->ar;
  1734. mmc_pm_flag_t pm_flag, pm_caps;
  1735. int ret;
  1736. if (!device_may_wakeup(ar->dev))
  1737. return 0;
  1738. ath10k_sdio_set_mbox_sleep(ar, true);
  1739. pm_flag = MMC_PM_KEEP_POWER;
  1740. ret = sdio_set_host_pm_flags(func, pm_flag);
  1741. if (ret) {
  1742. pm_caps = sdio_get_host_pm_caps(func);
  1743. ath10k_warn(ar, "failed to set sdio host pm flags (0x%x, 0x%x): %d\n",
  1744. pm_flag, pm_caps, ret);
  1745. return ret;
  1746. }
  1747. return ret;
  1748. }
  1749. static int ath10k_sdio_pm_resume(struct device *device)
  1750. {
  1751. return 0;
  1752. }
  1753. static SIMPLE_DEV_PM_OPS(ath10k_sdio_pm_ops, ath10k_sdio_pm_suspend,
  1754. ath10k_sdio_pm_resume);
  1755. #define ATH10K_SDIO_PM_OPS (&ath10k_sdio_pm_ops)
  1756. #else
  1757. #define ATH10K_SDIO_PM_OPS NULL
  1758. #endif /* CONFIG_PM_SLEEP */
  1759. static int ath10k_sdio_napi_poll(struct napi_struct *ctx, int budget)
  1760. {
  1761. struct ath10k *ar = container_of(ctx, struct ath10k, napi);
  1762. int done;
  1763. done = ath10k_htt_rx_hl_indication(ar, budget);
  1764. ath10k_dbg(ar, ATH10K_DBG_SDIO, "napi poll: done: %d, budget:%d\n", done, budget);
  1765. if (done < budget)
  1766. napi_complete_done(ctx, done);
  1767. return done;
  1768. }
  1769. static int ath10k_sdio_read_host_interest_value(struct ath10k *ar,
  1770. u32 item_offset,
  1771. u32 *val)
  1772. {
  1773. u32 addr;
  1774. int ret;
  1775. addr = host_interest_item_address(item_offset);
  1776. ret = ath10k_sdio_diag_read32(ar, addr, val);
  1777. if (ret)
  1778. ath10k_warn(ar, "unable to read host interest offset %d value\n",
  1779. item_offset);
  1780. return ret;
  1781. }
  1782. static int ath10k_sdio_read_mem(struct ath10k *ar, u32 address, void *buf,
  1783. u32 buf_len)
  1784. {
  1785. u32 val;
  1786. int i, ret;
  1787. for (i = 0; i < buf_len; i += 4) {
  1788. ret = ath10k_sdio_diag_read32(ar, address + i, &val);
  1789. if (ret) {
  1790. ath10k_warn(ar, "unable to read mem %d value\n", address + i);
  1791. break;
  1792. }
  1793. memcpy(buf + i, &val, 4);
  1794. }
  1795. return ret;
  1796. }
  1797. static bool ath10k_sdio_is_fast_dump_supported(struct ath10k *ar)
  1798. {
  1799. u32 param;
  1800. ath10k_sdio_read_host_interest_value(ar, HI_ITEM(hi_option_flag2), &param);
  1801. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio hi_option_flag2 %x\n", param);
  1802. return !!(param & HI_OPTION_SDIO_CRASH_DUMP_ENHANCEMENT_FW);
  1803. }
  1804. static void ath10k_sdio_dump_registers(struct ath10k *ar,
  1805. struct ath10k_fw_crash_data *crash_data,
  1806. bool fast_dump)
  1807. {
  1808. u32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
  1809. int i, ret;
  1810. u32 reg_dump_area;
  1811. ret = ath10k_sdio_read_host_interest_value(ar, HI_ITEM(hi_failure_state),
  1812. &reg_dump_area);
  1813. if (ret) {
  1814. ath10k_warn(ar, "failed to read firmware dump area: %d\n", ret);
  1815. return;
  1816. }
  1817. if (fast_dump)
  1818. ret = ath10k_bmi_read_memory(ar, reg_dump_area, reg_dump_values,
  1819. sizeof(reg_dump_values));
  1820. else
  1821. ret = ath10k_sdio_read_mem(ar, reg_dump_area, reg_dump_values,
  1822. sizeof(reg_dump_values));
  1823. if (ret) {
  1824. ath10k_warn(ar, "failed to read firmware dump value: %d\n", ret);
  1825. return;
  1826. }
  1827. ath10k_err(ar, "firmware register dump:\n");
  1828. for (i = 0; i < ARRAY_SIZE(reg_dump_values); i += 4)
  1829. ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
  1830. i,
  1831. reg_dump_values[i],
  1832. reg_dump_values[i + 1],
  1833. reg_dump_values[i + 2],
  1834. reg_dump_values[i + 3]);
  1835. if (!crash_data)
  1836. return;
  1837. for (i = 0; i < ARRAY_SIZE(reg_dump_values); i++)
  1838. crash_data->registers[i] = __cpu_to_le32(reg_dump_values[i]);
  1839. }
  1840. static int ath10k_sdio_dump_memory_section(struct ath10k *ar,
  1841. const struct ath10k_mem_region *mem_region,
  1842. u8 *buf, size_t buf_len)
  1843. {
  1844. const struct ath10k_mem_section *cur_section, *next_section;
  1845. unsigned int count, section_size, skip_size;
  1846. int ret, i, j;
  1847. if (!mem_region || !buf)
  1848. return 0;
  1849. cur_section = &mem_region->section_table.sections[0];
  1850. if (mem_region->start > cur_section->start) {
  1851. ath10k_warn(ar, "incorrect memdump region 0x%x with section start address 0x%x.\n",
  1852. mem_region->start, cur_section->start);
  1853. return 0;
  1854. }
  1855. skip_size = cur_section->start - mem_region->start;
  1856. /* fill the gap between the first register section and register
  1857. * start address
  1858. */
  1859. for (i = 0; i < skip_size; i++) {
  1860. *buf = ATH10K_MAGIC_NOT_COPIED;
  1861. buf++;
  1862. }
  1863. count = 0;
  1864. i = 0;
  1865. for (; cur_section; cur_section = next_section) {
  1866. section_size = cur_section->end - cur_section->start;
  1867. if (section_size <= 0) {
  1868. ath10k_warn(ar, "incorrect ramdump format with start address 0x%x and stop address 0x%x\n",
  1869. cur_section->start,
  1870. cur_section->end);
  1871. break;
  1872. }
  1873. if (++i == mem_region->section_table.size) {
  1874. /* last section */
  1875. next_section = NULL;
  1876. skip_size = 0;
  1877. } else {
  1878. next_section = cur_section + 1;
  1879. if (cur_section->end > next_section->start) {
  1880. ath10k_warn(ar, "next ramdump section 0x%x is smaller than current end address 0x%x\n",
  1881. next_section->start,
  1882. cur_section->end);
  1883. break;
  1884. }
  1885. skip_size = next_section->start - cur_section->end;
  1886. }
  1887. if (buf_len < (skip_size + section_size)) {
  1888. ath10k_warn(ar, "ramdump buffer is too small: %zu\n", buf_len);
  1889. break;
  1890. }
  1891. buf_len -= skip_size + section_size;
  1892. /* read section to dest memory */
  1893. ret = ath10k_sdio_read_mem(ar, cur_section->start,
  1894. buf, section_size);
  1895. if (ret) {
  1896. ath10k_warn(ar, "failed to read ramdump from section 0x%x: %d\n",
  1897. cur_section->start, ret);
  1898. break;
  1899. }
  1900. buf += section_size;
  1901. count += section_size;
  1902. /* fill in the gap between this section and the next */
  1903. for (j = 0; j < skip_size; j++) {
  1904. *buf = ATH10K_MAGIC_NOT_COPIED;
  1905. buf++;
  1906. }
  1907. count += skip_size;
  1908. }
  1909. return count;
  1910. }
  1911. /* if an error happened returns < 0, otherwise the length */
  1912. static int ath10k_sdio_dump_memory_generic(struct ath10k *ar,
  1913. const struct ath10k_mem_region *current_region,
  1914. u8 *buf,
  1915. bool fast_dump)
  1916. {
  1917. int ret;
  1918. if (current_region->section_table.size > 0)
  1919. /* Copy each section individually. */
  1920. return ath10k_sdio_dump_memory_section(ar,
  1921. current_region,
  1922. buf,
  1923. current_region->len);
  1924. /* No individiual memory sections defined so we can
  1925. * copy the entire memory region.
  1926. */
  1927. if (fast_dump)
  1928. ret = ath10k_bmi_read_memory(ar,
  1929. current_region->start,
  1930. buf,
  1931. current_region->len);
  1932. else
  1933. ret = ath10k_sdio_read_mem(ar,
  1934. current_region->start,
  1935. buf,
  1936. current_region->len);
  1937. if (ret) {
  1938. ath10k_warn(ar, "failed to copy ramdump region %s: %d\n",
  1939. current_region->name, ret);
  1940. return ret;
  1941. }
  1942. return current_region->len;
  1943. }
  1944. static void ath10k_sdio_dump_memory(struct ath10k *ar,
  1945. struct ath10k_fw_crash_data *crash_data,
  1946. bool fast_dump)
  1947. {
  1948. const struct ath10k_hw_mem_layout *mem_layout;
  1949. const struct ath10k_mem_region *current_region;
  1950. struct ath10k_dump_ram_data_hdr *hdr;
  1951. u32 count;
  1952. size_t buf_len;
  1953. int ret, i;
  1954. u8 *buf;
  1955. if (!crash_data)
  1956. return;
  1957. mem_layout = ath10k_coredump_get_mem_layout(ar);
  1958. if (!mem_layout)
  1959. return;
  1960. current_region = &mem_layout->region_table.regions[0];
  1961. buf = crash_data->ramdump_buf;
  1962. buf_len = crash_data->ramdump_buf_len;
  1963. memset(buf, 0, buf_len);
  1964. for (i = 0; i < mem_layout->region_table.size; i++) {
  1965. count = 0;
  1966. if (current_region->len > buf_len) {
  1967. ath10k_warn(ar, "memory region %s size %d is larger that remaining ramdump buffer size %zu\n",
  1968. current_region->name,
  1969. current_region->len,
  1970. buf_len);
  1971. break;
  1972. }
  1973. /* Reserve space for the header. */
  1974. hdr = (void *)buf;
  1975. buf += sizeof(*hdr);
  1976. buf_len -= sizeof(*hdr);
  1977. ret = ath10k_sdio_dump_memory_generic(ar, current_region, buf,
  1978. fast_dump);
  1979. if (ret >= 0)
  1980. count = ret;
  1981. hdr->region_type = cpu_to_le32(current_region->type);
  1982. hdr->start = cpu_to_le32(current_region->start);
  1983. hdr->length = cpu_to_le32(count);
  1984. if (count == 0)
  1985. /* Note: the header remains, just with zero length. */
  1986. break;
  1987. buf += count;
  1988. buf_len -= count;
  1989. current_region++;
  1990. }
  1991. }
  1992. void ath10k_sdio_fw_crashed_dump(struct ath10k *ar)
  1993. {
  1994. struct ath10k_fw_crash_data *crash_data;
  1995. char guid[UUID_STRING_LEN + 1];
  1996. bool fast_dump;
  1997. fast_dump = ath10k_sdio_is_fast_dump_supported(ar);
  1998. if (fast_dump)
  1999. ath10k_bmi_start(ar);
  2000. ar->stats.fw_crash_counter++;
  2001. ath10k_sdio_disable_intrs(ar);
  2002. crash_data = ath10k_coredump_new(ar);
  2003. if (crash_data)
  2004. scnprintf(guid, sizeof(guid), "%pUl", &crash_data->guid);
  2005. else
  2006. scnprintf(guid, sizeof(guid), "n/a");
  2007. ath10k_err(ar, "firmware crashed! (guid %s)\n", guid);
  2008. ath10k_print_driver_info(ar);
  2009. ath10k_sdio_dump_registers(ar, crash_data, fast_dump);
  2010. ath10k_sdio_dump_memory(ar, crash_data, fast_dump);
  2011. ath10k_sdio_enable_intrs(ar);
  2012. ath10k_core_start_recovery(ar);
  2013. }
  2014. static int ath10k_sdio_probe(struct sdio_func *func,
  2015. const struct sdio_device_id *id)
  2016. {
  2017. struct ath10k_sdio *ar_sdio;
  2018. struct ath10k *ar;
  2019. enum ath10k_hw_rev hw_rev;
  2020. u32 dev_id_base;
  2021. struct ath10k_bus_params bus_params = {};
  2022. int ret, i;
  2023. /* Assumption: All SDIO based chipsets (so far) are QCA6174 based.
  2024. * If there will be newer chipsets that does not use the hw reg
  2025. * setup as defined in qca6174_regs and qca6174_values, this
  2026. * assumption is no longer valid and hw_rev must be setup differently
  2027. * depending on chipset.
  2028. */
  2029. hw_rev = ATH10K_HW_QCA6174;
  2030. ar = ath10k_core_create(sizeof(*ar_sdio), &func->dev, ATH10K_BUS_SDIO,
  2031. hw_rev, &ath10k_sdio_hif_ops);
  2032. if (!ar) {
  2033. dev_err(&func->dev, "failed to allocate core\n");
  2034. return -ENOMEM;
  2035. }
  2036. netif_napi_add(&ar->napi_dev, &ar->napi, ath10k_sdio_napi_poll);
  2037. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  2038. "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
  2039. func->num, func->vendor, func->device,
  2040. func->max_blksize, func->cur_blksize);
  2041. ar_sdio = ath10k_sdio_priv(ar);
  2042. ar_sdio->irq_data.irq_proc_reg =
  2043. devm_kzalloc(ar->dev, sizeof(struct ath10k_sdio_irq_proc_regs),
  2044. GFP_KERNEL);
  2045. if (!ar_sdio->irq_data.irq_proc_reg) {
  2046. ret = -ENOMEM;
  2047. goto err_core_destroy;
  2048. }
  2049. ar_sdio->vsg_buffer = devm_kmalloc(ar->dev, ATH10K_SDIO_VSG_BUF_SIZE, GFP_KERNEL);
  2050. if (!ar_sdio->vsg_buffer) {
  2051. ret = -ENOMEM;
  2052. goto err_core_destroy;
  2053. }
  2054. ar_sdio->irq_data.irq_en_reg =
  2055. devm_kzalloc(ar->dev, sizeof(struct ath10k_sdio_irq_enable_regs),
  2056. GFP_KERNEL);
  2057. if (!ar_sdio->irq_data.irq_en_reg) {
  2058. ret = -ENOMEM;
  2059. goto err_core_destroy;
  2060. }
  2061. ar_sdio->bmi_buf = devm_kzalloc(ar->dev, BMI_MAX_LARGE_CMDBUF_SIZE, GFP_KERNEL);
  2062. if (!ar_sdio->bmi_buf) {
  2063. ret = -ENOMEM;
  2064. goto err_core_destroy;
  2065. }
  2066. ar_sdio->func = func;
  2067. sdio_set_drvdata(func, ar_sdio);
  2068. ar_sdio->is_disabled = true;
  2069. ar_sdio->ar = ar;
  2070. spin_lock_init(&ar_sdio->lock);
  2071. spin_lock_init(&ar_sdio->wr_async_lock);
  2072. mutex_init(&ar_sdio->irq_data.mtx);
  2073. INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
  2074. INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
  2075. INIT_WORK(&ar_sdio->wr_async_work, ath10k_sdio_write_async_work);
  2076. ar_sdio->workqueue = create_singlethread_workqueue("ath10k_sdio_wq");
  2077. if (!ar_sdio->workqueue) {
  2078. ret = -ENOMEM;
  2079. goto err_core_destroy;
  2080. }
  2081. for (i = 0; i < ATH10K_SDIO_BUS_REQUEST_MAX_NUM; i++)
  2082. ath10k_sdio_free_bus_req(ar, &ar_sdio->bus_req[i]);
  2083. skb_queue_head_init(&ar_sdio->rx_head);
  2084. INIT_WORK(&ar_sdio->async_work_rx, ath10k_rx_indication_async_work);
  2085. dev_id_base = (id->device & 0x0F00);
  2086. if (dev_id_base != (SDIO_DEVICE_ID_ATHEROS_AR6005 & 0x0F00) &&
  2087. dev_id_base != (SDIO_DEVICE_ID_ATHEROS_QCA9377 & 0x0F00)) {
  2088. ret = -ENODEV;
  2089. ath10k_err(ar, "unsupported device id %u (0x%x)\n",
  2090. dev_id_base, id->device);
  2091. goto err_free_wq;
  2092. }
  2093. ar->dev_id = QCA9377_1_0_DEVICE_ID;
  2094. ar->id.vendor = id->vendor;
  2095. ar->id.device = id->device;
  2096. ath10k_sdio_set_mbox_info(ar);
  2097. bus_params.dev_type = ATH10K_DEV_TYPE_HL;
  2098. /* TODO: don't know yet how to get chip_id with SDIO */
  2099. bus_params.chip_id = 0;
  2100. bus_params.hl_msdu_ids = true;
  2101. ar->hw->max_mtu = ETH_DATA_LEN;
  2102. ret = ath10k_core_register(ar, &bus_params);
  2103. if (ret) {
  2104. ath10k_err(ar, "failed to register driver core: %d\n", ret);
  2105. goto err_free_wq;
  2106. }
  2107. timer_setup(&ar_sdio->sleep_timer, ath10k_sdio_sleep_timer_handler, 0);
  2108. return 0;
  2109. err_free_wq:
  2110. destroy_workqueue(ar_sdio->workqueue);
  2111. err_core_destroy:
  2112. ath10k_core_destroy(ar);
  2113. return ret;
  2114. }
  2115. static void ath10k_sdio_remove(struct sdio_func *func)
  2116. {
  2117. struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
  2118. struct ath10k *ar = ar_sdio->ar;
  2119. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  2120. "sdio removed func %d vendor 0x%x device 0x%x\n",
  2121. func->num, func->vendor, func->device);
  2122. ath10k_core_unregister(ar);
  2123. netif_napi_del(&ar->napi);
  2124. ath10k_core_destroy(ar);
  2125. destroy_workqueue(ar_sdio->workqueue);
  2126. }
  2127. static const struct sdio_device_id ath10k_sdio_devices[] = {
  2128. {SDIO_DEVICE(SDIO_VENDOR_ID_ATHEROS, SDIO_DEVICE_ID_ATHEROS_AR6005)},
  2129. {SDIO_DEVICE(SDIO_VENDOR_ID_ATHEROS, SDIO_DEVICE_ID_ATHEROS_QCA9377)},
  2130. {},
  2131. };
  2132. MODULE_DEVICE_TABLE(sdio, ath10k_sdio_devices);
  2133. static struct sdio_driver ath10k_sdio_driver = {
  2134. .name = "ath10k_sdio",
  2135. .id_table = ath10k_sdio_devices,
  2136. .probe = ath10k_sdio_probe,
  2137. .remove = ath10k_sdio_remove,
  2138. .drv = {
  2139. .owner = THIS_MODULE,
  2140. .pm = ATH10K_SDIO_PM_OPS,
  2141. },
  2142. };
  2143. static int __init ath10k_sdio_init(void)
  2144. {
  2145. int ret;
  2146. ret = sdio_register_driver(&ath10k_sdio_driver);
  2147. if (ret)
  2148. pr_err("sdio driver registration failed: %d\n", ret);
  2149. return ret;
  2150. }
  2151. static void __exit ath10k_sdio_exit(void)
  2152. {
  2153. sdio_unregister_driver(&ath10k_sdio_driver);
  2154. }
  2155. module_init(ath10k_sdio_init);
  2156. module_exit(ath10k_sdio_exit);
  2157. MODULE_AUTHOR("Qualcomm Atheros");
  2158. MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN SDIO devices");
  2159. MODULE_LICENSE("Dual BSD/GPL");