rtl8180: don't write MAR registers for rtl8187se
[deliverable/linux.git] / drivers / net / wireless / rtl818x / rtl8180 / dev.c
index 8ec17aad0e520019fa0f93fbd50f1999885ea9ad..cce972d289ef3c20b9f97cf522e8b12fd454551d 100644 (file)
@@ -85,6 +85,30 @@ static const struct ieee80211_channel rtl818x_channels[] = {
        { .center_freq = 2484 },
 };
 
+/* Queues for rtl8180/rtl8185 cards
+ *
+ * name | reg  |  prio
+ *  BC  |  7   |   3
+ *  HI  |  6   |   0
+ *  NO  |  5   |   1
+ *  LO  |  4   |   2
+ *
+ * The complete map for DMA kick reg using all queue is:
+ * static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {6, 5, 4, 7};
+ *
+ * .. but .. Because the mac80211 needs at least 4 queues for QoS or
+ * otherwise QoS can't be done, we use just one.
+ * Beacon queue could be used, but this is not finished yet.
+ * Actual map is:
+ *
+ * name | reg  |  prio
+ *  BC  |  7   |   1  <- currently not used yet.
+ *  HI  |  6   |   x  <- not used
+ *  NO  |  5   |   x  <- not used
+ *  LO  |  4   |   0  <- used
+ */
+
+static const int rtl8180_queues_map[RTL8180_NR_TX_QUEUES] = {4, 7};
 
 void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
 {
@@ -105,13 +129,30 @@ void rtl8180_write_phy(struct ieee80211_hw *dev, u8 addr, u32 data)
 static void rtl8180_handle_rx(struct ieee80211_hw *dev)
 {
        struct rtl8180_priv *priv = dev->priv;
+       struct rtl818x_rx_cmd_desc *cmd_desc;
        unsigned int count = 32;
        u8 signal, agc, sq;
+       dma_addr_t mapping;
 
        while (count--) {
-               struct rtl8180_rx_desc *entry = &priv->rx_ring[priv->rx_idx];
+               void *entry = priv->rx_ring + priv->rx_idx * priv->rx_ring_sz;
                struct sk_buff *skb = priv->rx_buf[priv->rx_idx];
-               u32 flags = le32_to_cpu(entry->flags);
+               u32 flags, flags2;
+               u64 tsft;
+
+               if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
+                       struct rtl8187se_rx_desc *desc = entry;
+
+                       flags = le32_to_cpu(desc->flags);
+                       flags2 = le32_to_cpu(desc->flags2);
+                       tsft = le64_to_cpu(desc->tsft);
+               } else {
+                       struct rtl8180_rx_desc *desc = entry;
+
+                       flags = le32_to_cpu(desc->flags);
+                       flags2 = le32_to_cpu(desc->flags2);
+                       tsft = le64_to_cpu(desc->tsft);
+               }
 
                if (flags & RTL818X_RX_DESC_FLAG_OWN)
                        return;
@@ -121,13 +162,23 @@ static void rtl8180_handle_rx(struct ieee80211_hw *dev)
                                      RTL818X_RX_DESC_FLAG_RX_ERR)))
                        goto done;
                else {
-                       u32 flags2 = le32_to_cpu(entry->flags2);
                        struct ieee80211_rx_status rx_status = {0};
                        struct sk_buff *new_skb = dev_alloc_skb(MAX_RX_SIZE);
 
                        if (unlikely(!new_skb))
                                goto done;
 
+                       mapping = pci_map_single(priv->pdev,
+                                              skb_tail_pointer(new_skb),
+                                              MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
+
+                       if (pci_dma_mapping_error(priv->pdev, mapping)) {
+                               kfree_skb(new_skb);
+                               dev_err(&priv->pdev->dev, "RX DMA map error\n");
+
+                               goto done;
+                       }
+
                        pci_unmap_single(priv->pdev,
                                         *((dma_addr_t *)skb->cb),
                                         MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
@@ -136,19 +187,24 @@ static void rtl8180_handle_rx(struct ieee80211_hw *dev)
                        rx_status.antenna = (flags2 >> 15) & 1;
                        rx_status.rate_idx = (flags >> 20) & 0xF;
                        agc = (flags2 >> 17) & 0x7F;
-                       if (priv->r8185) {
+
+                       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
                                if (rx_status.rate_idx > 3)
                                        signal = 90 - clamp_t(u8, agc, 25, 90);
                                else
                                        signal = 95 - clamp_t(u8, agc, 30, 95);
-                       } else {
+                       } else if (priv->chip_family ==
+                                  RTL818X_CHIP_FAMILY_RTL8180) {
                                sq = flags2 & 0xff;
                                signal = priv->rf->calc_rssi(agc, sq);
+                       } else {
+                               /* TODO: rtl8187se rssi */
+                               signal = 10;
                        }
                        rx_status.signal = signal;
                        rx_status.freq = dev->conf.chandef.chan->center_freq;
                        rx_status.band = dev->conf.chandef.chan->band;
-                       rx_status.mactime = le64_to_cpu(entry->tsft);
+                       rx_status.mactime = tsft;
                        rx_status.flag |= RX_FLAG_MACTIME_START;
                        if (flags & RTL818X_RX_DESC_FLAG_CRC32_ERR)
                                rx_status.flag |= RX_FLAG_FAILED_FCS_CRC;
@@ -158,17 +214,17 @@ static void rtl8180_handle_rx(struct ieee80211_hw *dev)
 
                        skb = new_skb;
                        priv->rx_buf[priv->rx_idx] = skb;
-                       *((dma_addr_t *) skb->cb) =
-                               pci_map_single(priv->pdev, skb_tail_pointer(skb),
-                                              MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
+                       *((dma_addr_t *) skb->cb) = mapping;
                }
 
        done:
-               entry->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
-               entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
+               cmd_desc = entry;
+               cmd_desc->rx_buf = cpu_to_le32(*((dma_addr_t *)skb->cb));
+               cmd_desc->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
                                           MAX_RX_SIZE);
                if (priv->rx_idx == 31)
-                       entry->flags |= cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
+                       cmd_desc->flags |=
+                               cpu_to_le32(RTL818X_RX_DESC_FLAG_EOR);
                priv->rx_idx = (priv->rx_idx + 1) % 32;
        }
 }
