net.c 42 KB

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