#include <linux/io.h>
 #include <linux/kernel.h>
 
+#define DEBUG_QMGR     0
+
 #define HALF_QUEUES    32
 #define QUEUES         64      /* only 32 lower queues currently supported */
 #define MAX_QUEUE_LENGTH 4     /* in dwords */
 void qmgr_disable_irq(unsigned int queue);
 
 /* request_ and release_queue() must be called from non-IRQ context */
+
+#if DEBUG_QMGR
+extern char qmgr_queue_descs[QUEUES][32];
+
 int qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */,
                       unsigned int nearly_empty_watermark,
-                      unsigned int nearly_full_watermark);
+                      unsigned int nearly_full_watermark,
+                      const char *desc_format, const char* name);
+#else
+int __qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */,
+                        unsigned int nearly_empty_watermark,
+                        unsigned int nearly_full_watermark);
+#define qmgr_request_queue(queue, len, nearly_empty_watermark,         \
+                          nearly_full_watermark, desc_format, name)    \
+       __qmgr_request_queue(queue, len, nearly_empty_watermark,        \
+                            nearly_full_watermark)
+#endif
+
 void qmgr_release_queue(unsigned int queue);
 
 
 static inline void qmgr_put_entry(unsigned int queue, u32 val)
 {
        extern struct qmgr_regs __iomem *qmgr_regs;
+#if DEBUG_QMGR
+       BUG_ON(!qmgr_queue_descs[queue]); /* not yet requested */
+
+       printk(KERN_DEBUG "Queue %s(%i) put %X\n",
+              qmgr_queue_descs[queue], queue, val);
+#endif
        __raw_writel(val, &qmgr_regs->acc[queue][0]);
 }
 
 static inline u32 qmgr_get_entry(unsigned int queue)
 {
+       u32 val;
        extern struct qmgr_regs __iomem *qmgr_regs;
-       return __raw_readl(&qmgr_regs->acc[queue][0]);
+       val = __raw_readl(&qmgr_regs->acc[queue][0]);
+#if DEBUG_QMGR
+       BUG_ON(!qmgr_queue_descs[queue]); /* not yet requested */
+
+       printk(KERN_DEBUG "Queue %s(%i) get %X\n",
+              qmgr_queue_descs[queue], queue, val);
+#endif
+       return val;
 }
 
 static inline int qmgr_get_stat1(unsigned int queue)
 
 #include <linux/module.h>
 #include <mach/qmgr.h>
 
-#define DEBUG          0
-
 struct qmgr_regs __iomem *qmgr_regs;
 static struct resource *mem_res;
 static spinlock_t qmgr_lock;
 static void (*irq_handlers[HALF_QUEUES])(void *pdev);
 static void *irq_pdevs[HALF_QUEUES];
 
+#if DEBUG_QMGR
+char qmgr_queue_descs[QUEUES][32];
+#endif
+
 void qmgr_set_irq(unsigned int queue, int src,
                  void (*handler)(void *pdev), void *pdev)
 {
        mask[0] <<= 1;
 }
 
+#if DEBUG_QMGR
 int qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */,
                       unsigned int nearly_empty_watermark,
