ozproto.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /* -----------------------------------------------------------------------------
  2. * Copyright (c) 2011 Ozmo Inc
  3. * Released under the GNU General Public License Version 2 (GPLv2).
  4. * -----------------------------------------------------------------------------
  5. */
  6. #include <linux/module.h>
  7. #include <linux/timer.h>
  8. #include <linux/sched.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/errno.h>
  12. #include <linux/ieee80211.h>
  13. #include <linux/slab.h>
  14. #include "ozdbg.h"
  15. #include "ozprotocol.h"
  16. #include "ozeltbuf.h"
  17. #include "ozpd.h"
  18. #include "ozproto.h"
  19. #include "ozusbsvc.h"
  20. #include "ozappif.h"
  21. #include <asm/unaligned.h>
  22. #include <linux/uaccess.h>
  23. #include <net/psnap.h>
  24. #define OZ_CF_CONN_SUCCESS 1
  25. #define OZ_CF_CONN_FAILURE 2
  26. #define OZ_DO_STOP 1
  27. #define OZ_DO_SLEEP 2
  28. struct oz_binding {
  29. struct packet_type ptype;
  30. char name[OZ_MAX_BINDING_LEN];
  31. struct list_head link;
  32. };
  33. /*
  34. * External variable
  35. */
  36. DEFINE_SPINLOCK(g_polling_lock);
  37. /*
  38. * Static external variables.
  39. */
  40. static LIST_HEAD(g_pd_list);
  41. static LIST_HEAD(g_binding);
  42. static DEFINE_SPINLOCK(g_binding_lock);
  43. static struct sk_buff_head g_rx_queue;
  44. static u8 g_session_id;
  45. static u16 g_apps = 0x1;
  46. static int g_processing_rx;
  47. struct kmem_cache *oz_elt_info_cache;
  48. struct kmem_cache *oz_tx_frame_cache;
  49. /*
  50. * Context: softirq-serialized
  51. */
  52. static u8 oz_get_new_session_id(u8 exclude)
  53. {
  54. if (++g_session_id == 0)
  55. g_session_id = 1;
  56. if (g_session_id == exclude) {
  57. if (++g_session_id == 0)
  58. g_session_id = 1;
  59. }
  60. return g_session_id;
  61. }
  62. /*
  63. * Context: softirq-serialized
  64. */
  65. static void oz_send_conn_rsp(struct oz_pd *pd, u8 status)
  66. {
  67. struct sk_buff *skb;
  68. struct net_device *dev = pd->net_dev;
  69. struct oz_hdr *oz_hdr;
  70. struct oz_elt *elt;
  71. struct oz_elt_connect_rsp *body;
  72. int sz = sizeof(struct oz_hdr) + sizeof(struct oz_elt) +
  73. sizeof(struct oz_elt_connect_rsp);
  74. skb = alloc_skb(sz + OZ_ALLOCATED_SPACE(dev), GFP_ATOMIC);
  75. if (skb == NULL)
  76. return;
  77. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  78. skb_reset_network_header(skb);
  79. oz_hdr = (struct oz_hdr *)skb_put(skb, sz);
  80. elt = (struct oz_elt *)(oz_hdr+1);
  81. body = (struct oz_elt_connect_rsp *)(elt+1);
  82. skb->dev = dev;
  83. skb->protocol = htons(OZ_ETHERTYPE);
  84. /* Fill in device header */
  85. if (dev_hard_header(skb, dev, OZ_ETHERTYPE, pd->mac_addr,
  86. dev->dev_addr, skb->len) < 0) {
  87. kfree_skb(skb);
  88. return;
  89. }
  90. oz_hdr->control = (OZ_PROTOCOL_VERSION<<OZ_VERSION_SHIFT);
  91. oz_hdr->last_pkt_num = 0;
  92. put_unaligned(0, &oz_hdr->pkt_num);
  93. elt->type = OZ_ELT_CONNECT_RSP;
  94. elt->length = sizeof(struct oz_elt_connect_rsp);
  95. memset(body, 0, sizeof(struct oz_elt_connect_rsp));
  96. body->status = status;
  97. if (status == 0) {
  98. body->mode = pd->mode;
  99. body->session_id = pd->session_id;
  100. put_unaligned(cpu_to_le16(pd->total_apps), &body->apps);
  101. }
  102. oz_dbg(ON, "TX: OZ_ELT_CONNECT_RSP %d", status);
  103. dev_queue_xmit(skb);
  104. }
  105. /*
  106. * Context: softirq-serialized
  107. */
  108. static void pd_set_keepalive(struct oz_pd *pd, u8 kalive)
  109. {
  110. unsigned long keep_alive = kalive & OZ_KALIVE_VALUE_MASK;
  111. switch (kalive & OZ_KALIVE_TYPE_MASK) {
  112. case OZ_KALIVE_SPECIAL:
  113. pd->keep_alive = keep_alive * 1000*60*60*24*20;
  114. break;
  115. case OZ_KALIVE_SECS:
  116. pd->keep_alive = keep_alive*1000;
  117. break;
  118. case OZ_KALIVE_MINS:
  119. pd->keep_alive = keep_alive*1000*60;
  120. break;
  121. case OZ_KALIVE_HOURS:
  122. pd->keep_alive = keep_alive*1000*60*60;
  123. break;
  124. default:
  125. pd->keep_alive = 0;
  126. }
  127. oz_dbg(ON, "Keepalive = %lu mSec\n", pd->keep_alive);
  128. }
  129. /*
  130. * Context: softirq-serialized
  131. */
  132. static void pd_set_presleep(struct oz_pd *pd, u8 presleep, u8 start_timer)
  133. {
  134. if (presleep)
  135. pd->presleep = presleep*100;
  136. else
  137. pd->presleep = OZ_PRESLEEP_TOUT;
  138. if (start_timer) {
  139. spin_unlock(&g_polling_lock);
  140. oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
  141. spin_lock(&g_polling_lock);
  142. }
  143. oz_dbg(ON, "Presleep time = %lu mSec\n", pd->presleep);
  144. }
  145. /*
  146. * Context: softirq-serialized
  147. */
  148. static struct oz_pd *oz_connect_req(struct oz_pd *cur_pd, struct oz_elt *elt,
  149. const u8 *pd_addr, struct net_device *net_dev)
  150. {
  151. struct oz_pd *pd;
  152. struct oz_elt_connect_req *body =
  153. (struct oz_elt_connect_req *)(elt+1);
  154. u8 rsp_status = OZ_STATUS_SUCCESS;
  155. u8 stop_needed = 0;
  156. u16 new_apps = g_apps;
  157. struct net_device *old_net_dev = NULL;
  158. struct oz_pd *free_pd = NULL;
  159. if (cur_pd) {
  160. pd = cur_pd;
  161. spin_lock_bh(&g_polling_lock);
  162. } else {
  163. struct oz_pd *pd2 = NULL;
  164. struct list_head *e;
  165. pd = oz_pd_alloc(pd_addr);
  166. if (pd == NULL)
  167. return NULL;
  168. getnstimeofday(&pd->last_rx_timestamp);
  169. spin_lock_bh(&g_polling_lock);
  170. list_for_each(e, &g_pd_list) {
  171. pd2 = list_entry(e, struct oz_pd, link);
  172. if (ether_addr_equal(pd2->mac_addr, pd_addr)) {
  173. free_pd = pd;
  174. pd = pd2;
  175. break;
  176. }
  177. }
  178. if (pd != pd2)
  179. list_add_tail(&pd->link, &g_pd_list);
  180. }
  181. if (pd == NULL) {
  182. spin_unlock_bh(&g_polling_lock);
  183. return NULL;
  184. }
  185. if (pd->net_dev != net_dev) {
  186. old_net_dev = pd->net_dev;
  187. dev_hold(net_dev);
  188. pd->net_dev = net_dev;
  189. }
  190. oz_dbg(ON, "Host vendor: %d\n", body->host_vendor);
  191. pd->max_tx_size = OZ_MAX_TX_SIZE;
  192. pd->mode = body->mode;
  193. pd->pd_info = body->pd_info;
  194. if (pd->mode & OZ_F_ISOC_NO_ELTS) {
  195. pd->ms_per_isoc = body->ms_per_isoc;
  196. if (!pd->ms_per_isoc)
  197. pd->ms_per_isoc = 4;
  198. switch (body->ms_isoc_latency & OZ_LATENCY_MASK) {
  199. case OZ_ONE_MS_LATENCY:
  200. pd->isoc_latency = (body->ms_isoc_latency &
  201. ~OZ_LATENCY_MASK) / pd->ms_per_isoc;
  202. break;
  203. case OZ_TEN_MS_LATENCY:
  204. pd->isoc_latency = ((body->ms_isoc_latency &
  205. ~OZ_LATENCY_MASK) * 10) / pd->ms_per_isoc;
  206. break;
  207. default:
  208. pd->isoc_latency = OZ_MAX_TX_QUEUE_ISOC;
  209. }
  210. }
  211. if (body->max_len_div16)
  212. pd->max_tx_size = ((u16)body->max_len_div16)<<4;
  213. oz_dbg(ON, "Max frame:%u Ms per isoc:%u\n",
  214. pd->max_tx_size, pd->ms_per_isoc);
  215. pd->max_stream_buffering = 3*1024;
  216. pd->pulse_period = OZ_QUANTUM;
  217. pd_set_presleep(pd, body->presleep, 0);
  218. pd_set_keepalive(pd, body->keep_alive);
  219. new_apps &= le16_to_cpu(get_unaligned(&body->apps));
  220. if ((new_apps & 0x1) && (body->session_id)) {
  221. if (pd->session_id) {
  222. if (pd->session_id != body->session_id) {
  223. rsp_status = OZ_STATUS_SESSION_MISMATCH;
  224. goto done;
  225. }
  226. } else {
  227. new_apps &= ~0x1; /* Resume not permitted */
  228. pd->session_id =
  229. oz_get_new_session_id(body->session_id);
  230. }
  231. } else {
  232. if (pd->session_id && !body->session_id) {
  233. rsp_status = OZ_STATUS_SESSION_TEARDOWN;
  234. stop_needed = 1;
  235. } else {
  236. new_apps &= ~0x1; /* Resume not permitted */
  237. pd->session_id =
  238. oz_get_new_session_id(body->session_id);
  239. }
  240. }
  241. done:
  242. if (rsp_status == OZ_STATUS_SUCCESS) {
  243. u16 start_apps = new_apps & ~pd->total_apps & ~0x1;
  244. u16 stop_apps = pd->total_apps & ~new_apps & ~0x1;
  245. u16 resume_apps = new_apps & pd->paused_apps & ~0x1;
  246. spin_unlock_bh(&g_polling_lock);
  247. oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
  248. oz_dbg(ON, "new_apps=0x%x total_apps=0x%x paused_apps=0x%x\n",
  249. new_apps, pd->total_apps, pd->paused_apps);
  250. if (start_apps) {
  251. if (oz_services_start(pd, start_apps, 0))
  252. rsp_status = OZ_STATUS_TOO_MANY_PDS;
  253. }
  254. if (resume_apps)
  255. if (oz_services_start(pd, resume_apps, 1))
  256. rsp_status = OZ_STATUS_TOO_MANY_PDS;
  257. if (stop_apps)
  258. oz_services_stop(pd, stop_apps, 0);
  259. oz_pd_request_heartbeat(pd);
  260. } else {
  261. spin_unlock_bh(&g_polling_lock);
  262. }
  263. oz_send_conn_rsp(pd, rsp_status);
  264. if (rsp_status != OZ_STATUS_SUCCESS) {
  265. if (stop_needed)
  266. oz_pd_stop(pd);
  267. oz_pd_put(pd);
  268. pd = NULL;
  269. }
  270. if (old_net_dev)
  271. dev_put(old_net_dev);
  272. if (free_pd)
  273. oz_pd_destroy(free_pd);
  274. return pd;
  275. }
  276. /*
  277. * Context: softirq-serialized
  278. */
  279. static void oz_add_farewell(struct oz_pd *pd, u8 ep_num, u8 index,
  280. const u8 *report, u8 len)
  281. {
  282. struct oz_farewell *f;
  283. struct oz_farewell *f2;
  284. int found = 0;
  285. f = kmalloc(sizeof(struct oz_farewell) + len, GFP_ATOMIC);
  286. if (!f)
  287. return;
  288. f->ep_num = ep_num;
  289. f->index = index;
  290. f->len = len;
  291. memcpy(f->report, report, len);
  292. oz_dbg(ON, "RX: Adding farewell report\n");
  293. spin_lock(&g_polling_lock);
  294. list_for_each_entry(f2, &pd->farewell_list, link) {
  295. if ((f2->ep_num == ep_num) && (f2->index == index)) {
  296. found = 1;
  297. list_del(&f2->link);
  298. break;
  299. }
  300. }
  301. list_add_tail(&f->link, &pd->farewell_list);
  302. spin_unlock(&g_polling_lock);
  303. if (found)
  304. kfree(f2);
  305. }
  306. /*
  307. * Context: softirq-serialized
  308. */
  309. static void oz_rx_frame(struct sk_buff *skb)
  310. {
  311. u8 *mac_hdr;
  312. u8 *src_addr;
  313. struct oz_elt *elt;
  314. int length;
  315. struct oz_pd *pd = NULL;
  316. struct oz_hdr *oz_hdr = (struct oz_hdr *)skb_network_header(skb);
  317. struct timespec current_time;
  318. int dup = 0;
  319. u32 pkt_num;
  320. oz_dbg(RX_FRAMES, "RX frame PN=0x%x LPN=0x%x control=0x%x\n",
  321. oz_hdr->pkt_num, oz_hdr->last_pkt_num, oz_hdr->control);
  322. mac_hdr = skb_mac_header(skb);
  323. src_addr = &mac_hdr[ETH_ALEN];
  324. length = skb->len;
  325. /* Check the version field */
  326. if (oz_get_prot_ver(oz_hdr->control) != OZ_PROTOCOL_VERSION) {
  327. oz_dbg(ON, "Incorrect protocol version: %d\n",
  328. oz_get_prot_ver(oz_hdr->control));
  329. goto done;
  330. }
  331. pkt_num = le32_to_cpu(get_unaligned(&oz_hdr->pkt_num));
  332. pd = oz_pd_find(src_addr);
  333. if (pd) {
  334. if (!(pd->state & OZ_PD_S_CONNECTED))
  335. oz_pd_set_state(pd, OZ_PD_S_CONNECTED);
  336. getnstimeofday(&current_time);
  337. if ((current_time.tv_sec != pd->last_rx_timestamp.tv_sec) ||
  338. (pd->presleep < MSEC_PER_SEC)) {
  339. oz_timer_add(pd, OZ_TIMER_TOUT, pd->presleep);
  340. pd->last_rx_timestamp = current_time;
  341. }
  342. if (pkt_num != pd->last_rx_pkt_num) {
  343. pd->last_rx_pkt_num = pkt_num;
  344. } else {
  345. dup = 1;
  346. oz_dbg(ON, "Duplicate frame\n");
  347. }
  348. }
  349. if (pd && !dup && ((pd->mode & OZ_MODE_MASK) == OZ_MODE_TRIGGERED)) {
  350. oz_dbg(RX_FRAMES, "Received TRIGGER Frame\n");
  351. pd->last_sent_frame = &pd->tx_queue;
  352. if (oz_hdr->control & OZ_F_ACK) {
  353. /* Retire completed frames */
  354. oz_retire_tx_frames(pd, oz_hdr->last_pkt_num);
  355. }
  356. if ((oz_hdr->control & OZ_F_ACK_REQUESTED) &&
  357. (pd->state == OZ_PD_S_CONNECTED)) {
  358. int backlog = pd->nb_queued_frames;
  359. pd->trigger_pkt_num = pkt_num;
  360. /* Send queued frames */
  361. oz_send_queued_frames(pd, backlog);
  362. }
  363. }
  364. length -= sizeof(struct oz_hdr);
  365. elt = (struct oz_elt *)((u8 *)oz_hdr + sizeof(struct oz_hdr));
  366. while (length >= sizeof(struct oz_elt)) {
  367. length -= sizeof(struct oz_elt) + elt->length;
  368. if (length < 0)
  369. break;
  370. switch (elt->type) {
  371. case OZ_ELT_CONNECT_REQ:
  372. oz_dbg(ON, "RX: OZ_ELT_CONNECT_REQ\n");
  373. pd = oz_connect_req(pd, elt, src_addr, skb->dev);
  374. break;
  375. case OZ_ELT_DISCONNECT:
  376. oz_dbg(ON, "RX: OZ_ELT_DISCONNECT\n");
  377. if (pd)
  378. oz_pd_sleep(pd);
  379. break;
  380. case OZ_ELT_UPDATE_PARAM_REQ: {
  381. struct oz_elt_update_param *body =
  382. (struct oz_elt_update_param *)(elt + 1);
  383. oz_dbg(ON, "RX: OZ_ELT_UPDATE_PARAM_REQ\n");
  384. if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
  385. spin_lock(&g_polling_lock);
  386. pd_set_keepalive(pd, body->keepalive);
  387. pd_set_presleep(pd, body->presleep, 1);
  388. spin_unlock(&g_polling_lock);
  389. }
  390. }
  391. break;
  392. case OZ_ELT_FAREWELL_REQ: {
  393. struct oz_elt_farewell *body =
  394. (struct oz_elt_farewell *)(elt + 1);
  395. oz_dbg(ON, "RX: OZ_ELT_FAREWELL_REQ\n");
  396. oz_add_farewell(pd, body->ep_num,
  397. body->index, body->report,
  398. elt->length + 1 - sizeof(*body));
  399. }
  400. break;
  401. case OZ_ELT_APP_DATA:
  402. if (pd && (pd->state & OZ_PD_S_CONNECTED)) {
  403. struct oz_app_hdr *app_hdr =
  404. (struct oz_app_hdr *)(elt+1);
  405. if (dup)
  406. break;
  407. oz_handle_app_elt(pd, app_hdr->app_id, elt);
  408. }
  409. break;
  410. default:
  411. oz_dbg(ON, "RX: Unknown elt %02x\n", elt->type);
  412. }
  413. elt = oz_next_elt(elt);
  414. }
  415. done:
  416. if (pd)
  417. oz_pd_put(pd);
  418. consume_skb(skb);
  419. }
  420. /*
  421. * Context: process
  422. */
  423. void oz_protocol_term(void)
  424. {
  425. struct oz_binding *b, *t;
  426. /* Walk the list of bindings and remove each one.
  427. */
  428. spin_lock_bh(&g_binding_lock);
  429. list_for_each_entry_safe(b, t, &g_binding, link) {
  430. list_del(&b->link);
  431. spin_unlock_bh(&g_binding_lock);
  432. dev_remove_pack(&b->ptype);
  433. if (b->ptype.dev)
  434. dev_put(b->ptype.dev);
  435. kfree(b);
  436. spin_lock_bh(&g_binding_lock);
  437. }
  438. spin_unlock_bh(&g_binding_lock);
  439. /* Walk the list of PDs and stop each one. This causes the PD to be
  440. * removed from the list so we can just pull each one from the head
  441. * of the list.
  442. */
  443. spin_lock_bh(&g_polling_lock);
  444. while (!list_empty(&g_pd_list)) {
  445. struct oz_pd *pd =
  446. list_first_entry(&g_pd_list, struct oz_pd, link);
  447. oz_pd_get(pd);
  448. spin_unlock_bh(&g_polling_lock);
  449. oz_pd_stop(pd);
  450. oz_pd_put(pd);
  451. spin_lock_bh(&g_polling_lock);
  452. }
  453. spin_unlock_bh(&g_polling_lock);
  454. oz_dbg(ON, "Protocol stopped\n");
  455. kmem_cache_destroy(oz_tx_frame_cache);
  456. kmem_cache_destroy(oz_elt_info_cache);
  457. }
  458. /*
  459. * Context: softirq
  460. */
  461. void oz_pd_heartbeat_handler(unsigned long data)
  462. {
  463. struct oz_pd *pd = (struct oz_pd *)data;
  464. u16 apps = 0;
  465. spin_lock_bh(&g_polling_lock);
  466. if (pd->state & OZ_PD_S_CONNECTED)
  467. apps = pd->total_apps;
  468. spin_unlock_bh(&g_polling_lock);
  469. if (apps)
  470. oz_pd_heartbeat(pd, apps);
  471. oz_pd_put(pd);
  472. }
  473. /*
  474. * Context: softirq
  475. */
  476. void oz_pd_timeout_handler(unsigned long data)
  477. {
  478. int type;
  479. struct oz_pd *pd = (struct oz_pd *)data;
  480. spin_lock_bh(&g_polling_lock);
  481. type = pd->timeout_type;
  482. spin_unlock_bh(&g_polling_lock);
  483. switch (type) {
  484. case OZ_TIMER_TOUT:
  485. oz_pd_sleep(pd);
  486. break;
  487. case OZ_TIMER_STOP:
  488. oz_pd_stop(pd);
  489. break;
  490. }
  491. oz_pd_put(pd);
  492. }
  493. /*
  494. * Context: Interrupt
  495. */
  496. enum hrtimer_restart oz_pd_heartbeat_event(struct hrtimer *timer)
  497. {
  498. struct oz_pd *pd;
  499. pd = container_of(timer, struct oz_pd, heartbeat);
  500. hrtimer_forward_now(timer, ktime_set(pd->pulse_period /
  501. MSEC_PER_SEC, (pd->pulse_period % MSEC_PER_SEC) * NSEC_PER_MSEC));
  502. oz_pd_get(pd);
  503. tasklet_schedule(&pd->heartbeat_tasklet);
  504. return HRTIMER_RESTART;
  505. }
  506. /*
  507. * Context: Interrupt
  508. */
  509. enum hrtimer_restart oz_pd_timeout_event(struct hrtimer *timer)
  510. {
  511. struct oz_pd *pd;
  512. pd = container_of(timer, struct oz_pd, timeout);
  513. oz_pd_get(pd);
  514. tasklet_schedule(&pd->timeout_tasklet);
  515. return HRTIMER_NORESTART;
  516. }
  517. /*
  518. * Context: softirq or process
  519. */
  520. void oz_timer_add(struct oz_pd *pd, int type, unsigned long due_time)
  521. {
  522. spin_lock_bh(&g_polling_lock);
  523. switch (type) {
  524. case OZ_TIMER_TOUT:
  525. case OZ_TIMER_STOP:
  526. if (hrtimer_active(&pd->timeout)) {
  527. hrtimer_set_expires(&pd->timeout, ktime_set(due_time /
  528. MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
  529. NSEC_PER_MSEC));
  530. hrtimer_start_expires(&pd->timeout, HRTIMER_MODE_REL);
  531. } else {
  532. hrtimer_start(&pd->timeout, ktime_set(due_time /
  533. MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
  534. NSEC_PER_MSEC), HRTIMER_MODE_REL);
  535. }
  536. pd->timeout_type = type;
  537. break;
  538. case OZ_TIMER_HEARTBEAT:
  539. if (!hrtimer_active(&pd->heartbeat))
  540. hrtimer_start(&pd->heartbeat, ktime_set(due_time /
  541. MSEC_PER_SEC, (due_time % MSEC_PER_SEC) *
  542. NSEC_PER_MSEC), HRTIMER_MODE_REL);
  543. break;
  544. }
  545. spin_unlock_bh(&g_polling_lock);
  546. }
  547. /*
  548. * Context: softirq or process
  549. */
  550. void oz_pd_request_heartbeat(struct oz_pd *pd)
  551. {
  552. oz_timer_add(pd, OZ_TIMER_HEARTBEAT, pd->pulse_period > 0 ?
  553. pd->pulse_period : OZ_QUANTUM);
  554. }
  555. /*
  556. * Context: softirq or process
  557. */
  558. struct oz_pd *oz_pd_find(const u8 *mac_addr)
  559. {
  560. struct oz_pd *pd;
  561. spin_lock_bh(&g_polling_lock);
  562. list_for_each_entry(pd, &g_pd_list, link) {
  563. if (ether_addr_equal(pd->mac_addr, mac_addr)) {
  564. oz_pd_get(pd);
  565. spin_unlock_bh(&g_polling_lock);
  566. return pd;
  567. }
  568. }
  569. spin_unlock_bh(&g_polling_lock);
  570. return NULL;
  571. }
  572. /*
  573. * Context: process
  574. */
  575. void oz_app_enable(int app_id, int enable)
  576. {
  577. if (app_id < OZ_NB_APPS) {
  578. spin_lock_bh(&g_polling_lock);
  579. if (enable)
  580. g_apps |= (1<<app_id);
  581. else
  582. g_apps &= ~(1<<app_id);
  583. spin_unlock_bh(&g_polling_lock);
  584. }
  585. }
  586. /*
  587. * Context: softirq
  588. */
  589. static int oz_pkt_recv(struct sk_buff *skb, struct net_device *dev,
  590. struct packet_type *pt, struct net_device *orig_dev)
  591. {
  592. skb = skb_share_check(skb, GFP_ATOMIC);
  593. if (skb == NULL)
  594. return 0;
  595. spin_lock_bh(&g_rx_queue.lock);
  596. if (g_processing_rx) {
  597. /* We already hold the lock so use __ variant.
  598. */
  599. __skb_queue_head(&g_rx_queue, skb);
  600. spin_unlock_bh(&g_rx_queue.lock);
  601. } else {
  602. g_processing_rx = 1;
  603. do {
  604. spin_unlock_bh(&g_rx_queue.lock);
  605. oz_rx_frame(skb);
  606. spin_lock_bh(&g_rx_queue.lock);
  607. if (skb_queue_empty(&g_rx_queue)) {
  608. g_processing_rx = 0;
  609. spin_unlock_bh(&g_rx_queue.lock);
  610. break;
  611. }
  612. /* We already hold the lock so use __ variant.
  613. */
  614. skb = __skb_dequeue(&g_rx_queue);
  615. } while (1);
  616. }
  617. return 0;
  618. }
  619. /*
  620. * Context: process
  621. */
  622. void oz_binding_add(const char *net_dev)
  623. {
  624. struct oz_binding *binding;
  625. binding = kzalloc(sizeof(struct oz_binding), GFP_KERNEL);
  626. if (!binding)
  627. return;
  628. binding->ptype.type = htons(OZ_ETHERTYPE);
  629. binding->ptype.func = oz_pkt_recv;
  630. if (net_dev && *net_dev) {
  631. memcpy(binding->name, net_dev, OZ_MAX_BINDING_LEN);
  632. oz_dbg(ON, "Adding binding: %s\n", net_dev);
  633. binding->ptype.dev = dev_get_by_name(&init_net, net_dev);
  634. if (binding->ptype.dev == NULL) {
  635. oz_dbg(ON, "Netdev %s not found\n", net_dev);
  636. kfree(binding);
  637. return;
  638. }
  639. }
  640. dev_add_pack(&binding->ptype);
  641. spin_lock_bh(&g_binding_lock);
  642. list_add_tail(&binding->link, &g_binding);
  643. spin_unlock_bh(&g_binding_lock);
  644. }
  645. /*
  646. * Context: process
  647. */
  648. static void pd_stop_all_for_device(struct net_device *net_dev)
  649. {
  650. LIST_HEAD(h);
  651. struct oz_pd *pd;
  652. struct oz_pd *n;
  653. spin_lock_bh(&g_polling_lock);
  654. list_for_each_entry_safe(pd, n, &g_pd_list, link) {
  655. if (pd->net_dev == net_dev) {
  656. list_move(&pd->link, &h);
  657. oz_pd_get(pd);
  658. }
  659. }
  660. spin_unlock_bh(&g_polling_lock);
  661. while (!list_empty(&h)) {
  662. pd = list_first_entry(&h, struct oz_pd, link);
  663. oz_pd_stop(pd);
  664. oz_pd_put(pd);
  665. }
  666. }
  667. /*
  668. * Context: process
  669. */
  670. void oz_binding_remove(const char *net_dev)
  671. {
  672. struct oz_binding *binding;
  673. int found = 0;
  674. oz_dbg(ON, "Removing binding: %s\n", net_dev);
  675. spin_lock_bh(&g_binding_lock);
  676. list_for_each_entry(binding, &g_binding, link) {
  677. if (strncmp(binding->name, net_dev, OZ_MAX_BINDING_LEN) == 0) {
  678. oz_dbg(ON, "Binding '%s' found\n", net_dev);
  679. found = 1;
  680. break;
  681. }
  682. }
  683. spin_unlock_bh(&g_binding_lock);
  684. if (found) {
  685. dev_remove_pack(&binding->ptype);
  686. if (binding->ptype.dev) {
  687. dev_put(binding->ptype.dev);
  688. pd_stop_all_for_device(binding->ptype.dev);
  689. }
  690. list_del(&binding->link);
  691. kfree(binding);
  692. }
  693. }
  694. /*
  695. * Context: process
  696. */
  697. static char *oz_get_next_device_name(char *s, char *dname, int max_size)
  698. {
  699. while (*s == ',')
  700. s++;
  701. while (*s && (*s != ',') && max_size > 1) {
  702. *dname++ = *s++;
  703. max_size--;
  704. }
  705. *dname = 0;
  706. return s;
  707. }
  708. /*
  709. * Context: process
  710. */
  711. int oz_protocol_init(char *devs)
  712. {
  713. oz_elt_info_cache = KMEM_CACHE(oz_elt_info, 0);
  714. if (!oz_elt_info_cache)
  715. return -ENOMEM;
  716. oz_tx_frame_cache = KMEM_CACHE(oz_tx_frame, 0);
  717. if (!oz_tx_frame_cache) {
  718. kmem_cache_destroy(oz_elt_info_cache);
  719. return -ENOMEM;
  720. }
  721. skb_queue_head_init(&g_rx_queue);
  722. if (devs[0] == '*') {
  723. oz_binding_add(NULL);
  724. } else {
  725. char d[32];
  726. while (*devs) {
  727. devs = oz_get_next_device_name(devs, d, sizeof(d));
  728. if (d[0])
  729. oz_binding_add(d);
  730. }
  731. }
  732. return 0;
  733. }
  734. /*
  735. * Context: process
  736. */
  737. int oz_get_pd_list(struct oz_mac_addr *addr, int max_count)
  738. {
  739. struct oz_pd *pd;
  740. int count = 0;
  741. spin_lock_bh(&g_polling_lock);
  742. list_for_each_entry(pd, &g_pd_list, link) {
  743. if (count >= max_count)
  744. break;
  745. ether_addr_copy((u8 *)&addr[count++], pd->mac_addr);
  746. }
  747. spin_unlock_bh(&g_polling_lock);
  748. return count;
  749. }