]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/be2iscsi/be_iscsi.c
scsi: be2iscsi: Check all zeroes IP before issuing IOCTL
[karo-tx-linux.git] / drivers / scsi / be2iscsi / be_iscsi.c
1 /**
2  * Copyright (C) 2005 - 2015 Emulex
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 (jayamohan.kallickal@avagotech.com)
11  *
12  * Contact Information:
13  * linux-drivers@avagotech.com
14  *
15  * Emulex
16  * 3333 Susan Street
17  * Costa Mesa, CA 92626
18  */
19
20 #include <scsi/libiscsi.h>
21 #include <scsi/scsi_transport_iscsi.h>
22 #include <scsi/scsi_transport.h>
23 #include <scsi/scsi_cmnd.h>
24 #include <scsi/scsi_device.h>
25 #include <scsi/scsi_host.h>
26 #include <scsi/scsi_netlink.h>
27 #include <net/netlink.h>
28 #include <scsi/scsi.h>
29
30 #include "be_iscsi.h"
31
32 extern struct iscsi_transport beiscsi_iscsi_transport;
33
34 /**
35  * beiscsi_session_create - creates a new iscsi session
36  * @cmds_max: max commands supported
37  * @qdepth: max queue depth supported
38  * @initial_cmdsn: initial iscsi CMDSN
39  */
40 struct iscsi_cls_session *beiscsi_session_create(struct iscsi_endpoint *ep,
41                                                  u16 cmds_max,
42                                                  u16 qdepth,
43                                                  u32 initial_cmdsn)
44 {
45         struct Scsi_Host *shost;
46         struct beiscsi_endpoint *beiscsi_ep;
47         struct iscsi_cls_session *cls_session;
48         struct beiscsi_hba *phba;
49         struct iscsi_session *sess;
50         struct beiscsi_session *beiscsi_sess;
51         struct beiscsi_io_task *io_task;
52
53
54         if (!ep) {
55                 printk(KERN_ERR
56                        "beiscsi_session_create: invalid ep\n");
57                 return NULL;
58         }
59         beiscsi_ep = ep->dd_data;
60         phba = beiscsi_ep->phba;
61
62         if (phba->state & BE_ADAPTER_PCI_ERR) {
63                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
64                             "BS_%d : PCI_ERROR Recovery\n");
65                 return NULL;
66         } else {
67                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
68                             "BS_%d : In beiscsi_session_create\n");
69         }
70
71         if (cmds_max > beiscsi_ep->phba->params.wrbs_per_cxn) {
72                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
73                             "BS_%d : Cannot handle %d cmds."
74                             "Max cmds per session supported is %d. Using %d."
75                             "\n", cmds_max,
76                             beiscsi_ep->phba->params.wrbs_per_cxn,
77                             beiscsi_ep->phba->params.wrbs_per_cxn);
78
79                 cmds_max = beiscsi_ep->phba->params.wrbs_per_cxn;
80         }
81
82         shost = phba->shost;
83         cls_session = iscsi_session_setup(&beiscsi_iscsi_transport,
84                                           shost, cmds_max,
85                                           sizeof(*beiscsi_sess),
86                                           sizeof(*io_task),
87                                           initial_cmdsn, ISCSI_MAX_TARGET);
88         if (!cls_session)
89                 return NULL;
90         sess = cls_session->dd_data;
91         beiscsi_sess = sess->dd_data;
92         beiscsi_sess->bhs_pool =  pci_pool_create("beiscsi_bhs_pool",
93                                                    phba->pcidev,
94                                                    sizeof(struct be_cmd_bhs),
95                                                    64, 0);
96         if (!beiscsi_sess->bhs_pool)
97                 goto destroy_sess;
98
99         return cls_session;
100 destroy_sess:
101         iscsi_session_teardown(cls_session);
102         return NULL;
103 }
104
105 /**
106  * beiscsi_session_destroy - destroys iscsi session
107  * @cls_session:        pointer to iscsi cls session
108  *
109  * Destroys iSCSI session instance and releases
110  * resources allocated for it.
111  */
112 void beiscsi_session_destroy(struct iscsi_cls_session *cls_session)
113 {
114         struct iscsi_session *sess = cls_session->dd_data;
115         struct beiscsi_session *beiscsi_sess = sess->dd_data;
116
117         printk(KERN_INFO "In beiscsi_session_destroy\n");
118         pci_pool_destroy(beiscsi_sess->bhs_pool);
119         iscsi_session_teardown(cls_session);
120 }
121
122 /**
123  * beiscsi_conn_create - create an instance of iscsi connection
124  * @cls_session: ptr to iscsi_cls_session
125  * @cid: iscsi cid
126  */
127 struct iscsi_cls_conn *
128 beiscsi_conn_create(struct iscsi_cls_session *cls_session, u32 cid)
129 {
130         struct beiscsi_hba *phba;
131         struct Scsi_Host *shost;
132         struct iscsi_cls_conn *cls_conn;
133         struct beiscsi_conn *beiscsi_conn;
134         struct iscsi_conn *conn;
135         struct iscsi_session *sess;
136         struct beiscsi_session *beiscsi_sess;
137
138         shost = iscsi_session_to_shost(cls_session);
139         phba = iscsi_host_priv(shost);
140
141         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
142                     "BS_%d : In beiscsi_conn_create ,cid"
143                     "from iscsi layer=%d\n", cid);
144
145         cls_conn = iscsi_conn_setup(cls_session, sizeof(*beiscsi_conn), cid);
146         if (!cls_conn)
147                 return NULL;
148
149         conn = cls_conn->dd_data;
150         beiscsi_conn = conn->dd_data;
151         beiscsi_conn->ep = NULL;
152         beiscsi_conn->phba = phba;
153         beiscsi_conn->conn = conn;
154         sess = cls_session->dd_data;
155         beiscsi_sess = sess->dd_data;
156         beiscsi_conn->beiscsi_sess = beiscsi_sess;
157         return cls_conn;
158 }
159
160 /**
161  * beiscsi_bindconn_cid - Bind the beiscsi_conn with phba connection table
162  * @beiscsi_conn: The pointer to  beiscsi_conn structure
163  * @phba: The phba instance
164  * @cid: The cid to free
165  */
166 static int beiscsi_bindconn_cid(struct beiscsi_hba *phba,
167                                 struct beiscsi_conn *beiscsi_conn,
168                                 unsigned int cid)
169 {
170         uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
171
172         if (phba->conn_table[cri_index]) {
173                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
174                             "BS_%d : Connection table already occupied. Detected clash\n");
175
176                 return -EINVAL;
177         } else {
178                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
179                             "BS_%d : phba->conn_table[%d]=%p(beiscsi_conn)\n",
180                             cri_index, beiscsi_conn);
181
182                 phba->conn_table[cri_index] = beiscsi_conn;
183         }
184         return 0;
185 }
186
187 /**
188  * beiscsi_conn_bind - Binds iscsi session/connection with TCP connection
189  * @cls_session: pointer to iscsi cls session
190  * @cls_conn: pointer to iscsi cls conn
191  * @transport_fd: EP handle(64 bit)
192  *
193  * This function binds the TCP Conn with iSCSI Connection and Session.
194  */
195 int beiscsi_conn_bind(struct iscsi_cls_session *cls_session,
196                       struct iscsi_cls_conn *cls_conn,
197                       u64 transport_fd, int is_leading)
198 {
199         struct iscsi_conn *conn = cls_conn->dd_data;
200         struct beiscsi_conn *beiscsi_conn = conn->dd_data;
201         struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
202         struct beiscsi_hba *phba = iscsi_host_priv(shost);
203         struct hwi_controller *phwi_ctrlr = phba->phwi_ctrlr;
204         struct hwi_wrb_context *pwrb_context;
205         struct beiscsi_endpoint *beiscsi_ep;
206         struct iscsi_endpoint *ep;
207
208         ep = iscsi_lookup_endpoint(transport_fd);
209         if (!ep)
210                 return -EINVAL;
211
212         beiscsi_ep = ep->dd_data;
213
214         if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
215                 return -EINVAL;
216
217         if (beiscsi_ep->phba != phba) {
218                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
219                             "BS_%d : beiscsi_ep->hba=%p not equal to phba=%p\n",
220                             beiscsi_ep->phba, phba);
221
222                 return -EEXIST;
223         }
224
225         pwrb_context = &phwi_ctrlr->wrb_context[BE_GET_CRI_FROM_CID(
226                                                 beiscsi_ep->ep_cid)];
227
228         beiscsi_conn->beiscsi_conn_cid = beiscsi_ep->ep_cid;
229         beiscsi_conn->ep = beiscsi_ep;
230         beiscsi_ep->conn = beiscsi_conn;
231         beiscsi_conn->doorbell_offset = pwrb_context->doorbell_offset;
232
233         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
234                     "BS_%d : beiscsi_conn=%p conn=%p ep_cid=%d\n",
235                     beiscsi_conn, conn, beiscsi_ep->ep_cid);
236
237         return beiscsi_bindconn_cid(phba, beiscsi_conn, beiscsi_ep->ep_cid);
238 }
239
240 static int beiscsi_iface_create_ipv4(struct beiscsi_hba *phba)
241 {
242         if (phba->ipv4_iface)
243                 return 0;
244
245         phba->ipv4_iface = iscsi_create_iface(phba->shost,
246                                               &beiscsi_iscsi_transport,
247                                               ISCSI_IFACE_TYPE_IPV4,
248                                               0, 0);
249         if (!phba->ipv4_iface) {
250                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
251                             "BS_%d : Could not "
252                             "create default IPv4 address.\n");
253                 return -ENODEV;
254         }
255
256         return 0;
257 }
258
259 static int beiscsi_iface_create_ipv6(struct beiscsi_hba *phba)
260 {
261         if (phba->ipv6_iface)
262                 return 0;
263
264         phba->ipv6_iface = iscsi_create_iface(phba->shost,
265                                               &beiscsi_iscsi_transport,
266                                               ISCSI_IFACE_TYPE_IPV6,
267                                               0, 0);
268         if (!phba->ipv6_iface) {
269                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
270                             "BS_%d : Could not "
271                             "create default IPv6 address.\n");
272                 return -ENODEV;
273         }
274
275         return 0;
276 }
277
278 void beiscsi_iface_create_default(struct beiscsi_hba *phba)
279 {
280         struct be_cmd_get_if_info_resp *if_info;
281
282         if (!beiscsi_if_get_info(phba, BEISCSI_IP_TYPE_V4, &if_info)) {
283                 beiscsi_iface_create_ipv4(phba);
284                 kfree(if_info);
285         }
286
287         if (!beiscsi_if_get_info(phba, BEISCSI_IP_TYPE_V6, &if_info)) {
288                 beiscsi_iface_create_ipv6(phba);
289                 kfree(if_info);
290         }
291 }
292
293 void beiscsi_iface_destroy_default(struct beiscsi_hba *phba)
294 {
295         if (phba->ipv6_iface) {
296                 iscsi_destroy_iface(phba->ipv6_iface);
297                 phba->ipv6_iface = NULL;
298         }
299         if (phba->ipv4_iface) {
300                 iscsi_destroy_iface(phba->ipv4_iface);
301                 phba->ipv4_iface = NULL;
302         }
303 }
304
305 /**
306  * beiscsi_set_vlan_tag()- Set the VLAN TAG
307  * @shost: Scsi Host for the driver instance
308  * @iface_param: Interface paramters
309  *
310  * Set the VLAN TAG for the adapter or disable
311  * the VLAN config
312  *
313  * returns
314  *      Success: 0
315  *      Failure: Non-Zero Value
316  **/
317 static int
318 beiscsi_iface_config_vlan(struct Scsi_Host *shost,
319                           struct iscsi_iface_param_info *iface_param)
320 {
321         struct beiscsi_hba *phba = iscsi_host_priv(shost);
322         int ret = -EPERM;
323
324         switch (iface_param->param) {
325         case ISCSI_NET_PARAM_VLAN_ENABLED:
326                 ret = 0;
327                 if (iface_param->value[0] != ISCSI_VLAN_ENABLE)
328                         ret = beiscsi_if_set_vlan(phba, BEISCSI_VLAN_DISABLE);
329                 break;
330         case ISCSI_NET_PARAM_VLAN_TAG:
331                 ret = beiscsi_if_set_vlan(phba,
332                                           *((uint16_t *)iface_param->value));
333                 break;
334         }
335         return ret;
336 }
337
338
339 static int
340 beiscsi_iface_config_ipv4(struct Scsi_Host *shost,
341                           struct iscsi_iface_param_info *info,
342                           void *data, uint32_t dt_len)
343 {
344         struct beiscsi_hba *phba = iscsi_host_priv(shost);
345         u8 *ip = NULL, *subnet = NULL, *gw;
346         struct nlattr *nla;
347         int ret = -EPERM;
348
349         /* Check the param */
350         switch (info->param) {
351         case ISCSI_NET_PARAM_IFACE_ENABLE:
352                 if (info->value[0] == ISCSI_IFACE_ENABLE)
353                         ret = beiscsi_iface_create_ipv4(phba);
354                 else {
355                         iscsi_destroy_iface(phba->ipv4_iface);
356                         phba->ipv4_iface = NULL;
357                 }
358                 break;
359         case ISCSI_NET_PARAM_IPV4_GW:
360                 gw = info->value;
361                 ret = beiscsi_if_set_gw(phba, BEISCSI_IP_TYPE_V4, gw);
362                 break;
363         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
364                 if (info->value[0] == ISCSI_BOOTPROTO_DHCP)
365                         ret = beiscsi_if_en_dhcp(phba, BEISCSI_IP_TYPE_V4);
366                 else if (info->value[0] == ISCSI_BOOTPROTO_STATIC)
367                         /* release DHCP IP address */
368                         ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
369                                                    NULL, NULL);
370                 else
371                         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
372                                     "BS_%d : Invalid BOOTPROTO: %d\n",
373                                     info->value[0]);
374                 break;
375         case ISCSI_NET_PARAM_IPV4_ADDR:
376                 ip = info->value;
377                 nla = nla_find(data, dt_len, ISCSI_NET_PARAM_IPV4_SUBNET);
378                 if (nla) {
379                         info = nla_data(nla);
380                         subnet = info->value;
381                 }
382                 ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
383                                            ip, subnet);
384                 break;
385         case ISCSI_NET_PARAM_IPV4_SUBNET:
386                 /*
387                  * OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR ioctl needs IP
388                  * and subnet both. Find IP to be applied for this subnet.
389                  */
390                 subnet = info->value;
391                 nla = nla_find(data, dt_len, ISCSI_NET_PARAM_IPV4_ADDR);
392                 if (nla) {
393                         info = nla_data(nla);
394                         ip = info->value;
395                 }
396                 ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V4,
397                                            ip, subnet);
398                 break;
399         }
400
401         return ret;
402 }
403
404 static int
405 beiscsi_iface_config_ipv6(struct Scsi_Host *shost,
406                           struct iscsi_iface_param_info *iface_param,
407                           void *data, uint32_t dt_len)
408 {
409         struct beiscsi_hba *phba = iscsi_host_priv(shost);
410         int ret = -EPERM;
411
412         switch (iface_param->param) {
413         case ISCSI_NET_PARAM_IFACE_ENABLE:
414                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE)
415                         ret = beiscsi_iface_create_ipv6(phba);
416                 else {
417                         iscsi_destroy_iface(phba->ipv6_iface);
418                         phba->ipv6_iface = NULL;
419                 }
420                 break;
421         case ISCSI_NET_PARAM_IPV6_ADDR:
422                 ret = beiscsi_if_en_static(phba, BEISCSI_IP_TYPE_V6,
423                                            iface_param->value, NULL);
424                 break;
425         }
426
427         return ret;
428 }
429
430 int beiscsi_iface_set_param(struct Scsi_Host *shost,
431                             void *data, uint32_t dt_len)
432 {
433         struct iscsi_iface_param_info *iface_param = NULL;
434         struct beiscsi_hba *phba = iscsi_host_priv(shost);
435         struct nlattr *attrib;
436         uint32_t rm_len = dt_len;
437         int ret;
438
439         if (phba->state & BE_ADAPTER_PCI_ERR) {
440                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
441                             "BS_%d : In PCI_ERROR Recovery\n");
442                 return -EBUSY;
443         }
444
445         /* update interface_handle */
446         ret = beiscsi_if_get_handle(phba);
447         if (ret) {
448                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
449                             "BS_%d : Getting Interface Handle Failed\n");
450                 return ret;
451         }
452
453         nla_for_each_attr(attrib, data, dt_len, rm_len) {
454                 iface_param = nla_data(attrib);
455
456                 if (iface_param->param_type != ISCSI_NET_PARAM)
457                         continue;
458
459                 /*
460                  * BE2ISCSI only supports 1 interface
461                  */
462                 if (iface_param->iface_num) {
463                         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
464                                     "BS_%d : Invalid iface_num %d."
465                                     "Only iface_num 0 is supported.\n",
466                                     iface_param->iface_num);
467
468                         return -EINVAL;
469                 }
470
471                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
472                             "BS_%d : %s.0 set param %d",
473                             (iface_param->iface_type == ISCSI_IFACE_TYPE_IPV4) ?
474                             "ipv4" : "ipv6", iface_param->param);
475
476                 ret = -EPERM;
477                 switch (iface_param->param) {
478                 case ISCSI_NET_PARAM_VLAN_ENABLED:
479                 case ISCSI_NET_PARAM_VLAN_TAG:
480                         ret = beiscsi_iface_config_vlan(shost, iface_param);
481                         break;
482                 default:
483                         switch (iface_param->iface_type) {
484                         case ISCSI_IFACE_TYPE_IPV4:
485                                 ret = beiscsi_iface_config_ipv4(shost,
486                                                                 iface_param,
487                                                                 data, dt_len);
488                                 break;
489                         case ISCSI_IFACE_TYPE_IPV6:
490                                 ret = beiscsi_iface_config_ipv6(shost,
491                                                                 iface_param,
492                                                                 data, dt_len);
493                                 break;
494                         }
495                 }
496
497                 if (ret == -EPERM) {
498                         __beiscsi_log(phba, KERN_ERR,
499                                       "BS_%d : %s.0 set param %d not permitted",
500                                       (iface_param->iface_type ==
501                                        ISCSI_IFACE_TYPE_IPV4) ? "ipv4" : "ipv6",
502                                       iface_param->param);
503                         ret = 0;
504                 }
505                 if (ret)
506                         break;
507         }
508
509         return ret;
510 }
511
512 static int __beiscsi_iface_get_param(struct beiscsi_hba *phba,
513                                      struct iscsi_iface *iface,
514                                      int param, char *buf)
515 {
516         struct be_cmd_get_if_info_resp *if_info;
517         int len, ip_type = BEISCSI_IP_TYPE_V4;
518
519         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
520                 ip_type = BEISCSI_IP_TYPE_V6;
521
522         len = beiscsi_if_get_info(phba, ip_type, &if_info);
523         if (len)
524                 return len;
525
526         switch (param) {
527         case ISCSI_NET_PARAM_IPV4_ADDR:
528                 len = sprintf(buf, "%pI4\n", if_info->ip_addr.addr);
529                 break;
530         case ISCSI_NET_PARAM_IPV6_ADDR:
531                 len = sprintf(buf, "%pI6\n", if_info->ip_addr.addr);
532                 break;
533         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
534                 if (!if_info->dhcp_state)
535                         len = sprintf(buf, "static\n");
536                 else
537                         len = sprintf(buf, "dhcp\n");
538                 break;
539         case ISCSI_NET_PARAM_IPV4_SUBNET:
540                 len = sprintf(buf, "%pI4\n", if_info->ip_addr.subnet_mask);
541                 break;
542         case ISCSI_NET_PARAM_VLAN_ENABLED:
543                 len = sprintf(buf, "%s\n",
544                               (if_info->vlan_priority == BEISCSI_VLAN_DISABLE) ?
545                               "disable" : "enable");
546                 break;
547         case ISCSI_NET_PARAM_VLAN_ID:
548                 if (if_info->vlan_priority == BEISCSI_VLAN_DISABLE)
549                         len = -EINVAL;
550                 else
551                         len = sprintf(buf, "%d\n",
552                                       (if_info->vlan_priority &
553                                        ISCSI_MAX_VLAN_ID));
554                 break;
555         case ISCSI_NET_PARAM_VLAN_PRIORITY:
556                 if (if_info->vlan_priority == BEISCSI_VLAN_DISABLE)
557                         len = -EINVAL;
558                 else
559                         len = sprintf(buf, "%d\n",
560                                       ((if_info->vlan_priority >> 13) &
561                                        ISCSI_MAX_VLAN_PRIORITY));
562                 break;
563         default:
564                 WARN_ON(1);
565         }
566
567         kfree(if_info);
568         return len;
569 }
570
571 int beiscsi_iface_get_param(struct iscsi_iface *iface,
572                             enum iscsi_param_type param_type,
573                             int param, char *buf)
574 {
575         struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
576         struct beiscsi_hba *phba = iscsi_host_priv(shost);
577         struct be_cmd_get_def_gateway_resp gateway;
578         int len = -EPERM;
579
580         if (param_type != ISCSI_NET_PARAM)
581                 return 0;
582         if (phba->state & BE_ADAPTER_PCI_ERR) {
583                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
584                             "BS_%d : In PCI_ERROR Recovery\n");
585                 return -EBUSY;
586         }
587
588         switch (param) {
589         case ISCSI_NET_PARAM_IPV4_ADDR:
590         case ISCSI_NET_PARAM_IPV4_SUBNET:
591         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
592         case ISCSI_NET_PARAM_IPV6_ADDR:
593         case ISCSI_NET_PARAM_VLAN_ENABLED:
594         case ISCSI_NET_PARAM_VLAN_ID:
595         case ISCSI_NET_PARAM_VLAN_PRIORITY:
596                 len = __beiscsi_iface_get_param(phba, iface, param, buf);
597                 break;
598         case ISCSI_NET_PARAM_IFACE_ENABLE:
599                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
600                         len = sprintf(buf, "%s\n",
601                                       phba->ipv4_iface ? "enable" : "disable");
602                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
603                         len = sprintf(buf, "%s\n",
604                                       phba->ipv6_iface ? "enable" : "disable");
605                 break;
606         case ISCSI_NET_PARAM_IPV4_GW:
607                 memset(&gateway, 0, sizeof(gateway));
608                 len = beiscsi_if_get_gw(phba, BEISCSI_IP_TYPE_V4, &gateway);
609                 if (!len)
610                         len = sprintf(buf, "%pI4\n", &gateway.ip_addr.addr);
611                 break;
612         }
613
614         return len;
615 }
616
617 /**
618  * beiscsi_ep_get_param - get the iscsi parameter
619  * @ep: pointer to iscsi ep
620  * @param: parameter type identifier
621  * @buf: buffer pointer
622  *
623  * returns iscsi parameter
624  */
625 int beiscsi_ep_get_param(struct iscsi_endpoint *ep,
626                            enum iscsi_param param, char *buf)
627 {
628         struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
629         int len;
630
631         beiscsi_log(beiscsi_ep->phba, KERN_INFO,
632                     BEISCSI_LOG_CONFIG,
633                     "BS_%d : In beiscsi_ep_get_param,"
634                     " param= %d\n", param);
635
636         switch (param) {
637         case ISCSI_PARAM_CONN_PORT:
638                 len = sprintf(buf, "%hu\n", beiscsi_ep->dst_tcpport);
639                 break;
640         case ISCSI_PARAM_CONN_ADDRESS:
641                 if (beiscsi_ep->ip_type == BEISCSI_IP_TYPE_V4)
642                         len = sprintf(buf, "%pI4\n", &beiscsi_ep->dst_addr);
643                 else
644                         len = sprintf(buf, "%pI6\n", &beiscsi_ep->dst6_addr);
645                 break;
646         default:
647                 len = -EPERM;
648         }
649         return len;
650 }
651
652 int beiscsi_set_param(struct iscsi_cls_conn *cls_conn,
653                       enum iscsi_param param, char *buf, int buflen)
654 {
655         struct iscsi_conn *conn = cls_conn->dd_data;
656         struct iscsi_session *session = conn->session;
657         struct beiscsi_hba *phba = NULL;
658         int ret;
659
660         phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
661         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
662                     "BS_%d : In beiscsi_conn_set_param,"
663                     " param= %d\n", param);
664
665         ret = iscsi_set_param(cls_conn, param, buf, buflen);
666         if (ret)
667                 return ret;
668         /*
669          * If userspace tried to set the value to higher than we can
670          * support override here.
671          */
672         switch (param) {
673         case ISCSI_PARAM_FIRST_BURST:
674                 if (session->first_burst > 8192)
675                         session->first_burst = 8192;
676                 break;
677         case ISCSI_PARAM_MAX_RECV_DLENGTH:
678                 if (conn->max_recv_dlength > 65536)
679                         conn->max_recv_dlength = 65536;
680                 break;
681         case ISCSI_PARAM_MAX_BURST:
682                 if (session->max_burst > 262144)
683                         session->max_burst = 262144;
684                 break;
685         case ISCSI_PARAM_MAX_XMIT_DLENGTH:
686                 if (conn->max_xmit_dlength > 65536)
687                         conn->max_xmit_dlength = 65536;
688         default:
689                 return 0;
690         }
691
692         return 0;
693 }
694
695 /**
696  * beiscsi_get_initname - Read Initiator Name from flash
697  * @buf: buffer bointer
698  * @phba: The device priv structure instance
699  *
700  * returns number of bytes
701  */
702 static int beiscsi_get_initname(char *buf, struct beiscsi_hba *phba)
703 {
704         int rc;
705         unsigned int tag;
706         struct be_mcc_wrb *wrb;
707         struct be_cmd_hba_name *resp;
708
709         tag = be_cmd_get_initname(phba);
710         if (!tag) {
711                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
712                             "BS_%d : Getting Initiator Name Failed\n");
713
714                 return -EBUSY;
715         }
716
717         rc = beiscsi_mccq_compl_wait(phba, tag, &wrb, NULL);
718         if (rc) {
719                 beiscsi_log(phba, KERN_ERR,
720                             BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
721                             "BS_%d : Initiator Name MBX Failed\n");
722                 return rc;
723         }
724
725         resp = embedded_payload(wrb);
726         rc = sprintf(buf, "%s\n", resp->initiator_name);
727         return rc;
728 }
729
730 /**
731  * beiscsi_get_port_state - Get the Port State
732  * @shost : pointer to scsi_host structure
733  *
734  */
735 static void beiscsi_get_port_state(struct Scsi_Host *shost)
736 {
737         struct beiscsi_hba *phba = iscsi_host_priv(shost);
738         struct iscsi_cls_host *ihost = shost->shost_data;
739
740         ihost->port_state = (phba->state & BE_ADAPTER_LINK_UP) ?
741                 ISCSI_PORT_STATE_UP : ISCSI_PORT_STATE_DOWN;
742 }
743
744 /**
745  * beiscsi_get_port_speed  - Get the Port Speed from Adapter
746  * @shost : pointer to scsi_host structure
747  *
748  */
749 static void beiscsi_get_port_speed(struct Scsi_Host *shost)
750 {
751         struct beiscsi_hba *phba = iscsi_host_priv(shost);
752         struct iscsi_cls_host *ihost = shost->shost_data;
753
754         switch (phba->port_speed) {
755         case BE2ISCSI_LINK_SPEED_10MBPS:
756                 ihost->port_speed = ISCSI_PORT_SPEED_10MBPS;
757                 break;
758         case BE2ISCSI_LINK_SPEED_100MBPS:
759                 ihost->port_speed = ISCSI_PORT_SPEED_100MBPS;
760                 break;
761         case BE2ISCSI_LINK_SPEED_1GBPS:
762                 ihost->port_speed = ISCSI_PORT_SPEED_1GBPS;
763                 break;
764         case BE2ISCSI_LINK_SPEED_10GBPS:
765                 ihost->port_speed = ISCSI_PORT_SPEED_10GBPS;
766                 break;
767         case BE2ISCSI_LINK_SPEED_25GBPS:
768                 ihost->port_speed = ISCSI_PORT_SPEED_25GBPS;
769                 break;
770         case BE2ISCSI_LINK_SPEED_40GBPS:
771                 ihost->port_speed = ISCSI_PORT_SPEED_40GBPS;
772                 break;
773         default:
774                 ihost->port_speed = ISCSI_PORT_SPEED_UNKNOWN;
775         }
776 }
777
778 /**
779  * beiscsi_get_host_param - get the iscsi parameter
780  * @shost: pointer to scsi_host structure
781  * @param: parameter type identifier
782  * @buf: buffer pointer
783  *
784  * returns host parameter
785  */
786 int beiscsi_get_host_param(struct Scsi_Host *shost,
787                            enum iscsi_host_param param, char *buf)
788 {
789         struct beiscsi_hba *phba = iscsi_host_priv(shost);
790         int status = 0;
791
792
793         if (phba->state & BE_ADAPTER_PCI_ERR) {
794                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
795                             "BS_%d : In PCI_ERROR Recovery\n");
796                 return -EBUSY;
797         } else {
798                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
799                             "BS_%d : In beiscsi_get_host_param,"
800                             " param = %d\n", param);
801         }
802
803         switch (param) {
804         case ISCSI_HOST_PARAM_HWADDRESS:
805                 status = beiscsi_get_macaddr(buf, phba);
806                 if (status < 0) {
807                         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
808                                     "BS_%d : beiscsi_get_macaddr Failed\n");
809                         return status;
810                 }
811                 break;
812         case ISCSI_HOST_PARAM_INITIATOR_NAME:
813                 status = beiscsi_get_initname(buf, phba);
814                 if (status < 0) {
815                         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
816                                     "BS_%d : Retreiving Initiator Name Failed\n");
817                         return status;
818                 }
819                 break;
820         case ISCSI_HOST_PARAM_PORT_STATE:
821                 beiscsi_get_port_state(shost);
822                 status = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
823                 break;
824         case ISCSI_HOST_PARAM_PORT_SPEED:
825                 beiscsi_get_port_speed(shost);
826                 status = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
827                 break;
828         default:
829                 return iscsi_host_get_param(shost, param, buf);
830         }
831         return status;
832 }
833
834 int beiscsi_get_macaddr(char *buf, struct beiscsi_hba *phba)
835 {
836         struct be_cmd_get_nic_conf_resp resp;
837         int rc;
838
839         if (phba->mac_addr_set)
840                 return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
841
842         memset(&resp, 0, sizeof(resp));
843         rc = mgmt_get_nic_conf(phba, &resp);
844         if (rc)
845                 return rc;
846
847         phba->mac_addr_set = true;
848         memcpy(phba->mac_address, resp.mac_address, ETH_ALEN);
849         return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
850 }
851
852 /**
853  * beiscsi_conn_get_stats - get the iscsi stats
854  * @cls_conn: pointer to iscsi cls conn
855  * @stats: pointer to iscsi_stats structure
856  *
857  * returns iscsi stats
858  */
859 void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
860                             struct iscsi_stats *stats)
861 {
862         struct iscsi_conn *conn = cls_conn->dd_data;
863         struct beiscsi_hba *phba = NULL;
864
865         phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
866         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
867                     "BS_%d : In beiscsi_conn_get_stats\n");
868
869         stats->txdata_octets = conn->txdata_octets;
870         stats->rxdata_octets = conn->rxdata_octets;
871         stats->dataout_pdus = conn->dataout_pdus_cnt;
872         stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
873         stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
874         stats->datain_pdus = conn->datain_pdus_cnt;
875         stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
876         stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
877         stats->r2t_pdus = conn->r2t_pdus_cnt;
878         stats->digest_err = 0;
879         stats->timeout_err = 0;
880         stats->custom_length = 1;
881         strcpy(stats->custom[0].desc, "eh_abort_cnt");
882         stats->custom[0].value = conn->eh_abort_cnt;
883 }
884
885 /**
886  * beiscsi_set_params_for_offld - get the parameters for offload
887  * @beiscsi_conn: pointer to beiscsi_conn
888  * @params: pointer to offload_params structure
889  */
890 static void  beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
891                                           struct beiscsi_offload_params *params)
892 {
893         struct iscsi_conn *conn = beiscsi_conn->conn;
894         struct iscsi_session *session = conn->session;
895
896         AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
897                       params, session->max_burst);
898         AMAP_SET_BITS(struct amap_beiscsi_offload_params,
899                       max_send_data_segment_length, params,
900                       conn->max_xmit_dlength);
901         AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
902                       params, session->first_burst);
903         AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
904                       session->erl);
905         AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
906                       conn->datadgst_en);
907         AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
908                       conn->hdrdgst_en);
909         AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
910                       session->initial_r2t_en);
911         AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
912                       session->imm_data_en);
913         AMAP_SET_BITS(struct amap_beiscsi_offload_params,
914                       data_seq_inorder, params,
915                       session->dataseq_inorder_en);
916         AMAP_SET_BITS(struct amap_beiscsi_offload_params,
917                       pdu_seq_inorder, params,
918                       session->pdu_inorder_en);
919         AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_r2t, params,
920                       session->max_r2t);
921         AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
922                       (conn->exp_statsn - 1));
923         AMAP_SET_BITS(struct amap_beiscsi_offload_params,
924                       max_recv_data_segment_length, params,
925                       conn->max_recv_dlength);
926
927 }
928
929 /**
930  * beiscsi_conn_start - offload of session to chip
931  * @cls_conn: pointer to beiscsi_conn
932  */
933 int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
934 {
935         struct iscsi_conn *conn = cls_conn->dd_data;
936         struct beiscsi_conn *beiscsi_conn = conn->dd_data;
937         struct beiscsi_endpoint *beiscsi_ep;
938         struct beiscsi_offload_params params;
939         struct beiscsi_hba *phba;
940
941         phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
942
943         if (phba->state & BE_ADAPTER_PCI_ERR) {
944                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
945                             "BS_%d : In PCI_ERROR Recovery\n");
946                 return -EBUSY;
947         } else {
948                 beiscsi_log(beiscsi_conn->phba, KERN_INFO,
949                             BEISCSI_LOG_CONFIG,
950                             "BS_%d : In beiscsi_conn_start\n");
951         }
952
953         memset(&params, 0, sizeof(struct beiscsi_offload_params));
954         beiscsi_ep = beiscsi_conn->ep;
955         if (!beiscsi_ep)
956                 beiscsi_log(beiscsi_conn->phba, KERN_ERR,
957                             BEISCSI_LOG_CONFIG,
958                             "BS_%d : In beiscsi_conn_start , no beiscsi_ep\n");
959
960         beiscsi_conn->login_in_progress = 0;
961         beiscsi_set_params_for_offld(beiscsi_conn, &params);
962         beiscsi_offload_connection(beiscsi_conn, &params);
963         iscsi_conn_start(cls_conn);
964         return 0;
965 }
966
967 /**
968  * beiscsi_get_cid - Allocate a cid
969  * @phba: The phba instance
970  */
971 static int beiscsi_get_cid(struct beiscsi_hba *phba)
972 {
973         unsigned short cid = 0xFFFF, cid_from_ulp;
974         struct ulp_cid_info *cid_info = NULL;
975         uint16_t cid_avlbl_ulp0, cid_avlbl_ulp1;
976
977         /* Find the ULP which has more CID available */
978         cid_avlbl_ulp0 = (phba->cid_array_info[BEISCSI_ULP0]) ?
979                           BEISCSI_ULP0_AVLBL_CID(phba) : 0;
980         cid_avlbl_ulp1 = (phba->cid_array_info[BEISCSI_ULP1]) ?
981                           BEISCSI_ULP1_AVLBL_CID(phba) : 0;
982         cid_from_ulp = (cid_avlbl_ulp0 > cid_avlbl_ulp1) ?
983                         BEISCSI_ULP0 : BEISCSI_ULP1;
984
985         if (test_bit(cid_from_ulp, (void *)&phba->fw_config.ulp_supported)) {
986                 cid_info = phba->cid_array_info[cid_from_ulp];
987                 if (!cid_info->avlbl_cids)
988                         return cid;
989
990                 cid = cid_info->cid_array[cid_info->cid_alloc++];
991
992                 if (cid_info->cid_alloc == BEISCSI_GET_CID_COUNT(
993                                            phba, cid_from_ulp))
994                         cid_info->cid_alloc = 0;
995
996                 cid_info->avlbl_cids--;
997         }
998         return cid;
999 }
1000
1001 /**
1002  * beiscsi_put_cid - Free the cid
1003  * @phba: The phba for which the cid is being freed
1004  * @cid: The cid to free
1005  */
1006 static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
1007 {
1008         uint16_t cid_post_ulp;
1009         struct hwi_controller *phwi_ctrlr;
1010         struct hwi_wrb_context *pwrb_context;
1011         struct ulp_cid_info *cid_info = NULL;
1012         uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
1013
1014         phwi_ctrlr = phba->phwi_ctrlr;
1015         pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
1016         cid_post_ulp = pwrb_context->ulp_num;
1017
1018         cid_info = phba->cid_array_info[cid_post_ulp];
1019         cid_info->avlbl_cids++;
1020
1021         cid_info->cid_array[cid_info->cid_free++] = cid;
1022         if (cid_info->cid_free == BEISCSI_GET_CID_COUNT(phba, cid_post_ulp))
1023                 cid_info->cid_free = 0;
1024 }
1025
1026 /**
1027  * beiscsi_free_ep - free endpoint
1028  * @ep: pointer to iscsi endpoint structure
1029  */
1030 static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
1031 {
1032         struct beiscsi_hba *phba = beiscsi_ep->phba;
1033         struct beiscsi_conn *beiscsi_conn;
1034
1035         beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
1036         beiscsi_ep->phba = NULL;
1037         phba->ep_array[BE_GET_CRI_FROM_CID
1038                        (beiscsi_ep->ep_cid)] = NULL;
1039
1040         /**
1041          * Check if any connection resource allocated by driver
1042          * is to be freed.This case occurs when target redirection
1043          * or connection retry is done.
1044          **/
1045         if (!beiscsi_ep->conn)
1046                 return;
1047
1048         beiscsi_conn = beiscsi_ep->conn;
1049         if (beiscsi_conn->login_in_progress) {
1050                 beiscsi_free_mgmt_task_handles(beiscsi_conn,
1051                                                beiscsi_conn->task);
1052                 beiscsi_conn->login_in_progress = 0;
1053         }
1054 }
1055
1056 /**
1057  * beiscsi_open_conn - Ask FW to open a TCP connection
1058  * @ep: endpoint to be used
1059  * @src_addr: The source IP address
1060  * @dst_addr: The Destination  IP address
1061  *
1062  * Asks the FW to open a TCP connection
1063  */
1064 static int beiscsi_open_conn(struct iscsi_endpoint *ep,
1065                              struct sockaddr *src_addr,
1066                              struct sockaddr *dst_addr, int non_blocking)
1067 {
1068         struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
1069         struct beiscsi_hba *phba = beiscsi_ep->phba;
1070         struct tcp_connect_and_offload_out *ptcpcnct_out;
1071         struct be_dma_mem nonemb_cmd;
1072         unsigned int tag, req_memsize;
1073         int ret = -ENOMEM;
1074
1075         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1076                     "BS_%d : In beiscsi_open_conn\n");
1077
1078         beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
1079         if (beiscsi_ep->ep_cid == 0xFFFF) {
1080                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1081                             "BS_%d : No free cid available\n");
1082                 return ret;
1083         }
1084
1085         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1086                     "BS_%d : In beiscsi_open_conn, ep_cid=%d\n",
1087                     beiscsi_ep->ep_cid);
1088
1089         phba->ep_array[BE_GET_CRI_FROM_CID
1090                        (beiscsi_ep->ep_cid)] = ep;
1091
1092         beiscsi_ep->cid_vld = 0;
1093
1094         if (is_chip_be2_be3r(phba))
1095                 req_memsize = sizeof(struct tcp_connect_and_offload_in);
1096         else
1097                 req_memsize = sizeof(struct tcp_connect_and_offload_in_v1);
1098
1099         nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
1100                                 req_memsize,
1101                                 &nonemb_cmd.dma);
1102         if (nonemb_cmd.va == NULL) {
1103
1104                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1105                             "BS_%d : Failed to allocate memory for"
1106                             " mgmt_open_connection\n");
1107
1108                 beiscsi_free_ep(beiscsi_ep);
1109                 return -ENOMEM;
1110         }
1111         nonemb_cmd.size = req_memsize;
1112         memset(nonemb_cmd.va, 0, nonemb_cmd.size);
1113         tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
1114         if (tag <= 0) {
1115                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1116                             "BS_%d : mgmt_open_connection Failed for cid=%d\n",
1117                             beiscsi_ep->ep_cid);
1118
1119                 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1120                                     nonemb_cmd.va, nonemb_cmd.dma);
1121                 beiscsi_free_ep(beiscsi_ep);
1122                 return -EAGAIN;
1123         }
1124
1125         ret = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
1126         if (ret) {
1127                 beiscsi_log(phba, KERN_ERR,
1128                             BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
1129                             "BS_%d : mgmt_open_connection Failed");
1130
1131                 if (ret != -EBUSY)
1132                         pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1133                                             nonemb_cmd.va, nonemb_cmd.dma);
1134
1135                 beiscsi_free_ep(beiscsi_ep);
1136                 return ret;
1137         }
1138
1139         ptcpcnct_out = (struct tcp_connect_and_offload_out *)nonemb_cmd.va;
1140         beiscsi_ep = ep->dd_data;
1141         beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
1142         beiscsi_ep->cid_vld = 1;
1143         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1144                     "BS_%d : mgmt_open_connection Success\n");
1145
1146         pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1147                             nonemb_cmd.va, nonemb_cmd.dma);
1148         return 0;
1149 }
1150
1151 /**
1152  * beiscsi_ep_connect - Ask chip to create TCP Conn
1153  * @scsi_host: Pointer to scsi_host structure
1154  * @dst_addr: The IP address of Target
1155  * @non_blocking: blocking or non-blocking call
1156  *
1157  * This routines first asks chip to create a connection and then allocates an EP
1158  */
1159 struct iscsi_endpoint *
1160 beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
1161                    int non_blocking)
1162 {
1163         struct beiscsi_hba *phba;
1164         struct beiscsi_endpoint *beiscsi_ep;
1165         struct iscsi_endpoint *ep;
1166         int ret;
1167
1168         if (shost)
1169                 phba = iscsi_host_priv(shost);
1170         else {
1171                 ret = -ENXIO;
1172                 printk(KERN_ERR
1173                        "beiscsi_ep_connect shost is NULL\n");
1174                 return ERR_PTR(ret);
1175         }
1176
1177         if (beiscsi_error(phba)) {
1178                 ret = -EIO;
1179                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1180                             "BS_%d : The FW state Not Stable!!!\n");
1181                 return ERR_PTR(ret);
1182         }
1183
1184         if (phba->state & BE_ADAPTER_PCI_ERR) {
1185                 ret = -EBUSY;
1186                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1187                             "BS_%d : In PCI_ERROR Recovery\n");
1188                 return ERR_PTR(ret);
1189         } else if (phba->state & BE_ADAPTER_LINK_DOWN) {
1190                 ret = -EBUSY;
1191                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1192                             "BS_%d : The Adapter Port state is Down!!!\n");
1193                 return ERR_PTR(ret);
1194         }
1195
1196         ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
1197         if (!ep) {
1198                 ret = -ENOMEM;
1199                 return ERR_PTR(ret);
1200         }
1201
1202         beiscsi_ep = ep->dd_data;
1203         beiscsi_ep->phba = phba;
1204         beiscsi_ep->openiscsi_ep = ep;
1205         ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
1206         if (ret) {
1207                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1208                             "BS_%d : Failed in beiscsi_open_conn\n");
1209                 goto free_ep;
1210         }
1211
1212         return ep;
1213
1214 free_ep:
1215         iscsi_destroy_endpoint(ep);
1216         return ERR_PTR(ret);
1217 }
1218
1219 /**
1220  * beiscsi_ep_poll - Poll to see if connection is established
1221  * @ep: endpoint to be used
1222  * @timeout_ms: timeout specified in millisecs
1223  *
1224  * Poll to see if TCP connection established
1225  */
1226 int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1227 {
1228         struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
1229
1230         beiscsi_log(beiscsi_ep->phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1231                     "BS_%d : In  beiscsi_ep_poll\n");
1232
1233         if (beiscsi_ep->cid_vld == 1)
1234                 return 1;
1235         else
1236                 return 0;
1237 }
1238
1239 /**
1240  * beiscsi_flush_cq()- Flush the CQ created.
1241  * @phba: ptr device priv structure.
1242  *
1243  * Before the connection resource are freed flush
1244  * all the CQ enteries
1245  **/
1246 static void beiscsi_flush_cq(struct beiscsi_hba *phba)
1247 {
1248         uint16_t i;
1249         struct be_eq_obj *pbe_eq;
1250         struct hwi_controller *phwi_ctrlr;
1251         struct hwi_context_memory *phwi_context;
1252
1253         phwi_ctrlr = phba->phwi_ctrlr;
1254         phwi_context = phwi_ctrlr->phwi_ctxt;
1255
1256         for (i = 0; i < phba->num_cpus; i++) {
1257                 pbe_eq = &phwi_context->be_eq[i];
1258                 irq_poll_disable(&pbe_eq->iopoll);
1259                 beiscsi_process_cq(pbe_eq, BE2_MAX_NUM_CQ_PROC);
1260                 irq_poll_enable(&pbe_eq->iopoll);
1261         }
1262 }
1263
1264 /**
1265  * beiscsi_close_conn - Upload the  connection
1266  * @ep: The iscsi endpoint
1267  * @flag: The type of connection closure
1268  */
1269 static int beiscsi_close_conn(struct  beiscsi_endpoint *beiscsi_ep, int flag)
1270 {
1271         int ret = 0;
1272         unsigned int tag;
1273         struct beiscsi_hba *phba = beiscsi_ep->phba;
1274
1275         tag = mgmt_upload_connection(phba, beiscsi_ep->ep_cid, flag);
1276         if (!tag) {
1277                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1278                             "BS_%d : upload failed for cid 0x%x\n",
1279                             beiscsi_ep->ep_cid);
1280
1281                 ret = -EAGAIN;
1282         }
1283
1284         ret = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
1285
1286         /* Flush the CQ entries */
1287         beiscsi_flush_cq(phba);
1288
1289         return ret;
1290 }
1291
1292 /**
1293  * beiscsi_unbind_conn_to_cid - Unbind the beiscsi_conn from phba conn table
1294  * @phba: The phba instance
1295  * @cid: The cid to free
1296  */
1297 static int beiscsi_unbind_conn_to_cid(struct beiscsi_hba *phba,
1298                                       unsigned int cid)
1299 {
1300         uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
1301
1302         if (phba->conn_table[cri_index])
1303                 phba->conn_table[cri_index] = NULL;
1304         else {
1305                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1306                             "BS_%d : Connection table Not occupied.\n");
1307                 return -EINVAL;
1308         }
1309         return 0;
1310 }
1311
1312 /**
1313  * beiscsi_ep_disconnect - Tears down the TCP connection
1314  * @ep: endpoint to be used
1315  *
1316  * Tears down the TCP connection
1317  */
1318 void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
1319 {
1320         struct beiscsi_conn *beiscsi_conn;
1321         struct beiscsi_endpoint *beiscsi_ep;
1322         struct beiscsi_hba *phba;
1323         unsigned int tag;
1324         uint8_t mgmt_invalidate_flag, tcp_upload_flag;
1325         unsigned short savecfg_flag = CMD_ISCSI_SESSION_SAVE_CFG_ON_FLASH;
1326
1327         beiscsi_ep = ep->dd_data;
1328         phba = beiscsi_ep->phba;
1329         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1330                     "BS_%d : In beiscsi_ep_disconnect for ep_cid = %d\n",
1331                     beiscsi_ep->ep_cid);
1332
1333         if (beiscsi_ep->conn) {
1334                 beiscsi_conn = beiscsi_ep->conn;
1335                 iscsi_suspend_queue(beiscsi_conn->conn);
1336                 mgmt_invalidate_flag = ~BEISCSI_NO_RST_ISSUE;
1337                 tcp_upload_flag = CONNECTION_UPLOAD_GRACEFUL;
1338         } else {
1339                 mgmt_invalidate_flag = BEISCSI_NO_RST_ISSUE;
1340                 tcp_upload_flag = CONNECTION_UPLOAD_ABORT;
1341         }
1342
1343         if (phba->state & BE_ADAPTER_PCI_ERR) {
1344                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1345                             "BS_%d : PCI_ERROR Recovery\n");
1346                 goto free_ep;
1347         }
1348
1349         tag = mgmt_invalidate_connection(phba, beiscsi_ep,
1350                                           beiscsi_ep->ep_cid,
1351                                           mgmt_invalidate_flag,
1352                                           savecfg_flag);
1353         if (!tag) {
1354                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1355                             "BS_%d : mgmt_invalidate_connection Failed for cid=%d\n",
1356                             beiscsi_ep->ep_cid);
1357         }
1358
1359         beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
1360         beiscsi_close_conn(beiscsi_ep, tcp_upload_flag);
1361 free_ep:
1362         msleep(BEISCSI_LOGOUT_SYNC_DELAY);
1363         beiscsi_free_ep(beiscsi_ep);
1364         beiscsi_unbind_conn_to_cid(phba, beiscsi_ep->ep_cid);
1365         iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
1366 }
1367
1368 umode_t beiscsi_attr_is_visible(int param_type, int param)
1369 {
1370         switch (param_type) {
1371         case ISCSI_NET_PARAM:
1372                 switch (param) {
1373                 case ISCSI_NET_PARAM_IFACE_ENABLE:
1374                 case ISCSI_NET_PARAM_IPV4_ADDR:
1375                 case ISCSI_NET_PARAM_IPV4_SUBNET:
1376                 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1377                 case ISCSI_NET_PARAM_IPV4_GW:
1378                 case ISCSI_NET_PARAM_IPV6_ADDR:
1379                 case ISCSI_NET_PARAM_VLAN_ID:
1380                 case ISCSI_NET_PARAM_VLAN_PRIORITY:
1381                 case ISCSI_NET_PARAM_VLAN_ENABLED:
1382                         return S_IRUGO;
1383                 default:
1384                         return 0;
1385                 }
1386         case ISCSI_HOST_PARAM:
1387                 switch (param) {
1388                 case ISCSI_HOST_PARAM_HWADDRESS:
1389                 case ISCSI_HOST_PARAM_INITIATOR_NAME:
1390                 case ISCSI_HOST_PARAM_PORT_STATE:
1391                 case ISCSI_HOST_PARAM_PORT_SPEED:
1392                         return S_IRUGO;
1393                 default:
1394                         return 0;
1395                 }
1396         case ISCSI_PARAM:
1397                 switch (param) {
1398                 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1399                 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1400                 case ISCSI_PARAM_HDRDGST_EN:
1401                 case ISCSI_PARAM_DATADGST_EN:
1402                 case ISCSI_PARAM_CONN_ADDRESS:
1403                 case ISCSI_PARAM_CONN_PORT:
1404                 case ISCSI_PARAM_EXP_STATSN:
1405                 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1406                 case ISCSI_PARAM_PERSISTENT_PORT:
1407                 case ISCSI_PARAM_PING_TMO:
1408                 case ISCSI_PARAM_RECV_TMO:
1409                 case ISCSI_PARAM_INITIAL_R2T_EN:
1410                 case ISCSI_PARAM_MAX_R2T:
1411                 case ISCSI_PARAM_IMM_DATA_EN:
1412                 case ISCSI_PARAM_FIRST_BURST:
1413                 case ISCSI_PARAM_MAX_BURST:
1414                 case ISCSI_PARAM_PDU_INORDER_EN:
1415                 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1416                 case ISCSI_PARAM_ERL:
1417                 case ISCSI_PARAM_TARGET_NAME:
1418                 case ISCSI_PARAM_TPGT:
1419                 case ISCSI_PARAM_USERNAME:
1420                 case ISCSI_PARAM_PASSWORD:
1421                 case ISCSI_PARAM_USERNAME_IN:
1422                 case ISCSI_PARAM_PASSWORD_IN:
1423                 case ISCSI_PARAM_FAST_ABORT:
1424                 case ISCSI_PARAM_ABORT_TMO:
1425                 case ISCSI_PARAM_LU_RESET_TMO:
1426                 case ISCSI_PARAM_IFACE_NAME:
1427                 case ISCSI_PARAM_INITIATOR_NAME:
1428                         return S_IRUGO;
1429                 default:
1430                         return 0;
1431                 }
1432         }
1433
1434         return 0;
1435 }