blob: ff6bfd66733f06420700240015b5f8802bc417cf [file] [log] [blame]
Karen Xiec3673462008-12-09 14:15:32 -08001/* cxgb3i_iscsi.c: Chelsio S3xx iSCSI driver.
2 *
3 * Copyright (c) 2008 Chelsio Communications, Inc.
4 * Copyright (c) 2008 Mike Christie
5 * Copyright (c) 2008 Red Hat, Inc. All rights reserved.
6 *
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.
10 *
11 * Written by: Karen Xie (kxie@chelsio.com)
12 */
13
14#include <linux/inet.h>
15#include <linux/crypto.h>
16#include <net/tcp.h>
17#include <scsi/scsi_cmnd.h>
18#include <scsi/scsi_device.h>
19#include <scsi/scsi_eh.h>
20#include <scsi/scsi_host.h>
21#include <scsi/scsi.h>
22#include <scsi/iscsi_proto.h>
23#include <scsi/libiscsi.h>
24#include <scsi/scsi_transport_iscsi.h>
25
26#include "cxgb3i.h"
27#include "cxgb3i_pdu.h"
28
29#ifdef __DEBUG_CXGB3I_TAG__
30#define cxgb3i_tag_debug cxgb3i_log_debug
31#else
32#define cxgb3i_tag_debug(fmt...)
33#endif
34
35#ifdef __DEBUG_CXGB3I_API__
36#define cxgb3i_api_debug cxgb3i_log_debug
37#else
38#define cxgb3i_api_debug(fmt...)
39#endif
40
41/*
42 * align pdu size to multiple of 512 for better performance
43 */
44#define align_pdu_size(n) do { n = (n) & (~511); } while (0)
45
46static struct scsi_transport_template *cxgb3i_scsi_transport;
47static struct scsi_host_template cxgb3i_host_template;
48static struct iscsi_transport cxgb3i_iscsi_transport;
49static unsigned char sw_tag_idx_bits;
50static unsigned char sw_tag_age_bits;
51
52static LIST_HEAD(cxgb3i_snic_list);
53static DEFINE_RWLOCK(cxgb3i_snic_rwlock);
54
55/**
Karen Xie515f1c82009-04-01 13:11:23 -050056 * cxgb3i_adpater_find_by_tdev - find the cxgb3i_adapter structure via t3cdev
57 * @tdev: t3cdev pointer
58 */
59struct cxgb3i_adapter *cxgb3i_adapter_find_by_tdev(struct t3cdev *tdev)
60{
61 struct cxgb3i_adapter *snic;
62
63 read_lock(&cxgb3i_snic_rwlock);
64 list_for_each_entry(snic, &cxgb3i_snic_list, list_head) {
65 if (snic->tdev == tdev) {
66 read_unlock(&cxgb3i_snic_rwlock);
67 return snic;
68 }
69 }
70 read_unlock(&cxgb3i_snic_rwlock);
71 return NULL;
72}
73
74/**
75 * cxgb3i_adapter_open - init a s3 adapter structure and any h/w settings
Karen Xiec3673462008-12-09 14:15:32 -080076 * @t3dev: t3cdev adapter
77 * return the resulting cxgb3i_adapter struct
78 */
Karen Xie515f1c82009-04-01 13:11:23 -050079struct cxgb3i_adapter *cxgb3i_adapter_open(struct t3cdev *t3dev)
Karen Xiec3673462008-12-09 14:15:32 -080080{
81 struct cxgb3i_adapter *snic;
82 struct adapter *adapter = tdev2adap(t3dev);
83 int i;
84
85 snic = kzalloc(sizeof(*snic), GFP_KERNEL);
86 if (!snic) {
87 cxgb3i_api_debug("cxgb3 %s, OOM.\n", t3dev->name);
88 return NULL;
89 }
90 spin_lock_init(&snic->lock);
91
92 snic->tdev = t3dev;
93 snic->pdev = adapter->pdev;
94 snic->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
95
96 if (cxgb3i_adapter_ddp_init(t3dev, &snic->tag_format,
97 &snic->tx_max_size,
98 &snic->rx_max_size) < 0)
99 goto free_snic;
100
101 for_each_port(adapter, i) {
102 snic->hba[i] = cxgb3i_hba_host_add(snic, adapter->port[i]);
103 if (!snic->hba[i])
104 goto ulp_cleanup;
105 }
106 snic->hba_cnt = adapter->params.nports;
107
108 /* add to the list */
109 write_lock(&cxgb3i_snic_rwlock);
110 list_add_tail(&snic->list_head, &cxgb3i_snic_list);
111 write_unlock(&cxgb3i_snic_rwlock);
112
113 return snic;
114
115ulp_cleanup:
116 cxgb3i_adapter_ddp_cleanup(t3dev);
117free_snic:
118 kfree(snic);
119 return NULL;
120}
121
122/**
Karen Xie515f1c82009-04-01 13:11:23 -0500123 * cxgb3i_adapter_close - release the resources held and cleanup h/w settings
Karen Xiec3673462008-12-09 14:15:32 -0800124 * @t3dev: t3cdev adapter
125 */
Karen Xie515f1c82009-04-01 13:11:23 -0500126void cxgb3i_adapter_close(struct t3cdev *t3dev)
Karen Xiec3673462008-12-09 14:15:32 -0800127{
128 int i;
129 struct cxgb3i_adapter *snic;
130
131 /* remove from the list */
132 write_lock(&cxgb3i_snic_rwlock);
133 list_for_each_entry(snic, &cxgb3i_snic_list, list_head) {
134 if (snic->tdev == t3dev) {
135 list_del(&snic->list_head);
136 break;
137 }
138 }
139 write_unlock(&cxgb3i_snic_rwlock);
140
141 if (snic) {
142 for (i = 0; i < snic->hba_cnt; i++) {
143 if (snic->hba[i]) {
144 cxgb3i_hba_host_remove(snic->hba[i]);
145 snic->hba[i] = NULL;
146 }
147 }
148
149 /* release ddp resources */
150 cxgb3i_adapter_ddp_cleanup(snic->tdev);
151 kfree(snic);
152 }
153}
154
155/**
Karen Xie154229a2009-03-05 14:46:08 -0600156 * cxgb3i_hba_find_by_netdev - find the cxgb3i_hba structure via net_device
Karen Xiec3673462008-12-09 14:15:32 -0800157 * @t3dev: t3cdev adapter
158 */
159struct cxgb3i_hba *cxgb3i_hba_find_by_netdev(struct net_device *ndev)
160{
161 struct cxgb3i_adapter *snic;
162 int i;
163
164 read_lock(&cxgb3i_snic_rwlock);
165 list_for_each_entry(snic, &cxgb3i_snic_list, list_head) {
166 for (i = 0; i < snic->hba_cnt; i++) {
167 if (snic->hba[i]->ndev == ndev) {
168 read_unlock(&cxgb3i_snic_rwlock);
169 return snic->hba[i];
170 }
171 }
172 }
173 read_unlock(&cxgb3i_snic_rwlock);
174 return NULL;
175}
176
177/**
178 * cxgb3i_hba_host_add - register a new host with scsi/iscsi
179 * @snic: the cxgb3i adapter
180 * @ndev: associated net_device
181 */
182struct cxgb3i_hba *cxgb3i_hba_host_add(struct cxgb3i_adapter *snic,
183 struct net_device *ndev)
184{
185 struct cxgb3i_hba *hba;
186 struct Scsi_Host *shost;
187 int err;
188
189 shost = iscsi_host_alloc(&cxgb3i_host_template,
Mike Christie4d108352009-03-05 14:46:04 -0600190 sizeof(struct cxgb3i_hba), 1);
Karen Xiec3673462008-12-09 14:15:32 -0800191 if (!shost) {
192 cxgb3i_log_info("iscsi_host_alloc failed.\n");
193 return NULL;
194 }
195
196 shost->transportt = cxgb3i_scsi_transport;
197 shost->max_lun = CXGB3I_MAX_LUN;
198 shost->max_id = CXGB3I_MAX_TARGET;
199 shost->max_channel = 0;
200 shost->max_cmd_len = 16;
201
202 hba = iscsi_host_priv(shost);
203 hba->snic = snic;
204 hba->ndev = ndev;
205 hba->shost = shost;
206
207 pci_dev_get(snic->pdev);
208 err = iscsi_host_add(shost, &snic->pdev->dev);
209 if (err) {
210 cxgb3i_log_info("iscsi_host_add failed.\n");
211 goto pci_dev_put;
212 }
213
214 cxgb3i_api_debug("shost 0x%p, hba 0x%p, no %u.\n",
215 shost, hba, shost->host_no);
216
217 return hba;
218
219pci_dev_put:
220 pci_dev_put(snic->pdev);
221 scsi_host_put(shost);
222 return NULL;
223}
224
225/**
226 * cxgb3i_hba_host_remove - de-register the host with scsi/iscsi
227 * @hba: the cxgb3i hba
228 */
229void cxgb3i_hba_host_remove(struct cxgb3i_hba *hba)
230{
231 cxgb3i_api_debug("shost 0x%p, hba 0x%p, no %u.\n",
232 hba->shost, hba, hba->shost->host_no);
233 iscsi_host_remove(hba->shost);
234 pci_dev_put(hba->snic->pdev);
235 iscsi_host_free(hba->shost);
236}
237
238/**
239 * cxgb3i_ep_connect - establish TCP connection to target portal
240 * @dst_addr: target IP address
241 * @non_blocking: blocking or non-blocking call
242 *
243 * Initiates a TCP/IP connection to the dst_addr
244 */
245static struct iscsi_endpoint *cxgb3i_ep_connect(struct sockaddr *dst_addr,
246 int non_blocking)
247{
248 struct iscsi_endpoint *ep;
249 struct cxgb3i_endpoint *cep;
250 struct cxgb3i_hba *hba;
251 struct s3_conn *c3cn = NULL;
252 int err = 0;
253
254 c3cn = cxgb3i_c3cn_create();
255 if (!c3cn) {
256 cxgb3i_log_info("ep connect OOM.\n");
257 err = -ENOMEM;
258 goto release_conn;
259 }
260
261 err = cxgb3i_c3cn_connect(c3cn, (struct sockaddr_in *)dst_addr);
262 if (err < 0) {
263 cxgb3i_log_info("ep connect failed.\n");
264 goto release_conn;
265 }
266 hba = cxgb3i_hba_find_by_netdev(c3cn->dst_cache->dev);
267 if (!hba) {
268 err = -ENOSPC;
269 cxgb3i_log_info("NOT going through cxgbi device.\n");
270 goto release_conn;
271 }
272 if (c3cn_is_closing(c3cn)) {
273 err = -ENOSPC;
274 cxgb3i_log_info("ep connect unable to connect.\n");
275 goto release_conn;
276 }
277
278 ep = iscsi_create_endpoint(sizeof(*cep));
279 if (!ep) {
280 err = -ENOMEM;
281 cxgb3i_log_info("iscsi alloc ep, OOM.\n");
282 goto release_conn;
283 }
284 cep = ep->dd_data;
285 cep->c3cn = c3cn;
286 cep->hba = hba;
287
288 cxgb3i_api_debug("ep 0x%p, 0x%p, c3cn 0x%p, hba 0x%p.\n",
289 ep, cep, c3cn, hba);
290 return ep;
291
292release_conn:
293 cxgb3i_api_debug("conn 0x%p failed, release.\n", c3cn);
294 if (c3cn)
295 cxgb3i_c3cn_release(c3cn);
296 return ERR_PTR(err);
297}
298
299/**
300 * cxgb3i_ep_poll - polls for TCP connection establishement
301 * @ep: TCP connection (endpoint) handle
302 * @timeout_ms: timeout value in milli secs
303 *
304 * polls for TCP connect request to complete
305 */
306static int cxgb3i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
307{
308 struct cxgb3i_endpoint *cep = ep->dd_data;
309 struct s3_conn *c3cn = cep->c3cn;
310
311 if (!c3cn_is_established(c3cn))
312 return 0;
313 cxgb3i_api_debug("ep 0x%p, c3cn 0x%p established.\n", ep, c3cn);
314 return 1;
315}
316
317/**
318 * cxgb3i_ep_disconnect - teardown TCP connection
319 * @ep: TCP connection (endpoint) handle
320 *
321 * teardown TCP connection
322 */
323static void cxgb3i_ep_disconnect(struct iscsi_endpoint *ep)
324{
325 struct cxgb3i_endpoint *cep = ep->dd_data;
326 struct cxgb3i_conn *cconn = cep->cconn;
327
328 cxgb3i_api_debug("ep 0x%p, cep 0x%p.\n", ep, cep);
329
330 if (cconn && cconn->conn) {
331 /*
332 * stop the xmit path so the xmit_pdu function is
333 * not being called
334 */
335 iscsi_suspend_tx(cconn->conn);
336
337 write_lock_bh(&cep->c3cn->callback_lock);
338 cep->c3cn->user_data = NULL;
339 cconn->cep = NULL;
340 write_unlock_bh(&cep->c3cn->callback_lock);
341 }
342
343 cxgb3i_api_debug("ep 0x%p, cep 0x%p, release c3cn 0x%p.\n",
344 ep, cep, cep->c3cn);
345 cxgb3i_c3cn_release(cep->c3cn);
346 iscsi_destroy_endpoint(ep);
347}
348
349/**
350 * cxgb3i_session_create - create a new iscsi session
351 * @cmds_max: max # of commands
352 * @qdepth: scsi queue depth
353 * @initial_cmdsn: initial iscsi CMDSN for this session
Karen Xiec3673462008-12-09 14:15:32 -0800354 *
355 * Creates a new iSCSI session
356 */
357static struct iscsi_cls_session *
358cxgb3i_session_create(struct iscsi_endpoint *ep, u16 cmds_max, u16 qdepth,
Mike Christie5e7facb2009-03-05 14:46:06 -0600359 u32 initial_cmdsn)
Karen Xiec3673462008-12-09 14:15:32 -0800360{
361 struct cxgb3i_endpoint *cep;
362 struct cxgb3i_hba *hba;
363 struct Scsi_Host *shost;
364 struct iscsi_cls_session *cls_session;
365 struct iscsi_session *session;
366
367 if (!ep) {
368 cxgb3i_log_error("%s, missing endpoint.\n", __func__);
369 return NULL;
370 }
371
372 cep = ep->dd_data;
373 hba = cep->hba;
374 shost = hba->shost;
375 cxgb3i_api_debug("ep 0x%p, cep 0x%p, hba 0x%p.\n", ep, cep, hba);
376 BUG_ON(hba != iscsi_host_priv(shost));
377
Karen Xiec3673462008-12-09 14:15:32 -0800378 cls_session = iscsi_session_setup(&cxgb3i_iscsi_transport, shost,
379 cmds_max,
Karen Xie949847d2009-02-13 21:38:49 -0800380 sizeof(struct iscsi_tcp_task) +
381 sizeof(struct cxgb3i_task_data),
Karen Xiec3673462008-12-09 14:15:32 -0800382 initial_cmdsn, ISCSI_MAX_TARGET);
383 if (!cls_session)
384 return NULL;
385 session = cls_session->dd_data;
386 if (iscsi_tcp_r2tpool_alloc(session))
387 goto remove_session;
388
389 return cls_session;
390
391remove_session:
392 iscsi_session_teardown(cls_session);
393 return NULL;
394}
395
396/**
397 * cxgb3i_session_destroy - destroys iscsi session
398 * @cls_session: pointer to iscsi cls session
399 *
400 * Destroys an iSCSI session instance and releases its all resources held
401 */
402static void cxgb3i_session_destroy(struct iscsi_cls_session *cls_session)
403{
404 cxgb3i_api_debug("sess 0x%p.\n", cls_session);
405 iscsi_tcp_r2tpool_free(cls_session->dd_data);
406 iscsi_session_teardown(cls_session);
407}
408
409/**
Karen Xie154229a2009-03-05 14:46:08 -0600410 * cxgb3i_conn_max_xmit_dlength -- calc the max. xmit pdu segment size
Karen Xiec3673462008-12-09 14:15:32 -0800411 * @conn: iscsi connection
Karen Xie154229a2009-03-05 14:46:08 -0600412 * check the max. xmit pdu payload, reduce it if needed
Karen Xiec3673462008-12-09 14:15:32 -0800413 */
414static inline int cxgb3i_conn_max_xmit_dlength(struct iscsi_conn *conn)
415
416{
417 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
418 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
Karen Xief62d0892009-02-13 21:38:54 -0800419 unsigned int max = max(512 * MAX_SKB_FRAGS, SKB_TX_HEADROOM);
Karen Xiec3673462008-12-09 14:15:32 -0800420
Karen Xief62d0892009-02-13 21:38:54 -0800421 max = min(cconn->hba->snic->tx_max_size, max);
Karen Xiec3673462008-12-09 14:15:32 -0800422 if (conn->max_xmit_dlength)
Karen Xief62d0892009-02-13 21:38:54 -0800423 conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
Karen Xiec3673462008-12-09 14:15:32 -0800424 else
425 conn->max_xmit_dlength = max;
426 align_pdu_size(conn->max_xmit_dlength);
Karen Xief62d0892009-02-13 21:38:54 -0800427 cxgb3i_api_debug("conn 0x%p, max xmit %u.\n",
Karen Xiec3673462008-12-09 14:15:32 -0800428 conn, conn->max_xmit_dlength);
429 return 0;
430}
431
432/**
Karen Xie154229a2009-03-05 14:46:08 -0600433 * cxgb3i_conn_max_recv_dlength -- check the max. recv pdu segment size
Karen Xiec3673462008-12-09 14:15:32 -0800434 * @conn: iscsi connection
435 * return 0 if the value is valid, < 0 otherwise.
436 */
437static inline int cxgb3i_conn_max_recv_dlength(struct iscsi_conn *conn)
438{
439 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
440 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
Karen Xief62d0892009-02-13 21:38:54 -0800441 unsigned int max = cconn->hba->snic->rx_max_size;
Karen Xiec3673462008-12-09 14:15:32 -0800442
443 align_pdu_size(max);
444 if (conn->max_recv_dlength) {
445 if (conn->max_recv_dlength > max) {
446 cxgb3i_log_error("MaxRecvDataSegmentLength %u too big."
447 " Need to be <= %u.\n",
448 conn->max_recv_dlength, max);
449 return -EINVAL;
450 }
Karen Xief62d0892009-02-13 21:38:54 -0800451 conn->max_recv_dlength = min(conn->max_recv_dlength, max);
Karen Xiec3673462008-12-09 14:15:32 -0800452 align_pdu_size(conn->max_recv_dlength);
453 } else
454 conn->max_recv_dlength = max;
455 cxgb3i_api_debug("conn 0x%p, max recv %u.\n",
456 conn, conn->max_recv_dlength);
457 return 0;
458}
459
460/**
461 * cxgb3i_conn_create - create iscsi connection instance
462 * @cls_session: pointer to iscsi cls session
463 * @cid: iscsi cid
464 *
465 * Creates a new iSCSI connection instance for a given session
466 */
467static struct iscsi_cls_conn *cxgb3i_conn_create(struct iscsi_cls_session
468 *cls_session, u32 cid)
469{
470 struct iscsi_cls_conn *cls_conn;
471 struct iscsi_conn *conn;
472 struct iscsi_tcp_conn *tcp_conn;
473 struct cxgb3i_conn *cconn;
474
475 cxgb3i_api_debug("sess 0x%p, cid %u.\n", cls_session, cid);
476
477 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
478 if (!cls_conn)
479 return NULL;
480 conn = cls_conn->dd_data;
481 tcp_conn = conn->dd_data;
482 cconn = tcp_conn->dd_data;
483
484 cconn->conn = conn;
485 return cls_conn;
486}
487
488/**
489 * cxgb3i_conn_bind - binds iscsi sess, conn and endpoint together
490 * @cls_session: pointer to iscsi cls session
491 * @cls_conn: pointer to iscsi cls conn
492 * @transport_eph: 64-bit EP handle
493 * @is_leading: leading connection on this session?
494 *
495 * Binds together an iSCSI session, an iSCSI connection and a
496 * TCP connection. This routine returns error code if the TCP
497 * connection does not belong on the device iSCSI sess/conn is bound
498 */
499
500static int cxgb3i_conn_bind(struct iscsi_cls_session *cls_session,
501 struct iscsi_cls_conn *cls_conn,
502 u64 transport_eph, int is_leading)
503{
504 struct iscsi_conn *conn = cls_conn->dd_data;
505 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
506 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
507 struct cxgb3i_adapter *snic;
508 struct iscsi_endpoint *ep;
509 struct cxgb3i_endpoint *cep;
510 struct s3_conn *c3cn;
511 int err;
512
513 ep = iscsi_lookup_endpoint(transport_eph);
514 if (!ep)
515 return -EINVAL;
516
517 /* setup ddp pagesize */
518 cep = ep->dd_data;
519 c3cn = cep->c3cn;
520 snic = cep->hba->snic;
521 err = cxgb3i_setup_conn_host_pagesize(snic->tdev, c3cn->tid, 0);
522 if (err < 0)
523 return err;
524
525 cxgb3i_api_debug("ep 0x%p, cls sess 0x%p, cls conn 0x%p.\n",
526 ep, cls_session, cls_conn);
527
528 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
529 if (err)
530 return -EINVAL;
531
532 /* calculate the tag idx bits needed for this conn based on cmds_max */
533 cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
534 cxgb3i_api_debug("session cmds_max 0x%x, bits %u.\n",
535 conn->session->cmds_max, cconn->task_idx_bits);
536
537 read_lock(&c3cn->callback_lock);
538 c3cn->user_data = conn;
539 cconn->hba = cep->hba;
540 cconn->cep = cep;
541 cep->cconn = cconn;
542 read_unlock(&c3cn->callback_lock);
543
544 cxgb3i_conn_max_xmit_dlength(conn);
545 cxgb3i_conn_max_recv_dlength(conn);
546
547 spin_lock_bh(&conn->session->lock);
548 sprintf(conn->portal_address, NIPQUAD_FMT,
549 NIPQUAD(c3cn->daddr.sin_addr.s_addr));
550 conn->portal_port = ntohs(c3cn->daddr.sin_port);
551 spin_unlock_bh(&conn->session->lock);
552
553 /* init recv engine */
554 iscsi_tcp_hdr_recv_prep(tcp_conn);
555
556 return 0;
557}
558
559/**
560 * cxgb3i_conn_get_param - return iscsi connection parameter to caller
561 * @cls_conn: pointer to iscsi cls conn
562 * @param: parameter type identifier
563 * @buf: buffer pointer
564 *
565 * returns iSCSI connection parameters
566 */
567static int cxgb3i_conn_get_param(struct iscsi_cls_conn *cls_conn,
568 enum iscsi_param param, char *buf)
569{
570 struct iscsi_conn *conn = cls_conn->dd_data;
571 int len;
572
573 cxgb3i_api_debug("cls_conn 0x%p, param %d.\n", cls_conn, param);
574
575 switch (param) {
576 case ISCSI_PARAM_CONN_PORT:
577 spin_lock_bh(&conn->session->lock);
578 len = sprintf(buf, "%hu\n", conn->portal_port);
579 spin_unlock_bh(&conn->session->lock);
580 break;
581 case ISCSI_PARAM_CONN_ADDRESS:
582 spin_lock_bh(&conn->session->lock);
583 len = sprintf(buf, "%s\n", conn->portal_address);
584 spin_unlock_bh(&conn->session->lock);
585 break;
586 default:
587 return iscsi_conn_get_param(cls_conn, param, buf);
588 }
589
590 return len;
591}
592
593/**
594 * cxgb3i_conn_set_param - set iscsi connection parameter
595 * @cls_conn: pointer to iscsi cls conn
596 * @param: parameter type identifier
597 * @buf: buffer pointer
598 * @buflen: buffer length
599 *
600 * set iSCSI connection parameters
601 */
602static int cxgb3i_conn_set_param(struct iscsi_cls_conn *cls_conn,
603 enum iscsi_param param, char *buf, int buflen)
604{
605 struct iscsi_conn *conn = cls_conn->dd_data;
606 struct iscsi_session *session = conn->session;
607 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
608 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
609 struct cxgb3i_adapter *snic = cconn->hba->snic;
610 struct s3_conn *c3cn = cconn->cep->c3cn;
611 int value, err = 0;
612
613 switch (param) {
614 case ISCSI_PARAM_HDRDGST_EN:
615 err = iscsi_set_param(cls_conn, param, buf, buflen);
616 if (!err && conn->hdrdgst_en)
617 err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
618 conn->hdrdgst_en,
619 conn->datadgst_en, 0);
620 break;
621 case ISCSI_PARAM_DATADGST_EN:
622 err = iscsi_set_param(cls_conn, param, buf, buflen);
623 if (!err && conn->datadgst_en)
624 err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
625 conn->hdrdgst_en,
626 conn->datadgst_en, 0);
627 break;
628 case ISCSI_PARAM_MAX_R2T:
629 sscanf(buf, "%d", &value);
630 if (value <= 0 || !is_power_of_2(value))
631 return -EINVAL;
632 if (session->max_r2t == value)
633 break;
634 iscsi_tcp_r2tpool_free(session);
635 err = iscsi_set_param(cls_conn, param, buf, buflen);
636 if (!err && iscsi_tcp_r2tpool_alloc(session))
637 return -ENOMEM;
638 case ISCSI_PARAM_MAX_RECV_DLENGTH:
639 err = iscsi_set_param(cls_conn, param, buf, buflen);
640 if (!err)
641 err = cxgb3i_conn_max_recv_dlength(conn);
642 break;
643 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
644 err = iscsi_set_param(cls_conn, param, buf, buflen);
645 if (!err)
646 err = cxgb3i_conn_max_xmit_dlength(conn);
647 break;
648 default:
649 return iscsi_set_param(cls_conn, param, buf, buflen);
650 }
651 return err;
652}
653
654/**
655 * cxgb3i_host_set_param - configure host (adapter) related parameters
656 * @shost: scsi host pointer
657 * @param: parameter type identifier
658 * @buf: buffer pointer
659 */
660static int cxgb3i_host_set_param(struct Scsi_Host *shost,
661 enum iscsi_host_param param,
662 char *buf, int buflen)
663{
664 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
665
666 cxgb3i_api_debug("param %d, buf %s.\n", param, buf);
667
668 switch (param) {
669 case ISCSI_HOST_PARAM_IPADDRESS:
670 {
671 __be32 addr = in_aton(buf);
672 cxgb3i_set_private_ipv4addr(hba->ndev, addr);
673 return 0;
674 }
675 case ISCSI_HOST_PARAM_HWADDRESS:
676 case ISCSI_HOST_PARAM_NETDEV_NAME:
677 /* ignore */
678 return 0;
679 default:
680 return iscsi_host_set_param(shost, param, buf, buflen);
681 }
682}
683
684/**
685 * cxgb3i_host_get_param - returns host (adapter) related parameters
686 * @shost: scsi host pointer
687 * @param: parameter type identifier
688 * @buf: buffer pointer
689 */
690static int cxgb3i_host_get_param(struct Scsi_Host *shost,
691 enum iscsi_host_param param, char *buf)
692{
693 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
694 int len = 0;
695
696 cxgb3i_api_debug("hba %s, param %d.\n", hba->ndev->name, param);
697
698 switch (param) {
699 case ISCSI_HOST_PARAM_HWADDRESS:
700 len = sysfs_format_mac(buf, hba->ndev->dev_addr, 6);
701 break;
702 case ISCSI_HOST_PARAM_NETDEV_NAME:
703 len = sprintf(buf, "%s\n", hba->ndev->name);
704 break;
705 case ISCSI_HOST_PARAM_IPADDRESS:
706 {
707 __be32 addr;
708
709 addr = cxgb3i_get_private_ipv4addr(hba->ndev);
710 len = sprintf(buf, NIPQUAD_FMT, NIPQUAD(addr));
711 break;
712 }
713 default:
714 return iscsi_host_get_param(shost, param, buf);
715 }
716 return len;
717}
718
719/**
720 * cxgb3i_conn_get_stats - returns iSCSI stats
721 * @cls_conn: pointer to iscsi cls conn
722 * @stats: pointer to iscsi statistic struct
723 */
724static void cxgb3i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
725 struct iscsi_stats *stats)
726{
727 struct iscsi_conn *conn = cls_conn->dd_data;
728
729 stats->txdata_octets = conn->txdata_octets;
730 stats->rxdata_octets = conn->rxdata_octets;
731 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
732 stats->dataout_pdus = conn->dataout_pdus_cnt;
733 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
734 stats->datain_pdus = conn->datain_pdus_cnt;
735 stats->r2t_pdus = conn->r2t_pdus_cnt;
736 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
737 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
738 stats->digest_err = 0;
739 stats->timeout_err = 0;
740 stats->custom_length = 1;
741 strcpy(stats->custom[0].desc, "eh_abort_cnt");
742 stats->custom[0].value = conn->eh_abort_cnt;
743}
744
745/**
746 * cxgb3i_parse_itt - get the idx and age bits from a given tag
747 * @conn: iscsi connection
748 * @itt: itt tag
749 * @idx: task index, filled in by this function
750 * @age: session age, filled in by this function
751 */
752static void cxgb3i_parse_itt(struct iscsi_conn *conn, itt_t itt,
753 int *idx, int *age)
754{
755 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
756 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
757 struct cxgb3i_adapter *snic = cconn->hba->snic;
758 u32 tag = ntohl((__force u32) itt);
759 u32 sw_bits;
760
761 sw_bits = cxgb3i_tag_nonrsvd_bits(&snic->tag_format, tag);
762 if (idx)
763 *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
764 if (age)
765 *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
766
767 cxgb3i_tag_debug("parse tag 0x%x/0x%x, sw 0x%x, itt 0x%x, age 0x%x.\n",
768 tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
769 age ? *age : 0xFF);
770}
771
772/**
773 * cxgb3i_reserve_itt - generate tag for a give task
Karen Xiec3673462008-12-09 14:15:32 -0800774 * @task: iscsi task
775 * @hdr_itt: tag, filled in by this function
Karen Xie154229a2009-03-05 14:46:08 -0600776 * Set up ddp for scsi read tasks if possible.
Karen Xiec3673462008-12-09 14:15:32 -0800777 */
778int cxgb3i_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
779{
780 struct scsi_cmnd *sc = task->sc;
781 struct iscsi_conn *conn = task->conn;
782 struct iscsi_session *sess = conn->session;
783 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
784 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
785 struct cxgb3i_adapter *snic = cconn->hba->snic;
786 struct cxgb3i_tag_format *tformat = &snic->tag_format;
787 u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
788 u32 tag;
789 int err = -EINVAL;
790
791 if (sc &&
792 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
793 cxgb3i_sw_tag_usable(tformat, sw_tag)) {
794 struct s3_conn *c3cn = cconn->cep->c3cn;
795 struct cxgb3i_gather_list *gl;
796
797 gl = cxgb3i_ddp_make_gl(scsi_in(sc)->length,
798 scsi_in(sc)->table.sgl,
799 scsi_in(sc)->table.nents,
800 snic->pdev,
801 GFP_ATOMIC);
802 if (gl) {
803 tag = sw_tag;
804 err = cxgb3i_ddp_tag_reserve(snic->tdev, c3cn->tid,
805 tformat, &tag,
806 gl, GFP_ATOMIC);
807 if (err < 0)
808 cxgb3i_ddp_release_gl(gl, snic->pdev);
809 }
810 }
811
812 if (err < 0)
813 tag = cxgb3i_set_non_ddp_tag(tformat, sw_tag);
814 /* the itt need to sent in big-endian order */
815 *hdr_itt = (__force itt_t)htonl(tag);
816
817 cxgb3i_tag_debug("new tag 0x%x/0x%x (itt 0x%x, age 0x%x).\n",
818 tag, *hdr_itt, task->itt, sess->age);
819 return 0;
820}
821
822/**
823 * cxgb3i_release_itt - release the tag for a given task
Karen Xiec3673462008-12-09 14:15:32 -0800824 * @task: iscsi task
825 * @hdr_itt: tag
Karen Xie154229a2009-03-05 14:46:08 -0600826 * If the tag is a ddp tag, release the ddp setup
Karen Xiec3673462008-12-09 14:15:32 -0800827 */
828void cxgb3i_release_itt(struct iscsi_task *task, itt_t hdr_itt)
829{
830 struct scsi_cmnd *sc = task->sc;
831 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
832 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
833 struct cxgb3i_adapter *snic = cconn->hba->snic;
834 struct cxgb3i_tag_format *tformat = &snic->tag_format;
835 u32 tag = ntohl((__force u32)hdr_itt);
836
837 cxgb3i_tag_debug("release tag 0x%x.\n", tag);
838
839 if (sc &&
840 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
841 cxgb3i_is_ddp_tag(tformat, tag))
842 cxgb3i_ddp_tag_release(snic->tdev, tag);
843}
844
845/**
846 * cxgb3i_host_template -- Scsi_Host_Template structure
847 * used when registering with the scsi mid layer
848 */
849static struct scsi_host_template cxgb3i_host_template = {
850 .module = THIS_MODULE,
851 .name = "Chelsio S3xx iSCSI Initiator",
852 .proc_name = "cxgb3i",
853 .queuecommand = iscsi_queuecommand,
854 .change_queue_depth = iscsi_change_queue_depth,
Karen Xie949847d2009-02-13 21:38:49 -0800855 .can_queue = CXGB3I_SCSI_QDEPTH_DFLT - 1,
Karen Xiec3673462008-12-09 14:15:32 -0800856 .sg_tablesize = SG_ALL,
857 .max_sectors = 0xFFFF,
Mike Christie4d108352009-03-05 14:46:04 -0600858 .cmd_per_lun = CXGB3I_SCSI_QDEPTH_DFLT,
Karen Xiec3673462008-12-09 14:15:32 -0800859 .eh_abort_handler = iscsi_eh_abort,
860 .eh_device_reset_handler = iscsi_eh_device_reset,
861 .eh_target_reset_handler = iscsi_eh_target_reset,
862 .use_clustering = DISABLE_CLUSTERING,
863 .this_id = -1,
864};
865
866static struct iscsi_transport cxgb3i_iscsi_transport = {
867 .owner = THIS_MODULE,
868 .name = "cxgb3i",
869 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
870 | CAP_DATADGST | CAP_DIGEST_OFFLOAD |
871 CAP_PADDING_OFFLOAD,
872 .param_mask = ISCSI_MAX_RECV_DLENGTH |
873 ISCSI_MAX_XMIT_DLENGTH |
874 ISCSI_HDRDGST_EN |
875 ISCSI_DATADGST_EN |
876 ISCSI_INITIAL_R2T_EN |
877 ISCSI_MAX_R2T |
878 ISCSI_IMM_DATA_EN |
879 ISCSI_FIRST_BURST |
880 ISCSI_MAX_BURST |
881 ISCSI_PDU_INORDER_EN |
882 ISCSI_DATASEQ_INORDER_EN |
883 ISCSI_ERL |
884 ISCSI_CONN_PORT |
885 ISCSI_CONN_ADDRESS |
886 ISCSI_EXP_STATSN |
887 ISCSI_PERSISTENT_PORT |
888 ISCSI_PERSISTENT_ADDRESS |
889 ISCSI_TARGET_NAME | ISCSI_TPGT |
890 ISCSI_USERNAME | ISCSI_PASSWORD |
891 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
892 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
893 ISCSI_LU_RESET_TMO |
894 ISCSI_PING_TMO | ISCSI_RECV_TMO |
895 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
896 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
897 ISCSI_HOST_INITIATOR_NAME | ISCSI_HOST_NETDEV_NAME,
898 .get_host_param = cxgb3i_host_get_param,
899 .set_host_param = cxgb3i_host_set_param,
900 /* session management */
901 .create_session = cxgb3i_session_create,
902 .destroy_session = cxgb3i_session_destroy,
903 .get_session_param = iscsi_session_get_param,
904 /* connection management */
905 .create_conn = cxgb3i_conn_create,
906 .bind_conn = cxgb3i_conn_bind,
907 .destroy_conn = iscsi_tcp_conn_teardown,
908 .start_conn = iscsi_conn_start,
909 .stop_conn = iscsi_conn_stop,
910 .get_conn_param = cxgb3i_conn_get_param,
911 .set_param = cxgb3i_conn_set_param,
912 .get_stats = cxgb3i_conn_get_stats,
913 /* pdu xmit req. from user space */
914 .send_pdu = iscsi_conn_send_pdu,
915 /* task */
916 .init_task = iscsi_tcp_task_init,
917 .xmit_task = iscsi_tcp_task_xmit,
918 .cleanup_task = cxgb3i_conn_cleanup_task,
919
920 /* pdu */
921 .alloc_pdu = cxgb3i_conn_alloc_pdu,
922 .init_pdu = cxgb3i_conn_init_pdu,
923 .xmit_pdu = cxgb3i_conn_xmit_pdu,
924 .parse_pdu_itt = cxgb3i_parse_itt,
925
926 /* TCP connect/disconnect */
927 .ep_connect = cxgb3i_ep_connect,
928 .ep_poll = cxgb3i_ep_poll,
929 .ep_disconnect = cxgb3i_ep_disconnect,
930 /* Error recovery timeout call */
931 .session_recovery_timedout = iscsi_session_recovery_timedout,
932};
933
934int cxgb3i_iscsi_init(void)
935{
936 sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
937 sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
938 cxgb3i_log_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
939 ISCSI_ITT_MASK, sw_tag_idx_bits,
940 ISCSI_AGE_MASK, sw_tag_age_bits);
941
942 cxgb3i_scsi_transport =
943 iscsi_register_transport(&cxgb3i_iscsi_transport);
944 if (!cxgb3i_scsi_transport) {
945 cxgb3i_log_error("Could not register cxgb3i transport.\n");
946 return -ENODEV;
947 }
948 cxgb3i_api_debug("cxgb3i transport 0x%p.\n", cxgb3i_scsi_transport);
949 return 0;
950}
951
952void cxgb3i_iscsi_cleanup(void)
953{
954 if (cxgb3i_scsi_transport) {
955 cxgb3i_api_debug("cxgb3i transport 0x%p.\n",
956 cxgb3i_scsi_transport);
957 iscsi_unregister_transport(&cxgb3i_iscsi_transport);
958 }
959}