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