@@ -208,6 +264,55 @@ static void rtl8180_handle_tx(struct ieee80211_hw *dev, unsigned int prio)
        }
 }
 
+static irqreturn_t rtl8187se_interrupt(int irq, void *dev_id)
+{
+       struct ieee80211_hw *dev = dev_id;
+       struct rtl8180_priv *priv = dev->priv;
+       u32 reg;
+       unsigned long flags;
+       static int desc_err;
+
+       spin_lock_irqsave(&priv->lock, flags);
+       /* Note: 32-bit interrupt status */
+       reg = rtl818x_ioread32(priv, &priv->map->INT_STATUS_SE);
+       if (unlikely(reg == 0xFFFFFFFF)) {
+               spin_unlock_irqrestore(&priv->lock, flags);
+               return IRQ_HANDLED;
+       }
+
+       rtl818x_iowrite32(priv, &priv->map->INT_STATUS_SE, reg);
+
+       if (reg & IMR_TIMEOUT1)
+               rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
+
+       if (reg & (IMR_TBDOK | IMR_TBDER))
+               rtl8180_handle_tx(dev, 4);
+
+       if (reg & (IMR_TVODOK | IMR_TVODER))
+               rtl8180_handle_tx(dev, 0);
+
+       if (reg & (IMR_TVIDOK | IMR_TVIDER))
+               rtl8180_handle_tx(dev, 1);
+
+       if (reg & (IMR_TBEDOK | IMR_TBEDER))
+               rtl8180_handle_tx(dev, 2);
+
+       if (reg & (IMR_TBKDOK | IMR_TBKDER))
+               rtl8180_handle_tx(dev, 3);
+
+       if (reg & (IMR_ROK | IMR_RER | RTL818X_INT_SE_RX_DU | IMR_RQOSOK))
+               rtl8180_handle_rx(dev);
+       /* The interface sometimes generates several RX DMA descriptor errors
+        * at startup. Do not report these.
+        */
+       if ((reg & RTL818X_INT_SE_RX_DU) && desc_err++ > 2)
+               if (net_ratelimit())
+                       wiphy_err(dev->wiphy, "No RX DMA Descriptor avail\n");
+
+       spin_unlock_irqrestore(&priv->lock, flags);
+       return IRQ_HANDLED;
+}
+
 static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
 {
        struct ieee80211_hw *dev = dev_id;
@@ -224,12 +329,6 @@ static irqreturn_t rtl8180_interrupt(int irq, void *dev_id)
        rtl818x_iowrite16(priv, &priv->map->INT_STATUS, reg);
 
        if (reg & (RTL818X_INT_TXB_OK | RTL818X_INT_TXB_ERR))
-               rtl8180_handle_tx(dev, 3);
-
-       if (reg & (RTL818X_INT_TXH_OK | RTL818X_INT_TXH_ERR))
-               rtl8180_handle_tx(dev, 2);
-
-       if (reg & (RTL818X_INT_TXN_OK | RTL818X_INT_TXN_ERR))
                rtl8180_handle_tx(dev, 1);
 
        if (reg & (RTL818X_INT_TXL_OK | RTL818X_INT_TXL_ERR))
@@ -253,7 +352,7 @@ static void rtl8180_tx(struct ieee80211_hw *dev,
        struct rtl8180_tx_ring *ring;
        struct rtl8180_tx_desc *entry;
        unsigned long flags;
-       unsigned int idx, prio;
+       unsigned int idx, prio, hw_prio;
        dma_addr_t mapping;
        u32 tx_flags;
        u8 rc_flags;
@@ -266,12 +365,19 @@ static void rtl8180_tx(struct ieee80211_hw *dev,
        mapping = pci_map_single(priv->pdev, skb->data,
                                 skb->len, PCI_DMA_TODEVICE);
 
+       if (pci_dma_mapping_error(priv->pdev, mapping)) {
+               kfree_skb(skb);
+               dev_err(&priv->pdev->dev, "TX DMA mapping error\n");
+               return;
+
+       }
+
        tx_flags = RTL818X_TX_DESC_FLAG_OWN | RTL818X_TX_DESC_FLAG_FS |
                   RTL818X_TX_DESC_FLAG_LS |
                   (ieee80211_get_tx_rate(dev, info)->hw_value << 24) |
                   skb->len;
 
-       if (priv->r8185)
+       if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
                tx_flags |= RTL818X_TX_DESC_FLAG_DMA |
                            RTL818X_TX_DESC_FLAG_NO_ENC;
 
@@ -288,7 +394,7 @@ static void rtl8180_tx(struct ieee80211_hw *dev,
                rts_duration = ieee80211_rts_duration(dev, priv->vif, skb->len,
                                                      info);
 
-       if (!priv->r8185) {
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
                unsigned int remainder;
 
                plcp_len = DIV_ROUND_UP(16 * (skb->len + 4),
@@ -318,14 +424,29 @@ static void rtl8180_tx(struct ieee80211_hw *dev,
        entry->flags2 = info->control.rates[1].idx >= 0 ?
                ieee80211_get_alt_retry_rate(dev, info, 0)->bitrate << 4 : 0;
        entry->retry_limit = info->control.rates[0].count;
+
+       /* We must be sure that tx_flags is written last because the HW
+        * looks at it to check if the rest of data is valid or not
+        */
+       wmb();
        entry->flags = cpu_to_le32(tx_flags);
+       /* We must be sure this has been written before followings HW
+        * register write, because this write will made the HW attempts
+        * to DMA the just-written data
+        */
+       wmb();
+
        __skb_queue_tail(&ring->queue, skb);
        if (ring->entries - skb_queue_len(&ring->queue) < 2)
                ieee80211_stop_queue(dev, prio);
 
        spin_unlock_irqrestore(&priv->lock, flags);
 
-       rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING, (1 << (prio + 4)));
+       hw_prio = rtl8180_queues_map[prio];
+
+       rtl818x_iowrite8(priv, &priv->map->TX_DMA_POLLING,
+                        (1 << hw_prio) | /* ring to poll  */
+                        (1<<1) | (1<<2));/* stopped rings */
 }
 
 void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
@@ -342,6 +463,70 @@ void rtl8180_set_anaparam(struct rtl8180_priv *priv, u32 anaparam)
        rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
 }
 
+static void rtl8180_int_enable(struct ieee80211_hw *dev)
+{
+       struct rtl8180_priv *priv = dev->priv;
+
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
+               rtl818x_iowrite32(priv, &priv->map->IMR, IMR_TMGDOK |
+                         IMR_TBDER | IMR_THPDER |
+                         IMR_THPDER | IMR_THPDOK |
+                         IMR_TVODER | IMR_TVODOK |
+                         IMR_TVIDER | IMR_TVIDOK |
+                         IMR_TBEDER | IMR_TBEDOK |
+                         IMR_TBKDER | IMR_TBKDOK |
+                         IMR_RDU | IMR_RER |
+                         IMR_ROK | IMR_RQOSOK);
+       } else {
+               rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
+       }
+}
+
+static void rtl8180_int_disable(struct ieee80211_hw *dev)
+{
+       struct rtl8180_priv *priv = dev->priv;
+
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
+               rtl818x_iowrite32(priv, &priv->map->IMR, 0);
+       } else {
+               rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
+       }
+}
+
+static void rtl8180_conf_basic_rates(struct ieee80211_hw *dev,
+                           u32 rates_mask)
+{
+       struct rtl8180_priv *priv = dev->priv;
+
+       u8 max, min;
+       u16 reg;
+
+       max = fls(rates_mask) - 1;
+       min = ffs(rates_mask) - 1;
+
+       switch (priv->chip_family) {
+
+       case RTL818X_CHIP_FAMILY_RTL8180:
+               /* in 8180 this is NOT a BITMAP */
+               reg = rtl818x_ioread16(priv, &priv->map->BRSR);
+               reg &= ~3;
+               reg |= max;
+               rtl818x_iowrite16(priv, &priv->map->BRSR, reg);
+               break;
+
+       case RTL818X_CHIP_FAMILY_RTL8185:
+               /* in 8185 this is a BITMAP */
+               rtl818x_iowrite16(priv, &priv->map->BRSR, rates_mask);
+               rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (max << 4) | min);
+               break;
+
+       case RTL818X_CHIP_FAMILY_RTL8187SE:
+               /* in 8187se this is a BITMAP */
+               rtl818x_iowrite16(priv, &priv->map->BRSR_8187SE, rates_mask);
+               break;
+       }
+}
+
 static int rtl8180_init_hw(struct ieee80211_hw *dev)
 {
        struct rtl8180_priv *priv = dev->priv;
@@ -352,7 +537,7 @@ static int rtl8180_init_hw(struct ieee80211_hw *dev)
        msleep(10);
 
        /* reset */
-       rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
+       rtl8180_int_disable(dev);
        rtl818x_ioread8(priv, &priv->map->CMD);
 
        reg = rtl818x_ioread8(priv, &priv->map->CMD);
@@ -384,20 +569,18 @@ static int rtl8180_init_hw(struct ieee80211_hw *dev)
 
        rtl818x_iowrite8(priv, &priv->map->MSR, 0);
 
-       if (!priv->r8185)
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
                rtl8180_set_anaparam(priv, priv->anaparam);
 
        rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
-       rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
-       rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
-       rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
+       rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[1].dma);
        rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
 
        /* TODO: necessary? specs indicate not */
        rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG);
        reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
        rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg & ~(1 << 3));
