eth1394.c 47 KB

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