net.c 42 KB

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