-       if (priv->r8185) {
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185) {
                reg = rtl818x_ioread8(priv, &priv->map->CONFIG2);
                rtl818x_iowrite8(priv, &priv->map->CONFIG2, reg | (1 << 4));
        }
@@ -409,12 +592,9 @@ static int rtl8180_init_hw(struct ieee80211_hw *dev)
 
        rtl818x_iowrite32(priv, &priv->map->INT_TIMEOUT, 0);
 
-       if (priv->r8185) {
+       if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
                rtl818x_iowrite8(priv, &priv->map->WPA_CONF, 0);
                rtl818x_iowrite8(priv, &priv->map->RATE_FALLBACK, 0x81);
-               rtl818x_iowrite8(priv, &priv->map->RESP_RATE, (8 << 4) | 0);
-
-               rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
 
                /* TODO: set ClkRun enable? necessary? */
                reg = rtl818x_ioread8(priv, &priv->map->GP_ENABLE);
@@ -424,7 +604,6 @@ static int rtl8180_init_hw(struct ieee80211_hw *dev)
                rtl818x_iowrite8(priv, &priv->map->CONFIG3, reg | (1 << 2));
                rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
        } else {
-               rtl818x_iowrite16(priv, &priv->map->BRSR, 0x1);
                rtl818x_iowrite8(priv, &priv->map->SECURITY, 0);
 
                rtl818x_iowrite8(priv, &priv->map->PHY_DELAY, 0x6);
@@ -432,19 +611,34 @@ static int rtl8180_init_hw(struct ieee80211_hw *dev)
        }
 
        priv->rf->init(dev);