-                      unsigned int nearly_full_watermark)
+                      unsigned int nearly_full_watermark,
+                      const char *desc_format, const char* name)
+#else
+int __qmgr_request_queue(unsigned int queue, unsigned int len /* dwords */,
+                        unsigned int nearly_empty_watermark,
+                        unsigned int nearly_full_watermark)
+#endif
 {
        u32 cfg, addr = 0, mask[4]; /* in 16-dwords */
        int err;
        used_sram_bitmap[2] |= mask[2];
        used_sram_bitmap[3] |= mask[3];
        __raw_writel(cfg | (addr << 14), &qmgr_regs->sram[queue]);
-       spin_unlock_irq(&qmgr_lock);
-
-#if DEBUG
-       printk(KERN_DEBUG "qmgr: requested queue %i, addr = 0x%02X\n",
-              queue, addr);
+#if DEBUG_QMGR
+       snprintf(qmgr_queue_descs[queue], sizeof(qmgr_queue_descs[0]),
+                desc_format, name);
+       printk(KERN_DEBUG "qmgr: requested queue %s(%i) addr = 0x%02X\n",
+              qmgr_queue_descs[queue], queue, addr);
 #endif
+       spin_unlock_irq(&qmgr_lock);
        return 0;
 
 err:
        while (addr--)
                shift_mask(mask);
 
+#if DEBUG_QMGR
+       printk(KERN_DEBUG "qmgr: releasing queue %s(%i)\n",
+              qmgr_queue_descs[queue], queue);
+       qmgr_queue_descs[queue][0] = '\x0';
+#endif
        __raw_writel(0, &qmgr_regs->sram[queue]);
 
        used_sram_bitmap[0] &= ~mask[0];
        module_put(THIS_MODULE);
 
        while ((addr = qmgr_get_entry(queue)))
-               printk(KERN_ERR "qmgr: released queue %d not empty: 0x%08X\n",
+               printk(KERN_ERR "qmgr: released queue %i not empty: 0x%08X\n",
                       queue, addr);
-#if DEBUG
-       printk(KERN_DEBUG "qmgr: released queue %i\n", queue);
-#endif
 }
 
 static int qmgr_init(void)
 EXPORT_SYMBOL(qmgr_set_irq);
 EXPORT_SYMBOL(qmgr_enable_irq);
 EXPORT_SYMBOL(qmgr_disable_irq);
+#if DEBUG_QMGR
+EXPORT_SYMBOL(qmgr_queue_descs);
 EXPORT_SYMBOL(qmgr_request_queue);
+#else
+EXPORT_SYMBOL(__qmgr_request_queue);
+#endif
 EXPORT_SYMBOL(qmgr_release_queue);
 
 #include <mach/npe.h>
 #include <mach/qmgr.h>
 
-#define DEBUG_QUEUES           0
 #define DEBUG_DESC             0
 #define DEBUG_RX               0
 #define DEBUG_TX               0
 #endif
 }
 
