eth1394.c 48 KB

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