eth1394.c 45 KB

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