eth1394.c 48 KB

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