[SCSI] 53c700: remove dma_is_consistent usage
[linux-2.6-block.git] / drivers / scsi / be2iscsi / be_iscsi.c
CommitLineData
6733b39a 1/**
d2eeb1ac 2 * Copyright (C) 2005 - 2010 ServerEngines
6733b39a
JK
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
31extern 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 */
39struct 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;
6733b39a 47 struct beiscsi_hba *phba;
b8b9e1b8
JK
48 struct iscsi_session *sess;
49 struct beiscsi_session *beiscsi_sess;
6733b39a 50 struct beiscsi_io_task *io_task;
6733b39a
JK
51
52 SE_DEBUG(DBG_LVL_8, "In beiscsi_session_create\n");
53
54 if (!ep) {
457ff3b7 55 SE_DEBUG(DBG_LVL_1, "beiscsi_session_create: invalid ep\n");
6733b39a
JK
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
bfead3b2
JK
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);
6733b39a
JK
75 if (!cls_session)
76 return NULL;
77 sess = cls_session->dd_data;
2afc95bf
JK
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;
6733b39a 85
6733b39a 86 return cls_session;
2afc95bf
JK
87destroy_sess:
88 iscsi_session_teardown(cls_session);
89 return NULL;
6733b39a
JK
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 */
99void beiscsi_session_destroy(struct iscsi_cls_session *cls_session)
100{
6733b39a 101 struct iscsi_session *sess = cls_session->dd_data;
2afc95bf 102 struct beiscsi_session *beiscsi_sess = sess->dd_data;
6733b39a 103
756d29c8 104 SE_DEBUG(DBG_LVL_8, "In beiscsi_session_destroy\n");
2afc95bf 105 pci_pool_destroy(beiscsi_sess->bhs_pool);
6733b39a
JK
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 */
114struct iscsi_cls_conn *
115beiscsi_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;
2afc95bf
JK
122 struct iscsi_session *sess;
123 struct beiscsi_session *beiscsi_sess;
6733b39a
JK
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;
2afc95bf
JK
139 sess = cls_session->dd_data;
140 beiscsi_sess = sess->dd_data;
141 beiscsi_conn->beiscsi_sess = beiscsi_sess;
6733b39a
JK
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 */
151static 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 {
457ff3b7 160 SE_DEBUG(DBG_LVL_8, "phba->conn_table[%d]=%p(beiscsi_conn)\n",
6733b39a
JK
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 */
175int 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,
457ff3b7 199 "beiscsi_ep->hba=%p not equal to phba=%p\n",
6733b39a
JK
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;
457ff3b7 207 SE_DEBUG(DBG_LVL_8, "beiscsi_conn=%p conn=%p ep_cid=%d\n",
6733b39a
JK
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_conn_get_param - get the iscsi parameter
214 * @cls_conn: pointer to iscsi cls conn
215 * @param: parameter type identifier
216 * @buf: buffer pointer
217 *
218 * returns iscsi parameter
219 */
220int beiscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
221 enum iscsi_param param, char *buf)
222{
223 struct beiscsi_endpoint *beiscsi_ep;
224 struct iscsi_conn *conn = cls_conn->dd_data;
225 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
226 int len = 0;
227
756d29c8 228 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_param, param= %d\n", param);
6733b39a
JK
229 beiscsi_ep = beiscsi_conn->ep;
230 if (!beiscsi_ep) {
231 SE_DEBUG(DBG_LVL_1,
232 "In beiscsi_conn_get_param , no beiscsi_ep\n");
d3ad2bb3 233 return -ENODEV;
6733b39a
JK
234 }
235
236 switch (param) {
237 case ISCSI_PARAM_CONN_PORT:
238 len = sprintf(buf, "%hu\n", beiscsi_ep->dst_tcpport);
239 break;
240 case ISCSI_PARAM_CONN_ADDRESS:
241 if (beiscsi_ep->ip_type == BE2_IPV4)
242 len = sprintf(buf, "%pI4\n", &beiscsi_ep->dst_addr);
243 else
244 len = sprintf(buf, "%pI6\n", &beiscsi_ep->dst6_addr);
245 break;
246 default:
247 return iscsi_conn_get_param(cls_conn, param, buf);
248 }
249 return len;
250}
251
252int beiscsi_set_param(struct iscsi_cls_conn *cls_conn,
253 enum iscsi_param param, char *buf, int buflen)
254{
255 struct iscsi_conn *conn = cls_conn->dd_data;
256 struct iscsi_session *session = conn->session;
257 int ret;
258
756d29c8 259 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_set_param, param= %d\n", param);
6733b39a
JK
260 ret = iscsi_set_param(cls_conn, param, buf, buflen);
261 if (ret)
262 return ret;
263 /*
264 * If userspace tried to set the value to higher than we can
265 * support override here.
266 */
267 switch (param) {
268 case ISCSI_PARAM_FIRST_BURST:
269 if (session->first_burst > 8192)
270 session->first_burst = 8192;
271 break;
272 case ISCSI_PARAM_MAX_RECV_DLENGTH:
273 if (conn->max_recv_dlength > 65536)
274 conn->max_recv_dlength = 65536;
275 break;
276 case ISCSI_PARAM_MAX_BURST:
230dceb4
JK
277 if (session->max_burst > 262144)
278 session->max_burst = 262144;
6733b39a 279 break;
42f43c41
JK
280 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
281 if ((conn->max_xmit_dlength > 65536) ||
282 (conn->max_xmit_dlength == 0))
283 conn->max_xmit_dlength = 65536;
6733b39a
JK
284 default:
285 return 0;
286 }
287
288 return 0;
289}
290
291/**
292 * beiscsi_get_host_param - get the iscsi parameter
293 * @shost: pointer to scsi_host structure
294 * @param: parameter type identifier
295 * @buf: buffer pointer
296 *
297 * returns host parameter
298 */
299int beiscsi_get_host_param(struct Scsi_Host *shost,
300 enum iscsi_host_param param, char *buf)
301{
302 struct beiscsi_hba *phba = (struct beiscsi_hba *)iscsi_host_priv(shost);
756d29c8
JK
303 struct be_cmd_resp_get_mac_addr *resp;
304 struct be_mcc_wrb *wrb;
305 unsigned int tag, wrb_num;
6733b39a 306 int len = 0;
756d29c8
JK
307 unsigned short status, extd_status;
308 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
6733b39a 309
756d29c8 310 SE_DEBUG(DBG_LVL_8, "In beiscsi_get_host_param, param= %d\n", param);
6733b39a
JK
311 switch (param) {
312 case ISCSI_HOST_PARAM_HWADDRESS:
756d29c8
JK
313 tag = be_cmd_get_mac_addr(phba);
314 if (!tag) {
457ff3b7 315 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed\n");
d3ad2bb3 316 return -EAGAIN;
756d29c8
JK
317 } else
318 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
319 phba->ctrl.mcc_numtag[tag]);
320
321 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
322 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
323 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
324 if (status || extd_status) {
325 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed"
457ff3b7 326 " status = %d extd_status = %d\n",
756d29c8
JK
327 status, extd_status);
328 free_mcc_tag(&phba->ctrl, tag);
d3ad2bb3 329 return -EAGAIN;
756d29c8
JK
330 } else {
331 wrb = queue_get_wrb(mccq, wrb_num);
332 free_mcc_tag(&phba->ctrl, tag);
333 resp = embedded_payload(wrb);
334 memcpy(phba->mac_address, resp->mac_address, ETH_ALEN);
335 len = sysfs_format_mac(buf, phba->mac_address,
336 ETH_ALEN);
337 }
6733b39a
JK
338 break;
339 default:
340 return iscsi_host_get_param(shost, param, buf);
341 }
342 return len;
343}
344
345/**
346 * beiscsi_conn_get_stats - get the iscsi stats
347 * @cls_conn: pointer to iscsi cls conn
348 * @stats: pointer to iscsi_stats structure
349 *
350 * returns iscsi stats
351 */
352void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
353 struct iscsi_stats *stats)
354{
355 struct iscsi_conn *conn = cls_conn->dd_data;
356
357 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_stats\n");
358 stats->txdata_octets = conn->txdata_octets;
359 stats->rxdata_octets = conn->rxdata_octets;
360 stats->dataout_pdus = conn->dataout_pdus_cnt;
361 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
362 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
363 stats->datain_pdus = conn->datain_pdus_cnt;
364 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
365 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
366 stats->r2t_pdus = conn->r2t_pdus_cnt;
367 stats->digest_err = 0;
368 stats->timeout_err = 0;
369 stats->custom_length = 0;
370 strcpy(stats->custom[0].desc, "eh_abort_cnt");
371 stats->custom[0].value = conn->eh_abort_cnt;
372}
373
374/**
375 * beiscsi_set_params_for_offld - get the parameters for offload
376 * @beiscsi_conn: pointer to beiscsi_conn
377 * @params: pointer to offload_params structure
378 */
379static void beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
380 struct beiscsi_offload_params *params)
381{
382 struct iscsi_conn *conn = beiscsi_conn->conn;
383 struct iscsi_session *session = conn->session;
384
385 AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
386 params, session->max_burst);
387 AMAP_SET_BITS(struct amap_beiscsi_offload_params,
388 max_send_data_segment_length, params,
389 conn->max_xmit_dlength);
390 AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
391 params, session->first_burst);
392 AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
393 session->erl);
394 AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
395 conn->datadgst_en);
396 AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
397 conn->hdrdgst_en);
398 AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
399 session->initial_r2t_en);
400 AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
401 session->imm_data_en);
402 AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
403 (conn->exp_statsn - 1));
404}
405
406/**
407 * beiscsi_conn_start - offload of session to chip
408 * @cls_conn: pointer to beiscsi_conn
409 */
410int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
411{
412 struct iscsi_conn *conn = cls_conn->dd_data;
413 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
414 struct beiscsi_endpoint *beiscsi_ep;
415 struct beiscsi_offload_params params;
6733b39a 416
756d29c8 417 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_start\n");
6733b39a
JK
418 memset(&params, 0, sizeof(struct beiscsi_offload_params));
419 beiscsi_ep = beiscsi_conn->ep;
420 if (!beiscsi_ep)
421 SE_DEBUG(DBG_LVL_1, "In beiscsi_conn_start , no beiscsi_ep\n");
422
6733b39a
JK
423 beiscsi_conn->login_in_progress = 0;
424 beiscsi_set_params_for_offld(beiscsi_conn, &params);
425 beiscsi_offload_connection(beiscsi_conn, &params);
426 iscsi_conn_start(cls_conn);
427 return 0;
428}
429
430/**
431 * beiscsi_get_cid - Allocate a cid
432 * @phba: The phba instance
433 */
434static int beiscsi_get_cid(struct beiscsi_hba *phba)
435{
436 unsigned short cid = 0xFFFF;
437
438 if (!phba->avlbl_cids)
439 return cid;
440
441 cid = phba->cid_array[phba->cid_alloc++];
442 if (phba->cid_alloc == phba->params.cxns_per_ctrl)
443 phba->cid_alloc = 0;
444 phba->avlbl_cids--;
445 return cid;
446}
447
fa95d206
MC
448/**
449 * beiscsi_put_cid - Free the cid
450 * @phba: The phba for which the cid is being freed
451 * @cid: The cid to free
452 */
453static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
454{
455 phba->avlbl_cids++;
456 phba->cid_array[phba->cid_free++] = cid;
457 if (phba->cid_free == phba->params.cxns_per_ctrl)
458 phba->cid_free = 0;
459}
460
461/**
462 * beiscsi_free_ep - free endpoint
463 * @ep: pointer to iscsi endpoint structure
464 */
465static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
466{
467 struct beiscsi_hba *phba = beiscsi_ep->phba;
468
469 beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
470 beiscsi_ep->phba = NULL;
471}
472
6733b39a
JK
473/**
474 * beiscsi_open_conn - Ask FW to open a TCP connection
475 * @ep: endpoint to be used
476 * @src_addr: The source IP address
477 * @dst_addr: The Destination IP address
478 *
479 * Asks the FW to open a TCP connection
480 */
481static int beiscsi_open_conn(struct iscsi_endpoint *ep,
482 struct sockaddr *src_addr,
483 struct sockaddr *dst_addr, int non_blocking)
484{
485 struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
486 struct beiscsi_hba *phba = beiscsi_ep->phba;
756d29c8
JK
487 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
488 struct be_mcc_wrb *wrb;
489 struct tcp_connect_and_offload_out *ptcpcnct_out;
490 unsigned short status, extd_status;
3cbb7a74 491 struct be_dma_mem nonemb_cmd;
756d29c8 492 unsigned int tag, wrb_num;
d3ad2bb3 493 int ret = -ENOMEM;
6733b39a 494
756d29c8 495 SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn\n");
6733b39a
JK
496 beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
497 if (beiscsi_ep->ep_cid == 0xFFFF) {
498 SE_DEBUG(DBG_LVL_1, "No free cid available\n");
499 return ret;
500 }
457ff3b7 501 SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn, ep_cid=%d\n",
6733b39a 502 beiscsi_ep->ep_cid);
7da50879
JK
503 phba->ep_array[beiscsi_ep->ep_cid -
504 phba->fw_config.iscsi_cid_start] = ep;
505 if (beiscsi_ep->ep_cid > (phba->fw_config.iscsi_cid_start +
506 phba->params.cxns_per_ctrl * 2)) {
6733b39a 507 SE_DEBUG(DBG_LVL_1, "Failed in allocate iscsi cid\n");
3cbb7a74 508 beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
fa95d206 509 goto free_ep;
6733b39a
JK
510 }
511
512 beiscsi_ep->cid_vld = 0;
3cbb7a74
JK
513 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
514 sizeof(struct tcp_connect_and_offload_in),
515 &nonemb_cmd.dma);
516 if (nonemb_cmd.va == NULL) {
517 SE_DEBUG(DBG_LVL_1,
518 "Failed to allocate memory for mgmt_open_connection"
519 "\n");
520 beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
521 return -ENOMEM;
522 }
523 nonemb_cmd.size = sizeof(struct tcp_connect_and_offload_in);
524 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
525 tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
756d29c8
JK
526 if (!tag) {
527 SE_DEBUG(DBG_LVL_1,
457ff3b7 528 "mgmt_open_connection Failed for cid=%d\n",
756d29c8 529 beiscsi_ep->ep_cid);
1f92638f 530 beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
3cbb7a74
JK
531 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
532 nonemb_cmd.va, nonemb_cmd.dma);
1f92638f 533 return -EAGAIN;
756d29c8
JK
534 } else {
535 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
536 phba->ctrl.mcc_numtag[tag]);
537 }
538 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
539 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
540 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
541 if (status || extd_status) {
542 SE_DEBUG(DBG_LVL_1, "mgmt_open_connection Failed"
fa95d206 543 " status = %d extd_status = %d\n",
756d29c8 544 status, extd_status);
3cbb7a74 545 beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
756d29c8 546 free_mcc_tag(&phba->ctrl, tag);
3cbb7a74
JK
547 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
548 nonemb_cmd.va, nonemb_cmd.dma);
fa95d206 549 goto free_ep;
756d29c8
JK
550 } else {
551 wrb = queue_get_wrb(mccq, wrb_num);
552 free_mcc_tag(&phba->ctrl, tag);
553
457ff3b7 554 ptcpcnct_out = embedded_payload(wrb);
756d29c8
JK
555 beiscsi_ep = ep->dd_data;
556 beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
557 beiscsi_ep->cid_vld = 1;
558 SE_DEBUG(DBG_LVL_8, "mgmt_open_connection Success\n");
559 }
3cbb7a74
JK
560 beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
561 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
562 nonemb_cmd.va, nonemb_cmd.dma);
756d29c8 563 return 0;
6733b39a 564
fa95d206
MC
565free_ep:
566 beiscsi_free_ep(beiscsi_ep);
d3ad2bb3 567 return -EBUSY;
6733b39a
JK
568}
569
570/**
571 * beiscsi_ep_connect - Ask chip to create TCP Conn
572 * @scsi_host: Pointer to scsi_host structure
573 * @dst_addr: The IP address of Target
574 * @non_blocking: blocking or non-blocking call
575 *
576 * This routines first asks chip to create a connection and then allocates an EP
577 */
578struct iscsi_endpoint *
579beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
580 int non_blocking)
581{
582 struct beiscsi_hba *phba;
583 struct beiscsi_endpoint *beiscsi_ep;
584 struct iscsi_endpoint *ep;
585 int ret;
586
457ff3b7 587 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_connect\n");
6733b39a
JK
588 if (shost)
589 phba = iscsi_host_priv(shost);
590 else {
591 ret = -ENXIO;
457ff3b7 592 SE_DEBUG(DBG_LVL_1, "shost is NULL\n");
6733b39a
JK
593 return ERR_PTR(ret);
594 }
bfead3b2 595
5dc1c416 596 if (phba->state != BE_ADAPTER_UP) {
bfead3b2 597 ret = -EBUSY;
457ff3b7 598 SE_DEBUG(DBG_LVL_1, "The Adapter state is Not UP\n");
bfead3b2
JK
599 return ERR_PTR(ret);
600 }
601
6733b39a
JK
602 ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
603 if (!ep) {
604 ret = -ENOMEM;
605 return ERR_PTR(ret);
606 }
607
608 beiscsi_ep = ep->dd_data;
609 beiscsi_ep->phba = phba;
c2462288 610 beiscsi_ep->openiscsi_ep = ep;
f5ed7bd4
JK
611 ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
612 if (ret) {
457ff3b7 613 SE_DEBUG(DBG_LVL_1, "Failed in beiscsi_open_conn\n");
6733b39a
JK
614 goto free_ep;
615 }
616
617 return ep;
618
619free_ep:
fa95d206 620 iscsi_destroy_endpoint(ep);
6733b39a
JK
621 return ERR_PTR(ret);
622}
623
624/**
625 * beiscsi_ep_poll - Poll to see if connection is established
626 * @ep: endpoint to be used
627 * @timeout_ms: timeout specified in millisecs
628 *
629 * Poll to see if TCP connection established
630 */
631int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
632{
633 struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
634
635 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_poll\n");
636 if (beiscsi_ep->cid_vld == 1)
637 return 1;
638 else
639 return 0;
640}
641
642/**
643 * beiscsi_close_conn - Upload the connection
644 * @ep: The iscsi endpoint
645 * @flag: The type of connection closure
646 */
c2462288 647static int beiscsi_close_conn(struct beiscsi_endpoint *beiscsi_ep, int flag)
6733b39a
JK
648{
649 int ret = 0;
756d29c8 650 unsigned int tag;
6733b39a
JK
651 struct beiscsi_hba *phba = beiscsi_ep->phba;
652
756d29c8
JK
653 tag = mgmt_upload_connection(phba, beiscsi_ep->ep_cid, flag);
654 if (!tag) {
457ff3b7 655 SE_DEBUG(DBG_LVL_8, "upload failed for cid 0x%x\n",
6733b39a 656 beiscsi_ep->ep_cid);
d3ad2bb3 657 ret = -EAGAIN;
756d29c8
JK
658 } else {
659 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
660 phba->ctrl.mcc_numtag[tag]);
661 free_mcc_tag(&phba->ctrl, tag);
6733b39a 662 }
6733b39a
JK
663 return ret;
664}
665
6733b39a
JK
666/**
667 * beiscsi_unbind_conn_to_cid - Unbind the beiscsi_conn from phba conn table
668 * @phba: The phba instance
669 * @cid: The cid to free
670 */
671static int beiscsi_unbind_conn_to_cid(struct beiscsi_hba *phba,
672 unsigned int cid)
673{
674 if (phba->conn_table[cid])
675 phba->conn_table[cid] = NULL;
676 else {
457ff3b7 677 SE_DEBUG(DBG_LVL_8, "Connection table Not occupied.\n");
6733b39a
JK
678 return -EINVAL;
679 }
680 return 0;
681}
682
683/**
fa95d206
MC
684 * beiscsi_ep_disconnect - Tears down the TCP connection
685 * @ep: endpoint to be used
686 *
687 * Tears down the TCP connection
6733b39a 688 */
fa95d206 689void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
6733b39a 690{
fa95d206 691 struct beiscsi_conn *beiscsi_conn;
6733b39a 692 struct beiscsi_endpoint *beiscsi_ep;
fa95d206 693 struct beiscsi_hba *phba;
756d29c8 694 unsigned int tag;
6733b39a
JK
695 unsigned short savecfg_flag = CMD_ISCSI_SESSION_SAVE_CFG_ON_FLASH;
696
fa95d206
MC
697 beiscsi_ep = ep->dd_data;
698 phba = beiscsi_ep->phba;
699 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect for ep_cid = %d\n",
700 beiscsi_ep->ep_cid);
701
702 if (!beiscsi_ep->conn) {
703 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect, no "
704 "beiscsi_ep\n");
6733b39a
JK
705 return;
706 }
fa95d206
MC
707 beiscsi_conn = beiscsi_ep->conn;
708 iscsi_suspend_queue(beiscsi_conn->conn);
709
710 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect ep_cid = %d\n",
711 beiscsi_ep->ep_cid);
712
756d29c8 713 tag = mgmt_invalidate_connection(phba, beiscsi_ep,
6733b39a
JK
714 beiscsi_ep->ep_cid, 1,
715 savecfg_flag);
756d29c8 716 if (!tag) {
6733b39a 717 SE_DEBUG(DBG_LVL_1,
457ff3b7 718 "mgmt_invalidate_connection Failed for cid=%d\n",
90a289e8 719 beiscsi_ep->ep_cid);
756d29c8
JK
720 } else {
721 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
722 phba->ctrl.mcc_numtag[tag]);
723 free_mcc_tag(&phba->ctrl, tag);
6733b39a 724 }
fa95d206 725
c2462288
JK
726 beiscsi_close_conn(beiscsi_ep, CONNECTION_UPLOAD_GRACEFUL);
727 beiscsi_free_ep(beiscsi_ep);
6733b39a 728 beiscsi_unbind_conn_to_cid(phba, beiscsi_ep->ep_cid);
fa95d206 729 iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
6733b39a 730}