eth1394.c 48 KB

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