eth1394.c 48 KB

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