net.c 43 KB

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