eth1394.c 46 KB

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