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