net.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  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)
  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) << 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 = (struct fwnet_header *)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_rebuild(struct sk_buff *skb)
  203. {
  204. struct fwnet_header *h = (struct fwnet_header *)skb->data;
  205. if (get_unaligned_be16(&h->h_proto) == ETH_P_IP)
  206. return arp_find((unsigned char *)&h->h_dest, skb);
  207. dev_notice(&skb->dev->dev, "unable to resolve type %04x addresses\n",
  208. be16_to_cpu(h->h_proto));
  209. return 0;
  210. }
  211. static int fwnet_header_cache(const struct neighbour *neigh,
  212. struct hh_cache *hh, __be16 type)
  213. {
  214. struct net_device *net;
  215. struct fwnet_header *h;
  216. if (type == cpu_to_be16(ETH_P_802_3))
  217. return -1;
  218. net = neigh->dev;
  219. h = (struct fwnet_header *)((u8 *)hh->hh_data + HH_DATA_OFF(sizeof(*h)));
  220. h->h_proto = type;
  221. memcpy(h->h_dest, neigh->ha, net->addr_len);
  222. hh->hh_len = FWNET_HLEN;
  223. return 0;
  224. }
  225. /* Called by Address Resolution module to notify changes in address. */
  226. static void fwnet_header_cache_update(struct hh_cache *hh,
  227. const struct net_device *net, const unsigned char *haddr)
  228. {
  229. memcpy((u8 *)hh->hh_data + HH_DATA_OFF(FWNET_HLEN), haddr, net->addr_len);
  230. }
  231. static int fwnet_header_parse(const struct sk_buff *skb, unsigned char *haddr)
  232. {
  233. memcpy(haddr, skb->dev->dev_addr, FWNET_ALEN);
  234. return FWNET_ALEN;
  235. }
  236. static const struct header_ops fwnet_header_ops = {
  237. .create = fwnet_header_create,
  238. .rebuild = fwnet_header_rebuild,
  239. .cache = fwnet_header_cache,
  240. .cache_update = fwnet_header_cache_update,
  241. .parse = fwnet_header_parse,
  242. };
  243. /* FIXME: is this correct for all cases? */
  244. static bool fwnet_frag_overlap(struct fwnet_partial_datagram *pd,
  245. unsigned offset, unsigned len)
  246. {
  247. struct fwnet_fragment_info *fi;
  248. unsigned end = offset + len;
  249. list_for_each_entry(fi, &pd->fi_list, fi_link)
  250. if (offset < fi->offset + fi->len && end > fi->offset)
  251. return true;
  252. return false;
  253. }
  254. /* Assumes that new fragment does not overlap any existing fragments */
  255. static struct fwnet_fragment_info *fwnet_frag_new(
  256. struct fwnet_partial_datagram *pd, unsigned offset, unsigned len)
  257. {
  258. struct fwnet_fragment_info *fi, *fi2, *new;
  259. struct list_head *list;
  260. list = &pd->fi_list;
  261. list_for_each_entry(fi, &pd->fi_list, fi_link) {
  262. if (fi->offset + fi->len == offset) {
  263. /* The new fragment can be tacked on to the end */
  264. /* Did the new fragment plug a hole? */
  265. fi2 = list_entry(fi->fi_link.next,
  266. struct fwnet_fragment_info, fi_link);
  267. if (fi->offset + fi->len == fi2->offset) {
  268. /* glue fragments together */
  269. fi->len += len + fi2->len;
  270. list_del(&fi2->fi_link);
  271. kfree(fi2);
  272. } else {
  273. fi->len += len;
  274. }
  275. return fi;
  276. }
  277. if (offset + len == fi->offset) {
  278. /* The new fragment can be tacked on to the beginning */
  279. /* Did the new fragment plug a hole? */
  280. fi2 = list_entry(fi->fi_link.prev,
  281. struct fwnet_fragment_info, fi_link);
  282. if (fi2->offset + fi2->len == fi->offset) {
  283. /* glue fragments together */
  284. fi2->len += fi->len + len;
  285. list_del(&fi->fi_link);
  286. kfree(fi);
  287. return fi2;
  288. }
  289. fi->offset = offset;
  290. fi->len += len;
  291. return fi;
  292. }
  293. if (offset > fi->offset + fi->len) {
  294. list = &fi->fi_link;
  295. break;
  296. }
  297. if (offset + len < fi->offset) {
  298. list = fi->fi_link.prev;
  299. break;
  300. }
  301. }
  302. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  303. if (!new) {
  304. dev_err(&pd->skb->dev->dev, "out of memory\n");
  305. return NULL;
  306. }
  307. new->offset = offset;
  308. new->len = len;
  309. list_add(&new->fi_link, list);
  310. return new;
  311. }
  312. static struct fwnet_partial_datagram *fwnet_pd_new(struct net_device *net,
  313. struct fwnet_peer *peer, u16 datagram_label, unsigned dg_size,
  314. void *frag_buf, unsigned frag_off, unsigned frag_len)
  315. {
  316. struct fwnet_partial_datagram *new;
  317. struct fwnet_fragment_info *fi;
  318. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  319. if (!new)
  320. goto fail;
  321. INIT_LIST_HEAD(&new->fi_list);
  322. fi = fwnet_frag_new(new, frag_off, frag_len);
  323. if (fi == NULL)
  324. goto fail_w_new;
  325. new->datagram_label = datagram_label;
  326. new->datagram_size = dg_size;
  327. new->skb = dev_alloc_skb(dg_size + LL_RESERVED_SPACE(net));
  328. if (new->skb == NULL)
  329. goto fail_w_fi;
  330. skb_reserve(new->skb, LL_RESERVED_SPACE(net));
  331. new->pbuf = skb_put(new->skb, dg_size);
  332. memcpy(new->pbuf + frag_off, frag_buf, frag_len);
  333. list_add_tail(&new->pd_link, &peer->pd_list);
  334. return new;
  335. fail_w_fi:
  336. kfree(fi);
  337. fail_w_new:
  338. kfree(new);
  339. fail:
  340. dev_err(&net->dev, "out of memory\n");
  341. return NULL;
  342. }
  343. static struct fwnet_partial_datagram *fwnet_pd_find(struct fwnet_peer *peer,
  344. u16 datagram_label)
  345. {
  346. struct fwnet_partial_datagram *pd;
  347. list_for_each_entry(pd, &peer->pd_list, pd_link)
  348. if (pd->datagram_label == datagram_label)
  349. return pd;
  350. return NULL;
  351. }
  352. static void fwnet_pd_delete(struct fwnet_partial_datagram *old)
  353. {
  354. struct fwnet_fragment_info *fi, *n;
  355. list_for_each_entry_safe(fi, n, &old->fi_list, fi_link)
  356. kfree(fi);
  357. list_del(&old->pd_link);
  358. dev_kfree_skb_any(old->skb);
  359. kfree(old);
  360. }
  361. static bool fwnet_pd_update(struct fwnet_peer *peer,
  362. struct fwnet_partial_datagram *pd, void *frag_buf,
  363. unsigned frag_off, unsigned frag_len)
  364. {
  365. if (fwnet_frag_new(pd, frag_off, frag_len) == NULL)
  366. return false;
  367. memcpy(pd->pbuf + frag_off, frag_buf, frag_len);
  368. /*
  369. * Move list entry to beginning of list so that oldest partial
  370. * datagrams percolate to the end of the list
  371. */
  372. list_move_tail(&pd->pd_link, &peer->pd_list);
  373. return true;
  374. }
  375. static bool fwnet_pd_is_complete(struct fwnet_partial_datagram *pd)
  376. {
  377. struct fwnet_fragment_info *fi;
  378. fi = list_entry(pd->fi_list.next, struct fwnet_fragment_info, fi_link);
  379. return fi->len == pd->datagram_size;
  380. }
  381. /* caller must hold dev->lock */
  382. static struct fwnet_peer *fwnet_peer_find_by_guid(struct fwnet_device *dev,
  383. u64 guid)
  384. {
  385. struct fwnet_peer *peer;
  386. list_for_each_entry(peer, &dev->peer_list, peer_link)
  387. if (peer->guid == guid)
  388. return peer;
  389. return NULL;
  390. }
  391. /* caller must hold dev->lock */
  392. static struct fwnet_peer *fwnet_peer_find_by_node_id(struct fwnet_device *dev,
  393. int node_id, int generation)
  394. {
  395. struct fwnet_peer *peer;
  396. list_for_each_entry(peer, &dev->peer_list, peer_link)
  397. if (peer->node_id == node_id &&
  398. peer->generation == generation)
  399. return peer;
  400. return NULL;
  401. }
  402. /* See IEEE 1394-2008 table 6-4, table 8-8, table 16-18. */
  403. static unsigned fwnet_max_payload(unsigned max_rec, unsigned speed)
  404. {
  405. max_rec = min(max_rec, speed + 8);
  406. max_rec = clamp(max_rec, 8U, 11U); /* 512...4096 */
  407. return (1 << (max_rec + 1)) - RFC2374_FRAG_HDR_SIZE;
  408. }
  409. static int fwnet_finish_incoming_packet(struct net_device *net,
  410. struct sk_buff *skb, u16 source_node_id,
  411. bool is_broadcast, u16 ether_type)
  412. {
  413. struct fwnet_device *dev;
  414. int status;
  415. __be64 guid;
  416. switch (ether_type) {
  417. case ETH_P_ARP:
  418. case ETH_P_IP:
  419. #if IS_ENABLED(CONFIG_IPV6)
  420. case ETH_P_IPV6:
  421. #endif
  422. break;
  423. default:
  424. goto err;
  425. }
  426. dev = netdev_priv(net);
  427. /* Write metadata, and then pass to the receive level */
  428. skb->dev = net;
  429. skb->ip_summed = CHECKSUM_NONE;
  430. /*
  431. * Parse the encapsulation header. This actually does the job of
  432. * converting to an ethernet-like pseudo frame header.
  433. */
  434. guid = cpu_to_be64(dev->card->guid);
  435. if (dev_hard_header(skb, net, ether_type,
  436. is_broadcast ? net->broadcast : net->dev_addr,
  437. NULL, skb->len) >= 0) {
  438. struct fwnet_header *eth;
  439. u16 *rawp;
  440. __be16 protocol;
  441. skb_reset_mac_header(skb);
  442. skb_pull(skb, sizeof(*eth));
  443. eth = (struct fwnet_header *)skb_mac_header(skb);
  444. if (fwnet_hwaddr_is_multicast(eth->h_dest)) {
  445. if (memcmp(eth->h_dest, net->broadcast,
  446. net->addr_len) == 0)
  447. skb->pkt_type = PACKET_BROADCAST;
  448. #if 0
  449. else
  450. skb->pkt_type = PACKET_MULTICAST;
  451. #endif
  452. } else {
  453. if (memcmp(eth->h_dest, net->dev_addr, net->addr_len))
  454. skb->pkt_type = PACKET_OTHERHOST;
  455. }
  456. if (ntohs(eth->h_proto) >= 1536) {
  457. protocol = eth->h_proto;
  458. } else {
  459. rawp = (u16 *)skb->data;
  460. if (*rawp == 0xffff)
  461. protocol = htons(ETH_P_802_3);
  462. else
  463. protocol = htons(ETH_P_802_2);
  464. }
  465. skb->protocol = protocol;
  466. }
  467. status = netif_rx(skb);
  468. if (status == NET_RX_DROP) {
  469. net->stats.rx_errors++;
  470. net->stats.rx_dropped++;
  471. } else {
  472. net->stats.rx_packets++;
  473. net->stats.rx_bytes += skb->len;
  474. }
  475. return 0;
  476. err:
  477. net->stats.rx_errors++;
  478. net->stats.rx_dropped++;
  479. dev_kfree_skb_any(skb);
  480. return -ENOENT;
  481. }
  482. static int fwnet_incoming_packet(struct fwnet_device *dev, __be32 *buf, int len,
  483. int source_node_id, int generation,
  484. bool is_broadcast)
  485. {
  486. struct sk_buff *skb;
  487. struct net_device *net = dev->netdev;
  488. struct rfc2734_header hdr;
  489. unsigned lf;
  490. unsigned long flags;
  491. struct fwnet_peer *peer;
  492. struct fwnet_partial_datagram *pd;
  493. int fg_off;
  494. int dg_size;
  495. u16 datagram_label;
  496. int retval;
  497. u16 ether_type;
  498. hdr.w0 = be32_to_cpu(buf[0]);
  499. lf = fwnet_get_hdr_lf(&hdr);
  500. if (lf == RFC2374_HDR_UNFRAG) {
  501. /*
  502. * An unfragmented datagram has been received by the ieee1394
  503. * bus. Build an skbuff around it so we can pass it to the
  504. * high level network layer.
  505. */
  506. ether_type = fwnet_get_hdr_ether_type(&hdr);
  507. buf++;
  508. len -= RFC2374_UNFRAG_HDR_SIZE;
  509. skb = dev_alloc_skb(len + LL_RESERVED_SPACE(net));
  510. if (unlikely(!skb)) {
  511. dev_err(&net->dev, "out of memory\n");
  512. net->stats.rx_dropped++;
  513. return -ENOMEM;
  514. }
  515. skb_reserve(skb, LL_RESERVED_SPACE(net));
  516. memcpy(skb_put(skb, len), buf, len);
  517. return fwnet_finish_incoming_packet(net, skb, source_node_id,
  518. is_broadcast, ether_type);
  519. }
  520. /* A datagram fragment has been received, now the fun begins. */
  521. hdr.w1 = ntohl(buf[1]);
  522. buf += 2;
  523. len -= RFC2374_FRAG_HDR_SIZE;
  524. if (lf == RFC2374_HDR_FIRSTFRAG) {
  525. ether_type = fwnet_get_hdr_ether_type(&hdr);
  526. fg_off = 0;
  527. } else {
  528. ether_type = 0;
  529. fg_off = fwnet_get_hdr_fg_off(&hdr);
  530. }
  531. datagram_label = fwnet_get_hdr_dgl(&hdr);
  532. dg_size = fwnet_get_hdr_dg_size(&hdr); /* ??? + 1 */
  533. spin_lock_irqsave(&dev->lock, flags);
  534. peer = fwnet_peer_find_by_node_id(dev, source_node_id, generation);
  535. if (!peer) {
  536. retval = -ENOENT;
  537. goto fail;
  538. }
  539. pd = fwnet_pd_find(peer, datagram_label);
  540. if (pd == NULL) {
  541. while (peer->pdg_size >= FWNET_MAX_FRAGMENTS) {
  542. /* remove the oldest */
  543. fwnet_pd_delete(list_first_entry(&peer->pd_list,
  544. struct fwnet_partial_datagram, pd_link));
  545. peer->pdg_size--;
  546. }
  547. pd = fwnet_pd_new(net, peer, datagram_label,
  548. dg_size, buf, fg_off, len);
  549. if (pd == NULL) {
  550. retval = -ENOMEM;
  551. goto fail;
  552. }
  553. peer->pdg_size++;
  554. } else {
  555. if (fwnet_frag_overlap(pd, fg_off, len) ||
  556. pd->datagram_size != dg_size) {
  557. /*
  558. * Differing datagram sizes or overlapping fragments,
  559. * discard old datagram and start a new one.
  560. */
  561. fwnet_pd_delete(pd);
  562. pd = fwnet_pd_new(net, peer, datagram_label,
  563. dg_size, buf, fg_off, len);
  564. if (pd == NULL) {
  565. peer->pdg_size--;
  566. retval = -ENOMEM;
  567. goto fail;
  568. }
  569. } else {
  570. if (!fwnet_pd_update(peer, pd, buf, fg_off, len)) {
  571. /*
  572. * Couldn't save off fragment anyway
  573. * so might as well obliterate the
  574. * datagram now.
  575. */
  576. fwnet_pd_delete(pd);
  577. peer->pdg_size--;
  578. retval = -ENOMEM;
  579. goto fail;
  580. }
  581. }
  582. } /* new datagram or add to existing one */
  583. if (lf == RFC2374_HDR_FIRSTFRAG)
  584. pd->ether_type = ether_type;
  585. if (fwnet_pd_is_complete(pd)) {
  586. ether_type = pd->ether_type;
  587. peer->pdg_size--;
  588. skb = skb_get(pd->skb);
  589. fwnet_pd_delete(pd);
  590. spin_unlock_irqrestore(&dev->lock, flags);
  591. return fwnet_finish_incoming_packet(net, skb, source_node_id,
  592. false, ether_type);
  593. }
  594. /*
  595. * Datagram is not complete, we're done for the
  596. * moment.
  597. */
  598. retval = 0;
  599. fail:
  600. spin_unlock_irqrestore(&dev->lock, flags);
  601. return retval;
  602. }
  603. static void fwnet_receive_packet(struct fw_card *card, struct fw_request *r,
  604. int tcode, int destination, int source, int generation,
  605. unsigned long long offset, void *payload, size_t length,
  606. void *callback_data)
  607. {
  608. struct fwnet_device *dev = callback_data;
  609. int rcode;
  610. if (destination == IEEE1394_ALL_NODES) {
  611. kfree(r);
  612. return;
  613. }
  614. if (offset != dev->handler.offset)
  615. rcode = RCODE_ADDRESS_ERROR;
  616. else if (tcode != TCODE_WRITE_BLOCK_REQUEST)
  617. rcode = RCODE_TYPE_ERROR;
  618. else if (fwnet_incoming_packet(dev, payload, length,
  619. source, generation, false) != 0) {
  620. dev_err(&dev->netdev->dev, "incoming packet failure\n");
  621. rcode = RCODE_CONFLICT_ERROR;
  622. } else
  623. rcode = RCODE_COMPLETE;
  624. fw_send_response(card, r, rcode);
  625. }
  626. static void fwnet_receive_broadcast(struct fw_iso_context *context,
  627. u32 cycle, size_t header_length, void *header, void *data)
  628. {
  629. struct fwnet_device *dev;
  630. struct fw_iso_packet packet;
  631. __be16 *hdr_ptr;
  632. __be32 *buf_ptr;
  633. int retval;
  634. u32 length;
  635. u16 source_node_id;
  636. u32 specifier_id;
  637. u32 ver;
  638. unsigned long offset;
  639. unsigned long flags;
  640. dev = data;
  641. hdr_ptr = header;
  642. length = be16_to_cpup(hdr_ptr);
  643. spin_lock_irqsave(&dev->lock, flags);
  644. offset = dev->rcv_buffer_size * dev->broadcast_rcv_next_ptr;
  645. buf_ptr = dev->broadcast_rcv_buffer_ptrs[dev->broadcast_rcv_next_ptr++];
  646. if (dev->broadcast_rcv_next_ptr == dev->num_broadcast_rcv_ptrs)
  647. dev->broadcast_rcv_next_ptr = 0;
  648. spin_unlock_irqrestore(&dev->lock, flags);
  649. specifier_id = (be32_to_cpu(buf_ptr[0]) & 0xffff) << 8
  650. | (be32_to_cpu(buf_ptr[1]) & 0xff000000) >> 24;
  651. ver = be32_to_cpu(buf_ptr[1]) & 0xffffff;
  652. source_node_id = be32_to_cpu(buf_ptr[0]) >> 16;
  653. if (specifier_id == IANA_SPECIFIER_ID &&
  654. (ver == RFC2734_SW_VERSION
  655. #if IS_ENABLED(CONFIG_IPV6)
  656. || ver == RFC3146_SW_VERSION
  657. #endif
  658. )) {
  659. buf_ptr += 2;
  660. length -= IEEE1394_GASP_HDR_SIZE;
  661. fwnet_incoming_packet(dev, buf_ptr, length, source_node_id,
  662. context->card->generation, true);
  663. }
  664. packet.payload_length = dev->rcv_buffer_size;
  665. packet.interrupt = 1;
  666. packet.skip = 0;
  667. packet.tag = 3;
  668. packet.sy = 0;
  669. packet.header_length = IEEE1394_GASP_HDR_SIZE;
  670. spin_lock_irqsave(&dev->lock, flags);
  671. retval = fw_iso_context_queue(dev->broadcast_rcv_context, &packet,
  672. &dev->broadcast_rcv_buffer, offset);
  673. spin_unlock_irqrestore(&dev->lock, flags);
  674. if (retval >= 0)
  675. fw_iso_context_queue_flush(dev->broadcast_rcv_context);
  676. else
  677. dev_err(&dev->netdev->dev, "requeue failed\n");
  678. }
  679. static struct kmem_cache *fwnet_packet_task_cache;
  680. static void fwnet_free_ptask(struct fwnet_packet_task *ptask)
  681. {
  682. dev_kfree_skb_any(ptask->skb);
  683. kmem_cache_free(fwnet_packet_task_cache, ptask);
  684. }
  685. /* Caller must hold dev->lock. */
  686. static void dec_queued_datagrams(struct fwnet_device *dev)
  687. {
  688. if (--dev->queued_datagrams == FWNET_MIN_QUEUED_DATAGRAMS)
  689. netif_wake_queue(dev->netdev);
  690. }
  691. static int fwnet_send_packet(struct fwnet_packet_task *ptask);
  692. static void fwnet_transmit_packet_done(struct fwnet_packet_task *ptask)
  693. {
  694. struct fwnet_device *dev = ptask->dev;
  695. struct sk_buff *skb = ptask->skb;
  696. unsigned long flags;
  697. bool free;
  698. spin_lock_irqsave(&dev->lock, flags);
  699. ptask->outstanding_pkts--;
  700. /* Check whether we or the networking TX soft-IRQ is last user. */
  701. free = (ptask->outstanding_pkts == 0 && ptask->enqueued);
  702. if (free)
  703. dec_queued_datagrams(dev);
  704. if (ptask->outstanding_pkts == 0) {
  705. dev->netdev->stats.tx_packets++;
  706. dev->netdev->stats.tx_bytes += skb->len;
  707. }
  708. spin_unlock_irqrestore(&dev->lock, flags);
  709. if (ptask->outstanding_pkts > 0) {
  710. u16 dg_size;
  711. u16 fg_off;
  712. u16 datagram_label;
  713. u16 lf;
  714. /* Update the ptask to point to the next fragment and send it */
  715. lf = fwnet_get_hdr_lf(&ptask->hdr);
  716. switch (lf) {
  717. case RFC2374_HDR_LASTFRAG:
  718. case RFC2374_HDR_UNFRAG:
  719. default:
  720. dev_err(&dev->netdev->dev,
  721. "outstanding packet %x lf %x, header %x,%x\n",
  722. ptask->outstanding_pkts, lf, ptask->hdr.w0,
  723. ptask->hdr.w1);
  724. BUG();
  725. case RFC2374_HDR_FIRSTFRAG:
  726. /* Set frag type here for future interior fragments */
  727. dg_size = fwnet_get_hdr_dg_size(&ptask->hdr);
  728. fg_off = ptask->max_payload - RFC2374_FRAG_HDR_SIZE;
  729. datagram_label = fwnet_get_hdr_dgl(&ptask->hdr);
  730. break;
  731. case RFC2374_HDR_INTFRAG:
  732. dg_size = fwnet_get_hdr_dg_size(&ptask->hdr);
  733. fg_off = fwnet_get_hdr_fg_off(&ptask->hdr)
  734. + ptask->max_payload - RFC2374_FRAG_HDR_SIZE;
  735. datagram_label = fwnet_get_hdr_dgl(&ptask->hdr);
  736. break;
  737. }
  738. if (ptask->dest_node == IEEE1394_ALL_NODES) {
  739. skb_pull(skb,
  740. ptask->max_payload + IEEE1394_GASP_HDR_SIZE);
  741. } else {
  742. skb_pull(skb, ptask->max_payload);
  743. }
  744. if (ptask->outstanding_pkts > 1) {
  745. fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_INTFRAG,
  746. dg_size, fg_off, datagram_label);
  747. } else {
  748. fwnet_make_sf_hdr(&ptask->hdr, RFC2374_HDR_LASTFRAG,
  749. dg_size, fg_off, datagram_label);
  750. ptask->max_payload = skb->len + RFC2374_FRAG_HDR_SIZE;
  751. }
  752. fwnet_send_packet(ptask);
  753. }
  754. if (free)
  755. fwnet_free_ptask(ptask);
  756. }
  757. static void fwnet_transmit_packet_failed(struct fwnet_packet_task *ptask)
  758. {
  759. struct fwnet_device *dev = ptask->dev;
  760. unsigned long flags;
  761. bool free;
  762. spin_lock_irqsave(&dev->lock, flags);
  763. /* One fragment failed; don't try to send remaining fragments. */
  764. ptask->outstanding_pkts = 0;
  765. /* Check whether we or the networking TX soft-IRQ is last user. */
  766. free = ptask->enqueued;
  767. if (free)
  768. dec_queued_datagrams(dev);
  769. dev->netdev->stats.tx_dropped++;
  770. dev->netdev->stats.tx_errors++;
  771. spin_unlock_irqrestore(&dev->lock, flags);
  772. if (free)
  773. fwnet_free_ptask(ptask);
  774. }
  775. static void fwnet_write_complete(struct fw_card *card, int rcode,
  776. void *payload, size_t length, void *data)
  777. {
  778. struct fwnet_packet_task *ptask = data;
  779. static unsigned long j;
  780. static int last_rcode, errors_skipped;
  781. if (rcode == RCODE_COMPLETE) {
  782. fwnet_transmit_packet_done(ptask);
  783. } else {
  784. fwnet_transmit_packet_failed(ptask);
  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. }
  795. static int fwnet_send_packet(struct fwnet_packet_task *ptask)
  796. {
  797. struct fwnet_device *dev;
  798. unsigned tx_len;
  799. struct rfc2734_header *bufhdr;
  800. unsigned long flags;
  801. bool free;
  802. dev = ptask->dev;
  803. tx_len = ptask->max_payload;
  804. switch (fwnet_get_hdr_lf(&ptask->hdr)) {
  805. case RFC2374_HDR_UNFRAG:
  806. bufhdr = (struct rfc2734_header *)
  807. skb_push(ptask->skb, RFC2374_UNFRAG_HDR_SIZE);
  808. put_unaligned_be32(ptask->hdr.w0, &bufhdr->w0);
  809. break;
  810. case RFC2374_HDR_FIRSTFRAG:
  811. case RFC2374_HDR_INTFRAG:
  812. case RFC2374_HDR_LASTFRAG:
  813. bufhdr = (struct rfc2734_header *)
  814. 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. dev->netdev->trans_start = jiffies;
  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 int fwnet_change_mtu(struct net_device *net, int new_mtu)
  1135. {
  1136. if (new_mtu < 68)
  1137. return -EINVAL;
  1138. net->mtu = new_mtu;
  1139. return 0;
  1140. }
  1141. static const struct ethtool_ops fwnet_ethtool_ops = {
  1142. .get_link = ethtool_op_get_link,
  1143. };
  1144. static const struct net_device_ops fwnet_netdev_ops = {
  1145. .ndo_open = fwnet_open,
  1146. .ndo_stop = fwnet_stop,
  1147. .ndo_start_xmit = fwnet_tx,
  1148. .ndo_change_mtu = fwnet_change_mtu,
  1149. };
  1150. static void fwnet_init_dev(struct net_device *net)
  1151. {
  1152. net->header_ops = &fwnet_header_ops;
  1153. net->netdev_ops = &fwnet_netdev_ops;
  1154. net->watchdog_timeo = 2 * HZ;
  1155. net->flags = IFF_BROADCAST | IFF_MULTICAST;
  1156. net->features = NETIF_F_HIGHDMA;
  1157. net->addr_len = FWNET_ALEN;
  1158. net->hard_header_len = FWNET_HLEN;
  1159. net->type = ARPHRD_IEEE1394;
  1160. net->tx_queue_len = FWNET_TX_QUEUE_LEN;
  1161. net->ethtool_ops = &fwnet_ethtool_ops;
  1162. }
  1163. /* caller must hold fwnet_device_mutex */
  1164. static struct fwnet_device *fwnet_dev_find(struct fw_card *card)
  1165. {
  1166. struct fwnet_device *dev;
  1167. list_for_each_entry(dev, &fwnet_device_list, dev_link)
  1168. if (dev->card == card)
  1169. return dev;
  1170. return NULL;
  1171. }
  1172. static int fwnet_add_peer(struct fwnet_device *dev,
  1173. struct fw_unit *unit, struct fw_device *device)
  1174. {
  1175. struct fwnet_peer *peer;
  1176. peer = kmalloc(sizeof(*peer), GFP_KERNEL);
  1177. if (!peer)
  1178. return -ENOMEM;
  1179. dev_set_drvdata(&unit->device, peer);
  1180. peer->dev = dev;
  1181. peer->guid = (u64)device->config_rom[3] << 32 | device->config_rom[4];
  1182. INIT_LIST_HEAD(&peer->pd_list);
  1183. peer->pdg_size = 0;
  1184. peer->datagram_label = 0;
  1185. peer->speed = device->max_speed;
  1186. peer->max_payload = fwnet_max_payload(device->max_rec, peer->speed);
  1187. peer->generation = device->generation;
  1188. smp_rmb();
  1189. peer->node_id = device->node_id;
  1190. spin_lock_irq(&dev->lock);
  1191. list_add_tail(&peer->peer_link, &dev->peer_list);
  1192. dev->peer_count++;
  1193. set_carrier_state(dev);
  1194. spin_unlock_irq(&dev->lock);
  1195. return 0;
  1196. }
  1197. static int fwnet_probe(struct device *_dev)
  1198. {
  1199. struct fw_unit *unit = fw_unit(_dev);
  1200. struct fw_device *device = fw_parent_device(unit);
  1201. struct fw_card *card = device->card;
  1202. struct net_device *net;
  1203. bool allocated_netdev = false;
  1204. struct fwnet_device *dev;
  1205. unsigned max_mtu;
  1206. int ret;
  1207. union fwnet_hwaddr *ha;
  1208. mutex_lock(&fwnet_device_mutex);
  1209. dev = fwnet_dev_find(card);
  1210. if (dev) {
  1211. net = dev->netdev;
  1212. goto have_dev;
  1213. }
  1214. net = alloc_netdev(sizeof(*dev), "firewire%d", fwnet_init_dev);
  1215. if (net == NULL) {
  1216. ret = -ENOMEM;
  1217. goto out;
  1218. }
  1219. allocated_netdev = true;
  1220. SET_NETDEV_DEV(net, card->device);
  1221. dev = netdev_priv(net);
  1222. spin_lock_init(&dev->lock);
  1223. dev->broadcast_state = FWNET_BROADCAST_ERROR;
  1224. dev->broadcast_rcv_context = NULL;
  1225. dev->broadcast_xmt_max_payload = 0;
  1226. dev->broadcast_xmt_datagramlabel = 0;
  1227. dev->local_fifo = FWNET_NO_FIFO_ADDR;
  1228. dev->queued_datagrams = 0;
  1229. INIT_LIST_HEAD(&dev->peer_list);
  1230. dev->card = card;
  1231. dev->netdev = net;
  1232. ret = fwnet_fifo_start(dev);
  1233. if (ret < 0)
  1234. goto out;
  1235. dev->local_fifo = dev->handler.offset;
  1236. /*
  1237. * Use the RFC 2734 default 1500 octets or the maximum payload
  1238. * as initial MTU
  1239. */
  1240. max_mtu = (1 << (card->max_receive + 1))
  1241. - sizeof(struct rfc2734_header) - IEEE1394_GASP_HDR_SIZE;
  1242. net->mtu = min(1500U, max_mtu);
  1243. /* Set our hardware address while we're at it */
  1244. ha = (union fwnet_hwaddr *)net->dev_addr;
  1245. put_unaligned_be64(card->guid, &ha->uc.uniq_id);
  1246. ha->uc.max_rec = dev->card->max_receive;
  1247. ha->uc.sspd = dev->card->link_speed;
  1248. put_unaligned_be16(dev->local_fifo >> 32, &ha->uc.fifo_hi);
  1249. put_unaligned_be32(dev->local_fifo & 0xffffffff, &ha->uc.fifo_lo);
  1250. memset(net->broadcast, -1, net->addr_len);
  1251. ret = register_netdev(net);
  1252. if (ret)
  1253. goto out;
  1254. list_add_tail(&dev->dev_link, &fwnet_device_list);
  1255. dev_notice(&net->dev, "IP over IEEE 1394 on card %s\n",
  1256. dev_name(card->device));
  1257. have_dev:
  1258. ret = fwnet_add_peer(dev, unit, device);
  1259. if (ret && allocated_netdev) {
  1260. unregister_netdev(net);
  1261. list_del(&dev->dev_link);
  1262. out:
  1263. fwnet_fifo_stop(dev);
  1264. free_netdev(net);
  1265. }
  1266. mutex_unlock(&fwnet_device_mutex);
  1267. return ret;
  1268. }
  1269. static void fwnet_remove_peer(struct fwnet_peer *peer, struct fwnet_device *dev)
  1270. {
  1271. struct fwnet_partial_datagram *pd, *pd_next;
  1272. spin_lock_irq(&dev->lock);
  1273. list_del(&peer->peer_link);
  1274. dev->peer_count--;
  1275. set_carrier_state(dev);
  1276. spin_unlock_irq(&dev->lock);
  1277. list_for_each_entry_safe(pd, pd_next, &peer->pd_list, pd_link)
  1278. fwnet_pd_delete(pd);
  1279. kfree(peer);
  1280. }
  1281. static int fwnet_remove(struct device *_dev)
  1282. {
  1283. struct fwnet_peer *peer = dev_get_drvdata(_dev);
  1284. struct fwnet_device *dev = peer->dev;
  1285. struct net_device *net;
  1286. int i;
  1287. mutex_lock(&fwnet_device_mutex);
  1288. net = dev->netdev;
  1289. fwnet_remove_peer(peer, dev);
  1290. if (list_empty(&dev->peer_list)) {
  1291. unregister_netdev(net);
  1292. fwnet_fifo_stop(dev);
  1293. for (i = 0; dev->queued_datagrams && i < 5; i++)
  1294. ssleep(1);
  1295. WARN_ON(dev->queued_datagrams);
  1296. list_del(&dev->dev_link);
  1297. free_netdev(net);
  1298. }
  1299. mutex_unlock(&fwnet_device_mutex);
  1300. return 0;
  1301. }
  1302. /*
  1303. * FIXME abort partially sent fragmented datagrams,
  1304. * discard partially received fragmented datagrams
  1305. */
  1306. static void fwnet_update(struct fw_unit *unit)
  1307. {
  1308. struct fw_device *device = fw_parent_device(unit);
  1309. struct fwnet_peer *peer = dev_get_drvdata(&unit->device);
  1310. int generation;
  1311. generation = device->generation;
  1312. spin_lock_irq(&peer->dev->lock);
  1313. peer->node_id = device->node_id;
  1314. peer->generation = generation;
  1315. spin_unlock_irq(&peer->dev->lock);
  1316. }
  1317. static const struct ieee1394_device_id fwnet_id_table[] = {
  1318. {
  1319. .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
  1320. IEEE1394_MATCH_VERSION,
  1321. .specifier_id = IANA_SPECIFIER_ID,
  1322. .version = RFC2734_SW_VERSION,
  1323. },
  1324. #if IS_ENABLED(CONFIG_IPV6)
  1325. {
  1326. .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
  1327. IEEE1394_MATCH_VERSION,
  1328. .specifier_id = IANA_SPECIFIER_ID,
  1329. .version = RFC3146_SW_VERSION,
  1330. },
  1331. #endif
  1332. { }
  1333. };
  1334. static struct fw_driver fwnet_driver = {
  1335. .driver = {
  1336. .owner = THIS_MODULE,
  1337. .name = KBUILD_MODNAME,
  1338. .bus = &fw_bus_type,
  1339. .probe = fwnet_probe,
  1340. .remove = fwnet_remove,
  1341. },
  1342. .update = fwnet_update,
  1343. .id_table = fwnet_id_table,
  1344. };
  1345. static const u32 rfc2374_unit_directory_data[] = {
  1346. 0x00040000, /* directory_length */
  1347. 0x1200005e, /* unit_specifier_id: IANA */
  1348. 0x81000003, /* textual descriptor offset */
  1349. 0x13000001, /* unit_sw_version: RFC 2734 */
  1350. 0x81000005, /* textual descriptor offset */
  1351. 0x00030000, /* descriptor_length */
  1352. 0x00000000, /* text */
  1353. 0x00000000, /* minimal ASCII, en */
  1354. 0x49414e41, /* I A N A */
  1355. 0x00030000, /* descriptor_length */
  1356. 0x00000000, /* text */
  1357. 0x00000000, /* minimal ASCII, en */
  1358. 0x49507634, /* I P v 4 */
  1359. };
  1360. static struct fw_descriptor rfc2374_unit_directory = {
  1361. .length = ARRAY_SIZE(rfc2374_unit_directory_data),
  1362. .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
  1363. .data = rfc2374_unit_directory_data
  1364. };
  1365. #if IS_ENABLED(CONFIG_IPV6)
  1366. static const u32 rfc3146_unit_directory_data[] = {
  1367. 0x00040000, /* directory_length */
  1368. 0x1200005e, /* unit_specifier_id: IANA */
  1369. 0x81000003, /* textual descriptor offset */
  1370. 0x13000002, /* unit_sw_version: RFC 3146 */
  1371. 0x81000005, /* textual descriptor offset */
  1372. 0x00030000, /* descriptor_length */
  1373. 0x00000000, /* text */
  1374. 0x00000000, /* minimal ASCII, en */
  1375. 0x49414e41, /* I A N A */
  1376. 0x00030000, /* descriptor_length */
  1377. 0x00000000, /* text */
  1378. 0x00000000, /* minimal ASCII, en */
  1379. 0x49507636, /* I P v 6 */
  1380. };
  1381. static struct fw_descriptor rfc3146_unit_directory = {
  1382. .length = ARRAY_SIZE(rfc3146_unit_directory_data),
  1383. .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
  1384. .data = rfc3146_unit_directory_data
  1385. };
  1386. #endif
  1387. static int __init fwnet_init(void)
  1388. {
  1389. int err;
  1390. err = fw_core_add_descriptor(&rfc2374_unit_directory);
  1391. if (err)
  1392. return err;
  1393. #if IS_ENABLED(CONFIG_IPV6)
  1394. err = fw_core_add_descriptor(&rfc3146_unit_directory);
  1395. if (err)
  1396. goto out;
  1397. #endif
  1398. fwnet_packet_task_cache = kmem_cache_create("packet_task",
  1399. sizeof(struct fwnet_packet_task), 0, 0, NULL);
  1400. if (!fwnet_packet_task_cache) {
  1401. err = -ENOMEM;
  1402. goto out2;
  1403. }
  1404. err = driver_register(&fwnet_driver.driver);
  1405. if (!err)
  1406. return 0;
  1407. kmem_cache_destroy(fwnet_packet_task_cache);
  1408. out2:
  1409. #if IS_ENABLED(CONFIG_IPV6)
  1410. fw_core_remove_descriptor(&rfc3146_unit_directory);
  1411. out:
  1412. #endif
  1413. fw_core_remove_descriptor(&rfc2374_unit_directory);
  1414. return err;
  1415. }
  1416. module_init(fwnet_init);
  1417. static void __exit fwnet_cleanup(void)
  1418. {
  1419. driver_unregister(&fwnet_driver.driver);
  1420. kmem_cache_destroy(fwnet_packet_task_cache);
  1421. #if IS_ENABLED(CONFIG_IPV6)
  1422. fw_core_remove_descriptor(&rfc3146_unit_directory);
  1423. #endif
  1424. fw_core_remove_descriptor(&rfc2374_unit_directory);
  1425. }
  1426. module_exit(fwnet_cleanup);
  1427. MODULE_AUTHOR("Jay Fenlason <fenlason@redhat.com>");
  1428. MODULE_DESCRIPTION("IP over IEEE1394 as per RFC 2734/3146");
  1429. MODULE_LICENSE("GPL");
  1430. MODULE_DEVICE_TABLE(ieee1394, fwnet_id_table);