]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/be2iscsi/be_iscsi.c
scsi: be2iscsi: Update iface handle before any set param
[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_create_ipv4_iface(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_create_ipv6_iface(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_create_def_ifaces(struct beiscsi_hba *phba)
279 {
280         struct be_cmd_get_if_info_resp *if_info;
281
282         if (!mgmt_get_if_info(phba, BE2_IPV4, &if_info)) {
283                 beiscsi_create_ipv4_iface(phba);
284                 kfree(if_info);
285         }
286
287         if (!mgmt_get_if_info(phba, BE2_IPV6, &if_info)) {
288                 beiscsi_create_ipv6_iface(phba);
289                 kfree(if_info);
290         }
291 }
292
293 void beiscsi_destroy_def_ifaces(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_create_ipv4_iface(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, BE2_IPV4, 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, BE2_IPV4);
366                 else if (info->value[0] == ISCSI_BOOTPROTO_STATIC)
367                         /* release DHCP IP address */
368                         ret = beiscsi_if_en_static(phba, BE2_IPV4, NULL, NULL);
369                 else
370                         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
371                                     "BS_%d : Invalid BOOTPROTO: %d\n",
372                                     info->value[0]);
373                 break;
374         case ISCSI_NET_PARAM_IPV4_ADDR:
375                 ip = info->value;
376                 nla = nla_find(data, dt_len, ISCSI_NET_PARAM_IPV4_SUBNET);
377                 if (nla) {
378                         info = nla_data(nla);
379                         subnet = info->value;
380                 }
381                 ret = beiscsi_if_en_static(phba, BE2_IPV4, ip, subnet);
382                 break;
383         case ISCSI_NET_PARAM_IPV4_SUBNET:
384                 /*
385                  * OPCODE_COMMON_ISCSI_NTWK_MODIFY_IP_ADDR ioctl needs IP
386                  * and subnet both. Find IP to be applied for this subnet.
387                  */
388                 subnet = info->value;
389                 nla = nla_find(data, dt_len, ISCSI_NET_PARAM_IPV4_ADDR);
390                 if (nla) {
391                         info = nla_data(nla);
392                         ip = info->value;
393                 }
394                 ret = beiscsi_if_en_static(phba, BE2_IPV4, ip, subnet);
395                 break;
396         }
397
398         return ret;
399 }
400
401 static int
402 beiscsi_iface_config_ipv6(struct Scsi_Host *shost,
403                           struct iscsi_iface_param_info *iface_param,
404                           void *data, uint32_t dt_len)
405 {
406         struct beiscsi_hba *phba = iscsi_host_priv(shost);
407         int ret = -EPERM;
408
409         switch (iface_param->param) {
410         case ISCSI_NET_PARAM_IFACE_ENABLE:
411                 if (iface_param->value[0] == ISCSI_IFACE_ENABLE)
412                         ret = beiscsi_create_ipv6_iface(phba);
413                 else {
414                         iscsi_destroy_iface(phba->ipv6_iface);
415                         phba->ipv6_iface = NULL;
416                 }
417                 break;
418         case ISCSI_NET_PARAM_IPV6_ADDR:
419                 ret = beiscsi_if_en_static(phba, BE2_IPV6,
420                                            iface_param->value, NULL);
421                 break;
422         }
423
424         return ret;
425 }
426
427 int be2iscsi_iface_set_param(struct Scsi_Host *shost,
428                 void *data, uint32_t dt_len)
429 {
430         struct iscsi_iface_param_info *iface_param = NULL;
431         struct beiscsi_hba *phba = iscsi_host_priv(shost);
432         struct nlattr *attrib;
433         uint32_t rm_len = dt_len;
434         int ret;
435
436         if (phba->state & BE_ADAPTER_PCI_ERR) {
437                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
438                             "BS_%d : In PCI_ERROR Recovery\n");
439                 return -EBUSY;
440         }
441
442         /* update interface_handle */
443         ret = beiscsi_if_get_handle(phba);
444         if (ret) {
445                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
446                             "BS_%d : Getting Interface Handle Failed\n");
447                 return ret;
448         }
449
450         nla_for_each_attr(attrib, data, dt_len, rm_len) {
451                 iface_param = nla_data(attrib);
452
453                 if (iface_param->param_type != ISCSI_NET_PARAM)
454                         continue;
455
456                 /*
457                  * BE2ISCSI only supports 1 interface
458                  */
459                 if (iface_param->iface_num) {
460                         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
461                                     "BS_%d : Invalid iface_num %d."
462                                     "Only iface_num 0 is supported.\n",
463                                     iface_param->iface_num);
464
465                         return -EINVAL;
466                 }
467
468                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
469                             "BS_%d : %s.0 set param %d",
470                             (iface_param->iface_type == ISCSI_IFACE_TYPE_IPV4) ?
471                             "ipv4" : "ipv6", iface_param->param);
472
473                 ret = -EPERM;
474                 switch (iface_param->param) {
475                 case ISCSI_NET_PARAM_VLAN_ENABLED:
476                 case ISCSI_NET_PARAM_VLAN_TAG:
477                         ret = beiscsi_iface_config_vlan(shost, iface_param);
478                         break;
479                 default:
480                         switch (iface_param->iface_type) {
481                         case ISCSI_IFACE_TYPE_IPV4:
482                                 ret = beiscsi_iface_config_ipv4(shost,
483                                                                 iface_param,
484                                                                 data, dt_len);
485                                 break;
486                         case ISCSI_IFACE_TYPE_IPV6:
487                                 ret = beiscsi_iface_config_ipv6(shost,
488                                                                 iface_param,
489                                                                 data, dt_len);
490                                 break;
491                         }
492                 }
493
494                 if (ret == -EPERM) {
495                         __beiscsi_log(phba, KERN_ERR,
496                                       "BS_%d : %s.0 set param %d not permitted",
497                                       (iface_param->iface_type ==
498                                        ISCSI_IFACE_TYPE_IPV4) ? "ipv4" : "ipv6",
499                                       iface_param->param);
500                         ret = 0;
501                 }
502                 if (ret)
503                         break;
504         }
505
506         return ret;
507 }
508
509 static int be2iscsi_get_if_param(struct beiscsi_hba *phba,
510                 struct iscsi_iface *iface, int param,
511                 char *buf)
512 {
513         struct be_cmd_get_if_info_resp *if_info;
514         int len, ip_type = BE2_IPV4;
515
516         if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
517                 ip_type = BE2_IPV6;
518
519         len = mgmt_get_if_info(phba, ip_type, &if_info);
520         if (len)
521                 return len;
522
523         switch (param) {
524         case ISCSI_NET_PARAM_IPV4_ADDR:
525                 len = sprintf(buf, "%pI4\n", if_info->ip_addr.addr);
526                 break;
527         case ISCSI_NET_PARAM_IPV6_ADDR:
528                 len = sprintf(buf, "%pI6\n", if_info->ip_addr.addr);
529                 break;
530         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
531                 if (!if_info->dhcp_state)
532                         len = sprintf(buf, "static\n");
533                 else
534                         len = sprintf(buf, "dhcp\n");
535                 break;
536         case ISCSI_NET_PARAM_IPV4_SUBNET:
537                 len = sprintf(buf, "%pI4\n", if_info->ip_addr.subnet_mask);
538                 break;
539         case ISCSI_NET_PARAM_VLAN_ENABLED:
540                 len = sprintf(buf, "%s\n",
541                               (if_info->vlan_priority == BEISCSI_VLAN_DISABLE) ?
542                               "disable" : "enable");
543                 break;
544         case ISCSI_NET_PARAM_VLAN_ID:
545                 if (if_info->vlan_priority == BEISCSI_VLAN_DISABLE)
546                         len = -EINVAL;
547                 else
548                         len = sprintf(buf, "%d\n",
549                                       (if_info->vlan_priority &
550                                        ISCSI_MAX_VLAN_ID));
551                 break;
552         case ISCSI_NET_PARAM_VLAN_PRIORITY:
553                 if (if_info->vlan_priority == BEISCSI_VLAN_DISABLE)
554                         len = -EINVAL;
555                 else
556                         len = sprintf(buf, "%d\n",
557                                       ((if_info->vlan_priority >> 13) &
558                                        ISCSI_MAX_VLAN_PRIORITY));
559                 break;
560         default:
561                 WARN_ON(1);
562         }
563
564         kfree(if_info);
565         return len;
566 }
567
568 int be2iscsi_iface_get_param(struct iscsi_iface *iface,
569                 enum iscsi_param_type param_type,
570                 int param, char *buf)
571 {
572         struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
573         struct beiscsi_hba *phba = iscsi_host_priv(shost);
574         struct be_cmd_get_def_gateway_resp gateway;
575         int len = -EPERM;
576
577         if (phba->state & BE_ADAPTER_PCI_ERR) {
578                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
579                             "BS_%d : In PCI_ERROR Recovery\n");
580                 return -EBUSY;
581         }
582
583         switch (param) {
584         case ISCSI_NET_PARAM_IPV4_ADDR:
585         case ISCSI_NET_PARAM_IPV4_SUBNET:
586         case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
587         case ISCSI_NET_PARAM_IPV6_ADDR:
588         case ISCSI_NET_PARAM_VLAN_ENABLED:
589         case ISCSI_NET_PARAM_VLAN_ID:
590         case ISCSI_NET_PARAM_VLAN_PRIORITY:
591                 len = be2iscsi_get_if_param(phba, iface, param, buf);
592                 break;
593         case ISCSI_NET_PARAM_IFACE_ENABLE:
594                 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
595                         len = sprintf(buf, "%s\n",
596                                       phba->ipv4_iface ? "enable" : "disable");
597                 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
598                         len = sprintf(buf, "%s\n",
599                                       phba->ipv6_iface ? "enable" : "disable");
600                 break;
601         case ISCSI_NET_PARAM_IPV4_GW:
602                 memset(&gateway, 0, sizeof(gateway));
603                 len = beiscsi_if_get_gw(phba, BE2_IPV4, &gateway);
604                 if (!len)
605                         len = sprintf(buf, "%pI4\n", &gateway.ip_addr.addr);
606                 break;
607         }
608
609         return len;
610 }
611
612 /**
613  * beiscsi_ep_get_param - get the iscsi parameter
614  * @ep: pointer to iscsi ep
615  * @param: parameter type identifier
616  * @buf: buffer pointer
617  *
618  * returns iscsi parameter
619  */
620 int beiscsi_ep_get_param(struct iscsi_endpoint *ep,
621                            enum iscsi_param param, char *buf)
622 {
623         struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
624         int len;
625
626         beiscsi_log(beiscsi_ep->phba, KERN_INFO,
627                     BEISCSI_LOG_CONFIG,
628                     "BS_%d : In beiscsi_ep_get_param,"
629                     " param= %d\n", param);
630
631         switch (param) {
632         case ISCSI_PARAM_CONN_PORT:
633                 len = sprintf(buf, "%hu\n", beiscsi_ep->dst_tcpport);
634                 break;
635         case ISCSI_PARAM_CONN_ADDRESS:
636                 if (beiscsi_ep->ip_type == BE2_IPV4)
637                         len = sprintf(buf, "%pI4\n", &beiscsi_ep->dst_addr);
638                 else
639                         len = sprintf(buf, "%pI6\n", &beiscsi_ep->dst6_addr);
640                 break;
641         default:
642                 len = -EPERM;
643         }
644         return len;
645 }
646
647 int beiscsi_set_param(struct iscsi_cls_conn *cls_conn,
648                       enum iscsi_param param, char *buf, int buflen)
649 {
650         struct iscsi_conn *conn = cls_conn->dd_data;
651         struct iscsi_session *session = conn->session;
652         struct beiscsi_hba *phba = NULL;
653         int ret;
654
655         phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
656         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
657                     "BS_%d : In beiscsi_conn_set_param,"
658                     " param= %d\n", param);
659
660         ret = iscsi_set_param(cls_conn, param, buf, buflen);
661         if (ret)
662                 return ret;
663         /*
664          * If userspace tried to set the value to higher than we can
665          * support override here.
666          */
667         switch (param) {
668         case ISCSI_PARAM_FIRST_BURST:
669                 if (session->first_burst > 8192)
670                         session->first_burst = 8192;
671                 break;
672         case ISCSI_PARAM_MAX_RECV_DLENGTH:
673                 if (conn->max_recv_dlength > 65536)
674                         conn->max_recv_dlength = 65536;
675                 break;
676         case ISCSI_PARAM_MAX_BURST:
677                 if (session->max_burst > 262144)
678                         session->max_burst = 262144;
679                 break;
680         case ISCSI_PARAM_MAX_XMIT_DLENGTH:
681                 if (conn->max_xmit_dlength > 65536)
682                         conn->max_xmit_dlength = 65536;
683         default:
684                 return 0;
685         }
686
687         return 0;
688 }
689
690 /**
691  * beiscsi_get_initname - Read Initiator Name from flash
692  * @buf: buffer bointer
693  * @phba: The device priv structure instance
694  *
695  * returns number of bytes
696  */
697 static int beiscsi_get_initname(char *buf, struct beiscsi_hba *phba)
698 {
699         int rc;
700         unsigned int tag;
701         struct be_mcc_wrb *wrb;
702         struct be_cmd_hba_name *resp;
703
704         tag = be_cmd_get_initname(phba);
705         if (!tag) {
706                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
707                             "BS_%d : Getting Initiator Name Failed\n");
708
709                 return -EBUSY;
710         }
711
712         rc = beiscsi_mccq_compl_wait(phba, tag, &wrb, NULL);
713         if (rc) {
714                 beiscsi_log(phba, KERN_ERR,
715                             BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
716                             "BS_%d : Initiator Name MBX Failed\n");
717                 return rc;
718         }
719
720         resp = embedded_payload(wrb);
721         rc = sprintf(buf, "%s\n", resp->initiator_name);
722         return rc;
723 }
724
725 /**
726  * beiscsi_get_port_state - Get the Port State
727  * @shost : pointer to scsi_host structure
728  *
729  */
730 static void beiscsi_get_port_state(struct Scsi_Host *shost)
731 {
732         struct beiscsi_hba *phba = iscsi_host_priv(shost);
733         struct iscsi_cls_host *ihost = shost->shost_data;
734
735         ihost->port_state = (phba->state & BE_ADAPTER_LINK_UP) ?
736                 ISCSI_PORT_STATE_UP : ISCSI_PORT_STATE_DOWN;
737 }
738
739 /**
740  * beiscsi_get_port_speed  - Get the Port Speed from Adapter
741  * @shost : pointer to scsi_host structure
742  *
743  */
744 static void beiscsi_get_port_speed(struct Scsi_Host *shost)
745 {
746         struct beiscsi_hba *phba = iscsi_host_priv(shost);
747         struct iscsi_cls_host *ihost = shost->shost_data;
748
749         switch (phba->port_speed) {
750         case BE2ISCSI_LINK_SPEED_10MBPS:
751                 ihost->port_speed = ISCSI_PORT_SPEED_10MBPS;
752                 break;
753         case BE2ISCSI_LINK_SPEED_100MBPS:
754                 ihost->port_speed = ISCSI_PORT_SPEED_100MBPS;
755                 break;
756         case BE2ISCSI_LINK_SPEED_1GBPS:
757                 ihost->port_speed = ISCSI_PORT_SPEED_1GBPS;
758                 break;
759         case BE2ISCSI_LINK_SPEED_10GBPS:
760                 ihost->port_speed = ISCSI_PORT_SPEED_10GBPS;
761                 break;
762         case BE2ISCSI_LINK_SPEED_25GBPS:
763                 ihost->port_speed = ISCSI_PORT_SPEED_25GBPS;
764                 break;
765         case BE2ISCSI_LINK_SPEED_40GBPS:
766                 ihost->port_speed = ISCSI_PORT_SPEED_40GBPS;
767                 break;
768         default:
769                 ihost->port_speed = ISCSI_PORT_SPEED_UNKNOWN;
770         }
771 }
772
773 /**
774  * beiscsi_get_host_param - get the iscsi parameter
775  * @shost: pointer to scsi_host structure
776  * @param: parameter type identifier
777  * @buf: buffer pointer
778  *
779  * returns host parameter
780  */
781 int beiscsi_get_host_param(struct Scsi_Host *shost,
782                            enum iscsi_host_param param, char *buf)
783 {
784         struct beiscsi_hba *phba = iscsi_host_priv(shost);
785         int status = 0;
786
787
788         if (phba->state & BE_ADAPTER_PCI_ERR) {
789                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
790                             "BS_%d : In PCI_ERROR Recovery\n");
791                 return -EBUSY;
792         } else {
793                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
794                             "BS_%d : In beiscsi_get_host_param,"
795                             " param = %d\n", param);
796         }
797
798         switch (param) {
799         case ISCSI_HOST_PARAM_HWADDRESS:
800                 status = beiscsi_get_macaddr(buf, phba);
801                 if (status < 0) {
802                         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
803                                     "BS_%d : beiscsi_get_macaddr Failed\n");
804                         return status;
805                 }
806                 break;
807         case ISCSI_HOST_PARAM_INITIATOR_NAME:
808                 status = beiscsi_get_initname(buf, phba);
809                 if (status < 0) {
810                         beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
811                                     "BS_%d : Retreiving Initiator Name Failed\n");
812                         return status;
813                 }
814                 break;
815         case ISCSI_HOST_PARAM_PORT_STATE:
816                 beiscsi_get_port_state(shost);
817                 status = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
818                 break;
819         case ISCSI_HOST_PARAM_PORT_SPEED:
820                 beiscsi_get_port_speed(shost);
821                 status = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
822                 break;
823         default:
824                 return iscsi_host_get_param(shost, param, buf);
825         }
826         return status;
827 }
828
829 int beiscsi_get_macaddr(char *buf, struct beiscsi_hba *phba)
830 {
831         struct be_cmd_get_nic_conf_resp resp;
832         int rc;
833
834         if (phba->mac_addr_set)
835                 return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
836
837         memset(&resp, 0, sizeof(resp));
838         rc = mgmt_get_nic_conf(phba, &resp);
839         if (rc)
840                 return rc;
841
842         phba->mac_addr_set = true;
843         memcpy(phba->mac_address, resp.mac_address, ETH_ALEN);
844         return sysfs_format_mac(buf, phba->mac_address, ETH_ALEN);
845 }
846
847 /**
848  * beiscsi_conn_get_stats - get the iscsi stats
849  * @cls_conn: pointer to iscsi cls conn
850  * @stats: pointer to iscsi_stats structure
851  *
852  * returns iscsi stats
853  */
854 void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
855                             struct iscsi_stats *stats)
856 {
857         struct iscsi_conn *conn = cls_conn->dd_data;
858         struct beiscsi_hba *phba = NULL;
859
860         phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
861         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
862                     "BS_%d : In beiscsi_conn_get_stats\n");
863
864         stats->txdata_octets = conn->txdata_octets;
865         stats->rxdata_octets = conn->rxdata_octets;
866         stats->dataout_pdus = conn->dataout_pdus_cnt;
867         stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
868         stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
869         stats->datain_pdus = conn->datain_pdus_cnt;
870         stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
871         stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
872         stats->r2t_pdus = conn->r2t_pdus_cnt;
873         stats->digest_err = 0;
874         stats->timeout_err = 0;
875         stats->custom_length = 1;
876         strcpy(stats->custom[0].desc, "eh_abort_cnt");
877         stats->custom[0].value = conn->eh_abort_cnt;
878 }
879
880 /**
881  * beiscsi_set_params_for_offld - get the parameters for offload
882  * @beiscsi_conn: pointer to beiscsi_conn
883  * @params: pointer to offload_params structure
884  */
885 static void  beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
886                                           struct beiscsi_offload_params *params)
887 {
888         struct iscsi_conn *conn = beiscsi_conn->conn;
889         struct iscsi_session *session = conn->session;
890
891         AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
892                       params, session->max_burst);
893         AMAP_SET_BITS(struct amap_beiscsi_offload_params,
894                       max_send_data_segment_length, params,
895                       conn->max_xmit_dlength);
896         AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
897                       params, session->first_burst);
898         AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
899                       session->erl);
900         AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
901                       conn->datadgst_en);
902         AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
903                       conn->hdrdgst_en);
904         AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
905                       session->initial_r2t_en);
906         AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
907                       session->imm_data_en);
908         AMAP_SET_BITS(struct amap_beiscsi_offload_params,
909                       data_seq_inorder, params,
910                       session->dataseq_inorder_en);
911         AMAP_SET_BITS(struct amap_beiscsi_offload_params,
912                       pdu_seq_inorder, params,
913                       session->pdu_inorder_en);
914         AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_r2t, params,
915                       session->max_r2t);
916         AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
917                       (conn->exp_statsn - 1));
918         AMAP_SET_BITS(struct amap_beiscsi_offload_params,
919                       max_recv_data_segment_length, params,
920                       conn->max_recv_dlength);
921
922 }
923
924 /**
925  * beiscsi_conn_start - offload of session to chip
926  * @cls_conn: pointer to beiscsi_conn
927  */
928 int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
929 {
930         struct iscsi_conn *conn = cls_conn->dd_data;
931         struct beiscsi_conn *beiscsi_conn = conn->dd_data;
932         struct beiscsi_endpoint *beiscsi_ep;
933         struct beiscsi_offload_params params;
934         struct beiscsi_hba *phba;
935
936         phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
937
938         if (phba->state & BE_ADAPTER_PCI_ERR) {
939                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
940                             "BS_%d : In PCI_ERROR Recovery\n");
941                 return -EBUSY;
942         } else {
943                 beiscsi_log(beiscsi_conn->phba, KERN_INFO,
944                             BEISCSI_LOG_CONFIG,
945                             "BS_%d : In beiscsi_conn_start\n");
946         }
947
948         memset(&params, 0, sizeof(struct beiscsi_offload_params));
949         beiscsi_ep = beiscsi_conn->ep;
950         if (!beiscsi_ep)
951                 beiscsi_log(beiscsi_conn->phba, KERN_ERR,
952                             BEISCSI_LOG_CONFIG,
953                             "BS_%d : In beiscsi_conn_start , no beiscsi_ep\n");
954
955         beiscsi_conn->login_in_progress = 0;
956         beiscsi_set_params_for_offld(beiscsi_conn, &params);
957         beiscsi_offload_connection(beiscsi_conn, &params);
958         iscsi_conn_start(cls_conn);
959         return 0;
960 }
961
962 /**
963  * beiscsi_get_cid - Allocate a cid
964  * @phba: The phba instance
965  */
966 static int beiscsi_get_cid(struct beiscsi_hba *phba)
967 {
968         unsigned short cid = 0xFFFF, cid_from_ulp;
969         struct ulp_cid_info *cid_info = NULL;
970         uint16_t cid_avlbl_ulp0, cid_avlbl_ulp1;
971
972         /* Find the ULP which has more CID available */
973         cid_avlbl_ulp0 = (phba->cid_array_info[BEISCSI_ULP0]) ?
974                           BEISCSI_ULP0_AVLBL_CID(phba) : 0;
975         cid_avlbl_ulp1 = (phba->cid_array_info[BEISCSI_ULP1]) ?
976                           BEISCSI_ULP1_AVLBL_CID(phba) : 0;
977         cid_from_ulp = (cid_avlbl_ulp0 > cid_avlbl_ulp1) ?
978                         BEISCSI_ULP0 : BEISCSI_ULP1;
979
980         if (test_bit(cid_from_ulp, (void *)&phba->fw_config.ulp_supported)) {
981                 cid_info = phba->cid_array_info[cid_from_ulp];
982                 if (!cid_info->avlbl_cids)
983                         return cid;
984
985                 cid = cid_info->cid_array[cid_info->cid_alloc++];
986
987                 if (cid_info->cid_alloc == BEISCSI_GET_CID_COUNT(
988                                            phba, cid_from_ulp))
989                         cid_info->cid_alloc = 0;
990
991                 cid_info->avlbl_cids--;
992         }
993         return cid;
994 }
995
996 /**
997  * beiscsi_put_cid - Free the cid
998  * @phba: The phba for which the cid is being freed
999  * @cid: The cid to free
1000  */
1001 static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
1002 {
1003         uint16_t cid_post_ulp;
1004         struct hwi_controller *phwi_ctrlr;
1005         struct hwi_wrb_context *pwrb_context;
1006         struct ulp_cid_info *cid_info = NULL;
1007         uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
1008
1009         phwi_ctrlr = phba->phwi_ctrlr;
1010         pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
1011         cid_post_ulp = pwrb_context->ulp_num;
1012
1013         cid_info = phba->cid_array_info[cid_post_ulp];
1014         cid_info->avlbl_cids++;
1015
1016         cid_info->cid_array[cid_info->cid_free++] = cid;
1017         if (cid_info->cid_free == BEISCSI_GET_CID_COUNT(phba, cid_post_ulp))
1018                 cid_info->cid_free = 0;
1019 }
1020
1021 /**
1022  * beiscsi_free_ep - free endpoint
1023  * @ep: pointer to iscsi endpoint structure
1024  */
1025 static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
1026 {
1027         struct beiscsi_hba *phba = beiscsi_ep->phba;
1028         struct beiscsi_conn *beiscsi_conn;
1029
1030         beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
1031         beiscsi_ep->phba = NULL;
1032         phba->ep_array[BE_GET_CRI_FROM_CID
1033                        (beiscsi_ep->ep_cid)] = NULL;
1034
1035         /**
1036          * Check if any connection resource allocated by driver
1037          * is to be freed.This case occurs when target redirection
1038          * or connection retry is done.
1039          **/
1040         if (!beiscsi_ep->conn)
1041                 return;
1042
1043         beiscsi_conn = beiscsi_ep->conn;
1044         if (beiscsi_conn->login_in_progress) {
1045                 beiscsi_free_mgmt_task_handles(beiscsi_conn,
1046                                                beiscsi_conn->task);
1047                 beiscsi_conn->login_in_progress = 0;
1048         }
1049 }
1050
1051 /**
1052  * beiscsi_open_conn - Ask FW to open a TCP connection
1053  * @ep: endpoint to be used
1054  * @src_addr: The source IP address
1055  * @dst_addr: The Destination  IP address
1056  *
1057  * Asks the FW to open a TCP connection
1058  */
1059 static int beiscsi_open_conn(struct iscsi_endpoint *ep,
1060                              struct sockaddr *src_addr,
1061                              struct sockaddr *dst_addr, int non_blocking)
1062 {
1063         struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
1064         struct beiscsi_hba *phba = beiscsi_ep->phba;
1065         struct tcp_connect_and_offload_out *ptcpcnct_out;
1066         struct be_dma_mem nonemb_cmd;
1067         unsigned int tag, req_memsize;
1068         int ret = -ENOMEM;
1069
1070         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1071                     "BS_%d : In beiscsi_open_conn\n");
1072
1073         beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
1074         if (beiscsi_ep->ep_cid == 0xFFFF) {
1075                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1076                             "BS_%d : No free cid available\n");
1077                 return ret;
1078         }
1079
1080         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1081                     "BS_%d : In beiscsi_open_conn, ep_cid=%d\n",
1082                     beiscsi_ep->ep_cid);
1083
1084         phba->ep_array[BE_GET_CRI_FROM_CID
1085                        (beiscsi_ep->ep_cid)] = ep;
1086
1087         beiscsi_ep->cid_vld = 0;
1088
1089         if (is_chip_be2_be3r(phba))
1090                 req_memsize = sizeof(struct tcp_connect_and_offload_in);
1091         else
1092                 req_memsize = sizeof(struct tcp_connect_and_offload_in_v1);
1093
1094         nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
1095                                 req_memsize,
1096                                 &nonemb_cmd.dma);
1097         if (nonemb_cmd.va == NULL) {
1098
1099                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1100                             "BS_%d : Failed to allocate memory for"
1101                             " mgmt_open_connection\n");
1102
1103                 beiscsi_free_ep(beiscsi_ep);
1104                 return -ENOMEM;
1105         }
1106         nonemb_cmd.size = req_memsize;
1107         memset(nonemb_cmd.va, 0, nonemb_cmd.size);
1108         tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
1109         if (tag <= 0) {
1110                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1111                             "BS_%d : mgmt_open_connection Failed for cid=%d\n",
1112                             beiscsi_ep->ep_cid);
1113
1114                 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1115                                     nonemb_cmd.va, nonemb_cmd.dma);
1116                 beiscsi_free_ep(beiscsi_ep);
1117                 return -EAGAIN;
1118         }
1119
1120         ret = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
1121         if (ret) {
1122                 beiscsi_log(phba, KERN_ERR,
1123                             BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
1124                             "BS_%d : mgmt_open_connection Failed");
1125
1126                 if (ret != -EBUSY)
1127                         pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1128                                             nonemb_cmd.va, nonemb_cmd.dma);
1129
1130                 beiscsi_free_ep(beiscsi_ep);
1131                 return ret;
1132         }
1133
1134         ptcpcnct_out = (struct tcp_connect_and_offload_out *)nonemb_cmd.va;
1135         beiscsi_ep = ep->dd_data;
1136         beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
1137         beiscsi_ep->cid_vld = 1;
1138         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1139                     "BS_%d : mgmt_open_connection Success\n");
1140
1141         pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
1142                             nonemb_cmd.va, nonemb_cmd.dma);
1143         return 0;
1144 }
1145
1146 /**
1147  * beiscsi_ep_connect - Ask chip to create TCP Conn
1148  * @scsi_host: Pointer to scsi_host structure
1149  * @dst_addr: The IP address of Target
1150  * @non_blocking: blocking or non-blocking call
1151  *
1152  * This routines first asks chip to create a connection and then allocates an EP
1153  */
1154 struct iscsi_endpoint *
1155 beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
1156                    int non_blocking)
1157 {
1158         struct beiscsi_hba *phba;
1159         struct beiscsi_endpoint *beiscsi_ep;
1160         struct iscsi_endpoint *ep;
1161         int ret;
1162
1163         if (shost)
1164                 phba = iscsi_host_priv(shost);
1165         else {
1166                 ret = -ENXIO;
1167                 printk(KERN_ERR
1168                        "beiscsi_ep_connect shost is NULL\n");
1169                 return ERR_PTR(ret);
1170         }
1171
1172         if (beiscsi_error(phba)) {
1173                 ret = -EIO;
1174                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1175                             "BS_%d : The FW state Not Stable!!!\n");
1176                 return ERR_PTR(ret);
1177         }
1178
1179         if (phba->state & BE_ADAPTER_PCI_ERR) {
1180                 ret = -EBUSY;
1181                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1182                             "BS_%d : In PCI_ERROR Recovery\n");
1183                 return ERR_PTR(ret);
1184         } else if (phba->state & BE_ADAPTER_LINK_DOWN) {
1185                 ret = -EBUSY;
1186                 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1187                             "BS_%d : The Adapter Port state is Down!!!\n");
1188                 return ERR_PTR(ret);
1189         }
1190
1191         ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
1192         if (!ep) {
1193                 ret = -ENOMEM;
1194                 return ERR_PTR(ret);
1195         }
1196
1197         beiscsi_ep = ep->dd_data;
1198         beiscsi_ep->phba = phba;
1199         beiscsi_ep->openiscsi_ep = ep;
1200         ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
1201         if (ret) {
1202                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1203                             "BS_%d : Failed in beiscsi_open_conn\n");
1204                 goto free_ep;
1205         }
1206
1207         return ep;
1208
1209 free_ep:
1210         iscsi_destroy_endpoint(ep);
1211         return ERR_PTR(ret);
1212 }
1213
1214 /**
1215  * beiscsi_ep_poll - Poll to see if connection is established
1216  * @ep: endpoint to be used
1217  * @timeout_ms: timeout specified in millisecs
1218  *
1219  * Poll to see if TCP connection established
1220  */
1221 int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1222 {
1223         struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
1224
1225         beiscsi_log(beiscsi_ep->phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1226                     "BS_%d : In  beiscsi_ep_poll\n");
1227
1228         if (beiscsi_ep->cid_vld == 1)
1229                 return 1;
1230         else
1231                 return 0;
1232 }
1233
1234 /**
1235  * beiscsi_flush_cq()- Flush the CQ created.
1236  * @phba: ptr device priv structure.
1237  *
1238  * Before the connection resource are freed flush
1239  * all the CQ enteries
1240  **/
1241 static void beiscsi_flush_cq(struct beiscsi_hba *phba)
1242 {
1243         uint16_t i;
1244         struct be_eq_obj *pbe_eq;
1245         struct hwi_controller *phwi_ctrlr;
1246         struct hwi_context_memory *phwi_context;
1247
1248         phwi_ctrlr = phba->phwi_ctrlr;
1249         phwi_context = phwi_ctrlr->phwi_ctxt;
1250
1251         for (i = 0; i < phba->num_cpus; i++) {
1252                 pbe_eq = &phwi_context->be_eq[i];
1253                 irq_poll_disable(&pbe_eq->iopoll);
1254                 beiscsi_process_cq(pbe_eq, BE2_MAX_NUM_CQ_PROC);
1255                 irq_poll_enable(&pbe_eq->iopoll);
1256         }
1257 }
1258
1259 /**
1260  * beiscsi_close_conn - Upload the  connection
1261  * @ep: The iscsi endpoint
1262  * @flag: The type of connection closure
1263  */
1264 static int beiscsi_close_conn(struct  beiscsi_endpoint *beiscsi_ep, int flag)
1265 {
1266         int ret = 0;
1267         unsigned int tag;
1268         struct beiscsi_hba *phba = beiscsi_ep->phba;
1269
1270         tag = mgmt_upload_connection(phba, beiscsi_ep->ep_cid, flag);
1271         if (!tag) {
1272                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1273                             "BS_%d : upload failed for cid 0x%x\n",
1274                             beiscsi_ep->ep_cid);
1275
1276                 ret = -EAGAIN;
1277         }
1278
1279         ret = beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
1280
1281         /* Flush the CQ entries */
1282         beiscsi_flush_cq(phba);
1283
1284         return ret;
1285 }
1286
1287 /**
1288  * beiscsi_unbind_conn_to_cid - Unbind the beiscsi_conn from phba conn table
1289  * @phba: The phba instance
1290  * @cid: The cid to free
1291  */
1292 static int beiscsi_unbind_conn_to_cid(struct beiscsi_hba *phba,
1293                                       unsigned int cid)
1294 {
1295         uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
1296
1297         if (phba->conn_table[cri_index])
1298                 phba->conn_table[cri_index] = NULL;
1299         else {
1300                 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1301                             "BS_%d : Connection table Not occupied.\n");
1302                 return -EINVAL;
1303         }
1304         return 0;
1305 }
1306
1307 /**
1308  * beiscsi_ep_disconnect - Tears down the TCP connection
1309  * @ep: endpoint to be used
1310  *
1311  * Tears down the TCP connection
1312  */
1313 void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
1314 {
1315         struct beiscsi_conn *beiscsi_conn;
1316         struct beiscsi_endpoint *beiscsi_ep;
1317         struct beiscsi_hba *phba;
1318         unsigned int tag;
1319         uint8_t mgmt_invalidate_flag, tcp_upload_flag;
1320         unsigned short savecfg_flag = CMD_ISCSI_SESSION_SAVE_CFG_ON_FLASH;
1321
1322         beiscsi_ep = ep->dd_data;
1323         phba = beiscsi_ep->phba;
1324         beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1325                     "BS_%d : In beiscsi_ep_disconnect for ep_cid = %d\n",
1326                     beiscsi_ep->ep_cid);
1327
1328         if (beiscsi_ep->conn) {
1329                 beiscsi_conn = beiscsi_ep->conn;
1330                 iscsi_suspend_queue(beiscsi_conn->conn);
1331                 mgmt_invalidate_flag = ~BEISCSI_NO_RST_ISSUE;
1332                 tcp_upload_flag = CONNECTION_UPLOAD_GRACEFUL;
1333         } else {
1334                 mgmt_invalidate_flag = BEISCSI_NO_RST_ISSUE;
1335                 tcp_upload_flag = CONNECTION_UPLOAD_ABORT;
1336         }
1337
1338         if (phba->state & BE_ADAPTER_PCI_ERR) {
1339                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1340                             "BS_%d : PCI_ERROR Recovery\n");
1341                 goto free_ep;
1342         }
1343
1344         tag = mgmt_invalidate_connection(phba, beiscsi_ep,
1345                                           beiscsi_ep->ep_cid,
1346                                           mgmt_invalidate_flag,
1347                                           savecfg_flag);
1348         if (!tag) {
1349                 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
1350                             "BS_%d : mgmt_invalidate_connection Failed for cid=%d\n",
1351                             beiscsi_ep->ep_cid);
1352         }
1353
1354         beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
1355         beiscsi_close_conn(beiscsi_ep, tcp_upload_flag);
1356 free_ep:
1357         msleep(BEISCSI_LOGOUT_SYNC_DELAY);
1358         beiscsi_free_ep(beiscsi_ep);
1359         beiscsi_unbind_conn_to_cid(phba, beiscsi_ep->ep_cid);
1360         iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
1361 }
1362
1363 umode_t be2iscsi_attr_is_visible(int param_type, int param)
1364 {
1365         switch (param_type) {
1366         case ISCSI_NET_PARAM:
1367                 switch (param) {
1368                 case ISCSI_NET_PARAM_IFACE_ENABLE:
1369                 case ISCSI_NET_PARAM_IPV4_ADDR:
1370                 case ISCSI_NET_PARAM_IPV4_SUBNET:
1371                 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1372                 case ISCSI_NET_PARAM_IPV4_GW:
1373                 case ISCSI_NET_PARAM_IPV6_ADDR:
1374                 case ISCSI_NET_PARAM_VLAN_ID:
1375                 case ISCSI_NET_PARAM_VLAN_PRIORITY:
1376                 case ISCSI_NET_PARAM_VLAN_ENABLED:
1377                         return S_IRUGO;
1378                 default:
1379                         return 0;
1380                 }
1381         case ISCSI_HOST_PARAM:
1382                 switch (param) {
1383                 case ISCSI_HOST_PARAM_HWADDRESS:
1384                 case ISCSI_HOST_PARAM_INITIATOR_NAME:
1385                 case ISCSI_HOST_PARAM_PORT_STATE:
1386                 case ISCSI_HOST_PARAM_PORT_SPEED:
1387                         return S_IRUGO;
1388                 default:
1389                         return 0;
1390                 }
1391         case ISCSI_PARAM:
1392                 switch (param) {
1393                 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1394                 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1395                 case ISCSI_PARAM_HDRDGST_EN:
1396                 case ISCSI_PARAM_DATADGST_EN:
1397                 case ISCSI_PARAM_CONN_ADDRESS:
1398                 case ISCSI_PARAM_CONN_PORT:
1399                 case ISCSI_PARAM_EXP_STATSN:
1400                 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1401                 case ISCSI_PARAM_PERSISTENT_PORT:
1402                 case ISCSI_PARAM_PING_TMO:
1403                 case ISCSI_PARAM_RECV_TMO:
1404                 case ISCSI_PARAM_INITIAL_R2T_EN:
1405                 case ISCSI_PARAM_MAX_R2T:
1406                 case ISCSI_PARAM_IMM_DATA_EN:
1407                 case ISCSI_PARAM_FIRST_BURST:
1408                 case ISCSI_PARAM_MAX_BURST:
1409                 case ISCSI_PARAM_PDU_INORDER_EN:
1410                 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1411                 case ISCSI_PARAM_ERL:
1412                 case ISCSI_PARAM_TARGET_NAME:
1413                 case ISCSI_PARAM_TPGT:
1414                 case ISCSI_PARAM_USERNAME:
1415                 case ISCSI_PARAM_PASSWORD:
1416                 case ISCSI_PARAM_USERNAME_IN:
1417                 case ISCSI_PARAM_PASSWORD_IN:
1418                 case ISCSI_PARAM_FAST_ABORT:
1419                 case ISCSI_PARAM_ABORT_TMO:
1420                 case ISCSI_PARAM_LU_RESET_TMO:
1421                 case ISCSI_PARAM_IFACE_NAME:
1422                 case ISCSI_PARAM_INITIATOR_NAME:
1423                         return S_IRUGO;
1424                 default:
1425                         return 0;
1426                 }
1427         }
1428
1429         return 0;
1430 }