cmd.c 45 KB

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