eth1394.c 49 KB

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