eth1394.c 46 KB

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