xref: /linux/drivers/net/ethernet/brocade/bna/cna_fwimg.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Linux network driver for QLogic BR-series Converged Network Adapter.
4  */
5 /*
6  * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
7  * Copyright (c) 2014-2015 QLogic Corporation
8  * All rights reserved
9  * www.qlogic.com
10  */
11 #include <linux/firmware.h>
12 #include "bnad.h"
13 #include "bfi.h"
14 #include "cna.h"
15 
16 const struct firmware *bfi_fw;
17 static u32 *bfi_image_ct_cna, *bfi_image_ct2_cna;
18 static u32 bfi_image_ct_cna_size, bfi_image_ct2_cna_size;
19 
20 static u32 *
cna_read_firmware(struct pci_dev * pdev,u32 ** bfi_image,u32 * bfi_image_size,char * fw_name)21 cna_read_firmware(struct pci_dev *pdev, u32 **bfi_image,
22 			u32 *bfi_image_size, char *fw_name)
23 {
24 	const struct firmware *fw;
25 	u32 n;
26 
27 	if (request_firmware(&fw, fw_name, &pdev->dev)) {
28 		dev_alert(&pdev->dev, "can't load firmware %s\n", fw_name);
29 		goto error;
30 	}
31 
32 	*bfi_image = (u32 *)fw->data;
33 	*bfi_image_size = fw->size/sizeof(u32);
34 	bfi_fw = fw;
35 
36 	/* Convert loaded firmware to host order as it is stored in file
37 	 * as sequence of LE32 integers.
38 	 */
39 	for (n = 0; n < *bfi_image_size; n++)
40 		le32_to_cpus(*bfi_image + n);
41 
42 	return *bfi_image;
43 error:
44 	return NULL;
45 }
46 
47 u32 *
cna_get_firmware_buf(struct pci_dev * pdev)48 cna_get_firmware_buf(struct pci_dev *pdev)
49 {
50 	if (pdev->device == BFA_PCI_DEVICE_ID_CT2) {
51 		if (bfi_image_ct2_cna_size == 0)
52 			cna_read_firmware(pdev, &bfi_image_ct2_cna,
53 				&bfi_image_ct2_cna_size, CNA_FW_FILE_CT2);
54 		return bfi_image_ct2_cna;
55 	} else if (bfa_asic_id_ct(pdev->device)) {
56 		if (bfi_image_ct_cna_size == 0)
57 			cna_read_firmware(pdev, &bfi_image_ct_cna,
58 				&bfi_image_ct_cna_size, CNA_FW_FILE_CT);
59 		return bfi_image_ct_cna;
60 	}
61 
62 	return NULL;
63 }
64 
65 u32 *
bfa_cb_image_get_chunk(enum bfi_asic_gen asic_gen,u32 off)66 bfa_cb_image_get_chunk(enum bfi_asic_gen asic_gen, u32 off)
67 {
68 	switch (asic_gen) {
69 	case BFI_ASIC_GEN_CT:
70 		return (bfi_image_ct_cna + off);
71 	case BFI_ASIC_GEN_CT2:
72 		return (bfi_image_ct2_cna + off);
73 	default:
74 		return NULL;
75 	}
76 }
77 
78 u32
bfa_cb_image_get_size(enum bfi_asic_gen asic_gen)79 bfa_cb_image_get_size(enum bfi_asic_gen asic_gen)
80 {
81 	switch (asic_gen) {
82 	case BFI_ASIC_GEN_CT:
83 		return bfi_image_ct_cna_size;
84 	case BFI_ASIC_GEN_CT2:
85 		return bfi_image_ct2_cna_size;
86 	default:
87 		return 0;
88 	}
89 }
90