eth1394.c 45 KB

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