diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2007-09-30 20:55:51 -0400 |
---|---|---|
committer | David S. Miller <davem@sunset.davemloft.net> | 2007-10-10 19:53:57 -0400 |
commit | b3448b0bde5f1a858397fe791f76632e978a1dc8 (patch) | |
tree | 930bd73182f12eb37c2f14121556b1c6ff67d6bd /drivers/net/bnx2.c | |
parent | 89e536a190f90d038bae7905a0c582cb7089b739 (diff) |
[BNX2]: factor out gzip unpacker
This patch modifies gzip unpacking code in bnx2 driver so that
it does not depend on bnx2 internals. I will move this code
out of the driver and into zlib in follow-on patch.
It can be useful in other drivers which need to store firmwares
or any other relatively big binary blobs - fonts, cursor bitmaps,
whatever.
Patch is run tested by Michael Chan (driver author).
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Acked-by: Michael Chan <mchan@broadcom.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'drivers/net/bnx2.c')
-rw-r--r-- | drivers/net/bnx2.c | 160 |
1 files changed, 58 insertions, 102 deletions
diff --git a/drivers/net/bnx2.c b/drivers/net/bnx2.c index 57f7d994c9c1..73d4a579790c 100644 --- a/drivers/net/bnx2.c +++ b/drivers/net/bnx2.c | |||
@@ -52,6 +52,8 @@ | |||
52 | #include "bnx2_fw.h" | 52 | #include "bnx2_fw.h" |
53 | #include "bnx2_fw2.h" | 53 | #include "bnx2_fw2.h" |
54 | 54 | ||
55 | #define FW_BUF_SIZE 0x8000 | ||
56 | |||
55 | #define DRV_MODULE_NAME "bnx2" | 57 | #define DRV_MODULE_NAME "bnx2" |
56 | #define PFX DRV_MODULE_NAME ": " | 58 | #define PFX DRV_MODULE_NAME ": " |
57 | #define DRV_MODULE_VERSION "1.6.5" | 59 | #define DRV_MODULE_VERSION "1.6.5" |
@@ -2759,89 +2761,45 @@ bnx2_set_rx_mode(struct net_device *dev) | |||
2759 | spin_unlock_bh(&bp->phy_lock); | 2761 | spin_unlock_bh(&bp->phy_lock); |
2760 | } | 2762 | } |
2761 | 2763 | ||
2762 | #define FW_BUF_SIZE 0x8000 | 2764 | /* To be moved to generic lib/ */ |
2763 | |||
2764 | static int | 2765 | static int |
2765 | bnx2_gunzip_init(struct bnx2 *bp) | 2766 | bnx2_gunzip(void *gunzip_buf, unsigned sz, u8 *zbuf, int len) |
2766 | { | 2767 | { |
2767 | if ((bp->gunzip_buf = vmalloc(FW_BUF_SIZE)) == NULL) | 2768 | struct z_stream_s *strm; |
2768 | goto gunzip_nomem1; | 2769 | int rc; |
2769 | 2770 | ||
2770 | if ((bp->strm = kmalloc(sizeof(*bp->strm), GFP_KERNEL)) == NULL) | 2771 | /* gzip header (1f,8b,08... 10 bytes total + possible asciz filename) |
2771 | goto gunzip_nomem2; | 2772 | * is stripped */ |
2772 | 2773 | ||
2773 | bp->strm->workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL); | 2774 | rc = -ENOMEM; |
2774 | if (bp->strm->workspace == NULL) | 2775 | strm = kmalloc(sizeof(*strm), GFP_KERNEL); |
2776 | if (strm == NULL) | ||
2777 | goto gunzip_nomem2; | ||
2778 | strm->workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL); | ||
2779 | if (strm->workspace == NULL) | ||
2775 | goto gunzip_nomem3; | 2780 | goto gunzip_nomem3; |
2776 | 2781 | ||
2777 | return 0; | 2782 | strm->next_in = zbuf; |
2783 | strm->avail_in = len; | ||
2784 | strm->next_out = gunzip_buf; | ||
2785 | strm->avail_out = sz; | ||
2786 | |||
2787 | rc = zlib_inflateInit2(strm, -MAX_WBITS); | ||
2788 | if (rc == Z_OK) { | ||
2789 | rc = zlib_inflate(strm, Z_FINISH); | ||
2790 | /* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */ | ||
2791 | if (rc == Z_STREAM_END) | ||
2792 | rc = sz - strm->avail_out; | ||
2793 | else | ||
2794 | rc = -EINVAL; | ||
2795 | zlib_inflateEnd(strm); | ||
2796 | } else | ||
2797 | rc = -EINVAL; | ||
2778 | 2798 | ||
2799 | kfree(strm->workspace); | ||
2779 | gunzip_nomem3: | 2800 | gunzip_nomem3: |
2780 | kfree(bp->strm); | 2801 | kfree(strm); |
2781 | bp->strm = NULL; | ||
2782 | |||
2783 | gunzip_nomem2: | 2802 | gunzip_nomem2: |
2784 | vfree(bp->gunzip_buf); | ||
2785 | bp->gunzip_buf = NULL; | ||
2786 | |||
2787 | gunzip_nomem1: | ||
2788 | printk(KERN_ERR PFX "%s: Cannot allocate firmware buffer for " | ||
2789 | "uncompression.\n", bp->dev->name); | ||
2790 | return -ENOMEM; | ||
2791 | } | ||
2792 | |||
2793 | static void | ||
2794 | bnx2_gunzip_end(struct bnx2 *bp) | ||
2795 | { | ||
2796 | kfree(bp->strm->workspace); | ||
2797 | |||
2798 | kfree(bp->strm); | ||
2799 | bp->strm = NULL; | ||
2800 | |||
2801 | if (bp->gunzip_buf) { | ||
2802 | vfree(bp->gunzip_buf); | ||
2803 | bp->gunzip_buf = NULL; | ||
2804 | } | ||
2805 | } | ||
2806 | |||
2807 | static int | ||
2808 | bnx2_gunzip(struct bnx2 *bp, u8 *zbuf, int len, void **outbuf, int *outlen) | ||
2809 | { | ||
2810 | int n, rc; | ||
2811 | |||
2812 | /* check gzip header */ | ||
2813 | if ((zbuf[0] != 0x1f) || (zbuf[1] != 0x8b) || (zbuf[2] != Z_DEFLATED)) | ||
2814 | return -EINVAL; | ||
2815 | |||
2816 | n = 10; | ||
2817 | |||
2818 | #define FNAME 0x8 | ||
2819 | if (zbuf[3] & FNAME) | ||
2820 | while ((zbuf[n++] != 0) && (n < len)); | ||
2821 | |||
2822 | bp->strm->next_in = zbuf + n; | ||
2823 | bp->strm->avail_in = len - n; | ||
2824 | bp->strm->next_out = bp->gunzip_buf; | ||
2825 | bp->strm->avail_out = FW_BUF_SIZE; | ||
2826 | |||
2827 | rc = zlib_inflateInit2(bp->strm, -MAX_WBITS); | ||
2828 | if (rc != Z_OK) | ||
2829 | return rc; | ||
2830 | |||
2831 | rc = zlib_inflate(bp->strm, Z_FINISH); | ||
2832 | |||
2833 | *outlen = FW_BUF_SIZE - bp->strm->avail_out; | ||
2834 | *outbuf = bp->gunzip_buf; | ||
2835 | |||
2836 | if ((rc != Z_OK) && (rc != Z_STREAM_END)) | ||
2837 | printk(KERN_ERR PFX "%s: Firmware decompression error: %s\n", | ||
2838 | bp->dev->name, bp->strm->msg); | ||
2839 | |||
2840 | zlib_inflateEnd(bp->strm); | ||
2841 | |||
2842 | if (rc == Z_STREAM_END) | ||
2843 | return 0; | ||
2844 | |||
2845 | return rc; | 2803 | return rc; |
2846 | } | 2804 | } |
2847 | 2805 | ||
@@ -2894,22 +2852,21 @@ load_cpu_fw(struct bnx2 *bp, struct cpu_reg *cpu_reg, struct fw_info *fw) | |||
2894 | /* Load the Text area. */ | 2852 | /* Load the Text area. */ |
2895 | offset = cpu_reg->spad_base + (fw->text_addr - cpu_reg->mips_view_base); | 2853 | offset = cpu_reg->spad_base + (fw->text_addr - cpu_reg->mips_view_base); |
2896 | if (fw->gz_text) { | 2854 | if (fw->gz_text) { |
2897 | u32 text_len; | 2855 | u32 *text; |
2898 | void *text; | ||
2899 | |||
2900 | rc = bnx2_gunzip(bp, fw->gz_text, fw->gz_text_len, &text, | ||
2901 | &text_len); | ||
2902 | if (rc) | ||
2903 | return rc; | ||
2904 | |||
2905 | fw->text = text; | ||
2906 | } | ||
2907 | if (fw->gz_text) { | ||
2908 | int j; | 2856 | int j; |
2909 | 2857 | ||
2858 | text = vmalloc(FW_BUF_SIZE); | ||
2859 | if (!text) | ||
2860 | return -ENOMEM; | ||
2861 | rc = bnx2_gunzip(text, FW_BUF_SIZE, fw->gz_text, fw->gz_text_len); | ||
2862 | if (rc < 0) { | ||
2863 | vfree(text); | ||
2864 | return rc; | ||
2865 | } | ||
2910 | for (j = 0; j < (fw->text_len / 4); j++, offset += 4) { | 2866 | for (j = 0; j < (fw->text_len / 4); j++, offset += 4) { |
2911 | REG_WR_IND(bp, offset, cpu_to_le32(fw->text[j])); | 2867 | REG_WR_IND(bp, offset, cpu_to_le32(text[j])); |
2912 | } | 2868 | } |
2869 | vfree(text); | ||
2913 | } | 2870 | } |
2914 | 2871 | ||
2915 | /* Load the Data area. */ | 2872 | /* Load the Data area. */ |
@@ -2971,27 +2928,27 @@ bnx2_init_cpus(struct bnx2 *bp) | |||
2971 | { | 2928 | { |
2972 | struct cpu_reg cpu_reg; | 2929 | struct cpu_reg cpu_reg; |
2973 | struct fw_info *fw; | 2930 | struct fw_info *fw; |
2974 | int rc = 0; | 2931 | int rc; |
2975 | void *text; | 2932 | void *text; |
2976 | u32 text_len; | ||
2977 | |||
2978 | if ((rc = bnx2_gunzip_init(bp)) != 0) | ||
2979 | return rc; | ||
2980 | 2933 | ||
2981 | /* Initialize the RV2P processor. */ | 2934 | /* Initialize the RV2P processor. */ |
2982 | rc = bnx2_gunzip(bp, bnx2_rv2p_proc1, sizeof(bnx2_rv2p_proc1), &text, | 2935 | text = vmalloc(FW_BUF_SIZE); |
2983 | &text_len); | 2936 | if (!text) |
2984 | if (rc) | 2937 | return -ENOMEM; |
2938 | rc = bnx2_gunzip(text, FW_BUF_SIZE, bnx2_rv2p_proc1, sizeof(bnx2_rv2p_proc1)); | ||
2939 | if (rc < 0) { | ||
2940 | vfree(text); | ||
2985 | goto init_cpu_err; | 2941 | goto init_cpu_err; |
2942 | } | ||
2943 | load_rv2p_fw(bp, text, rc /* == len */, RV2P_PROC1); | ||
2986 | 2944 | ||
2987 | load_rv2p_fw(bp, text, text_len, RV2P_PROC1); | 2945 | rc = bnx2_gunzip(text, FW_BUF_SIZE, bnx2_rv2p_proc2, sizeof(bnx2_rv2p_proc2)); |
2988 | 2946 | if (rc < 0) { | |
2989 | rc = bnx2_gunzip(bp, bnx2_rv2p_proc2, sizeof(bnx2_rv2p_proc2), &text, | 2947 | vfree(text); |
2990 | &text_len); | ||
2991 | if (rc) | ||
2992 | goto init_cpu_err; | 2948 | goto init_cpu_err; |
2993 | 2949 | } | |
2994 | load_rv2p_fw(bp, text, text_len, RV2P_PROC2); | 2950 | load_rv2p_fw(bp, text, rc /* == len */, RV2P_PROC2); |
2951 | vfree(text); | ||
2995 | 2952 | ||
2996 | /* Initialize the RX Processor. */ | 2953 | /* Initialize the RX Processor. */ |
2997 | cpu_reg.mode = BNX2_RXP_CPU_MODE; | 2954 | cpu_reg.mode = BNX2_RXP_CPU_MODE; |
@@ -3107,7 +3064,6 @@ bnx2_init_cpus(struct bnx2 *bp) | |||
3107 | goto init_cpu_err; | 3064 | goto init_cpu_err; |
3108 | } | 3065 | } |
3109 | init_cpu_err: | 3066 | init_cpu_err: |
3110 | bnx2_gunzip_end(bp); | ||
3111 | return rc; | 3067 | return rc; |
3112 | } | 3068 | } |
3113 | 3069 | ||