blob: 04a43744aedfc589de6b232161eb4b071bfb3110 [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 */
Mike Christie10eb0f02009-05-13 17:57:38 -0500181static struct cxgb3i_hba *cxgb3i_hba_find_by_netdev(struct net_device *ndev)
Karen Xiec3673462008-12-09 14:15:32 -0800182{
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
Mike Christie10eb0f02009-05-13 17:57:38 -0500264 * @shost: scsi host to use
Karen Xiec3673462008-12-09 14:15:32 -0800265 * @dst_addr: target IP address
266 * @non_blocking: blocking or non-blocking call
267 *
268 * Initiates a TCP/IP connection to the dst_addr
269 */
Mike Christie10eb0f02009-05-13 17:57:38 -0500270static struct iscsi_endpoint *cxgb3i_ep_connect(struct Scsi_Host *shost,
271 struct sockaddr *dst_addr,
Karen Xiec3673462008-12-09 14:15:32 -0800272 int non_blocking)
273{
274 struct iscsi_endpoint *ep;
275 struct cxgb3i_endpoint *cep;
Mike Christie10eb0f02009-05-13 17:57:38 -0500276 struct cxgb3i_hba *hba = NULL;
Karen Xiec3673462008-12-09 14:15:32 -0800277 struct s3_conn *c3cn = NULL;
278 int err = 0;
279
Mike Christie10eb0f02009-05-13 17:57:38 -0500280 if (shost)
281 hba = iscsi_host_priv(shost);
282
283 cxgb3i_api_debug("shost 0x%p, hba 0x%p.\n", shost, hba);
284
Karen Xiec3673462008-12-09 14:15:32 -0800285 c3cn = cxgb3i_c3cn_create();
286 if (!c3cn) {
287 cxgb3i_log_info("ep connect OOM.\n");
288 err = -ENOMEM;
289 goto release_conn;
290 }
291
Mike Christie10eb0f02009-05-13 17:57:38 -0500292 err = cxgb3i_c3cn_connect(hba ? hba->ndev : NULL, c3cn,
293 (struct sockaddr_in *)dst_addr);
Karen Xiec3673462008-12-09 14:15:32 -0800294 if (err < 0) {
295 cxgb3i_log_info("ep connect failed.\n");
296 goto release_conn;
297 }
Mike Christie10eb0f02009-05-13 17:57:38 -0500298
Karen Xiec3673462008-12-09 14:15:32 -0800299 hba = cxgb3i_hba_find_by_netdev(c3cn->dst_cache->dev);
300 if (!hba) {
301 err = -ENOSPC;
302 cxgb3i_log_info("NOT going through cxgbi device.\n");
303 goto release_conn;
304 }
Mike Christie10eb0f02009-05-13 17:57:38 -0500305
306 if (shost && hba != iscsi_host_priv(shost)) {
307 err = -ENOSPC;
308 cxgb3i_log_info("Could not connect through request host%u\n",
309 shost->host_no);
310 goto release_conn;
311 }
312
Karen Xiec3673462008-12-09 14:15:32 -0800313 if (c3cn_is_closing(c3cn)) {
314 err = -ENOSPC;
315 cxgb3i_log_info("ep connect unable to connect.\n");
316 goto release_conn;
317 }
318
319 ep = iscsi_create_endpoint(sizeof(*cep));
320 if (!ep) {
321 err = -ENOMEM;
322 cxgb3i_log_info("iscsi alloc ep, OOM.\n");
323 goto release_conn;
324 }
325 cep = ep->dd_data;
326 cep->c3cn = c3cn;
327 cep->hba = hba;
328
329 cxgb3i_api_debug("ep 0x%p, 0x%p, c3cn 0x%p, hba 0x%p.\n",
330 ep, cep, c3cn, hba);
331 return ep;
332
333release_conn:
334 cxgb3i_api_debug("conn 0x%p failed, release.\n", c3cn);
335 if (c3cn)
336 cxgb3i_c3cn_release(c3cn);
337 return ERR_PTR(err);
338}
339
340/**
341 * cxgb3i_ep_poll - polls for TCP connection establishement
342 * @ep: TCP connection (endpoint) handle
343 * @timeout_ms: timeout value in milli secs
344 *
345 * polls for TCP connect request to complete
346 */
347static int cxgb3i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
348{
349 struct cxgb3i_endpoint *cep = ep->dd_data;
350 struct s3_conn *c3cn = cep->c3cn;
351
352 if (!c3cn_is_established(c3cn))
353 return 0;
354 cxgb3i_api_debug("ep 0x%p, c3cn 0x%p established.\n", ep, c3cn);
355 return 1;
356}
357
358/**
359 * cxgb3i_ep_disconnect - teardown TCP connection
360 * @ep: TCP connection (endpoint) handle
361 *
362 * teardown TCP connection
363 */
364static void cxgb3i_ep_disconnect(struct iscsi_endpoint *ep)
365{
366 struct cxgb3i_endpoint *cep = ep->dd_data;
367 struct cxgb3i_conn *cconn = cep->cconn;
368
369 cxgb3i_api_debug("ep 0x%p, cep 0x%p.\n", ep, cep);
370
371 if (cconn && cconn->conn) {
372 /*
373 * stop the xmit path so the xmit_pdu function is
374 * not being called
375 */
376 iscsi_suspend_tx(cconn->conn);
377
378 write_lock_bh(&cep->c3cn->callback_lock);
379 cep->c3cn->user_data = NULL;
380 cconn->cep = NULL;
381 write_unlock_bh(&cep->c3cn->callback_lock);
382 }
383
384 cxgb3i_api_debug("ep 0x%p, cep 0x%p, release c3cn 0x%p.\n",
385 ep, cep, cep->c3cn);
386 cxgb3i_c3cn_release(cep->c3cn);
387 iscsi_destroy_endpoint(ep);
388}
389
390/**
391 * cxgb3i_session_create - create a new iscsi session
392 * @cmds_max: max # of commands
393 * @qdepth: scsi queue depth
394 * @initial_cmdsn: initial iscsi CMDSN for this session
Karen Xiec3673462008-12-09 14:15:32 -0800395 *
396 * Creates a new iSCSI session
397 */
398static struct iscsi_cls_session *
399cxgb3i_session_create(struct iscsi_endpoint *ep, u16 cmds_max, u16 qdepth,
Mike Christie5e7facb2009-03-05 14:46:06 -0600400 u32 initial_cmdsn)
Karen Xiec3673462008-12-09 14:15:32 -0800401{
402 struct cxgb3i_endpoint *cep;
403 struct cxgb3i_hba *hba;
404 struct Scsi_Host *shost;
405 struct iscsi_cls_session *cls_session;
406 struct iscsi_session *session;
407
408 if (!ep) {
409 cxgb3i_log_error("%s, missing endpoint.\n", __func__);
410 return NULL;
411 }
412
413 cep = ep->dd_data;
414 hba = cep->hba;
415 shost = hba->shost;
416 cxgb3i_api_debug("ep 0x%p, cep 0x%p, hba 0x%p.\n", ep, cep, hba);
417 BUG_ON(hba != iscsi_host_priv(shost));
418
Karen Xiec3673462008-12-09 14:15:32 -0800419 cls_session = iscsi_session_setup(&cxgb3i_iscsi_transport, shost,
420 cmds_max,
Karen Xie949847d2009-02-13 21:38:49 -0800421 sizeof(struct iscsi_tcp_task) +
422 sizeof(struct cxgb3i_task_data),
Karen Xiec3673462008-12-09 14:15:32 -0800423 initial_cmdsn, ISCSI_MAX_TARGET);
424 if (!cls_session)
425 return NULL;
426 session = cls_session->dd_data;
427 if (iscsi_tcp_r2tpool_alloc(session))
428 goto remove_session;
429
430 return cls_session;
431
432remove_session:
433 iscsi_session_teardown(cls_session);
434 return NULL;
435}
436
437/**
438 * cxgb3i_session_destroy - destroys iscsi session
439 * @cls_session: pointer to iscsi cls session
440 *
441 * Destroys an iSCSI session instance and releases its all resources held
442 */
443static void cxgb3i_session_destroy(struct iscsi_cls_session *cls_session)
444{
445 cxgb3i_api_debug("sess 0x%p.\n", cls_session);
446 iscsi_tcp_r2tpool_free(cls_session->dd_data);
447 iscsi_session_teardown(cls_session);
448}
449
450/**
Karen Xie154229a2009-03-05 14:46:08 -0600451 * cxgb3i_conn_max_xmit_dlength -- calc the max. xmit pdu segment size
Karen Xiec3673462008-12-09 14:15:32 -0800452 * @conn: iscsi connection
Karen Xie154229a2009-03-05 14:46:08 -0600453 * check the max. xmit pdu payload, reduce it if needed
Karen Xiec3673462008-12-09 14:15:32 -0800454 */
455static inline int cxgb3i_conn_max_xmit_dlength(struct iscsi_conn *conn)
456
457{
458 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
459 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
Karen Xief62d0892009-02-13 21:38:54 -0800460 unsigned int max = max(512 * MAX_SKB_FRAGS, SKB_TX_HEADROOM);
Karen Xiec3673462008-12-09 14:15:32 -0800461
Karen Xief62d0892009-02-13 21:38:54 -0800462 max = min(cconn->hba->snic->tx_max_size, max);
Karen Xiec3673462008-12-09 14:15:32 -0800463 if (conn->max_xmit_dlength)
Karen Xief62d0892009-02-13 21:38:54 -0800464 conn->max_xmit_dlength = min(conn->max_xmit_dlength, max);
Karen Xiec3673462008-12-09 14:15:32 -0800465 else
466 conn->max_xmit_dlength = max;
467 align_pdu_size(conn->max_xmit_dlength);
Karen Xief62d0892009-02-13 21:38:54 -0800468 cxgb3i_api_debug("conn 0x%p, max xmit %u.\n",
Karen Xiec3673462008-12-09 14:15:32 -0800469 conn, conn->max_xmit_dlength);
470 return 0;
471}
472
473/**
Karen Xie154229a2009-03-05 14:46:08 -0600474 * cxgb3i_conn_max_recv_dlength -- check the max. recv pdu segment size
Karen Xiec3673462008-12-09 14:15:32 -0800475 * @conn: iscsi connection
476 * return 0 if the value is valid, < 0 otherwise.
477 */
478static inline int cxgb3i_conn_max_recv_dlength(struct iscsi_conn *conn)
479{
480 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
481 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
Karen Xief62d0892009-02-13 21:38:54 -0800482 unsigned int max = cconn->hba->snic->rx_max_size;
Karen Xiec3673462008-12-09 14:15:32 -0800483
484 align_pdu_size(max);
485 if (conn->max_recv_dlength) {
486 if (conn->max_recv_dlength > max) {
487 cxgb3i_log_error("MaxRecvDataSegmentLength %u too big."
488 " Need to be <= %u.\n",
489 conn->max_recv_dlength, max);
490 return -EINVAL;
491 }
Karen Xief62d0892009-02-13 21:38:54 -0800492 conn->max_recv_dlength = min(conn->max_recv_dlength, max);
Karen Xiec3673462008-12-09 14:15:32 -0800493 align_pdu_size(conn->max_recv_dlength);
494 } else
495 conn->max_recv_dlength = max;
496 cxgb3i_api_debug("conn 0x%p, max recv %u.\n",
497 conn, conn->max_recv_dlength);
498 return 0;
499}
500
501/**
502 * cxgb3i_conn_create - create iscsi connection instance
503 * @cls_session: pointer to iscsi cls session
504 * @cid: iscsi cid
505 *
506 * Creates a new iSCSI connection instance for a given session
507 */
508static struct iscsi_cls_conn *cxgb3i_conn_create(struct iscsi_cls_session
509 *cls_session, u32 cid)
510{
511 struct iscsi_cls_conn *cls_conn;
512 struct iscsi_conn *conn;
513 struct iscsi_tcp_conn *tcp_conn;
514 struct cxgb3i_conn *cconn;
515
516 cxgb3i_api_debug("sess 0x%p, cid %u.\n", cls_session, cid);
517
518 cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*cconn), cid);
519 if (!cls_conn)
520 return NULL;
521 conn = cls_conn->dd_data;
522 tcp_conn = conn->dd_data;
523 cconn = tcp_conn->dd_data;
524
525 cconn->conn = conn;
526 return cls_conn;
527}
528
529/**
530 * cxgb3i_conn_bind - binds iscsi sess, conn and endpoint together
531 * @cls_session: pointer to iscsi cls session
532 * @cls_conn: pointer to iscsi cls conn
533 * @transport_eph: 64-bit EP handle
534 * @is_leading: leading connection on this session?
535 *
536 * Binds together an iSCSI session, an iSCSI connection and a
537 * TCP connection. This routine returns error code if the TCP
538 * connection does not belong on the device iSCSI sess/conn is bound
539 */
540
541static int cxgb3i_conn_bind(struct iscsi_cls_session *cls_session,
542 struct iscsi_cls_conn *cls_conn,
543 u64 transport_eph, int is_leading)
544{
545 struct iscsi_conn *conn = cls_conn->dd_data;
546 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
547 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
548 struct cxgb3i_adapter *snic;
549 struct iscsi_endpoint *ep;
550 struct cxgb3i_endpoint *cep;
551 struct s3_conn *c3cn;
552 int err;
553
554 ep = iscsi_lookup_endpoint(transport_eph);
555 if (!ep)
556 return -EINVAL;
557
558 /* setup ddp pagesize */
559 cep = ep->dd_data;
560 c3cn = cep->c3cn;
561 snic = cep->hba->snic;
562 err = cxgb3i_setup_conn_host_pagesize(snic->tdev, c3cn->tid, 0);
563 if (err < 0)
564 return err;
565
566 cxgb3i_api_debug("ep 0x%p, cls sess 0x%p, cls conn 0x%p.\n",
567 ep, cls_session, cls_conn);
568
569 err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
570 if (err)
571 return -EINVAL;
572
573 /* calculate the tag idx bits needed for this conn based on cmds_max */
574 cconn->task_idx_bits = (__ilog2_u32(conn->session->cmds_max - 1)) + 1;
575 cxgb3i_api_debug("session cmds_max 0x%x, bits %u.\n",
576 conn->session->cmds_max, cconn->task_idx_bits);
577
578 read_lock(&c3cn->callback_lock);
579 c3cn->user_data = conn;
580 cconn->hba = cep->hba;
581 cconn->cep = cep;
582 cep->cconn = cconn;
583 read_unlock(&c3cn->callback_lock);
584
585 cxgb3i_conn_max_xmit_dlength(conn);
586 cxgb3i_conn_max_recv_dlength(conn);
587
588 spin_lock_bh(&conn->session->lock);
589 sprintf(conn->portal_address, NIPQUAD_FMT,
590 NIPQUAD(c3cn->daddr.sin_addr.s_addr));
591 conn->portal_port = ntohs(c3cn->daddr.sin_port);
592 spin_unlock_bh(&conn->session->lock);
593
594 /* init recv engine */
595 iscsi_tcp_hdr_recv_prep(tcp_conn);
596
597 return 0;
598}
599
600/**
601 * cxgb3i_conn_get_param - return iscsi connection parameter to caller
602 * @cls_conn: pointer to iscsi cls conn
603 * @param: parameter type identifier
604 * @buf: buffer pointer
605 *
606 * returns iSCSI connection parameters
607 */
608static int cxgb3i_conn_get_param(struct iscsi_cls_conn *cls_conn,
609 enum iscsi_param param, char *buf)
610{
611 struct iscsi_conn *conn = cls_conn->dd_data;
612 int len;
613
614 cxgb3i_api_debug("cls_conn 0x%p, param %d.\n", cls_conn, param);
615
616 switch (param) {
617 case ISCSI_PARAM_CONN_PORT:
618 spin_lock_bh(&conn->session->lock);
619 len = sprintf(buf, "%hu\n", conn->portal_port);
620 spin_unlock_bh(&conn->session->lock);
621 break;
622 case ISCSI_PARAM_CONN_ADDRESS:
623 spin_lock_bh(&conn->session->lock);
624 len = sprintf(buf, "%s\n", conn->portal_address);
625 spin_unlock_bh(&conn->session->lock);
626 break;
627 default:
628 return iscsi_conn_get_param(cls_conn, param, buf);
629 }
630
631 return len;
632}
633
634/**
635 * cxgb3i_conn_set_param - set iscsi connection parameter
636 * @cls_conn: pointer to iscsi cls conn
637 * @param: parameter type identifier
638 * @buf: buffer pointer
639 * @buflen: buffer length
640 *
641 * set iSCSI connection parameters
642 */
643static int cxgb3i_conn_set_param(struct iscsi_cls_conn *cls_conn,
644 enum iscsi_param param, char *buf, int buflen)
645{
646 struct iscsi_conn *conn = cls_conn->dd_data;
647 struct iscsi_session *session = conn->session;
648 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
649 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
650 struct cxgb3i_adapter *snic = cconn->hba->snic;
651 struct s3_conn *c3cn = cconn->cep->c3cn;
652 int value, err = 0;
653
654 switch (param) {
655 case ISCSI_PARAM_HDRDGST_EN:
656 err = iscsi_set_param(cls_conn, param, buf, buflen);
657 if (!err && conn->hdrdgst_en)
658 err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
659 conn->hdrdgst_en,
660 conn->datadgst_en, 0);
661 break;
662 case ISCSI_PARAM_DATADGST_EN:
663 err = iscsi_set_param(cls_conn, param, buf, buflen);
664 if (!err && conn->datadgst_en)
665 err = cxgb3i_setup_conn_digest(snic->tdev, c3cn->tid,
666 conn->hdrdgst_en,
667 conn->datadgst_en, 0);
668 break;
669 case ISCSI_PARAM_MAX_R2T:
670 sscanf(buf, "%d", &value);
671 if (value <= 0 || !is_power_of_2(value))
672 return -EINVAL;
673 if (session->max_r2t == value)
674 break;
675 iscsi_tcp_r2tpool_free(session);
676 err = iscsi_set_param(cls_conn, param, buf, buflen);
677 if (!err && iscsi_tcp_r2tpool_alloc(session))
678 return -ENOMEM;
679 case ISCSI_PARAM_MAX_RECV_DLENGTH:
680 err = iscsi_set_param(cls_conn, param, buf, buflen);
681 if (!err)
682 err = cxgb3i_conn_max_recv_dlength(conn);
683 break;
684 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
685 err = iscsi_set_param(cls_conn, param, buf, buflen);
686 if (!err)
687 err = cxgb3i_conn_max_xmit_dlength(conn);
688 break;
689 default:
690 return iscsi_set_param(cls_conn, param, buf, buflen);
691 }
692 return err;
693}
694
695/**
696 * cxgb3i_host_set_param - configure host (adapter) related parameters
697 * @shost: scsi host pointer
698 * @param: parameter type identifier
699 * @buf: buffer pointer
700 */
701static int cxgb3i_host_set_param(struct Scsi_Host *shost,
702 enum iscsi_host_param param,
703 char *buf, int buflen)
704{
705 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
706
707 cxgb3i_api_debug("param %d, buf %s.\n", param, buf);
708
709 switch (param) {
710 case ISCSI_HOST_PARAM_IPADDRESS:
711 {
712 __be32 addr = in_aton(buf);
713 cxgb3i_set_private_ipv4addr(hba->ndev, addr);
714 return 0;
715 }
716 case ISCSI_HOST_PARAM_HWADDRESS:
717 case ISCSI_HOST_PARAM_NETDEV_NAME:
718 /* ignore */
719 return 0;
720 default:
721 return iscsi_host_set_param(shost, param, buf, buflen);
722 }
723}
724
725/**
726 * cxgb3i_host_get_param - returns host (adapter) related parameters
727 * @shost: scsi host pointer
728 * @param: parameter type identifier
729 * @buf: buffer pointer
730 */
731static int cxgb3i_host_get_param(struct Scsi_Host *shost,
732 enum iscsi_host_param param, char *buf)
733{
734 struct cxgb3i_hba *hba = iscsi_host_priv(shost);
735 int len = 0;
736
737 cxgb3i_api_debug("hba %s, param %d.\n", hba->ndev->name, param);
738
739 switch (param) {
740 case ISCSI_HOST_PARAM_HWADDRESS:
741 len = sysfs_format_mac(buf, hba->ndev->dev_addr, 6);
742 break;
743 case ISCSI_HOST_PARAM_NETDEV_NAME:
744 len = sprintf(buf, "%s\n", hba->ndev->name);
745 break;
746 case ISCSI_HOST_PARAM_IPADDRESS:
747 {
748 __be32 addr;
749
750 addr = cxgb3i_get_private_ipv4addr(hba->ndev);
751 len = sprintf(buf, NIPQUAD_FMT, NIPQUAD(addr));
752 break;
753 }
754 default:
755 return iscsi_host_get_param(shost, param, buf);
756 }
757 return len;
758}
759
760/**
761 * cxgb3i_conn_get_stats - returns iSCSI stats
762 * @cls_conn: pointer to iscsi cls conn
763 * @stats: pointer to iscsi statistic struct
764 */
765static void cxgb3i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
766 struct iscsi_stats *stats)
767{
768 struct iscsi_conn *conn = cls_conn->dd_data;
769
770 stats->txdata_octets = conn->txdata_octets;
771 stats->rxdata_octets = conn->rxdata_octets;
772 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
773 stats->dataout_pdus = conn->dataout_pdus_cnt;
774 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
775 stats->datain_pdus = conn->datain_pdus_cnt;
776 stats->r2t_pdus = conn->r2t_pdus_cnt;
777 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
778 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
779 stats->digest_err = 0;
780 stats->timeout_err = 0;
781 stats->custom_length = 1;
782 strcpy(stats->custom[0].desc, "eh_abort_cnt");
783 stats->custom[0].value = conn->eh_abort_cnt;
784}
785
786/**
787 * cxgb3i_parse_itt - get the idx and age bits from a given tag
788 * @conn: iscsi connection
789 * @itt: itt tag
790 * @idx: task index, filled in by this function
791 * @age: session age, filled in by this function
792 */
793static void cxgb3i_parse_itt(struct iscsi_conn *conn, itt_t itt,
794 int *idx, int *age)
795{
796 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
797 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
798 struct cxgb3i_adapter *snic = cconn->hba->snic;
799 u32 tag = ntohl((__force u32) itt);
800 u32 sw_bits;
801
802 sw_bits = cxgb3i_tag_nonrsvd_bits(&snic->tag_format, tag);
803 if (idx)
804 *idx = sw_bits & ((1 << cconn->task_idx_bits) - 1);
805 if (age)
806 *age = (sw_bits >> cconn->task_idx_bits) & ISCSI_AGE_MASK;
807
808 cxgb3i_tag_debug("parse tag 0x%x/0x%x, sw 0x%x, itt 0x%x, age 0x%x.\n",
809 tag, itt, sw_bits, idx ? *idx : 0xFFFFF,
810 age ? *age : 0xFF);
811}
812
813/**
814 * cxgb3i_reserve_itt - generate tag for a give task
Karen Xiec3673462008-12-09 14:15:32 -0800815 * @task: iscsi task
816 * @hdr_itt: tag, filled in by this function
Karen Xie154229a2009-03-05 14:46:08 -0600817 * Set up ddp for scsi read tasks if possible.
Karen Xiec3673462008-12-09 14:15:32 -0800818 */
819int cxgb3i_reserve_itt(struct iscsi_task *task, itt_t *hdr_itt)
820{
821 struct scsi_cmnd *sc = task->sc;
822 struct iscsi_conn *conn = task->conn;
823 struct iscsi_session *sess = conn->session;
824 struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
825 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
826 struct cxgb3i_adapter *snic = cconn->hba->snic;
827 struct cxgb3i_tag_format *tformat = &snic->tag_format;
828 u32 sw_tag = (sess->age << cconn->task_idx_bits) | task->itt;
829 u32 tag;
830 int err = -EINVAL;
831
832 if (sc &&
833 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
834 cxgb3i_sw_tag_usable(tformat, sw_tag)) {
835 struct s3_conn *c3cn = cconn->cep->c3cn;
836 struct cxgb3i_gather_list *gl;
837
838 gl = cxgb3i_ddp_make_gl(scsi_in(sc)->length,
839 scsi_in(sc)->table.sgl,
840 scsi_in(sc)->table.nents,
841 snic->pdev,
842 GFP_ATOMIC);
843 if (gl) {
844 tag = sw_tag;
845 err = cxgb3i_ddp_tag_reserve(snic->tdev, c3cn->tid,
846 tformat, &tag,
847 gl, GFP_ATOMIC);
848 if (err < 0)
849 cxgb3i_ddp_release_gl(gl, snic->pdev);
850 }
851 }
852
853 if (err < 0)
854 tag = cxgb3i_set_non_ddp_tag(tformat, sw_tag);
855 /* the itt need to sent in big-endian order */
856 *hdr_itt = (__force itt_t)htonl(tag);
857
858 cxgb3i_tag_debug("new tag 0x%x/0x%x (itt 0x%x, age 0x%x).\n",
859 tag, *hdr_itt, task->itt, sess->age);
860 return 0;
861}
862
863/**
864 * cxgb3i_release_itt - release the tag for a given task
Karen Xiec3673462008-12-09 14:15:32 -0800865 * @task: iscsi task
866 * @hdr_itt: tag
Karen Xie154229a2009-03-05 14:46:08 -0600867 * If the tag is a ddp tag, release the ddp setup
Karen Xiec3673462008-12-09 14:15:32 -0800868 */
869void cxgb3i_release_itt(struct iscsi_task *task, itt_t hdr_itt)
870{
871 struct scsi_cmnd *sc = task->sc;
872 struct iscsi_tcp_conn *tcp_conn = task->conn->dd_data;
873 struct cxgb3i_conn *cconn = tcp_conn->dd_data;
874 struct cxgb3i_adapter *snic = cconn->hba->snic;
875 struct cxgb3i_tag_format *tformat = &snic->tag_format;
876 u32 tag = ntohl((__force u32)hdr_itt);
877
878 cxgb3i_tag_debug("release tag 0x%x.\n", tag);
879
880 if (sc &&
881 (scsi_bidi_cmnd(sc) || sc->sc_data_direction == DMA_FROM_DEVICE) &&
882 cxgb3i_is_ddp_tag(tformat, tag))
883 cxgb3i_ddp_tag_release(snic->tdev, tag);
884}
885
886/**
887 * cxgb3i_host_template -- Scsi_Host_Template structure
888 * used when registering with the scsi mid layer
889 */
890static struct scsi_host_template cxgb3i_host_template = {
891 .module = THIS_MODULE,
892 .name = "Chelsio S3xx iSCSI Initiator",
893 .proc_name = "cxgb3i",
894 .queuecommand = iscsi_queuecommand,
895 .change_queue_depth = iscsi_change_queue_depth,
Mike Christiedd0af9f2009-04-21 15:32:33 -0500896 .can_queue = CXGB3I_SCSI_HOST_QDEPTH,
Karen Xiec3673462008-12-09 14:15:32 -0800897 .sg_tablesize = SG_ALL,
898 .max_sectors = 0xFFFF,
Mike Christiedd0af9f2009-04-21 15:32:33 -0500899 .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
Karen Xiec3673462008-12-09 14:15:32 -0800900 .eh_abort_handler = iscsi_eh_abort,
901 .eh_device_reset_handler = iscsi_eh_device_reset,
902 .eh_target_reset_handler = iscsi_eh_target_reset,
Mike Christie6b5d6c42009-04-21 15:32:32 -0500903 .target_alloc = iscsi_target_alloc,
Karen Xiec3673462008-12-09 14:15:32 -0800904 .use_clustering = DISABLE_CLUSTERING,
905 .this_id = -1,
906};
907
908static struct iscsi_transport cxgb3i_iscsi_transport = {
909 .owner = THIS_MODULE,
910 .name = "cxgb3i",
911 .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
912 | CAP_DATADGST | CAP_DIGEST_OFFLOAD |
913 CAP_PADDING_OFFLOAD,
914 .param_mask = ISCSI_MAX_RECV_DLENGTH |
915 ISCSI_MAX_XMIT_DLENGTH |
916 ISCSI_HDRDGST_EN |
917 ISCSI_DATADGST_EN |
918 ISCSI_INITIAL_R2T_EN |
919 ISCSI_MAX_R2T |
920 ISCSI_IMM_DATA_EN |
921 ISCSI_FIRST_BURST |
922 ISCSI_MAX_BURST |
923 ISCSI_PDU_INORDER_EN |
924 ISCSI_DATASEQ_INORDER_EN |
925 ISCSI_ERL |
926 ISCSI_CONN_PORT |
927 ISCSI_CONN_ADDRESS |
928 ISCSI_EXP_STATSN |
929 ISCSI_PERSISTENT_PORT |
930 ISCSI_PERSISTENT_ADDRESS |
931 ISCSI_TARGET_NAME | ISCSI_TPGT |
932 ISCSI_USERNAME | ISCSI_PASSWORD |
933 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
934 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
935 ISCSI_LU_RESET_TMO |
936 ISCSI_PING_TMO | ISCSI_RECV_TMO |
937 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
938 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
939 ISCSI_HOST_INITIATOR_NAME | ISCSI_HOST_NETDEV_NAME,
940 .get_host_param = cxgb3i_host_get_param,
941 .set_host_param = cxgb3i_host_set_param,
942 /* session management */
943 .create_session = cxgb3i_session_create,
944 .destroy_session = cxgb3i_session_destroy,
945 .get_session_param = iscsi_session_get_param,
946 /* connection management */
947 .create_conn = cxgb3i_conn_create,
948 .bind_conn = cxgb3i_conn_bind,
949 .destroy_conn = iscsi_tcp_conn_teardown,
950 .start_conn = iscsi_conn_start,
951 .stop_conn = iscsi_conn_stop,
952 .get_conn_param = cxgb3i_conn_get_param,
953 .set_param = cxgb3i_conn_set_param,
954 .get_stats = cxgb3i_conn_get_stats,
955 /* pdu xmit req. from user space */
956 .send_pdu = iscsi_conn_send_pdu,
957 /* task */
958 .init_task = iscsi_tcp_task_init,
959 .xmit_task = iscsi_tcp_task_xmit,
960 .cleanup_task = cxgb3i_conn_cleanup_task,
961
962 /* pdu */
963 .alloc_pdu = cxgb3i_conn_alloc_pdu,
964 .init_pdu = cxgb3i_conn_init_pdu,
965 .xmit_pdu = cxgb3i_conn_xmit_pdu,
966 .parse_pdu_itt = cxgb3i_parse_itt,
967
968 /* TCP connect/disconnect */
969 .ep_connect = cxgb3i_ep_connect,
970 .ep_poll = cxgb3i_ep_poll,
971 .ep_disconnect = cxgb3i_ep_disconnect,
972 /* Error recovery timeout call */
973 .session_recovery_timedout = iscsi_session_recovery_timedout,
974};
975
976int cxgb3i_iscsi_init(void)
977{
978 sw_tag_idx_bits = (__ilog2_u32(ISCSI_ITT_MASK)) + 1;
979 sw_tag_age_bits = (__ilog2_u32(ISCSI_AGE_MASK)) + 1;
980 cxgb3i_log_info("tag itt 0x%x, %u bits, age 0x%x, %u bits.\n",
981 ISCSI_ITT_MASK, sw_tag_idx_bits,
982 ISCSI_AGE_MASK, sw_tag_age_bits);
983
984 cxgb3i_scsi_transport =
985 iscsi_register_transport(&cxgb3i_iscsi_transport);
986 if (!cxgb3i_scsi_transport) {
987 cxgb3i_log_error("Could not register cxgb3i transport.\n");
988 return -ENODEV;
989 }
990 cxgb3i_api_debug("cxgb3i transport 0x%p.\n", cxgb3i_scsi_transport);
991 return 0;
992}
993
994void cxgb3i_iscsi_cleanup(void)
995{
996 if (cxgb3i_scsi_transport) {
997 cxgb3i_api_debug("cxgb3i transport 0x%p.\n",
998 cxgb3i_scsi_transport);
999 iscsi_unregister_transport(&cxgb3i_iscsi_transport);
1000 }
1001}