-       if (priv->r8185)
-               rtl818x_iowrite16(priv, &priv->map->BRSR, 0x01F3);
+
+       /* default basic rates are 1,2 Mbps for rtl8180. 1,2,6,9,12,18,24 Mbps
+        * otherwise. bitmask 0x3 and 0x01f3 respectively.
+        * NOTE: currenty rtl8225 RF code changes basic rates, so we need to do
+        * this after rf init.
+        * TODO: try to find out whether RF code really needs to do this..
+        */
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
+               rtl8180_conf_basic_rates(dev, 0x3);
+       else
+               rtl8180_conf_basic_rates(dev, 0x1f3);
+
        return 0;
 }
 
 static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
 {
        struct rtl8180_priv *priv = dev->priv;
-       struct rtl8180_rx_desc *entry;
+       struct rtl818x_rx_cmd_desc *entry;
        int i;
 
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE)
+               priv->rx_ring_sz = sizeof(struct rtl8187se_rx_desc);
+       else
+               priv->rx_ring_sz = sizeof(struct rtl8180_rx_desc);
+
        priv->rx_ring = pci_alloc_consistent(priv->pdev,
-                                            sizeof(*priv->rx_ring) * 32,
+                                            priv->rx_ring_sz * 32,
                                             &priv->rx_ring_dma);
 
        if (!priv->rx_ring || (unsigned long)priv->rx_ring & 0xFF) {
@@ -452,20 +646,28 @@ static int rtl8180_init_rx_ring(struct ieee80211_hw *dev)
                return -ENOMEM;
        }
 
-       memset(priv->rx_ring, 0, sizeof(*priv->rx_ring) * 32);
+       memset(priv->rx_ring, 0, priv->rx_ring_sz * 32);
        priv->rx_idx = 0;
 
        for (i = 0; i < 32; i++) {
                struct sk_buff *skb = dev_alloc_skb(MAX_RX_SIZE);
                dma_addr_t *mapping;
-               entry = &priv->rx_ring[i];
-               if (!skb)
-                       return 0;
-
+               entry = priv->rx_ring + priv->rx_ring_sz*i;
+               if (!skb) {
+                       wiphy_err(dev->wiphy, "Cannot allocate RX skb\n");
+                       return -ENOMEM;
+               }
                priv->rx_buf[i] = skb;
                mapping = (dma_addr_t *)skb->cb;
                *mapping = pci_map_single(priv->pdev, skb_tail_pointer(skb),
                                          MAX_RX_SIZE, PCI_DMA_FROMDEVICE);
+
+               if (pci_dma_mapping_error(priv->pdev, *mapping)) {
+                       kfree_skb(skb);
+                       wiphy_err(dev->wiphy, "Cannot map DMA for RX skb\n");
+                       return -ENOMEM;
+               }
+
                entry->rx_buf = cpu_to_le32(*mapping);
                entry->flags = cpu_to_le32(RTL818X_RX_DESC_FLAG_OWN |
                                           MAX_RX_SIZE);
@@ -490,7 +692,7 @@ static void rtl8180_free_rx_ring(struct ieee80211_hw *dev)
                kfree_skb(skb);
        }
 
