net.c 43 KB

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