net.c 42 KB

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