blob: 87b7ae1ef4881a83cedbb478784581ae41e10457 [file] [log] [blame]
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301/**
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -07002 * Copyright (C) 2005 - 2011 Emulex
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 *
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070010 * Written by: Jayamohan Kallickal (jayamohan.kallickal@emulex.com)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053011 *
12 * Contact Information:
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070013 * linux-drivers@emulex.com
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053014 *
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070015 * Emulex
16 * 3333 Susan Street
17 * Costa Mesa, CA 92626
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053018 */
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.h>
27
28#include "be_iscsi.h"
29
30extern struct iscsi_transport beiscsi_iscsi_transport;
31
32/**
33 * beiscsi_session_create - creates a new iscsi session
34 * @cmds_max: max commands supported
35 * @qdepth: max queue depth supported
36 * @initial_cmdsn: initial iscsi CMDSN
37 */
38struct iscsi_cls_session *beiscsi_session_create(struct iscsi_endpoint *ep,
39 u16 cmds_max,
40 u16 qdepth,
41 u32 initial_cmdsn)
42{
43 struct Scsi_Host *shost;
44 struct beiscsi_endpoint *beiscsi_ep;
45 struct iscsi_cls_session *cls_session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053046 struct beiscsi_hba *phba;
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +053047 struct iscsi_session *sess;
48 struct beiscsi_session *beiscsi_sess;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053049 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053050
51 SE_DEBUG(DBG_LVL_8, "In beiscsi_session_create\n");
52
53 if (!ep) {
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +053054 SE_DEBUG(DBG_LVL_1, "beiscsi_session_create: invalid ep\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053055 return NULL;
56 }
57 beiscsi_ep = ep->dd_data;
58 phba = beiscsi_ep->phba;
59 shost = phba->shost;
60 if (cmds_max > beiscsi_ep->phba->params.wrbs_per_cxn) {
61 shost_printk(KERN_ERR, shost, "Cannot handle %d cmds."
62 "Max cmds per session supported is %d. Using %d. "
63 "\n", cmds_max,
64 beiscsi_ep->phba->params.wrbs_per_cxn,
65 beiscsi_ep->phba->params.wrbs_per_cxn);
66 cmds_max = beiscsi_ep->phba->params.wrbs_per_cxn;
67 }
68
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +053069 cls_session = iscsi_session_setup(&beiscsi_iscsi_transport,
70 shost, cmds_max,
71 sizeof(*beiscsi_sess),
72 sizeof(*io_task),
73 initial_cmdsn, ISCSI_MAX_TARGET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053074 if (!cls_session)
75 return NULL;
76 sess = cls_session->dd_data;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +053077 beiscsi_sess = sess->dd_data;
78 beiscsi_sess->bhs_pool = pci_pool_create("beiscsi_bhs_pool",
79 phba->pcidev,
80 sizeof(struct be_cmd_bhs),
81 64, 0);
82 if (!beiscsi_sess->bhs_pool)
83 goto destroy_sess;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053084
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053085 return cls_session;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +053086destroy_sess:
87 iscsi_session_teardown(cls_session);
88 return NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053089}
90
91/**
92 * beiscsi_session_destroy - destroys iscsi session
93 * @cls_session: pointer to iscsi cls session
94 *
95 * Destroys iSCSI session instance and releases
96 * resources allocated for it.
97 */
98void beiscsi_session_destroy(struct iscsi_cls_session *cls_session)
99{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530100 struct iscsi_session *sess = cls_session->dd_data;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +0530101 struct beiscsi_session *beiscsi_sess = sess->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530102
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530103 SE_DEBUG(DBG_LVL_8, "In beiscsi_session_destroy\n");
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +0530104 pci_pool_destroy(beiscsi_sess->bhs_pool);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530105 iscsi_session_teardown(cls_session);
106}
107
108/**
109 * beiscsi_conn_create - create an instance of iscsi connection
110 * @cls_session: ptr to iscsi_cls_session
111 * @cid: iscsi cid
112 */
113struct iscsi_cls_conn *
114beiscsi_conn_create(struct iscsi_cls_session *cls_session, u32 cid)
115{
116 struct beiscsi_hba *phba;
117 struct Scsi_Host *shost;
118 struct iscsi_cls_conn *cls_conn;
119 struct beiscsi_conn *beiscsi_conn;
120 struct iscsi_conn *conn;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +0530121 struct iscsi_session *sess;
122 struct beiscsi_session *beiscsi_sess;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530123
124 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_create ,cid"
125 "from iscsi layer=%d\n", cid);
126 shost = iscsi_session_to_shost(cls_session);
127 phba = iscsi_host_priv(shost);
128
129 cls_conn = iscsi_conn_setup(cls_session, sizeof(*beiscsi_conn), cid);
130 if (!cls_conn)
131 return NULL;
132
133 conn = cls_conn->dd_data;
134 beiscsi_conn = conn->dd_data;
135 beiscsi_conn->ep = NULL;
136 beiscsi_conn->phba = phba;
137 beiscsi_conn->conn = conn;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +0530138 sess = cls_session->dd_data;
139 beiscsi_sess = sess->dd_data;
140 beiscsi_conn->beiscsi_sess = beiscsi_sess;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530141 return cls_conn;
142}
143
144/**
145 * beiscsi_bindconn_cid - Bind the beiscsi_conn with phba connection table
146 * @beiscsi_conn: The pointer to beiscsi_conn structure
147 * @phba: The phba instance
148 * @cid: The cid to free
149 */
150static int beiscsi_bindconn_cid(struct beiscsi_hba *phba,
151 struct beiscsi_conn *beiscsi_conn,
152 unsigned int cid)
153{
154 if (phba->conn_table[cid]) {
155 SE_DEBUG(DBG_LVL_1,
156 "Connection table already occupied. Detected clash\n");
157 return -EINVAL;
158 } else {
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530159 SE_DEBUG(DBG_LVL_8, "phba->conn_table[%d]=%p(beiscsi_conn)\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530160 cid, beiscsi_conn);
161 phba->conn_table[cid] = beiscsi_conn;
162 }
163 return 0;
164}
165
166/**
167 * beiscsi_conn_bind - Binds iscsi session/connection with TCP connection
168 * @cls_session: pointer to iscsi cls session
169 * @cls_conn: pointer to iscsi cls conn
170 * @transport_fd: EP handle(64 bit)
171 *
172 * This function binds the TCP Conn with iSCSI Connection and Session.
173 */
174int beiscsi_conn_bind(struct iscsi_cls_session *cls_session,
175 struct iscsi_cls_conn *cls_conn,
176 u64 transport_fd, int is_leading)
177{
178 struct iscsi_conn *conn = cls_conn->dd_data;
179 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
180 struct Scsi_Host *shost =
181 (struct Scsi_Host *)iscsi_session_to_shost(cls_session);
182 struct beiscsi_hba *phba = (struct beiscsi_hba *)iscsi_host_priv(shost);
183 struct beiscsi_endpoint *beiscsi_ep;
184 struct iscsi_endpoint *ep;
185
186 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_bind\n");
187 ep = iscsi_lookup_endpoint(transport_fd);
188 if (!ep)
189 return -EINVAL;
190
191 beiscsi_ep = ep->dd_data;
192
193 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
194 return -EINVAL;
195
196 if (beiscsi_ep->phba != phba) {
197 SE_DEBUG(DBG_LVL_8,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530198 "beiscsi_ep->hba=%p not equal to phba=%p\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530199 beiscsi_ep->phba, phba);
200 return -EEXIST;
201 }
202
203 beiscsi_conn->beiscsi_conn_cid = beiscsi_ep->ep_cid;
204 beiscsi_conn->ep = beiscsi_ep;
205 beiscsi_ep->conn = beiscsi_conn;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530206 SE_DEBUG(DBG_LVL_8, "beiscsi_conn=%p conn=%p ep_cid=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530207 beiscsi_conn, conn, beiscsi_ep->ep_cid);
208 return beiscsi_bindconn_cid(phba, beiscsi_conn, beiscsi_ep->ep_cid);
209}
210
211/**
Mike Christiec7f7fd52011-02-16 15:04:41 -0600212 * beiscsi_ep_get_param - get the iscsi parameter
213 * @ep: pointer to iscsi ep
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530214 * @param: parameter type identifier
215 * @buf: buffer pointer
216 *
217 * returns iscsi parameter
218 */
Mike Christiec7f7fd52011-02-16 15:04:41 -0600219int beiscsi_ep_get_param(struct iscsi_endpoint *ep,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530220 enum iscsi_param param, char *buf)
221{
Mike Christiec7f7fd52011-02-16 15:04:41 -0600222 struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530223 int len = 0;
224
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530225 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_param, param= %d\n", param);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530226
227 switch (param) {
228 case ISCSI_PARAM_CONN_PORT:
229 len = sprintf(buf, "%hu\n", beiscsi_ep->dst_tcpport);
230 break;
231 case ISCSI_PARAM_CONN_ADDRESS:
232 if (beiscsi_ep->ip_type == BE2_IPV4)
233 len = sprintf(buf, "%pI4\n", &beiscsi_ep->dst_addr);
234 else
235 len = sprintf(buf, "%pI6\n", &beiscsi_ep->dst6_addr);
236 break;
237 default:
Mike Christiec7f7fd52011-02-16 15:04:41 -0600238 return -ENOSYS;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530239 }
240 return len;
241}
242
243int beiscsi_set_param(struct iscsi_cls_conn *cls_conn,
244 enum iscsi_param param, char *buf, int buflen)
245{
246 struct iscsi_conn *conn = cls_conn->dd_data;
247 struct iscsi_session *session = conn->session;
248 int ret;
249
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530250 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_set_param, param= %d\n", param);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530251 ret = iscsi_set_param(cls_conn, param, buf, buflen);
252 if (ret)
253 return ret;
254 /*
255 * If userspace tried to set the value to higher than we can
256 * support override here.
257 */
258 switch (param) {
259 case ISCSI_PARAM_FIRST_BURST:
260 if (session->first_burst > 8192)
261 session->first_burst = 8192;
262 break;
263 case ISCSI_PARAM_MAX_RECV_DLENGTH:
264 if (conn->max_recv_dlength > 65536)
265 conn->max_recv_dlength = 65536;
266 break;
267 case ISCSI_PARAM_MAX_BURST:
Jayamohan Kallickal230dceb2010-01-23 05:36:10 +0530268 if (session->max_burst > 262144)
269 session->max_burst = 262144;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530270 break;
Jayamohan Kallickal42f43c42010-07-22 04:26:45 +0530271 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
272 if ((conn->max_xmit_dlength > 65536) ||
273 (conn->max_xmit_dlength == 0))
274 conn->max_xmit_dlength = 65536;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530275 default:
276 return 0;
277 }
278
279 return 0;
280}
281
282/**
283 * beiscsi_get_host_param - get the iscsi parameter
284 * @shost: pointer to scsi_host structure
285 * @param: parameter type identifier
286 * @buf: buffer pointer
287 *
288 * returns host parameter
289 */
290int beiscsi_get_host_param(struct Scsi_Host *shost,
291 enum iscsi_host_param param, char *buf)
292{
293 struct beiscsi_hba *phba = (struct beiscsi_hba *)iscsi_host_priv(shost);
Jayamohan Kallickalb15d05b2010-08-12 23:36:06 +0530294 int status = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530295
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530296 SE_DEBUG(DBG_LVL_8, "In beiscsi_get_host_param, param= %d\n", param);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530297 switch (param) {
298 case ISCSI_HOST_PARAM_HWADDRESS:
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530299 status = beiscsi_get_macaddr(buf, phba);
300 if (status < 0) {
301 SE_DEBUG(DBG_LVL_1, "beiscsi_get_macaddr Failed\n");
302 return status;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530303 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530304 break;
305 default:
306 return iscsi_host_get_param(shost, param, buf);
307 }
Jayamohan Kallickalb15d05b2010-08-12 23:36:06 +0530308 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530309}
310
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530311int beiscsi_get_macaddr(char *buf, struct beiscsi_hba *phba)
312{
313 struct be_cmd_resp_get_mac_addr *resp;
314 struct be_mcc_wrb *wrb;
315 unsigned int tag, wrb_num;
316 unsigned short status, extd_status;
317 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
318 int rc;
319
320 if (phba->read_mac_address)
321 return sysfs_format_mac(buf, phba->mac_address,
322 ETH_ALEN);
323
324 tag = be_cmd_get_mac_addr(phba);
325 if (!tag) {
326 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed\n");
327 return -EBUSY;
328 } else
329 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
330 phba->ctrl.mcc_numtag[tag]);
331
332 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
333 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
334 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
335 if (status || extd_status) {
336 SE_DEBUG(DBG_LVL_1, "Failed to get be_cmd_get_mac_addr"
337 " status = %d extd_status = %d\n",
338 status, extd_status);
339 free_mcc_tag(&phba->ctrl, tag);
340 return -EAGAIN;
341 }
342 wrb = queue_get_wrb(mccq, wrb_num);
343 free_mcc_tag(&phba->ctrl, tag);
344 resp = embedded_payload(wrb);
345 memcpy(phba->mac_address, resp->mac_address, ETH_ALEN);
346 rc = sysfs_format_mac(buf, phba->mac_address,
347 ETH_ALEN);
348 phba->read_mac_address = 1;
349 return rc;
350}
351
352
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530353/**
354 * beiscsi_conn_get_stats - get the iscsi stats
355 * @cls_conn: pointer to iscsi cls conn
356 * @stats: pointer to iscsi_stats structure
357 *
358 * returns iscsi stats
359 */
360void beiscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn,
361 struct iscsi_stats *stats)
362{
363 struct iscsi_conn *conn = cls_conn->dd_data;
364
365 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_get_stats\n");
366 stats->txdata_octets = conn->txdata_octets;
367 stats->rxdata_octets = conn->rxdata_octets;
368 stats->dataout_pdus = conn->dataout_pdus_cnt;
369 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
370 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
371 stats->datain_pdus = conn->datain_pdus_cnt;
372 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
373 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
374 stats->r2t_pdus = conn->r2t_pdus_cnt;
375 stats->digest_err = 0;
376 stats->timeout_err = 0;
377 stats->custom_length = 0;
378 strcpy(stats->custom[0].desc, "eh_abort_cnt");
379 stats->custom[0].value = conn->eh_abort_cnt;
380}
381
382/**
383 * beiscsi_set_params_for_offld - get the parameters for offload
384 * @beiscsi_conn: pointer to beiscsi_conn
385 * @params: pointer to offload_params structure
386 */
387static void beiscsi_set_params_for_offld(struct beiscsi_conn *beiscsi_conn,
388 struct beiscsi_offload_params *params)
389{
390 struct iscsi_conn *conn = beiscsi_conn->conn;
391 struct iscsi_session *session = conn->session;
392
393 AMAP_SET_BITS(struct amap_beiscsi_offload_params, max_burst_length,
394 params, session->max_burst);
395 AMAP_SET_BITS(struct amap_beiscsi_offload_params,
396 max_send_data_segment_length, params,
397 conn->max_xmit_dlength);
398 AMAP_SET_BITS(struct amap_beiscsi_offload_params, first_burst_length,
399 params, session->first_burst);
400 AMAP_SET_BITS(struct amap_beiscsi_offload_params, erl, params,
401 session->erl);
402 AMAP_SET_BITS(struct amap_beiscsi_offload_params, dde, params,
403 conn->datadgst_en);
404 AMAP_SET_BITS(struct amap_beiscsi_offload_params, hde, params,
405 conn->hdrdgst_en);
406 AMAP_SET_BITS(struct amap_beiscsi_offload_params, ir2t, params,
407 session->initial_r2t_en);
408 AMAP_SET_BITS(struct amap_beiscsi_offload_params, imd, params,
409 session->imm_data_en);
410 AMAP_SET_BITS(struct amap_beiscsi_offload_params, exp_statsn, params,
411 (conn->exp_statsn - 1));
412}
413
414/**
415 * beiscsi_conn_start - offload of session to chip
416 * @cls_conn: pointer to beiscsi_conn
417 */
418int beiscsi_conn_start(struct iscsi_cls_conn *cls_conn)
419{
420 struct iscsi_conn *conn = cls_conn->dd_data;
421 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
422 struct beiscsi_endpoint *beiscsi_ep;
423 struct beiscsi_offload_params params;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530424
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530425 SE_DEBUG(DBG_LVL_8, "In beiscsi_conn_start\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530426 memset(&params, 0, sizeof(struct beiscsi_offload_params));
427 beiscsi_ep = beiscsi_conn->ep;
428 if (!beiscsi_ep)
429 SE_DEBUG(DBG_LVL_1, "In beiscsi_conn_start , no beiscsi_ep\n");
430
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530431 beiscsi_conn->login_in_progress = 0;
432 beiscsi_set_params_for_offld(beiscsi_conn, &params);
433 beiscsi_offload_connection(beiscsi_conn, &params);
434 iscsi_conn_start(cls_conn);
435 return 0;
436}
437
438/**
439 * beiscsi_get_cid - Allocate a cid
440 * @phba: The phba instance
441 */
442static int beiscsi_get_cid(struct beiscsi_hba *phba)
443{
444 unsigned short cid = 0xFFFF;
445
446 if (!phba->avlbl_cids)
447 return cid;
448
449 cid = phba->cid_array[phba->cid_alloc++];
450 if (phba->cid_alloc == phba->params.cxns_per_ctrl)
451 phba->cid_alloc = 0;
452 phba->avlbl_cids--;
453 return cid;
454}
455
456/**
Mike Christiefa95d202010-06-09 03:30:08 -0500457 * beiscsi_put_cid - Free the cid
458 * @phba: The phba for which the cid is being freed
459 * @cid: The cid to free
460 */
461static void beiscsi_put_cid(struct beiscsi_hba *phba, unsigned short cid)
462{
463 phba->avlbl_cids++;
464 phba->cid_array[phba->cid_free++] = cid;
465 if (phba->cid_free == phba->params.cxns_per_ctrl)
466 phba->cid_free = 0;
467}
468
469/**
470 * beiscsi_free_ep - free endpoint
471 * @ep: pointer to iscsi endpoint structure
472 */
473static void beiscsi_free_ep(struct beiscsi_endpoint *beiscsi_ep)
474{
475 struct beiscsi_hba *phba = beiscsi_ep->phba;
476
477 beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
478 beiscsi_ep->phba = NULL;
479}
480
481/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530482 * beiscsi_open_conn - Ask FW to open a TCP connection
483 * @ep: endpoint to be used
484 * @src_addr: The source IP address
485 * @dst_addr: The Destination IP address
486 *
487 * Asks the FW to open a TCP connection
488 */
489static int beiscsi_open_conn(struct iscsi_endpoint *ep,
490 struct sockaddr *src_addr,
491 struct sockaddr *dst_addr, int non_blocking)
492{
493 struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
494 struct beiscsi_hba *phba = beiscsi_ep->phba;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530495 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
496 struct be_mcc_wrb *wrb;
497 struct tcp_connect_and_offload_out *ptcpcnct_out;
498 unsigned short status, extd_status;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530499 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530500 unsigned int tag, wrb_num;
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +0530501 int ret = -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530502
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530503 SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530504 beiscsi_ep->ep_cid = beiscsi_get_cid(phba);
505 if (beiscsi_ep->ep_cid == 0xFFFF) {
506 SE_DEBUG(DBG_LVL_1, "No free cid available\n");
507 return ret;
508 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530509 SE_DEBUG(DBG_LVL_8, "In beiscsi_open_conn, ep_cid=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530510 beiscsi_ep->ep_cid);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530511 phba->ep_array[beiscsi_ep->ep_cid -
512 phba->fw_config.iscsi_cid_start] = ep;
513 if (beiscsi_ep->ep_cid > (phba->fw_config.iscsi_cid_start +
514 phba->params.cxns_per_ctrl * 2)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530515 SE_DEBUG(DBG_LVL_1, "Failed in allocate iscsi cid\n");
Mike Christiefa95d202010-06-09 03:30:08 -0500516 goto free_ep;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530517 }
518
519 beiscsi_ep->cid_vld = 0;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530520 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
521 sizeof(struct tcp_connect_and_offload_in),
522 &nonemb_cmd.dma);
523 if (nonemb_cmd.va == NULL) {
524 SE_DEBUG(DBG_LVL_1,
525 "Failed to allocate memory for mgmt_open_connection"
526 "\n");
527 beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
528 return -ENOMEM;
529 }
530 nonemb_cmd.size = sizeof(struct tcp_connect_and_offload_in);
531 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
532 tag = mgmt_open_connection(phba, dst_addr, beiscsi_ep, &nonemb_cmd);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530533 if (!tag) {
534 SE_DEBUG(DBG_LVL_1,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530535 "mgmt_open_connection Failed for cid=%d\n",
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530536 beiscsi_ep->ep_cid);
Jayamohan Kallickal1f926382010-07-22 04:22:27 +0530537 beiscsi_put_cid(phba, beiscsi_ep->ep_cid);
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530538 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
539 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal1f926382010-07-22 04:22:27 +0530540 return -EAGAIN;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530541 } else {
542 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
543 phba->ctrl.mcc_numtag[tag]);
544 }
545 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
546 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
547 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
548 if (status || extd_status) {
549 SE_DEBUG(DBG_LVL_1, "mgmt_open_connection Failed"
Mike Christiefa95d202010-06-09 03:30:08 -0500550 " status = %d extd_status = %d\n",
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530551 status, extd_status);
552 free_mcc_tag(&phba->ctrl, tag);
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530553 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
554 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christiefa95d202010-06-09 03:30:08 -0500555 goto free_ep;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530556 } else {
557 wrb = queue_get_wrb(mccq, wrb_num);
558 free_mcc_tag(&phba->ctrl, tag);
559
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530560 ptcpcnct_out = embedded_payload(wrb);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530561 beiscsi_ep = ep->dd_data;
562 beiscsi_ep->fw_handle = ptcpcnct_out->connection_handle;
563 beiscsi_ep->cid_vld = 1;
564 SE_DEBUG(DBG_LVL_8, "mgmt_open_connection Success\n");
565 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530566 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
567 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530568 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530569
Mike Christiefa95d202010-06-09 03:30:08 -0500570free_ep:
571 beiscsi_free_ep(beiscsi_ep);
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +0530572 return -EBUSY;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530573}
574
575/**
576 * beiscsi_ep_connect - Ask chip to create TCP Conn
577 * @scsi_host: Pointer to scsi_host structure
578 * @dst_addr: The IP address of Target
579 * @non_blocking: blocking or non-blocking call
580 *
581 * This routines first asks chip to create a connection and then allocates an EP
582 */
583struct iscsi_endpoint *
584beiscsi_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
585 int non_blocking)
586{
587 struct beiscsi_hba *phba;
588 struct beiscsi_endpoint *beiscsi_ep;
589 struct iscsi_endpoint *ep;
590 int ret;
591
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530592 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_connect\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530593 if (shost)
594 phba = iscsi_host_priv(shost);
595 else {
596 ret = -ENXIO;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530597 SE_DEBUG(DBG_LVL_1, "shost is NULL\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530598 return ERR_PTR(ret);
599 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530600
Jayamohan Kallickal5dc1c412010-01-23 05:36:52 +0530601 if (phba->state != BE_ADAPTER_UP) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530602 ret = -EBUSY;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530603 SE_DEBUG(DBG_LVL_1, "The Adapter state is Not UP\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530604 return ERR_PTR(ret);
605 }
606
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530607 ep = iscsi_create_endpoint(sizeof(struct beiscsi_endpoint));
608 if (!ep) {
609 ret = -ENOMEM;
610 return ERR_PTR(ret);
611 }
612
613 beiscsi_ep = ep->dd_data;
614 beiscsi_ep->phba = phba;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +0530615 beiscsi_ep->openiscsi_ep = ep;
Jayamohan Kallickalf5ed7bd2010-07-22 04:18:01 +0530616 ret = beiscsi_open_conn(ep, NULL, dst_addr, non_blocking);
617 if (ret) {
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530618 SE_DEBUG(DBG_LVL_1, "Failed in beiscsi_open_conn\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530619 goto free_ep;
620 }
621
622 return ep;
623
624free_ep:
Mike Christiefa95d202010-06-09 03:30:08 -0500625 iscsi_destroy_endpoint(ep);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530626 return ERR_PTR(ret);
627}
628
629/**
630 * beiscsi_ep_poll - Poll to see if connection is established
631 * @ep: endpoint to be used
632 * @timeout_ms: timeout specified in millisecs
633 *
634 * Poll to see if TCP connection established
635 */
636int beiscsi_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
637{
638 struct beiscsi_endpoint *beiscsi_ep = ep->dd_data;
639
640 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_poll\n");
641 if (beiscsi_ep->cid_vld == 1)
642 return 1;
643 else
644 return 0;
645}
646
647/**
648 * beiscsi_close_conn - Upload the connection
649 * @ep: The iscsi endpoint
650 * @flag: The type of connection closure
651 */
Jayamohan Kallickalc2462282010-01-05 05:05:34 +0530652static int beiscsi_close_conn(struct beiscsi_endpoint *beiscsi_ep, int flag)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530653{
654 int ret = 0;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530655 unsigned int tag;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530656 struct beiscsi_hba *phba = beiscsi_ep->phba;
657
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530658 tag = mgmt_upload_connection(phba, beiscsi_ep->ep_cid, flag);
659 if (!tag) {
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530660 SE_DEBUG(DBG_LVL_8, "upload failed for cid 0x%x\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530661 beiscsi_ep->ep_cid);
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +0530662 ret = -EAGAIN;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530663 } else {
664 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
665 phba->ctrl.mcc_numtag[tag]);
666 free_mcc_tag(&phba->ctrl, tag);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530667 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530668 return ret;
669}
670
671/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530672 * beiscsi_unbind_conn_to_cid - Unbind the beiscsi_conn from phba conn table
673 * @phba: The phba instance
674 * @cid: The cid to free
675 */
676static int beiscsi_unbind_conn_to_cid(struct beiscsi_hba *phba,
677 unsigned int cid)
678{
679 if (phba->conn_table[cid])
680 phba->conn_table[cid] = NULL;
681 else {
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530682 SE_DEBUG(DBG_LVL_8, "Connection table Not occupied.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530683 return -EINVAL;
684 }
685 return 0;
686}
687
688/**
Mike Christiefa95d202010-06-09 03:30:08 -0500689 * beiscsi_ep_disconnect - Tears down the TCP connection
690 * @ep: endpoint to be used
691 *
692 * Tears down the TCP connection
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530693 */
Mike Christiefa95d202010-06-09 03:30:08 -0500694void beiscsi_ep_disconnect(struct iscsi_endpoint *ep)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530695{
Mike Christiefa95d202010-06-09 03:30:08 -0500696 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530697 struct beiscsi_endpoint *beiscsi_ep;
Mike Christiefa95d202010-06-09 03:30:08 -0500698 struct beiscsi_hba *phba;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530699 unsigned int tag;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530700 unsigned short savecfg_flag = CMD_ISCSI_SESSION_SAVE_CFG_ON_FLASH;
701
Mike Christiefa95d202010-06-09 03:30:08 -0500702 beiscsi_ep = ep->dd_data;
703 phba = beiscsi_ep->phba;
704 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect for ep_cid = %d\n",
705 beiscsi_ep->ep_cid);
706
707 if (!beiscsi_ep->conn) {
708 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect, no "
709 "beiscsi_ep\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530710 return;
711 }
Mike Christiefa95d202010-06-09 03:30:08 -0500712 beiscsi_conn = beiscsi_ep->conn;
713 iscsi_suspend_queue(beiscsi_conn->conn);
714
715 SE_DEBUG(DBG_LVL_8, "In beiscsi_ep_disconnect ep_cid = %d\n",
716 beiscsi_ep->ep_cid);
717
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530718 tag = mgmt_invalidate_connection(phba, beiscsi_ep,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530719 beiscsi_ep->ep_cid, 1,
720 savecfg_flag);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530721 if (!tag) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530722 SE_DEBUG(DBG_LVL_1,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530723 "mgmt_invalidate_connection Failed for cid=%d\n",
Jayamohan Kallickal90a289e2010-02-20 08:04:28 +0530724 beiscsi_ep->ep_cid);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530725 } else {
726 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
727 phba->ctrl.mcc_numtag[tag]);
728 free_mcc_tag(&phba->ctrl, tag);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530729 }
Mike Christiefa95d202010-06-09 03:30:08 -0500730
Jayamohan Kallickalc2462282010-01-05 05:05:34 +0530731 beiscsi_close_conn(beiscsi_ep, CONNECTION_UPLOAD_GRACEFUL);
732 beiscsi_free_ep(beiscsi_ep);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530733 beiscsi_unbind_conn_to_cid(phba, beiscsi_ep->ep_cid);
Mike Christiefa95d202010-06-09 03:30:08 -0500734 iscsi_destroy_endpoint(beiscsi_ep->openiscsi_ep);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530735}
Mike Christie3128c6c2011-07-25 13:48:42 -0500736
737mode_t be2iscsi_attr_is_visible(int param_type, int param)
738{
739 switch (param_type) {
740 case ISCSI_PARAM:
741 switch (param) {
742 case ISCSI_PARAM_MAX_RECV_DLENGTH:
743 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
744 case ISCSI_PARAM_HDRDGST_EN:
745 case ISCSI_PARAM_DATADGST_EN:
746 case ISCSI_PARAM_CONN_ADDRESS:
747 case ISCSI_PARAM_CONN_PORT:
748 case ISCSI_PARAM_EXP_STATSN:
749 case ISCSI_PARAM_PERSISTENT_ADDRESS:
750 case ISCSI_PARAM_PERSISTENT_PORT:
751 case ISCSI_PARAM_PING_TMO:
752 case ISCSI_PARAM_RECV_TMO:
753 return S_IRUGO;
754 default:
755 return 0;
756 }
757 }
758
759 return 0;
760}