cmd.c 40 KB

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