cmd.c 40 KB

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