net.c 42 KB

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