net.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  1. /*
  2. * IPv4 over IEEE 1394, per RFC 2734
  3. * IPv6 over IEEE 1394, per RFC 3146
  4. *
  5. * Copyright (C) 2009 Jay Fenlason <fenlason@redhat.com>
  6. *
  7. * based on eth1394 by Ben Collins et al
  8. */
  9. #include <linux/bug.h>
  10. #include <linux/compiler.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/ethtool.h>
  14. #include <linux/firewire.h>
  15. #include <linux/firewire-constants.h>
  16. #include <linux/highmem.h>
  17. #include <linux/in.h>
  18. #include <linux/ip.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/mod_devicetable.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/mutex.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <asm/unaligned.h>
  29. #include <net/arp.h>
  30. #include <net/firewire.h>
  31. /* rx limits */
  32. #define FWNET_MAX_FRAGMENTS 30 /* arbitrary, > TX queue depth */
  33. #define FWNET_ISO_PAGE_COUNT (PAGE_SIZE < 16*1024 ? 4 : 2)
  34. /* tx limits */
  35. #define FWNET_MAX_QUEUED_DATAGRAMS 20 /* < 64 = number of tlabels */
  36. #define FWNET_MIN_QUEUED_DATAGRAMS 10 /* should keep AT DMA busy enough */
  37. #define FWNET_TX_QUEUE_LEN FWNET_MAX_QUEUED_DATAGRAMS /* ? */
  38. #define IEEE1394_BROADCAST_CHANNEL 31
  39. #define IEEE1394_ALL_NODES (0xffc0 | 0x003f)
  40. #define IEEE1394_MAX_PAYLOAD_S100 512
  41. #define FWNET_NO_FIFO_ADDR (~0ULL)
  42. #define IANA_SPECIFIER_ID 0x00005eU
  43. #define RFC2734_SW_VERSION 0x000001U
  44. #define RFC3146_SW_VERSION 0x000002U
  45. #define IEEE1394_GASP_HDR_SIZE 8
  46. #define RFC2374_UNFRAG_HDR_SIZE 4
  47. #define RFC2374_FRAG_HDR_SIZE 8
  48. #define RFC2374_FRAG_OVERHEAD 4
  49. #define RFC2374_HDR_UNFRAG 0 /* unfragmented */
  50. #define RFC2374_HDR_FIRSTFRAG 1 /* first fragment */
  51. #define RFC2374_HDR_LASTFRAG 2 /* last fragment */
  52. #define RFC2374_HDR_INTFRAG 3 /* interior fragment */
  53. static bool fwnet_hwaddr_is_multicast(u8 *ha)
  54. {
  55. return !!(*ha & 1);
  56. }
  57. /* IPv4 and IPv6 encapsulation header */
  58. struct rfc2734_header {
  59. u32 w0;
  60. u32 w1;
  61. };
  62. #define fwnet_get_hdr_lf(h) (((h)->w0 & 0xc0000000) >> 30)
  63. #define fwnet_get_hdr_ether_type(h) (((h)->w0 & 0x0000ffff))
  64. #define fwnet_get_hdr_dg_size(h) ((((h)->w0 & 0x0fff0000) >> 16) + 1)
  65. #define fwnet_get_hdr_fg_off(h) (((h)->w0 & 0x00000fff))
  66. #define fwnet_get_hdr_dgl(h) (((h)->w1 & 0xffff0000) >> 16)
  67. #define fwnet_set_hdr_lf(lf) ((lf) << 30)
  68. #define fwnet_set_hdr_ether_type(et) (et)
  69. #define fwnet_set_hdr_dg_size(dgs) (((dgs) - 1) << 16)
  70. #define fwnet_set_hdr_fg_off(fgo) (fgo)
  71. #define fwnet_set_hdr_dgl(dgl) ((dgl) << 16)
  72. static inline void fwnet_make_uf_hdr(struct rfc2734_header *hdr,
  73. unsigned ether_type)
  74. {
  75. hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_UNFRAG)
  76. | fwnet_set_hdr_ether_type(ether_type);
  77. }
  78. static inline void fwnet_make_ff_hdr(struct rfc2734_header *hdr,
  79. unsigned ether_type, unsigned dg_size, unsigned dgl)
  80. {
  81. hdr->w0 = fwnet_set_hdr_lf(RFC2374_HDR_FIRSTFRAG)
  82. | fwnet_set_hdr_dg_size(dg_size)
  83. | fwnet_set_hdr_ether_type(ether_type);
  84. hdr->w1 = fwnet_set_hdr_dgl(dgl);
  85. }
  86. static inline void fwnet_make_sf_hdr(struct rfc2734_header *hdr,
  87. unsigned lf, unsigned dg_size, unsigned fg_off, unsigned dgl)
  88. {
  89. hdr->w0 = fwnet_set_hdr_lf(lf)
  90. | fwnet_set_hdr_dg_size(dg_size)
  91. | fwnet_set_hdr_fg_off(fg_off);
  92. hdr->w1 = fwnet_set_hdr_dgl(dgl);
  93. }
  94. /* This list keeps track of what parts of the datagram have been filled in */
  95. struct fwnet_fragment_info {
  96. struct list_head fi_link;
  97. u16 offset;
  98. u16 len;
  99. };
  100. struct fwnet_partial_datagram {
  101. struct list_head pd_link;
  102. struct list_head fi_list;
  103. struct sk_buff *skb;
  104. /* FIXME Why not use skb->data? */
  105. char *pbuf;
  106. u16 datagram_label;
  107. u16 ether_type;
  108. u16 datagram_size;
  109. };
  110. static DEFINE_MUTEX(fwnet_device_mutex);
  111. static LIST_HEAD(fwnet_device_list);
  112. struct fwnet_device {
  113. struct list_head dev_link;
  114. spinlock_t lock;
  115. enum {
  116. FWNET_BROADCAST_ERROR,
  117. FWNET_BROADCAST_RUNNING,
  118. FWNET_BROADCAST_STOPPED,
  119. } broadcast_state;
  120. struct fw_iso_context *broadcast_rcv_context;
  121. struct fw_iso_buffer broadcast_rcv_buffer;
  122. void **broadcast_rcv_buffer_ptrs;
  123. unsigned broadcast_rcv_next_ptr;
  124. unsigned num_broadcast_rcv_ptrs;
  125. unsigned rcv_buffer_size;
  126. /*
  127. * This value is the maximum unfragmented datagram size that can be
  128. * sent by the hardware. It already has the GASP overhead and the
  129. * unfragmented datagram header overhead calculated into it.
  130. */
  131. unsigned broadcast_xmt_max_payload;
  132. u16 broadcast_xmt_datagramlabel;
  133. /*
  134. * The CSR address that remote nodes must send datagrams to for us to
  135. * receive them.
  136. */
  137. struct fw_address_handler handler;
  138. u64 local_fifo;
  139. /* Number of tx datagrams that have been queued but not yet acked */
  140. int queued_datagrams;
  141. int peer_count;
  142. struct list_head peer_list;
  143. struct fw_card *card;
  144. struct net_device *netdev;
  145. };
  146. struct fwnet_peer {
  147. struct list_head peer_link;
  148. struct fwnet_device *dev;
  149. u64 guid;
  150. /* guarded by dev->lock */
  151. struct list_head pd_list; /* received partial datagrams */
  152. unsigned pdg_size; /* pd_list size */
  153. u16 datagram_label; /* outgoing datagram label */
  154. u16 max_payload; /* includes RFC2374_FRAG_HDR_SIZE overhead */
  155. int node_id;
  156. int generation;
  157. unsigned speed;
  158. };
  159. /* This is our task struct. It's used for the packet complete callback. */
  160. struct fwnet_packet_task {
  161. struct fw_transaction transaction;
  162. struct rfc2734_header hdr;
  163. struct sk_buff *skb;
  164. struct fwnet_device *dev;
  165. int outstanding_pkts;
  166. u64 fifo_addr;
  167. u16 dest_node;
  168. u16 max_payload;
  169. u8 generation;
  170. u8 speed;
  171. u8 enqueued;
  172. };
  173. /*
  174. * Get fifo address embedded in hwaddr
  175. */
  176. static __u64 fwnet_hwaddr_fifo(union fwnet_hwaddr *ha)
  177. {
  178. return (u64)get_unaligned_be16(&ha->uc.fifo_hi) << 32
  179. | get_unaligned_be32(&ha->uc.fifo_lo);
  180. }
  181. /*
  182. * saddr == NULL means use device source address.
  183. * daddr == NULL means leave destination address (eg unresolved arp).
  184. */
  185. static int fwnet_header_create(struct sk_buff *skb, struct net_device *net,
  186. unsigned short type, const void *daddr,
  187. const void *saddr, unsigned len)
  188. {
  189. struct fwnet_header *h;
  190. h = skb_push(skb, sizeof(*h));
  191. put_unaligned_be16(type, &h->h_proto);
  192. if (net->flags & (IFF_LOOPBACK | IFF_NOARP)) {
  193. memset(h->h_dest, 0, net->addr_len);
  194. return net->hard_header_len;
  195. }
  196. if (daddr) {
  197. memcpy(h->h_dest, daddr, net->addr_len);
  198. return net->hard_header_len;
  199. }
  200. return -net->hard_header_len;
  201. }
  202. static int fwnet_header_cache(const struct neighbour *neigh,
  203. struct hh_cache *hh, __be16 type)
  204. {
  205. struct net_device *net;
  206. struct fwnet_header *h;
  207. if (type == cpu_to_be16(ETH_P_802_3))
  208. return -1;
  209. net = neigh->dev;
  210. h = (struct fwnet_header *)((u8 *)hh->hh_data + HH_DATA_OFF(sizeof(*h)));
  211. h->h_proto = type;
  212. memcpy(h->h_dest, neigh->ha, net->addr_len);
  213. /* Pairs with the READ_ONCE() in neigh_resolve_output(),
  214. * neigh_hh_output() and neigh_update_hhs().
  215. */
  216. smp_store_release(&hh->hh_len, FWNET_HLEN);
  217. return 0;
  218. }
  219. /* Called by Address Resolution module to notify changes in address. */
  220. static void fwnet_header_cache_update(struct hh_cache *hh,
  221. const struct net_device *net, const unsigned char *haddr)
  222. {
  223. memcpy((u8 *)hh->hh_data + HH_DATA_OFF(FWNET_HLEN), haddr, net->addr_len);
  224. }
  225. static int fwnet_header_parse(const struct sk_buff *skb, unsigned char *haddr)
  226. {
  227. memcpy(haddr, skb->dev->dev_addr, FWNET_ALEN);
  228. return FWNET_ALEN;
  229. }
  230. static const struct header_ops fwnet_header_ops = {
  231. .create = fwnet_header_create,
  232. .cache = fwnet_header_cache,
  233. .cache_update = fwnet_header_cache_update,
  234. .parse = fwnet_header_parse,
  235. };
  236. /* FIXME: is this correct for all cases? */
  237. static bool fwnet_frag_overlap(struct fwnet_partial_datagram *pd,
  238. unsigned offset, unsigned len)
  239. {
  240. struct fwnet_fragment_info *fi;
  241. unsigned end = offset + len;
  242. list_for_each_entry(fi, &pd->fi_list, fi_link)
  243. if (offset < fi->offset + fi->len && end > fi->offset)
  244. return true;
  245. return false;
  246. }
  247. /* Assumes that new fragment does not overlap any existing fragments */
  248. static struct fwnet_fragment_info *fwnet_frag_new(
  249. struct fwnet_partial_datagram *pd, unsigned offset, unsigned len)
  250. {
  251. struct fwnet_fragment_info *fi, *fi2, *new;
  252. struct list_head *list;
  253. list = &pd->fi_list;
  254. list_for_each_entry(fi, &pd->fi_list, fi_link) {
  255. if (fi->offset + fi->len == offset) {
  256. /* The new fragment can be tacked on to the end */
  257. /* Did the new fragment plug a hole? */
  258. fi2 = list_entry(fi->fi_link.next,
  259. struct fwnet_fragment_info, fi_link);
  260. if (fi->offset + fi->len == fi2->offset) {
  261. /* glue fragments together */
  262. fi->len += len + fi2->len;
  263. list_del(&fi2->fi_link);
  264. kfree(fi2);
  265. } else {
  266. fi->len += len;
  267. }
  268. return fi;
  269. }
  270. if (offset + len == fi->offset) {
  271. /* The new fragment can be tacked on to the beginning */
  272. /* Did the new fragment plug a hole? */
  273. fi2 = list_entry(fi->fi_link.prev,
  274. struct fwnet_fragment_info, fi_link);
  275. if (fi2->offset + fi2->len == fi->offset) {
  276. /* glue fragments together */
  277. fi2->len += fi->len + len;
  278. list_del(&fi->fi_link);
  279. kfree(fi);
  280. return fi2;
  281. }
  282. fi->offset = offset;
  283. fi->len += len;
  284. return fi;
  285. }
  286. if (offset > fi->offset + fi->len) {
  287. list = &fi->fi_link;
  288. break;
  289. }
  290. if (offset + len < fi->offset) {
  291. list = fi->fi_link.prev;
  292. break;
  293. }
  294. }
  295. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  296. if (!new)
  297. return NULL;
  298. new->offset = offset;
  299. new->len = len;
  300. list_add(&new->fi_link, list);
  301. return new;
  302. }
  303. static struct fwnet_partial_datagram *fwnet_pd_new(struct net_device *net,
  304. struct fwnet_peer *peer, u16 datagram_label, unsigned dg_size,
  305. void *frag_buf, unsigned frag_off, unsigned frag_len)
  306. {
  307. struct fwnet_partial_datagram *new;
  308. struct fwnet_fragment_info *fi;
  309. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  310. if (!new)
  311. goto fail;
  312. INIT_LIST_HEAD(&new->fi_list);
  313. fi = fwnet_frag_new(new, frag_off, frag_len);
  314. if (fi == NULL)
  315. goto fail_w_new;
  316. new->datagram_label = datagram_label;
  317. new->datagram_size = dg_size;
  318. new->skb = dev_alloc_skb(dg_size + LL_RESERVED_SPACE(net));
  319. if (new->skb == NULL)
  320. goto fail_w_fi;
  321. skb_reserve(new->skb, LL_RESERVED_SPACE(net));
  322. new->pbuf = skb_put(new->skb, dg_size);
  323. memcpy(new->pbuf + frag_off, frag_buf, frag_len);
  324. list_add_tail(&new->pd_link, &peer->pd_list);
  325. return new;
  326. fail_w_fi:
  327. kfree(fi);
  328. fail_w_new:
  329. kfree(new);
  330. fail:
  331. return NULL;
  332. }
  333. static struct fwnet_partial_datagram *fwnet_pd_find(struct fwnet_peer *peer,
  334. u16 datagram_label)
  335. {
  336. struct fwnet_partial_datagram *pd;
  337. list_for_each_entry(pd, &peer->pd_list, pd_link)
  338. if (pd->datagram_label == datagram_label)
  339. return pd;
  340. return NULL;
  341. }
  342. static void fwnet_pd_delete(struct fwnet_partial_datagram *old)
  343. {
  344. struct fwnet_fragment_info *fi, *n;
  345. list_for_each_entry_safe(fi, n, &old->fi_list, fi_link)
  346. kfree(fi);
  347. list_del(&old->pd_link);
  348. dev_kfree_skb_any(old->skb);
  349. kfree(old);
  350. }
  351. static bool fwnet_pd_update(struct fwnet_peer *peer,
  352. struct fwnet_partial_datagram *pd, void *frag_buf,
  353. unsigned frag_off, unsigned frag_len)
  354. {
  355. if (fwnet_frag_new(pd, frag_off, frag_len) == NULL)
  356. return false;
  357. memcpy(pd->pbuf + frag_off, frag_buf, frag_len);
  358. /*
  359. * Move list entry to beginning of list so that oldest partial
  360. * datagrams percolate to the end of the list
  361. */
  362. list_move_tail(&pd->pd_link, &peer->pd_list);
  363. return true;
  364. }
  365. static bool fwnet_pd_is_complete(struct fwnet_partial_datagram *pd)
  366. {
  367. struct fwnet_fragment_info *fi;
  368. fi = list_entry(pd->fi_list.next, struct fwnet_fragment_info, fi_link);
  369. return fi->len == pd->datagram_size;
  370. }
  371. /* caller must hold dev->lock */
  372. static struct fwnet_peer *fwnet_peer_find_by_guid(struct fwnet_device *dev,
  373. u64 guid)
  374. {
  375. struct fwnet_peer *peer;
  376. list_for_each_entry(peer, &dev->peer_list, peer_link)
  377. if (peer->guid == guid)
  378. return peer;
  379. return NULL;
  380. }
  381. /* caller must hold dev->lock */
  382. static struct fwnet_peer *fwnet_peer_find_by_node_id(struct fwnet_device *dev,
  383. int node_id, int generation)
  384. {
  385. struct fwnet_peer *peer;
  386. list_for_each_entry(peer, &dev->peer_list, peer_link)
  387. if (peer->node_id == node_id &&
  388. peer->generation == generation)
  389. return peer;
  390. return NULL;
  391. }
  392. /* See IEEE 1394-2008 table 6-4, table 8-8, table 16-18. */
  393. static unsigned fwnet_max_payload(unsigned max_rec, unsigned speed)
  394. {
  395. max_rec = min(max_rec, speed + 8);
  396. max_rec = clamp(max_rec, 8U, 11U); /* 512...4096 */
  397. return (1 << (max_rec + 1)) - RFC2374_FRAG_HDR_SIZE;
  398. }
  399. static int fwnet_finish_incoming_packet(struct net_device *net,
  400. struct sk_buff *skb, u16 source_node_id,
  401. bool is_broadcast, u16 ether_type)
  402. {
  403. struct fwnet_device *dev;
  404. int status;
  405. __be64 guid;
  406. switch (ether_type) {
  407. case ETH_P_ARP:
  408. case ETH_P_IP:
  409. #if IS_ENABLED(CONFIG_IPV6)
  410. case ETH_P_IPV6:
  411. #endif
  412. break;
  413. default:
  414. goto err;
  415. }
  416. dev = netdev_priv(net);
  417. /* Write metadata, and then pass to the receive level */
  418. skb->dev = net;
  419. skb->ip_summed = CHECKSUM_NONE;
  420. /*
  421. * Parse the encapsulation header. This actually does the job of
  422. * converting to an ethernet-like pseudo frame header.
  423. */
  424. guid = cpu_to_be64(dev->card->guid);
  425. if (dev_hard_header(skb, net, ether_type,
  426. is_broadcast ? net->broadcast : net->dev_addr,
  427. NULL, skb->len) >= 0) {
  428. struct fwnet_header *eth;
  429. u16 *rawp;
  430. __be16 protocol;
  431. skb_reset_mac_header(skb);
  432. skb_pull(skb, sizeof(*eth));
  433. eth = (struct fwnet_header *)skb_mac_header(skb);
  434. if (fwnet_hwaddr_is_multicast(eth->h_dest)) {
  435. if (memcmp(eth->h_dest, net->broadcast,
  436. net->addr_len) == 0)
  437. skb->pkt_type = PACKET_BROADCAST;
  438. #if 0
  439. else
  440. skb->pkt_type = PACKET_MULTICAST;
  441. #endif
  442. } else {
  443. if (memcmp(eth->h_dest, net->dev_addr, net->addr_len))
  444. skb->pkt_type = PACKET_OTHERHOST;
  445. }
  446. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) {
  447. protocol = eth->h_proto;
  448. } else {
  449. rawp = (u16 *)skb->data;
  450. if (*rawp == 0xffff)
  451. protocol = htons(ETH_P_802_3);
  452. else
  453. protocol = htons(ETH_P_802_2);
  454. }
  455. skb->protocol = protocol;
  456. }
  457. status = netif_rx(skb);
  458. if (status == NET_RX_DROP) {
  459. net->stats.rx_errors++;
  460. net->stats.rx_dropped++;
  461. } else {
  462. net->stats.rx_packets++;
  463. net->stats.rx_bytes += skb->len;
  464. }
  465. return 0;
  466. err:
  467. net->stats.rx_errors++;
  468. net->stats.rx_dropped++;
  469. dev_kfree_skb_any(skb);
  470. return -ENOENT;
  471. }
  472. static int fwnet_incoming_packet(struct fwnet_device *dev, __be32 *buf, int len,
  473. int source_node_id, int generation,
  474. bool is_broadcast)
  475. {
  476. struct sk_buff *skb;
  477. struct net_device *net = dev->netdev;
  478. struct rfc2734_header hdr;
  479. unsigned lf;
  480. unsigned long flags;
  481. struct fwnet_peer *peer;
  482. struct fwnet_partial_datagram *pd;
  483. int fg_off;
  484. int dg_size;
  485. u16 datagram_label;
  486. int retval;
  487. u16 ether_type;
  488. if (len <= RFC2374_UNFRAG_HDR_SIZE)
  489. return 0;
  490. hdr.w0 = be32_to_cpu(buf[0]);
  491. lf = fwnet_get_hdr_lf(&hdr);
  492. if (lf == RFC2374_HDR_UNFRAG) {
  493. /*
  494. * An unfragmented datagram has been received by the ieee1394
  495. * bus. Build an skbuff around it so we can pass it to the
  496. * high level network layer.
  497. */
  498. ether_type = fwnet_get_hdr_ether_type(&hdr);
  499. buf++;
  500. len -= RFC2374_UNFRAG_HDR_SIZE;
  501. skb = dev_alloc_skb(len + LL_RESERVED_SPACE(net));
  502. if (unlikely(!skb)) {
  503. net->stats.rx_dropped++;
  504. return -ENOMEM;
  505. }
  506. skb_reserve(skb, LL_RESERVED_SPACE(net));
  507. skb_put_data(skb, buf, len);
  508. return fwnet_finish_incoming_packet(net, skb, source_node_id,
  509. is_broadcast, ether_type);
  510. }
  511. /* A datagram fragment has been received, now the fun begins. */
  512. if (len <= RFC2374_FRAG_HDR_SIZE)
  513. return 0;
  514. hdr.w1 = ntohl(buf[1]);
  515. buf += 2;
  516. len -= RFC2374_FRAG_HDR_SIZE;
  517. if (lf == RFC2374_HDR_FIRSTFRAG) {
  518. ether_type = fwnet_get_hdr_ether_type(&hdr);
  519. fg_off = 0;
  520. } else {
  521. ether_type = 0;
  522. fg_off = fwnet_get_hdr_fg_off(&hdr);
  523. }
  524. datagram_label = fwnet_get_hdr_dgl(&hdr);
  525. dg_size = fwnet_get_hdr_dg_size(&hdr);
  526. if (fg_off + len > dg_size)
  527. return 0;
  528. spin_lock_irqsave(&dev->lock, flags);
  529. peer = fwnet_peer_find_by_node_id(dev, source_node_id, generation);
  530. if (!peer) {
  531. retval = -ENOENT;
  532. goto fail;
  533. }
  534. pd = fwnet_pd_find(peer, datagram_label);
  535. if (pd == NULL) {
  536. while (peer->pdg_size >= FWNET_MAX_FRAGMENTS) {
  537. /* remove the oldest */
  538. fwnet_pd_delete(list_first_entry(&peer->pd_list,
  539. struct fwnet_partial_datagram, pd_link));
  540. peer->pdg_size--;
  541. }
  542. pd = fwnet_pd_new(net, peer, datagram_label,
  543. dg_size, buf, fg_off, len);
  544. if (pd == NULL) {
  545. retval = -ENOMEM;
  546. goto fail;
  547. }
  548. peer->pdg_size++;
  549. } else {
  550. if (fwnet_frag_overlap(pd, fg_off, len) ||
  551. pd->datagram_size != dg_size) {
  552. /*
  553. * Differing datagram sizes or overlapping fragments,
  554. * discard old datagram and start a new one.
  555. */
  556. fwnet_pd_delete(pd);
  557. pd = fwnet_pd_new(net, peer, datagram_label,
  558. dg_size, buf, fg_off, len);
  559. if (pd == NULL) {
  560. peer->pdg_size--;
  561. retval = -ENOMEM;
  562. goto fail;
  563. }
  564. } else {
  565. if (!fwnet_pd_update(peer, pd, buf, fg_off, len)) {
  566. /*
  567. * Couldn't save off fragment anyway
  568. * so might as well obliterate the
  569. * datagram now.
  570. */
  571. fwnet_pd_delete(pd);
  572. peer->pdg_size--;
  573. retval = -ENOMEM;
  574. goto fail;
  575. }
  576. }
  577. } /* new datagram or add to existing one */
  578. if (lf == RFC2374_HDR_FIRSTFRAG)
  579. pd->ether_type = ether_type;
  580. if (fwnet_pd_is_complete(pd)) {
  581. ether_type = pd->ether_type;
  582. peer->pdg_size--;
  583. skb = skb_get(pd->skb);
  584. fwnet_pd_delete(pd);
  585. spin_unlock_irqrestore(&dev->lock, flags);
  586. return fwnet_finish_incoming_packet(net, skb, source_node_id,
  587. false, ether_type);
  588. }
  589. /*
  590. * Datagram is not complete, we're done for the
  591. * moment.
  592. */
  593. retval = 0;
  594. fail:
  595. spin_unlock_irqrestore(&dev->lock, flags);
  596. return retval;
  597. }
  598. static void fwnet_receive_packet(struct fw_card *card, struct fw_request *r,
  599. int tcode, int destination, int source, int generation,
  600. unsigned long long offset, void *payload, size_t length,
  601. void *callback_data)
  602. {
  603. struct fwnet_device *dev = callback_data;
  604. int rcode;
  605. if (destination == IEEE1394_ALL_NODES) {
  606. kfree(r);
  607. return;
  608. }
  609. if (offset != dev->handler.offset)
  610. rcode = RCODE_ADDRESS_ERROR;
  611. else if (tcode != TCODE_WRITE_BLOCK_REQUEST)
  612. rcode = RCODE_TYPE_ERROR;
  613. else if (fwnet_incoming_packet(dev, payload, length,
  614. source, generation, false) != 0) {
  615. dev_err(&dev->netdev->dev, "incoming packet failure\n");
  616. rcode = RCODE_CONFLICT_ERROR;
  617. } else
  618. rcode = RCODE_COMPLETE;
  619. fw_send_response(card, r, rcode);
  620. }
  621. static int gasp_source_id(__be32 *p)
  622. {
  623. return be32_to_cpu(p[0]) >> 16;
  624. }
  625. static u32 gasp_specifier_id(__be32 *p)
  626. {
  627. return (be32_to_cpu(p[0]) & 0xffff) << 8 |
  628. (be32_to_cpu(p[1]) & 0xff000000) >> 24;
  629. }
  630. static u32 gasp_version(__be32 *p)
  631. {
  632. return be32_to_cpu(p[1]) & 0xffffff;
  633. }
  634. static void fwnet_receive_broadcast(struct fw_iso_context *context,
  635. u32 cycle, size_t header_length, void *header, void *data)
  636. {
  637. struct fwnet_device *dev;
  638. struct fw_iso_packet packet;
  639. __be16 *hdr_ptr;
  640. __be32 *buf_ptr;
  641. int retval;
  642. u32 length;
  643. unsigned long offset;
  644. unsigned long flags;
  645. dev = data;
  646. hdr_ptr = header;
  647. length = be16_to_cpup(hdr_ptr);
  648. spin_lock_irqsave(&dev->lock, flags);
  649. offset = dev->rcv_buffer_size * dev->broadcast_rcv_next_ptr;
  650. buf_ptr = dev->broadcast_rcv_buffer_ptrs[dev->broadcast_rcv_next_ptr++];
  651. if (dev->broadcast_rcv_next_ptr == dev->num_broadcast_rcv_ptrs)
  652. dev->broadcast_rcv_next_ptr = 0;
  653. spin_unlock_irqrestore(&dev->lock, flags);
  654. if (length > IEEE1394_GASP_HDR_SIZE &&
  655. gasp_specifier_id(buf_ptr) == IANA_SPECIFIER_ID &&
  656. (gasp_version(buf_ptr) == RFC2734_SW_VERSION
  657. #if IS_ENABLED(CONFIG_IPV6)
  658. || gasp_version(buf_ptr) == RFC3146_SW_VERSION
  659. #endif
  660. ))
  661. fwnet_incoming_packet(dev, buf_ptr + 2,
  662. length - IEEE1394_GASP_HDR_SIZE,
  663. gasp_source_id(buf_ptr),
  664. context->card->generation, true);
  665. packet.payload_length = dev->rcv_buffer_size;
  666. packet.interrupt = 1;
  667. packet.skip = 0;
  668. packet.tag = 3;
  669. packet.sy = 0;
  670. packet.header_length = IEEE1394_GASP_HDR_SIZE;
  671. spin_lock_irqsave(&dev->lock, flags);
  672. retval = fw_iso_context_queue(dev->broadcast_rcv_context, &packet,
  673. &dev->broadcast_rcv_buffer, offset);
  674. spin_unlock_irqrestore(&dev->lock, flags);
  675. if (retval >= 0)
  676. fw_iso_context_queue_flush(dev->broadcast_rcv_context);
  677. else
  678. dev_err(&dev->netdev->dev, "requeue failed\n");
  679. }
  680. static struct kmem_cache *fwnet_packet_task_cache;
  681. static void fwnet_free_ptask(struct fwnet_packet_task *ptask)
  682. {
  683. dev_kfree_skb_any(ptask->skb);
  684. kmem_cache_free(fwnet_packet_task_cache, ptask);
  685. }
  686. /* Caller must hold dev->lock. */
  687. static void dec_queued_datagrams(struct fwnet_device *dev)
  688. {
  689. if (--dev->queued_datagrams == FWNET_MIN_QUEUED_DATAGRAMS)
  690. netif_wake_queue(dev->netdev);
  691. }
  692. static int fwnet_send_packet(struct fwnet_packet_task *ptask);
  693. static void fwnet_transmit_packet_done(struct fwnet_packet_task *ptask)
  694. {
  695. struct fwnet_device *dev = ptask->dev;
  696. struct sk_buff *skb = ptask->skb;
  697. unsigned long flags;
  698. bool free;
  699. spin_lock_irqsave(&dev->lock, flags);
  700. ptask->outstanding_pkts--;
  701. /* Check whether we or the networking TX soft-IRQ is last user. */
  702. free = (ptask->outstanding_pkts == 0 && ptask->enqueued);
  703. if (free)
  704. dec_queued_datagrams(dev);
  705. if (ptask->outstanding_pkts == 0) {
  706. dev->netdev->stats.tx_packets++;
  707. dev->netdev->stats.tx_bytes += skb->len;
  708. }
  709. spin_unlock_irqrestore(&dev->lock, flags);
  710. if (ptask->outstanding_pkts > 0) {
  711. u16 dg_size;
  712. u16 fg_off;
  713. u16 datagram_label;
  714. u16 lf;
  715. /* Update the ptask to point to the next fragment and send it */
  716. lf = fwnet_get_hdr_lf(&ptask->hdr);
  717. switch (lf) {
  718. case RFC2374_HDR_LASTFRAG:
  719. case RFC2374_HDR_UNFRAG:
  720. default:
  721. dev_err(&dev->netdev->dev,
  722. "outstanding packet %x lf %x, header %x,%x\n",
  723. ptask->outstanding_pkts, lf, ptask->hdr.w0,
  724. ptask->hdr.w1);
  725. BUG();
  726. case RFC2374_HDR_FIRSTFRAG:
  727. /* Set frag type here for future interior fragments */
  728. dg_size = fwnet_get_hdr_dg_size(&ptask->hdr);
  729. fg_off = ptask->max_payload - RFC2374_FRAG_HDR_SIZE;
  730. datagram_label = fwnet_get_hdr_dgl(&ptask->hdr);
  731. break;
  732. case RFC2374_HDR_INTFRAG:
  733. dg_size = fwnet_get_hdr_dg_size(&ptask->hdr);
  734. fg_off = fwnet_get_hdr_fg_off(&ptask->hdr)
  735. + ptask->max_payload - RFC2374_FRAG_HDR_SIZE;
  736. datagram_label = fwnet_get_hdr_dgl(&ptask->hdr);
  737. break;
  738. }
  739. if (ptask->dest_node == IEEE1394_ALL_NODES) {
  740. skb_pull(skb,
  741. ptask->max_payload + IEEE1394_GASP_HDR_SIZE);
  742. } else {
  743. skb_pull(skb, ptask->max_payload);
  744. }
  745. if (ptask->outstanding_pkts > 1) {
  746. fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_INTFRAG,
  747. dg_size, fg_off, datagram_label);
  748. } else {
  749. fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_LASTFRAG,
  750. dg_size, fg_off, datagram_label);
  751. ptask->max_payload = skb->len + RFC2374_FRAG_HDR_SIZE;
  752. }
  753. fwnet_send_packet(ptask);
  754. }
  755. if (free)
  756. fwnet_free_ptask(ptask);
  757. }
  758. static void fwnet_transmit_packet_failed(struct fwnet_packet_task *ptask)
  759. {
  760. struct fwnet_device *dev = ptask->dev;
  761. unsigned long flags;
  762. bool free;
  763. spin_lock_irqsave(&dev->lock, flags);
  764. /* One fragment failed; don't try to send remaining fragments. */
  765. ptask->outstanding_pkts = 0;
  766. /* Check whether we or the networking TX soft-IRQ is last user. */
  767. free = ptask->enqueued;
  768. if (free)
  769. dec_queued_datagrams(dev);
  770. dev->netdev->stats.tx_dropped++;
  771. dev->netdev->stats.tx_errors++;
  772. spin_unlock_irqrestore(&dev->lock, flags);
  773. if (free)
  774. fwnet_free_ptask(ptask);
  775. }
  776. static void fwnet_write_complete(struct fw_card *card, int rcode,
  777. void *payload, size_t length, void *data)
  778. {
  779. struct fwnet_packet_task *ptask = data;
  780. static unsigned long j;
  781. static int last_rcode, errors_skipped;
  782. if (rcode == RCODE_COMPLETE) {
  783. fwnet_transmit_packet_done(ptask);
  784. } else {
  785. if (printk_timed_ratelimit(&j, 1000) || rcode != last_rcode) {
  786. dev_err(&ptask->dev->netdev->dev,
  787. "fwnet_write_complete failed: %x (skipped %d)\n",
  788. rcode, errors_skipped);
  789. errors_skipped = 0;
  790. last_rcode = rcode;
  791. } else {
  792. errors_skipped++;
  793. }
  794. fwnet_transmit_packet_failed(ptask);
  795. }
  796. }
  797. static int fwnet_send_packet(struct fwnet_packet_task *ptask)
  798. {
  799. struct fwnet_device *dev;
  800. unsigned tx_len;
  801. struct rfc2734_header *bufhdr;
  802. unsigned long flags;
  803. bool free;
  804. dev = ptask->dev;
  805. tx_len = ptask->max_payload;
  806. switch (fwnet_get_hdr_lf(&ptask->hdr)) {
  807. case RFC2374_HDR_UNFRAG:
  808. bufhdr = skb_push(ptask->skb, RFC2374_UNFRAG_HDR_SIZE);
  809. put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0);
  810. break;
  811. case RFC2374_HDR_FIRSTFRAG:
  812. case RFC2374_HDR_INTFRAG:
  813. case RFC2374_HDR_LASTFRAG:
  814. bufhdr = skb_push(ptask->skb, RFC2374_FRAG_HDR_SIZE);
  815. put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0);
  816. put_unaligned_be32(ptask->hdr.w1, &bufhdr->w1);
  817. break;
  818. default:
  819. BUG();
  820. }
  821. if (ptask->dest_node == IEEE1394_ALL_NODES) {
  822. u8 *p;
  823. int generation;
  824. int node_id;
  825. unsigned int sw_version;
  826. /* ptask->generation may not have been set yet */
  827. generation = dev->card->generation;
  828. smp_rmb();
  829. node_id = dev->card->node_id;
  830. switch (ptask->skb->protocol) {
  831. default:
  832. sw_version = RFC2734_SW_VERSION;
  833. break;
  834. #if IS_ENABLED(CONFIG_IPV6)
  835. case htons(ETH_P_IPV6):
  836. sw_version = RFC3146_SW_VERSION;
  837. #endif
  838. }
  839. p = skb_push(ptask->skb, IEEE1394_GASP_HDR_SIZE);
  840. put_unaligned_be32(node_id << 16 | IANA_SPECIFIER_ID >> 8, p);
  841. put_unaligned_be32((IANA_SPECIFIER_ID & 0xff) << 24
  842. | sw_version, &p[4]);
  843. /* We should not transmit if broadcast_channel.valid == 0. */
  844. fw_send_request(dev->card, &ptask->transaction,
  845. TCODE_STREAM_DATA,
  846. fw_stream_packet_destination_id(3,
  847. IEEE1394_BROADCAST_CHANNEL, 0),
  848. generation, SCODE_100, 0ULL, ptask->skb->data,
  849. tx_len + 8, fwnet_write_complete, ptask);
  850. spin_lock_irqsave(&dev->lock, flags);
  851. /* If the AT tasklet already ran, we may be last user. */
  852. free = (ptask->outstanding_pkts == 0 && !ptask->enqueued);
  853. if (!free)
  854. ptask->enqueued = true;
  855. else
  856. dec_queued_datagrams(dev);
  857. spin_unlock_irqrestore(&dev->lock, flags);
  858. goto out;
  859. }
  860. fw_send_request(dev->card, &ptask->transaction,
  861. TCODE_WRITE_BLOCK_REQUEST, ptask->dest_node,
  862. ptask->generation, ptask->speed, ptask->fifo_addr,
  863. ptask->skb->data, tx_len, fwnet_write_complete, ptask);
  864. spin_lock_irqsave(&dev->lock, flags);
  865. /* If the AT tasklet already ran, we may be last user. */
  866. free = (ptask->outstanding_pkts == 0 && !ptask->enqueued);
  867. if (!free)
  868. ptask->enqueued = true;
  869. else
  870. dec_queued_datagrams(dev);
  871. spin_unlock_irqrestore(&dev->lock, flags);
  872. netif_trans_update(dev->netdev);
  873. out:
  874. if (free)
  875. fwnet_free_ptask(ptask);
  876. return 0;
  877. }
  878. static void fwnet_fifo_stop(struct fwnet_device *dev)
  879. {
  880. if (dev->local_fifo == FWNET_NO_FIFO_ADDR)
  881. return;
  882. fw_core_remove_address_handler(&dev->handler);
  883. dev->local_fifo = FWNET_NO_FIFO_ADDR;
  884. }
  885. static int fwnet_fifo_start(struct fwnet_device *dev)
  886. {
  887. int retval;
  888. if (dev->local_fifo != FWNET_NO_FIFO_ADDR)
  889. return 0;
  890. dev->handler.length = 4096;
  891. dev->handler.address_callback = fwnet_receive_packet;
  892. dev->handler.callback_data = dev;
  893. retval = fw_core_add_address_handler(&dev->handler,
  894. &fw_high_memory_region);
  895. if (retval < 0)
  896. return retval;
  897. dev->local_fifo = dev->handler.offset;
  898. return 0;
  899. }
  900. static void __fwnet_broadcast_stop(struct fwnet_device *dev)
  901. {
  902. unsigned u;
  903. if (dev->broadcast_state != FWNET_BROADCAST_ERROR) {
  904. for (u = 0; u < FWNET_ISO_PAGE_COUNT; u++)
  905. kunmap(dev->broadcast_rcv_buffer.pages[u]);
  906. fw_iso_buffer_destroy(&dev->broadcast_rcv_buffer, dev->card);
  907. }
  908. if (dev->broadcast_rcv_context) {
  909. fw_iso_context_destroy(dev->broadcast_rcv_context);
  910. dev->broadcast_rcv_context = NULL;
  911. }
  912. kfree(dev->broadcast_rcv_buffer_ptrs);
  913. dev->broadcast_rcv_buffer_ptrs = NULL;
  914. dev->broadcast_state = FWNET_BROADCAST_ERROR;
  915. }
  916. static void fwnet_broadcast_stop(struct fwnet_device *dev)
  917. {
  918. if (dev->broadcast_state == FWNET_BROADCAST_ERROR)
  919. return;
  920. fw_iso_context_stop(dev->broadcast_rcv_context);
  921. __fwnet_broadcast_stop(dev);
  922. }
  923. static int fwnet_broadcast_start(struct fwnet_device *dev)
  924. {
  925. struct fw_iso_context *context;
  926. int retval;
  927. unsigned num_packets;
  928. unsigned max_receive;
  929. struct fw_iso_packet packet;
  930. unsigned long offset;
  931. void **ptrptr;
  932. unsigned u;
  933. if (dev->broadcast_state != FWNET_BROADCAST_ERROR)
  934. return 0;
  935. max_receive = 1U << (dev->card->max_receive + 1);
  936. num_packets = (FWNET_ISO_PAGE_COUNT * PAGE_SIZE) / max_receive;
  937. ptrptr = kmalloc(sizeof(void *) * num_packets, GFP_KERNEL);
  938. if (!ptrptr) {
  939. retval = -ENOMEM;
  940. goto failed;
  941. }
  942. dev->broadcast_rcv_buffer_ptrs = ptrptr;
  943. context = fw_iso_context_create(dev->card, FW_ISO_CONTEXT_RECEIVE,
  944. IEEE1394_BROADCAST_CHANNEL,
  945. dev->card->link_speed, 8,
  946. fwnet_receive_broadcast, dev);
  947. if (IS_ERR(context)) {
  948. retval = PTR_ERR(context);
  949. goto failed;
  950. }
  951. retval = fw_iso_buffer_init(&dev->broadcast_rcv_buffer, dev->card,
  952. FWNET_ISO_PAGE_COUNT, DMA_FROM_DEVICE);
  953. if (retval < 0)
  954. goto failed;
  955. dev->broadcast_state = FWNET_BROADCAST_STOPPED;
  956. for (u = 0; u < FWNET_ISO_PAGE_COUNT; u++) {
  957. void *ptr;
  958. unsigned v;
  959. ptr = kmap(dev->broadcast_rcv_buffer.pages[u]);
  960. for (v = 0; v < num_packets / FWNET_ISO_PAGE_COUNT; v++)
  961. *ptrptr++ = (void *) ((char *)ptr + v * max_receive);
  962. }
  963. dev->broadcast_rcv_context = context;
  964. packet.payload_length = max_receive;
  965. packet.interrupt = 1;
  966. packet.skip = 0;
  967. packet.tag = 3;
  968. packet.sy = 0;
  969. packet.header_length = IEEE1394_GASP_HDR_SIZE;
  970. offset = 0;
  971. for (u = 0; u < num_packets; u++) {
  972. retval = fw_iso_context_queue(context, &packet,
  973. &dev->broadcast_rcv_buffer, offset);
  974. if (retval < 0)
  975. goto failed;
  976. offset += max_receive;
  977. }
  978. dev->num_broadcast_rcv_ptrs = num_packets;
  979. dev->rcv_buffer_size = max_receive;
  980. dev->broadcast_rcv_next_ptr = 0U;
  981. retval = fw_iso_context_start(context, -1, 0,
  982. FW_ISO_CONTEXT_MATCH_ALL_TAGS); /* ??? sync */
  983. if (retval < 0)
  984. goto failed;
  985. /* FIXME: adjust it according to the min. speed of all known peers? */
  986. dev->broadcast_xmt_max_payload = IEEE1394_MAX_PAYLOAD_S100
  987. - IEEE1394_GASP_HDR_SIZE - RFC2374_UNFRAG_HDR_SIZE;
  988. dev->broadcast_state = FWNET_BROADCAST_RUNNING;
  989. return 0;
  990. failed:
  991. __fwnet_broadcast_stop(dev);
  992. return retval;
  993. }
  994. static void set_carrier_state(struct fwnet_device *dev)
  995. {
  996. if (dev->peer_count > 1)
  997. netif_carrier_on(dev->netdev);
  998. else
  999. netif_carrier_off(dev->netdev);
  1000. }
  1001. /* ifup */
  1002. static int fwnet_open(struct net_device *net)
  1003. {
  1004. struct fwnet_device *dev = netdev_priv(net);
  1005. int ret;
  1006. ret = fwnet_broadcast_start(dev);
  1007. if (ret)
  1008. return ret;
  1009. netif_start_queue(net);
  1010. spin_lock_irq(&dev->lock);
  1011. set_carrier_state(dev);
  1012. spin_unlock_irq(&dev->lock);
  1013. return 0;
  1014. }
  1015. /* ifdown */
  1016. static int fwnet_stop(struct net_device *net)
  1017. {
  1018. struct fwnet_device *dev = netdev_priv(net);
  1019. netif_stop_queue(net);
  1020. fwnet_broadcast_stop(dev);
  1021. return 0;
  1022. }
  1023. static netdev_tx_t fwnet_tx(struct sk_buff *skb, struct net_device *net)
  1024. {
  1025. struct fwnet_header hdr_buf;
  1026. struct fwnet_device *dev = netdev_priv(net);
  1027. __be16 proto;
  1028. u16 dest_node;
  1029. unsigned max_payload;
  1030. u16 dg_size;
  1031. u16 *datagram_label_ptr;
  1032. struct fwnet_packet_task *ptask;
  1033. struct fwnet_peer *peer;
  1034. unsigned long flags;
  1035. spin_lock_irqsave(&dev->lock, flags);
  1036. /* Can this happen? */
  1037. if (netif_queue_stopped(dev->netdev)) {
  1038. spin_unlock_irqrestore(&dev->lock, flags);
  1039. return NETDEV_TX_BUSY;
  1040. }
  1041. ptask = kmem_cache_alloc(fwnet_packet_task_cache, GFP_ATOMIC);
  1042. if (ptask == NULL)
  1043. goto fail;
  1044. skb = skb_share_check(skb, GFP_ATOMIC);
  1045. if (!skb)
  1046. goto fail;
  1047. /*
  1048. * Make a copy of the driver-specific header.
  1049. * We might need to rebuild the header on tx failure.
  1050. */
  1051. memcpy(&hdr_buf, skb->data, sizeof(hdr_buf));
  1052. proto = hdr_buf.h_proto;
  1053. switch (proto) {
  1054. case htons(ETH_P_ARP):
  1055. case htons(ETH_P_IP):
  1056. #if IS_ENABLED(CONFIG_IPV6)
  1057. case htons(ETH_P_IPV6):
  1058. #endif
  1059. break;
  1060. default:
  1061. goto fail;
  1062. }
  1063. skb_pull(skb, sizeof(hdr_buf));
  1064. dg_size = skb->len;
  1065. /*
  1066. * Set the transmission type for the packet. ARP packets and IP
  1067. * broadcast packets are sent via GASP.
  1068. */
  1069. if (fwnet_hwaddr_is_multicast(hdr_buf.h_dest)) {
  1070. max_payload = dev->broadcast_xmt_max_payload;
  1071. datagram_label_ptr = &dev->broadcast_xmt_datagramlabel;
  1072. ptask->fifo_addr = FWNET_NO_FIFO_ADDR;
  1073. ptask->generation = 0;
  1074. ptask->dest_node = IEEE1394_ALL_NODES;
  1075. ptask->speed = SCODE_100;
  1076. } else {
  1077. union fwnet_hwaddr *ha = (union fwnet_hwaddr *)hdr_buf.h_dest;
  1078. __be64 guid = get_unaligned(&ha->uc.uniq_id);
  1079. u8 generation;
  1080. peer = fwnet_peer_find_by_guid(dev, be64_to_cpu(guid));
  1081. if (!peer)
  1082. goto fail;
  1083. generation = peer->generation;
  1084. dest_node = peer->node_id;
  1085. max_payload = peer->max_payload;
  1086. datagram_label_ptr = &peer->datagram_label;
  1087. ptask->fifo_addr = fwnet_hwaddr_fifo(ha);
  1088. ptask->generation = generation;
  1089. ptask->dest_node = dest_node;
  1090. ptask->speed = peer->speed;
  1091. }
  1092. ptask->hdr.w0 = 0;
  1093. ptask->hdr.w1 = 0;
  1094. ptask->skb = skb;
  1095. ptask->dev = dev;
  1096. /* Does it all fit in one packet? */
  1097. if (dg_size <= max_payload) {
  1098. fwnet_make_uf_hdr(&ptask->hdr, ntohs(proto));
  1099. ptask->outstanding_pkts = 1;
  1100. max_payload = dg_size + RFC2374_UNFRAG_HDR_SIZE;
  1101. } else {
  1102. u16 datagram_label;
  1103. max_payload -= RFC2374_FRAG_OVERHEAD;
  1104. datagram_label = (*datagram_label_ptr)++;
  1105. fwnet_make_ff_hdr(&ptask->hdr, ntohs(proto), dg_size,
  1106. datagram_label);
  1107. ptask->outstanding_pkts = DIV_ROUND_UP(dg_size, max_payload);
  1108. max_payload += RFC2374_FRAG_HDR_SIZE;
  1109. }
  1110. if (++dev->queued_datagrams == FWNET_MAX_QUEUED_DATAGRAMS)
  1111. netif_stop_queue(dev->netdev);
  1112. spin_unlock_irqrestore(&dev->lock, flags);
  1113. ptask->max_payload = max_payload;
  1114. ptask->enqueued = 0;
  1115. fwnet_send_packet(ptask);
  1116. return NETDEV_TX_OK;
  1117. fail:
  1118. spin_unlock_irqrestore(&dev->lock, flags);
  1119. if (ptask)
  1120. kmem_cache_free(fwnet_packet_task_cache, ptask);
  1121. if (skb != NULL)
  1122. dev_kfree_skb(skb);
  1123. net->stats.tx_dropped++;
  1124. net->stats.tx_errors++;
  1125. /*
  1126. * FIXME: According to a patch from 2003-02-26, "returning non-zero
  1127. * causes serious problems" here, allegedly. Before that patch,
  1128. * -ERRNO was returned which is not appropriate under Linux 2.6.
  1129. * Perhaps more needs to be done? Stop the queue in serious
  1130. * conditions and restart it elsewhere?
  1131. */
  1132. return NETDEV_TX_OK;
  1133. }
  1134. static const struct ethtool_ops fwnet_ethtool_ops = {
  1135. .get_link = ethtool_op_get_link,
  1136. };
  1137. static const struct net_device_ops fwnet_netdev_ops = {
  1138. .ndo_open = fwnet_open,
  1139. .ndo_stop = fwnet_stop,
  1140. .ndo_start_xmit = fwnet_tx,
  1141. };
  1142. static void fwnet_init_dev(struct net_device *net)
  1143. {
  1144. net->header_ops = &fwnet_header_ops;
  1145. net->netdev_ops = &fwnet_netdev_ops;
  1146. net->watchdog_timeo = 2 * HZ;
  1147. net->flags = IFF_BROADCAST | IFF_MULTICAST;
  1148. net->features = NETIF_F_HIGHDMA;
  1149. net->addr_len = FWNET_ALEN;
  1150. net->hard_header_len = FWNET_HLEN;
  1151. net->type = ARPHRD_IEEE1394;
  1152. net->tx_queue_len = FWNET_TX_QUEUE_LEN;
  1153. net->ethtool_ops = &fwnet_ethtool_ops;
  1154. }
  1155. /* caller must hold fwnet_device_mutex */
  1156. static struct fwnet_device *fwnet_dev_find(struct fw_card *card)
  1157. {
  1158. struct fwnet_device *dev;
  1159. list_for_each_entry(dev, &fwnet_device_list, dev_link)
  1160. if (dev->card == card)
  1161. return dev;
  1162. return NULL;
  1163. }
  1164. static int fwnet_add_peer(struct fwnet_device *dev,
  1165. struct fw_unit *unit, struct fw_device *device)
  1166. {
  1167. struct fwnet_peer *peer;
  1168. peer = kmalloc(sizeof(*peer), GFP_KERNEL);
  1169. if (!peer)
  1170. return -ENOMEM;
  1171. dev_set_drvdata(&unit->device, peer);
  1172. peer->dev = dev;
  1173. peer->guid = (u64)device->config_rom[3] << 32 | device->config_rom[4];
  1174. INIT_LIST_HEAD(&peer->pd_list);
  1175. peer->pdg_size = 0;
  1176. peer->datagram_label = 0;
  1177. peer->speed = device->max_speed;
  1178. peer->max_payload = fwnet_max_payload(device->max_rec, peer->speed);
  1179. peer->generation = device->generation;
  1180. smp_rmb();
  1181. peer->node_id = device->node_id;
  1182. spin_lock_irq(&dev->lock);
  1183. list_add_tail(&peer->peer_link, &dev->peer_list);
  1184. dev->peer_count++;
  1185. set_carrier_state(dev);
  1186. spin_unlock_irq(&dev->lock);
  1187. return 0;
  1188. }
  1189. static int fwnet_probe(struct fw_unit *unit,
  1190. const struct ieee1394_device_id *id)
  1191. {
  1192. struct fw_device *device = fw_parent_device(unit);
  1193. struct fw_card *card = device->card;
  1194. struct net_device *net;
  1195. bool allocated_netdev = false;
  1196. struct fwnet_device *dev;
  1197. int ret;
  1198. union fwnet_hwaddr *ha;
  1199. mutex_lock(&fwnet_device_mutex);
  1200. dev = fwnet_dev_find(card);
  1201. if (dev) {
  1202. net = dev->netdev;
  1203. goto have_dev;
  1204. }
  1205. net = alloc_netdev(sizeof(*dev), "firewire%d", NET_NAME_UNKNOWN,
  1206. fwnet_init_dev);
  1207. if (net == NULL) {
  1208. mutex_unlock(&fwnet_device_mutex);
  1209. return -ENOMEM;
  1210. }
  1211. allocated_netdev = true;
  1212. SET_NETDEV_DEV(net, card->device);
  1213. dev = netdev_priv(net);
  1214. spin_lock_init(&dev->lock);
  1215. dev->broadcast_state = FWNET_BROADCAST_ERROR;
  1216. dev->broadcast_rcv_context = NULL;
  1217. dev->broadcast_xmt_max_payload = 0;
  1218. dev->broadcast_xmt_datagramlabel = 0;
  1219. dev->local_fifo = FWNET_NO_FIFO_ADDR;
  1220. dev->queued_datagrams = 0;
  1221. INIT_LIST_HEAD(&dev->peer_list);
  1222. dev->card = card;
  1223. dev->netdev = net;
  1224. ret = fwnet_fifo_start(dev);
  1225. if (ret < 0)
  1226. goto out;
  1227. dev->local_fifo = dev->handler.offset;
  1228. net->mtu = 1500U;
  1229. net->min_mtu = ETH_MIN_MTU;
  1230. net->max_mtu = 0xfff;
  1231. /* Set our hardware address while we're at it */
  1232. ha = (union fwnet_hwaddr *)net->dev_addr;
  1233. put_unaligned_be64(card->guid, &ha->uc.uniq_id);
  1234. ha->uc.max_rec = dev->card->max_receive;
  1235. ha->uc.sspd = dev->card->link_speed;
  1236. put_unaligned_be16(dev->local_fifo >> 32, &ha->uc.fifo_hi);
  1237. put_unaligned_be32(dev->local_fifo & 0xffffffff, &ha->uc.fifo_lo);
  1238. memset(net->broadcast, -1, net->addr_len);
  1239. ret = register_netdev(net);
  1240. if (ret)
  1241. goto out;
  1242. list_add_tail(&dev->dev_link, &fwnet_device_list);
  1243. dev_notice(&net->dev, "IP over IEEE 1394 on card %s\n",
  1244. dev_name(card->device));
  1245. have_dev:
  1246. ret = fwnet_add_peer(dev, unit, device);
  1247. if (ret && allocated_netdev) {
  1248. unregister_netdev(net);
  1249. list_del(&dev->dev_link);
  1250. out:
  1251. fwnet_fifo_stop(dev);
  1252. free_netdev(net);
  1253. }
  1254. mutex_unlock(&fwnet_device_mutex);
  1255. return ret;
  1256. }
  1257. /*
  1258. * FIXME abort partially sent fragmented datagrams,
  1259. * discard partially received fragmented datagrams
  1260. */
  1261. static void fwnet_update(struct fw_unit *unit)
  1262. {
  1263. struct fw_device *device = fw_parent_device(unit);
  1264. struct fwnet_peer *peer = dev_get_drvdata(&unit->device);
  1265. int generation;
  1266. generation = device->generation;
  1267. spin_lock_irq(&peer->dev->lock);
  1268. peer->node_id = device->node_id;
  1269. peer->generation = generation;
  1270. spin_unlock_irq(&peer->dev->lock);
  1271. }
  1272. static void fwnet_remove_peer(struct fwnet_peer *peer, struct fwnet_device *dev)
  1273. {
  1274. struct fwnet_partial_datagram *pd, *pd_next;
  1275. spin_lock_irq(&dev->lock);
  1276. list_del(&peer->peer_link);
  1277. dev->peer_count--;
  1278. set_carrier_state(dev);
  1279. spin_unlock_irq(&dev->lock);
  1280. list_for_each_entry_safe(pd, pd_next, &peer->pd_list, pd_link)
  1281. fwnet_pd_delete(pd);
  1282. kfree(peer);
  1283. }
  1284. static void fwnet_remove(struct fw_unit *unit)
  1285. {
  1286. struct fwnet_peer *peer = dev_get_drvdata(&unit->device);
  1287. struct fwnet_device *dev = peer->dev;
  1288. struct net_device *net;
  1289. int i;
  1290. mutex_lock(&fwnet_device_mutex);
  1291. net = dev->netdev;
  1292. fwnet_remove_peer(peer, dev);
  1293. if (list_empty(&dev->peer_list)) {
  1294. unregister_netdev(net);
  1295. fwnet_fifo_stop(dev);
  1296. for (i = 0; dev->queued_datagrams && i < 5; i++)
  1297. ssleep(1);
  1298. WARN_ON(dev->queued_datagrams);
  1299. list_del(&dev->dev_link);
  1300. free_netdev(net);
  1301. }
  1302. mutex_unlock(&fwnet_device_mutex);
  1303. }
  1304. static const struct ieee1394_device_id fwnet_id_table[] = {
  1305. {
  1306. .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
  1307. IEEE1394_MATCH_VERSION,
  1308. .specifier_id = IANA_SPECIFIER_ID,
  1309. .version = RFC2734_SW_VERSION,
  1310. },
  1311. #if IS_ENABLED(CONFIG_IPV6)
  1312. {
  1313. .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
  1314. IEEE1394_MATCH_VERSION,
  1315. .specifier_id = IANA_SPECIFIER_ID,
  1316. .version = RFC3146_SW_VERSION,
  1317. },
  1318. #endif
  1319. { }
  1320. };
  1321. static struct fw_driver fwnet_driver = {
  1322. .driver = {
  1323. .owner = THIS_MODULE,
  1324. .name = KBUILD_MODNAME,
  1325. .bus = &fw_bus_type,
  1326. },
  1327. .probe = fwnet_probe,
  1328. .update = fwnet_update,
  1329. .remove = fwnet_remove,
  1330. .id_table = fwnet_id_table,
  1331. };
  1332. static const u32 rfc2374_unit_directory_data[] = {
  1333. 0x00040000, /* directory_length */
  1334. 0x1200005e, /* unit_specifier_id: IANA */
  1335. 0x81000003, /* textual descriptor offset */
  1336. 0x13000001, /* unit_sw_version: RFC 2734 */
  1337. 0x81000005, /* textual descriptor offset */
  1338. 0x00030000, /* descriptor_length */
  1339. 0x00000000, /* text */
  1340. 0x00000000, /* minimal ASCII, en */
  1341. 0x49414e41, /* I A N A */
  1342. 0x00030000, /* descriptor_length */
  1343. 0x00000000, /* text */
  1344. 0x00000000, /* minimal ASCII, en */
  1345. 0x49507634, /* I P v 4 */
  1346. };
  1347. static struct fw_descriptor rfc2374_unit_directory = {
  1348. .length = ARRAY_SIZE(rfc2374_unit_directory_data),
  1349. .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
  1350. .data = rfc2374_unit_directory_data
  1351. };
  1352. #if IS_ENABLED(CONFIG_IPV6)
  1353. static const u32 rfc3146_unit_directory_data[] = {
  1354. 0x00040000, /* directory_length */
  1355. 0x1200005e, /* unit_specifier_id: IANA */
  1356. 0x81000003, /* textual descriptor offset */
  1357. 0x13000002, /* unit_sw_version: RFC 3146 */
  1358. 0x81000005, /* textual descriptor offset */
  1359. 0x00030000, /* descriptor_length */
  1360. 0x00000000, /* text */
  1361. 0x00000000, /* minimal ASCII, en */
  1362. 0x49414e41, /* I A N A */
  1363. 0x00030000, /* descriptor_length */
  1364. 0x00000000, /* text */
  1365. 0x00000000, /* minimal ASCII, en */
  1366. 0x49507636, /* I P v 6 */
  1367. };
  1368. static struct fw_descriptor rfc3146_unit_directory = {
  1369. .length = ARRAY_SIZE(rfc3146_unit_directory_data),
  1370. .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
  1371. .data = rfc3146_unit_directory_data
  1372. };
  1373. #endif
  1374. static int __init fwnet_init(void)
  1375. {
  1376. int err;
  1377. err = fw_core_add_descriptor(&rfc2374_unit_directory);
  1378. if (err)
  1379. return err;
  1380. #if IS_ENABLED(CONFIG_IPV6)
  1381. err = fw_core_add_descriptor(&rfc3146_unit_directory);
  1382. if (err)
  1383. goto out;
  1384. #endif
  1385. fwnet_packet_task_cache = kmem_cache_create("packet_task",
  1386. sizeof(struct fwnet_packet_task), 0, 0, NULL);
  1387. if (!fwnet_packet_task_cache) {
  1388. err = -ENOMEM;
  1389. goto out2;
  1390. }
  1391. err = driver_register(&fwnet_driver.driver);
  1392. if (!err)
  1393. return 0;
  1394. kmem_cache_destroy(fwnet_packet_task_cache);
  1395. out2:
  1396. #if IS_ENABLED(CONFIG_IPV6)
  1397. fw_core_remove_descriptor(&rfc3146_unit_directory);
  1398. out:
  1399. #endif
  1400. fw_core_remove_descriptor(&rfc2374_unit_directory);
  1401. return err;
  1402. }
  1403. module_init(fwnet_init);
  1404. static void __exit fwnet_cleanup(void)
  1405. {
  1406. driver_unregister(&fwnet_driver.driver);
  1407. kmem_cache_destroy(fwnet_packet_task_cache);
  1408. #if IS_ENABLED(CONFIG_IPV6)
  1409. fw_core_remove_descriptor(&rfc3146_unit_directory);
  1410. #endif
  1411. fw_core_remove_descriptor(&rfc2374_unit_directory);
  1412. }
  1413. module_exit(fwnet_cleanup);
  1414. MODULE_AUTHOR("Jay Fenlason <fenlason@redhat.com>");
  1415. MODULE_DESCRIPTION("IP over IEEE1394 as per RFC 2734/3146");
  1416. MODULE_LICENSE("GPL");
  1417. MODULE_DEVICE_TABLE(ieee1394, fwnet_id_table);