cmd.c 41 KB

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