pcie.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733
  1. /*
  2. * Marvell Wireless LAN device driver: PCIE specific handling
  3. *
  4. * Copyright (C) 2011-2014, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available by writing to the Free Software Foundation, Inc.,
  11. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. *
  14. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  17. * this warranty disclaimer.
  18. */
  19. #include <linux/firmware.h>
  20. #include "decl.h"
  21. #include "ioctl.h"
  22. #include "util.h"
  23. #include "fw.h"
  24. #include "main.h"
  25. #include "wmm.h"
  26. #include "11n.h"
  27. #include "pcie.h"
  28. #define PCIE_VERSION "1.0"
  29. #define DRV_NAME "Marvell mwifiex PCIe"
  30. static u8 user_rmmod;
  31. static struct mwifiex_if_ops pcie_ops;
  32. static struct semaphore add_remove_card_sem;
  33. static struct memory_type_mapping mem_type_mapping_tbl[] = {
  34. {"ITCM", NULL, 0, 0xF0},
  35. {"DTCM", NULL, 0, 0xF1},
  36. {"SQRAM", NULL, 0, 0xF2},
  37. {"IRAM", NULL, 0, 0xF3},
  38. {"APU", NULL, 0, 0xF4},
  39. {"CIU", NULL, 0, 0xF5},
  40. {"ICU", NULL, 0, 0xF6},
  41. {"MAC", NULL, 0, 0xF7},
  42. };
  43. static int
  44. mwifiex_map_pci_memory(struct mwifiex_adapter *adapter, struct sk_buff *skb,
  45. size_t size, int flags)
  46. {
  47. struct pcie_service_card *card = adapter->card;
  48. struct mwifiex_dma_mapping mapping;
  49. mapping.addr = pci_map_single(card->dev, skb->data, size, flags);
  50. if (pci_dma_mapping_error(card->dev, mapping.addr)) {
  51. mwifiex_dbg(adapter, ERROR, "failed to map pci memory!\n");
  52. return -1;
  53. }
  54. mapping.len = size;
  55. mwifiex_store_mapping(skb, &mapping);
  56. return 0;
  57. }
  58. static void mwifiex_unmap_pci_memory(struct mwifiex_adapter *adapter,
  59. struct sk_buff *skb, int flags)
  60. {
  61. struct pcie_service_card *card = adapter->card;
  62. struct mwifiex_dma_mapping mapping;
  63. mwifiex_get_mapping(skb, &mapping);
  64. pci_unmap_single(card->dev, mapping.addr, mapping.len, flags);
  65. }
  66. /*
  67. * This function reads sleep cookie and checks if FW is ready
  68. */
  69. static bool mwifiex_pcie_ok_to_access_hw(struct mwifiex_adapter *adapter)
  70. {
  71. u32 *cookie_addr;
  72. struct pcie_service_card *card = adapter->card;
  73. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  74. if (!reg->sleep_cookie)
  75. return true;
  76. if (card->sleep_cookie_vbase) {
  77. cookie_addr = (u32 *)card->sleep_cookie_vbase;
  78. mwifiex_dbg(adapter, INFO,
  79. "info: ACCESS_HW: sleep cookie=0x%x\n",
  80. *cookie_addr);
  81. if (*cookie_addr == FW_AWAKE_COOKIE)
  82. return true;
  83. }
  84. return false;
  85. }
  86. #ifdef CONFIG_PM_SLEEP
  87. /*
  88. * Kernel needs to suspend all functions separately. Therefore all
  89. * registered functions must have drivers with suspend and resume
  90. * methods. Failing that the kernel simply removes the whole card.
  91. *
  92. * If already not suspended, this function allocates and sends a host
  93. * sleep activate request to the firmware and turns off the traffic.
  94. */
  95. static int mwifiex_pcie_suspend(struct device *dev)
  96. {
  97. struct mwifiex_adapter *adapter;
  98. struct pcie_service_card *card;
  99. int hs_actived;
  100. struct pci_dev *pdev = to_pci_dev(dev);
  101. if (pdev) {
  102. card = pci_get_drvdata(pdev);
  103. if (!card || !card->adapter) {
  104. pr_err("Card or adapter structure is not valid\n");
  105. return 0;
  106. }
  107. } else {
  108. pr_err("PCIE device is not specified\n");
  109. return 0;
  110. }
  111. adapter = card->adapter;
  112. hs_actived = mwifiex_enable_hs(adapter);
  113. /* Indicate device suspended */
  114. adapter->is_suspended = true;
  115. adapter->hs_enabling = false;
  116. return 0;
  117. }
  118. /*
  119. * Kernel needs to suspend all functions separately. Therefore all
  120. * registered functions must have drivers with suspend and resume
  121. * methods. Failing that the kernel simply removes the whole card.
  122. *
  123. * If already not resumed, this function turns on the traffic and
  124. * sends a host sleep cancel request to the firmware.
  125. */
  126. static int mwifiex_pcie_resume(struct device *dev)
  127. {
  128. struct mwifiex_adapter *adapter;
  129. struct pcie_service_card *card;
  130. struct pci_dev *pdev = to_pci_dev(dev);
  131. if (pdev) {
  132. card = pci_get_drvdata(pdev);
  133. if (!card || !card->adapter) {
  134. pr_err("Card or adapter structure is not valid\n");
  135. return 0;
  136. }
  137. } else {
  138. pr_err("PCIE device is not specified\n");
  139. return 0;
  140. }
  141. adapter = card->adapter;
  142. if (!adapter->is_suspended) {
  143. mwifiex_dbg(adapter, WARN,
  144. "Device already resumed\n");
  145. return 0;
  146. }
  147. adapter->is_suspended = false;
  148. mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
  149. MWIFIEX_ASYNC_CMD);
  150. return 0;
  151. }
  152. #endif
  153. /*
  154. * This function probes an mwifiex device and registers it. It allocates
  155. * the card structure, enables PCIE function number and initiates the
  156. * device registration and initialization procedure by adding a logical
  157. * interface.
  158. */
  159. static int mwifiex_pcie_probe(struct pci_dev *pdev,
  160. const struct pci_device_id *ent)
  161. {
  162. struct pcie_service_card *card;
  163. pr_debug("info: vendor=0x%4.04X device=0x%4.04X rev=%d\n",
  164. pdev->vendor, pdev->device, pdev->revision);
  165. card = kzalloc(sizeof(struct pcie_service_card), GFP_KERNEL);
  166. if (!card)
  167. return -ENOMEM;
  168. card->dev = pdev;
  169. if (ent->driver_data) {
  170. struct mwifiex_pcie_device *data = (void *)ent->driver_data;
  171. card->pcie.firmware = data->firmware;
  172. card->pcie.reg = data->reg;
  173. card->pcie.blksz_fw_dl = data->blksz_fw_dl;
  174. card->pcie.tx_buf_size = data->tx_buf_size;
  175. card->pcie.can_dump_fw = data->can_dump_fw;
  176. card->pcie.can_ext_scan = data->can_ext_scan;
  177. }
  178. if (mwifiex_add_card(card, &add_remove_card_sem, &pcie_ops,
  179. MWIFIEX_PCIE)) {
  180. pr_err("%s failed\n", __func__);
  181. kfree(card);
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. /*
  187. * This function removes the interface and frees up the card structure.
  188. */
  189. static void mwifiex_pcie_remove(struct pci_dev *pdev)
  190. {
  191. struct pcie_service_card *card;
  192. struct mwifiex_adapter *adapter;
  193. struct mwifiex_private *priv;
  194. card = pci_get_drvdata(pdev);
  195. if (!card)
  196. return;
  197. adapter = card->adapter;
  198. if (!adapter || !adapter->priv_num)
  199. return;
  200. if (user_rmmod) {
  201. #ifdef CONFIG_PM_SLEEP
  202. if (adapter->is_suspended)
  203. mwifiex_pcie_resume(&pdev->dev);
  204. #endif
  205. mwifiex_deauthenticate_all(adapter);
  206. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  207. mwifiex_disable_auto_ds(priv);
  208. mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
  209. }
  210. mwifiex_remove_card(card->adapter, &add_remove_card_sem);
  211. }
  212. static void mwifiex_pcie_shutdown(struct pci_dev *pdev)
  213. {
  214. user_rmmod = 1;
  215. mwifiex_pcie_remove(pdev);
  216. return;
  217. }
  218. static const struct pci_device_id mwifiex_ids[] = {
  219. {
  220. PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8766P,
  221. PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  222. .driver_data = (unsigned long) &mwifiex_pcie8766,
  223. },
  224. {
  225. PCIE_VENDOR_ID_MARVELL, PCIE_DEVICE_ID_MARVELL_88W8897,
  226. PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  227. .driver_data = (unsigned long) &mwifiex_pcie8897,
  228. },
  229. {},
  230. };
  231. MODULE_DEVICE_TABLE(pci, mwifiex_ids);
  232. #ifdef CONFIG_PM_SLEEP
  233. /* Power Management Hooks */
  234. static SIMPLE_DEV_PM_OPS(mwifiex_pcie_pm_ops, mwifiex_pcie_suspend,
  235. mwifiex_pcie_resume);
  236. #endif
  237. /* PCI Device Driver */
  238. static struct pci_driver __refdata mwifiex_pcie = {
  239. .name = "mwifiex_pcie",
  240. .id_table = mwifiex_ids,
  241. .probe = mwifiex_pcie_probe,
  242. .remove = mwifiex_pcie_remove,
  243. #ifdef CONFIG_PM_SLEEP
  244. .driver = {
  245. .pm = &mwifiex_pcie_pm_ops,
  246. },
  247. #endif
  248. .shutdown = mwifiex_pcie_shutdown,
  249. };
  250. /*
  251. * This function writes data into PCIE card register.
  252. */
  253. static int mwifiex_write_reg(struct mwifiex_adapter *adapter, int reg, u32 data)
  254. {
  255. struct pcie_service_card *card = adapter->card;
  256. iowrite32(data, card->pci_mmap1 + reg);
  257. return 0;
  258. }
  259. /*
  260. * This function reads data from PCIE card register.
  261. */
  262. static int mwifiex_read_reg(struct mwifiex_adapter *adapter, int reg, u32 *data)
  263. {
  264. struct pcie_service_card *card = adapter->card;
  265. *data = ioread32(card->pci_mmap1 + reg);
  266. return 0;
  267. }
  268. /* This function reads u8 data from PCIE card register. */
  269. static int mwifiex_read_reg_byte(struct mwifiex_adapter *adapter,
  270. int reg, u8 *data)
  271. {
  272. struct pcie_service_card *card = adapter->card;
  273. *data = ioread8(card->pci_mmap1 + reg);
  274. return 0;
  275. }
  276. /*
  277. * This function adds delay loop to ensure FW is awake before proceeding.
  278. */
  279. static void mwifiex_pcie_dev_wakeup_delay(struct mwifiex_adapter *adapter)
  280. {
  281. int i = 0;
  282. while (mwifiex_pcie_ok_to_access_hw(adapter)) {
  283. i++;
  284. usleep_range(10, 20);
  285. /* 50ms max wait */
  286. if (i == 5000)
  287. break;
  288. }
  289. return;
  290. }
  291. static void mwifiex_delay_for_sleep_cookie(struct mwifiex_adapter *adapter,
  292. u32 max_delay_loop_cnt)
  293. {
  294. struct pcie_service_card *card = adapter->card;
  295. u8 *buffer;
  296. u32 sleep_cookie, count;
  297. for (count = 0; count < max_delay_loop_cnt; count++) {
  298. buffer = card->cmdrsp_buf->data - INTF_HEADER_LEN;
  299. sleep_cookie = *(u32 *)buffer;
  300. if (sleep_cookie == MWIFIEX_DEF_SLEEP_COOKIE) {
  301. mwifiex_dbg(adapter, INFO,
  302. "sleep cookie found at count %d\n", count);
  303. break;
  304. }
  305. usleep_range(20, 30);
  306. }
  307. if (count >= max_delay_loop_cnt)
  308. mwifiex_dbg(adapter, INFO,
  309. "max count reached while accessing sleep cookie\n");
  310. }
  311. /* This function wakes up the card by reading fw_status register. */
  312. static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
  313. {
  314. u32 fw_status;
  315. struct pcie_service_card *card = adapter->card;
  316. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  317. mwifiex_dbg(adapter, EVENT,
  318. "event: Wakeup device...\n");
  319. if (reg->sleep_cookie)
  320. mwifiex_pcie_dev_wakeup_delay(adapter);
  321. /* Reading fw_status register will wakeup device */
  322. if (mwifiex_read_reg(adapter, reg->fw_status, &fw_status)) {
  323. mwifiex_dbg(adapter, ERROR,
  324. "Reading fw_status register failed\n");
  325. return -1;
  326. }
  327. if (reg->sleep_cookie) {
  328. mwifiex_pcie_dev_wakeup_delay(adapter);
  329. mwifiex_dbg(adapter, INFO,
  330. "PCIE wakeup: Setting PS_STATE_AWAKE\n");
  331. adapter->ps_state = PS_STATE_AWAKE;
  332. }
  333. return 0;
  334. }
  335. /*
  336. * This function is called after the card has woken up.
  337. *
  338. * The card configuration register is reset.
  339. */
  340. static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
  341. {
  342. mwifiex_dbg(adapter, CMD,
  343. "cmd: Wakeup device completed\n");
  344. return 0;
  345. }
  346. /*
  347. * This function disables the host interrupt.
  348. *
  349. * The host interrupt mask is read, the disable bit is reset and
  350. * written back to the card host interrupt mask register.
  351. */
  352. static int mwifiex_pcie_disable_host_int(struct mwifiex_adapter *adapter)
  353. {
  354. if (mwifiex_pcie_ok_to_access_hw(adapter)) {
  355. if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
  356. 0x00000000)) {
  357. mwifiex_dbg(adapter, ERROR,
  358. "Disable host interrupt failed\n");
  359. return -1;
  360. }
  361. }
  362. return 0;
  363. }
  364. /*
  365. * This function enables the host interrupt.
  366. *
  367. * The host interrupt enable mask is written to the card
  368. * host interrupt mask register.
  369. */
  370. static int mwifiex_pcie_enable_host_int(struct mwifiex_adapter *adapter)
  371. {
  372. if (mwifiex_pcie_ok_to_access_hw(adapter)) {
  373. /* Simply write the mask to the register */
  374. if (mwifiex_write_reg(adapter, PCIE_HOST_INT_MASK,
  375. HOST_INTR_MASK)) {
  376. mwifiex_dbg(adapter, ERROR,
  377. "Enable host interrupt failed\n");
  378. return -1;
  379. }
  380. }
  381. return 0;
  382. }
  383. /*
  384. * This function initializes TX buffer ring descriptors
  385. */
  386. static int mwifiex_init_txq_ring(struct mwifiex_adapter *adapter)
  387. {
  388. struct pcie_service_card *card = adapter->card;
  389. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  390. struct mwifiex_pcie_buf_desc *desc;
  391. struct mwifiex_pfu_buf_desc *desc2;
  392. int i;
  393. for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
  394. card->tx_buf_list[i] = NULL;
  395. if (reg->pfu_enabled) {
  396. card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
  397. (sizeof(*desc2) * i);
  398. desc2 = card->txbd_ring[i];
  399. memset(desc2, 0, sizeof(*desc2));
  400. } else {
  401. card->txbd_ring[i] = (void *)card->txbd_ring_vbase +
  402. (sizeof(*desc) * i);
  403. desc = card->txbd_ring[i];
  404. memset(desc, 0, sizeof(*desc));
  405. }
  406. }
  407. return 0;
  408. }
  409. /* This function initializes RX buffer ring descriptors. Each SKB is allocated
  410. * here and after mapping PCI memory, its physical address is assigned to
  411. * PCIE Rx buffer descriptor's physical address.
  412. */
  413. static int mwifiex_init_rxq_ring(struct mwifiex_adapter *adapter)
  414. {
  415. struct pcie_service_card *card = adapter->card;
  416. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  417. struct sk_buff *skb;
  418. struct mwifiex_pcie_buf_desc *desc;
  419. struct mwifiex_pfu_buf_desc *desc2;
  420. dma_addr_t buf_pa;
  421. int i;
  422. for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
  423. /* Allocate skb here so that firmware can DMA data from it */
  424. skb = mwifiex_alloc_dma_align_buf(MWIFIEX_RX_DATA_BUF_SIZE,
  425. GFP_KERNEL | GFP_DMA);
  426. if (!skb) {
  427. mwifiex_dbg(adapter, ERROR,
  428. "Unable to allocate skb for RX ring.\n");
  429. kfree(card->rxbd_ring_vbase);
  430. return -ENOMEM;
  431. }
  432. if (mwifiex_map_pci_memory(adapter, skb,
  433. MWIFIEX_RX_DATA_BUF_SIZE,
  434. PCI_DMA_FROMDEVICE))
  435. return -1;
  436. buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
  437. mwifiex_dbg(adapter, INFO,
  438. "info: RX ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
  439. skb, skb->len, skb->data, (u32)buf_pa,
  440. (u32)((u64)buf_pa >> 32));
  441. card->rx_buf_list[i] = skb;
  442. if (reg->pfu_enabled) {
  443. card->rxbd_ring[i] = (void *)card->rxbd_ring_vbase +
  444. (sizeof(*desc2) * i);
  445. desc2 = card->rxbd_ring[i];
  446. desc2->paddr = buf_pa;
  447. desc2->len = (u16)skb->len;
  448. desc2->frag_len = (u16)skb->len;
  449. desc2->flags = reg->ring_flag_eop | reg->ring_flag_sop;
  450. desc2->offset = 0;
  451. } else {
  452. card->rxbd_ring[i] = (void *)(card->rxbd_ring_vbase +
  453. (sizeof(*desc) * i));
  454. desc = card->rxbd_ring[i];
  455. desc->paddr = buf_pa;
  456. desc->len = (u16)skb->len;
  457. desc->flags = 0;
  458. }
  459. }
  460. return 0;
  461. }
  462. /* This function initializes event buffer ring descriptors. Each SKB is
  463. * allocated here and after mapping PCI memory, its physical address is assigned
  464. * to PCIE Rx buffer descriptor's physical address
  465. */
  466. static int mwifiex_pcie_init_evt_ring(struct mwifiex_adapter *adapter)
  467. {
  468. struct pcie_service_card *card = adapter->card;
  469. struct mwifiex_evt_buf_desc *desc;
  470. struct sk_buff *skb;
  471. dma_addr_t buf_pa;
  472. int i;
  473. for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
  474. /* Allocate skb here so that firmware can DMA data from it */
  475. skb = dev_alloc_skb(MAX_EVENT_SIZE);
  476. if (!skb) {
  477. mwifiex_dbg(adapter, ERROR,
  478. "Unable to allocate skb for EVENT buf.\n");
  479. kfree(card->evtbd_ring_vbase);
  480. return -ENOMEM;
  481. }
  482. skb_put(skb, MAX_EVENT_SIZE);
  483. if (mwifiex_map_pci_memory(adapter, skb, MAX_EVENT_SIZE,
  484. PCI_DMA_FROMDEVICE))
  485. return -1;
  486. buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
  487. mwifiex_dbg(adapter, EVENT,
  488. "info: EVT ring: skb=%p len=%d data=%p buf_pa=%#x:%x\n",
  489. skb, skb->len, skb->data, (u32)buf_pa,
  490. (u32)((u64)buf_pa >> 32));
  491. card->evt_buf_list[i] = skb;
  492. card->evtbd_ring[i] = (void *)(card->evtbd_ring_vbase +
  493. (sizeof(*desc) * i));
  494. desc = card->evtbd_ring[i];
  495. desc->paddr = buf_pa;
  496. desc->len = (u16)skb->len;
  497. desc->flags = 0;
  498. }
  499. return 0;
  500. }
  501. /* This function cleans up TX buffer rings. If any of the buffer list has valid
  502. * SKB address, associated SKB is freed.
  503. */
  504. static void mwifiex_cleanup_txq_ring(struct mwifiex_adapter *adapter)
  505. {
  506. struct pcie_service_card *card = adapter->card;
  507. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  508. struct sk_buff *skb;
  509. struct mwifiex_pcie_buf_desc *desc;
  510. struct mwifiex_pfu_buf_desc *desc2;
  511. int i;
  512. for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
  513. if (reg->pfu_enabled) {
  514. desc2 = card->txbd_ring[i];
  515. if (card->tx_buf_list[i]) {
  516. skb = card->tx_buf_list[i];
  517. mwifiex_unmap_pci_memory(adapter, skb,
  518. PCI_DMA_TODEVICE);
  519. dev_kfree_skb_any(skb);
  520. }
  521. memset(desc2, 0, sizeof(*desc2));
  522. } else {
  523. desc = card->txbd_ring[i];
  524. if (card->tx_buf_list[i]) {
  525. skb = card->tx_buf_list[i];
  526. mwifiex_unmap_pci_memory(adapter, skb,
  527. PCI_DMA_TODEVICE);
  528. dev_kfree_skb_any(skb);
  529. }
  530. memset(desc, 0, sizeof(*desc));
  531. }
  532. card->tx_buf_list[i] = NULL;
  533. }
  534. return;
  535. }
  536. /* This function cleans up RX buffer rings. If any of the buffer list has valid
  537. * SKB address, associated SKB is freed.
  538. */
  539. static void mwifiex_cleanup_rxq_ring(struct mwifiex_adapter *adapter)
  540. {
  541. struct pcie_service_card *card = adapter->card;
  542. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  543. struct mwifiex_pcie_buf_desc *desc;
  544. struct mwifiex_pfu_buf_desc *desc2;
  545. struct sk_buff *skb;
  546. int i;
  547. for (i = 0; i < MWIFIEX_MAX_TXRX_BD; i++) {
  548. if (reg->pfu_enabled) {
  549. desc2 = card->rxbd_ring[i];
  550. if (card->rx_buf_list[i]) {
  551. skb = card->rx_buf_list[i];
  552. mwifiex_unmap_pci_memory(adapter, skb,
  553. PCI_DMA_FROMDEVICE);
  554. dev_kfree_skb_any(skb);
  555. }
  556. memset(desc2, 0, sizeof(*desc2));
  557. } else {
  558. desc = card->rxbd_ring[i];
  559. if (card->rx_buf_list[i]) {
  560. skb = card->rx_buf_list[i];
  561. mwifiex_unmap_pci_memory(adapter, skb,
  562. PCI_DMA_FROMDEVICE);
  563. dev_kfree_skb_any(skb);
  564. }
  565. memset(desc, 0, sizeof(*desc));
  566. }
  567. card->rx_buf_list[i] = NULL;
  568. }
  569. return;
  570. }
  571. /* This function cleans up event buffer rings. If any of the buffer list has
  572. * valid SKB address, associated SKB is freed.
  573. */
  574. static void mwifiex_cleanup_evt_ring(struct mwifiex_adapter *adapter)
  575. {
  576. struct pcie_service_card *card = adapter->card;
  577. struct mwifiex_evt_buf_desc *desc;
  578. struct sk_buff *skb;
  579. int i;
  580. for (i = 0; i < MWIFIEX_MAX_EVT_BD; i++) {
  581. desc = card->evtbd_ring[i];
  582. if (card->evt_buf_list[i]) {
  583. skb = card->evt_buf_list[i];
  584. mwifiex_unmap_pci_memory(adapter, skb,
  585. PCI_DMA_FROMDEVICE);
  586. dev_kfree_skb_any(skb);
  587. }
  588. card->evt_buf_list[i] = NULL;
  589. memset(desc, 0, sizeof(*desc));
  590. }
  591. return;
  592. }
  593. /* This function creates buffer descriptor ring for TX
  594. */
  595. static int mwifiex_pcie_create_txbd_ring(struct mwifiex_adapter *adapter)
  596. {
  597. struct pcie_service_card *card = adapter->card;
  598. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  599. /*
  600. * driver maintaines the write pointer and firmware maintaines the read
  601. * pointer. The write pointer starts at 0 (zero) while the read pointer
  602. * starts at zero with rollover bit set
  603. */
  604. card->txbd_wrptr = 0;
  605. if (reg->pfu_enabled)
  606. card->txbd_rdptr = 0;
  607. else
  608. card->txbd_rdptr |= reg->tx_rollover_ind;
  609. /* allocate shared memory for the BD ring and divide the same in to
  610. several descriptors */
  611. if (reg->pfu_enabled)
  612. card->txbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
  613. MWIFIEX_MAX_TXRX_BD;
  614. else
  615. card->txbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
  616. MWIFIEX_MAX_TXRX_BD;
  617. mwifiex_dbg(adapter, INFO,
  618. "info: txbd_ring: Allocating %d bytes\n",
  619. card->txbd_ring_size);
  620. card->txbd_ring_vbase = pci_alloc_consistent(card->dev,
  621. card->txbd_ring_size,
  622. &card->txbd_ring_pbase);
  623. if (!card->txbd_ring_vbase) {
  624. mwifiex_dbg(adapter, ERROR,
  625. "allocate consistent memory (%d bytes) failed!\n",
  626. card->txbd_ring_size);
  627. return -ENOMEM;
  628. }
  629. mwifiex_dbg(adapter, DATA,
  630. "info: txbd_ring - base: %p, pbase: %#x:%x, len: %x\n",
  631. card->txbd_ring_vbase, (unsigned int)card->txbd_ring_pbase,
  632. (u32)((u64)card->txbd_ring_pbase >> 32),
  633. card->txbd_ring_size);
  634. return mwifiex_init_txq_ring(adapter);
  635. }
  636. static int mwifiex_pcie_delete_txbd_ring(struct mwifiex_adapter *adapter)
  637. {
  638. struct pcie_service_card *card = adapter->card;
  639. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  640. mwifiex_cleanup_txq_ring(adapter);
  641. if (card->txbd_ring_vbase)
  642. pci_free_consistent(card->dev, card->txbd_ring_size,
  643. card->txbd_ring_vbase,
  644. card->txbd_ring_pbase);
  645. card->txbd_ring_size = 0;
  646. card->txbd_wrptr = 0;
  647. card->txbd_rdptr = 0 | reg->tx_rollover_ind;
  648. card->txbd_ring_vbase = NULL;
  649. card->txbd_ring_pbase = 0;
  650. return 0;
  651. }
  652. /*
  653. * This function creates buffer descriptor ring for RX
  654. */
  655. static int mwifiex_pcie_create_rxbd_ring(struct mwifiex_adapter *adapter)
  656. {
  657. struct pcie_service_card *card = adapter->card;
  658. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  659. /*
  660. * driver maintaines the read pointer and firmware maintaines the write
  661. * pointer. The write pointer starts at 0 (zero) while the read pointer
  662. * starts at zero with rollover bit set
  663. */
  664. card->rxbd_wrptr = 0;
  665. card->rxbd_rdptr = reg->rx_rollover_ind;
  666. if (reg->pfu_enabled)
  667. card->rxbd_ring_size = sizeof(struct mwifiex_pfu_buf_desc) *
  668. MWIFIEX_MAX_TXRX_BD;
  669. else
  670. card->rxbd_ring_size = sizeof(struct mwifiex_pcie_buf_desc) *
  671. MWIFIEX_MAX_TXRX_BD;
  672. mwifiex_dbg(adapter, INFO,
  673. "info: rxbd_ring: Allocating %d bytes\n",
  674. card->rxbd_ring_size);
  675. card->rxbd_ring_vbase = pci_alloc_consistent(card->dev,
  676. card->rxbd_ring_size,
  677. &card->rxbd_ring_pbase);
  678. if (!card->rxbd_ring_vbase) {
  679. mwifiex_dbg(adapter, ERROR,
  680. "allocate consistent memory (%d bytes) failed!\n",
  681. card->rxbd_ring_size);
  682. return -ENOMEM;
  683. }
  684. mwifiex_dbg(adapter, DATA,
  685. "info: rxbd_ring - base: %p, pbase: %#x:%x, len: %#x\n",
  686. card->rxbd_ring_vbase, (u32)card->rxbd_ring_pbase,
  687. (u32)((u64)card->rxbd_ring_pbase >> 32),
  688. card->rxbd_ring_size);
  689. return mwifiex_init_rxq_ring(adapter);
  690. }
  691. /*
  692. * This function deletes Buffer descriptor ring for RX
  693. */
  694. static int mwifiex_pcie_delete_rxbd_ring(struct mwifiex_adapter *adapter)
  695. {
  696. struct pcie_service_card *card = adapter->card;
  697. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  698. mwifiex_cleanup_rxq_ring(adapter);
  699. if (card->rxbd_ring_vbase)
  700. pci_free_consistent(card->dev, card->rxbd_ring_size,
  701. card->rxbd_ring_vbase,
  702. card->rxbd_ring_pbase);
  703. card->rxbd_ring_size = 0;
  704. card->rxbd_wrptr = 0;
  705. card->rxbd_rdptr = 0 | reg->rx_rollover_ind;
  706. card->rxbd_ring_vbase = NULL;
  707. card->rxbd_ring_pbase = 0;
  708. return 0;
  709. }
  710. /*
  711. * This function creates buffer descriptor ring for Events
  712. */
  713. static int mwifiex_pcie_create_evtbd_ring(struct mwifiex_adapter *adapter)
  714. {
  715. struct pcie_service_card *card = adapter->card;
  716. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  717. /*
  718. * driver maintaines the read pointer and firmware maintaines the write
  719. * pointer. The write pointer starts at 0 (zero) while the read pointer
  720. * starts at zero with rollover bit set
  721. */
  722. card->evtbd_wrptr = 0;
  723. card->evtbd_rdptr = reg->evt_rollover_ind;
  724. card->evtbd_ring_size = sizeof(struct mwifiex_evt_buf_desc) *
  725. MWIFIEX_MAX_EVT_BD;
  726. mwifiex_dbg(adapter, INFO,
  727. "info: evtbd_ring: Allocating %d bytes\n",
  728. card->evtbd_ring_size);
  729. card->evtbd_ring_vbase = pci_alloc_consistent(card->dev,
  730. card->evtbd_ring_size,
  731. &card->evtbd_ring_pbase);
  732. if (!card->evtbd_ring_vbase) {
  733. mwifiex_dbg(adapter, ERROR,
  734. "allocate consistent memory (%d bytes) failed!\n",
  735. card->evtbd_ring_size);
  736. return -ENOMEM;
  737. }
  738. mwifiex_dbg(adapter, EVENT,
  739. "info: CMDRSP/EVT bd_ring - base: %p pbase: %#x:%x len: %#x\n",
  740. card->evtbd_ring_vbase, (u32)card->evtbd_ring_pbase,
  741. (u32)((u64)card->evtbd_ring_pbase >> 32),
  742. card->evtbd_ring_size);
  743. return mwifiex_pcie_init_evt_ring(adapter);
  744. }
  745. /*
  746. * This function deletes Buffer descriptor ring for Events
  747. */
  748. static int mwifiex_pcie_delete_evtbd_ring(struct mwifiex_adapter *adapter)
  749. {
  750. struct pcie_service_card *card = adapter->card;
  751. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  752. mwifiex_cleanup_evt_ring(adapter);
  753. if (card->evtbd_ring_vbase)
  754. pci_free_consistent(card->dev, card->evtbd_ring_size,
  755. card->evtbd_ring_vbase,
  756. card->evtbd_ring_pbase);
  757. card->evtbd_wrptr = 0;
  758. card->evtbd_rdptr = 0 | reg->evt_rollover_ind;
  759. card->evtbd_ring_size = 0;
  760. card->evtbd_ring_vbase = NULL;
  761. card->evtbd_ring_pbase = 0;
  762. return 0;
  763. }
  764. /*
  765. * This function allocates a buffer for CMDRSP
  766. */
  767. static int mwifiex_pcie_alloc_cmdrsp_buf(struct mwifiex_adapter *adapter)
  768. {
  769. struct pcie_service_card *card = adapter->card;
  770. struct sk_buff *skb;
  771. /* Allocate memory for receiving command response data */
  772. skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
  773. if (!skb) {
  774. mwifiex_dbg(adapter, ERROR,
  775. "Unable to allocate skb for command response data.\n");
  776. return -ENOMEM;
  777. }
  778. skb_put(skb, MWIFIEX_UPLD_SIZE);
  779. if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
  780. PCI_DMA_FROMDEVICE))
  781. return -1;
  782. card->cmdrsp_buf = skb;
  783. return 0;
  784. }
  785. /*
  786. * This function deletes a buffer for CMDRSP
  787. */
  788. static int mwifiex_pcie_delete_cmdrsp_buf(struct mwifiex_adapter *adapter)
  789. {
  790. struct pcie_service_card *card;
  791. if (!adapter)
  792. return 0;
  793. card = adapter->card;
  794. if (card && card->cmdrsp_buf) {
  795. mwifiex_unmap_pci_memory(adapter, card->cmdrsp_buf,
  796. PCI_DMA_FROMDEVICE);
  797. dev_kfree_skb_any(card->cmdrsp_buf);
  798. }
  799. if (card && card->cmd_buf) {
  800. mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
  801. PCI_DMA_TODEVICE);
  802. }
  803. return 0;
  804. }
  805. /*
  806. * This function allocates a buffer for sleep cookie
  807. */
  808. static int mwifiex_pcie_alloc_sleep_cookie_buf(struct mwifiex_adapter *adapter)
  809. {
  810. struct pcie_service_card *card = adapter->card;
  811. card->sleep_cookie_vbase = pci_alloc_consistent(card->dev, sizeof(u32),
  812. &card->sleep_cookie_pbase);
  813. if (!card->sleep_cookie_vbase) {
  814. mwifiex_dbg(adapter, ERROR,
  815. "pci_alloc_consistent failed!\n");
  816. return -ENOMEM;
  817. }
  818. /* Init val of Sleep Cookie */
  819. *(u32 *)card->sleep_cookie_vbase = FW_AWAKE_COOKIE;
  820. mwifiex_dbg(adapter, INFO,
  821. "alloc_scook: sleep cookie=0x%x\n",
  822. *((u32 *)card->sleep_cookie_vbase));
  823. return 0;
  824. }
  825. /*
  826. * This function deletes buffer for sleep cookie
  827. */
  828. static int mwifiex_pcie_delete_sleep_cookie_buf(struct mwifiex_adapter *adapter)
  829. {
  830. struct pcie_service_card *card;
  831. if (!adapter)
  832. return 0;
  833. card = adapter->card;
  834. if (card && card->sleep_cookie_vbase) {
  835. pci_free_consistent(card->dev, sizeof(u32),
  836. card->sleep_cookie_vbase,
  837. card->sleep_cookie_pbase);
  838. card->sleep_cookie_vbase = NULL;
  839. }
  840. return 0;
  841. }
  842. /* This function flushes the TX buffer descriptor ring
  843. * This function defined as handler is also called while cleaning TXRX
  844. * during disconnect/ bss stop.
  845. */
  846. static int mwifiex_clean_pcie_ring_buf(struct mwifiex_adapter *adapter)
  847. {
  848. struct pcie_service_card *card = adapter->card;
  849. if (!mwifiex_pcie_txbd_empty(card, card->txbd_rdptr)) {
  850. card->txbd_flush = 1;
  851. /* write pointer already set at last send
  852. * send dnld-rdy intr again, wait for completion.
  853. */
  854. if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
  855. CPU_INTR_DNLD_RDY)) {
  856. mwifiex_dbg(adapter, ERROR,
  857. "failed to assert dnld-rdy interrupt.\n");
  858. return -1;
  859. }
  860. }
  861. return 0;
  862. }
  863. /*
  864. * This function unmaps and frees downloaded data buffer
  865. */
  866. static int mwifiex_pcie_send_data_complete(struct mwifiex_adapter *adapter)
  867. {
  868. struct sk_buff *skb;
  869. u32 wrdoneidx, rdptr, num_tx_buffs, unmap_count = 0;
  870. struct mwifiex_pcie_buf_desc *desc;
  871. struct mwifiex_pfu_buf_desc *desc2;
  872. struct pcie_service_card *card = adapter->card;
  873. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  874. if (!mwifiex_pcie_ok_to_access_hw(adapter))
  875. mwifiex_pm_wakeup_card(adapter);
  876. /* Read the TX ring read pointer set by firmware */
  877. if (mwifiex_read_reg(adapter, reg->tx_rdptr, &rdptr)) {
  878. mwifiex_dbg(adapter, ERROR,
  879. "SEND COMP: failed to read reg->tx_rdptr\n");
  880. return -1;
  881. }
  882. mwifiex_dbg(adapter, DATA,
  883. "SEND COMP: rdptr_prev=0x%x, rdptr=0x%x\n",
  884. card->txbd_rdptr, rdptr);
  885. num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
  886. /* free from previous txbd_rdptr to current txbd_rdptr */
  887. while (((card->txbd_rdptr & reg->tx_mask) !=
  888. (rdptr & reg->tx_mask)) ||
  889. ((card->txbd_rdptr & reg->tx_rollover_ind) !=
  890. (rdptr & reg->tx_rollover_ind))) {
  891. wrdoneidx = (card->txbd_rdptr & reg->tx_mask) >>
  892. reg->tx_start_ptr;
  893. skb = card->tx_buf_list[wrdoneidx];
  894. if (skb) {
  895. mwifiex_dbg(adapter, DATA,
  896. "SEND COMP: Detach skb %p at txbd_rdidx=%d\n",
  897. skb, wrdoneidx);
  898. mwifiex_unmap_pci_memory(adapter, skb,
  899. PCI_DMA_TODEVICE);
  900. unmap_count++;
  901. if (card->txbd_flush)
  902. mwifiex_write_data_complete(adapter, skb, 0,
  903. -1);
  904. else
  905. mwifiex_write_data_complete(adapter, skb, 0, 0);
  906. }
  907. card->tx_buf_list[wrdoneidx] = NULL;
  908. if (reg->pfu_enabled) {
  909. desc2 = card->txbd_ring[wrdoneidx];
  910. memset(desc2, 0, sizeof(*desc2));
  911. } else {
  912. desc = card->txbd_ring[wrdoneidx];
  913. memset(desc, 0, sizeof(*desc));
  914. }
  915. switch (card->dev->device) {
  916. case PCIE_DEVICE_ID_MARVELL_88W8766P:
  917. card->txbd_rdptr++;
  918. break;
  919. case PCIE_DEVICE_ID_MARVELL_88W8897:
  920. card->txbd_rdptr += reg->ring_tx_start_ptr;
  921. break;
  922. }
  923. if ((card->txbd_rdptr & reg->tx_mask) == num_tx_buffs)
  924. card->txbd_rdptr = ((card->txbd_rdptr &
  925. reg->tx_rollover_ind) ^
  926. reg->tx_rollover_ind);
  927. }
  928. if (unmap_count)
  929. adapter->data_sent = false;
  930. if (card->txbd_flush) {
  931. if (mwifiex_pcie_txbd_empty(card, card->txbd_rdptr))
  932. card->txbd_flush = 0;
  933. else
  934. mwifiex_clean_pcie_ring_buf(adapter);
  935. }
  936. return 0;
  937. }
  938. /* This function sends data buffer to device. First 4 bytes of payload
  939. * are filled with payload length and payload type. Then this payload
  940. * is mapped to PCI device memory. Tx ring pointers are advanced accordingly.
  941. * Download ready interrupt to FW is deffered if Tx ring is not full and
  942. * additional payload can be accomodated.
  943. * Caller must ensure tx_param parameter to this function is not NULL.
  944. */
  945. static int
  946. mwifiex_pcie_send_data(struct mwifiex_adapter *adapter, struct sk_buff *skb,
  947. struct mwifiex_tx_param *tx_param)
  948. {
  949. struct pcie_service_card *card = adapter->card;
  950. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  951. u32 wrindx, num_tx_buffs, rx_val;
  952. int ret;
  953. dma_addr_t buf_pa;
  954. struct mwifiex_pcie_buf_desc *desc = NULL;
  955. struct mwifiex_pfu_buf_desc *desc2 = NULL;
  956. __le16 *tmp;
  957. if (!(skb->data && skb->len)) {
  958. mwifiex_dbg(adapter, ERROR,
  959. "%s(): invalid parameter <%p, %#x>\n",
  960. __func__, skb->data, skb->len);
  961. return -1;
  962. }
  963. if (!mwifiex_pcie_ok_to_access_hw(adapter))
  964. mwifiex_pm_wakeup_card(adapter);
  965. num_tx_buffs = MWIFIEX_MAX_TXRX_BD << reg->tx_start_ptr;
  966. mwifiex_dbg(adapter, DATA,
  967. "info: SEND DATA: <Rd: %#x, Wr: %#x>\n",
  968. card->txbd_rdptr, card->txbd_wrptr);
  969. if (mwifiex_pcie_txbd_not_full(card)) {
  970. u8 *payload;
  971. adapter->data_sent = true;
  972. payload = skb->data;
  973. tmp = (__le16 *)&payload[0];
  974. *tmp = cpu_to_le16((u16)skb->len);
  975. tmp = (__le16 *)&payload[2];
  976. *tmp = cpu_to_le16(MWIFIEX_TYPE_DATA);
  977. if (mwifiex_map_pci_memory(adapter, skb, skb->len,
  978. PCI_DMA_TODEVICE))
  979. return -1;
  980. wrindx = (card->txbd_wrptr & reg->tx_mask) >> reg->tx_start_ptr;
  981. buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
  982. card->tx_buf_list[wrindx] = skb;
  983. if (reg->pfu_enabled) {
  984. desc2 = card->txbd_ring[wrindx];
  985. desc2->paddr = buf_pa;
  986. desc2->len = (u16)skb->len;
  987. desc2->frag_len = (u16)skb->len;
  988. desc2->offset = 0;
  989. desc2->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
  990. MWIFIEX_BD_FLAG_LAST_DESC;
  991. } else {
  992. desc = card->txbd_ring[wrindx];
  993. desc->paddr = buf_pa;
  994. desc->len = (u16)skb->len;
  995. desc->flags = MWIFIEX_BD_FLAG_FIRST_DESC |
  996. MWIFIEX_BD_FLAG_LAST_DESC;
  997. }
  998. switch (card->dev->device) {
  999. case PCIE_DEVICE_ID_MARVELL_88W8766P:
  1000. card->txbd_wrptr++;
  1001. break;
  1002. case PCIE_DEVICE_ID_MARVELL_88W8897:
  1003. card->txbd_wrptr += reg->ring_tx_start_ptr;
  1004. break;
  1005. }
  1006. if ((card->txbd_wrptr & reg->tx_mask) == num_tx_buffs)
  1007. card->txbd_wrptr = ((card->txbd_wrptr &
  1008. reg->tx_rollover_ind) ^
  1009. reg->tx_rollover_ind);
  1010. rx_val = card->rxbd_rdptr & reg->rx_wrap_mask;
  1011. /* Write the TX ring write pointer in to reg->tx_wrptr */
  1012. if (mwifiex_write_reg(adapter, reg->tx_wrptr,
  1013. card->txbd_wrptr | rx_val)) {
  1014. mwifiex_dbg(adapter, ERROR,
  1015. "SEND DATA: failed to write reg->tx_wrptr\n");
  1016. ret = -1;
  1017. goto done_unmap;
  1018. }
  1019. if ((mwifiex_pcie_txbd_not_full(card)) &&
  1020. tx_param->next_pkt_len) {
  1021. /* have more packets and TxBD still can hold more */
  1022. mwifiex_dbg(adapter, DATA,
  1023. "SEND DATA: delay dnld-rdy interrupt.\n");
  1024. adapter->data_sent = false;
  1025. } else {
  1026. /* Send the TX ready interrupt */
  1027. if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
  1028. CPU_INTR_DNLD_RDY)) {
  1029. mwifiex_dbg(adapter, ERROR,
  1030. "SEND DATA: failed to assert dnld-rdy interrupt.\n");
  1031. ret = -1;
  1032. goto done_unmap;
  1033. }
  1034. }
  1035. mwifiex_dbg(adapter, DATA,
  1036. "info: SEND DATA: Updated <Rd: %#x, Wr:\t"
  1037. "%#x> and sent packet to firmware successfully\n",
  1038. card->txbd_rdptr, card->txbd_wrptr);
  1039. } else {
  1040. mwifiex_dbg(adapter, DATA,
  1041. "info: TX Ring full, can't send packets to fw\n");
  1042. adapter->data_sent = true;
  1043. /* Send the TX ready interrupt */
  1044. if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
  1045. CPU_INTR_DNLD_RDY))
  1046. mwifiex_dbg(adapter, ERROR,
  1047. "SEND DATA: failed to assert door-bell intr\n");
  1048. return -EBUSY;
  1049. }
  1050. return -EINPROGRESS;
  1051. done_unmap:
  1052. mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
  1053. card->tx_buf_list[wrindx] = NULL;
  1054. if (reg->pfu_enabled)
  1055. memset(desc2, 0, sizeof(*desc2));
  1056. else
  1057. memset(desc, 0, sizeof(*desc));
  1058. return ret;
  1059. }
  1060. /*
  1061. * This function handles received buffer ring and
  1062. * dispatches packets to upper
  1063. */
  1064. static int mwifiex_pcie_process_recv_data(struct mwifiex_adapter *adapter)
  1065. {
  1066. struct pcie_service_card *card = adapter->card;
  1067. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  1068. u32 wrptr, rd_index, tx_val;
  1069. dma_addr_t buf_pa;
  1070. int ret = 0;
  1071. struct sk_buff *skb_tmp = NULL;
  1072. struct mwifiex_pcie_buf_desc *desc;
  1073. struct mwifiex_pfu_buf_desc *desc2;
  1074. if (!mwifiex_pcie_ok_to_access_hw(adapter))
  1075. mwifiex_pm_wakeup_card(adapter);
  1076. /* Read the RX ring Write pointer set by firmware */
  1077. if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
  1078. mwifiex_dbg(adapter, ERROR,
  1079. "RECV DATA: failed to read reg->rx_wrptr\n");
  1080. ret = -1;
  1081. goto done;
  1082. }
  1083. card->rxbd_wrptr = wrptr;
  1084. while (((wrptr & reg->rx_mask) !=
  1085. (card->rxbd_rdptr & reg->rx_mask)) ||
  1086. ((wrptr & reg->rx_rollover_ind) ==
  1087. (card->rxbd_rdptr & reg->rx_rollover_ind))) {
  1088. struct sk_buff *skb_data;
  1089. u16 rx_len;
  1090. __le16 pkt_len;
  1091. rd_index = card->rxbd_rdptr & reg->rx_mask;
  1092. skb_data = card->rx_buf_list[rd_index];
  1093. /* If skb allocation was failed earlier for Rx packet,
  1094. * rx_buf_list[rd_index] would have been left with a NULL.
  1095. */
  1096. if (!skb_data)
  1097. return -ENOMEM;
  1098. mwifiex_unmap_pci_memory(adapter, skb_data, PCI_DMA_FROMDEVICE);
  1099. card->rx_buf_list[rd_index] = NULL;
  1100. /* Get data length from interface header -
  1101. * first 2 bytes for len, next 2 bytes is for type
  1102. */
  1103. pkt_len = *((__le16 *)skb_data->data);
  1104. rx_len = le16_to_cpu(pkt_len);
  1105. if (WARN_ON(rx_len <= INTF_HEADER_LEN ||
  1106. rx_len > MWIFIEX_RX_DATA_BUF_SIZE)) {
  1107. mwifiex_dbg(adapter, ERROR,
  1108. "Invalid RX len %d, Rd=%#x, Wr=%#x\n",
  1109. rx_len, card->rxbd_rdptr, wrptr);
  1110. dev_kfree_skb_any(skb_data);
  1111. } else {
  1112. skb_put(skb_data, rx_len);
  1113. mwifiex_dbg(adapter, DATA,
  1114. "info: RECV DATA: Rd=%#x, Wr=%#x, Len=%d\n",
  1115. card->rxbd_rdptr, wrptr, rx_len);
  1116. skb_pull(skb_data, INTF_HEADER_LEN);
  1117. if (adapter->rx_work_enabled) {
  1118. skb_queue_tail(&adapter->rx_data_q, skb_data);
  1119. adapter->data_received = true;
  1120. atomic_inc(&adapter->rx_pending);
  1121. } else {
  1122. mwifiex_handle_rx_packet(adapter, skb_data);
  1123. }
  1124. }
  1125. skb_tmp = mwifiex_alloc_dma_align_buf(MWIFIEX_RX_DATA_BUF_SIZE,
  1126. GFP_KERNEL | GFP_DMA);
  1127. if (!skb_tmp) {
  1128. mwifiex_dbg(adapter, ERROR,
  1129. "Unable to allocate skb.\n");
  1130. return -ENOMEM;
  1131. }
  1132. if (mwifiex_map_pci_memory(adapter, skb_tmp,
  1133. MWIFIEX_RX_DATA_BUF_SIZE,
  1134. PCI_DMA_FROMDEVICE))
  1135. return -1;
  1136. buf_pa = MWIFIEX_SKB_DMA_ADDR(skb_tmp);
  1137. mwifiex_dbg(adapter, INFO,
  1138. "RECV DATA: Attach new sk_buff %p at rxbd_rdidx=%d\n",
  1139. skb_tmp, rd_index);
  1140. card->rx_buf_list[rd_index] = skb_tmp;
  1141. if (reg->pfu_enabled) {
  1142. desc2 = card->rxbd_ring[rd_index];
  1143. desc2->paddr = buf_pa;
  1144. desc2->len = skb_tmp->len;
  1145. desc2->frag_len = skb_tmp->len;
  1146. desc2->offset = 0;
  1147. desc2->flags = reg->ring_flag_sop | reg->ring_flag_eop;
  1148. } else {
  1149. desc = card->rxbd_ring[rd_index];
  1150. desc->paddr = buf_pa;
  1151. desc->len = skb_tmp->len;
  1152. desc->flags = 0;
  1153. }
  1154. if ((++card->rxbd_rdptr & reg->rx_mask) ==
  1155. MWIFIEX_MAX_TXRX_BD) {
  1156. card->rxbd_rdptr = ((card->rxbd_rdptr &
  1157. reg->rx_rollover_ind) ^
  1158. reg->rx_rollover_ind);
  1159. }
  1160. mwifiex_dbg(adapter, DATA,
  1161. "info: RECV DATA: <Rd: %#x, Wr: %#x>\n",
  1162. card->rxbd_rdptr, wrptr);
  1163. tx_val = card->txbd_wrptr & reg->tx_wrap_mask;
  1164. /* Write the RX ring read pointer in to reg->rx_rdptr */
  1165. if (mwifiex_write_reg(adapter, reg->rx_rdptr,
  1166. card->rxbd_rdptr | tx_val)) {
  1167. mwifiex_dbg(adapter, DATA,
  1168. "RECV DATA: failed to write reg->rx_rdptr\n");
  1169. ret = -1;
  1170. goto done;
  1171. }
  1172. /* Read the RX ring Write pointer set by firmware */
  1173. if (mwifiex_read_reg(adapter, reg->rx_wrptr, &wrptr)) {
  1174. mwifiex_dbg(adapter, ERROR,
  1175. "RECV DATA: failed to read reg->rx_wrptr\n");
  1176. ret = -1;
  1177. goto done;
  1178. }
  1179. mwifiex_dbg(adapter, DATA,
  1180. "info: RECV DATA: Rcvd packet from fw successfully\n");
  1181. card->rxbd_wrptr = wrptr;
  1182. }
  1183. done:
  1184. return ret;
  1185. }
  1186. /*
  1187. * This function downloads the boot command to device
  1188. */
  1189. static int
  1190. mwifiex_pcie_send_boot_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
  1191. {
  1192. dma_addr_t buf_pa;
  1193. struct pcie_service_card *card = adapter->card;
  1194. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  1195. if (!(skb->data && skb->len)) {
  1196. mwifiex_dbg(adapter, ERROR,
  1197. "Invalid parameter in %s <%p. len %d>\n",
  1198. __func__, skb->data, skb->len);
  1199. return -1;
  1200. }
  1201. if (mwifiex_map_pci_memory(adapter, skb, skb->len , PCI_DMA_TODEVICE))
  1202. return -1;
  1203. buf_pa = MWIFIEX_SKB_DMA_ADDR(skb);
  1204. /* Write the lower 32bits of the physical address to low command
  1205. * address scratch register
  1206. */
  1207. if (mwifiex_write_reg(adapter, reg->cmd_addr_lo, (u32)buf_pa)) {
  1208. mwifiex_dbg(adapter, ERROR,
  1209. "%s: failed to write download command to boot code.\n",
  1210. __func__);
  1211. mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
  1212. return -1;
  1213. }
  1214. /* Write the upper 32bits of the physical address to high command
  1215. * address scratch register
  1216. */
  1217. if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
  1218. (u32)((u64)buf_pa >> 32))) {
  1219. mwifiex_dbg(adapter, ERROR,
  1220. "%s: failed to write download command to boot code.\n",
  1221. __func__);
  1222. mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
  1223. return -1;
  1224. }
  1225. /* Write the command length to cmd_size scratch register */
  1226. if (mwifiex_write_reg(adapter, reg->cmd_size, skb->len)) {
  1227. mwifiex_dbg(adapter, ERROR,
  1228. "%s: failed to write command len to cmd_size scratch reg\n",
  1229. __func__);
  1230. mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
  1231. return -1;
  1232. }
  1233. /* Ring the door bell */
  1234. if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
  1235. CPU_INTR_DOOR_BELL)) {
  1236. mwifiex_dbg(adapter, ERROR,
  1237. "%s: failed to assert door-bell intr\n", __func__);
  1238. mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
  1239. return -1;
  1240. }
  1241. return 0;
  1242. }
  1243. /* This function init rx port in firmware which in turn enables to receive data
  1244. * from device before transmitting any packet.
  1245. */
  1246. static int mwifiex_pcie_init_fw_port(struct mwifiex_adapter *adapter)
  1247. {
  1248. struct pcie_service_card *card = adapter->card;
  1249. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  1250. int tx_wrap = card->txbd_wrptr & reg->tx_wrap_mask;
  1251. /* Write the RX ring read pointer in to reg->rx_rdptr */
  1252. if (mwifiex_write_reg(adapter, reg->rx_rdptr, card->rxbd_rdptr |
  1253. tx_wrap)) {
  1254. mwifiex_dbg(adapter, ERROR,
  1255. "RECV DATA: failed to write reg->rx_rdptr\n");
  1256. return -1;
  1257. }
  1258. return 0;
  1259. }
  1260. /* This function downloads commands to the device
  1261. */
  1262. static int
  1263. mwifiex_pcie_send_cmd(struct mwifiex_adapter *adapter, struct sk_buff *skb)
  1264. {
  1265. struct pcie_service_card *card = adapter->card;
  1266. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  1267. int ret = 0;
  1268. dma_addr_t cmd_buf_pa, cmdrsp_buf_pa;
  1269. u8 *payload = (u8 *)skb->data;
  1270. if (!(skb->data && skb->len)) {
  1271. mwifiex_dbg(adapter, ERROR,
  1272. "Invalid parameter in %s <%p, %#x>\n",
  1273. __func__, skb->data, skb->len);
  1274. return -1;
  1275. }
  1276. /* Make sure a command response buffer is available */
  1277. if (!card->cmdrsp_buf) {
  1278. mwifiex_dbg(adapter, ERROR,
  1279. "No response buffer available, send command failed\n");
  1280. return -EBUSY;
  1281. }
  1282. if (!mwifiex_pcie_ok_to_access_hw(adapter))
  1283. mwifiex_pm_wakeup_card(adapter);
  1284. adapter->cmd_sent = true;
  1285. *(__le16 *)&payload[0] = cpu_to_le16((u16)skb->len);
  1286. *(__le16 *)&payload[2] = cpu_to_le16(MWIFIEX_TYPE_CMD);
  1287. if (mwifiex_map_pci_memory(adapter, skb, skb->len, PCI_DMA_TODEVICE))
  1288. return -1;
  1289. card->cmd_buf = skb;
  1290. /* To send a command, the driver will:
  1291. 1. Write the 64bit physical address of the data buffer to
  1292. cmd response address low + cmd response address high
  1293. 2. Ring the door bell (i.e. set the door bell interrupt)
  1294. In response to door bell interrupt, the firmware will perform
  1295. the DMA of the command packet (first header to obtain the total
  1296. length and then rest of the command).
  1297. */
  1298. if (card->cmdrsp_buf) {
  1299. cmdrsp_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmdrsp_buf);
  1300. /* Write the lower 32bits of the cmdrsp buffer physical
  1301. address */
  1302. if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo,
  1303. (u32)cmdrsp_buf_pa)) {
  1304. mwifiex_dbg(adapter, ERROR,
  1305. "Failed to write download cmd to boot code.\n");
  1306. ret = -1;
  1307. goto done;
  1308. }
  1309. /* Write the upper 32bits of the cmdrsp buffer physical
  1310. address */
  1311. if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi,
  1312. (u32)((u64)cmdrsp_buf_pa >> 32))) {
  1313. mwifiex_dbg(adapter, ERROR,
  1314. "Failed to write download cmd to boot code.\n");
  1315. ret = -1;
  1316. goto done;
  1317. }
  1318. }
  1319. cmd_buf_pa = MWIFIEX_SKB_DMA_ADDR(card->cmd_buf);
  1320. /* Write the lower 32bits of the physical address to reg->cmd_addr_lo */
  1321. if (mwifiex_write_reg(adapter, reg->cmd_addr_lo,
  1322. (u32)cmd_buf_pa)) {
  1323. mwifiex_dbg(adapter, ERROR,
  1324. "Failed to write download cmd to boot code.\n");
  1325. ret = -1;
  1326. goto done;
  1327. }
  1328. /* Write the upper 32bits of the physical address to reg->cmd_addr_hi */
  1329. if (mwifiex_write_reg(adapter, reg->cmd_addr_hi,
  1330. (u32)((u64)cmd_buf_pa >> 32))) {
  1331. mwifiex_dbg(adapter, ERROR,
  1332. "Failed to write download cmd to boot code.\n");
  1333. ret = -1;
  1334. goto done;
  1335. }
  1336. /* Write the command length to reg->cmd_size */
  1337. if (mwifiex_write_reg(adapter, reg->cmd_size,
  1338. card->cmd_buf->len)) {
  1339. mwifiex_dbg(adapter, ERROR,
  1340. "Failed to write cmd len to reg->cmd_size\n");
  1341. ret = -1;
  1342. goto done;
  1343. }
  1344. /* Ring the door bell */
  1345. if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
  1346. CPU_INTR_DOOR_BELL)) {
  1347. mwifiex_dbg(adapter, ERROR,
  1348. "Failed to assert door-bell intr\n");
  1349. ret = -1;
  1350. goto done;
  1351. }
  1352. done:
  1353. if (ret)
  1354. adapter->cmd_sent = false;
  1355. return 0;
  1356. }
  1357. /*
  1358. * This function handles command complete interrupt
  1359. */
  1360. static int mwifiex_pcie_process_cmd_complete(struct mwifiex_adapter *adapter)
  1361. {
  1362. struct pcie_service_card *card = adapter->card;
  1363. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  1364. struct sk_buff *skb = card->cmdrsp_buf;
  1365. int count = 0;
  1366. u16 rx_len;
  1367. __le16 pkt_len;
  1368. mwifiex_dbg(adapter, CMD,
  1369. "info: Rx CMD Response\n");
  1370. mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_FROMDEVICE);
  1371. /* Unmap the command as a response has been received. */
  1372. if (card->cmd_buf) {
  1373. mwifiex_unmap_pci_memory(adapter, card->cmd_buf,
  1374. PCI_DMA_TODEVICE);
  1375. card->cmd_buf = NULL;
  1376. }
  1377. pkt_len = *((__le16 *)skb->data);
  1378. rx_len = le16_to_cpu(pkt_len);
  1379. skb_trim(skb, rx_len);
  1380. skb_pull(skb, INTF_HEADER_LEN);
  1381. if (!adapter->curr_cmd) {
  1382. if (adapter->ps_state == PS_STATE_SLEEP_CFM) {
  1383. mwifiex_process_sleep_confirm_resp(adapter, skb->data,
  1384. skb->len);
  1385. mwifiex_pcie_enable_host_int(adapter);
  1386. if (mwifiex_write_reg(adapter,
  1387. PCIE_CPU_INT_EVENT,
  1388. CPU_INTR_SLEEP_CFM_DONE)) {
  1389. mwifiex_dbg(adapter, ERROR,
  1390. "Write register failed\n");
  1391. return -1;
  1392. }
  1393. mwifiex_delay_for_sleep_cookie(adapter,
  1394. MWIFIEX_MAX_DELAY_COUNT);
  1395. while (reg->sleep_cookie && (count++ < 10) &&
  1396. mwifiex_pcie_ok_to_access_hw(adapter))
  1397. usleep_range(50, 60);
  1398. } else {
  1399. mwifiex_dbg(adapter, ERROR,
  1400. "There is no command but got cmdrsp\n");
  1401. }
  1402. memcpy(adapter->upld_buf, skb->data,
  1403. min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER, skb->len));
  1404. skb_push(skb, INTF_HEADER_LEN);
  1405. if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
  1406. PCI_DMA_FROMDEVICE))
  1407. return -1;
  1408. } else if (mwifiex_pcie_ok_to_access_hw(adapter)) {
  1409. adapter->curr_cmd->resp_skb = skb;
  1410. adapter->cmd_resp_received = true;
  1411. /* Take the pointer and set it to CMD node and will
  1412. return in the response complete callback */
  1413. card->cmdrsp_buf = NULL;
  1414. /* Clear the cmd-rsp buffer address in scratch registers. This
  1415. will prevent firmware from writing to the same response
  1416. buffer again. */
  1417. if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_lo, 0)) {
  1418. mwifiex_dbg(adapter, ERROR,
  1419. "cmd_done: failed to clear cmd_rsp_addr_lo\n");
  1420. return -1;
  1421. }
  1422. /* Write the upper 32bits of the cmdrsp buffer physical
  1423. address */
  1424. if (mwifiex_write_reg(adapter, reg->cmdrsp_addr_hi, 0)) {
  1425. mwifiex_dbg(adapter, ERROR,
  1426. "cmd_done: failed to clear cmd_rsp_addr_hi\n");
  1427. return -1;
  1428. }
  1429. }
  1430. return 0;
  1431. }
  1432. /*
  1433. * Command Response processing complete handler
  1434. */
  1435. static int mwifiex_pcie_cmdrsp_complete(struct mwifiex_adapter *adapter,
  1436. struct sk_buff *skb)
  1437. {
  1438. struct pcie_service_card *card = adapter->card;
  1439. if (skb) {
  1440. card->cmdrsp_buf = skb;
  1441. skb_push(card->cmdrsp_buf, INTF_HEADER_LEN);
  1442. if (mwifiex_map_pci_memory(adapter, skb, MWIFIEX_UPLD_SIZE,
  1443. PCI_DMA_FROMDEVICE))
  1444. return -1;
  1445. }
  1446. return 0;
  1447. }
  1448. /*
  1449. * This function handles firmware event ready interrupt
  1450. */
  1451. static int mwifiex_pcie_process_event_ready(struct mwifiex_adapter *adapter)
  1452. {
  1453. struct pcie_service_card *card = adapter->card;
  1454. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  1455. u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
  1456. u32 wrptr, event;
  1457. struct mwifiex_evt_buf_desc *desc;
  1458. if (!mwifiex_pcie_ok_to_access_hw(adapter))
  1459. mwifiex_pm_wakeup_card(adapter);
  1460. if (adapter->event_received) {
  1461. mwifiex_dbg(adapter, EVENT,
  1462. "info: Event being processed,\t"
  1463. "do not process this interrupt just yet\n");
  1464. return 0;
  1465. }
  1466. if (rdptr >= MWIFIEX_MAX_EVT_BD) {
  1467. mwifiex_dbg(adapter, ERROR,
  1468. "info: Invalid read pointer...\n");
  1469. return -1;
  1470. }
  1471. /* Read the event ring write pointer set by firmware */
  1472. if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
  1473. mwifiex_dbg(adapter, ERROR,
  1474. "EventReady: failed to read reg->evt_wrptr\n");
  1475. return -1;
  1476. }
  1477. mwifiex_dbg(adapter, EVENT,
  1478. "info: EventReady: Initial <Rd: 0x%x, Wr: 0x%x>",
  1479. card->evtbd_rdptr, wrptr);
  1480. if (((wrptr & MWIFIEX_EVTBD_MASK) != (card->evtbd_rdptr
  1481. & MWIFIEX_EVTBD_MASK)) ||
  1482. ((wrptr & reg->evt_rollover_ind) ==
  1483. (card->evtbd_rdptr & reg->evt_rollover_ind))) {
  1484. struct sk_buff *skb_cmd;
  1485. __le16 data_len = 0;
  1486. u16 evt_len;
  1487. mwifiex_dbg(adapter, INFO,
  1488. "info: Read Index: %d\n", rdptr);
  1489. skb_cmd = card->evt_buf_list[rdptr];
  1490. mwifiex_unmap_pci_memory(adapter, skb_cmd, PCI_DMA_FROMDEVICE);
  1491. /* Take the pointer and set it to event pointer in adapter
  1492. and will return back after event handling callback */
  1493. card->evt_buf_list[rdptr] = NULL;
  1494. desc = card->evtbd_ring[rdptr];
  1495. memset(desc, 0, sizeof(*desc));
  1496. event = *(u32 *) &skb_cmd->data[INTF_HEADER_LEN];
  1497. adapter->event_cause = event;
  1498. /* The first 4bytes will be the event transfer header
  1499. len is 2 bytes followed by type which is 2 bytes */
  1500. memcpy(&data_len, skb_cmd->data, sizeof(__le16));
  1501. evt_len = le16_to_cpu(data_len);
  1502. skb_trim(skb_cmd, evt_len);
  1503. skb_pull(skb_cmd, INTF_HEADER_LEN);
  1504. mwifiex_dbg(adapter, EVENT,
  1505. "info: Event length: %d\n", evt_len);
  1506. if ((evt_len > 0) && (evt_len < MAX_EVENT_SIZE))
  1507. memcpy(adapter->event_body, skb_cmd->data +
  1508. MWIFIEX_EVENT_HEADER_LEN, evt_len -
  1509. MWIFIEX_EVENT_HEADER_LEN);
  1510. adapter->event_received = true;
  1511. adapter->event_skb = skb_cmd;
  1512. /* Do not update the event read pointer here, wait till the
  1513. buffer is released. This is just to make things simpler,
  1514. we need to find a better method of managing these buffers.
  1515. */
  1516. } else {
  1517. if (mwifiex_write_reg(adapter, PCIE_CPU_INT_EVENT,
  1518. CPU_INTR_EVENT_DONE)) {
  1519. mwifiex_dbg(adapter, ERROR,
  1520. "Write register failed\n");
  1521. return -1;
  1522. }
  1523. }
  1524. return 0;
  1525. }
  1526. /*
  1527. * Event processing complete handler
  1528. */
  1529. static int mwifiex_pcie_event_complete(struct mwifiex_adapter *adapter,
  1530. struct sk_buff *skb)
  1531. {
  1532. struct pcie_service_card *card = adapter->card;
  1533. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  1534. int ret = 0;
  1535. u32 rdptr = card->evtbd_rdptr & MWIFIEX_EVTBD_MASK;
  1536. u32 wrptr;
  1537. struct mwifiex_evt_buf_desc *desc;
  1538. if (!skb)
  1539. return 0;
  1540. if (rdptr >= MWIFIEX_MAX_EVT_BD) {
  1541. mwifiex_dbg(adapter, ERROR,
  1542. "event_complete: Invalid rdptr 0x%x\n",
  1543. rdptr);
  1544. return -EINVAL;
  1545. }
  1546. /* Read the event ring write pointer set by firmware */
  1547. if (mwifiex_read_reg(adapter, reg->evt_wrptr, &wrptr)) {
  1548. mwifiex_dbg(adapter, ERROR,
  1549. "event_complete: failed to read reg->evt_wrptr\n");
  1550. return -1;
  1551. }
  1552. if (!card->evt_buf_list[rdptr]) {
  1553. skb_push(skb, INTF_HEADER_LEN);
  1554. if (mwifiex_map_pci_memory(adapter, skb,
  1555. MAX_EVENT_SIZE,
  1556. PCI_DMA_FROMDEVICE))
  1557. return -1;
  1558. card->evt_buf_list[rdptr] = skb;
  1559. desc = card->evtbd_ring[rdptr];
  1560. desc->paddr = MWIFIEX_SKB_DMA_ADDR(skb);
  1561. desc->len = (u16)skb->len;
  1562. desc->flags = 0;
  1563. skb = NULL;
  1564. } else {
  1565. mwifiex_dbg(adapter, ERROR,
  1566. "info: ERROR: buf still valid at index %d, <%p, %p>\n",
  1567. rdptr, card->evt_buf_list[rdptr], skb);
  1568. }
  1569. if ((++card->evtbd_rdptr & MWIFIEX_EVTBD_MASK) == MWIFIEX_MAX_EVT_BD) {
  1570. card->evtbd_rdptr = ((card->evtbd_rdptr &
  1571. reg->evt_rollover_ind) ^
  1572. reg->evt_rollover_ind);
  1573. }
  1574. mwifiex_dbg(adapter, EVENT,
  1575. "info: Updated <Rd: 0x%x, Wr: 0x%x>",
  1576. card->evtbd_rdptr, wrptr);
  1577. /* Write the event ring read pointer in to reg->evt_rdptr */
  1578. if (mwifiex_write_reg(adapter, reg->evt_rdptr,
  1579. card->evtbd_rdptr)) {
  1580. mwifiex_dbg(adapter, ERROR,
  1581. "event_complete: failed to read reg->evt_rdptr\n");
  1582. return -1;
  1583. }
  1584. mwifiex_dbg(adapter, EVENT,
  1585. "info: Check Events Again\n");
  1586. ret = mwifiex_pcie_process_event_ready(adapter);
  1587. return ret;
  1588. }
  1589. /*
  1590. * This function downloads the firmware to the card.
  1591. *
  1592. * Firmware is downloaded to the card in blocks. Every block download
  1593. * is tested for CRC errors, and retried a number of times before
  1594. * returning failure.
  1595. */
  1596. static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
  1597. struct mwifiex_fw_image *fw)
  1598. {
  1599. int ret;
  1600. u8 *firmware = fw->fw_buf;
  1601. u32 firmware_len = fw->fw_len;
  1602. u32 offset = 0;
  1603. struct sk_buff *skb;
  1604. u32 txlen, tx_blocks = 0, tries, len;
  1605. u32 block_retry_cnt = 0;
  1606. struct pcie_service_card *card = adapter->card;
  1607. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  1608. if (!firmware || !firmware_len) {
  1609. mwifiex_dbg(adapter, ERROR,
  1610. "No firmware image found! Terminating download\n");
  1611. return -1;
  1612. }
  1613. mwifiex_dbg(adapter, INFO,
  1614. "info: Downloading FW image (%d bytes)\n",
  1615. firmware_len);
  1616. if (mwifiex_pcie_disable_host_int(adapter)) {
  1617. mwifiex_dbg(adapter, ERROR,
  1618. "%s: Disabling interrupts failed.\n", __func__);
  1619. return -1;
  1620. }
  1621. skb = dev_alloc_skb(MWIFIEX_UPLD_SIZE);
  1622. if (!skb) {
  1623. ret = -ENOMEM;
  1624. goto done;
  1625. }
  1626. /* Perform firmware data transfer */
  1627. do {
  1628. u32 ireg_intr = 0;
  1629. /* More data? */
  1630. if (offset >= firmware_len)
  1631. break;
  1632. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  1633. ret = mwifiex_read_reg(adapter, reg->cmd_size,
  1634. &len);
  1635. if (ret) {
  1636. mwifiex_dbg(adapter, FATAL,
  1637. "Failed reading len from boot code\n");
  1638. goto done;
  1639. }
  1640. if (len)
  1641. break;
  1642. usleep_range(10, 20);
  1643. }
  1644. if (!len) {
  1645. break;
  1646. } else if (len > MWIFIEX_UPLD_SIZE) {
  1647. mwifiex_dbg(adapter, ERROR,
  1648. "FW download failure @ %d, invalid length %d\n",
  1649. offset, len);
  1650. ret = -1;
  1651. goto done;
  1652. }
  1653. txlen = len;
  1654. if (len & BIT(0)) {
  1655. block_retry_cnt++;
  1656. if (block_retry_cnt > MAX_WRITE_IOMEM_RETRY) {
  1657. mwifiex_dbg(adapter, ERROR,
  1658. "FW download failure @ %d, over max\t"
  1659. "retry count\n", offset);
  1660. ret = -1;
  1661. goto done;
  1662. }
  1663. mwifiex_dbg(adapter, ERROR,
  1664. "FW CRC error indicated by the\t"
  1665. "helper: len = 0x%04X, txlen = %d\n",
  1666. len, txlen);
  1667. len &= ~BIT(0);
  1668. /* Setting this to 0 to resend from same offset */
  1669. txlen = 0;
  1670. } else {
  1671. block_retry_cnt = 0;
  1672. /* Set blocksize to transfer - checking for
  1673. last block */
  1674. if (firmware_len - offset < txlen)
  1675. txlen = firmware_len - offset;
  1676. mwifiex_dbg(adapter, INFO, ".");
  1677. tx_blocks = (txlen + card->pcie.blksz_fw_dl - 1) /
  1678. card->pcie.blksz_fw_dl;
  1679. /* Copy payload to buffer */
  1680. memmove(skb->data, &firmware[offset], txlen);
  1681. }
  1682. skb_put(skb, MWIFIEX_UPLD_SIZE - skb->len);
  1683. skb_trim(skb, tx_blocks * card->pcie.blksz_fw_dl);
  1684. /* Send the boot command to device */
  1685. if (mwifiex_pcie_send_boot_cmd(adapter, skb)) {
  1686. mwifiex_dbg(adapter, ERROR,
  1687. "Failed to send firmware download command\n");
  1688. ret = -1;
  1689. goto done;
  1690. }
  1691. /* Wait for the command done interrupt */
  1692. do {
  1693. if (mwifiex_read_reg(adapter, PCIE_CPU_INT_STATUS,
  1694. &ireg_intr)) {
  1695. mwifiex_dbg(adapter, ERROR,
  1696. "%s: Failed to read\t"
  1697. "interrupt status during fw dnld.\n",
  1698. __func__);
  1699. mwifiex_unmap_pci_memory(adapter, skb,
  1700. PCI_DMA_TODEVICE);
  1701. ret = -1;
  1702. goto done;
  1703. }
  1704. } while ((ireg_intr & CPU_INTR_DOOR_BELL) ==
  1705. CPU_INTR_DOOR_BELL);
  1706. mwifiex_unmap_pci_memory(adapter, skb, PCI_DMA_TODEVICE);
  1707. offset += txlen;
  1708. } while (true);
  1709. mwifiex_dbg(adapter, MSG,
  1710. "info: FW download over, size %d bytes\n", offset);
  1711. ret = 0;
  1712. done:
  1713. dev_kfree_skb_any(skb);
  1714. return ret;
  1715. }
  1716. /*
  1717. * This function checks the firmware status in card.
  1718. *
  1719. * The winner interface is also determined by this function.
  1720. */
  1721. static int
  1722. mwifiex_check_fw_status(struct mwifiex_adapter *adapter, u32 poll_num)
  1723. {
  1724. int ret = 0;
  1725. u32 firmware_stat, winner_status;
  1726. struct pcie_service_card *card = adapter->card;
  1727. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  1728. u32 tries;
  1729. /* Mask spurios interrupts */
  1730. if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS_MASK,
  1731. HOST_INTR_MASK)) {
  1732. mwifiex_dbg(adapter, ERROR,
  1733. "Write register failed\n");
  1734. return -1;
  1735. }
  1736. mwifiex_dbg(adapter, INFO,
  1737. "Setting driver ready signature\n");
  1738. if (mwifiex_write_reg(adapter, reg->drv_rdy,
  1739. FIRMWARE_READY_PCIE)) {
  1740. mwifiex_dbg(adapter, ERROR,
  1741. "Failed to write driver ready signature\n");
  1742. return -1;
  1743. }
  1744. /* Wait for firmware initialization event */
  1745. for (tries = 0; tries < poll_num; tries++) {
  1746. if (mwifiex_read_reg(adapter, reg->fw_status,
  1747. &firmware_stat))
  1748. ret = -1;
  1749. else
  1750. ret = 0;
  1751. if (ret)
  1752. continue;
  1753. if (firmware_stat == FIRMWARE_READY_PCIE) {
  1754. ret = 0;
  1755. break;
  1756. } else {
  1757. msleep(100);
  1758. ret = -1;
  1759. }
  1760. }
  1761. if (ret) {
  1762. if (mwifiex_read_reg(adapter, reg->fw_status,
  1763. &winner_status))
  1764. ret = -1;
  1765. else if (!winner_status) {
  1766. mwifiex_dbg(adapter, INFO,
  1767. "PCI-E is the winner\n");
  1768. adapter->winner = 1;
  1769. } else {
  1770. mwifiex_dbg(adapter, ERROR,
  1771. "PCI-E is not the winner <%#x,%d>, exit dnld\n",
  1772. ret, adapter->winner);
  1773. }
  1774. }
  1775. return ret;
  1776. }
  1777. /*
  1778. * This function reads the interrupt status from card.
  1779. */
  1780. static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
  1781. {
  1782. u32 pcie_ireg;
  1783. unsigned long flags;
  1784. if (!mwifiex_pcie_ok_to_access_hw(adapter))
  1785. return;
  1786. if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS, &pcie_ireg)) {
  1787. mwifiex_dbg(adapter, ERROR, "Read register failed\n");
  1788. return;
  1789. }
  1790. if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
  1791. mwifiex_pcie_disable_host_int(adapter);
  1792. /* Clear the pending interrupts */
  1793. if (mwifiex_write_reg(adapter, PCIE_HOST_INT_STATUS,
  1794. ~pcie_ireg)) {
  1795. mwifiex_dbg(adapter, ERROR,
  1796. "Write register failed\n");
  1797. return;
  1798. }
  1799. spin_lock_irqsave(&adapter->int_lock, flags);
  1800. adapter->int_status |= pcie_ireg;
  1801. spin_unlock_irqrestore(&adapter->int_lock, flags);
  1802. if (!adapter->pps_uapsd_mode &&
  1803. adapter->ps_state == PS_STATE_SLEEP &&
  1804. mwifiex_pcie_ok_to_access_hw(adapter)) {
  1805. /* Potentially for PCIe we could get other
  1806. * interrupts like shared. Don't change power
  1807. * state until cookie is set */
  1808. adapter->ps_state = PS_STATE_AWAKE;
  1809. adapter->pm_wakeup_fw_try = false;
  1810. del_timer(&adapter->wakeup_timer);
  1811. }
  1812. }
  1813. }
  1814. /*
  1815. * Interrupt handler for PCIe root port
  1816. *
  1817. * This function reads the interrupt status from firmware and assigns
  1818. * the main process in workqueue which will handle the interrupt.
  1819. */
  1820. static irqreturn_t mwifiex_pcie_interrupt(int irq, void *context)
  1821. {
  1822. struct pci_dev *pdev = (struct pci_dev *)context;
  1823. struct pcie_service_card *card;
  1824. struct mwifiex_adapter *adapter;
  1825. if (!pdev) {
  1826. pr_debug("info: %s: pdev is NULL\n", (u8 *)pdev);
  1827. goto exit;
  1828. }
  1829. card = pci_get_drvdata(pdev);
  1830. if (!card || !card->adapter) {
  1831. pr_debug("info: %s: card=%p adapter=%p\n", __func__, card,
  1832. card ? card->adapter : NULL);
  1833. goto exit;
  1834. }
  1835. adapter = card->adapter;
  1836. if (adapter->surprise_removed)
  1837. goto exit;
  1838. mwifiex_interrupt_status(adapter);
  1839. mwifiex_queue_main_work(adapter);
  1840. exit:
  1841. return IRQ_HANDLED;
  1842. }
  1843. /*
  1844. * This function checks the current interrupt status.
  1845. *
  1846. * The following interrupts are checked and handled by this function -
  1847. * - Data sent
  1848. * - Command sent
  1849. * - Command received
  1850. * - Packets received
  1851. * - Events received
  1852. *
  1853. * In case of Rx packets received, the packets are uploaded from card to
  1854. * host and processed accordingly.
  1855. */
  1856. static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
  1857. {
  1858. int ret;
  1859. u32 pcie_ireg;
  1860. unsigned long flags;
  1861. spin_lock_irqsave(&adapter->int_lock, flags);
  1862. /* Clear out unused interrupts */
  1863. pcie_ireg = adapter->int_status;
  1864. adapter->int_status = 0;
  1865. spin_unlock_irqrestore(&adapter->int_lock, flags);
  1866. while (pcie_ireg & HOST_INTR_MASK) {
  1867. if (pcie_ireg & HOST_INTR_DNLD_DONE) {
  1868. pcie_ireg &= ~HOST_INTR_DNLD_DONE;
  1869. mwifiex_dbg(adapter, INTR,
  1870. "info: TX DNLD Done\n");
  1871. ret = mwifiex_pcie_send_data_complete(adapter);
  1872. if (ret)
  1873. return ret;
  1874. }
  1875. if (pcie_ireg & HOST_INTR_UPLD_RDY) {
  1876. pcie_ireg &= ~HOST_INTR_UPLD_RDY;
  1877. mwifiex_dbg(adapter, INTR,
  1878. "info: Rx DATA\n");
  1879. ret = mwifiex_pcie_process_recv_data(adapter);
  1880. if (ret)
  1881. return ret;
  1882. }
  1883. if (pcie_ireg & HOST_INTR_EVENT_RDY) {
  1884. pcie_ireg &= ~HOST_INTR_EVENT_RDY;
  1885. mwifiex_dbg(adapter, INTR,
  1886. "info: Rx EVENT\n");
  1887. ret = mwifiex_pcie_process_event_ready(adapter);
  1888. if (ret)
  1889. return ret;
  1890. }
  1891. if (pcie_ireg & HOST_INTR_CMD_DONE) {
  1892. pcie_ireg &= ~HOST_INTR_CMD_DONE;
  1893. if (adapter->cmd_sent) {
  1894. mwifiex_dbg(adapter, INTR,
  1895. "info: CMD sent Interrupt\n");
  1896. adapter->cmd_sent = false;
  1897. }
  1898. /* Handle command response */
  1899. ret = mwifiex_pcie_process_cmd_complete(adapter);
  1900. if (ret)
  1901. return ret;
  1902. }
  1903. if (mwifiex_pcie_ok_to_access_hw(adapter)) {
  1904. if (mwifiex_read_reg(adapter, PCIE_HOST_INT_STATUS,
  1905. &pcie_ireg)) {
  1906. mwifiex_dbg(adapter, ERROR,
  1907. "Read register failed\n");
  1908. return -1;
  1909. }
  1910. if ((pcie_ireg != 0xFFFFFFFF) && (pcie_ireg)) {
  1911. if (mwifiex_write_reg(adapter,
  1912. PCIE_HOST_INT_STATUS,
  1913. ~pcie_ireg)) {
  1914. mwifiex_dbg(adapter, ERROR,
  1915. "Write register failed\n");
  1916. return -1;
  1917. }
  1918. }
  1919. }
  1920. }
  1921. mwifiex_dbg(adapter, INTR,
  1922. "info: cmd_sent=%d data_sent=%d\n",
  1923. adapter->cmd_sent, adapter->data_sent);
  1924. if (adapter->ps_state != PS_STATE_SLEEP)
  1925. mwifiex_pcie_enable_host_int(adapter);
  1926. return 0;
  1927. }
  1928. /*
  1929. * This function downloads data from driver to card.
  1930. *
  1931. * Both commands and data packets are transferred to the card by this
  1932. * function.
  1933. *
  1934. * This function adds the PCIE specific header to the front of the buffer
  1935. * before transferring. The header contains the length of the packet and
  1936. * the type. The firmware handles the packets based upon this set type.
  1937. */
  1938. static int mwifiex_pcie_host_to_card(struct mwifiex_adapter *adapter, u8 type,
  1939. struct sk_buff *skb,
  1940. struct mwifiex_tx_param *tx_param)
  1941. {
  1942. if (!skb) {
  1943. mwifiex_dbg(adapter, ERROR,
  1944. "Passed NULL skb to %s\n", __func__);
  1945. return -1;
  1946. }
  1947. if (type == MWIFIEX_TYPE_DATA)
  1948. return mwifiex_pcie_send_data(adapter, skb, tx_param);
  1949. else if (type == MWIFIEX_TYPE_CMD)
  1950. return mwifiex_pcie_send_cmd(adapter, skb);
  1951. return 0;
  1952. }
  1953. /* This function read/write firmware */
  1954. static enum rdwr_status
  1955. mwifiex_pcie_rdwr_firmware(struct mwifiex_adapter *adapter, u8 doneflag)
  1956. {
  1957. int ret, tries;
  1958. u8 ctrl_data;
  1959. struct pcie_service_card *card = adapter->card;
  1960. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  1961. ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl, FW_DUMP_HOST_READY);
  1962. if (ret) {
  1963. mwifiex_dbg(adapter, ERROR,
  1964. "PCIE write err\n");
  1965. return RDWR_STATUS_FAILURE;
  1966. }
  1967. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  1968. mwifiex_read_reg_byte(adapter, reg->fw_dump_ctrl, &ctrl_data);
  1969. if (ctrl_data == FW_DUMP_DONE)
  1970. return RDWR_STATUS_SUCCESS;
  1971. if (doneflag && ctrl_data == doneflag)
  1972. return RDWR_STATUS_DONE;
  1973. if (ctrl_data != FW_DUMP_HOST_READY) {
  1974. mwifiex_dbg(adapter, WARN,
  1975. "The ctrl reg was changed, re-try again!\n");
  1976. ret = mwifiex_write_reg(adapter, reg->fw_dump_ctrl,
  1977. FW_DUMP_HOST_READY);
  1978. if (ret) {
  1979. mwifiex_dbg(adapter, ERROR,
  1980. "PCIE write err\n");
  1981. return RDWR_STATUS_FAILURE;
  1982. }
  1983. }
  1984. usleep_range(100, 200);
  1985. }
  1986. mwifiex_dbg(adapter, ERROR, "Fail to pull ctrl_data\n");
  1987. return RDWR_STATUS_FAILURE;
  1988. }
  1989. /* This function dump firmware memory to file */
  1990. static void mwifiex_pcie_fw_dump(struct mwifiex_adapter *adapter)
  1991. {
  1992. struct pcie_service_card *card = adapter->card;
  1993. const struct mwifiex_pcie_card_reg *creg = card->pcie.reg;
  1994. unsigned int reg, reg_start, reg_end;
  1995. u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
  1996. enum rdwr_status stat;
  1997. u32 memory_size;
  1998. int ret;
  1999. if (!card->pcie.can_dump_fw)
  2000. return;
  2001. for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
  2002. struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
  2003. if (entry->mem_ptr) {
  2004. vfree(entry->mem_ptr);
  2005. entry->mem_ptr = NULL;
  2006. }
  2007. entry->mem_size = 0;
  2008. }
  2009. mwifiex_dbg(adapter, DUMP, "== mwifiex firmware dump start ==\n");
  2010. /* Read the number of the memories which will dump */
  2011. stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
  2012. if (stat == RDWR_STATUS_FAILURE)
  2013. return;
  2014. reg = creg->fw_dump_start;
  2015. mwifiex_read_reg_byte(adapter, reg, &dump_num);
  2016. /* Read the length of every memory which will dump */
  2017. for (idx = 0; idx < dump_num; idx++) {
  2018. struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
  2019. stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
  2020. if (stat == RDWR_STATUS_FAILURE)
  2021. return;
  2022. memory_size = 0;
  2023. reg = creg->fw_dump_start;
  2024. for (i = 0; i < 4; i++) {
  2025. mwifiex_read_reg_byte(adapter, reg, &read_reg);
  2026. memory_size |= (read_reg << (i * 8));
  2027. reg++;
  2028. }
  2029. if (memory_size == 0) {
  2030. mwifiex_dbg(adapter, MSG, "Firmware dump Finished!\n");
  2031. ret = mwifiex_write_reg(adapter, creg->fw_dump_ctrl,
  2032. FW_DUMP_READ_DONE);
  2033. if (ret) {
  2034. mwifiex_dbg(adapter, ERROR, "PCIE write err\n");
  2035. return;
  2036. }
  2037. break;
  2038. }
  2039. mwifiex_dbg(adapter, DUMP,
  2040. "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
  2041. entry->mem_ptr = vmalloc(memory_size + 1);
  2042. entry->mem_size = memory_size;
  2043. if (!entry->mem_ptr) {
  2044. mwifiex_dbg(adapter, ERROR,
  2045. "Vmalloc %s failed\n", entry->mem_name);
  2046. return;
  2047. }
  2048. dbg_ptr = entry->mem_ptr;
  2049. end_ptr = dbg_ptr + memory_size;
  2050. doneflag = entry->done_flag;
  2051. mwifiex_dbg(adapter, DUMP, "Start %s output, please wait...\n",
  2052. entry->mem_name);
  2053. do {
  2054. stat = mwifiex_pcie_rdwr_firmware(adapter, doneflag);
  2055. if (RDWR_STATUS_FAILURE == stat)
  2056. return;
  2057. reg_start = creg->fw_dump_start;
  2058. reg_end = creg->fw_dump_end;
  2059. for (reg = reg_start; reg <= reg_end; reg++) {
  2060. mwifiex_read_reg_byte(adapter, reg, dbg_ptr);
  2061. if (dbg_ptr < end_ptr) {
  2062. dbg_ptr++;
  2063. } else {
  2064. mwifiex_dbg(adapter, ERROR,
  2065. "Allocated buf not enough\n");
  2066. return;
  2067. }
  2068. }
  2069. if (stat != RDWR_STATUS_DONE)
  2070. continue;
  2071. mwifiex_dbg(adapter, DUMP,
  2072. "%s done: size=0x%tx\n",
  2073. entry->mem_name, dbg_ptr - entry->mem_ptr);
  2074. break;
  2075. } while (true);
  2076. }
  2077. mwifiex_dbg(adapter, DUMP, "== mwifiex firmware dump end ==\n");
  2078. }
  2079. static void mwifiex_pcie_device_dump_work(struct mwifiex_adapter *adapter)
  2080. {
  2081. mwifiex_drv_info_dump(adapter);
  2082. mwifiex_pcie_fw_dump(adapter);
  2083. mwifiex_upload_device_dump(adapter);
  2084. }
  2085. static unsigned long iface_work_flags;
  2086. static struct mwifiex_adapter *save_adapter;
  2087. static void mwifiex_pcie_work(struct work_struct *work)
  2088. {
  2089. if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
  2090. &iface_work_flags))
  2091. mwifiex_pcie_device_dump_work(save_adapter);
  2092. }
  2093. static DECLARE_WORK(pcie_work, mwifiex_pcie_work);
  2094. /* This function dumps FW information */
  2095. static void mwifiex_pcie_device_dump(struct mwifiex_adapter *adapter)
  2096. {
  2097. save_adapter = adapter;
  2098. if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
  2099. return;
  2100. set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
  2101. schedule_work(&pcie_work);
  2102. }
  2103. /*
  2104. * This function initializes the PCI-E host memory space, WCB rings, etc.
  2105. *
  2106. * The following initializations steps are followed -
  2107. * - Allocate TXBD ring buffers
  2108. * - Allocate RXBD ring buffers
  2109. * - Allocate event BD ring buffers
  2110. * - Allocate command response ring buffer
  2111. * - Allocate sleep cookie buffer
  2112. */
  2113. static int mwifiex_pcie_init(struct mwifiex_adapter *adapter)
  2114. {
  2115. struct pcie_service_card *card = adapter->card;
  2116. int ret;
  2117. struct pci_dev *pdev = card->dev;
  2118. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  2119. pci_set_drvdata(pdev, card);
  2120. ret = pci_enable_device(pdev);
  2121. if (ret)
  2122. goto err_enable_dev;
  2123. pci_set_master(pdev);
  2124. mwifiex_dbg(adapter, INFO,
  2125. "try set_consistent_dma_mask(32)\n");
  2126. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2127. if (ret) {
  2128. mwifiex_dbg(adapter, ERROR,
  2129. "set_dma_mask(32) failed\n");
  2130. goto err_set_dma_mask;
  2131. }
  2132. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2133. if (ret) {
  2134. mwifiex_dbg(adapter, ERROR,
  2135. "set_consistent_dma_mask(64) failed\n");
  2136. goto err_set_dma_mask;
  2137. }
  2138. ret = pci_request_region(pdev, 0, DRV_NAME);
  2139. if (ret) {
  2140. mwifiex_dbg(adapter, ERROR,
  2141. "req_reg(0) error\n");
  2142. goto err_req_region0;
  2143. }
  2144. card->pci_mmap = pci_iomap(pdev, 0, 0);
  2145. if (!card->pci_mmap) {
  2146. mwifiex_dbg(adapter, ERROR, "iomap(0) error\n");
  2147. ret = -EIO;
  2148. goto err_iomap0;
  2149. }
  2150. ret = pci_request_region(pdev, 2, DRV_NAME);
  2151. if (ret) {
  2152. mwifiex_dbg(adapter, ERROR, "req_reg(2) error\n");
  2153. goto err_req_region2;
  2154. }
  2155. card->pci_mmap1 = pci_iomap(pdev, 2, 0);
  2156. if (!card->pci_mmap1) {
  2157. mwifiex_dbg(adapter, ERROR,
  2158. "iomap(2) error\n");
  2159. ret = -EIO;
  2160. goto err_iomap2;
  2161. }
  2162. mwifiex_dbg(adapter, INFO,
  2163. "PCI memory map Virt0: %p PCI memory map Virt2: %p\n",
  2164. card->pci_mmap, card->pci_mmap1);
  2165. card->cmdrsp_buf = NULL;
  2166. ret = mwifiex_pcie_create_txbd_ring(adapter);
  2167. if (ret)
  2168. goto err_cre_txbd;
  2169. ret = mwifiex_pcie_create_rxbd_ring(adapter);
  2170. if (ret)
  2171. goto err_cre_rxbd;
  2172. ret = mwifiex_pcie_create_evtbd_ring(adapter);
  2173. if (ret)
  2174. goto err_cre_evtbd;
  2175. ret = mwifiex_pcie_alloc_cmdrsp_buf(adapter);
  2176. if (ret)
  2177. goto err_alloc_cmdbuf;
  2178. if (reg->sleep_cookie) {
  2179. ret = mwifiex_pcie_alloc_sleep_cookie_buf(adapter);
  2180. if (ret)
  2181. goto err_alloc_cookie;
  2182. } else {
  2183. card->sleep_cookie_vbase = NULL;
  2184. }
  2185. return ret;
  2186. err_alloc_cookie:
  2187. mwifiex_pcie_delete_cmdrsp_buf(adapter);
  2188. err_alloc_cmdbuf:
  2189. mwifiex_pcie_delete_evtbd_ring(adapter);
  2190. err_cre_evtbd:
  2191. mwifiex_pcie_delete_rxbd_ring(adapter);
  2192. err_cre_rxbd:
  2193. mwifiex_pcie_delete_txbd_ring(adapter);
  2194. err_cre_txbd:
  2195. pci_iounmap(pdev, card->pci_mmap1);
  2196. err_iomap2:
  2197. pci_release_region(pdev, 2);
  2198. err_req_region2:
  2199. pci_iounmap(pdev, card->pci_mmap);
  2200. err_iomap0:
  2201. pci_release_region(pdev, 0);
  2202. err_req_region0:
  2203. err_set_dma_mask:
  2204. pci_disable_device(pdev);
  2205. err_enable_dev:
  2206. pci_set_drvdata(pdev, NULL);
  2207. return ret;
  2208. }
  2209. /*
  2210. * This function cleans up the allocated card buffers.
  2211. *
  2212. * The following are freed by this function -
  2213. * - TXBD ring buffers
  2214. * - RXBD ring buffers
  2215. * - Event BD ring buffers
  2216. * - Command response ring buffer
  2217. * - Sleep cookie buffer
  2218. */
  2219. static void mwifiex_pcie_cleanup(struct mwifiex_adapter *adapter)
  2220. {
  2221. struct pcie_service_card *card = adapter->card;
  2222. struct pci_dev *pdev = card->dev;
  2223. const struct mwifiex_pcie_card_reg *reg = card->pcie.reg;
  2224. if (user_rmmod) {
  2225. mwifiex_dbg(adapter, INFO,
  2226. "Clearing driver ready signature\n");
  2227. if (mwifiex_write_reg(adapter, reg->drv_rdy, 0x00000000))
  2228. mwifiex_dbg(adapter, ERROR,
  2229. "Failed to write driver not-ready signature\n");
  2230. }
  2231. if (pdev) {
  2232. pci_iounmap(pdev, card->pci_mmap);
  2233. pci_iounmap(pdev, card->pci_mmap1);
  2234. pci_disable_device(pdev);
  2235. pci_release_region(pdev, 2);
  2236. pci_release_region(pdev, 0);
  2237. pci_set_drvdata(pdev, NULL);
  2238. }
  2239. kfree(card);
  2240. }
  2241. /*
  2242. * This function registers the PCIE device.
  2243. *
  2244. * PCIE IRQ is claimed, block size is set and driver data is initialized.
  2245. */
  2246. static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
  2247. {
  2248. int ret;
  2249. struct pcie_service_card *card = adapter->card;
  2250. struct pci_dev *pdev = card->dev;
  2251. /* save adapter pointer in card */
  2252. card->adapter = adapter;
  2253. ret = request_irq(pdev->irq, mwifiex_pcie_interrupt, IRQF_SHARED,
  2254. "MRVL_PCIE", pdev);
  2255. if (ret) {
  2256. mwifiex_dbg(adapter, ERROR,
  2257. "request_irq failed: ret=%d\n", ret);
  2258. adapter->card = NULL;
  2259. return -1;
  2260. }
  2261. adapter->dev = &pdev->dev;
  2262. adapter->tx_buf_size = card->pcie.tx_buf_size;
  2263. adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
  2264. adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
  2265. strcpy(adapter->fw_name, card->pcie.firmware);
  2266. adapter->ext_scan = card->pcie.can_ext_scan;
  2267. return 0;
  2268. }
  2269. /*
  2270. * This function unregisters the PCIE device.
  2271. *
  2272. * The PCIE IRQ is released, the function is disabled and driver
  2273. * data is set to null.
  2274. */
  2275. static void mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
  2276. {
  2277. struct pcie_service_card *card = adapter->card;
  2278. const struct mwifiex_pcie_card_reg *reg;
  2279. if (card) {
  2280. mwifiex_dbg(adapter, INFO,
  2281. "%s(): calling free_irq()\n", __func__);
  2282. free_irq(card->dev->irq, card->dev);
  2283. reg = card->pcie.reg;
  2284. if (reg->sleep_cookie)
  2285. mwifiex_pcie_delete_sleep_cookie_buf(adapter);
  2286. mwifiex_pcie_delete_cmdrsp_buf(adapter);
  2287. mwifiex_pcie_delete_evtbd_ring(adapter);
  2288. mwifiex_pcie_delete_rxbd_ring(adapter);
  2289. mwifiex_pcie_delete_txbd_ring(adapter);
  2290. card->cmdrsp_buf = NULL;
  2291. }
  2292. }
  2293. static struct mwifiex_if_ops pcie_ops = {
  2294. .init_if = mwifiex_pcie_init,
  2295. .cleanup_if = mwifiex_pcie_cleanup,
  2296. .check_fw_status = mwifiex_check_fw_status,
  2297. .prog_fw = mwifiex_prog_fw_w_helper,
  2298. .register_dev = mwifiex_register_dev,
  2299. .unregister_dev = mwifiex_unregister_dev,
  2300. .enable_int = mwifiex_pcie_enable_host_int,
  2301. .process_int_status = mwifiex_process_int_status,
  2302. .host_to_card = mwifiex_pcie_host_to_card,
  2303. .wakeup = mwifiex_pm_wakeup_card,
  2304. .wakeup_complete = mwifiex_pm_wakeup_card_complete,
  2305. /* PCIE specific */
  2306. .cmdrsp_complete = mwifiex_pcie_cmdrsp_complete,
  2307. .event_complete = mwifiex_pcie_event_complete,
  2308. .update_mp_end_port = NULL,
  2309. .cleanup_mpa_buf = NULL,
  2310. .init_fw_port = mwifiex_pcie_init_fw_port,
  2311. .clean_pcie_ring = mwifiex_clean_pcie_ring_buf,
  2312. .device_dump = mwifiex_pcie_device_dump,
  2313. };
  2314. /*
  2315. * This function initializes the PCIE driver module.
  2316. *
  2317. * This initiates the semaphore and registers the device with
  2318. * PCIE bus.
  2319. */
  2320. static int mwifiex_pcie_init_module(void)
  2321. {
  2322. int ret;
  2323. pr_debug("Marvell PCIe Driver\n");
  2324. sema_init(&add_remove_card_sem, 1);
  2325. /* Clear the flag in case user removes the card. */
  2326. user_rmmod = 0;
  2327. ret = pci_register_driver(&mwifiex_pcie);
  2328. if (ret)
  2329. pr_err("Driver register failed!\n");
  2330. else
  2331. pr_debug("info: Driver registered successfully!\n");
  2332. return ret;
  2333. }
  2334. /*
  2335. * This function cleans up the PCIE driver.
  2336. *
  2337. * The following major steps are followed for cleanup -
  2338. * - Resume the device if its suspended
  2339. * - Disconnect the device if connected
  2340. * - Shutdown the firmware
  2341. * - Unregister the device from PCIE bus.
  2342. */
  2343. static void mwifiex_pcie_cleanup_module(void)
  2344. {
  2345. if (!down_interruptible(&add_remove_card_sem))
  2346. up(&add_remove_card_sem);
  2347. /* Set the flag as user is removing this module. */
  2348. user_rmmod = 1;
  2349. cancel_work_sync(&pcie_work);
  2350. pci_unregister_driver(&mwifiex_pcie);
  2351. }
  2352. module_init(mwifiex_pcie_init_module);
  2353. module_exit(mwifiex_pcie_cleanup_module);
  2354. MODULE_AUTHOR("Marvell International Ltd.");
  2355. MODULE_DESCRIPTION("Marvell WiFi-Ex PCI-Express Driver version " PCIE_VERSION);
  2356. MODULE_VERSION(PCIE_VERSION);
  2357. MODULE_LICENSE("GPL v2");
  2358. /*(DEBLOBBED)*/