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