Commit 014da2a5 authored by David S. Miller's avatar David S. Miller

Merge branch 'r8169-make-firmware-handling-code-ready-to-be-factored-out'

Heiner Kallweit says:

====================
r8169: make firmware handling code ready to be factored out

This series contains the final steps to make firmware handling code
ready to be factored out into a separate source code file.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 6e36d77c 47ad5931
...@@ -677,6 +677,8 @@ struct rtl8169_private { ...@@ -677,6 +677,8 @@ struct rtl8169_private {
rtl_fw_write_t mac_mcu_write; rtl_fw_write_t mac_mcu_write;
rtl_fw_read_t mac_mcu_read; rtl_fw_read_t mac_mcu_read;
const struct firmware *fw; const struct firmware *fw;
const char *fw_name;
struct device *dev;
#define RTL_VER_SIZE 32 #define RTL_VER_SIZE 32
...@@ -2298,19 +2300,21 @@ static void __rtl_writephy_batch(struct rtl8169_private *tp, ...@@ -2298,19 +2300,21 @@ static void __rtl_writephy_batch(struct rtl8169_private *tp,
#define rtl_writephy_batch(tp, a) __rtl_writephy_batch(tp, a, ARRAY_SIZE(a)) #define rtl_writephy_batch(tp, a) __rtl_writephy_batch(tp, a, ARRAY_SIZE(a))
#define PHY_READ 0x00000000 enum rtl_fw_opcode {
#define PHY_DATA_OR 0x10000000 PHY_READ = 0x0,
#define PHY_DATA_AND 0x20000000 PHY_DATA_OR = 0x1,
#define PHY_BJMPN 0x30000000 PHY_DATA_AND = 0x2,
#define PHY_MDIO_CHG 0x40000000 PHY_BJMPN = 0x3,
#define PHY_CLEAR_READCOUNT 0x70000000 PHY_MDIO_CHG = 0x4,
#define PHY_WRITE 0x80000000 PHY_CLEAR_READCOUNT = 0x7,
#define PHY_READCOUNT_EQ_SKIP 0x90000000 PHY_WRITE = 0x8,
#define PHY_COMP_EQ_SKIPN 0xa0000000 PHY_READCOUNT_EQ_SKIP = 0x9,
#define PHY_COMP_NEQ_SKIPN 0xb0000000 PHY_COMP_EQ_SKIPN = 0xa,
#define PHY_WRITE_PREVIOUS 0xc0000000 PHY_COMP_NEQ_SKIPN = 0xb,
#define PHY_SKIPN 0xd0000000 PHY_WRITE_PREVIOUS = 0xc,
#define PHY_DELAY_MS 0xe0000000 PHY_SKIPN = 0xd,
PHY_DELAY_MS = 0xe,
};
struct fw_info { struct fw_info {
u32 magic; u32 magic;
...@@ -2322,7 +2326,7 @@ struct fw_info { ...@@ -2322,7 +2326,7 @@ struct fw_info {
#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code)) #define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw) static bool rtl_fw_format_ok(struct rtl_fw *rtl_fw)
{ {
const struct firmware *fw = rtl_fw->fw; const struct firmware *fw = rtl_fw->fw;
struct fw_info *fw_info = (struct fw_info *)fw->data; struct fw_info *fw_info = (struct fw_info *)fw->data;
...@@ -2359,7 +2363,7 @@ static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw) ...@@ -2359,7 +2363,7 @@ static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
if (fw->size % FW_OPCODE_SIZE) if (fw->size % FW_OPCODE_SIZE)
return false; return false;
strscpy(rtl_fw->version, tp->fw_name, RTL_VER_SIZE); strscpy(rtl_fw->version, rtl_fw->fw_name, RTL_VER_SIZE);
pa->code = (__le32 *)fw->data; pa->code = (__le32 *)fw->data;
pa->size = fw->size / FW_OPCODE_SIZE; pa->size = fw->size / FW_OPCODE_SIZE;
...@@ -2368,17 +2372,16 @@ static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw) ...@@ -2368,17 +2372,16 @@ static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
return true; return true;
} }
static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev, static bool rtl_fw_data_ok(struct rtl_fw *rtl_fw)
struct rtl_fw_phy_action *pa)
{ {
bool rc = false; struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
size_t index; size_t index;
for (index = 0; index < pa->size; index++) { for (index = 0; index < pa->size; index++) {
u32 action = le32_to_cpu(pa->code[index]); u32 action = le32_to_cpu(pa->code[index]);
u32 regno = (action & 0x0fff0000) >> 16; u32 regno = (action & 0x0fff0000) >> 16;
switch(action & 0xf0000000) { switch (action >> 28) {
case PHY_READ: case PHY_READ:
case PHY_DATA_OR: case PHY_DATA_OR:
case PHY_DATA_AND: case PHY_DATA_AND:
...@@ -2390,54 +2393,30 @@ static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev, ...@@ -2390,54 +2393,30 @@ static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
break; break;
case PHY_BJMPN: case PHY_BJMPN:
if (regno > index) { if (regno > index)
netif_err(tp, ifup, tp->dev,
"Out of range of firmware\n");
goto out; goto out;
}
break; break;
case PHY_READCOUNT_EQ_SKIP: case PHY_READCOUNT_EQ_SKIP:
if (index + 2 >= pa->size) { if (index + 2 >= pa->size)
netif_err(tp, ifup, tp->dev,
"Out of range of firmware\n");
goto out; goto out;
}
break; break;
case PHY_COMP_EQ_SKIPN: case PHY_COMP_EQ_SKIPN:
case PHY_COMP_NEQ_SKIPN: case PHY_COMP_NEQ_SKIPN:
case PHY_SKIPN: case PHY_SKIPN:
if (index + 1 + regno >= pa->size) { if (index + 1 + regno >= pa->size)
netif_err(tp, ifup, tp->dev,
"Out of range of firmware\n");
goto out; goto out;
}
break; break;
default: default:
netif_err(tp, ifup, tp->dev, dev_err(rtl_fw->dev, "Invalid action 0x%08x\n", action);
"Invalid action 0x%08x\n", action); return false;
goto out;
}
} }
rc = true;
out:
return rc;
}
static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
{
struct net_device *dev = tp->dev;
int rc = -EINVAL;
if (!rtl_fw_format_ok(tp, rtl_fw)) {
netif_err(tp, ifup, dev, "invalid firmware\n");
goto out;
} }
if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action)) return true;
rc = 0;
out: out:
return rc; dev_err(rtl_fw->dev, "Out of range of firmware\n");
return false;
} }
static void rtl_fw_write_firmware(struct rtl8169_private *tp, static void rtl_fw_write_firmware(struct rtl8169_private *tp,
...@@ -2449,30 +2428,28 @@ static void rtl_fw_write_firmware(struct rtl8169_private *tp, ...@@ -2449,30 +2428,28 @@ static void rtl_fw_write_firmware(struct rtl8169_private *tp,
int predata = 0, count = 0; int predata = 0, count = 0;
size_t index; size_t index;
for (index = 0; index < pa->size; ) { for (index = 0; index < pa->size; index++) {
u32 action = le32_to_cpu(pa->code[index]); u32 action = le32_to_cpu(pa->code[index]);
u32 data = action & 0x0000ffff; u32 data = action & 0x0000ffff;
u32 regno = (action & 0x0fff0000) >> 16; u32 regno = (action & 0x0fff0000) >> 16;
enum rtl_fw_opcode opcode = action >> 28;
if (!action) if (!action)
break; break;
switch(action & 0xf0000000) { switch (opcode) {
case PHY_READ: case PHY_READ:
predata = fw_read(tp, regno); predata = fw_read(tp, regno);
count++; count++;
index++;
break; break;
case PHY_DATA_OR: case PHY_DATA_OR:
predata |= data; predata |= data;
index++;
break; break;
case PHY_DATA_AND: case PHY_DATA_AND:
predata &= data; predata &= data;
index++;
break; break;
case PHY_BJMPN: case PHY_BJMPN:
index -= regno; index -= (regno + 1);
break; break;
case PHY_MDIO_CHG: case PHY_MDIO_CHG:
if (data == 0) { if (data == 0) {
...@@ -2483,51 +2460,47 @@ static void rtl_fw_write_firmware(struct rtl8169_private *tp, ...@@ -2483,51 +2460,47 @@ static void rtl_fw_write_firmware(struct rtl8169_private *tp,
fw_read = rtl_fw->mac_mcu_read; fw_read = rtl_fw->mac_mcu_read;
} }
index++;
break; break;
case PHY_CLEAR_READCOUNT: case PHY_CLEAR_READCOUNT:
count = 0; count = 0;
index++;
break; break;
case PHY_WRITE: case PHY_WRITE:
fw_write(tp, regno, data); fw_write(tp, regno, data);
index++;
break; break;
case PHY_READCOUNT_EQ_SKIP: case PHY_READCOUNT_EQ_SKIP:
index += (count == data) ? 2 : 1; if (count == data)
index++;
break; break;
case PHY_COMP_EQ_SKIPN: case PHY_COMP_EQ_SKIPN:
if (predata == data) if (predata == data)
index += regno; index += regno;
index++;
break; break;
case PHY_COMP_NEQ_SKIPN: case PHY_COMP_NEQ_SKIPN:
if (predata != data) if (predata != data)
index += regno; index += regno;
index++;
break; break;
case PHY_WRITE_PREVIOUS: case PHY_WRITE_PREVIOUS:
fw_write(tp, regno, predata); fw_write(tp, regno, predata);
index++;
break; break;
case PHY_SKIPN: case PHY_SKIPN:
index += regno + 1; index += regno;
break; break;
case PHY_DELAY_MS: case PHY_DELAY_MS:
mdelay(data); mdelay(data);
index++;
break; break;
default:
BUG();
} }
} }
} }
static void rtl_fw_release_firmware(struct rtl_fw *rtl_fw)
{
release_firmware(rtl_fw->fw);
}
static void rtl_release_firmware(struct rtl8169_private *tp) static void rtl_release_firmware(struct rtl8169_private *tp)
{ {
if (tp->rtl_fw) { if (tp->rtl_fw) {
release_firmware(tp->rtl_fw->fw); rtl_fw_release_firmware(tp->rtl_fw);
kfree(tp->rtl_fw); kfree(tp->rtl_fw);
tp->rtl_fw = NULL; tp->rtl_fw = NULL;
} }
...@@ -4281,43 +4254,51 @@ static void rtl_hw_reset(struct rtl8169_private *tp) ...@@ -4281,43 +4254,51 @@ static void rtl_hw_reset(struct rtl8169_private *tp)
rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100); rtl_udelay_loop_wait_low(tp, &rtl_chipcmd_cond, 100, 100);
} }
static int rtl_fw_request_firmware(struct rtl_fw *rtl_fw)
{
int rc;
rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, rtl_fw->dev);
if (rc < 0)
goto out;
if (!rtl_fw_format_ok(rtl_fw) || !rtl_fw_data_ok(rtl_fw)) {
release_firmware(rtl_fw->fw);
goto out;
}
return 0;
out:
dev_err(rtl_fw->dev, "Unable to load firmware %s (%d)\n",
rtl_fw->fw_name, rc);
return rc;
}
static void rtl_request_firmware(struct rtl8169_private *tp) static void rtl_request_firmware(struct rtl8169_private *tp)
{ {
struct rtl_fw *rtl_fw; struct rtl_fw *rtl_fw;
int rc = -ENOMEM;
/* firmware loaded already or no firmware available */ /* firmware loaded already or no firmware available */
if (tp->rtl_fw || !tp->fw_name) if (tp->rtl_fw || !tp->fw_name)
return; return;
rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL); rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
if (!rtl_fw) if (!rtl_fw) {
goto err_warn; netif_warn(tp, ifup, tp->dev, "Unable to load firmware, out of memory\n");
return;
}
rtl_fw->phy_write = rtl_writephy; rtl_fw->phy_write = rtl_writephy;
rtl_fw->phy_read = rtl_readphy; rtl_fw->phy_read = rtl_readphy;
rtl_fw->mac_mcu_write = mac_mcu_write; rtl_fw->mac_mcu_write = mac_mcu_write;
rtl_fw->mac_mcu_read = mac_mcu_read; rtl_fw->mac_mcu_read = mac_mcu_read;
rtl_fw->fw_name = tp->fw_name;
rtl_fw->dev = tp_to_dev(tp);
rc = request_firmware(&rtl_fw->fw, tp->fw_name, tp_to_dev(tp)); if (rtl_fw_request_firmware(rtl_fw))
if (rc < 0)
goto err_free;
rc = rtl_check_firmware(tp, rtl_fw);
if (rc < 0)
goto err_release_firmware;
tp->rtl_fw = rtl_fw;
return;
err_release_firmware:
release_firmware(rtl_fw->fw);
err_free:
kfree(rtl_fw); kfree(rtl_fw);
err_warn: else
netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n", tp->rtl_fw = rtl_fw;
tp->fw_name, rc);
} }
static void rtl_rx_close(struct rtl8169_private *tp) static void rtl_rx_close(struct rtl8169_private *tp)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment