blob: 454027ccbf16fa47d004e55a9ef949f55e53b827 [file] [log] [blame]
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301/**
Jayamohan Kallickald2eeb1a2010-01-23 05:35:15 +05302 * Copyright (C) 2005 - 2010 ServerEngines
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303 * 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;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053047 struct beiscsi_hba *phba;
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +053048 struct iscsi_session *sess;
49 struct beiscsi_session *beiscsi_sess;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053050 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053051
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
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +053070 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);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053075 if (!cls_session)
76 return NULL;
77 sess = cls_session->dd_data;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +053078 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;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053085
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053086 return cls_session;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +053087destroy_sess:
88 iscsi_session_teardown(cls_session);
89 return NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053090}
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{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530101 struct iscsi_session *sess = cls_session->dd_data;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +0530102 struct beiscsi_session *beiscsi_sess = sess->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530103
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530104 SE_DEBUG(DBG_LVL_8, "In beiscsi_session_destroy\n");
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +0530105 pci_pool_destroy(beiscsi_sess->bhs_pool);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530106 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;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +0530122 struct iscsi_session *sess;
123 struct beiscsi_session *beiscsi_sess;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530124
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;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +0530139 sess = cls_session->dd_data;
140 beiscsi_sess = sess->dd_data;
141 beiscsi_conn->beiscsi_sess = beiscsi_sess;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530142 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 {
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 */
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,
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_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
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530228 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_param, param= %d\n", param);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530229 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");
233 return -1;
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
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530259 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_set_param, param= %d\n", param);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530260 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:
Jayamohan Kallickal230dceb2010-01-23 05:36:10 +0530277 if (session->max_burst > 262144)
278 session->max_burst = 262144;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530279 break;
280 default:
281 return 0;
282 }
283
284 return 0;
285}
286
287/**
288 * beiscsi_get_host_param - get the iscsi parameter
289 * @shost: pointer to scsi_host structure
290 * @param: parameter type identifier
291 * @buf: buffer pointer
292 *
293 * returns host parameter
294 */
295int beiscsi_get_host_param(struct Scsi_Host *shost,
296 enum iscsi_host_param param, char *buf)
297{
298 struct beiscsi_hba *phba = (struct beiscsi_hba *)iscsi_host_priv(shost);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530299 struct be_cmd_resp_get_mac_addr *resp;
300 struct be_mcc_wrb *wrb;
301 unsigned int tag, wrb_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530302 int len = 0;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530303 unsigned short status, extd_status;
304 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530305
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530306 SE_DEBUG(DBG_LVL_8, "In beiscsi_get_host_param, param= %d\n", param);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530307 switch (param) {
308 case ISCSI_HOST_PARAM_HWADDRESS:
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530309 tag = be_cmd_get_mac_addr(phba);
310 if (!tag) {
311 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed \n");
312 return -1;
313 } else
314 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
315 phba->ctrl.mcc_numtag[tag]);
316
317 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
318 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
319 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
320 if (status || extd_status) {
321 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed"
322 " status = %d extd_status = %d \n",
323 status, extd_status);
324 free_mcc_tag(&phba->ctrl, tag);
325 return -1;
326 } else {
327 wrb = queue_get_wrb(mccq, wrb_num);
328 free_mcc_tag(&phba->ctrl, tag);
329 resp = embedded_payload(wrb);
330 memcpy(phba->mac_address, resp->mac_address, ETH_ALEN);
331 len = sysfs_format_mac(buf, phba->mac_address,
332 ETH_ALEN);
333 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530334 break;
335 default:
336 return iscsi_host_get_param(shost, param, buf);
337 }
338 return len;
339}
340
341/**
342 * beiscsi_conn_get_stats - get the iscsi stats
343 * @cls_conn: pointer to iscsi cls conn
344 * @stats: pointer to iscsi_stats structure
345 *
346 * returns iscsi stats
347 */
348void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
349 struct iscsi_stats *stats)
350{
351 struct iscsi_conn *conn = cls_conn->dd_data;
352
353 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_stats\n");
354 stats->txdata_octets = conn->txdata_octets;
355 stats->rxdata_octets = conn->rxdata_octets;
356 stats->dataout_pdus = conn->dataout_pdus_cnt;
357 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
358 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
359 stats->datain_pdus = conn->datain_pdus_cnt;
360 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
361 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
362 stats->r2t_pdus = conn->r2t_pdus_cnt;
363 stats->digest_err = 0;
364 stats->timeout_err = 0;
365 stats->custom_length = 0;
366 strcpy(stats->custom[0].desc, "eh_abort_cnt");
367 stats->custom[0].value = conn->eh_abort_cnt;
368}
369
370/**
371 * beiscsi_set_params_for_offld - get the parameters for offload
372 * @beiscsi_conn: pointer to beiscsi_conn
373 * @params: pointer to offload_params structure
374 */
375static void beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
376 struct beiscsi_offload_params *params)
377{
378 struct iscsi_conn *conn = beiscsi_conn->conn;
379 struct iscsi_session *session = conn->session;
380
381 AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
382 params, session->max_burst);
383 AMAP_SET_BITS(struct amap_beiscsi_offload_params,
384 max_send_data_segment_length, params,
385 conn->max_xmit_dlength);
386 AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
387 params, session->first_burst);
388 AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
389 session->erl);
390 AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
391 conn->datadgst_en);
392 AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
393 conn->hdrdgst_en);
394 AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
395 session->initial_r2t_en);
396 AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
397 session->imm_data_en);
398 AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
399 (conn->exp_statsn - 1));
400}
401
402/**
403 * beiscsi_conn_start - offload of session to chip
404 * @cls_conn: pointer to beiscsi_conn
405 */
406int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
407{
408 struct iscsi_conn *conn = cls_conn->dd_data;
409 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
410 struct beiscsi_endpoint *beiscsi_ep;
411 struct beiscsi_offload_params params;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530412
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530413 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_start\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530414 memset(&params, 0, sizeof(struct beiscsi_offload_params));
415 beiscsi_ep = beiscsi_conn->ep;
416 if (!beiscsi_ep)
417 SE_DEBUG(DBG_LVL_1, "In beiscsi_conn_start , no beiscsi_ep\n");
418
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530419 beiscsi_conn->login_in_progress = 0;
420 beiscsi_set_params_for_offld(beiscsi_conn, &params);
421 beiscsi_offload_connection(beiscsi_conn, &params);
422 iscsi_conn_start(cls_conn);
423 return 0;
424}
425
426/**
427 * beiscsi_get_cid - Allocate a cid
428 * @phba: The phba instance
429 */
430static int beiscsi_get_cid(struct beiscsi_hba *phba)
431{
432 unsigned short cid = 0xFFFF;
433
434 if (!phba->avlbl_cids)
435 return cid;
436
437 cid = phba->cid_array[phba->cid_alloc++];
438 if (phba->cid_alloc == phba->params.cxns_per_ctrl)
439 phba->cid_alloc = 0;
440 phba->avlbl_cids--;
441 return cid;
442}
443
444/**
Mike Christiefa95d202010-06-09 03:30:08 -0500445 * beiscsi_put_cid - Free the cid
446 * @phba: The phba for which the cid is being freed
447 * @cid: The cid to free
448 */
449static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
450{
451 phba->avlbl_cids++;
452 phba->cid_array[phba->cid_free++] = cid;
453 if (phba->cid_free == phba->params.cxns_per_ctrl)
454 phba->cid_free = 0;
455}
456
457/**
458 * beiscsi_free_ep - free endpoint
459 * @ep: pointer to iscsi endpoint structure
460 */
461static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
462{
463 struct beiscsi_hba *phba = beiscsi_ep->phba;
464
465 beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
466 beiscsi_ep->phba = NULL;
467}
468
469/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530470 * beiscsi_open_conn - Ask FW to open a TCP connection
471 * @ep: endpoint to be used
472 * @src_addr: The source IP address
473 * @dst_addr: The Destination IP address
474 *
475 * Asks the FW to open a TCP connection
476 */
477static int beiscsi_open_conn(struct iscsi_endpoint *ep,
478 struct sockaddr *src_addr,
479 struct sockaddr *dst_addr, int non_blocking)
480{
481 struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
482 struct beiscsi_hba *phba = beiscsi_ep->phba;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530483 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
484 struct be_mcc_wrb *wrb;
485 struct tcp_connect_and_offload_out *ptcpcnct_out;
486 unsigned short status, extd_status;
487 unsigned int tag, wrb_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530488 int ret = -1;
489
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530490 SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530491 beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
492 if (beiscsi_ep->ep_cid == 0xFFFF) {
493 SE_DEBUG(DBG_LVL_1, "No free cid available\n");
494 return ret;
495 }
496 SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn, ep_cid=%d ",
497 beiscsi_ep->ep_cid);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530498 phba->ep_array[beiscsi_ep->ep_cid -
499 phba->fw_config.iscsi_cid_start] = ep;
500 if (beiscsi_ep->ep_cid > (phba->fw_config.iscsi_cid_start +
501 phba->params.cxns_per_ctrl * 2)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530502 SE_DEBUG(DBG_LVL_1, "Failed in allocate iscsi cid\n");
Mike Christiefa95d202010-06-09 03:30:08 -0500503 goto free_ep;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530504 }
505
506 beiscsi_ep->cid_vld = 0;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530507 tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep);
508 if (!tag) {
509 SE_DEBUG(DBG_LVL_1,
Jayamohan Kallickal90a289e2010-02-20 08:04:28 +0530510 "mgmt_open_connection Failed for cid=%d \n",
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530511 beiscsi_ep->ep_cid);
512 } else {
513 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
514 phba->ctrl.mcc_numtag[tag]);
515 }
516 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
517 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
518 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
519 if (status || extd_status) {
520 SE_DEBUG(DBG_LVL_1, "mgmt_open_connection Failed"
Mike Christiefa95d202010-06-09 03:30:08 -0500521 " status = %d extd_status = %d\n",
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530522 status, extd_status);
523 free_mcc_tag(&phba->ctrl, tag);
Mike Christiefa95d202010-06-09 03:30:08 -0500524 goto free_ep;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530525 } else {
526 wrb = queue_get_wrb(mccq, wrb_num);
527 free_mcc_tag(&phba->ctrl, tag);
528
529 ptcpcnct_out = embedded_payload(wrb);
530 beiscsi_ep = ep->dd_data;
531 beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
532 beiscsi_ep->cid_vld = 1;
533 SE_DEBUG(DBG_LVL_8, "mgmt_open_connection Success\n");
534 }
535 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530536
Mike Christiefa95d202010-06-09 03:30:08 -0500537free_ep:
538 beiscsi_free_ep(beiscsi_ep);
539 return -1;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530540}
541
542/**
543 * beiscsi_ep_connect - Ask chip to create TCP Conn
544 * @scsi_host: Pointer to scsi_host structure
545 * @dst_addr: The IP address of Target
546 * @non_blocking: blocking or non-blocking call
547 *
548 * This routines first asks chip to create a connection and then allocates an EP
549 */
550struct iscsi_endpoint *
551beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
552 int non_blocking)
553{
554 struct beiscsi_hba *phba;
555 struct beiscsi_endpoint *beiscsi_ep;
556 struct iscsi_endpoint *ep;
557 int ret;
558
559 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_connect \n");
560 if (shost)
561 phba = iscsi_host_priv(shost);
562 else {
563 ret = -ENXIO;
564 SE_DEBUG(DBG_LVL_1, "shost is NULL \n");
565 return ERR_PTR(ret);
566 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530567
Jayamohan Kallickal5dc1c412010-01-23 05:36:52 +0530568 if (phba->state != BE_ADAPTER_UP) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530569 ret = -EBUSY;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +0530570 SE_DEBUG(DBG_LVL_1, "The Adapter state is Not UP \n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530571 return ERR_PTR(ret);
572 }
573
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530574 ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
575 if (!ep) {
576 ret = -ENOMEM;
577 return ERR_PTR(ret);
578 }
579
580 beiscsi_ep = ep->dd_data;
581 beiscsi_ep->phba = phba;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +0530582 beiscsi_ep->openiscsi_ep = ep;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530583 if (beiscsi_open_conn(ep, NULL, dst_addr, non_blocking)) {
Jayamohan Kallickalc2462282010-01-05 05:05:34 +0530584 SE_DEBUG(DBG_LVL_1, "Failed in beiscsi_open_conn \n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530585 ret = -ENOMEM;
586 goto free_ep;
587 }
588
589 return ep;
590
591free_ep:
Mike Christiefa95d202010-06-09 03:30:08 -0500592 iscsi_destroy_endpoint(ep);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530593 return ERR_PTR(ret);
594}
595
596/**
597 * beiscsi_ep_poll - Poll to see if connection is established
598 * @ep: endpoint to be used
599 * @timeout_ms: timeout specified in millisecs
600 *
601 * Poll to see if TCP connection established
602 */
603int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
604{
605 struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
606
607 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_poll\n");
608 if (beiscsi_ep->cid_vld == 1)
609 return 1;
610 else
611 return 0;
612}
613
614/**
615 * beiscsi_close_conn - Upload the connection
616 * @ep: The iscsi endpoint
617 * @flag: The type of connection closure
618 */
Jayamohan Kallickalc2462282010-01-05 05:05:34 +0530619static int beiscsi_close_conn(struct beiscsi_endpoint *beiscsi_ep, int flag)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530620{
621 int ret = 0;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530622 unsigned int tag;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530623 struct beiscsi_hba *phba = beiscsi_ep->phba;
624
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530625 tag = mgmt_upload_connection(phba, beiscsi_ep->ep_cid, flag);
626 if (!tag) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530627 SE_DEBUG(DBG_LVL_8, "upload failed for cid 0x%x",
628 beiscsi_ep->ep_cid);
629 ret = -1;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530630 } else {
631 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
632 phba->ctrl.mcc_numtag[tag]);
633 free_mcc_tag(&phba->ctrl, tag);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530634 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530635 return ret;
636}
637
638/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530639 * beiscsi_unbind_conn_to_cid - Unbind the beiscsi_conn from phba conn table
640 * @phba: The phba instance
641 * @cid: The cid to free
642 */
643static int beiscsi_unbind_conn_to_cid(struct beiscsi_hba *phba,
644 unsigned int cid)
645{
646 if (phba->conn_table[cid])
647 phba->conn_table[cid] = NULL;
648 else {
649 SE_DEBUG(DBG_LVL_8, "Connection table Not occupied. \n");
650 return -EINVAL;
651 }
652 return 0;
653}
654
655/**
Mike Christiefa95d202010-06-09 03:30:08 -0500656 * beiscsi_ep_disconnect - Tears down the TCP connection
657 * @ep: endpoint to be used
658 *
659 * Tears down the TCP connection
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530660 */
Mike Christiefa95d202010-06-09 03:30:08 -0500661void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530662{
Mike Christiefa95d202010-06-09 03:30:08 -0500663 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530664 struct beiscsi_endpoint *beiscsi_ep;
Mike Christiefa95d202010-06-09 03:30:08 -0500665 struct beiscsi_hba *phba;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530666 unsigned int tag;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530667 unsigned short savecfg_flag = CMD_ISCSI_SESSION_SAVE_CFG_ON_FLASH;
668
Mike Christiefa95d202010-06-09 03:30:08 -0500669 beiscsi_ep = ep->dd_data;
670 phba = beiscsi_ep->phba;
671 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect for ep_cid = %d\n",
672 beiscsi_ep->ep_cid);
673
674 if (!beiscsi_ep->conn) {
675 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect, no "
676 "beiscsi_ep\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530677 return;
678 }
Mike Christiefa95d202010-06-09 03:30:08 -0500679 beiscsi_conn = beiscsi_ep->conn;
680 iscsi_suspend_queue(beiscsi_conn->conn);
681
682 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect ep_cid = %d\n",
683 beiscsi_ep->ep_cid);
684
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530685 tag = mgmt_invalidate_connection(phba, beiscsi_ep,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530686 beiscsi_ep->ep_cid, 1,
687 savecfg_flag);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530688 if (!tag) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530689 SE_DEBUG(DBG_LVL_1,
690 "mgmt_invalidate_connection Failed for cid=%d \n",
Jayamohan Kallickal90a289e2010-02-20 08:04:28 +0530691 beiscsi_ep->ep_cid);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530692 } else {
693 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
694 phba->ctrl.mcc_numtag[tag]);
695 free_mcc_tag(&phba->ctrl, tag);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530696 }
Mike Christiefa95d202010-06-09 03:30:08 -0500697
Jayamohan Kallickalc2462282010-01-05 05:05:34 +0530698 beiscsi_close_conn(beiscsi_ep, CONNECTION_UPLOAD_GRACEFUL);
699 beiscsi_free_ep(beiscsi_ep);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530700 beiscsi_unbind_conn_to_cid(phba, beiscsi_ep->ep_cid);
Mike Christiefa95d202010-06-09 03:30:08 -0500701 iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530702}