-static inline void debug_queue(unsigned int queue, int is_get, u32 phys)
-{
-#if DEBUG_QUEUES
-       static struct {
-               int queue;
-               char *name;
-       } names[] = {
-               { TX_QUEUE(0x10), "TX#0 " },
-               { TX_QUEUE(0x20), "TX#1 " },
-               { TX_QUEUE(0x00), "TX#2 " },
-               { RXFREE_QUEUE(0x10), "RX-free#0 " },
-               { RXFREE_QUEUE(0x20), "RX-free#1 " },
-               { RXFREE_QUEUE(0x00), "RX-free#2 " },
-               { TXDONE_QUEUE, "TX-done " },
-       };
-       int i;
-
-       for (i = 0; i < ARRAY_SIZE(names); i++)
-               if (names[i].queue == queue)
-                       break;
-
-       printk(KERN_DEBUG "Queue %i %s%s %X\n", queue,
-              i < ARRAY_SIZE(names) ? names[i].name : "",
-              is_get ? "->" : "<-", phys);
-#endif
-}
-
-static inline u32 queue_get_entry(unsigned int queue)
-{
-       u32 phys = qmgr_get_entry(queue);
-       debug_queue(queue, 1, phys);
-       return phys;
-}
-
 static inline int queue_get_desc(unsigned int queue, struct port *port,
                                 int is_tx)
 {
        u32 phys, tab_phys, n_desc;
        struct desc *tab;
 
-       if (!(phys = queue_get_entry(queue)))
+       if (!(phys = qmgr_get_entry(queue)))
                return -1;
 
        phys &= ~0x1F; /* mask out non-address bits */
 static inline void queue_put_desc(unsigned int queue, u32 phys,
                                  struct desc *desc)
 {
-       debug_queue(queue, 0, phys);
        debug_desc(phys, desc);
        BUG_ON(phys & 0x1F);
        qmgr_put_entry(queue, phys);
 #if DEBUG_TX
        printk(KERN_DEBUG DRV_NAME ": eth_txdone_irq\n");
 #endif
-       while ((phys = queue_get_entry(TXDONE_QUEUE)) != 0) {
+       while ((phys = qmgr_get_entry(TXDONE_QUEUE)) != 0) {
                u32 npe_id, n_desc;
                struct port *port;
                struct desc *desc;
 {
        int err;
 
-       err = qmgr_request_queue(RXFREE_QUEUE(port->id), RX_DESCS, 0, 0);
+       err = qmgr_request_queue(RXFREE_QUEUE(port->id), RX_DESCS, 0, 0,
+                           "%s:RX-free", port->netdev->name);
        if (err)
                return err;
 
-       err = qmgr_request_queue(port->plat->rxq, RX_DESCS, 0, 0);
+       err = qmgr_request_queue(port->plat->rxq, RX_DESCS, 0, 0,
+                           "%s:RX", port->netdev->name);
        if (err)
                goto rel_rxfree;
 
-       err = qmgr_request_queue(TX_QUEUE(port->id), TX_DESCS, 0, 0);
+       err = qmgr_request_queue(TX_QUEUE(port->id), TX_DESCS, 0, 0,
+                           "%s:TX", port->netdev->name);
        if (err)
                goto rel_rx;
 
-       err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0);
+       err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0,
+                           "%s:TX-ready", port->netdev->name);
        if (err)
                goto rel_tx;
 
        /* TX-done queue handles skbs sent out by the NPEs */
        if (!ports_open) {
-               err = qmgr_request_queue(TXDONE_QUEUE, TXDONE_QUEUE_LEN, 0, 0);
+               err = qmgr_request_queue(TXDONE_QUEUE, TXDONE_QUEUE_LEN, 0, 0,
+                                   "%s:TX-done", DRV_NAME);
                if (err)
                        goto rel_txready;
        }
 
 #include <mach/npe.h>
 #include <mach/qmgr.h>
 
-#define DEBUG_QUEUES           0
 #define DEBUG_DESC             0
 #define DEBUG_RX               0
 #define DEBUG_TX               0
 #endif
 }
 
-static inline void debug_queue(unsigned int queue, int is_get, u32 phys)
-{
-#if DEBUG_QUEUES
-       static struct {
-               int queue;
-               char *name;
-       } names[] = {
-               { HSS0_PKT_TX0_QUEUE, "TX#0 " },
-               { HSS0_PKT_TXDONE_QUEUE, "TX-done#0 " },
-               { HSS0_PKT_RX_QUEUE, "RX#0 " },
-               { HSS0_PKT_RXFREE0_QUEUE, "RX-free#0 " },
-               { HSS1_PKT_TX0_QUEUE, "TX#1 " },
-               { HSS1_PKT_TXDONE_QUEUE, "TX-done#1 " },
-               { HSS1_PKT_RX_QUEUE, "RX#1 " },
-               { HSS1_PKT_RXFREE0_QUEUE, "RX-free#1 " },
-       };
-       int i;
-
-       for (i = 0; i < ARRAY_SIZE(names); i++)
-               if (names[i].queue == queue)
-                       break;
-
-       printk(KERN_DEBUG "Queue %i %s%s %X\n", queue,
-              i < ARRAY_SIZE(names) ? names[i].name : "",
-              is_get ? "->" : "<-", phys);
-#endif
-}
-
-static inline u32 queue_get_entry(unsigned int queue)
-{
-       u32 phys = qmgr_get_entry(queue);
-       debug_queue(queue, 1, phys);
-       return phys;
-}
-
 static inline int queue_get_desc(unsigned int queue, struct port *port,
                                 int is_tx)
 {
        u32 phys, tab_phys, n_desc;
        struct desc *tab;
 
-       if (!(phys = queue_get_entry(queue)))
+       if (!(phys = qmgr_get_entry(queue)))
                return -1;
 
        BUG_ON(phys & 0x1F);
 static inline void queue_put_desc(unsigned int queue, u32 phys,
                                  struct desc *desc)
 {
-       debug_queue(queue, 0, phys);
        debug_desc(phys, desc);
        BUG_ON(phys & 0x1F);
        qmgr_put_entry(queue, phys);
 {
        int err;
 
-       err = qmgr_request_queue(queue_ids[port->id].rxfree, RX_DESCS, 0, 0);
+       err = qmgr_request_queue(queue_ids[port->id].rxfree, RX_DESCS, 0, 0,
+                                "%s:RX-free", port->netdev->name);
        if (err)
                return err;
 
-       err = qmgr_request_queue(queue_ids[port->id].rx, RX_DESCS, 0, 0);
+       err = qmgr_request_queue(queue_ids[port->id].rx, RX_DESCS, 0, 0,
+                                "%s:RX", port->netdev->name);
        if (err)
                goto rel_rxfree;
 
-       err = qmgr_request_queue(queue_ids[port->id].tx, TX_DESCS, 0, 0);
+       err = qmgr_request_queue(queue_ids[port->id].tx, TX_DESCS, 0, 0,
+                                "%s:TX", port->netdev->name);
        if (err)
                goto rel_rx;
 
-       err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0);
+       err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0,
+                                "%s:TX-ready", port->netdev->name);
        if (err)
                goto rel_tx;
 
-       err = qmgr_request_queue(queue_ids[port->id].txdone, TX_DESCS, 0, 0);
+       err = qmgr_request_queue(queue_ids[port->id].txdone, TX_DESCS, 0, 0,
+                                "%s:TX-done", port->netdev->name);
        if (err)
                goto rel_txready;
        return 0;