-       pci_free_consistent(priv->pdev, sizeof(*priv->rx_ring) * 32,
+       pci_free_consistent(priv->pdev, priv->rx_ring_sz * 32,
                            priv->rx_ring, priv->rx_ring_dma);
        priv->rx_ring = NULL;
 }
@@ -554,7 +756,7 @@ static int rtl8180_start(struct ieee80211_hw *dev)
        if (ret)
                return ret;
 
-       for (i = 0; i < 4; i++)
+       for (i = 0; i < (dev->queues + 1); i++)
                if ((ret = rtl8180_init_tx_ring(dev, i, 16)))
                        goto err_free_rings;
 
@@ -562,23 +764,28 @@ static int rtl8180_start(struct ieee80211_hw *dev)
        if (ret)
                goto err_free_rings;
 
-       rtl818x_iowrite32(priv, &priv->map->RDSAR, priv->rx_ring_dma);
-       rtl818x_iowrite32(priv, &priv->map->TBDA, priv->tx_ring[3].dma);
-       rtl818x_iowrite32(priv, &priv->map->THPDA, priv->tx_ring[2].dma);
-       rtl818x_iowrite32(priv, &priv->map->TNPDA, priv->tx_ring[1].dma);
-       rtl818x_iowrite32(priv, &priv->map->TLPDA, priv->tx_ring[0].dma);
-
-       ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8187SE) {
+               ret = request_irq(priv->pdev->irq, rtl8187se_interrupt,
+                         IRQF_SHARED, KBUILD_MODNAME, dev);
+       } else {
+               ret = request_irq(priv->pdev->irq, rtl8180_interrupt,
                          IRQF_SHARED, KBUILD_MODNAME, dev);
+       }
+
        if (ret) {
                wiphy_err(dev->wiphy, "failed to register IRQ handler\n");
                goto err_free_rings;
        }
 
-       rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0xFFFF);
+       rtl8180_int_enable(dev);
 
-       rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
-       rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
+       /* in rtl8187se at MAR regs offset there is the management
+        * TX descriptor DMA addres..
+        */
+       if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8187SE) {
+               rtl818x_iowrite32(priv, &priv->map->MAR[0], ~0);
+               rtl818x_iowrite32(priv, &priv->map->MAR[1], ~0);
+       }
 
        reg = RTL818X_RX_CONF_ONLYERLPKT |
              RTL818X_RX_CONF_RX_AUTORESETPHY |
@@ -588,7 +795,7 @@ static int rtl8180_start(struct ieee80211_hw *dev)
              RTL818X_RX_CONF_BROADCAST |
              RTL818X_RX_CONF_NICMAC;
 
-       if (priv->r8185)
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8185)
                reg |= RTL818X_RX_CONF_CSDM1 | RTL818X_RX_CONF_CSDM2;
        else {
                reg |= (priv->rfparam & RF_PARAM_CARRIERSENSE1)
@@ -600,15 +807,27 @@ static int rtl8180_start(struct ieee80211_hw *dev)
        priv->rx_conf = reg;
        rtl818x_iowrite32(priv, &priv->map->RX_CONF, reg);
 
-       if (priv->r8185) {
+       if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
                reg = rtl818x_ioread8(priv, &priv->map->CW_CONF);
-               reg &= ~RTL818X_CW_CONF_PERPACKET_CW_SHIFT;
-               reg |= RTL818X_CW_CONF_PERPACKET_RETRY_SHIFT;
+
+               /* CW is not on per-packet basis.
+                * in rtl8185 the CW_VALUE reg is used.
+                */
+               reg &= ~RTL818X_CW_CONF_PERPACKET_CW;
+               /* retry limit IS on per-packet basis.
+                * the short and long retry limit in TX_CONF
+                * reg are ignored
+                */
+               reg |= RTL818X_CW_CONF_PERPACKET_RETRY;
                rtl818x_iowrite8(priv, &priv->map->CW_CONF, reg);
 
                reg = rtl818x_ioread8(priv, &priv->map->TX_AGC_CTL);
-               reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN_SHIFT;
-               reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL_SHIFT;
+               /* TX antenna and TX gain are not on per-packet basis.
+                * TX Antenna is selected by ANTSEL reg (RX in BB regs).
+                * TX gain is selected with CCK_TX_AGC and OFDM_TX_AGC regs
+                */
+               reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_GAIN;
+               reg &= ~RTL818X_TX_AGC_CTL_PERPACKET_ANTSEL;
                reg |=  RTL818X_TX_AGC_CTL_FEEDBACK_ANT;
                rtl818x_iowrite8(priv, &priv->map->TX_AGC_CTL, reg);
 
@@ -620,11 +839,15 @@ static int rtl8180_start(struct ieee80211_hw *dev)
        reg |= (6 << 21 /* MAX TX DMA */) |
               RTL818X_TX_CONF_NO_ICV;
 
-       if (priv->r8185)
+
+
+       if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180)
                reg &= ~RTL818X_TX_CONF_PROBE_DTS;
        else
                reg &= ~RTL818X_TX_CONF_HW_SEQNUM;
 
+       reg &= ~RTL818X_TX_CONF_DISCW;
+
        /* different meaning, same value on both rtl8185 and rtl8180 */
        reg &= ~RTL818X_TX_CONF_SAT_HWPLCP;
 
@@ -639,7 +862,7 @@ static int rtl8180_start(struct ieee80211_hw *dev)
 
  err_free_rings:
        rtl8180_free_rx_ring(dev);
-       for (i = 0; i < 4; i++)
+       for (i = 0; i < (dev->queues + 1); i++)
                if (priv->tx_ring[i].desc)
                        rtl8180_free_tx_ring(dev, i);
 
@@ -652,7 +875,7 @@ static void rtl8180_stop(struct ieee80211_hw *dev)
        u8 reg;
        int i;
 
-       rtl818x_iowrite16(priv, &priv->map->INT_MASK, 0);
+       rtl8180_int_disable(dev);
 
        reg = rtl818x_ioread8(priv, &priv->map->CMD);
        reg &= ~RTL818X_CMD_TX_ENABLE;
@@ -669,7 +892,7 @@ static void rtl8180_stop(struct ieee80211_hw *dev)
        free_irq(priv->pdev->irq, dev);
 
        rtl8180_free_rx_ring(dev);
-       for (i = 0; i < 4; i++)
+       for (i = 0; i < (dev->queues + 1); i++)
                rtl8180_free_tx_ring(dev, i);
 }
 
