Linux 2.6.34-rc3
[pohmelfs.git] / drivers / scsi / cxgb3i / cxgb3i_iscsi.c
blobb7c30585daddb577fc833179bf564d2646ef51d9
1 /* cxgb3i_iscsi.c: Chelsio S3xx iSCSI driver.
3 * Copyright (c) 2008 Chelsio Communications, Inc.
4 * Copyright (c) 2008 Mike Christie
5 * Copyright (c) 2008 Red Hat, Inc. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation.
11 * Written by: Karen Xie (kxie@chelsio.com)
14 #include <linux/inet.h>
15 #include <linux/crypto.h>
16 #include <linux/if_vlan.h>
17 #include <net/dst.h>
18 #include <net/tcp.h>
19 #include <scsi/scsi_cmnd.h>
20 #include <scsi/scsi_device.h>
21 #include <scsi/scsi_eh.h>
22 #include <scsi/scsi_host.h>
23 #include <scsi/scsi.h>
24 #include <scsi/iscsi_proto.h>
25 #include <scsi/libiscsi.h>
26 #include <scsi/scsi_transport_iscsi.h>
28 #include "cxgb3i.h"
29 #include "cxgb3i_pdu.h"
31 #ifdef __DEBUG_CXGB3I_TAG__
32 #define cxgb3i_tag_debug cxgb3i_log_debug
33 #else
34 #define cxgb3i_tag_debug(fmt...)
35 #endif
37 #ifdef __DEBUG_CXGB3I_API__
38 #define cxgb3i_api_debug cxgb3i_log_debug
39 #else
40 #define cxgb3i_api_debug(fmt...)
41 #endif
44 * align pdu size to multiple of 512 for better performance
46 #define align_pdu_size(n) do { n = (n) & (~511); } while (0)
48 static struct scsi_transport_template *cxgb3i_scsi_transport;
49 static struct scsi_host_template cxgb3i_host_template;
50 static struct iscsi_transport cxgb3i_iscsi_transport;
51 static unsigned char sw_tag_idx_bits;
52 static unsigned char sw_tag_age_bits;
54 static LIST_HEAD(cxgb3i_snic_list);
55 static DEFINE_RWLOCK(cxgb3i_snic_rwlock);
57 /**
58 * cxgb3i_adpater_find_by_tdev - find the cxgb3i_adapter structure via t3cdev
59 * @tdev: t3cdev pointer
61 struct cxgb3i_adapter *cxgb3i_adapter_find_by_tdev(struct t3cdev *tdev)
63 struct cxgb3i_adapter *snic;
65 read_lock(&cxgb3i_snic_rwlock);
66 list_for_each_entry(snic, &cxgb3i_snic_list, list_head) {
67 if (snic->tdev == tdev) {
68 read_unlock(&cxgb3i_snic_rwlock);
69 return snic;
72 read_unlock(&cxgb3i_snic_rwlock);
73 return NULL;
76 static inline int adapter_update(struct cxgb3i_adapter *snic)
78 cxgb3i_log_info("snic 0x%p, t3dev 0x%p, updating.\n",
79 snic, snic->tdev);
80 return cxgb3i_adapter_ddp_info(snic->tdev, &snic->tag_format,
81 &snic->tx_max_size,
82 &snic->rx_max_size);
85 static int adapter_add(struct cxgb3i_adapter *snic)
87 struct t3cdev *t3dev = snic->tdev;
88 struct adapter *adapter = tdev2adap(t3dev);
89 int i, err;
91 snic->pdev = adapter->pdev;
92 snic->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
94 err = cxgb3i_adapter_ddp_info(t3dev, &snic->tag_format,
95 &snic->tx_max_size,
96 &snic->rx_max_size);
97 if (err < 0)
98 return err;
100 for_each_port(adapter, i) {
101 snic->hba[i] = cxgb3i_hba_host_add(snic, adapter->port[i]);
102 if (!snic->hba[i])
103 return -EINVAL;
105 snic->hba_cnt = adapter->params.nports;
107 /* add to the list */
108 write_lock(&cxgb3i_snic_rwlock);
109 list_add_tail(&snic->list_head, &cxgb3i_snic_list);
110 write_unlock(&cxgb3i_snic_rwlock);
112 cxgb3i_log_info("t3dev 0x%p open, snic 0x%p, %u scsi hosts added.\n",
113 t3dev, snic, snic->hba_cnt);
114 return 0;
118 * cxgb3i_adapter_open - init a s3 adapter structure and any h/w settings
119 * @t3dev: t3cdev adapter
121 void cxgb3i_adapter_open(struct t3cdev *t3dev)
123 struct cxgb3i_adapter *snic = cxgb3i_adapter_find_by_tdev(t3dev);
124 int err;
126 if (snic)
127 err = adapter_update(snic);
128 else {
129 snic = kzalloc(sizeof(*snic), GFP_KERNEL);
130 if (snic) {
131 spin_lock_init(&snic->lock);
132 snic->tdev = t3dev;
133 err = adapter_add(snic);
134 } else
135 err = -ENOMEM;
138 if (err < 0) {
139 cxgb3i_log_info("snic 0x%p, f 0x%x, t3dev 0x%p open, err %d.\n",
140 snic, snic ? snic->flags : 0, t3dev, err);
141 if (snic) {
142 snic->flags &= ~CXGB3I_ADAPTER_FLAG_RESET;
143 cxgb3i_adapter_close(t3dev);
149 * cxgb3i_adapter_close - release the resources held and cleanup h/w settings
150 * @t3dev: t3cdev adapter
152 void cxgb3i_adapter_close(struct t3cdev *t3dev)
154 struct cxgb3i_adapter *snic = cxgb3i_adapter_find_by_tdev(t3dev);
155 int i;
157 if (!snic || snic->flags & CXGB3I_ADAPTER_FLAG_RESET) {
158 cxgb3i_log_info("t3dev 0x%p close, snic 0x%p, f 0x%x.\n",
159 t3dev, snic, snic ? snic->flags : 0);
160 return;
163 /* remove from the list */
164 write_lock(&cxgb3i_snic_rwlock);
165 list_del(&snic->list_head);
166 write_unlock(&cxgb3i_snic_rwlock);
168 for (i = 0; i < snic->hba_cnt; i++) {
169 if (snic->hba[i]) {
170 cxgb3i_hba_host_remove(snic->hba[i]);
171 snic->hba[i] = NULL;
174 cxgb3i_log_info("t3dev 0x%p close, snic 0x%p, %u scsi hosts removed.\n",
175 t3dev, snic, snic->hba_cnt);
176 kfree(snic);
180 * cxgb3i_hba_find_by_netdev - find the cxgb3i_hba structure via net_device
181 * @t3dev: t3cdev adapter
183 static struct cxgb3i_hba *cxgb3i_hba_find_by_netdev(struct net_device *ndev)
185 struct cxgb3i_adapter *snic;
186 int i;
188 if (ndev->priv_flags & IFF_802_1Q_VLAN)
189 ndev = vlan_dev_real_dev(ndev);
191 read_lock(&cxgb3i_snic_rwlock);
192 list_for_each_entry(snic, &cxgb3i_snic_list, list_head) {
193 for (i = 0; i < snic->hba_cnt; i++) {
194 if (snic->hba[i]->ndev == ndev) {
195 read_unlock(&cxgb3i_snic_rwlock);
196 return snic->hba[i];
200 read_unlock(&cxgb3i_snic_rwlock);
201 return NULL;
205 * cxgb3i_hba_host_add - register a new host with scsi/iscsi
206 * @snic: the cxgb3i adapter
207 * @ndev: associated net_device
209 struct cxgb3i_hba *cxgb3i_hba_host_add(struct cxgb3i_adapter *snic,
210 struct net_device *ndev)
212 struct cxgb3i_hba *hba;
213 struct Scsi_Host *shost;
214 int err;
216 shost = iscsi_host_alloc(&cxgb3i_host_template,
217 sizeof(struct cxgb3i_hba), 1);
218 if (!shost) {
219 cxgb3i_log_info("snic 0x%p, ndev 0x%p, host_alloc failed.\n",
220 snic, ndev);
221 return NULL;
224 shost->transportt = cxgb3i_scsi_transport;
225 shost->max_lun = CXGB3I_MAX_LUN;
226 shost->max_id = CXGB3I_MAX_TARGET;
227 shost->max_channel = 0;
228 shost->max_cmd_len = 16;
230 hba = iscsi_host_priv(shost);
231 hba->snic = snic;
232 hba->ndev = ndev;
233 hba->shost = shost;
235 pci_dev_get(snic->pdev);
236 err = iscsi_host_add(shost, &snic->pdev->dev);
237 if (err) {
238 cxgb3i_log_info("snic 0x%p, ndev 0x%p, host_add failed.\n",
239 snic, ndev);
240 goto pci_dev_put;
243 cxgb3i_api_debug("shost 0x%p, hba 0x%p, no %u.\n",
244 shost, hba, shost->host_no);
246 return hba;
248 pci_dev_put:
249 pci_dev_put(snic->pdev);
250 scsi_host_put(shost);
251 return NULL;
255 * cxgb3i_hba_host_remove - de-register the host with scsi/iscsi
256 * @hba: the cxgb3i hba
258 void cxgb3i_hba_host_remove(struct cxgb3i_hba *hba)
260 cxgb3i_api_debug("shost 0x%p, hba 0x%p, no %u.\n",
261 hba->shost, hba, hba->shost->host_no);
262 iscsi_host_remove(hba->shost);
263 pci_dev_put(hba->snic->pdev);
264 iscsi_host_free(hba->shost);
268 * cxgb3i_ep_connect - establish TCP connection to target portal
269 * @shost: scsi host to use
270 * @dst_addr: target IP address
271 * @non_blocking: blocking or non-blocking call
273 * Initiates a TCP/IP connection to the dst_addr
275 static struct iscsi_endpoint *cxgb3i_ep_connect(struct Scsi_Host *shost,
276 struct sockaddr *dst_addr,
277 int non_blocking)
279 struct iscsi_endpoint *ep;
280 struct cxgb3i_endpoint *cep;
281 struct cxgb3i_hba *hba = NULL;
282 struct s3_conn *c3cn = NULL;
283 int err = 0;
285 if (shost)
286 hba = iscsi_host_priv(shost);
288 cxgb3i_api_debug("shost 0x%p, hba 0x%p.\n", shost, hba);
290 c3cn = cxgb3i_c3cn_create();
291 if (!c3cn) {
292 cxgb3i_log_info("ep connect OOM.\n");
293 err = -ENOMEM;
294 goto release_conn;
297 err = cxgb3i_c3cn_connect(hba ? hba->ndev : NULL, c3cn,
298 (struct sockaddr_in *)dst_addr);
299 if (err < 0) {
300 cxgb3i_log_info("ep connect failed.\n");
301 goto release_conn;
304 hba = cxgb3i_hba_find_by_netdev(c3cn->dst_cache->dev);
305 if (!hba) {
306 err = -ENOSPC;
307 cxgb3i_log_info("NOT going through cxgbi device.\n");
308 goto release_conn;
311 if (shost && hba != iscsi_host_priv(shost)) {
312 err = -ENOSPC;
313 cxgb3i_log_info("Could not connect through request host%u\n",
314 shost->host_no);
315 goto release_conn;
318 if (c3cn_is_closing(c3cn)) {
319 err = -ENOSPC;
320 cxgb3i_log_info("ep connect unable to connect.\n");
321 goto release_conn;
324 ep = iscsi_create_endpoint(sizeof(*cep));
325 if (!ep) {
326 err = -ENOMEM;
327 cxgb3i_log_info("iscsi alloc ep, OOM.\n");
328 goto release_conn;
330 cep = ep->dd_data;
331 cep->c3cn = c3cn;
332 cep->hba = hba;
334 cxgb3i_api_debug("ep 0x%p, 0x%p, c3cn 0x%p, hba 0x%p.\n",
335 ep, cep, c3cn, hba);
336 return ep;
338 release_conn:
339 cxgb3i_api_debug("conn 0x%p failed, release.\n", c3cn);
340 if (c3cn)
341 cxgb3i_c3cn_release(c3cn);
342 return ERR_PTR(err);
346 * cxgb3i_ep_poll - polls for TCP connection establishement
347 * @ep: TCP connection (endpoint) handle
348 * @timeout_ms: timeout value in milli secs
350 * polls for TCP connect request to complete
352 static int cxgb3i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
354 struct cxgb3i_endpoint *cep = ep->dd_data;
355 struct s3_conn *c3cn = cep->c3cn;
357 if (!c3cn_is_established(c3cn))
358 return 0;
359 cxgb3i_api_debug("ep 0x%p, c3cn 0x%p established.\n", ep, c3cn);
360 return 1;
364 * cxgb3i_ep_disconnect - teardown TCP connection
365 * @ep: TCP connection (endpoint) handle
367 * teardown TCP connection
369 static void cxgb3i_ep_disconnect(struct iscsi_endpoint *ep)
371 struct cxgb3i_endpoint *cep = ep->dd_data;
372 struct cxgb3i_conn *cconn = cep->cconn;
374 cxgb3i_api_debug("ep 0x%p, cep 0x%p.\n", ep, cep);
376 if (cconn && cconn->conn) {
378 * stop the xmit path so the xmit_pdu function is
379 * not being called
381 iscsi_suspend_tx(cconn->conn);
383 write_lock_bh(&cep->c3cn->callback_lock);
384 cep->c3cn->user_data = NULL;
385 cconn->cep = NULL;
386 write_unlock_bh(&cep->c3cn->callback_lock);
389 cxgb3i_api_debug("ep 0x%p, cep 0x%p, release c3cn 0x%p.\n",
390 ep, cep, cep->c3cn);
391 cxgb3i_c3cn_release(cep->c3cn);
392 iscsi_destroy_endpoint(ep);
396 * cxgb3i_session_create - create a new iscsi session
397 * @cmds_max: max # of commands
398 * @qdepth: scsi queue depth
399 * @initial_cmdsn: initial iscsi CMDSN for this session
401 * Creates a new iSCSI session
403 static struct iscsi_cls_session *
404 cxgb3i_session_create(struct iscsi_endpoint *ep, u16 cmds_max, u16 qdepth,
405 u32 initial_cmdsn)
407 struct cxgb3i_endpoint *cep;
408 struct cxgb3i_hba *hba;
409 struct Scsi_Host *shost;
410 struct iscsi_cls_session *cls_session;
411 struct iscsi_session *session;
413 if (!ep) {
414 cxgb3i_log_error("%s, missing endpoint.\n", __func__);
415 return NULL;
418 cep = ep->dd_data;
419 hba = cep->hba;
420 shost = hba->shost;
421 cxgb3i_api_debug("ep 0x%p, cep 0x%p, hba 0x%p.\n", ep, cep, hba);
422 BUG_ON(hba != iscsi_host_priv(shost));
424 cls_session = iscsi_session_setup(&cxgb3i_iscsi_transport, shost,
425 cmds_max, 0,
426 sizeof(struct iscsi_tcp_task) +
427 sizeof(struct cxgb3i_task_data),
428 initial_cmdsn, ISCSI_MAX_TARGET);
429 if (!cls_session)
430 return NULL;
431 session = cls_session->dd_data;
432 if (iscsi_tcp_r2tpool_alloc(session))
433 goto remove_session;
435 return cls_session;
437 remove_session:
438 iscsi_session_teardown(cls_session);
439 return NULL;
443 * cxgb3i_session_destroy - destroys iscsi session
444 * @cls_session: pointer to iscsi cls session
446 * Destroys an iSCSI session instance and releases its all resources held
448 static void cxgb3i_session_destroy(struct iscsi_cls_session *cls_session)
450 cxgb3i_api_debug("sess 0x%p.\n", cls_session);
451 iscsi_tcp_r2tpool_free(cls_session->dd_data);
452 iscsi_session_teardown(cls_session);
456 * cxgb3i_conn_max_xmit_dlength -- calc the max. xmit pdu segment size
457 * @conn: iscsi connection
458 * check the max. xmit pdu payload, reduce it if needed
460 static inline int cxgb3i_conn_max_xmit_dlength(struct iscsi_conn *conn)
463 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
464 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
465 unsigned int max = max(512 * MAX_SKB_FRAGS, SKB_TX_HEADROOM);
467 max = min(cconn->hba->snic->tx_max_size, max);
468 if (conn->max_xmit_dlength)
469 conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
470 else
471 conn->max_xmit_dlength = max;
472 align_pdu_size(conn->max_xmit_dlength);
473 cxgb3i_api_debug("conn 0x%p, max xmit %u.\n",
474 conn, conn->max_xmit_dlength);
475 return 0;
479 * cxgb3i_conn_max_recv_dlength -- check the max. recv pdu segment size
480 * @conn: iscsi connection
481 * return 0 if the value is valid, < 0 otherwise.
483 static inline int cxgb3i_conn_max_recv_dlength(struct iscsi_conn *conn)
485 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
486 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
487 unsigned int max = cconn->hba->snic->rx_max_size;
489 align_pdu_size(max);
490 if (conn->max_recv_dlength) {
491 if (conn->max_recv_dlength > max) {
492 cxgb3i_log_error("MaxRecvDataSegmentLength %u too big."
493 " Need to be <= %u.\n",
494 conn->max_recv_dlength, max);
495 return -EINVAL;
497 conn->max_recv_dlength = min(conn->max_recv_dlength, max);
498 align_pdu_size(conn->max_recv_dlength);
499 } else
500 conn->max_recv_dlength = max;
501 cxgb3i_api_debug("conn 0x%p, max recv %u.\n",
502 conn, conn->max_recv_dlength);
503 return 0;
507 * cxgb3i_conn_create - create iscsi connection instance
508 * @cls_session: pointer to iscsi cls session
509 * @cid: iscsi cid
511 * Creates a new iSCSI connection instance for a given session
513 static struct iscsi_cls_conn *cxgb3i_conn_create(struct iscsi_cls_session
514 *cls_session, u32 cid)
516 struct iscsi_cls_conn *cls_conn;
517 struct iscsi_conn *conn;
518 struct iscsi_tcp_conn *tcp_conn;
519 struct cxgb3i_conn *cconn;
521 cxgb3i_api_debug("sess 0x%p, cid %u.\n", cls_session, cid);
523 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
524 if (!cls_conn)
525 return NULL;
526 conn = cls_conn->dd_data;
527 tcp_conn = conn->dd_data;
528 cconn = tcp_conn->dd_data;
530 cconn->conn = conn;
531 return cls_conn;
535 * cxgb3i_conn_bind - binds iscsi sess, conn and endpoint together
536 * @cls_session: pointer to iscsi cls session
537 * @cls_conn: pointer to iscsi cls conn
538 * @transport_eph: 64-bit EP handle
539 * @is_leading: leading connection on this session?
541 * Binds together an iSCSI session, an iSCSI connection and a
542 * TCP connection. This routine returns error code if the TCP
543 * connection does not belong on the device iSCSI sess/conn is bound
546 static int cxgb3i_conn_bind(struct iscsi_cls_session *cls_session,
547 struct iscsi_cls_conn *cls_conn,
548 u64 transport_eph, int is_leading)
550 struct iscsi_conn *conn = cls_conn->dd_data;
551 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
552 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
553 struct cxgb3i_adapter *snic;
554 struct iscsi_endpoint *ep;
555 struct cxgb3i_endpoint *cep;
556 struct s3_conn *c3cn;
557 int err;
559 ep = iscsi_lookup_endpoint(transport_eph);
560 if (!ep)
561 return -EINVAL;
563 /* setup ddp pagesize */
564 cep = ep->dd_data;
565 c3cn = cep->c3cn;
566 snic = cep->hba->snic;
567 err = cxgb3i_setup_conn_host_pagesize(snic->tdev, c3cn->tid, 0);
568 if (err < 0)
569 return err;
571 cxgb3i_api_debug("ep 0x%p, cls sess 0x%p, cls conn 0x%p.\n",
572 ep, cls_session, cls_conn);
574 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
575 if (err)
576 return -EINVAL;
578 /* calculate the tag idx bits needed for this conn based on cmds_max */
579 cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
580 cxgb3i_api_debug("session cmds_max 0x%x, bits %u.\n",
581 conn->session->cmds_max, cconn->task_idx_bits);
583 read_lock(&c3cn->callback_lock);
584 c3cn->user_data = conn;
585 cconn->hba = cep->hba;
586 cconn->cep = cep;
587 cep->cconn = cconn;
588 read_unlock(&c3cn->callback_lock);
590 cxgb3i_conn_max_xmit_dlength(conn);
591 cxgb3i_conn_max_recv_dlength(conn);
593 spin_lock_bh(&conn->session->lock);
594 sprintf(conn->portal_address, "%pI4", &c3cn->daddr.sin_addr.s_addr);
595 conn->portal_port = ntohs(c3cn->daddr.sin_port);
596 spin_unlock_bh(&conn->session->lock);
598 /* init recv engine */
599 iscsi_tcp_hdr_recv_prep(tcp_conn);
601 return 0;
605 * cxgb3i_conn_get_param - return iscsi connection parameter to caller
606 * @cls_conn: pointer to iscsi cls conn
607 * @param: parameter type identifier
608 * @buf: buffer pointer
610 * returns iSCSI connection parameters
612 static int cxgb3i_conn_get_param(struct iscsi_cls_conn *cls_conn,
613 enum iscsi_param param, char *buf)
615 struct iscsi_conn *conn = cls_conn->dd_data;
616 int len;
618 cxgb3i_api_debug("cls_conn 0x%p, param %d.\n", cls_conn, param);
620 switch (param) {
621 case ISCSI_PARAM_CONN_PORT:
622 spin_lock_bh(&conn->session->lock);
623 len = sprintf(buf, "%hu\n", conn->portal_port);
624 spin_unlock_bh(&conn->session->lock);
625 break;
626 case ISCSI_PARAM_CONN_ADDRESS:
627 spin_lock_bh(&conn->session->lock);
628 len = sprintf(buf, "%s\n", conn->portal_address);
629 spin_unlock_bh(&conn->session->lock);
630 break;
631 default:
632 return iscsi_conn_get_param(cls_conn, param, buf);
635 return len;
639 * cxgb3i_conn_set_param - set iscsi connection parameter
640 * @cls_conn: pointer to iscsi cls conn
641 * @param: parameter type identifier
642 * @buf: buffer pointer
643 * @buflen: buffer length
645 * set iSCSI connection parameters
647 static int cxgb3i_conn_set_param(struct iscsi_cls_conn *cls_conn,
648 enum iscsi_param param, char *buf, int buflen)
650 struct iscsi_conn *conn = cls_conn->dd_data;
651 struct iscsi_session *session = conn->session;
652 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
653 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
654 struct cxgb3i_adapter *snic = cconn->hba->snic;
655 struct s3_conn *c3cn = cconn->cep->c3cn;
656 int value, err = 0;
658 switch (param) {
659 case ISCSI_PARAM_HDRDGST_EN:
660 err = iscsi_set_param(cls_conn, param, buf, buflen);
661 if (!err && conn->hdrdgst_en)
662 err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
663 conn->hdrdgst_en,
664 conn->datadgst_en, 0);
665 break;
666 case ISCSI_PARAM_DATADGST_EN:
667 err = iscsi_set_param(cls_conn, param, buf, buflen);
668 if (!err && conn->datadgst_en)
669 err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
670 conn->hdrdgst_en,
671 conn->datadgst_en, 0);
672 break;
673 case ISCSI_PARAM_MAX_R2T:
674 sscanf(buf, "%d", &value);
675 if (value <= 0 || !is_power_of_2(value))
676 return -EINVAL;
677 if (session->max_r2t == value)
678 break;
679 iscsi_tcp_r2tpool_free(session);
680 err = iscsi_set_param(cls_conn, param, buf, buflen);
681 if (!err && iscsi_tcp_r2tpool_alloc(session))
682 return -ENOMEM;
683 case ISCSI_PARAM_MAX_RECV_DLENGTH:
684 err = iscsi_set_param(cls_conn, param, buf, buflen);
685 if (!err)
686 err = cxgb3i_conn_max_recv_dlength(conn);
687 break;
688 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
689 err = iscsi_set_param(cls_conn, param, buf, buflen);
690 if (!err)
691 err = cxgb3i_conn_max_xmit_dlength(conn);
692 break;
693 default:
694 return iscsi_set_param(cls_conn, param, buf, buflen);
696 return err;
700 * cxgb3i_host_set_param - configure host (adapter) related parameters
701 * @shost: scsi host pointer
702 * @param: parameter type identifier
703 * @buf: buffer pointer
705 static int cxgb3i_host_set_param(struct Scsi_Host *shost,
706 enum iscsi_host_param param,
707 char *buf, int buflen)
709 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
711 if (!hba->ndev) {
712 shost_printk(KERN_ERR, shost, "Could not set host param. "
713 "Netdev for host not set.\n");
714 return -ENODEV;
717 cxgb3i_api_debug("param %d, buf %s.\n", param, buf);
719 switch (param) {
720 case ISCSI_HOST_PARAM_IPADDRESS:
722 __be32 addr = in_aton(buf);
723 cxgb3i_set_private_ipv4addr(hba->ndev, addr);
724 return 0;
726 case ISCSI_HOST_PARAM_HWADDRESS:
727 case ISCSI_HOST_PARAM_NETDEV_NAME:
728 /* ignore */
729 return 0;
730 default:
731 return iscsi_host_set_param(shost, param, buf, buflen);
736 * cxgb3i_host_get_param - returns host (adapter) related parameters
737 * @shost: scsi host pointer
738 * @param: parameter type identifier
739 * @buf: buffer pointer
741 static int cxgb3i_host_get_param(struct Scsi_Host *shost,
742 enum iscsi_host_param param, char *buf)
744 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
745 int len = 0;
747 if (!hba->ndev) {
748 shost_printk(KERN_ERR, shost, "Could not set host param. "
749 "Netdev for host not set.\n");
750 return -ENODEV;
753 cxgb3i_api_debug("hba %s, param %d.\n", hba->ndev->name, param);
755 switch (param) {
756 case ISCSI_HOST_PARAM_HWADDRESS:
757 len = sysfs_format_mac(buf, hba->ndev->dev_addr, 6);
758 break;
759 case ISCSI_HOST_PARAM_NETDEV_NAME:
760 len = sprintf(buf, "%s\n", hba->ndev->name);
761 break;
762 case ISCSI_HOST_PARAM_IPADDRESS:
764 __be32 addr;
766 addr = cxgb3i_get_private_ipv4addr(hba->ndev);
767 len = sprintf(buf, "%pI4", &addr);
768 break;
770 default:
771 return iscsi_host_get_param(shost, param, buf);
773 return len;
777 * cxgb3i_conn_get_stats - returns iSCSI stats
778 * @cls_conn: pointer to iscsi cls conn
779 * @stats: pointer to iscsi statistic struct
781 static void cxgb3i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
782 struct iscsi_stats *stats)
784 struct iscsi_conn *conn = cls_conn->dd_data;
786 stats->txdata_octets = conn->txdata_octets;
787 stats->rxdata_octets = conn->rxdata_octets;
788 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
789 stats->dataout_pdus = conn->dataout_pdus_cnt;
790 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
791 stats->datain_pdus = conn->datain_pdus_cnt;
792 stats->r2t_pdus = conn->r2t_pdus_cnt;
793 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
794 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
795 stats->digest_err = 0;
796 stats->timeout_err = 0;
797 stats->custom_length = 1;
798 strcpy(stats->custom[0].desc, "eh_abort_cnt");
799 stats->custom[0].value = conn->eh_abort_cnt;
803 * cxgb3i_parse_itt - get the idx and age bits from a given tag
804 * @conn: iscsi connection
805 * @itt: itt tag
806 * @idx: task index, filled in by this function
807 * @age: session age, filled in by this function
809 static void cxgb3i_parse_itt(struct iscsi_conn *conn, itt_t itt,
810 int *idx, int *age)
812 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
813 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
814 struct cxgb3i_adapter *snic = cconn->hba->snic;
815 u32 tag = ntohl((__force u32) itt);
816 u32 sw_bits;
818 sw_bits = cxgb3i_tag_nonrsvd_bits(&snic->tag_format, tag);
819 if (idx)
820 *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
821 if (age)
822 *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
824 cxgb3i_tag_debug("parse tag 0x%x/0x%x, sw 0x%x, itt 0x%x, age 0x%x.\n",
825 tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
826 age ? *age : 0xFF);
830 * cxgb3i_reserve_itt - generate tag for a give task
831 * @task: iscsi task
832 * @hdr_itt: tag, filled in by this function
833 * Set up ddp for scsi read tasks if possible.
835 int cxgb3i_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
837 struct scsi_cmnd *sc = task->sc;
838 struct iscsi_conn *conn = task->conn;
839 struct iscsi_session *sess = conn->session;
840 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
841 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
842 struct cxgb3i_adapter *snic = cconn->hba->snic;
843 struct cxgb3i_tag_format *tformat = &snic->tag_format;
844 u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
845 u32 tag;
846 int err = -EINVAL;
848 if (sc &&
849 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
850 cxgb3i_sw_tag_usable(tformat, sw_tag)) {
851 struct s3_conn *c3cn = cconn->cep->c3cn;
852 struct cxgb3i_gather_list *gl;
854 gl = cxgb3i_ddp_make_gl(scsi_in(sc)->length,
855 scsi_in(sc)->table.sgl,
856 scsi_in(sc)->table.nents,
857 snic->pdev,
858 GFP_ATOMIC);
859 if (gl) {
860 tag = sw_tag;
861 err = cxgb3i_ddp_tag_reserve(snic->tdev, c3cn->tid,
862 tformat, &tag,
863 gl, GFP_ATOMIC);
864 if (err < 0)
865 cxgb3i_ddp_release_gl(gl, snic->pdev);
869 if (err < 0)
870 tag = cxgb3i_set_non_ddp_tag(tformat, sw_tag);
871 /* the itt need to sent in big-endian order */
872 *hdr_itt = (__force itt_t)htonl(tag);
874 cxgb3i_tag_debug("new tag 0x%x/0x%x (itt 0x%x, age 0x%x).\n",
875 tag, *hdr_itt, task->itt, sess->age);
876 return 0;
880 * cxgb3i_release_itt - release the tag for a given task
881 * @task: iscsi task
882 * @hdr_itt: tag
883 * If the tag is a ddp tag, release the ddp setup
885 void cxgb3i_release_itt(struct iscsi_task *task, itt_t hdr_itt)
887 struct scsi_cmnd *sc = task->sc;
888 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
889 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
890 struct cxgb3i_adapter *snic = cconn->hba->snic;
891 struct cxgb3i_tag_format *tformat = &snic->tag_format;
892 u32 tag = ntohl((__force u32)hdr_itt);
894 cxgb3i_tag_debug("release tag 0x%x.\n", tag);
896 if (sc &&
897 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
898 cxgb3i_is_ddp_tag(tformat, tag))
899 cxgb3i_ddp_tag_release(snic->tdev, tag);
903 * cxgb3i_host_template -- Scsi_Host_Template structure
904 * used when registering with the scsi mid layer
906 static struct scsi_host_template cxgb3i_host_template = {
907 .module = THIS_MODULE,
908 .name = "Chelsio S3xx iSCSI Initiator",
909 .proc_name = "cxgb3i",
910 .queuecommand = iscsi_queuecommand,
911 .change_queue_depth = iscsi_change_queue_depth,
912 .can_queue = CXGB3I_SCSI_HOST_QDEPTH,
913 .sg_tablesize = SG_ALL,
914 .max_sectors = 0xFFFF,
915 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
916 .eh_abort_handler = iscsi_eh_abort,
917 .eh_device_reset_handler = iscsi_eh_device_reset,
918 .eh_target_reset_handler = iscsi_eh_recover_target,
919 .target_alloc = iscsi_target_alloc,
920 .use_clustering = DISABLE_CLUSTERING,
921 .this_id = -1,
924 static struct iscsi_transport cxgb3i_iscsi_transport = {
925 .owner = THIS_MODULE,
926 .name = "cxgb3i",
927 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
928 | CAP_DATADGST | CAP_DIGEST_OFFLOAD |
929 CAP_PADDING_OFFLOAD,
930 .param_mask = ISCSI_MAX_RECV_DLENGTH |
931 ISCSI_MAX_XMIT_DLENGTH |
932 ISCSI_HDRDGST_EN |
933 ISCSI_DATADGST_EN |
934 ISCSI_INITIAL_R2T_EN |
935 ISCSI_MAX_R2T |
936 ISCSI_IMM_DATA_EN |
937 ISCSI_FIRST_BURST |
938 ISCSI_MAX_BURST |
939 ISCSI_PDU_INORDER_EN |
940 ISCSI_DATASEQ_INORDER_EN |
941 ISCSI_ERL |
942 ISCSI_CONN_PORT |
943 ISCSI_CONN_ADDRESS |
944 ISCSI_EXP_STATSN |
945 ISCSI_PERSISTENT_PORT |
946 ISCSI_PERSISTENT_ADDRESS |
947 ISCSI_TARGET_NAME | ISCSI_TPGT |
948 ISCSI_USERNAME | ISCSI_PASSWORD |
949 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
950 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
951 ISCSI_LU_RESET_TMO | ISCSI_TGT_RESET_TMO |
952 ISCSI_PING_TMO | ISCSI_RECV_TMO |
953 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
954 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
955 ISCSI_HOST_INITIATOR_NAME | ISCSI_HOST_NETDEV_NAME,
956 .get_host_param = cxgb3i_host_get_param,
957 .set_host_param = cxgb3i_host_set_param,
958 /* session management */
959 .create_session = cxgb3i_session_create,
960 .destroy_session = cxgb3i_session_destroy,
961 .get_session_param = iscsi_session_get_param,
962 /* connection management */
963 .create_conn = cxgb3i_conn_create,
964 .bind_conn = cxgb3i_conn_bind,
965 .destroy_conn = iscsi_tcp_conn_teardown,
966 .start_conn = iscsi_conn_start,
967 .stop_conn = iscsi_conn_stop,
968 .get_conn_param = cxgb3i_conn_get_param,
969 .set_param = cxgb3i_conn_set_param,
970 .get_stats = cxgb3i_conn_get_stats,
971 /* pdu xmit req. from user space */
972 .send_pdu = iscsi_conn_send_pdu,
973 /* task */
974 .init_task = iscsi_tcp_task_init,
975 .xmit_task = iscsi_tcp_task_xmit,
976 .cleanup_task = cxgb3i_conn_cleanup_task,
978 /* pdu */
979 .alloc_pdu = cxgb3i_conn_alloc_pdu,
980 .init_pdu = cxgb3i_conn_init_pdu,
981 .xmit_pdu = cxgb3i_conn_xmit_pdu,
982 .parse_pdu_itt = cxgb3i_parse_itt,
984 /* TCP connect/disconnect */
985 .ep_connect = cxgb3i_ep_connect,
986 .ep_poll = cxgb3i_ep_poll,
987 .ep_disconnect = cxgb3i_ep_disconnect,
988 /* Error recovery timeout call */
989 .session_recovery_timedout = iscsi_session_recovery_timedout,
992 int cxgb3i_iscsi_init(void)
994 sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
995 sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
996 cxgb3i_log_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
997 ISCSI_ITT_MASK, sw_tag_idx_bits,
998 ISCSI_AGE_MASK, sw_tag_age_bits);
1000 cxgb3i_scsi_transport =
1001 iscsi_register_transport(&cxgb3i_iscsi_transport);
1002 if (!cxgb3i_scsi_transport) {
1003 cxgb3i_log_error("Could not register cxgb3i transport.\n");
1004 return -ENODEV;
1006 cxgb3i_api_debug("cxgb3i transport 0x%p.\n", cxgb3i_scsi_transport);
1007 return 0;
1010 void cxgb3i_iscsi_cleanup(void)
1012 if (cxgb3i_scsi_transport) {
1013 cxgb3i_api_debug("cxgb3i transport 0x%p.\n",
1014 cxgb3i_scsi_transport);
1015 iscsi_unregister_transport(&cxgb3i_iscsi_transport);