net.c 42 KB

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