[SCSI] be2iscsi: Use start cid and number of cid and icd from FW
[safe/jmp/linux-2.6] / drivers / scsi / be2iscsi / be_mgmt.c
1 /**
2  * Copyright (C) 2005 - 2009 ServerEngines
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation.  The full GNU General
8  * Public License is included in this distribution in the file called COPYING.
9  *
10  * Written by: Jayamohan Kallickal (jayamohank@serverengines.com)
11  *
12  * Contact Information:
13  * linux-drivers@serverengines.com
14  *
15  * ServerEngines
16  * 209 N. Fair Oaks Ave
17  * Sunnyvale, CA 94085
18  *
19  */
20
21 #include "be_mgmt.h"
22 #include "be_iscsi.h"
23
24 unsigned char mgmt_get_fw_config(struct be_ctrl_info *ctrl,
25                                 struct beiscsi_hba *phba)
26 {
27         struct be_mcc_wrb *wrb = wrb_from_mbox(&ctrl->mbox_mem);
28         struct be_fw_cfg *req = embedded_payload(wrb);
29         int status = 0;
30
31         spin_lock(&ctrl->mbox_lock);
32         memset(wrb, 0, sizeof(*wrb));
33
34         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
35
36         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_COMMON,
37                            OPCODE_COMMON_QUERY_FIRMWARE_CONFIG, sizeof(*req));
38         status = be_mbox_notify(ctrl);
39         if (!status) {
40                 struct be_fw_cfg *pfw_cfg;
41                 pfw_cfg = req;
42                 phba->fw_config.phys_port = pfw_cfg->phys_port;
43                 phba->fw_config.iscsi_icd_start =
44                                         pfw_cfg->ulp[0].icd_base;
45                 phba->fw_config.iscsi_icd_count =
46                                         pfw_cfg->ulp[0].icd_count;
47                 phba->fw_config.iscsi_cid_start =
48                                         pfw_cfg->ulp[0].sq_base;
49                 phba->fw_config.iscsi_cid_count =
50                                         pfw_cfg->ulp[0].sq_count;
51                 if (phba->fw_config.iscsi_cid_count > (BE2_MAX_SESSIONS / 2)) {
52                         status = 1;
53                         shost_printk(KERN_WARNING, phba->shost,
54                              "FW reported MAX CXNS as %d \t"
55                              "Max Supported = %d. Failing to load \n",
56                                         phba->fw_config.iscsi_cid_count,
57                                         BE2_MAX_SESSIONS);
58                 }
59         } else {
60                 shost_printk(KERN_WARNING, phba->shost,
61                              "Failed in mgmt_get_fw_config \n");
62         }
63
64         spin_unlock(&ctrl->mbox_lock);
65         return status;
66 }
67
68 unsigned char mgmt_check_supported_fw(struct be_ctrl_info *ctrl,
69                                       struct beiscsi_hba *phba)
70 {
71         struct be_dma_mem nonemb_cmd;
72         struct be_mcc_wrb *wrb = wrb_from_mbox(&ctrl->mbox_mem);
73         struct be_mgmt_controller_attributes *req;
74         struct be_sge *sge = nonembedded_sgl(wrb);
75         int status = 0;
76
77         nonemb_cmd.va = pci_alloc_consistent(ctrl->pdev,
78                                 sizeof(struct be_mgmt_controller_attributes),
79                                 &nonemb_cmd.dma);
80         if (nonemb_cmd.va == NULL) {
81                 SE_DEBUG(DBG_LVL_1,
82                          "Failed to allocate memory for mgmt_check_supported_fw"
83                          "\n");
84                 return -1;
85         }
86         nonemb_cmd.size = sizeof(struct be_mgmt_controller_attributes);
87         req = nonemb_cmd.va;
88         spin_lock(&ctrl->mbox_lock);
89         memset(wrb, 0, sizeof(*wrb));
90         be_wrb_hdr_prepare(wrb, sizeof(*req), false, 1);
91         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_COMMON,
92                            OPCODE_COMMON_GET_CNTL_ATTRIBUTES, sizeof(*req));
93         sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd.dma));
94         sge->pa_lo = cpu_to_le32(nonemb_cmd.dma & 0xFFFFFFFF);
95         sge->len = cpu_to_le32(nonemb_cmd.size);
96         status = be_mbox_notify(ctrl);
97         if (!status) {
98                 struct be_mgmt_controller_attributes_resp *resp = nonemb_cmd.va;
99                 SE_DEBUG(DBG_LVL_8, "Firmware version of CMD: %s\n",
100                         resp->params.hba_attribs.flashrom_version_string);
101                 SE_DEBUG(DBG_LVL_8, "Firmware version is : %s\n",
102                         resp->params.hba_attribs.firmware_version_string);
103                 SE_DEBUG(DBG_LVL_8,
104                         "Developer Build, not performing version check...\n");
105                 phba->fw_config.iscsi_features =
106                                 resp->params.hba_attribs.iscsi_features;
107                 SE_DEBUG(DBG_LVL_8, " phba->fw_config.iscsi_features = %d\n",
108                                       phba->fw_config.iscsi_features);
109         } else
110                 SE_DEBUG(DBG_LVL_1, " Failed in mgmt_check_supported_fw\n");
111         spin_unlock(&ctrl->mbox_lock);
112         if (nonemb_cmd.va)
113                 pci_free_consistent(ctrl->pdev, nonemb_cmd.size,
114                                     nonemb_cmd.va, nonemb_cmd.dma);
115
116         return status;
117 }
118
119
120 unsigned char mgmt_epfw_cleanup(struct beiscsi_hba *phba, unsigned short chute)
121 {
122         struct be_ctrl_info *ctrl = &phba->ctrl;
123         struct be_mcc_wrb *wrb = wrb_from_mccq(phba);
124         struct iscsi_cleanup_req *req = embedded_payload(wrb);
125         int status = 0;
126
127         spin_lock(&ctrl->mbox_lock);
128         memset(wrb, 0, sizeof(*wrb));
129
130         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
131         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
132                            OPCODE_COMMON_ISCSI_CLEANUP, sizeof(*req));
133
134         req->chute = chute;
135         req->hdr_ring_id = 0;
136         req->data_ring_id = 0;
137
138         status =  be_mcc_notify_wait(phba);
139         if (status)
140                 shost_printk(KERN_WARNING, phba->shost,
141                              " mgmt_epfw_cleanup , FAILED\n");
142         spin_unlock(&ctrl->mbox_lock);
143         return status;
144 }
145
146 unsigned char mgmt_invalidate_icds(struct beiscsi_hba *phba,
147                                    unsigned int icd, unsigned int cid)
148 {
149         struct be_dma_mem nonemb_cmd;
150         struct be_ctrl_info *ctrl = &phba->ctrl;
151         struct be_mcc_wrb *wrb = wrb_from_mccq(phba);
152         struct be_sge *sge = nonembedded_sgl(wrb);
153         struct invalidate_commands_params_in *req;
154         int status = 0;
155
156         nonemb_cmd.va = pci_alloc_consistent(ctrl->pdev,
157                                 sizeof(struct invalidate_commands_params_in),
158                                 &nonemb_cmd.dma);
159         if (nonemb_cmd.va == NULL) {
160                 SE_DEBUG(DBG_LVL_1,
161                          "Failed to allocate memory for"
162                          "mgmt_invalidate_icds \n");
163                 return -1;
164         }
165         nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
166         req = nonemb_cmd.va;
167         spin_lock(&ctrl->mbox_lock);
168         memset(wrb, 0, sizeof(*wrb));
169
170         be_wrb_hdr_prepare(wrb, sizeof(*req), false, 1);
171         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
172                         OPCODE_COMMON_ISCSI_ERROR_RECOVERY_INVALIDATE_COMMANDS,
173                         sizeof(*req));
174         req->ref_handle = 0;
175         req->cleanup_type = CMD_ISCSI_COMMAND_INVALIDATE;
176         req->icd_count = 0;
177         req->table[req->icd_count].icd = icd;
178         req->table[req->icd_count].cid = cid;
179         sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd.dma));
180         sge->pa_lo = cpu_to_le32(nonemb_cmd.dma & 0xFFFFFFFF);
181         sge->len = cpu_to_le32(nonemb_cmd.size);
182
183         status = be_mcc_notify_wait(phba);
184         if (status)
185                 SE_DEBUG(DBG_LVL_1, "ICDS Invalidation Failed\n");
186         spin_unlock(&ctrl->mbox_lock);
187         if (nonemb_cmd.va)
188                 pci_free_consistent(ctrl->pdev, nonemb_cmd.size,
189                                     nonemb_cmd.va, nonemb_cmd.dma);
190         return status;
191 }
192
193 unsigned char mgmt_invalidate_connection(struct beiscsi_hba *phba,
194                                          struct beiscsi_endpoint *beiscsi_ep,
195                                          unsigned short cid,
196                                          unsigned short issue_reset,
197                                          unsigned short savecfg_flag)
198 {
199         struct be_ctrl_info *ctrl = &phba->ctrl;
200         struct be_mcc_wrb *wrb = wrb_from_mccq(phba);
201         struct iscsi_invalidate_connection_params_in *req =
202                                                 embedded_payload(wrb);
203         int status = 0;
204
205         spin_lock(&ctrl->mbox_lock);
206         memset(wrb, 0, sizeof(*wrb));
207
208         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
209         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
210                            OPCODE_ISCSI_INI_DRIVER_INVALIDATE_CONNECTION,
211                            sizeof(*req));
212         req->session_handle = beiscsi_ep->fw_handle;
213         req->cid = cid;
214         if (issue_reset)
215                 req->cleanup_type = CMD_ISCSI_CONNECTION_ISSUE_TCP_RST;
216         else
217                 req->cleanup_type = CMD_ISCSI_CONNECTION_INVALIDATE;
218         req->save_cfg = savecfg_flag;
219         status =  be_mcc_notify_wait(phba);
220         if (status)
221                 SE_DEBUG(DBG_LVL_1, "Invalidation Failed\n");
222
223         spin_unlock(&ctrl->mbox_lock);
224         return status;
225 }
226
227 unsigned char mgmt_upload_connection(struct beiscsi_hba *phba,
228                                 unsigned short cid, unsigned int upload_flag)
229 {
230         struct be_ctrl_info *ctrl = &phba->ctrl;
231         struct be_mcc_wrb *wrb = wrb_from_mccq(phba);
232         struct tcp_upload_params_in *req = embedded_payload(wrb);
233         int status = 0;
234
235         spin_lock(&ctrl->mbox_lock);
236         memset(wrb, 0, sizeof(*wrb));
237
238         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
239         be_cmd_hdr_prepare(&req->hdr, CMD_COMMON_TCP_UPLOAD,
240                            OPCODE_COMMON_TCP_UPLOAD, sizeof(*req));
241         req->id = (unsigned short)cid;
242         req->upload_type = (unsigned char)upload_flag;
243         status = be_mcc_notify_wait(phba);
244         if (status)
245                 SE_DEBUG(DBG_LVL_1, "mgmt_upload_connection Failed\n");
246         spin_unlock(&ctrl->mbox_lock);
247         return status;
248 }
249
250 int mgmt_open_connection(struct beiscsi_hba *phba,
251                          struct sockaddr *dst_addr,
252                          struct beiscsi_endpoint *beiscsi_ep)
253 {
254         struct hwi_controller *phwi_ctrlr;
255         struct hwi_context_memory *phwi_context;
256         struct sockaddr_in *daddr_in = (struct sockaddr_in *)dst_addr;
257         struct sockaddr_in6 *daddr_in6 = (struct sockaddr_in6 *)dst_addr;
258         struct be_ctrl_info *ctrl = &phba->ctrl;
259         struct be_mcc_wrb *wrb = wrb_from_mccq(phba);
260         struct tcp_connect_and_offload_in *req = embedded_payload(wrb);
261         unsigned short def_hdr_id;
262         unsigned short def_data_id;
263         struct phys_addr template_address = { 0, 0 };
264         struct phys_addr *ptemplate_address;
265         int status = 0;
266         unsigned int i;
267         unsigned short cid = beiscsi_ep->ep_cid;
268
269         phwi_ctrlr = phba->phwi_ctrlr;
270         phwi_context = phwi_ctrlr->phwi_ctxt;
271         def_hdr_id = (unsigned short)HWI_GET_DEF_HDRQ_ID(phba);
272         def_data_id = (unsigned short)HWI_GET_DEF_BUFQ_ID(phba);
273
274         ptemplate_address = &template_address;
275         ISCSI_GET_PDU_TEMPLATE_ADDRESS(phba, ptemplate_address);
276         spin_lock(&ctrl->mbox_lock);
277         memset(wrb, 0, sizeof(*wrb));
278
279         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
280         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
281                            OPCODE_COMMON_ISCSI_TCP_CONNECT_AND_OFFLOAD,
282                            sizeof(*req));
283         if (dst_addr->sa_family == PF_INET) {
284                 __be32 s_addr = daddr_in->sin_addr.s_addr;
285                 req->ip_address.ip_type = BE2_IPV4;
286                 req->ip_address.ip_address[0] = s_addr & 0x000000ff;
287                 req->ip_address.ip_address[1] = (s_addr & 0x0000ff00) >> 8;
288                 req->ip_address.ip_address[2] = (s_addr & 0x00ff0000) >> 16;
289                 req->ip_address.ip_address[3] = (s_addr & 0xff000000) >> 24;
290                 req->tcp_port = ntohs(daddr_in->sin_port);
291                 beiscsi_ep->dst_addr = daddr_in->sin_addr.s_addr;
292                 beiscsi_ep->dst_tcpport = ntohs(daddr_in->sin_port);
293                 beiscsi_ep->ip_type = BE2_IPV4;
294         } else if (dst_addr->sa_family == PF_INET6) {
295                 req->ip_address.ip_type = BE2_IPV6;
296                 memcpy(&req->ip_address.ip_address,
297                        &daddr_in6->sin6_addr.in6_u.u6_addr8, 16);
298                 req->tcp_port = ntohs(daddr_in6->sin6_port);
299                 beiscsi_ep->dst_tcpport = ntohs(daddr_in6->sin6_port);
300                 memcpy(&beiscsi_ep->dst6_addr,
301                        &daddr_in6->sin6_addr.in6_u.u6_addr8, 16);
302                 beiscsi_ep->ip_type = BE2_IPV6;
303         } else{
304                 shost_printk(KERN_ERR, phba->shost, "unknown addr family %d\n",
305                              dst_addr->sa_family);
306                 spin_unlock(&ctrl->mbox_lock);
307                 return -EINVAL;
308
309         }
310         req->cid = cid;
311         i = phba->nxt_cqid++;
312         if (phba->nxt_cqid == phba->num_cpus)
313                 phba->nxt_cqid = 0;
314         req->cq_id = phwi_context->be_cq[i].id;
315         SE_DEBUG(DBG_LVL_8, "i=%d cq_id=%d \n", i, req->cq_id);
316         req->defq_id = def_hdr_id;
317         req->hdr_ring_id = def_hdr_id;
318         req->data_ring_id = def_data_id;
319         req->do_offload = 1;
320         req->dataout_template_pa.lo = ptemplate_address->lo;
321         req->dataout_template_pa.hi = ptemplate_address->hi;
322         status = be_mcc_notify_wait(phba);
323         if (!status) {
324                 struct iscsi_endpoint *ep;
325                 struct tcp_connect_and_offload_out *ptcpcnct_out =
326                                                         embedded_payload(wrb);
327
328                 ep = phba->ep_array[ptcpcnct_out->cid -
329                                     phba->fw_config.iscsi_cid_start];
330                 beiscsi_ep = ep->dd_data;
331                 beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
332                 beiscsi_ep->cid_vld = 1;
333                 SE_DEBUG(DBG_LVL_8, "mgmt_open_connection Success\n");
334         } else
335                 SE_DEBUG(DBG_LVL_1, "mgmt_open_connection Failed\n");
336         spin_unlock(&ctrl->mbox_lock);
337         return status;
338 }
339
340 int be_cmd_get_mac_addr(struct beiscsi_hba *phba, u8 *mac_addr)
341 {
342         struct be_ctrl_info *ctrl = &phba->ctrl;
343         struct be_mcc_wrb *wrb = wrb_from_mccq(phba);
344         struct be_cmd_req_get_mac_addr *req = embedded_payload(wrb);
345         int status;
346
347         SE_DEBUG(DBG_LVL_8, "In be_cmd_get_mac_addr\n");
348         spin_lock(&ctrl->mbox_lock);
349         memset(wrb, 0, sizeof(*wrb));
350         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
351         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
352                            OPCODE_COMMON_ISCSI_NTWK_GET_NIC_CONFIG,
353                            sizeof(*req));
354
355         status = be_mcc_notify_wait(phba);
356         if (!status) {
357                 struct be_cmd_resp_get_mac_addr *resp = embedded_payload(wrb);
358
359                 memcpy(mac_addr, resp->mac_address, ETH_ALEN);
360         }
361
362         spin_unlock(&ctrl->mbox_lock);
363         return status;
364 }
365