eth1394.c 46 KB

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