1 /**
2  * Copyright (C) 2005 - 2010 ServerEngines
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 (jayamohank@serverengines.com)
11  *
12  * Contact Information:
13  * linux-drivers@serverengines.com
14  *
15  * ServerEngines
16  * 209 N. Fair Oaks Ave
17  * Sunnyvale, CA 94085
18  *
19  */
20 
21 #include <scsi/libiscsi.h>
22 #include <scsi/scsi_transport_iscsi.h>
23 #include <scsi/scsi_transport.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_device.h>
26 #include <scsi/scsi_host.h>
27 #include <scsi/scsi.h>
28 
29 #include "be_iscsi.h"
30 
31 extern struct iscsi_transport beiscsi_iscsi_transport;
32 
33 /**
34  * beiscsi_session_create - creates a new iscsi session
35  * @cmds_max: max commands supported
36  * @qdepth: max queue depth supported
37  * @initial_cmdsn: initial iscsi CMDSN
38  */
beiscsi_session_create(struct iscsi_endpoint * ep,u16 cmds_max,u16 qdepth,u32 initial_cmdsn)39 struct iscsi_cls_session *beiscsi_session_create(struct iscsi_endpoint *ep,
40 						 u16 cmds_max,
41 						 u16 qdepth,
42 						 u32 initial_cmdsn)
43 {
44 	struct Scsi_Host *shost;
45 	struct beiscsi_endpoint *beiscsi_ep;
46 	struct iscsi_cls_session *cls_session;
47 	struct beiscsi_hba *phba;
48 	struct iscsi_session *sess;
49 	struct beiscsi_session *beiscsi_sess;
50 	struct beiscsi_io_task *io_task;
51 
52 	SE_DEBUG(DBG_LVL_8, "In beiscsi_session_create\n");
53 
54 	if (!ep) {
55 		SE_DEBUG(DBG_LVL_1, "beiscsi_session_create: invalid ep\n");
56 		return NULL;
57 	}
58 	beiscsi_ep = ep->dd_data;
59 	phba = beiscsi_ep->phba;
60 	shost = phba->shost;
61 	if (cmds_max > beiscsi_ep->phba->params.wrbs_per_cxn) {
62 		shost_printk(KERN_ERR, shost, "Cannot handle %d cmds."
63 			     "Max cmds per session supported is %d. Using %d. "
64 			     "\n", cmds_max,
65 			      beiscsi_ep->phba->params.wrbs_per_cxn,
66 			      beiscsi_ep->phba->params.wrbs_per_cxn);
67 		cmds_max = beiscsi_ep->phba->params.wrbs_per_cxn;
68 	}
69 
70 	cls_session = iscsi_session_setup(&beiscsi_iscsi_transport,
71 					  shost, cmds_max,
72 					  sizeof(*beiscsi_sess),
73 					  sizeof(*io_task),
74 					  initial_cmdsn, ISCSI_MAX_TARGET);
75 	if (!cls_session)
76 		return NULL;
77 	sess = cls_session->dd_data;
78 	beiscsi_sess = sess->dd_data;
79 	beiscsi_sess->bhs_pool =  pci_pool_create("beiscsi_bhs_pool",
80 						   phba->pcidev,
81 						   sizeof(struct be_cmd_bhs),
82 						   64, 0);
83 	if (!beiscsi_sess->bhs_pool)
84 		goto destroy_sess;
85 
86 	return cls_session;
87 destroy_sess:
88 	iscsi_session_teardown(cls_session);
89 	return NULL;
90 }
91 
92 /**
93  * beiscsi_session_destroy - destroys iscsi session
94  * @cls_session:	pointer to iscsi cls session
95  *
96  * Destroys iSCSI session instance and releases
97  * resources allocated for it.
98  */
beiscsi_session_destroy(struct iscsi_cls_session * cls_session)99 void beiscsi_session_destroy(struct iscsi_cls_session *cls_session)
100 {
101 	struct iscsi_session *sess = cls_session->dd_data;
102 	struct beiscsi_session *beiscsi_sess = sess->dd_data;
103 
104 	SE_DEBUG(DBG_LVL_8, "In beiscsi_session_destroy\n");
105 	pci_pool_destroy(beiscsi_sess->bhs_pool);
106 	iscsi_session_teardown(cls_session);
107 }
108 
109 /**
110  * beiscsi_conn_create - create an instance of iscsi connection
111  * @cls_session: ptr to iscsi_cls_session
112  * @cid: iscsi cid
113  */
114 struct iscsi_cls_conn *
beiscsi_conn_create(struct iscsi_cls_session * cls_session,u32 cid)115 beiscsi_conn_create(struct iscsi_cls_session *cls_session, u32 cid)
116 {
117 	struct beiscsi_hba *phba;
118 	struct Scsi_Host *shost;
119 	struct iscsi_cls_conn *cls_conn;
120 	struct beiscsi_conn *beiscsi_conn;
121 	struct iscsi_conn *conn;
122 	struct iscsi_session *sess;
123 	struct beiscsi_session *beiscsi_sess;
124 
125 	SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_create ,cid"
126 		 "from iscsi layer=%d\n", cid);
127 	shost = iscsi_session_to_shost(cls_session);
128 	phba = iscsi_host_priv(shost);
129 
130 	cls_conn = iscsi_conn_setup(cls_session, sizeof(*beiscsi_conn), cid);
131 	if (!cls_conn)
132 		return NULL;
133 
134 	conn = cls_conn->dd_data;
135 	beiscsi_conn = conn->dd_data;
136 	beiscsi_conn->ep = NULL;
137 	beiscsi_conn->phba = phba;
138 	beiscsi_conn->conn = conn;
139 	sess = cls_session->dd_data;
140 	beiscsi_sess = sess->dd_data;
141 	beiscsi_conn->beiscsi_sess = beiscsi_sess;
142 	return cls_conn;
143 }
144 
145 /**
146  * beiscsi_bindconn_cid - Bind the beiscsi_conn with phba connection table
147  * @beiscsi_conn: The pointer to  beiscsi_conn structure
148  * @phba: The phba instance
149  * @cid: The cid to free
150  */
beiscsi_bindconn_cid(struct beiscsi_hba * phba,struct beiscsi_conn * beiscsi_conn,unsigned int cid)151 static int beiscsi_bindconn_cid(struct beiscsi_hba *phba,
152 				struct beiscsi_conn *beiscsi_conn,
153 				unsigned int cid)
154 {
155 	if (phba->conn_table[cid]) {
156 		SE_DEBUG(DBG_LVL_1,
157 			 "Connection table already occupied. Detected clash\n");
158 		return -EINVAL;
159 	} else {
160 		SE_DEBUG(DBG_LVL_8, "phba->conn_table[%d]=%p(beiscsi_conn)\n",
161 			 cid, beiscsi_conn);
162 		phba->conn_table[cid] = beiscsi_conn;
163 	}
164 	return 0;
165 }
166 
167 /**
168  * beiscsi_conn_bind - Binds iscsi session/connection with TCP connection
169  * @cls_session: pointer to iscsi cls session
170  * @cls_conn: pointer to iscsi cls conn
171  * @transport_fd: EP handle(64 bit)
172  *
173  * This function binds the TCP Conn with iSCSI Connection and Session.
174  */
beiscsi_conn_bind(struct iscsi_cls_session * cls_session,struct iscsi_cls_conn * cls_conn,u64 transport_fd,int is_leading)175 int beiscsi_conn_bind(struct iscsi_cls_session *cls_session,
176 		      struct iscsi_cls_conn *cls_conn,
177 		      u64 transport_fd, int is_leading)
178 {
179 	struct iscsi_conn *conn = cls_conn->dd_data;
180 	struct beiscsi_conn *beiscsi_conn = conn->dd_data;
181 	struct Scsi_Host *shost =
182 		(struct Scsi_Host *)iscsi_session_to_shost(cls_session);
183 	struct beiscsi_hba *phba = (struct beiscsi_hba *)iscsi_host_priv(shost);
184 	struct beiscsi_endpoint *beiscsi_ep;
185 	struct iscsi_endpoint *ep;
186 
187 	SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_bind\n");
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 		SE_DEBUG(DBG_LVL_8,
199 			 "beiscsi_ep->hba=%p not equal to phba=%p\n",
200 			 beiscsi_ep->phba, phba);
201 		return -EEXIST;
202 	}
203 
204 	beiscsi_conn->beiscsi_conn_cid = beiscsi_ep->ep_cid;
205 	beiscsi_conn->ep = beiscsi_ep;
206 	beiscsi_ep->conn = beiscsi_conn;
207 	SE_DEBUG(DBG_LVL_8, "beiscsi_conn=%p conn=%p ep_cid=%d\n",
208 		 beiscsi_conn, conn, beiscsi_ep->ep_cid);
209 	return beiscsi_bindconn_cid(phba, beiscsi_conn, beiscsi_ep->ep_cid);
210 }
211 
212 /**
213  * beiscsi_ep_get_param - get the iscsi parameter
214  * @ep: pointer to iscsi ep
215  * @param: parameter type identifier
216  * @buf: buffer pointer
217  *
218  * returns iscsi parameter
219  */
beiscsi_ep_get_param(struct iscsi_endpoint * ep,enum iscsi_param param,char * buf)220 int beiscsi_ep_get_param(struct iscsi_endpoint *ep,
221 			   enum iscsi_param param, char *buf)
222 {
223 	struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
224 	int len = 0;
225 
226 	SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_param, param= %d\n", param);
227 
228 	switch (param) {
229 	case ISCSI_PARAM_CONN_PORT:
230 		len = sprintf(buf, "%hu\n", beiscsi_ep->dst_tcpport);
231 		break;
232 	case ISCSI_PARAM_CONN_ADDRESS:
233 		if (beiscsi_ep->ip_type == BE2_IPV4)
234 			len = sprintf(buf, "%pI4\n", &beiscsi_ep->dst_addr);
235 		else
236 			len = sprintf(buf, "%pI6\n", &beiscsi_ep->dst6_addr);
237 		break;
238 	default:
239 		return -ENOSYS;
240 	}
241 	return len;
242 }
243 
beiscsi_set_param(struct iscsi_cls_conn * cls_conn,enum iscsi_param param,char * buf,int buflen)244 int beiscsi_set_param(struct iscsi_cls_conn *cls_conn,
245 		      enum iscsi_param param, char *buf, int buflen)
246 {
247 	struct iscsi_conn *conn = cls_conn->dd_data;
248 	struct iscsi_session *session = conn->session;
249 	int ret;
250 
251 	SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_set_param, param= %d\n", param);
252 	ret = iscsi_set_param(cls_conn, param, buf, buflen);
253 	if (ret)
254 		return ret;
255 	/*
256 	 * If userspace tried to set the value to higher than we can
257 	 * support override here.
258 	 */
259 	switch (param) {
260 	case ISCSI_PARAM_FIRST_BURST:
261 		if (session->first_burst > 8192)
262 			session->first_burst = 8192;
263 		break;
264 	case ISCSI_PARAM_MAX_RECV_DLENGTH:
265 		if (conn->max_recv_dlength > 65536)
266 			conn->max_recv_dlength = 65536;
267 		break;
268 	case ISCSI_PARAM_MAX_BURST:
269 		if (session->max_burst > 262144)
270 			session->max_burst = 262144;
271 		break;
272 	case ISCSI_PARAM_MAX_XMIT_DLENGTH:
273 		if ((conn->max_xmit_dlength > 65536) ||
274 		    (conn->max_xmit_dlength == 0))
275 			conn->max_xmit_dlength = 65536;
276 	default:
277 		return 0;
278 	}
279 
280 	return 0;
281 }
282 
283 /**
284  * beiscsi_get_host_param - get the iscsi parameter
285  * @shost: pointer to scsi_host structure
286  * @param: parameter type identifier
287  * @buf: buffer pointer
288  *
289  * returns host parameter
290  */
beiscsi_get_host_param(struct Scsi_Host * shost,enum iscsi_host_param param,char * buf)291 int beiscsi_get_host_param(struct Scsi_Host *shost,
292 			   enum iscsi_host_param param, char *buf)
293 {
294 	struct beiscsi_hba *phba = (struct beiscsi_hba *)iscsi_host_priv(shost);
295 	int status = 0;
296 
297 	SE_DEBUG(DBG_LVL_8, "In beiscsi_get_host_param, param= %d\n", param);
298 	switch (param) {
299 	case ISCSI_HOST_PARAM_HWADDRESS:
300 		status = beiscsi_get_macaddr(buf, phba);
301 		if (status < 0) {
302 			SE_DEBUG(DBG_LVL_1, "beiscsi_get_macaddr Failed\n");
303 			return status;
304 		}
305 		break;
306 	default:
307 		return iscsi_host_get_param(shost, param, buf);
308 	}
309 	return status;
310 }
311 
beiscsi_get_macaddr(char * buf,struct beiscsi_hba * phba)312 int beiscsi_get_macaddr(char *buf, struct beiscsi_hba *phba)
313 {
314 	struct be_cmd_resp_get_mac_addr *resp;
315 	struct be_mcc_wrb *wrb;
316 	unsigned int tag, wrb_num;
317 	unsigned short status, extd_status;
318 	struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
319 	int rc;
320 
321 	if (phba->read_mac_address)
322 		return sysfs_format_mac(buf, phba->mac_address,
323 					ETH_ALEN);
324 
325 	tag = be_cmd_get_mac_addr(phba);
326 	if (!tag) {
327 		SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed\n");
328 		return -EBUSY;
329 	} else
330 		wait_event_interruptible(phba->ctrl.mcc_wait[tag],
331 					 phba->ctrl.mcc_numtag[tag]);
332 
333 	wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
334 	extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
335 	status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
336 	if (status || extd_status) {
337 		SE_DEBUG(DBG_LVL_1, "Failed to get be_cmd_get_mac_addr"
338 				    " status = %d extd_status = %d\n",
339 				    status, extd_status);
340 		free_mcc_tag(&phba->ctrl, tag);
341 		return -EAGAIN;
342 	}
343 	wrb = queue_get_wrb(mccq, wrb_num);
344 	free_mcc_tag(&phba->ctrl, tag);
345 	resp = embedded_payload(wrb);
346 	memcpy(phba->mac_address, resp->mac_address, ETH_ALEN);
347 	rc = sysfs_format_mac(buf, phba->mac_address,
348 			       ETH_ALEN);
349 	phba->read_mac_address = 1;
350 	return rc;
351 }
352 
353 
354 /**
355  * beiscsi_conn_get_stats - get the iscsi stats
356  * @cls_conn: pointer to iscsi cls conn
357  * @stats: pointer to iscsi_stats structure
358  *
359  * returns iscsi stats
360  */
beiscsi_conn_get_stats(struct iscsi_cls_conn * cls_conn,struct iscsi_stats * stats)361 void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
362 			    struct iscsi_stats *stats)
363 {
364 	struct iscsi_conn *conn = cls_conn->dd_data;
365 
366 	SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_stats\n");
367 	stats->txdata_octets = conn->txdata_octets;
368 	stats->rxdata_octets = conn->rxdata_octets;
369 	stats->dataout_pdus = conn->dataout_pdus_cnt;
370 	stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
371 	stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
372 	stats->datain_pdus = conn->datain_pdus_cnt;
373 	stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
374 	stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
375 	stats->r2t_pdus = conn->r2t_pdus_cnt;
376 	stats->digest_err = 0;
377 	stats->timeout_err = 0;
378 	stats->custom_length = 0;
379 	strcpy(stats->custom[0].desc, "eh_abort_cnt");
380 	stats->custom[0].value = conn->eh_abort_cnt;
381 }
382 
383 /**
384  * beiscsi_set_params_for_offld - get the parameters for offload
385  * @beiscsi_conn: pointer to beiscsi_conn
386  * @params: pointer to offload_params structure
387  */
beiscsi_set_params_for_offld(struct beiscsi_conn * beiscsi_conn,struct beiscsi_offload_params * params)388 static void  beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
389 					  struct beiscsi_offload_params *params)
390 {
391 	struct iscsi_conn *conn = beiscsi_conn->conn;
392 	struct iscsi_session *session = conn->session;
393 
394 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
395 		      params, session->max_burst);
396 	AMAP_SET_BITS(struct amap_beiscsi_offload_params,
397 		      max_send_data_segment_length, params,
398 		      conn->max_xmit_dlength);
399 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
400 		      params, session->first_burst);
401 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
402 		      session->erl);
403 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
404 		      conn->datadgst_en);
405 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
406 		      conn->hdrdgst_en);
407 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
408 		      session->initial_r2t_en);
409 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
410 		      session->imm_data_en);
411 	AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
412 		      (conn->exp_statsn - 1));
413 }
414 
415 /**
416  * beiscsi_conn_start - offload of session to chip
417  * @cls_conn: pointer to beiscsi_conn
418  */
beiscsi_conn_start(struct iscsi_cls_conn * cls_conn)419 int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
420 {
421 	struct iscsi_conn *conn = cls_conn->dd_data;
422 	struct beiscsi_conn *beiscsi_conn = conn->dd_data;
423 	struct beiscsi_endpoint *beiscsi_ep;
424 	struct beiscsi_offload_params params;
425 
426 	SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_start\n");
427 	memset(&params, 0, sizeof(struct beiscsi_offload_params));
428 	beiscsi_ep = beiscsi_conn->ep;
429 	if (!beiscsi_ep)
430 		SE_DEBUG(DBG_LVL_1, "In beiscsi_conn_start , no beiscsi_ep\n");
431 
432 	beiscsi_conn->login_in_progress = 0;
433 	beiscsi_set_params_for_offld(beiscsi_conn, &params);
434 	beiscsi_offload_connection(beiscsi_conn, &params);
435 	iscsi_conn_start(cls_conn);
436 	return 0;
437 }
438 
439 /**
440  * beiscsi_get_cid - Allocate a cid
441  * @phba: The phba instance
442  */
beiscsi_get_cid(struct beiscsi_hba * phba)443 static int beiscsi_get_cid(struct beiscsi_hba *phba)
444 {
445 	unsigned short cid = 0xFFFF;
446 
447 	if (!phba->avlbl_cids)
448 		return cid;
449 
450 	cid = phba->cid_array[phba->cid_alloc++];
451 	if (phba->cid_alloc == phba->params.cxns_per_ctrl)
452 		phba->cid_alloc = 0;
453 	phba->avlbl_cids--;
454 	return cid;
455 }
456 
457 /**
458  * beiscsi_put_cid - Free the cid
459  * @phba: The phba for which the cid is being freed
460  * @cid: The cid to free
461  */
beiscsi_put_cid(struct beiscsi_hba * phba,unsigned short cid)462 static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
463 {
464 	phba->avlbl_cids++;
465 	phba->cid_array[phba->cid_free++] = cid;
466 	if (phba->cid_free == phba->params.cxns_per_ctrl)
467 		phba->cid_free = 0;
468 }
469 
470 /**
471  * beiscsi_free_ep - free endpoint
472  * @ep:	pointer to iscsi endpoint structure
473  */
beiscsi_free_ep(struct beiscsi_endpoint * beiscsi_ep)474 static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
475 {
476 	struct beiscsi_hba *phba = beiscsi_ep->phba;
477 
478 	beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
479 	beiscsi_ep->phba = NULL;
480 }
481 
482 /**
483  * beiscsi_open_conn - Ask FW to open a TCP connection
484  * @ep:	endpoint to be used
485  * @src_addr: The source IP address
486  * @dst_addr: The Destination  IP address
487  *
488  * Asks the FW to open a TCP connection
489  */
beiscsi_open_conn(struct iscsi_endpoint * ep,struct sockaddr * src_addr,struct sockaddr * dst_addr,int non_blocking)490 static int beiscsi_open_conn(struct iscsi_endpoint *ep,
491 			     struct sockaddr *src_addr,
492 			     struct sockaddr *dst_addr, int non_blocking)
493 {
494 	struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
495 	struct beiscsi_hba *phba = beiscsi_ep->phba;
496 	struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
497 	struct be_mcc_wrb *wrb;
498 	struct tcp_connect_and_offload_out *ptcpcnct_out;
499 	unsigned short status, extd_status;
500 	struct be_dma_mem nonemb_cmd;
501 	unsigned int tag, wrb_num;
502 	int ret = -ENOMEM;
503 
504 	SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn\n");
505 	beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
506 	if (beiscsi_ep->ep_cid == 0xFFFF) {
507 		SE_DEBUG(DBG_LVL_1, "No free cid available\n");
508 		return ret;
509 	}
510 	SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn, ep_cid=%d\n",
511 		 beiscsi_ep->ep_cid);
512 	phba->ep_array[beiscsi_ep->ep_cid -
513 		       phba->fw_config.iscsi_cid_start] = ep;
514 	if (beiscsi_ep->ep_cid > (phba->fw_config.iscsi_cid_start +
515 				  phba->params.cxns_per_ctrl * 2)) {
516 		SE_DEBUG(DBG_LVL_1, "Failed in allocate iscsi cid\n");
517 		goto free_ep;
518 	}
519 
520 	beiscsi_ep->cid_vld = 0;
521 	nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
522 				sizeof(struct tcp_connect_and_offload_in),
523 				&nonemb_cmd.dma);
524 	if (nonemb_cmd.va == NULL) {
525 		SE_DEBUG(DBG_LVL_1,
526 			 "Failed to allocate memory for mgmt_open_connection"
527 			 "\n");
528 		beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
529 		return -ENOMEM;
530 	}
531 	nonemb_cmd.size = sizeof(struct tcp_connect_and_offload_in);
532 	memset(nonemb_cmd.va, 0, nonemb_cmd.size);
533 	tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
534 	if (!tag) {
535 		SE_DEBUG(DBG_LVL_1,
536 			 "mgmt_open_connection Failed for cid=%d\n",
537 			 beiscsi_ep->ep_cid);
538 		beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
539 		pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
540 				    nonemb_cmd.va, nonemb_cmd.dma);
541 		return -EAGAIN;
542 	} else {
543 		wait_event_interruptible(phba->ctrl.mcc_wait[tag],
544 					 phba->ctrl.mcc_numtag[tag]);
545 	}
546 	wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
547 	extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
548 	status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
549 	if (status || extd_status) {
550 		SE_DEBUG(DBG_LVL_1, "mgmt_open_connection Failed"
551 				    " status = %d extd_status = %d\n",
552 				    status, extd_status);
553 		free_mcc_tag(&phba->ctrl, tag);
554 		pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
555 			    nonemb_cmd.va, nonemb_cmd.dma);
556 		goto free_ep;
557 	} else {
558 		wrb = queue_get_wrb(mccq, wrb_num);
559 		free_mcc_tag(&phba->ctrl, tag);
560 
561 		ptcpcnct_out = embedded_payload(wrb);
562 		beiscsi_ep = ep->dd_data;
563 		beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
564 		beiscsi_ep->cid_vld = 1;
565 		SE_DEBUG(DBG_LVL_8, "mgmt_open_connection Success\n");
566 	}
567 	pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
568 			    nonemb_cmd.va, nonemb_cmd.dma);
569 	return 0;
570 
571 free_ep:
572 	beiscsi_free_ep(beiscsi_ep);
573 	return -EBUSY;
574 }
575 
576 /**
577  * beiscsi_ep_connect - Ask chip to create TCP Conn
578  * @scsi_host: Pointer to scsi_host structure
579  * @dst_addr: The IP address of Target
580  * @non_blocking: blocking or non-blocking call
581  *
582  * This routines first asks chip to create a connection and then allocates an EP
583  */
584 struct iscsi_endpoint *
beiscsi_ep_connect(struct Scsi_Host * shost,struct sockaddr * dst_addr,int non_blocking)585 beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
586 		   int non_blocking)
587 {
588 	struct beiscsi_hba *phba;
589 	struct beiscsi_endpoint *beiscsi_ep;
590 	struct iscsi_endpoint *ep;
591 	int ret;
592 
593 	SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_connect\n");
594 	if (shost)
595 		phba = iscsi_host_priv(shost);
596 	else {
597 		ret = -ENXIO;
598 		SE_DEBUG(DBG_LVL_1, "shost is NULL\n");
599 		return ERR_PTR(ret);
600 	}
601 
602 	if (phba->state != BE_ADAPTER_UP) {
603 		ret = -EBUSY;
604 		SE_DEBUG(DBG_LVL_1, "The Adapter state is Not UP\n");
605 		return ERR_PTR(ret);
606 	}
607 
608 	ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
609 	if (!ep) {
610 		ret = -ENOMEM;
611 		return ERR_PTR(ret);
612 	}
613 
614 	beiscsi_ep = ep->dd_data;
615 	beiscsi_ep->phba = phba;
616 	beiscsi_ep->openiscsi_ep = ep;
617 	ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
618 	if (ret) {
619 		SE_DEBUG(DBG_LVL_1, "Failed in beiscsi_open_conn\n");
620 		goto free_ep;
621 	}
622 
623 	return ep;
624 
625 free_ep:
626 	iscsi_destroy_endpoint(ep);
627 	return ERR_PTR(ret);
628 }
629 
630 /**
631  * beiscsi_ep_poll - Poll to see if connection is established
632  * @ep:	endpoint to be used
633  * @timeout_ms: timeout specified in millisecs
634  *
635  * Poll to see if TCP connection established
636  */
beiscsi_ep_poll(struct iscsi_endpoint * ep,int timeout_ms)637 int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
638 {
639 	struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
640 
641 	SE_DEBUG(DBG_LVL_8, "In  beiscsi_ep_poll\n");
642 	if (beiscsi_ep->cid_vld == 1)
643 		return 1;
644 	else
645 		return 0;
646 }
647 
648 /**
649  * beiscsi_close_conn - Upload the  connection
650  * @ep: The iscsi endpoint
651  * @flag: The type of connection closure
652  */
beiscsi_close_conn(struct beiscsi_endpoint * beiscsi_ep,int flag)653 static int beiscsi_close_conn(struct  beiscsi_endpoint *beiscsi_ep, int flag)
654 {
655 	int ret = 0;
656 	unsigned int tag;
657 	struct beiscsi_hba *phba = beiscsi_ep->phba;
658 
659 	tag = mgmt_upload_connection(phba, beiscsi_ep->ep_cid, flag);
660 	if (!tag) {
661 		SE_DEBUG(DBG_LVL_8, "upload failed for cid 0x%x\n",
662 			 beiscsi_ep->ep_cid);
663 		ret = -EAGAIN;
664 	} else {
665 		wait_event_interruptible(phba->ctrl.mcc_wait[tag],
666 					 phba->ctrl.mcc_numtag[tag]);
667 		free_mcc_tag(&phba->ctrl, tag);
668 	}
669 	return ret;
670 }
671 
672 /**
673  * beiscsi_unbind_conn_to_cid - Unbind the beiscsi_conn from phba conn table
674  * @phba: The phba instance
675  * @cid: The cid to free
676  */
beiscsi_unbind_conn_to_cid(struct beiscsi_hba * phba,unsigned int cid)677 static int beiscsi_unbind_conn_to_cid(struct beiscsi_hba *phba,
678 				      unsigned int cid)
679 {
680 	if (phba->conn_table[cid])
681 		phba->conn_table[cid] = NULL;
682 	else {
683 		SE_DEBUG(DBG_LVL_8, "Connection table Not occupied.\n");
684 		return -EINVAL;
685 	}
686 	return 0;
687 }
688 
689 /**
690  * beiscsi_ep_disconnect - Tears down the TCP connection
691  * @ep:	endpoint to be used
692  *
693  * Tears down the TCP connection
694  */
beiscsi_ep_disconnect(struct iscsi_endpoint * ep)695 void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
696 {
697 	struct beiscsi_conn *beiscsi_conn;
698 	struct beiscsi_endpoint *beiscsi_ep;
699 	struct beiscsi_hba *phba;
700 	unsigned int tag;
701 	unsigned short savecfg_flag = CMD_ISCSI_SESSION_SAVE_CFG_ON_FLASH;
702 
703 	beiscsi_ep = ep->dd_data;
704 	phba = beiscsi_ep->phba;
705 	SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect for ep_cid = %d\n",
706 			     beiscsi_ep->ep_cid);
707 
708 	if (!beiscsi_ep->conn) {
709 		SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect, no "
710 			 "beiscsi_ep\n");
711 		return;
712 	}
713 	beiscsi_conn = beiscsi_ep->conn;
714 	iscsi_suspend_queue(beiscsi_conn->conn);
715 
716 	SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect ep_cid = %d\n",
717 		 beiscsi_ep->ep_cid);
718 
719 	tag = mgmt_invalidate_connection(phba, beiscsi_ep,
720 					    beiscsi_ep->ep_cid, 1,
721 					    savecfg_flag);
722 	if (!tag) {
723 		SE_DEBUG(DBG_LVL_1,
724 			 "mgmt_invalidate_connection Failed for cid=%d\n",
725 			  beiscsi_ep->ep_cid);
726 	} else {
727 		wait_event_interruptible(phba->ctrl.mcc_wait[tag],
728 					 phba->ctrl.mcc_numtag[tag]);
729 		free_mcc_tag(&phba->ctrl, tag);
730 	}
731 
732 	beiscsi_close_conn(beiscsi_ep, CONNECTION_UPLOAD_GRACEFUL);
733 	beiscsi_free_ep(beiscsi_ep);
734 	beiscsi_unbind_conn_to_cid(phba, beiscsi_ep->ep_cid);
735 	iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
736 }
737