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