eth1394.c 46 KB

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