Add guidelines for commit messages
[linux-libre-firmware.git] / isci / create_fw.c
1 /*
2  * This file is provided under a dual BSD/GPLv2 license.  When using or
3  * redistributing this file, you may do so under either license.
4  *
5  * GPL LICENSE SUMMARY
6  *
7  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  *
24  * BSD LICENSE
25  *
26  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  *
33  *   * Redistributions of source code must retain the above copyright
34  *     notice, this list of conditions and the following disclaimer.
35  *   * Redistributions in binary form must reproduce the above copyright
36  *     notice, this list of conditions and the following disclaimer in
37  *     the documentation and/or other materials provided with the
38  *     distribution.
39  *   * Neither the name of Intel Corporation nor the names of its
40  *     contributors may be used to endorse or promote products derived
41  *     from this software without specific prior written permission.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54  */
55
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <fcntl.h>
62 #include <string.h>
63 #include <errno.h>
64 #include <asm/types.h>
65 #include <strings.h>
66 #include <stdint.h>
67
68 #include "create_fw.h"
69
70 int write_blob(struct isci_orom *isci_orom)
71 {
72         FILE *fd;
73         int err;
74         size_t count;
75
76         fd = fopen(blob_name, "w+");
77         if (!fd) {
78                 perror("Open file for write failed");
79                 fclose(fd);
80                 return -EIO;
81         }
82
83         count = fwrite(isci_orom, sizeof(struct isci_orom), 1, fd);
84         if (count != 1) {
85                 perror("Write data failed");
86                 fclose(fd);
87                 return -EIO;
88         }
89
90         fclose(fd);
91
92         return 0;
93 }
94
95 void set_binary_values(struct isci_orom *isci_orom)
96 {
97         int c, phy_idx, port_idx;
98
99         /* setting OROM signature */
100         strncpy(isci_orom->hdr.signature, sig, strlen(sig));
101         isci_orom->hdr.version = version;
102         isci_orom->hdr.total_block_length = sizeof(struct isci_orom);
103         isci_orom->hdr.hdr_length = sizeof(struct sci_bios_oem_param_block_hdr);
104         isci_orom->hdr.num_elements = num_elements;
105
106         for (c = 0; c < 2; c++) {
107                 struct sci_oem_params *ctrl = &isci_orom->ctrl[c];
108                 __u8 cable_selection_mask = 0;
109
110                 ctrl->controller.mode_type = mode_type;
111                 ctrl->controller.max_concurr_spin_up = max_num_concurrent_dev_spin_up;
112                 ctrl->controller.do_enable_ssc = enable_ssc;
113
114                 for (port_idx = 0; port_idx < SCI_MAX_PORTS; port_idx++)
115                         ctrl->ports[port_idx].phy_mask = phy_mask[c][port_idx];
116
117                 for (phy_idx = 0; phy_idx < SCI_MAX_PHYS; phy_idx++) {
118                         struct sci_phy_oem_params *phy = &ctrl->phys[phy_idx];
119                         __u8 cable_phy = cable_selection[c][phy_idx];
120
121                         phy->sas_address.high = sas_addr[c][phy_idx] >> 32;
122                         phy->sas_address.low = sas_addr[c][phy_idx];
123
124                         phy->afe_tx_amp_control0 = afe_tx_amp_control0;
125                         phy->afe_tx_amp_control1 = afe_tx_amp_control1;
126                         phy->afe_tx_amp_control2 = afe_tx_amp_control2;
127                         phy->afe_tx_amp_control3 = afe_tx_amp_control3;
128
129                         cable_selection_mask |= (cable_phy & 1) << phy_idx;
130                         cable_selection_mask |= (cable_phy & 2) << (phy_idx + 3);
131                 }
132                 ctrl->controller.cable_selection_mask = cable_selection_mask;
133         }
134 }
135
136 int main(void)
137 {
138         int err;
139         struct isci_orom *isci_orom;
140
141         isci_orom = malloc(sizeof(struct isci_orom));
142         memset(isci_orom, 0, sizeof(struct isci_orom));
143
144         set_binary_values(isci_orom);
145
146         err = write_blob(isci_orom);
147         if (err < 0) {
148                 free(isci_orom);
149                 return err;
150         }
151
152         free(isci_orom);
153         return 0;
154 }