@@ -777,6 +1000,72 @@ static int rtl8180_config(struct ieee80211_hw *dev, u32 changed)
        return 0;
 }
 
+static int rtl8180_conf_tx(struct ieee80211_hw *dev,
+                           struct ieee80211_vif *vif, u16 queue,
+                           const struct ieee80211_tx_queue_params *params)
+{
+       struct rtl8180_priv *priv = dev->priv;
+       u8 cw_min, cw_max;
+
+       /* nothing to do ? */
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
+               return 0;
+
+       cw_min = fls(params->cw_min);
+       cw_max = fls(params->cw_max);
+
+       rtl818x_iowrite8(priv, &priv->map->CW_VAL, (cw_max << 4) | cw_min);
+
+       return 0;
+}
+
+static void rtl8180_conf_erp(struct ieee80211_hw *dev,
+                           struct ieee80211_bss_conf *info)
+{
+       struct rtl8180_priv *priv = dev->priv;
+       u8 sifs, difs;
+       int eifs;
+       u8 hw_eifs;
+
+       /* TODO: should we do something ? */
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180)
+               return;
+
+       /* I _hope_ this means 10uS for the HW.
+        * In reference code it is 0x22 for
+        * both rtl8187L and rtl8187SE
+        */
+       sifs = 0x22;
+
+       if (info->use_short_slot)
+               priv->slot_time = 9;
+       else
+               priv->slot_time = 20;
+
+       /* 10 is SIFS time in uS */
+       difs = 10 + 2 * priv->slot_time;
+       eifs = 10 + difs + priv->ack_time;
+
+       /* HW should use 4uS units for EIFS (I'm sure for rtl8185)*/
+       hw_eifs = DIV_ROUND_UP(eifs, 4);
+
+
+       rtl818x_iowrite8(priv, &priv->map->SLOT, priv->slot_time);
+       rtl818x_iowrite8(priv, &priv->map->SIFS, sifs);
+       rtl818x_iowrite8(priv, &priv->map->DIFS, difs);
+
+       /* from reference code. set ack timeout reg = eifs reg */
+       rtl818x_iowrite8(priv, &priv->map->CARRIER_SENSE_COUNTER, hw_eifs);
+
+       /* rtl8187/rtl8185 HW bug. After EIFS is elapsed,
+        * the HW still wait for DIFS.
+        * HW uses 4uS units for EIFS.
+        */
+       hw_eifs = DIV_ROUND_UP(eifs - difs, 4);
+
+       rtl818x_iowrite8(priv, &priv->map->EIFS, hw_eifs);
+}
+
 static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
                                     struct ieee80211_vif *vif,
                                     struct ieee80211_bss_conf *info,
@@ -804,8 +1093,23 @@ static void rtl8180_bss_info_changed(struct ieee80211_hw *dev,
                rtl818x_iowrite8(priv, &priv->map->MSR, reg);
        }
 
-       if (changed & BSS_CHANGED_ERP_SLOT && priv->rf->conf_erp)
-               priv->rf->conf_erp(dev, info);
+       if (changed & BSS_CHANGED_BASIC_RATES)
+               rtl8180_conf_basic_rates(dev, info->basic_rates);
+
+       if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_ERP_PREAMBLE)) {
+
+               /* when preamble changes, acktime duration changes, and erp must
+                * be recalculated. ACK time is calculated at lowest rate.
+                * Since mac80211 include SIFS time we remove it (-10)
+                */
+               priv->ack_time =
+                       le16_to_cpu(ieee80211_generic_frame_duration(dev,
+                                       priv->vif,
+                                       IEEE80211_BAND_2GHZ, 10,
+                                       &priv->rates[0])) - 10;
+
+               rtl8180_conf_erp(dev, info);
+       }
 
        if (changed & BSS_CHANGED_BEACON_ENABLED)
                vif_priv->enable_beacon = info->enable_beacon;
