dhd_sdio.c 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/kthread.h>
  20. #include <linux/printk.h>
  21. #include <linux/pci_ids.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/sched.h>
  25. #include <linux/mmc/sdio.h>
  26. #include <linux/mmc/sdio_func.h>
  27. #include <linux/mmc/card.h>
  28. #include <linux/semaphore.h>
  29. #include <linux/firmware.h>
  30. #include <linux/module.h>
  31. #include <linux/bcma/bcma.h>
  32. #include <linux/debugfs.h>
  33. #include <linux/vmalloc.h>
  34. #include <asm/unaligned.h>
  35. #include <defs.h>
  36. #include <brcmu_wifi.h>
  37. #include <brcmu_utils.h>
  38. #include <brcm_hw_ids.h>
  39. #include <soc.h>
  40. #include "sdio_host.h"
  41. #include "sdio_chip.h"
  42. #define DCMD_RESP_TIMEOUT 2000 /* In milli second */
  43. #ifdef DEBUG
  44. #define BRCMF_TRAP_INFO_SIZE 80
  45. #define CBUF_LEN (128)
  46. /* Device console log buffer state */
  47. #define CONSOLE_BUFFER_MAX 2024
  48. struct rte_log_le {
  49. __le32 buf; /* Can't be pointer on (64-bit) hosts */
  50. __le32 buf_size;
  51. __le32 idx;
  52. char *_buf_compat; /* Redundant pointer for backward compat. */
  53. };
  54. struct rte_console {
  55. /* Virtual UART
  56. * When there is no UART (e.g. Quickturn),
  57. * the host should write a complete
  58. * input line directly into cbuf and then write
  59. * the length into vcons_in.
  60. * This may also be used when there is a real UART
  61. * (at risk of conflicting with
  62. * the real UART). vcons_out is currently unused.
  63. */
  64. uint vcons_in;
  65. uint vcons_out;
  66. /* Output (logging) buffer
  67. * Console output is written to a ring buffer log_buf at index log_idx.
  68. * The host may read the output when it sees log_idx advance.
  69. * Output will be lost if the output wraps around faster than the host
  70. * polls.
  71. */
  72. struct rte_log_le log_le;
  73. /* Console input line buffer
  74. * Characters are read one at a time into cbuf
  75. * until <CR> is received, then
  76. * the buffer is processed as a command line.
  77. * Also used for virtual UART.
  78. */
  79. uint cbuf_idx;
  80. char cbuf[CBUF_LEN];
  81. };
  82. #endif /* DEBUG */
  83. #include <chipcommon.h>
  84. #include "dhd_bus.h"
  85. #include "dhd_dbg.h"
  86. #define TXQLEN 2048 /* bulk tx queue length */
  87. #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
  88. #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
  89. #define PRIOMASK 7
  90. #define TXRETRIES 2 /* # of retries for tx frames */
  91. #define BRCMF_RXBOUND 50 /* Default for max rx frames in
  92. one scheduling */
  93. #define BRCMF_TXBOUND 20 /* Default for max tx frames in
  94. one scheduling */
  95. #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
  96. #define MEMBLOCK 2048 /* Block size used for downloading
  97. of dongle image */
  98. #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
  99. biggest possible glom */
  100. #define BRCMF_FIRSTREAD (1 << 6)
  101. /* SBSDIO_DEVICE_CTL */
  102. /* 1: device will assert busy signal when receiving CMD53 */
  103. #define SBSDIO_DEVCTL_SETBUSY 0x01
  104. /* 1: assertion of sdio interrupt is synchronous to the sdio clock */
  105. #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
  106. /* 1: mask all interrupts to host except the chipActive (rev 8) */
  107. #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
  108. /* 1: isolate internal sdio signals, put external pads in tri-state; requires
  109. * sdio bus power cycle to clear (rev 9) */
  110. #define SBSDIO_DEVCTL_PADS_ISO 0x08
  111. /* Force SD->SB reset mapping (rev 11) */
  112. #define SBSDIO_DEVCTL_SB_RST_CTL 0x30
  113. /* Determined by CoreControl bit */
  114. #define SBSDIO_DEVCTL_RST_CORECTL 0x00
  115. /* Force backplane reset */
  116. #define SBSDIO_DEVCTL_RST_BPRESET 0x10
  117. /* Force no backplane reset */
  118. #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
  119. /* direct(mapped) cis space */
  120. /* MAPPED common CIS address */
  121. #define SBSDIO_CIS_BASE_COMMON 0x1000
  122. /* maximum bytes in one CIS */
  123. #define SBSDIO_CIS_SIZE_LIMIT 0x200
  124. /* cis offset addr is < 17 bits */
  125. #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
  126. /* manfid tuple length, include tuple, link bytes */
  127. #define SBSDIO_CIS_MANFID_TUPLE_LEN 6
  128. /* intstatus */
  129. #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
  130. #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
  131. #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
  132. #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
  133. #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
  134. #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
  135. #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
  136. #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
  137. #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
  138. #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
  139. #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
  140. #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
  141. #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
  142. #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
  143. #define I_PC (1 << 10) /* descriptor error */
  144. #define I_PD (1 << 11) /* data error */
  145. #define I_DE (1 << 12) /* Descriptor protocol Error */
  146. #define I_RU (1 << 13) /* Receive descriptor Underflow */
  147. #define I_RO (1 << 14) /* Receive fifo Overflow */
  148. #define I_XU (1 << 15) /* Transmit fifo Underflow */
  149. #define I_RI (1 << 16) /* Receive Interrupt */
  150. #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
  151. #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
  152. #define I_XI (1 << 24) /* Transmit Interrupt */
  153. #define I_RF_TERM (1 << 25) /* Read Frame Terminate */
  154. #define I_WF_TERM (1 << 26) /* Write Frame Terminate */
  155. #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
  156. #define I_SBINT (1 << 28) /* sbintstatus Interrupt */
  157. #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
  158. #define I_SRESET (1 << 30) /* CCCR RES interrupt */
  159. #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
  160. #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
  161. #define I_DMA (I_RI | I_XI | I_ERRORS)
  162. /* corecontrol */
  163. #define CC_CISRDY (1 << 0) /* CIS Ready */
  164. #define CC_BPRESEN (1 << 1) /* CCCR RES signal */
  165. #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
  166. #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
  167. #define CC_XMTDATAAVAIL_MODE (1 << 4)
  168. #define CC_XMTDATAAVAIL_CTRL (1 << 5)
  169. /* SDA_FRAMECTRL */
  170. #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
  171. #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
  172. #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
  173. #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
  174. /* HW frame tag */
  175. #define SDPCM_FRAMETAG_LEN 4 /* 2 bytes len, 2 bytes check val */
  176. /* Total length of frame header for dongle protocol */
  177. #define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
  178. #define SDPCM_RESERVE (SDPCM_HDRLEN + BRCMF_SDALIGN)
  179. /*
  180. * Software allocation of To SB Mailbox resources
  181. */
  182. /* tosbmailbox bits corresponding to intstatus bits */
  183. #define SMB_NAK (1 << 0) /* Frame NAK */
  184. #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
  185. #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
  186. #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
  187. /* tosbmailboxdata */
  188. #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
  189. /*
  190. * Software allocation of To Host Mailbox resources
  191. */
  192. /* intstatus bits */
  193. #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
  194. #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
  195. #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
  196. #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
  197. /* tohostmailboxdata */
  198. #define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
  199. #define HMB_DATA_DEVREADY 2 /* talk to host after enable */
  200. #define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
  201. #define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
  202. #define HMB_DATA_FCDATA_MASK 0xff000000
  203. #define HMB_DATA_FCDATA_SHIFT 24
  204. #define HMB_DATA_VERSION_MASK 0x00ff0000
  205. #define HMB_DATA_VERSION_SHIFT 16
  206. /*
  207. * Software-defined protocol header
  208. */
  209. /* Current protocol version */
  210. #define SDPCM_PROT_VERSION 4
  211. /* SW frame header */
  212. #define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff)
  213. #define SDPCM_CHANNEL_MASK 0x00000f00
  214. #define SDPCM_CHANNEL_SHIFT 8
  215. #define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f)
  216. #define SDPCM_NEXTLEN_OFFSET 2
  217. /* Data Offset from SOF (HW Tag, SW Tag, Pad) */
  218. #define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */
  219. #define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff)
  220. #define SDPCM_DOFFSET_MASK 0xff000000
  221. #define SDPCM_DOFFSET_SHIFT 24
  222. #define SDPCM_FCMASK_OFFSET 4 /* Flow control */
  223. #define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff)
  224. #define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */
  225. #define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff)
  226. #define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */
  227. /* logical channel numbers */
  228. #define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */
  229. #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */
  230. #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */
  231. #define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */
  232. #define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */
  233. #define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */
  234. #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
  235. /*
  236. * Shared structure between dongle and the host.
  237. * The structure contains pointers to trap or assert information.
  238. */
  239. #define SDPCM_SHARED_VERSION 0x0003
  240. #define SDPCM_SHARED_VERSION_MASK 0x00FF
  241. #define SDPCM_SHARED_ASSERT_BUILT 0x0100
  242. #define SDPCM_SHARED_ASSERT 0x0200
  243. #define SDPCM_SHARED_TRAP 0x0400
  244. /* Space for header read, limit for data packets */
  245. #define MAX_HDR_READ (1 << 6)
  246. #define MAX_RX_DATASZ 2048
  247. /* Maximum milliseconds to wait for F2 to come up */
  248. #define BRCMF_WAIT_F2RDY 3000
  249. /* Bump up limit on waiting for HT to account for first startup;
  250. * if the image is doing a CRC calculation before programming the PMU
  251. * for HT availability, it could take a couple hundred ms more, so
  252. * max out at a 1 second (1000000us).
  253. */
  254. #undef PMU_MAX_TRANSITION_DLY
  255. #define PMU_MAX_TRANSITION_DLY 1000000
  256. /* Value for ChipClockCSR during initial setup */
  257. #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
  258. SBSDIO_ALP_AVAIL_REQ)
  259. /* Flags for SDH calls */
  260. #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
  261. #define BRCMF_SDIO_FW_NAME "brcm/brcmfmac-sdio.bin"
  262. #define BRCMF_SDIO_NV_NAME "brcm/brcmfmac-sdio.txt"
  263. MODULE_FIRMWARE(BRCMF_SDIO_FW_NAME);
  264. MODULE_FIRMWARE(BRCMF_SDIO_NV_NAME);
  265. #define BRCMF_IDLE_IMMEDIATE (-1) /* Enter idle immediately */
  266. #define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
  267. * when idle
  268. */
  269. #define BRCMF_IDLE_INTERVAL 1
  270. /*
  271. * Conversion of 802.1D priority to precedence level
  272. */
  273. static uint prio2prec(u32 prio)
  274. {
  275. return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
  276. (prio^2) : prio;
  277. }
  278. /* core registers */
  279. struct sdpcmd_regs {
  280. u32 corecontrol; /* 0x00, rev8 */
  281. u32 corestatus; /* rev8 */
  282. u32 PAD[1];
  283. u32 biststatus; /* rev8 */
  284. /* PCMCIA access */
  285. u16 pcmciamesportaladdr; /* 0x010, rev8 */
  286. u16 PAD[1];
  287. u16 pcmciamesportalmask; /* rev8 */
  288. u16 PAD[1];
  289. u16 pcmciawrframebc; /* rev8 */
  290. u16 PAD[1];
  291. u16 pcmciaunderflowtimer; /* rev8 */
  292. u16 PAD[1];
  293. /* interrupt */
  294. u32 intstatus; /* 0x020, rev8 */
  295. u32 hostintmask; /* rev8 */
  296. u32 intmask; /* rev8 */
  297. u32 sbintstatus; /* rev8 */
  298. u32 sbintmask; /* rev8 */
  299. u32 funcintmask; /* rev4 */
  300. u32 PAD[2];
  301. u32 tosbmailbox; /* 0x040, rev8 */
  302. u32 tohostmailbox; /* rev8 */
  303. u32 tosbmailboxdata; /* rev8 */
  304. u32 tohostmailboxdata; /* rev8 */
  305. /* synchronized access to registers in SDIO clock domain */
  306. u32 sdioaccess; /* 0x050, rev8 */
  307. u32 PAD[3];
  308. /* PCMCIA frame control */
  309. u8 pcmciaframectrl; /* 0x060, rev8 */
  310. u8 PAD[3];
  311. u8 pcmciawatermark; /* rev8 */
  312. u8 PAD[155];
  313. /* interrupt batching control */
  314. u32 intrcvlazy; /* 0x100, rev8 */
  315. u32 PAD[3];
  316. /* counters */
  317. u32 cmd52rd; /* 0x110, rev8 */
  318. u32 cmd52wr; /* rev8 */
  319. u32 cmd53rd; /* rev8 */
  320. u32 cmd53wr; /* rev8 */
  321. u32 abort; /* rev8 */
  322. u32 datacrcerror; /* rev8 */
  323. u32 rdoutofsync; /* rev8 */
  324. u32 wroutofsync; /* rev8 */
  325. u32 writebusy; /* rev8 */
  326. u32 readwait; /* rev8 */
  327. u32 readterm; /* rev8 */
  328. u32 writeterm; /* rev8 */
  329. u32 PAD[40];
  330. u32 clockctlstatus; /* rev8 */
  331. u32 PAD[7];
  332. u32 PAD[128]; /* DMA engines */
  333. /* SDIO/PCMCIA CIS region */
  334. char cis[512]; /* 0x400-0x5ff, rev6 */
  335. /* PCMCIA function control registers */
  336. char pcmciafcr[256]; /* 0x600-6ff, rev6 */
  337. u16 PAD[55];
  338. /* PCMCIA backplane access */
  339. u16 backplanecsr; /* 0x76E, rev6 */
  340. u16 backplaneaddr0; /* rev6 */
  341. u16 backplaneaddr1; /* rev6 */
  342. u16 backplaneaddr2; /* rev6 */
  343. u16 backplaneaddr3; /* rev6 */
  344. u16 backplanedata0; /* rev6 */
  345. u16 backplanedata1; /* rev6 */
  346. u16 backplanedata2; /* rev6 */
  347. u16 backplanedata3; /* rev6 */
  348. u16 PAD[31];
  349. /* sprom "size" & "blank" info */
  350. u16 spromstatus; /* 0x7BE, rev2 */
  351. u32 PAD[464];
  352. u16 PAD[0x80];
  353. };
  354. #ifdef DEBUG
  355. /* Device console log buffer state */
  356. struct brcmf_console {
  357. uint count; /* Poll interval msec counter */
  358. uint log_addr; /* Log struct address (fixed) */
  359. struct rte_log_le log_le; /* Log struct (host copy) */
  360. uint bufsize; /* Size of log buffer */
  361. u8 *buf; /* Log buffer (host copy) */
  362. uint last; /* Last buffer read index */
  363. };
  364. struct brcmf_trap_info {
  365. __le32 type;
  366. __le32 epc;
  367. __le32 cpsr;
  368. __le32 spsr;
  369. __le32 r0; /* a1 */
  370. __le32 r1; /* a2 */
  371. __le32 r2; /* a3 */
  372. __le32 r3; /* a4 */
  373. __le32 r4; /* v1 */
  374. __le32 r5; /* v2 */
  375. __le32 r6; /* v3 */
  376. __le32 r7; /* v4 */
  377. __le32 r8; /* v5 */
  378. __le32 r9; /* sb/v6 */
  379. __le32 r10; /* sl/v7 */
  380. __le32 r11; /* fp/v8 */
  381. __le32 r12; /* ip */
  382. __le32 r13; /* sp */
  383. __le32 r14; /* lr */
  384. __le32 pc; /* r15 */
  385. };
  386. #endif /* DEBUG */
  387. struct sdpcm_shared {
  388. u32 flags;
  389. u32 trap_addr;
  390. u32 assert_exp_addr;
  391. u32 assert_file_addr;
  392. u32 assert_line;
  393. u32 console_addr; /* Address of struct rte_console */
  394. u32 msgtrace_addr;
  395. u8 tag[32];
  396. u32 brpt_addr;
  397. };
  398. struct sdpcm_shared_le {
  399. __le32 flags;
  400. __le32 trap_addr;
  401. __le32 assert_exp_addr;
  402. __le32 assert_file_addr;
  403. __le32 assert_line;
  404. __le32 console_addr; /* Address of struct rte_console */
  405. __le32 msgtrace_addr;
  406. u8 tag[32];
  407. __le32 brpt_addr;
  408. };
  409. /* SDIO read frame info */
  410. struct brcmf_sdio_read {
  411. u8 seq_num;
  412. u8 channel;
  413. u16 len;
  414. u16 len_left;
  415. u16 len_nxtfrm;
  416. u8 dat_offset;
  417. };
  418. /* misc chip info needed by some of the routines */
  419. /* Private data for SDIO bus interaction */
  420. struct brcmf_sdio {
  421. struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
  422. struct chip_info *ci; /* Chip info struct */
  423. char *vars; /* Variables (from CIS and/or other) */
  424. uint varsz; /* Size of variables buffer */
  425. u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
  426. u32 hostintmask; /* Copy of Host Interrupt Mask */
  427. atomic_t intstatus; /* Intstatus bits (events) pending */
  428. atomic_t fcstate; /* State of dongle flow-control */
  429. uint blocksize; /* Block size of SDIO transfers */
  430. uint roundup; /* Max roundup limit */
  431. struct pktq txq; /* Queue length used for flow-control */
  432. u8 flowcontrol; /* per prio flow control bitmask */
  433. u8 tx_seq; /* Transmit sequence number (next) */
  434. u8 tx_max; /* Maximum transmit sequence allowed */
  435. u8 hdrbuf[MAX_HDR_READ + BRCMF_SDALIGN];
  436. u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
  437. u8 rx_seq; /* Receive sequence number (expected) */
  438. struct brcmf_sdio_read cur_read;
  439. /* info of current read frame */
  440. bool rxskip; /* Skip receive (awaiting NAK ACK) */
  441. bool rxpending; /* Data frame pending in dongle */
  442. uint rxbound; /* Rx frames to read before resched */
  443. uint txbound; /* Tx frames to send before resched */
  444. uint txminmax;
  445. struct sk_buff *glomd; /* Packet containing glomming descriptor */
  446. struct sk_buff_head glom; /* Packet list for glommed superframe */
  447. uint glomerr; /* Glom packet read errors */
  448. u8 *rxbuf; /* Buffer for receiving control packets */
  449. uint rxblen; /* Allocated length of rxbuf */
  450. u8 *rxctl; /* Aligned pointer into rxbuf */
  451. u8 *databuf; /* Buffer for receiving big glom packet */
  452. u8 *dataptr; /* Aligned pointer into databuf */
  453. uint rxlen; /* Length of valid data in buffer */
  454. u8 sdpcm_ver; /* Bus protocol reported by dongle */
  455. bool intr; /* Use interrupts */
  456. bool poll; /* Use polling */
  457. atomic_t ipend; /* Device interrupt is pending */
  458. uint spurious; /* Count of spurious interrupts */
  459. uint pollrate; /* Ticks between device polls */
  460. uint polltick; /* Tick counter */
  461. #ifdef DEBUG
  462. uint console_interval;
  463. struct brcmf_console console; /* Console output polling support */
  464. uint console_addr; /* Console address from shared struct */
  465. #endif /* DEBUG */
  466. uint clkstate; /* State of sd and backplane clock(s) */
  467. bool activity; /* Activity flag for clock down */
  468. s32 idletime; /* Control for activity timeout */
  469. s32 idlecount; /* Activity timeout counter */
  470. s32 idleclock; /* How to set bus driver when idle */
  471. s32 sd_rxchain;
  472. bool use_rxchain; /* If brcmf should use PKT chains */
  473. bool rxflow_mode; /* Rx flow control mode */
  474. bool rxflow; /* Is rx flow control on */
  475. bool alp_only; /* Don't use HT clock (ALP only) */
  476. u8 *ctrl_frame_buf;
  477. u32 ctrl_frame_len;
  478. bool ctrl_frame_stat;
  479. spinlock_t txqlock;
  480. wait_queue_head_t ctrl_wait;
  481. wait_queue_head_t dcmd_resp_wait;
  482. struct timer_list timer;
  483. struct completion watchdog_wait;
  484. struct task_struct *watchdog_tsk;
  485. bool wd_timer_valid;
  486. uint save_ms;
  487. struct workqueue_struct *brcmf_wq;
  488. struct work_struct datawork;
  489. struct list_head dpc_tsklst;
  490. spinlock_t dpc_tl_lock;
  491. struct semaphore sdsem;
  492. const struct firmware *firmware;
  493. u32 fw_ptr;
  494. bool txoff; /* Transmit flow-controlled */
  495. struct brcmf_sdio_count sdcnt;
  496. };
  497. /* clkstate */
  498. #define CLK_NONE 0
  499. #define CLK_SDONLY 1
  500. #define CLK_PENDING 2 /* Not used yet */
  501. #define CLK_AVAIL 3
  502. #ifdef DEBUG
  503. static int qcount[NUMPRIO];
  504. static int tx_packets[NUMPRIO];
  505. #endif /* DEBUG */
  506. #define SDIO_DRIVE_STRENGTH 6 /* in milliamps */
  507. #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
  508. /* Retry count for register access failures */
  509. static const uint retry_limit = 2;
  510. /* Limit on rounding up frames */
  511. static const uint max_roundup = 512;
  512. #define ALIGNMENT 4
  513. enum brcmf_sdio_frmtype {
  514. BRCMF_SDIO_FT_NORMAL,
  515. BRCMF_SDIO_FT_SUPER,
  516. BRCMF_SDIO_FT_SUB,
  517. };
  518. static void pkt_align(struct sk_buff *p, int len, int align)
  519. {
  520. uint datalign;
  521. datalign = (unsigned long)(p->data);
  522. datalign = roundup(datalign, (align)) - datalign;
  523. if (datalign)
  524. skb_pull(p, datalign);
  525. __skb_trim(p, len);
  526. }
  527. /* To check if there's window offered */
  528. static bool data_ok(struct brcmf_sdio *bus)
  529. {
  530. return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
  531. ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
  532. }
  533. /*
  534. * Reads a register in the SDIO hardware block. This block occupies a series of
  535. * adresses on the 32 bit backplane bus.
  536. */
  537. static int
  538. r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset)
  539. {
  540. u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
  541. int ret;
  542. *regvar = brcmf_sdio_regrl(bus->sdiodev,
  543. bus->ci->c_inf[idx].base + offset, &ret);
  544. return ret;
  545. }
  546. static int
  547. w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset)
  548. {
  549. u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
  550. int ret;
  551. brcmf_sdio_regwl(bus->sdiodev,
  552. bus->ci->c_inf[idx].base + reg_offset,
  553. regval, &ret);
  554. return ret;
  555. }
  556. #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
  557. #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
  558. /* Turn backplane clock on or off */
  559. static int brcmf_sdbrcm_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
  560. {
  561. int err;
  562. u8 clkctl, clkreq, devctl;
  563. unsigned long timeout;
  564. brcmf_dbg(TRACE, "Enter\n");
  565. clkctl = 0;
  566. if (on) {
  567. /* Request HT Avail */
  568. clkreq =
  569. bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
  570. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  571. clkreq, &err);
  572. if (err) {
  573. brcmf_dbg(ERROR, "HT Avail request error: %d\n", err);
  574. return -EBADE;
  575. }
  576. /* Check current status */
  577. clkctl = brcmf_sdio_regrb(bus->sdiodev,
  578. SBSDIO_FUNC1_CHIPCLKCSR, &err);
  579. if (err) {
  580. brcmf_dbg(ERROR, "HT Avail read error: %d\n", err);
  581. return -EBADE;
  582. }
  583. /* Go to pending and await interrupt if appropriate */
  584. if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
  585. /* Allow only clock-available interrupt */
  586. devctl = brcmf_sdio_regrb(bus->sdiodev,
  587. SBSDIO_DEVICE_CTL, &err);
  588. if (err) {
  589. brcmf_dbg(ERROR, "Devctl error setting CA: %d\n",
  590. err);
  591. return -EBADE;
  592. }
  593. devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
  594. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
  595. devctl, &err);
  596. brcmf_dbg(INFO, "CLKCTL: set PENDING\n");
  597. bus->clkstate = CLK_PENDING;
  598. return 0;
  599. } else if (bus->clkstate == CLK_PENDING) {
  600. /* Cancel CA-only interrupt filter */
  601. devctl = brcmf_sdio_regrb(bus->sdiodev,
  602. SBSDIO_DEVICE_CTL, &err);
  603. devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
  604. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
  605. devctl, &err);
  606. }
  607. /* Otherwise, wait here (polling) for HT Avail */
  608. timeout = jiffies +
  609. msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
  610. while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
  611. clkctl = brcmf_sdio_regrb(bus->sdiodev,
  612. SBSDIO_FUNC1_CHIPCLKCSR,
  613. &err);
  614. if (time_after(jiffies, timeout))
  615. break;
  616. else
  617. usleep_range(5000, 10000);
  618. }
  619. if (err) {
  620. brcmf_dbg(ERROR, "HT Avail request error: %d\n", err);
  621. return -EBADE;
  622. }
  623. if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
  624. brcmf_dbg(ERROR, "HT Avail timeout (%d): clkctl 0x%02x\n",
  625. PMU_MAX_TRANSITION_DLY, clkctl);
  626. return -EBADE;
  627. }
  628. /* Mark clock available */
  629. bus->clkstate = CLK_AVAIL;
  630. brcmf_dbg(INFO, "CLKCTL: turned ON\n");
  631. #if defined(DEBUG)
  632. if (!bus->alp_only) {
  633. if (SBSDIO_ALPONLY(clkctl))
  634. brcmf_dbg(ERROR, "HT Clock should be on\n");
  635. }
  636. #endif /* defined (DEBUG) */
  637. bus->activity = true;
  638. } else {
  639. clkreq = 0;
  640. if (bus->clkstate == CLK_PENDING) {
  641. /* Cancel CA-only interrupt filter */
  642. devctl = brcmf_sdio_regrb(bus->sdiodev,
  643. SBSDIO_DEVICE_CTL, &err);
  644. devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
  645. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
  646. devctl, &err);
  647. }
  648. bus->clkstate = CLK_SDONLY;
  649. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  650. clkreq, &err);
  651. brcmf_dbg(INFO, "CLKCTL: turned OFF\n");
  652. if (err) {
  653. brcmf_dbg(ERROR, "Failed access turning clock off: %d\n",
  654. err);
  655. return -EBADE;
  656. }
  657. }
  658. return 0;
  659. }
  660. /* Change idle/active SD state */
  661. static int brcmf_sdbrcm_sdclk(struct brcmf_sdio *bus, bool on)
  662. {
  663. brcmf_dbg(TRACE, "Enter\n");
  664. if (on)
  665. bus->clkstate = CLK_SDONLY;
  666. else
  667. bus->clkstate = CLK_NONE;
  668. return 0;
  669. }
  670. /* Transition SD and backplane clock readiness */
  671. static int brcmf_sdbrcm_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
  672. {
  673. #ifdef DEBUG
  674. uint oldstate = bus->clkstate;
  675. #endif /* DEBUG */
  676. brcmf_dbg(TRACE, "Enter\n");
  677. /* Early exit if we're already there */
  678. if (bus->clkstate == target) {
  679. if (target == CLK_AVAIL) {
  680. brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
  681. bus->activity = true;
  682. }
  683. return 0;
  684. }
  685. switch (target) {
  686. case CLK_AVAIL:
  687. /* Make sure SD clock is available */
  688. if (bus->clkstate == CLK_NONE)
  689. brcmf_sdbrcm_sdclk(bus, true);
  690. /* Now request HT Avail on the backplane */
  691. brcmf_sdbrcm_htclk(bus, true, pendok);
  692. brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
  693. bus->activity = true;
  694. break;
  695. case CLK_SDONLY:
  696. /* Remove HT request, or bring up SD clock */
  697. if (bus->clkstate == CLK_NONE)
  698. brcmf_sdbrcm_sdclk(bus, true);
  699. else if (bus->clkstate == CLK_AVAIL)
  700. brcmf_sdbrcm_htclk(bus, false, false);
  701. else
  702. brcmf_dbg(ERROR, "request for %d -> %d\n",
  703. bus->clkstate, target);
  704. brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
  705. break;
  706. case CLK_NONE:
  707. /* Make sure to remove HT request */
  708. if (bus->clkstate == CLK_AVAIL)
  709. brcmf_sdbrcm_htclk(bus, false, false);
  710. /* Now remove the SD clock */
  711. brcmf_sdbrcm_sdclk(bus, false);
  712. brcmf_sdbrcm_wd_timer(bus, 0);
  713. break;
  714. }
  715. #ifdef DEBUG
  716. brcmf_dbg(INFO, "%d -> %d\n", oldstate, bus->clkstate);
  717. #endif /* DEBUG */
  718. return 0;
  719. }
  720. static u32 brcmf_sdbrcm_hostmail(struct brcmf_sdio *bus)
  721. {
  722. u32 intstatus = 0;
  723. u32 hmb_data;
  724. u8 fcbits;
  725. int ret;
  726. brcmf_dbg(TRACE, "Enter\n");
  727. /* Read mailbox data and ack that we did so */
  728. ret = r_sdreg32(bus, &hmb_data,
  729. offsetof(struct sdpcmd_regs, tohostmailboxdata));
  730. if (ret == 0)
  731. w_sdreg32(bus, SMB_INT_ACK,
  732. offsetof(struct sdpcmd_regs, tosbmailbox));
  733. bus->sdcnt.f1regdata += 2;
  734. /* Dongle recomposed rx frames, accept them again */
  735. if (hmb_data & HMB_DATA_NAKHANDLED) {
  736. brcmf_dbg(INFO, "Dongle reports NAK handled, expect rtx of %d\n",
  737. bus->rx_seq);
  738. if (!bus->rxskip)
  739. brcmf_dbg(ERROR, "unexpected NAKHANDLED!\n");
  740. bus->rxskip = false;
  741. intstatus |= I_HMB_FRAME_IND;
  742. }
  743. /*
  744. * DEVREADY does not occur with gSPI.
  745. */
  746. if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
  747. bus->sdpcm_ver =
  748. (hmb_data & HMB_DATA_VERSION_MASK) >>
  749. HMB_DATA_VERSION_SHIFT;
  750. if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
  751. brcmf_dbg(ERROR, "Version mismatch, dongle reports %d, "
  752. "expecting %d\n",
  753. bus->sdpcm_ver, SDPCM_PROT_VERSION);
  754. else
  755. brcmf_dbg(INFO, "Dongle ready, protocol version %d\n",
  756. bus->sdpcm_ver);
  757. }
  758. /*
  759. * Flow Control has been moved into the RX headers and this out of band
  760. * method isn't used any more.
  761. * remaining backward compatible with older dongles.
  762. */
  763. if (hmb_data & HMB_DATA_FC) {
  764. fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
  765. HMB_DATA_FCDATA_SHIFT;
  766. if (fcbits & ~bus->flowcontrol)
  767. bus->sdcnt.fc_xoff++;
  768. if (bus->flowcontrol & ~fcbits)
  769. bus->sdcnt.fc_xon++;
  770. bus->sdcnt.fc_rcvd++;
  771. bus->flowcontrol = fcbits;
  772. }
  773. /* Shouldn't be any others */
  774. if (hmb_data & ~(HMB_DATA_DEVREADY |
  775. HMB_DATA_NAKHANDLED |
  776. HMB_DATA_FC |
  777. HMB_DATA_FWREADY |
  778. HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
  779. brcmf_dbg(ERROR, "Unknown mailbox data content: 0x%02x\n",
  780. hmb_data);
  781. return intstatus;
  782. }
  783. static void brcmf_sdbrcm_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
  784. {
  785. uint retries = 0;
  786. u16 lastrbc;
  787. u8 hi, lo;
  788. int err;
  789. brcmf_dbg(ERROR, "%sterminate frame%s\n",
  790. abort ? "abort command, " : "",
  791. rtx ? ", send NAK" : "");
  792. if (abort)
  793. brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
  794. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
  795. SFC_RF_TERM, &err);
  796. bus->sdcnt.f1regdata++;
  797. /* Wait until the packet has been flushed (device/FIFO stable) */
  798. for (lastrbc = retries = 0xffff; retries > 0; retries--) {
  799. hi = brcmf_sdio_regrb(bus->sdiodev,
  800. SBSDIO_FUNC1_RFRAMEBCHI, &err);
  801. lo = brcmf_sdio_regrb(bus->sdiodev,
  802. SBSDIO_FUNC1_RFRAMEBCLO, &err);
  803. bus->sdcnt.f1regdata += 2;
  804. if ((hi == 0) && (lo == 0))
  805. break;
  806. if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
  807. brcmf_dbg(ERROR, "count growing: last 0x%04x now 0x%04x\n",
  808. lastrbc, (hi << 8) + lo);
  809. }
  810. lastrbc = (hi << 8) + lo;
  811. }
  812. if (!retries)
  813. brcmf_dbg(ERROR, "count never zeroed: last 0x%04x\n", lastrbc);
  814. else
  815. brcmf_dbg(INFO, "flush took %d iterations\n", 0xffff - retries);
  816. if (rtx) {
  817. bus->sdcnt.rxrtx++;
  818. err = w_sdreg32(bus, SMB_NAK,
  819. offsetof(struct sdpcmd_regs, tosbmailbox));
  820. bus->sdcnt.f1regdata++;
  821. if (err == 0)
  822. bus->rxskip = true;
  823. }
  824. /* Clear partial in any case */
  825. bus->cur_read.len = 0;
  826. /* If we can't reach the device, signal failure */
  827. if (err)
  828. bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
  829. }
  830. /* copy a buffer into a pkt buffer chain */
  831. static uint brcmf_sdbrcm_glom_from_buf(struct brcmf_sdio *bus, uint len)
  832. {
  833. uint n, ret = 0;
  834. struct sk_buff *p;
  835. u8 *buf;
  836. buf = bus->dataptr;
  837. /* copy the data */
  838. skb_queue_walk(&bus->glom, p) {
  839. n = min_t(uint, p->len, len);
  840. memcpy(p->data, buf, n);
  841. buf += n;
  842. len -= n;
  843. ret += n;
  844. if (!len)
  845. break;
  846. }
  847. return ret;
  848. }
  849. /* return total length of buffer chain */
  850. static uint brcmf_sdbrcm_glom_len(struct brcmf_sdio *bus)
  851. {
  852. struct sk_buff *p;
  853. uint total;
  854. total = 0;
  855. skb_queue_walk(&bus->glom, p)
  856. total += p->len;
  857. return total;
  858. }
  859. static void brcmf_sdbrcm_free_glom(struct brcmf_sdio *bus)
  860. {
  861. struct sk_buff *cur, *next;
  862. skb_queue_walk_safe(&bus->glom, cur, next) {
  863. skb_unlink(cur, &bus->glom);
  864. brcmu_pkt_buf_free_skb(cur);
  865. }
  866. }
  867. static bool brcmf_sdio_hdparser(struct brcmf_sdio *bus, u8 *header,
  868. struct brcmf_sdio_read *rd,
  869. enum brcmf_sdio_frmtype type)
  870. {
  871. u16 len, checksum;
  872. u8 rx_seq, fc, tx_seq_max;
  873. /*
  874. * 4 bytes hardware header (frame tag)
  875. * Byte 0~1: Frame length
  876. * Byte 2~3: Checksum, bit-wise inverse of frame length
  877. */
  878. len = get_unaligned_le16(header);
  879. checksum = get_unaligned_le16(header + sizeof(u16));
  880. /* All zero means no more to read */
  881. if (!(len | checksum)) {
  882. bus->rxpending = false;
  883. return false;
  884. }
  885. if ((u16)(~(len ^ checksum))) {
  886. brcmf_dbg(ERROR, "HW header checksum error\n");
  887. bus->sdcnt.rx_badhdr++;
  888. brcmf_sdbrcm_rxfail(bus, false, false);
  889. return false;
  890. }
  891. if (len < SDPCM_HDRLEN) {
  892. brcmf_dbg(ERROR, "HW header length error\n");
  893. return false;
  894. }
  895. if (type == BRCMF_SDIO_FT_SUPER &&
  896. (roundup(len, bus->blocksize) != rd->len)) {
  897. brcmf_dbg(ERROR, "HW superframe header length error\n");
  898. return false;
  899. }
  900. if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
  901. brcmf_dbg(ERROR, "HW subframe header length error\n");
  902. return false;
  903. }
  904. rd->len = len;
  905. /*
  906. * 8 bytes hardware header
  907. * Byte 0: Rx sequence number
  908. * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
  909. * Byte 2: Length of next data frame
  910. * Byte 3: Data offset
  911. * Byte 4: Flow control bits
  912. * Byte 5: Maximum Sequence number allow for Tx
  913. * Byte 6~7: Reserved
  914. */
  915. if (type == BRCMF_SDIO_FT_SUPER &&
  916. SDPCM_GLOMDESC(&header[SDPCM_FRAMETAG_LEN])) {
  917. brcmf_dbg(ERROR, "Glom descriptor found in superframe head\n");
  918. rd->len = 0;
  919. return false;
  920. }
  921. rx_seq = SDPCM_PACKET_SEQUENCE(&header[SDPCM_FRAMETAG_LEN]);
  922. rd->channel = SDPCM_PACKET_CHANNEL(&header[SDPCM_FRAMETAG_LEN]);
  923. if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
  924. type != BRCMF_SDIO_FT_SUPER) {
  925. brcmf_dbg(ERROR, "HW header length too long\n");
  926. bus->sdiodev->bus_if->dstats.rx_errors++;
  927. bus->sdcnt.rx_toolong++;
  928. brcmf_sdbrcm_rxfail(bus, false, false);
  929. rd->len = 0;
  930. return false;
  931. }
  932. if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
  933. brcmf_dbg(ERROR, "Wrong channel for superframe\n");
  934. rd->len = 0;
  935. return false;
  936. }
  937. if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
  938. rd->channel != SDPCM_EVENT_CHANNEL) {
  939. brcmf_dbg(ERROR, "Wrong channel for subframe\n");
  940. rd->len = 0;
  941. return false;
  942. }
  943. rd->dat_offset = SDPCM_DOFFSET_VALUE(&header[SDPCM_FRAMETAG_LEN]);
  944. if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
  945. brcmf_dbg(ERROR, "seq %d: bad data offset\n", rx_seq);
  946. bus->sdcnt.rx_badhdr++;
  947. brcmf_sdbrcm_rxfail(bus, false, false);
  948. rd->len = 0;
  949. return false;
  950. }
  951. if (rd->seq_num != rx_seq) {
  952. brcmf_dbg(ERROR, "seq %d: sequence number error, expect %d\n",
  953. rx_seq, rd->seq_num);
  954. bus->sdcnt.rx_badseq++;
  955. rd->seq_num = rx_seq;
  956. }
  957. /* no need to check the reset for subframe */
  958. if (type == BRCMF_SDIO_FT_SUB)
  959. return true;
  960. rd->len_nxtfrm = header[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
  961. if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
  962. /* only warm for NON glom packet */
  963. if (rd->channel != SDPCM_GLOM_CHANNEL)
  964. brcmf_dbg(ERROR, "seq %d: next length error\n", rx_seq);
  965. rd->len_nxtfrm = 0;
  966. }
  967. fc = SDPCM_FCMASK_VALUE(&header[SDPCM_FRAMETAG_LEN]);
  968. if (bus->flowcontrol != fc) {
  969. if (~bus->flowcontrol & fc)
  970. bus->sdcnt.fc_xoff++;
  971. if (bus->flowcontrol & ~fc)
  972. bus->sdcnt.fc_xon++;
  973. bus->sdcnt.fc_rcvd++;
  974. bus->flowcontrol = fc;
  975. }
  976. tx_seq_max = SDPCM_WINDOW_VALUE(&header[SDPCM_FRAMETAG_LEN]);
  977. if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
  978. brcmf_dbg(ERROR, "seq %d: max tx seq number error\n", rx_seq);
  979. tx_seq_max = bus->tx_seq + 2;
  980. }
  981. bus->tx_max = tx_seq_max;
  982. return true;
  983. }
  984. static u8 brcmf_sdbrcm_rxglom(struct brcmf_sdio *bus, u8 rxseq)
  985. {
  986. u16 dlen, totlen;
  987. u8 *dptr, num = 0;
  988. u16 sublen;
  989. struct sk_buff *pfirst, *pnext;
  990. int errcode;
  991. u8 doff, sfdoff;
  992. int ifidx = 0;
  993. bool usechain = bus->use_rxchain;
  994. struct brcmf_sdio_read rd_new;
  995. /* If packets, issue read(s) and send up packet chain */
  996. /* Return sequence numbers consumed? */
  997. brcmf_dbg(TRACE, "start: glomd %p glom %p\n",
  998. bus->glomd, skb_peek(&bus->glom));
  999. /* If there's a descriptor, generate the packet chain */
  1000. if (bus->glomd) {
  1001. pfirst = pnext = NULL;
  1002. dlen = (u16) (bus->glomd->len);
  1003. dptr = bus->glomd->data;
  1004. if (!dlen || (dlen & 1)) {
  1005. brcmf_dbg(ERROR, "bad glomd len(%d), ignore descriptor\n",
  1006. dlen);
  1007. dlen = 0;
  1008. }
  1009. for (totlen = num = 0; dlen; num++) {
  1010. /* Get (and move past) next length */
  1011. sublen = get_unaligned_le16(dptr);
  1012. dlen -= sizeof(u16);
  1013. dptr += sizeof(u16);
  1014. if ((sublen < SDPCM_HDRLEN) ||
  1015. ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
  1016. brcmf_dbg(ERROR, "descriptor len %d bad: %d\n",
  1017. num, sublen);
  1018. pnext = NULL;
  1019. break;
  1020. }
  1021. if (sublen % BRCMF_SDALIGN) {
  1022. brcmf_dbg(ERROR, "sublen %d not multiple of %d\n",
  1023. sublen, BRCMF_SDALIGN);
  1024. usechain = false;
  1025. }
  1026. totlen += sublen;
  1027. /* For last frame, adjust read len so total
  1028. is a block multiple */
  1029. if (!dlen) {
  1030. sublen +=
  1031. (roundup(totlen, bus->blocksize) - totlen);
  1032. totlen = roundup(totlen, bus->blocksize);
  1033. }
  1034. /* Allocate/chain packet for next subframe */
  1035. pnext = brcmu_pkt_buf_get_skb(sublen + BRCMF_SDALIGN);
  1036. if (pnext == NULL) {
  1037. brcmf_dbg(ERROR, "bcm_pkt_buf_get_skb failed, num %d len %d\n",
  1038. num, sublen);
  1039. break;
  1040. }
  1041. skb_queue_tail(&bus->glom, pnext);
  1042. /* Adhere to start alignment requirements */
  1043. pkt_align(pnext, sublen, BRCMF_SDALIGN);
  1044. }
  1045. /* If all allocations succeeded, save packet chain
  1046. in bus structure */
  1047. if (pnext) {
  1048. brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
  1049. totlen, num);
  1050. if (BRCMF_GLOM_ON() && bus->cur_read.len &&
  1051. totlen != bus->cur_read.len) {
  1052. brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
  1053. bus->cur_read.len, totlen, rxseq);
  1054. }
  1055. pfirst = pnext = NULL;
  1056. } else {
  1057. brcmf_sdbrcm_free_glom(bus);
  1058. num = 0;
  1059. }
  1060. /* Done with descriptor packet */
  1061. brcmu_pkt_buf_free_skb(bus->glomd);
  1062. bus->glomd = NULL;
  1063. bus->cur_read.len = 0;
  1064. }
  1065. /* Ok -- either we just generated a packet chain,
  1066. or had one from before */
  1067. if (!skb_queue_empty(&bus->glom)) {
  1068. if (BRCMF_GLOM_ON()) {
  1069. brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
  1070. skb_queue_walk(&bus->glom, pnext) {
  1071. brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
  1072. pnext, (u8 *) (pnext->data),
  1073. pnext->len, pnext->len);
  1074. }
  1075. }
  1076. pfirst = skb_peek(&bus->glom);
  1077. dlen = (u16) brcmf_sdbrcm_glom_len(bus);
  1078. /* Do an SDIO read for the superframe. Configurable iovar to
  1079. * read directly into the chained packet, or allocate a large
  1080. * packet and and copy into the chain.
  1081. */
  1082. if (usechain) {
  1083. errcode = brcmf_sdcard_recv_chain(bus->sdiodev,
  1084. bus->sdiodev->sbwad,
  1085. SDIO_FUNC_2, F2SYNC, &bus->glom);
  1086. } else if (bus->dataptr) {
  1087. errcode = brcmf_sdcard_recv_buf(bus->sdiodev,
  1088. bus->sdiodev->sbwad,
  1089. SDIO_FUNC_2, F2SYNC,
  1090. bus->dataptr, dlen);
  1091. sublen = (u16) brcmf_sdbrcm_glom_from_buf(bus, dlen);
  1092. if (sublen != dlen) {
  1093. brcmf_dbg(ERROR, "FAILED TO COPY, dlen %d sublen %d\n",
  1094. dlen, sublen);
  1095. errcode = -1;
  1096. }
  1097. pnext = NULL;
  1098. } else {
  1099. brcmf_dbg(ERROR, "COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
  1100. dlen);
  1101. errcode = -1;
  1102. }
  1103. bus->sdcnt.f2rxdata++;
  1104. /* On failure, kill the superframe, allow a couple retries */
  1105. if (errcode < 0) {
  1106. brcmf_dbg(ERROR, "glom read of %d bytes failed: %d\n",
  1107. dlen, errcode);
  1108. bus->sdiodev->bus_if->dstats.rx_errors++;
  1109. if (bus->glomerr++ < 3) {
  1110. brcmf_sdbrcm_rxfail(bus, true, true);
  1111. } else {
  1112. bus->glomerr = 0;
  1113. brcmf_sdbrcm_rxfail(bus, true, false);
  1114. bus->sdcnt.rxglomfail++;
  1115. brcmf_sdbrcm_free_glom(bus);
  1116. }
  1117. return 0;
  1118. }
  1119. brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
  1120. pfirst->data, min_t(int, pfirst->len, 48),
  1121. "SUPERFRAME:\n");
  1122. rd_new.seq_num = rxseq;
  1123. rd_new.len = dlen;
  1124. errcode = -!brcmf_sdio_hdparser(bus, pfirst->data, &rd_new,
  1125. BRCMF_SDIO_FT_SUPER);
  1126. bus->cur_read.len = rd_new.len_nxtfrm << 4;
  1127. /* Remove superframe header, remember offset */
  1128. skb_pull(pfirst, rd_new.dat_offset);
  1129. sfdoff = rd_new.dat_offset;
  1130. num = 0;
  1131. /* Validate all the subframe headers */
  1132. skb_queue_walk(&bus->glom, pnext) {
  1133. /* leave when invalid subframe is found */
  1134. if (errcode)
  1135. break;
  1136. rd_new.len = pnext->len;
  1137. rd_new.seq_num = rxseq++;
  1138. errcode = -!brcmf_sdio_hdparser(bus, pnext->data,
  1139. &rd_new,
  1140. BRCMF_SDIO_FT_SUB);
  1141. brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
  1142. pnext->data, 32, "subframe:\n");
  1143. num++;
  1144. }
  1145. if (errcode) {
  1146. /* Terminate frame on error, request
  1147. a couple retries */
  1148. if (bus->glomerr++ < 3) {
  1149. /* Restore superframe header space */
  1150. skb_push(pfirst, sfdoff);
  1151. brcmf_sdbrcm_rxfail(bus, true, true);
  1152. } else {
  1153. bus->glomerr = 0;
  1154. brcmf_sdbrcm_rxfail(bus, true, false);
  1155. bus->sdcnt.rxglomfail++;
  1156. brcmf_sdbrcm_free_glom(bus);
  1157. }
  1158. bus->cur_read.len = 0;
  1159. return 0;
  1160. }
  1161. /* Basic SD framing looks ok - process each packet (header) */
  1162. skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
  1163. dptr = (u8 *) (pfirst->data);
  1164. sublen = get_unaligned_le16(dptr);
  1165. doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
  1166. brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
  1167. dptr, pfirst->len,
  1168. "Rx Subframe Data:\n");
  1169. __skb_trim(pfirst, sublen);
  1170. skb_pull(pfirst, doff);
  1171. if (pfirst->len == 0) {
  1172. skb_unlink(pfirst, &bus->glom);
  1173. brcmu_pkt_buf_free_skb(pfirst);
  1174. continue;
  1175. } else if (brcmf_proto_hdrpull(bus->sdiodev->dev,
  1176. &ifidx, pfirst) != 0) {
  1177. brcmf_dbg(ERROR, "rx protocol error\n");
  1178. bus->sdiodev->bus_if->dstats.rx_errors++;
  1179. skb_unlink(pfirst, &bus->glom);
  1180. brcmu_pkt_buf_free_skb(pfirst);
  1181. continue;
  1182. }
  1183. brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
  1184. pfirst->data,
  1185. min_t(int, pfirst->len, 32),
  1186. "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
  1187. bus->glom.qlen, pfirst, pfirst->data,
  1188. pfirst->len, pfirst->next,
  1189. pfirst->prev);
  1190. }
  1191. /* sent any remaining packets up */
  1192. if (bus->glom.qlen) {
  1193. up(&bus->sdsem);
  1194. brcmf_rx_frame(bus->sdiodev->dev, ifidx, &bus->glom);
  1195. down(&bus->sdsem);
  1196. }
  1197. bus->sdcnt.rxglomframes++;
  1198. bus->sdcnt.rxglompkts += bus->glom.qlen;
  1199. }
  1200. return num;
  1201. }
  1202. static int brcmf_sdbrcm_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
  1203. bool *pending)
  1204. {
  1205. DECLARE_WAITQUEUE(wait, current);
  1206. int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
  1207. /* Wait until control frame is available */
  1208. add_wait_queue(&bus->dcmd_resp_wait, &wait);
  1209. set_current_state(TASK_INTERRUPTIBLE);
  1210. while (!(*condition) && (!signal_pending(current) && timeout))
  1211. timeout = schedule_timeout(timeout);
  1212. if (signal_pending(current))
  1213. *pending = true;
  1214. set_current_state(TASK_RUNNING);
  1215. remove_wait_queue(&bus->dcmd_resp_wait, &wait);
  1216. return timeout;
  1217. }
  1218. static int brcmf_sdbrcm_dcmd_resp_wake(struct brcmf_sdio *bus)
  1219. {
  1220. if (waitqueue_active(&bus->dcmd_resp_wait))
  1221. wake_up_interruptible(&bus->dcmd_resp_wait);
  1222. return 0;
  1223. }
  1224. static void
  1225. brcmf_sdbrcm_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
  1226. {
  1227. uint rdlen, pad;
  1228. int sdret;
  1229. brcmf_dbg(TRACE, "Enter\n");
  1230. /* Set rxctl for frame (w/optional alignment) */
  1231. bus->rxctl = bus->rxbuf;
  1232. bus->rxctl += BRCMF_FIRSTREAD;
  1233. pad = ((unsigned long)bus->rxctl % BRCMF_SDALIGN);
  1234. if (pad)
  1235. bus->rxctl += (BRCMF_SDALIGN - pad);
  1236. bus->rxctl -= BRCMF_FIRSTREAD;
  1237. /* Copy the already-read portion over */
  1238. memcpy(bus->rxctl, hdr, BRCMF_FIRSTREAD);
  1239. if (len <= BRCMF_FIRSTREAD)
  1240. goto gotpkt;
  1241. /* Raise rdlen to next SDIO block to avoid tail command */
  1242. rdlen = len - BRCMF_FIRSTREAD;
  1243. if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
  1244. pad = bus->blocksize - (rdlen % bus->blocksize);
  1245. if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
  1246. ((len + pad) < bus->sdiodev->bus_if->maxctl))
  1247. rdlen += pad;
  1248. } else if (rdlen % BRCMF_SDALIGN) {
  1249. rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
  1250. }
  1251. /* Satisfy length-alignment requirements */
  1252. if (rdlen & (ALIGNMENT - 1))
  1253. rdlen = roundup(rdlen, ALIGNMENT);
  1254. /* Drop if the read is too big or it exceeds our maximum */
  1255. if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
  1256. brcmf_dbg(ERROR, "%d-byte control read exceeds %d-byte buffer\n",
  1257. rdlen, bus->sdiodev->bus_if->maxctl);
  1258. bus->sdiodev->bus_if->dstats.rx_errors++;
  1259. brcmf_sdbrcm_rxfail(bus, false, false);
  1260. goto done;
  1261. }
  1262. if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
  1263. brcmf_dbg(ERROR, "%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
  1264. len, len - doff, bus->sdiodev->bus_if->maxctl);
  1265. bus->sdiodev->bus_if->dstats.rx_errors++;
  1266. bus->sdcnt.rx_toolong++;
  1267. brcmf_sdbrcm_rxfail(bus, false, false);
  1268. goto done;
  1269. }
  1270. /* Read remainder of frame body into the rxctl buffer */
  1271. sdret = brcmf_sdcard_recv_buf(bus->sdiodev,
  1272. bus->sdiodev->sbwad,
  1273. SDIO_FUNC_2,
  1274. F2SYNC, (bus->rxctl + BRCMF_FIRSTREAD), rdlen);
  1275. bus->sdcnt.f2rxdata++;
  1276. /* Control frame failures need retransmission */
  1277. if (sdret < 0) {
  1278. brcmf_dbg(ERROR, "read %d control bytes failed: %d\n",
  1279. rdlen, sdret);
  1280. bus->sdcnt.rxc_errors++;
  1281. brcmf_sdbrcm_rxfail(bus, true, true);
  1282. goto done;
  1283. }
  1284. gotpkt:
  1285. brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
  1286. bus->rxctl, len, "RxCtrl:\n");
  1287. /* Point to valid data and indicate its length */
  1288. bus->rxctl += doff;
  1289. bus->rxlen = len - doff;
  1290. done:
  1291. /* Awake any waiters */
  1292. brcmf_sdbrcm_dcmd_resp_wake(bus);
  1293. }
  1294. /* Pad read to blocksize for efficiency */
  1295. static void brcmf_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
  1296. {
  1297. if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
  1298. *pad = bus->blocksize - (*rdlen % bus->blocksize);
  1299. if (*pad <= bus->roundup && *pad < bus->blocksize &&
  1300. *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
  1301. *rdlen += *pad;
  1302. } else if (*rdlen % BRCMF_SDALIGN) {
  1303. *rdlen += BRCMF_SDALIGN - (*rdlen % BRCMF_SDALIGN);
  1304. }
  1305. }
  1306. static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
  1307. {
  1308. struct sk_buff *pkt; /* Packet for event or data frames */
  1309. u16 pad; /* Number of pad bytes to read */
  1310. uint rxleft = 0; /* Remaining number of frames allowed */
  1311. int sdret; /* Return code from calls */
  1312. int ifidx = 0;
  1313. uint rxcount = 0; /* Total frames read */
  1314. struct brcmf_sdio_read *rd = &bus->cur_read, rd_new;
  1315. u8 head_read = 0;
  1316. brcmf_dbg(TRACE, "Enter\n");
  1317. /* Not finished unless we encounter no more frames indication */
  1318. bus->rxpending = true;
  1319. for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
  1320. !bus->rxskip && rxleft &&
  1321. bus->sdiodev->bus_if->state != BRCMF_BUS_DOWN;
  1322. rd->seq_num++, rxleft--) {
  1323. /* Handle glomming separately */
  1324. if (bus->glomd || !skb_queue_empty(&bus->glom)) {
  1325. u8 cnt;
  1326. brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
  1327. bus->glomd, skb_peek(&bus->glom));
  1328. cnt = brcmf_sdbrcm_rxglom(bus, rd->seq_num);
  1329. brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
  1330. rd->seq_num += cnt - 1;
  1331. rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
  1332. continue;
  1333. }
  1334. rd->len_left = rd->len;
  1335. /* read header first for unknow frame length */
  1336. if (!rd->len) {
  1337. sdret = brcmf_sdcard_recv_buf(bus->sdiodev,
  1338. bus->sdiodev->sbwad,
  1339. SDIO_FUNC_2, F2SYNC,
  1340. bus->rxhdr,
  1341. BRCMF_FIRSTREAD);
  1342. bus->sdcnt.f2rxhdrs++;
  1343. if (sdret < 0) {
  1344. brcmf_dbg(ERROR, "RXHEADER FAILED: %d\n",
  1345. sdret);
  1346. bus->sdcnt.rx_hdrfail++;
  1347. brcmf_sdbrcm_rxfail(bus, true, true);
  1348. continue;
  1349. }
  1350. brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
  1351. bus->rxhdr, SDPCM_HDRLEN,
  1352. "RxHdr:\n");
  1353. if (!brcmf_sdio_hdparser(bus, bus->rxhdr, rd,
  1354. BRCMF_SDIO_FT_NORMAL)) {
  1355. if (!bus->rxpending)
  1356. break;
  1357. else
  1358. continue;
  1359. }
  1360. if (rd->channel == SDPCM_CONTROL_CHANNEL) {
  1361. brcmf_sdbrcm_read_control(bus, bus->rxhdr,
  1362. rd->len,
  1363. rd->dat_offset);
  1364. /* prepare the descriptor for the next read */
  1365. rd->len = rd->len_nxtfrm << 4;
  1366. rd->len_nxtfrm = 0;
  1367. /* treat all packet as event if we don't know */
  1368. rd->channel = SDPCM_EVENT_CHANNEL;
  1369. continue;
  1370. }
  1371. rd->len_left = rd->len > BRCMF_FIRSTREAD ?
  1372. rd->len - BRCMF_FIRSTREAD : 0;
  1373. head_read = BRCMF_FIRSTREAD;
  1374. }
  1375. brcmf_pad(bus, &pad, &rd->len_left);
  1376. pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
  1377. BRCMF_SDALIGN);
  1378. if (!pkt) {
  1379. /* Give up on data, request rtx of events */
  1380. brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed\n");
  1381. bus->sdiodev->bus_if->dstats.rx_dropped++;
  1382. brcmf_sdbrcm_rxfail(bus, false,
  1383. RETRYCHAN(rd->channel));
  1384. continue;
  1385. }
  1386. skb_pull(pkt, head_read);
  1387. pkt_align(pkt, rd->len_left, BRCMF_SDALIGN);
  1388. sdret = brcmf_sdcard_recv_pkt(bus->sdiodev, bus->sdiodev->sbwad,
  1389. SDIO_FUNC_2, F2SYNC, pkt);
  1390. bus->sdcnt.f2rxdata++;
  1391. if (sdret < 0) {
  1392. brcmf_dbg(ERROR, "read %d bytes from channel %d failed: %d\n",
  1393. rd->len, rd->channel, sdret);
  1394. brcmu_pkt_buf_free_skb(pkt);
  1395. bus->sdiodev->bus_if->dstats.rx_errors++;
  1396. brcmf_sdbrcm_rxfail(bus, true,
  1397. RETRYCHAN(rd->channel));
  1398. continue;
  1399. }
  1400. if (head_read) {
  1401. skb_push(pkt, head_read);
  1402. memcpy(pkt->data, bus->rxhdr, head_read);
  1403. head_read = 0;
  1404. } else {
  1405. memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
  1406. rd_new.seq_num = rd->seq_num;
  1407. if (!brcmf_sdio_hdparser(bus, bus->rxhdr, &rd_new,
  1408. BRCMF_SDIO_FT_NORMAL)) {
  1409. rd->len = 0;
  1410. brcmu_pkt_buf_free_skb(pkt);
  1411. }
  1412. bus->sdcnt.rx_readahead_cnt++;
  1413. if (rd->len != roundup(rd_new.len, 16)) {
  1414. brcmf_dbg(ERROR, "frame length mismatch:read %d, should be %d\n",
  1415. rd->len,
  1416. roundup(rd_new.len, 16) >> 4);
  1417. rd->len = 0;
  1418. brcmf_sdbrcm_rxfail(bus, true, true);
  1419. brcmu_pkt_buf_free_skb(pkt);
  1420. continue;
  1421. }
  1422. rd->len_nxtfrm = rd_new.len_nxtfrm;
  1423. rd->channel = rd_new.channel;
  1424. rd->dat_offset = rd_new.dat_offset;
  1425. brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
  1426. BRCMF_DATA_ON()) &&
  1427. BRCMF_HDRS_ON(),
  1428. bus->rxhdr, SDPCM_HDRLEN,
  1429. "RxHdr:\n");
  1430. if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
  1431. brcmf_dbg(ERROR, "readahead on control packet %d?\n",
  1432. rd_new.seq_num);
  1433. /* Force retry w/normal header read */
  1434. rd->len = 0;
  1435. brcmf_sdbrcm_rxfail(bus, false, true);
  1436. brcmu_pkt_buf_free_skb(pkt);
  1437. continue;
  1438. }
  1439. }
  1440. brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
  1441. pkt->data, rd->len, "Rx Data:\n");
  1442. /* Save superframe descriptor and allocate packet frame */
  1443. if (rd->channel == SDPCM_GLOM_CHANNEL) {
  1444. if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
  1445. brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
  1446. rd->len);
  1447. brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
  1448. pkt->data, rd->len,
  1449. "Glom Data:\n");
  1450. __skb_trim(pkt, rd->len);
  1451. skb_pull(pkt, SDPCM_HDRLEN);
  1452. bus->glomd = pkt;
  1453. } else {
  1454. brcmf_dbg(ERROR, "%s: glom superframe w/o "
  1455. "descriptor!\n", __func__);
  1456. brcmf_sdbrcm_rxfail(bus, false, false);
  1457. }
  1458. /* prepare the descriptor for the next read */
  1459. rd->len = rd->len_nxtfrm << 4;
  1460. rd->len_nxtfrm = 0;
  1461. /* treat all packet as event if we don't know */
  1462. rd->channel = SDPCM_EVENT_CHANNEL;
  1463. continue;
  1464. }
  1465. /* Fill in packet len and prio, deliver upward */
  1466. __skb_trim(pkt, rd->len);
  1467. skb_pull(pkt, rd->dat_offset);
  1468. /* prepare the descriptor for the next read */
  1469. rd->len = rd->len_nxtfrm << 4;
  1470. rd->len_nxtfrm = 0;
  1471. /* treat all packet as event if we don't know */
  1472. rd->channel = SDPCM_EVENT_CHANNEL;
  1473. if (pkt->len == 0) {
  1474. brcmu_pkt_buf_free_skb(pkt);
  1475. continue;
  1476. } else if (brcmf_proto_hdrpull(bus->sdiodev->dev, &ifidx,
  1477. pkt) != 0) {
  1478. brcmf_dbg(ERROR, "rx protocol error\n");
  1479. brcmu_pkt_buf_free_skb(pkt);
  1480. bus->sdiodev->bus_if->dstats.rx_errors++;
  1481. continue;
  1482. }
  1483. /* Unlock during rx call */
  1484. up(&bus->sdsem);
  1485. brcmf_rx_packet(bus->sdiodev->dev, ifidx, pkt);
  1486. down(&bus->sdsem);
  1487. }
  1488. rxcount = maxframes - rxleft;
  1489. /* Message if we hit the limit */
  1490. if (!rxleft)
  1491. brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
  1492. else
  1493. brcmf_dbg(DATA, "processed %d frames\n", rxcount);
  1494. /* Back off rxseq if awaiting rtx, update rx_seq */
  1495. if (bus->rxskip)
  1496. rd->seq_num--;
  1497. bus->rx_seq = rd->seq_num;
  1498. return rxcount;
  1499. }
  1500. static void
  1501. brcmf_sdbrcm_wait_for_event(struct brcmf_sdio *bus, bool *lockvar)
  1502. {
  1503. up(&bus->sdsem);
  1504. wait_event_interruptible_timeout(bus->ctrl_wait, !*lockvar, HZ * 2);
  1505. down(&bus->sdsem);
  1506. return;
  1507. }
  1508. static void
  1509. brcmf_sdbrcm_wait_event_wakeup(struct brcmf_sdio *bus)
  1510. {
  1511. if (waitqueue_active(&bus->ctrl_wait))
  1512. wake_up_interruptible(&bus->ctrl_wait);
  1513. return;
  1514. }
  1515. /* Writes a HW/SW header into the packet and sends it. */
  1516. /* Assumes: (a) header space already there, (b) caller holds lock */
  1517. static int brcmf_sdbrcm_txpkt(struct brcmf_sdio *bus, struct sk_buff *pkt,
  1518. uint chan, bool free_pkt)
  1519. {
  1520. int ret;
  1521. u8 *frame;
  1522. u16 len, pad = 0;
  1523. u32 swheader;
  1524. struct sk_buff *new;
  1525. int i;
  1526. brcmf_dbg(TRACE, "Enter\n");
  1527. frame = (u8 *) (pkt->data);
  1528. /* Add alignment padding, allocate new packet if needed */
  1529. pad = ((unsigned long)frame % BRCMF_SDALIGN);
  1530. if (pad) {
  1531. if (skb_headroom(pkt) < pad) {
  1532. brcmf_dbg(INFO, "insufficient headroom %d for %d pad\n",
  1533. skb_headroom(pkt), pad);
  1534. bus->sdiodev->bus_if->tx_realloc++;
  1535. new = brcmu_pkt_buf_get_skb(pkt->len + BRCMF_SDALIGN);
  1536. if (!new) {
  1537. brcmf_dbg(ERROR, "couldn't allocate new %d-byte packet\n",
  1538. pkt->len + BRCMF_SDALIGN);
  1539. ret = -ENOMEM;
  1540. goto done;
  1541. }
  1542. pkt_align(new, pkt->len, BRCMF_SDALIGN);
  1543. memcpy(new->data, pkt->data, pkt->len);
  1544. if (free_pkt)
  1545. brcmu_pkt_buf_free_skb(pkt);
  1546. /* free the pkt if canned one is not used */
  1547. free_pkt = true;
  1548. pkt = new;
  1549. frame = (u8 *) (pkt->data);
  1550. /* precondition: (frame % BRCMF_SDALIGN) == 0) */
  1551. pad = 0;
  1552. } else {
  1553. skb_push(pkt, pad);
  1554. frame = (u8 *) (pkt->data);
  1555. /* precondition: pad + SDPCM_HDRLEN <= pkt->len */
  1556. memset(frame, 0, pad + SDPCM_HDRLEN);
  1557. }
  1558. }
  1559. /* precondition: pad < BRCMF_SDALIGN */
  1560. /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
  1561. len = (u16) (pkt->len);
  1562. *(__le16 *) frame = cpu_to_le16(len);
  1563. *(((__le16 *) frame) + 1) = cpu_to_le16(~len);
  1564. /* Software tag: channel, sequence number, data offset */
  1565. swheader =
  1566. ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
  1567. (((pad +
  1568. SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
  1569. put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
  1570. put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
  1571. #ifdef DEBUG
  1572. tx_packets[pkt->priority]++;
  1573. #endif
  1574. brcmf_dbg_hex_dump(BRCMF_BYTES_ON() &&
  1575. ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
  1576. (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)),
  1577. frame, len, "Tx Frame:\n");
  1578. brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
  1579. ((BRCMF_CTL_ON() &&
  1580. chan == SDPCM_CONTROL_CHANNEL) ||
  1581. (BRCMF_DATA_ON() &&
  1582. chan != SDPCM_CONTROL_CHANNEL))) &&
  1583. BRCMF_HDRS_ON(),
  1584. frame, min_t(u16, len, 16), "TxHdr:\n");
  1585. /* Raise len to next SDIO block to eliminate tail command */
  1586. if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
  1587. u16 pad = bus->blocksize - (len % bus->blocksize);
  1588. if ((pad <= bus->roundup) && (pad < bus->blocksize))
  1589. len += pad;
  1590. } else if (len % BRCMF_SDALIGN) {
  1591. len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
  1592. }
  1593. /* Some controllers have trouble with odd bytes -- round to even */
  1594. if (len & (ALIGNMENT - 1))
  1595. len = roundup(len, ALIGNMENT);
  1596. ret = brcmf_sdcard_send_pkt(bus->sdiodev, bus->sdiodev->sbwad,
  1597. SDIO_FUNC_2, F2SYNC, pkt);
  1598. bus->sdcnt.f2txdata++;
  1599. if (ret < 0) {
  1600. /* On failure, abort the command and terminate the frame */
  1601. brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
  1602. ret);
  1603. bus->sdcnt.tx_sderrs++;
  1604. brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
  1605. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
  1606. SFC_WF_TERM, NULL);
  1607. bus->sdcnt.f1regdata++;
  1608. for (i = 0; i < 3; i++) {
  1609. u8 hi, lo;
  1610. hi = brcmf_sdio_regrb(bus->sdiodev,
  1611. SBSDIO_FUNC1_WFRAMEBCHI, NULL);
  1612. lo = brcmf_sdio_regrb(bus->sdiodev,
  1613. SBSDIO_FUNC1_WFRAMEBCLO, NULL);
  1614. bus->sdcnt.f1regdata += 2;
  1615. if ((hi == 0) && (lo == 0))
  1616. break;
  1617. }
  1618. }
  1619. if (ret == 0)
  1620. bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
  1621. done:
  1622. /* restore pkt buffer pointer before calling tx complete routine */
  1623. skb_pull(pkt, SDPCM_HDRLEN + pad);
  1624. up(&bus->sdsem);
  1625. brcmf_txcomplete(bus->sdiodev->dev, pkt, ret != 0);
  1626. down(&bus->sdsem);
  1627. if (free_pkt)
  1628. brcmu_pkt_buf_free_skb(pkt);
  1629. return ret;
  1630. }
  1631. static uint brcmf_sdbrcm_sendfromq(struct brcmf_sdio *bus, uint maxframes)
  1632. {
  1633. struct sk_buff *pkt;
  1634. u32 intstatus = 0;
  1635. int ret = 0, prec_out;
  1636. uint cnt = 0;
  1637. uint datalen;
  1638. u8 tx_prec_map;
  1639. brcmf_dbg(TRACE, "Enter\n");
  1640. tx_prec_map = ~bus->flowcontrol;
  1641. /* Send frames until the limit or some other event */
  1642. for (cnt = 0; (cnt < maxframes) && data_ok(bus); cnt++) {
  1643. spin_lock_bh(&bus->txqlock);
  1644. pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
  1645. if (pkt == NULL) {
  1646. spin_unlock_bh(&bus->txqlock);
  1647. break;
  1648. }
  1649. spin_unlock_bh(&bus->txqlock);
  1650. datalen = pkt->len - SDPCM_HDRLEN;
  1651. ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
  1652. if (ret)
  1653. bus->sdiodev->bus_if->dstats.tx_errors++;
  1654. else
  1655. bus->sdiodev->bus_if->dstats.tx_bytes += datalen;
  1656. /* In poll mode, need to check for other events */
  1657. if (!bus->intr && cnt) {
  1658. /* Check device status, signal pending interrupt */
  1659. ret = r_sdreg32(bus, &intstatus,
  1660. offsetof(struct sdpcmd_regs,
  1661. intstatus));
  1662. bus->sdcnt.f2txdata++;
  1663. if (ret != 0)
  1664. break;
  1665. if (intstatus & bus->hostintmask)
  1666. atomic_set(&bus->ipend, 1);
  1667. }
  1668. }
  1669. /* Deflow-control stack if needed */
  1670. if (bus->sdiodev->bus_if->drvr_up &&
  1671. (bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
  1672. bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
  1673. bus->txoff = false;
  1674. brcmf_txflowblock(bus->sdiodev->dev, false);
  1675. }
  1676. return cnt;
  1677. }
  1678. static void brcmf_sdbrcm_bus_stop(struct device *dev)
  1679. {
  1680. u32 local_hostintmask;
  1681. u8 saveclk;
  1682. int err;
  1683. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  1684. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  1685. struct brcmf_sdio *bus = sdiodev->bus;
  1686. brcmf_dbg(TRACE, "Enter\n");
  1687. if (bus->watchdog_tsk) {
  1688. send_sig(SIGTERM, bus->watchdog_tsk, 1);
  1689. kthread_stop(bus->watchdog_tsk);
  1690. bus->watchdog_tsk = NULL;
  1691. }
  1692. down(&bus->sdsem);
  1693. /* Enable clock for device interrupts */
  1694. brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
  1695. /* Disable and clear interrupts at the chip level also */
  1696. w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask));
  1697. local_hostintmask = bus->hostintmask;
  1698. bus->hostintmask = 0;
  1699. /* Change our idea of bus state */
  1700. bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
  1701. /* Force clocks on backplane to be sure F2 interrupt propagates */
  1702. saveclk = brcmf_sdio_regrb(bus->sdiodev,
  1703. SBSDIO_FUNC1_CHIPCLKCSR, &err);
  1704. if (!err) {
  1705. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  1706. (saveclk | SBSDIO_FORCE_HT), &err);
  1707. }
  1708. if (err)
  1709. brcmf_dbg(ERROR, "Failed to force clock for F2: err %d\n", err);
  1710. /* Turn off the bus (F2), free any pending packets */
  1711. brcmf_dbg(INTR, "disable SDIO interrupts\n");
  1712. brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, SDIO_FUNC_ENABLE_1,
  1713. NULL);
  1714. /* Clear any pending interrupts now that F2 is disabled */
  1715. w_sdreg32(bus, local_hostintmask,
  1716. offsetof(struct sdpcmd_regs, intstatus));
  1717. /* Turn off the backplane clock (only) */
  1718. brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
  1719. /* Clear the data packet queues */
  1720. brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
  1721. /* Clear any held glomming stuff */
  1722. if (bus->glomd)
  1723. brcmu_pkt_buf_free_skb(bus->glomd);
  1724. brcmf_sdbrcm_free_glom(bus);
  1725. /* Clear rx control and wake any waiters */
  1726. bus->rxlen = 0;
  1727. brcmf_sdbrcm_dcmd_resp_wake(bus);
  1728. /* Reset some F2 state stuff */
  1729. bus->rxskip = false;
  1730. bus->tx_seq = bus->rx_seq = 0;
  1731. up(&bus->sdsem);
  1732. }
  1733. #ifdef CONFIG_BRCMFMAC_SDIO_OOB
  1734. static inline void brcmf_sdbrcm_clrintr(struct brcmf_sdio *bus)
  1735. {
  1736. unsigned long flags;
  1737. spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
  1738. if (!bus->sdiodev->irq_en && !atomic_read(&bus->ipend)) {
  1739. enable_irq(bus->sdiodev->irq);
  1740. bus->sdiodev->irq_en = true;
  1741. }
  1742. spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
  1743. }
  1744. #else
  1745. static inline void brcmf_sdbrcm_clrintr(struct brcmf_sdio *bus)
  1746. {
  1747. }
  1748. #endif /* CONFIG_BRCMFMAC_SDIO_OOB */
  1749. static inline void brcmf_sdbrcm_adddpctsk(struct brcmf_sdio *bus)
  1750. {
  1751. struct list_head *new_hd;
  1752. unsigned long flags;
  1753. if (in_interrupt())
  1754. new_hd = kzalloc(sizeof(struct list_head), GFP_ATOMIC);
  1755. else
  1756. new_hd = kzalloc(sizeof(struct list_head), GFP_KERNEL);
  1757. if (new_hd == NULL)
  1758. return;
  1759. spin_lock_irqsave(&bus->dpc_tl_lock, flags);
  1760. list_add_tail(new_hd, &bus->dpc_tsklst);
  1761. spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
  1762. }
  1763. static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
  1764. {
  1765. u8 idx;
  1766. u32 addr;
  1767. unsigned long val;
  1768. int n, ret;
  1769. idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
  1770. addr = bus->ci->c_inf[idx].base +
  1771. offsetof(struct sdpcmd_regs, intstatus);
  1772. ret = brcmf_sdio_regrw_helper(bus->sdiodev, addr, &val, false);
  1773. bus->sdcnt.f1regdata++;
  1774. if (ret != 0)
  1775. val = 0;
  1776. val &= bus->hostintmask;
  1777. atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
  1778. /* Clear interrupts */
  1779. if (val) {
  1780. ret = brcmf_sdio_regrw_helper(bus->sdiodev, addr, &val, true);
  1781. bus->sdcnt.f1regdata++;
  1782. }
  1783. if (ret) {
  1784. atomic_set(&bus->intstatus, 0);
  1785. } else if (val) {
  1786. for_each_set_bit(n, &val, 32)
  1787. set_bit(n, (unsigned long *)&bus->intstatus.counter);
  1788. }
  1789. return ret;
  1790. }
  1791. static void brcmf_sdbrcm_dpc(struct brcmf_sdio *bus)
  1792. {
  1793. u32 newstatus = 0;
  1794. unsigned long intstatus;
  1795. uint rxlimit = bus->rxbound; /* Rx frames to read before resched */
  1796. uint txlimit = bus->txbound; /* Tx frames to send before resched */
  1797. uint framecnt = 0; /* Temporary counter of tx/rx frames */
  1798. int err = 0, n;
  1799. brcmf_dbg(TRACE, "Enter\n");
  1800. down(&bus->sdsem);
  1801. /* If waiting for HTAVAIL, check status */
  1802. if (bus->clkstate == CLK_PENDING) {
  1803. u8 clkctl, devctl = 0;
  1804. #ifdef DEBUG
  1805. /* Check for inconsistent device control */
  1806. devctl = brcmf_sdio_regrb(bus->sdiodev,
  1807. SBSDIO_DEVICE_CTL, &err);
  1808. if (err) {
  1809. brcmf_dbg(ERROR, "error reading DEVCTL: %d\n", err);
  1810. bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
  1811. }
  1812. #endif /* DEBUG */
  1813. /* Read CSR, if clock on switch to AVAIL, else ignore */
  1814. clkctl = brcmf_sdio_regrb(bus->sdiodev,
  1815. SBSDIO_FUNC1_CHIPCLKCSR, &err);
  1816. if (err) {
  1817. brcmf_dbg(ERROR, "error reading CSR: %d\n",
  1818. err);
  1819. bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
  1820. }
  1821. brcmf_dbg(INFO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
  1822. devctl, clkctl);
  1823. if (SBSDIO_HTAV(clkctl)) {
  1824. devctl = brcmf_sdio_regrb(bus->sdiodev,
  1825. SBSDIO_DEVICE_CTL, &err);
  1826. if (err) {
  1827. brcmf_dbg(ERROR, "error reading DEVCTL: %d\n",
  1828. err);
  1829. bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
  1830. }
  1831. devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
  1832. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
  1833. devctl, &err);
  1834. if (err) {
  1835. brcmf_dbg(ERROR, "error writing DEVCTL: %d\n",
  1836. err);
  1837. bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
  1838. }
  1839. bus->clkstate = CLK_AVAIL;
  1840. }
  1841. }
  1842. /* Make sure backplane clock is on */
  1843. brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, true);
  1844. /* Pending interrupt indicates new device status */
  1845. if (atomic_read(&bus->ipend) > 0) {
  1846. atomic_set(&bus->ipend, 0);
  1847. sdio_claim_host(bus->sdiodev->func[1]);
  1848. err = brcmf_sdio_intr_rstatus(bus);
  1849. sdio_release_host(bus->sdiodev->func[1]);
  1850. }
  1851. /* Start with leftover status bits */
  1852. intstatus = atomic_xchg(&bus->intstatus, 0);
  1853. /* Handle flow-control change: read new state in case our ack
  1854. * crossed another change interrupt. If change still set, assume
  1855. * FC ON for safety, let next loop through do the debounce.
  1856. */
  1857. if (intstatus & I_HMB_FC_CHANGE) {
  1858. intstatus &= ~I_HMB_FC_CHANGE;
  1859. err = w_sdreg32(bus, I_HMB_FC_CHANGE,
  1860. offsetof(struct sdpcmd_regs, intstatus));
  1861. err = r_sdreg32(bus, &newstatus,
  1862. offsetof(struct sdpcmd_regs, intstatus));
  1863. bus->sdcnt.f1regdata += 2;
  1864. atomic_set(&bus->fcstate,
  1865. !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
  1866. intstatus |= (newstatus & bus->hostintmask);
  1867. }
  1868. /* Handle host mailbox indication */
  1869. if (intstatus & I_HMB_HOST_INT) {
  1870. intstatus &= ~I_HMB_HOST_INT;
  1871. intstatus |= brcmf_sdbrcm_hostmail(bus);
  1872. }
  1873. /* Generally don't ask for these, can get CRC errors... */
  1874. if (intstatus & I_WR_OOSYNC) {
  1875. brcmf_dbg(ERROR, "Dongle reports WR_OOSYNC\n");
  1876. intstatus &= ~I_WR_OOSYNC;
  1877. }
  1878. if (intstatus & I_RD_OOSYNC) {
  1879. brcmf_dbg(ERROR, "Dongle reports RD_OOSYNC\n");
  1880. intstatus &= ~I_RD_OOSYNC;
  1881. }
  1882. if (intstatus & I_SBINT) {
  1883. brcmf_dbg(ERROR, "Dongle reports SBINT\n");
  1884. intstatus &= ~I_SBINT;
  1885. }
  1886. /* Would be active due to wake-wlan in gSPI */
  1887. if (intstatus & I_CHIPACTIVE) {
  1888. brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
  1889. intstatus &= ~I_CHIPACTIVE;
  1890. }
  1891. /* Ignore frame indications if rxskip is set */
  1892. if (bus->rxskip)
  1893. intstatus &= ~I_HMB_FRAME_IND;
  1894. /* On frame indication, read available frames */
  1895. if (PKT_AVAILABLE() && bus->clkstate == CLK_AVAIL) {
  1896. framecnt = brcmf_sdio_readframes(bus, rxlimit);
  1897. if (!bus->rxpending)
  1898. intstatus &= ~I_HMB_FRAME_IND;
  1899. rxlimit -= min(framecnt, rxlimit);
  1900. }
  1901. /* Keep still-pending events for next scheduling */
  1902. if (intstatus) {
  1903. for_each_set_bit(n, &intstatus, 32)
  1904. set_bit(n, (unsigned long *)&bus->intstatus.counter);
  1905. }
  1906. brcmf_sdbrcm_clrintr(bus);
  1907. if (data_ok(bus) && bus->ctrl_frame_stat &&
  1908. (bus->clkstate == CLK_AVAIL)) {
  1909. int i;
  1910. err = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
  1911. SDIO_FUNC_2, F2SYNC, bus->ctrl_frame_buf,
  1912. (u32) bus->ctrl_frame_len);
  1913. if (err < 0) {
  1914. /* On failure, abort the command and
  1915. terminate the frame */
  1916. brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
  1917. err);
  1918. bus->sdcnt.tx_sderrs++;
  1919. brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
  1920. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
  1921. SFC_WF_TERM, &err);
  1922. bus->sdcnt.f1regdata++;
  1923. for (i = 0; i < 3; i++) {
  1924. u8 hi, lo;
  1925. hi = brcmf_sdio_regrb(bus->sdiodev,
  1926. SBSDIO_FUNC1_WFRAMEBCHI,
  1927. &err);
  1928. lo = brcmf_sdio_regrb(bus->sdiodev,
  1929. SBSDIO_FUNC1_WFRAMEBCLO,
  1930. &err);
  1931. bus->sdcnt.f1regdata += 2;
  1932. if ((hi == 0) && (lo == 0))
  1933. break;
  1934. }
  1935. } else {
  1936. bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
  1937. }
  1938. bus->ctrl_frame_stat = false;
  1939. brcmf_sdbrcm_wait_event_wakeup(bus);
  1940. }
  1941. /* Send queued frames (limit 1 if rx may still be pending) */
  1942. else if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
  1943. brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
  1944. && data_ok(bus)) {
  1945. framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
  1946. txlimit;
  1947. framecnt = brcmf_sdbrcm_sendfromq(bus, framecnt);
  1948. txlimit -= framecnt;
  1949. }
  1950. if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) || (err != 0)) {
  1951. brcmf_dbg(ERROR, "failed backplane access over SDIO, halting operation\n");
  1952. bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
  1953. atomic_set(&bus->intstatus, 0);
  1954. } else if (atomic_read(&bus->intstatus) ||
  1955. atomic_read(&bus->ipend) > 0 ||
  1956. (!atomic_read(&bus->fcstate) &&
  1957. brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
  1958. data_ok(bus)) || PKT_AVAILABLE()) {
  1959. brcmf_sdbrcm_adddpctsk(bus);
  1960. }
  1961. /* If we're done for now, turn off clock request. */
  1962. if ((bus->clkstate != CLK_PENDING)
  1963. && bus->idletime == BRCMF_IDLE_IMMEDIATE) {
  1964. bus->activity = false;
  1965. brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
  1966. }
  1967. up(&bus->sdsem);
  1968. }
  1969. static int brcmf_sdbrcm_bus_txdata(struct device *dev, struct sk_buff *pkt)
  1970. {
  1971. int ret = -EBADE;
  1972. uint datalen, prec;
  1973. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  1974. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  1975. struct brcmf_sdio *bus = sdiodev->bus;
  1976. unsigned long flags;
  1977. brcmf_dbg(TRACE, "Enter\n");
  1978. datalen = pkt->len;
  1979. /* Add space for the header */
  1980. skb_push(pkt, SDPCM_HDRLEN);
  1981. /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
  1982. prec = prio2prec((pkt->priority & PRIOMASK));
  1983. /* Check for existing queue, current flow-control,
  1984. pending event, or pending clock */
  1985. brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
  1986. bus->sdcnt.fcqueued++;
  1987. /* Priority based enq */
  1988. spin_lock_bh(&bus->txqlock);
  1989. if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
  1990. skb_pull(pkt, SDPCM_HDRLEN);
  1991. brcmf_txcomplete(bus->sdiodev->dev, pkt, false);
  1992. brcmu_pkt_buf_free_skb(pkt);
  1993. brcmf_dbg(ERROR, "out of bus->txq !!!\n");
  1994. ret = -ENOSR;
  1995. } else {
  1996. ret = 0;
  1997. }
  1998. spin_unlock_bh(&bus->txqlock);
  1999. if (pktq_len(&bus->txq) >= TXHI) {
  2000. bus->txoff = true;
  2001. brcmf_txflowblock(bus->sdiodev->dev, true);
  2002. }
  2003. #ifdef DEBUG
  2004. if (pktq_plen(&bus->txq, prec) > qcount[prec])
  2005. qcount[prec] = pktq_plen(&bus->txq, prec);
  2006. #endif
  2007. spin_lock_irqsave(&bus->dpc_tl_lock, flags);
  2008. if (list_empty(&bus->dpc_tsklst)) {
  2009. spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
  2010. brcmf_sdbrcm_adddpctsk(bus);
  2011. queue_work(bus->brcmf_wq, &bus->datawork);
  2012. } else {
  2013. spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
  2014. }
  2015. return ret;
  2016. }
  2017. static int
  2018. brcmf_sdbrcm_membytes(struct brcmf_sdio *bus, bool write, u32 address, u8 *data,
  2019. uint size)
  2020. {
  2021. int bcmerror = 0;
  2022. u32 sdaddr;
  2023. uint dsize;
  2024. /* Determine initial transfer parameters */
  2025. sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
  2026. if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
  2027. dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
  2028. else
  2029. dsize = size;
  2030. sdio_claim_host(bus->sdiodev->func[1]);
  2031. /* Set the backplane window to include the start address */
  2032. bcmerror = brcmf_sdcard_set_sbaddr_window(bus->sdiodev, address);
  2033. if (bcmerror) {
  2034. brcmf_dbg(ERROR, "window change failed\n");
  2035. goto xfer_done;
  2036. }
  2037. /* Do the transfer(s) */
  2038. while (size) {
  2039. brcmf_dbg(INFO, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
  2040. write ? "write" : "read", dsize,
  2041. sdaddr, address & SBSDIO_SBWINDOW_MASK);
  2042. bcmerror = brcmf_sdcard_rwdata(bus->sdiodev, write,
  2043. sdaddr, data, dsize);
  2044. if (bcmerror) {
  2045. brcmf_dbg(ERROR, "membytes transfer failed\n");
  2046. break;
  2047. }
  2048. /* Adjust for next transfer (if any) */
  2049. size -= dsize;
  2050. if (size) {
  2051. data += dsize;
  2052. address += dsize;
  2053. bcmerror = brcmf_sdcard_set_sbaddr_window(bus->sdiodev,
  2054. address);
  2055. if (bcmerror) {
  2056. brcmf_dbg(ERROR, "window change failed\n");
  2057. break;
  2058. }
  2059. sdaddr = 0;
  2060. dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
  2061. }
  2062. }
  2063. xfer_done:
  2064. /* Return the window to backplane enumeration space for core access */
  2065. if (brcmf_sdcard_set_sbaddr_window(bus->sdiodev, bus->sdiodev->sbwad))
  2066. brcmf_dbg(ERROR, "FAILED to set window back to 0x%x\n",
  2067. bus->sdiodev->sbwad);
  2068. sdio_release_host(bus->sdiodev->func[1]);
  2069. return bcmerror;
  2070. }
  2071. #ifdef DEBUG
  2072. #define CONSOLE_LINE_MAX 192
  2073. static int brcmf_sdbrcm_readconsole(struct brcmf_sdio *bus)
  2074. {
  2075. struct brcmf_console *c = &bus->console;
  2076. u8 line[CONSOLE_LINE_MAX], ch;
  2077. u32 n, idx, addr;
  2078. int rv;
  2079. /* Don't do anything until FWREADY updates console address */
  2080. if (bus->console_addr == 0)
  2081. return 0;
  2082. /* Read console log struct */
  2083. addr = bus->console_addr + offsetof(struct rte_console, log_le);
  2084. rv = brcmf_sdbrcm_membytes(bus, false, addr, (u8 *)&c->log_le,
  2085. sizeof(c->log_le));
  2086. if (rv < 0)
  2087. return rv;
  2088. /* Allocate console buffer (one time only) */
  2089. if (c->buf == NULL) {
  2090. c->bufsize = le32_to_cpu(c->log_le.buf_size);
  2091. c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
  2092. if (c->buf == NULL)
  2093. return -ENOMEM;
  2094. }
  2095. idx = le32_to_cpu(c->log_le.idx);
  2096. /* Protect against corrupt value */
  2097. if (idx > c->bufsize)
  2098. return -EBADE;
  2099. /* Skip reading the console buffer if the index pointer
  2100. has not moved */
  2101. if (idx == c->last)
  2102. return 0;
  2103. /* Read the console buffer */
  2104. addr = le32_to_cpu(c->log_le.buf);
  2105. rv = brcmf_sdbrcm_membytes(bus, false, addr, c->buf, c->bufsize);
  2106. if (rv < 0)
  2107. return rv;
  2108. while (c->last != idx) {
  2109. for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
  2110. if (c->last == idx) {
  2111. /* This would output a partial line.
  2112. * Instead, back up
  2113. * the buffer pointer and output this
  2114. * line next time around.
  2115. */
  2116. if (c->last >= n)
  2117. c->last -= n;
  2118. else
  2119. c->last = c->bufsize - n;
  2120. goto break2;
  2121. }
  2122. ch = c->buf[c->last];
  2123. c->last = (c->last + 1) % c->bufsize;
  2124. if (ch == '\n')
  2125. break;
  2126. line[n] = ch;
  2127. }
  2128. if (n > 0) {
  2129. if (line[n - 1] == '\r')
  2130. n--;
  2131. line[n] = 0;
  2132. pr_debug("CONSOLE: %s\n", line);
  2133. }
  2134. }
  2135. break2:
  2136. return 0;
  2137. }
  2138. #endif /* DEBUG */
  2139. static int brcmf_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len)
  2140. {
  2141. int i;
  2142. int ret;
  2143. bus->ctrl_frame_stat = false;
  2144. ret = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
  2145. SDIO_FUNC_2, F2SYNC, frame, len);
  2146. if (ret < 0) {
  2147. /* On failure, abort the command and terminate the frame */
  2148. brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
  2149. ret);
  2150. bus->sdcnt.tx_sderrs++;
  2151. brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
  2152. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
  2153. SFC_WF_TERM, NULL);
  2154. bus->sdcnt.f1regdata++;
  2155. for (i = 0; i < 3; i++) {
  2156. u8 hi, lo;
  2157. hi = brcmf_sdio_regrb(bus->sdiodev,
  2158. SBSDIO_FUNC1_WFRAMEBCHI, NULL);
  2159. lo = brcmf_sdio_regrb(bus->sdiodev,
  2160. SBSDIO_FUNC1_WFRAMEBCLO, NULL);
  2161. bus->sdcnt.f1regdata += 2;
  2162. if (hi == 0 && lo == 0)
  2163. break;
  2164. }
  2165. return ret;
  2166. }
  2167. bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
  2168. return ret;
  2169. }
  2170. static int
  2171. brcmf_sdbrcm_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
  2172. {
  2173. u8 *frame;
  2174. u16 len;
  2175. u32 swheader;
  2176. uint retries = 0;
  2177. u8 doff = 0;
  2178. int ret = -1;
  2179. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  2180. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2181. struct brcmf_sdio *bus = sdiodev->bus;
  2182. unsigned long flags;
  2183. brcmf_dbg(TRACE, "Enter\n");
  2184. /* Back the pointer to make a room for bus header */
  2185. frame = msg - SDPCM_HDRLEN;
  2186. len = (msglen += SDPCM_HDRLEN);
  2187. /* Add alignment padding (optional for ctl frames) */
  2188. doff = ((unsigned long)frame % BRCMF_SDALIGN);
  2189. if (doff) {
  2190. frame -= doff;
  2191. len += doff;
  2192. msglen += doff;
  2193. memset(frame, 0, doff + SDPCM_HDRLEN);
  2194. }
  2195. /* precondition: doff < BRCMF_SDALIGN */
  2196. doff += SDPCM_HDRLEN;
  2197. /* Round send length to next SDIO block */
  2198. if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
  2199. u16 pad = bus->blocksize - (len % bus->blocksize);
  2200. if ((pad <= bus->roundup) && (pad < bus->blocksize))
  2201. len += pad;
  2202. } else if (len % BRCMF_SDALIGN) {
  2203. len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
  2204. }
  2205. /* Satisfy length-alignment requirements */
  2206. if (len & (ALIGNMENT - 1))
  2207. len = roundup(len, ALIGNMENT);
  2208. /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
  2209. /* Need to lock here to protect txseq and SDIO tx calls */
  2210. down(&bus->sdsem);
  2211. /* Make sure backplane clock is on */
  2212. brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
  2213. /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
  2214. *(__le16 *) frame = cpu_to_le16((u16) msglen);
  2215. *(((__le16 *) frame) + 1) = cpu_to_le16(~msglen);
  2216. /* Software tag: channel, sequence number, data offset */
  2217. swheader =
  2218. ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
  2219. SDPCM_CHANNEL_MASK)
  2220. | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
  2221. SDPCM_DOFFSET_MASK);
  2222. put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
  2223. put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
  2224. if (!data_ok(bus)) {
  2225. brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
  2226. bus->tx_max, bus->tx_seq);
  2227. bus->ctrl_frame_stat = true;
  2228. /* Send from dpc */
  2229. bus->ctrl_frame_buf = frame;
  2230. bus->ctrl_frame_len = len;
  2231. brcmf_sdbrcm_wait_for_event(bus, &bus->ctrl_frame_stat);
  2232. if (!bus->ctrl_frame_stat) {
  2233. brcmf_dbg(INFO, "ctrl_frame_stat == false\n");
  2234. ret = 0;
  2235. } else {
  2236. brcmf_dbg(INFO, "ctrl_frame_stat == true\n");
  2237. ret = -1;
  2238. }
  2239. }
  2240. if (ret == -1) {
  2241. brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
  2242. frame, len, "Tx Frame:\n");
  2243. brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
  2244. BRCMF_HDRS_ON(),
  2245. frame, min_t(u16, len, 16), "TxHdr:\n");
  2246. do {
  2247. ret = brcmf_tx_frame(bus, frame, len);
  2248. } while (ret < 0 && retries++ < TXRETRIES);
  2249. }
  2250. spin_lock_irqsave(&bus->dpc_tl_lock, flags);
  2251. if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) &&
  2252. list_empty(&bus->dpc_tsklst)) {
  2253. spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
  2254. bus->activity = false;
  2255. brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
  2256. } else {
  2257. spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
  2258. }
  2259. up(&bus->sdsem);
  2260. if (ret)
  2261. bus->sdcnt.tx_ctlerrs++;
  2262. else
  2263. bus->sdcnt.tx_ctlpkts++;
  2264. return ret ? -EIO : 0;
  2265. }
  2266. #ifdef DEBUG
  2267. static inline bool brcmf_sdio_valid_shared_address(u32 addr)
  2268. {
  2269. return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
  2270. }
  2271. static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
  2272. struct sdpcm_shared *sh)
  2273. {
  2274. u32 addr;
  2275. int rv;
  2276. u32 shaddr = 0;
  2277. struct sdpcm_shared_le sh_le;
  2278. __le32 addr_le;
  2279. shaddr = bus->ramsize - 4;
  2280. /*
  2281. * Read last word in socram to determine
  2282. * address of sdpcm_shared structure
  2283. */
  2284. rv = brcmf_sdbrcm_membytes(bus, false, shaddr,
  2285. (u8 *)&addr_le, 4);
  2286. if (rv < 0)
  2287. return rv;
  2288. addr = le32_to_cpu(addr_le);
  2289. brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
  2290. /*
  2291. * Check if addr is valid.
  2292. * NVRAM length at the end of memory should have been overwritten.
  2293. */
  2294. if (!brcmf_sdio_valid_shared_address(addr)) {
  2295. brcmf_dbg(ERROR, "invalid sdpcm_shared address 0x%08X\n",
  2296. addr);
  2297. return -EINVAL;
  2298. }
  2299. /* Read hndrte_shared structure */
  2300. rv = brcmf_sdbrcm_membytes(bus, false, addr, (u8 *)&sh_le,
  2301. sizeof(struct sdpcm_shared_le));
  2302. if (rv < 0)
  2303. return rv;
  2304. /* Endianness */
  2305. sh->flags = le32_to_cpu(sh_le.flags);
  2306. sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
  2307. sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
  2308. sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
  2309. sh->assert_line = le32_to_cpu(sh_le.assert_line);
  2310. sh->console_addr = le32_to_cpu(sh_le.console_addr);
  2311. sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
  2312. if ((sh->flags & SDPCM_SHARED_VERSION_MASK) != SDPCM_SHARED_VERSION) {
  2313. brcmf_dbg(ERROR,
  2314. "sdpcm_shared version mismatch: dhd %d dongle %d\n",
  2315. SDPCM_SHARED_VERSION,
  2316. sh->flags & SDPCM_SHARED_VERSION_MASK);
  2317. return -EPROTO;
  2318. }
  2319. return 0;
  2320. }
  2321. static int brcmf_sdio_dump_console(struct brcmf_sdio *bus,
  2322. struct sdpcm_shared *sh, char __user *data,
  2323. size_t count)
  2324. {
  2325. u32 addr, console_ptr, console_size, console_index;
  2326. char *conbuf = NULL;
  2327. __le32 sh_val;
  2328. int rv;
  2329. loff_t pos = 0;
  2330. int nbytes = 0;
  2331. /* obtain console information from device memory */
  2332. addr = sh->console_addr + offsetof(struct rte_console, log_le);
  2333. rv = brcmf_sdbrcm_membytes(bus, false, addr,
  2334. (u8 *)&sh_val, sizeof(u32));
  2335. if (rv < 0)
  2336. return rv;
  2337. console_ptr = le32_to_cpu(sh_val);
  2338. addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
  2339. rv = brcmf_sdbrcm_membytes(bus, false, addr,
  2340. (u8 *)&sh_val, sizeof(u32));
  2341. if (rv < 0)
  2342. return rv;
  2343. console_size = le32_to_cpu(sh_val);
  2344. addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
  2345. rv = brcmf_sdbrcm_membytes(bus, false, addr,
  2346. (u8 *)&sh_val, sizeof(u32));
  2347. if (rv < 0)
  2348. return rv;
  2349. console_index = le32_to_cpu(sh_val);
  2350. /* allocate buffer for console data */
  2351. if (console_size <= CONSOLE_BUFFER_MAX)
  2352. conbuf = vzalloc(console_size+1);
  2353. if (!conbuf)
  2354. return -ENOMEM;
  2355. /* obtain the console data from device */
  2356. conbuf[console_size] = '\0';
  2357. rv = brcmf_sdbrcm_membytes(bus, false, console_ptr, (u8 *)conbuf,
  2358. console_size);
  2359. if (rv < 0)
  2360. goto done;
  2361. rv = simple_read_from_buffer(data, count, &pos,
  2362. conbuf + console_index,
  2363. console_size - console_index);
  2364. if (rv < 0)
  2365. goto done;
  2366. nbytes = rv;
  2367. if (console_index > 0) {
  2368. pos = 0;
  2369. rv = simple_read_from_buffer(data+nbytes, count, &pos,
  2370. conbuf, console_index - 1);
  2371. if (rv < 0)
  2372. goto done;
  2373. rv += nbytes;
  2374. }
  2375. done:
  2376. vfree(conbuf);
  2377. return rv;
  2378. }
  2379. static int brcmf_sdio_trap_info(struct brcmf_sdio *bus, struct sdpcm_shared *sh,
  2380. char __user *data, size_t count)
  2381. {
  2382. int error, res;
  2383. char buf[350];
  2384. struct brcmf_trap_info tr;
  2385. int nbytes;
  2386. loff_t pos = 0;
  2387. if ((sh->flags & SDPCM_SHARED_TRAP) == 0)
  2388. return 0;
  2389. error = brcmf_sdbrcm_membytes(bus, false, sh->trap_addr, (u8 *)&tr,
  2390. sizeof(struct brcmf_trap_info));
  2391. if (error < 0)
  2392. return error;
  2393. nbytes = brcmf_sdio_dump_console(bus, sh, data, count);
  2394. if (nbytes < 0)
  2395. return nbytes;
  2396. res = scnprintf(buf, sizeof(buf),
  2397. "dongle trap info: type 0x%x @ epc 0x%08x\n"
  2398. " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
  2399. " lr 0x%08x pc 0x%08x offset 0x%x\n"
  2400. " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n"
  2401. " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n",
  2402. le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
  2403. le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
  2404. le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
  2405. le32_to_cpu(tr.pc), sh->trap_addr,
  2406. le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
  2407. le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
  2408. le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
  2409. le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
  2410. error = simple_read_from_buffer(data+nbytes, count, &pos, buf, res);
  2411. if (error < 0)
  2412. return error;
  2413. nbytes += error;
  2414. return nbytes;
  2415. }
  2416. static int brcmf_sdio_assert_info(struct brcmf_sdio *bus,
  2417. struct sdpcm_shared *sh, char __user *data,
  2418. size_t count)
  2419. {
  2420. int error = 0;
  2421. char buf[200];
  2422. char file[80] = "?";
  2423. char expr[80] = "<???>";
  2424. int res;
  2425. loff_t pos = 0;
  2426. if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
  2427. brcmf_dbg(INFO, "firmware not built with -assert\n");
  2428. return 0;
  2429. } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
  2430. brcmf_dbg(INFO, "no assert in dongle\n");
  2431. return 0;
  2432. }
  2433. if (sh->assert_file_addr != 0) {
  2434. error = brcmf_sdbrcm_membytes(bus, false, sh->assert_file_addr,
  2435. (u8 *)file, 80);
  2436. if (error < 0)
  2437. return error;
  2438. }
  2439. if (sh->assert_exp_addr != 0) {
  2440. error = brcmf_sdbrcm_membytes(bus, false, sh->assert_exp_addr,
  2441. (u8 *)expr, 80);
  2442. if (error < 0)
  2443. return error;
  2444. }
  2445. res = scnprintf(buf, sizeof(buf),
  2446. "dongle assert: %s:%d: assert(%s)\n",
  2447. file, sh->assert_line, expr);
  2448. return simple_read_from_buffer(data, count, &pos, buf, res);
  2449. }
  2450. static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
  2451. {
  2452. int error;
  2453. struct sdpcm_shared sh;
  2454. down(&bus->sdsem);
  2455. error = brcmf_sdio_readshared(bus, &sh);
  2456. up(&bus->sdsem);
  2457. if (error < 0)
  2458. return error;
  2459. if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
  2460. brcmf_dbg(INFO, "firmware not built with -assert\n");
  2461. else if (sh.flags & SDPCM_SHARED_ASSERT)
  2462. brcmf_dbg(ERROR, "assertion in dongle\n");
  2463. if (sh.flags & SDPCM_SHARED_TRAP)
  2464. brcmf_dbg(ERROR, "firmware trap in dongle\n");
  2465. return 0;
  2466. }
  2467. static int brcmf_sdbrcm_died_dump(struct brcmf_sdio *bus, char __user *data,
  2468. size_t count, loff_t *ppos)
  2469. {
  2470. int error = 0;
  2471. struct sdpcm_shared sh;
  2472. int nbytes = 0;
  2473. loff_t pos = *ppos;
  2474. if (pos != 0)
  2475. return 0;
  2476. down(&bus->sdsem);
  2477. error = brcmf_sdio_readshared(bus, &sh);
  2478. if (error < 0)
  2479. goto done;
  2480. error = brcmf_sdio_assert_info(bus, &sh, data, count);
  2481. if (error < 0)
  2482. goto done;
  2483. nbytes = error;
  2484. error = brcmf_sdio_trap_info(bus, &sh, data, count);
  2485. if (error < 0)
  2486. goto done;
  2487. error += nbytes;
  2488. *ppos += error;
  2489. done:
  2490. up(&bus->sdsem);
  2491. return error;
  2492. }
  2493. static ssize_t brcmf_sdio_forensic_read(struct file *f, char __user *data,
  2494. size_t count, loff_t *ppos)
  2495. {
  2496. struct brcmf_sdio *bus = f->private_data;
  2497. int res;
  2498. res = brcmf_sdbrcm_died_dump(bus, data, count, ppos);
  2499. if (res > 0)
  2500. *ppos += res;
  2501. return (ssize_t)res;
  2502. }
  2503. static const struct file_operations brcmf_sdio_forensic_ops = {
  2504. .owner = THIS_MODULE,
  2505. .open = simple_open,
  2506. .read = brcmf_sdio_forensic_read
  2507. };
  2508. static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
  2509. {
  2510. struct brcmf_pub *drvr = bus->sdiodev->bus_if->drvr;
  2511. struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
  2512. if (IS_ERR_OR_NULL(dentry))
  2513. return;
  2514. debugfs_create_file("forensics", S_IRUGO, dentry, bus,
  2515. &brcmf_sdio_forensic_ops);
  2516. brcmf_debugfs_create_sdio_count(drvr, &bus->sdcnt);
  2517. }
  2518. #else
  2519. static int brcmf_sdbrcm_checkdied(struct brcmf_sdio *bus)
  2520. {
  2521. return 0;
  2522. }
  2523. static void brcmf_sdio_debugfs_create(struct brcmf_sdio *bus)
  2524. {
  2525. }
  2526. #endif /* DEBUG */
  2527. static int
  2528. brcmf_sdbrcm_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
  2529. {
  2530. int timeleft;
  2531. uint rxlen = 0;
  2532. bool pending;
  2533. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  2534. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2535. struct brcmf_sdio *bus = sdiodev->bus;
  2536. brcmf_dbg(TRACE, "Enter\n");
  2537. /* Wait until control frame is available */
  2538. timeleft = brcmf_sdbrcm_dcmd_resp_wait(bus, &bus->rxlen, &pending);
  2539. down(&bus->sdsem);
  2540. rxlen = bus->rxlen;
  2541. memcpy(msg, bus->rxctl, min(msglen, rxlen));
  2542. bus->rxlen = 0;
  2543. up(&bus->sdsem);
  2544. if (rxlen) {
  2545. brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
  2546. rxlen, msglen);
  2547. } else if (timeleft == 0) {
  2548. brcmf_dbg(ERROR, "resumed on timeout\n");
  2549. brcmf_sdbrcm_checkdied(bus);
  2550. } else if (pending) {
  2551. brcmf_dbg(CTL, "cancelled\n");
  2552. return -ERESTARTSYS;
  2553. } else {
  2554. brcmf_dbg(CTL, "resumed for unknown reason?\n");
  2555. brcmf_sdbrcm_checkdied(bus);
  2556. }
  2557. if (rxlen)
  2558. bus->sdcnt.rx_ctlpkts++;
  2559. else
  2560. bus->sdcnt.rx_ctlerrs++;
  2561. return rxlen ? (int)rxlen : -ETIMEDOUT;
  2562. }
  2563. static int brcmf_sdbrcm_write_vars(struct brcmf_sdio *bus)
  2564. {
  2565. int bcmerror = 0;
  2566. u32 varaddr;
  2567. u32 varsizew;
  2568. __le32 varsizew_le;
  2569. #ifdef DEBUG
  2570. char *nvram_ularray;
  2571. #endif /* DEBUG */
  2572. /* Even if there are no vars are to be written, we still
  2573. need to set the ramsize. */
  2574. varaddr = (bus->ramsize - 4) - bus->varsz;
  2575. if (bus->vars) {
  2576. /* Write the vars list */
  2577. bcmerror = brcmf_sdbrcm_membytes(bus, true, varaddr,
  2578. bus->vars, bus->varsz);
  2579. #ifdef DEBUG
  2580. /* Verify NVRAM bytes */
  2581. brcmf_dbg(INFO, "Compare NVRAM dl & ul; varsize=%d\n",
  2582. bus->varsz);
  2583. nvram_ularray = kmalloc(bus->varsz, GFP_ATOMIC);
  2584. if (!nvram_ularray)
  2585. return -ENOMEM;
  2586. /* Upload image to verify downloaded contents. */
  2587. memset(nvram_ularray, 0xaa, bus->varsz);
  2588. /* Read the vars list to temp buffer for comparison */
  2589. bcmerror = brcmf_sdbrcm_membytes(bus, false, varaddr,
  2590. nvram_ularray, bus->varsz);
  2591. if (bcmerror) {
  2592. brcmf_dbg(ERROR, "error %d on reading %d nvram bytes at 0x%08x\n",
  2593. bcmerror, bus->varsz, varaddr);
  2594. }
  2595. /* Compare the org NVRAM with the one read from RAM */
  2596. if (memcmp(bus->vars, nvram_ularray, bus->varsz))
  2597. brcmf_dbg(ERROR, "Downloaded NVRAM image is corrupted\n");
  2598. else
  2599. brcmf_dbg(ERROR, "Download/Upload/Compare of NVRAM ok\n");
  2600. kfree(nvram_ularray);
  2601. #endif /* DEBUG */
  2602. }
  2603. /* adjust to the user specified RAM */
  2604. brcmf_dbg(INFO, "Physical memory size: %d\n", bus->ramsize);
  2605. brcmf_dbg(INFO, "Vars are at %d, orig varsize is %d\n",
  2606. varaddr, bus->varsz);
  2607. /*
  2608. * Determine the length token:
  2609. * Varsize, converted to words, in lower 16-bits, checksum
  2610. * in upper 16-bits.
  2611. */
  2612. if (bcmerror) {
  2613. varsizew = 0;
  2614. varsizew_le = cpu_to_le32(0);
  2615. } else {
  2616. varsizew = bus->varsz / 4;
  2617. varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
  2618. varsizew_le = cpu_to_le32(varsizew);
  2619. }
  2620. brcmf_dbg(INFO, "New varsize is %d, length token=0x%08x\n",
  2621. bus->varsz, varsizew);
  2622. /* Write the length token to the last word */
  2623. bcmerror = brcmf_sdbrcm_membytes(bus, true, (bus->ramsize - 4),
  2624. (u8 *)&varsizew_le, 4);
  2625. return bcmerror;
  2626. }
  2627. static int brcmf_sdbrcm_download_state(struct brcmf_sdio *bus, bool enter)
  2628. {
  2629. int bcmerror = 0;
  2630. struct chip_info *ci = bus->ci;
  2631. /* To enter download state, disable ARM and reset SOCRAM.
  2632. * To exit download state, simply reset ARM (default is RAM boot).
  2633. */
  2634. if (enter) {
  2635. bus->alp_only = true;
  2636. ci->coredisable(bus->sdiodev, ci, BCMA_CORE_ARM_CM3);
  2637. ci->resetcore(bus->sdiodev, ci, BCMA_CORE_INTERNAL_MEM);
  2638. /* Clear the top bit of memory */
  2639. if (bus->ramsize) {
  2640. u32 zeros = 0;
  2641. brcmf_sdbrcm_membytes(bus, true, bus->ramsize - 4,
  2642. (u8 *)&zeros, 4);
  2643. }
  2644. } else {
  2645. if (!ci->iscoreup(bus->sdiodev, ci, BCMA_CORE_INTERNAL_MEM)) {
  2646. brcmf_dbg(ERROR, "SOCRAM core is down after reset?\n");
  2647. bcmerror = -EBADE;
  2648. goto fail;
  2649. }
  2650. bcmerror = brcmf_sdbrcm_write_vars(bus);
  2651. if (bcmerror) {
  2652. brcmf_dbg(ERROR, "no vars written to RAM\n");
  2653. bcmerror = 0;
  2654. }
  2655. w_sdreg32(bus, 0xFFFFFFFF,
  2656. offsetof(struct sdpcmd_regs, intstatus));
  2657. ci->resetcore(bus->sdiodev, ci, BCMA_CORE_ARM_CM3);
  2658. /* Allow HT Clock now that the ARM is running. */
  2659. bus->alp_only = false;
  2660. bus->sdiodev->bus_if->state = BRCMF_BUS_LOAD;
  2661. }
  2662. fail:
  2663. return bcmerror;
  2664. }
  2665. static int brcmf_sdbrcm_get_image(char *buf, int len, struct brcmf_sdio *bus)
  2666. {
  2667. if (bus->firmware->size < bus->fw_ptr + len)
  2668. len = bus->firmware->size - bus->fw_ptr;
  2669. memcpy(buf, &bus->firmware->data[bus->fw_ptr], len);
  2670. bus->fw_ptr += len;
  2671. return len;
  2672. }
  2673. static int brcmf_sdbrcm_download_code_file(struct brcmf_sdio *bus)
  2674. {
  2675. int offset = 0;
  2676. uint len;
  2677. u8 *memblock = NULL, *memptr;
  2678. int ret;
  2679. brcmf_dbg(INFO, "Enter\n");
  2680. ret = request_firmware(&bus->firmware, BRCMF_SDIO_FW_NAME,
  2681. &bus->sdiodev->func[2]->dev);
  2682. if (ret) {
  2683. brcmf_dbg(ERROR, "Fail to request firmware %d\n", ret);
  2684. return ret;
  2685. }
  2686. bus->fw_ptr = 0;
  2687. memptr = memblock = kmalloc(MEMBLOCK + BRCMF_SDALIGN, GFP_ATOMIC);
  2688. if (memblock == NULL) {
  2689. ret = -ENOMEM;
  2690. goto err;
  2691. }
  2692. if ((u32)(unsigned long)memblock % BRCMF_SDALIGN)
  2693. memptr += (BRCMF_SDALIGN -
  2694. ((u32)(unsigned long)memblock % BRCMF_SDALIGN));
  2695. /* Download image */
  2696. while ((len =
  2697. brcmf_sdbrcm_get_image((char *)memptr, MEMBLOCK, bus))) {
  2698. ret = brcmf_sdbrcm_membytes(bus, true, offset, memptr, len);
  2699. if (ret) {
  2700. brcmf_dbg(ERROR, "error %d on writing %d membytes at 0x%08x\n",
  2701. ret, MEMBLOCK, offset);
  2702. goto err;
  2703. }
  2704. offset += MEMBLOCK;
  2705. }
  2706. err:
  2707. kfree(memblock);
  2708. release_firmware(bus->firmware);
  2709. bus->fw_ptr = 0;
  2710. return ret;
  2711. }
  2712. /*
  2713. * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
  2714. * and ending in a NUL.
  2715. * Removes carriage returns, empty lines, comment lines, and converts
  2716. * newlines to NULs.
  2717. * Shortens buffer as needed and pads with NULs. End of buffer is marked
  2718. * by two NULs.
  2719. */
  2720. static int brcmf_process_nvram_vars(struct brcmf_sdio *bus)
  2721. {
  2722. char *varbuf;
  2723. char *dp;
  2724. bool findNewline;
  2725. int column;
  2726. int ret = 0;
  2727. uint buf_len, n, len;
  2728. len = bus->firmware->size;
  2729. varbuf = vmalloc(len);
  2730. if (!varbuf)
  2731. return -ENOMEM;
  2732. memcpy(varbuf, bus->firmware->data, len);
  2733. dp = varbuf;
  2734. findNewline = false;
  2735. column = 0;
  2736. for (n = 0; n < len; n++) {
  2737. if (varbuf[n] == 0)
  2738. break;
  2739. if (varbuf[n] == '\r')
  2740. continue;
  2741. if (findNewline && varbuf[n] != '\n')
  2742. continue;
  2743. findNewline = false;
  2744. if (varbuf[n] == '#') {
  2745. findNewline = true;
  2746. continue;
  2747. }
  2748. if (varbuf[n] == '\n') {
  2749. if (column == 0)
  2750. continue;
  2751. *dp++ = 0;
  2752. column = 0;
  2753. continue;
  2754. }
  2755. *dp++ = varbuf[n];
  2756. column++;
  2757. }
  2758. buf_len = dp - varbuf;
  2759. while (dp < varbuf + n)
  2760. *dp++ = 0;
  2761. kfree(bus->vars);
  2762. /* roundup needed for download to device */
  2763. bus->varsz = roundup(buf_len + 1, 4);
  2764. bus->vars = kmalloc(bus->varsz, GFP_KERNEL);
  2765. if (bus->vars == NULL) {
  2766. bus->varsz = 0;
  2767. ret = -ENOMEM;
  2768. goto err;
  2769. }
  2770. /* copy the processed variables and add null termination */
  2771. memcpy(bus->vars, varbuf, buf_len);
  2772. bus->vars[buf_len] = 0;
  2773. err:
  2774. vfree(varbuf);
  2775. return ret;
  2776. }
  2777. static int brcmf_sdbrcm_download_nvram(struct brcmf_sdio *bus)
  2778. {
  2779. int ret;
  2780. if (bus->sdiodev->bus_if->drvr_up)
  2781. return -EISCONN;
  2782. ret = request_firmware(&bus->firmware, BRCMF_SDIO_NV_NAME,
  2783. &bus->sdiodev->func[2]->dev);
  2784. if (ret) {
  2785. brcmf_dbg(ERROR, "Fail to request nvram %d\n", ret);
  2786. return ret;
  2787. }
  2788. ret = brcmf_process_nvram_vars(bus);
  2789. release_firmware(bus->firmware);
  2790. return ret;
  2791. }
  2792. static int _brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
  2793. {
  2794. int bcmerror = -1;
  2795. /* Keep arm in reset */
  2796. if (brcmf_sdbrcm_download_state(bus, true)) {
  2797. brcmf_dbg(ERROR, "error placing ARM core in reset\n");
  2798. goto err;
  2799. }
  2800. /* External image takes precedence if specified */
  2801. if (brcmf_sdbrcm_download_code_file(bus)) {
  2802. brcmf_dbg(ERROR, "dongle image file download failed\n");
  2803. goto err;
  2804. }
  2805. /* External nvram takes precedence if specified */
  2806. if (brcmf_sdbrcm_download_nvram(bus))
  2807. brcmf_dbg(ERROR, "dongle nvram file download failed\n");
  2808. /* Take arm out of reset */
  2809. if (brcmf_sdbrcm_download_state(bus, false)) {
  2810. brcmf_dbg(ERROR, "error getting out of ARM core reset\n");
  2811. goto err;
  2812. }
  2813. bcmerror = 0;
  2814. err:
  2815. return bcmerror;
  2816. }
  2817. static bool
  2818. brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
  2819. {
  2820. bool ret;
  2821. /* Download the firmware */
  2822. brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
  2823. ret = _brcmf_sdbrcm_download_firmware(bus) == 0;
  2824. brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
  2825. return ret;
  2826. }
  2827. static int brcmf_sdbrcm_bus_init(struct device *dev)
  2828. {
  2829. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  2830. struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
  2831. struct brcmf_sdio *bus = sdiodev->bus;
  2832. unsigned long timeout;
  2833. u8 ready, enable;
  2834. int err, ret = 0;
  2835. u8 saveclk;
  2836. brcmf_dbg(TRACE, "Enter\n");
  2837. /* try to download image and nvram to the dongle */
  2838. if (bus_if->state == BRCMF_BUS_DOWN) {
  2839. if (!(brcmf_sdbrcm_download_firmware(bus)))
  2840. return -1;
  2841. }
  2842. if (!bus->sdiodev->bus_if->drvr)
  2843. return 0;
  2844. /* Start the watchdog timer */
  2845. bus->sdcnt.tickcnt = 0;
  2846. brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
  2847. down(&bus->sdsem);
  2848. /* Make sure backplane clock is on, needed to generate F2 interrupt */
  2849. brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
  2850. if (bus->clkstate != CLK_AVAIL)
  2851. goto exit;
  2852. /* Force clocks on backplane to be sure F2 interrupt propagates */
  2853. saveclk = brcmf_sdio_regrb(bus->sdiodev,
  2854. SBSDIO_FUNC1_CHIPCLKCSR, &err);
  2855. if (!err) {
  2856. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  2857. (saveclk | SBSDIO_FORCE_HT), &err);
  2858. }
  2859. if (err) {
  2860. brcmf_dbg(ERROR, "Failed to force clock for F2: err %d\n", err);
  2861. goto exit;
  2862. }
  2863. /* Enable function 2 (frame transfers) */
  2864. w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
  2865. offsetof(struct sdpcmd_regs, tosbmailboxdata));
  2866. enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
  2867. brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
  2868. timeout = jiffies + msecs_to_jiffies(BRCMF_WAIT_F2RDY);
  2869. ready = 0;
  2870. while (enable != ready) {
  2871. ready = brcmf_sdio_regrb(bus->sdiodev,
  2872. SDIO_CCCR_IORx, NULL);
  2873. if (time_after(jiffies, timeout))
  2874. break;
  2875. else if (time_after(jiffies, timeout - BRCMF_WAIT_F2RDY + 50))
  2876. /* prevent busy waiting if it takes too long */
  2877. msleep_interruptible(20);
  2878. }
  2879. brcmf_dbg(INFO, "enable 0x%02x, ready 0x%02x\n", enable, ready);
  2880. /* If F2 successfully enabled, set core and enable interrupts */
  2881. if (ready == enable) {
  2882. /* Set up the interrupt mask and enable interrupts */
  2883. bus->hostintmask = HOSTINTMASK;
  2884. w_sdreg32(bus, bus->hostintmask,
  2885. offsetof(struct sdpcmd_regs, hostintmask));
  2886. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_WATERMARK, 8, &err);
  2887. } else {
  2888. /* Disable F2 again */
  2889. enable = SDIO_FUNC_ENABLE_1;
  2890. brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
  2891. ret = -ENODEV;
  2892. }
  2893. /* Restore previous clock setting */
  2894. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, saveclk, &err);
  2895. if (ret == 0) {
  2896. ret = brcmf_sdio_intr_register(bus->sdiodev);
  2897. if (ret != 0)
  2898. brcmf_dbg(ERROR, "intr register failed:%d\n", ret);
  2899. }
  2900. /* If we didn't come up, turn off backplane clock */
  2901. if (bus_if->state != BRCMF_BUS_DATA)
  2902. brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
  2903. exit:
  2904. up(&bus->sdsem);
  2905. return ret;
  2906. }
  2907. void brcmf_sdbrcm_isr(void *arg)
  2908. {
  2909. struct brcmf_sdio *bus = (struct brcmf_sdio *) arg;
  2910. brcmf_dbg(TRACE, "Enter\n");
  2911. if (!bus) {
  2912. brcmf_dbg(ERROR, "bus is null pointer, exiting\n");
  2913. return;
  2914. }
  2915. if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
  2916. brcmf_dbg(ERROR, "bus is down. we have nothing to do\n");
  2917. return;
  2918. }
  2919. /* Count the interrupt call */
  2920. bus->sdcnt.intrcount++;
  2921. if (in_interrupt())
  2922. atomic_set(&bus->ipend, 1);
  2923. else
  2924. if (brcmf_sdio_intr_rstatus(bus)) {
  2925. brcmf_dbg(ERROR, "failed backplane access\n");
  2926. bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
  2927. }
  2928. /* Disable additional interrupts (is this needed now)? */
  2929. if (!bus->intr)
  2930. brcmf_dbg(ERROR, "isr w/o interrupt configured!\n");
  2931. brcmf_sdbrcm_adddpctsk(bus);
  2932. queue_work(bus->brcmf_wq, &bus->datawork);
  2933. }
  2934. static bool brcmf_sdbrcm_bus_watchdog(struct brcmf_sdio *bus)
  2935. {
  2936. #ifdef DEBUG
  2937. struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
  2938. #endif /* DEBUG */
  2939. unsigned long flags;
  2940. brcmf_dbg(TIMER, "Enter\n");
  2941. down(&bus->sdsem);
  2942. /* Poll period: check device if appropriate. */
  2943. if (bus->poll && (++bus->polltick >= bus->pollrate)) {
  2944. u32 intstatus = 0;
  2945. /* Reset poll tick */
  2946. bus->polltick = 0;
  2947. /* Check device if no interrupts */
  2948. if (!bus->intr ||
  2949. (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
  2950. spin_lock_irqsave(&bus->dpc_tl_lock, flags);
  2951. if (list_empty(&bus->dpc_tsklst)) {
  2952. u8 devpend;
  2953. spin_unlock_irqrestore(&bus->dpc_tl_lock,
  2954. flags);
  2955. devpend = brcmf_sdio_regrb(bus->sdiodev,
  2956. SDIO_CCCR_INTx,
  2957. NULL);
  2958. intstatus =
  2959. devpend & (INTR_STATUS_FUNC1 |
  2960. INTR_STATUS_FUNC2);
  2961. } else {
  2962. spin_unlock_irqrestore(&bus->dpc_tl_lock,
  2963. flags);
  2964. }
  2965. /* If there is something, make like the ISR and
  2966. schedule the DPC */
  2967. if (intstatus) {
  2968. bus->sdcnt.pollcnt++;
  2969. atomic_set(&bus->ipend, 1);
  2970. brcmf_sdbrcm_adddpctsk(bus);
  2971. queue_work(bus->brcmf_wq, &bus->datawork);
  2972. }
  2973. }
  2974. /* Update interrupt tracking */
  2975. bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
  2976. }
  2977. #ifdef DEBUG
  2978. /* Poll for console output periodically */
  2979. if (bus_if->state == BRCMF_BUS_DATA &&
  2980. bus->console_interval != 0) {
  2981. bus->console.count += BRCMF_WD_POLL_MS;
  2982. if (bus->console.count >= bus->console_interval) {
  2983. bus->console.count -= bus->console_interval;
  2984. /* Make sure backplane clock is on */
  2985. brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
  2986. if (brcmf_sdbrcm_readconsole(bus) < 0)
  2987. /* stop on error */
  2988. bus->console_interval = 0;
  2989. }
  2990. }
  2991. #endif /* DEBUG */
  2992. /* On idle timeout clear activity flag and/or turn off clock */
  2993. if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
  2994. if (++bus->idlecount >= bus->idletime) {
  2995. bus->idlecount = 0;
  2996. if (bus->activity) {
  2997. bus->activity = false;
  2998. brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
  2999. } else {
  3000. brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
  3001. }
  3002. }
  3003. }
  3004. up(&bus->sdsem);
  3005. return (atomic_read(&bus->ipend) > 0);
  3006. }
  3007. static bool brcmf_sdbrcm_chipmatch(u16 chipid)
  3008. {
  3009. if (chipid == BCM43241_CHIP_ID)
  3010. return true;
  3011. if (chipid == BCM4329_CHIP_ID)
  3012. return true;
  3013. if (chipid == BCM4330_CHIP_ID)
  3014. return true;
  3015. if (chipid == BCM4334_CHIP_ID)
  3016. return true;
  3017. return false;
  3018. }
  3019. static void brcmf_sdio_dataworker(struct work_struct *work)
  3020. {
  3021. struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
  3022. datawork);
  3023. struct list_head *cur_hd, *tmp_hd;
  3024. unsigned long flags;
  3025. spin_lock_irqsave(&bus->dpc_tl_lock, flags);
  3026. list_for_each_safe(cur_hd, tmp_hd, &bus->dpc_tsklst) {
  3027. spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
  3028. brcmf_sdbrcm_dpc(bus);
  3029. spin_lock_irqsave(&bus->dpc_tl_lock, flags);
  3030. list_del(cur_hd);
  3031. kfree(cur_hd);
  3032. }
  3033. spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
  3034. }
  3035. static void brcmf_sdbrcm_release_malloc(struct brcmf_sdio *bus)
  3036. {
  3037. brcmf_dbg(TRACE, "Enter\n");
  3038. kfree(bus->rxbuf);
  3039. bus->rxctl = bus->rxbuf = NULL;
  3040. bus->rxlen = 0;
  3041. kfree(bus->databuf);
  3042. bus->databuf = NULL;
  3043. }
  3044. static bool brcmf_sdbrcm_probe_malloc(struct brcmf_sdio *bus)
  3045. {
  3046. brcmf_dbg(TRACE, "Enter\n");
  3047. if (bus->sdiodev->bus_if->maxctl) {
  3048. bus->rxblen =
  3049. roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
  3050. ALIGNMENT) + BRCMF_SDALIGN;
  3051. bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
  3052. if (!(bus->rxbuf))
  3053. goto fail;
  3054. }
  3055. /* Allocate buffer to receive glomed packet */
  3056. bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
  3057. if (!(bus->databuf)) {
  3058. /* release rxbuf which was already located as above */
  3059. if (!bus->rxblen)
  3060. kfree(bus->rxbuf);
  3061. goto fail;
  3062. }
  3063. /* Align the buffer */
  3064. if ((unsigned long)bus->databuf % BRCMF_SDALIGN)
  3065. bus->dataptr = bus->databuf + (BRCMF_SDALIGN -
  3066. ((unsigned long)bus->databuf % BRCMF_SDALIGN));
  3067. else
  3068. bus->dataptr = bus->databuf;
  3069. return true;
  3070. fail:
  3071. return false;
  3072. }
  3073. static bool
  3074. brcmf_sdbrcm_probe_attach(struct brcmf_sdio *bus, u32 regsva)
  3075. {
  3076. u8 clkctl = 0;
  3077. int err = 0;
  3078. int reg_addr;
  3079. u32 reg_val;
  3080. u8 idx;
  3081. bus->alp_only = true;
  3082. pr_debug("F1 signature read @0x18000000=0x%4x\n",
  3083. brcmf_sdio_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
  3084. /*
  3085. * Force PLL off until brcmf_sdio_chip_attach()
  3086. * programs PLL control regs
  3087. */
  3088. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
  3089. BRCMF_INIT_CLKCTL1, &err);
  3090. if (!err)
  3091. clkctl = brcmf_sdio_regrb(bus->sdiodev,
  3092. SBSDIO_FUNC1_CHIPCLKCSR, &err);
  3093. if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
  3094. brcmf_dbg(ERROR, "ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
  3095. err, BRCMF_INIT_CLKCTL1, clkctl);
  3096. goto fail;
  3097. }
  3098. if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci, regsva)) {
  3099. brcmf_dbg(ERROR, "brcmf_sdio_chip_attach failed!\n");
  3100. goto fail;
  3101. }
  3102. if (!brcmf_sdbrcm_chipmatch((u16) bus->ci->chip)) {
  3103. brcmf_dbg(ERROR, "unsupported chip: 0x%04x\n", bus->ci->chip);
  3104. goto fail;
  3105. }
  3106. brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci,
  3107. SDIO_DRIVE_STRENGTH);
  3108. /* Get info on the SOCRAM cores... */
  3109. bus->ramsize = bus->ci->ramsize;
  3110. if (!(bus->ramsize)) {
  3111. brcmf_dbg(ERROR, "failed to find SOCRAM memory!\n");
  3112. goto fail;
  3113. }
  3114. /* Set core control so an SDIO reset does a backplane reset */
  3115. idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
  3116. reg_addr = bus->ci->c_inf[idx].base +
  3117. offsetof(struct sdpcmd_regs, corecontrol);
  3118. reg_val = brcmf_sdio_regrl(bus->sdiodev, reg_addr, NULL);
  3119. brcmf_sdio_regwl(bus->sdiodev, reg_addr, reg_val | CC_BPRESEN, NULL);
  3120. brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
  3121. /* Locate an appropriately-aligned portion of hdrbuf */
  3122. bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
  3123. BRCMF_SDALIGN);
  3124. /* Set the poll and/or interrupt flags */
  3125. bus->intr = true;
  3126. bus->poll = false;
  3127. if (bus->poll)
  3128. bus->pollrate = 1;
  3129. return true;
  3130. fail:
  3131. return false;
  3132. }
  3133. static bool brcmf_sdbrcm_probe_init(struct brcmf_sdio *bus)
  3134. {
  3135. brcmf_dbg(TRACE, "Enter\n");
  3136. /* Disable F2 to clear any intermediate frame state on the dongle */
  3137. brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx,
  3138. SDIO_FUNC_ENABLE_1, NULL);
  3139. bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
  3140. bus->rxflow = false;
  3141. /* Done with backplane-dependent accesses, can drop clock... */
  3142. brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
  3143. /* ...and initialize clock/power states */
  3144. bus->clkstate = CLK_SDONLY;
  3145. bus->idletime = BRCMF_IDLE_INTERVAL;
  3146. bus->idleclock = BRCMF_IDLE_ACTIVE;
  3147. /* Query the F2 block size, set roundup accordingly */
  3148. bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
  3149. bus->roundup = min(max_roundup, bus->blocksize);
  3150. /* bus module does not support packet chaining */
  3151. bus->use_rxchain = false;
  3152. bus->sd_rxchain = false;
  3153. return true;
  3154. }
  3155. static int
  3156. brcmf_sdbrcm_watchdog_thread(void *data)
  3157. {
  3158. struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
  3159. allow_signal(SIGTERM);
  3160. /* Run until signal received */
  3161. while (1) {
  3162. if (kthread_should_stop())
  3163. break;
  3164. if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
  3165. brcmf_sdbrcm_bus_watchdog(bus);
  3166. /* Count the tick for reference */
  3167. bus->sdcnt.tickcnt++;
  3168. } else
  3169. break;
  3170. }
  3171. return 0;
  3172. }
  3173. static void
  3174. brcmf_sdbrcm_watchdog(unsigned long data)
  3175. {
  3176. struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
  3177. if (bus->watchdog_tsk) {
  3178. complete(&bus->watchdog_wait);
  3179. /* Reschedule the watchdog */
  3180. if (bus->wd_timer_valid)
  3181. mod_timer(&bus->timer,
  3182. jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
  3183. }
  3184. }
  3185. static void brcmf_sdbrcm_release_dongle(struct brcmf_sdio *bus)
  3186. {
  3187. brcmf_dbg(TRACE, "Enter\n");
  3188. if (bus->ci) {
  3189. brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
  3190. brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
  3191. brcmf_sdio_chip_detach(&bus->ci);
  3192. if (bus->vars && bus->varsz)
  3193. kfree(bus->vars);
  3194. bus->vars = NULL;
  3195. }
  3196. brcmf_dbg(TRACE, "Disconnected\n");
  3197. }
  3198. /* Detach and free everything */
  3199. static void brcmf_sdbrcm_release(struct brcmf_sdio *bus)
  3200. {
  3201. brcmf_dbg(TRACE, "Enter\n");
  3202. if (bus) {
  3203. /* De-register interrupt handler */
  3204. brcmf_sdio_intr_unregister(bus->sdiodev);
  3205. cancel_work_sync(&bus->datawork);
  3206. destroy_workqueue(bus->brcmf_wq);
  3207. if (bus->sdiodev->bus_if->drvr) {
  3208. brcmf_detach(bus->sdiodev->dev);
  3209. brcmf_sdbrcm_release_dongle(bus);
  3210. }
  3211. brcmf_sdbrcm_release_malloc(bus);
  3212. kfree(bus);
  3213. }
  3214. brcmf_dbg(TRACE, "Disconnected\n");
  3215. }
  3216. void *brcmf_sdbrcm_probe(u32 regsva, struct brcmf_sdio_dev *sdiodev)
  3217. {
  3218. int ret;
  3219. struct brcmf_sdio *bus;
  3220. struct brcmf_bus_dcmd *dlst;
  3221. u32 dngl_txglom;
  3222. u32 dngl_txglomalign;
  3223. u8 idx;
  3224. brcmf_dbg(TRACE, "Enter\n");
  3225. /* We make an assumption about address window mappings:
  3226. * regsva == SI_ENUM_BASE*/
  3227. /* Allocate private bus interface state */
  3228. bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
  3229. if (!bus)
  3230. goto fail;
  3231. bus->sdiodev = sdiodev;
  3232. sdiodev->bus = bus;
  3233. skb_queue_head_init(&bus->glom);
  3234. bus->txbound = BRCMF_TXBOUND;
  3235. bus->rxbound = BRCMF_RXBOUND;
  3236. bus->txminmax = BRCMF_TXMINMAX;
  3237. bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
  3238. /* attempt to attach to the dongle */
  3239. if (!(brcmf_sdbrcm_probe_attach(bus, regsva))) {
  3240. brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_attach failed\n");
  3241. goto fail;
  3242. }
  3243. spin_lock_init(&bus->txqlock);
  3244. init_waitqueue_head(&bus->ctrl_wait);
  3245. init_waitqueue_head(&bus->dcmd_resp_wait);
  3246. bus->brcmf_wq = create_singlethread_workqueue("brcmf_wq");
  3247. if (bus->brcmf_wq == NULL) {
  3248. brcmf_dbg(ERROR, "insufficient memory to create txworkqueue\n");
  3249. goto fail;
  3250. }
  3251. INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
  3252. /* Set up the watchdog timer */
  3253. init_timer(&bus->timer);
  3254. bus->timer.data = (unsigned long)bus;
  3255. bus->timer.function = brcmf_sdbrcm_watchdog;
  3256. /* Initialize thread based operation and lock */
  3257. sema_init(&bus->sdsem, 1);
  3258. /* Initialize watchdog thread */
  3259. init_completion(&bus->watchdog_wait);
  3260. bus->watchdog_tsk = kthread_run(brcmf_sdbrcm_watchdog_thread,
  3261. bus, "brcmf_watchdog");
  3262. if (IS_ERR(bus->watchdog_tsk)) {
  3263. pr_warn("brcmf_watchdog thread failed to start\n");
  3264. bus->watchdog_tsk = NULL;
  3265. }
  3266. /* Initialize DPC thread */
  3267. INIT_LIST_HEAD(&bus->dpc_tsklst);
  3268. spin_lock_init(&bus->dpc_tl_lock);
  3269. /* Assign bus interface call back */
  3270. bus->sdiodev->bus_if->brcmf_bus_stop = brcmf_sdbrcm_bus_stop;
  3271. bus->sdiodev->bus_if->brcmf_bus_init = brcmf_sdbrcm_bus_init;
  3272. bus->sdiodev->bus_if->brcmf_bus_txdata = brcmf_sdbrcm_bus_txdata;
  3273. bus->sdiodev->bus_if->brcmf_bus_txctl = brcmf_sdbrcm_bus_txctl;
  3274. bus->sdiodev->bus_if->brcmf_bus_rxctl = brcmf_sdbrcm_bus_rxctl;
  3275. /* Attach to the brcmf/OS/network interface */
  3276. ret = brcmf_attach(SDPCM_RESERVE, bus->sdiodev->dev);
  3277. if (ret != 0) {
  3278. brcmf_dbg(ERROR, "brcmf_attach failed\n");
  3279. goto fail;
  3280. }
  3281. /* Allocate buffers */
  3282. if (!(brcmf_sdbrcm_probe_malloc(bus))) {
  3283. brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_malloc failed\n");
  3284. goto fail;
  3285. }
  3286. if (!(brcmf_sdbrcm_probe_init(bus))) {
  3287. brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_init failed\n");
  3288. goto fail;
  3289. }
  3290. brcmf_sdio_debugfs_create(bus);
  3291. brcmf_dbg(INFO, "completed!!\n");
  3292. /* sdio bus core specific dcmd */
  3293. idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
  3294. dlst = kzalloc(sizeof(struct brcmf_bus_dcmd), GFP_KERNEL);
  3295. if (dlst) {
  3296. if (bus->ci->c_inf[idx].rev < 12) {
  3297. /* for sdio core rev < 12, disable txgloming */
  3298. dngl_txglom = 0;
  3299. dlst->name = "bus:txglom";
  3300. dlst->param = (char *)&dngl_txglom;
  3301. dlst->param_len = sizeof(u32);
  3302. } else {
  3303. /* otherwise, set txglomalign */
  3304. dngl_txglomalign = bus->sdiodev->bus_if->align;
  3305. dlst->name = "bus:txglomalign";
  3306. dlst->param = (char *)&dngl_txglomalign;
  3307. dlst->param_len = sizeof(u32);
  3308. }
  3309. list_add(&dlst->list, &bus->sdiodev->bus_if->dcmd_list);
  3310. }
  3311. /* if firmware path present try to download and bring up bus */
  3312. ret = brcmf_bus_start(bus->sdiodev->dev);
  3313. if (ret != 0) {
  3314. if (ret == -ENOLINK) {
  3315. brcmf_dbg(ERROR, "dongle is not responding\n");
  3316. goto fail;
  3317. }
  3318. }
  3319. return bus;
  3320. fail:
  3321. brcmf_sdbrcm_release(bus);
  3322. return NULL;
  3323. }
  3324. void brcmf_sdbrcm_disconnect(void *ptr)
  3325. {
  3326. struct brcmf_sdio *bus = (struct brcmf_sdio *)ptr;
  3327. brcmf_dbg(TRACE, "Enter\n");
  3328. if (bus)
  3329. brcmf_sdbrcm_release(bus);
  3330. brcmf_dbg(TRACE, "Disconnected\n");
  3331. }
  3332. void
  3333. brcmf_sdbrcm_wd_timer(struct brcmf_sdio *bus, uint wdtick)
  3334. {
  3335. /* Totally stop the timer */
  3336. if (!wdtick && bus->wd_timer_valid) {
  3337. del_timer_sync(&bus->timer);
  3338. bus->wd_timer_valid = false;
  3339. bus->save_ms = wdtick;
  3340. return;
  3341. }
  3342. /* don't start the wd until fw is loaded */
  3343. if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN)
  3344. return;
  3345. if (wdtick) {
  3346. if (bus->save_ms != BRCMF_WD_POLL_MS) {
  3347. if (bus->wd_timer_valid)
  3348. /* Stop timer and restart at new value */
  3349. del_timer_sync(&bus->timer);
  3350. /* Create timer again when watchdog period is
  3351. dynamically changed or in the first instance
  3352. */
  3353. bus->timer.expires =
  3354. jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
  3355. add_timer(&bus->timer);
  3356. } else {
  3357. /* Re arm the timer, at last watchdog period */
  3358. mod_timer(&bus->timer,
  3359. jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
  3360. }
  3361. bus->wd_timer_valid = true;
  3362. bus->save_ms = wdtick;
  3363. }
  3364. }