cmd.c 47 KB

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