net.c 41 KB

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