wsm.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * WSM host interface (HI) implementation for
  4. * ST-Ericsson CW1200 mac80211 drivers.
  5. *
  6. * Copyright (c) 2010, ST-Ericsson
  7. * Author: Dmitry Tarnyagin <[email protected]>
  8. */
  9. #include <linux/skbuff.h>
  10. #include <linux/wait.h>
  11. #include <linux/delay.h>
  12. #include <linux/sched.h>
  13. #include <linux/random.h>
  14. #include "cw1200.h"
  15. #include "wsm.h"
  16. #include "bh.h"
  17. #include "sta.h"
  18. #include "debug.h"
  19. #define WSM_CMD_TIMEOUT (2 * HZ) /* With respect to interrupt loss */
  20. #define WSM_CMD_START_TIMEOUT (7 * HZ)
  21. #define WSM_CMD_RESET_TIMEOUT (3 * HZ) /* 2 sec. timeout was observed. */
  22. #define WSM_CMD_MAX_TIMEOUT (3 * HZ)
  23. #define WSM_SKIP(buf, size) \
  24. do { \
  25. if ((buf)->data + size > (buf)->end) \
  26. goto underflow; \
  27. (buf)->data += size; \
  28. } while (0)
  29. #define WSM_GET(buf, ptr, size) \
  30. do { \
  31. if ((buf)->data + size > (buf)->end) \
  32. goto underflow; \
  33. memcpy(ptr, (buf)->data, size); \
  34. (buf)->data += size; \
  35. } while (0)
  36. #define __WSM_GET(buf, type, type2, cvt) \
  37. ({ \
  38. type val; \
  39. if ((buf)->data + sizeof(type) > (buf)->end) \
  40. goto underflow; \
  41. val = cvt(*(type2 *)(buf)->data); \
  42. (buf)->data += sizeof(type); \
  43. val; \
  44. })
  45. #define WSM_GET8(buf) __WSM_GET(buf, u8, u8, (u8))
  46. #define WSM_GET16(buf) __WSM_GET(buf, u16, __le16, __le16_to_cpu)
  47. #define WSM_GET32(buf) __WSM_GET(buf, u32, __le32, __le32_to_cpu)
  48. #define WSM_PUT(buf, ptr, size) \
  49. do { \
  50. if ((buf)->data + size > (buf)->end) \
  51. if (wsm_buf_reserve((buf), size)) \
  52. goto nomem; \
  53. memcpy((buf)->data, ptr, size); \
  54. (buf)->data += size; \
  55. } while (0)
  56. #define __WSM_PUT(buf, val, type, type2, cvt) \
  57. do { \
  58. if ((buf)->data + sizeof(type) > (buf)->end) \
  59. if (wsm_buf_reserve((buf), sizeof(type))) \
  60. goto nomem; \
  61. *(type2 *)(buf)->data = cvt(val); \
  62. (buf)->data += sizeof(type); \
  63. } while (0)
  64. #define WSM_PUT8(buf, val) __WSM_PUT(buf, val, u8, u8, (u8))
  65. #define WSM_PUT16(buf, val) __WSM_PUT(buf, val, u16, __le16, __cpu_to_le16)
  66. #define WSM_PUT32(buf, val) __WSM_PUT(buf, val, u32, __le32, __cpu_to_le32)
  67. static void wsm_buf_reset(struct wsm_buf *buf);
  68. static int wsm_buf_reserve(struct wsm_buf *buf, size_t extra_size);
  69. static int wsm_cmd_send(struct cw1200_common *priv,
  70. struct wsm_buf *buf,
  71. void *arg, u16 cmd, long tmo);
  72. #define wsm_cmd_lock(__priv) mutex_lock(&((__priv)->wsm_cmd_mux))
  73. #define wsm_cmd_unlock(__priv) mutex_unlock(&((__priv)->wsm_cmd_mux))
  74. /* ******************************************************************** */
  75. /* WSM API implementation */
  76. static int wsm_generic_confirm(struct cw1200_common *priv,
  77. void *arg,
  78. struct wsm_buf *buf)
  79. {
  80. u32 status = WSM_GET32(buf);
  81. if (status != WSM_STATUS_SUCCESS)
  82. return -EINVAL;
  83. return 0;
  84. underflow:
  85. WARN_ON(1);
  86. return -EINVAL;
  87. }
  88. int wsm_configuration(struct cw1200_common *priv, struct wsm_configuration *arg)
  89. {
  90. int ret;
  91. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  92. wsm_cmd_lock(priv);
  93. WSM_PUT32(buf, arg->dot11MaxTransmitMsduLifeTime);
  94. WSM_PUT32(buf, arg->dot11MaxReceiveLifeTime);
  95. WSM_PUT32(buf, arg->dot11RtsThreshold);
  96. /* DPD block. */
  97. WSM_PUT16(buf, arg->dpdData_size + 12);
  98. WSM_PUT16(buf, 1); /* DPD version */
  99. WSM_PUT(buf, arg->dot11StationId, ETH_ALEN);
  100. WSM_PUT16(buf, 5); /* DPD flags */
  101. WSM_PUT(buf, arg->dpdData, arg->dpdData_size);
  102. ret = wsm_cmd_send(priv, buf, arg,
  103. WSM_CONFIGURATION_REQ_ID, WSM_CMD_TIMEOUT);
  104. wsm_cmd_unlock(priv);
  105. return ret;
  106. nomem:
  107. wsm_cmd_unlock(priv);
  108. return -ENOMEM;
  109. }
  110. static int wsm_configuration_confirm(struct cw1200_common *priv,
  111. struct wsm_configuration *arg,
  112. struct wsm_buf *buf)
  113. {
  114. int i;
  115. int status;
  116. status = WSM_GET32(buf);
  117. if (WARN_ON(status != WSM_STATUS_SUCCESS))
  118. return -EINVAL;
  119. WSM_GET(buf, arg->dot11StationId, ETH_ALEN);
  120. arg->dot11FrequencyBandsSupported = WSM_GET8(buf);
  121. WSM_SKIP(buf, 1);
  122. arg->supportedRateMask = WSM_GET32(buf);
  123. for (i = 0; i < 2; ++i) {
  124. arg->txPowerRange[i].min_power_level = WSM_GET32(buf);
  125. arg->txPowerRange[i].max_power_level = WSM_GET32(buf);
  126. arg->txPowerRange[i].stepping = WSM_GET32(buf);
  127. }
  128. return 0;
  129. underflow:
  130. WARN_ON(1);
  131. return -EINVAL;
  132. }
  133. /* ******************************************************************** */
  134. int wsm_reset(struct cw1200_common *priv, const struct wsm_reset *arg)
  135. {
  136. int ret;
  137. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  138. u16 cmd = WSM_RESET_REQ_ID | WSM_TX_LINK_ID(arg->link_id);
  139. wsm_cmd_lock(priv);
  140. WSM_PUT32(buf, arg->reset_statistics ? 0 : 1);
  141. ret = wsm_cmd_send(priv, buf, NULL, cmd, WSM_CMD_RESET_TIMEOUT);
  142. wsm_cmd_unlock(priv);
  143. return ret;
  144. nomem:
  145. wsm_cmd_unlock(priv);
  146. return -ENOMEM;
  147. }
  148. /* ******************************************************************** */
  149. struct wsm_mib {
  150. u16 mib_id;
  151. void *buf;
  152. size_t buf_size;
  153. };
  154. int wsm_read_mib(struct cw1200_common *priv, u16 mib_id, void *_buf,
  155. size_t buf_size)
  156. {
  157. int ret;
  158. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  159. struct wsm_mib mib_buf = {
  160. .mib_id = mib_id,
  161. .buf = _buf,
  162. .buf_size = buf_size,
  163. };
  164. wsm_cmd_lock(priv);
  165. WSM_PUT16(buf, mib_id);
  166. WSM_PUT16(buf, 0);
  167. ret = wsm_cmd_send(priv, buf, &mib_buf,
  168. WSM_READ_MIB_REQ_ID, WSM_CMD_TIMEOUT);
  169. wsm_cmd_unlock(priv);
  170. return ret;
  171. nomem:
  172. wsm_cmd_unlock(priv);
  173. return -ENOMEM;
  174. }
  175. static int wsm_read_mib_confirm(struct cw1200_common *priv,
  176. struct wsm_mib *arg,
  177. struct wsm_buf *buf)
  178. {
  179. u16 size;
  180. if (WARN_ON(WSM_GET32(buf) != WSM_STATUS_SUCCESS))
  181. return -EINVAL;
  182. if (WARN_ON(WSM_GET16(buf) != arg->mib_id))
  183. return -EINVAL;
  184. size = WSM_GET16(buf);
  185. if (size > arg->buf_size)
  186. size = arg->buf_size;
  187. WSM_GET(buf, arg->buf, size);
  188. arg->buf_size = size;
  189. return 0;
  190. underflow:
  191. WARN_ON(1);
  192. return -EINVAL;
  193. }
  194. /* ******************************************************************** */
  195. int wsm_write_mib(struct cw1200_common *priv, u16 mib_id, void *_buf,
  196. size_t buf_size)
  197. {
  198. int ret;
  199. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  200. struct wsm_mib mib_buf = {
  201. .mib_id = mib_id,
  202. .buf = _buf,
  203. .buf_size = buf_size,
  204. };
  205. wsm_cmd_lock(priv);
  206. WSM_PUT16(buf, mib_id);
  207. WSM_PUT16(buf, buf_size);
  208. WSM_PUT(buf, _buf, buf_size);
  209. ret = wsm_cmd_send(priv, buf, &mib_buf,
  210. WSM_WRITE_MIB_REQ_ID, WSM_CMD_TIMEOUT);
  211. wsm_cmd_unlock(priv);
  212. return ret;
  213. nomem:
  214. wsm_cmd_unlock(priv);
  215. return -ENOMEM;
  216. }
  217. static int wsm_write_mib_confirm(struct cw1200_common *priv,
  218. struct wsm_mib *arg,
  219. struct wsm_buf *buf)
  220. {
  221. int ret;
  222. ret = wsm_generic_confirm(priv, arg, buf);
  223. if (ret)
  224. return ret;
  225. if (arg->mib_id == WSM_MIB_ID_OPERATIONAL_POWER_MODE) {
  226. /* OperationalMode: update PM status. */
  227. const char *p = arg->buf;
  228. cw1200_enable_powersave(priv, (p[0] & 0x0F) ? true : false);
  229. }
  230. return 0;
  231. }
  232. /* ******************************************************************** */
  233. int wsm_scan(struct cw1200_common *priv, const struct wsm_scan *arg)
  234. {
  235. int i;
  236. int ret;
  237. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  238. if (arg->num_channels > 48)
  239. return -EINVAL;
  240. if (arg->num_ssids > 2)
  241. return -EINVAL;
  242. if (arg->band > 1)
  243. return -EINVAL;
  244. wsm_cmd_lock(priv);
  245. WSM_PUT8(buf, arg->band);
  246. WSM_PUT8(buf, arg->type);
  247. WSM_PUT8(buf, arg->flags);
  248. WSM_PUT8(buf, arg->max_tx_rate);
  249. WSM_PUT32(buf, arg->auto_scan_interval);
  250. WSM_PUT8(buf, arg->num_probes);
  251. WSM_PUT8(buf, arg->num_channels);
  252. WSM_PUT8(buf, arg->num_ssids);
  253. WSM_PUT8(buf, arg->probe_delay);
  254. for (i = 0; i < arg->num_channels; ++i) {
  255. WSM_PUT16(buf, arg->ch[i].number);
  256. WSM_PUT16(buf, 0);
  257. WSM_PUT32(buf, arg->ch[i].min_chan_time);
  258. WSM_PUT32(buf, arg->ch[i].max_chan_time);
  259. WSM_PUT32(buf, 0);
  260. }
  261. for (i = 0; i < arg->num_ssids; ++i) {
  262. WSM_PUT32(buf, arg->ssids[i].length);
  263. WSM_PUT(buf, &arg->ssids[i].ssid[0],
  264. sizeof(arg->ssids[i].ssid));
  265. }
  266. ret = wsm_cmd_send(priv, buf, NULL,
  267. WSM_START_SCAN_REQ_ID, WSM_CMD_TIMEOUT);
  268. wsm_cmd_unlock(priv);
  269. return ret;
  270. nomem:
  271. wsm_cmd_unlock(priv);
  272. return -ENOMEM;
  273. }
  274. /* ******************************************************************** */
  275. int wsm_stop_scan(struct cw1200_common *priv)
  276. {
  277. int ret;
  278. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  279. wsm_cmd_lock(priv);
  280. ret = wsm_cmd_send(priv, buf, NULL,
  281. WSM_STOP_SCAN_REQ_ID, WSM_CMD_TIMEOUT);
  282. wsm_cmd_unlock(priv);
  283. return ret;
  284. }
  285. static int wsm_tx_confirm(struct cw1200_common *priv,
  286. struct wsm_buf *buf,
  287. int link_id)
  288. {
  289. struct wsm_tx_confirm tx_confirm;
  290. tx_confirm.packet_id = WSM_GET32(buf);
  291. tx_confirm.status = WSM_GET32(buf);
  292. tx_confirm.tx_rate = WSM_GET8(buf);
  293. tx_confirm.ack_failures = WSM_GET8(buf);
  294. tx_confirm.flags = WSM_GET16(buf);
  295. tx_confirm.media_delay = WSM_GET32(buf);
  296. tx_confirm.tx_queue_delay = WSM_GET32(buf);
  297. cw1200_tx_confirm_cb(priv, link_id, &tx_confirm);
  298. return 0;
  299. underflow:
  300. WARN_ON(1);
  301. return -EINVAL;
  302. }
  303. static int wsm_multi_tx_confirm(struct cw1200_common *priv,
  304. struct wsm_buf *buf, int link_id)
  305. {
  306. int ret;
  307. int count;
  308. count = WSM_GET32(buf);
  309. if (WARN_ON(count <= 0))
  310. return -EINVAL;
  311. if (count > 1) {
  312. /* We already released one buffer, now for the rest */
  313. ret = wsm_release_tx_buffer(priv, count - 1);
  314. if (ret < 0)
  315. return ret;
  316. else if (ret > 0)
  317. cw1200_bh_wakeup(priv);
  318. }
  319. cw1200_debug_txed_multi(priv, count);
  320. do {
  321. ret = wsm_tx_confirm(priv, buf, link_id);
  322. } while (!ret && --count);
  323. return ret;
  324. underflow:
  325. WARN_ON(1);
  326. return -EINVAL;
  327. }
  328. /* ******************************************************************** */
  329. static int wsm_join_confirm(struct cw1200_common *priv,
  330. struct wsm_join_cnf *arg,
  331. struct wsm_buf *buf)
  332. {
  333. arg->status = WSM_GET32(buf);
  334. if (WARN_ON(arg->status) != WSM_STATUS_SUCCESS)
  335. return -EINVAL;
  336. arg->min_power_level = WSM_GET32(buf);
  337. arg->max_power_level = WSM_GET32(buf);
  338. return 0;
  339. underflow:
  340. WARN_ON(1);
  341. return -EINVAL;
  342. }
  343. int wsm_join(struct cw1200_common *priv, struct wsm_join *arg)
  344. {
  345. int ret;
  346. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  347. struct wsm_join_cnf resp;
  348. wsm_cmd_lock(priv);
  349. WSM_PUT8(buf, arg->mode);
  350. WSM_PUT8(buf, arg->band);
  351. WSM_PUT16(buf, arg->channel_number);
  352. WSM_PUT(buf, &arg->bssid[0], sizeof(arg->bssid));
  353. WSM_PUT16(buf, arg->atim_window);
  354. WSM_PUT8(buf, arg->preamble_type);
  355. WSM_PUT8(buf, arg->probe_for_join);
  356. WSM_PUT8(buf, arg->dtim_period);
  357. WSM_PUT8(buf, arg->flags);
  358. WSM_PUT32(buf, arg->ssid_len);
  359. WSM_PUT(buf, &arg->ssid[0], sizeof(arg->ssid));
  360. WSM_PUT32(buf, arg->beacon_interval);
  361. WSM_PUT32(buf, arg->basic_rate_set);
  362. priv->tx_burst_idx = -1;
  363. ret = wsm_cmd_send(priv, buf, &resp,
  364. WSM_JOIN_REQ_ID, WSM_CMD_TIMEOUT);
  365. /* TODO: Update state based on resp.min|max_power_level */
  366. priv->join_complete_status = resp.status;
  367. wsm_cmd_unlock(priv);
  368. return ret;
  369. nomem:
  370. wsm_cmd_unlock(priv);
  371. return -ENOMEM;
  372. }
  373. /* ******************************************************************** */
  374. int wsm_set_bss_params(struct cw1200_common *priv,
  375. const struct wsm_set_bss_params *arg)
  376. {
  377. int ret;
  378. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  379. wsm_cmd_lock(priv);
  380. WSM_PUT8(buf, (arg->reset_beacon_loss ? 0x1 : 0));
  381. WSM_PUT8(buf, arg->beacon_lost_count);
  382. WSM_PUT16(buf, arg->aid);
  383. WSM_PUT32(buf, arg->operational_rate_set);
  384. ret = wsm_cmd_send(priv, buf, NULL,
  385. WSM_SET_BSS_PARAMS_REQ_ID, WSM_CMD_TIMEOUT);
  386. wsm_cmd_unlock(priv);
  387. return ret;
  388. nomem:
  389. wsm_cmd_unlock(priv);
  390. return -ENOMEM;
  391. }
  392. /* ******************************************************************** */
  393. int wsm_add_key(struct cw1200_common *priv, const struct wsm_add_key *arg)
  394. {
  395. int ret;
  396. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  397. wsm_cmd_lock(priv);
  398. WSM_PUT(buf, arg, sizeof(*arg));
  399. ret = wsm_cmd_send(priv, buf, NULL,
  400. WSM_ADD_KEY_REQ_ID, WSM_CMD_TIMEOUT);
  401. wsm_cmd_unlock(priv);
  402. return ret;
  403. nomem:
  404. wsm_cmd_unlock(priv);
  405. return -ENOMEM;
  406. }
  407. /* ******************************************************************** */
  408. int wsm_remove_key(struct cw1200_common *priv, const struct wsm_remove_key *arg)
  409. {
  410. int ret;
  411. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  412. wsm_cmd_lock(priv);
  413. WSM_PUT8(buf, arg->index);
  414. WSM_PUT8(buf, 0);
  415. WSM_PUT16(buf, 0);
  416. ret = wsm_cmd_send(priv, buf, NULL,
  417. WSM_REMOVE_KEY_REQ_ID, WSM_CMD_TIMEOUT);
  418. wsm_cmd_unlock(priv);
  419. return ret;
  420. nomem:
  421. wsm_cmd_unlock(priv);
  422. return -ENOMEM;
  423. }
  424. /* ******************************************************************** */
  425. int wsm_set_tx_queue_params(struct cw1200_common *priv,
  426. const struct wsm_set_tx_queue_params *arg, u8 id)
  427. {
  428. int ret;
  429. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  430. static const u8 queue_id_to_wmm_aci[] = { 3, 2, 0, 1 };
  431. wsm_cmd_lock(priv);
  432. WSM_PUT8(buf, queue_id_to_wmm_aci[id]);
  433. WSM_PUT8(buf, 0);
  434. WSM_PUT8(buf, arg->ackPolicy);
  435. WSM_PUT8(buf, 0);
  436. WSM_PUT32(buf, arg->maxTransmitLifetime);
  437. WSM_PUT16(buf, arg->allowedMediumTime);
  438. WSM_PUT16(buf, 0);
  439. ret = wsm_cmd_send(priv, buf, NULL, 0x0012, WSM_CMD_TIMEOUT);
  440. wsm_cmd_unlock(priv);
  441. return ret;
  442. nomem:
  443. wsm_cmd_unlock(priv);
  444. return -ENOMEM;
  445. }
  446. /* ******************************************************************** */
  447. int wsm_set_edca_params(struct cw1200_common *priv,
  448. const struct wsm_edca_params *arg)
  449. {
  450. int ret;
  451. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  452. wsm_cmd_lock(priv);
  453. /* Implemented according to specification. */
  454. WSM_PUT16(buf, arg->params[3].cwmin);
  455. WSM_PUT16(buf, arg->params[2].cwmin);
  456. WSM_PUT16(buf, arg->params[1].cwmin);
  457. WSM_PUT16(buf, arg->params[0].cwmin);
  458. WSM_PUT16(buf, arg->params[3].cwmax);
  459. WSM_PUT16(buf, arg->params[2].cwmax);
  460. WSM_PUT16(buf, arg->params[1].cwmax);
  461. WSM_PUT16(buf, arg->params[0].cwmax);
  462. WSM_PUT8(buf, arg->params[3].aifns);
  463. WSM_PUT8(buf, arg->params[2].aifns);
  464. WSM_PUT8(buf, arg->params[1].aifns);
  465. WSM_PUT8(buf, arg->params[0].aifns);
  466. WSM_PUT16(buf, arg->params[3].txop_limit);
  467. WSM_PUT16(buf, arg->params[2].txop_limit);
  468. WSM_PUT16(buf, arg->params[1].txop_limit);
  469. WSM_PUT16(buf, arg->params[0].txop_limit);
  470. WSM_PUT32(buf, arg->params[3].max_rx_lifetime);
  471. WSM_PUT32(buf, arg->params[2].max_rx_lifetime);
  472. WSM_PUT32(buf, arg->params[1].max_rx_lifetime);
  473. WSM_PUT32(buf, arg->params[0].max_rx_lifetime);
  474. ret = wsm_cmd_send(priv, buf, NULL,
  475. WSM_EDCA_PARAMS_REQ_ID, WSM_CMD_TIMEOUT);
  476. wsm_cmd_unlock(priv);
  477. return ret;
  478. nomem:
  479. wsm_cmd_unlock(priv);
  480. return -ENOMEM;
  481. }
  482. /* ******************************************************************** */
  483. int wsm_switch_channel(struct cw1200_common *priv,
  484. const struct wsm_switch_channel *arg)
  485. {
  486. int ret;
  487. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  488. wsm_cmd_lock(priv);
  489. WSM_PUT8(buf, arg->mode);
  490. WSM_PUT8(buf, arg->switch_count);
  491. WSM_PUT16(buf, arg->channel_number);
  492. priv->channel_switch_in_progress = 1;
  493. ret = wsm_cmd_send(priv, buf, NULL,
  494. WSM_SWITCH_CHANNEL_REQ_ID, WSM_CMD_TIMEOUT);
  495. if (ret)
  496. priv->channel_switch_in_progress = 0;
  497. wsm_cmd_unlock(priv);
  498. return ret;
  499. nomem:
  500. wsm_cmd_unlock(priv);
  501. return -ENOMEM;
  502. }
  503. /* ******************************************************************** */
  504. int wsm_set_pm(struct cw1200_common *priv, const struct wsm_set_pm *arg)
  505. {
  506. int ret;
  507. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  508. priv->ps_mode_switch_in_progress = 1;
  509. wsm_cmd_lock(priv);
  510. WSM_PUT8(buf, arg->mode);
  511. WSM_PUT8(buf, arg->fast_psm_idle_period);
  512. WSM_PUT8(buf, arg->ap_psm_change_period);
  513. WSM_PUT8(buf, arg->min_auto_pspoll_period);
  514. ret = wsm_cmd_send(priv, buf, NULL,
  515. WSM_SET_PM_REQ_ID, WSM_CMD_TIMEOUT);
  516. wsm_cmd_unlock(priv);
  517. return ret;
  518. nomem:
  519. wsm_cmd_unlock(priv);
  520. return -ENOMEM;
  521. }
  522. /* ******************************************************************** */
  523. int wsm_start(struct cw1200_common *priv, const struct wsm_start *arg)
  524. {
  525. int ret;
  526. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  527. wsm_cmd_lock(priv);
  528. WSM_PUT8(buf, arg->mode);
  529. WSM_PUT8(buf, arg->band);
  530. WSM_PUT16(buf, arg->channel_number);
  531. WSM_PUT32(buf, arg->ct_window);
  532. WSM_PUT32(buf, arg->beacon_interval);
  533. WSM_PUT8(buf, arg->dtim_period);
  534. WSM_PUT8(buf, arg->preamble);
  535. WSM_PUT8(buf, arg->probe_delay);
  536. WSM_PUT8(buf, arg->ssid_len);
  537. WSM_PUT(buf, arg->ssid, sizeof(arg->ssid));
  538. WSM_PUT32(buf, arg->basic_rate_set);
  539. priv->tx_burst_idx = -1;
  540. ret = wsm_cmd_send(priv, buf, NULL,
  541. WSM_START_REQ_ID, WSM_CMD_START_TIMEOUT);
  542. wsm_cmd_unlock(priv);
  543. return ret;
  544. nomem:
  545. wsm_cmd_unlock(priv);
  546. return -ENOMEM;
  547. }
  548. /* ******************************************************************** */
  549. int wsm_beacon_transmit(struct cw1200_common *priv,
  550. const struct wsm_beacon_transmit *arg)
  551. {
  552. int ret;
  553. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  554. wsm_cmd_lock(priv);
  555. WSM_PUT32(buf, arg->enable_beaconing ? 1 : 0);
  556. ret = wsm_cmd_send(priv, buf, NULL,
  557. WSM_BEACON_TRANSMIT_REQ_ID, WSM_CMD_TIMEOUT);
  558. wsm_cmd_unlock(priv);
  559. return ret;
  560. nomem:
  561. wsm_cmd_unlock(priv);
  562. return -ENOMEM;
  563. }
  564. /* ******************************************************************** */
  565. int wsm_start_find(struct cw1200_common *priv)
  566. {
  567. int ret;
  568. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  569. wsm_cmd_lock(priv);
  570. ret = wsm_cmd_send(priv, buf, NULL, 0x0019, WSM_CMD_TIMEOUT);
  571. wsm_cmd_unlock(priv);
  572. return ret;
  573. }
  574. /* ******************************************************************** */
  575. int wsm_stop_find(struct cw1200_common *priv)
  576. {
  577. int ret;
  578. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  579. wsm_cmd_lock(priv);
  580. ret = wsm_cmd_send(priv, buf, NULL, 0x001A, WSM_CMD_TIMEOUT);
  581. wsm_cmd_unlock(priv);
  582. return ret;
  583. }
  584. /* ******************************************************************** */
  585. int wsm_map_link(struct cw1200_common *priv, const struct wsm_map_link *arg)
  586. {
  587. int ret;
  588. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  589. u16 cmd = 0x001C | WSM_TX_LINK_ID(arg->link_id);
  590. wsm_cmd_lock(priv);
  591. WSM_PUT(buf, &arg->mac_addr[0], sizeof(arg->mac_addr));
  592. WSM_PUT16(buf, 0);
  593. ret = wsm_cmd_send(priv, buf, NULL, cmd, WSM_CMD_TIMEOUT);
  594. wsm_cmd_unlock(priv);
  595. return ret;
  596. nomem:
  597. wsm_cmd_unlock(priv);
  598. return -ENOMEM;
  599. }
  600. /* ******************************************************************** */
  601. int wsm_update_ie(struct cw1200_common *priv,
  602. const struct wsm_update_ie *arg)
  603. {
  604. int ret;
  605. struct wsm_buf *buf = &priv->wsm_cmd_buf;
  606. wsm_cmd_lock(priv);
  607. WSM_PUT16(buf, arg->what);
  608. WSM_PUT16(buf, arg->count);
  609. WSM_PUT(buf, arg->ies, arg->length);
  610. ret = wsm_cmd_send(priv, buf, NULL, 0x001B, WSM_CMD_TIMEOUT);
  611. wsm_cmd_unlock(priv);
  612. return ret;
  613. nomem:
  614. wsm_cmd_unlock(priv);
  615. return -ENOMEM;
  616. }
  617. /* ******************************************************************** */
  618. int wsm_set_probe_responder(struct cw1200_common *priv, bool enable)
  619. {
  620. priv->rx_filter.probeResponder = enable;
  621. return wsm_set_rx_filter(priv, &priv->rx_filter);
  622. }
  623. /* ******************************************************************** */
  624. /* WSM indication events implementation */
  625. const char * const cw1200_fw_types[] = {
  626. "ETF",
  627. "WFM",
  628. "WSM",
  629. "HI test",
  630. "Platform test"
  631. };
  632. static int wsm_startup_indication(struct cw1200_common *priv,
  633. struct wsm_buf *buf)
  634. {
  635. priv->wsm_caps.input_buffers = WSM_GET16(buf);
  636. priv->wsm_caps.input_buffer_size = WSM_GET16(buf);
  637. priv->wsm_caps.hw_id = WSM_GET16(buf);
  638. priv->wsm_caps.hw_subid = WSM_GET16(buf);
  639. priv->wsm_caps.status = WSM_GET16(buf);
  640. priv->wsm_caps.fw_cap = WSM_GET16(buf);
  641. priv->wsm_caps.fw_type = WSM_GET16(buf);
  642. priv->wsm_caps.fw_api = WSM_GET16(buf);
  643. priv->wsm_caps.fw_build = WSM_GET16(buf);
  644. priv->wsm_caps.fw_ver = WSM_GET16(buf);
  645. WSM_GET(buf, priv->wsm_caps.fw_label, sizeof(priv->wsm_caps.fw_label));
  646. priv->wsm_caps.fw_label[sizeof(priv->wsm_caps.fw_label) - 1] = 0; /* Do not trust FW too much... */
  647. if (WARN_ON(priv->wsm_caps.status))
  648. return -EINVAL;
  649. if (WARN_ON(priv->wsm_caps.fw_type > 4))
  650. return -EINVAL;
  651. pr_info("CW1200 WSM init done.\n"
  652. " Input buffers: %d x %d bytes\n"
  653. " Hardware: %d.%d\n"
  654. " %s firmware [%s], ver: %d, build: %d,"
  655. " api: %d, cap: 0x%.4X\n",
  656. priv->wsm_caps.input_buffers,
  657. priv->wsm_caps.input_buffer_size,
  658. priv->wsm_caps.hw_id, priv->wsm_caps.hw_subid,
  659. cw1200_fw_types[priv->wsm_caps.fw_type],
  660. priv->wsm_caps.fw_label, priv->wsm_caps.fw_ver,
  661. priv->wsm_caps.fw_build,
  662. priv->wsm_caps.fw_api, priv->wsm_caps.fw_cap);
  663. /* Disable unsupported frequency bands */
  664. if (!(priv->wsm_caps.fw_cap & 0x1))
  665. priv->hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL;
  666. if (!(priv->wsm_caps.fw_cap & 0x2))
  667. priv->hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL;
  668. priv->firmware_ready = 1;
  669. wake_up(&priv->wsm_startup_done);
  670. return 0;
  671. underflow:
  672. WARN_ON(1);
  673. return -EINVAL;
  674. }
  675. static int wsm_receive_indication(struct cw1200_common *priv,
  676. int link_id,
  677. struct wsm_buf *buf,
  678. struct sk_buff **skb_p)
  679. {
  680. struct wsm_rx rx;
  681. struct ieee80211_hdr *hdr;
  682. size_t hdr_len;
  683. __le16 fctl;
  684. rx.status = WSM_GET32(buf);
  685. rx.channel_number = WSM_GET16(buf);
  686. rx.rx_rate = WSM_GET8(buf);
  687. rx.rcpi_rssi = WSM_GET8(buf);
  688. rx.flags = WSM_GET32(buf);
  689. /* FW Workaround: Drop probe resp or
  690. beacon when RSSI is 0
  691. */
  692. hdr = (struct ieee80211_hdr *)(*skb_p)->data;
  693. if (!rx.rcpi_rssi &&
  694. (ieee80211_is_probe_resp(hdr->frame_control) ||
  695. ieee80211_is_beacon(hdr->frame_control)))
  696. return 0;
  697. /* If no RSSI subscription has been made,
  698. * convert RCPI to RSSI here
  699. */
  700. if (!priv->cqm_use_rssi)
  701. rx.rcpi_rssi = rx.rcpi_rssi / 2 - 110;
  702. fctl = *(__le16 *)buf->data;
  703. hdr_len = buf->data - buf->begin;
  704. skb_pull(*skb_p, hdr_len);
  705. if (!rx.status && ieee80211_is_deauth(fctl)) {
  706. if (priv->join_status == CW1200_JOIN_STATUS_STA) {
  707. /* Shedule unjoin work */
  708. pr_debug("[WSM] Issue unjoin command (RX).\n");
  709. wsm_lock_tx_async(priv);
  710. if (queue_work(priv->workqueue,
  711. &priv->unjoin_work) <= 0)
  712. wsm_unlock_tx(priv);
  713. }
  714. }
  715. cw1200_rx_cb(priv, &rx, link_id, skb_p);
  716. if (*skb_p)
  717. skb_push(*skb_p, hdr_len);
  718. return 0;
  719. underflow:
  720. return -EINVAL;
  721. }
  722. static int wsm_event_indication(struct cw1200_common *priv, struct wsm_buf *buf)
  723. {
  724. int first;
  725. struct cw1200_wsm_event *event;
  726. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) {
  727. /* STA is stopped. */
  728. return 0;
  729. }
  730. event = kzalloc(sizeof(struct cw1200_wsm_event), GFP_KERNEL);
  731. if (!event)
  732. return -ENOMEM;
  733. event->evt.id = WSM_GET32(buf);
  734. event->evt.data = WSM_GET32(buf);
  735. pr_debug("[WSM] Event: %d(%d)\n",
  736. event->evt.id, event->evt.data);
  737. spin_lock(&priv->event_queue_lock);
  738. first = list_empty(&priv->event_queue);
  739. list_add_tail(&event->link, &priv->event_queue);
  740. spin_unlock(&priv->event_queue_lock);
  741. if (first)
  742. queue_work(priv->workqueue, &priv->event_handler);
  743. return 0;
  744. underflow:
  745. kfree(event);
  746. return -EINVAL;
  747. }
  748. static int wsm_channel_switch_indication(struct cw1200_common *priv,
  749. struct wsm_buf *buf)
  750. {
  751. WARN_ON(WSM_GET32(buf));
  752. priv->channel_switch_in_progress = 0;
  753. wake_up(&priv->channel_switch_done);
  754. wsm_unlock_tx(priv);
  755. return 0;
  756. underflow:
  757. return -EINVAL;
  758. }
  759. static int wsm_set_pm_indication(struct cw1200_common *priv,
  760. struct wsm_buf *buf)
  761. {
  762. /* TODO: Check buf (struct wsm_set_pm_complete) for validity */
  763. if (priv->ps_mode_switch_in_progress) {
  764. priv->ps_mode_switch_in_progress = 0;
  765. wake_up(&priv->ps_mode_switch_done);
  766. }
  767. return 0;
  768. }
  769. static int wsm_scan_started(struct cw1200_common *priv, void *arg,
  770. struct wsm_buf *buf)
  771. {
  772. u32 status = WSM_GET32(buf);
  773. if (status != WSM_STATUS_SUCCESS) {
  774. cw1200_scan_failed_cb(priv);
  775. return -EINVAL;
  776. }
  777. return 0;
  778. underflow:
  779. WARN_ON(1);
  780. return -EINVAL;
  781. }
  782. static int wsm_scan_complete_indication(struct cw1200_common *priv,
  783. struct wsm_buf *buf)
  784. {
  785. struct wsm_scan_complete arg;
  786. arg.status = WSM_GET32(buf);
  787. arg.psm = WSM_GET8(buf);
  788. arg.num_channels = WSM_GET8(buf);
  789. cw1200_scan_complete_cb(priv, &arg);
  790. return 0;
  791. underflow:
  792. return -EINVAL;
  793. }
  794. static int wsm_join_complete_indication(struct cw1200_common *priv,
  795. struct wsm_buf *buf)
  796. {
  797. struct wsm_join_complete arg;
  798. arg.status = WSM_GET32(buf);
  799. pr_debug("[WSM] Join complete indication, status: %d\n", arg.status);
  800. cw1200_join_complete_cb(priv, &arg);
  801. return 0;
  802. underflow:
  803. return -EINVAL;
  804. }
  805. static int wsm_find_complete_indication(struct cw1200_common *priv,
  806. struct wsm_buf *buf)
  807. {
  808. pr_warn("Implement find_complete_indication\n");
  809. return 0;
  810. }
  811. static int wsm_ba_timeout_indication(struct cw1200_common *priv,
  812. struct wsm_buf *buf)
  813. {
  814. u8 tid;
  815. u8 addr[ETH_ALEN];
  816. WSM_GET32(buf);
  817. tid = WSM_GET8(buf);
  818. WSM_GET8(buf);
  819. WSM_GET(buf, addr, ETH_ALEN);
  820. pr_info("BlockACK timeout, tid %d, addr %pM\n",
  821. tid, addr);
  822. return 0;
  823. underflow:
  824. return -EINVAL;
  825. }
  826. static int wsm_suspend_resume_indication(struct cw1200_common *priv,
  827. int link_id, struct wsm_buf *buf)
  828. {
  829. u32 flags;
  830. struct wsm_suspend_resume arg;
  831. flags = WSM_GET32(buf);
  832. arg.link_id = link_id;
  833. arg.stop = !(flags & 1);
  834. arg.multicast = !!(flags & 8);
  835. arg.queue = (flags >> 1) & 3;
  836. cw1200_suspend_resume(priv, &arg);
  837. return 0;
  838. underflow:
  839. return -EINVAL;
  840. }
  841. /* ******************************************************************** */
  842. /* WSM TX */
  843. static int wsm_cmd_send(struct cw1200_common *priv,
  844. struct wsm_buf *buf,
  845. void *arg, u16 cmd, long tmo)
  846. {
  847. size_t buf_len = buf->data - buf->begin;
  848. int ret;
  849. /* Don't bother if we're dead. */
  850. if (priv->bh_error) {
  851. ret = 0;
  852. goto done;
  853. }
  854. /* Block until the cmd buffer is completed. Tortuous. */
  855. spin_lock(&priv->wsm_cmd.lock);
  856. while (!priv->wsm_cmd.done) {
  857. spin_unlock(&priv->wsm_cmd.lock);
  858. spin_lock(&priv->wsm_cmd.lock);
  859. }
  860. priv->wsm_cmd.done = 0;
  861. spin_unlock(&priv->wsm_cmd.lock);
  862. if (cmd == WSM_WRITE_MIB_REQ_ID ||
  863. cmd == WSM_READ_MIB_REQ_ID)
  864. pr_debug("[WSM] >>> 0x%.4X [MIB: 0x%.4X] (%zu)\n",
  865. cmd, __le16_to_cpu(((__le16 *)buf->begin)[2]),
  866. buf_len);
  867. else
  868. pr_debug("[WSM] >>> 0x%.4X (%zu)\n", cmd, buf_len);
  869. /* Due to buggy SPI on CW1200, we need to
  870. * pad the message by a few bytes to ensure
  871. * that it's completely received.
  872. */
  873. buf_len += 4;
  874. /* Fill HI message header */
  875. /* BH will add sequence number */
  876. ((__le16 *)buf->begin)[0] = __cpu_to_le16(buf_len);
  877. ((__le16 *)buf->begin)[1] = __cpu_to_le16(cmd);
  878. spin_lock(&priv->wsm_cmd.lock);
  879. BUG_ON(priv->wsm_cmd.ptr);
  880. priv->wsm_cmd.ptr = buf->begin;
  881. priv->wsm_cmd.len = buf_len;
  882. priv->wsm_cmd.arg = arg;
  883. priv->wsm_cmd.cmd = cmd;
  884. spin_unlock(&priv->wsm_cmd.lock);
  885. cw1200_bh_wakeup(priv);
  886. /* Wait for command completion */
  887. ret = wait_event_timeout(priv->wsm_cmd_wq,
  888. priv->wsm_cmd.done, tmo);
  889. if (!ret && !priv->wsm_cmd.done) {
  890. spin_lock(&priv->wsm_cmd.lock);
  891. priv->wsm_cmd.done = 1;
  892. priv->wsm_cmd.ptr = NULL;
  893. spin_unlock(&priv->wsm_cmd.lock);
  894. if (priv->bh_error) {
  895. /* Return ok to help system cleanup */
  896. ret = 0;
  897. } else {
  898. pr_err("CMD req (0x%04x) stuck in firmware, killing BH\n", priv->wsm_cmd.cmd);
  899. print_hex_dump_bytes("REQDUMP: ", DUMP_PREFIX_NONE,
  900. buf->begin, buf_len);
  901. pr_err("Outstanding outgoing frames: %d\n", priv->hw_bufs_used);
  902. /* Kill BH thread to report the error to the top layer. */
  903. atomic_inc(&priv->bh_term);
  904. wake_up(&priv->bh_wq);
  905. ret = -ETIMEDOUT;
  906. }
  907. } else {
  908. spin_lock(&priv->wsm_cmd.lock);
  909. BUG_ON(!priv->wsm_cmd.done);
  910. ret = priv->wsm_cmd.ret;
  911. spin_unlock(&priv->wsm_cmd.lock);
  912. }
  913. done:
  914. wsm_buf_reset(buf);
  915. return ret;
  916. }
  917. /* ******************************************************************** */
  918. /* WSM TX port control */
  919. void wsm_lock_tx(struct cw1200_common *priv)
  920. {
  921. wsm_cmd_lock(priv);
  922. if (atomic_inc_return(&priv->tx_lock) == 1) {
  923. if (wsm_flush_tx(priv))
  924. pr_debug("[WSM] TX is locked.\n");
  925. }
  926. wsm_cmd_unlock(priv);
  927. }
  928. void wsm_lock_tx_async(struct cw1200_common *priv)
  929. {
  930. if (atomic_inc_return(&priv->tx_lock) == 1)
  931. pr_debug("[WSM] TX is locked (async).\n");
  932. }
  933. bool wsm_flush_tx(struct cw1200_common *priv)
  934. {
  935. unsigned long timestamp = jiffies;
  936. bool pending = false;
  937. long timeout;
  938. int i;
  939. /* Flush must be called with TX lock held. */
  940. BUG_ON(!atomic_read(&priv->tx_lock));
  941. /* First check if we really need to do something.
  942. * It is safe to use unprotected access, as hw_bufs_used
  943. * can only decrements.
  944. */
  945. if (!priv->hw_bufs_used)
  946. return true;
  947. if (priv->bh_error) {
  948. /* In case of failure do not wait for magic. */
  949. pr_err("[WSM] Fatal error occurred, will not flush TX.\n");
  950. return false;
  951. } else {
  952. /* Get a timestamp of "oldest" frame */
  953. for (i = 0; i < 4; ++i)
  954. pending |= cw1200_queue_get_xmit_timestamp(
  955. &priv->tx_queue[i],
  956. &timestamp, 0xffffffff);
  957. /* If there's nothing pending, we're good */
  958. if (!pending)
  959. return true;
  960. timeout = timestamp + WSM_CMD_LAST_CHANCE_TIMEOUT - jiffies;
  961. if (timeout < 0 || wait_event_timeout(priv->bh_evt_wq,
  962. !priv->hw_bufs_used,
  963. timeout) <= 0) {
  964. /* Hmmm... Not good. Frame had stuck in firmware. */
  965. priv->bh_error = 1;
  966. wiphy_err(priv->hw->wiphy, "[WSM] TX Frames (%d) stuck in firmware, killing BH\n", priv->hw_bufs_used);
  967. wake_up(&priv->bh_wq);
  968. return false;
  969. }
  970. /* Ok, everything is flushed. */
  971. return true;
  972. }
  973. }
  974. void wsm_unlock_tx(struct cw1200_common *priv)
  975. {
  976. int tx_lock;
  977. tx_lock = atomic_dec_return(&priv->tx_lock);
  978. BUG_ON(tx_lock < 0);
  979. if (tx_lock == 0) {
  980. if (!priv->bh_error)
  981. cw1200_bh_wakeup(priv);
  982. pr_debug("[WSM] TX is unlocked.\n");
  983. }
  984. }
  985. /* ******************************************************************** */
  986. /* WSM RX */
  987. int wsm_handle_exception(struct cw1200_common *priv, u8 *data, size_t len)
  988. {
  989. struct wsm_buf buf;
  990. u32 reason;
  991. u32 reg[18];
  992. char fname[48];
  993. unsigned int i;
  994. static const char * const reason_str[] = {
  995. "undefined instruction",
  996. "prefetch abort",
  997. "data abort",
  998. "unknown error",
  999. };
  1000. buf.begin = buf.data = data;
  1001. buf.end = &buf.begin[len];
  1002. reason = WSM_GET32(&buf);
  1003. for (i = 0; i < ARRAY_SIZE(reg); ++i)
  1004. reg[i] = WSM_GET32(&buf);
  1005. WSM_GET(&buf, fname, sizeof(fname));
  1006. if (reason < 4)
  1007. wiphy_err(priv->hw->wiphy,
  1008. "Firmware exception: %s.\n",
  1009. reason_str[reason]);
  1010. else
  1011. wiphy_err(priv->hw->wiphy,
  1012. "Firmware assert at %.*s, line %d\n",
  1013. (int) sizeof(fname), fname, reg[1]);
  1014. for (i = 0; i < 12; i += 4)
  1015. wiphy_err(priv->hw->wiphy,
  1016. "R%d: 0x%.8X, R%d: 0x%.8X, R%d: 0x%.8X, R%d: 0x%.8X,\n",
  1017. i + 0, reg[i + 0], i + 1, reg[i + 1],
  1018. i + 2, reg[i + 2], i + 3, reg[i + 3]);
  1019. wiphy_err(priv->hw->wiphy,
  1020. "R12: 0x%.8X, SP: 0x%.8X, LR: 0x%.8X, PC: 0x%.8X,\n",
  1021. reg[i + 0], reg[i + 1], reg[i + 2], reg[i + 3]);
  1022. i += 4;
  1023. wiphy_err(priv->hw->wiphy,
  1024. "CPSR: 0x%.8X, SPSR: 0x%.8X\n",
  1025. reg[i + 0], reg[i + 1]);
  1026. print_hex_dump_bytes("R1: ", DUMP_PREFIX_NONE,
  1027. fname, sizeof(fname));
  1028. return 0;
  1029. underflow:
  1030. wiphy_err(priv->hw->wiphy, "Firmware exception.\n");
  1031. print_hex_dump_bytes("Exception: ", DUMP_PREFIX_NONE,
  1032. data, len);
  1033. return -EINVAL;
  1034. }
  1035. int wsm_handle_rx(struct cw1200_common *priv, u16 id,
  1036. struct wsm_hdr *wsm, struct sk_buff **skb_p)
  1037. {
  1038. int ret = 0;
  1039. struct wsm_buf wsm_buf;
  1040. int link_id = (id >> 6) & 0x0F;
  1041. /* Strip link id. */
  1042. id &= ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX);
  1043. wsm_buf.begin = (u8 *)&wsm[0];
  1044. wsm_buf.data = (u8 *)&wsm[1];
  1045. wsm_buf.end = &wsm_buf.begin[__le16_to_cpu(wsm->len)];
  1046. pr_debug("[WSM] <<< 0x%.4X (%td)\n", id,
  1047. wsm_buf.end - wsm_buf.begin);
  1048. if (id == WSM_TX_CONFIRM_IND_ID) {
  1049. ret = wsm_tx_confirm(priv, &wsm_buf, link_id);
  1050. } else if (id == WSM_MULTI_TX_CONFIRM_ID) {
  1051. ret = wsm_multi_tx_confirm(priv, &wsm_buf, link_id);
  1052. } else if (id & 0x0400) {
  1053. void *wsm_arg;
  1054. u16 wsm_cmd;
  1055. /* Do not trust FW too much. Protection against repeated
  1056. * response and race condition removal (see above).
  1057. */
  1058. spin_lock(&priv->wsm_cmd.lock);
  1059. wsm_arg = priv->wsm_cmd.arg;
  1060. wsm_cmd = priv->wsm_cmd.cmd &
  1061. ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX);
  1062. priv->wsm_cmd.cmd = 0xFFFF;
  1063. spin_unlock(&priv->wsm_cmd.lock);
  1064. if (WARN_ON((id & ~0x0400) != wsm_cmd)) {
  1065. /* Note that any non-zero is a fatal retcode. */
  1066. ret = -EINVAL;
  1067. goto out;
  1068. }
  1069. /* Note that wsm_arg can be NULL in case of timeout in
  1070. * wsm_cmd_send().
  1071. */
  1072. switch (id) {
  1073. case WSM_READ_MIB_RESP_ID:
  1074. if (wsm_arg)
  1075. ret = wsm_read_mib_confirm(priv, wsm_arg,
  1076. &wsm_buf);
  1077. break;
  1078. case WSM_WRITE_MIB_RESP_ID:
  1079. if (wsm_arg)
  1080. ret = wsm_write_mib_confirm(priv, wsm_arg,
  1081. &wsm_buf);
  1082. break;
  1083. case WSM_START_SCAN_RESP_ID:
  1084. if (wsm_arg)
  1085. ret = wsm_scan_started(priv, wsm_arg, &wsm_buf);
  1086. break;
  1087. case WSM_CONFIGURATION_RESP_ID:
  1088. if (wsm_arg)
  1089. ret = wsm_configuration_confirm(priv, wsm_arg,
  1090. &wsm_buf);
  1091. break;
  1092. case WSM_JOIN_RESP_ID:
  1093. if (wsm_arg)
  1094. ret = wsm_join_confirm(priv, wsm_arg, &wsm_buf);
  1095. break;
  1096. case WSM_STOP_SCAN_RESP_ID:
  1097. case WSM_RESET_RESP_ID:
  1098. case WSM_ADD_KEY_RESP_ID:
  1099. case WSM_REMOVE_KEY_RESP_ID:
  1100. case WSM_SET_PM_RESP_ID:
  1101. case WSM_SET_BSS_PARAMS_RESP_ID:
  1102. case 0x0412: /* set_tx_queue_params */
  1103. case WSM_EDCA_PARAMS_RESP_ID:
  1104. case WSM_SWITCH_CHANNEL_RESP_ID:
  1105. case WSM_START_RESP_ID:
  1106. case WSM_BEACON_TRANSMIT_RESP_ID:
  1107. case 0x0419: /* start_find */
  1108. case 0x041A: /* stop_find */
  1109. case 0x041B: /* update_ie */
  1110. case 0x041C: /* map_link */
  1111. WARN_ON(wsm_arg != NULL);
  1112. ret = wsm_generic_confirm(priv, wsm_arg, &wsm_buf);
  1113. if (ret) {
  1114. wiphy_warn(priv->hw->wiphy,
  1115. "wsm_generic_confirm failed for request 0x%04x.\n",
  1116. id & ~0x0400);
  1117. /* often 0x407 and 0x410 occur, this means we're dead.. */
  1118. if (priv->join_status >= CW1200_JOIN_STATUS_JOINING) {
  1119. wsm_lock_tx(priv);
  1120. if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
  1121. wsm_unlock_tx(priv);
  1122. }
  1123. }
  1124. break;
  1125. default:
  1126. wiphy_warn(priv->hw->wiphy,
  1127. "Unrecognized confirmation 0x%04x\n",
  1128. id & ~0x0400);
  1129. }
  1130. spin_lock(&priv->wsm_cmd.lock);
  1131. priv->wsm_cmd.ret = ret;
  1132. priv->wsm_cmd.done = 1;
  1133. spin_unlock(&priv->wsm_cmd.lock);
  1134. ret = 0; /* Error response from device should ne stop BH. */
  1135. wake_up(&priv->wsm_cmd_wq);
  1136. } else if (id & 0x0800) {
  1137. switch (id) {
  1138. case WSM_STARTUP_IND_ID:
  1139. ret = wsm_startup_indication(priv, &wsm_buf);
  1140. break;
  1141. case WSM_RECEIVE_IND_ID:
  1142. ret = wsm_receive_indication(priv, link_id,
  1143. &wsm_buf, skb_p);
  1144. break;
  1145. case 0x0805:
  1146. ret = wsm_event_indication(priv, &wsm_buf);
  1147. break;
  1148. case WSM_SCAN_COMPLETE_IND_ID:
  1149. ret = wsm_scan_complete_indication(priv, &wsm_buf);
  1150. break;
  1151. case 0x0808:
  1152. ret = wsm_ba_timeout_indication(priv, &wsm_buf);
  1153. break;
  1154. case 0x0809:
  1155. ret = wsm_set_pm_indication(priv, &wsm_buf);
  1156. break;
  1157. case 0x080A:
  1158. ret = wsm_channel_switch_indication(priv, &wsm_buf);
  1159. break;
  1160. case 0x080B:
  1161. ret = wsm_find_complete_indication(priv, &wsm_buf);
  1162. break;
  1163. case 0x080C:
  1164. ret = wsm_suspend_resume_indication(priv,
  1165. link_id, &wsm_buf);
  1166. break;
  1167. case 0x080F:
  1168. ret = wsm_join_complete_indication(priv, &wsm_buf);
  1169. break;
  1170. default:
  1171. pr_warn("Unrecognised WSM ID %04x\n", id);
  1172. }
  1173. } else {
  1174. WARN_ON(1);
  1175. ret = -EINVAL;
  1176. }
  1177. out:
  1178. return ret;
  1179. }
  1180. static bool wsm_handle_tx_data(struct cw1200_common *priv,
  1181. struct wsm_tx *wsm,
  1182. const struct ieee80211_tx_info *tx_info,
  1183. const struct cw1200_txpriv *txpriv,
  1184. struct cw1200_queue *queue)
  1185. {
  1186. bool handled = false;
  1187. const struct ieee80211_hdr *frame =
  1188. (struct ieee80211_hdr *)&((u8 *)wsm)[txpriv->offset];
  1189. __le16 fctl = frame->frame_control;
  1190. enum {
  1191. do_probe,
  1192. do_drop,
  1193. do_wep,
  1194. do_tx,
  1195. } action = do_tx;
  1196. switch (priv->mode) {
  1197. case NL80211_IFTYPE_STATION:
  1198. if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
  1199. action = do_tx;
  1200. else if (priv->join_status < CW1200_JOIN_STATUS_PRE_STA)
  1201. action = do_drop;
  1202. break;
  1203. case NL80211_IFTYPE_AP:
  1204. if (!priv->join_status) {
  1205. action = do_drop;
  1206. } else if (!(BIT(txpriv->raw_link_id) &
  1207. (BIT(0) | priv->link_id_map))) {
  1208. wiphy_warn(priv->hw->wiphy,
  1209. "A frame with expired link id is dropped.\n");
  1210. action = do_drop;
  1211. }
  1212. if (cw1200_queue_get_generation(wsm->packet_id) >
  1213. CW1200_MAX_REQUEUE_ATTEMPTS) {
  1214. /* HACK!!! WSM324 firmware has tendency to requeue
  1215. * multicast frames in a loop, causing performance
  1216. * drop and high power consumption of the driver.
  1217. * In this situation it is better just to drop
  1218. * the problematic frame.
  1219. */
  1220. wiphy_warn(priv->hw->wiphy,
  1221. "Too many attempts to requeue a frame; dropped.\n");
  1222. action = do_drop;
  1223. }
  1224. break;
  1225. case NL80211_IFTYPE_ADHOC:
  1226. if (priv->join_status != CW1200_JOIN_STATUS_IBSS)
  1227. action = do_drop;
  1228. break;
  1229. case NL80211_IFTYPE_MESH_POINT:
  1230. action = do_tx; /* TODO: Test me! */
  1231. break;
  1232. case NL80211_IFTYPE_MONITOR:
  1233. default:
  1234. action = do_drop;
  1235. break;
  1236. }
  1237. if (action == do_tx) {
  1238. if (ieee80211_is_nullfunc(fctl)) {
  1239. spin_lock(&priv->bss_loss_lock);
  1240. if (priv->bss_loss_state) {
  1241. priv->bss_loss_confirm_id = wsm->packet_id;
  1242. wsm->queue_id = WSM_QUEUE_VOICE;
  1243. }
  1244. spin_unlock(&priv->bss_loss_lock);
  1245. } else if (ieee80211_is_probe_req(fctl)) {
  1246. action = do_probe;
  1247. } else if (ieee80211_is_deauth(fctl) &&
  1248. priv->mode != NL80211_IFTYPE_AP) {
  1249. pr_debug("[WSM] Issue unjoin command due to tx deauth.\n");
  1250. wsm_lock_tx_async(priv);
  1251. if (queue_work(priv->workqueue,
  1252. &priv->unjoin_work) <= 0)
  1253. wsm_unlock_tx(priv);
  1254. } else if (ieee80211_has_protected(fctl) &&
  1255. tx_info->control.hw_key &&
  1256. tx_info->control.hw_key->keyidx != priv->wep_default_key_id &&
  1257. (tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  1258. tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_WEP104)) {
  1259. action = do_wep;
  1260. }
  1261. }
  1262. switch (action) {
  1263. case do_probe:
  1264. /* An interesting FW "feature". Device filters probe responses.
  1265. * The easiest way to get it back is to convert
  1266. * probe request into WSM start_scan command.
  1267. */
  1268. pr_debug("[WSM] Convert probe request to scan.\n");
  1269. wsm_lock_tx_async(priv);
  1270. priv->pending_frame_id = wsm->packet_id;
  1271. if (queue_delayed_work(priv->workqueue,
  1272. &priv->scan.probe_work, 0) <= 0)
  1273. wsm_unlock_tx(priv);
  1274. handled = true;
  1275. break;
  1276. case do_drop:
  1277. pr_debug("[WSM] Drop frame (0x%.4X).\n", fctl);
  1278. BUG_ON(cw1200_queue_remove(queue, wsm->packet_id));
  1279. handled = true;
  1280. break;
  1281. case do_wep:
  1282. pr_debug("[WSM] Issue set_default_wep_key.\n");
  1283. wsm_lock_tx_async(priv);
  1284. priv->wep_default_key_id = tx_info->control.hw_key->keyidx;
  1285. priv->pending_frame_id = wsm->packet_id;
  1286. if (queue_work(priv->workqueue, &priv->wep_key_work) <= 0)
  1287. wsm_unlock_tx(priv);
  1288. handled = true;
  1289. break;
  1290. case do_tx:
  1291. pr_debug("[WSM] Transmit frame.\n");
  1292. break;
  1293. default:
  1294. /* Do nothing */
  1295. break;
  1296. }
  1297. return handled;
  1298. }
  1299. static int cw1200_get_prio_queue(struct cw1200_common *priv,
  1300. u32 link_id_map, int *total)
  1301. {
  1302. static const int urgent = BIT(CW1200_LINK_ID_AFTER_DTIM) |
  1303. BIT(CW1200_LINK_ID_UAPSD);
  1304. struct wsm_edca_queue_params *edca;
  1305. unsigned score, best = -1;
  1306. int winner = -1;
  1307. int queued;
  1308. int i;
  1309. /* search for a winner using edca params */
  1310. for (i = 0; i < 4; ++i) {
  1311. queued = cw1200_queue_get_num_queued(&priv->tx_queue[i],
  1312. link_id_map);
  1313. if (!queued)
  1314. continue;
  1315. *total += queued;
  1316. edca = &priv->edca.params[i];
  1317. score = ((edca->aifns + edca->cwmin) << 16) +
  1318. ((edca->cwmax - edca->cwmin) *
  1319. get_random_u16());
  1320. if (score < best && (winner < 0 || i != 3)) {
  1321. best = score;
  1322. winner = i;
  1323. }
  1324. }
  1325. /* override winner if bursting */
  1326. if (winner >= 0 && priv->tx_burst_idx >= 0 &&
  1327. winner != priv->tx_burst_idx &&
  1328. !cw1200_queue_get_num_queued(
  1329. &priv->tx_queue[winner],
  1330. link_id_map & urgent) &&
  1331. cw1200_queue_get_num_queued(
  1332. &priv->tx_queue[priv->tx_burst_idx],
  1333. link_id_map))
  1334. winner = priv->tx_burst_idx;
  1335. return winner;
  1336. }
  1337. static int wsm_get_tx_queue_and_mask(struct cw1200_common *priv,
  1338. struct cw1200_queue **queue_p,
  1339. u32 *tx_allowed_mask_p,
  1340. bool *more)
  1341. {
  1342. int idx;
  1343. u32 tx_allowed_mask;
  1344. int total = 0;
  1345. /* Search for a queue with multicast frames buffered */
  1346. if (priv->tx_multicast) {
  1347. tx_allowed_mask = BIT(CW1200_LINK_ID_AFTER_DTIM);
  1348. idx = cw1200_get_prio_queue(priv,
  1349. tx_allowed_mask, &total);
  1350. if (idx >= 0) {
  1351. *more = total > 1;
  1352. goto found;
  1353. }
  1354. }
  1355. /* Search for unicast traffic */
  1356. tx_allowed_mask = ~priv->sta_asleep_mask;
  1357. tx_allowed_mask |= BIT(CW1200_LINK_ID_UAPSD);
  1358. if (priv->sta_asleep_mask) {
  1359. tx_allowed_mask |= priv->pspoll_mask;
  1360. tx_allowed_mask &= ~BIT(CW1200_LINK_ID_AFTER_DTIM);
  1361. } else {
  1362. tx_allowed_mask |= BIT(CW1200_LINK_ID_AFTER_DTIM);
  1363. }
  1364. idx = cw1200_get_prio_queue(priv,
  1365. tx_allowed_mask, &total);
  1366. if (idx < 0)
  1367. return -ENOENT;
  1368. found:
  1369. *queue_p = &priv->tx_queue[idx];
  1370. *tx_allowed_mask_p = tx_allowed_mask;
  1371. return 0;
  1372. }
  1373. int wsm_get_tx(struct cw1200_common *priv, u8 **data,
  1374. size_t *tx_len, int *burst)
  1375. {
  1376. struct wsm_tx *wsm = NULL;
  1377. struct ieee80211_tx_info *tx_info;
  1378. struct cw1200_queue *queue = NULL;
  1379. int queue_num;
  1380. u32 tx_allowed_mask = 0;
  1381. const struct cw1200_txpriv *txpriv = NULL;
  1382. int count = 0;
  1383. /* More is used only for broadcasts. */
  1384. bool more = false;
  1385. if (priv->wsm_cmd.ptr) { /* CMD request */
  1386. ++count;
  1387. spin_lock(&priv->wsm_cmd.lock);
  1388. BUG_ON(!priv->wsm_cmd.ptr);
  1389. *data = priv->wsm_cmd.ptr;
  1390. *tx_len = priv->wsm_cmd.len;
  1391. *burst = 1;
  1392. spin_unlock(&priv->wsm_cmd.lock);
  1393. } else {
  1394. for (;;) {
  1395. int ret;
  1396. if (atomic_add_return(0, &priv->tx_lock))
  1397. break;
  1398. spin_lock_bh(&priv->ps_state_lock);
  1399. ret = wsm_get_tx_queue_and_mask(priv, &queue,
  1400. &tx_allowed_mask, &more);
  1401. queue_num = queue - priv->tx_queue;
  1402. if (priv->buffered_multicasts &&
  1403. (ret || !more) &&
  1404. (priv->tx_multicast || !priv->sta_asleep_mask)) {
  1405. priv->buffered_multicasts = false;
  1406. if (priv->tx_multicast) {
  1407. priv->tx_multicast = false;
  1408. queue_work(priv->workqueue,
  1409. &priv->multicast_stop_work);
  1410. }
  1411. }
  1412. spin_unlock_bh(&priv->ps_state_lock);
  1413. if (ret)
  1414. break;
  1415. if (cw1200_queue_get(queue,
  1416. tx_allowed_mask,
  1417. &wsm, &tx_info, &txpriv))
  1418. continue;
  1419. if (wsm_handle_tx_data(priv, wsm,
  1420. tx_info, txpriv, queue))
  1421. continue; /* Handled by WSM */
  1422. wsm->hdr.id &= __cpu_to_le16(
  1423. ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX));
  1424. wsm->hdr.id |= cpu_to_le16(
  1425. WSM_TX_LINK_ID(txpriv->raw_link_id));
  1426. priv->pspoll_mask &= ~BIT(txpriv->raw_link_id);
  1427. *data = (u8 *)wsm;
  1428. *tx_len = __le16_to_cpu(wsm->hdr.len);
  1429. /* allow bursting if txop is set */
  1430. if (priv->edca.params[queue_num].txop_limit)
  1431. *burst = min(*burst,
  1432. (int)cw1200_queue_get_num_queued(queue, tx_allowed_mask) + 1);
  1433. else
  1434. *burst = 1;
  1435. /* store index of bursting queue */
  1436. if (*burst > 1)
  1437. priv->tx_burst_idx = queue_num;
  1438. else
  1439. priv->tx_burst_idx = -1;
  1440. if (more) {
  1441. struct ieee80211_hdr *hdr =
  1442. (struct ieee80211_hdr *)
  1443. &((u8 *)wsm)[txpriv->offset];
  1444. /* more buffered multicast/broadcast frames
  1445. * ==> set MoreData flag in IEEE 802.11 header
  1446. * to inform PS STAs
  1447. */
  1448. hdr->frame_control |=
  1449. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  1450. }
  1451. pr_debug("[WSM] >>> 0x%.4X (%zu) %p %c\n",
  1452. 0x0004, *tx_len, *data,
  1453. wsm->more ? 'M' : ' ');
  1454. ++count;
  1455. break;
  1456. }
  1457. }
  1458. return count;
  1459. }
  1460. void wsm_txed(struct cw1200_common *priv, u8 *data)
  1461. {
  1462. if (data == priv->wsm_cmd.ptr) {
  1463. spin_lock(&priv->wsm_cmd.lock);
  1464. priv->wsm_cmd.ptr = NULL;
  1465. spin_unlock(&priv->wsm_cmd.lock);
  1466. }
  1467. }
  1468. /* ******************************************************************** */
  1469. /* WSM buffer */
  1470. void wsm_buf_init(struct wsm_buf *buf)
  1471. {
  1472. BUG_ON(buf->begin);
  1473. buf->begin = kmalloc(FWLOAD_BLOCK_SIZE, GFP_KERNEL | GFP_DMA);
  1474. buf->end = buf->begin ? &buf->begin[FWLOAD_BLOCK_SIZE] : buf->begin;
  1475. wsm_buf_reset(buf);
  1476. }
  1477. void wsm_buf_deinit(struct wsm_buf *buf)
  1478. {
  1479. kfree(buf->begin);
  1480. buf->begin = buf->data = buf->end = NULL;
  1481. }
  1482. static void wsm_buf_reset(struct wsm_buf *buf)
  1483. {
  1484. if (buf->begin) {
  1485. buf->data = &buf->begin[4];
  1486. *(u32 *)buf->begin = 0;
  1487. } else {
  1488. buf->data = buf->begin;
  1489. }
  1490. }
  1491. static int wsm_buf_reserve(struct wsm_buf *buf, size_t extra_size)
  1492. {
  1493. size_t pos = buf->data - buf->begin;
  1494. size_t size = pos + extra_size;
  1495. u8 *tmp;
  1496. size = round_up(size, FWLOAD_BLOCK_SIZE);
  1497. tmp = krealloc(buf->begin, size, GFP_KERNEL | GFP_DMA);
  1498. if (!tmp) {
  1499. wsm_buf_deinit(buf);
  1500. return -ENOMEM;
  1501. }
  1502. buf->begin = tmp;
  1503. buf->data = &buf->begin[pos];
  1504. buf->end = &buf->begin[size];
  1505. return 0;
  1506. }