eth1394.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. /*
  2. * eth1394.c -- IPv4 driver for Linux IEEE-1394 Subsystem
  3. *
  4. * Copyright (C) 2001-2003 Ben Collins <bcollins@debian.org>
  5. * 2000 Bonin Franck <boninf@free.fr>
  6. * 2003 Steve Kinneberg <kinnebergsteve@acmsystems.com>
  7. *
  8. * Mainly based on work by Emanuel Pirker and Andreas E. Bombe
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software Foundation,
  22. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. /*
  25. * This driver intends to support RFC 2734, which describes a method for
  26. * transporting IPv4 datagrams over IEEE-1394 serial busses.
  27. *
  28. * TODO:
  29. * RFC 2734 related:
  30. * - Add MCAP. Limited Multicast exists only to 224.0.0.1 and 224.0.0.2.
  31. *
  32. * Non-RFC 2734 related:
  33. * - Handle fragmented skb's coming from the networking layer.
  34. * - Move generic GASP reception to core 1394 code
  35. * - Convert kmalloc/kfree for link fragments to use kmem_cache_* instead
  36. * - Stability improvements
  37. * - Performance enhancements
  38. * - Consider garbage collecting old partial datagrams after X amount of time
  39. */
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. #include <linux/slab.h>
  43. #include <linux/errno.h>
  44. #include <linux/types.h>
  45. #include <linux/delay.h>
  46. #include <linux/init.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/netdevice.h>
  49. #include <linux/inetdevice.h>
  50. #include <linux/if_arp.h>
  51. #include <linux/if_ether.h>
  52. #include <linux/ip.h>
  53. #include <linux/in.h>
  54. #include <linux/tcp.h>
  55. #include <linux/skbuff.h>
  56. #include <linux/bitops.h>
  57. #include <linux/ethtool.h>
  58. #include <asm/uaccess.h>
  59. #include <asm/delay.h>
  60. #include <asm/unaligned.h>
  61. #include <net/arp.h>
  62. #include "config_roms.h"
  63. #include "csr1212.h"
  64. #include "eth1394.h"
  65. #include "highlevel.h"
  66. #include "ieee1394.h"
  67. #include "ieee1394_core.h"
  68. #include "ieee1394_hotplug.h"
  69. #include "ieee1394_transactions.h"
  70. #include "ieee1394_types.h"
  71. #include "iso.h"
  72. #include "nodemgr.h"
  73. #define ETH1394_PRINT_G(level, fmt, args...) \
  74. printk(level "%s: " fmt, driver_name, ## args)
  75. #define ETH1394_PRINT(level, dev_name, fmt, args...) \
  76. printk(level "%s: %s: " fmt, driver_name, dev_name, ## args)
  77. struct fragment_info {
  78. struct list_head list;
  79. int offset;
  80. int len;
  81. };
  82. struct partial_datagram {
  83. struct list_head list;
  84. u16 dgl;
  85. u16 dg_size;
  86. __be16 ether_type;
  87. struct sk_buff *skb;
  88. char *pbuf;
  89. struct list_head frag_info;
  90. };
  91. struct pdg_list {
  92. struct list_head list; /* partial datagram list per node */
  93. unsigned int sz; /* partial datagram list size per node */
  94. spinlock_t lock; /* partial datagram lock */
  95. };
  96. struct eth1394_host_info {
  97. struct hpsb_host *host;
  98. struct net_device *dev;
  99. };
  100. struct eth1394_node_ref {
  101. struct unit_directory *ud;
  102. struct list_head list;
  103. };
  104. struct eth1394_node_info {
  105. u16 maxpayload; /* max payload */
  106. u8 sspd; /* max speed */
  107. u64 fifo; /* FIFO address */
  108. struct pdg_list pdg; /* partial RX datagram lists */
  109. int dgl; /* outgoing datagram label */
  110. };
  111. static const char driver_name[] = "eth1394";
  112. static struct kmem_cache *packet_task_cache;
  113. static struct hpsb_highlevel eth1394_highlevel;
  114. /* Use common.lf to determine header len */
  115. static const int hdr_type_len[] = {
  116. sizeof(struct eth1394_uf_hdr),
  117. sizeof(struct eth1394_ff_hdr),
  118. sizeof(struct eth1394_sf_hdr),
  119. sizeof(struct eth1394_sf_hdr)
  120. };
  121. static const u16 eth1394_speedto_maxpayload[] = {
  122. /* S100, S200, S400, S800, S1600, S3200 */
  123. 512, 1024, 2048, 4096, 4096, 4096
  124. };
  125. MODULE_AUTHOR("Ben Collins (bcollins@debian.org)");
  126. MODULE_DESCRIPTION("IEEE 1394 IPv4 Driver (IPv4-over-1394 as per RFC 2734)");
  127. MODULE_LICENSE("GPL");
  128. /*
  129. * The max_partial_datagrams parameter is the maximum number of fragmented
  130. * datagrams per node that eth1394 will keep in memory. Providing an upper
  131. * bound allows us to limit the amount of memory that partial datagrams
  132. * consume in the event that some partial datagrams are never completed.
  133. */
  134. static int max_partial_datagrams = 25;
  135. module_param(max_partial_datagrams, int, S_IRUGO | S_IWUSR);
  136. MODULE_PARM_DESC(max_partial_datagrams,
  137. "Maximum number of partially received fragmented datagrams "
  138. "(default = 25).");
  139. static int ether1394_header(struct sk_buff *skb, struct net_device *dev,
  140. unsigned short type, const void *daddr,
  141. const void *saddr, unsigned len);
  142. static int ether1394_rebuild_header(struct sk_buff *skb);
  143. static int ether1394_header_parse(const struct sk_buff *skb,
  144. unsigned char *haddr);
  145. static int ether1394_header_cache(const struct neighbour *neigh,
  146. struct hh_cache *hh);
  147. static void ether1394_header_cache_update(struct hh_cache *hh,
  148. const struct net_device *dev,
  149. const unsigned char *haddr);
  150. static netdev_tx_t ether1394_tx(struct sk_buff *skb,
  151. struct net_device *dev);
  152. static void ether1394_iso(struct hpsb_iso *iso);
  153. static const struct ethtool_ops ethtool_ops;
  154. static int ether1394_write(struct hpsb_host *host, int srcid, int destid,
  155. quadlet_t *data, u64 addr, size_t len, u16 flags);
  156. static void ether1394_add_host(struct hpsb_host *host);
  157. static void ether1394_remove_host(struct hpsb_host *host);
  158. static void ether1394_host_reset(struct hpsb_host *host);
  159. /* Function for incoming 1394 packets */
  160. static const struct hpsb_address_ops addr_ops = {
  161. .write = ether1394_write,
  162. };
  163. /* Ieee1394 highlevel driver functions */
  164. static struct hpsb_highlevel eth1394_highlevel = {
  165. .name = driver_name,
  166. .add_host = ether1394_add_host,
  167. .remove_host = ether1394_remove_host,
  168. .host_reset = ether1394_host_reset,
  169. };
  170. static int ether1394_recv_init(struct eth1394_priv *priv)
  171. {
  172. unsigned int iso_buf_size;
  173. /* FIXME: rawiso limits us to PAGE_SIZE */
  174. iso_buf_size = min((unsigned int)PAGE_SIZE,
  175. 2 * (1U << (priv->host->csr.max_rec + 1)));
  176. priv->iso = hpsb_iso_recv_init(priv->host,
  177. ETHER1394_GASP_BUFFERS * iso_buf_size,
  178. ETHER1394_GASP_BUFFERS,
  179. priv->broadcast_channel,
  180. HPSB_ISO_DMA_PACKET_PER_BUFFER,
  181. 1, ether1394_iso);
  182. if (priv->iso == NULL) {
  183. ETH1394_PRINT_G(KERN_ERR, "Failed to allocate IR context\n");
  184. priv->bc_state = ETHER1394_BC_ERROR;
  185. return -EAGAIN;
  186. }
  187. if (hpsb_iso_recv_start(priv->iso, -1, (1 << 3), -1) < 0)
  188. priv->bc_state = ETHER1394_BC_STOPPED;
  189. else
  190. priv->bc_state = ETHER1394_BC_RUNNING;
  191. return 0;
  192. }
  193. /* This is called after an "ifup" */
  194. static int ether1394_open(struct net_device *dev)
  195. {
  196. struct eth1394_priv *priv = netdev_priv(dev);
  197. int ret;
  198. if (priv->bc_state == ETHER1394_BC_ERROR) {
  199. ret = ether1394_recv_init(priv);
  200. if (ret)
  201. return ret;
  202. }
  203. netif_start_queue(dev);
  204. return 0;
  205. }
  206. /* This is called after an "ifdown" */
  207. static int ether1394_stop(struct net_device *dev)
  208. {
  209. /* flush priv->wake */
  210. flush_scheduled_work();
  211. netif_stop_queue(dev);
  212. return 0;
  213. }
  214. /* FIXME: What to do if we timeout? I think a host reset is probably in order,
  215. * so that's what we do. Should we increment the stat counters too? */
  216. static void ether1394_tx_timeout(struct net_device *dev)
  217. {
  218. struct hpsb_host *host =
  219. ((struct eth1394_priv *)netdev_priv(dev))->host;
  220. ETH1394_PRINT(KERN_ERR, dev->name, "Timeout, resetting host\n");
  221. ether1394_host_reset(host);
  222. }
  223. static inline int ether1394_max_mtu(struct hpsb_host* host)
  224. {
  225. return (1 << (host->csr.max_rec + 1))
  226. - sizeof(union eth1394_hdr) - ETHER1394_GASP_OVERHEAD;
  227. }
  228. static int ether1394_change_mtu(struct net_device *dev, int new_mtu)
  229. {
  230. int max_mtu;
  231. if (new_mtu < 68)
  232. return -EINVAL;
  233. max_mtu = ether1394_max_mtu(
  234. ((struct eth1394_priv *)netdev_priv(dev))->host);
  235. if (new_mtu > max_mtu) {
  236. ETH1394_PRINT(KERN_INFO, dev->name,
  237. "Local node constrains MTU to %d\n", max_mtu);
  238. return -ERANGE;
  239. }
  240. dev->mtu = new_mtu;
  241. return 0;
  242. }
  243. static void purge_partial_datagram(struct list_head *old)
  244. {
  245. struct partial_datagram *pd;
  246. struct list_head *lh, *n;
  247. struct fragment_info *fi;
  248. pd = list_entry(old, struct partial_datagram, list);
  249. list_for_each_safe(lh, n, &pd->frag_info) {
  250. fi = list_entry(lh, struct fragment_info, list);
  251. list_del(lh);
  252. kfree(fi);
  253. }
  254. list_del(old);
  255. kfree_skb(pd->skb);
  256. kfree(pd);
  257. }
  258. /******************************************
  259. * 1394 bus activity functions
  260. ******************************************/
  261. static struct eth1394_node_ref *eth1394_find_node(struct list_head *inl,
  262. struct unit_directory *ud)
  263. {
  264. struct eth1394_node_ref *node;
  265. list_for_each_entry(node, inl, list)
  266. if (node->ud == ud)
  267. return node;
  268. return NULL;
  269. }
  270. static struct eth1394_node_ref *eth1394_find_node_guid(struct list_head *inl,
  271. u64 guid)
  272. {
  273. struct eth1394_node_ref *node;
  274. list_for_each_entry(node, inl, list)
  275. if (node->ud->ne->guid == guid)
  276. return node;
  277. return NULL;
  278. }
  279. static struct eth1394_node_ref *eth1394_find_node_nodeid(struct list_head *inl,
  280. nodeid_t nodeid)
  281. {
  282. struct eth1394_node_ref *node;
  283. list_for_each_entry(node, inl, list)
  284. if (node->ud->ne->nodeid == nodeid)
  285. return node;
  286. return NULL;
  287. }
  288. static int eth1394_new_node(struct eth1394_host_info *hi,
  289. struct unit_directory *ud)
  290. {
  291. struct eth1394_priv *priv;
  292. struct eth1394_node_ref *new_node;
  293. struct eth1394_node_info *node_info;
  294. new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
  295. if (!new_node)
  296. return -ENOMEM;
  297. node_info = kmalloc(sizeof(*node_info), GFP_KERNEL);
  298. if (!node_info) {
  299. kfree(new_node);
  300. return -ENOMEM;
  301. }
  302. spin_lock_init(&node_info->pdg.lock);
  303. INIT_LIST_HEAD(&node_info->pdg.list);
  304. node_info->pdg.sz = 0;
  305. node_info->fifo = CSR1212_INVALID_ADDR_SPACE;
  306. dev_set_drvdata(&ud->device, node_info);
  307. new_node->ud = ud;
  308. priv = netdev_priv(hi->dev);
  309. list_add_tail(&new_node->list, &priv->ip_node_list);
  310. return 0;
  311. }
  312. static int eth1394_probe(struct device *dev)
  313. {
  314. struct unit_directory *ud;
  315. struct eth1394_host_info *hi;
  316. ud = container_of(dev, struct unit_directory, device);
  317. hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
  318. if (!hi)
  319. return -ENOENT;
  320. return eth1394_new_node(hi, ud);
  321. }
  322. static int eth1394_remove(struct device *dev)
  323. {
  324. struct unit_directory *ud;
  325. struct eth1394_host_info *hi;
  326. struct eth1394_priv *priv;
  327. struct eth1394_node_ref *old_node;
  328. struct eth1394_node_info *node_info;
  329. struct list_head *lh, *n;
  330. unsigned long flags;
  331. ud = container_of(dev, struct unit_directory, device);
  332. hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
  333. if (!hi)
  334. return -ENOENT;
  335. priv = netdev_priv(hi->dev);
  336. old_node = eth1394_find_node(&priv->ip_node_list, ud);
  337. if (!old_node)
  338. return 0;
  339. list_del(&old_node->list);
  340. kfree(old_node);
  341. node_info = dev_get_drvdata(&ud->device);
  342. spin_lock_irqsave(&node_info->pdg.lock, flags);
  343. /* The partial datagram list should be empty, but we'll just
  344. * make sure anyway... */
  345. list_for_each_safe(lh, n, &node_info->pdg.list)
  346. purge_partial_datagram(lh);
  347. spin_unlock_irqrestore(&node_info->pdg.lock, flags);
  348. kfree(node_info);
  349. dev_set_drvdata(&ud->device, NULL);
  350. return 0;
  351. }
  352. static int eth1394_update(struct unit_directory *ud)
  353. {
  354. struct eth1394_host_info *hi;
  355. struct eth1394_priv *priv;
  356. struct eth1394_node_ref *node;
  357. hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
  358. if (!hi)
  359. return -ENOENT;
  360. priv = netdev_priv(hi->dev);
  361. node = eth1394_find_node(&priv->ip_node_list, ud);
  362. if (node)
  363. return 0;
  364. return eth1394_new_node(hi, ud);
  365. }
  366. static const struct ieee1394_device_id eth1394_id_table[] = {
  367. {
  368. .match_flags = (IEEE1394_MATCH_SPECIFIER_ID |
  369. IEEE1394_MATCH_VERSION),
  370. .specifier_id = ETHER1394_GASP_SPECIFIER_ID,
  371. .version = ETHER1394_GASP_VERSION,
  372. },
  373. {}
  374. };
  375. MODULE_DEVICE_TABLE(ieee1394, eth1394_id_table);
  376. static struct hpsb_protocol_driver eth1394_proto_driver = {
  377. .name = driver_name,
  378. .id_table = eth1394_id_table,
  379. .update = eth1394_update,
  380. .driver = {
  381. .probe = eth1394_probe,
  382. .remove = eth1394_remove,
  383. },
  384. };
  385. static void ether1394_reset_priv(struct net_device *dev, int set_mtu)
  386. {
  387. unsigned long flags;
  388. int i;
  389. struct eth1394_priv *priv = netdev_priv(dev);
  390. struct hpsb_host *host = priv->host;
  391. u64 guid = get_unaligned((u64 *)&(host->csr.rom->bus_info_data[3]));
  392. int max_speed = IEEE1394_SPEED_MAX;
  393. spin_lock_irqsave(&priv->lock, flags);
  394. memset(priv->ud_list, 0, sizeof(priv->ud_list));
  395. priv->bc_maxpayload = 512;
  396. /* Determine speed limit */
  397. /* FIXME: This is broken for nodes with link speed < PHY speed,
  398. * and it is suboptimal for S200B...S800B hardware.
  399. * The result of nodemgr's speed probe should be used somehow. */
  400. for (i = 0; i < host->node_count; i++) {
  401. /* take care of S100B...S400B PHY ports */
  402. if (host->speed[i] == SELFID_SPEED_UNKNOWN) {
  403. max_speed = IEEE1394_SPEED_100;
  404. break;
  405. }
  406. if (max_speed > host->speed[i])
  407. max_speed = host->speed[i];
  408. }
  409. priv->bc_sspd = max_speed;
  410. if (set_mtu) {
  411. /* Use the RFC 2734 default 1500 octets or the maximum payload
  412. * as initial MTU */
  413. dev->mtu = min(1500, ether1394_max_mtu(host));
  414. /* Set our hardware address while we're at it */
  415. memcpy(dev->dev_addr, &guid, sizeof(u64));
  416. memset(dev->broadcast, 0xff, sizeof(u64));
  417. }
  418. spin_unlock_irqrestore(&priv->lock, flags);
  419. }
  420. static const struct header_ops ether1394_header_ops = {
  421. .create = ether1394_header,
  422. .rebuild = ether1394_rebuild_header,
  423. .cache = ether1394_header_cache,
  424. .cache_update = ether1394_header_cache_update,
  425. .parse = ether1394_header_parse,
  426. };
  427. static const struct net_device_ops ether1394_netdev_ops = {
  428. .ndo_open = ether1394_open,
  429. .ndo_stop = ether1394_stop,
  430. .ndo_start_xmit = ether1394_tx,
  431. .ndo_tx_timeout = ether1394_tx_timeout,
  432. .ndo_change_mtu = ether1394_change_mtu,
  433. };
  434. static void ether1394_init_dev(struct net_device *dev)
  435. {
  436. dev->header_ops = &ether1394_header_ops;
  437. dev->netdev_ops = &ether1394_netdev_ops;
  438. SET_ETHTOOL_OPS(dev, &ethtool_ops);
  439. dev->watchdog_timeo = ETHER1394_TIMEOUT;
  440. dev->flags = IFF_BROADCAST | IFF_MULTICAST;
  441. dev->features = NETIF_F_HIGHDMA;
  442. dev->addr_len = ETH1394_ALEN;
  443. dev->hard_header_len = ETH1394_HLEN;
  444. dev->type = ARPHRD_IEEE1394;
  445. /* FIXME: This value was copied from ether_setup(). Is it too much? */
  446. dev->tx_queue_len = 1000;
  447. }
  448. /*
  449. * Wake the queue up after commonly encountered transmit failure conditions are
  450. * hopefully over. Currently only tlabel exhaustion is accounted for.
  451. */
  452. static void ether1394_wake_queue(struct work_struct *work)
  453. {
  454. struct eth1394_priv *priv;
  455. struct hpsb_packet *packet;
  456. priv = container_of(work, struct eth1394_priv, wake);
  457. packet = hpsb_alloc_packet(0);
  458. /* This is really bad, but unjam the queue anyway. */
  459. if (!packet)
  460. goto out;
  461. packet->host = priv->host;
  462. packet->node_id = priv->wake_node;
  463. /*
  464. * A transaction label is all we really want. If we get one, it almost
  465. * always means we can get a lot more because the ieee1394 core recycled
  466. * a whole batch of tlabels, at last.
  467. */
  468. if (hpsb_get_tlabel(packet) == 0)
  469. hpsb_free_tlabel(packet);
  470. hpsb_free_packet(packet);
  471. out:
  472. netif_wake_queue(priv->wake_dev);
  473. }
  474. /*
  475. * This function is called every time a card is found. It is generally called
  476. * when the module is installed. This is where we add all of our ethernet
  477. * devices. One for each host.
  478. */
  479. static void ether1394_add_host(struct hpsb_host *host)
  480. {
  481. struct eth1394_host_info *hi = NULL;
  482. struct net_device *dev = NULL;
  483. struct eth1394_priv *priv;
  484. u64 fifo_addr;
  485. if (hpsb_config_rom_ip1394_add(host) != 0) {
  486. ETH1394_PRINT_G(KERN_ERR, "Can't add IP-over-1394 ROM entry\n");
  487. return;
  488. }
  489. fifo_addr = hpsb_allocate_and_register_addrspace(
  490. &eth1394_highlevel, host, &addr_ops,
  491. ETHER1394_REGION_ADDR_LEN, ETHER1394_REGION_ADDR_LEN,
  492. CSR1212_INVALID_ADDR_SPACE, CSR1212_INVALID_ADDR_SPACE);
  493. if (fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
  494. ETH1394_PRINT_G(KERN_ERR, "Cannot register CSR space\n");
  495. hpsb_config_rom_ip1394_remove(host);
  496. return;
  497. }
  498. dev = alloc_netdev(sizeof(*priv), "eth%d", ether1394_init_dev);
  499. if (dev == NULL) {
  500. ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
  501. goto out;
  502. }
  503. SET_NETDEV_DEV(dev, &host->device);
  504. priv = netdev_priv(dev);
  505. INIT_LIST_HEAD(&priv->ip_node_list);
  506. spin_lock_init(&priv->lock);
  507. priv->host = host;
  508. priv->local_fifo = fifo_addr;
  509. INIT_WORK(&priv->wake, ether1394_wake_queue);
  510. priv->wake_dev = dev;
  511. hi = hpsb_create_hostinfo(&eth1394_highlevel, host, sizeof(*hi));
  512. if (hi == NULL) {
  513. ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
  514. goto out;
  515. }
  516. ether1394_reset_priv(dev, 1);
  517. if (register_netdev(dev)) {
  518. ETH1394_PRINT_G(KERN_ERR, "Cannot register the driver\n");
  519. goto out;
  520. }
  521. ETH1394_PRINT(KERN_INFO, dev->name, "IPv4 over IEEE 1394 (fw-host%d)\n",
  522. host->id);
  523. hi->host = host;
  524. hi->dev = dev;
  525. /* Ignore validity in hopes that it will be set in the future. It'll
  526. * be checked when the eth device is opened. */
  527. priv->broadcast_channel = host->csr.broadcast_channel & 0x3f;
  528. ether1394_recv_init(priv);
  529. return;
  530. out:
  531. if (dev)
  532. free_netdev(dev);
  533. if (hi)
  534. hpsb_destroy_hostinfo(&eth1394_highlevel, host);
  535. hpsb_unregister_addrspace(&eth1394_highlevel, host, fifo_addr);
  536. hpsb_config_rom_ip1394_remove(host);
  537. }
  538. /* Remove a card from our list */
  539. static void ether1394_remove_host(struct hpsb_host *host)
  540. {
  541. struct eth1394_host_info *hi;
  542. struct eth1394_priv *priv;
  543. hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
  544. if (!hi)
  545. return;
  546. priv = netdev_priv(hi->dev);
  547. hpsb_unregister_addrspace(&eth1394_highlevel, host, priv->local_fifo);
  548. hpsb_config_rom_ip1394_remove(host);
  549. if (priv->iso)
  550. hpsb_iso_shutdown(priv->iso);
  551. unregister_netdev(hi->dev);
  552. free_netdev(hi->dev);
  553. }
  554. /* A bus reset happened */
  555. static void ether1394_host_reset(struct hpsb_host *host)
  556. {
  557. struct eth1394_host_info *hi;
  558. struct eth1394_priv *priv;
  559. struct net_device *dev;
  560. struct list_head *lh, *n;
  561. struct eth1394_node_ref *node;
  562. struct eth1394_node_info *node_info;
  563. unsigned long flags;
  564. hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
  565. /* This can happen for hosts that we don't use */
  566. if (!hi)
  567. return;
  568. dev = hi->dev;
  569. priv = netdev_priv(dev);
  570. /* Reset our private host data, but not our MTU */
  571. netif_stop_queue(dev);
  572. ether1394_reset_priv(dev, 0);
  573. list_for_each_entry(node, &priv->ip_node_list, list) {
  574. node_info = dev_get_drvdata(&node->ud->device);
  575. spin_lock_irqsave(&node_info->pdg.lock, flags);
  576. list_for_each_safe(lh, n, &node_info->pdg.list)
  577. purge_partial_datagram(lh);
  578. INIT_LIST_HEAD(&(node_info->pdg.list));
  579. node_info->pdg.sz = 0;
  580. spin_unlock_irqrestore(&node_info->pdg.lock, flags);
  581. }
  582. netif_wake_queue(dev);
  583. }
  584. /******************************************
  585. * HW Header net device functions
  586. ******************************************/
  587. /* These functions have been adapted from net/ethernet/eth.c */
  588. /* Create a fake MAC header for an arbitrary protocol layer.
  589. * saddr=NULL means use device source address
  590. * daddr=NULL means leave destination address (eg unresolved arp). */
  591. static int ether1394_header(struct sk_buff *skb, struct net_device *dev,
  592. unsigned short type, const void *daddr,
  593. const void *saddr, unsigned len)
  594. {
  595. struct eth1394hdr *eth =
  596. (struct eth1394hdr *)skb_push(skb, ETH1394_HLEN);
  597. eth->h_proto = htons(type);
  598. if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
  599. memset(eth->h_dest, 0, dev->addr_len);
  600. return dev->hard_header_len;
  601. }
  602. if (daddr) {
  603. memcpy(eth->h_dest, daddr, dev->addr_len);
  604. return dev->hard_header_len;
  605. }
  606. return -dev->hard_header_len;
  607. }
  608. /* Rebuild the faked MAC header. This is called after an ARP
  609. * (or in future other address resolution) has completed on this
  610. * sk_buff. We now let ARP fill in the other fields.
  611. *
  612. * This routine CANNOT use cached dst->neigh!
  613. * Really, it is used only when dst->neigh is wrong.
  614. */
  615. static int ether1394_rebuild_header(struct sk_buff *skb)
  616. {
  617. struct eth1394hdr *eth = (struct eth1394hdr *)skb->data;
  618. if (eth->h_proto == htons(ETH_P_IP))
  619. return arp_find((unsigned char *)&eth->h_dest, skb);
  620. ETH1394_PRINT(KERN_DEBUG, skb->dev->name,
  621. "unable to resolve type %04x addresses\n",
  622. ntohs(eth->h_proto));
  623. return 0;
  624. }
  625. static int ether1394_header_parse(const struct sk_buff *skb,
  626. unsigned char *haddr)
  627. {
  628. memcpy(haddr, skb->dev->dev_addr, ETH1394_ALEN);
  629. return ETH1394_ALEN;
  630. }
  631. static int ether1394_header_cache(const struct neighbour *neigh,
  632. struct hh_cache *hh)
  633. {
  634. __be16 type = hh->hh_type;
  635. struct net_device *dev = neigh->dev;
  636. struct eth1394hdr *eth =
  637. (struct eth1394hdr *)((u8 *)hh->hh_data + 16 - ETH1394_HLEN);
  638. if (type == htons(ETH_P_802_3))
  639. return -1;
  640. eth->h_proto = type;
  641. memcpy(eth->h_dest, neigh->ha, dev->addr_len);
  642. hh->hh_len = ETH1394_HLEN;
  643. return 0;
  644. }
  645. /* Called by Address Resolution module to notify changes in address. */
  646. static void ether1394_header_cache_update(struct hh_cache *hh,
  647. const struct net_device *dev,
  648. const unsigned char * haddr)
  649. {
  650. memcpy((u8 *)hh->hh_data + 16 - ETH1394_HLEN, haddr, dev->addr_len);
  651. }
  652. /******************************************
  653. * Datagram reception code
  654. ******************************************/
  655. /* Copied from net/ethernet/eth.c */
  656. static __be16 ether1394_type_trans(struct sk_buff *skb, struct net_device *dev)
  657. {
  658. struct eth1394hdr *eth;
  659. unsigned char *rawp;
  660. skb_reset_mac_header(skb);
  661. skb_pull(skb, ETH1394_HLEN);
  662. eth = eth1394_hdr(skb);
  663. if (*eth->h_dest & 1) {
  664. if (memcmp(eth->h_dest, dev->broadcast, dev->addr_len) == 0)
  665. skb->pkt_type = PACKET_BROADCAST;
  666. #if 0
  667. else
  668. skb->pkt_type = PACKET_MULTICAST;
  669. #endif
  670. } else {
  671. if (memcmp(eth->h_dest, dev->dev_addr, dev->addr_len))
  672. skb->pkt_type = PACKET_OTHERHOST;
  673. }
  674. if (ntohs(eth->h_proto) >= 1536)
  675. return eth->h_proto;
  676. rawp = skb->data;
  677. if (*(unsigned short *)rawp == 0xFFFF)
  678. return htons(ETH_P_802_3);
  679. return htons(ETH_P_802_2);
  680. }
  681. /* Parse an encapsulated IP1394 header into an ethernet frame packet.
  682. * We also perform ARP translation here, if need be. */
  683. static __be16 ether1394_parse_encap(struct sk_buff *skb, struct net_device *dev,
  684. nodeid_t srcid, nodeid_t destid,
  685. __be16 ether_type)
  686. {
  687. struct eth1394_priv *priv = netdev_priv(dev);
  688. __be64 dest_hw;
  689. __be16 ret = 0;
  690. /* Setup our hw addresses. We use these to build the ethernet header. */
  691. if (destid == (LOCAL_BUS | ALL_NODES))
  692. dest_hw = ~cpu_to_be64(0); /* broadcast */
  693. else
  694. dest_hw = cpu_to_be64((u64)priv->host->csr.guid_hi << 32 |
  695. priv->host->csr.guid_lo);
  696. /* If this is an ARP packet, convert it. First, we want to make
  697. * use of some of the fields, since they tell us a little bit
  698. * about the sending machine. */
  699. if (ether_type == htons(ETH_P_ARP)) {
  700. struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
  701. struct arphdr *arp = (struct arphdr *)skb->data;
  702. unsigned char *arp_ptr = (unsigned char *)(arp + 1);
  703. u64 fifo_addr = (u64)ntohs(arp1394->fifo_hi) << 32 |
  704. ntohl(arp1394->fifo_lo);
  705. u8 max_rec = min(priv->host->csr.max_rec,
  706. (u8)(arp1394->max_rec));
  707. int sspd = arp1394->sspd;
  708. u16 maxpayload;
  709. struct eth1394_node_ref *node;
  710. struct eth1394_node_info *node_info;
  711. __be64 guid;
  712. /* Sanity check. MacOSX seems to be sending us 131 in this
  713. * field (atleast on my Panther G5). Not sure why. */
  714. if (sspd > 5 || sspd < 0)
  715. sspd = 0;
  716. maxpayload = min(eth1394_speedto_maxpayload[sspd],
  717. (u16)(1 << (max_rec + 1)));
  718. guid = get_unaligned(&arp1394->s_uniq_id);
  719. node = eth1394_find_node_guid(&priv->ip_node_list,
  720. be64_to_cpu(guid));
  721. if (!node)
  722. return cpu_to_be16(0);
  723. node_info = dev_get_drvdata(&node->ud->device);
  724. /* Update our speed/payload/fifo_offset table */
  725. node_info->maxpayload = maxpayload;
  726. node_info->sspd = sspd;
  727. node_info->fifo = fifo_addr;
  728. /* Now that we're done with the 1394 specific stuff, we'll
  729. * need to alter some of the data. Believe it or not, all
  730. * that needs to be done is sender_IP_address needs to be
  731. * moved, the destination hardware address get stuffed
  732. * in and the hardware address length set to 8.
  733. *
  734. * IMPORTANT: The code below overwrites 1394 specific data
  735. * needed above so keep the munging of the data for the
  736. * higher level IP stack last. */
  737. arp->ar_hln = 8;
  738. arp_ptr += arp->ar_hln; /* skip over sender unique id */
  739. *(u32 *)arp_ptr = arp1394->sip; /* move sender IP addr */
  740. arp_ptr += arp->ar_pln; /* skip over sender IP addr */
  741. if (arp->ar_op == htons(ARPOP_REQUEST))
  742. memset(arp_ptr, 0, sizeof(u64));
  743. else
  744. memcpy(arp_ptr, dev->dev_addr, sizeof(u64));
  745. }
  746. /* Now add the ethernet header. */
  747. if (dev_hard_header(skb, dev, ntohs(ether_type), &dest_hw, NULL,
  748. skb->len) >= 0)
  749. ret = ether1394_type_trans(skb, dev);
  750. return ret;
  751. }
  752. static int fragment_overlap(struct list_head *frag_list, int offset, int len)
  753. {
  754. struct fragment_info *fi;
  755. int end = offset + len;
  756. list_for_each_entry(fi, frag_list, list)
  757. if (offset < fi->offset + fi->len && end > fi->offset)
  758. return 1;
  759. return 0;
  760. }
  761. static struct list_head *find_partial_datagram(struct list_head *pdgl, int dgl)
  762. {
  763. struct partial_datagram *pd;
  764. list_for_each_entry(pd, pdgl, list)
  765. if (pd->dgl == dgl)
  766. return &pd->list;
  767. return NULL;
  768. }
  769. /* Assumes that new fragment does not overlap any existing fragments */
  770. static int new_fragment(struct list_head *frag_info, int offset, int len)
  771. {
  772. struct list_head *lh;
  773. struct fragment_info *fi, *fi2, *new;
  774. list_for_each(lh, frag_info) {
  775. fi = list_entry(lh, struct fragment_info, list);
  776. if (fi->offset + fi->len == offset) {
  777. /* The new fragment can be tacked on to the end */
  778. fi->len += len;
  779. /* Did the new fragment plug a hole? */
  780. fi2 = list_entry(lh->next, struct fragment_info, list);
  781. if (fi->offset + fi->len == fi2->offset) {
  782. /* glue fragments together */
  783. fi->len += fi2->len;
  784. list_del(lh->next);
  785. kfree(fi2);
  786. }
  787. return 0;
  788. } else if (offset + len == fi->offset) {
  789. /* The new fragment can be tacked on to the beginning */
  790. fi->offset = offset;
  791. fi->len += len;
  792. /* Did the new fragment plug a hole? */
  793. fi2 = list_entry(lh->prev, struct fragment_info, list);
  794. if (fi2->offset + fi2->len == fi->offset) {
  795. /* glue fragments together */
  796. fi2->len += fi->len;
  797. list_del(lh);
  798. kfree(fi);
  799. }
  800. return 0;
  801. } else if (offset > fi->offset + fi->len) {
  802. break;
  803. } else if (offset + len < fi->offset) {
  804. lh = lh->prev;
  805. break;
  806. }
  807. }
  808. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  809. if (!new)
  810. return -ENOMEM;
  811. new->offset = offset;
  812. new->len = len;
  813. list_add(&new->list, lh);
  814. return 0;
  815. }
  816. static int new_partial_datagram(struct net_device *dev, struct list_head *pdgl,
  817. int dgl, int dg_size, char *frag_buf,
  818. int frag_off, int frag_len)
  819. {
  820. struct partial_datagram *new;
  821. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  822. if (!new)
  823. return -ENOMEM;
  824. INIT_LIST_HEAD(&new->frag_info);
  825. if (new_fragment(&new->frag_info, frag_off, frag_len) < 0) {
  826. kfree(new);
  827. return -ENOMEM;
  828. }
  829. new->dgl = dgl;
  830. new->dg_size = dg_size;
  831. new->skb = dev_alloc_skb(dg_size + dev->hard_header_len + 15);
  832. if (!new->skb) {
  833. struct fragment_info *fi = list_entry(new->frag_info.next,
  834. struct fragment_info,
  835. list);
  836. kfree(fi);
  837. kfree(new);
  838. return -ENOMEM;
  839. }
  840. skb_reserve(new->skb, (dev->hard_header_len + 15) & ~15);
  841. new->pbuf = skb_put(new->skb, dg_size);
  842. memcpy(new->pbuf + frag_off, frag_buf, frag_len);
  843. list_add(&new->list, pdgl);
  844. return 0;
  845. }
  846. static int update_partial_datagram(struct list_head *pdgl, struct list_head *lh,
  847. char *frag_buf, int frag_off, int frag_len)
  848. {
  849. struct partial_datagram *pd =
  850. list_entry(lh, struct partial_datagram, list);
  851. if (new_fragment(&pd->frag_info, frag_off, frag_len) < 0)
  852. return -ENOMEM;
  853. memcpy(pd->pbuf + frag_off, frag_buf, frag_len);
  854. /* Move list entry to beginnig of list so that oldest partial
  855. * datagrams percolate to the end of the list */
  856. list_move(lh, pdgl);
  857. return 0;
  858. }
  859. static int is_datagram_complete(struct list_head *lh, int dg_size)
  860. {
  861. struct partial_datagram *pd;
  862. struct fragment_info *fi;
  863. pd = list_entry(lh, struct partial_datagram, list);
  864. fi = list_entry(pd->frag_info.next, struct fragment_info, list);
  865. return (fi->len == dg_size);
  866. }
  867. /* Packet reception. We convert the IP1394 encapsulation header to an
  868. * ethernet header, and fill it with some of our other fields. This is
  869. * an incoming packet from the 1394 bus. */
  870. static int ether1394_data_handler(struct net_device *dev, int srcid, int destid,
  871. char *buf, int len)
  872. {
  873. struct sk_buff *skb;
  874. unsigned long flags;
  875. struct eth1394_priv *priv = netdev_priv(dev);
  876. union eth1394_hdr *hdr = (union eth1394_hdr *)buf;
  877. __be16 ether_type = cpu_to_be16(0); /* initialized to clear warning */
  878. int hdr_len;
  879. struct unit_directory *ud = priv->ud_list[NODEID_TO_NODE(srcid)];
  880. struct eth1394_node_info *node_info;
  881. if (!ud) {
  882. struct eth1394_node_ref *node;
  883. node = eth1394_find_node_nodeid(&priv->ip_node_list, srcid);
  884. if (unlikely(!node)) {
  885. HPSB_PRINT(KERN_ERR, "ether1394 rx: sender nodeid "
  886. "lookup failure: " NODE_BUS_FMT,
  887. NODE_BUS_ARGS(priv->host, srcid));
  888. dev->stats.rx_dropped++;
  889. return -1;
  890. }
  891. ud = node->ud;
  892. priv->ud_list[NODEID_TO_NODE(srcid)] = ud;
  893. }
  894. node_info = dev_get_drvdata(&ud->device);
  895. /* First, did we receive a fragmented or unfragmented datagram? */
  896. hdr->words.word1 = ntohs(hdr->words.word1);
  897. hdr_len = hdr_type_len[hdr->common.lf];
  898. if (hdr->common.lf == ETH1394_HDR_LF_UF) {
  899. /* An unfragmented datagram has been received by the ieee1394
  900. * bus. Build an skbuff around it so we can pass it to the
  901. * high level network layer. */
  902. skb = dev_alloc_skb(len + dev->hard_header_len + 15);
  903. if (unlikely(!skb)) {
  904. ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
  905. dev->stats.rx_dropped++;
  906. return -1;
  907. }
  908. skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
  909. memcpy(skb_put(skb, len - hdr_len), buf + hdr_len,
  910. len - hdr_len);
  911. ether_type = hdr->uf.ether_type;
  912. } else {
  913. /* A datagram fragment has been received, now the fun begins. */
  914. struct list_head *pdgl, *lh;
  915. struct partial_datagram *pd;
  916. int fg_off;
  917. int fg_len = len - hdr_len;
  918. int dg_size;
  919. int dgl;
  920. int retval;
  921. struct pdg_list *pdg = &(node_info->pdg);
  922. hdr->words.word3 = ntohs(hdr->words.word3);
  923. /* The 4th header word is reserved so no need to do ntohs() */
  924. if (hdr->common.lf == ETH1394_HDR_LF_FF) {
  925. ether_type = hdr->ff.ether_type;
  926. dgl = hdr->ff.dgl;
  927. dg_size = hdr->ff.dg_size + 1;
  928. fg_off = 0;
  929. } else {
  930. hdr->words.word2 = ntohs(hdr->words.word2);
  931. dgl = hdr->sf.dgl;
  932. dg_size = hdr->sf.dg_size + 1;
  933. fg_off = hdr->sf.fg_off;
  934. }
  935. spin_lock_irqsave(&pdg->lock, flags);
  936. pdgl = &(pdg->list);
  937. lh = find_partial_datagram(pdgl, dgl);
  938. if (lh == NULL) {
  939. while (pdg->sz >= max_partial_datagrams) {
  940. /* remove the oldest */
  941. purge_partial_datagram(pdgl->prev);
  942. pdg->sz--;
  943. }
  944. retval = new_partial_datagram(dev, pdgl, dgl, dg_size,
  945. buf + hdr_len, fg_off,
  946. fg_len);
  947. if (retval < 0) {
  948. spin_unlock_irqrestore(&pdg->lock, flags);
  949. goto bad_proto;
  950. }
  951. pdg->sz++;
  952. lh = find_partial_datagram(pdgl, dgl);
  953. } else {
  954. pd = list_entry(lh, struct partial_datagram, list);
  955. if (fragment_overlap(&pd->frag_info, fg_off, fg_len)) {
  956. /* Overlapping fragments, obliterate old
  957. * datagram and start new one. */
  958. purge_partial_datagram(lh);
  959. retval = new_partial_datagram(dev, pdgl, dgl,
  960. dg_size,
  961. buf + hdr_len,
  962. fg_off, fg_len);
  963. if (retval < 0) {
  964. pdg->sz--;
  965. spin_unlock_irqrestore(&pdg->lock, flags);
  966. goto bad_proto;
  967. }
  968. } else {
  969. retval = update_partial_datagram(pdgl, lh,
  970. buf + hdr_len,
  971. fg_off, fg_len);
  972. if (retval < 0) {
  973. /* Couldn't save off fragment anyway
  974. * so might as well obliterate the
  975. * datagram now. */
  976. purge_partial_datagram(lh);
  977. pdg->sz--;
  978. spin_unlock_irqrestore(&pdg->lock, flags);
  979. goto bad_proto;
  980. }
  981. } /* fragment overlap */
  982. } /* new datagram or add to existing one */
  983. pd = list_entry(lh, struct partial_datagram, list);
  984. if (hdr->common.lf == ETH1394_HDR_LF_FF)
  985. pd->ether_type = ether_type;
  986. if (is_datagram_complete(lh, dg_size)) {
  987. ether_type = pd->ether_type;
  988. pdg->sz--;
  989. skb = skb_get(pd->skb);
  990. purge_partial_datagram(lh);
  991. spin_unlock_irqrestore(&pdg->lock, flags);
  992. } else {
  993. /* Datagram is not complete, we're done for the
  994. * moment. */
  995. spin_unlock_irqrestore(&pdg->lock, flags);
  996. return 0;
  997. }
  998. } /* unframgented datagram or fragmented one */
  999. /* Write metadata, and then pass to the receive level */
  1000. skb->dev = dev;
  1001. skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
  1002. /* Parse the encapsulation header. This actually does the job of
  1003. * converting to an ethernet frame header, aswell as arp
  1004. * conversion if needed. ARP conversion is easier in this
  1005. * direction, since we are using ethernet as our backend. */
  1006. skb->protocol = ether1394_parse_encap(skb, dev, srcid, destid,
  1007. ether_type);
  1008. spin_lock_irqsave(&priv->lock, flags);
  1009. if (!skb->protocol) {
  1010. dev->stats.rx_errors++;
  1011. dev->stats.rx_dropped++;
  1012. dev_kfree_skb_any(skb);
  1013. } else if (netif_rx(skb) == NET_RX_DROP) {
  1014. dev->stats.rx_errors++;
  1015. dev->stats.rx_dropped++;
  1016. } else {
  1017. dev->stats.rx_packets++;
  1018. dev->stats.rx_bytes += skb->len;
  1019. }
  1020. spin_unlock_irqrestore(&priv->lock, flags);
  1021. bad_proto:
  1022. if (netif_queue_stopped(dev))
  1023. netif_wake_queue(dev);
  1024. return 0;
  1025. }
  1026. static int ether1394_write(struct hpsb_host *host, int srcid, int destid,
  1027. quadlet_t *data, u64 addr, size_t len, u16 flags)
  1028. {
  1029. struct eth1394_host_info *hi;
  1030. hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
  1031. if (unlikely(!hi)) {
  1032. ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
  1033. host->id);
  1034. return RCODE_ADDRESS_ERROR;
  1035. }
  1036. if (ether1394_data_handler(hi->dev, srcid, destid, (char*)data, len))
  1037. return RCODE_ADDRESS_ERROR;
  1038. else
  1039. return RCODE_COMPLETE;
  1040. }
  1041. static void ether1394_iso(struct hpsb_iso *iso)
  1042. {
  1043. __be32 *data;
  1044. char *buf;
  1045. struct eth1394_host_info *hi;
  1046. struct net_device *dev;
  1047. struct eth1394_priv *priv;
  1048. unsigned int len;
  1049. u32 specifier_id;
  1050. u16 source_id;
  1051. int i;
  1052. int nready;
  1053. hi = hpsb_get_hostinfo(&eth1394_highlevel, iso->host);
  1054. if (unlikely(!hi)) {
  1055. ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
  1056. iso->host->id);
  1057. return;
  1058. }
  1059. dev = hi->dev;
  1060. nready = hpsb_iso_n_ready(iso);
  1061. for (i = 0; i < nready; i++) {
  1062. struct hpsb_iso_packet_info *info =
  1063. &iso->infos[(iso->first_packet + i) % iso->buf_packets];
  1064. data = (__be32 *)(iso->data_buf.kvirt + info->offset);
  1065. /* skip over GASP header */
  1066. buf = (char *)data + 8;
  1067. len = info->len - 8;
  1068. specifier_id = (be32_to_cpu(data[0]) & 0xffff) << 8 |
  1069. (be32_to_cpu(data[1]) & 0xff000000) >> 24;
  1070. source_id = be32_to_cpu(data[0]) >> 16;
  1071. priv = netdev_priv(dev);
  1072. if (info->channel != (iso->host->csr.broadcast_channel & 0x3f)
  1073. || specifier_id != ETHER1394_GASP_SPECIFIER_ID) {
  1074. /* This packet is not for us */
  1075. continue;
  1076. }
  1077. ether1394_data_handler(dev, source_id, LOCAL_BUS | ALL_NODES,
  1078. buf, len);
  1079. }
  1080. hpsb_iso_recv_release_packets(iso, i);
  1081. dev->last_rx = jiffies;
  1082. }
  1083. /******************************************
  1084. * Datagram transmission code
  1085. ******************************************/
  1086. /* Convert a standard ARP packet to 1394 ARP. The first 8 bytes (the entire
  1087. * arphdr) is the same format as the ip1394 header, so they overlap. The rest
  1088. * needs to be munged a bit. The remainder of the arphdr is formatted based
  1089. * on hwaddr len and ipaddr len. We know what they'll be, so it's easy to
  1090. * judge.
  1091. *
  1092. * Now that the EUI is used for the hardware address all we need to do to make
  1093. * this work for 1394 is to insert 2 quadlets that contain max_rec size,
  1094. * speed, and unicast FIFO address information between the sender_unique_id
  1095. * and the IP addresses.
  1096. */
  1097. static void ether1394_arp_to_1394arp(struct sk_buff *skb,
  1098. struct net_device *dev)
  1099. {
  1100. struct eth1394_priv *priv = netdev_priv(dev);
  1101. struct arphdr *arp = (struct arphdr *)skb->data;
  1102. unsigned char *arp_ptr = (unsigned char *)(arp + 1);
  1103. struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
  1104. arp1394->hw_addr_len = 16;
  1105. arp1394->sip = *(u32*)(arp_ptr + ETH1394_ALEN);
  1106. arp1394->max_rec = priv->host->csr.max_rec;
  1107. arp1394->sspd = priv->host->csr.lnk_spd;
  1108. arp1394->fifo_hi = htons(priv->local_fifo >> 32);
  1109. arp1394->fifo_lo = htonl(priv->local_fifo & ~0x0);
  1110. }
  1111. /* We need to encapsulate the standard header with our own. We use the
  1112. * ethernet header's proto for our own. */
  1113. static unsigned int ether1394_encapsulate_prep(unsigned int max_payload,
  1114. __be16 proto,
  1115. union eth1394_hdr *hdr,
  1116. u16 dg_size, u16 dgl)
  1117. {
  1118. unsigned int adj_max_payload =
  1119. max_payload - hdr_type_len[ETH1394_HDR_LF_UF];
  1120. /* Does it all fit in one packet? */
  1121. if (dg_size <= adj_max_payload) {
  1122. hdr->uf.lf = ETH1394_HDR_LF_UF;
  1123. hdr->uf.ether_type = proto;
  1124. } else {
  1125. hdr->ff.lf = ETH1394_HDR_LF_FF;
  1126. hdr->ff.ether_type = proto;
  1127. hdr->ff.dg_size = dg_size - 1;
  1128. hdr->ff.dgl = dgl;
  1129. adj_max_payload = max_payload - hdr_type_len[ETH1394_HDR_LF_FF];
  1130. }
  1131. return DIV_ROUND_UP(dg_size, adj_max_payload);
  1132. }
  1133. static unsigned int ether1394_encapsulate(struct sk_buff *skb,
  1134. unsigned int max_payload,
  1135. union eth1394_hdr *hdr)
  1136. {
  1137. union eth1394_hdr *bufhdr;
  1138. int ftype = hdr->common.lf;
  1139. int hdrsz = hdr_type_len[ftype];
  1140. unsigned int adj_max_payload = max_payload - hdrsz;
  1141. switch (ftype) {
  1142. case ETH1394_HDR_LF_UF:
  1143. bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
  1144. bufhdr->words.word1 = htons(hdr->words.word1);
  1145. bufhdr->words.word2 = hdr->words.word2;
  1146. break;
  1147. case ETH1394_HDR_LF_FF:
  1148. bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
  1149. bufhdr->words.word1 = htons(hdr->words.word1);
  1150. bufhdr->words.word2 = hdr->words.word2;
  1151. bufhdr->words.word3 = htons(hdr->words.word3);
  1152. bufhdr->words.word4 = 0;
  1153. /* Set frag type here for future interior fragments */
  1154. hdr->common.lf = ETH1394_HDR_LF_IF;
  1155. hdr->sf.fg_off = 0;
  1156. break;
  1157. default:
  1158. hdr->sf.fg_off += adj_max_payload;
  1159. bufhdr = (union eth1394_hdr *)skb_pull(skb, adj_max_payload);
  1160. if (max_payload >= skb->len)
  1161. hdr->common.lf = ETH1394_HDR_LF_LF;
  1162. bufhdr->words.word1 = htons(hdr->words.word1);
  1163. bufhdr->words.word2 = htons(hdr->words.word2);
  1164. bufhdr->words.word3 = htons(hdr->words.word3);
  1165. bufhdr->words.word4 = 0;
  1166. }
  1167. return min(max_payload, skb->len);
  1168. }
  1169. static struct hpsb_packet *ether1394_alloc_common_packet(struct hpsb_host *host)
  1170. {
  1171. struct hpsb_packet *p;
  1172. p = hpsb_alloc_packet(0);
  1173. if (p) {
  1174. p->host = host;
  1175. p->generation = get_hpsb_generation(host);
  1176. p->type = hpsb_async;
  1177. }
  1178. return p;
  1179. }
  1180. static int ether1394_prep_write_packet(struct hpsb_packet *p,
  1181. struct hpsb_host *host, nodeid_t node,
  1182. u64 addr, void *data, int tx_len)
  1183. {
  1184. p->node_id = node;
  1185. if (hpsb_get_tlabel(p))
  1186. return -EAGAIN;
  1187. p->tcode = TCODE_WRITEB;
  1188. p->header_size = 16;
  1189. p->expect_response = 1;
  1190. p->header[0] =
  1191. p->node_id << 16 | p->tlabel << 10 | 1 << 8 | TCODE_WRITEB << 4;
  1192. p->header[1] = host->node_id << 16 | addr >> 32;
  1193. p->header[2] = addr & 0xffffffff;
  1194. p->header[3] = tx_len << 16;
  1195. p->data_size = (tx_len + 3) & ~3;
  1196. p->data = data;
  1197. return 0;
  1198. }
  1199. static void ether1394_prep_gasp_packet(struct hpsb_packet *p,
  1200. struct eth1394_priv *priv,
  1201. struct sk_buff *skb, int length)
  1202. {
  1203. p->header_size = 4;
  1204. p->tcode = TCODE_STREAM_DATA;
  1205. p->header[0] = length << 16 | 3 << 14 | priv->broadcast_channel << 8 |
  1206. TCODE_STREAM_DATA << 4;
  1207. p->data_size = length;
  1208. p->data = (quadlet_t *)skb->data - 2;
  1209. p->data[0] = cpu_to_be32(priv->host->node_id << 16 |
  1210. ETHER1394_GASP_SPECIFIER_ID_HI);
  1211. p->data[1] = cpu_to_be32(ETHER1394_GASP_SPECIFIER_ID_LO << 24 |
  1212. ETHER1394_GASP_VERSION);
  1213. p->speed_code = priv->bc_sspd;
  1214. /* prevent hpsb_send_packet() from overriding our speed code */
  1215. p->node_id = LOCAL_BUS | ALL_NODES;
  1216. }
  1217. static void ether1394_free_packet(struct hpsb_packet *packet)
  1218. {
  1219. if (packet->tcode != TCODE_STREAM_DATA)
  1220. hpsb_free_tlabel(packet);
  1221. hpsb_free_packet(packet);
  1222. }
  1223. static void ether1394_complete_cb(void *__ptask);
  1224. static int ether1394_send_packet(struct packet_task *ptask, unsigned int tx_len)
  1225. {
  1226. struct eth1394_priv *priv = ptask->priv;
  1227. struct hpsb_packet *packet = NULL;
  1228. packet = ether1394_alloc_common_packet(priv->host);
  1229. if (!packet)
  1230. return -ENOMEM;
  1231. if (ptask->tx_type == ETH1394_GASP) {
  1232. int length = tx_len + 2 * sizeof(quadlet_t);
  1233. ether1394_prep_gasp_packet(packet, priv, ptask->skb, length);
  1234. } else if (ether1394_prep_write_packet(packet, priv->host,
  1235. ptask->dest_node,
  1236. ptask->addr, ptask->skb->data,
  1237. tx_len)) {
  1238. hpsb_free_packet(packet);
  1239. return -EAGAIN;
  1240. }
  1241. ptask->packet = packet;
  1242. hpsb_set_packet_complete_task(ptask->packet, ether1394_complete_cb,
  1243. ptask);
  1244. if (hpsb_send_packet(packet) < 0) {
  1245. ether1394_free_packet(packet);
  1246. return -EIO;
  1247. }
  1248. return 0;
  1249. }
  1250. /* Task function to be run when a datagram transmission is completed */
  1251. static void ether1394_dg_complete(struct packet_task *ptask, int fail)
  1252. {
  1253. struct sk_buff *skb = ptask->skb;
  1254. struct net_device *dev = skb->dev;
  1255. struct eth1394_priv *priv = netdev_priv(dev);
  1256. unsigned long flags;
  1257. /* Statistics */
  1258. spin_lock_irqsave(&priv->lock, flags);
  1259. if (fail) {
  1260. dev->stats.tx_dropped++;
  1261. dev->stats.tx_errors++;
  1262. } else {
  1263. dev->stats.tx_bytes += skb->len;
  1264. dev->stats.tx_packets++;
  1265. }
  1266. spin_unlock_irqrestore(&priv->lock, flags);
  1267. dev_kfree_skb_any(skb);
  1268. kmem_cache_free(packet_task_cache, ptask);
  1269. }
  1270. /* Callback for when a packet has been sent and the status of that packet is
  1271. * known */
  1272. static void ether1394_complete_cb(void *__ptask)
  1273. {
  1274. struct packet_task *ptask = (struct packet_task *)__ptask;
  1275. struct hpsb_packet *packet = ptask->packet;
  1276. int fail = 0;
  1277. if (packet->tcode != TCODE_STREAM_DATA)
  1278. fail = hpsb_packet_success(packet);
  1279. ether1394_free_packet(packet);
  1280. ptask->outstanding_pkts--;
  1281. if (ptask->outstanding_pkts > 0 && !fail) {
  1282. int tx_len, err;
  1283. /* Add the encapsulation header to the fragment */
  1284. tx_len = ether1394_encapsulate(ptask->skb, ptask->max_payload,
  1285. &ptask->hdr);
  1286. err = ether1394_send_packet(ptask, tx_len);
  1287. if (err) {
  1288. if (err == -EAGAIN)
  1289. ETH1394_PRINT_G(KERN_ERR, "Out of tlabels\n");
  1290. ether1394_dg_complete(ptask, 1);
  1291. }
  1292. } else {
  1293. ether1394_dg_complete(ptask, fail);
  1294. }
  1295. }
  1296. /* Transmit a packet (called by kernel) */
  1297. static netdev_tx_t ether1394_tx(struct sk_buff *skb,
  1298. struct net_device *dev)
  1299. {
  1300. struct eth1394hdr hdr_buf;
  1301. struct eth1394_priv *priv = netdev_priv(dev);
  1302. __be16 proto;
  1303. unsigned long flags;
  1304. nodeid_t dest_node;
  1305. eth1394_tx_type tx_type;
  1306. unsigned int tx_len;
  1307. unsigned int max_payload;
  1308. u16 dg_size;
  1309. u16 dgl;
  1310. struct packet_task *ptask;
  1311. struct eth1394_node_ref *node;
  1312. struct eth1394_node_info *node_info = NULL;
  1313. ptask = kmem_cache_alloc(packet_task_cache, GFP_ATOMIC);
  1314. if (ptask == NULL)
  1315. goto fail;
  1316. /* XXX Ignore this for now. Noticed that when MacOSX is the IRM,
  1317. * it does not set our validity bit. We need to compensate for
  1318. * that somewhere else, but not in eth1394. */
  1319. #if 0
  1320. if ((priv->host->csr.broadcast_channel & 0xc0000000) != 0xc0000000)
  1321. goto fail;
  1322. #endif
  1323. skb = skb_share_check(skb, GFP_ATOMIC);
  1324. if (!skb)
  1325. goto fail;
  1326. /* Get rid of the fake eth1394 header, but first make a copy.
  1327. * We might need to rebuild the header on tx failure. */
  1328. memcpy(&hdr_buf, skb->data, sizeof(hdr_buf));
  1329. skb_pull(skb, ETH1394_HLEN);
  1330. proto = hdr_buf.h_proto;
  1331. dg_size = skb->len;
  1332. /* Set the transmission type for the packet. ARP packets and IP
  1333. * broadcast packets are sent via GASP. */
  1334. if (memcmp(hdr_buf.h_dest, dev->broadcast, ETH1394_ALEN) == 0 ||
  1335. proto == htons(ETH_P_ARP) ||
  1336. (proto == htons(ETH_P_IP) &&
  1337. IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) {
  1338. tx_type = ETH1394_GASP;
  1339. dest_node = LOCAL_BUS | ALL_NODES;
  1340. max_payload = priv->bc_maxpayload - ETHER1394_GASP_OVERHEAD;
  1341. BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
  1342. dgl = priv->bc_dgl;
  1343. if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
  1344. priv->bc_dgl++;
  1345. } else {
  1346. __be64 guid = get_unaligned((__be64 *)hdr_buf.h_dest);
  1347. node = eth1394_find_node_guid(&priv->ip_node_list,
  1348. be64_to_cpu(guid));
  1349. if (!node)
  1350. goto fail;
  1351. node_info = dev_get_drvdata(&node->ud->device);
  1352. if (node_info->fifo == CSR1212_INVALID_ADDR_SPACE)
  1353. goto fail;
  1354. dest_node = node->ud->ne->nodeid;
  1355. max_payload = node_info->maxpayload;
  1356. BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
  1357. dgl = node_info->dgl;
  1358. if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
  1359. node_info->dgl++;
  1360. tx_type = ETH1394_WRREQ;
  1361. }
  1362. /* If this is an ARP packet, convert it */
  1363. if (proto == htons(ETH_P_ARP))
  1364. ether1394_arp_to_1394arp(skb, dev);
  1365. ptask->hdr.words.word1 = 0;
  1366. ptask->hdr.words.word2 = 0;
  1367. ptask->hdr.words.word3 = 0;
  1368. ptask->hdr.words.word4 = 0;
  1369. ptask->skb = skb;
  1370. ptask->priv = priv;
  1371. ptask->tx_type = tx_type;
  1372. if (tx_type != ETH1394_GASP) {
  1373. u64 addr;
  1374. spin_lock_irqsave(&priv->lock, flags);
  1375. addr = node_info->fifo;
  1376. spin_unlock_irqrestore(&priv->lock, flags);
  1377. ptask->addr = addr;
  1378. ptask->dest_node = dest_node;
  1379. }
  1380. ptask->tx_type = tx_type;
  1381. ptask->max_payload = max_payload;
  1382. ptask->outstanding_pkts = ether1394_encapsulate_prep(max_payload,
  1383. proto, &ptask->hdr, dg_size, dgl);
  1384. /* Add the encapsulation header to the fragment */
  1385. tx_len = ether1394_encapsulate(skb, max_payload, &ptask->hdr);
  1386. dev->trans_start = jiffies;
  1387. if (ether1394_send_packet(ptask, tx_len)) {
  1388. if (dest_node == (LOCAL_BUS | ALL_NODES))
  1389. goto fail;
  1390. /* At this point we want to restore the packet. When we return
  1391. * here with NETDEV_TX_BUSY we will get another entrance in this
  1392. * routine with the same skb and we need it to look the same.
  1393. * So we pull 4 more bytes, then build the header again. */
  1394. skb_pull(skb, 4);
  1395. ether1394_header(skb, dev, ntohs(hdr_buf.h_proto),
  1396. hdr_buf.h_dest, NULL, 0);
  1397. /* Most failures of ether1394_send_packet are recoverable. */
  1398. netif_stop_queue(dev);
  1399. priv->wake_node = dest_node;
  1400. schedule_work(&priv->wake);
  1401. kmem_cache_free(packet_task_cache, ptask);
  1402. return NETDEV_TX_BUSY;
  1403. }
  1404. return NETDEV_TX_OK;
  1405. fail:
  1406. if (ptask)
  1407. kmem_cache_free(packet_task_cache, ptask);
  1408. if (skb != NULL)
  1409. dev_kfree_skb(skb);
  1410. spin_lock_irqsave(&priv->lock, flags);
  1411. dev->stats.tx_dropped++;
  1412. dev->stats.tx_errors++;
  1413. spin_unlock_irqrestore(&priv->lock, flags);
  1414. return NETDEV_TX_OK;
  1415. }
  1416. static void ether1394_get_drvinfo(struct net_device *dev,
  1417. struct ethtool_drvinfo *info)
  1418. {
  1419. strcpy(info->driver, driver_name);
  1420. strcpy(info->bus_info, "ieee1394"); /* FIXME provide more detail? */
  1421. }
  1422. static const struct ethtool_ops ethtool_ops = {
  1423. .get_drvinfo = ether1394_get_drvinfo
  1424. };
  1425. static int __init ether1394_init_module(void)
  1426. {
  1427. int err;
  1428. packet_task_cache = kmem_cache_create("packet_task",
  1429. sizeof(struct packet_task),
  1430. 0, 0, NULL);
  1431. if (!packet_task_cache)
  1432. return -ENOMEM;
  1433. hpsb_register_highlevel(&eth1394_highlevel);
  1434. err = hpsb_register_protocol(&eth1394_proto_driver);
  1435. if (err) {
  1436. hpsb_unregister_highlevel(&eth1394_highlevel);
  1437. kmem_cache_destroy(packet_task_cache);
  1438. }
  1439. return err;
  1440. }
  1441. static void __exit ether1394_exit_module(void)
  1442. {
  1443. hpsb_unregister_protocol(&eth1394_proto_driver);
  1444. hpsb_unregister_highlevel(&eth1394_highlevel);
  1445. kmem_cache_destroy(packet_task_cache);
  1446. }
  1447. module_init(ether1394_init_module);
  1448. module_exit(ether1394_exit_module);