2  * drivers/net/ibm_emac/ibm_emac_mal.c
 
   4  * Memory Access Layer (MAL) support
 
   6  * Copyright (c) 2004, 2005 Zultys Technologies.
 
   7  * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
 
   9  * Based on original work by
 
  10  *      Benjamin Herrenschmidt <benh@kernel.crashing.org>,
 
  11  *      David Gibson <hermes@gibson.dropbear.id.au>,
 
  13  *      Armin Kuster <akuster@mvista.com>
 
  14  *      Copyright 2002 MontaVista Softare Inc.
 
  16  * This program is free software; you can redistribute  it and/or modify it
 
  17  * under  the terms of  the GNU General  Public License as published by the
 
  18  * Free Software Foundation;  either version 2 of the  License, or (at your
 
  19  * option) any later version.
 
  22 #include <linux/module.h>
 
  23 #include <linux/kernel.h>
 
  24 #include <linux/errno.h>
 
  25 #include <linux/netdevice.h>
 
  26 #include <linux/init.h>
 
  27 #include <linux/interrupt.h>
 
  28 #include <linux/dma-mapping.h>
 
  32 #include "ibm_emac_core.h"
 
  33 #include "ibm_emac_mal.h"
 
  34 #include "ibm_emac_debug.h"
 
  36 int __init mal_register_commac(struct ibm_ocp_mal *mal,
 
  37                                struct mal_commac *commac)
 
  40         local_irq_save(flags);
 
  42         MAL_DBG("%d: reg(%08x, %08x)" NL, mal->def->index,
 
  43                 commac->tx_chan_mask, commac->rx_chan_mask);
 
  45         /* Don't let multiple commacs claim the same channel(s) */
 
  46         if ((mal->tx_chan_mask & commac->tx_chan_mask) ||
 
  47             (mal->rx_chan_mask & commac->rx_chan_mask)) {
 
  48                 local_irq_restore(flags);
 
  49                 printk(KERN_WARNING "mal%d: COMMAC channels conflict!\n",
 
  54         mal->tx_chan_mask |= commac->tx_chan_mask;
 
  55         mal->rx_chan_mask |= commac->rx_chan_mask;
 
  56         list_add(&commac->list, &mal->list);
 
  58         local_irq_restore(flags);
 
  62 void __exit mal_unregister_commac(struct ibm_ocp_mal *mal,
 
  63                                   struct mal_commac *commac)
 
  66         local_irq_save(flags);
 
  68         MAL_DBG("%d: unreg(%08x, %08x)" NL, mal->def->index,
 
  69                 commac->tx_chan_mask, commac->rx_chan_mask);
 
  71         mal->tx_chan_mask &= ~commac->tx_chan_mask;
 
  72         mal->rx_chan_mask &= ~commac->rx_chan_mask;
 
  73         list_del_init(&commac->list);
 
  75         local_irq_restore(flags);
 
  78 int mal_set_rcbs(struct ibm_ocp_mal *mal, int channel, unsigned long size)
 
  80         struct ocp_func_mal_data *maldata = mal->def->additions;
 
  81         BUG_ON(channel < 0 || channel >= maldata->num_rx_chans ||
 
  82                size > MAL_MAX_RX_SIZE);
 
  84         MAL_DBG("%d: set_rbcs(%d, %lu)" NL, mal->def->index, channel, size);
 
  88                        "mal%d: incorrect RX size %lu for the channel %d\n",
 
  89                        mal->def->index, size, channel);
 
  93         set_mal_dcrn(mal, MAL_RCBS(channel), size >> 4);
 
  97 int mal_tx_bd_offset(struct ibm_ocp_mal *mal, int channel)
 
  99         struct ocp_func_mal_data *maldata = mal->def->additions;
 
 100         BUG_ON(channel < 0 || channel >= maldata->num_tx_chans);
 
 101         return channel * NUM_TX_BUFF;
 
 104 int mal_rx_bd_offset(struct ibm_ocp_mal *mal, int channel)
 
 106         struct ocp_func_mal_data *maldata = mal->def->additions;
 
 107         BUG_ON(channel < 0 || channel >= maldata->num_rx_chans);
 
 108         return maldata->num_tx_chans * NUM_TX_BUFF + channel * NUM_RX_BUFF;
 
 111 void mal_enable_tx_channel(struct ibm_ocp_mal *mal, int channel)
 
 114         MAL_DBG("%d: enable_tx(%d)" NL, mal->def->index, channel);
 
 115         set_mal_dcrn(mal, MAL_TXCASR,
 
 116                      get_mal_dcrn(mal, MAL_TXCASR) | MAL_CHAN_MASK(channel));
 
 120 void mal_disable_tx_channel(struct ibm_ocp_mal *mal, int channel)
 
 122         set_mal_dcrn(mal, MAL_TXCARR, MAL_CHAN_MASK(channel));
 
 123         MAL_DBG("%d: disable_tx(%d)" NL, mal->def->index, channel);
 
 126 void mal_enable_rx_channel(struct ibm_ocp_mal *mal, int channel)
 
 129         MAL_DBG("%d: enable_rx(%d)" NL, mal->def->index, channel);
 
 130         set_mal_dcrn(mal, MAL_RXCASR,
 
 131                      get_mal_dcrn(mal, MAL_RXCASR) | MAL_CHAN_MASK(channel));
 
 135 void mal_disable_rx_channel(struct ibm_ocp_mal *mal, int channel)
 
 137         set_mal_dcrn(mal, MAL_RXCARR, MAL_CHAN_MASK(channel));
 
 138         MAL_DBG("%d: disable_rx(%d)" NL, mal->def->index, channel);
 
 141 void mal_poll_add(struct ibm_ocp_mal *mal, struct mal_commac *commac)
 
 144         MAL_DBG("%d: poll_add(%p)" NL, mal->def->index, commac);
 
 145         list_add_tail(&commac->poll_list, &mal->poll_list);
 
 149 void mal_poll_del(struct ibm_ocp_mal *mal, struct mal_commac *commac)
 
 152         MAL_DBG("%d: poll_del(%p)" NL, mal->def->index, commac);
 
 153         list_del(&commac->poll_list);
 
 157 /* synchronized by mal_poll() */
 
 158 static inline void mal_enable_eob_irq(struct ibm_ocp_mal *mal)
 
 160         MAL_DBG2("%d: enable_irq" NL, mal->def->index);
 
 161         set_mal_dcrn(mal, MAL_CFG, get_mal_dcrn(mal, MAL_CFG) | MAL_CFG_EOPIE);
 
 164 /* synchronized by __LINK_STATE_RX_SCHED bit in ndev->state */
 
 165 static inline void mal_disable_eob_irq(struct ibm_ocp_mal *mal)
 
 167         set_mal_dcrn(mal, MAL_CFG, get_mal_dcrn(mal, MAL_CFG) & ~MAL_CFG_EOPIE);
 
 168         MAL_DBG2("%d: disable_irq" NL, mal->def->index);
 
 171 static irqreturn_t mal_serr(int irq, void *dev_instance)
 
 173         struct ibm_ocp_mal *mal = dev_instance;
 
 174         u32 esr = get_mal_dcrn(mal, MAL_ESR);
 
 176         /* Clear the error status register */
 
 177         set_mal_dcrn(mal, MAL_ESR, esr);
 
 179         MAL_DBG("%d: SERR %08x" NL, mal->def->index, esr);
 
 181         if (esr & MAL_ESR_EVB) {
 
 182                 if (esr & MAL_ESR_DE) {
 
 183                         /* We ignore Descriptor error,
 
 184                          * TXDE or RXDE interrupt will be generated anyway.
 
 189                 if (esr & MAL_ESR_PEIN) {
 
 190                         /* PLB error, it's probably buggy hardware or
 
 191                          * incorrect physical address in BD (i.e. bug)
 
 195                                        "mal%d: system error, PLB (ESR = 0x%08x)\n",
 
 196                                        mal->def->index, esr);
 
 200                 /* OPB error, it's probably buggy hardware or incorrect EBC setup */
 
 203                                "mal%d: system error, OPB (ESR = 0x%08x)\n",
 
 204                                mal->def->index, esr);
 
 209 static inline void mal_schedule_poll(struct ibm_ocp_mal *mal)
 
 211         if (likely(netif_rx_schedule_prep(&mal->poll_dev))) {
 
 212                 MAL_DBG2("%d: schedule_poll" NL, mal->def->index);
 
 213                 mal_disable_eob_irq(mal);
 
 214                 __netif_rx_schedule(&mal->poll_dev);
 
 216                 MAL_DBG2("%d: already in poll" NL, mal->def->index);
 
 219 static irqreturn_t mal_txeob(int irq, void *dev_instance)
 
 221         struct ibm_ocp_mal *mal = dev_instance;
 
 222         u32 r = get_mal_dcrn(mal, MAL_TXEOBISR);
 
 223         MAL_DBG2("%d: txeob %08x" NL, mal->def->index, r);
 
 224         mal_schedule_poll(mal);
 
 225         set_mal_dcrn(mal, MAL_TXEOBISR, r);
 
 229 static irqreturn_t mal_rxeob(int irq, void *dev_instance)
 
 231         struct ibm_ocp_mal *mal = dev_instance;
 
 232         u32 r = get_mal_dcrn(mal, MAL_RXEOBISR);
 
 233         MAL_DBG2("%d: rxeob %08x" NL, mal->def->index, r);
 
 234         mal_schedule_poll(mal);
 
 235         set_mal_dcrn(mal, MAL_RXEOBISR, r);
 
 239 static irqreturn_t mal_txde(int irq, void *dev_instance)
 
 241         struct ibm_ocp_mal *mal = dev_instance;
 
 242         u32 deir = get_mal_dcrn(mal, MAL_TXDEIR);
 
 243         set_mal_dcrn(mal, MAL_TXDEIR, deir);
 
 245         MAL_DBG("%d: txde %08x" NL, mal->def->index, deir);
 
 249                        "mal%d: TX descriptor error (TXDEIR = 0x%08x)\n",
 
 250                        mal->def->index, deir);
 
 255 static irqreturn_t mal_rxde(int irq, void *dev_instance)
 
 257         struct ibm_ocp_mal *mal = dev_instance;
 
 259         u32 deir = get_mal_dcrn(mal, MAL_RXDEIR);
 
 261         MAL_DBG("%d: rxde %08x" NL, mal->def->index, deir);
 
 263         list_for_each(l, &mal->list) {
 
 264                 struct mal_commac *mc = list_entry(l, struct mal_commac, list);
 
 265                 if (deir & mc->rx_chan_mask) {
 
 267                         mc->ops->rxde(mc->dev);
 
 271         mal_schedule_poll(mal);
 
 272         set_mal_dcrn(mal, MAL_RXDEIR, deir);
 
 277 static int mal_poll(struct net_device *ndev, int *budget)
 
 279         struct ibm_ocp_mal *mal = ndev->priv;
 
 281         int rx_work_limit = min(ndev->quota, *budget), received = 0, done;
 
 283         MAL_DBG2("%d: poll(%d) %d ->" NL, mal->def->index, *budget,
 
 286         /* Process TX skbs */
 
 287         list_for_each(l, &mal->poll_list) {
 
 288                 struct mal_commac *mc =
 
 289                     list_entry(l, struct mal_commac, poll_list);
 
 290                 mc->ops->poll_tx(mc->dev);
 
 294          * We _might_ need something more smart here to enforce polling fairness.
 
 296         list_for_each(l, &mal->poll_list) {
 
 297                 struct mal_commac *mc =
 
 298                     list_entry(l, struct mal_commac, poll_list);
 
 299                 int n = mc->ops->poll_rx(mc->dev, rx_work_limit);
 
 303                         if (rx_work_limit <= 0) {
 
 305                                 goto more_work; // XXX What if this is the last one ?
 
 310         /* We need to disable IRQs to protect from RXDE IRQ here */
 
 312         __netif_rx_complete(ndev);
 
 313         mal_enable_eob_irq(mal);
 
 318         /* Check for "rotting" packet(s) */
 
 319         list_for_each(l, &mal->poll_list) {
 
 320                 struct mal_commac *mc =
 
 321                     list_entry(l, struct mal_commac, poll_list);
 
 322                 if (unlikely(mc->ops->peek_rx(mc->dev) || mc->rx_stopped)) {
 
 323                         MAL_DBG2("%d: rotting packet" NL, mal->def->index);
 
 324                         if (netif_rx_reschedule(ndev, received))
 
 325                                 mal_disable_eob_irq(mal);
 
 327                                 MAL_DBG2("%d: already in poll list" NL,
 
 330                         if (rx_work_limit > 0)
 
 335                 mc->ops->poll_tx(mc->dev);
 
 339         ndev->quota -= received;
 
 342         MAL_DBG2("%d: poll() %d <- %d" NL, mal->def->index, *budget,
 
 347 static void mal_reset(struct ibm_ocp_mal *mal)
 
 350         MAL_DBG("%d: reset" NL, mal->def->index);
 
 352         set_mal_dcrn(mal, MAL_CFG, MAL_CFG_SR);
 
 354         /* Wait for reset to complete (1 system clock) */
 
 355         while ((get_mal_dcrn(mal, MAL_CFG) & MAL_CFG_SR) && n)
 
 359                 printk(KERN_ERR "mal%d: reset timeout\n", mal->def->index);
 
 362 int mal_get_regs_len(struct ibm_ocp_mal *mal)
 
 364         return sizeof(struct emac_ethtool_regs_subhdr) +
 
 365             sizeof(struct ibm_mal_regs);
 
 368 void *mal_dump_regs(struct ibm_ocp_mal *mal, void *buf)
 
 370         struct emac_ethtool_regs_subhdr *hdr = buf;
 
 371         struct ibm_mal_regs *regs = (struct ibm_mal_regs *)(hdr + 1);
 
 372         struct ocp_func_mal_data *maldata = mal->def->additions;
 
 375         hdr->version = MAL_VERSION;
 
 376         hdr->index = mal->def->index;
 
 378         regs->tx_count = maldata->num_tx_chans;
 
 379         regs->rx_count = maldata->num_rx_chans;
 
 381         regs->cfg = get_mal_dcrn(mal, MAL_CFG);
 
 382         regs->esr = get_mal_dcrn(mal, MAL_ESR);
 
 383         regs->ier = get_mal_dcrn(mal, MAL_IER);
 
 384         regs->tx_casr = get_mal_dcrn(mal, MAL_TXCASR);
 
 385         regs->tx_carr = get_mal_dcrn(mal, MAL_TXCARR);
 
 386         regs->tx_eobisr = get_mal_dcrn(mal, MAL_TXEOBISR);
 
 387         regs->tx_deir = get_mal_dcrn(mal, MAL_TXDEIR);
 
 388         regs->rx_casr = get_mal_dcrn(mal, MAL_RXCASR);
 
 389         regs->rx_carr = get_mal_dcrn(mal, MAL_RXCARR);
 
 390         regs->rx_eobisr = get_mal_dcrn(mal, MAL_RXEOBISR);
 
 391         regs->rx_deir = get_mal_dcrn(mal, MAL_RXDEIR);
 
 393         for (i = 0; i < regs->tx_count; ++i)
 
 394                 regs->tx_ctpr[i] = get_mal_dcrn(mal, MAL_TXCTPR(i));
 
 396         for (i = 0; i < regs->rx_count; ++i) {
 
 397                 regs->rx_ctpr[i] = get_mal_dcrn(mal, MAL_RXCTPR(i));
 
 398                 regs->rcbs[i] = get_mal_dcrn(mal, MAL_RCBS(i));
 
 403 static int __init mal_probe(struct ocp_device *ocpdev)
 
 405         struct ibm_ocp_mal *mal;
 
 406         struct ocp_func_mal_data *maldata;
 
 407         int err = 0, i, bd_size;
 
 409         MAL_DBG("%d: probe" NL, ocpdev->def->index);
 
 411         maldata = ocpdev->def->additions;
 
 412         if (maldata == NULL) {
 
 413                 printk(KERN_ERR "mal%d: missing additional data!\n",
 
 418         mal = kzalloc(sizeof(struct ibm_ocp_mal), GFP_KERNEL);
 
 421                        "mal%d: out of memory allocating MAL structure!\n",
 
 425         mal->dcrbase = maldata->dcr_base;
 
 426         mal->def = ocpdev->def;
 
 428         INIT_LIST_HEAD(&mal->poll_list);
 
 429         set_bit(__LINK_STATE_START, &mal->poll_dev.state);
 
 430         mal->poll_dev.weight = CONFIG_IBM_EMAC_POLL_WEIGHT;
 
 431         mal->poll_dev.poll = mal_poll;
 
 432         mal->poll_dev.priv = mal;
 
 433         atomic_set(&mal->poll_dev.refcnt, 1);
 
 435         INIT_LIST_HEAD(&mal->list);
 
 437         /* Load power-on reset defaults */
 
 440         /* Set the MAL configuration register */
 
 441         set_mal_dcrn(mal, MAL_CFG, MAL_CFG_DEFAULT | MAL_CFG_PLBB |
 
 442                      MAL_CFG_OPBBL | MAL_CFG_LEA);
 
 444         mal_enable_eob_irq(mal);
 
 446         /* Allocate space for BD rings */
 
 447         BUG_ON(maldata->num_tx_chans <= 0 || maldata->num_tx_chans > 32);
 
 448         BUG_ON(maldata->num_rx_chans <= 0 || maldata->num_rx_chans > 32);
 
 449         bd_size = sizeof(struct mal_descriptor) *
 
 450             (NUM_TX_BUFF * maldata->num_tx_chans +
 
 451              NUM_RX_BUFF * maldata->num_rx_chans);
 
 453             dma_alloc_coherent(&ocpdev->dev, bd_size, &mal->bd_dma, GFP_KERNEL);
 
 457                        "mal%d: out of memory allocating RX/TX descriptors!\n",
 
 462         memset(mal->bd_virt, 0, bd_size);
 
 464         for (i = 0; i < maldata->num_tx_chans; ++i)
 
 465                 set_mal_dcrn(mal, MAL_TXCTPR(i), mal->bd_dma +
 
 466                              sizeof(struct mal_descriptor) *
 
 467                              mal_tx_bd_offset(mal, i));
 
 469         for (i = 0; i < maldata->num_rx_chans; ++i)
 
 470                 set_mal_dcrn(mal, MAL_RXCTPR(i), mal->bd_dma +
 
 471                              sizeof(struct mal_descriptor) *
 
 472                              mal_rx_bd_offset(mal, i));
 
 474         err = request_irq(maldata->serr_irq, mal_serr, 0, "MAL SERR", mal);
 
 477         err = request_irq(maldata->txde_irq, mal_txde, 0, "MAL TX DE", mal);
 
 480         err = request_irq(maldata->txeob_irq, mal_txeob, 0, "MAL TX EOB", mal);
 
 483         err = request_irq(maldata->rxde_irq, mal_rxde, 0, "MAL RX DE", mal);
 
 486         err = request_irq(maldata->rxeob_irq, mal_rxeob, 0, "MAL RX EOB", mal);
 
 490         /* Enable all MAL SERR interrupt sources */
 
 491         set_mal_dcrn(mal, MAL_IER, MAL_IER_EVENTS);
 
 493         /* Advertise this instance to the rest of the world */
 
 494         ocp_set_drvdata(ocpdev, mal);
 
 496         mal_dbg_register(mal->def->index, mal);
 
 498         printk(KERN_INFO "mal%d: initialized, %d TX channels, %d RX channels\n",
 
 499                mal->def->index, maldata->num_tx_chans, maldata->num_rx_chans);
 
 503         free_irq(maldata->rxde_irq, mal);
 
 505         free_irq(maldata->txeob_irq, mal);
 
 507         free_irq(maldata->txde_irq, mal);
 
 509         free_irq(maldata->serr_irq, mal);
 
 511         dma_free_coherent(&ocpdev->dev, bd_size, mal->bd_virt, mal->bd_dma);
 
 517 static void __exit mal_remove(struct ocp_device *ocpdev)
 
 519         struct ibm_ocp_mal *mal = ocp_get_drvdata(ocpdev);
 
 520         struct ocp_func_mal_data *maldata = mal->def->additions;
 
 522         MAL_DBG("%d: remove" NL, mal->def->index);
 
 524         /* Syncronize with scheduled polling, 
 
 525            stolen from net/core/dev.c:dev_close() 
 
 527         clear_bit(__LINK_STATE_START, &mal->poll_dev.state);
 
 528         netif_poll_disable(&mal->poll_dev);
 
 530         if (!list_empty(&mal->list)) {
 
 531                 /* This is *very* bad */
 
 533                        "mal%d: commac list is not empty on remove!\n",
 
 537         ocp_set_drvdata(ocpdev, NULL);
 
 539         free_irq(maldata->serr_irq, mal);
 
 540         free_irq(maldata->txde_irq, mal);
 
 541         free_irq(maldata->txeob_irq, mal);
 
 542         free_irq(maldata->rxde_irq, mal);
 
 543         free_irq(maldata->rxeob_irq, mal);
 
 547         mal_dbg_register(mal->def->index, NULL);
 
 549         dma_free_coherent(&ocpdev->dev,
 
 550                           sizeof(struct mal_descriptor) *
 
 551                           (NUM_TX_BUFF * maldata->num_tx_chans +
 
 552                            NUM_RX_BUFF * maldata->num_rx_chans), mal->bd_virt,
 
 558 /* Structure for a device driver */
 
 559 static struct ocp_device_id mal_ids[] = {
 
 560         { .vendor = OCP_VENDOR_IBM, .function = OCP_FUNC_MAL },
 
 561         { .vendor = OCP_VENDOR_INVALID}
 
 564 static struct ocp_driver mal_driver = {
 
 569         .remove = mal_remove,
 
 572 int __init mal_init(void)
 
 574         MAL_DBG(": init" NL);
 
 575         return ocp_register_driver(&mal_driver);
 
 578 void __exit mal_exit(void)
 
 580         MAL_DBG(": exit" NL);
 
 581         ocp_unregister_driver(&mal_driver);