2 * Copyright (C) 2005 - 2009 ServerEngines
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.
10 * Written by: Jayamohan Kallickal (jayamohank@serverengines.com)
12 * Contact Information:
13 * linux-drivers@serverengines.com
16 * 209 N. Fair Oaks Ave
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>
31 extern struct iscsi_transport beiscsi_iscsi_transport
;
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
39 struct iscsi_cls_session
*beiscsi_session_create(struct iscsi_endpoint
*ep
,
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
;
52 SE_DEBUG(DBG_LVL_8
, "In beiscsi_session_create\n");
55 SE_DEBUG(DBG_LVL_1
, "beiscsi_session_create: invalid ep \n");
58 beiscsi_ep
= ep
->dd_data
;
59 phba
= beiscsi_ep
->phba
;
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. "
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
;
70 cls_session
= iscsi_session_setup(&beiscsi_iscsi_transport
,
72 sizeof(*beiscsi_sess
),
74 initial_cmdsn
, ISCSI_MAX_TARGET
);
77 sess
= cls_session
->dd_data
;
78 beiscsi_sess
= sess
->dd_data
;
79 beiscsi_sess
->bhs_pool
= pci_pool_create("beiscsi_bhs_pool",
81 sizeof(struct be_cmd_bhs
),
83 if (!beiscsi_sess
->bhs_pool
)
88 iscsi_session_teardown(cls_session
);
93 * beiscsi_session_destroy - destroys iscsi session
94 * @cls_session: pointer to iscsi cls session
96 * Destroys iSCSI session instance and releases
97 * resources allocated for it.
99 void beiscsi_session_destroy(struct iscsi_cls_session
*cls_session
)
101 struct iscsi_session
*sess
= cls_session
->dd_data
;
102 struct beiscsi_session
*beiscsi_sess
= sess
->dd_data
;
104 pci_pool_destroy(beiscsi_sess
->bhs_pool
);
105 iscsi_session_teardown(cls_session
);
109 * beiscsi_conn_create - create an instance of iscsi connection
110 * @cls_session: ptr to iscsi_cls_session
113 struct iscsi_cls_conn
*
114 beiscsi_conn_create(struct iscsi_cls_session
*cls_session
, u32 cid
)
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
;
121 struct iscsi_session
*sess
;
122 struct beiscsi_session
*beiscsi_sess
;
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
);
129 cls_conn
= iscsi_conn_setup(cls_session
, sizeof(*beiscsi_conn
), cid
);
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
;
138 sess
= cls_session
->dd_data
;
139 beiscsi_sess
= sess
->dd_data
;
140 beiscsi_conn
->beiscsi_sess
= beiscsi_sess
;
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
150 static int beiscsi_bindconn_cid(struct beiscsi_hba
*phba
,
151 struct beiscsi_conn
*beiscsi_conn
,
154 if (phba
->conn_table
[cid
]) {
156 "Connection table already occupied. Detected clash\n");
159 SE_DEBUG(DBG_LVL_8
, "phba->conn_table[%d]=%p(beiscsi_conn) \n",
161 phba
->conn_table
[cid
] = beiscsi_conn
;
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)
172 * This function binds the TCP Conn with iSCSI Connection and Session.
174 int beiscsi_conn_bind(struct iscsi_cls_session
*cls_session
,
175 struct iscsi_cls_conn
*cls_conn
,
176 u64 transport_fd
, int is_leading
)
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
;
186 SE_DEBUG(DBG_LVL_8
, "In beiscsi_conn_bind\n");
187 ep
= iscsi_lookup_endpoint(transport_fd
);
191 beiscsi_ep
= ep
->dd_data
;
193 if (iscsi_conn_bind(cls_session
, cls_conn
, is_leading
))
196 if (beiscsi_ep
->phba
!= phba
) {
198 "beiscsi_ep->hba=%p not equal to phba=%p \n",
199 beiscsi_ep
->phba
, phba
);
203 beiscsi_conn
->beiscsi_conn_cid
= beiscsi_ep
->ep_cid
;
204 beiscsi_conn
->ep
= beiscsi_ep
;
205 beiscsi_ep
->conn
= beiscsi_conn
;
206 SE_DEBUG(DBG_LVL_8
, "beiscsi_conn=%p conn=%p ep_cid=%d \n",
207 beiscsi_conn
, conn
, beiscsi_ep
->ep_cid
);
208 return beiscsi_bindconn_cid(phba
, beiscsi_conn
, beiscsi_ep
->ep_cid
);
212 * beiscsi_conn_get_param - get the iscsi parameter
213 * @cls_conn: pointer to iscsi cls conn
214 * @param: parameter type identifier
215 * @buf: buffer pointer
217 * returns iscsi parameter
219 int beiscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
220 enum iscsi_param param
, char *buf
)
222 struct beiscsi_endpoint
*beiscsi_ep
;
223 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
224 struct beiscsi_conn
*beiscsi_conn
= conn
->dd_data
;
227 beiscsi_ep
= beiscsi_conn
->ep
;
230 "In beiscsi_conn_get_param , no beiscsi_ep\n");
235 case ISCSI_PARAM_CONN_PORT
:
236 len
= sprintf(buf
, "%hu\n", beiscsi_ep
->dst_tcpport
);
238 case ISCSI_PARAM_CONN_ADDRESS
:
239 if (beiscsi_ep
->ip_type
== BE2_IPV4
)
240 len
= sprintf(buf
, "%pI4\n", &beiscsi_ep
->dst_addr
);
242 len
= sprintf(buf
, "%pI6\n", &beiscsi_ep
->dst6_addr
);
245 return iscsi_conn_get_param(cls_conn
, param
, buf
);
250 int beiscsi_set_param(struct iscsi_cls_conn
*cls_conn
,
251 enum iscsi_param param
, char *buf
, int buflen
)
253 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
254 struct iscsi_session
*session
= conn
->session
;
257 ret
= iscsi_set_param(cls_conn
, param
, buf
, buflen
);
261 * If userspace tried to set the value to higher than we can
262 * support override here.
265 case ISCSI_PARAM_FIRST_BURST
:
266 if (session
->first_burst
> 8192)
267 session
->first_burst
= 8192;
269 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
270 if (conn
->max_recv_dlength
> 65536)
271 conn
->max_recv_dlength
= 65536;
273 case ISCSI_PARAM_MAX_BURST
:
274 if (session
->first_burst
> 262144)
275 session
->first_burst
= 262144;
285 * beiscsi_get_host_param - get the iscsi parameter
286 * @shost: pointer to scsi_host structure
287 * @param: parameter type identifier
288 * @buf: buffer pointer
290 * returns host parameter
292 int beiscsi_get_host_param(struct Scsi_Host
*shost
,
293 enum iscsi_host_param param
, char *buf
)
295 struct beiscsi_hba
*phba
= (struct beiscsi_hba
*)iscsi_host_priv(shost
);
299 case ISCSI_HOST_PARAM_HWADDRESS
:
300 be_cmd_get_mac_addr(&phba
->ctrl
, phba
->mac_address
);
301 len
= sysfs_format_mac(buf
, phba
->mac_address
, ETH_ALEN
);
304 return iscsi_host_get_param(shost
, param
, buf
);
310 * beiscsi_conn_get_stats - get the iscsi stats
311 * @cls_conn: pointer to iscsi cls conn
312 * @stats: pointer to iscsi_stats structure
314 * returns iscsi stats
316 void beiscsi_conn_get_stats(struct iscsi_cls_conn
*cls_conn
,
317 struct iscsi_stats
*stats
)
319 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
321 SE_DEBUG(DBG_LVL_8
, "In beiscsi_conn_get_stats\n");
322 stats
->txdata_octets
= conn
->txdata_octets
;
323 stats
->rxdata_octets
= conn
->rxdata_octets
;
324 stats
->dataout_pdus
= conn
->dataout_pdus_cnt
;
325 stats
->scsirsp_pdus
= conn
->scsirsp_pdus_cnt
;
326 stats
->scsicmd_pdus
= conn
->scsicmd_pdus_cnt
;
327 stats
->datain_pdus
= conn
->datain_pdus_cnt
;
328 stats
->tmfrsp_pdus
= conn
->tmfrsp_pdus_cnt
;
329 stats
->tmfcmd_pdus
= conn
->tmfcmd_pdus_cnt
;
330 stats
->r2t_pdus
= conn
->r2t_pdus_cnt
;
331 stats
->digest_err
= 0;
332 stats
->timeout_err
= 0;
333 stats
->custom_length
= 0;
334 strcpy(stats
->custom
[0].desc
, "eh_abort_cnt");
335 stats
->custom
[0].value
= conn
->eh_abort_cnt
;
339 * beiscsi_set_params_for_offld - get the parameters for offload
340 * @beiscsi_conn: pointer to beiscsi_conn
341 * @params: pointer to offload_params structure
343 static void beiscsi_set_params_for_offld(struct beiscsi_conn
*beiscsi_conn
,
344 struct beiscsi_offload_params
*params
)
346 struct iscsi_conn
*conn
= beiscsi_conn
->conn
;
347 struct iscsi_session
*session
= conn
->session
;
349 AMAP_SET_BITS(struct amap_beiscsi_offload_params
, max_burst_length
,
350 params
, session
->max_burst
);
351 AMAP_SET_BITS(struct amap_beiscsi_offload_params
,
352 max_send_data_segment_length
, params
,
353 conn
->max_xmit_dlength
);
354 AMAP_SET_BITS(struct amap_beiscsi_offload_params
, first_burst_length
,
355 params
, session
->first_burst
);
356 AMAP_SET_BITS(struct amap_beiscsi_offload_params
, erl
, params
,
358 AMAP_SET_BITS(struct amap_beiscsi_offload_params
, dde
, params
,
360 AMAP_SET_BITS(struct amap_beiscsi_offload_params
, hde
, params
,
362 AMAP_SET_BITS(struct amap_beiscsi_offload_params
, ir2t
, params
,
363 session
->initial_r2t_en
);
364 AMAP_SET_BITS(struct amap_beiscsi_offload_params
, imd
, params
,
365 session
->imm_data_en
);
366 AMAP_SET_BITS(struct amap_beiscsi_offload_params
, exp_statsn
, params
,
367 (conn
->exp_statsn
- 1));
371 * beiscsi_conn_start - offload of session to chip
372 * @cls_conn: pointer to beiscsi_conn
374 int beiscsi_conn_start(struct iscsi_cls_conn
*cls_conn
)
376 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
377 struct beiscsi_conn
*beiscsi_conn
= conn
->dd_data
;
378 struct beiscsi_endpoint
*beiscsi_ep
;
379 struct beiscsi_offload_params params
;
380 struct iscsi_session
*session
= conn
->session
;
381 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
->cls_session
);
382 struct beiscsi_hba
*phba
= iscsi_host_priv(shost
);
384 memset(¶ms
, 0, sizeof(struct beiscsi_offload_params
));
385 beiscsi_ep
= beiscsi_conn
->ep
;
387 SE_DEBUG(DBG_LVL_1
, "In beiscsi_conn_start , no beiscsi_ep\n");
389 free_mgmt_sgl_handle(phba
, beiscsi_conn
->plogin_sgl_handle
);
390 beiscsi_conn
->login_in_progress
= 0;
391 beiscsi_set_params_for_offld(beiscsi_conn
, ¶ms
);
392 beiscsi_offload_connection(beiscsi_conn
, ¶ms
);
393 iscsi_conn_start(cls_conn
);
398 * beiscsi_get_cid - Allocate a cid
399 * @phba: The phba instance
401 static int beiscsi_get_cid(struct beiscsi_hba
*phba
)
403 unsigned short cid
= 0xFFFF;
405 if (!phba
->avlbl_cids
)
408 cid
= phba
->cid_array
[phba
->cid_alloc
++];
409 if (phba
->cid_alloc
== phba
->params
.cxns_per_ctrl
)
416 * beiscsi_open_conn - Ask FW to open a TCP connection
417 * @ep: endpoint to be used
418 * @src_addr: The source IP address
419 * @dst_addr: The Destination IP address
421 * Asks the FW to open a TCP connection
423 static int beiscsi_open_conn(struct iscsi_endpoint
*ep
,
424 struct sockaddr
*src_addr
,
425 struct sockaddr
*dst_addr
, int non_blocking
)
427 struct beiscsi_endpoint
*beiscsi_ep
= ep
->dd_data
;
428 struct beiscsi_hba
*phba
= beiscsi_ep
->phba
;
431 beiscsi_ep
->ep_cid
= beiscsi_get_cid(phba
);
432 if (beiscsi_ep
->ep_cid
== 0xFFFF) {
433 SE_DEBUG(DBG_LVL_1
, "No free cid available\n");
436 SE_DEBUG(DBG_LVL_8
, "In beiscsi_open_conn, ep_cid=%d ",
438 phba
->ep_array
[beiscsi_ep
->ep_cid
] = ep
;
439 if (beiscsi_ep
->ep_cid
>
440 (phba
->fw_config
.iscsi_cid_start
+ phba
->params
.cxns_per_ctrl
)) {
441 SE_DEBUG(DBG_LVL_1
, "Failed in allocate iscsi cid\n");
445 beiscsi_ep
->cid_vld
= 0;
446 return mgmt_open_connection(phba
, dst_addr
, beiscsi_ep
);
450 * beiscsi_put_cid - Free the cid
451 * @phba: The phba for which the cid is being freed
452 * @cid: The cid to free
454 static void beiscsi_put_cid(struct beiscsi_hba
*phba
, unsigned short cid
)
457 phba
->cid_array
[phba
->cid_free
++] = cid
;
458 if (phba
->cid_free
== phba
->params
.cxns_per_ctrl
)
463 * beiscsi_free_ep - free endpoint
464 * @ep: pointer to iscsi endpoint structure
466 static void beiscsi_free_ep(struct iscsi_endpoint
*ep
)
468 struct beiscsi_endpoint
*beiscsi_ep
= ep
->dd_data
;
469 struct beiscsi_hba
*phba
= beiscsi_ep
->phba
;
471 beiscsi_put_cid(phba
, beiscsi_ep
->ep_cid
);
472 beiscsi_ep
->phba
= NULL
;
473 iscsi_destroy_endpoint(ep
);
477 * beiscsi_ep_connect - Ask chip to create TCP Conn
478 * @scsi_host: Pointer to scsi_host structure
479 * @dst_addr: The IP address of Target
480 * @non_blocking: blocking or non-blocking call
482 * This routines first asks chip to create a connection and then allocates an EP
484 struct iscsi_endpoint
*
485 beiscsi_ep_connect(struct Scsi_Host
*shost
, struct sockaddr
*dst_addr
,
488 struct beiscsi_hba
*phba
;
489 struct beiscsi_endpoint
*beiscsi_ep
;
490 struct iscsi_endpoint
*ep
;
493 SE_DEBUG(DBG_LVL_8
, "In beiscsi_ep_connect \n");
495 phba
= iscsi_host_priv(shost
);
498 SE_DEBUG(DBG_LVL_1
, "shost is NULL \n");
501 ep
= iscsi_create_endpoint(sizeof(struct beiscsi_endpoint
));
507 beiscsi_ep
= ep
->dd_data
;
508 beiscsi_ep
->phba
= phba
;
510 if (beiscsi_open_conn(ep
, NULL
, dst_addr
, non_blocking
)) {
511 SE_DEBUG(DBG_LVL_1
, "Failed in allocate iscsi cid\n");
524 * beiscsi_ep_poll - Poll to see if connection is established
525 * @ep: endpoint to be used
526 * @timeout_ms: timeout specified in millisecs
528 * Poll to see if TCP connection established
530 int beiscsi_ep_poll(struct iscsi_endpoint
*ep
, int timeout_ms
)
532 struct beiscsi_endpoint
*beiscsi_ep
= ep
->dd_data
;
534 SE_DEBUG(DBG_LVL_8
, "In beiscsi_ep_poll\n");
535 if (beiscsi_ep
->cid_vld
== 1)
542 * beiscsi_close_conn - Upload the connection
543 * @ep: The iscsi endpoint
544 * @flag: The type of connection closure
546 static int beiscsi_close_conn(struct iscsi_endpoint
*ep
, int flag
)
549 struct beiscsi_endpoint
*beiscsi_ep
= ep
->dd_data
;
550 struct beiscsi_hba
*phba
= beiscsi_ep
->phba
;
552 if (MGMT_STATUS_SUCCESS
!=
553 mgmt_upload_connection(phba
, beiscsi_ep
->ep_cid
,
554 CONNECTION_UPLOAD_GRACEFUL
)) {
555 SE_DEBUG(DBG_LVL_8
, "upload failed for cid 0x%x",
564 * beiscsi_ep_disconnect - Tears down the TCP connection
565 * @ep: endpoint to be used
567 * Tears down the TCP connection
569 void beiscsi_ep_disconnect(struct iscsi_endpoint
*ep
)
571 struct beiscsi_conn
*beiscsi_conn
;
572 struct beiscsi_endpoint
*beiscsi_ep
;
573 struct beiscsi_hba
*phba
;
576 beiscsi_ep
= ep
->dd_data
;
577 phba
= beiscsi_ep
->phba
;
578 SE_DEBUG(DBG_LVL_8
, "In beiscsi_ep_disconnect\n");
580 if (beiscsi_ep
->conn
) {
581 beiscsi_conn
= beiscsi_ep
->conn
;
582 iscsi_suspend_queue(beiscsi_conn
->conn
);
583 beiscsi_close_conn(ep
, flag
);
590 * beiscsi_unbind_conn_to_cid - Unbind the beiscsi_conn from phba conn table
591 * @phba: The phba instance
592 * @cid: The cid to free
594 static int beiscsi_unbind_conn_to_cid(struct beiscsi_hba
*phba
,
597 if (phba
->conn_table
[cid
])
598 phba
->conn_table
[cid
] = NULL
;
600 SE_DEBUG(DBG_LVL_8
, "Connection table Not occupied. \n");
607 * beiscsi_conn_stop - Invalidate and stop the connection
608 * @cls_conn: pointer to get iscsi_conn
609 * @flag: The type of connection closure
611 void beiscsi_conn_stop(struct iscsi_cls_conn
*cls_conn
, int flag
)
613 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
614 struct beiscsi_conn
*beiscsi_conn
= conn
->dd_data
;
615 struct beiscsi_endpoint
*beiscsi_ep
;
616 struct iscsi_session
*session
= conn
->session
;
617 struct Scsi_Host
*shost
= iscsi_session_to_shost(session
->cls_session
);
618 struct beiscsi_hba
*phba
= iscsi_host_priv(shost
);
620 unsigned short savecfg_flag
= CMD_ISCSI_SESSION_SAVE_CFG_ON_FLASH
;
622 SE_DEBUG(DBG_LVL_8
, "In beiscsi_conn_stop\n");
623 beiscsi_ep
= beiscsi_conn
->ep
;
625 SE_DEBUG(DBG_LVL_8
, "In beiscsi_conn_stop , no beiscsi_ep\n");
628 status
= mgmt_invalidate_connection(phba
, beiscsi_ep
,
629 beiscsi_ep
->ep_cid
, 1,
631 if (status
!= MGMT_STATUS_SUCCESS
) {
633 "mgmt_invalidate_connection Failed for cid=%d \n",
636 beiscsi_unbind_conn_to_cid(phba
, beiscsi_ep
->ep_cid
);
637 iscsi_conn_stop(cls_conn
, flag
);