net.c 42 KB

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