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