network.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. *
  3. * arch/xtensa/platform-iss/network.c
  4. *
  5. * Platform specific initialization.
  6. *
  7. * Authors: Chris Zankel <chris@zankel.net>
  8. * Based on work form the UML team.
  9. *
  10. * Copyright 2005 Tensilica Inc.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. */
  18. #include <linux/config.h>
  19. #include <linux/list.h>
  20. #include <linux/irq.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/slab.h>
  23. #include <linux/timer.h>
  24. #include <linux/if_ether.h>
  25. #include <linux/inetdevice.h>
  26. #include <linux/init.h>
  27. #include <linux/if_tun.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/ioctl.h>
  31. #include <linux/bootmem.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/rtnetlink.h>
  34. #include <linux/timer.h>
  35. #include <linux/platform_device.h>
  36. #include <xtensa/simcall.h>
  37. #define DRIVER_NAME "iss-netdev"
  38. #define ETH_MAX_PACKET 1500
  39. #define ETH_HEADER_OTHER 14
  40. #define ISS_NET_TIMER_VALUE (2 * HZ)
  41. static DEFINE_SPINLOCK(opened_lock);
  42. static LIST_HEAD(opened);
  43. static DEFINE_SPINLOCK(devices_lock);
  44. static LIST_HEAD(devices);
  45. /* ------------------------------------------------------------------------- */
  46. /* We currently only support the TUNTAP transport protocol. */
  47. #define TRANSPORT_TUNTAP_NAME "tuntap"
  48. #define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
  49. struct tuntap_info {
  50. char dev_name[IFNAMSIZ];
  51. int fixed_config;
  52. unsigned char gw[ETH_ALEN];
  53. int fd;
  54. };
  55. /* ------------------------------------------------------------------------- */
  56. /* This structure contains out private information for the driver. */
  57. struct iss_net_private {
  58. struct list_head device_list;
  59. struct list_head opened_list;
  60. spinlock_t lock;
  61. struct net_device *dev;
  62. struct platform_device pdev;
  63. struct timer_list tl;
  64. struct net_device_stats stats;
  65. struct timer_list timer;
  66. unsigned int timer_val;
  67. int index;
  68. int mtu;
  69. unsigned char mac[ETH_ALEN];
  70. int have_mac;
  71. struct {
  72. union {
  73. struct tuntap_info tuntap;
  74. } info;
  75. int (*open)(struct iss_net_private *lp);
  76. void (*close)(struct iss_net_private *lp);
  77. int (*read)(struct iss_net_private *lp, struct sk_buff **skb);
  78. int (*write)(struct iss_net_private *lp, struct sk_buff **skb);
  79. unsigned short (*protocol)(struct sk_buff *skb);
  80. int (*poll)(struct iss_net_private *lp);
  81. } tp;
  82. };
  83. /* ======================= ISS SIMCALL INTERFACE =========================== */
  84. /* Note: __simc must _not_ be declared inline! */
  85. static int errno;
  86. static int __simc (int a, int b, int c, int d, int e, int f)
  87. {
  88. int ret;
  89. __asm__ __volatile__ ("simcall\n"
  90. "mov %0, a2\n"
  91. "mov %1, a3\n" : "=a" (ret), "=a" (errno)
  92. : : "a2", "a3");
  93. return ret;
  94. }
  95. static int inline simc_open(char *file, int flags, int mode)
  96. {
  97. return __simc(SYS_open, (int) file, flags, mode, 0, 0);
  98. }
  99. static int inline simc_close(int fd)
  100. {
  101. return __simc(SYS_close, fd, 0, 0, 0, 0);
  102. }
  103. static int inline simc_ioctl(int fd, int request, void *arg)
  104. {
  105. return __simc(SYS_ioctl, fd, request, (int) arg, 0, 0);
  106. }
  107. static int inline simc_read(int fd, void *buf, size_t count)
  108. {
  109. return __simc(SYS_read, fd, (int) buf, count, 0, 0);
  110. }
  111. static int inline simc_write(int fd, void *buf, size_t count)
  112. {
  113. return __simc(SYS_write, fd, (int) buf, count, 0, 0);
  114. }
  115. static int inline simc_poll(int fd)
  116. {
  117. struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
  118. return __simc(SYS_select_one, fd, XTISS_SELECT_ONE_READ, (int)&tv,0,0);
  119. }
  120. /* ================================ HELPERS ================================ */
  121. static char *split_if_spec(char *str, ...)
  122. {
  123. char **arg, *end;
  124. va_list ap;
  125. va_start(ap, str);
  126. while ((arg = va_arg(ap, char**)) != NULL) {
  127. if (*str == '\0')
  128. return NULL;
  129. end = strchr(str, ',');
  130. if (end != str)
  131. *arg = str;
  132. if (end == NULL)
  133. return NULL;
  134. *end ++ = '\0';
  135. str = end;
  136. }
  137. va_end(ap);
  138. return str;
  139. }
  140. #if 0
  141. /* Adjust SKB. */
  142. struct sk_buff *ether_adjust_skb(struct sk_buff *skb, int extra)
  143. {
  144. if ((skb != NULL) && (skb_tailroom(skb) < extra)) {
  145. struct sk_buff *skb2;
  146. skb2 = skb_copy_expand(skb, 0, extra, GFP_ATOMIC);
  147. dev_kfree_skb(skb);
  148. skb = skb2;
  149. }
  150. if (skb != NULL)
  151. skb_put(skb, extra);
  152. return skb;
  153. }
  154. #endif
  155. /* Return the IP address as a string for a given device. */
  156. static void dev_ip_addr(void *d, char *buf, char *bin_buf)
  157. {
  158. struct net_device *dev = d;
  159. struct in_device *ip = dev->ip_ptr;
  160. struct in_ifaddr *in;
  161. u32 addr;
  162. if ((ip == NULL) || ((in = ip->ifa_list) == NULL)) {
  163. printk(KERN_WARNING "Device not assigned an IP address!\n");
  164. return;
  165. }
  166. addr = in->ifa_address;
  167. sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
  168. (addr >> 16) & 0xff, addr >> 24);
  169. if (bin_buf) {
  170. bin_buf[0] = addr & 0xff;
  171. bin_buf[1] = (addr >> 8) & 0xff;
  172. bin_buf[2] = (addr >> 16) & 0xff;
  173. bin_buf[3] = addr >> 24;
  174. }
  175. }
  176. /* Set Ethernet address of the specified device. */
  177. static void inline set_ether_mac(void *d, unsigned char *addr)
  178. {
  179. struct net_device *dev = d;
  180. memcpy(dev->dev_addr, addr, ETH_ALEN);
  181. }
  182. /* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
  183. static int tuntap_open(struct iss_net_private *lp)
  184. {
  185. struct ifreq ifr;
  186. char *dev_name = lp->tp.info.tuntap.dev_name;
  187. int err = -EINVAL;
  188. int fd;
  189. /* We currently only support a fixed configuration. */
  190. if (!lp->tp.info.tuntap.fixed_config)
  191. return -EINVAL;
  192. if ((fd = simc_open("/dev/net/tun", 02, 0)) < 0) { /* O_RDWR */
  193. printk("Failed to open /dev/net/tun, returned %d "
  194. "(errno = %d)\n", fd, errno);
  195. return fd;
  196. }
  197. memset(&ifr, 0, sizeof ifr);
  198. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  199. strlcpy(ifr.ifr_name, dev_name, sizeof ifr.ifr_name - 1);
  200. if ((err = simc_ioctl(fd, TUNSETIFF, (void*) &ifr)) < 0) {
  201. printk("Failed to set interface, returned %d "
  202. "(errno = %d)\n", err, errno);
  203. simc_close(fd);
  204. return err;
  205. }
  206. lp->tp.info.tuntap.fd = fd;
  207. return err;
  208. }
  209. static void tuntap_close(struct iss_net_private *lp)
  210. {
  211. #if 0
  212. if (lp->tp.info.tuntap.fixed_config)
  213. iter_addresses(lp->tp.info.tuntap.dev, close_addr, lp->host.dev_name);
  214. #endif
  215. simc_close(lp->tp.info.tuntap.fd);
  216. lp->tp.info.tuntap.fd = -1;
  217. }
  218. static int tuntap_read (struct iss_net_private *lp, struct sk_buff **skb)
  219. {
  220. #if 0
  221. *skb = ether_adjust_skb(*skb, ETH_HEADER_OTHER);
  222. if (*skb == NULL)
  223. return -ENOMEM;
  224. #endif
  225. return simc_read(lp->tp.info.tuntap.fd,
  226. (*skb)->data, (*skb)->dev->mtu + ETH_HEADER_OTHER);
  227. }
  228. static int tuntap_write (struct iss_net_private *lp, struct sk_buff **skb)
  229. {
  230. return simc_write(lp->tp.info.tuntap.fd, (*skb)->data, (*skb)->len);
  231. }
  232. unsigned short tuntap_protocol(struct sk_buff *skb)
  233. {
  234. return eth_type_trans(skb, skb->dev);
  235. }
  236. static int tuntap_poll(struct iss_net_private *lp)
  237. {
  238. return simc_poll(lp->tp.info.tuntap.fd);
  239. }
  240. /*
  241. * Currently only a device name is supported.
  242. * ethX=tuntap[,[mac address][,[device name]]]
  243. */
  244. static int tuntap_probe(struct iss_net_private *lp, int index, char *init)
  245. {
  246. const int len = strlen(TRANSPORT_TUNTAP_NAME);
  247. char *dev_name = NULL, *mac_str = NULL, *rem = NULL;
  248. /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
  249. if (strncmp(init, TRANSPORT_TUNTAP_NAME, len))
  250. return 0;
  251. if (*(init += strlen(TRANSPORT_TUNTAP_NAME)) == ',') {
  252. if ((rem=split_if_spec(init+1, &mac_str, &dev_name)) != NULL) {
  253. printk("Extra garbage on specification : '%s'\n", rem);
  254. return 0;
  255. }
  256. } else if (*init != '\0') {
  257. printk("Invalid argument: %s. Skipping device!\n", init);
  258. return 0;
  259. }
  260. if (dev_name) {
  261. strncpy(lp->tp.info.tuntap.dev_name, dev_name,
  262. sizeof lp->tp.info.tuntap.dev_name);
  263. lp->tp.info.tuntap.fixed_config = 1;
  264. } else
  265. strcpy(lp->tp.info.tuntap.dev_name, TRANSPORT_TUNTAP_NAME);
  266. #if 0
  267. if (setup_etheraddr(mac_str, lp->mac))
  268. lp->have_mac = 1;
  269. #endif
  270. lp->mtu = TRANSPORT_TUNTAP_MTU;
  271. //lp->info.tuntap.gate_addr = gate_addr;
  272. lp->tp.info.tuntap.fd = -1;
  273. lp->tp.open = tuntap_open;
  274. lp->tp.close = tuntap_close;
  275. lp->tp.read = tuntap_read;
  276. lp->tp.write = tuntap_write;
  277. lp->tp.protocol = tuntap_protocol;
  278. lp->tp.poll = tuntap_poll;
  279. printk("TUN/TAP backend - ");
  280. #if 0
  281. if (lp->host.gate_addr != NULL)
  282. printk("IP = %s", lp->host.gate_addr);
  283. #endif
  284. printk("\n");
  285. return 1;
  286. }
  287. /* ================================ ISS NET ================================ */
  288. static int iss_net_rx(struct net_device *dev)
  289. {
  290. struct iss_net_private *lp = dev->priv;
  291. int pkt_len;
  292. struct sk_buff *skb;
  293. /* Check if there is any new data. */
  294. if (lp->tp.poll(lp) == 0)
  295. return 0;
  296. /* Try to allocate memory, if it fails, try again next round. */
  297. if ((skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER)) == NULL) {
  298. lp->stats.rx_dropped++;
  299. return 0;
  300. }
  301. skb_reserve(skb, 2);
  302. /* Setup skb */
  303. skb->dev = dev;
  304. skb->mac.raw = skb->data;
  305. pkt_len = lp->tp.read(lp, &skb);
  306. skb_put(skb, pkt_len);
  307. if (pkt_len > 0) {
  308. skb_trim(skb, pkt_len);
  309. skb->protocol = lp->tp.protocol(skb);
  310. // netif_rx(skb);
  311. netif_rx_ni(skb);
  312. lp->stats.rx_bytes += skb->len;
  313. lp->stats.rx_packets++;
  314. return pkt_len;
  315. }
  316. kfree_skb(skb);
  317. return pkt_len;
  318. }
  319. static int iss_net_poll(void)
  320. {
  321. struct list_head *ele;
  322. int err, ret = 0;
  323. spin_lock(&opened_lock);
  324. list_for_each(ele, &opened) {
  325. struct iss_net_private *lp;
  326. lp = list_entry(ele, struct iss_net_private, opened_list);
  327. if (!netif_running(lp->dev))
  328. break;
  329. spin_lock(&lp->lock);
  330. while ((err = iss_net_rx(lp->dev)) > 0)
  331. ret++;
  332. spin_unlock(&lp->lock);
  333. if (err < 0) {
  334. printk(KERN_ERR "Device '%s' read returned %d, "
  335. "shutting it down\n", lp->dev->name, err);
  336. dev_close(lp->dev);
  337. } else {
  338. // FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ);
  339. }
  340. }
  341. spin_unlock(&opened_lock);
  342. return ret;
  343. }
  344. static void iss_net_timer(unsigned long priv)
  345. {
  346. struct iss_net_private* lp = (struct iss_net_private*) priv;
  347. spin_lock(&lp->lock);
  348. iss_net_poll();
  349. mod_timer(&lp->timer, jiffies + lp->timer_val);
  350. spin_unlock(&lp->lock);
  351. }
  352. static int iss_net_open(struct net_device *dev)
  353. {
  354. struct iss_net_private *lp = dev->priv;
  355. char addr[sizeof "255.255.255.255\0"];
  356. int err;
  357. spin_lock(&lp->lock);
  358. if ((err = lp->tp.open(lp)) < 0)
  359. goto out;
  360. if (!lp->have_mac) {
  361. dev_ip_addr(dev, addr, &lp->mac[2]);
  362. set_ether_mac(dev, lp->mac);
  363. }
  364. netif_start_queue(dev);
  365. /* clear buffer - it can happen that the host side of the interface
  366. * is full when we gethere. In this case, new data is never queued,
  367. * SIGIOs never arrive, and the net never works.
  368. */
  369. while ((err = iss_net_rx(dev)) > 0)
  370. ;
  371. spin_lock(&opened_lock);
  372. list_add(&lp->opened_list, &opened);
  373. spin_unlock(&opened_lock);
  374. init_timer(&lp->timer);
  375. lp->timer_val = ISS_NET_TIMER_VALUE;
  376. lp->timer.data = (unsigned long) lp;
  377. lp->timer.function = iss_net_timer;
  378. mod_timer(&lp->timer, jiffies + lp->timer_val);
  379. out:
  380. spin_unlock(&lp->lock);
  381. return err;
  382. }
  383. static int iss_net_close(struct net_device *dev)
  384. {
  385. struct iss_net_private *lp = dev->priv;
  386. printk("iss_net_close!\n");
  387. netif_stop_queue(dev);
  388. spin_lock(&lp->lock);
  389. spin_lock(&opened_lock);
  390. list_del(&opened);
  391. spin_unlock(&opened_lock);
  392. del_timer_sync(&lp->timer);
  393. lp->tp.close(lp);
  394. spin_unlock(&lp->lock);
  395. return 0;
  396. }
  397. static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  398. {
  399. struct iss_net_private *lp = dev->priv;
  400. unsigned long flags;
  401. int len;
  402. netif_stop_queue(dev);
  403. spin_lock_irqsave(&lp->lock, flags);
  404. len = lp->tp.write(lp, &skb);
  405. if (len == skb->len) {
  406. lp->stats.tx_packets++;
  407. lp->stats.tx_bytes += skb->len;
  408. dev->trans_start = jiffies;
  409. netif_start_queue(dev);
  410. /* this is normally done in the interrupt when tx finishes */
  411. netif_wake_queue(dev);
  412. } else if (len == 0) {
  413. netif_start_queue(dev);
  414. lp->stats.tx_dropped++;
  415. } else {
  416. netif_start_queue(dev);
  417. printk(KERN_ERR "iss_net_start_xmit: failed(%d)\n", len);
  418. }
  419. spin_unlock_irqrestore(&lp->lock, flags);
  420. dev_kfree_skb(skb);
  421. return 0;
  422. }
  423. static struct net_device_stats *iss_net_get_stats(struct net_device *dev)
  424. {
  425. struct iss_net_private *lp = dev->priv;
  426. return &lp->stats;
  427. }
  428. static void iss_net_set_multicast_list(struct net_device *dev)
  429. {
  430. #if 0
  431. if (dev->flags & IFF_PROMISC)
  432. return;
  433. else if (dev->mc_count)
  434. dev->flags |= IFF_ALLMULTI;
  435. else
  436. dev->flags &= ~IFF_ALLMULTI;
  437. #endif
  438. }
  439. static void iss_net_tx_timeout(struct net_device *dev)
  440. {
  441. #if 0
  442. dev->trans_start = jiffies;
  443. netif_wake_queue(dev);
  444. #endif
  445. }
  446. static int iss_net_set_mac(struct net_device *dev, void *addr)
  447. {
  448. #if 0
  449. struct iss_net_private *lp = dev->priv;
  450. struct sockaddr *hwaddr = addr;
  451. spin_lock(&lp->lock);
  452. memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
  453. spin_unlock(&lp->lock);
  454. #endif
  455. return 0;
  456. }
  457. static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
  458. {
  459. #if 0
  460. struct iss_net_private *lp = dev->priv;
  461. int err = 0;
  462. spin_lock(&lp->lock);
  463. // FIXME not needed new_mtu = transport_set_mtu(new_mtu, &lp->user);
  464. if (new_mtu < 0)
  465. err = new_mtu;
  466. else
  467. dev->mtu = new_mtu;
  468. spin_unlock(&lp->lock);
  469. return err;
  470. #endif
  471. return -EINVAL;
  472. }
  473. void iss_net_user_timer_expire(unsigned long _conn)
  474. {
  475. }
  476. static struct platform_driver iss_net_driver = {
  477. .driver = {
  478. .name = DRIVER_NAME,
  479. },
  480. };
  481. static int driver_registered;
  482. static int iss_net_configure(int index, char *init)
  483. {
  484. struct net_device *dev;
  485. struct iss_net_private *lp;
  486. int err;
  487. if ((dev = alloc_etherdev(sizeof *lp)) == NULL) {
  488. printk(KERN_ERR "eth_configure: failed to allocate device\n");
  489. return 1;
  490. }
  491. /* Initialize private element. */
  492. lp = dev->priv;
  493. *lp = ((struct iss_net_private) {
  494. .device_list = LIST_HEAD_INIT(lp->device_list),
  495. .opened_list = LIST_HEAD_INIT(lp->opened_list),
  496. .lock = SPIN_LOCK_UNLOCKED,
  497. .dev = dev,
  498. .index = index,
  499. //.fd = -1,
  500. .mac = { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 },
  501. .have_mac = 0,
  502. });
  503. /*
  504. * Try all transport protocols.
  505. * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
  506. */
  507. if (!tuntap_probe(lp, index, init)) {
  508. printk("Invalid arguments. Skipping device!\n");
  509. goto errout;
  510. }
  511. printk(KERN_INFO "Netdevice %d ", index);
  512. if (lp->have_mac)
  513. printk("(%02x:%02x:%02x:%02x:%02x:%02x) ",
  514. lp->mac[0], lp->mac[1],
  515. lp->mac[2], lp->mac[3],
  516. lp->mac[4], lp->mac[5]);
  517. printk(": ");
  518. /* sysfs register */
  519. if (!driver_registered) {
  520. platform_driver_register(&iss_net_driver);
  521. driver_registered = 1;
  522. }
  523. spin_lock(&devices_lock);
  524. list_add(&lp->device_list, &devices);
  525. spin_unlock(&devices_lock);
  526. lp->pdev.id = index;
  527. lp->pdev.name = DRIVER_NAME;
  528. platform_device_register(&lp->pdev);
  529. SET_NETDEV_DEV(dev,&lp->pdev.dev);
  530. /*
  531. * If this name ends up conflicting with an existing registered
  532. * netdevice, that is OK, register_netdev{,ice}() will notice this
  533. * and fail.
  534. */
  535. snprintf(dev->name, sizeof dev->name, "eth%d", index);
  536. dev->mtu = lp->mtu;
  537. dev->open = iss_net_open;
  538. dev->hard_start_xmit = iss_net_start_xmit;
  539. dev->stop = iss_net_close;
  540. dev->get_stats = iss_net_get_stats;
  541. dev->set_multicast_list = iss_net_set_multicast_list;
  542. dev->tx_timeout = iss_net_tx_timeout;
  543. dev->set_mac_address = iss_net_set_mac;
  544. dev->change_mtu = iss_net_change_mtu;
  545. dev->watchdog_timeo = (HZ >> 1);
  546. dev->irq = -1;
  547. rtnl_lock();
  548. err = register_netdevice(dev);
  549. rtnl_unlock();
  550. if (err) {
  551. printk("Error registering net device!\n");
  552. /* XXX: should we call ->remove() here? */
  553. free_netdev(dev);
  554. return 1;
  555. }
  556. init_timer(&lp->tl);
  557. lp->tl.function = iss_net_user_timer_expire;
  558. #if 0
  559. if (lp->have_mac)
  560. set_ether_mac(dev, lp->mac);
  561. #endif
  562. return 0;
  563. errout:
  564. // FIXME: unregister; free, etc..
  565. return -EIO;
  566. }
  567. /* ------------------------------------------------------------------------- */
  568. /* Filled in during early boot */
  569. struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
  570. struct iss_net_init {
  571. struct list_head list;
  572. char *init; /* init string */
  573. int index;
  574. };
  575. /*
  576. * Parse the command line and look for 'ethX=...' fields, and register all
  577. * those fields. They will be later initialized in iss_net_init.
  578. */
  579. #define ERR KERN_ERR "iss_net_setup: "
  580. static int iss_net_setup(char *str)
  581. {
  582. struct iss_net_private *device = NULL;
  583. struct iss_net_init *new;
  584. struct list_head *ele;
  585. char *end;
  586. int n;
  587. n = simple_strtoul(str, &end, 0);
  588. if (end == str) {
  589. printk(ERR "Failed to parse '%s'\n", str);
  590. return 1;
  591. }
  592. if (n < 0) {
  593. printk(ERR "Device %d is negative\n", n);
  594. return 1;
  595. }
  596. if (*(str = end) != '=') {
  597. printk(ERR "Expected '=' after device number\n");
  598. return 1;
  599. }
  600. spin_lock(&devices_lock);
  601. list_for_each(ele, &devices) {
  602. device = list_entry(ele, struct iss_net_private, device_list);
  603. if (device->index == n)
  604. break;
  605. }
  606. spin_unlock(&devices_lock);
  607. if (device && device->index == n) {
  608. printk(ERR "Device %d already configured\n", n);
  609. return 1;
  610. }
  611. if ((new = alloc_bootmem(sizeof new)) == NULL) {
  612. printk("Alloc_bootmem failed\n");
  613. return 1;
  614. }
  615. INIT_LIST_HEAD(&new->list);
  616. new->index = n;
  617. new->init = str + 1;
  618. list_add_tail(&new->list, &eth_cmd_line);
  619. return 1;
  620. }
  621. #undef ERR
  622. __setup("eth", iss_net_setup);
  623. /*
  624. * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
  625. */
  626. static int iss_net_init(void)
  627. {
  628. struct list_head *ele, *next;
  629. /* Walk through all Ethernet devices specified in the command line. */
  630. list_for_each_safe(ele, next, &eth_cmd_line) {
  631. struct iss_net_init *eth;
  632. eth = list_entry(ele, struct iss_net_init, list);
  633. iss_net_configure(eth->index, eth->init);
  634. }
  635. return 1;
  636. }
  637. module_init(iss_net_init);