wsm.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. /*
  2. * WSM host interface (HI) implementation for
  3. * ST-Ericsson CW1200 mac80211 drivers.
  4. *
  5. * Copyright (c) 2010, ST-Ericsson
  6. * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/skbuff.h>
  13. #include <linux/wait.h>
  14. #include <linux/delay.h>
  15. #include <linux/sched.h>
  16. #include <linux/random.h>
  17. #include "cw1200.h"
  18. #include "wsm.h"
  19. #include "bh.h"
  20. #include "sta.h"
  21. #include "debug.h"
  22. #define WSM_CMD_TIMEOUT (2 * HZ) /* With respect to interrupt loss */
  23. #define WSM_CMD_START_TIMEOUT (7 * HZ)
  24. #define WSM_CMD_RESET_TIMEOUT (3 * HZ) /* 2 sec. timeout was observed. */
  25. #define WSM_CMD_MAX_TIMEOUT (3 * HZ)
  26. #define WSM_SKIP(buf, size) \
  27. do { \
  28. if ((buf)->data + size > (buf)->end) \
  29. goto underflow; \
  30. (buf)->data += size; \
  31. } while (0)
  32. #define WSM_GET(buf, ptr, size) \
  33. do { \
  34. if ((buf)->data + size > (buf)->end) \
  35. goto underflow; \
  36. memcpy(ptr, (buf)->data, size); \
  37. (buf)->data += size; \
  38. } while (0)
  39. #define __WSM_GET(buf, type, type2, cvt) \
  40. ({ \
  41. type val; \
  42. if ((buf)->data + sizeof(type) > (buf)->end) \
  43. goto underflow; \
  44. val = cvt(*(type2 *)(buf)->data); \
  45. (buf)->data += sizeof(type); \
  46. val; \
  47. })
  48. #define WSM_GET8(buf) __WSM_GET(buf, u8, u8, (u8))
  49. #define WSM_GET16(buf) __WSM_GET(buf, u16, __le16, __le16_to_cpu)
  50. #define WSM_GET32(buf) __WSM_GET(buf, u32, __le32, __le32_to_cpu)
  51. #define WSM_PUT(buf, ptr, size) \
  52. do { \
  53. if ((buf)->data + size > (buf)->end) \
  54. if (wsm_buf_reserve((buf), size)) \
  55. goto nomem; \
  56. memcpy((buf)->data, ptr, size); \
  57. (buf)->data += size; \
  58. } while (0)
  59. #define __WSM_PUT(buf, val, type, type2, cvt) \
  60. do { \
  61. if ((buf)->data + sizeof(type) > (buf)->end) \
  62. if (wsm_buf_reserve((buf), sizeof(type))) \
  63. goto nomem; \
  64. *(type2 *)(buf)->data = cvt(val); \
  65. (buf)->data += sizeof(type); \
  66. } while (0)
  67. #define WSM_PUT8(buf, val) __WSM_PUT(buf, val, u8, u8, (u8))
  68. #define WSM_PUT16(buf, val) __WSM_PUT(buf, val, u16, __le16, __cpu_to_le16)
  69. #define WSM_PUT32(buf, val) __WSM_PUT(buf, val, u32, __le32, __cpu_to_le32)
  70. static void wsm_buf_reset(struct wsm_buf *buf);
  71. static int wsm_buf_reserve(struct wsm_buf *buf, size_t extra_size);
  72. static int wsm_cmd_send(struct cw1200_common *priv,
  73. struct wsm_buf *buf,
  74. void *arg, u16 cmd, long tmo);
  75. #define wsm_cmd_lock(__priv) mutex_lock(&((__priv)->wsm_cmd_mux))
  76. #define wsm_cmd_unlock(__priv) mutex_unlock(&((__priv)->wsm_cmd_mux))
  77. /* ******************************************************************** */
  78. /* WSM API implementation */
  79. static int wsm_generic_confirm(struct cw1200_common *priv,
  80. void *arg,
  81. struct wsm_buf *buf)
  82. {
  83. u32 status = WSM_GET32(buf);
  84. if (status != WSM_STATUS_SUCCESS)
  85. return -EINVAL;
  86. return 0;
  87. underflow:
  88. WARN_ON(1);
  89. return -EINVAL;
  90. }
  91. int wsm_configuration(struct cw1200_common *priv, struct wsm_configuration *arg)
  92. {
  93. int ret;
  94. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  95. wsm_cmd_lock(priv);
  96. WSM_PUT32(buf, arg->dot11MaxTransmitMsduLifeTime);
  97. WSM_PUT32(buf, arg->dot11MaxReceiveLifeTime);
  98. WSM_PUT32(buf, arg->dot11RtsThreshold);
  99. /* DPD block. */
  100. WSM_PUT16(buf, arg->dpdData_size + 12);
  101. WSM_PUT16(buf, 1); /* DPD version */
  102. WSM_PUT(buf, arg->dot11StationId, ETH_ALEN);
  103. WSM_PUT16(buf, 5); /* DPD flags */
  104. WSM_PUT(buf, arg->dpdData, arg->dpdData_size);
  105. ret = wsm_cmd_send(priv, buf, arg,
  106. WSM_CONFIGURATION_REQ_ID, WSM_CMD_TIMEOUT);
  107. wsm_cmd_unlock(priv);
  108. return ret;
  109. nomem:
  110. wsm_cmd_unlock(priv);
  111. return -ENOMEM;
  112. }
  113. static int wsm_configuration_confirm(struct cw1200_common *priv,
  114. struct wsm_configuration *arg,
  115. struct wsm_buf *buf)
  116. {
  117. int i;
  118. int status;
  119. status = WSM_GET32(buf);
  120. if (WARN_ON(status != WSM_STATUS_SUCCESS))
  121. return -EINVAL;
  122. WSM_GET(buf, arg->dot11StationId, ETH_ALEN);
  123. arg->dot11FrequencyBandsSupported = WSM_GET8(buf);
  124. WSM_SKIP(buf, 1);
  125. arg->supportedRateMask = WSM_GET32(buf);
  126. for (i = 0; i < 2; ++i) {
  127. arg->txPowerRange[i].min_power_level = WSM_GET32(buf);
  128. arg->txPowerRange[i].max_power_level = WSM_GET32(buf);
  129. arg->txPowerRange[i].stepping = WSM_GET32(buf);
  130. }
  131. return 0;
  132. underflow:
  133. WARN_ON(1);
  134. return -EINVAL;
  135. }
  136. /* ******************************************************************** */
  137. int wsm_reset(struct cw1200_common *priv, const struct wsm_reset *arg)
  138. {
  139. int ret;
  140. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  141. u16 cmd = WSM_RESET_REQ_ID | WSM_TX_LINK_ID(arg->link_id);
  142. wsm_cmd_lock(priv);
  143. WSM_PUT32(buf, arg->reset_statistics ? 0 : 1);
  144. ret = wsm_cmd_send(priv, buf, NULL, cmd, WSM_CMD_RESET_TIMEOUT);
  145. wsm_cmd_unlock(priv);
  146. return ret;
  147. nomem:
  148. wsm_cmd_unlock(priv);
  149. return -ENOMEM;
  150. }
  151. /* ******************************************************************** */
  152. struct wsm_mib {
  153. u16 mib_id;
  154. void *buf;
  155. size_t buf_size;
  156. };
  157. int wsm_read_mib(struct cw1200_common *priv, u16 mib_id, void *_buf,
  158. size_t buf_size)
  159. {
  160. int ret;
  161. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  162. struct wsm_mib mib_buf = {
  163. .mib_id = mib_id,
  164. .buf = _buf,
  165. .buf_size = buf_size,
  166. };
  167. wsm_cmd_lock(priv);
  168. WSM_PUT16(buf, mib_id);
  169. WSM_PUT16(buf, 0);
  170. ret = wsm_cmd_send(priv, buf, &mib_buf,
  171. WSM_READ_MIB_REQ_ID, WSM_CMD_TIMEOUT);
  172. wsm_cmd_unlock(priv);
  173. return ret;
  174. nomem:
  175. wsm_cmd_unlock(priv);
  176. return -ENOMEM;
  177. }
  178. static int wsm_read_mib_confirm(struct cw1200_common *priv,
  179. struct wsm_mib *arg,
  180. struct wsm_buf *buf)
  181. {
  182. u16 size;
  183. if (WARN_ON(WSM_GET32(buf) != WSM_STATUS_SUCCESS))
  184. return -EINVAL;
  185. if (WARN_ON(WSM_GET16(buf) != arg->mib_id))
  186. return -EINVAL;
  187. size = WSM_GET16(buf);
  188. if (size > arg->buf_size)
  189. size = arg->buf_size;
  190. WSM_GET(buf, arg->buf, size);
  191. arg->buf_size = size;
  192. return 0;
  193. underflow:
  194. WARN_ON(1);
  195. return -EINVAL;
  196. }
  197. /* ******************************************************************** */
  198. int wsm_write_mib(struct cw1200_common *priv, u16 mib_id, void *_buf,
  199. size_t buf_size)
  200. {
  201. int ret;
  202. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  203. struct wsm_mib mib_buf = {
  204. .mib_id = mib_id,
  205. .buf = _buf,
  206. .buf_size = buf_size,
  207. };
  208. wsm_cmd_lock(priv);
  209. WSM_PUT16(buf, mib_id);
  210. WSM_PUT16(buf, buf_size);
  211. WSM_PUT(buf, _buf, buf_size);
  212. ret = wsm_cmd_send(priv, buf, &mib_buf,
  213. WSM_WRITE_MIB_REQ_ID, WSM_CMD_TIMEOUT);
  214. wsm_cmd_unlock(priv);
  215. return ret;
  216. nomem:
  217. wsm_cmd_unlock(priv);
  218. return -ENOMEM;
  219. }
  220. static int wsm_write_mib_confirm(struct cw1200_common *priv,
  221. struct wsm_mib *arg,
  222. struct wsm_buf *buf)
  223. {
  224. int ret;
  225. ret = wsm_generic_confirm(priv, arg, buf);
  226. if (ret)
  227. return ret;
  228. if (arg->mib_id == WSM_MIB_ID_OPERATIONAL_POWER_MODE) {
  229. /* OperationalMode: update PM status. */
  230. const char *p = arg->buf;
  231. cw1200_enable_powersave(priv, (p[0] & 0x0F) ? true : false);
  232. }
  233. return 0;
  234. }
  235. /* ******************************************************************** */
  236. int wsm_scan(struct cw1200_common *priv, const struct wsm_scan *arg)
  237. {
  238. int i;
  239. int ret;
  240. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  241. if (arg->num_channels > 48)
  242. return -EINVAL;
  243. if (arg->num_ssids > 2)
  244. return -EINVAL;
  245. if (arg->band > 1)
  246. return -EINVAL;
  247. wsm_cmd_lock(priv);
  248. WSM_PUT8(buf, arg->band);
  249. WSM_PUT8(buf, arg->type);
  250. WSM_PUT8(buf, arg->flags);
  251. WSM_PUT8(buf, arg->max_tx_rate);
  252. WSM_PUT32(buf, arg->auto_scan_interval);
  253. WSM_PUT8(buf, arg->num_probes);
  254. WSM_PUT8(buf, arg->num_channels);
  255. WSM_PUT8(buf, arg->num_ssids);
  256. WSM_PUT8(buf, arg->probe_delay);
  257. for (i = 0; i < arg->num_channels; ++i) {
  258. WSM_PUT16(buf, arg->ch[i].number);
  259. WSM_PUT16(buf, 0);
  260. WSM_PUT32(buf, arg->ch[i].min_chan_time);
  261. WSM_PUT32(buf, arg->ch[i].max_chan_time);
  262. WSM_PUT32(buf, 0);
  263. }
  264. for (i = 0; i < arg->num_ssids; ++i) {
  265. WSM_PUT32(buf, arg->ssids[i].length);
  266. WSM_PUT(buf, &arg->ssids[i].ssid[0],
  267. sizeof(arg->ssids[i].ssid));
  268. }
  269. ret = wsm_cmd_send(priv, buf, NULL,
  270. WSM_START_SCAN_REQ_ID, WSM_CMD_TIMEOUT);
  271. wsm_cmd_unlock(priv);
  272. return ret;
  273. nomem:
  274. wsm_cmd_unlock(priv);
  275. return -ENOMEM;
  276. }
  277. /* ******************************************************************** */
  278. int wsm_stop_scan(struct cw1200_common *priv)
  279. {
  280. int ret;
  281. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  282. wsm_cmd_lock(priv);
  283. ret = wsm_cmd_send(priv, buf, NULL,
  284. WSM_STOP_SCAN_REQ_ID, WSM_CMD_TIMEOUT);
  285. wsm_cmd_unlock(priv);
  286. return ret;
  287. }
  288. static int wsm_tx_confirm(struct cw1200_common *priv,
  289. struct wsm_buf *buf,
  290. int link_id)
  291. {
  292. struct wsm_tx_confirm tx_confirm;
  293. tx_confirm.packet_id = WSM_GET32(buf);
  294. tx_confirm.status = WSM_GET32(buf);
  295. tx_confirm.tx_rate = WSM_GET8(buf);
  296. tx_confirm.ack_failures = WSM_GET8(buf);
  297. tx_confirm.flags = WSM_GET16(buf);
  298. tx_confirm.media_delay = WSM_GET32(buf);
  299. tx_confirm.tx_queue_delay = WSM_GET32(buf);
  300. cw1200_tx_confirm_cb(priv, link_id, &tx_confirm);
  301. return 0;
  302. underflow:
  303. WARN_ON(1);
  304. return -EINVAL;
  305. }
  306. static int wsm_multi_tx_confirm(struct cw1200_common *priv,
  307. struct wsm_buf *buf, int link_id)
  308. {
  309. int ret;
  310. int count;
  311. int i;
  312. count = WSM_GET32(buf);
  313. if (WARN_ON(count <= 0))
  314. return -EINVAL;
  315. if (count > 1) {
  316. /* We already released one buffer, now for the rest */
  317. ret = wsm_release_tx_buffer(priv, count - 1);
  318. if (ret < 0)
  319. return ret;
  320. else if (ret > 0)
  321. cw1200_bh_wakeup(priv);
  322. }
  323. cw1200_debug_txed_multi(priv, count);
  324. for (i = 0; i < count; ++i) {
  325. ret = wsm_tx_confirm(priv, buf, link_id);
  326. if (ret)
  327. return ret;
  328. }
  329. return ret;
  330. underflow:
  331. WARN_ON(1);
  332. return -EINVAL;
  333. }
  334. /* ******************************************************************** */
  335. static int wsm_join_confirm(struct cw1200_common *priv,
  336. struct wsm_join_cnf *arg,
  337. struct wsm_buf *buf)
  338. {
  339. arg->status = WSM_GET32(buf);
  340. if (WARN_ON(arg->status) != WSM_STATUS_SUCCESS)
  341. return -EINVAL;
  342. arg->min_power_level = WSM_GET32(buf);
  343. arg->max_power_level = WSM_GET32(buf);
  344. return 0;
  345. underflow:
  346. WARN_ON(1);
  347. return -EINVAL;
  348. }
  349. int wsm_join(struct cw1200_common *priv, struct wsm_join *arg)
  350. {
  351. int ret;
  352. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  353. struct wsm_join_cnf resp;
  354. wsm_cmd_lock(priv);
  355. WSM_PUT8(buf, arg->mode);
  356. WSM_PUT8(buf, arg->band);
  357. WSM_PUT16(buf, arg->channel_number);
  358. WSM_PUT(buf, &arg->bssid[0], sizeof(arg->bssid));
  359. WSM_PUT16(buf, arg->atim_window);
  360. WSM_PUT8(buf, arg->preamble_type);
  361. WSM_PUT8(buf, arg->probe_for_join);
  362. WSM_PUT8(buf, arg->dtim_period);
  363. WSM_PUT8(buf, arg->flags);
  364. WSM_PUT32(buf, arg->ssid_len);
  365. WSM_PUT(buf, &arg->ssid[0], sizeof(arg->ssid));
  366. WSM_PUT32(buf, arg->beacon_interval);
  367. WSM_PUT32(buf, arg->basic_rate_set);
  368. priv->tx_burst_idx = -1;
  369. ret = wsm_cmd_send(priv, buf, &resp,
  370. WSM_JOIN_REQ_ID, WSM_CMD_TIMEOUT);
  371. /* TODO: Update state based on resp.min|max_power_level */
  372. priv->join_complete_status = resp.status;
  373. wsm_cmd_unlock(priv);
  374. return ret;
  375. nomem:
  376. wsm_cmd_unlock(priv);
  377. return -ENOMEM;
  378. }
  379. /* ******************************************************************** */
  380. int wsm_set_bss_params(struct cw1200_common *priv,
  381. const struct wsm_set_bss_params *arg)
  382. {
  383. int ret;
  384. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  385. wsm_cmd_lock(priv);
  386. WSM_PUT8(buf, (arg->reset_beacon_loss ? 0x1 : 0));
  387. WSM_PUT8(buf, arg->beacon_lost_count);
  388. WSM_PUT16(buf, arg->aid);
  389. WSM_PUT32(buf, arg->operational_rate_set);
  390. ret = wsm_cmd_send(priv, buf, NULL,
  391. WSM_SET_BSS_PARAMS_REQ_ID, WSM_CMD_TIMEOUT);
  392. wsm_cmd_unlock(priv);
  393. return ret;
  394. nomem:
  395. wsm_cmd_unlock(priv);
  396. return -ENOMEM;
  397. }
  398. /* ******************************************************************** */
  399. int wsm_add_key(struct cw1200_common *priv, const struct wsm_add_key *arg)
  400. {
  401. int ret;
  402. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  403. wsm_cmd_lock(priv);
  404. WSM_PUT(buf, arg, sizeof(*arg));
  405. ret = wsm_cmd_send(priv, buf, NULL,
  406. WSM_ADD_KEY_REQ_ID, WSM_CMD_TIMEOUT);
  407. wsm_cmd_unlock(priv);
  408. return ret;
  409. nomem:
  410. wsm_cmd_unlock(priv);
  411. return -ENOMEM;
  412. }
  413. /* ******************************************************************** */
  414. int wsm_remove_key(struct cw1200_common *priv, const struct wsm_remove_key *arg)
  415. {
  416. int ret;
  417. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  418. wsm_cmd_lock(priv);
  419. WSM_PUT8(buf, arg->index);
  420. WSM_PUT8(buf, 0);
  421. WSM_PUT16(buf, 0);
  422. ret = wsm_cmd_send(priv, buf, NULL,
  423. WSM_REMOVE_KEY_REQ_ID, WSM_CMD_TIMEOUT);
  424. wsm_cmd_unlock(priv);
  425. return ret;
  426. nomem:
  427. wsm_cmd_unlock(priv);
  428. return -ENOMEM;
  429. }
  430. /* ******************************************************************** */
  431. int wsm_set_tx_queue_params(struct cw1200_common *priv,
  432. const struct wsm_set_tx_queue_params *arg, u8 id)
  433. {
  434. int ret;
  435. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  436. u8 queue_id_to_wmm_aci[] = {3, 2, 0, 1};
  437. wsm_cmd_lock(priv);
  438. WSM_PUT8(buf, queue_id_to_wmm_aci[id]);
  439. WSM_PUT8(buf, 0);
  440. WSM_PUT8(buf, arg->ackPolicy);
  441. WSM_PUT8(buf, 0);
  442. WSM_PUT32(buf, arg->maxTransmitLifetime);
  443. WSM_PUT16(buf, arg->allowedMediumTime);
  444. WSM_PUT16(buf, 0);
  445. ret = wsm_cmd_send(priv, buf, NULL, 0x0012, WSM_CMD_TIMEOUT);
  446. wsm_cmd_unlock(priv);
  447. return ret;
  448. nomem:
  449. wsm_cmd_unlock(priv);
  450. return -ENOMEM;
  451. }
  452. /* ******************************************************************** */
  453. int wsm_set_edca_params(struct cw1200_common *priv,
  454. const struct wsm_edca_params *arg)
  455. {
  456. int ret;
  457. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  458. wsm_cmd_lock(priv);
  459. /* Implemented according to specification. */
  460. WSM_PUT16(buf, arg->params[3].cwmin);
  461. WSM_PUT16(buf, arg->params[2].cwmin);
  462. WSM_PUT16(buf, arg->params[1].cwmin);
  463. WSM_PUT16(buf, arg->params[0].cwmin);
  464. WSM_PUT16(buf, arg->params[3].cwmax);
  465. WSM_PUT16(buf, arg->params[2].cwmax);
  466. WSM_PUT16(buf, arg->params[1].cwmax);
  467. WSM_PUT16(buf, arg->params[0].cwmax);
  468. WSM_PUT8(buf, arg->params[3].aifns);
  469. WSM_PUT8(buf, arg->params[2].aifns);
  470. WSM_PUT8(buf, arg->params[1].aifns);
  471. WSM_PUT8(buf, arg->params[0].aifns);
  472. WSM_PUT16(buf, arg->params[3].txop_limit);
  473. WSM_PUT16(buf, arg->params[2].txop_limit);
  474. WSM_PUT16(buf, arg->params[1].txop_limit);
  475. WSM_PUT16(buf, arg->params[0].txop_limit);
  476. WSM_PUT32(buf, arg->params[3].max_rx_lifetime);
  477. WSM_PUT32(buf, arg->params[2].max_rx_lifetime);
  478. WSM_PUT32(buf, arg->params[1].max_rx_lifetime);
  479. WSM_PUT32(buf, arg->params[0].max_rx_lifetime);
  480. ret = wsm_cmd_send(priv, buf, NULL,
  481. WSM_EDCA_PARAMS_REQ_ID, WSM_CMD_TIMEOUT);
  482. wsm_cmd_unlock(priv);
  483. return ret;
  484. nomem:
  485. wsm_cmd_unlock(priv);
  486. return -ENOMEM;
  487. }
  488. /* ******************************************************************** */
  489. int wsm_switch_channel(struct cw1200_common *priv,
  490. const struct wsm_switch_channel *arg)
  491. {
  492. int ret;
  493. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  494. wsm_cmd_lock(priv);
  495. WSM_PUT8(buf, arg->mode);
  496. WSM_PUT8(buf, arg->switch_count);
  497. WSM_PUT16(buf, arg->channel_number);
  498. priv->channel_switch_in_progress = 1;
  499. ret = wsm_cmd_send(priv, buf, NULL,
  500. WSM_SWITCH_CHANNEL_REQ_ID, WSM_CMD_TIMEOUT);
  501. if (ret)
  502. priv->channel_switch_in_progress = 0;
  503. wsm_cmd_unlock(priv);
  504. return ret;
  505. nomem:
  506. wsm_cmd_unlock(priv);
  507. return -ENOMEM;
  508. }
  509. /* ******************************************************************** */
  510. int wsm_set_pm(struct cw1200_common *priv, const struct wsm_set_pm *arg)
  511. {
  512. int ret;
  513. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  514. priv->ps_mode_switch_in_progress = 1;
  515. wsm_cmd_lock(priv);
  516. WSM_PUT8(buf, arg->mode);
  517. WSM_PUT8(buf, arg->fast_psm_idle_period);
  518. WSM_PUT8(buf, arg->ap_psm_change_period);
  519. WSM_PUT8(buf, arg->min_auto_pspoll_period);
  520. ret = wsm_cmd_send(priv, buf, NULL,
  521. WSM_SET_PM_REQ_ID, WSM_CMD_TIMEOUT);
  522. wsm_cmd_unlock(priv);
  523. return ret;
  524. nomem:
  525. wsm_cmd_unlock(priv);
  526. return -ENOMEM;
  527. }
  528. /* ******************************************************************** */
  529. int wsm_start(struct cw1200_common *priv, const struct wsm_start *arg)
  530. {
  531. int ret;
  532. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  533. wsm_cmd_lock(priv);
  534. WSM_PUT8(buf, arg->mode);
  535. WSM_PUT8(buf, arg->band);
  536. WSM_PUT16(buf, arg->channel_number);
  537. WSM_PUT32(buf, arg->ct_window);
  538. WSM_PUT32(buf, arg->beacon_interval);
  539. WSM_PUT8(buf, arg->dtim_period);
  540. WSM_PUT8(buf, arg->preamble);
  541. WSM_PUT8(buf, arg->probe_delay);
  542. WSM_PUT8(buf, arg->ssid_len);
  543. WSM_PUT(buf, arg->ssid, sizeof(arg->ssid));
  544. WSM_PUT32(buf, arg->basic_rate_set);
  545. priv->tx_burst_idx = -1;
  546. ret = wsm_cmd_send(priv, buf, NULL,
  547. WSM_START_REQ_ID, WSM_CMD_START_TIMEOUT);
  548. wsm_cmd_unlock(priv);
  549. return ret;
  550. nomem:
  551. wsm_cmd_unlock(priv);
  552. return -ENOMEM;
  553. }
  554. /* ******************************************************************** */
  555. int wsm_beacon_transmit(struct cw1200_common *priv,
  556. const struct wsm_beacon_transmit *arg)
  557. {
  558. int ret;
  559. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  560. wsm_cmd_lock(priv);
  561. WSM_PUT32(buf, arg->enable_beaconing ? 1 : 0);
  562. ret = wsm_cmd_send(priv, buf, NULL,
  563. WSM_BEACON_TRANSMIT_REQ_ID, WSM_CMD_TIMEOUT);
  564. wsm_cmd_unlock(priv);
  565. return ret;
  566. nomem:
  567. wsm_cmd_unlock(priv);
  568. return -ENOMEM;
  569. }
  570. /* ******************************************************************** */
  571. int wsm_start_find(struct cw1200_common *priv)
  572. {
  573. int ret;
  574. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  575. wsm_cmd_lock(priv);
  576. ret = wsm_cmd_send(priv, buf, NULL, 0x0019, WSM_CMD_TIMEOUT);
  577. wsm_cmd_unlock(priv);
  578. return ret;
  579. }
  580. /* ******************************************************************** */
  581. int wsm_stop_find(struct cw1200_common *priv)
  582. {
  583. int ret;
  584. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  585. wsm_cmd_lock(priv);
  586. ret = wsm_cmd_send(priv, buf, NULL, 0x001A, WSM_CMD_TIMEOUT);
  587. wsm_cmd_unlock(priv);
  588. return ret;
  589. }
  590. /* ******************************************************************** */
  591. int wsm_map_link(struct cw1200_common *priv, const struct wsm_map_link *arg)
  592. {
  593. int ret;
  594. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  595. u16 cmd = 0x001C | WSM_TX_LINK_ID(arg->link_id);
  596. wsm_cmd_lock(priv);
  597. WSM_PUT(buf, &arg->mac_addr[0], sizeof(arg->mac_addr));
  598. WSM_PUT16(buf, 0);
  599. ret = wsm_cmd_send(priv, buf, NULL, cmd, WSM_CMD_TIMEOUT);
  600. wsm_cmd_unlock(priv);
  601. return ret;
  602. nomem:
  603. wsm_cmd_unlock(priv);
  604. return -ENOMEM;
  605. }
  606. /* ******************************************************************** */
  607. int wsm_update_ie(struct cw1200_common *priv,
  608. const struct wsm_update_ie *arg)
  609. {
  610. int ret;
  611. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  612. wsm_cmd_lock(priv);
  613. WSM_PUT16(buf, arg->what);
  614. WSM_PUT16(buf, arg->count);
  615. WSM_PUT(buf, arg->ies, arg->length);
  616. ret = wsm_cmd_send(priv, buf, NULL, 0x001B, WSM_CMD_TIMEOUT);
  617. wsm_cmd_unlock(priv);
  618. return ret;
  619. nomem:
  620. wsm_cmd_unlock(priv);
  621. return -ENOMEM;
  622. }
  623. /* ******************************************************************** */
  624. int wsm_set_probe_responder(struct cw1200_common *priv, bool enable)
  625. {
  626. priv->rx_filter.probeResponder = enable;
  627. return wsm_set_rx_filter(priv, &priv->rx_filter);
  628. }
  629. /* ******************************************************************** */
  630. /* WSM indication events implementation */
  631. const char * const cw1200_fw_types[] = {
  632. "ETF",
  633. "WFM",
  634. "WSM",
  635. "HI test",
  636. "Platform test"
  637. };
  638. static int wsm_startup_indication(struct cw1200_common *priv,
  639. struct wsm_buf *buf)
  640. {
  641. priv->wsm_caps.input_buffers = WSM_GET16(buf);
  642. priv->wsm_caps.input_buffer_size = WSM_GET16(buf);
  643. priv->wsm_caps.hw_id = WSM_GET16(buf);
  644. priv->wsm_caps.hw_subid = WSM_GET16(buf);
  645. priv->wsm_caps.status = WSM_GET16(buf);
  646. priv->wsm_caps.fw_cap = WSM_GET16(buf);
  647. priv->wsm_caps.fw_type = WSM_GET16(buf);
  648. priv->wsm_caps.fw_api = WSM_GET16(buf);
  649. priv->wsm_caps.fw_build = WSM_GET16(buf);
  650. priv->wsm_caps.fw_ver = WSM_GET16(buf);
  651. WSM_GET(buf, priv->wsm_caps.fw_label, sizeof(priv->wsm_caps.fw_label));
  652. priv->wsm_caps.fw_label[sizeof(priv->wsm_caps.fw_label) - 1] = 0; /* Do not trust FW too much... */
  653. if (WARN_ON(priv->wsm_caps.status))
  654. return -EINVAL;
  655. if (WARN_ON(priv->wsm_caps.fw_type > 4))
  656. return -EINVAL;
  657. pr_info("CW1200 WSM init done.\n"
  658. " Input buffers: %d x %d bytes\n"
  659. " Hardware: %d.%d\n"
  660. " %s firmware [%s], ver: %d, build: %d,"
  661. " api: %d, cap: 0x%.4X\n",
  662. priv->wsm_caps.input_buffers,
  663. priv->wsm_caps.input_buffer_size,
  664. priv->wsm_caps.hw_id, priv->wsm_caps.hw_subid,
  665. cw1200_fw_types[priv->wsm_caps.fw_type],
  666. priv->wsm_caps.fw_label, priv->wsm_caps.fw_ver,
  667. priv->wsm_caps.fw_build,
  668. priv->wsm_caps.fw_api, priv->wsm_caps.fw_cap);
  669. /* Disable unsupported frequency bands */
  670. if (!(priv->wsm_caps.fw_cap & 0x1))
  671. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  672. if (!(priv->wsm_caps.fw_cap & 0x2))
  673. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  674. priv->firmware_ready = 1;
  675. wake_up(&priv->wsm_startup_done);
  676. return 0;
  677. underflow:
  678. WARN_ON(1);
  679. return -EINVAL;
  680. }
  681. static int wsm_receive_indication(struct cw1200_common *priv,
  682. int link_id,
  683. struct wsm_buf *buf,
  684. struct sk_buff **skb_p)
  685. {
  686. struct wsm_rx rx;
  687. struct ieee80211_hdr *hdr;
  688. size_t hdr_len;
  689. __le16 fctl;
  690. rx.status = WSM_GET32(buf);
  691. rx.channel_number = WSM_GET16(buf);
  692. rx.rx_rate = WSM_GET8(buf);
  693. rx.rcpi_rssi = WSM_GET8(buf);
  694. rx.flags = WSM_GET32(buf);
  695. /* FW Workaround: Drop probe resp or
  696. beacon when RSSI is 0
  697. */
  698. hdr = (struct ieee80211_hdr *)(*skb_p)->data;
  699. if (!rx.rcpi_rssi &&
  700. (ieee80211_is_probe_resp(hdr->frame_control) ||
  701. ieee80211_is_beacon(hdr->frame_control)))
  702. return 0;
  703. /* If no RSSI subscription has been made,
  704. * convert RCPI to RSSI here
  705. */
  706. if (!priv->cqm_use_rssi)
  707. rx.rcpi_rssi = rx.rcpi_rssi / 2 - 110;
  708. fctl = *(__le16 *)buf->data;
  709. hdr_len = buf->data - buf->begin;
  710. skb_pull(*skb_p, hdr_len);
  711. if (!rx.status && ieee80211_is_deauth(fctl)) {
  712. if (priv->join_status == CW1200_JOIN_STATUS_STA) {
  713. /* Shedule unjoin work */
  714. pr_debug("[WSM] Issue unjoin command (RX).\n");
  715. wsm_lock_tx_async(priv);
  716. if (queue_work(priv->workqueue,
  717. &priv->unjoin_work) <= 0)
  718. wsm_unlock_tx(priv);
  719. }
  720. }
  721. cw1200_rx_cb(priv, &rx, link_id, skb_p);
  722. if (*skb_p)
  723. skb_push(*skb_p, hdr_len);
  724. return 0;
  725. underflow:
  726. return -EINVAL;
  727. }
  728. static int wsm_event_indication(struct cw1200_common *priv, struct wsm_buf *buf)
  729. {
  730. int first;
  731. struct cw1200_wsm_event *event;
  732. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) {
  733. /* STA is stopped. */
  734. return 0;
  735. }
  736. event = kzalloc(sizeof(struct cw1200_wsm_event), GFP_KERNEL);
  737. if (!event)
  738. return -ENOMEM;
  739. event->evt.id = WSM_GET32(buf);
  740. event->evt.data = WSM_GET32(buf);
  741. pr_debug("[WSM] Event: %d(%d)\n",
  742. event->evt.id, event->evt.data);
  743. spin_lock(&priv->event_queue_lock);
  744. first = list_empty(&priv->event_queue);
  745. list_add_tail(&event->link, &priv->event_queue);
  746. spin_unlock(&priv->event_queue_lock);
  747. if (first)
  748. queue_work(priv->workqueue, &priv->event_handler);
  749. return 0;
  750. underflow:
  751. kfree(event);
  752. return -EINVAL;
  753. }
  754. static int wsm_channel_switch_indication(struct cw1200_common *priv,
  755. struct wsm_buf *buf)
  756. {
  757. WARN_ON(WSM_GET32(buf));
  758. priv->channel_switch_in_progress = 0;
  759. wake_up(&priv->channel_switch_done);
  760. wsm_unlock_tx(priv);
  761. return 0;
  762. underflow:
  763. return -EINVAL;
  764. }
  765. static int wsm_set_pm_indication(struct cw1200_common *priv,
  766. struct wsm_buf *buf)
  767. {
  768. /* TODO: Check buf (struct wsm_set_pm_complete) for validity */
  769. if (priv->ps_mode_switch_in_progress) {
  770. priv->ps_mode_switch_in_progress = 0;
  771. wake_up(&priv->ps_mode_switch_done);
  772. }
  773. return 0;
  774. }
  775. static int wsm_scan_started(struct cw1200_common *priv, void *arg,
  776. struct wsm_buf *buf)
  777. {
  778. u32 status = WSM_GET32(buf);
  779. if (status != WSM_STATUS_SUCCESS) {
  780. cw1200_scan_failed_cb(priv);
  781. return -EINVAL;
  782. }
  783. return 0;
  784. underflow:
  785. WARN_ON(1);
  786. return -EINVAL;
  787. }
  788. static int wsm_scan_complete_indication(struct cw1200_common *priv,
  789. struct wsm_buf *buf)
  790. {
  791. struct wsm_scan_complete arg;
  792. arg.status = WSM_GET32(buf);
  793. arg.psm = WSM_GET8(buf);
  794. arg.num_channels = WSM_GET8(buf);
  795. cw1200_scan_complete_cb(priv, &arg);
  796. return 0;
  797. underflow:
  798. return -EINVAL;
  799. }
  800. static int wsm_join_complete_indication(struct cw1200_common *priv,
  801. struct wsm_buf *buf)
  802. {
  803. struct wsm_join_complete arg;
  804. arg.status = WSM_GET32(buf);
  805. pr_debug("[WSM] Join complete indication, status: %d\n", arg.status);
  806. cw1200_join_complete_cb(priv, &arg);
  807. return 0;
  808. underflow:
  809. return -EINVAL;
  810. }
  811. static int wsm_find_complete_indication(struct cw1200_common *priv,
  812. struct wsm_buf *buf)
  813. {
  814. pr_warn("Implement find_complete_indication\n");
  815. return 0;
  816. }
  817. static int wsm_ba_timeout_indication(struct cw1200_common *priv,
  818. struct wsm_buf *buf)
  819. {
  820. u32 dummy;
  821. u8 tid;
  822. u8 dummy2;
  823. u8 addr[ETH_ALEN];
  824. dummy = WSM_GET32(buf);
  825. tid = WSM_GET8(buf);
  826. dummy2 = WSM_GET8(buf);
  827. WSM_GET(buf, addr, ETH_ALEN);
  828. pr_info("BlockACK timeout, tid %d, addr %pM\n",
  829. tid, addr);
  830. return 0;
  831. underflow:
  832. return -EINVAL;
  833. }
  834. static int wsm_suspend_resume_indication(struct cw1200_common *priv,
  835. int link_id, struct wsm_buf *buf)
  836. {
  837. u32 flags;
  838. struct wsm_suspend_resume arg;
  839. flags = WSM_GET32(buf);
  840. arg.link_id = link_id;
  841. arg.stop = !(flags & 1);
  842. arg.multicast = !!(flags & 8);
  843. arg.queue = (flags >> 1) & 3;
  844. cw1200_suspend_resume(priv, &arg);
  845. return 0;
  846. underflow:
  847. return -EINVAL;
  848. }
  849. /* ******************************************************************** */
  850. /* WSM TX */
  851. static int wsm_cmd_send(struct cw1200_common *priv,
  852. struct wsm_buf *buf,
  853. void *arg, u16 cmd, long tmo)
  854. {
  855. size_t buf_len = buf->data - buf->begin;
  856. int ret;
  857. /* Don't bother if we're dead. */
  858. if (priv->bh_error) {
  859. ret = 0;
  860. goto done;
  861. }
  862. /* Block until the cmd buffer is completed. Tortuous. */
  863. spin_lock(&priv->wsm_cmd.lock);
  864. while (!priv->wsm_cmd.done) {
  865. spin_unlock(&priv->wsm_cmd.lock);
  866. spin_lock(&priv->wsm_cmd.lock);
  867. }
  868. priv->wsm_cmd.done = 0;
  869. spin_unlock(&priv->wsm_cmd.lock);
  870. if (cmd == WSM_WRITE_MIB_REQ_ID ||
  871. cmd == WSM_READ_MIB_REQ_ID)
  872. pr_debug("[WSM] >>> 0x%.4X [MIB: 0x%.4X] (%zu)\n",
  873. cmd, __le16_to_cpu(((__le16 *)buf->begin)[2]),
  874. buf_len);
  875. else
  876. pr_debug("[WSM] >>> 0x%.4X (%zu)\n", cmd, buf_len);
  877. /* Due to buggy SPI on CW1200, we need to
  878. * pad the message by a few bytes to ensure
  879. * that it's completely received.
  880. */
  881. buf_len += 4;
  882. /* Fill HI message header */
  883. /* BH will add sequence number */
  884. ((__le16 *)buf->begin)[0] = __cpu_to_le16(buf_len);
  885. ((__le16 *)buf->begin)[1] = __cpu_to_le16(cmd);
  886. spin_lock(&priv->wsm_cmd.lock);
  887. BUG_ON(priv->wsm_cmd.ptr);
  888. priv->wsm_cmd.ptr = buf->begin;
  889. priv->wsm_cmd.len = buf_len;
  890. priv->wsm_cmd.arg = arg;
  891. priv->wsm_cmd.cmd = cmd;
  892. spin_unlock(&priv->wsm_cmd.lock);
  893. cw1200_bh_wakeup(priv);
  894. /* Wait for command completion */
  895. ret = wait_event_timeout(priv->wsm_cmd_wq,
  896. priv->wsm_cmd.done, tmo);
  897. if (!ret && !priv->wsm_cmd.done) {
  898. spin_lock(&priv->wsm_cmd.lock);
  899. priv->wsm_cmd.done = 1;
  900. priv->wsm_cmd.ptr = NULL;
  901. spin_unlock(&priv->wsm_cmd.lock);
  902. if (priv->bh_error) {
  903. /* Return ok to help system cleanup */
  904. ret = 0;
  905. } else {
  906. pr_err("CMD req (0x%04x) stuck in firmware, killing BH\n", priv->wsm_cmd.cmd);
  907. print_hex_dump_bytes("REQDUMP: ", DUMP_PREFIX_NONE,
  908. buf->begin, buf_len);
  909. pr_err("Outstanding outgoing frames: %d\n", priv->hw_bufs_used);
  910. /* Kill BH thread to report the error to the top layer. */
  911. atomic_add(1, &priv->bh_term);
  912. wake_up(&priv->bh_wq);
  913. ret = -ETIMEDOUT;
  914. }
  915. } else {
  916. spin_lock(&priv->wsm_cmd.lock);
  917. BUG_ON(!priv->wsm_cmd.done);
  918. ret = priv->wsm_cmd.ret;
  919. spin_unlock(&priv->wsm_cmd.lock);
  920. }
  921. done:
  922. wsm_buf_reset(buf);
  923. return ret;
  924. }
  925. /* ******************************************************************** */
  926. /* WSM TX port control */
  927. void wsm_lock_tx(struct cw1200_common *priv)
  928. {
  929. wsm_cmd_lock(priv);
  930. if (atomic_add_return(1, &priv->tx_lock) == 1) {
  931. if (wsm_flush_tx(priv))
  932. pr_debug("[WSM] TX is locked.\n");
  933. }
  934. wsm_cmd_unlock(priv);
  935. }
  936. void wsm_lock_tx_async(struct cw1200_common *priv)
  937. {
  938. if (atomic_add_return(1, &priv->tx_lock) == 1)
  939. pr_debug("[WSM] TX is locked (async).\n");
  940. }
  941. bool wsm_flush_tx(struct cw1200_common *priv)
  942. {
  943. unsigned long timestamp = jiffies;
  944. bool pending = false;
  945. long timeout;
  946. int i;
  947. /* Flush must be called with TX lock held. */
  948. BUG_ON(!atomic_read(&priv->tx_lock));
  949. /* First check if we really need to do something.
  950. * It is safe to use unprotected access, as hw_bufs_used
  951. * can only decrements.
  952. */
  953. if (!priv->hw_bufs_used)
  954. return true;
  955. if (priv->bh_error) {
  956. /* In case of failure do not wait for magic. */
  957. pr_err("[WSM] Fatal error occurred, will not flush TX.\n");
  958. return false;
  959. } else {
  960. /* Get a timestamp of "oldest" frame */
  961. for (i = 0; i < 4; ++i)
  962. pending |= cw1200_queue_get_xmit_timestamp(
  963. &priv->tx_queue[i],
  964. &timestamp, 0xffffffff);
  965. /* If there's nothing pending, we're good */
  966. if (!pending)
  967. return true;
  968. timeout = timestamp + WSM_CMD_LAST_CHANCE_TIMEOUT - jiffies;
  969. if (timeout < 0 || wait_event_timeout(priv->bh_evt_wq,
  970. !priv->hw_bufs_used,
  971. timeout) <= 0) {
  972. /* Hmmm... Not good. Frame had stuck in firmware. */
  973. priv->bh_error = 1;
  974. wiphy_err(priv->hw->wiphy, "[WSM] TX Frames (%d) stuck in firmware, killing BH\n", priv->hw_bufs_used);
  975. wake_up(&priv->bh_wq);
  976. return false;
  977. }
  978. /* Ok, everything is flushed. */
  979. return true;
  980. }
  981. }
  982. void wsm_unlock_tx(struct cw1200_common *priv)
  983. {
  984. int tx_lock;
  985. tx_lock = atomic_sub_return(1, &priv->tx_lock);
  986. BUG_ON(tx_lock < 0);
  987. if (tx_lock == 0) {
  988. if (!priv->bh_error)
  989. cw1200_bh_wakeup(priv);
  990. pr_debug("[WSM] TX is unlocked.\n");
  991. }
  992. }
  993. /* ******************************************************************** */
  994. /* WSM RX */
  995. int wsm_handle_exception(struct cw1200_common *priv, u8 *data, size_t len)
  996. {
  997. struct wsm_buf buf;
  998. u32 reason;
  999. u32 reg[18];
  1000. char fname[48];
  1001. unsigned int i;
  1002. static const char * const reason_str[] = {
  1003. "undefined instruction",
  1004. "prefetch abort",
  1005. "data abort",
  1006. "unknown error",
  1007. };
  1008. buf.begin = buf.data = data;
  1009. buf.end = &buf.begin[len];
  1010. reason = WSM_GET32(&buf);
  1011. for (i = 0; i < ARRAY_SIZE(reg); ++i)
  1012. reg[i] = WSM_GET32(&buf);
  1013. WSM_GET(&buf, fname, sizeof(fname));
  1014. if (reason < 4)
  1015. wiphy_err(priv->hw->wiphy,
  1016. "Firmware exception: %s.\n",
  1017. reason_str[reason]);
  1018. else
  1019. wiphy_err(priv->hw->wiphy,
  1020. "Firmware assert at %.*s, line %d\n",
  1021. (int) sizeof(fname), fname, reg[1]);
  1022. for (i = 0; i < 12; i += 4)
  1023. wiphy_err(priv->hw->wiphy,
  1024. "R%d: 0x%.8X, R%d: 0x%.8X, R%d: 0x%.8X, R%d: 0x%.8X,\n",
  1025. i + 0, reg[i + 0], i + 1, reg[i + 1],
  1026. i + 2, reg[i + 2], i + 3, reg[i + 3]);
  1027. wiphy_err(priv->hw->wiphy,
  1028. "R12: 0x%.8X, SP: 0x%.8X, LR: 0x%.8X, PC: 0x%.8X,\n",
  1029. reg[i + 0], reg[i + 1], reg[i + 2], reg[i + 3]);
  1030. i += 4;
  1031. wiphy_err(priv->hw->wiphy,
  1032. "CPSR: 0x%.8X, SPSR: 0x%.8X\n",
  1033. reg[i + 0], reg[i + 1]);
  1034. print_hex_dump_bytes("R1: ", DUMP_PREFIX_NONE,
  1035. fname, sizeof(fname));
  1036. return 0;
  1037. underflow:
  1038. wiphy_err(priv->hw->wiphy, "Firmware exception.\n");
  1039. print_hex_dump_bytes("Exception: ", DUMP_PREFIX_NONE,
  1040. data, len);
  1041. return -EINVAL;
  1042. }
  1043. int wsm_handle_rx(struct cw1200_common *priv, u16 id,
  1044. struct wsm_hdr *wsm, struct sk_buff **skb_p)
  1045. {
  1046. int ret = 0;
  1047. struct wsm_buf wsm_buf;
  1048. int link_id = (id >> 6) & 0x0F;
  1049. /* Strip link id. */
  1050. id &= ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX);
  1051. wsm_buf.begin = (u8 *)&wsm[0];
  1052. wsm_buf.data = (u8 *)&wsm[1];
  1053. wsm_buf.end = &wsm_buf.begin[__le16_to_cpu(wsm->len)];
  1054. pr_debug("[WSM] <<< 0x%.4X (%td)\n", id,
  1055. wsm_buf.end - wsm_buf.begin);
  1056. if (id == WSM_TX_CONFIRM_IND_ID) {
  1057. ret = wsm_tx_confirm(priv, &wsm_buf, link_id);
  1058. } else if (id == WSM_MULTI_TX_CONFIRM_ID) {
  1059. ret = wsm_multi_tx_confirm(priv, &wsm_buf, link_id);
  1060. } else if (id & 0x0400) {
  1061. void *wsm_arg;
  1062. u16 wsm_cmd;
  1063. /* Do not trust FW too much. Protection against repeated
  1064. * response and race condition removal (see above).
  1065. */
  1066. spin_lock(&priv->wsm_cmd.lock);
  1067. wsm_arg = priv->wsm_cmd.arg;
  1068. wsm_cmd = priv->wsm_cmd.cmd &
  1069. ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX);
  1070. priv->wsm_cmd.cmd = 0xFFFF;
  1071. spin_unlock(&priv->wsm_cmd.lock);
  1072. if (WARN_ON((id & ~0x0400) != wsm_cmd)) {
  1073. /* Note that any non-zero is a fatal retcode. */
  1074. ret = -EINVAL;
  1075. goto out;
  1076. }
  1077. /* Note that wsm_arg can be NULL in case of timeout in
  1078. * wsm_cmd_send().
  1079. */
  1080. switch (id) {
  1081. case WSM_READ_MIB_RESP_ID:
  1082. if (wsm_arg)
  1083. ret = wsm_read_mib_confirm(priv, wsm_arg,
  1084. &wsm_buf);
  1085. break;
  1086. case WSM_WRITE_MIB_RESP_ID:
  1087. if (wsm_arg)
  1088. ret = wsm_write_mib_confirm(priv, wsm_arg,
  1089. &wsm_buf);
  1090. break;
  1091. case WSM_START_SCAN_RESP_ID:
  1092. if (wsm_arg)
  1093. ret = wsm_scan_started(priv, wsm_arg, &wsm_buf);
  1094. break;
  1095. case WSM_CONFIGURATION_RESP_ID:
  1096. if (wsm_arg)
  1097. ret = wsm_configuration_confirm(priv, wsm_arg,
  1098. &wsm_buf);
  1099. break;
  1100. case WSM_JOIN_RESP_ID:
  1101. if (wsm_arg)
  1102. ret = wsm_join_confirm(priv, wsm_arg, &wsm_buf);
  1103. break;
  1104. case WSM_STOP_SCAN_RESP_ID:
  1105. case WSM_RESET_RESP_ID:
  1106. case WSM_ADD_KEY_RESP_ID:
  1107. case WSM_REMOVE_KEY_RESP_ID:
  1108. case WSM_SET_PM_RESP_ID:
  1109. case WSM_SET_BSS_PARAMS_RESP_ID:
  1110. case 0x0412: /* set_tx_queue_params */
  1111. case WSM_EDCA_PARAMS_RESP_ID:
  1112. case WSM_SWITCH_CHANNEL_RESP_ID:
  1113. case WSM_START_RESP_ID:
  1114. case WSM_BEACON_TRANSMIT_RESP_ID:
  1115. case 0x0419: /* start_find */
  1116. case 0x041A: /* stop_find */
  1117. case 0x041B: /* update_ie */
  1118. case 0x041C: /* map_link */
  1119. WARN_ON(wsm_arg != NULL);
  1120. ret = wsm_generic_confirm(priv, wsm_arg, &wsm_buf);
  1121. if (ret) {
  1122. wiphy_warn(priv->hw->wiphy,
  1123. "wsm_generic_confirm failed for request 0x%04x.\n",
  1124. id & ~0x0400);
  1125. /* often 0x407 and 0x410 occur, this means we're dead.. */
  1126. if (priv->join_status >= CW1200_JOIN_STATUS_JOINING) {
  1127. wsm_lock_tx(priv);
  1128. if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
  1129. wsm_unlock_tx(priv);
  1130. }
  1131. }
  1132. break;
  1133. default:
  1134. wiphy_warn(priv->hw->wiphy,
  1135. "Unrecognized confirmation 0x%04x\n",
  1136. id & ~0x0400);
  1137. }
  1138. spin_lock(&priv->wsm_cmd.lock);
  1139. priv->wsm_cmd.ret = ret;
  1140. priv->wsm_cmd.done = 1;
  1141. spin_unlock(&priv->wsm_cmd.lock);
  1142. ret = 0; /* Error response from device should ne stop BH. */
  1143. wake_up(&priv->wsm_cmd_wq);
  1144. } else if (id & 0x0800) {
  1145. switch (id) {
  1146. case WSM_STARTUP_IND_ID:
  1147. ret = wsm_startup_indication(priv, &wsm_buf);
  1148. break;
  1149. case WSM_RECEIVE_IND_ID:
  1150. ret = wsm_receive_indication(priv, link_id,
  1151. &wsm_buf, skb_p);
  1152. break;
  1153. case 0x0805:
  1154. ret = wsm_event_indication(priv, &wsm_buf);
  1155. break;
  1156. case WSM_SCAN_COMPLETE_IND_ID:
  1157. ret = wsm_scan_complete_indication(priv, &wsm_buf);
  1158. break;
  1159. case 0x0808:
  1160. ret = wsm_ba_timeout_indication(priv, &wsm_buf);
  1161. break;
  1162. case 0x0809:
  1163. ret = wsm_set_pm_indication(priv, &wsm_buf);
  1164. break;
  1165. case 0x080A:
  1166. ret = wsm_channel_switch_indication(priv, &wsm_buf);
  1167. break;
  1168. case 0x080B:
  1169. ret = wsm_find_complete_indication(priv, &wsm_buf);
  1170. break;
  1171. case 0x080C:
  1172. ret = wsm_suspend_resume_indication(priv,
  1173. link_id, &wsm_buf);
  1174. break;
  1175. case 0x080F:
  1176. ret = wsm_join_complete_indication(priv, &wsm_buf);
  1177. break;
  1178. default:
  1179. pr_warn("Unrecognised WSM ID %04x\n", id);
  1180. }
  1181. } else {
  1182. WARN_ON(1);
  1183. ret = -EINVAL;
  1184. }
  1185. out:
  1186. return ret;
  1187. }
  1188. static bool wsm_handle_tx_data(struct cw1200_common *priv,
  1189. struct wsm_tx *wsm,
  1190. const struct ieee80211_tx_info *tx_info,
  1191. const struct cw1200_txpriv *txpriv,
  1192. struct cw1200_queue *queue)
  1193. {
  1194. bool handled = false;
  1195. const struct ieee80211_hdr *frame =
  1196. (struct ieee80211_hdr *)&((u8 *)wsm)[txpriv->offset];
  1197. __le16 fctl = frame->frame_control;
  1198. enum {
  1199. do_probe,
  1200. do_drop,
  1201. do_wep,
  1202. do_tx,
  1203. } action = do_tx;
  1204. switch (priv->mode) {
  1205. case NL80211_IFTYPE_STATION:
  1206. if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
  1207. action = do_tx;
  1208. else if (priv->join_status < CW1200_JOIN_STATUS_PRE_STA)
  1209. action = do_drop;
  1210. break;
  1211. case NL80211_IFTYPE_AP:
  1212. if (!priv->join_status) {
  1213. action = do_drop;
  1214. } else if (!(BIT(txpriv->raw_link_id) &
  1215. (BIT(0) | priv->link_id_map))) {
  1216. wiphy_warn(priv->hw->wiphy,
  1217. "A frame with expired link id is dropped.\n");
  1218. action = do_drop;
  1219. }
  1220. if (cw1200_queue_get_generation(wsm->packet_id) >
  1221. CW1200_MAX_REQUEUE_ATTEMPTS) {
  1222. /* HACK!!! WSM324 firmware has tendency to requeue
  1223. * multicast frames in a loop, causing performance
  1224. * drop and high power consumption of the driver.
  1225. * In this situation it is better just to drop
  1226. * the problematic frame.
  1227. */
  1228. wiphy_warn(priv->hw->wiphy,
  1229. "Too many attempts to requeue a frame; dropped.\n");
  1230. action = do_drop;
  1231. }
  1232. break;
  1233. case NL80211_IFTYPE_ADHOC:
  1234. if (priv->join_status != CW1200_JOIN_STATUS_IBSS)
  1235. action = do_drop;
  1236. break;
  1237. case NL80211_IFTYPE_MESH_POINT:
  1238. action = do_tx; /* TODO: Test me! */
  1239. break;
  1240. case NL80211_IFTYPE_MONITOR:
  1241. default:
  1242. action = do_drop;
  1243. break;
  1244. }
  1245. if (action == do_tx) {
  1246. if (ieee80211_is_nullfunc(fctl)) {
  1247. spin_lock(&priv->bss_loss_lock);
  1248. if (priv->bss_loss_state) {
  1249. priv->bss_loss_confirm_id = wsm->packet_id;
  1250. wsm->queue_id = WSM_QUEUE_VOICE;
  1251. }
  1252. spin_unlock(&priv->bss_loss_lock);
  1253. } else if (ieee80211_is_probe_req(fctl)) {
  1254. action = do_probe;
  1255. } else if (ieee80211_is_deauth(fctl) &&
  1256. priv->mode != NL80211_IFTYPE_AP) {
  1257. pr_debug("[WSM] Issue unjoin command due to tx deauth.\n");
  1258. wsm_lock_tx_async(priv);
  1259. if (queue_work(priv->workqueue,
  1260. &priv->unjoin_work) <= 0)
  1261. wsm_unlock_tx(priv);
  1262. } else if (ieee80211_has_protected(fctl) &&
  1263. tx_info->control.hw_key &&
  1264. tx_info->control.hw_key->keyidx != priv->wep_default_key_id &&
  1265. (tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  1266. tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_WEP104)) {
  1267. action = do_wep;
  1268. }
  1269. }
  1270. switch (action) {
  1271. case do_probe:
  1272. /* An interesting FW "feature". Device filters probe responses.
  1273. * The easiest way to get it back is to convert
  1274. * probe request into WSM start_scan command.
  1275. */
  1276. pr_debug("[WSM] Convert probe request to scan.\n");
  1277. wsm_lock_tx_async(priv);
  1278. priv->pending_frame_id = wsm->packet_id;
  1279. if (queue_delayed_work(priv->workqueue,
  1280. &priv->scan.probe_work, 0) <= 0)
  1281. wsm_unlock_tx(priv);
  1282. handled = true;
  1283. break;
  1284. case do_drop:
  1285. pr_debug("[WSM] Drop frame (0x%.4X).\n", fctl);
  1286. BUG_ON(cw1200_queue_remove(queue, wsm->packet_id));
  1287. handled = true;
  1288. break;
  1289. case do_wep:
  1290. pr_debug("[WSM] Issue set_default_wep_key.\n");
  1291. wsm_lock_tx_async(priv);
  1292. priv->wep_default_key_id = tx_info->control.hw_key->keyidx;
  1293. priv->pending_frame_id = wsm->packet_id;
  1294. if (queue_work(priv->workqueue, &priv->wep_key_work) <= 0)
  1295. wsm_unlock_tx(priv);
  1296. handled = true;
  1297. break;
  1298. case do_tx:
  1299. pr_debug("[WSM] Transmit frame.\n");
  1300. break;
  1301. default:
  1302. /* Do nothing */
  1303. break;
  1304. }
  1305. return handled;
  1306. }
  1307. static int cw1200_get_prio_queue(struct cw1200_common *priv,
  1308. u32 link_id_map, int *total)
  1309. {
  1310. static const int urgent = BIT(CW1200_LINK_ID_AFTER_DTIM) |
  1311. BIT(CW1200_LINK_ID_UAPSD);
  1312. struct wsm_edca_queue_params *edca;
  1313. unsigned score, best = -1;
  1314. int winner = -1;
  1315. int queued;
  1316. int i;
  1317. /* search for a winner using edca params */
  1318. for (i = 0; i < 4; ++i) {
  1319. queued = cw1200_queue_get_num_queued(&priv->tx_queue[i],
  1320. link_id_map);
  1321. if (!queued)
  1322. continue;
  1323. *total += queued;
  1324. edca = &priv->edca.params[i];
  1325. score = ((edca->aifns + edca->cwmin) << 16) +
  1326. ((edca->cwmax - edca->cwmin) *
  1327. (get_random_int() & 0xFFFF));
  1328. if (score < best && (winner < 0 || i != 3)) {
  1329. best = score;
  1330. winner = i;
  1331. }
  1332. }
  1333. /* override winner if bursting */
  1334. if (winner >= 0 && priv->tx_burst_idx >= 0 &&
  1335. winner != priv->tx_burst_idx &&
  1336. !cw1200_queue_get_num_queued(
  1337. &priv->tx_queue[winner],
  1338. link_id_map & urgent) &&
  1339. cw1200_queue_get_num_queued(
  1340. &priv->tx_queue[priv->tx_burst_idx],
  1341. link_id_map))
  1342. winner = priv->tx_burst_idx;
  1343. return winner;
  1344. }
  1345. static int wsm_get_tx_queue_and_mask(struct cw1200_common *priv,
  1346. struct cw1200_queue **queue_p,
  1347. u32 *tx_allowed_mask_p,
  1348. bool *more)
  1349. {
  1350. int idx;
  1351. u32 tx_allowed_mask;
  1352. int total = 0;
  1353. /* Search for a queue with multicast frames buffered */
  1354. if (priv->tx_multicast) {
  1355. tx_allowed_mask = BIT(CW1200_LINK_ID_AFTER_DTIM);
  1356. idx = cw1200_get_prio_queue(priv,
  1357. tx_allowed_mask, &total);
  1358. if (idx >= 0) {
  1359. *more = total > 1;
  1360. goto found;
  1361. }
  1362. }
  1363. /* Search for unicast traffic */
  1364. tx_allowed_mask = ~priv->sta_asleep_mask;
  1365. tx_allowed_mask |= BIT(CW1200_LINK_ID_UAPSD);
  1366. if (priv->sta_asleep_mask) {
  1367. tx_allowed_mask |= priv->pspoll_mask;
  1368. tx_allowed_mask &= ~BIT(CW1200_LINK_ID_AFTER_DTIM);
  1369. } else {
  1370. tx_allowed_mask |= BIT(CW1200_LINK_ID_AFTER_DTIM);
  1371. }
  1372. idx = cw1200_get_prio_queue(priv,
  1373. tx_allowed_mask, &total);
  1374. if (idx < 0)
  1375. return -ENOENT;
  1376. found:
  1377. *queue_p = &priv->tx_queue[idx];
  1378. *tx_allowed_mask_p = tx_allowed_mask;
  1379. return 0;
  1380. }
  1381. int wsm_get_tx(struct cw1200_common *priv, u8 **data,
  1382. size_t *tx_len, int *burst)
  1383. {
  1384. struct wsm_tx *wsm = NULL;
  1385. struct ieee80211_tx_info *tx_info;
  1386. struct cw1200_queue *queue = NULL;
  1387. int queue_num;
  1388. u32 tx_allowed_mask = 0;
  1389. const struct cw1200_txpriv *txpriv = NULL;
  1390. int count = 0;
  1391. /* More is used only for broadcasts. */
  1392. bool more = false;
  1393. if (priv->wsm_cmd.ptr) { /* CMD request */
  1394. ++count;
  1395. spin_lock(&priv->wsm_cmd.lock);
  1396. BUG_ON(!priv->wsm_cmd.ptr);
  1397. *data = priv->wsm_cmd.ptr;
  1398. *tx_len = priv->wsm_cmd.len;
  1399. *burst = 1;
  1400. spin_unlock(&priv->wsm_cmd.lock);
  1401. } else {
  1402. for (;;) {
  1403. int ret;
  1404. if (atomic_add_return(0, &priv->tx_lock))
  1405. break;
  1406. spin_lock_bh(&priv->ps_state_lock);
  1407. ret = wsm_get_tx_queue_and_mask(priv, &queue,
  1408. &tx_allowed_mask, &more);
  1409. queue_num = queue - priv->tx_queue;
  1410. if (priv->buffered_multicasts &&
  1411. (ret || !more) &&
  1412. (priv->tx_multicast || !priv->sta_asleep_mask)) {
  1413. priv->buffered_multicasts = false;
  1414. if (priv->tx_multicast) {
  1415. priv->tx_multicast = false;
  1416. queue_work(priv->workqueue,
  1417. &priv->multicast_stop_work);
  1418. }
  1419. }
  1420. spin_unlock_bh(&priv->ps_state_lock);
  1421. if (ret)
  1422. break;
  1423. if (cw1200_queue_get(queue,
  1424. tx_allowed_mask,
  1425. &wsm, &tx_info, &txpriv))
  1426. continue;
  1427. if (wsm_handle_tx_data(priv, wsm,
  1428. tx_info, txpriv, queue))
  1429. continue; /* Handled by WSM */
  1430. wsm->hdr.id &= __cpu_to_le16(
  1431. ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX));
  1432. wsm->hdr.id |= cpu_to_le16(
  1433. WSM_TX_LINK_ID(txpriv->raw_link_id));
  1434. priv->pspoll_mask &= ~BIT(txpriv->raw_link_id);
  1435. *data = (u8 *)wsm;
  1436. *tx_len = __le16_to_cpu(wsm->hdr.len);
  1437. /* allow bursting if txop is set */
  1438. if (priv->edca.params[queue_num].txop_limit)
  1439. *burst = min(*burst,
  1440. (int)cw1200_queue_get_num_queued(queue, tx_allowed_mask) + 1);
  1441. else
  1442. *burst = 1;
  1443. /* store index of bursting queue */
  1444. if (*burst > 1)
  1445. priv->tx_burst_idx = queue_num;
  1446. else
  1447. priv->tx_burst_idx = -1;
  1448. if (more) {
  1449. struct ieee80211_hdr *hdr =
  1450. (struct ieee80211_hdr *)
  1451. &((u8 *)wsm)[txpriv->offset];
  1452. /* more buffered multicast/broadcast frames
  1453. * ==> set MoreData flag in IEEE 802.11 header
  1454. * to inform PS STAs
  1455. */
  1456. hdr->frame_control |=
  1457. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1458. }
  1459. pr_debug("[WSM] >>> 0x%.4X (%zu) %p %c\n",
  1460. 0x0004, *tx_len, *data,
  1461. wsm->more ? 'M' : ' ');
  1462. ++count;
  1463. break;
  1464. }
  1465. }
  1466. return count;
  1467. }
  1468. void wsm_txed(struct cw1200_common *priv, u8 *data)
  1469. {
  1470. if (data == priv->wsm_cmd.ptr) {
  1471. spin_lock(&priv->wsm_cmd.lock);
  1472. priv->wsm_cmd.ptr = NULL;
  1473. spin_unlock(&priv->wsm_cmd.lock);
  1474. }
  1475. }
  1476. /* ******************************************************************** */
  1477. /* WSM buffer */
  1478. void wsm_buf_init(struct wsm_buf *buf)
  1479. {
  1480. BUG_ON(buf->begin);
  1481. buf->begin = kmalloc(FWLOAD_BLOCK_SIZE, GFP_KERNEL | GFP_DMA);
  1482. buf->end = buf->begin ? &buf->begin[FWLOAD_BLOCK_SIZE] : buf->begin;
  1483. wsm_buf_reset(buf);
  1484. }
  1485. void wsm_buf_deinit(struct wsm_buf *buf)
  1486. {
  1487. kfree(buf->begin);
  1488. buf->begin = buf->data = buf->end = NULL;
  1489. }
  1490. static void wsm_buf_reset(struct wsm_buf *buf)
  1491. {
  1492. if (buf->begin) {
  1493. buf->data = &buf->begin[4];
  1494. *(u32 *)buf->begin = 0;
  1495. } else {
  1496. buf->data = buf->begin;
  1497. }
  1498. }
  1499. static int wsm_buf_reserve(struct wsm_buf *buf, size_t extra_size)
  1500. {
  1501. size_t pos = buf->data - buf->begin;
  1502. size_t size = pos + extra_size;
  1503. size = round_up(size, FWLOAD_BLOCK_SIZE);
  1504. buf->begin = krealloc(buf->begin, size, GFP_KERNEL | GFP_DMA);
  1505. if (buf->begin) {
  1506. buf->data = &buf->begin[pos];
  1507. buf->end = &buf->begin[size];
  1508. return 0;
  1509. } else {
  1510. buf->end = buf->data = buf->begin;
  1511. return -ENOMEM;
  1512. }
  1513. }