cmd.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/ieee80211.h>
  28. #include <linux/slab.h>
  29. #include "wlcore.h"
  30. #include "debug.h"
  31. #include "io.h"
  32. #include "acx.h"
  33. #include "wl12xx_80211.h"
  34. #include "cmd.h"
  35. #include "event.h"
  36. #include "tx.h"
  37. #include "hw_ops.h"
  38. #define WL1271_CMD_FAST_POLL_COUNT 50
  39. #define WL1271_WAIT_EVENT_FAST_POLL_COUNT 20
  40. /*
  41. * send command to firmware
  42. *
  43. * @wl: wl struct
  44. * @id: command id
  45. * @buf: buffer containing the command, must work with dma
  46. * @len: length of the buffer
  47. * return the cmd status code on success.
  48. */
  49. static int __wlcore_cmd_send(struct wl1271 *wl, u16 id, void *buf,
  50. size_t len, size_t res_len)
  51. {
  52. struct wl1271_cmd_header *cmd;
  53. unsigned long timeout;
  54. u32 intr;
  55. int ret;
  56. u16 status;
  57. u16 poll_count = 0;
  58. if (WARN_ON(unlikely(wl->state == WLCORE_STATE_RESTARTING)))
  59. return -EIO;
  60. cmd = buf;
  61. cmd->id = cpu_to_le16(id);
  62. cmd->status = 0;
  63. WARN_ON(len % 4 != 0);
  64. WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
  65. ret = wlcore_write(wl, wl->cmd_box_addr, buf, len, false);
  66. if (ret < 0)
  67. return ret;
  68. /*
  69. * TODO: we just need this because one bit is in a different
  70. * place. Is there any better way?
  71. */
  72. ret = wl->ops->trigger_cmd(wl, wl->cmd_box_addr, buf, len);
  73. if (ret < 0)
  74. return ret;
  75. timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
  76. ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
  77. if (ret < 0)
  78. return ret;
  79. while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
  80. if (time_after(jiffies, timeout)) {
  81. wl1271_error("command complete timeout");
  82. return -ETIMEDOUT;
  83. }
  84. poll_count++;
  85. if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
  86. udelay(10);
  87. else
  88. msleep(1);
  89. ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
  90. if (ret < 0)
  91. return ret;
  92. }
  93. /* read back the status code of the command */
  94. if (res_len == 0)
  95. res_len = sizeof(struct wl1271_cmd_header);
  96. ret = wlcore_read(wl, wl->cmd_box_addr, cmd, res_len, false);
  97. if (ret < 0)
  98. return ret;
  99. status = le16_to_cpu(cmd->status);
  100. ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
  101. WL1271_ACX_INTR_CMD_COMPLETE);
  102. if (ret < 0)
  103. return ret;
  104. return status;
  105. }
  106. /*
  107. * send command to fw and return cmd status on success
  108. * valid_rets contains a bitmap of allowed error codes
  109. */
  110. int wlcore_cmd_send_failsafe(struct wl1271 *wl, u16 id, void *buf, size_t len,
  111. size_t res_len, unsigned long valid_rets)
  112. {
  113. int ret = __wlcore_cmd_send(wl, id, buf, len, res_len);
  114. if (ret < 0)
  115. goto fail;
  116. /* success is always a valid status */
  117. valid_rets |= BIT(CMD_STATUS_SUCCESS);
  118. if (ret >= MAX_COMMAND_STATUS ||
  119. !test_bit(ret, &valid_rets)) {
  120. wl1271_error("command execute failure %d", ret);
  121. ret = -EIO;
  122. goto fail;
  123. }
  124. return ret;
  125. fail:
  126. wl12xx_queue_recovery_work(wl);
  127. return ret;
  128. }
  129. EXPORT_SYMBOL_GPL(wl1271_cmd_send);
  130. /*
  131. * wrapper for wlcore_cmd_send that accept only CMD_STATUS_SUCCESS
  132. * return 0 on success.
  133. */
  134. int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
  135. size_t res_len)
  136. {
  137. int ret = wlcore_cmd_send_failsafe(wl, id, buf, len, res_len, 0);
  138. if (ret < 0)
  139. return ret;
  140. return 0;
  141. }
  142. /*
  143. * Poll the mailbox event field until any of the bits in the mask is set or a
  144. * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
  145. */
  146. int wlcore_cmd_wait_for_event_or_timeout(struct wl1271 *wl,
  147. u32 mask, bool *timeout)
  148. {
  149. u32 *events_vector;
  150. u32 event;
  151. unsigned long timeout_time;
  152. u16 poll_count = 0;
  153. int ret = 0;
  154. *timeout = false;
  155. events_vector = kmalloc(sizeof(*events_vector), GFP_KERNEL | GFP_DMA);
  156. if (!events_vector)
  157. return -ENOMEM;
  158. timeout_time = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
  159. do {
  160. if (time_after(jiffies, timeout_time)) {
  161. wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
  162. (int)mask);
  163. *timeout = true;
  164. goto out;
  165. }
  166. poll_count++;
  167. if (poll_count < WL1271_WAIT_EVENT_FAST_POLL_COUNT)
  168. usleep_range(50, 51);
  169. else
  170. usleep_range(1000, 5000);
  171. /* read from both event fields */
  172. ret = wlcore_read(wl, wl->mbox_ptr[0], events_vector,
  173. sizeof(*events_vector), false);
  174. if (ret < 0)
  175. goto out;
  176. event = *events_vector & mask;
  177. ret = wlcore_read(wl, wl->mbox_ptr[1], events_vector,
  178. sizeof(*events_vector), false);
  179. if (ret < 0)
  180. goto out;
  181. event |= *events_vector & mask;
  182. } while (!event);
  183. out:
  184. kfree(events_vector);
  185. return ret;
  186. }
  187. EXPORT_SYMBOL_GPL(wlcore_cmd_wait_for_event_or_timeout);
  188. int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
  189. u8 *role_id)
  190. {
  191. struct wl12xx_cmd_role_enable *cmd;
  192. int ret;
  193. wl1271_debug(DEBUG_CMD, "cmd role enable");
  194. if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
  195. return -EBUSY;
  196. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  197. if (!cmd) {
  198. ret = -ENOMEM;
  199. goto out;
  200. }
  201. /* get role id */
  202. cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
  203. if (cmd->role_id >= WL12XX_MAX_ROLES) {
  204. ret = -EBUSY;
  205. goto out_free;
  206. }
  207. memcpy(cmd->mac_address, addr, ETH_ALEN);
  208. cmd->role_type = role_type;
  209. ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
  210. if (ret < 0) {
  211. wl1271_error("failed to initiate cmd role enable");
  212. goto out_free;
  213. }
  214. __set_bit(cmd->role_id, wl->roles_map);
  215. *role_id = cmd->role_id;
  216. out_free:
  217. kfree(cmd);
  218. out:
  219. return ret;
  220. }
  221. int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
  222. {
  223. struct wl12xx_cmd_role_disable *cmd;
  224. int ret;
  225. wl1271_debug(DEBUG_CMD, "cmd role disable");
  226. if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
  227. return -ENOENT;
  228. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  229. if (!cmd) {
  230. ret = -ENOMEM;
  231. goto out;
  232. }
  233. cmd->role_id = *role_id;
  234. ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
  235. if (ret < 0) {
  236. wl1271_error("failed to initiate cmd role disable");
  237. goto out_free;
  238. }
  239. __clear_bit(*role_id, wl->roles_map);
  240. *role_id = WL12XX_INVALID_ROLE_ID;
  241. out_free:
  242. kfree(cmd);
  243. out:
  244. return ret;
  245. }
  246. static int wlcore_get_new_session_id(struct wl1271 *wl, u8 hlid)
  247. {
  248. if (wl->session_ids[hlid] >= SESSION_COUNTER_MAX)
  249. wl->session_ids[hlid] = 0;
  250. wl->session_ids[hlid]++;
  251. return wl->session_ids[hlid];
  252. }
  253. int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
  254. {
  255. unsigned long flags;
  256. u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
  257. if (link >= WL12XX_MAX_LINKS)
  258. return -EBUSY;
  259. wl->session_ids[link] = wlcore_get_new_session_id(wl, link);
  260. /* these bits are used by op_tx */
  261. spin_lock_irqsave(&wl->wl_lock, flags);
  262. __set_bit(link, wl->links_map);
  263. __set_bit(link, wlvif->links_map);
  264. spin_unlock_irqrestore(&wl->wl_lock, flags);
  265. /* take the last "freed packets" value from the current FW status */
  266. wl->links[link].prev_freed_pkts =
  267. wl->fw_status_2->counters.tx_lnk_free_pkts[link];
  268. wl->links[link].wlvif = wlvif;
  269. /*
  270. * Take saved value for total freed packets from wlvif, in case this is
  271. * recovery/resume
  272. */
  273. if (wlvif->bss_type != BSS_TYPE_AP_BSS)
  274. wl->links[link].total_freed_pkts = wlvif->total_freed_pkts;
  275. *hlid = link;
  276. wl->active_link_count++;
  277. return 0;
  278. }
  279. void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
  280. {
  281. unsigned long flags;
  282. if (*hlid == WL12XX_INVALID_LINK_ID)
  283. return;
  284. /* these bits are used by op_tx */
  285. spin_lock_irqsave(&wl->wl_lock, flags);
  286. __clear_bit(*hlid, wl->links_map);
  287. __clear_bit(*hlid, wlvif->links_map);
  288. spin_unlock_irqrestore(&wl->wl_lock, flags);
  289. wl->links[*hlid].allocated_pkts = 0;
  290. wl->links[*hlid].prev_freed_pkts = 0;
  291. wl->links[*hlid].ba_bitmap = 0;
  292. memset(wl->links[*hlid].addr, 0, ETH_ALEN);
  293. /*
  294. * At this point op_tx() will not add more packets to the queues. We
  295. * can purge them.
  296. */
  297. wl1271_tx_reset_link_queues(wl, *hlid);
  298. wl->links[*hlid].wlvif = NULL;
  299. if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
  300. (wlvif->bss_type == BSS_TYPE_AP_BSS &&
  301. *hlid == wlvif->ap.bcast_hlid)) {
  302. /*
  303. * save the total freed packets in the wlvif, in case this is
  304. * recovery or suspend
  305. */
  306. wlvif->total_freed_pkts = wl->links[*hlid].total_freed_pkts;
  307. /*
  308. * increment the initial seq number on recovery to account for
  309. * transmitted packets that we haven't yet got in the FW status
  310. */
  311. if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
  312. wlvif->total_freed_pkts +=
  313. WL1271_TX_SQN_POST_RECOVERY_PADDING;
  314. }
  315. wl->links[*hlid].total_freed_pkts = 0;
  316. *hlid = WL12XX_INVALID_LINK_ID;
  317. wl->active_link_count--;
  318. WARN_ON_ONCE(wl->active_link_count < 0);
  319. }
  320. static u8 wlcore_get_native_channel_type(u8 nl_channel_type)
  321. {
  322. switch (nl_channel_type) {
  323. case NL80211_CHAN_NO_HT:
  324. return WLCORE_CHAN_NO_HT;
  325. case NL80211_CHAN_HT20:
  326. return WLCORE_CHAN_HT20;
  327. case NL80211_CHAN_HT40MINUS:
  328. return WLCORE_CHAN_HT40MINUS;
  329. case NL80211_CHAN_HT40PLUS:
  330. return WLCORE_CHAN_HT40PLUS;
  331. default:
  332. WARN_ON(1);
  333. return WLCORE_CHAN_NO_HT;
  334. }
  335. }
  336. static int wl12xx_cmd_role_start_dev(struct wl1271 *wl,
  337. struct wl12xx_vif *wlvif,
  338. enum ieee80211_band band,
  339. int channel)
  340. {
  341. struct wl12xx_cmd_role_start *cmd;
  342. int ret;
  343. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  344. if (!cmd) {
  345. ret = -ENOMEM;
  346. goto out;
  347. }
  348. wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
  349. cmd->role_id = wlvif->dev_role_id;
  350. if (band == IEEE80211_BAND_5GHZ)
  351. cmd->band = WLCORE_BAND_5GHZ;
  352. cmd->channel = channel;
  353. if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
  354. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
  355. if (ret)
  356. goto out_free;
  357. }
  358. cmd->device.hlid = wlvif->dev_hlid;
  359. cmd->device.session = wl->session_ids[wlvif->dev_hlid];
  360. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
  361. cmd->role_id, cmd->device.hlid, cmd->device.session);
  362. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  363. if (ret < 0) {
  364. wl1271_error("failed to initiate cmd role enable");
  365. goto err_hlid;
  366. }
  367. goto out_free;
  368. err_hlid:
  369. /* clear links on error */
  370. wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
  371. out_free:
  372. kfree(cmd);
  373. out:
  374. return ret;
  375. }
  376. static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl,
  377. struct wl12xx_vif *wlvif)
  378. {
  379. struct wl12xx_cmd_role_stop *cmd;
  380. int ret;
  381. if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
  382. return -EINVAL;
  383. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  384. if (!cmd) {
  385. ret = -ENOMEM;
  386. goto out;
  387. }
  388. wl1271_debug(DEBUG_CMD, "cmd role stop dev");
  389. cmd->role_id = wlvif->dev_role_id;
  390. cmd->disc_type = DISCONNECT_IMMEDIATE;
  391. cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
  392. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  393. if (ret < 0) {
  394. wl1271_error("failed to initiate cmd role stop");
  395. goto out_free;
  396. }
  397. wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
  398. out_free:
  399. kfree(cmd);
  400. out:
  401. return ret;
  402. }
  403. int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  404. {
  405. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  406. struct wl12xx_cmd_role_start *cmd;
  407. u32 supported_rates;
  408. int ret;
  409. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  410. if (!cmd) {
  411. ret = -ENOMEM;
  412. goto out;
  413. }
  414. wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
  415. cmd->role_id = wlvif->role_id;
  416. if (wlvif->band == IEEE80211_BAND_5GHZ)
  417. cmd->band = WLCORE_BAND_5GHZ;
  418. cmd->channel = wlvif->channel;
  419. cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  420. cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  421. cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
  422. cmd->sta.ssid_len = wlvif->ssid_len;
  423. memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
  424. memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
  425. supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
  426. wlcore_hw_sta_get_ap_rate_mask(wl, wlvif);
  427. if (wlvif->p2p)
  428. supported_rates &= ~CONF_TX_CCK_RATES;
  429. cmd->sta.local_rates = cpu_to_le32(supported_rates);
  430. cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
  431. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
  432. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
  433. if (ret)
  434. goto out_free;
  435. }
  436. cmd->sta.hlid = wlvif->sta.hlid;
  437. cmd->sta.session = wl->session_ids[wlvif->sta.hlid];
  438. /*
  439. * We don't have the correct remote rates in this stage. The
  440. * rates will be reconfigured later, after association, if the
  441. * firmware supports ACX_PEER_CAP. Otherwise, there's nothing
  442. * we can do, so use all supported_rates here.
  443. */
  444. cmd->sta.remote_rates = cpu_to_le32(supported_rates);
  445. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
  446. "basic_rate_set: 0x%x, remote_rates: 0x%x",
  447. wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
  448. wlvif->basic_rate_set, wlvif->rate_set);
  449. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  450. if (ret < 0) {
  451. wl1271_error("failed to initiate cmd role start sta");
  452. goto err_hlid;
  453. }
  454. wlvif->sta.role_chan_type = wlvif->channel_type;
  455. goto out_free;
  456. err_hlid:
  457. /* clear links on error. */
  458. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  459. out_free:
  460. kfree(cmd);
  461. out:
  462. return ret;
  463. }
  464. /* use this function to stop ibss as well */
  465. int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  466. {
  467. struct wl12xx_cmd_role_stop *cmd;
  468. int ret;
  469. if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
  470. return -EINVAL;
  471. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  472. if (!cmd) {
  473. ret = -ENOMEM;
  474. goto out;
  475. }
  476. wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
  477. cmd->role_id = wlvif->role_id;
  478. cmd->disc_type = DISCONNECT_IMMEDIATE;
  479. cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
  480. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  481. if (ret < 0) {
  482. wl1271_error("failed to initiate cmd role stop sta");
  483. goto out_free;
  484. }
  485. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  486. out_free:
  487. kfree(cmd);
  488. out:
  489. return ret;
  490. }
  491. int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  492. {
  493. struct wl12xx_cmd_role_start *cmd;
  494. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  495. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  496. u32 supported_rates;
  497. int ret;
  498. wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
  499. /* trying to use hidden SSID with an old hostapd version */
  500. if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
  501. wl1271_error("got a null SSID from beacon/bss");
  502. ret = -EINVAL;
  503. goto out;
  504. }
  505. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  506. if (!cmd) {
  507. ret = -ENOMEM;
  508. goto out;
  509. }
  510. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
  511. if (ret < 0)
  512. goto out_free;
  513. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  514. if (ret < 0)
  515. goto out_free_global;
  516. /* use the previous security seq, if this is a recovery/resume */
  517. wl->links[wlvif->ap.bcast_hlid].total_freed_pkts =
  518. wlvif->total_freed_pkts;
  519. cmd->role_id = wlvif->role_id;
  520. cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
  521. cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
  522. cmd->ap.global_hlid = wlvif->ap.global_hlid;
  523. cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
  524. cmd->ap.global_session_id = wl->session_ids[wlvif->ap.global_hlid];
  525. cmd->ap.bcast_session_id = wl->session_ids[wlvif->ap.bcast_hlid];
  526. cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  527. cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  528. cmd->ap.dtim_interval = bss_conf->dtim_period;
  529. cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
  530. /* FIXME: Change when adding DFS */
  531. cmd->ap.reset_tsf = 1; /* By default reset AP TSF */
  532. cmd->ap.wmm = wlvif->wmm_enabled;
  533. cmd->channel = wlvif->channel;
  534. cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
  535. if (!bss_conf->hidden_ssid) {
  536. /* take the SSID from the beacon for backward compatibility */
  537. cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
  538. cmd->ap.ssid_len = wlvif->ssid_len;
  539. memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
  540. } else {
  541. cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
  542. cmd->ap.ssid_len = bss_conf->ssid_len;
  543. memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
  544. }
  545. supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
  546. wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif);
  547. if (wlvif->p2p)
  548. supported_rates &= ~CONF_TX_CCK_RATES;
  549. wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x",
  550. supported_rates);
  551. cmd->ap.local_rates = cpu_to_le32(supported_rates);
  552. switch (wlvif->band) {
  553. case IEEE80211_BAND_2GHZ:
  554. cmd->band = WLCORE_BAND_2_4GHZ;
  555. break;
  556. case IEEE80211_BAND_5GHZ:
  557. cmd->band = WLCORE_BAND_5GHZ;
  558. break;
  559. default:
  560. wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
  561. cmd->band = WLCORE_BAND_2_4GHZ;
  562. break;
  563. }
  564. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  565. if (ret < 0) {
  566. wl1271_error("failed to initiate cmd role start ap");
  567. goto out_free_bcast;
  568. }
  569. goto out_free;
  570. out_free_bcast:
  571. wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  572. out_free_global:
  573. wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
  574. out_free:
  575. kfree(cmd);
  576. out:
  577. return ret;
  578. }
  579. int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  580. {
  581. struct wl12xx_cmd_role_stop *cmd;
  582. int ret;
  583. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  584. if (!cmd) {
  585. ret = -ENOMEM;
  586. goto out;
  587. }
  588. wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
  589. cmd->role_id = wlvif->role_id;
  590. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  591. if (ret < 0) {
  592. wl1271_error("failed to initiate cmd role stop ap");
  593. goto out_free;
  594. }
  595. wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  596. wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
  597. out_free:
  598. kfree(cmd);
  599. out:
  600. return ret;
  601. }
  602. int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  603. {
  604. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  605. struct wl12xx_cmd_role_start *cmd;
  606. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  607. int ret;
  608. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  609. if (!cmd) {
  610. ret = -ENOMEM;
  611. goto out;
  612. }
  613. wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
  614. cmd->role_id = wlvif->role_id;
  615. if (wlvif->band == IEEE80211_BAND_5GHZ)
  616. cmd->band = WLCORE_BAND_5GHZ;
  617. cmd->channel = wlvif->channel;
  618. cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  619. cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  620. cmd->ibss.dtim_interval = bss_conf->dtim_period;
  621. cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
  622. cmd->ibss.ssid_len = wlvif->ssid_len;
  623. memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
  624. memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
  625. cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
  626. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
  627. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
  628. if (ret)
  629. goto out_free;
  630. }
  631. cmd->ibss.hlid = wlvif->sta.hlid;
  632. cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
  633. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
  634. "basic_rate_set: 0x%x, remote_rates: 0x%x",
  635. wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
  636. wlvif->basic_rate_set, wlvif->rate_set);
  637. wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
  638. vif->bss_conf.bssid);
  639. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  640. if (ret < 0) {
  641. wl1271_error("failed to initiate cmd role enable");
  642. goto err_hlid;
  643. }
  644. goto out_free;
  645. err_hlid:
  646. /* clear links on error. */
  647. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  648. out_free:
  649. kfree(cmd);
  650. out:
  651. return ret;
  652. }
  653. /**
  654. * send test command to firmware
  655. *
  656. * @wl: wl struct
  657. * @buf: buffer containing the command, with all headers, must work with dma
  658. * @len: length of the buffer
  659. * @answer: is answer needed
  660. */
  661. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  662. {
  663. int ret;
  664. size_t res_len = 0;
  665. wl1271_debug(DEBUG_CMD, "cmd test");
  666. if (answer)
  667. res_len = buf_len;
  668. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
  669. if (ret < 0) {
  670. wl1271_warning("TEST command failed");
  671. return ret;
  672. }
  673. return ret;
  674. }
  675. EXPORT_SYMBOL_GPL(wl1271_cmd_test);
  676. /**
  677. * read acx from firmware
  678. *
  679. * @wl: wl struct
  680. * @id: acx id
  681. * @buf: buffer for the response, including all headers, must work with dma
  682. * @len: length of buf
  683. */
  684. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  685. {
  686. struct acx_header *acx = buf;
  687. int ret;
  688. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  689. acx->id = cpu_to_le16(id);
  690. /* payload length, does not include any headers */
  691. acx->len = cpu_to_le16(len - sizeof(*acx));
  692. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
  693. if (ret < 0)
  694. wl1271_error("INTERROGATE command failed");
  695. return ret;
  696. }
  697. /**
  698. * write acx value to firmware
  699. *
  700. * @wl: wl struct
  701. * @id: acx id
  702. * @buf: buffer containing acx, including all headers, must work with dma
  703. * @len: length of buf
  704. * @valid_rets: bitmap of valid cmd status codes (i.e. return values).
  705. * return the cmd status on success.
  706. */
  707. int wlcore_cmd_configure_failsafe(struct wl1271 *wl, u16 id, void *buf,
  708. size_t len, unsigned long valid_rets)
  709. {
  710. struct acx_header *acx = buf;
  711. int ret;
  712. wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
  713. acx->id = cpu_to_le16(id);
  714. /* payload length, does not include any headers */
  715. acx->len = cpu_to_le16(len - sizeof(*acx));
  716. ret = wlcore_cmd_send_failsafe(wl, CMD_CONFIGURE, acx, len, 0,
  717. valid_rets);
  718. if (ret < 0) {
  719. wl1271_warning("CONFIGURE command NOK");
  720. return ret;
  721. }
  722. return ret;
  723. }
  724. /*
  725. * wrapper for wlcore_cmd_configure that accepts only success status.
  726. * return 0 on success
  727. */
  728. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  729. {
  730. int ret = wlcore_cmd_configure_failsafe(wl, id, buf, len, 0);
  731. if (ret < 0)
  732. return ret;
  733. return 0;
  734. }
  735. EXPORT_SYMBOL_GPL(wl1271_cmd_configure);
  736. int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
  737. {
  738. struct cmd_enabledisable_path *cmd;
  739. int ret;
  740. u16 cmd_rx, cmd_tx;
  741. wl1271_debug(DEBUG_CMD, "cmd data path");
  742. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  743. if (!cmd) {
  744. ret = -ENOMEM;
  745. goto out;
  746. }
  747. /* the channel here is only used for calibration, so hardcoded to 1 */
  748. cmd->channel = 1;
  749. if (enable) {
  750. cmd_rx = CMD_ENABLE_RX;
  751. cmd_tx = CMD_ENABLE_TX;
  752. } else {
  753. cmd_rx = CMD_DISABLE_RX;
  754. cmd_tx = CMD_DISABLE_TX;
  755. }
  756. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
  757. if (ret < 0) {
  758. wl1271_error("rx %s cmd for channel %d failed",
  759. enable ? "start" : "stop", cmd->channel);
  760. goto out;
  761. }
  762. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  763. enable ? "start" : "stop", cmd->channel);
  764. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
  765. if (ret < 0) {
  766. wl1271_error("tx %s cmd for channel %d failed",
  767. enable ? "start" : "stop", cmd->channel);
  768. goto out;
  769. }
  770. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  771. enable ? "start" : "stop", cmd->channel);
  772. out:
  773. kfree(cmd);
  774. return ret;
  775. }
  776. EXPORT_SYMBOL_GPL(wl1271_cmd_data_path);
  777. int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  778. u8 ps_mode, u16 auto_ps_timeout)
  779. {
  780. struct wl1271_cmd_ps_params *ps_params = NULL;
  781. int ret = 0;
  782. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  783. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  784. if (!ps_params) {
  785. ret = -ENOMEM;
  786. goto out;
  787. }
  788. ps_params->role_id = wlvif->role_id;
  789. ps_params->ps_mode = ps_mode;
  790. ps_params->auto_ps_timeout = auto_ps_timeout;
  791. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  792. sizeof(*ps_params), 0);
  793. if (ret < 0) {
  794. wl1271_error("cmd set_ps_mode failed");
  795. goto out;
  796. }
  797. out:
  798. kfree(ps_params);
  799. return ret;
  800. }
  801. int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
  802. u16 template_id, void *buf, size_t buf_len,
  803. int index, u32 rates)
  804. {
  805. struct wl1271_cmd_template_set *cmd;
  806. int ret = 0;
  807. wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
  808. template_id, role_id);
  809. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  810. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  811. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  812. if (!cmd) {
  813. ret = -ENOMEM;
  814. goto out;
  815. }
  816. /* during initialization wlvif is NULL */
  817. cmd->role_id = role_id;
  818. cmd->len = cpu_to_le16(buf_len);
  819. cmd->template_type = template_id;
  820. cmd->enabled_rates = cpu_to_le32(rates);
  821. cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
  822. cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
  823. cmd->index = index;
  824. if (buf)
  825. memcpy(cmd->template_data, buf, buf_len);
  826. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  827. if (ret < 0) {
  828. wl1271_warning("cmd set_template failed: %d", ret);
  829. goto out_free;
  830. }
  831. out_free:
  832. kfree(cmd);
  833. out:
  834. return ret;
  835. }
  836. int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  837. {
  838. struct sk_buff *skb = NULL;
  839. int size;
  840. void *ptr;
  841. int ret = -ENOMEM;
  842. if (wlvif->bss_type == BSS_TYPE_IBSS) {
  843. size = sizeof(struct wl12xx_null_data_template);
  844. ptr = NULL;
  845. } else {
  846. skb = ieee80211_nullfunc_get(wl->hw,
  847. wl12xx_wlvif_to_vif(wlvif));
  848. if (!skb)
  849. goto out;
  850. size = skb->len;
  851. ptr = skb->data;
  852. }
  853. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  854. CMD_TEMPL_NULL_DATA, ptr, size, 0,
  855. wlvif->basic_rate);
  856. out:
  857. dev_kfree_skb(skb);
  858. if (ret)
  859. wl1271_warning("cmd buld null data failed %d", ret);
  860. return ret;
  861. }
  862. int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
  863. struct wl12xx_vif *wlvif)
  864. {
  865. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  866. struct sk_buff *skb = NULL;
  867. int ret = -ENOMEM;
  868. skb = ieee80211_nullfunc_get(wl->hw, vif);
  869. if (!skb)
  870. goto out;
  871. ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
  872. skb->data, skb->len,
  873. wlvif->sta.klv_template_id,
  874. wlvif->basic_rate);
  875. out:
  876. dev_kfree_skb(skb);
  877. if (ret)
  878. wl1271_warning("cmd build klv null data failed %d", ret);
  879. return ret;
  880. }
  881. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  882. u16 aid)
  883. {
  884. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  885. struct sk_buff *skb;
  886. int ret = 0;
  887. skb = ieee80211_pspoll_get(wl->hw, vif);
  888. if (!skb)
  889. goto out;
  890. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  891. CMD_TEMPL_PS_POLL, skb->data,
  892. skb->len, 0, wlvif->basic_rate_set);
  893. out:
  894. dev_kfree_skb(skb);
  895. return ret;
  896. }
  897. int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  898. u8 role_id, u8 band,
  899. const u8 *ssid, size_t ssid_len,
  900. const u8 *ie, size_t ie_len, bool sched_scan)
  901. {
  902. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  903. struct sk_buff *skb;
  904. int ret;
  905. u32 rate;
  906. u16 template_id_2_4 = wl->scan_templ_id_2_4;
  907. u16 template_id_5 = wl->scan_templ_id_5;
  908. skb = ieee80211_probereq_get(wl->hw, vif, ssid, ssid_len,
  909. ie_len);
  910. if (!skb) {
  911. ret = -ENOMEM;
  912. goto out;
  913. }
  914. if (ie_len)
  915. memcpy(skb_put(skb, ie_len), ie, ie_len);
  916. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
  917. if (sched_scan &&
  918. (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) {
  919. template_id_2_4 = wl->sched_scan_templ_id_2_4;
  920. template_id_5 = wl->sched_scan_templ_id_5;
  921. }
  922. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  923. if (band == IEEE80211_BAND_2GHZ)
  924. ret = wl1271_cmd_template_set(wl, role_id,
  925. template_id_2_4,
  926. skb->data, skb->len, 0, rate);
  927. else
  928. ret = wl1271_cmd_template_set(wl, role_id,
  929. template_id_5,
  930. skb->data, skb->len, 0, rate);
  931. out:
  932. dev_kfree_skb(skb);
  933. return ret;
  934. }
  935. EXPORT_SYMBOL_GPL(wl12xx_cmd_build_probe_req);
  936. struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
  937. struct wl12xx_vif *wlvif,
  938. struct sk_buff *skb)
  939. {
  940. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  941. int ret;
  942. u32 rate;
  943. if (!skb)
  944. skb = ieee80211_ap_probereq_get(wl->hw, vif);
  945. if (!skb)
  946. goto out;
  947. wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
  948. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
  949. if (wlvif->band == IEEE80211_BAND_2GHZ)
  950. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  951. CMD_TEMPL_CFG_PROBE_REQ_2_4,
  952. skb->data, skb->len, 0, rate);
  953. else
  954. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  955. CMD_TEMPL_CFG_PROBE_REQ_5,
  956. skb->data, skb->len, 0, rate);
  957. if (ret < 0)
  958. wl1271_error("Unable to set ap probe request template.");
  959. out:
  960. return skb;
  961. }
  962. int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  963. {
  964. int ret, extra = 0;
  965. u16 fc;
  966. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  967. struct sk_buff *skb;
  968. struct wl12xx_arp_rsp_template *tmpl;
  969. struct ieee80211_hdr_3addr *hdr;
  970. struct arphdr *arp_hdr;
  971. skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
  972. WL1271_EXTRA_SPACE_MAX);
  973. if (!skb) {
  974. wl1271_error("failed to allocate buffer for arp rsp template");
  975. return -ENOMEM;
  976. }
  977. skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
  978. tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl));
  979. memset(tmpl, 0, sizeof(*tmpl));
  980. /* llc layer */
  981. memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
  982. tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
  983. /* arp header */
  984. arp_hdr = &tmpl->arp_hdr;
  985. arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
  986. arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
  987. arp_hdr->ar_hln = ETH_ALEN;
  988. arp_hdr->ar_pln = 4;
  989. arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
  990. /* arp payload */
  991. memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
  992. tmpl->sender_ip = wlvif->ip_addr;
  993. /* encryption space */
  994. switch (wlvif->encryption_type) {
  995. case KEY_TKIP:
  996. if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
  997. extra = WL1271_EXTRA_SPACE_TKIP;
  998. break;
  999. case KEY_AES:
  1000. extra = WL1271_EXTRA_SPACE_AES;
  1001. break;
  1002. case KEY_NONE:
  1003. case KEY_WEP:
  1004. case KEY_GEM:
  1005. extra = 0;
  1006. break;
  1007. default:
  1008. wl1271_warning("Unknown encryption type: %d",
  1009. wlvif->encryption_type);
  1010. ret = -EINVAL;
  1011. goto out;
  1012. }
  1013. if (extra) {
  1014. u8 *space = skb_push(skb, extra);
  1015. memset(space, 0, extra);
  1016. }
  1017. /* QoS header - BE */
  1018. if (wlvif->sta.qos)
  1019. memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
  1020. /* mac80211 header */
  1021. hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr));
  1022. memset(hdr, 0, sizeof(*hdr));
  1023. fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
  1024. if (wlvif->sta.qos)
  1025. fc |= IEEE80211_STYPE_QOS_DATA;
  1026. else
  1027. fc |= IEEE80211_STYPE_DATA;
  1028. if (wlvif->encryption_type != KEY_NONE)
  1029. fc |= IEEE80211_FCTL_PROTECTED;
  1030. hdr->frame_control = cpu_to_le16(fc);
  1031. memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
  1032. memcpy(hdr->addr2, vif->addr, ETH_ALEN);
  1033. memset(hdr->addr3, 0xff, ETH_ALEN);
  1034. ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
  1035. skb->data, skb->len, 0,
  1036. wlvif->basic_rate);
  1037. out:
  1038. dev_kfree_skb(skb);
  1039. return ret;
  1040. }
  1041. int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
  1042. {
  1043. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  1044. struct ieee80211_qos_hdr template;
  1045. memset(&template, 0, sizeof(template));
  1046. memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
  1047. memcpy(template.addr2, vif->addr, ETH_ALEN);
  1048. memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
  1049. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1050. IEEE80211_STYPE_QOS_NULLFUNC |
  1051. IEEE80211_FCTL_TODS);
  1052. /* FIXME: not sure what priority to use here */
  1053. template.qos_ctrl = cpu_to_le16(0);
  1054. return wl1271_cmd_template_set(wl, wlvif->role_id,
  1055. CMD_TEMPL_QOS_NULL_DATA, &template,
  1056. sizeof(template), 0,
  1057. wlvif->basic_rate);
  1058. }
  1059. int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
  1060. {
  1061. struct wl1271_cmd_set_keys *cmd;
  1062. int ret = 0;
  1063. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  1064. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1065. if (!cmd) {
  1066. ret = -ENOMEM;
  1067. goto out;
  1068. }
  1069. cmd->hlid = hlid;
  1070. cmd->key_id = id;
  1071. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  1072. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  1073. cmd->key_type = KEY_WEP;
  1074. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1075. if (ret < 0) {
  1076. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  1077. goto out;
  1078. }
  1079. out:
  1080. kfree(cmd);
  1081. return ret;
  1082. }
  1083. int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1084. u16 action, u8 id, u8 key_type,
  1085. u8 key_size, const u8 *key, const u8 *addr,
  1086. u32 tx_seq_32, u16 tx_seq_16)
  1087. {
  1088. struct wl1271_cmd_set_keys *cmd;
  1089. int ret = 0;
  1090. /* hlid might have already been deleted */
  1091. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
  1092. return 0;
  1093. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1094. if (!cmd) {
  1095. ret = -ENOMEM;
  1096. goto out;
  1097. }
  1098. cmd->hlid = wlvif->sta.hlid;
  1099. if (key_type == KEY_WEP)
  1100. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  1101. else if (is_broadcast_ether_addr(addr))
  1102. cmd->lid_key_type = BROADCAST_LID_TYPE;
  1103. else
  1104. cmd->lid_key_type = UNICAST_LID_TYPE;
  1105. cmd->key_action = cpu_to_le16(action);
  1106. cmd->key_size = key_size;
  1107. cmd->key_type = key_type;
  1108. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1109. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1110. cmd->key_id = id;
  1111. if (key_type == KEY_TKIP) {
  1112. /*
  1113. * We get the key in the following form:
  1114. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1115. * but the target is expecting:
  1116. * TKIP - RX MIC - TX MIC
  1117. */
  1118. memcpy(cmd->key, key, 16);
  1119. memcpy(cmd->key + 16, key + 24, 8);
  1120. memcpy(cmd->key + 24, key + 16, 8);
  1121. } else {
  1122. memcpy(cmd->key, key, key_size);
  1123. }
  1124. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  1125. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1126. if (ret < 0) {
  1127. wl1271_warning("could not set keys");
  1128. goto out;
  1129. }
  1130. out:
  1131. kfree(cmd);
  1132. return ret;
  1133. }
  1134. /*
  1135. * TODO: merge with sta/ibss into 1 set_key function.
  1136. * note there are slight diffs
  1137. */
  1138. int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1139. u16 action, u8 id, u8 key_type,
  1140. u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
  1141. u16 tx_seq_16)
  1142. {
  1143. struct wl1271_cmd_set_keys *cmd;
  1144. int ret = 0;
  1145. u8 lid_type;
  1146. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1147. if (!cmd)
  1148. return -ENOMEM;
  1149. if (hlid == wlvif->ap.bcast_hlid) {
  1150. if (key_type == KEY_WEP)
  1151. lid_type = WEP_DEFAULT_LID_TYPE;
  1152. else
  1153. lid_type = BROADCAST_LID_TYPE;
  1154. } else {
  1155. lid_type = UNICAST_LID_TYPE;
  1156. }
  1157. wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
  1158. " hlid: %d", (int)action, (int)id, (int)lid_type,
  1159. (int)key_type, (int)hlid);
  1160. cmd->lid_key_type = lid_type;
  1161. cmd->hlid = hlid;
  1162. cmd->key_action = cpu_to_le16(action);
  1163. cmd->key_size = key_size;
  1164. cmd->key_type = key_type;
  1165. cmd->key_id = id;
  1166. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1167. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1168. if (key_type == KEY_TKIP) {
  1169. /*
  1170. * We get the key in the following form:
  1171. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1172. * but the target is expecting:
  1173. * TKIP - RX MIC - TX MIC
  1174. */
  1175. memcpy(cmd->key, key, 16);
  1176. memcpy(cmd->key + 16, key + 24, 8);
  1177. memcpy(cmd->key + 24, key + 16, 8);
  1178. } else {
  1179. memcpy(cmd->key, key, key_size);
  1180. }
  1181. wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
  1182. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1183. if (ret < 0) {
  1184. wl1271_warning("could not set ap keys");
  1185. goto out;
  1186. }
  1187. out:
  1188. kfree(cmd);
  1189. return ret;
  1190. }
  1191. int wl12xx_cmd_set_peer_state(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1192. u8 hlid)
  1193. {
  1194. struct wl12xx_cmd_set_peer_state *cmd;
  1195. int ret = 0;
  1196. wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
  1197. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1198. if (!cmd) {
  1199. ret = -ENOMEM;
  1200. goto out;
  1201. }
  1202. cmd->hlid = hlid;
  1203. cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
  1204. /* wmm param is valid only for station role */
  1205. if (wlvif->bss_type == BSS_TYPE_STA_BSS)
  1206. cmd->wmm = wlvif->wmm_enabled;
  1207. ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
  1208. if (ret < 0) {
  1209. wl1271_error("failed to send set peer state command");
  1210. goto out_free;
  1211. }
  1212. out_free:
  1213. kfree(cmd);
  1214. out:
  1215. return ret;
  1216. }
  1217. int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1218. struct ieee80211_sta *sta, u8 hlid)
  1219. {
  1220. struct wl12xx_cmd_add_peer *cmd;
  1221. int i, ret;
  1222. u32 sta_rates;
  1223. wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
  1224. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1225. if (!cmd) {
  1226. ret = -ENOMEM;
  1227. goto out;
  1228. }
  1229. memcpy(cmd->addr, sta->addr, ETH_ALEN);
  1230. cmd->bss_index = WL1271_AP_BSS_INDEX;
  1231. cmd->aid = sta->aid;
  1232. cmd->hlid = hlid;
  1233. cmd->sp_len = sta->max_sp;
  1234. cmd->wmm = sta->wme ? 1 : 0;
  1235. cmd->session_id = wl->session_ids[hlid];
  1236. for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
  1237. if (sta->wme && (sta->uapsd_queues & BIT(i)))
  1238. cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
  1239. WL1271_PSD_UPSD_TRIGGER;
  1240. else
  1241. cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
  1242. WL1271_PSD_LEGACY;
  1243. sta_rates = sta->supp_rates[wlvif->band];
  1244. if (sta->ht_cap.ht_supported)
  1245. sta_rates |=
  1246. (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
  1247. (sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
  1248. cmd->supported_rates =
  1249. cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
  1250. wlvif->band));
  1251. wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
  1252. cmd->supported_rates, sta->uapsd_queues);
  1253. ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
  1254. if (ret < 0) {
  1255. wl1271_error("failed to initiate cmd add peer");
  1256. goto out_free;
  1257. }
  1258. out_free:
  1259. kfree(cmd);
  1260. out:
  1261. return ret;
  1262. }
  1263. int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
  1264. {
  1265. struct wl12xx_cmd_remove_peer *cmd;
  1266. int ret;
  1267. bool timeout = false;
  1268. wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
  1269. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1270. if (!cmd) {
  1271. ret = -ENOMEM;
  1272. goto out;
  1273. }
  1274. cmd->hlid = hlid;
  1275. /* We never send a deauth, mac80211 is in charge of this */
  1276. cmd->reason_opcode = 0;
  1277. cmd->send_deauth_flag = 0;
  1278. ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
  1279. if (ret < 0) {
  1280. wl1271_error("failed to initiate cmd remove peer");
  1281. goto out_free;
  1282. }
  1283. ret = wl->ops->wait_for_event(wl,
  1284. WLCORE_EVENT_PEER_REMOVE_COMPLETE,
  1285. &timeout);
  1286. /*
  1287. * We are ok with a timeout here. The event is sometimes not sent
  1288. * due to a firmware bug. In case of another error (like SDIO timeout)
  1289. * queue a recovery.
  1290. */
  1291. if (ret)
  1292. wl12xx_queue_recovery_work(wl);
  1293. out_free:
  1294. kfree(cmd);
  1295. out:
  1296. return ret;
  1297. }
  1298. static int wlcore_get_reg_conf_ch_idx(enum ieee80211_band band, u16 ch)
  1299. {
  1300. int idx = -1;
  1301. switch (band) {
  1302. case IEEE80211_BAND_5GHZ:
  1303. if (ch >= 8 && ch <= 16)
  1304. idx = ((ch-8)/4 + 18);
  1305. else if (ch >= 34 && ch <= 64)
  1306. idx = ((ch-34)/2 + 3 + 18);
  1307. else if (ch >= 100 && ch <= 140)
  1308. idx = ((ch-100)/4 + 15 + 18);
  1309. else if (ch >= 149 && ch <= 165)
  1310. idx = ((ch-149)/4 + 26 + 18);
  1311. else
  1312. idx = -1;
  1313. break;
  1314. case IEEE80211_BAND_2GHZ:
  1315. if (ch >= 1 && ch <= 14)
  1316. idx = ch - 1;
  1317. else
  1318. idx = -1;
  1319. break;
  1320. default:
  1321. wl1271_error("get reg conf ch idx - unknown band: %d",
  1322. (int)band);
  1323. }
  1324. return idx;
  1325. }
  1326. void wlcore_set_pending_regdomain_ch(struct wl1271 *wl, u16 channel,
  1327. enum ieee80211_band band)
  1328. {
  1329. int ch_bit_idx = 0;
  1330. if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
  1331. return;
  1332. ch_bit_idx = wlcore_get_reg_conf_ch_idx(band, channel);
  1333. if (ch_bit_idx > 0 && ch_bit_idx <= WL1271_MAX_CHANNELS)
  1334. set_bit(ch_bit_idx, (long *)wl->reg_ch_conf_pending);
  1335. }
  1336. int wlcore_cmd_regdomain_config_locked(struct wl1271 *wl)
  1337. {
  1338. struct wl12xx_cmd_regdomain_dfs_config *cmd = NULL;
  1339. int ret = 0, i, b, ch_bit_idx;
  1340. struct ieee80211_channel *channel;
  1341. u32 tmp_ch_bitmap[2];
  1342. u16 ch;
  1343. struct wiphy *wiphy = wl->hw->wiphy;
  1344. struct ieee80211_supported_band *band;
  1345. bool timeout = false;
  1346. if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
  1347. return 0;
  1348. wl1271_debug(DEBUG_CMD, "cmd reg domain config");
  1349. memset(tmp_ch_bitmap, 0, sizeof(tmp_ch_bitmap));
  1350. for (b = IEEE80211_BAND_2GHZ; b <= IEEE80211_BAND_5GHZ; b++) {
  1351. band = wiphy->bands[b];
  1352. for (i = 0; i < band->n_channels; i++) {
  1353. channel = &band->channels[i];
  1354. ch = channel->hw_value;
  1355. if (channel->flags & (IEEE80211_CHAN_DISABLED |
  1356. IEEE80211_CHAN_RADAR |
  1357. IEEE80211_CHAN_PASSIVE_SCAN))
  1358. continue;
  1359. ch_bit_idx = wlcore_get_reg_conf_ch_idx(b, ch);
  1360. if (ch_bit_idx < 0)
  1361. continue;
  1362. set_bit(ch_bit_idx, (long *)tmp_ch_bitmap);
  1363. }
  1364. }
  1365. tmp_ch_bitmap[0] |= wl->reg_ch_conf_pending[0];
  1366. tmp_ch_bitmap[1] |= wl->reg_ch_conf_pending[1];
  1367. if (!memcmp(tmp_ch_bitmap, wl->reg_ch_conf_last, sizeof(tmp_ch_bitmap)))
  1368. goto out;
  1369. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1370. if (!cmd) {
  1371. ret = -ENOMEM;
  1372. goto out;
  1373. }
  1374. cmd->ch_bit_map1 = cpu_to_le32(tmp_ch_bitmap[0]);
  1375. cmd->ch_bit_map2 = cpu_to_le32(tmp_ch_bitmap[1]);
  1376. wl1271_debug(DEBUG_CMD,
  1377. "cmd reg domain bitmap1: 0x%08x, bitmap2: 0x%08x",
  1378. cmd->ch_bit_map1, cmd->ch_bit_map2);
  1379. ret = wl1271_cmd_send(wl, CMD_DFS_CHANNEL_CONFIG, cmd, sizeof(*cmd), 0);
  1380. if (ret < 0) {
  1381. wl1271_error("failed to send reg domain dfs config");
  1382. goto out;
  1383. }
  1384. ret = wl->ops->wait_for_event(wl,
  1385. WLCORE_EVENT_DFS_CONFIG_COMPLETE,
  1386. &timeout);
  1387. if (ret < 0 || timeout) {
  1388. wl1271_error("reg domain conf %serror",
  1389. timeout ? "completion " : "");
  1390. ret = timeout ? -ETIMEDOUT : ret;
  1391. goto out;
  1392. }
  1393. memcpy(wl->reg_ch_conf_last, tmp_ch_bitmap, sizeof(tmp_ch_bitmap));
  1394. memset(wl->reg_ch_conf_pending, 0, sizeof(wl->reg_ch_conf_pending));
  1395. out:
  1396. kfree(cmd);
  1397. return ret;
  1398. }
  1399. int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
  1400. {
  1401. struct wl12xx_cmd_config_fwlog *cmd;
  1402. int ret = 0;
  1403. wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
  1404. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1405. if (!cmd) {
  1406. ret = -ENOMEM;
  1407. goto out;
  1408. }
  1409. cmd->logger_mode = wl->conf.fwlog.mode;
  1410. cmd->log_severity = wl->conf.fwlog.severity;
  1411. cmd->timestamp = wl->conf.fwlog.timestamp;
  1412. cmd->output = wl->conf.fwlog.output;
  1413. cmd->threshold = wl->conf.fwlog.threshold;
  1414. ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
  1415. if (ret < 0) {
  1416. wl1271_error("failed to send config firmware logger command");
  1417. goto out_free;
  1418. }
  1419. out_free:
  1420. kfree(cmd);
  1421. out:
  1422. return ret;
  1423. }
  1424. int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
  1425. {
  1426. struct wl12xx_cmd_start_fwlog *cmd;
  1427. int ret = 0;
  1428. wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
  1429. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1430. if (!cmd) {
  1431. ret = -ENOMEM;
  1432. goto out;
  1433. }
  1434. ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
  1435. if (ret < 0) {
  1436. wl1271_error("failed to send start firmware logger command");
  1437. goto out_free;
  1438. }
  1439. out_free:
  1440. kfree(cmd);
  1441. out:
  1442. return ret;
  1443. }
  1444. int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
  1445. {
  1446. struct wl12xx_cmd_stop_fwlog *cmd;
  1447. int ret = 0;
  1448. wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
  1449. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1450. if (!cmd) {
  1451. ret = -ENOMEM;
  1452. goto out;
  1453. }
  1454. ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
  1455. if (ret < 0) {
  1456. wl1271_error("failed to send stop firmware logger command");
  1457. goto out_free;
  1458. }
  1459. out_free:
  1460. kfree(cmd);
  1461. out:
  1462. return ret;
  1463. }
  1464. static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1465. u8 role_id, enum ieee80211_band band, u8 channel)
  1466. {
  1467. struct wl12xx_cmd_roc *cmd;
  1468. int ret = 0;
  1469. wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", channel, role_id);
  1470. if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
  1471. return -EINVAL;
  1472. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1473. if (!cmd) {
  1474. ret = -ENOMEM;
  1475. goto out;
  1476. }
  1477. cmd->role_id = role_id;
  1478. cmd->channel = channel;
  1479. switch (band) {
  1480. case IEEE80211_BAND_2GHZ:
  1481. cmd->band = WLCORE_BAND_2_4GHZ;
  1482. break;
  1483. case IEEE80211_BAND_5GHZ:
  1484. cmd->band = WLCORE_BAND_5GHZ;
  1485. break;
  1486. default:
  1487. wl1271_error("roc - unknown band: %d", (int)wlvif->band);
  1488. ret = -EINVAL;
  1489. goto out_free;
  1490. }
  1491. ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
  1492. if (ret < 0) {
  1493. wl1271_error("failed to send ROC command");
  1494. goto out_free;
  1495. }
  1496. out_free:
  1497. kfree(cmd);
  1498. out:
  1499. return ret;
  1500. }
  1501. static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
  1502. {
  1503. struct wl12xx_cmd_croc *cmd;
  1504. int ret = 0;
  1505. wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
  1506. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1507. if (!cmd) {
  1508. ret = -ENOMEM;
  1509. goto out;
  1510. }
  1511. cmd->role_id = role_id;
  1512. ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
  1513. sizeof(*cmd), 0);
  1514. if (ret < 0) {
  1515. wl1271_error("failed to send ROC command");
  1516. goto out_free;
  1517. }
  1518. out_free:
  1519. kfree(cmd);
  1520. out:
  1521. return ret;
  1522. }
  1523. int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id,
  1524. enum ieee80211_band band, u8 channel)
  1525. {
  1526. int ret = 0;
  1527. if (WARN_ON(test_bit(role_id, wl->roc_map)))
  1528. return 0;
  1529. ret = wl12xx_cmd_roc(wl, wlvif, role_id, band, channel);
  1530. if (ret < 0)
  1531. goto out;
  1532. __set_bit(role_id, wl->roc_map);
  1533. out:
  1534. return ret;
  1535. }
  1536. int wl12xx_croc(struct wl1271 *wl, u8 role_id)
  1537. {
  1538. int ret = 0;
  1539. if (WARN_ON(!test_bit(role_id, wl->roc_map)))
  1540. return 0;
  1541. ret = wl12xx_cmd_croc(wl, role_id);
  1542. if (ret < 0)
  1543. goto out;
  1544. __clear_bit(role_id, wl->roc_map);
  1545. /*
  1546. * Rearm the tx watchdog when removing the last ROC. This prevents
  1547. * recoveries due to just finished ROCs - when Tx hasn't yet had
  1548. * a chance to get out.
  1549. */
  1550. if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
  1551. wl12xx_rearm_tx_watchdog_locked(wl);
  1552. out:
  1553. return ret;
  1554. }
  1555. int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  1556. {
  1557. struct wl12xx_cmd_stop_channel_switch *cmd;
  1558. int ret;
  1559. wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
  1560. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1561. if (!cmd) {
  1562. ret = -ENOMEM;
  1563. goto out;
  1564. }
  1565. cmd->role_id = wlvif->role_id;
  1566. ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
  1567. if (ret < 0) {
  1568. wl1271_error("failed to stop channel switch command");
  1569. goto out_free;
  1570. }
  1571. out_free:
  1572. kfree(cmd);
  1573. out:
  1574. return ret;
  1575. }
  1576. /* start dev role and roc on its channel */
  1577. int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1578. enum ieee80211_band band, int channel)
  1579. {
  1580. int ret;
  1581. if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
  1582. wlvif->bss_type == BSS_TYPE_IBSS)))
  1583. return -EINVAL;
  1584. ret = wl12xx_cmd_role_enable(wl,
  1585. wl12xx_wlvif_to_vif(wlvif)->addr,
  1586. WL1271_ROLE_DEVICE,
  1587. &wlvif->dev_role_id);
  1588. if (ret < 0)
  1589. goto out;
  1590. ret = wl12xx_cmd_role_start_dev(wl, wlvif, band, channel);
  1591. if (ret < 0)
  1592. goto out_disable;
  1593. ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id, band, channel);
  1594. if (ret < 0)
  1595. goto out_stop;
  1596. return 0;
  1597. out_stop:
  1598. wl12xx_cmd_role_stop_dev(wl, wlvif);
  1599. out_disable:
  1600. wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
  1601. out:
  1602. return ret;
  1603. }
  1604. /* croc dev hlid, and stop the role */
  1605. int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  1606. {
  1607. int ret;
  1608. if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
  1609. wlvif->bss_type == BSS_TYPE_IBSS)))
  1610. return -EINVAL;
  1611. /* flush all pending packets */
  1612. ret = wlcore_tx_work_locked(wl);
  1613. if (ret < 0)
  1614. goto out;
  1615. if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
  1616. ret = wl12xx_croc(wl, wlvif->dev_role_id);
  1617. if (ret < 0)
  1618. goto out;
  1619. }
  1620. ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
  1621. if (ret < 0)
  1622. goto out;
  1623. ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
  1624. if (ret < 0)
  1625. goto out;
  1626. out:
  1627. return ret;
  1628. }