net.c 42 KB

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