msgbuf.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. /* Copyright (c) 2014 Broadcom Corporation
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. /*******************************************************************************
  16. * Communicates with the dongle by using dcmd codes.
  17. * For certain dcmd codes, the dongle interprets string data from the host.
  18. ******************************************************************************/
  19. #include <linux/types.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/etherdevice.h>
  22. #include <brcmu_utils.h>
  23. #include <brcmu_wifi.h>
  24. #include "core.h"
  25. #include "debug.h"
  26. #include "proto.h"
  27. #include "msgbuf.h"
  28. #include "commonring.h"
  29. #include "flowring.h"
  30. #include "bus.h"
  31. #include "tracepoint.h"
  32. #define MSGBUF_IOCTL_RESP_TIMEOUT msecs_to_jiffies(2000)
  33. #define MSGBUF_TYPE_GEN_STATUS 0x1
  34. #define MSGBUF_TYPE_RING_STATUS 0x2
  35. #define MSGBUF_TYPE_FLOW_RING_CREATE 0x3
  36. #define MSGBUF_TYPE_FLOW_RING_CREATE_CMPLT 0x4
  37. #define MSGBUF_TYPE_FLOW_RING_DELETE 0x5
  38. #define MSGBUF_TYPE_FLOW_RING_DELETE_CMPLT 0x6
  39. #define MSGBUF_TYPE_FLOW_RING_FLUSH 0x7
  40. #define MSGBUF_TYPE_FLOW_RING_FLUSH_CMPLT 0x8
  41. #define MSGBUF_TYPE_IOCTLPTR_REQ 0x9
  42. #define MSGBUF_TYPE_IOCTLPTR_REQ_ACK 0xA
  43. #define MSGBUF_TYPE_IOCTLRESP_BUF_POST 0xB
  44. #define MSGBUF_TYPE_IOCTL_CMPLT 0xC
  45. #define MSGBUF_TYPE_EVENT_BUF_POST 0xD
  46. #define MSGBUF_TYPE_WL_EVENT 0xE
  47. #define MSGBUF_TYPE_TX_POST 0xF
  48. #define MSGBUF_TYPE_TX_STATUS 0x10
  49. #define MSGBUF_TYPE_RXBUF_POST 0x11
  50. #define MSGBUF_TYPE_RX_CMPLT 0x12
  51. #define MSGBUF_TYPE_LPBK_DMAXFER 0x13
  52. #define MSGBUF_TYPE_LPBK_DMAXFER_CMPLT 0x14
  53. #define NR_TX_PKTIDS 2048
  54. #define NR_RX_PKTIDS 1024
  55. #define BRCMF_IOCTL_REQ_PKTID 0xFFFE
  56. #define BRCMF_MSGBUF_MAX_PKT_SIZE 2048
  57. #define BRCMF_MSGBUF_RXBUFPOST_THRESHOLD 32
  58. #define BRCMF_MSGBUF_MAX_IOCTLRESPBUF_POST 8
  59. #define BRCMF_MSGBUF_MAX_EVENTBUF_POST 8
  60. #define BRCMF_MSGBUF_PKT_FLAGS_FRAME_802_3 0x01
  61. #define BRCMF_MSGBUF_PKT_FLAGS_FRAME_802_11 0x02
  62. #define BRCMF_MSGBUF_PKT_FLAGS_FRAME_MASK 0x07
  63. #define BRCMF_MSGBUF_PKT_FLAGS_PRIO_SHIFT 5
  64. #define BRCMF_MSGBUF_TX_FLUSH_CNT1 32
  65. #define BRCMF_MSGBUF_TX_FLUSH_CNT2 96
  66. #define BRCMF_MSGBUF_DELAY_TXWORKER_THRS 96
  67. #define BRCMF_MSGBUF_TRICKLE_TXWORKER_THRS 32
  68. #define BRCMF_MSGBUF_UPDATE_RX_PTR_THRS 48
  69. struct msgbuf_common_hdr {
  70. u8 msgtype;
  71. u8 ifidx;
  72. u8 flags;
  73. u8 rsvd0;
  74. __le32 request_id;
  75. };
  76. struct msgbuf_ioctl_req_hdr {
  77. struct msgbuf_common_hdr msg;
  78. __le32 cmd;
  79. __le16 trans_id;
  80. __le16 input_buf_len;
  81. __le16 output_buf_len;
  82. __le16 rsvd0[3];
  83. struct msgbuf_buf_addr req_buf_addr;
  84. __le32 rsvd1[2];
  85. };
  86. struct msgbuf_tx_msghdr {
  87. struct msgbuf_common_hdr msg;
  88. u8 txhdr[ETH_HLEN];
  89. u8 flags;
  90. u8 seg_cnt;
  91. struct msgbuf_buf_addr metadata_buf_addr;
  92. struct msgbuf_buf_addr data_buf_addr;
  93. __le16 metadata_buf_len;
  94. __le16 data_len;
  95. __le32 rsvd0;
  96. };
  97. struct msgbuf_rx_bufpost {
  98. struct msgbuf_common_hdr msg;
  99. __le16 metadata_buf_len;
  100. __le16 data_buf_len;
  101. __le32 rsvd0;
  102. struct msgbuf_buf_addr metadata_buf_addr;
  103. struct msgbuf_buf_addr data_buf_addr;
  104. };
  105. struct msgbuf_rx_ioctl_resp_or_event {
  106. struct msgbuf_common_hdr msg;
  107. __le16 host_buf_len;
  108. __le16 rsvd0[3];
  109. struct msgbuf_buf_addr host_buf_addr;
  110. __le32 rsvd1[4];
  111. };
  112. struct msgbuf_completion_hdr {
  113. __le16 status;
  114. __le16 flow_ring_id;
  115. };
  116. struct msgbuf_rx_event {
  117. struct msgbuf_common_hdr msg;
  118. struct msgbuf_completion_hdr compl_hdr;
  119. __le16 event_data_len;
  120. __le16 seqnum;
  121. __le16 rsvd0[4];
  122. };
  123. struct msgbuf_ioctl_resp_hdr {
  124. struct msgbuf_common_hdr msg;
  125. struct msgbuf_completion_hdr compl_hdr;
  126. __le16 resp_len;
  127. __le16 trans_id;
  128. __le32 cmd;
  129. __le32 rsvd0;
  130. };
  131. struct msgbuf_tx_status {
  132. struct msgbuf_common_hdr msg;
  133. struct msgbuf_completion_hdr compl_hdr;
  134. __le16 metadata_len;
  135. __le16 tx_status;
  136. };
  137. struct msgbuf_rx_complete {
  138. struct msgbuf_common_hdr msg;
  139. struct msgbuf_completion_hdr compl_hdr;
  140. __le16 metadata_len;
  141. __le16 data_len;
  142. __le16 data_offset;
  143. __le16 flags;
  144. __le32 rx_status_0;
  145. __le32 rx_status_1;
  146. __le32 rsvd0;
  147. };
  148. struct msgbuf_tx_flowring_create_req {
  149. struct msgbuf_common_hdr msg;
  150. u8 da[ETH_ALEN];
  151. u8 sa[ETH_ALEN];
  152. u8 tid;
  153. u8 if_flags;
  154. __le16 flow_ring_id;
  155. u8 tc;
  156. u8 priority;
  157. __le16 int_vector;
  158. __le16 max_items;
  159. __le16 len_item;
  160. struct msgbuf_buf_addr flow_ring_addr;
  161. };
  162. struct msgbuf_tx_flowring_delete_req {
  163. struct msgbuf_common_hdr msg;
  164. __le16 flow_ring_id;
  165. __le16 reason;
  166. __le32 rsvd0[7];
  167. };
  168. struct msgbuf_flowring_create_resp {
  169. struct msgbuf_common_hdr msg;
  170. struct msgbuf_completion_hdr compl_hdr;
  171. __le32 rsvd0[3];
  172. };
  173. struct msgbuf_flowring_delete_resp {
  174. struct msgbuf_common_hdr msg;
  175. struct msgbuf_completion_hdr compl_hdr;
  176. __le32 rsvd0[3];
  177. };
  178. struct msgbuf_flowring_flush_resp {
  179. struct msgbuf_common_hdr msg;
  180. struct msgbuf_completion_hdr compl_hdr;
  181. __le32 rsvd0[3];
  182. };
  183. struct brcmf_msgbuf_work_item {
  184. struct list_head queue;
  185. u32 flowid;
  186. int ifidx;
  187. u8 sa[ETH_ALEN];
  188. u8 da[ETH_ALEN];
  189. };
  190. struct brcmf_msgbuf {
  191. struct brcmf_pub *drvr;
  192. struct brcmf_commonring **commonrings;
  193. struct brcmf_commonring **flowrings;
  194. dma_addr_t *flowring_dma_handle;
  195. u16 max_flowrings;
  196. u16 max_submissionrings;
  197. u16 max_completionrings;
  198. u16 rx_dataoffset;
  199. u32 max_rxbufpost;
  200. u16 rx_metadata_offset;
  201. u32 rxbufpost;
  202. u32 max_ioctlrespbuf;
  203. u32 cur_ioctlrespbuf;
  204. u32 max_eventbuf;
  205. u32 cur_eventbuf;
  206. void *ioctbuf;
  207. dma_addr_t ioctbuf_handle;
  208. u32 ioctbuf_phys_hi;
  209. u32 ioctbuf_phys_lo;
  210. int ioctl_resp_status;
  211. u32 ioctl_resp_ret_len;
  212. u32 ioctl_resp_pktid;
  213. u16 data_seq_no;
  214. u16 ioctl_seq_no;
  215. u32 reqid;
  216. wait_queue_head_t ioctl_resp_wait;
  217. bool ctl_completed;
  218. struct brcmf_msgbuf_pktids *tx_pktids;
  219. struct brcmf_msgbuf_pktids *rx_pktids;
  220. struct brcmf_flowring *flow;
  221. struct workqueue_struct *txflow_wq;
  222. struct work_struct txflow_work;
  223. unsigned long *flow_map;
  224. unsigned long *txstatus_done_map;
  225. struct work_struct flowring_work;
  226. spinlock_t flowring_work_lock;
  227. struct list_head work_queue;
  228. };
  229. struct brcmf_msgbuf_pktid {
  230. atomic_t allocated;
  231. u16 data_offset;
  232. struct sk_buff *skb;
  233. dma_addr_t physaddr;
  234. };
  235. struct brcmf_msgbuf_pktids {
  236. u32 array_size;
  237. u32 last_allocated_idx;
  238. enum dma_data_direction direction;
  239. struct brcmf_msgbuf_pktid *array;
  240. };
  241. static void brcmf_msgbuf_rxbuf_ioctlresp_post(struct brcmf_msgbuf *msgbuf);
  242. static struct brcmf_msgbuf_pktids *
  243. brcmf_msgbuf_init_pktids(u32 nr_array_entries,
  244. enum dma_data_direction direction)
  245. {
  246. struct brcmf_msgbuf_pktid *array;
  247. struct brcmf_msgbuf_pktids *pktids;
  248. array = kcalloc(nr_array_entries, sizeof(*array), GFP_KERNEL);
  249. if (!array)
  250. return NULL;
  251. pktids = kzalloc(sizeof(*pktids), GFP_KERNEL);
  252. if (!pktids) {
  253. kfree(array);
  254. return NULL;
  255. }
  256. pktids->array = array;
  257. pktids->array_size = nr_array_entries;
  258. return pktids;
  259. }
  260. static int
  261. brcmf_msgbuf_alloc_pktid(struct device *dev,
  262. struct brcmf_msgbuf_pktids *pktids,
  263. struct sk_buff *skb, u16 data_offset,
  264. dma_addr_t *physaddr, u32 *idx)
  265. {
  266. struct brcmf_msgbuf_pktid *array;
  267. u32 count;
  268. array = pktids->array;
  269. *physaddr = dma_map_single(dev, skb->data + data_offset,
  270. skb->len - data_offset, pktids->direction);
  271. if (dma_mapping_error(dev, *physaddr)) {
  272. brcmf_err("dma_map_single failed !!\n");
  273. return -ENOMEM;
  274. }
  275. *idx = pktids->last_allocated_idx;
  276. count = 0;
  277. do {
  278. (*idx)++;
  279. if (*idx == pktids->array_size)
  280. *idx = 0;
  281. if (array[*idx].allocated.counter == 0)
  282. if (atomic_cmpxchg(&array[*idx].allocated, 0, 1) == 0)
  283. break;
  284. count++;
  285. } while (count < pktids->array_size);
  286. if (count == pktids->array_size)
  287. return -ENOMEM;
  288. array[*idx].data_offset = data_offset;
  289. array[*idx].physaddr = *physaddr;
  290. array[*idx].skb = skb;
  291. pktids->last_allocated_idx = *idx;
  292. return 0;
  293. }
  294. static struct sk_buff *
  295. brcmf_msgbuf_get_pktid(struct device *dev, struct brcmf_msgbuf_pktids *pktids,
  296. u32 idx)
  297. {
  298. struct brcmf_msgbuf_pktid *pktid;
  299. struct sk_buff *skb;
  300. if (idx >= pktids->array_size) {
  301. brcmf_err("Invalid packet id %d (max %d)\n", idx,
  302. pktids->array_size);
  303. return NULL;
  304. }
  305. if (pktids->array[idx].allocated.counter) {
  306. pktid = &pktids->array[idx];
  307. dma_unmap_single(dev, pktid->physaddr,
  308. pktid->skb->len - pktid->data_offset,
  309. pktids->direction);
  310. skb = pktid->skb;
  311. pktid->allocated.counter = 0;
  312. return skb;
  313. } else {
  314. brcmf_err("Invalid packet id %d (not in use)\n", idx);
  315. }
  316. return NULL;
  317. }
  318. static void
  319. brcmf_msgbuf_release_array(struct device *dev,
  320. struct brcmf_msgbuf_pktids *pktids)
  321. {
  322. struct brcmf_msgbuf_pktid *array;
  323. struct brcmf_msgbuf_pktid *pktid;
  324. u32 count;
  325. array = pktids->array;
  326. count = 0;
  327. do {
  328. if (array[count].allocated.counter) {
  329. pktid = &array[count];
  330. dma_unmap_single(dev, pktid->physaddr,
  331. pktid->skb->len - pktid->data_offset,
  332. pktids->direction);
  333. brcmu_pkt_buf_free_skb(pktid->skb);
  334. }
  335. count++;
  336. } while (count < pktids->array_size);
  337. kfree(array);
  338. kfree(pktids);
  339. }
  340. static void brcmf_msgbuf_release_pktids(struct brcmf_msgbuf *msgbuf)
  341. {
  342. if (msgbuf->rx_pktids)
  343. brcmf_msgbuf_release_array(msgbuf->drvr->bus_if->dev,
  344. msgbuf->rx_pktids);
  345. if (msgbuf->tx_pktids)
  346. brcmf_msgbuf_release_array(msgbuf->drvr->bus_if->dev,
  347. msgbuf->tx_pktids);
  348. }
  349. static int brcmf_msgbuf_tx_ioctl(struct brcmf_pub *drvr, int ifidx,
  350. uint cmd, void *buf, uint len)
  351. {
  352. struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
  353. struct brcmf_commonring *commonring;
  354. struct msgbuf_ioctl_req_hdr *request;
  355. u16 buf_len;
  356. void *ret_ptr;
  357. int err;
  358. commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_CONTROL_SUBMIT];
  359. brcmf_commonring_lock(commonring);
  360. ret_ptr = brcmf_commonring_reserve_for_write(commonring);
  361. if (!ret_ptr) {
  362. brcmf_err("Failed to reserve space in commonring\n");
  363. brcmf_commonring_unlock(commonring);
  364. return -ENOMEM;
  365. }
  366. msgbuf->reqid++;
  367. request = (struct msgbuf_ioctl_req_hdr *)ret_ptr;
  368. request->msg.msgtype = MSGBUF_TYPE_IOCTLPTR_REQ;
  369. request->msg.ifidx = (u8)ifidx;
  370. request->msg.flags = 0;
  371. request->msg.request_id = cpu_to_le32(BRCMF_IOCTL_REQ_PKTID);
  372. request->cmd = cpu_to_le32(cmd);
  373. request->output_buf_len = cpu_to_le16(len);
  374. request->trans_id = cpu_to_le16(msgbuf->reqid);
  375. buf_len = min_t(u16, len, BRCMF_TX_IOCTL_MAX_MSG_SIZE);
  376. request->input_buf_len = cpu_to_le16(buf_len);
  377. request->req_buf_addr.high_addr = cpu_to_le32(msgbuf->ioctbuf_phys_hi);
  378. request->req_buf_addr.low_addr = cpu_to_le32(msgbuf->ioctbuf_phys_lo);
  379. if (buf)
  380. memcpy(msgbuf->ioctbuf, buf, buf_len);
  381. else
  382. memset(msgbuf->ioctbuf, 0, buf_len);
  383. err = brcmf_commonring_write_complete(commonring);
  384. brcmf_commonring_unlock(commonring);
  385. return err;
  386. }
  387. static int brcmf_msgbuf_ioctl_resp_wait(struct brcmf_msgbuf *msgbuf)
  388. {
  389. return wait_event_timeout(msgbuf->ioctl_resp_wait,
  390. msgbuf->ctl_completed,
  391. MSGBUF_IOCTL_RESP_TIMEOUT);
  392. }
  393. static void brcmf_msgbuf_ioctl_resp_wake(struct brcmf_msgbuf *msgbuf)
  394. {
  395. msgbuf->ctl_completed = true;
  396. wake_up(&msgbuf->ioctl_resp_wait);
  397. }
  398. static int brcmf_msgbuf_query_dcmd(struct brcmf_pub *drvr, int ifidx,
  399. uint cmd, void *buf, uint len, int *fwerr)
  400. {
  401. struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
  402. struct sk_buff *skb = NULL;
  403. int timeout;
  404. int err;
  405. brcmf_dbg(MSGBUF, "ifidx=%d, cmd=%d, len=%d\n", ifidx, cmd, len);
  406. *fwerr = 0;
  407. msgbuf->ctl_completed = false;
  408. err = brcmf_msgbuf_tx_ioctl(drvr, ifidx, cmd, buf, len);
  409. if (err)
  410. return err;
  411. timeout = brcmf_msgbuf_ioctl_resp_wait(msgbuf);
  412. if (!timeout) {
  413. brcmf_err("Timeout on response for query command\n");
  414. return -EIO;
  415. }
  416. skb = brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev,
  417. msgbuf->rx_pktids,
  418. msgbuf->ioctl_resp_pktid);
  419. if (msgbuf->ioctl_resp_ret_len != 0) {
  420. if (!skb)
  421. return -EBADF;
  422. memcpy(buf, skb->data, (len < msgbuf->ioctl_resp_ret_len) ?
  423. len : msgbuf->ioctl_resp_ret_len);
  424. }
  425. brcmu_pkt_buf_free_skb(skb);
  426. *fwerr = msgbuf->ioctl_resp_status;
  427. return 0;
  428. }
  429. static int brcmf_msgbuf_set_dcmd(struct brcmf_pub *drvr, int ifidx,
  430. uint cmd, void *buf, uint len, int *fwerr)
  431. {
  432. return brcmf_msgbuf_query_dcmd(drvr, ifidx, cmd, buf, len, fwerr);
  433. }
  434. static int brcmf_msgbuf_hdrpull(struct brcmf_pub *drvr, bool do_fws,
  435. struct sk_buff *skb, struct brcmf_if **ifp)
  436. {
  437. return -ENODEV;
  438. }
  439. static void brcmf_msgbuf_rxreorder(struct brcmf_if *ifp, struct sk_buff *skb)
  440. {
  441. }
  442. static void
  443. brcmf_msgbuf_remove_flowring(struct brcmf_msgbuf *msgbuf, u16 flowid)
  444. {
  445. u32 dma_sz;
  446. void *dma_buf;
  447. brcmf_dbg(MSGBUF, "Removing flowring %d\n", flowid);
  448. dma_sz = BRCMF_H2D_TXFLOWRING_MAX_ITEM * BRCMF_H2D_TXFLOWRING_ITEMSIZE;
  449. dma_buf = msgbuf->flowrings[flowid]->buf_addr;
  450. dma_free_coherent(msgbuf->drvr->bus_if->dev, dma_sz, dma_buf,
  451. msgbuf->flowring_dma_handle[flowid]);
  452. brcmf_flowring_delete(msgbuf->flow, flowid);
  453. }
  454. static struct brcmf_msgbuf_work_item *
  455. brcmf_msgbuf_dequeue_work(struct brcmf_msgbuf *msgbuf)
  456. {
  457. struct brcmf_msgbuf_work_item *work = NULL;
  458. ulong flags;
  459. spin_lock_irqsave(&msgbuf->flowring_work_lock, flags);
  460. if (!list_empty(&msgbuf->work_queue)) {
  461. work = list_first_entry(&msgbuf->work_queue,
  462. struct brcmf_msgbuf_work_item, queue);
  463. list_del(&work->queue);
  464. }
  465. spin_unlock_irqrestore(&msgbuf->flowring_work_lock, flags);
  466. return work;
  467. }
  468. static u32
  469. brcmf_msgbuf_flowring_create_worker(struct brcmf_msgbuf *msgbuf,
  470. struct brcmf_msgbuf_work_item *work)
  471. {
  472. struct msgbuf_tx_flowring_create_req *create;
  473. struct brcmf_commonring *commonring;
  474. void *ret_ptr;
  475. u32 flowid;
  476. void *dma_buf;
  477. u32 dma_sz;
  478. u64 address;
  479. int err;
  480. flowid = work->flowid;
  481. dma_sz = BRCMF_H2D_TXFLOWRING_MAX_ITEM * BRCMF_H2D_TXFLOWRING_ITEMSIZE;
  482. dma_buf = dma_alloc_coherent(msgbuf->drvr->bus_if->dev, dma_sz,
  483. &msgbuf->flowring_dma_handle[flowid],
  484. GFP_KERNEL);
  485. if (!dma_buf) {
  486. brcmf_err("dma_alloc_coherent failed\n");
  487. brcmf_flowring_delete(msgbuf->flow, flowid);
  488. return BRCMF_FLOWRING_INVALID_ID;
  489. }
  490. brcmf_commonring_config(msgbuf->flowrings[flowid],
  491. BRCMF_H2D_TXFLOWRING_MAX_ITEM,
  492. BRCMF_H2D_TXFLOWRING_ITEMSIZE, dma_buf);
  493. commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_CONTROL_SUBMIT];
  494. brcmf_commonring_lock(commonring);
  495. ret_ptr = brcmf_commonring_reserve_for_write(commonring);
  496. if (!ret_ptr) {
  497. brcmf_err("Failed to reserve space in commonring\n");
  498. brcmf_commonring_unlock(commonring);
  499. brcmf_msgbuf_remove_flowring(msgbuf, flowid);
  500. return BRCMF_FLOWRING_INVALID_ID;
  501. }
  502. create = (struct msgbuf_tx_flowring_create_req *)ret_ptr;
  503. create->msg.msgtype = MSGBUF_TYPE_FLOW_RING_CREATE;
  504. create->msg.ifidx = work->ifidx;
  505. create->msg.request_id = 0;
  506. create->tid = brcmf_flowring_tid(msgbuf->flow, flowid);
  507. create->flow_ring_id = cpu_to_le16(flowid +
  508. BRCMF_H2D_MSGRING_FLOWRING_IDSTART);
  509. memcpy(create->sa, work->sa, ETH_ALEN);
  510. memcpy(create->da, work->da, ETH_ALEN);
  511. address = (u64)msgbuf->flowring_dma_handle[flowid];
  512. create->flow_ring_addr.high_addr = cpu_to_le32(address >> 32);
  513. create->flow_ring_addr.low_addr = cpu_to_le32(address & 0xffffffff);
  514. create->max_items = cpu_to_le16(BRCMF_H2D_TXFLOWRING_MAX_ITEM);
  515. create->len_item = cpu_to_le16(BRCMF_H2D_TXFLOWRING_ITEMSIZE);
  516. brcmf_dbg(MSGBUF, "Send Flow Create Req flow ID %d for peer %pM prio %d ifindex %d\n",
  517. flowid, work->da, create->tid, work->ifidx);
  518. err = brcmf_commonring_write_complete(commonring);
  519. brcmf_commonring_unlock(commonring);
  520. if (err) {
  521. brcmf_err("Failed to write commonring\n");
  522. brcmf_msgbuf_remove_flowring(msgbuf, flowid);
  523. return BRCMF_FLOWRING_INVALID_ID;
  524. }
  525. return flowid;
  526. }
  527. static void brcmf_msgbuf_flowring_worker(struct work_struct *work)
  528. {
  529. struct brcmf_msgbuf *msgbuf;
  530. struct brcmf_msgbuf_work_item *create;
  531. msgbuf = container_of(work, struct brcmf_msgbuf, flowring_work);
  532. while ((create = brcmf_msgbuf_dequeue_work(msgbuf))) {
  533. brcmf_msgbuf_flowring_create_worker(msgbuf, create);
  534. kfree(create);
  535. }
  536. }
  537. static u32 brcmf_msgbuf_flowring_create(struct brcmf_msgbuf *msgbuf, int ifidx,
  538. struct sk_buff *skb)
  539. {
  540. struct brcmf_msgbuf_work_item *create;
  541. struct ethhdr *eh = (struct ethhdr *)(skb->data);
  542. u32 flowid;
  543. ulong flags;
  544. create = kzalloc(sizeof(*create), GFP_ATOMIC);
  545. if (create == NULL)
  546. return BRCMF_FLOWRING_INVALID_ID;
  547. flowid = brcmf_flowring_create(msgbuf->flow, eh->h_dest,
  548. skb->priority, ifidx);
  549. if (flowid == BRCMF_FLOWRING_INVALID_ID) {
  550. kfree(create);
  551. return flowid;
  552. }
  553. create->flowid = flowid;
  554. create->ifidx = ifidx;
  555. memcpy(create->sa, eh->h_source, ETH_ALEN);
  556. memcpy(create->da, eh->h_dest, ETH_ALEN);
  557. spin_lock_irqsave(&msgbuf->flowring_work_lock, flags);
  558. list_add_tail(&create->queue, &msgbuf->work_queue);
  559. spin_unlock_irqrestore(&msgbuf->flowring_work_lock, flags);
  560. schedule_work(&msgbuf->flowring_work);
  561. return flowid;
  562. }
  563. static void brcmf_msgbuf_txflow(struct brcmf_msgbuf *msgbuf, u16 flowid)
  564. {
  565. struct brcmf_flowring *flow = msgbuf->flow;
  566. struct brcmf_commonring *commonring;
  567. void *ret_ptr;
  568. u32 count;
  569. struct sk_buff *skb;
  570. dma_addr_t physaddr;
  571. u32 pktid;
  572. struct msgbuf_tx_msghdr *tx_msghdr;
  573. u64 address;
  574. commonring = msgbuf->flowrings[flowid];
  575. if (!brcmf_commonring_write_available(commonring))
  576. return;
  577. brcmf_commonring_lock(commonring);
  578. count = BRCMF_MSGBUF_TX_FLUSH_CNT2 - BRCMF_MSGBUF_TX_FLUSH_CNT1;
  579. while (brcmf_flowring_qlen(flow, flowid)) {
  580. skb = brcmf_flowring_dequeue(flow, flowid);
  581. if (skb == NULL) {
  582. brcmf_err("No SKB, but qlen %d\n",
  583. brcmf_flowring_qlen(flow, flowid));
  584. break;
  585. }
  586. skb_orphan(skb);
  587. if (brcmf_msgbuf_alloc_pktid(msgbuf->drvr->bus_if->dev,
  588. msgbuf->tx_pktids, skb, ETH_HLEN,
  589. &physaddr, &pktid)) {
  590. brcmf_flowring_reinsert(flow, flowid, skb);
  591. brcmf_err("No PKTID available !!\n");
  592. break;
  593. }
  594. ret_ptr = brcmf_commonring_reserve_for_write(commonring);
  595. if (!ret_ptr) {
  596. brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev,
  597. msgbuf->tx_pktids, pktid);
  598. brcmf_flowring_reinsert(flow, flowid, skb);
  599. break;
  600. }
  601. count++;
  602. tx_msghdr = (struct msgbuf_tx_msghdr *)ret_ptr;
  603. tx_msghdr->msg.msgtype = MSGBUF_TYPE_TX_POST;
  604. tx_msghdr->msg.request_id = cpu_to_le32(pktid);
  605. tx_msghdr->msg.ifidx = brcmf_flowring_ifidx_get(flow, flowid);
  606. tx_msghdr->flags = BRCMF_MSGBUF_PKT_FLAGS_FRAME_802_3;
  607. tx_msghdr->flags |= (skb->priority & 0x07) <<
  608. BRCMF_MSGBUF_PKT_FLAGS_PRIO_SHIFT;
  609. tx_msghdr->seg_cnt = 1;
  610. memcpy(tx_msghdr->txhdr, skb->data, ETH_HLEN);
  611. tx_msghdr->data_len = cpu_to_le16(skb->len - ETH_HLEN);
  612. address = (u64)physaddr;
  613. tx_msghdr->data_buf_addr.high_addr = cpu_to_le32(address >> 32);
  614. tx_msghdr->data_buf_addr.low_addr =
  615. cpu_to_le32(address & 0xffffffff);
  616. tx_msghdr->metadata_buf_len = 0;
  617. tx_msghdr->metadata_buf_addr.high_addr = 0;
  618. tx_msghdr->metadata_buf_addr.low_addr = 0;
  619. atomic_inc(&commonring->outstanding_tx);
  620. if (count >= BRCMF_MSGBUF_TX_FLUSH_CNT2) {
  621. brcmf_commonring_write_complete(commonring);
  622. count = 0;
  623. }
  624. }
  625. if (count)
  626. brcmf_commonring_write_complete(commonring);
  627. brcmf_commonring_unlock(commonring);
  628. }
  629. static void brcmf_msgbuf_txflow_worker(struct work_struct *worker)
  630. {
  631. struct brcmf_msgbuf *msgbuf;
  632. u32 flowid;
  633. msgbuf = container_of(worker, struct brcmf_msgbuf, txflow_work);
  634. for_each_set_bit(flowid, msgbuf->flow_map, msgbuf->max_flowrings) {
  635. clear_bit(flowid, msgbuf->flow_map);
  636. brcmf_msgbuf_txflow(msgbuf, flowid);
  637. }
  638. }
  639. static int brcmf_msgbuf_schedule_txdata(struct brcmf_msgbuf *msgbuf, u32 flowid,
  640. bool force)
  641. {
  642. struct brcmf_commonring *commonring;
  643. set_bit(flowid, msgbuf->flow_map);
  644. commonring = msgbuf->flowrings[flowid];
  645. if ((force) || (atomic_read(&commonring->outstanding_tx) <
  646. BRCMF_MSGBUF_DELAY_TXWORKER_THRS))
  647. queue_work(msgbuf->txflow_wq, &msgbuf->txflow_work);
  648. return 0;
  649. }
  650. static int brcmf_msgbuf_tx_queue_data(struct brcmf_pub *drvr, int ifidx,
  651. struct sk_buff *skb)
  652. {
  653. struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
  654. struct brcmf_flowring *flow = msgbuf->flow;
  655. struct ethhdr *eh = (struct ethhdr *)(skb->data);
  656. u32 flowid;
  657. u32 queue_count;
  658. bool force;
  659. flowid = brcmf_flowring_lookup(flow, eh->h_dest, skb->priority, ifidx);
  660. if (flowid == BRCMF_FLOWRING_INVALID_ID) {
  661. flowid = brcmf_msgbuf_flowring_create(msgbuf, ifidx, skb);
  662. if (flowid == BRCMF_FLOWRING_INVALID_ID)
  663. return -ENOMEM;
  664. }
  665. queue_count = brcmf_flowring_enqueue(flow, flowid, skb);
  666. force = ((queue_count % BRCMF_MSGBUF_TRICKLE_TXWORKER_THRS) == 0);
  667. brcmf_msgbuf_schedule_txdata(msgbuf, flowid, force);
  668. return 0;
  669. }
  670. static void
  671. brcmf_msgbuf_configure_addr_mode(struct brcmf_pub *drvr, int ifidx,
  672. enum proto_addr_mode addr_mode)
  673. {
  674. struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
  675. brcmf_flowring_configure_addr_mode(msgbuf->flow, ifidx, addr_mode);
  676. }
  677. static void
  678. brcmf_msgbuf_delete_peer(struct brcmf_pub *drvr, int ifidx, u8 peer[ETH_ALEN])
  679. {
  680. struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
  681. brcmf_flowring_delete_peer(msgbuf->flow, ifidx, peer);
  682. }
  683. static void
  684. brcmf_msgbuf_add_tdls_peer(struct brcmf_pub *drvr, int ifidx, u8 peer[ETH_ALEN])
  685. {
  686. struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
  687. brcmf_flowring_add_tdls_peer(msgbuf->flow, ifidx, peer);
  688. }
  689. static void
  690. brcmf_msgbuf_process_ioctl_complete(struct brcmf_msgbuf *msgbuf, void *buf)
  691. {
  692. struct msgbuf_ioctl_resp_hdr *ioctl_resp;
  693. ioctl_resp = (struct msgbuf_ioctl_resp_hdr *)buf;
  694. msgbuf->ioctl_resp_status =
  695. (s16)le16_to_cpu(ioctl_resp->compl_hdr.status);
  696. msgbuf->ioctl_resp_ret_len = le16_to_cpu(ioctl_resp->resp_len);
  697. msgbuf->ioctl_resp_pktid = le32_to_cpu(ioctl_resp->msg.request_id);
  698. brcmf_msgbuf_ioctl_resp_wake(msgbuf);
  699. if (msgbuf->cur_ioctlrespbuf)
  700. msgbuf->cur_ioctlrespbuf--;
  701. brcmf_msgbuf_rxbuf_ioctlresp_post(msgbuf);
  702. }
  703. static void
  704. brcmf_msgbuf_process_txstatus(struct brcmf_msgbuf *msgbuf, void *buf)
  705. {
  706. struct brcmf_commonring *commonring;
  707. struct msgbuf_tx_status *tx_status;
  708. u32 idx;
  709. struct sk_buff *skb;
  710. u16 flowid;
  711. tx_status = (struct msgbuf_tx_status *)buf;
  712. idx = le32_to_cpu(tx_status->msg.request_id);
  713. flowid = le16_to_cpu(tx_status->compl_hdr.flow_ring_id);
  714. flowid -= BRCMF_H2D_MSGRING_FLOWRING_IDSTART;
  715. skb = brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev,
  716. msgbuf->tx_pktids, idx);
  717. if (!skb)
  718. return;
  719. set_bit(flowid, msgbuf->txstatus_done_map);
  720. commonring = msgbuf->flowrings[flowid];
  721. atomic_dec(&commonring->outstanding_tx);
  722. brcmf_txfinalize(brcmf_get_ifp(msgbuf->drvr, tx_status->msg.ifidx),
  723. skb, true);
  724. }
  725. static u32 brcmf_msgbuf_rxbuf_data_post(struct brcmf_msgbuf *msgbuf, u32 count)
  726. {
  727. struct brcmf_commonring *commonring;
  728. void *ret_ptr;
  729. struct sk_buff *skb;
  730. u16 alloced;
  731. u32 pktlen;
  732. dma_addr_t physaddr;
  733. struct msgbuf_rx_bufpost *rx_bufpost;
  734. u64 address;
  735. u32 pktid;
  736. u32 i;
  737. commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_RXPOST_SUBMIT];
  738. ret_ptr = brcmf_commonring_reserve_for_write_multiple(commonring,
  739. count,
  740. &alloced);
  741. if (!ret_ptr) {
  742. brcmf_dbg(MSGBUF, "Failed to reserve space in commonring\n");
  743. return 0;
  744. }
  745. for (i = 0; i < alloced; i++) {
  746. rx_bufpost = (struct msgbuf_rx_bufpost *)ret_ptr;
  747. memset(rx_bufpost, 0, sizeof(*rx_bufpost));
  748. skb = brcmu_pkt_buf_get_skb(BRCMF_MSGBUF_MAX_PKT_SIZE);
  749. if (skb == NULL) {
  750. brcmf_err("Failed to alloc SKB\n");
  751. brcmf_commonring_write_cancel(commonring, alloced - i);
  752. break;
  753. }
  754. pktlen = skb->len;
  755. if (brcmf_msgbuf_alloc_pktid(msgbuf->drvr->bus_if->dev,
  756. msgbuf->rx_pktids, skb, 0,
  757. &physaddr, &pktid)) {
  758. dev_kfree_skb_any(skb);
  759. brcmf_err("No PKTID available !!\n");
  760. brcmf_commonring_write_cancel(commonring, alloced - i);
  761. break;
  762. }
  763. if (msgbuf->rx_metadata_offset) {
  764. address = (u64)physaddr;
  765. rx_bufpost->metadata_buf_len =
  766. cpu_to_le16(msgbuf->rx_metadata_offset);
  767. rx_bufpost->metadata_buf_addr.high_addr =
  768. cpu_to_le32(address >> 32);
  769. rx_bufpost->metadata_buf_addr.low_addr =
  770. cpu_to_le32(address & 0xffffffff);
  771. skb_pull(skb, msgbuf->rx_metadata_offset);
  772. pktlen = skb->len;
  773. physaddr += msgbuf->rx_metadata_offset;
  774. }
  775. rx_bufpost->msg.msgtype = MSGBUF_TYPE_RXBUF_POST;
  776. rx_bufpost->msg.request_id = cpu_to_le32(pktid);
  777. address = (u64)physaddr;
  778. rx_bufpost->data_buf_len = cpu_to_le16((u16)pktlen);
  779. rx_bufpost->data_buf_addr.high_addr =
  780. cpu_to_le32(address >> 32);
  781. rx_bufpost->data_buf_addr.low_addr =
  782. cpu_to_le32(address & 0xffffffff);
  783. ret_ptr += brcmf_commonring_len_item(commonring);
  784. }
  785. if (i)
  786. brcmf_commonring_write_complete(commonring);
  787. return i;
  788. }
  789. static void
  790. brcmf_msgbuf_rxbuf_data_fill(struct brcmf_msgbuf *msgbuf)
  791. {
  792. u32 fillbufs;
  793. u32 retcount;
  794. fillbufs = msgbuf->max_rxbufpost - msgbuf->rxbufpost;
  795. while (fillbufs) {
  796. retcount = brcmf_msgbuf_rxbuf_data_post(msgbuf, fillbufs);
  797. if (!retcount)
  798. break;
  799. msgbuf->rxbufpost += retcount;
  800. fillbufs -= retcount;
  801. }
  802. }
  803. static void
  804. brcmf_msgbuf_update_rxbufpost_count(struct brcmf_msgbuf *msgbuf, u16 rxcnt)
  805. {
  806. msgbuf->rxbufpost -= rxcnt;
  807. if (msgbuf->rxbufpost <= (msgbuf->max_rxbufpost -
  808. BRCMF_MSGBUF_RXBUFPOST_THRESHOLD))
  809. brcmf_msgbuf_rxbuf_data_fill(msgbuf);
  810. }
  811. static u32
  812. brcmf_msgbuf_rxbuf_ctrl_post(struct brcmf_msgbuf *msgbuf, bool event_buf,
  813. u32 count)
  814. {
  815. struct brcmf_commonring *commonring;
  816. void *ret_ptr;
  817. struct sk_buff *skb;
  818. u16 alloced;
  819. u32 pktlen;
  820. dma_addr_t physaddr;
  821. struct msgbuf_rx_ioctl_resp_or_event *rx_bufpost;
  822. u64 address;
  823. u32 pktid;
  824. u32 i;
  825. commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_CONTROL_SUBMIT];
  826. brcmf_commonring_lock(commonring);
  827. ret_ptr = brcmf_commonring_reserve_for_write_multiple(commonring,
  828. count,
  829. &alloced);
  830. if (!ret_ptr) {
  831. brcmf_err("Failed to reserve space in commonring\n");
  832. brcmf_commonring_unlock(commonring);
  833. return 0;
  834. }
  835. for (i = 0; i < alloced; i++) {
  836. rx_bufpost = (struct msgbuf_rx_ioctl_resp_or_event *)ret_ptr;
  837. memset(rx_bufpost, 0, sizeof(*rx_bufpost));
  838. skb = brcmu_pkt_buf_get_skb(BRCMF_MSGBUF_MAX_PKT_SIZE);
  839. if (skb == NULL) {
  840. brcmf_err("Failed to alloc SKB\n");
  841. brcmf_commonring_write_cancel(commonring, alloced - i);
  842. break;
  843. }
  844. pktlen = skb->len;
  845. if (brcmf_msgbuf_alloc_pktid(msgbuf->drvr->bus_if->dev,
  846. msgbuf->rx_pktids, skb, 0,
  847. &physaddr, &pktid)) {
  848. dev_kfree_skb_any(skb);
  849. brcmf_err("No PKTID available !!\n");
  850. brcmf_commonring_write_cancel(commonring, alloced - i);
  851. break;
  852. }
  853. if (event_buf)
  854. rx_bufpost->msg.msgtype = MSGBUF_TYPE_EVENT_BUF_POST;
  855. else
  856. rx_bufpost->msg.msgtype =
  857. MSGBUF_TYPE_IOCTLRESP_BUF_POST;
  858. rx_bufpost->msg.request_id = cpu_to_le32(pktid);
  859. address = (u64)physaddr;
  860. rx_bufpost->host_buf_len = cpu_to_le16((u16)pktlen);
  861. rx_bufpost->host_buf_addr.high_addr =
  862. cpu_to_le32(address >> 32);
  863. rx_bufpost->host_buf_addr.low_addr =
  864. cpu_to_le32(address & 0xffffffff);
  865. ret_ptr += brcmf_commonring_len_item(commonring);
  866. }
  867. if (i)
  868. brcmf_commonring_write_complete(commonring);
  869. brcmf_commonring_unlock(commonring);
  870. return i;
  871. }
  872. static void brcmf_msgbuf_rxbuf_ioctlresp_post(struct brcmf_msgbuf *msgbuf)
  873. {
  874. u32 count;
  875. count = msgbuf->max_ioctlrespbuf - msgbuf->cur_ioctlrespbuf;
  876. count = brcmf_msgbuf_rxbuf_ctrl_post(msgbuf, false, count);
  877. msgbuf->cur_ioctlrespbuf += count;
  878. }
  879. static void brcmf_msgbuf_rxbuf_event_post(struct brcmf_msgbuf *msgbuf)
  880. {
  881. u32 count;
  882. count = msgbuf->max_eventbuf - msgbuf->cur_eventbuf;
  883. count = brcmf_msgbuf_rxbuf_ctrl_post(msgbuf, true, count);
  884. msgbuf->cur_eventbuf += count;
  885. }
  886. static void brcmf_msgbuf_process_event(struct brcmf_msgbuf *msgbuf, void *buf)
  887. {
  888. struct msgbuf_rx_event *event;
  889. u32 idx;
  890. u16 buflen;
  891. struct sk_buff *skb;
  892. struct brcmf_if *ifp;
  893. event = (struct msgbuf_rx_event *)buf;
  894. idx = le32_to_cpu(event->msg.request_id);
  895. buflen = le16_to_cpu(event->event_data_len);
  896. if (msgbuf->cur_eventbuf)
  897. msgbuf->cur_eventbuf--;
  898. brcmf_msgbuf_rxbuf_event_post(msgbuf);
  899. skb = brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev,
  900. msgbuf->rx_pktids, idx);
  901. if (!skb)
  902. return;
  903. if (msgbuf->rx_dataoffset)
  904. skb_pull(skb, msgbuf->rx_dataoffset);
  905. skb_trim(skb, buflen);
  906. ifp = brcmf_get_ifp(msgbuf->drvr, event->msg.ifidx);
  907. if (!ifp || !ifp->ndev) {
  908. brcmf_err("Received pkt for invalid ifidx %d\n",
  909. event->msg.ifidx);
  910. goto exit;
  911. }
  912. skb->protocol = eth_type_trans(skb, ifp->ndev);
  913. brcmf_fweh_process_skb(ifp->drvr, skb, 0);
  914. exit:
  915. brcmu_pkt_buf_free_skb(skb);
  916. }
  917. static void
  918. brcmf_msgbuf_process_rx_complete(struct brcmf_msgbuf *msgbuf, void *buf)
  919. {
  920. struct msgbuf_rx_complete *rx_complete;
  921. struct sk_buff *skb;
  922. u16 data_offset;
  923. u16 buflen;
  924. u16 flags;
  925. u32 idx;
  926. struct brcmf_if *ifp;
  927. brcmf_msgbuf_update_rxbufpost_count(msgbuf, 1);
  928. rx_complete = (struct msgbuf_rx_complete *)buf;
  929. data_offset = le16_to_cpu(rx_complete->data_offset);
  930. buflen = le16_to_cpu(rx_complete->data_len);
  931. idx = le32_to_cpu(rx_complete->msg.request_id);
  932. flags = le16_to_cpu(rx_complete->flags);
  933. skb = brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev,
  934. msgbuf->rx_pktids, idx);
  935. if (!skb)
  936. return;
  937. if (data_offset)
  938. skb_pull(skb, data_offset);
  939. else if (msgbuf->rx_dataoffset)
  940. skb_pull(skb, msgbuf->rx_dataoffset);
  941. skb_trim(skb, buflen);
  942. if ((flags & BRCMF_MSGBUF_PKT_FLAGS_FRAME_MASK) ==
  943. BRCMF_MSGBUF_PKT_FLAGS_FRAME_802_11) {
  944. ifp = msgbuf->drvr->mon_if;
  945. if (!ifp) {
  946. brcmf_err("Received unexpected monitor pkt\n");
  947. brcmu_pkt_buf_free_skb(skb);
  948. return;
  949. }
  950. brcmf_netif_mon_rx(ifp, skb);
  951. return;
  952. }
  953. ifp = brcmf_get_ifp(msgbuf->drvr, rx_complete->msg.ifidx);
  954. if (!ifp || !ifp->ndev) {
  955. brcmf_err("Received pkt for invalid ifidx %d\n",
  956. rx_complete->msg.ifidx);
  957. brcmu_pkt_buf_free_skb(skb);
  958. return;
  959. }
  960. skb->protocol = eth_type_trans(skb, ifp->ndev);
  961. brcmf_netif_rx(ifp, skb);
  962. }
  963. static void
  964. brcmf_msgbuf_process_flow_ring_create_response(struct brcmf_msgbuf *msgbuf,
  965. void *buf)
  966. {
  967. struct msgbuf_flowring_create_resp *flowring_create_resp;
  968. u16 status;
  969. u16 flowid;
  970. flowring_create_resp = (struct msgbuf_flowring_create_resp *)buf;
  971. flowid = le16_to_cpu(flowring_create_resp->compl_hdr.flow_ring_id);
  972. flowid -= BRCMF_H2D_MSGRING_FLOWRING_IDSTART;
  973. status = le16_to_cpu(flowring_create_resp->compl_hdr.status);
  974. if (status) {
  975. brcmf_err("Flowring creation failed, code %d\n", status);
  976. brcmf_msgbuf_remove_flowring(msgbuf, flowid);
  977. return;
  978. }
  979. brcmf_dbg(MSGBUF, "Flowring %d Create response status %d\n", flowid,
  980. status);
  981. brcmf_flowring_open(msgbuf->flow, flowid);
  982. brcmf_msgbuf_schedule_txdata(msgbuf, flowid, true);
  983. }
  984. static void
  985. brcmf_msgbuf_process_flow_ring_delete_response(struct brcmf_msgbuf *msgbuf,
  986. void *buf)
  987. {
  988. struct msgbuf_flowring_delete_resp *flowring_delete_resp;
  989. u16 status;
  990. u16 flowid;
  991. flowring_delete_resp = (struct msgbuf_flowring_delete_resp *)buf;
  992. flowid = le16_to_cpu(flowring_delete_resp->compl_hdr.flow_ring_id);
  993. flowid -= BRCMF_H2D_MSGRING_FLOWRING_IDSTART;
  994. status = le16_to_cpu(flowring_delete_resp->compl_hdr.status);
  995. if (status) {
  996. brcmf_err("Flowring deletion failed, code %d\n", status);
  997. brcmf_flowring_delete(msgbuf->flow, flowid);
  998. return;
  999. }
  1000. brcmf_dbg(MSGBUF, "Flowring %d Delete response status %d\n", flowid,
  1001. status);
  1002. brcmf_msgbuf_remove_flowring(msgbuf, flowid);
  1003. }
  1004. static void brcmf_msgbuf_process_msgtype(struct brcmf_msgbuf *msgbuf, void *buf)
  1005. {
  1006. struct msgbuf_common_hdr *msg;
  1007. msg = (struct msgbuf_common_hdr *)buf;
  1008. switch (msg->msgtype) {
  1009. case MSGBUF_TYPE_FLOW_RING_CREATE_CMPLT:
  1010. brcmf_dbg(MSGBUF, "MSGBUF_TYPE_FLOW_RING_CREATE_CMPLT\n");
  1011. brcmf_msgbuf_process_flow_ring_create_response(msgbuf, buf);
  1012. break;
  1013. case MSGBUF_TYPE_FLOW_RING_DELETE_CMPLT:
  1014. brcmf_dbg(MSGBUF, "MSGBUF_TYPE_FLOW_RING_DELETE_CMPLT\n");
  1015. brcmf_msgbuf_process_flow_ring_delete_response(msgbuf, buf);
  1016. break;
  1017. case MSGBUF_TYPE_IOCTLPTR_REQ_ACK:
  1018. brcmf_dbg(MSGBUF, "MSGBUF_TYPE_IOCTLPTR_REQ_ACK\n");
  1019. break;
  1020. case MSGBUF_TYPE_IOCTL_CMPLT:
  1021. brcmf_dbg(MSGBUF, "MSGBUF_TYPE_IOCTL_CMPLT\n");
  1022. brcmf_msgbuf_process_ioctl_complete(msgbuf, buf);
  1023. break;
  1024. case MSGBUF_TYPE_WL_EVENT:
  1025. brcmf_dbg(MSGBUF, "MSGBUF_TYPE_WL_EVENT\n");
  1026. brcmf_msgbuf_process_event(msgbuf, buf);
  1027. break;
  1028. case MSGBUF_TYPE_TX_STATUS:
  1029. brcmf_dbg(MSGBUF, "MSGBUF_TYPE_TX_STATUS\n");
  1030. brcmf_msgbuf_process_txstatus(msgbuf, buf);
  1031. break;
  1032. case MSGBUF_TYPE_RX_CMPLT:
  1033. brcmf_dbg(MSGBUF, "MSGBUF_TYPE_RX_CMPLT\n");
  1034. brcmf_msgbuf_process_rx_complete(msgbuf, buf);
  1035. break;
  1036. default:
  1037. brcmf_err("Unsupported msgtype %d\n", msg->msgtype);
  1038. break;
  1039. }
  1040. }
  1041. static void brcmf_msgbuf_process_rx(struct brcmf_msgbuf *msgbuf,
  1042. struct brcmf_commonring *commonring)
  1043. {
  1044. void *buf;
  1045. u16 count;
  1046. u16 processed;
  1047. again:
  1048. buf = brcmf_commonring_get_read_ptr(commonring, &count);
  1049. if (buf == NULL)
  1050. return;
  1051. processed = 0;
  1052. while (count) {
  1053. brcmf_msgbuf_process_msgtype(msgbuf,
  1054. buf + msgbuf->rx_dataoffset);
  1055. buf += brcmf_commonring_len_item(commonring);
  1056. processed++;
  1057. if (processed == BRCMF_MSGBUF_UPDATE_RX_PTR_THRS) {
  1058. brcmf_commonring_read_complete(commonring, processed);
  1059. processed = 0;
  1060. }
  1061. count--;
  1062. }
  1063. if (processed)
  1064. brcmf_commonring_read_complete(commonring, processed);
  1065. if (commonring->r_ptr == 0)
  1066. goto again;
  1067. }
  1068. int brcmf_proto_msgbuf_rx_trigger(struct device *dev)
  1069. {
  1070. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  1071. struct brcmf_pub *drvr = bus_if->drvr;
  1072. struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
  1073. struct brcmf_commonring *commonring;
  1074. void *buf;
  1075. u32 flowid;
  1076. int qlen;
  1077. buf = msgbuf->commonrings[BRCMF_D2H_MSGRING_RX_COMPLETE];
  1078. brcmf_msgbuf_process_rx(msgbuf, buf);
  1079. buf = msgbuf->commonrings[BRCMF_D2H_MSGRING_TX_COMPLETE];
  1080. brcmf_msgbuf_process_rx(msgbuf, buf);
  1081. buf = msgbuf->commonrings[BRCMF_D2H_MSGRING_CONTROL_COMPLETE];
  1082. brcmf_msgbuf_process_rx(msgbuf, buf);
  1083. for_each_set_bit(flowid, msgbuf->txstatus_done_map,
  1084. msgbuf->max_flowrings) {
  1085. clear_bit(flowid, msgbuf->txstatus_done_map);
  1086. commonring = msgbuf->flowrings[flowid];
  1087. qlen = brcmf_flowring_qlen(msgbuf->flow, flowid);
  1088. if ((qlen > BRCMF_MSGBUF_TRICKLE_TXWORKER_THRS) ||
  1089. ((qlen) && (atomic_read(&commonring->outstanding_tx) <
  1090. BRCMF_MSGBUF_TRICKLE_TXWORKER_THRS)))
  1091. brcmf_msgbuf_schedule_txdata(msgbuf, flowid, true);
  1092. }
  1093. return 0;
  1094. }
  1095. void brcmf_msgbuf_delete_flowring(struct brcmf_pub *drvr, u16 flowid)
  1096. {
  1097. struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
  1098. struct msgbuf_tx_flowring_delete_req *delete;
  1099. struct brcmf_commonring *commonring;
  1100. void *ret_ptr;
  1101. u8 ifidx;
  1102. int err;
  1103. commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_CONTROL_SUBMIT];
  1104. brcmf_commonring_lock(commonring);
  1105. ret_ptr = brcmf_commonring_reserve_for_write(commonring);
  1106. if (!ret_ptr) {
  1107. brcmf_err("FW unaware, flowring will be removed !!\n");
  1108. brcmf_commonring_unlock(commonring);
  1109. brcmf_msgbuf_remove_flowring(msgbuf, flowid);
  1110. return;
  1111. }
  1112. delete = (struct msgbuf_tx_flowring_delete_req *)ret_ptr;
  1113. ifidx = brcmf_flowring_ifidx_get(msgbuf->flow, flowid);
  1114. delete->msg.msgtype = MSGBUF_TYPE_FLOW_RING_DELETE;
  1115. delete->msg.ifidx = ifidx;
  1116. delete->msg.request_id = 0;
  1117. delete->flow_ring_id = cpu_to_le16(flowid +
  1118. BRCMF_H2D_MSGRING_FLOWRING_IDSTART);
  1119. delete->reason = 0;
  1120. brcmf_dbg(MSGBUF, "Send Flow Delete Req flow ID %d, ifindex %d\n",
  1121. flowid, ifidx);
  1122. err = brcmf_commonring_write_complete(commonring);
  1123. brcmf_commonring_unlock(commonring);
  1124. if (err) {
  1125. brcmf_err("Failed to submit RING_DELETE, flowring will be removed\n");
  1126. brcmf_msgbuf_remove_flowring(msgbuf, flowid);
  1127. }
  1128. }
  1129. #ifdef DEBUG
  1130. static int brcmf_msgbuf_stats_read(struct seq_file *seq, void *data)
  1131. {
  1132. struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
  1133. struct brcmf_pub *drvr = bus_if->drvr;
  1134. struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
  1135. struct brcmf_commonring *commonring;
  1136. u16 i;
  1137. struct brcmf_flowring_ring *ring;
  1138. struct brcmf_flowring_hash *hash;
  1139. commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_CONTROL_SUBMIT];
  1140. seq_printf(seq, "h2d_ctl_submit: rp %4u, wp %4u, depth %4u\n",
  1141. commonring->r_ptr, commonring->w_ptr, commonring->depth);
  1142. commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_RXPOST_SUBMIT];
  1143. seq_printf(seq, "h2d_rx_submit: rp %4u, wp %4u, depth %4u\n",
  1144. commonring->r_ptr, commonring->w_ptr, commonring->depth);
  1145. commonring = msgbuf->commonrings[BRCMF_D2H_MSGRING_CONTROL_COMPLETE];
  1146. seq_printf(seq, "d2h_ctl_cmplt: rp %4u, wp %4u, depth %4u\n",
  1147. commonring->r_ptr, commonring->w_ptr, commonring->depth);
  1148. commonring = msgbuf->commonrings[BRCMF_D2H_MSGRING_TX_COMPLETE];
  1149. seq_printf(seq, "d2h_tx_cmplt: rp %4u, wp %4u, depth %4u\n",
  1150. commonring->r_ptr, commonring->w_ptr, commonring->depth);
  1151. commonring = msgbuf->commonrings[BRCMF_D2H_MSGRING_RX_COMPLETE];
  1152. seq_printf(seq, "d2h_rx_cmplt: rp %4u, wp %4u, depth %4u\n",
  1153. commonring->r_ptr, commonring->w_ptr, commonring->depth);
  1154. seq_printf(seq, "\nh2d_flowrings: depth %u\n",
  1155. BRCMF_H2D_TXFLOWRING_MAX_ITEM);
  1156. seq_puts(seq, "Active flowrings:\n");
  1157. hash = msgbuf->flow->hash;
  1158. for (i = 0; i < msgbuf->flow->nrofrings; i++) {
  1159. if (!msgbuf->flow->rings[i])
  1160. continue;
  1161. ring = msgbuf->flow->rings[i];
  1162. if (ring->status != RING_OPEN)
  1163. continue;
  1164. commonring = msgbuf->flowrings[i];
  1165. hash = &msgbuf->flow->hash[ring->hash_id];
  1166. seq_printf(seq, "id %3u: rp %4u, wp %4u, qlen %4u, blocked %u\n"
  1167. " ifidx %u, fifo %u, da %pM\n",
  1168. i, commonring->r_ptr, commonring->w_ptr,
  1169. skb_queue_len(&ring->skblist), ring->blocked,
  1170. hash->ifidx, hash->fifo, hash->mac);
  1171. }
  1172. return 0;
  1173. }
  1174. #else
  1175. static int brcmf_msgbuf_stats_read(struct seq_file *seq, void *data)
  1176. {
  1177. return 0;
  1178. }
  1179. #endif
  1180. static void brcmf_msgbuf_debugfs_create(struct brcmf_pub *drvr)
  1181. {
  1182. brcmf_debugfs_add_entry(drvr, "msgbuf_stats", brcmf_msgbuf_stats_read);
  1183. }
  1184. int brcmf_proto_msgbuf_attach(struct brcmf_pub *drvr)
  1185. {
  1186. struct brcmf_bus_msgbuf *if_msgbuf;
  1187. struct brcmf_msgbuf *msgbuf;
  1188. u64 address;
  1189. u32 count;
  1190. if_msgbuf = drvr->bus_if->msgbuf;
  1191. if (if_msgbuf->max_flowrings >= BRCMF_FLOWRING_HASHSIZE) {
  1192. brcmf_err("driver not configured for this many flowrings %d\n",
  1193. if_msgbuf->max_flowrings);
  1194. if_msgbuf->max_flowrings = BRCMF_FLOWRING_HASHSIZE - 1;
  1195. }
  1196. msgbuf = kzalloc(sizeof(*msgbuf), GFP_KERNEL);
  1197. if (!msgbuf)
  1198. goto fail;
  1199. msgbuf->txflow_wq = create_singlethread_workqueue("msgbuf_txflow");
  1200. if (msgbuf->txflow_wq == NULL) {
  1201. brcmf_err("workqueue creation failed\n");
  1202. goto fail;
  1203. }
  1204. INIT_WORK(&msgbuf->txflow_work, brcmf_msgbuf_txflow_worker);
  1205. count = BITS_TO_LONGS(if_msgbuf->max_flowrings);
  1206. count = count * sizeof(unsigned long);
  1207. msgbuf->flow_map = kzalloc(count, GFP_KERNEL);
  1208. if (!msgbuf->flow_map)
  1209. goto fail;
  1210. msgbuf->txstatus_done_map = kzalloc(count, GFP_KERNEL);
  1211. if (!msgbuf->txstatus_done_map)
  1212. goto fail;
  1213. msgbuf->drvr = drvr;
  1214. msgbuf->ioctbuf = dma_alloc_coherent(drvr->bus_if->dev,
  1215. BRCMF_TX_IOCTL_MAX_MSG_SIZE,
  1216. &msgbuf->ioctbuf_handle,
  1217. GFP_KERNEL);
  1218. if (!msgbuf->ioctbuf)
  1219. goto fail;
  1220. address = (u64)msgbuf->ioctbuf_handle;
  1221. msgbuf->ioctbuf_phys_hi = address >> 32;
  1222. msgbuf->ioctbuf_phys_lo = address & 0xffffffff;
  1223. drvr->proto->hdrpull = brcmf_msgbuf_hdrpull;
  1224. drvr->proto->query_dcmd = brcmf_msgbuf_query_dcmd;
  1225. drvr->proto->set_dcmd = brcmf_msgbuf_set_dcmd;
  1226. drvr->proto->tx_queue_data = brcmf_msgbuf_tx_queue_data;
  1227. drvr->proto->configure_addr_mode = brcmf_msgbuf_configure_addr_mode;
  1228. drvr->proto->delete_peer = brcmf_msgbuf_delete_peer;
  1229. drvr->proto->add_tdls_peer = brcmf_msgbuf_add_tdls_peer;
  1230. drvr->proto->rxreorder = brcmf_msgbuf_rxreorder;
  1231. drvr->proto->debugfs_create = brcmf_msgbuf_debugfs_create;
  1232. drvr->proto->pd = msgbuf;
  1233. init_waitqueue_head(&msgbuf->ioctl_resp_wait);
  1234. msgbuf->commonrings =
  1235. (struct brcmf_commonring **)if_msgbuf->commonrings;
  1236. msgbuf->flowrings = (struct brcmf_commonring **)if_msgbuf->flowrings;
  1237. msgbuf->max_flowrings = if_msgbuf->max_flowrings;
  1238. msgbuf->flowring_dma_handle =
  1239. kcalloc(msgbuf->max_flowrings,
  1240. sizeof(*msgbuf->flowring_dma_handle), GFP_KERNEL);
  1241. if (!msgbuf->flowring_dma_handle)
  1242. goto fail;
  1243. msgbuf->rx_dataoffset = if_msgbuf->rx_dataoffset;
  1244. msgbuf->max_rxbufpost = if_msgbuf->max_rxbufpost;
  1245. msgbuf->max_ioctlrespbuf = BRCMF_MSGBUF_MAX_IOCTLRESPBUF_POST;
  1246. msgbuf->max_eventbuf = BRCMF_MSGBUF_MAX_EVENTBUF_POST;
  1247. msgbuf->tx_pktids = brcmf_msgbuf_init_pktids(NR_TX_PKTIDS,
  1248. DMA_TO_DEVICE);
  1249. if (!msgbuf->tx_pktids)
  1250. goto fail;
  1251. msgbuf->rx_pktids = brcmf_msgbuf_init_pktids(NR_RX_PKTIDS,
  1252. DMA_FROM_DEVICE);
  1253. if (!msgbuf->rx_pktids)
  1254. goto fail;
  1255. msgbuf->flow = brcmf_flowring_attach(drvr->bus_if->dev,
  1256. if_msgbuf->max_flowrings);
  1257. if (!msgbuf->flow)
  1258. goto fail;
  1259. brcmf_dbg(MSGBUF, "Feeding buffers, rx data %d, rx event %d, rx ioctl resp %d\n",
  1260. msgbuf->max_rxbufpost, msgbuf->max_eventbuf,
  1261. msgbuf->max_ioctlrespbuf);
  1262. count = 0;
  1263. do {
  1264. brcmf_msgbuf_rxbuf_data_fill(msgbuf);
  1265. if (msgbuf->max_rxbufpost != msgbuf->rxbufpost)
  1266. msleep(10);
  1267. else
  1268. break;
  1269. count++;
  1270. } while (count < 10);
  1271. brcmf_msgbuf_rxbuf_event_post(msgbuf);
  1272. brcmf_msgbuf_rxbuf_ioctlresp_post(msgbuf);
  1273. INIT_WORK(&msgbuf->flowring_work, brcmf_msgbuf_flowring_worker);
  1274. spin_lock_init(&msgbuf->flowring_work_lock);
  1275. INIT_LIST_HEAD(&msgbuf->work_queue);
  1276. return 0;
  1277. fail:
  1278. if (msgbuf) {
  1279. kfree(msgbuf->flow_map);
  1280. kfree(msgbuf->txstatus_done_map);
  1281. brcmf_msgbuf_release_pktids(msgbuf);
  1282. kfree(msgbuf->flowring_dma_handle);
  1283. if (msgbuf->ioctbuf)
  1284. dma_free_coherent(drvr->bus_if->dev,
  1285. BRCMF_TX_IOCTL_MAX_MSG_SIZE,
  1286. msgbuf->ioctbuf,
  1287. msgbuf->ioctbuf_handle);
  1288. kfree(msgbuf);
  1289. }
  1290. return -ENOMEM;
  1291. }
  1292. void brcmf_proto_msgbuf_detach(struct brcmf_pub *drvr)
  1293. {
  1294. struct brcmf_msgbuf *msgbuf;
  1295. struct brcmf_msgbuf_work_item *work;
  1296. brcmf_dbg(TRACE, "Enter\n");
  1297. if (drvr->proto->pd) {
  1298. msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
  1299. cancel_work_sync(&msgbuf->flowring_work);
  1300. while (!list_empty(&msgbuf->work_queue)) {
  1301. work = list_first_entry(&msgbuf->work_queue,
  1302. struct brcmf_msgbuf_work_item,
  1303. queue);
  1304. list_del(&work->queue);
  1305. kfree(work);
  1306. }
  1307. kfree(msgbuf->flow_map);
  1308. kfree(msgbuf->txstatus_done_map);
  1309. if (msgbuf->txflow_wq)
  1310. destroy_workqueue(msgbuf->txflow_wq);
  1311. brcmf_flowring_detach(msgbuf->flow);
  1312. dma_free_coherent(drvr->bus_if->dev,
  1313. BRCMF_TX_IOCTL_MAX_MSG_SIZE,
  1314. msgbuf->ioctbuf, msgbuf->ioctbuf_handle);
  1315. brcmf_msgbuf_release_pktids(msgbuf);
  1316. kfree(msgbuf->flowring_dma_handle);
  1317. kfree(msgbuf);
  1318. drvr->proto->pd = NULL;
  1319. }
  1320. }