GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / scsi / be2iscsi / be_mgmt.c
1 /*
2  * This file is part of the Emulex Linux Device Driver for Enterprise iSCSI
3  * Host Bus Adapters. Refer to the README file included with this package
4  * for driver version and adapter compatibility.
5  *
6  * Copyright (c) 2018 Broadcom. All Rights Reserved.
7  * The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of version 2 of the GNU General Public License as published
11  * by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful. ALL EXPRESS
14  * OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
15  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
16  * OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH
17  * DISCLAIMERS ARE HELD TO BE LEGALLY INVALID.
18  * See the GNU General Public License for more details, a copy of which
19  * can be found in the file COPYING included with this package.
20  *
21  * Contact Information:
22  * linux-drivers@broadcom.com
23  *
24  */
25
26 #include <linux/bsg-lib.h>
27 #include <scsi/scsi_transport_iscsi.h>
28 #include <scsi/scsi_bsg_iscsi.h>
29 #include "be_mgmt.h"
30 #include "be_iscsi.h"
31 #include "be_main.h"
32
33 unsigned int mgmt_vendor_specific_fw_cmd(struct be_ctrl_info *ctrl,
34                                          struct beiscsi_hba *phba,
35                                          struct bsg_job *job,
36                                          struct be_dma_mem *nonemb_cmd)
37 {
38         struct be_mcc_wrb *wrb;
39         struct be_sge *mcc_sge;
40         unsigned int tag = 0;
41         struct iscsi_bsg_request *bsg_req = job->request;
42         struct be_bsg_vendor_cmd *req = nonemb_cmd->va;
43         unsigned short region, sector_size, sector, offset;
44
45         nonemb_cmd->size = job->request_payload.payload_len;
46         memset(nonemb_cmd->va, 0, nonemb_cmd->size);
47         region =  bsg_req->rqst_data.h_vendor.vendor_cmd[1];
48         sector_size =  bsg_req->rqst_data.h_vendor.vendor_cmd[2];
49         sector =  bsg_req->rqst_data.h_vendor.vendor_cmd[3];
50         offset =  bsg_req->rqst_data.h_vendor.vendor_cmd[4];
51         req->region = region;
52         req->sector = sector;
53         req->offset = offset;
54
55         if (mutex_lock_interruptible(&ctrl->mbox_lock))
56                 return 0;
57         switch (bsg_req->rqst_data.h_vendor.vendor_cmd[0]) {
58         case BEISCSI_WRITE_FLASH:
59                 offset = sector * sector_size + offset;
60                 be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
61                                    OPCODE_COMMON_WRITE_FLASH, sizeof(*req));
62                 sg_copy_to_buffer(job->request_payload.sg_list,
63                                   job->request_payload.sg_cnt,
64                                   nonemb_cmd->va + offset, job->request_len);
65                 break;
66         case BEISCSI_READ_FLASH:
67                 be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
68                            OPCODE_COMMON_READ_FLASH, sizeof(*req));
69                 break;
70         default:
71                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
72                             "BG_%d : Unsupported cmd = 0x%x\n\n",
73                             bsg_req->rqst_data.h_vendor.vendor_cmd[0]);
74
75                 mutex_unlock(&ctrl->mbox_lock);
76                 return -EPERM;
77         }
78
79         wrb = alloc_mcc_wrb(phba, &tag);
80         if (!wrb) {
81                 mutex_unlock(&ctrl->mbox_lock);
82                 return 0;
83         }
84
85         mcc_sge = nonembedded_sgl(wrb);
86         be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false,
87                            job->request_payload.sg_cnt);
88         mcc_sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
89         mcc_sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
90         mcc_sge->len = cpu_to_le32(nonemb_cmd->size);
91
92         be_mcc_notify(phba, tag);
93
94         mutex_unlock(&ctrl->mbox_lock);
95         return tag;
96 }
97
98 /**
99  * mgmt_open_connection()- Establish a TCP CXN
100  * @dst_addr: Destination Address
101  * @beiscsi_ep: ptr to device endpoint struct
102  * @nonemb_cmd: ptr to memory allocated for command
103  *
104  * return
105  *      Success: Tag number of the MBX Command issued
106  *      Failure: Error code
107  **/
108 int mgmt_open_connection(struct beiscsi_hba *phba,
109                          struct sockaddr *dst_addr,
110                          struct beiscsi_endpoint *beiscsi_ep,
111                          struct be_dma_mem *nonemb_cmd)
112 {
113         struct hwi_controller *phwi_ctrlr;
114         struct hwi_context_memory *phwi_context;
115         struct sockaddr_in *daddr_in = (struct sockaddr_in *)dst_addr;
116         struct sockaddr_in6 *daddr_in6 = (struct sockaddr_in6 *)dst_addr;
117         struct be_ctrl_info *ctrl = &phba->ctrl;
118         struct be_mcc_wrb *wrb;
119         struct tcp_connect_and_offload_in_v1 *req;
120         unsigned short def_hdr_id;
121         unsigned short def_data_id;
122         struct phys_addr template_address = { 0, 0 };
123         struct phys_addr *ptemplate_address;
124         unsigned int tag = 0;
125         unsigned int i, ulp_num;
126         unsigned short cid = beiscsi_ep->ep_cid;
127         struct be_sge *sge;
128
129         if (dst_addr->sa_family != PF_INET && dst_addr->sa_family != PF_INET6) {
130                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
131                             "BG_%d : unknown addr family %d\n",
132                             dst_addr->sa_family);
133                 return 0;
134         }
135
136         phwi_ctrlr = phba->phwi_ctrlr;
137         phwi_context = phwi_ctrlr->phwi_ctxt;
138
139         ulp_num = phwi_ctrlr->wrb_context[BE_GET_CRI_FROM_CID(cid)].ulp_num;
140
141         def_hdr_id = (unsigned short)HWI_GET_DEF_HDRQ_ID(phba, ulp_num);
142         def_data_id = (unsigned short)HWI_GET_DEF_BUFQ_ID(phba, ulp_num);
143
144         ptemplate_address = &template_address;
145         ISCSI_GET_PDU_TEMPLATE_ADDRESS(phba, ptemplate_address);
146         if (mutex_lock_interruptible(&ctrl->mbox_lock))
147                 return 0;
148         wrb = alloc_mcc_wrb(phba, &tag);
149         if (!wrb) {
150                 mutex_unlock(&ctrl->mbox_lock);
151                 return 0;
152         }
153
154         sge = nonembedded_sgl(wrb);
155         req = nonemb_cmd->va;
156         memset(req, 0, sizeof(*req));
157
158         be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false, 1);
159         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
160                            OPCODE_COMMON_ISCSI_TCP_CONNECT_AND_OFFLOAD,
161                            nonemb_cmd->size);
162         if (dst_addr->sa_family == PF_INET) {
163                 __be32 s_addr = daddr_in->sin_addr.s_addr;
164                 req->ip_address.ip_type = BEISCSI_IP_TYPE_V4;
165                 req->ip_address.addr[0] = s_addr & 0x000000ff;
166                 req->ip_address.addr[1] = (s_addr & 0x0000ff00) >> 8;
167                 req->ip_address.addr[2] = (s_addr & 0x00ff0000) >> 16;
168                 req->ip_address.addr[3] = (s_addr & 0xff000000) >> 24;
169                 req->tcp_port = ntohs(daddr_in->sin_port);
170                 beiscsi_ep->dst_addr = daddr_in->sin_addr.s_addr;
171                 beiscsi_ep->dst_tcpport = ntohs(daddr_in->sin_port);
172                 beiscsi_ep->ip_type = BEISCSI_IP_TYPE_V4;
173         } else {
174                 /* else its PF_INET6 family */
175                 req->ip_address.ip_type = BEISCSI_IP_TYPE_V6;
176                 memcpy(&req->ip_address.addr,
177                        &daddr_in6->sin6_addr.in6_u.u6_addr8, 16);
178                 req->tcp_port = ntohs(daddr_in6->sin6_port);
179                 beiscsi_ep->dst_tcpport = ntohs(daddr_in6->sin6_port);
180                 memcpy(&beiscsi_ep->dst6_addr,
181                        &daddr_in6->sin6_addr.in6_u.u6_addr8, 16);
182                 beiscsi_ep->ip_type = BEISCSI_IP_TYPE_V6;
183         }
184         req->cid = cid;
185         i = phba->nxt_cqid++;
186         if (phba->nxt_cqid == phba->num_cpus)
187                 phba->nxt_cqid = 0;
188         req->cq_id = phwi_context->be_cq[i].id;
189         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
190                     "BG_%d : i=%d cq_id=%d\n", i, req->cq_id);
191         req->defq_id = def_hdr_id;
192         req->hdr_ring_id = def_hdr_id;
193         req->data_ring_id = def_data_id;
194         req->do_offload = 1;
195         req->dataout_template_pa.lo = ptemplate_address->lo;
196         req->dataout_template_pa.hi = ptemplate_address->hi;
197         sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
198         sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
199         sge->len = cpu_to_le32(nonemb_cmd->size);
200
201         if (!is_chip_be2_be3r(phba)) {
202                 req->hdr.version = MBX_CMD_VER1;
203                 req->tcp_window_size = 0x8000;
204                 req->tcp_window_scale_count = 2;
205         }
206
207         be_mcc_notify(phba, tag);
208         mutex_unlock(&ctrl->mbox_lock);
209         return tag;
210 }
211
212 /*
213  * beiscsi_exec_nemb_cmd()- execute non-embedded MBX cmd
214  * @phba: driver priv structure
215  * @nonemb_cmd: DMA address of the MBX command to be issued
216  * @cbfn: callback func on MCC completion
217  * @resp_buf: buffer to copy the MBX cmd response
218  * @resp_buf_len: response length to be copied
219  *
220  **/
221 static int beiscsi_exec_nemb_cmd(struct beiscsi_hba *phba,
222                                  struct be_dma_mem *nonemb_cmd,
223                                  void (*cbfn)(struct beiscsi_hba *,
224                                               unsigned int),
225                                  void *resp_buf, u32 resp_buf_len)
226 {
227         struct be_ctrl_info *ctrl = &phba->ctrl;
228         struct be_mcc_wrb *wrb;
229         struct be_sge *sge;
230         unsigned int tag;
231         int rc = 0;
232
233         mutex_lock(&ctrl->mbox_lock);
234         wrb = alloc_mcc_wrb(phba, &tag);
235         if (!wrb) {
236                 mutex_unlock(&ctrl->mbox_lock);
237                 rc = -ENOMEM;
238                 goto free_cmd;
239         }
240
241         sge = nonembedded_sgl(wrb);
242         be_wrb_hdr_prepare(wrb, nonemb_cmd->size, false, 1);
243         sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
244         sge->pa_lo = cpu_to_le32(lower_32_bits(nonemb_cmd->dma));
245         sge->len = cpu_to_le32(nonemb_cmd->size);
246
247         if (cbfn) {
248                 struct be_dma_mem *tag_mem;
249
250                 set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
251                 ctrl->ptag_state[tag].cbfn = cbfn;
252                 tag_mem = &phba->ctrl.ptag_state[tag].tag_mem_state;
253
254                 /* store DMA mem to be freed in callback */
255                 tag_mem->size = nonemb_cmd->size;
256                 tag_mem->va = nonemb_cmd->va;
257                 tag_mem->dma = nonemb_cmd->dma;
258         }
259         be_mcc_notify(phba, tag);
260         mutex_unlock(&ctrl->mbox_lock);
261
262         /* with cbfn set, its async cmd, don't wait */
263         if (cbfn)
264                 return 0;
265
266         rc = beiscsi_mccq_compl_wait(phba, tag, NULL, nonemb_cmd);
267
268         /* copy the response, if any */
269         if (resp_buf)
270                 memcpy(resp_buf, nonemb_cmd->va, resp_buf_len);
271         /**
272          * This is special case of NTWK_GET_IF_INFO where the size of
273          * response is not known. beiscsi_if_get_info checks the return
274          * value to free DMA buffer.
275          */
276         if (rc == -EAGAIN)
277                 return rc;
278
279         /**
280          * If FW is busy that is driver timed out, DMA buffer is saved with
281          * the tag, only when the cmd completes this buffer is freed.
282          */
283         if (rc == -EBUSY)
284                 return rc;
285
286 free_cmd:
287         pci_free_consistent(ctrl->pdev, nonemb_cmd->size,
288                             nonemb_cmd->va, nonemb_cmd->dma);
289         return rc;
290 }
291
292 static int beiscsi_prep_nemb_cmd(struct beiscsi_hba *phba,
293                                  struct be_dma_mem *cmd,
294                                  u8 subsystem, u8 opcode, u32 size)
295 {
296         cmd->va = pci_zalloc_consistent(phba->ctrl.pdev, size, &cmd->dma);
297         if (!cmd->va) {
298                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
299                             "BG_%d : Failed to allocate memory for if info\n");
300                 return -ENOMEM;
301         }
302         cmd->size = size;
303         be_cmd_hdr_prepare(cmd->va, subsystem, opcode, size);
304         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
305                     "BG_%d : subsystem %u cmd %u size %u\n",
306                     subsystem, opcode, size);
307         return 0;
308 }
309
310 static void __beiscsi_eq_delay_compl(struct beiscsi_hba *phba, unsigned int tag)
311 {
312         struct be_dma_mem *tag_mem;
313
314         /* status is ignored */
315         __beiscsi_mcc_compl_status(phba, tag, NULL, NULL);
316         tag_mem = &phba->ctrl.ptag_state[tag].tag_mem_state;
317         if (tag_mem->size) {
318                 pci_free_consistent(phba->pcidev, tag_mem->size,
319                                     tag_mem->va, tag_mem->dma);
320                 tag_mem->size = 0;
321         }
322 }
323
324 int beiscsi_modify_eq_delay(struct beiscsi_hba *phba,
325                             struct be_set_eqd *set_eqd, int num)
326 {
327         struct be_cmd_req_modify_eq_delay *req;
328         struct be_dma_mem nonemb_cmd;
329         int i, rc;
330
331         rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_COMMON,
332                         OPCODE_COMMON_MODIFY_EQ_DELAY, sizeof(*req));
333         if (rc)
334                 return rc;
335
336         req = nonemb_cmd.va;
337         req->num_eq = cpu_to_le32(num);
338         for (i = 0; i < num; i++) {
339                 req->delay[i].eq_id = cpu_to_le32(set_eqd[i].eq_id);
340                 req->delay[i].phase = 0;
341                 req->delay[i].delay_multiplier =
342                                 cpu_to_le32(set_eqd[i].delay_multiplier);
343         }
344
345         return beiscsi_exec_nemb_cmd(phba, &nonemb_cmd,
346                                      __beiscsi_eq_delay_compl, NULL, 0);
347 }
348
349 /**
350  * beiscsi_get_initiator_name - read initiator name from flash
351  * @phba: device priv structure
352  * @name: buffer pointer
353  * @cfg: fetch user configured
354  *
355  */
356 int beiscsi_get_initiator_name(struct beiscsi_hba *phba, char *name, bool cfg)
357 {
358         struct be_dma_mem nonemb_cmd;
359         struct be_cmd_hba_name resp;
360         struct be_cmd_hba_name *req;
361         int rc;
362
363         rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI_INI,
364                         OPCODE_ISCSI_INI_CFG_GET_HBA_NAME, sizeof(resp));
365         if (rc)
366                 return rc;
367
368         req = nonemb_cmd.va;
369         if (cfg)
370                 req->hdr.version = 1;
371         rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL,
372                                    &resp, sizeof(resp));
373         if (rc) {
374                 beiscsi_log(phba, KERN_ERR,
375                             BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
376                             "BS_%d : Initiator Name MBX Failed\n");
377                 return rc;
378         }
379         rc = sprintf(name, "%s\n", resp.initiator_name);
380         return rc;
381 }
382
383 unsigned int beiscsi_if_get_handle(struct beiscsi_hba *phba)
384 {
385         struct be_ctrl_info *ctrl = &phba->ctrl;
386         struct be_mcc_wrb *wrb;
387         struct be_cmd_get_all_if_id_req *req;
388         struct be_cmd_get_all_if_id_req *pbe_allid;
389         unsigned int tag;
390         int status = 0;
391
392         if (mutex_lock_interruptible(&ctrl->mbox_lock))
393                 return -EINTR;
394         wrb = alloc_mcc_wrb(phba, &tag);
395         if (!wrb) {
396                 mutex_unlock(&ctrl->mbox_lock);
397                 return -ENOMEM;
398         }
399
400         req = embedded_payload(wrb);
401         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
402         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
403                            OPCODE_COMMON_ISCSI_NTWK_GET_ALL_IF_ID,
404                            sizeof(*req));
405         be_mcc_notify(phba, tag);
406         mutex_unlock(&ctrl->mbox_lock);
407
408         status = beiscsi_mccq_compl_wait(phba, tag, &wrb, NULL);
409         if (status) {
410                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
411                             "BG_%d : %s failed: %d\n", __func__, status);
412                 return -EBUSY;
413         }
414
415         pbe_allid = embedded_payload(wrb);
416         /* we now support only one interface per function */
417         phba->interface_handle = pbe_allid->if_hndl_list[0];
418
419         return status;
420 }
421
422 static inline bool beiscsi_if_zero_ip(u8 *ip, u32 ip_type)
423 {
424         u32 len;
425
426         len = (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN;
427         while (len && !ip[len - 1])
428                 len--;
429         return (len == 0);
430 }
431
432 static int beiscsi_if_mod_gw(struct beiscsi_hba *phba,
433                              u32 action, u32 ip_type, u8 *gw)
434 {
435         struct be_cmd_set_def_gateway_req *req;
436         struct be_dma_mem nonemb_cmd;
437         int rt_val;
438
439         rt_val = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
440                         OPCODE_COMMON_ISCSI_NTWK_MODIFY_DEFAULT_GATEWAY,
441                         sizeof(*req));
442         if (rt_val)
443                 return rt_val;
444
445         req = nonemb_cmd.va;
446         req->action = action;
447         req->ip_addr.ip_type = ip_type;
448         memcpy(req->ip_addr.addr, gw,
449                (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN);
450         return beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
451 }
452
453 int beiscsi_if_set_gw(struct beiscsi_hba *phba, u32 ip_type, u8 *gw)
454 {
455         struct be_cmd_get_def_gateway_resp gw_resp;
456         int rt_val;
457
458         memset(&gw_resp, 0, sizeof(gw_resp));
459         rt_val = beiscsi_if_get_gw(phba, ip_type, &gw_resp);
460         if (rt_val) {
461                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
462                             "BG_%d : Failed to Get Gateway Addr\n");
463                 return rt_val;
464         }
465
466         if (!beiscsi_if_zero_ip(gw_resp.ip_addr.addr, ip_type)) {
467                 rt_val = beiscsi_if_mod_gw(phba, IP_ACTION_DEL, ip_type,
468                                            gw_resp.ip_addr.addr);
469                 if (rt_val) {
470                         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
471                                     "BG_%d : Failed to clear Gateway Addr Set\n");
472                         return rt_val;
473                 }
474         }
475
476         rt_val = beiscsi_if_mod_gw(phba, IP_ACTION_ADD, ip_type, gw);
477         if (rt_val)
478                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
479                             "BG_%d : Failed to Set Gateway Addr\n");
480
481         return rt_val;
482 }
483
484 int beiscsi_if_get_gw(struct beiscsi_hba *phba, u32 ip_type,
485                       struct be_cmd_get_def_gateway_resp *resp)
486 {
487         struct be_cmd_get_def_gateway_req *req;
488         struct be_dma_mem nonemb_cmd;
489         int rc;
490
491         rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
492                         OPCODE_COMMON_ISCSI_NTWK_GET_DEFAULT_GATEWAY,
493                         sizeof(*resp));
494         if (rc)
495                 return rc;
496
497         req = nonemb_cmd.va;
498         req->ip_type = ip_type;
499
500         return beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL,
501                                      resp, sizeof(*resp));
502 }
503
504 static int
505 beiscsi_if_clr_ip(struct beiscsi_hba *phba,
506                   struct be_cmd_get_if_info_resp *if_info)
507 {
508         struct be_cmd_set_ip_addr_req *req;
509         struct be_dma_mem nonemb_cmd;
510         int rc;
511
512         rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
513                         OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR,
514                         sizeof(*req));
515         if (rc)
516                 return rc;
517
518         req = nonemb_cmd.va;
519         req->ip_params.record_entry_count = 1;
520         req->ip_params.ip_record.action = IP_ACTION_DEL;
521         req->ip_params.ip_record.interface_hndl =
522                 phba->interface_handle;
523         req->ip_params.ip_record.ip_addr.size_of_structure =
524                 sizeof(struct be_ip_addr_subnet_format);
525         req->ip_params.ip_record.ip_addr.ip_type = if_info->ip_addr.ip_type;
526         memcpy(req->ip_params.ip_record.ip_addr.addr,
527                if_info->ip_addr.addr,
528                sizeof(if_info->ip_addr.addr));
529         memcpy(req->ip_params.ip_record.ip_addr.subnet_mask,
530                if_info->ip_addr.subnet_mask,
531                sizeof(if_info->ip_addr.subnet_mask));
532         rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
533         if (rc < 0 || req->ip_params.ip_record.status) {
534                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
535                             "BG_%d : failed to clear IP: rc %d status %d\n",
536                             rc, req->ip_params.ip_record.status);
537         }
538         return rc;
539 }
540
541 static int
542 beiscsi_if_set_ip(struct beiscsi_hba *phba, u8 *ip,
543                   u8 *subnet, u32 ip_type)
544 {
545         struct be_cmd_set_ip_addr_req *req;
546         struct be_dma_mem nonemb_cmd;
547         uint32_t ip_len;
548         int rc;
549
550         rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
551                         OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR,
552                         sizeof(*req));
553         if (rc)
554                 return rc;
555
556         req = nonemb_cmd.va;
557         req->ip_params.record_entry_count = 1;
558         req->ip_params.ip_record.action = IP_ACTION_ADD;
559         req->ip_params.ip_record.interface_hndl =
560                 phba->interface_handle;
561         req->ip_params.ip_record.ip_addr.size_of_structure =
562                 sizeof(struct be_ip_addr_subnet_format);
563         req->ip_params.ip_record.ip_addr.ip_type = ip_type;
564         ip_len = (ip_type < BEISCSI_IP_TYPE_V6) ? IP_V4_LEN : IP_V6_LEN;
565         memcpy(req->ip_params.ip_record.ip_addr.addr, ip, ip_len);
566         if (subnet)
567                 memcpy(req->ip_params.ip_record.ip_addr.subnet_mask,
568                        subnet, ip_len);
569
570         rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
571         /**
572          * In some cases, host needs to look into individual record status
573          * even though FW reported success for that IOCTL.
574          */
575         if (rc < 0 || req->ip_params.ip_record.status) {
576                 __beiscsi_log(phba, KERN_ERR,
577                             "BG_%d : failed to set IP: rc %d status %d\n",
578                             rc, req->ip_params.ip_record.status);
579                 if (req->ip_params.ip_record.status)
580                         rc = -EINVAL;
581         }
582         return rc;
583 }
584
585 int beiscsi_if_en_static(struct beiscsi_hba *phba, u32 ip_type,
586                          u8 *ip, u8 *subnet)
587 {
588         struct be_cmd_get_if_info_resp *if_info;
589         struct be_cmd_rel_dhcp_req *reldhcp;
590         struct be_dma_mem nonemb_cmd;
591         int rc;
592
593         rc = beiscsi_if_get_info(phba, ip_type, &if_info);
594         if (rc)
595                 return rc;
596
597         if (if_info->dhcp_state) {
598                 rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd,
599                                 CMD_SUBSYSTEM_ISCSI,
600                                 OPCODE_COMMON_ISCSI_NTWK_REL_STATELESS_IP_ADDR,
601                                 sizeof(*reldhcp));
602                 if (rc)
603                         goto exit;
604
605                 reldhcp = nonemb_cmd.va;
606                 reldhcp->interface_hndl = phba->interface_handle;
607                 reldhcp->ip_type = ip_type;
608                 rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
609                 if (rc < 0) {
610                         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
611                                     "BG_%d : failed to release existing DHCP: %d\n",
612                                     rc);
613                         goto exit;
614                 }
615         }
616
617         /* first delete any IP set */
618         if (!beiscsi_if_zero_ip(if_info->ip_addr.addr, ip_type)) {
619                 rc = beiscsi_if_clr_ip(phba, if_info);
620                 if (rc)
621                         goto exit;
622         }
623
624         /* if ip == NULL then this is called just to release DHCP IP */
625         if (ip)
626                 rc = beiscsi_if_set_ip(phba, ip, subnet, ip_type);
627 exit:
628         kfree(if_info);
629         return rc;
630 }
631
632 int beiscsi_if_en_dhcp(struct beiscsi_hba *phba, u32 ip_type)
633 {
634         struct be_cmd_get_def_gateway_resp gw_resp;
635         struct be_cmd_get_if_info_resp *if_info;
636         struct be_cmd_set_dhcp_req *dhcpreq;
637         struct be_dma_mem nonemb_cmd;
638         u8 *gw;
639         int rc;
640
641         rc = beiscsi_if_get_info(phba, ip_type, &if_info);
642         if (rc)
643                 return rc;
644
645         if (if_info->dhcp_state) {
646                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
647                                 "BG_%d : DHCP Already Enabled\n");
648                 goto exit;
649         }
650
651         /* first delete any IP set */
652         if (!beiscsi_if_zero_ip(if_info->ip_addr.addr, ip_type)) {
653                 rc = beiscsi_if_clr_ip(phba, if_info);
654                 if (rc)
655                         goto exit;
656         }
657
658         /* delete gateway settings if mode change is to DHCP */
659         memset(&gw_resp, 0, sizeof(gw_resp));
660         /* use ip_type provided in if_info */
661         rc = beiscsi_if_get_gw(phba, if_info->ip_addr.ip_type, &gw_resp);
662         if (rc) {
663                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
664                             "BG_%d : Failed to Get Gateway Addr\n");
665                 goto exit;
666         }
667         gw = (u8 *)&gw_resp.ip_addr.addr;
668         if (!beiscsi_if_zero_ip(gw, if_info->ip_addr.ip_type)) {
669                 rc = beiscsi_if_mod_gw(phba, IP_ACTION_DEL,
670                                        if_info->ip_addr.ip_type, gw);
671                 if (rc) {
672                         beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
673                                     "BG_%d : Failed to clear Gateway Addr Set\n");
674                         goto exit;
675                 }
676         }
677
678         rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
679                         OPCODE_COMMON_ISCSI_NTWK_CONFIG_STATELESS_IP_ADDR,
680                         sizeof(*dhcpreq));
681         if (rc)
682                 goto exit;
683
684         dhcpreq = nonemb_cmd.va;
685         dhcpreq->flags = 1; /* 1 - blocking; 0 - non-blocking */
686         dhcpreq->retry_count = 1;
687         dhcpreq->interface_hndl = phba->interface_handle;
688         dhcpreq->ip_type = ip_type;
689         rc = beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, NULL, 0);
690
691 exit:
692         kfree(if_info);
693         return rc;
694 }
695
696 /**
697  * beiscsi_if_set_vlan()- Issue and wait for CMD completion
698  * @phba: device private structure instance
699  * @vlan_tag: VLAN tag
700  *
701  * Issue the MBX Cmd and wait for the completion of the
702  * command.
703  *
704  * returns
705  *      Success: 0
706  *      Failure: Non-Xero Value
707  **/
708 int beiscsi_if_set_vlan(struct beiscsi_hba *phba, uint16_t vlan_tag)
709 {
710         int rc;
711         unsigned int tag;
712
713         tag = be_cmd_set_vlan(phba, vlan_tag);
714         if (!tag) {
715                 beiscsi_log(phba, KERN_ERR,
716                             (BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX),
717                             "BG_%d : VLAN Setting Failed\n");
718                 return -EBUSY;
719         }
720
721         rc = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
722         if (rc) {
723                 beiscsi_log(phba, KERN_ERR,
724                             (BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX),
725                             "BS_%d : VLAN MBX Cmd Failed\n");
726                 return rc;
727         }
728         return rc;
729 }
730
731
732 int beiscsi_if_get_info(struct beiscsi_hba *phba, int ip_type,
733                         struct be_cmd_get_if_info_resp **if_info)
734 {
735         struct be_cmd_get_if_info_req *req;
736         struct be_dma_mem nonemb_cmd;
737         uint32_t ioctl_size = sizeof(struct be_cmd_get_if_info_resp);
738         int rc;
739
740         rc = beiscsi_if_get_handle(phba);
741         if (rc)
742                 return rc;
743
744         do {
745                 rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd,
746                                 CMD_SUBSYSTEM_ISCSI,
747                                 OPCODE_COMMON_ISCSI_NTWK_GET_IF_INFO,
748                                 ioctl_size);
749                 if (rc)
750                         return rc;
751
752                 req = nonemb_cmd.va;
753                 req->interface_hndl = phba->interface_handle;
754                 req->ip_type = ip_type;
755
756                 /* Allocate memory for if_info */
757                 *if_info = kzalloc(ioctl_size, GFP_KERNEL);
758                 if (!*if_info) {
759                         beiscsi_log(phba, KERN_ERR,
760                                     BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
761                                     "BG_%d : Memory Allocation Failure\n");
762
763                                 /* Free the DMA memory for the IOCTL issuing */
764                                 pci_free_consistent(phba->ctrl.pdev,
765                                                     nonemb_cmd.size,
766                                                     nonemb_cmd.va,
767                                                     nonemb_cmd.dma);
768                                 return -ENOMEM;
769                 }
770
771                 rc =  beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL, *if_info,
772                                             ioctl_size);
773
774                 /* Check if the error is because of Insufficent_Buffer */
775                 if (rc == -EAGAIN) {
776
777                         /* Get the new memory size */
778                         ioctl_size = ((struct be_cmd_resp_hdr *)
779                                       nonemb_cmd.va)->actual_resp_len;
780                         ioctl_size += sizeof(struct be_cmd_req_hdr);
781
782                         /* Free the previous allocated DMA memory */
783                         pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
784                                             nonemb_cmd.va,
785                                             nonemb_cmd.dma);
786
787                         /* Free the virtual memory */
788                         kfree(*if_info);
789                 } else
790                         break;
791         } while (true);
792         return rc;
793 }
794
795 int mgmt_get_nic_conf(struct beiscsi_hba *phba,
796                       struct be_cmd_get_nic_conf_resp *nic)
797 {
798         struct be_dma_mem nonemb_cmd;
799         int rc;
800
801         rc = beiscsi_prep_nemb_cmd(phba, &nonemb_cmd, CMD_SUBSYSTEM_ISCSI,
802                         OPCODE_COMMON_ISCSI_NTWK_GET_NIC_CONFIG,
803                         sizeof(*nic));
804         if (rc)
805                 return rc;
806
807         return beiscsi_exec_nemb_cmd(phba, &nonemb_cmd, NULL,
808                                      nic, sizeof(*nic));
809 }
810
811 static void beiscsi_boot_process_compl(struct beiscsi_hba *phba,
812                                        unsigned int tag)
813 {
814         struct be_cmd_get_boot_target_resp *boot_resp;
815         struct be_cmd_resp_logout_fw_sess *logo_resp;
816         struct be_cmd_get_session_resp *sess_resp;
817         struct be_mcc_wrb *wrb;
818         struct boot_struct *bs;
819         int boot_work, status;
820
821         if (!test_bit(BEISCSI_HBA_BOOT_WORK, &phba->state)) {
822                 __beiscsi_log(phba, KERN_ERR,
823                               "BG_%d : %s no boot work %lx\n",
824                               __func__, phba->state);
825                 return;
826         }
827
828         if (phba->boot_struct.tag != tag) {
829                 __beiscsi_log(phba, KERN_ERR,
830                               "BG_%d : %s tag mismatch %d:%d\n",
831                               __func__, tag, phba->boot_struct.tag);
832                 return;
833         }
834         bs = &phba->boot_struct;
835         boot_work = 1;
836         status = 0;
837         switch (bs->action) {
838         case BEISCSI_BOOT_REOPEN_SESS:
839                 status = __beiscsi_mcc_compl_status(phba, tag, NULL, NULL);
840                 if (!status)
841                         bs->action = BEISCSI_BOOT_GET_SHANDLE;
842                 else
843                         bs->retry--;
844                 break;
845         case BEISCSI_BOOT_GET_SHANDLE:
846                 status = __beiscsi_mcc_compl_status(phba, tag, &wrb, NULL);
847                 if (!status) {
848                         boot_resp = embedded_payload(wrb);
849                         bs->s_handle = boot_resp->boot_session_handle;
850                 }
851                 if (bs->s_handle == BE_BOOT_INVALID_SHANDLE) {
852                         bs->action = BEISCSI_BOOT_REOPEN_SESS;
853                         bs->retry--;
854                 } else {
855                         bs->action = BEISCSI_BOOT_GET_SINFO;
856                 }
857                 break;
858         case BEISCSI_BOOT_GET_SINFO:
859                 status = __beiscsi_mcc_compl_status(phba, tag, NULL,
860                                                     &bs->nonemb_cmd);
861                 if (!status) {
862                         sess_resp = bs->nonemb_cmd.va;
863                         memcpy(&bs->boot_sess, &sess_resp->session_info,
864                                sizeof(struct mgmt_session_info));
865                         bs->action = BEISCSI_BOOT_LOGOUT_SESS;
866                 } else {
867                         __beiscsi_log(phba, KERN_ERR,
868                                       "BG_%d : get boot session info error : 0x%x\n",
869                                       status);
870                         boot_work = 0;
871                 }
872                 pci_free_consistent(phba->ctrl.pdev, bs->nonemb_cmd.size,
873                                     bs->nonemb_cmd.va, bs->nonemb_cmd.dma);
874                 bs->nonemb_cmd.va = NULL;
875                 break;
876         case BEISCSI_BOOT_LOGOUT_SESS:
877                 status = __beiscsi_mcc_compl_status(phba, tag, &wrb, NULL);
878                 if (!status) {
879                         logo_resp = embedded_payload(wrb);
880                         if (logo_resp->session_status != BE_SESS_STATUS_CLOSE) {
881                                 __beiscsi_log(phba, KERN_ERR,
882                                               "BG_%d : FW boot session logout error : 0x%x\n",
883                                               logo_resp->session_status);
884                         }
885                 }
886                 /* continue to create boot_kset even if logout failed? */
887                 bs->action = BEISCSI_BOOT_CREATE_KSET;
888                 break;
889         default:
890                 break;
891         }
892
893         /* clear the tag so no other completion matches this tag */
894         bs->tag = 0;
895         if (!bs->retry) {
896                 boot_work = 0;
897                 __beiscsi_log(phba, KERN_ERR,
898                               "BG_%d : failed to setup boot target: status %d action %d\n",
899                               status, bs->action);
900         }
901         if (!boot_work) {
902                 /* wait for next event to start boot_work */
903                 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
904                 return;
905         }
906         schedule_work(&phba->boot_work);
907 }
908
909 /**
910  * beiscsi_boot_logout_sess()- Logout from boot FW session
911  * @phba: Device priv structure instance
912  *
913  * return
914  *      the TAG used for MBOX Command
915  *
916  */
917 unsigned int beiscsi_boot_logout_sess(struct beiscsi_hba *phba)
918 {
919         struct be_ctrl_info *ctrl = &phba->ctrl;
920         struct be_mcc_wrb *wrb;
921         struct be_cmd_req_logout_fw_sess *req;
922         unsigned int tag;
923
924         mutex_lock(&ctrl->mbox_lock);
925         wrb = alloc_mcc_wrb(phba, &tag);
926         if (!wrb) {
927                 mutex_unlock(&ctrl->mbox_lock);
928                 return 0;
929         }
930
931         req = embedded_payload(wrb);
932         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
933         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
934                            OPCODE_ISCSI_INI_SESSION_LOGOUT_TARGET,
935                            sizeof(struct be_cmd_req_logout_fw_sess));
936         /* Use the session handle copied into boot_sess */
937         req->session_handle = phba->boot_struct.boot_sess.session_handle;
938
939         phba->boot_struct.tag = tag;
940         set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
941         ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
942
943         be_mcc_notify(phba, tag);
944         mutex_unlock(&ctrl->mbox_lock);
945
946         return tag;
947 }
948 /**
949  * beiscsi_boot_reopen_sess()- Reopen boot session
950  * @phba: Device priv structure instance
951  *
952  * return
953  *      the TAG used for MBOX Command
954  *
955  **/
956 unsigned int beiscsi_boot_reopen_sess(struct beiscsi_hba *phba)
957 {
958         struct be_ctrl_info *ctrl = &phba->ctrl;
959         struct be_mcc_wrb *wrb;
960         struct be_cmd_reopen_session_req *req;
961         unsigned int tag;
962
963         mutex_lock(&ctrl->mbox_lock);
964         wrb = alloc_mcc_wrb(phba, &tag);
965         if (!wrb) {
966                 mutex_unlock(&ctrl->mbox_lock);
967                 return 0;
968         }
969
970         req = embedded_payload(wrb);
971         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
972         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
973                            OPCODE_ISCSI_INI_DRIVER_REOPEN_ALL_SESSIONS,
974                            sizeof(struct be_cmd_reopen_session_resp));
975         req->reopen_type = BE_REOPEN_BOOT_SESSIONS;
976         req->session_handle = BE_BOOT_INVALID_SHANDLE;
977
978         phba->boot_struct.tag = tag;
979         set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
980         ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
981
982         be_mcc_notify(phba, tag);
983         mutex_unlock(&ctrl->mbox_lock);
984         return tag;
985 }
986
987
988 /**
989  * beiscsi_boot_get_sinfo()- Get boot session info
990  * @phba: device priv structure instance
991  *
992  * Fetches the boot_struct.s_handle info from FW.
993  * return
994  *      the TAG used for MBOX Command
995  *
996  **/
997 unsigned int beiscsi_boot_get_sinfo(struct beiscsi_hba *phba)
998 {
999         struct be_ctrl_info *ctrl = &phba->ctrl;
1000         struct be_cmd_get_session_req *req;
1001         struct be_dma_mem *nonemb_cmd;
1002         struct be_mcc_wrb *wrb;
1003         struct be_sge *sge;
1004         unsigned int tag;
1005
1006         mutex_lock(&ctrl->mbox_lock);
1007         wrb = alloc_mcc_wrb(phba, &tag);
1008         if (!wrb) {
1009                 mutex_unlock(&ctrl->mbox_lock);
1010                 return 0;
1011         }
1012
1013         nonemb_cmd = &phba->boot_struct.nonemb_cmd;
1014         nonemb_cmd->size = sizeof(struct be_cmd_get_session_resp);
1015         nonemb_cmd->va = pci_alloc_consistent(phba->ctrl.pdev,
1016                                               nonemb_cmd->size,
1017                                               &nonemb_cmd->dma);
1018         if (!nonemb_cmd->va) {
1019                 mutex_unlock(&ctrl->mbox_lock);
1020                 return 0;
1021         }
1022
1023         req = nonemb_cmd->va;
1024         memset(req, 0, sizeof(*req));
1025         sge = nonembedded_sgl(wrb);
1026         be_wrb_hdr_prepare(wrb, sizeof(*req), false, 1);
1027         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1028                            OPCODE_ISCSI_INI_SESSION_GET_A_SESSION,
1029                            sizeof(struct be_cmd_get_session_resp));
1030         req->session_handle = phba->boot_struct.s_handle;
1031         sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd->dma));
1032         sge->pa_lo = cpu_to_le32(nonemb_cmd->dma & 0xFFFFFFFF);
1033         sge->len = cpu_to_le32(nonemb_cmd->size);
1034
1035         phba->boot_struct.tag = tag;
1036         set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
1037         ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
1038
1039         be_mcc_notify(phba, tag);
1040         mutex_unlock(&ctrl->mbox_lock);
1041         return tag;
1042 }
1043
1044 unsigned int __beiscsi_boot_get_shandle(struct beiscsi_hba *phba, int async)
1045 {
1046         struct be_ctrl_info *ctrl = &phba->ctrl;
1047         struct be_mcc_wrb *wrb;
1048         struct be_cmd_get_boot_target_req *req;
1049         unsigned int tag;
1050
1051         mutex_lock(&ctrl->mbox_lock);
1052         wrb = alloc_mcc_wrb(phba, &tag);
1053         if (!wrb) {
1054                 mutex_unlock(&ctrl->mbox_lock);
1055                 return 0;
1056         }
1057
1058         req = embedded_payload(wrb);
1059         be_wrb_hdr_prepare(wrb, sizeof(*req), true, 0);
1060         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1061                            OPCODE_ISCSI_INI_BOOT_GET_BOOT_TARGET,
1062                            sizeof(struct be_cmd_get_boot_target_resp));
1063
1064         if (async) {
1065                 phba->boot_struct.tag = tag;
1066                 set_bit(MCC_TAG_STATE_ASYNC, &ctrl->ptag_state[tag].tag_state);
1067                 ctrl->ptag_state[tag].cbfn = beiscsi_boot_process_compl;
1068         }
1069
1070         be_mcc_notify(phba, tag);
1071         mutex_unlock(&ctrl->mbox_lock);
1072         return tag;
1073 }
1074
1075 /**
1076  * beiscsi_boot_get_shandle()- Get boot session handle
1077  * @phba: device priv structure instance
1078  * @s_handle: session handle returned for boot session.
1079  *
1080  * return
1081  *      Success: 1
1082  *      Failure: negative
1083  *
1084  **/
1085 int beiscsi_boot_get_shandle(struct beiscsi_hba *phba, unsigned int *s_handle)
1086 {
1087         struct be_cmd_get_boot_target_resp *boot_resp;
1088         struct be_mcc_wrb *wrb;
1089         unsigned int tag;
1090         int rc;
1091
1092         *s_handle = BE_BOOT_INVALID_SHANDLE;
1093         /* get configured boot session count and handle */
1094         tag = __beiscsi_boot_get_shandle(phba, 0);
1095         if (!tag) {
1096                 beiscsi_log(phba, KERN_ERR,
1097                             BEISCSI_LOG_CONFIG | BEISCSI_LOG_INIT,
1098                             "BG_%d : Getting Boot Target Info Failed\n");
1099                 return -EAGAIN;
1100         }
1101
1102         rc = beiscsi_mccq_compl_wait(phba, tag, &wrb, NULL);
1103         if (rc) {
1104                 beiscsi_log(phba, KERN_ERR,
1105                             BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
1106                             "BG_%d : MBX CMD get_boot_target Failed\n");
1107                 return -EBUSY;
1108         }
1109
1110         boot_resp = embedded_payload(wrb);
1111         /* check if there are any boot targets configured */
1112         if (!boot_resp->boot_session_count) {
1113                 __beiscsi_log(phba, KERN_INFO,
1114                               "BG_%d : No boot targets configured\n");
1115                 return -ENXIO;
1116         }
1117
1118         /* only if FW has logged in to the boot target, s_handle is valid */
1119         *s_handle = boot_resp->boot_session_handle;
1120         return 1;
1121 }
1122
1123 /**
1124  * beiscsi_drvr_ver_disp()- Display the driver Name and Version
1125  * @dev: ptr to device not used.
1126  * @attr: device attribute, not used.
1127  * @buf: contains formatted text driver name and version
1128  *
1129  * return
1130  * size of the formatted string
1131  **/
1132 ssize_t
1133 beiscsi_drvr_ver_disp(struct device *dev, struct device_attribute *attr,
1134                        char *buf)
1135 {
1136         return snprintf(buf, PAGE_SIZE, BE_NAME "\n");
1137 }
1138
1139 /**
1140  * beiscsi_fw_ver_disp()- Display Firmware Version
1141  * @dev: ptr to device not used.
1142  * @attr: device attribute, not used.
1143  * @buf: contains formatted text Firmware version
1144  *
1145  * return
1146  * size of the formatted string
1147  **/
1148 ssize_t
1149 beiscsi_fw_ver_disp(struct device *dev, struct device_attribute *attr,
1150                      char *buf)
1151 {
1152         struct Scsi_Host *shost = class_to_shost(dev);
1153         struct beiscsi_hba *phba = iscsi_host_priv(shost);
1154
1155         return snprintf(buf, PAGE_SIZE, "%s\n", phba->fw_ver_str);
1156 }
1157
1158 /**
1159  * beiscsi_active_session_disp()- Display Sessions Active
1160  * @dev: ptr to device not used.
1161  * @attr: device attribute, not used.
1162  * @buf: contains formatted text Session Count
1163  *
1164  * return
1165  * size of the formatted string
1166  **/
1167 ssize_t
1168 beiscsi_active_session_disp(struct device *dev, struct device_attribute *attr,
1169                          char *buf)
1170 {
1171         struct Scsi_Host *shost = class_to_shost(dev);
1172         struct beiscsi_hba *phba = iscsi_host_priv(shost);
1173         uint16_t avlbl_cids = 0, ulp_num, len = 0, total_cids = 0;
1174
1175         for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
1176                 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
1177                         avlbl_cids = BEISCSI_ULP_AVLBL_CID(phba, ulp_num);
1178                         total_cids = BEISCSI_GET_CID_COUNT(phba, ulp_num);
1179                         len += snprintf(buf+len, PAGE_SIZE - len,
1180                                         "ULP%d : %d\n", ulp_num,
1181                                         (total_cids - avlbl_cids));
1182                 } else
1183                         len += snprintf(buf+len, PAGE_SIZE - len,
1184                                         "ULP%d : %d\n", ulp_num, 0);
1185         }
1186
1187         return len;
1188 }
1189
1190 /**
1191  * beiscsi_free_session_disp()- Display Avaliable Session
1192  * @dev: ptr to device not used.
1193  * @attr: device attribute, not used.
1194  * @buf: contains formatted text Session Count
1195  *
1196  * return
1197  * size of the formatted string
1198  **/
1199 ssize_t
1200 beiscsi_free_session_disp(struct device *dev, struct device_attribute *attr,
1201                        char *buf)
1202 {
1203         struct Scsi_Host *shost = class_to_shost(dev);
1204         struct beiscsi_hba *phba = iscsi_host_priv(shost);
1205         uint16_t ulp_num, len = 0;
1206
1207         for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
1208                 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported))
1209                         len += snprintf(buf+len, PAGE_SIZE - len,
1210                                         "ULP%d : %d\n", ulp_num,
1211                                         BEISCSI_ULP_AVLBL_CID(phba, ulp_num));
1212                 else
1213                         len += snprintf(buf+len, PAGE_SIZE - len,
1214                                         "ULP%d : %d\n", ulp_num, 0);
1215         }
1216
1217         return len;
1218 }
1219
1220 /**
1221  * beiscsi_adap_family_disp()- Display adapter family.
1222  * @dev: ptr to device to get priv structure
1223  * @attr: device attribute, not used.
1224  * @buf: contains formatted text driver name and version
1225  *
1226  * return
1227  * size of the formatted string
1228  **/
1229 ssize_t
1230 beiscsi_adap_family_disp(struct device *dev, struct device_attribute *attr,
1231                           char *buf)
1232 {
1233         uint16_t dev_id = 0;
1234         struct Scsi_Host *shost = class_to_shost(dev);
1235         struct beiscsi_hba *phba = iscsi_host_priv(shost);
1236
1237         dev_id = phba->pcidev->device;
1238         switch (dev_id) {
1239         case BE_DEVICE_ID1:
1240         case OC_DEVICE_ID1:
1241         case OC_DEVICE_ID2:
1242                 return snprintf(buf, PAGE_SIZE,
1243                                 "Obsolete/Unsupported BE2 Adapter Family\n");
1244                 break;
1245         case BE_DEVICE_ID2:
1246         case OC_DEVICE_ID3:
1247                 return snprintf(buf, PAGE_SIZE, "BE3-R Adapter Family\n");
1248                 break;
1249         case OC_SKH_ID1:
1250                 return snprintf(buf, PAGE_SIZE, "Skyhawk-R Adapter Family\n");
1251                 break;
1252         default:
1253                 return snprintf(buf, PAGE_SIZE,
1254                                 "Unknown Adapter Family: 0x%x\n", dev_id);
1255                 break;
1256         }
1257 }
1258
1259 /**
1260  * beiscsi_phys_port()- Display Physical Port Identifier
1261  * @dev: ptr to device not used.
1262  * @attr: device attribute, not used.
1263  * @buf: contains formatted text port identifier
1264  *
1265  * return
1266  * size of the formatted string
1267  **/
1268 ssize_t
1269 beiscsi_phys_port_disp(struct device *dev, struct device_attribute *attr,
1270                          char *buf)
1271 {
1272         struct Scsi_Host *shost = class_to_shost(dev);
1273         struct beiscsi_hba *phba = iscsi_host_priv(shost);
1274
1275         return snprintf(buf, PAGE_SIZE, "Port Identifier : %u\n",
1276                         phba->fw_config.phys_port);
1277 }
1278
1279 void beiscsi_offload_cxn_v0(struct beiscsi_offload_params *params,
1280                              struct wrb_handle *pwrb_handle,
1281                              struct be_mem_descriptor *mem_descr,
1282                              struct hwi_wrb_context *pwrb_context)
1283 {
1284         struct iscsi_wrb *pwrb = pwrb_handle->pwrb;
1285
1286         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1287                       max_send_data_segment_length, pwrb,
1288                       params->dw[offsetof(struct amap_beiscsi_offload_params,
1289                       max_send_data_segment_length) / 32]);
1290         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
1291                       BE_TGT_CTX_UPDT_CMD);
1292         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1293                       first_burst_length,
1294                       pwrb,
1295                       params->dw[offsetof(struct amap_beiscsi_offload_params,
1296                       first_burst_length) / 32]);
1297         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
1298                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1299                       erl) / 32] & OFFLD_PARAMS_ERL));
1300         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
1301                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1302                        dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
1303         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
1304                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1305                       hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
1306         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
1307                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1308                       ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
1309         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
1310                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1311                       imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
1312         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
1313                       pwrb,
1314                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1315                       exp_statsn) / 32] + 1));
1316         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
1317                       pwrb, pwrb_handle->wrb_index);
1318
1319         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1320                       max_burst_length, pwrb, params->dw[offsetof
1321                       (struct amap_beiscsi_offload_params,
1322                       max_burst_length) / 32]);
1323
1324         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
1325                       pwrb, pwrb_handle->wrb_index);
1326         if (pwrb_context->plast_wrb)
1327                 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1328                               ptr2nextwrb,
1329                               pwrb_context->plast_wrb,
1330                               pwrb_handle->wrb_index);
1331         pwrb_context->plast_wrb = pwrb;
1332
1333         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1334                       session_state, pwrb, 0);
1335         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
1336                       pwrb, 1);
1337         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
1338                       pwrb, 0);
1339         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
1340                       0);
1341
1342         mem_descr += ISCSI_MEM_GLOBAL_HEADER;
1343         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1344                       pad_buffer_addr_hi, pwrb,
1345                       mem_descr->mem_array[0].bus_address.u.a32.address_hi);
1346         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
1347                       pad_buffer_addr_lo, pwrb,
1348                       mem_descr->mem_array[0].bus_address.u.a32.address_lo);
1349 }
1350
1351 void beiscsi_offload_cxn_v2(struct beiscsi_offload_params *params,
1352                              struct wrb_handle *pwrb_handle,
1353                              struct hwi_wrb_context *pwrb_context)
1354 {
1355         struct iscsi_wrb *pwrb = pwrb_handle->pwrb;
1356
1357         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1358                       max_burst_length, pwrb, params->dw[offsetof
1359                       (struct amap_beiscsi_offload_params,
1360                       max_burst_length) / 32]);
1361         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1362                       type, pwrb,
1363                       BE_TGT_CTX_UPDT_CMD);
1364         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1365                       ptr2nextwrb,
1366                       pwrb, pwrb_handle->wrb_index);
1367         if (pwrb_context->plast_wrb)
1368                 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1369                               ptr2nextwrb,
1370                               pwrb_context->plast_wrb,
1371                               pwrb_handle->wrb_index);
1372         pwrb_context->plast_wrb = pwrb;
1373
1374         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, wrb_idx,
1375                       pwrb, pwrb_handle->wrb_index);
1376         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1377                       max_send_data_segment_length, pwrb,
1378                       params->dw[offsetof(struct amap_beiscsi_offload_params,
1379                       max_send_data_segment_length) / 32]);
1380         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1381                       first_burst_length, pwrb,
1382                       params->dw[offsetof(struct amap_beiscsi_offload_params,
1383                       first_burst_length) / 32]);
1384         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1385                       max_recv_dataseg_len, pwrb,
1386                       params->dw[offsetof(struct amap_beiscsi_offload_params,
1387                       max_recv_data_segment_length) / 32]);
1388         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1389                       max_cxns, pwrb, BEISCSI_MAX_CXNS);
1390         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, erl, pwrb,
1391                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1392                       erl) / 32] & OFFLD_PARAMS_ERL));
1393         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, dde, pwrb,
1394                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1395                       dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
1396         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, hde, pwrb,
1397                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1398                       hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
1399         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1400                       ir2t, pwrb,
1401                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1402                       ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
1403         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, imd, pwrb,
1404                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1405                       imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
1406         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1407                       data_seq_inorder,
1408                       pwrb,
1409                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1410                       data_seq_inorder) / 32] &
1411                       OFFLD_PARAMS_DATA_SEQ_INORDER) >> 6);
1412         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2,
1413                       pdu_seq_inorder,
1414                       pwrb,
1415                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1416                       pdu_seq_inorder) / 32] &
1417                       OFFLD_PARAMS_PDU_SEQ_INORDER) >> 7);
1418         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, max_r2t,
1419                       pwrb,
1420                       (params->dw[offsetof(struct amap_beiscsi_offload_params,
1421                       max_r2t) / 32] &
1422                       OFFLD_PARAMS_MAX_R2T) >> 8);
1423         AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb_v2, stat_sn,
1424                       pwrb,
1425                      (params->dw[offsetof(struct amap_beiscsi_offload_params,
1426                       exp_statsn) / 32] + 1));
1427 }
1428
1429 unsigned int beiscsi_invalidate_cxn(struct beiscsi_hba *phba,
1430                                     struct beiscsi_endpoint *beiscsi_ep)
1431 {
1432         struct be_invalidate_connection_params_in *req;
1433         struct be_ctrl_info *ctrl = &phba->ctrl;
1434         struct be_mcc_wrb *wrb;
1435         unsigned int tag = 0;
1436
1437         mutex_lock(&ctrl->mbox_lock);
1438         wrb = alloc_mcc_wrb(phba, &tag);
1439         if (!wrb) {
1440                 mutex_unlock(&ctrl->mbox_lock);
1441                 return 0;
1442         }
1443
1444         req = embedded_payload(wrb);
1445         be_wrb_hdr_prepare(wrb, sizeof(union be_invalidate_connection_params),
1446                            true, 0);
1447         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI_INI,
1448                            OPCODE_ISCSI_INI_DRIVER_INVALIDATE_CONNECTION,
1449                            sizeof(*req));
1450         req->session_handle = beiscsi_ep->fw_handle;
1451         req->cid = beiscsi_ep->ep_cid;
1452         if (beiscsi_ep->conn)
1453                 req->cleanup_type = BE_CLEANUP_TYPE_INVALIDATE;
1454         else
1455                 req->cleanup_type = BE_CLEANUP_TYPE_ISSUE_TCP_RST;
1456         /**
1457          * 0 - non-persistent targets
1458          * 1 - save session info on flash
1459          */
1460         req->save_cfg = 0;
1461         be_mcc_notify(phba, tag);
1462         mutex_unlock(&ctrl->mbox_lock);
1463         return tag;
1464 }
1465
1466 unsigned int beiscsi_upload_cxn(struct beiscsi_hba *phba,
1467                                 struct beiscsi_endpoint *beiscsi_ep)
1468 {
1469         struct be_ctrl_info *ctrl = &phba->ctrl;
1470         struct be_mcc_wrb *wrb;
1471         struct be_tcp_upload_params_in *req;
1472         unsigned int tag;
1473
1474         mutex_lock(&ctrl->mbox_lock);
1475         wrb = alloc_mcc_wrb(phba, &tag);
1476         if (!wrb) {
1477                 mutex_unlock(&ctrl->mbox_lock);
1478                 return 0;
1479         }
1480
1481         req = embedded_payload(wrb);
1482         be_wrb_hdr_prepare(wrb, sizeof(union be_tcp_upload_params), true, 0);
1483         be_cmd_hdr_prepare(&req->hdr, CMD_COMMON_TCP_UPLOAD,
1484                            OPCODE_COMMON_TCP_UPLOAD, sizeof(*req));
1485         req->id = beiscsi_ep->ep_cid;
1486         if (beiscsi_ep->conn)
1487                 req->upload_type = BE_UPLOAD_TYPE_GRACEFUL;
1488         else
1489                 req->upload_type = BE_UPLOAD_TYPE_ABORT;
1490         be_mcc_notify(phba, tag);
1491         mutex_unlock(&ctrl->mbox_lock);
1492         return tag;
1493 }
1494
1495 int beiscsi_mgmt_invalidate_icds(struct beiscsi_hba *phba,
1496                                  struct invldt_cmd_tbl *inv_tbl,
1497                                  unsigned int nents)
1498 {
1499         struct be_ctrl_info *ctrl = &phba->ctrl;
1500         struct invldt_cmds_params_in *req;
1501         struct be_dma_mem nonemb_cmd;
1502         struct be_mcc_wrb *wrb;
1503         unsigned int i, tag;
1504         struct be_sge *sge;
1505         int rc;
1506
1507         if (!nents || nents > BE_INVLDT_CMD_TBL_SZ)
1508                 return -EINVAL;
1509
1510         nonemb_cmd.size = sizeof(union be_invldt_cmds_params);
1511         nonemb_cmd.va = pci_zalloc_consistent(phba->ctrl.pdev,
1512                                               nonemb_cmd.size,
1513                                               &nonemb_cmd.dma);
1514         if (!nonemb_cmd.va) {
1515                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
1516                             "BM_%d : invldt_cmds_params alloc failed\n");
1517                 return -ENOMEM;
1518         }
1519
1520         mutex_lock(&ctrl->mbox_lock);
1521         wrb = alloc_mcc_wrb(phba, &tag);
1522         if (!wrb) {
1523                 mutex_unlock(&ctrl->mbox_lock);
1524                 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1525                                     nonemb_cmd.va, nonemb_cmd.dma);
1526                 return -ENOMEM;
1527         }
1528
1529         req = nonemb_cmd.va;
1530         be_wrb_hdr_prepare(wrb, nonemb_cmd.size, false, 1);
1531         be_cmd_hdr_prepare(&req->hdr, CMD_SUBSYSTEM_ISCSI,
1532                         OPCODE_COMMON_ISCSI_ERROR_RECOVERY_INVALIDATE_COMMANDS,
1533                         sizeof(*req));
1534         req->ref_handle = 0;
1535         req->cleanup_type = CMD_ISCSI_COMMAND_INVALIDATE;
1536         for (i = 0; i < nents; i++) {
1537                 req->table[i].icd = inv_tbl[i].icd;
1538                 req->table[i].cid = inv_tbl[i].cid;
1539                 req->icd_count++;
1540         }
1541         sge = nonembedded_sgl(wrb);
1542         sge->pa_hi = cpu_to_le32(upper_32_bits(nonemb_cmd.dma));
1543         sge->pa_lo = cpu_to_le32(lower_32_bits(nonemb_cmd.dma));
1544         sge->len = cpu_to_le32(nonemb_cmd.size);
1545
1546         be_mcc_notify(phba, tag);
1547         mutex_unlock(&ctrl->mbox_lock);
1548
1549         rc = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
1550         if (rc != -EBUSY)
1551                 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1552                                     nonemb_cmd.va, nonemb_cmd.dma);
1553         return rc;
1554 }