@@ -863,6 +1167,7 @@ static const struct ieee80211_ops rtl8180_ops = {
        .remove_interface       = rtl8180_remove_interface,
        .config                 = rtl8180_config,
        .bss_info_changed       = rtl8180_bss_info_changed,
+       .conf_tx                = rtl8180_conf_tx,
        .prepare_multicast      = rtl8180_prepare_multicast,
        .configure_filter       = rtl8180_configure_filter,
        .get_tsf                = rtl8180_get_tsf,
@@ -870,8 +1175,7 @@ static const struct ieee80211_ops rtl8180_ops = {
 
 static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
 {
-       struct ieee80211_hw *dev = eeprom->data;
-       struct rtl8180_priv *priv = dev->priv;
+       struct rtl8180_priv *priv = eeprom->data;
        u8 reg = rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
 
        eeprom->reg_data_in = reg & RTL818X_EEPROM_CMD_WRITE;
@@ -882,8 +1186,7 @@ static void rtl8180_eeprom_register_read(struct eeprom_93cx6 *eeprom)
 
 static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
 {
-       struct ieee80211_hw *dev = eeprom->data;
-       struct rtl8180_priv *priv = dev->priv;
+       struct rtl8180_priv *priv = eeprom->data;
        u8 reg = 2 << 6;
 
        if (eeprom->reg_data_in)
@@ -900,6 +1203,67 @@ static void rtl8180_eeprom_register_write(struct eeprom_93cx6 *eeprom)
        udelay(10);
 }
 
+static void rtl8180_eeprom_read(struct rtl8180_priv *priv)
+{
+       struct eeprom_93cx6 eeprom;
+       int eeprom_cck_table_adr;
+       u16 eeprom_val;
+       int i;
+
+       eeprom.data = priv;
+       eeprom.register_read = rtl8180_eeprom_register_read;
+       eeprom.register_write = rtl8180_eeprom_register_write;
+       if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
+               eeprom.width = PCI_EEPROM_WIDTH_93C66;
+       else
+               eeprom.width = PCI_EEPROM_WIDTH_93C46;
+
+       rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
+                       RTL818X_EEPROM_CMD_PROGRAM);
+       rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
+       udelay(10);
+
+       eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
+       eeprom_val &= 0xFF;
+       priv->rf_type = eeprom_val;
+
+       eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
+       priv->csthreshold = eeprom_val >> 8;
+
+       eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)priv->mac_addr, 3);
+
+       eeprom_cck_table_adr = 0x10;
+
+       /* CCK TX power */
+       for (i = 0; i < 14; i += 2) {
+               u16 txpwr;
+               eeprom_93cx6_read(&eeprom, eeprom_cck_table_adr + (i >> 1),
+                               &txpwr);
+               priv->channels[i].hw_value = txpwr & 0xFF;
+               priv->channels[i + 1].hw_value = txpwr >> 8;
+       }
+
+       /* OFDM TX power */
+       if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
+               for (i = 0; i < 14; i += 2) {
+                       u16 txpwr;
+                       eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
+                       priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
+                       priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
+               }
+       }
+
+       if (priv->chip_family == RTL818X_CHIP_FAMILY_RTL8180) {
+               __le32 anaparam;
+               eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
+               priv->anaparam = le32_to_cpu(anaparam);
+               eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
+       }
+
+       rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD,
+                       RTL818X_EEPROM_CMD_NORMAL);
+}
+
 static int rtl8180_probe(struct pci_dev *pdev,
                                   const struct pci_device_id *id)
 {
@@ -907,12 +1271,9 @@ static int rtl8180_probe(struct pci_dev *pdev,
        struct rtl8180_priv *priv;
        unsigned long mem_addr, mem_len;
        unsigned int io_addr, io_len;
-       int err, i;
-       struct eeprom_93cx6 eeprom;
+       int err;
        const char *chip_name, *rf_name = NULL;
        u32 reg;
-       u16 eeprom_val;
-       u8 mac_addr[ETH_ALEN];
 
        err = pci_enable_device(pdev);
        if (err) {
@@ -994,7 +1355,6 @@ static int rtl8180_probe(struct pci_dev *pdev,
        dev->vif_data_size = sizeof(struct rtl8180_vif);
        dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
                                        BIT(NL80211_IFTYPE_ADHOC);
-       dev->queues = 1;
        dev->max_signal = 65;
 
        reg = rtl818x_ioread32(priv, &priv->map->TX_CONF);
@@ -1002,15 +1362,22 @@ static int rtl8180_probe(struct pci_dev *pdev,
        switch (reg) {
        case RTL818X_TX_CONF_R8180_ABCD:
                chip_name = "RTL8180";
+               priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
                break;
+
        case RTL818X_TX_CONF_R8180_F:
                chip_name = "RTL8180vF";
+               priv->chip_family = RTL818X_CHIP_FAMILY_RTL8180;
                break;
+
        case RTL818X_TX_CONF_R8185_ABC:
                chip_name = "RTL8185";
+               priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
                break;
+
        case RTL818X_TX_CONF_R8185_D:
                chip_name = "RTL8185vD";
+               priv->chip_family = RTL818X_CHIP_FAMILY_RTL8185;
                break;
        default:
                printk(KERN_ERR "%s (rtl8180): Unknown chip! (0x%x)\n",
@@ -1018,27 +1385,23 @@ static int rtl8180_probe(struct pci_dev *pdev,
                goto err_iounmap;
        }
 
-       priv->r8185 = reg & RTL818X_TX_CONF_R8185_ABC;
-       if (priv->r8185) {
+       /* we declare to MAC80211 all the queues except for beacon queue
+        * that will be eventually handled by DRV.
+        * TX rings are arranged in such a way that lower is the IDX,
+        * higher is the priority, in order to achieve direct mapping
+        * with mac80211, however the beacon queue is an exception and it
+        * is mapped on the highst tx ring IDX.
+        */
+       dev->queues = RTL8180_NR_TX_QUEUES - 1;
+
+       if (priv->chip_family != RTL818X_CHIP_FAMILY_RTL8180) {
                priv->band.n_bitrates = ARRAY_SIZE(rtl818x_rates);
                pci_try_set_mwi(pdev);
        }
 
-       eeprom.data = dev;
-       eeprom.register_read = rtl8180_eeprom_register_read;
-       eeprom.register_write = rtl8180_eeprom_register_write;
-       if (rtl818x_ioread32(priv, &priv->map->RX_CONF) & (1 << 6))
-               eeprom.width = PCI_EEPROM_WIDTH_93C66;
-       else
-               eeprom.width = PCI_EEPROM_WIDTH_93C46;
+       rtl8180_eeprom_read(priv);
 
-       rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_PROGRAM);
-       rtl818x_ioread8(priv, &priv->map->EEPROM_CMD);
-       udelay(10);
-
-       eeprom_93cx6_read(&eeprom, 0x06, &eeprom_val);
-       eeprom_val &= 0xFF;
-       switch (eeprom_val) {
+       switch (priv->rf_type) {
        case 1: rf_name = "Intersil";
                break;
        case 2: rf_name = "RFMD";
@@ -1056,7 +1419,7 @@ static int rtl8180_probe(struct pci_dev *pdev,
                break;
        default:
                printk(KERN_ERR "%s (rtl8180): Unknown RF! (0x%x)\n",
-                      pci_name(pdev), eeprom_val);
+                      pci_name(pdev), priv->rf_type);
                goto err_iounmap;
        }
 
@@ -1066,42 +1429,12 @@ static int rtl8180_probe(struct pci_dev *pdev,
                goto err_iounmap;
        }
 
-       eeprom_93cx6_read(&eeprom, 0x17, &eeprom_val);
-       priv->csthreshold = eeprom_val >> 8;
-       if (!priv->r8185) {
-               __le32 anaparam;
-               eeprom_93cx6_multiread(&eeprom, 0xD, (__le16 *)&anaparam, 2);
-               priv->anaparam = le32_to_cpu(anaparam);
-               eeprom_93cx6_read(&eeprom, 0x19, &priv->rfparam);
-       }
-
-       eeprom_93cx6_multiread(&eeprom, 0x7, (__le16 *)mac_addr, 3);
-       if (!is_valid_ether_addr(mac_addr)) {
+       if (!is_valid_ether_addr(priv->mac_addr)) {
                printk(KERN_WARNING "%s (rtl8180): Invalid hwaddr! Using"
                       " randomly generated MAC addr\n", pci_name(pdev));
-               eth_random_addr(mac_addr);
-       }
-       SET_IEEE80211_PERM_ADDR(dev, mac_addr);
-
-       /* CCK TX power */
-       for (i = 0; i < 14; i += 2) {
-               u16 txpwr;
-               eeprom_93cx6_read(&eeprom, 0x10 + (i >> 1), &txpwr);
-               priv->channels[i].hw_value = txpwr & 0xFF;
-               priv->channels[i + 1].hw_value = txpwr >> 8;
+               eth_random_addr(priv->mac_addr);
        }
-
-       /* OFDM TX power */
-       if (priv->r8185) {
-               for (i = 0; i < 14; i += 2) {
-                       u16 txpwr;
-                       eeprom_93cx6_read(&eeprom, 0x20 + (i >> 1), &txpwr);
-                       priv->channels[i].hw_value |= (txpwr & 0xFF) << 8;
-                       priv->channels[i + 1].hw_value |= txpwr & 0xFF00;
-               }
-       }
-
-       rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_NORMAL);
+       SET_IEEE80211_PERM_ADDR(dev, priv->mac_addr);
 
        spin_lock_init(&priv->lock);
 
@@ -1113,12 +1446,12 @@ static int rtl8180_probe(struct pci_dev *pdev,
        }
 
        wiphy_info(dev->wiphy, "hwaddr %pm, %s + %s\n",
-                  mac_addr, chip_name, priv->rf->name);
+                  priv->mac_addr, chip_name, priv->rf->name);
 
        return 0;
 
  err_iounmap:
-       iounmap(priv->map);
+       pci_iounmap(pdev, priv->map);
 
  err_free_dev:
        ieee80211_free_hw(dev);
This page took 0.05914 seconds and 5 git commands to generate.