eth1394.c 46 KB

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