eth1394.c 48 KB

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