net.c 43 KB

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