net.c 42 KB

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