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