net.c 43 KB

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