cmd.c 41 KB

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