net.c 43 KB

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