net.c 44 KB

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