blob: 2631bddd255e8530cadcf9d39813ccf5a4cdf1f8 [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>
Karen Xiea222ad12009-06-26 15:17:29 -070016#include <linux/if_vlan.h>
Herbert Xuf00a3322009-05-30 13:40:04 +100017#include <net/dst.h>
Karen Xiec3673462008-12-09 14:15:32 -080018#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>
27
28#include "cxgb3i.h"
29#include "cxgb3i_pdu.h"
30
31#ifdef __DEBUG_CXGB3I_TAG__
32#define cxgb3i_tag_debug cxgb3i_log_debug
33#else
34#define cxgb3i_tag_debug(fmt...)
35#endif
36
37#ifdef __DEBUG_CXGB3I_API__
38#define cxgb3i_api_debug cxgb3i_log_debug
39#else
40#define cxgb3i_api_debug(fmt...)
41#endif
42
43/*
44 * align pdu size to multiple of 512 for better performance
45 */
46#define align_pdu_size(n) do { n = (n) & (~511); } while (0)
47
48static struct scsi_transport_template *cxgb3i_scsi_transport;
49static struct scsi_host_template cxgb3i_host_template;
50static struct iscsi_transport cxgb3i_iscsi_transport;
51static unsigned char sw_tag_idx_bits;
52static unsigned char sw_tag_age_bits;
53
54static LIST_HEAD(cxgb3i_snic_list);
55static DEFINE_RWLOCK(cxgb3i_snic_rwlock);
56
57/**
Karen Xie515f1c82009-04-01 13:11:23 -050058 * cxgb3i_adpater_find_by_tdev - find the cxgb3i_adapter structure via t3cdev
59 * @tdev: t3cdev pointer
60 */
61struct cxgb3i_adapter *cxgb3i_adapter_find_by_tdev(struct t3cdev *tdev)
62{
63 struct cxgb3i_adapter *snic;
64
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;
70 }
71 }
72 read_unlock(&cxgb3i_snic_rwlock);
73 return NULL;
74}
75
Mike Christieed6f7742009-04-01 13:11:25 -050076static inline int adapter_update(struct cxgb3i_adapter *snic)
Karen Xiec3673462008-12-09 14:15:32 -080077{
Mike Christieed6f7742009-04-01 13:11:25 -050078 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);
83}
84
85static int adapter_add(struct cxgb3i_adapter *snic)
86{
87 struct t3cdev *t3dev = snic->tdev;
Karen Xiec3673462008-12-09 14:15:32 -080088 struct adapter *adapter = tdev2adap(t3dev);
Mike Christieed6f7742009-04-01 13:11:25 -050089 int i, err;
Karen Xiec3673462008-12-09 14:15:32 -080090
Karen Xiec3673462008-12-09 14:15:32 -080091 snic->pdev = adapter->pdev;
92 snic->tag_format.sw_bits = sw_tag_idx_bits + sw_tag_age_bits;
93
Mike Christieed6f7742009-04-01 13:11:25 -050094 err = cxgb3i_adapter_ddp_info(t3dev, &snic->tag_format,
Karen Xiec3673462008-12-09 14:15:32 -080095 &snic->tx_max_size,
Mike Christieed6f7742009-04-01 13:11:25 -050096 &snic->rx_max_size);
97 if (err < 0)
98 return err;
Karen Xiec3673462008-12-09 14:15:32 -080099
100 for_each_port(adapter, i) {
101 snic->hba[i] = cxgb3i_hba_host_add(snic, adapter->port[i]);
102 if (!snic->hba[i])
Mike Christieed6f7742009-04-01 13:11:25 -0500103 return -EINVAL;
Karen Xiec3673462008-12-09 14:15:32 -0800104 }
105 snic->hba_cnt = adapter->params.nports;
106
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);
111
Mike Christieed6f7742009-04-01 13:11:25 -0500112 cxgb3i_log_info("t3dev 0x%p open, snic 0x%p, %u scsi hosts added.\n",
113 t3dev, snic, snic->hba_cnt);
114 return 0;
115}
Karen Xiec3673462008-12-09 14:15:32 -0800116
Mike Christieed6f7742009-04-01 13:11:25 -0500117/**
118 * cxgb3i_adapter_open - init a s3 adapter structure and any h/w settings
119 * @t3dev: t3cdev adapter
120 */
121void cxgb3i_adapter_open(struct t3cdev *t3dev)
122{
123 struct cxgb3i_adapter *snic = cxgb3i_adapter_find_by_tdev(t3dev);
124 int err;
125
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;
136 }
137
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);
144 }
145 }
Karen Xiec3673462008-12-09 14:15:32 -0800146}
147
148/**
Karen Xie515f1c82009-04-01 13:11:23 -0500149 * cxgb3i_adapter_close - release the resources held and cleanup h/w settings
Karen Xiec3673462008-12-09 14:15:32 -0800150 * @t3dev: t3cdev adapter
151 */
Karen Xie515f1c82009-04-01 13:11:23 -0500152void cxgb3i_adapter_close(struct t3cdev *t3dev)
Karen Xiec3673462008-12-09 14:15:32 -0800153{
Mike Christieed6f7742009-04-01 13:11:25 -0500154 struct cxgb3i_adapter *snic = cxgb3i_adapter_find_by_tdev(t3dev);
Karen Xiec3673462008-12-09 14:15:32 -0800155 int i;
Mike Christieed6f7742009-04-01 13:11:25 -0500156
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;
161 }
Karen Xiec3673462008-12-09 14:15:32 -0800162
163 /* remove from the list */
164 write_lock(&cxgb3i_snic_rwlock);
Mike Christieed6f7742009-04-01 13:11:25 -0500165 list_del(&snic->list_head);
Karen Xiec3673462008-12-09 14:15:32 -0800166 write_unlock(&cxgb3i_snic_rwlock);
167
Mike Christieed6f7742009-04-01 13:11:25 -0500168 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;
Karen Xiec3673462008-12-09 14:15:32 -0800172 }
Karen Xiec3673462008-12-09 14:15:32 -0800173 }
Mike Christieed6f7742009-04-01 13:11:25 -0500174 cxgb3i_log_info("t3dev 0x%p close, snic 0x%p, %u scsi hosts removed.\n",
175 t3dev, snic, snic->hba_cnt);
176 kfree(snic);
Karen Xiec3673462008-12-09 14:15:32 -0800177}
178
179/**
Karen Xie154229a2009-03-05 14:46:08 -0600180 * cxgb3i_hba_find_by_netdev - find the cxgb3i_hba structure via net_device
Karen Xiec3673462008-12-09 14:15:32 -0800181 * @t3dev: t3cdev adapter
182 */
Mike Christie10eb0f02009-05-13 17:57:38 -0500183static struct cxgb3i_hba *cxgb3i_hba_find_by_netdev(struct net_device *ndev)
Karen Xiec3673462008-12-09 14:15:32 -0800184{
185 struct cxgb3i_adapter *snic;
186 int i;
187
Karen Xiea222ad12009-06-26 15:17:29 -0700188 if (ndev->priv_flags & IFF_802_1Q_VLAN)
189 ndev = vlan_dev_real_dev(ndev);
190
Karen Xiec3673462008-12-09 14:15:32 -0800191 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];
197 }
198 }
199 }
200 read_unlock(&cxgb3i_snic_rwlock);
201 return NULL;
202}
203
204/**
205 * cxgb3i_hba_host_add - register a new host with scsi/iscsi
206 * @snic: the cxgb3i adapter
207 * @ndev: associated net_device
208 */
209struct cxgb3i_hba *cxgb3i_hba_host_add(struct cxgb3i_adapter *snic,
210 struct net_device *ndev)
211{
212 struct cxgb3i_hba *hba;
213 struct Scsi_Host *shost;
214 int err;
215
216 shost = iscsi_host_alloc(&cxgb3i_host_template,
Mike Christie4d108352009-03-05 14:46:04 -0600217 sizeof(struct cxgb3i_hba), 1);
Karen Xiec3673462008-12-09 14:15:32 -0800218 if (!shost) {
Mike Christieed6f7742009-04-01 13:11:25 -0500219 cxgb3i_log_info("snic 0x%p, ndev 0x%p, host_alloc failed.\n",
220 snic, ndev);
Karen Xiec3673462008-12-09 14:15:32 -0800221 return NULL;
222 }
223
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;
229
230 hba = iscsi_host_priv(shost);
231 hba->snic = snic;
232 hba->ndev = ndev;
233 hba->shost = shost;
234
235 pci_dev_get(snic->pdev);
236 err = iscsi_host_add(shost, &snic->pdev->dev);
237 if (err) {
Mike Christieed6f7742009-04-01 13:11:25 -0500238 cxgb3i_log_info("snic 0x%p, ndev 0x%p, host_add failed.\n",
239 snic, ndev);
Karen Xiec3673462008-12-09 14:15:32 -0800240 goto pci_dev_put;
241 }
242
243 cxgb3i_api_debug("shost 0x%p, hba 0x%p, no %u.\n",
244 shost, hba, shost->host_no);
245
246 return hba;
247
248pci_dev_put:
249 pci_dev_put(snic->pdev);
250 scsi_host_put(shost);
251 return NULL;
252}
253
254/**
255 * cxgb3i_hba_host_remove - de-register the host with scsi/iscsi
256 * @hba: the cxgb3i hba
257 */
258void cxgb3i_hba_host_remove(struct cxgb3i_hba *hba)
259{
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);
265}
266
267/**
268 * cxgb3i_ep_connect - establish TCP connection to target portal
Mike Christie10eb0f02009-05-13 17:57:38 -0500269 * @shost: scsi host to use
Karen Xiec3673462008-12-09 14:15:32 -0800270 * @dst_addr: target IP address
271 * @non_blocking: blocking or non-blocking call
272 *
273 * Initiates a TCP/IP connection to the dst_addr
274 */
Mike Christie10eb0f02009-05-13 17:57:38 -0500275static struct iscsi_endpoint *cxgb3i_ep_connect(struct Scsi_Host *shost,
276 struct sockaddr *dst_addr,
Karen Xiec3673462008-12-09 14:15:32 -0800277 int non_blocking)
278{
279 struct iscsi_endpoint *ep;
280 struct cxgb3i_endpoint *cep;
Mike Christie10eb0f02009-05-13 17:57:38 -0500281 struct cxgb3i_hba *hba = NULL;
Karen Xiec3673462008-12-09 14:15:32 -0800282 struct s3_conn *c3cn = NULL;
283 int err = 0;
284
Mike Christie10eb0f02009-05-13 17:57:38 -0500285 if (shost)
286 hba = iscsi_host_priv(shost);
287
288 cxgb3i_api_debug("shost 0x%p, hba 0x%p.\n", shost, hba);
289
Karen Xiec3673462008-12-09 14:15:32 -0800290 c3cn = cxgb3i_c3cn_create();
291 if (!c3cn) {
292 cxgb3i_log_info("ep connect OOM.\n");
293 err = -ENOMEM;
294 goto release_conn;
295 }
296
Mike Christie10eb0f02009-05-13 17:57:38 -0500297 err = cxgb3i_c3cn_connect(hba ? hba->ndev : NULL, c3cn,
298 (struct sockaddr_in *)dst_addr);
Karen Xiec3673462008-12-09 14:15:32 -0800299 if (err < 0) {
300 cxgb3i_log_info("ep connect failed.\n");
301 goto release_conn;
302 }
Mike Christie10eb0f02009-05-13 17:57:38 -0500303
Karen Xiec3673462008-12-09 14:15:32 -0800304 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;
309 }
Mike Christie10eb0f02009-05-13 17:57:38 -0500310
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;
316 }
317
Karen Xiec3673462008-12-09 14:15:32 -0800318 if (c3cn_is_closing(c3cn)) {
319 err = -ENOSPC;
320 cxgb3i_log_info("ep connect unable to connect.\n");
321 goto release_conn;
322 }
323
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;
329 }
330 cep = ep->dd_data;
331 cep->c3cn = c3cn;
332 cep->hba = hba;
333
334 cxgb3i_api_debug("ep 0x%p, 0x%p, c3cn 0x%p, hba 0x%p.\n",
335 ep, cep, c3cn, hba);
336 return ep;
337
338release_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);
343}
344
345/**
346 * cxgb3i_ep_poll - polls for TCP connection establishement
347 * @ep: TCP connection (endpoint) handle
348 * @timeout_ms: timeout value in milli secs
349 *
350 * polls for TCP connect request to complete
351 */
352static int cxgb3i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
353{
354 struct cxgb3i_endpoint *cep = ep->dd_data;
355 struct s3_conn *c3cn = cep->c3cn;
356
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;
361}
362
363/**
364 * cxgb3i_ep_disconnect - teardown TCP connection
365 * @ep: TCP connection (endpoint) handle
366 *
367 * teardown TCP connection
368 */
369static void cxgb3i_ep_disconnect(struct iscsi_endpoint *ep)
370{
371 struct cxgb3i_endpoint *cep = ep->dd_data;
372 struct cxgb3i_conn *cconn = cep->cconn;
373
374 cxgb3i_api_debug("ep 0x%p, cep 0x%p.\n", ep, cep);
375
376 if (cconn && cconn->conn) {
377 /*
378 * stop the xmit path so the xmit_pdu function is
379 * not being called
380 */
381 iscsi_suspend_tx(cconn->conn);
382
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);
387 }
388
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);
393}
394
395/**
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
Karen Xiec3673462008-12-09 14:15:32 -0800400 *
401 * Creates a new iSCSI session
402 */
403static struct iscsi_cls_session *
404cxgb3i_session_create(struct iscsi_endpoint *ep, u16 cmds_max, u16 qdepth,
Mike Christie5e7facb2009-03-05 14:46:06 -0600405 u32 initial_cmdsn)
Karen Xiec3673462008-12-09 14:15:32 -0800406{
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;
412
413 if (!ep) {
414 cxgb3i_log_error("%s, missing endpoint.\n", __func__);
415 return NULL;
416 }
417
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));
423
Karen Xiec3673462008-12-09 14:15:32 -0800424 cls_session = iscsi_session_setup(&cxgb3i_iscsi_transport, shost,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +0530425 cmds_max, 0,
Karen Xie949847d2009-02-13 21:38:49 -0800426 sizeof(struct iscsi_tcp_task) +
427 sizeof(struct cxgb3i_task_data),
Karen Xiec3673462008-12-09 14:15:32 -0800428 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;
434
435 return cls_session;
436
437remove_session:
438 iscsi_session_teardown(cls_session);
439 return NULL;
440}
441
442/**
443 * cxgb3i_session_destroy - destroys iscsi session
444 * @cls_session: pointer to iscsi cls session
445 *
446 * Destroys an iSCSI session instance and releases its all resources held
447 */
448static void cxgb3i_session_destroy(struct iscsi_cls_session *cls_session)
449{
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);
453}
454
455/**
Karen Xie154229a2009-03-05 14:46:08 -0600456 * cxgb3i_conn_max_xmit_dlength -- calc the max. xmit pdu segment size
Karen Xiec3673462008-12-09 14:15:32 -0800457 * @conn: iscsi connection
Karen Xie154229a2009-03-05 14:46:08 -0600458 * check the max. xmit pdu payload, reduce it if needed
Karen Xiec3673462008-12-09 14:15:32 -0800459 */
460static inline int cxgb3i_conn_max_xmit_dlength(struct iscsi_conn *conn)
461
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 = max(512 * MAX_SKB_FRAGS, SKB_TX_HEADROOM);
Karen Xiec3673462008-12-09 14:15:32 -0800466
Karen Xief62d0892009-02-13 21:38:54 -0800467 max = min(cconn->hba->snic->tx_max_size, max);
Karen Xiec3673462008-12-09 14:15:32 -0800468 if (conn->max_xmit_dlength)
Karen Xief62d0892009-02-13 21:38:54 -0800469 conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
Karen Xiec3673462008-12-09 14:15:32 -0800470 else
471 conn->max_xmit_dlength = max;
472 align_pdu_size(conn->max_xmit_dlength);
Karen Xief62d0892009-02-13 21:38:54 -0800473 cxgb3i_api_debug("conn 0x%p, max xmit %u.\n",
Karen Xiec3673462008-12-09 14:15:32 -0800474 conn, conn->max_xmit_dlength);
475 return 0;
476}
477
478/**
Karen Xie154229a2009-03-05 14:46:08 -0600479 * cxgb3i_conn_max_recv_dlength -- check the max. recv pdu segment size
Karen Xiec3673462008-12-09 14:15:32 -0800480 * @conn: iscsi connection
481 * return 0 if the value is valid, < 0 otherwise.
482 */
483static inline int cxgb3i_conn_max_recv_dlength(struct iscsi_conn *conn)
484{
485 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
486 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
Karen Xief62d0892009-02-13 21:38:54 -0800487 unsigned int max = cconn->hba->snic->rx_max_size;
Karen Xiec3673462008-12-09 14:15:32 -0800488
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;
496 }
Karen Xief62d0892009-02-13 21:38:54 -0800497 conn->max_recv_dlength = min(conn->max_recv_dlength, max);
Karen Xiec3673462008-12-09 14:15:32 -0800498 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;
504}
505
506/**
507 * cxgb3i_conn_create - create iscsi connection instance
508 * @cls_session: pointer to iscsi cls session
509 * @cid: iscsi cid
510 *
511 * Creates a new iSCSI connection instance for a given session
512 */
513static struct iscsi_cls_conn *cxgb3i_conn_create(struct iscsi_cls_session
514 *cls_session, u32 cid)
515{
516 struct iscsi_cls_conn *cls_conn;
517 struct iscsi_conn *conn;
518 struct iscsi_tcp_conn *tcp_conn;
519 struct cxgb3i_conn *cconn;
520
521 cxgb3i_api_debug("sess 0x%p, cid %u.\n", cls_session, cid);
522
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;
529
530 cconn->conn = conn;
531 return cls_conn;
532}
533
534/**
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?
540 *
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
544 */
545
546static int cxgb3i_conn_bind(struct iscsi_cls_session *cls_session,
547 struct iscsi_cls_conn *cls_conn,
548 u64 transport_eph, int is_leading)
549{
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;
558
559 ep = iscsi_lookup_endpoint(transport_eph);
560 if (!ep)
561 return -EINVAL;
562
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;
570
571 cxgb3i_api_debug("ep 0x%p, cls sess 0x%p, cls conn 0x%p.\n",
572 ep, cls_session, cls_conn);
573
574 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
575 if (err)
576 return -EINVAL;
577
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);
582
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);
589
590 cxgb3i_conn_max_xmit_dlength(conn);
591 cxgb3i_conn_max_recv_dlength(conn);
592
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);
598
599 /* init recv engine */
600 iscsi_tcp_hdr_recv_prep(tcp_conn);
601
602 return 0;
603}
604
605/**
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
610 *
611 * returns iSCSI connection parameters
612 */
613static int cxgb3i_conn_get_param(struct iscsi_cls_conn *cls_conn,
614 enum iscsi_param param, char *buf)
615{
616 struct iscsi_conn *conn = cls_conn->dd_data;
617 int len;
618
619 cxgb3i_api_debug("cls_conn 0x%p, param %d.\n", cls_conn, param);
620
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);
634 }
635
636 return len;
637}
638
639/**
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
645 *
646 * set iSCSI connection parameters
647 */
648static int cxgb3i_conn_set_param(struct iscsi_cls_conn *cls_conn,
649 enum iscsi_param param, char *buf, int buflen)
650{
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;
658
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);
696 }
697 return err;
698}
699
700/**
701 * cxgb3i_host_set_param - configure host (adapter) related parameters
702 * @shost: scsi host pointer
703 * @param: parameter type identifier
704 * @buf: buffer pointer
705 */
706static int cxgb3i_host_set_param(struct Scsi_Host *shost,
707 enum iscsi_host_param param,
708 char *buf, int buflen)
709{
710 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
711
712 cxgb3i_api_debug("param %d, buf %s.\n", param, buf);
713
714 switch (param) {
715 case ISCSI_HOST_PARAM_IPADDRESS:
716 {
717 __be32 addr = in_aton(buf);
718 cxgb3i_set_private_ipv4addr(hba->ndev, addr);
719 return 0;
720 }
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);
727 }
728}
729
730/**
731 * cxgb3i_host_get_param - returns host (adapter) related parameters
732 * @shost: scsi host pointer
733 * @param: parameter type identifier
734 * @buf: buffer pointer
735 */
736static int cxgb3i_host_get_param(struct Scsi_Host *shost,
737 enum iscsi_host_param param, char *buf)
738{
739 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
740 int len = 0;
741
742 cxgb3i_api_debug("hba %s, param %d.\n", hba->ndev->name, param);
743
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:
752 {
753 __be32 addr;
754
755 addr = cxgb3i_get_private_ipv4addr(hba->ndev);
756 len = sprintf(buf, NIPQUAD_FMT, NIPQUAD(addr));
757 break;
758 }
759 default:
760 return iscsi_host_get_param(shost, param, buf);
761 }
762 return len;
763}
764
765/**
766 * cxgb3i_conn_get_stats - returns iSCSI stats
767 * @cls_conn: pointer to iscsi cls conn
768 * @stats: pointer to iscsi statistic struct
769 */
770static void cxgb3i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
771 struct iscsi_stats *stats)
772{
773 struct iscsi_conn *conn = cls_conn->dd_data;
774
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;
789}
790
791/**
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
797 */
798static void cxgb3i_parse_itt(struct iscsi_conn *conn, itt_t itt,
799 int *idx, int *age)
800{
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;
806
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;
812
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);
816}
817
818/**
819 * cxgb3i_reserve_itt - generate tag for a give task
Karen Xiec3673462008-12-09 14:15:32 -0800820 * @task: iscsi task
821 * @hdr_itt: tag, filled in by this function
Karen Xie154229a2009-03-05 14:46:08 -0600822 * Set up ddp for scsi read tasks if possible.
Karen Xiec3673462008-12-09 14:15:32 -0800823 */
824int cxgb3i_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
825{
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;
836
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;
842
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);
855 }
856 }
857
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);
862
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;
866}
867
868/**
869 * cxgb3i_release_itt - release the tag for a given task
Karen Xiec3673462008-12-09 14:15:32 -0800870 * @task: iscsi task
871 * @hdr_itt: tag
Karen Xie154229a2009-03-05 14:46:08 -0600872 * If the tag is a ddp tag, release the ddp setup
Karen Xiec3673462008-12-09 14:15:32 -0800873 */
874void cxgb3i_release_itt(struct iscsi_task *task, itt_t hdr_itt)
875{
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);
882
883 cxgb3i_tag_debug("release tag 0x%x.\n", tag);
884
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);
889}
890
891/**
892 * cxgb3i_host_template -- Scsi_Host_Template structure
893 * used when registering with the scsi mid layer
894 */
895static 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,
Mike Christiedd0af9f2009-04-21 15:32:33 -0500901 .can_queue = CXGB3I_SCSI_HOST_QDEPTH,
Karen Xiec3673462008-12-09 14:15:32 -0800902 .sg_tablesize = SG_ALL,
903 .max_sectors = 0xFFFF,
Mike Christiedd0af9f2009-04-21 15:32:33 -0500904 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
Karen Xiec3673462008-12-09 14:15:32 -0800905 .eh_abort_handler = iscsi_eh_abort,
906 .eh_device_reset_handler = iscsi_eh_device_reset,
907 .eh_target_reset_handler = iscsi_eh_target_reset,
Mike Christie6b5d6c42009-04-21 15:32:32 -0500908 .target_alloc = iscsi_target_alloc,
Karen Xiec3673462008-12-09 14:15:32 -0800909 .use_clustering = DISABLE_CLUSTERING,
910 .this_id = -1,
911};
912
913static 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,
966
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,
972
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,
979};
980
981int cxgb3i_iscsi_init(void)
982{
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);
988
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;
994 }
995 cxgb3i_api_debug("cxgb3i transport 0x%p.\n", cxgb3i_scsi_transport);
996 return 0;
997}
998
999void cxgb3i_iscsi_cleanup(void)
1000{
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);
1005 }
1006}