various bugfixes
[cor_2_6_31.git] / drivers / scsi / cxgb3i / cxgb3i_iscsi.c
blobc399f485aa7d1228c415c703c85ceddafcbdc0cf
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,
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, NIPQUAD_FMT,
595 NIPQUAD(c3cn->daddr.sin_addr.s_addr));
596 conn->portal_port = ntohs(c3cn->daddr.sin_port);
597 spin_unlock_bh(&conn->session->lock);
599 /* init recv engine */
600 iscsi_tcp_hdr_recv_prep(tcp_conn);
602 return 0;
606 * cxgb3i_conn_get_param - return iscsi connection parameter to caller
607 * @cls_conn: pointer to iscsi cls conn
608 * @param: parameter type identifier
609 * @buf: buffer pointer
611 * returns iSCSI connection parameters
613 static int cxgb3i_conn_get_param(struct iscsi_cls_conn *cls_conn,
614 enum iscsi_param param, char *buf)
616 struct iscsi_conn *conn = cls_conn->dd_data;
617 int len;
619 cxgb3i_api_debug("cls_conn 0x%p, param %d.\n", cls_conn, param);
621 switch (param) {
622 case ISCSI_PARAM_CONN_PORT:
623 spin_lock_bh(&conn->session->lock);
624 len = sprintf(buf, "%hu\n", conn->portal_port);
625 spin_unlock_bh(&conn->session->lock);
626 break;
627 case ISCSI_PARAM_CONN_ADDRESS:
628 spin_lock_bh(&conn->session->lock);
629 len = sprintf(buf, "%s\n", conn->portal_address);
630 spin_unlock_bh(&conn->session->lock);
631 break;
632 default:
633 return iscsi_conn_get_param(cls_conn, param, buf);
636 return len;
640 * cxgb3i_conn_set_param - set iscsi connection parameter
641 * @cls_conn: pointer to iscsi cls conn
642 * @param: parameter type identifier
643 * @buf: buffer pointer
644 * @buflen: buffer length
646 * set iSCSI connection parameters
648 static int cxgb3i_conn_set_param(struct iscsi_cls_conn *cls_conn,
649 enum iscsi_param param, char *buf, int buflen)
651 struct iscsi_conn *conn = cls_conn->dd_data;
652 struct iscsi_session *session = conn->session;
653 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
654 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
655 struct cxgb3i_adapter *snic = cconn->hba->snic;
656 struct s3_conn *c3cn = cconn->cep->c3cn;
657 int value, err = 0;
659 switch (param) {
660 case ISCSI_PARAM_HDRDGST_EN:
661 err = iscsi_set_param(cls_conn, param, buf, buflen);
662 if (!err && conn->hdrdgst_en)
663 err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
664 conn->hdrdgst_en,
665 conn->datadgst_en, 0);
666 break;
667 case ISCSI_PARAM_DATADGST_EN:
668 err = iscsi_set_param(cls_conn, param, buf, buflen);
669 if (!err && conn->datadgst_en)
670 err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
671 conn->hdrdgst_en,
672 conn->datadgst_en, 0);
673 break;
674 case ISCSI_PARAM_MAX_R2T:
675 sscanf(buf, "%d", &value);
676 if (value <= 0 || !is_power_of_2(value))
677 return -EINVAL;
678 if (session->max_r2t == value)
679 break;
680 iscsi_tcp_r2tpool_free(session);
681 err = iscsi_set_param(cls_conn, param, buf, buflen);
682 if (!err && iscsi_tcp_r2tpool_alloc(session))
683 return -ENOMEM;
684 case ISCSI_PARAM_MAX_RECV_DLENGTH:
685 err = iscsi_set_param(cls_conn, param, buf, buflen);
686 if (!err)
687 err = cxgb3i_conn_max_recv_dlength(conn);
688 break;
689 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
690 err = iscsi_set_param(cls_conn, param, buf, buflen);
691 if (!err)
692 err = cxgb3i_conn_max_xmit_dlength(conn);
693 break;
694 default:
695 return iscsi_set_param(cls_conn, param, buf, buflen);
697 return err;
701 * cxgb3i_host_set_param - configure host (adapter) related parameters
702 * @shost: scsi host pointer
703 * @param: parameter type identifier
704 * @buf: buffer pointer
706 static int cxgb3i_host_set_param(struct Scsi_Host *shost,
707 enum iscsi_host_param param,
708 char *buf, int buflen)
710 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
712 cxgb3i_api_debug("param %d, buf %s.\n", param, buf);
714 switch (param) {
715 case ISCSI_HOST_PARAM_IPADDRESS:
717 __be32 addr = in_aton(buf);
718 cxgb3i_set_private_ipv4addr(hba->ndev, addr);
719 return 0;
721 case ISCSI_HOST_PARAM_HWADDRESS:
722 case ISCSI_HOST_PARAM_NETDEV_NAME:
723 /* ignore */
724 return 0;
725 default:
726 return iscsi_host_set_param(shost, param, buf, buflen);
731 * cxgb3i_host_get_param - returns host (adapter) related parameters
732 * @shost: scsi host pointer
733 * @param: parameter type identifier
734 * @buf: buffer pointer
736 static int cxgb3i_host_get_param(struct Scsi_Host *shost,
737 enum iscsi_host_param param, char *buf)
739 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
740 int len = 0;
742 cxgb3i_api_debug("hba %s, param %d.\n", hba->ndev->name, param);
744 switch (param) {
745 case ISCSI_HOST_PARAM_HWADDRESS:
746 len = sysfs_format_mac(buf, hba->ndev->dev_addr, 6);
747 break;
748 case ISCSI_HOST_PARAM_NETDEV_NAME:
749 len = sprintf(buf, "%s\n", hba->ndev->name);
750 break;
751 case ISCSI_HOST_PARAM_IPADDRESS:
753 __be32 addr;
755 addr = cxgb3i_get_private_ipv4addr(hba->ndev);
756 len = sprintf(buf, NIPQUAD_FMT, NIPQUAD(addr));
757 break;
759 default:
760 return iscsi_host_get_param(shost, param, buf);
762 return len;
766 * cxgb3i_conn_get_stats - returns iSCSI stats
767 * @cls_conn: pointer to iscsi cls conn
768 * @stats: pointer to iscsi statistic struct
770 static void cxgb3i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
771 struct iscsi_stats *stats)
773 struct iscsi_conn *conn = cls_conn->dd_data;
775 stats->txdata_octets = conn->txdata_octets;
776 stats->rxdata_octets = conn->rxdata_octets;
777 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
778 stats->dataout_pdus = conn->dataout_pdus_cnt;
779 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
780 stats->datain_pdus = conn->datain_pdus_cnt;
781 stats->r2t_pdus = conn->r2t_pdus_cnt;
782 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
783 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
784 stats->digest_err = 0;
785 stats->timeout_err = 0;
786 stats->custom_length = 1;
787 strcpy(stats->custom[0].desc, "eh_abort_cnt");
788 stats->custom[0].value = conn->eh_abort_cnt;
792 * cxgb3i_parse_itt - get the idx and age bits from a given tag
793 * @conn: iscsi connection
794 * @itt: itt tag
795 * @idx: task index, filled in by this function
796 * @age: session age, filled in by this function
798 static void cxgb3i_parse_itt(struct iscsi_conn *conn, itt_t itt,
799 int *idx, int *age)
801 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
802 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
803 struct cxgb3i_adapter *snic = cconn->hba->snic;
804 u32 tag = ntohl((__force u32) itt);
805 u32 sw_bits;
807 sw_bits = cxgb3i_tag_nonrsvd_bits(&snic->tag_format, tag);
808 if (idx)
809 *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
810 if (age)
811 *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
813 cxgb3i_tag_debug("parse tag 0x%x/0x%x, sw 0x%x, itt 0x%x, age 0x%x.\n",
814 tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
815 age ? *age : 0xFF);
819 * cxgb3i_reserve_itt - generate tag for a give task
820 * @task: iscsi task
821 * @hdr_itt: tag, filled in by this function
822 * Set up ddp for scsi read tasks if possible.
824 int cxgb3i_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
826 struct scsi_cmnd *sc = task->sc;
827 struct iscsi_conn *conn = task->conn;
828 struct iscsi_session *sess = conn->session;
829 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
830 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
831 struct cxgb3i_adapter *snic = cconn->hba->snic;
832 struct cxgb3i_tag_format *tformat = &snic->tag_format;
833 u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
834 u32 tag;
835 int err = -EINVAL;
837 if (sc &&
838 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
839 cxgb3i_sw_tag_usable(tformat, sw_tag)) {
840 struct s3_conn *c3cn = cconn->cep->c3cn;
841 struct cxgb3i_gather_list *gl;
843 gl = cxgb3i_ddp_make_gl(scsi_in(sc)->length,
844 scsi_in(sc)->table.sgl,
845 scsi_in(sc)->table.nents,
846 snic->pdev,
847 GFP_ATOMIC);
848 if (gl) {
849 tag = sw_tag;
850 err = cxgb3i_ddp_tag_reserve(snic->tdev, c3cn->tid,
851 tformat, &tag,
852 gl, GFP_ATOMIC);
853 if (err < 0)
854 cxgb3i_ddp_release_gl(gl, snic->pdev);
858 if (err < 0)
859 tag = cxgb3i_set_non_ddp_tag(tformat, sw_tag);
860 /* the itt need to sent in big-endian order */
861 *hdr_itt = (__force itt_t)htonl(tag);
863 cxgb3i_tag_debug("new tag 0x%x/0x%x (itt 0x%x, age 0x%x).\n",
864 tag, *hdr_itt, task->itt, sess->age);
865 return 0;
869 * cxgb3i_release_itt - release the tag for a given task
870 * @task: iscsi task
871 * @hdr_itt: tag
872 * If the tag is a ddp tag, release the ddp setup
874 void cxgb3i_release_itt(struct iscsi_task *task, itt_t hdr_itt)
876 struct scsi_cmnd *sc = task->sc;
877 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
878 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
879 struct cxgb3i_adapter *snic = cconn->hba->snic;
880 struct cxgb3i_tag_format *tformat = &snic->tag_format;
881 u32 tag = ntohl((__force u32)hdr_itt);
883 cxgb3i_tag_debug("release tag 0x%x.\n", tag);
885 if (sc &&
886 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
887 cxgb3i_is_ddp_tag(tformat, tag))
888 cxgb3i_ddp_tag_release(snic->tdev, tag);
892 * cxgb3i_host_template -- Scsi_Host_Template structure
893 * used when registering with the scsi mid layer
895 static struct scsi_host_template cxgb3i_host_template = {
896 .module = THIS_MODULE,
897 .name = "Chelsio S3xx iSCSI Initiator",
898 .proc_name = "cxgb3i",
899 .queuecommand = iscsi_queuecommand,
900 .change_queue_depth = iscsi_change_queue_depth,
901 .can_queue = CXGB3I_SCSI_HOST_QDEPTH,
902 .sg_tablesize = SG_ALL,
903 .max_sectors = 0xFFFF,
904 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
905 .eh_abort_handler = iscsi_eh_abort,
906 .eh_device_reset_handler = iscsi_eh_device_reset,
907 .eh_target_reset_handler = iscsi_eh_target_reset,
908 .target_alloc = iscsi_target_alloc,
909 .use_clustering = DISABLE_CLUSTERING,
910 .this_id = -1,
913 static struct iscsi_transport cxgb3i_iscsi_transport = {
914 .owner = THIS_MODULE,
915 .name = "cxgb3i",
916 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
917 | CAP_DATADGST | CAP_DIGEST_OFFLOAD |
918 CAP_PADDING_OFFLOAD,
919 .param_mask = ISCSI_MAX_RECV_DLENGTH |
920 ISCSI_MAX_XMIT_DLENGTH |
921 ISCSI_HDRDGST_EN |
922 ISCSI_DATADGST_EN |
923 ISCSI_INITIAL_R2T_EN |
924 ISCSI_MAX_R2T |
925 ISCSI_IMM_DATA_EN |
926 ISCSI_FIRST_BURST |
927 ISCSI_MAX_BURST |
928 ISCSI_PDU_INORDER_EN |
929 ISCSI_DATASEQ_INORDER_EN |
930 ISCSI_ERL |
931 ISCSI_CONN_PORT |
932 ISCSI_CONN_ADDRESS |
933 ISCSI_EXP_STATSN |
934 ISCSI_PERSISTENT_PORT |
935 ISCSI_PERSISTENT_ADDRESS |
936 ISCSI_TARGET_NAME | ISCSI_TPGT |
937 ISCSI_USERNAME | ISCSI_PASSWORD |
938 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
939 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
940 ISCSI_LU_RESET_TMO |
941 ISCSI_PING_TMO | ISCSI_RECV_TMO |
942 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
943 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
944 ISCSI_HOST_INITIATOR_NAME | ISCSI_HOST_NETDEV_NAME,
945 .get_host_param = cxgb3i_host_get_param,
946 .set_host_param = cxgb3i_host_set_param,
947 /* session management */
948 .create_session = cxgb3i_session_create,
949 .destroy_session = cxgb3i_session_destroy,
950 .get_session_param = iscsi_session_get_param,
951 /* connection management */
952 .create_conn = cxgb3i_conn_create,
953 .bind_conn = cxgb3i_conn_bind,
954 .destroy_conn = iscsi_tcp_conn_teardown,
955 .start_conn = iscsi_conn_start,
956 .stop_conn = iscsi_conn_stop,
957 .get_conn_param = cxgb3i_conn_get_param,
958 .set_param = cxgb3i_conn_set_param,
959 .get_stats = cxgb3i_conn_get_stats,
960 /* pdu xmit req. from user space */
961 .send_pdu = iscsi_conn_send_pdu,
962 /* task */
963 .init_task = iscsi_tcp_task_init,
964 .xmit_task = iscsi_tcp_task_xmit,
965 .cleanup_task = cxgb3i_conn_cleanup_task,
967 /* pdu */
968 .alloc_pdu = cxgb3i_conn_alloc_pdu,
969 .init_pdu = cxgb3i_conn_init_pdu,
970 .xmit_pdu = cxgb3i_conn_xmit_pdu,
971 .parse_pdu_itt = cxgb3i_parse_itt,
973 /* TCP connect/disconnect */
974 .ep_connect = cxgb3i_ep_connect,
975 .ep_poll = cxgb3i_ep_poll,
976 .ep_disconnect = cxgb3i_ep_disconnect,
977 /* Error recovery timeout call */
978 .session_recovery_timedout = iscsi_session_recovery_timedout,
981 int cxgb3i_iscsi_init(void)
983 sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
984 sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
985 cxgb3i_log_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
986 ISCSI_ITT_MASK, sw_tag_idx_bits,
987 ISCSI_AGE_MASK, sw_tag_age_bits);
989 cxgb3i_scsi_transport =
990 iscsi_register_transport(&cxgb3i_iscsi_transport);
991 if (!cxgb3i_scsi_transport) {
992 cxgb3i_log_error("Could not register cxgb3i transport.\n");
993 return -ENODEV;
995 cxgb3i_api_debug("cxgb3i transport 0x%p.\n", cxgb3i_scsi_transport);
996 return 0;
999 void cxgb3i_iscsi_cleanup(void)
1001 if (cxgb3i_scsi_transport) {
1002 cxgb3i_api_debug("cxgb3i transport 0x%p.\n",
1003 cxgb3i_scsi_transport);
1004 iscsi_unregister_transport(&cxgb3i_iscsi_transport);