blob: 9212400b9b13ecde501a5fe75831855d742fe86e [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
Mike Christieed6f7742009-04-01 13:11:25 -050074static inline int adapter_update(struct cxgb3i_adapter *snic)
Karen Xiec3673462008-12-09 14:15:32 -080075{
Mike Christieed6f7742009-04-01 13:11:25 -050076 cxgb3i_log_info("snic 0x%p, t3dev 0x%p, updating.\n",
77 snic, snic->tdev);
78 return cxgb3i_adapter_ddp_info(snic->tdev, &snic->tag_format,
79 &snic->tx_max_size,
80 &snic->rx_max_size);
81}
82
83static int adapter_add(struct cxgb3i_adapter *snic)
84{
85 struct t3cdev *t3dev = snic->tdev;
Karen Xiec3673462008-12-09 14:15:32 -080086 struct adapter *adapter = tdev2adap(t3dev);
Mike Christieed6f7742009-04-01 13:11:25 -050087 int i, err;
Karen Xiec3673462008-12-09 14:15:32 -080088
Karen Xiec3673462008-12-09 14:15:32 -080089 snic->pdev = adapter->pdev;
90 snic->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
91
Mike Christieed6f7742009-04-01 13:11:25 -050092 err = cxgb3i_adapter_ddp_info(t3dev, &snic->tag_format,
Karen Xiec3673462008-12-09 14:15:32 -080093 &snic->tx_max_size,
Mike Christieed6f7742009-04-01 13:11:25 -050094 &snic->rx_max_size);
95 if (err < 0)
96 return err;
Karen Xiec3673462008-12-09 14:15:32 -080097
98 for_each_port(adapter, i) {
99 snic->hba[i] = cxgb3i_hba_host_add(snic, adapter->port[i]);
100 if (!snic->hba[i])
Mike Christieed6f7742009-04-01 13:11:25 -0500101 return -EINVAL;
Karen Xiec3673462008-12-09 14:15:32 -0800102 }
103 snic->hba_cnt = adapter->params.nports;
104
105 /* add to the list */
106 write_lock(&cxgb3i_snic_rwlock);
107 list_add_tail(&snic->list_head, &cxgb3i_snic_list);
108 write_unlock(&cxgb3i_snic_rwlock);
109
Mike Christieed6f7742009-04-01 13:11:25 -0500110 cxgb3i_log_info("t3dev 0x%p open, snic 0x%p, %u scsi hosts added.\n",
111 t3dev, snic, snic->hba_cnt);
112 return 0;
113}
Karen Xiec3673462008-12-09 14:15:32 -0800114
Mike Christieed6f7742009-04-01 13:11:25 -0500115/**
116 * cxgb3i_adapter_open - init a s3 adapter structure and any h/w settings
117 * @t3dev: t3cdev adapter
118 */
119void cxgb3i_adapter_open(struct t3cdev *t3dev)
120{
121 struct cxgb3i_adapter *snic = cxgb3i_adapter_find_by_tdev(t3dev);
122 int err;
123
124 if (snic)
125 err = adapter_update(snic);
126 else {
127 snic = kzalloc(sizeof(*snic), GFP_KERNEL);
128 if (snic) {
129 spin_lock_init(&snic->lock);
130 snic->tdev = t3dev;
131 err = adapter_add(snic);
132 } else
133 err = -ENOMEM;
134 }
135
136 if (err < 0) {
137 cxgb3i_log_info("snic 0x%p, f 0x%x, t3dev 0x%p open, err %d.\n",
138 snic, snic ? snic->flags : 0, t3dev, err);
139 if (snic) {
140 snic->flags &= ~CXGB3I_ADAPTER_FLAG_RESET;
141 cxgb3i_adapter_close(t3dev);
142 }
143 }
Karen Xiec3673462008-12-09 14:15:32 -0800144}
145
146/**
Karen Xie515f1c82009-04-01 13:11:23 -0500147 * cxgb3i_adapter_close - release the resources held and cleanup h/w settings
Karen Xiec3673462008-12-09 14:15:32 -0800148 * @t3dev: t3cdev adapter
149 */
Karen Xie515f1c82009-04-01 13:11:23 -0500150void cxgb3i_adapter_close(struct t3cdev *t3dev)
Karen Xiec3673462008-12-09 14:15:32 -0800151{
Mike Christieed6f7742009-04-01 13:11:25 -0500152 struct cxgb3i_adapter *snic = cxgb3i_adapter_find_by_tdev(t3dev);
Karen Xiec3673462008-12-09 14:15:32 -0800153 int i;
Mike Christieed6f7742009-04-01 13:11:25 -0500154
155 if (!snic || snic->flags & CXGB3I_ADAPTER_FLAG_RESET) {
156 cxgb3i_log_info("t3dev 0x%p close, snic 0x%p, f 0x%x.\n",
157 t3dev, snic, snic ? snic->flags : 0);
158 return;
159 }
Karen Xiec3673462008-12-09 14:15:32 -0800160
161 /* remove from the list */
162 write_lock(&cxgb3i_snic_rwlock);
Mike Christieed6f7742009-04-01 13:11:25 -0500163 list_del(&snic->list_head);
Karen Xiec3673462008-12-09 14:15:32 -0800164 write_unlock(&cxgb3i_snic_rwlock);
165
Mike Christieed6f7742009-04-01 13:11:25 -0500166 for (i = 0; i < snic->hba_cnt; i++) {
167 if (snic->hba[i]) {
168 cxgb3i_hba_host_remove(snic->hba[i]);
169 snic->hba[i] = NULL;
Karen Xiec3673462008-12-09 14:15:32 -0800170 }
Karen Xiec3673462008-12-09 14:15:32 -0800171 }
Mike Christieed6f7742009-04-01 13:11:25 -0500172 cxgb3i_log_info("t3dev 0x%p close, snic 0x%p, %u scsi hosts removed.\n",
173 t3dev, snic, snic->hba_cnt);
174 kfree(snic);
Karen Xiec3673462008-12-09 14:15:32 -0800175}
176
177/**
Karen Xie154229a2009-03-05 14:46:08 -0600178 * cxgb3i_hba_find_by_netdev - find the cxgb3i_hba structure via net_device
Karen Xiec3673462008-12-09 14:15:32 -0800179 * @t3dev: t3cdev adapter
180 */
181struct cxgb3i_hba *cxgb3i_hba_find_by_netdev(struct net_device *ndev)
182{
183 struct cxgb3i_adapter *snic;
184 int i;
185
186 read_lock(&cxgb3i_snic_rwlock);
187 list_for_each_entry(snic, &cxgb3i_snic_list, list_head) {
188 for (i = 0; i < snic->hba_cnt; i++) {
189 if (snic->hba[i]->ndev == ndev) {
190 read_unlock(&cxgb3i_snic_rwlock);
191 return snic->hba[i];
192 }
193 }
194 }
195 read_unlock(&cxgb3i_snic_rwlock);
196 return NULL;
197}
198
199/**
200 * cxgb3i_hba_host_add - register a new host with scsi/iscsi
201 * @snic: the cxgb3i adapter
202 * @ndev: associated net_device
203 */
204struct cxgb3i_hba *cxgb3i_hba_host_add(struct cxgb3i_adapter *snic,
205 struct net_device *ndev)
206{
207 struct cxgb3i_hba *hba;
208 struct Scsi_Host *shost;
209 int err;
210
211 shost = iscsi_host_alloc(&cxgb3i_host_template,
Mike Christie4d108352009-03-05 14:46:04 -0600212 sizeof(struct cxgb3i_hba), 1);
Karen Xiec3673462008-12-09 14:15:32 -0800213 if (!shost) {
Mike Christieed6f7742009-04-01 13:11:25 -0500214 cxgb3i_log_info("snic 0x%p, ndev 0x%p, host_alloc failed.\n",
215 snic, ndev);
Karen Xiec3673462008-12-09 14:15:32 -0800216 return NULL;
217 }
218
219 shost->transportt = cxgb3i_scsi_transport;
220 shost->max_lun = CXGB3I_MAX_LUN;
221 shost->max_id = CXGB3I_MAX_TARGET;
222 shost->max_channel = 0;
223 shost->max_cmd_len = 16;
224
225 hba = iscsi_host_priv(shost);
226 hba->snic = snic;
227 hba->ndev = ndev;
228 hba->shost = shost;
229
230 pci_dev_get(snic->pdev);
231 err = iscsi_host_add(shost, &snic->pdev->dev);
232 if (err) {
Mike Christieed6f7742009-04-01 13:11:25 -0500233 cxgb3i_log_info("snic 0x%p, ndev 0x%p, host_add failed.\n",
234 snic, ndev);
Karen Xiec3673462008-12-09 14:15:32 -0800235 goto pci_dev_put;
236 }
237
238 cxgb3i_api_debug("shost 0x%p, hba 0x%p, no %u.\n",
239 shost, hba, shost->host_no);
240
241 return hba;
242
243pci_dev_put:
244 pci_dev_put(snic->pdev);
245 scsi_host_put(shost);
246 return NULL;
247}
248
249/**
250 * cxgb3i_hba_host_remove - de-register the host with scsi/iscsi
251 * @hba: the cxgb3i hba
252 */
253void cxgb3i_hba_host_remove(struct cxgb3i_hba *hba)
254{
255 cxgb3i_api_debug("shost 0x%p, hba 0x%p, no %u.\n",
256 hba->shost, hba, hba->shost->host_no);
257 iscsi_host_remove(hba->shost);
258 pci_dev_put(hba->snic->pdev);
259 iscsi_host_free(hba->shost);
260}
261
262/**
263 * cxgb3i_ep_connect - establish TCP connection to target portal
264 * @dst_addr: target IP address
265 * @non_blocking: blocking or non-blocking call
266 *
267 * Initiates a TCP/IP connection to the dst_addr
268 */
269static struct iscsi_endpoint *cxgb3i_ep_connect(struct sockaddr *dst_addr,
270 int non_blocking)
271{
272 struct iscsi_endpoint *ep;
273 struct cxgb3i_endpoint *cep;
274 struct cxgb3i_hba *hba;
275 struct s3_conn *c3cn = NULL;
276 int err = 0;
277
278 c3cn = cxgb3i_c3cn_create();
279 if (!c3cn) {
280 cxgb3i_log_info("ep connect OOM.\n");
281 err = -ENOMEM;
282 goto release_conn;
283 }
284
285 err = cxgb3i_c3cn_connect(c3cn, (struct sockaddr_in *)dst_addr);
286 if (err < 0) {
287 cxgb3i_log_info("ep connect failed.\n");
288 goto release_conn;
289 }
290 hba = cxgb3i_hba_find_by_netdev(c3cn->dst_cache->dev);
291 if (!hba) {
292 err = -ENOSPC;
293 cxgb3i_log_info("NOT going through cxgbi device.\n");
294 goto release_conn;
295 }
296 if (c3cn_is_closing(c3cn)) {
297 err = -ENOSPC;
298 cxgb3i_log_info("ep connect unable to connect.\n");
299 goto release_conn;
300 }
301
302 ep = iscsi_create_endpoint(sizeof(*cep));
303 if (!ep) {
304 err = -ENOMEM;
305 cxgb3i_log_info("iscsi alloc ep, OOM.\n");
306 goto release_conn;
307 }
308 cep = ep->dd_data;
309 cep->c3cn = c3cn;
310 cep->hba = hba;
311
312 cxgb3i_api_debug("ep 0x%p, 0x%p, c3cn 0x%p, hba 0x%p.\n",
313 ep, cep, c3cn, hba);
314 return ep;
315
316release_conn:
317 cxgb3i_api_debug("conn 0x%p failed, release.\n", c3cn);
318 if (c3cn)
319 cxgb3i_c3cn_release(c3cn);
320 return ERR_PTR(err);
321}
322
323/**
324 * cxgb3i_ep_poll - polls for TCP connection establishement
325 * @ep: TCP connection (endpoint) handle
326 * @timeout_ms: timeout value in milli secs
327 *
328 * polls for TCP connect request to complete
329 */
330static int cxgb3i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
331{
332 struct cxgb3i_endpoint *cep = ep->dd_data;
333 struct s3_conn *c3cn = cep->c3cn;
334
335 if (!c3cn_is_established(c3cn))
336 return 0;
337 cxgb3i_api_debug("ep 0x%p, c3cn 0x%p established.\n", ep, c3cn);
338 return 1;
339}
340
341/**
342 * cxgb3i_ep_disconnect - teardown TCP connection
343 * @ep: TCP connection (endpoint) handle
344 *
345 * teardown TCP connection
346 */
347static void cxgb3i_ep_disconnect(struct iscsi_endpoint *ep)
348{
349 struct cxgb3i_endpoint *cep = ep->dd_data;
350 struct cxgb3i_conn *cconn = cep->cconn;
351
352 cxgb3i_api_debug("ep 0x%p, cep 0x%p.\n", ep, cep);
353
354 if (cconn && cconn->conn) {
355 /*
356 * stop the xmit path so the xmit_pdu function is
357 * not being called
358 */
359 iscsi_suspend_tx(cconn->conn);
360
361 write_lock_bh(&cep->c3cn->callback_lock);
362 cep->c3cn->user_data = NULL;
363 cconn->cep = NULL;
364 write_unlock_bh(&cep->c3cn->callback_lock);
365 }
366
367 cxgb3i_api_debug("ep 0x%p, cep 0x%p, release c3cn 0x%p.\n",
368 ep, cep, cep->c3cn);
369 cxgb3i_c3cn_release(cep->c3cn);
370 iscsi_destroy_endpoint(ep);
371}
372
373/**
374 * cxgb3i_session_create - create a new iscsi session
375 * @cmds_max: max # of commands
376 * @qdepth: scsi queue depth
377 * @initial_cmdsn: initial iscsi CMDSN for this session
Karen Xiec3673462008-12-09 14:15:32 -0800378 *
379 * Creates a new iSCSI session
380 */
381static struct iscsi_cls_session *
382cxgb3i_session_create(struct iscsi_endpoint *ep, u16 cmds_max, u16 qdepth,
Mike Christie5e7facb2009-03-05 14:46:06 -0600383 u32 initial_cmdsn)
Karen Xiec3673462008-12-09 14:15:32 -0800384{
385 struct cxgb3i_endpoint *cep;
386 struct cxgb3i_hba *hba;
387 struct Scsi_Host *shost;
388 struct iscsi_cls_session *cls_session;
389 struct iscsi_session *session;
390
391 if (!ep) {
392 cxgb3i_log_error("%s, missing endpoint.\n", __func__);
393 return NULL;
394 }
395
396 cep = ep->dd_data;
397 hba = cep->hba;
398 shost = hba->shost;
399 cxgb3i_api_debug("ep 0x%p, cep 0x%p, hba 0x%p.\n", ep, cep, hba);
400 BUG_ON(hba != iscsi_host_priv(shost));
401
Karen Xiec3673462008-12-09 14:15:32 -0800402 cls_session = iscsi_session_setup(&cxgb3i_iscsi_transport, shost,
403 cmds_max,
Karen Xie949847d2009-02-13 21:38:49 -0800404 sizeof(struct iscsi_tcp_task) +
405 sizeof(struct cxgb3i_task_data),
Karen Xiec3673462008-12-09 14:15:32 -0800406 initial_cmdsn, ISCSI_MAX_TARGET);
407 if (!cls_session)
408 return NULL;
409 session = cls_session->dd_data;
410 if (iscsi_tcp_r2tpool_alloc(session))
411 goto remove_session;
412
413 return cls_session;
414
415remove_session:
416 iscsi_session_teardown(cls_session);
417 return NULL;
418}
419
420/**
421 * cxgb3i_session_destroy - destroys iscsi session
422 * @cls_session: pointer to iscsi cls session
423 *
424 * Destroys an iSCSI session instance and releases its all resources held
425 */
426static void cxgb3i_session_destroy(struct iscsi_cls_session *cls_session)
427{
428 cxgb3i_api_debug("sess 0x%p.\n", cls_session);
429 iscsi_tcp_r2tpool_free(cls_session->dd_data);
430 iscsi_session_teardown(cls_session);
431}
432
433/**
Karen Xie154229a2009-03-05 14:46:08 -0600434 * cxgb3i_conn_max_xmit_dlength -- calc the max. xmit pdu segment size
Karen Xiec3673462008-12-09 14:15:32 -0800435 * @conn: iscsi connection
Karen Xie154229a2009-03-05 14:46:08 -0600436 * check the max. xmit pdu payload, reduce it if needed
Karen Xiec3673462008-12-09 14:15:32 -0800437 */
438static inline int cxgb3i_conn_max_xmit_dlength(struct iscsi_conn *conn)
439
440{
441 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
442 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
Karen Xief62d0892009-02-13 21:38:54 -0800443 unsigned int max = max(512 * MAX_SKB_FRAGS, SKB_TX_HEADROOM);
Karen Xiec3673462008-12-09 14:15:32 -0800444
Karen Xief62d0892009-02-13 21:38:54 -0800445 max = min(cconn->hba->snic->tx_max_size, max);
Karen Xiec3673462008-12-09 14:15:32 -0800446 if (conn->max_xmit_dlength)
Karen Xief62d0892009-02-13 21:38:54 -0800447 conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
Karen Xiec3673462008-12-09 14:15:32 -0800448 else
449 conn->max_xmit_dlength = max;
450 align_pdu_size(conn->max_xmit_dlength);
Karen Xief62d0892009-02-13 21:38:54 -0800451 cxgb3i_api_debug("conn 0x%p, max xmit %u.\n",
Karen Xiec3673462008-12-09 14:15:32 -0800452 conn, conn->max_xmit_dlength);
453 return 0;
454}
455
456/**
Karen Xie154229a2009-03-05 14:46:08 -0600457 * cxgb3i_conn_max_recv_dlength -- check the max. recv pdu segment size
Karen Xiec3673462008-12-09 14:15:32 -0800458 * @conn: iscsi connection
459 * return 0 if the value is valid, < 0 otherwise.
460 */
461static inline int cxgb3i_conn_max_recv_dlength(struct iscsi_conn *conn)
462{
463 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
464 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
Karen Xief62d0892009-02-13 21:38:54 -0800465 unsigned int max = cconn->hba->snic->rx_max_size;
Karen Xiec3673462008-12-09 14:15:32 -0800466
467 align_pdu_size(max);
468 if (conn->max_recv_dlength) {
469 if (conn->max_recv_dlength > max) {
470 cxgb3i_log_error("MaxRecvDataSegmentLength %u too big."
471 " Need to be <= %u.\n",
472 conn->max_recv_dlength, max);
473 return -EINVAL;
474 }
Karen Xief62d0892009-02-13 21:38:54 -0800475 conn->max_recv_dlength = min(conn->max_recv_dlength, max);
Karen Xiec3673462008-12-09 14:15:32 -0800476 align_pdu_size(conn->max_recv_dlength);
477 } else
478 conn->max_recv_dlength = max;
479 cxgb3i_api_debug("conn 0x%p, max recv %u.\n",
480 conn, conn->max_recv_dlength);
481 return 0;
482}
483
484/**
485 * cxgb3i_conn_create - create iscsi connection instance
486 * @cls_session: pointer to iscsi cls session
487 * @cid: iscsi cid
488 *
489 * Creates a new iSCSI connection instance for a given session
490 */
491static struct iscsi_cls_conn *cxgb3i_conn_create(struct iscsi_cls_session
492 *cls_session, u32 cid)
493{
494 struct iscsi_cls_conn *cls_conn;
495 struct iscsi_conn *conn;
496 struct iscsi_tcp_conn *tcp_conn;
497 struct cxgb3i_conn *cconn;
498
499 cxgb3i_api_debug("sess 0x%p, cid %u.\n", cls_session, cid);
500
501 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
502 if (!cls_conn)
503 return NULL;
504 conn = cls_conn->dd_data;
505 tcp_conn = conn->dd_data;
506 cconn = tcp_conn->dd_data;
507
508 cconn->conn = conn;
509 return cls_conn;
510}
511
512/**
513 * cxgb3i_conn_bind - binds iscsi sess, conn and endpoint together
514 * @cls_session: pointer to iscsi cls session
515 * @cls_conn: pointer to iscsi cls conn
516 * @transport_eph: 64-bit EP handle
517 * @is_leading: leading connection on this session?
518 *
519 * Binds together an iSCSI session, an iSCSI connection and a
520 * TCP connection. This routine returns error code if the TCP
521 * connection does not belong on the device iSCSI sess/conn is bound
522 */
523
524static int cxgb3i_conn_bind(struct iscsi_cls_session *cls_session,
525 struct iscsi_cls_conn *cls_conn,
526 u64 transport_eph, int is_leading)
527{
528 struct iscsi_conn *conn = cls_conn->dd_data;
529 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
530 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
531 struct cxgb3i_adapter *snic;
532 struct iscsi_endpoint *ep;
533 struct cxgb3i_endpoint *cep;
534 struct s3_conn *c3cn;
535 int err;
536
537 ep = iscsi_lookup_endpoint(transport_eph);
538 if (!ep)
539 return -EINVAL;
540
541 /* setup ddp pagesize */
542 cep = ep->dd_data;
543 c3cn = cep->c3cn;
544 snic = cep->hba->snic;
545 err = cxgb3i_setup_conn_host_pagesize(snic->tdev, c3cn->tid, 0);
546 if (err < 0)
547 return err;
548
549 cxgb3i_api_debug("ep 0x%p, cls sess 0x%p, cls conn 0x%p.\n",
550 ep, cls_session, cls_conn);
551
552 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
553 if (err)
554 return -EINVAL;
555
556 /* calculate the tag idx bits needed for this conn based on cmds_max */
557 cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
558 cxgb3i_api_debug("session cmds_max 0x%x, bits %u.\n",
559 conn->session->cmds_max, cconn->task_idx_bits);
560
561 read_lock(&c3cn->callback_lock);
562 c3cn->user_data = conn;
563 cconn->hba = cep->hba;
564 cconn->cep = cep;
565 cep->cconn = cconn;
566 read_unlock(&c3cn->callback_lock);
567
568 cxgb3i_conn_max_xmit_dlength(conn);
569 cxgb3i_conn_max_recv_dlength(conn);
570
571 spin_lock_bh(&conn->session->lock);
572 sprintf(conn->portal_address, NIPQUAD_FMT,
573 NIPQUAD(c3cn->daddr.sin_addr.s_addr));
574 conn->portal_port = ntohs(c3cn->daddr.sin_port);
575 spin_unlock_bh(&conn->session->lock);
576
577 /* init recv engine */
578 iscsi_tcp_hdr_recv_prep(tcp_conn);
579
580 return 0;
581}
582
583/**
584 * cxgb3i_conn_get_param - return iscsi connection parameter to caller
585 * @cls_conn: pointer to iscsi cls conn
586 * @param: parameter type identifier
587 * @buf: buffer pointer
588 *
589 * returns iSCSI connection parameters
590 */
591static int cxgb3i_conn_get_param(struct iscsi_cls_conn *cls_conn,
592 enum iscsi_param param, char *buf)
593{
594 struct iscsi_conn *conn = cls_conn->dd_data;
595 int len;
596
597 cxgb3i_api_debug("cls_conn 0x%p, param %d.\n", cls_conn, param);
598
599 switch (param) {
600 case ISCSI_PARAM_CONN_PORT:
601 spin_lock_bh(&conn->session->lock);
602 len = sprintf(buf, "%hu\n", conn->portal_port);
603 spin_unlock_bh(&conn->session->lock);
604 break;
605 case ISCSI_PARAM_CONN_ADDRESS:
606 spin_lock_bh(&conn->session->lock);
607 len = sprintf(buf, "%s\n", conn->portal_address);
608 spin_unlock_bh(&conn->session->lock);
609 break;
610 default:
611 return iscsi_conn_get_param(cls_conn, param, buf);
612 }
613
614 return len;
615}
616
617/**
618 * cxgb3i_conn_set_param - set iscsi connection parameter
619 * @cls_conn: pointer to iscsi cls conn
620 * @param: parameter type identifier
621 * @buf: buffer pointer
622 * @buflen: buffer length
623 *
624 * set iSCSI connection parameters
625 */
626static int cxgb3i_conn_set_param(struct iscsi_cls_conn *cls_conn,
627 enum iscsi_param param, char *buf, int buflen)
628{
629 struct iscsi_conn *conn = cls_conn->dd_data;
630 struct iscsi_session *session = conn->session;
631 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
632 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
633 struct cxgb3i_adapter *snic = cconn->hba->snic;
634 struct s3_conn *c3cn = cconn->cep->c3cn;
635 int value, err = 0;
636
637 switch (param) {
638 case ISCSI_PARAM_HDRDGST_EN:
639 err = iscsi_set_param(cls_conn, param, buf, buflen);
640 if (!err && conn->hdrdgst_en)
641 err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
642 conn->hdrdgst_en,
643 conn->datadgst_en, 0);
644 break;
645 case ISCSI_PARAM_DATADGST_EN:
646 err = iscsi_set_param(cls_conn, param, buf, buflen);
647 if (!err && conn->datadgst_en)
648 err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
649 conn->hdrdgst_en,
650 conn->datadgst_en, 0);
651 break;
652 case ISCSI_PARAM_MAX_R2T:
653 sscanf(buf, "%d", &value);
654 if (value <= 0 || !is_power_of_2(value))
655 return -EINVAL;
656 if (session->max_r2t == value)
657 break;
658 iscsi_tcp_r2tpool_free(session);
659 err = iscsi_set_param(cls_conn, param, buf, buflen);
660 if (!err && iscsi_tcp_r2tpool_alloc(session))
661 return -ENOMEM;
662 case ISCSI_PARAM_MAX_RECV_DLENGTH:
663 err = iscsi_set_param(cls_conn, param, buf, buflen);
664 if (!err)
665 err = cxgb3i_conn_max_recv_dlength(conn);
666 break;
667 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
668 err = iscsi_set_param(cls_conn, param, buf, buflen);
669 if (!err)
670 err = cxgb3i_conn_max_xmit_dlength(conn);
671 break;
672 default:
673 return iscsi_set_param(cls_conn, param, buf, buflen);
674 }
675 return err;
676}
677
678/**
679 * cxgb3i_host_set_param - configure host (adapter) related parameters
680 * @shost: scsi host pointer
681 * @param: parameter type identifier
682 * @buf: buffer pointer
683 */
684static int cxgb3i_host_set_param(struct Scsi_Host *shost,
685 enum iscsi_host_param param,
686 char *buf, int buflen)
687{
688 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
689
690 cxgb3i_api_debug("param %d, buf %s.\n", param, buf);
691
692 switch (param) {
693 case ISCSI_HOST_PARAM_IPADDRESS:
694 {
695 __be32 addr = in_aton(buf);
696 cxgb3i_set_private_ipv4addr(hba->ndev, addr);
697 return 0;
698 }
699 case ISCSI_HOST_PARAM_HWADDRESS:
700 case ISCSI_HOST_PARAM_NETDEV_NAME:
701 /* ignore */
702 return 0;
703 default:
704 return iscsi_host_set_param(shost, param, buf, buflen);
705 }
706}
707
708/**
709 * cxgb3i_host_get_param - returns host (adapter) related parameters
710 * @shost: scsi host pointer
711 * @param: parameter type identifier
712 * @buf: buffer pointer
713 */
714static int cxgb3i_host_get_param(struct Scsi_Host *shost,
715 enum iscsi_host_param param, char *buf)
716{
717 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
718 int len = 0;
719
720 cxgb3i_api_debug("hba %s, param %d.\n", hba->ndev->name, param);
721
722 switch (param) {
723 case ISCSI_HOST_PARAM_HWADDRESS:
724 len = sysfs_format_mac(buf, hba->ndev->dev_addr, 6);
725 break;
726 case ISCSI_HOST_PARAM_NETDEV_NAME:
727 len = sprintf(buf, "%s\n", hba->ndev->name);
728 break;
729 case ISCSI_HOST_PARAM_IPADDRESS:
730 {
731 __be32 addr;
732
733 addr = cxgb3i_get_private_ipv4addr(hba->ndev);
734 len = sprintf(buf, NIPQUAD_FMT, NIPQUAD(addr));
735 break;
736 }
737 default:
738 return iscsi_host_get_param(shost, param, buf);
739 }
740 return len;
741}
742
743/**
744 * cxgb3i_conn_get_stats - returns iSCSI stats
745 * @cls_conn: pointer to iscsi cls conn
746 * @stats: pointer to iscsi statistic struct
747 */
748static void cxgb3i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
749 struct iscsi_stats *stats)
750{
751 struct iscsi_conn *conn = cls_conn->dd_data;
752
753 stats->txdata_octets = conn->txdata_octets;
754 stats->rxdata_octets = conn->rxdata_octets;
755 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
756 stats->dataout_pdus = conn->dataout_pdus_cnt;
757 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
758 stats->datain_pdus = conn->datain_pdus_cnt;
759 stats->r2t_pdus = conn->r2t_pdus_cnt;
760 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
761 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
762 stats->digest_err = 0;
763 stats->timeout_err = 0;
764 stats->custom_length = 1;
765 strcpy(stats->custom[0].desc, "eh_abort_cnt");
766 stats->custom[0].value = conn->eh_abort_cnt;
767}
768
769/**
770 * cxgb3i_parse_itt - get the idx and age bits from a given tag
771 * @conn: iscsi connection
772 * @itt: itt tag
773 * @idx: task index, filled in by this function
774 * @age: session age, filled in by this function
775 */
776static void cxgb3i_parse_itt(struct iscsi_conn *conn, itt_t itt,
777 int *idx, int *age)
778{
779 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
780 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
781 struct cxgb3i_adapter *snic = cconn->hba->snic;
782 u32 tag = ntohl((__force u32) itt);
783 u32 sw_bits;
784
785 sw_bits = cxgb3i_tag_nonrsvd_bits(&snic->tag_format, tag);
786 if (idx)
787 *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
788 if (age)
789 *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
790
791 cxgb3i_tag_debug("parse tag 0x%x/0x%x, sw 0x%x, itt 0x%x, age 0x%x.\n",
792 tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
793 age ? *age : 0xFF);
794}
795
796/**
797 * cxgb3i_reserve_itt - generate tag for a give task
Karen Xiec3673462008-12-09 14:15:32 -0800798 * @task: iscsi task
799 * @hdr_itt: tag, filled in by this function
Karen Xie154229a2009-03-05 14:46:08 -0600800 * Set up ddp for scsi read tasks if possible.
Karen Xiec3673462008-12-09 14:15:32 -0800801 */
802int cxgb3i_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
803{
804 struct scsi_cmnd *sc = task->sc;
805 struct iscsi_conn *conn = task->conn;
806 struct iscsi_session *sess = conn->session;
807 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
808 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
809 struct cxgb3i_adapter *snic = cconn->hba->snic;
810 struct cxgb3i_tag_format *tformat = &snic->tag_format;
811 u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
812 u32 tag;
813 int err = -EINVAL;
814
815 if (sc &&
816 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
817 cxgb3i_sw_tag_usable(tformat, sw_tag)) {
818 struct s3_conn *c3cn = cconn->cep->c3cn;
819 struct cxgb3i_gather_list *gl;
820
821 gl = cxgb3i_ddp_make_gl(scsi_in(sc)->length,
822 scsi_in(sc)->table.sgl,
823 scsi_in(sc)->table.nents,
824 snic->pdev,
825 GFP_ATOMIC);
826 if (gl) {
827 tag = sw_tag;
828 err = cxgb3i_ddp_tag_reserve(snic->tdev, c3cn->tid,
829 tformat, &tag,
830 gl, GFP_ATOMIC);
831 if (err < 0)
832 cxgb3i_ddp_release_gl(gl, snic->pdev);
833 }
834 }
835
836 if (err < 0)
837 tag = cxgb3i_set_non_ddp_tag(tformat, sw_tag);
838 /* the itt need to sent in big-endian order */
839 *hdr_itt = (__force itt_t)htonl(tag);
840
841 cxgb3i_tag_debug("new tag 0x%x/0x%x (itt 0x%x, age 0x%x).\n",
842 tag, *hdr_itt, task->itt, sess->age);
843 return 0;
844}
845
846/**
847 * cxgb3i_release_itt - release the tag for a given task
Karen Xiec3673462008-12-09 14:15:32 -0800848 * @task: iscsi task
849 * @hdr_itt: tag
Karen Xie154229a2009-03-05 14:46:08 -0600850 * If the tag is a ddp tag, release the ddp setup
Karen Xiec3673462008-12-09 14:15:32 -0800851 */
852void cxgb3i_release_itt(struct iscsi_task *task, itt_t hdr_itt)
853{
854 struct scsi_cmnd *sc = task->sc;
855 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
856 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
857 struct cxgb3i_adapter *snic = cconn->hba->snic;
858 struct cxgb3i_tag_format *tformat = &snic->tag_format;
859 u32 tag = ntohl((__force u32)hdr_itt);
860
861 cxgb3i_tag_debug("release tag 0x%x.\n", tag);
862
863 if (sc &&
864 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
865 cxgb3i_is_ddp_tag(tformat, tag))
866 cxgb3i_ddp_tag_release(snic->tdev, tag);
867}
868
869/**
870 * cxgb3i_host_template -- Scsi_Host_Template structure
871 * used when registering with the scsi mid layer
872 */
873static struct scsi_host_template cxgb3i_host_template = {
874 .module = THIS_MODULE,
875 .name = "Chelsio S3xx iSCSI Initiator",
876 .proc_name = "cxgb3i",
877 .queuecommand = iscsi_queuecommand,
878 .change_queue_depth = iscsi_change_queue_depth,
Mike Christiedd0af9f2009-04-21 15:32:33 -0500879 .can_queue = CXGB3I_SCSI_HOST_QDEPTH,
Karen Xiec3673462008-12-09 14:15:32 -0800880 .sg_tablesize = SG_ALL,
881 .max_sectors = 0xFFFF,
Mike Christiedd0af9f2009-04-21 15:32:33 -0500882 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
Karen Xiec3673462008-12-09 14:15:32 -0800883 .eh_abort_handler = iscsi_eh_abort,
884 .eh_device_reset_handler = iscsi_eh_device_reset,
885 .eh_target_reset_handler = iscsi_eh_target_reset,
Mike Christie6b5d6c42009-04-21 15:32:32 -0500886 .target_alloc = iscsi_target_alloc,
Karen Xiec3673462008-12-09 14:15:32 -0800887 .use_clustering = DISABLE_CLUSTERING,
888 .this_id = -1,
889};
890
891static struct iscsi_transport cxgb3i_iscsi_transport = {
892 .owner = THIS_MODULE,
893 .name = "cxgb3i",
894 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
895 | CAP_DATADGST | CAP_DIGEST_OFFLOAD |
896 CAP_PADDING_OFFLOAD,
897 .param_mask = ISCSI_MAX_RECV_DLENGTH |
898 ISCSI_MAX_XMIT_DLENGTH |
899 ISCSI_HDRDGST_EN |
900 ISCSI_DATADGST_EN |
901 ISCSI_INITIAL_R2T_EN |
902 ISCSI_MAX_R2T |
903 ISCSI_IMM_DATA_EN |
904 ISCSI_FIRST_BURST |
905 ISCSI_MAX_BURST |
906 ISCSI_PDU_INORDER_EN |
907 ISCSI_DATASEQ_INORDER_EN |
908 ISCSI_ERL |
909 ISCSI_CONN_PORT |
910 ISCSI_CONN_ADDRESS |
911 ISCSI_EXP_STATSN |
912 ISCSI_PERSISTENT_PORT |
913 ISCSI_PERSISTENT_ADDRESS |
914 ISCSI_TARGET_NAME | ISCSI_TPGT |
915 ISCSI_USERNAME | ISCSI_PASSWORD |
916 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
917 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
918 ISCSI_LU_RESET_TMO |
919 ISCSI_PING_TMO | ISCSI_RECV_TMO |
920 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
921 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
922 ISCSI_HOST_INITIATOR_NAME | ISCSI_HOST_NETDEV_NAME,
923 .get_host_param = cxgb3i_host_get_param,
924 .set_host_param = cxgb3i_host_set_param,
925 /* session management */
926 .create_session = cxgb3i_session_create,
927 .destroy_session = cxgb3i_session_destroy,
928 .get_session_param = iscsi_session_get_param,
929 /* connection management */
930 .create_conn = cxgb3i_conn_create,
931 .bind_conn = cxgb3i_conn_bind,
932 .destroy_conn = iscsi_tcp_conn_teardown,
933 .start_conn = iscsi_conn_start,
934 .stop_conn = iscsi_conn_stop,
935 .get_conn_param = cxgb3i_conn_get_param,
936 .set_param = cxgb3i_conn_set_param,
937 .get_stats = cxgb3i_conn_get_stats,
938 /* pdu xmit req. from user space */
939 .send_pdu = iscsi_conn_send_pdu,
940 /* task */
941 .init_task = iscsi_tcp_task_init,
942 .xmit_task = iscsi_tcp_task_xmit,
943 .cleanup_task = cxgb3i_conn_cleanup_task,
944
945 /* pdu */
946 .alloc_pdu = cxgb3i_conn_alloc_pdu,
947 .init_pdu = cxgb3i_conn_init_pdu,
948 .xmit_pdu = cxgb3i_conn_xmit_pdu,
949 .parse_pdu_itt = cxgb3i_parse_itt,
950
951 /* TCP connect/disconnect */
952 .ep_connect = cxgb3i_ep_connect,
953 .ep_poll = cxgb3i_ep_poll,
954 .ep_disconnect = cxgb3i_ep_disconnect,
955 /* Error recovery timeout call */
956 .session_recovery_timedout = iscsi_session_recovery_timedout,
957};
958
959int cxgb3i_iscsi_init(void)
960{
961 sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
962 sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
963 cxgb3i_log_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
964 ISCSI_ITT_MASK, sw_tag_idx_bits,
965 ISCSI_AGE_MASK, sw_tag_age_bits);
966
967 cxgb3i_scsi_transport =
968 iscsi_register_transport(&cxgb3i_iscsi_transport);
969 if (!cxgb3i_scsi_transport) {
970 cxgb3i_log_error("Could not register cxgb3i transport.\n");
971 return -ENODEV;
972 }
973 cxgb3i_api_debug("cxgb3i transport 0x%p.\n", cxgb3i_scsi_transport);
974 return 0;
975}
976
977void cxgb3i_iscsi_cleanup(void)
978{
979 if (cxgb3i_scsi_transport) {
980 cxgb3i_api_debug("cxgb3i transport 0x%p.\n",
981 cxgb3i_scsi_transport);
982 iscsi_unregister_transport(&cxgb3i_iscsi_transport);
983 }
984}