Merge branches 'roccat', 'upstream' and 'wiimote' into for-linus
[pandora-kernel.git] / drivers / scsi / bnx2i / bnx2i_iscsi.c
1 /*
2  * bnx2i_iscsi.c: Broadcom NetXtreme II iSCSI driver.
3  *
4  * Copyright (c) 2006 - 2010 Broadcom Corporation
5  * Copyright (c) 2007, 2008 Red Hat, Inc.  All rights reserved.
6  * Copyright (c) 2007, 2008 Mike Christie
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation.
11  *
12  * Written by: Anil Veerabhadrappa (anilgv@broadcom.com)
13  * Maintained by: Eddie Wai (eddie.wai@broadcom.com)
14  */
15
16 #include <linux/slab.h>
17 #include <scsi/scsi_tcq.h>
18 #include <scsi/libiscsi.h>
19 #include "bnx2i.h"
20
21 struct scsi_transport_template *bnx2i_scsi_xport_template;
22 struct iscsi_transport bnx2i_iscsi_transport;
23 static struct scsi_host_template bnx2i_host_template;
24
25 /*
26  * Global endpoint resource info
27  */
28 static DEFINE_SPINLOCK(bnx2i_resc_lock); /* protects global resources */
29
30
31 static int bnx2i_adapter_ready(struct bnx2i_hba *hba)
32 {
33         int retval = 0;
34
35         if (!hba || !test_bit(ADAPTER_STATE_UP, &hba->adapter_state) ||
36             test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state) ||
37             test_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state))
38                 retval = -EPERM;
39         return retval;
40 }
41
42 /**
43  * bnx2i_get_write_cmd_bd_idx - identifies various BD bookmarks
44  * @cmd:                iscsi cmd struct pointer
45  * @buf_off:            absolute buffer offset
46  * @start_bd_off:       u32 pointer to return the offset within the BD
47  *                      indicated by 'start_bd_idx' on which 'buf_off' falls
48  * @start_bd_idx:       index of the BD on which 'buf_off' falls
49  *
50  * identifies & marks various bd info for scsi command's imm data,
51  * unsolicited data and the first solicited data seq.
52  */
53 static void bnx2i_get_write_cmd_bd_idx(struct bnx2i_cmd *cmd, u32 buf_off,
54                                        u32 *start_bd_off, u32 *start_bd_idx)
55 {
56         struct iscsi_bd *bd_tbl = cmd->io_tbl.bd_tbl;
57         u32 cur_offset = 0;
58         u32 cur_bd_idx = 0;
59
60         if (buf_off) {
61                 while (buf_off >= (cur_offset + bd_tbl->buffer_length)) {
62                         cur_offset += bd_tbl->buffer_length;
63                         cur_bd_idx++;
64                         bd_tbl++;
65                 }
66         }
67
68         *start_bd_off = buf_off - cur_offset;
69         *start_bd_idx = cur_bd_idx;
70 }
71
72 /**
73  * bnx2i_setup_write_cmd_bd_info - sets up BD various information
74  * @task:       transport layer's cmd struct pointer
75  *
76  * identifies & marks various bd info for scsi command's immediate data,
77  * unsolicited data and first solicited data seq which includes BD start
78  * index & BD buf off. his function takes into account iscsi parameter such
79  * as immediate data and unsolicited data is support on this connection.
80  */
81 static void bnx2i_setup_write_cmd_bd_info(struct iscsi_task *task)
82 {
83         struct bnx2i_cmd *cmd = task->dd_data;
84         u32 start_bd_offset;
85         u32 start_bd_idx;
86         u32 buffer_offset = 0;
87         u32 cmd_len = cmd->req.total_data_transfer_length;
88
89         /* if ImmediateData is turned off & IntialR2T is turned on,
90          * there will be no immediate or unsolicited data, just return.
91          */
92         if (!iscsi_task_has_unsol_data(task) && !task->imm_count)
93                 return;
94
95         /* Immediate data */
96         buffer_offset += task->imm_count;
97         if (task->imm_count == cmd_len)
98                 return;
99
100         if (iscsi_task_has_unsol_data(task)) {
101                 bnx2i_get_write_cmd_bd_idx(cmd, buffer_offset,
102                                            &start_bd_offset, &start_bd_idx);
103                 cmd->req.ud_buffer_offset = start_bd_offset;
104                 cmd->req.ud_start_bd_index = start_bd_idx;
105                 buffer_offset += task->unsol_r2t.data_length;
106         }
107
108         if (buffer_offset != cmd_len) {
109                 bnx2i_get_write_cmd_bd_idx(cmd, buffer_offset,
110                                            &start_bd_offset, &start_bd_idx);
111                 if ((start_bd_offset > task->conn->session->first_burst) ||
112                     (start_bd_idx > scsi_sg_count(cmd->scsi_cmd))) {
113                         int i = 0;
114
115                         iscsi_conn_printk(KERN_ALERT, task->conn,
116                                           "bnx2i- error, buf offset 0x%x "
117                                           "bd_valid %d use_sg %d\n",
118                                           buffer_offset, cmd->io_tbl.bd_valid,
119                                           scsi_sg_count(cmd->scsi_cmd));
120                         for (i = 0; i < cmd->io_tbl.bd_valid; i++)
121                                 iscsi_conn_printk(KERN_ALERT, task->conn,
122                                                   "bnx2i err, bd[%d]: len %x\n",
123                                                   i, cmd->io_tbl.bd_tbl[i].\
124                                                   buffer_length);
125                 }
126                 cmd->req.sd_buffer_offset = start_bd_offset;
127                 cmd->req.sd_start_bd_index = start_bd_idx;
128         }
129 }
130
131
132
133 /**
134  * bnx2i_map_scsi_sg - maps IO buffer and prepares the BD table
135  * @hba:        adapter instance
136  * @cmd:        iscsi cmd struct pointer
137  *
138  * map SG list
139  */
140 static int bnx2i_map_scsi_sg(struct bnx2i_hba *hba, struct bnx2i_cmd *cmd)
141 {
142         struct scsi_cmnd *sc = cmd->scsi_cmd;
143         struct iscsi_bd *bd = cmd->io_tbl.bd_tbl;
144         struct scatterlist *sg;
145         int byte_count = 0;
146         int bd_count = 0;
147         int sg_count;
148         int sg_len;
149         u64 addr;
150         int i;
151
152         BUG_ON(scsi_sg_count(sc) > ISCSI_MAX_BDS_PER_CMD);
153
154         sg_count = scsi_dma_map(sc);
155
156         scsi_for_each_sg(sc, sg, sg_count, i) {
157                 sg_len = sg_dma_len(sg);
158                 addr = (u64) sg_dma_address(sg);
159                 bd[bd_count].buffer_addr_lo = addr & 0xffffffff;
160                 bd[bd_count].buffer_addr_hi = addr >> 32;
161                 bd[bd_count].buffer_length = sg_len;
162                 bd[bd_count].flags = 0;
163                 if (bd_count == 0)
164                         bd[bd_count].flags = ISCSI_BD_FIRST_IN_BD_CHAIN;
165
166                 byte_count += sg_len;
167                 bd_count++;
168         }
169
170         if (bd_count)
171                 bd[bd_count - 1].flags |= ISCSI_BD_LAST_IN_BD_CHAIN;
172
173         BUG_ON(byte_count != scsi_bufflen(sc));
174         return bd_count;
175 }
176
177 /**
178  * bnx2i_iscsi_map_sg_list - maps SG list
179  * @cmd:        iscsi cmd struct pointer
180  *
181  * creates BD list table for the command
182  */
183 static void bnx2i_iscsi_map_sg_list(struct bnx2i_cmd *cmd)
184 {
185         int bd_count;
186
187         bd_count  = bnx2i_map_scsi_sg(cmd->conn->hba, cmd);
188         if (!bd_count) {
189                 struct iscsi_bd *bd = cmd->io_tbl.bd_tbl;
190
191                 bd[0].buffer_addr_lo = bd[0].buffer_addr_hi = 0;
192                 bd[0].buffer_length = bd[0].flags = 0;
193         }
194         cmd->io_tbl.bd_valid = bd_count;
195 }
196
197
198 /**
199  * bnx2i_iscsi_unmap_sg_list - unmaps SG list
200  * @cmd:        iscsi cmd struct pointer
201  *
202  * unmap IO buffers and invalidate the BD table
203  */
204 void bnx2i_iscsi_unmap_sg_list(struct bnx2i_cmd *cmd)
205 {
206         struct scsi_cmnd *sc = cmd->scsi_cmd;
207
208         if (cmd->io_tbl.bd_valid && sc) {
209                 scsi_dma_unmap(sc);
210                 cmd->io_tbl.bd_valid = 0;
211         }
212 }
213
214 static void bnx2i_setup_cmd_wqe_template(struct bnx2i_cmd *cmd)
215 {
216         memset(&cmd->req, 0x00, sizeof(cmd->req));
217         cmd->req.op_code = 0xFF;
218         cmd->req.bd_list_addr_lo = (u32) cmd->io_tbl.bd_tbl_dma;
219         cmd->req.bd_list_addr_hi =
220                 (u32) ((u64) cmd->io_tbl.bd_tbl_dma >> 32);
221
222 }
223
224
225 /**
226  * bnx2i_bind_conn_to_iscsi_cid - bind conn structure to 'iscsi_cid'
227  * @hba:        pointer to adapter instance
228  * @conn:       pointer to iscsi connection
229  * @iscsi_cid:  iscsi context ID, range 0 - (MAX_CONN - 1)
230  *
231  * update iscsi cid table entry with connection pointer. This enables
232  *      driver to quickly get hold of connection structure pointer in
233  *      completion/interrupt thread using iscsi context ID
234  */
235 static int bnx2i_bind_conn_to_iscsi_cid(struct bnx2i_hba *hba,
236                                         struct bnx2i_conn *bnx2i_conn,
237                                         u32 iscsi_cid)
238 {
239         if (hba && hba->cid_que.conn_cid_tbl[iscsi_cid]) {
240                 iscsi_conn_printk(KERN_ALERT, bnx2i_conn->cls_conn->dd_data,
241                                  "conn bind - entry #%d not free\n", iscsi_cid);
242                 return -EBUSY;
243         }
244
245         hba->cid_que.conn_cid_tbl[iscsi_cid] = bnx2i_conn;
246         return 0;
247 }
248
249
250 /**
251  * bnx2i_get_conn_from_id - maps an iscsi cid to corresponding conn ptr
252  * @hba:        pointer to adapter instance
253  * @iscsi_cid:  iscsi context ID, range 0 - (MAX_CONN - 1)
254  */
255 struct bnx2i_conn *bnx2i_get_conn_from_id(struct bnx2i_hba *hba,
256                                           u16 iscsi_cid)
257 {
258         if (!hba->cid_que.conn_cid_tbl) {
259                 printk(KERN_ERR "bnx2i: ERROR - missing conn<->cid table\n");
260                 return NULL;
261
262         } else if (iscsi_cid >= hba->max_active_conns) {
263                 printk(KERN_ERR "bnx2i: wrong cid #%d\n", iscsi_cid);
264                 return NULL;
265         }
266         return hba->cid_que.conn_cid_tbl[iscsi_cid];
267 }
268
269
270 /**
271  * bnx2i_alloc_iscsi_cid - allocates a iscsi_cid from free pool
272  * @hba:        pointer to adapter instance
273  */
274 static u32 bnx2i_alloc_iscsi_cid(struct bnx2i_hba *hba)
275 {
276         int idx;
277
278         if (!hba->cid_que.cid_free_cnt)
279                 return -1;
280
281         idx = hba->cid_que.cid_q_cons_idx;
282         hba->cid_que.cid_q_cons_idx++;
283         if (hba->cid_que.cid_q_cons_idx == hba->cid_que.cid_q_max_idx)
284                 hba->cid_que.cid_q_cons_idx = 0;
285
286         hba->cid_que.cid_free_cnt--;
287         return hba->cid_que.cid_que[idx];
288 }
289
290
291 /**
292  * bnx2i_free_iscsi_cid - returns tcp port to free list
293  * @hba:                pointer to adapter instance
294  * @iscsi_cid:          iscsi context ID to free
295  */
296 static void bnx2i_free_iscsi_cid(struct bnx2i_hba *hba, u16 iscsi_cid)
297 {
298         int idx;
299
300         if (iscsi_cid == (u16) -1)
301                 return;
302
303         hba->cid_que.cid_free_cnt++;
304
305         idx = hba->cid_que.cid_q_prod_idx;
306         hba->cid_que.cid_que[idx] = iscsi_cid;
307         hba->cid_que.conn_cid_tbl[iscsi_cid] = NULL;
308         hba->cid_que.cid_q_prod_idx++;
309         if (hba->cid_que.cid_q_prod_idx == hba->cid_que.cid_q_max_idx)
310                 hba->cid_que.cid_q_prod_idx = 0;
311 }
312
313
314 /**
315  * bnx2i_setup_free_cid_que - sets up free iscsi cid queue
316  * @hba:        pointer to adapter instance
317  *
318  * allocates memory for iscsi cid queue & 'cid - conn ptr' mapping table,
319  *      and initialize table attributes
320  */
321 static int bnx2i_setup_free_cid_que(struct bnx2i_hba *hba)
322 {
323         int mem_size;
324         int i;
325
326         mem_size = hba->max_active_conns * sizeof(u32);
327         mem_size = (mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
328
329         hba->cid_que.cid_que_base = kmalloc(mem_size, GFP_KERNEL);
330         if (!hba->cid_que.cid_que_base)
331                 return -ENOMEM;
332
333         mem_size = hba->max_active_conns * sizeof(struct bnx2i_conn *);
334         mem_size = (mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
335         hba->cid_que.conn_cid_tbl = kmalloc(mem_size, GFP_KERNEL);
336         if (!hba->cid_que.conn_cid_tbl) {
337                 kfree(hba->cid_que.cid_que_base);
338                 hba->cid_que.cid_que_base = NULL;
339                 return -ENOMEM;
340         }
341
342         hba->cid_que.cid_que = (u32 *)hba->cid_que.cid_que_base;
343         hba->cid_que.cid_q_prod_idx = 0;
344         hba->cid_que.cid_q_cons_idx = 0;
345         hba->cid_que.cid_q_max_idx = hba->max_active_conns;
346         hba->cid_que.cid_free_cnt = hba->max_active_conns;
347
348         for (i = 0; i < hba->max_active_conns; i++) {
349                 hba->cid_que.cid_que[i] = i;
350                 hba->cid_que.conn_cid_tbl[i] = NULL;
351         }
352         return 0;
353 }
354
355
356 /**
357  * bnx2i_release_free_cid_que - releases 'iscsi_cid' queue resources
358  * @hba:        pointer to adapter instance
359  */
360 static void bnx2i_release_free_cid_que(struct bnx2i_hba *hba)
361 {
362         kfree(hba->cid_que.cid_que_base);
363         hba->cid_que.cid_que_base = NULL;
364
365         kfree(hba->cid_que.conn_cid_tbl);
366         hba->cid_que.conn_cid_tbl = NULL;
367 }
368
369
370 /**
371  * bnx2i_alloc_ep - allocates ep structure from global pool
372  * @hba:        pointer to adapter instance
373  *
374  * routine allocates a free endpoint structure from global pool and
375  *      a tcp port to be used for this connection.  Global resource lock,
376  *      'bnx2i_resc_lock' is held while accessing shared global data structures
377  */
378 static struct iscsi_endpoint *bnx2i_alloc_ep(struct bnx2i_hba *hba)
379 {
380         struct iscsi_endpoint *ep;
381         struct bnx2i_endpoint *bnx2i_ep;
382         u32 ec_div;
383
384         ep = iscsi_create_endpoint(sizeof(*bnx2i_ep));
385         if (!ep) {
386                 printk(KERN_ERR "bnx2i: Could not allocate ep\n");
387                 return NULL;
388         }
389
390         bnx2i_ep = ep->dd_data;
391         bnx2i_ep->cls_ep = ep;
392         INIT_LIST_HEAD(&bnx2i_ep->link);
393         bnx2i_ep->state = EP_STATE_IDLE;
394         bnx2i_ep->ep_iscsi_cid = (u16) -1;
395         bnx2i_ep->hba = hba;
396         bnx2i_ep->hba_age = hba->age;
397
398         ec_div = event_coal_div;
399         while (ec_div >>= 1)
400                 bnx2i_ep->ec_shift += 1;
401
402         hba->ofld_conns_active++;
403         init_waitqueue_head(&bnx2i_ep->ofld_wait);
404         return ep;
405 }
406
407
408 /**
409  * bnx2i_free_ep - free endpoint
410  * @ep:         pointer to iscsi endpoint structure
411  */
412 static void bnx2i_free_ep(struct iscsi_endpoint *ep)
413 {
414         struct bnx2i_endpoint *bnx2i_ep = ep->dd_data;
415         unsigned long flags;
416
417         spin_lock_irqsave(&bnx2i_resc_lock, flags);
418         bnx2i_ep->state = EP_STATE_IDLE;
419         bnx2i_ep->hba->ofld_conns_active--;
420
421         if (bnx2i_ep->ep_iscsi_cid != (u16) -1)
422                 bnx2i_free_iscsi_cid(bnx2i_ep->hba, bnx2i_ep->ep_iscsi_cid);
423
424         if (bnx2i_ep->conn) {
425                 bnx2i_ep->conn->ep = NULL;
426                 bnx2i_ep->conn = NULL;
427         }
428
429         bnx2i_ep->hba = NULL;
430         spin_unlock_irqrestore(&bnx2i_resc_lock, flags);
431         iscsi_destroy_endpoint(ep);
432 }
433
434
435 /**
436  * bnx2i_alloc_bdt - allocates buffer descriptor (BD) table for the command
437  * @hba:        adapter instance pointer
438  * @session:    iscsi session pointer
439  * @cmd:        iscsi command structure
440  */
441 static int bnx2i_alloc_bdt(struct bnx2i_hba *hba, struct iscsi_session *session,
442                            struct bnx2i_cmd *cmd)
443 {
444         struct io_bdt *io = &cmd->io_tbl;
445         struct iscsi_bd *bd;
446
447         io->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
448                                         ISCSI_MAX_BDS_PER_CMD * sizeof(*bd),
449                                         &io->bd_tbl_dma, GFP_KERNEL);
450         if (!io->bd_tbl) {
451                 iscsi_session_printk(KERN_ERR, session, "Could not "
452                                      "allocate bdt.\n");
453                 return -ENOMEM;
454         }
455         io->bd_valid = 0;
456         return 0;
457 }
458
459 /**
460  * bnx2i_destroy_cmd_pool - destroys iscsi command pool and release BD table
461  * @hba:        adapter instance pointer
462  * @session:    iscsi session pointer
463  * @cmd:        iscsi command structure
464  */
465 static void bnx2i_destroy_cmd_pool(struct bnx2i_hba *hba,
466                                    struct iscsi_session *session)
467 {
468         int i;
469
470         for (i = 0; i < session->cmds_max; i++) {
471                 struct iscsi_task *task = session->cmds[i];
472                 struct bnx2i_cmd *cmd = task->dd_data;
473
474                 if (cmd->io_tbl.bd_tbl)
475                         dma_free_coherent(&hba->pcidev->dev,
476                                           ISCSI_MAX_BDS_PER_CMD *
477                                           sizeof(struct iscsi_bd),
478                                           cmd->io_tbl.bd_tbl,
479                                           cmd->io_tbl.bd_tbl_dma);
480         }
481
482 }
483
484
485 /**
486  * bnx2i_setup_cmd_pool - sets up iscsi command pool for the session
487  * @hba:        adapter instance pointer
488  * @session:    iscsi session pointer
489  */
490 static int bnx2i_setup_cmd_pool(struct bnx2i_hba *hba,
491                                 struct iscsi_session *session)
492 {
493         int i;
494
495         for (i = 0; i < session->cmds_max; i++) {
496                 struct iscsi_task *task = session->cmds[i];
497                 struct bnx2i_cmd *cmd = task->dd_data;
498
499                 task->hdr = &cmd->hdr;
500                 task->hdr_max = sizeof(struct iscsi_hdr);
501
502                 if (bnx2i_alloc_bdt(hba, session, cmd))
503                         goto free_bdts;
504         }
505
506         return 0;
507
508 free_bdts:
509         bnx2i_destroy_cmd_pool(hba, session);
510         return -ENOMEM;
511 }
512
513
514 /**
515  * bnx2i_setup_mp_bdt - allocate BD table resources
516  * @hba:        pointer to adapter structure
517  *
518  * Allocate memory for dummy buffer and associated BD
519  * table to be used by middle path (MP) requests
520  */
521 static int bnx2i_setup_mp_bdt(struct bnx2i_hba *hba)
522 {
523         int rc = 0;
524         struct iscsi_bd *mp_bdt;
525         u64 addr;
526
527         hba->mp_bd_tbl = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
528                                             &hba->mp_bd_dma, GFP_KERNEL);
529         if (!hba->mp_bd_tbl) {
530                 printk(KERN_ERR "unable to allocate Middle Path BDT\n");
531                 rc = -1;
532                 goto out;
533         }
534
535         hba->dummy_buffer = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
536                                                &hba->dummy_buf_dma, GFP_KERNEL);
537         if (!hba->dummy_buffer) {
538                 printk(KERN_ERR "unable to alloc Middle Path Dummy Buffer\n");
539                 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
540                                   hba->mp_bd_tbl, hba->mp_bd_dma);
541                 hba->mp_bd_tbl = NULL;
542                 rc = -1;
543                 goto out;
544         }
545
546         mp_bdt = (struct iscsi_bd *) hba->mp_bd_tbl;
547         addr = (unsigned long) hba->dummy_buf_dma;
548         mp_bdt->buffer_addr_lo = addr & 0xffffffff;
549         mp_bdt->buffer_addr_hi = addr >> 32;
550         mp_bdt->buffer_length = PAGE_SIZE;
551         mp_bdt->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
552                         ISCSI_BD_FIRST_IN_BD_CHAIN;
553 out:
554         return rc;
555 }
556
557
558 /**
559  * bnx2i_free_mp_bdt - releases ITT back to free pool
560  * @hba:        pointer to adapter instance
561  *
562  * free MP dummy buffer and associated BD table
563  */
564 static void bnx2i_free_mp_bdt(struct bnx2i_hba *hba)
565 {
566         if (hba->mp_bd_tbl) {
567                 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
568                                   hba->mp_bd_tbl, hba->mp_bd_dma);
569                 hba->mp_bd_tbl = NULL;
570         }
571         if (hba->dummy_buffer) {
572                 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
573                                   hba->dummy_buffer, hba->dummy_buf_dma);
574                 hba->dummy_buffer = NULL;
575         }
576                 return;
577 }
578
579 /**
580  * bnx2i_drop_session - notifies iscsid of connection error.
581  * @hba:        adapter instance pointer
582  * @session:    iscsi session pointer
583  *
584  * This notifies iscsid that there is a error, so it can initiate
585  * recovery.
586  *
587  * This relies on caller using the iscsi class iterator so the object
588  * is refcounted and does not disapper from under us.
589  */
590 void bnx2i_drop_session(struct iscsi_cls_session *cls_session)
591 {
592         iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
593 }
594
595 /**
596  * bnx2i_ep_destroy_list_add - add an entry to EP destroy list
597  * @hba:        pointer to adapter instance
598  * @ep:         pointer to endpoint (transport indentifier) structure
599  *
600  * EP destroy queue manager
601  */
602 static int bnx2i_ep_destroy_list_add(struct bnx2i_hba *hba,
603                                      struct bnx2i_endpoint *ep)
604 {
605         write_lock_bh(&hba->ep_rdwr_lock);
606         list_add_tail(&ep->link, &hba->ep_destroy_list);
607         write_unlock_bh(&hba->ep_rdwr_lock);
608         return 0;
609 }
610
611 /**
612  * bnx2i_ep_destroy_list_del - add an entry to EP destroy list
613  *
614  * @hba:                pointer to adapter instance
615  * @ep:                 pointer to endpoint (transport indentifier) structure
616  *
617  * EP destroy queue manager
618  */
619 static int bnx2i_ep_destroy_list_del(struct bnx2i_hba *hba,
620                                      struct bnx2i_endpoint *ep)
621 {
622         write_lock_bh(&hba->ep_rdwr_lock);
623         list_del_init(&ep->link);
624         write_unlock_bh(&hba->ep_rdwr_lock);
625
626         return 0;
627 }
628
629 /**
630  * bnx2i_ep_ofld_list_add - add an entry to ep offload pending list
631  * @hba:        pointer to adapter instance
632  * @ep:         pointer to endpoint (transport indentifier) structure
633  *
634  * pending conn offload completion queue manager
635  */
636 static int bnx2i_ep_ofld_list_add(struct bnx2i_hba *hba,
637                                   struct bnx2i_endpoint *ep)
638 {
639         write_lock_bh(&hba->ep_rdwr_lock);
640         list_add_tail(&ep->link, &hba->ep_ofld_list);
641         write_unlock_bh(&hba->ep_rdwr_lock);
642         return 0;
643 }
644
645 /**
646  * bnx2i_ep_ofld_list_del - add an entry to ep offload pending list
647  * @hba:                pointer to adapter instance
648  * @ep:                 pointer to endpoint (transport indentifier) structure
649  *
650  * pending conn offload completion queue manager
651  */
652 static int bnx2i_ep_ofld_list_del(struct bnx2i_hba *hba,
653                                   struct bnx2i_endpoint *ep)
654 {
655         write_lock_bh(&hba->ep_rdwr_lock);
656         list_del_init(&ep->link);
657         write_unlock_bh(&hba->ep_rdwr_lock);
658         return 0;
659 }
660
661
662 /**
663  * bnx2i_find_ep_in_ofld_list - find iscsi_cid in pending list of endpoints
664  *
665  * @hba:                pointer to adapter instance
666  * @iscsi_cid:          iscsi context ID to find
667  *
668  */
669 struct bnx2i_endpoint *
670 bnx2i_find_ep_in_ofld_list(struct bnx2i_hba *hba, u32 iscsi_cid)
671 {
672         struct list_head *list;
673         struct list_head *tmp;
674         struct bnx2i_endpoint *ep;
675
676         read_lock_bh(&hba->ep_rdwr_lock);
677         list_for_each_safe(list, tmp, &hba->ep_ofld_list) {
678                 ep = (struct bnx2i_endpoint *)list;
679
680                 if (ep->ep_iscsi_cid == iscsi_cid)
681                         break;
682                 ep = NULL;
683         }
684         read_unlock_bh(&hba->ep_rdwr_lock);
685
686         if (!ep)
687                 printk(KERN_ERR "l5 cid %d not found\n", iscsi_cid);
688         return ep;
689 }
690
691 /**
692  * bnx2i_find_ep_in_destroy_list - find iscsi_cid in destroy list
693  * @hba:                pointer to adapter instance
694  * @iscsi_cid:          iscsi context ID to find
695  *
696  */
697 struct bnx2i_endpoint *
698 bnx2i_find_ep_in_destroy_list(struct bnx2i_hba *hba, u32 iscsi_cid)
699 {
700         struct list_head *list;
701         struct list_head *tmp;
702         struct bnx2i_endpoint *ep;
703
704         read_lock_bh(&hba->ep_rdwr_lock);
705         list_for_each_safe(list, tmp, &hba->ep_destroy_list) {
706                 ep = (struct bnx2i_endpoint *)list;
707
708                 if (ep->ep_iscsi_cid == iscsi_cid)
709                         break;
710                 ep = NULL;
711         }
712         read_unlock_bh(&hba->ep_rdwr_lock);
713
714         if (!ep)
715                 printk(KERN_ERR "l5 cid %d not found\n", iscsi_cid);
716
717         return ep;
718 }
719
720 /**
721  * bnx2i_ep_active_list_add - add an entry to ep active list
722  * @hba:        pointer to adapter instance
723  * @ep:         pointer to endpoint (transport indentifier) structure
724  *
725  * current active conn queue manager
726  */
727 static void bnx2i_ep_active_list_add(struct bnx2i_hba *hba,
728                                      struct bnx2i_endpoint *ep)
729 {
730         write_lock_bh(&hba->ep_rdwr_lock);
731         list_add_tail(&ep->link, &hba->ep_active_list);
732         write_unlock_bh(&hba->ep_rdwr_lock);
733 }
734
735
736 /**
737  * bnx2i_ep_active_list_del - deletes an entry to ep active list
738  * @hba:        pointer to adapter instance
739  * @ep:         pointer to endpoint (transport indentifier) structure
740  *
741  * current active conn queue manager
742  */
743 static void bnx2i_ep_active_list_del(struct bnx2i_hba *hba,
744                                      struct bnx2i_endpoint *ep)
745 {
746         write_lock_bh(&hba->ep_rdwr_lock);
747         list_del_init(&ep->link);
748         write_unlock_bh(&hba->ep_rdwr_lock);
749 }
750
751
752 /**
753  * bnx2i_setup_host_queue_size - assigns shost->can_queue param
754  * @hba:        pointer to adapter instance
755  * @shost:      scsi host pointer
756  *
757  * Initializes 'can_queue' parameter based on how many outstanding commands
758  *      the device can handle. Each device 5708/5709/57710 has different
759  *      capabilities
760  */
761 static void bnx2i_setup_host_queue_size(struct bnx2i_hba *hba,
762                                         struct Scsi_Host *shost)
763 {
764         if (test_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type))
765                 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5708;
766         else if (test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type))
767                 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5709;
768         else if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type))
769                 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_57710;
770         else
771                 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5708;
772 }
773
774
775 /**
776  * bnx2i_alloc_hba - allocate and init adapter instance
777  * @cnic:       cnic device pointer
778  *
779  * allocate & initialize adapter structure and call other
780  *      support routines to do per adapter initialization
781  */
782 struct bnx2i_hba *bnx2i_alloc_hba(struct cnic_dev *cnic)
783 {
784         struct Scsi_Host *shost;
785         struct bnx2i_hba *hba;
786
787         shost = iscsi_host_alloc(&bnx2i_host_template, sizeof(*hba), 0);
788         if (!shost)
789                 return NULL;
790         shost->dma_boundary = cnic->pcidev->dma_mask;
791         shost->transportt = bnx2i_scsi_xport_template;
792         shost->max_id = ISCSI_MAX_CONNS_PER_HBA;
793         shost->max_channel = 0;
794         shost->max_lun = 512;
795         shost->max_cmd_len = 16;
796
797         hba = iscsi_host_priv(shost);
798         hba->shost = shost;
799         hba->netdev = cnic->netdev;
800         /* Get PCI related information and update hba struct members */
801         hba->pcidev = cnic->pcidev;
802         pci_dev_get(hba->pcidev);
803         hba->pci_did = hba->pcidev->device;
804         hba->pci_vid = hba->pcidev->vendor;
805         hba->pci_sdid = hba->pcidev->subsystem_device;
806         hba->pci_svid = hba->pcidev->subsystem_vendor;
807         hba->pci_func = PCI_FUNC(hba->pcidev->devfn);
808         hba->pci_devno = PCI_SLOT(hba->pcidev->devfn);
809
810         bnx2i_identify_device(hba);
811         bnx2i_setup_host_queue_size(hba, shost);
812
813         if (test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type)) {
814                 hba->regview = ioremap_nocache(hba->netdev->base_addr,
815                                                BNX2_MQ_CONFIG2);
816                 if (!hba->regview)
817                         goto ioreg_map_err;
818         } else if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
819                 hba->regview = ioremap_nocache(hba->netdev->base_addr, 4096);
820                 if (!hba->regview)
821                         goto ioreg_map_err;
822         }
823
824         if (bnx2i_setup_mp_bdt(hba))
825                 goto mp_bdt_mem_err;
826
827         INIT_LIST_HEAD(&hba->ep_ofld_list);
828         INIT_LIST_HEAD(&hba->ep_active_list);
829         INIT_LIST_HEAD(&hba->ep_destroy_list);
830         rwlock_init(&hba->ep_rdwr_lock);
831
832         hba->mtu_supported = BNX2I_MAX_MTU_SUPPORTED;
833
834         /* different values for 5708/5709/57710 */
835         hba->max_active_conns = ISCSI_MAX_CONNS_PER_HBA;
836
837         if (bnx2i_setup_free_cid_que(hba))
838                 goto cid_que_err;
839
840         /* SQ/RQ/CQ size can be changed via sysfx interface */
841         if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
842                 if (sq_size && sq_size <= BNX2I_5770X_SQ_WQES_MAX)
843                         hba->max_sqes = sq_size;
844                 else
845                         hba->max_sqes = BNX2I_5770X_SQ_WQES_DEFAULT;
846         } else {        /* 5706/5708/5709 */
847                 if (sq_size && sq_size <= BNX2I_570X_SQ_WQES_MAX)
848                         hba->max_sqes = sq_size;
849                 else
850                         hba->max_sqes = BNX2I_570X_SQ_WQES_DEFAULT;
851         }
852
853         hba->max_rqes = rq_size;
854         hba->max_cqes = hba->max_sqes + rq_size;
855         if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
856                 if (hba->max_cqes > BNX2I_5770X_CQ_WQES_MAX)
857                         hba->max_cqes = BNX2I_5770X_CQ_WQES_MAX;
858         } else if (hba->max_cqes > BNX2I_570X_CQ_WQES_MAX)
859                 hba->max_cqes = BNX2I_570X_CQ_WQES_MAX;
860
861         hba->num_ccell = hba->max_sqes / 2;
862
863         spin_lock_init(&hba->lock);
864         mutex_init(&hba->net_dev_lock);
865         init_waitqueue_head(&hba->eh_wait);
866         if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
867                 hba->hba_shutdown_tmo = 30 * HZ;
868                 hba->conn_teardown_tmo = 20 * HZ;
869                 hba->conn_ctx_destroy_tmo = 6 * HZ;
870         } else {        /* 5706/5708/5709 */
871                 hba->hba_shutdown_tmo = 20 * HZ;
872                 hba->conn_teardown_tmo = 10 * HZ;
873                 hba->conn_ctx_destroy_tmo = 2 * HZ;
874         }
875
876         if (iscsi_host_add(shost, &hba->pcidev->dev))
877                 goto free_dump_mem;
878         return hba;
879
880 free_dump_mem:
881         bnx2i_release_free_cid_que(hba);
882 cid_que_err:
883         bnx2i_free_mp_bdt(hba);
884 mp_bdt_mem_err:
885         if (hba->regview) {
886                 iounmap(hba->regview);
887                 hba->regview = NULL;
888         }
889 ioreg_map_err:
890         pci_dev_put(hba->pcidev);
891         scsi_host_put(shost);
892         return NULL;
893 }
894
895 /**
896  * bnx2i_free_hba- releases hba structure and resources held by the adapter
897  * @hba:        pointer to adapter instance
898  *
899  * free adapter structure and call various cleanup routines.
900  */
901 void bnx2i_free_hba(struct bnx2i_hba *hba)
902 {
903         struct Scsi_Host *shost = hba->shost;
904
905         iscsi_host_remove(shost);
906         INIT_LIST_HEAD(&hba->ep_ofld_list);
907         INIT_LIST_HEAD(&hba->ep_active_list);
908         INIT_LIST_HEAD(&hba->ep_destroy_list);
909         pci_dev_put(hba->pcidev);
910
911         if (hba->regview) {
912                 iounmap(hba->regview);
913                 hba->regview = NULL;
914         }
915         bnx2i_free_mp_bdt(hba);
916         bnx2i_release_free_cid_que(hba);
917         iscsi_host_free(shost);
918 }
919
920 /**
921  * bnx2i_conn_free_login_resources - free DMA resources used for login process
922  * @hba:                pointer to adapter instance
923  * @bnx2i_conn:         iscsi connection pointer
924  *
925  * Login related resources, mostly BDT & payload DMA memory is freed
926  */
927 static void bnx2i_conn_free_login_resources(struct bnx2i_hba *hba,
928                                             struct bnx2i_conn *bnx2i_conn)
929 {
930         if (bnx2i_conn->gen_pdu.resp_bd_tbl) {
931                 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
932                                   bnx2i_conn->gen_pdu.resp_bd_tbl,
933                                   bnx2i_conn->gen_pdu.resp_bd_dma);
934                 bnx2i_conn->gen_pdu.resp_bd_tbl = NULL;
935         }
936
937         if (bnx2i_conn->gen_pdu.req_bd_tbl) {
938                 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
939                                   bnx2i_conn->gen_pdu.req_bd_tbl,
940                                   bnx2i_conn->gen_pdu.req_bd_dma);
941                 bnx2i_conn->gen_pdu.req_bd_tbl = NULL;
942         }
943
944         if (bnx2i_conn->gen_pdu.resp_buf) {
945                 dma_free_coherent(&hba->pcidev->dev,
946                                   ISCSI_DEF_MAX_RECV_SEG_LEN,
947                                   bnx2i_conn->gen_pdu.resp_buf,
948                                   bnx2i_conn->gen_pdu.resp_dma_addr);
949                 bnx2i_conn->gen_pdu.resp_buf = NULL;
950         }
951
952         if (bnx2i_conn->gen_pdu.req_buf) {
953                 dma_free_coherent(&hba->pcidev->dev,
954                                   ISCSI_DEF_MAX_RECV_SEG_LEN,
955                                   bnx2i_conn->gen_pdu.req_buf,
956                                   bnx2i_conn->gen_pdu.req_dma_addr);
957                 bnx2i_conn->gen_pdu.req_buf = NULL;
958         }
959 }
960
961 /**
962  * bnx2i_conn_alloc_login_resources - alloc DMA resources for login/nop.
963  * @hba:                pointer to adapter instance
964  * @bnx2i_conn:         iscsi connection pointer
965  *
966  * Mgmt task DNA resources are allocated in this routine.
967  */
968 static int bnx2i_conn_alloc_login_resources(struct bnx2i_hba *hba,
969                                             struct bnx2i_conn *bnx2i_conn)
970 {
971         /* Allocate memory for login request/response buffers */
972         bnx2i_conn->gen_pdu.req_buf =
973                 dma_alloc_coherent(&hba->pcidev->dev,
974                                    ISCSI_DEF_MAX_RECV_SEG_LEN,
975                                    &bnx2i_conn->gen_pdu.req_dma_addr,
976                                    GFP_KERNEL);
977         if (bnx2i_conn->gen_pdu.req_buf == NULL)
978                 goto login_req_buf_failure;
979
980         bnx2i_conn->gen_pdu.req_buf_size = 0;
981         bnx2i_conn->gen_pdu.req_wr_ptr = bnx2i_conn->gen_pdu.req_buf;
982
983         bnx2i_conn->gen_pdu.resp_buf =
984                 dma_alloc_coherent(&hba->pcidev->dev,
985                                    ISCSI_DEF_MAX_RECV_SEG_LEN,
986                                    &bnx2i_conn->gen_pdu.resp_dma_addr,
987                                    GFP_KERNEL);
988         if (bnx2i_conn->gen_pdu.resp_buf == NULL)
989                 goto login_resp_buf_failure;
990
991         bnx2i_conn->gen_pdu.resp_buf_size = ISCSI_DEF_MAX_RECV_SEG_LEN;
992         bnx2i_conn->gen_pdu.resp_wr_ptr = bnx2i_conn->gen_pdu.resp_buf;
993
994         bnx2i_conn->gen_pdu.req_bd_tbl =
995                 dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
996                                    &bnx2i_conn->gen_pdu.req_bd_dma, GFP_KERNEL);
997         if (bnx2i_conn->gen_pdu.req_bd_tbl == NULL)
998                 goto login_req_bd_tbl_failure;
999
1000         bnx2i_conn->gen_pdu.resp_bd_tbl =
1001                 dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
1002                                    &bnx2i_conn->gen_pdu.resp_bd_dma,
1003                                    GFP_KERNEL);
1004         if (bnx2i_conn->gen_pdu.resp_bd_tbl == NULL)
1005                 goto login_resp_bd_tbl_failure;
1006
1007         return 0;
1008
1009 login_resp_bd_tbl_failure:
1010         dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
1011                           bnx2i_conn->gen_pdu.req_bd_tbl,
1012                           bnx2i_conn->gen_pdu.req_bd_dma);
1013         bnx2i_conn->gen_pdu.req_bd_tbl = NULL;
1014
1015 login_req_bd_tbl_failure:
1016         dma_free_coherent(&hba->pcidev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
1017                           bnx2i_conn->gen_pdu.resp_buf,
1018                           bnx2i_conn->gen_pdu.resp_dma_addr);
1019         bnx2i_conn->gen_pdu.resp_buf = NULL;
1020 login_resp_buf_failure:
1021         dma_free_coherent(&hba->pcidev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
1022                           bnx2i_conn->gen_pdu.req_buf,
1023                           bnx2i_conn->gen_pdu.req_dma_addr);
1024         bnx2i_conn->gen_pdu.req_buf = NULL;
1025 login_req_buf_failure:
1026         iscsi_conn_printk(KERN_ERR, bnx2i_conn->cls_conn->dd_data,
1027                           "login resource alloc failed!!\n");
1028         return -ENOMEM;
1029
1030 }
1031
1032
1033 /**
1034  * bnx2i_iscsi_prep_generic_pdu_bd - prepares BD table.
1035  * @bnx2i_conn:         iscsi connection pointer
1036  *
1037  * Allocates buffers and BD tables before shipping requests to cnic
1038  *      for PDUs prepared by 'iscsid' daemon
1039  */
1040 static void bnx2i_iscsi_prep_generic_pdu_bd(struct bnx2i_conn *bnx2i_conn)
1041 {
1042         struct iscsi_bd *bd_tbl;
1043
1044         bd_tbl = (struct iscsi_bd *) bnx2i_conn->gen_pdu.req_bd_tbl;
1045
1046         bd_tbl->buffer_addr_hi =
1047                 (u32) ((u64) bnx2i_conn->gen_pdu.req_dma_addr >> 32);
1048         bd_tbl->buffer_addr_lo = (u32) bnx2i_conn->gen_pdu.req_dma_addr;
1049         bd_tbl->buffer_length = bnx2i_conn->gen_pdu.req_wr_ptr -
1050                                 bnx2i_conn->gen_pdu.req_buf;
1051         bd_tbl->reserved0 = 0;
1052         bd_tbl->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
1053                         ISCSI_BD_FIRST_IN_BD_CHAIN;
1054
1055         bd_tbl = (struct iscsi_bd  *) bnx2i_conn->gen_pdu.resp_bd_tbl;
1056         bd_tbl->buffer_addr_hi = (u64) bnx2i_conn->gen_pdu.resp_dma_addr >> 32;
1057         bd_tbl->buffer_addr_lo = (u32) bnx2i_conn->gen_pdu.resp_dma_addr;
1058         bd_tbl->buffer_length = ISCSI_DEF_MAX_RECV_SEG_LEN;
1059         bd_tbl->reserved0 = 0;
1060         bd_tbl->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
1061                         ISCSI_BD_FIRST_IN_BD_CHAIN;
1062 }
1063
1064
1065 /**
1066  * bnx2i_iscsi_send_generic_request - called to send mgmt tasks.
1067  * @task:       transport layer task pointer
1068  *
1069  * called to transmit PDUs prepared by the 'iscsid' daemon. iSCSI login,
1070  *      Nop-out and Logout requests flow through this path.
1071  */
1072 static int bnx2i_iscsi_send_generic_request(struct iscsi_task *task)
1073 {
1074         struct bnx2i_cmd *cmd = task->dd_data;
1075         struct bnx2i_conn *bnx2i_conn = cmd->conn;
1076         int rc = 0;
1077         char *buf;
1078         int data_len;
1079
1080         bnx2i_iscsi_prep_generic_pdu_bd(bnx2i_conn);
1081         switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
1082         case ISCSI_OP_LOGIN:
1083                 bnx2i_send_iscsi_login(bnx2i_conn, task);
1084                 break;
1085         case ISCSI_OP_NOOP_OUT:
1086                 data_len = bnx2i_conn->gen_pdu.req_buf_size;
1087                 buf = bnx2i_conn->gen_pdu.req_buf;
1088                 if (data_len)
1089                         rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
1090                                                      buf, data_len, 1);
1091                 else
1092                         rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
1093                                                      NULL, 0, 1);
1094                 break;
1095         case ISCSI_OP_LOGOUT:
1096                 rc = bnx2i_send_iscsi_logout(bnx2i_conn, task);
1097                 break;
1098         case ISCSI_OP_SCSI_TMFUNC:
1099                 rc = bnx2i_send_iscsi_tmf(bnx2i_conn, task);
1100                 break;
1101         case ISCSI_OP_TEXT:
1102                 rc = bnx2i_send_iscsi_text(bnx2i_conn, task);
1103                 break;
1104         default:
1105                 iscsi_conn_printk(KERN_ALERT, bnx2i_conn->cls_conn->dd_data,
1106                                   "send_gen: unsupported op 0x%x\n",
1107                                   task->hdr->opcode);
1108         }
1109         return rc;
1110 }
1111
1112
1113 /**********************************************************************
1114  *              SCSI-ML Interface
1115  **********************************************************************/
1116
1117 /**
1118  * bnx2i_cpy_scsi_cdb - copies LUN & CDB fields in required format to sq wqe
1119  * @sc:         SCSI-ML command pointer
1120  * @cmd:        iscsi cmd pointer
1121  */
1122 static void bnx2i_cpy_scsi_cdb(struct scsi_cmnd *sc, struct bnx2i_cmd *cmd)
1123 {
1124         u32 dword;
1125         int lpcnt;
1126         u8 *srcp;
1127         u32 *dstp;
1128         u32 scsi_lun[2];
1129
1130         int_to_scsilun(sc->device->lun, (struct scsi_lun *) scsi_lun);
1131         cmd->req.lun[0] = be32_to_cpu(scsi_lun[0]);
1132         cmd->req.lun[1] = be32_to_cpu(scsi_lun[1]);
1133
1134         lpcnt = cmd->scsi_cmd->cmd_len / sizeof(dword);
1135         srcp = (u8 *) sc->cmnd;
1136         dstp = (u32 *) cmd->req.cdb;
1137         while (lpcnt--) {
1138                 memcpy(&dword, (const void *) srcp, 4);
1139                 *dstp = cpu_to_be32(dword);
1140                 srcp += 4;
1141                 dstp++;
1142         }
1143         if (sc->cmd_len & 0x3) {
1144                 dword = (u32) srcp[0] | ((u32) srcp[1] << 8);
1145                 *dstp = cpu_to_be32(dword);
1146         }
1147 }
1148
1149 static void bnx2i_cleanup_task(struct iscsi_task *task)
1150 {
1151         struct iscsi_conn *conn = task->conn;
1152         struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1153         struct bnx2i_hba *hba = bnx2i_conn->hba;
1154
1155         /*
1156          * mgmt task or cmd was never sent to us to transmit.
1157          */
1158         if (!task->sc || task->state == ISCSI_TASK_PENDING)
1159                 return;
1160         /*
1161          * need to clean-up task context to claim dma buffers
1162          */
1163         if (task->state == ISCSI_TASK_ABRT_TMF) {
1164                 bnx2i_send_cmd_cleanup_req(hba, task->dd_data);
1165
1166                 spin_unlock_bh(&conn->session->lock);
1167                 wait_for_completion_timeout(&bnx2i_conn->cmd_cleanup_cmpl,
1168                                 msecs_to_jiffies(ISCSI_CMD_CLEANUP_TIMEOUT));
1169                 spin_lock_bh(&conn->session->lock);
1170         }
1171         bnx2i_iscsi_unmap_sg_list(task->dd_data);
1172 }
1173
1174 /**
1175  * bnx2i_mtask_xmit - transmit mtask to chip for further processing
1176  * @conn:       transport layer conn structure pointer
1177  * @task:       transport layer command structure pointer
1178  */
1179 static int
1180 bnx2i_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
1181 {
1182         struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1183         struct bnx2i_cmd *cmd = task->dd_data;
1184
1185         memset(bnx2i_conn->gen_pdu.req_buf, 0, ISCSI_DEF_MAX_RECV_SEG_LEN);
1186
1187         bnx2i_setup_cmd_wqe_template(cmd);
1188         bnx2i_conn->gen_pdu.req_buf_size = task->data_count;
1189         if (task->data_count) {
1190                 memcpy(bnx2i_conn->gen_pdu.req_buf, task->data,
1191                        task->data_count);
1192                 bnx2i_conn->gen_pdu.req_wr_ptr =
1193                         bnx2i_conn->gen_pdu.req_buf + task->data_count;
1194         }
1195         cmd->conn = conn->dd_data;
1196         cmd->scsi_cmd = NULL;
1197         return bnx2i_iscsi_send_generic_request(task);
1198 }
1199
1200 /**
1201  * bnx2i_task_xmit - transmit iscsi command to chip for further processing
1202  * @task:       transport layer command structure pointer
1203  *
1204  * maps SG buffers and send request to chip/firmware in the form of SQ WQE
1205  */
1206 static int bnx2i_task_xmit(struct iscsi_task *task)
1207 {
1208         struct iscsi_conn *conn = task->conn;
1209         struct iscsi_session *session = conn->session;
1210         struct Scsi_Host *shost = iscsi_session_to_shost(session->cls_session);
1211         struct bnx2i_hba *hba = iscsi_host_priv(shost);
1212         struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1213         struct scsi_cmnd *sc = task->sc;
1214         struct bnx2i_cmd *cmd = task->dd_data;
1215         struct iscsi_cmd *hdr = (struct iscsi_cmd *) task->hdr;
1216
1217         if (bnx2i_conn->ep->num_active_cmds + 1 > hba->max_sqes)
1218                 return -ENOMEM;
1219
1220         /*
1221          * If there is no scsi_cmnd this must be a mgmt task
1222          */
1223         if (!sc)
1224                 return bnx2i_mtask_xmit(conn, task);
1225
1226         bnx2i_setup_cmd_wqe_template(cmd);
1227         cmd->req.op_code = ISCSI_OP_SCSI_CMD;
1228         cmd->conn = bnx2i_conn;
1229         cmd->scsi_cmd = sc;
1230         cmd->req.total_data_transfer_length = scsi_bufflen(sc);
1231         cmd->req.cmd_sn = be32_to_cpu(hdr->cmdsn);
1232
1233         bnx2i_iscsi_map_sg_list(cmd);
1234         bnx2i_cpy_scsi_cdb(sc, cmd);
1235
1236         cmd->req.op_attr = ISCSI_ATTR_SIMPLE;
1237         if (sc->sc_data_direction == DMA_TO_DEVICE) {
1238                 cmd->req.op_attr |= ISCSI_CMD_REQUEST_WRITE;
1239                 cmd->req.itt = task->itt |
1240                         (ISCSI_TASK_TYPE_WRITE << ISCSI_CMD_REQUEST_TYPE_SHIFT);
1241                 bnx2i_setup_write_cmd_bd_info(task);
1242         } else {
1243                 if (scsi_bufflen(sc))
1244                         cmd->req.op_attr |= ISCSI_CMD_REQUEST_READ;
1245                 cmd->req.itt = task->itt |
1246                         (ISCSI_TASK_TYPE_READ << ISCSI_CMD_REQUEST_TYPE_SHIFT);
1247         }
1248
1249         cmd->req.num_bds = cmd->io_tbl.bd_valid;
1250         if (!cmd->io_tbl.bd_valid) {
1251                 cmd->req.bd_list_addr_lo = (u32) hba->mp_bd_dma;
1252                 cmd->req.bd_list_addr_hi = (u32) ((u64) hba->mp_bd_dma >> 32);
1253                 cmd->req.num_bds = 1;
1254         }
1255
1256         bnx2i_send_iscsi_scsicmd(bnx2i_conn, cmd);
1257         return 0;
1258 }
1259
1260 /**
1261  * bnx2i_session_create - create a new iscsi session
1262  * @cmds_max:           max commands supported
1263  * @qdepth:             scsi queue depth to support
1264  * @initial_cmdsn:      initial iscsi CMDSN to be used for this session
1265  *
1266  * Creates a new iSCSI session instance on given device.
1267  */
1268 static struct iscsi_cls_session *
1269 bnx2i_session_create(struct iscsi_endpoint *ep,
1270                      uint16_t cmds_max, uint16_t qdepth,
1271                      uint32_t initial_cmdsn)
1272 {
1273         struct Scsi_Host *shost;
1274         struct iscsi_cls_session *cls_session;
1275         struct bnx2i_hba *hba;
1276         struct bnx2i_endpoint *bnx2i_ep;
1277
1278         if (!ep) {
1279                 printk(KERN_ERR "bnx2i: missing ep.\n");
1280                 return NULL;
1281         }
1282
1283         bnx2i_ep = ep->dd_data;
1284         shost = bnx2i_ep->hba->shost;
1285         hba = iscsi_host_priv(shost);
1286         if (bnx2i_adapter_ready(hba))
1287                 return NULL;
1288
1289         /*
1290          * user can override hw limit as long as it is within
1291          * the min/max.
1292          */
1293         if (cmds_max > hba->max_sqes)
1294                 cmds_max = hba->max_sqes;
1295         else if (cmds_max < BNX2I_SQ_WQES_MIN)
1296                 cmds_max = BNX2I_SQ_WQES_MIN;
1297
1298         cls_session = iscsi_session_setup(&bnx2i_iscsi_transport, shost,
1299                                           cmds_max, 0, sizeof(struct bnx2i_cmd),
1300                                           initial_cmdsn, ISCSI_MAX_TARGET);
1301         if (!cls_session)
1302                 return NULL;
1303
1304         if (bnx2i_setup_cmd_pool(hba, cls_session->dd_data))
1305                 goto session_teardown;
1306         return cls_session;
1307
1308 session_teardown:
1309         iscsi_session_teardown(cls_session);
1310         return NULL;
1311 }
1312
1313
1314 /**
1315  * bnx2i_session_destroy - destroys iscsi session
1316  * @cls_session:        pointer to iscsi cls session
1317  *
1318  * Destroys previously created iSCSI session instance and releases
1319  *      all resources held by it
1320  */
1321 static void bnx2i_session_destroy(struct iscsi_cls_session *cls_session)
1322 {
1323         struct iscsi_session *session = cls_session->dd_data;
1324         struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1325         struct bnx2i_hba *hba = iscsi_host_priv(shost);
1326
1327         bnx2i_destroy_cmd_pool(hba, session);
1328         iscsi_session_teardown(cls_session);
1329 }
1330
1331
1332 /**
1333  * bnx2i_conn_create - create iscsi connection instance
1334  * @cls_session:        pointer to iscsi cls session
1335  * @cid:                iscsi cid as per rfc (not NX2's CID terminology)
1336  *
1337  * Creates a new iSCSI connection instance for a given session
1338  */
1339 static struct iscsi_cls_conn *
1340 bnx2i_conn_create(struct iscsi_cls_session *cls_session, uint32_t cid)
1341 {
1342         struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1343         struct bnx2i_hba *hba = iscsi_host_priv(shost);
1344         struct bnx2i_conn *bnx2i_conn;
1345         struct iscsi_cls_conn *cls_conn;
1346         struct iscsi_conn *conn;
1347
1348         cls_conn = iscsi_conn_setup(cls_session, sizeof(*bnx2i_conn),
1349                                     cid);
1350         if (!cls_conn)
1351                 return NULL;
1352         conn = cls_conn->dd_data;
1353
1354         bnx2i_conn = conn->dd_data;
1355         bnx2i_conn->cls_conn = cls_conn;
1356         bnx2i_conn->hba = hba;
1357         /* 'ep' ptr will be assigned in bind() call */
1358         bnx2i_conn->ep = NULL;
1359         init_completion(&bnx2i_conn->cmd_cleanup_cmpl);
1360
1361         if (bnx2i_conn_alloc_login_resources(hba, bnx2i_conn)) {
1362                 iscsi_conn_printk(KERN_ALERT, conn,
1363                                   "conn_new: login resc alloc failed!!\n");
1364                 goto free_conn;
1365         }
1366
1367         return cls_conn;
1368
1369 free_conn:
1370         iscsi_conn_teardown(cls_conn);
1371         return NULL;
1372 }
1373
1374 /**
1375  * bnx2i_conn_bind - binds iscsi sess, conn and ep objects together
1376  * @cls_session:        pointer to iscsi cls session
1377  * @cls_conn:           pointer to iscsi cls conn
1378  * @transport_fd:       64-bit EP handle
1379  * @is_leading:         leading connection on this session?
1380  *
1381  * Binds together iSCSI session instance, iSCSI connection instance
1382  *      and the TCP connection. This routine returns error code if
1383  *      TCP connection does not belong on the device iSCSI sess/conn
1384  *      is bound
1385  */
1386 static int bnx2i_conn_bind(struct iscsi_cls_session *cls_session,
1387                            struct iscsi_cls_conn *cls_conn,
1388                            uint64_t transport_fd, int is_leading)
1389 {
1390         struct iscsi_conn *conn = cls_conn->dd_data;
1391         struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1392         struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1393         struct bnx2i_hba *hba = iscsi_host_priv(shost);
1394         struct bnx2i_endpoint *bnx2i_ep;
1395         struct iscsi_endpoint *ep;
1396         int ret_code;
1397
1398         ep = iscsi_lookup_endpoint(transport_fd);
1399         if (!ep)
1400                 return -EINVAL;
1401         /*
1402          * Forcefully terminate all in progress connection recovery at the
1403          * earliest, either in bind(), send_pdu(LOGIN), or conn_start()
1404          */
1405         if (bnx2i_adapter_ready(hba))
1406                 return -EIO;
1407
1408         bnx2i_ep = ep->dd_data;
1409         if ((bnx2i_ep->state == EP_STATE_TCP_FIN_RCVD) ||
1410             (bnx2i_ep->state == EP_STATE_TCP_RST_RCVD))
1411                 /* Peer disconnect via' FIN or RST */
1412                 return -EINVAL;
1413
1414         if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1415                 return -EINVAL;
1416
1417         if (bnx2i_ep->hba != hba) {
1418                 /* Error - TCP connection does not belong to this device
1419                  */
1420                 iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
1421                                   "conn bind, ep=0x%p (%s) does not",
1422                                   bnx2i_ep, bnx2i_ep->hba->netdev->name);
1423                 iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
1424                                   "belong to hba (%s)\n",
1425                                   hba->netdev->name);
1426                 return -EEXIST;
1427         }
1428         bnx2i_ep->conn = bnx2i_conn;
1429         bnx2i_conn->ep = bnx2i_ep;
1430         bnx2i_conn->iscsi_conn_cid = bnx2i_ep->ep_iscsi_cid;
1431         bnx2i_conn->fw_cid = bnx2i_ep->ep_cid;
1432
1433         ret_code = bnx2i_bind_conn_to_iscsi_cid(hba, bnx2i_conn,
1434                                                 bnx2i_ep->ep_iscsi_cid);
1435
1436         /* 5706/5708/5709 FW takes RQ as full when initiated, but for 57710
1437          * driver needs to explicitly replenish RQ index during setup.
1438          */
1439         if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
1440                 bnx2i_put_rq_buf(bnx2i_conn, 0);
1441
1442         bnx2i_arm_cq_event_coalescing(bnx2i_conn->ep, CNIC_ARM_CQE);
1443         return ret_code;
1444 }
1445
1446
1447 /**
1448  * bnx2i_conn_destroy - destroy iscsi connection instance & release resources
1449  * @cls_conn:   pointer to iscsi cls conn
1450  *
1451  * Destroy an iSCSI connection instance and release memory resources held by
1452  *      this connection
1453  */
1454 static void bnx2i_conn_destroy(struct iscsi_cls_conn *cls_conn)
1455 {
1456         struct iscsi_conn *conn = cls_conn->dd_data;
1457         struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1458         struct Scsi_Host *shost;
1459         struct bnx2i_hba *hba;
1460
1461         shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
1462         hba = iscsi_host_priv(shost);
1463
1464         bnx2i_conn_free_login_resources(hba, bnx2i_conn);
1465         iscsi_conn_teardown(cls_conn);
1466 }
1467
1468
1469 /**
1470  * bnx2i_ep_get_param - return iscsi ep parameter to caller
1471  * @ep:         pointer to iscsi endpoint
1472  * @param:      parameter type identifier
1473  * @buf:        buffer pointer
1474  *
1475  * returns iSCSI ep parameters
1476  */
1477 static int bnx2i_ep_get_param(struct iscsi_endpoint *ep,
1478                               enum iscsi_param param, char *buf)
1479 {
1480         struct bnx2i_endpoint *bnx2i_ep = ep->dd_data;
1481         struct bnx2i_hba *hba = bnx2i_ep->hba;
1482         int len = -ENOTCONN;
1483
1484         if (!hba)
1485                 return -ENOTCONN;
1486
1487         switch (param) {
1488         case ISCSI_PARAM_CONN_PORT:
1489                 mutex_lock(&hba->net_dev_lock);
1490                 if (bnx2i_ep->cm_sk)
1491                         len = sprintf(buf, "%hu\n", bnx2i_ep->cm_sk->dst_port);
1492                 mutex_unlock(&hba->net_dev_lock);
1493                 break;
1494         case ISCSI_PARAM_CONN_ADDRESS:
1495                 mutex_lock(&hba->net_dev_lock);
1496                 if (bnx2i_ep->cm_sk)
1497                         len = sprintf(buf, "%pI4\n", &bnx2i_ep->cm_sk->dst_ip);
1498                 mutex_unlock(&hba->net_dev_lock);
1499                 break;
1500         default:
1501                 return -ENOSYS;
1502         }
1503
1504         return len;
1505 }
1506
1507 /**
1508  * bnx2i_host_get_param - returns host (adapter) related parameters
1509  * @shost:      scsi host pointer
1510  * @param:      parameter type identifier
1511  * @buf:        buffer pointer
1512  */
1513 static int bnx2i_host_get_param(struct Scsi_Host *shost,
1514                                 enum iscsi_host_param param, char *buf)
1515 {
1516         struct bnx2i_hba *hba = iscsi_host_priv(shost);
1517         int len = 0;
1518
1519         switch (param) {
1520         case ISCSI_HOST_PARAM_HWADDRESS:
1521                 len = sysfs_format_mac(buf, hba->cnic->mac_addr, 6);
1522                 break;
1523         case ISCSI_HOST_PARAM_NETDEV_NAME:
1524                 len = sprintf(buf, "%s\n", hba->netdev->name);
1525                 break;
1526         case ISCSI_HOST_PARAM_IPADDRESS: {
1527                 struct list_head *active_list = &hba->ep_active_list;
1528
1529                 read_lock_bh(&hba->ep_rdwr_lock);
1530                 if (!list_empty(&hba->ep_active_list)) {
1531                         struct bnx2i_endpoint *bnx2i_ep;
1532                         struct cnic_sock *csk;
1533
1534                         bnx2i_ep = list_first_entry(active_list,
1535                                                     struct bnx2i_endpoint,
1536                                                     link);
1537                         csk = bnx2i_ep->cm_sk;
1538                         if (test_bit(SK_F_IPV6, &csk->flags))
1539                                 len = sprintf(buf, "%pI6\n", csk->src_ip);
1540                         else
1541                                 len = sprintf(buf, "%pI4\n", csk->src_ip);
1542                 }
1543                 read_unlock_bh(&hba->ep_rdwr_lock);
1544                 break;
1545         }
1546         default:
1547                 return iscsi_host_get_param(shost, param, buf);
1548         }
1549         return len;
1550 }
1551
1552 /**
1553  * bnx2i_conn_start - completes iscsi connection migration to FFP
1554  * @cls_conn:   pointer to iscsi cls conn
1555  *
1556  * last call in FFP migration to handover iscsi conn to the driver
1557  */
1558 static int bnx2i_conn_start(struct iscsi_cls_conn *cls_conn)
1559 {
1560         struct iscsi_conn *conn = cls_conn->dd_data;
1561         struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1562
1563         bnx2i_conn->ep->state = EP_STATE_ULP_UPDATE_START;
1564         bnx2i_update_iscsi_conn(conn);
1565
1566         /*
1567          * this should normally not sleep for a long time so it should
1568          * not disrupt the caller.
1569          */
1570         bnx2i_conn->ep->ofld_timer.expires = 1 * HZ + jiffies;
1571         bnx2i_conn->ep->ofld_timer.function = bnx2i_ep_ofld_timer;
1572         bnx2i_conn->ep->ofld_timer.data = (unsigned long) bnx2i_conn->ep;
1573         add_timer(&bnx2i_conn->ep->ofld_timer);
1574         /* update iSCSI context for this conn, wait for CNIC to complete */
1575         wait_event_interruptible(bnx2i_conn->ep->ofld_wait,
1576                         bnx2i_conn->ep->state != EP_STATE_ULP_UPDATE_START);
1577
1578         if (signal_pending(current))
1579                 flush_signals(current);
1580         del_timer_sync(&bnx2i_conn->ep->ofld_timer);
1581
1582         iscsi_conn_start(cls_conn);
1583         return 0;
1584 }
1585
1586
1587 /**
1588  * bnx2i_conn_get_stats - returns iSCSI stats
1589  * @cls_conn:   pointer to iscsi cls conn
1590  * @stats:      pointer to iscsi statistic struct
1591  */
1592 static void bnx2i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1593                                  struct iscsi_stats *stats)
1594 {
1595         struct iscsi_conn *conn = cls_conn->dd_data;
1596
1597         stats->txdata_octets = conn->txdata_octets;
1598         stats->rxdata_octets = conn->rxdata_octets;
1599         stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1600         stats->dataout_pdus = conn->dataout_pdus_cnt;
1601         stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1602         stats->datain_pdus = conn->datain_pdus_cnt;
1603         stats->r2t_pdus = conn->r2t_pdus_cnt;
1604         stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1605         stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1606         stats->custom_length = 3;
1607         strcpy(stats->custom[2].desc, "eh_abort_cnt");
1608         stats->custom[2].value = conn->eh_abort_cnt;
1609         stats->digest_err = 0;
1610         stats->timeout_err = 0;
1611         stats->custom_length = 0;
1612 }
1613
1614
1615 /**
1616  * bnx2i_check_route - checks if target IP route belongs to one of NX2 devices
1617  * @dst_addr:   target IP address
1618  *
1619  * check if route resolves to BNX2 device
1620  */
1621 static struct bnx2i_hba *bnx2i_check_route(struct sockaddr *dst_addr)
1622 {
1623         struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
1624         struct bnx2i_hba *hba;
1625         struct cnic_dev *cnic = NULL;
1626
1627         hba = get_adapter_list_head();
1628         if (hba && hba->cnic)
1629                 cnic = hba->cnic->cm_select_dev(desti, CNIC_ULP_ISCSI);
1630         if (!cnic) {
1631                 printk(KERN_ALERT "bnx2i: no route,"
1632                        "can't connect using cnic\n");
1633                 goto no_nx2_route;
1634         }
1635         hba = bnx2i_find_hba_for_cnic(cnic);
1636         if (!hba)
1637                 goto no_nx2_route;
1638
1639         if (bnx2i_adapter_ready(hba)) {
1640                 printk(KERN_ALERT "bnx2i: check route, hba not found\n");
1641                 goto no_nx2_route;
1642         }
1643         if (hba->netdev->mtu > hba->mtu_supported) {
1644                 printk(KERN_ALERT "bnx2i: %s network i/f mtu is set to %d\n",
1645                                   hba->netdev->name, hba->netdev->mtu);
1646                 printk(KERN_ALERT "bnx2i: iSCSI HBA can support mtu of %d\n",
1647                                   hba->mtu_supported);
1648                 goto no_nx2_route;
1649         }
1650         return hba;
1651 no_nx2_route:
1652         return NULL;
1653 }
1654
1655
1656 /**
1657  * bnx2i_tear_down_conn - tear down iscsi/tcp connection and free resources
1658  * @hba:        pointer to adapter instance
1659  * @ep:         endpoint (transport indentifier) structure
1660  *
1661  * destroys cm_sock structure and on chip iscsi context
1662  */
1663 static int bnx2i_tear_down_conn(struct bnx2i_hba *hba,
1664                                  struct bnx2i_endpoint *ep)
1665 {
1666         if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) && ep->cm_sk)
1667                 hba->cnic->cm_destroy(ep->cm_sk);
1668
1669         if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type) &&
1670             ep->state == EP_STATE_DISCONN_TIMEDOUT) {
1671                 if (ep->conn && ep->conn->cls_conn &&
1672                     ep->conn->cls_conn->dd_data) {
1673                         struct iscsi_conn *conn = ep->conn->cls_conn->dd_data;
1674
1675                         /* Must suspend all rx queue activity for this ep */
1676                         set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1677                 }
1678                 /* CONN_DISCONNECT timeout may or may not be an issue depending
1679                  * on what transcribed in TCP layer, different targets behave
1680                  * differently
1681                  */
1682                 printk(KERN_ALERT "bnx2i (%s): - WARN - CONN_DISCON timed out, "
1683                                   "please submit GRC Dump, NW/PCIe trace, "
1684                                   "driver msgs to developers for analysis\n",
1685                                   hba->netdev->name);
1686         }
1687
1688         ep->state = EP_STATE_CLEANUP_START;
1689         init_timer(&ep->ofld_timer);
1690         ep->ofld_timer.expires = hba->conn_ctx_destroy_tmo + jiffies;
1691         ep->ofld_timer.function = bnx2i_ep_ofld_timer;
1692         ep->ofld_timer.data = (unsigned long) ep;
1693         add_timer(&ep->ofld_timer);
1694
1695         bnx2i_ep_destroy_list_add(hba, ep);
1696
1697         /* destroy iSCSI context, wait for it to complete */
1698         if (bnx2i_send_conn_destroy(hba, ep))
1699                 ep->state = EP_STATE_CLEANUP_CMPL;
1700
1701         wait_event_interruptible(ep->ofld_wait,
1702                                  (ep->state != EP_STATE_CLEANUP_START));
1703
1704         if (signal_pending(current))
1705                 flush_signals(current);
1706         del_timer_sync(&ep->ofld_timer);
1707
1708         bnx2i_ep_destroy_list_del(hba, ep);
1709
1710         if (ep->state != EP_STATE_CLEANUP_CMPL)
1711                 /* should never happen */
1712                 printk(KERN_ALERT "bnx2i - conn destroy failed\n");
1713
1714         return 0;
1715 }
1716
1717
1718 /**
1719  * bnx2i_ep_connect - establish TCP connection to target portal
1720  * @shost:              scsi host
1721  * @dst_addr:           target IP address
1722  * @non_blocking:       blocking or non-blocking call
1723  *
1724  * this routine initiates the TCP/IP connection by invoking Option-2 i/f
1725  *      with l5_core and the CNIC. This is a multi-step process of resolving
1726  *      route to target, create a iscsi connection context, handshaking with
1727  *      CNIC module to create/initialize the socket struct and finally
1728  *      sending down option-2 request to complete TCP 3-way handshake
1729  */
1730 static struct iscsi_endpoint *bnx2i_ep_connect(struct Scsi_Host *shost,
1731                                                struct sockaddr *dst_addr,
1732                                                int non_blocking)
1733 {
1734         u32 iscsi_cid = BNX2I_CID_RESERVED;
1735         struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
1736         struct sockaddr_in6 *desti6;
1737         struct bnx2i_endpoint *bnx2i_ep;
1738         struct bnx2i_hba *hba;
1739         struct cnic_dev *cnic;
1740         struct cnic_sockaddr saddr;
1741         struct iscsi_endpoint *ep;
1742         int rc = 0;
1743
1744         if (shost) {
1745                 /* driver is given scsi host to work with */
1746                 hba = iscsi_host_priv(shost);
1747         } else
1748                 /*
1749                  * check if the given destination can be reached through
1750                  * a iscsi capable NetXtreme2 device
1751                  */
1752                 hba = bnx2i_check_route(dst_addr);
1753
1754         if (!hba) {
1755                 rc = -EINVAL;
1756                 goto nohba;
1757         }
1758         mutex_lock(&hba->net_dev_lock);
1759
1760         if (bnx2i_adapter_ready(hba) || !hba->cid_que.cid_free_cnt) {
1761                 rc = -EPERM;
1762                 goto check_busy;
1763         }
1764         cnic = hba->cnic;
1765         ep = bnx2i_alloc_ep(hba);
1766         if (!ep) {
1767                 rc = -ENOMEM;
1768                 goto check_busy;
1769         }
1770         bnx2i_ep = ep->dd_data;
1771
1772         bnx2i_ep->num_active_cmds = 0;
1773         iscsi_cid = bnx2i_alloc_iscsi_cid(hba);
1774         if (iscsi_cid == -1) {
1775                 printk(KERN_ALERT "bnx2i (%s): alloc_ep - unable to allocate "
1776                         "iscsi cid\n", hba->netdev->name);
1777                 rc = -ENOMEM;
1778                 bnx2i_free_ep(ep);
1779                 goto check_busy;
1780         }
1781         bnx2i_ep->hba_age = hba->age;
1782
1783         rc = bnx2i_alloc_qp_resc(hba, bnx2i_ep);
1784         if (rc != 0) {
1785                 printk(KERN_ALERT "bnx2i (%s): ep_conn - alloc QP resc error"
1786                         "\n", hba->netdev->name);
1787                 rc = -ENOMEM;
1788                 goto qp_resc_err;
1789         }
1790
1791         bnx2i_ep->ep_iscsi_cid = (u16)iscsi_cid;
1792         bnx2i_ep->state = EP_STATE_OFLD_START;
1793         bnx2i_ep_ofld_list_add(hba, bnx2i_ep);
1794
1795         init_timer(&bnx2i_ep->ofld_timer);
1796         bnx2i_ep->ofld_timer.expires = 2 * HZ + jiffies;
1797         bnx2i_ep->ofld_timer.function = bnx2i_ep_ofld_timer;
1798         bnx2i_ep->ofld_timer.data = (unsigned long) bnx2i_ep;
1799         add_timer(&bnx2i_ep->ofld_timer);
1800
1801         if (bnx2i_send_conn_ofld_req(hba, bnx2i_ep)) {
1802                 if (bnx2i_ep->state == EP_STATE_OFLD_FAILED_CID_BUSY) {
1803                         printk(KERN_ALERT "bnx2i (%s): iscsi cid %d is busy\n",
1804                                 hba->netdev->name, bnx2i_ep->ep_iscsi_cid);
1805                         rc = -EBUSY;
1806                 } else
1807                         rc = -ENOSPC;
1808                 printk(KERN_ALERT "bnx2i (%s): unable to send conn offld kwqe"
1809                         "\n", hba->netdev->name);
1810                 bnx2i_ep_ofld_list_del(hba, bnx2i_ep);
1811                 goto conn_failed;
1812         }
1813
1814         /* Wait for CNIC hardware to setup conn context and return 'cid' */
1815         wait_event_interruptible(bnx2i_ep->ofld_wait,
1816                                  bnx2i_ep->state != EP_STATE_OFLD_START);
1817
1818         if (signal_pending(current))
1819                 flush_signals(current);
1820         del_timer_sync(&bnx2i_ep->ofld_timer);
1821
1822         bnx2i_ep_ofld_list_del(hba, bnx2i_ep);
1823
1824         if (bnx2i_ep->state != EP_STATE_OFLD_COMPL) {
1825                 if (bnx2i_ep->state == EP_STATE_OFLD_FAILED_CID_BUSY) {
1826                         printk(KERN_ALERT "bnx2i (%s): iscsi cid %d is busy\n",
1827                                 hba->netdev->name, bnx2i_ep->ep_iscsi_cid);
1828                         rc = -EBUSY;
1829                 } else
1830                         rc = -ENOSPC;
1831                 goto conn_failed;
1832         }
1833
1834         rc = cnic->cm_create(cnic, CNIC_ULP_ISCSI, bnx2i_ep->ep_cid,
1835                              iscsi_cid, &bnx2i_ep->cm_sk, bnx2i_ep);
1836         if (rc) {
1837                 rc = -EINVAL;
1838                 /* Need to terminate and cleanup the connection */
1839                 goto release_ep;
1840         }
1841
1842         bnx2i_ep->cm_sk->rcv_buf = 256 * 1024;
1843         bnx2i_ep->cm_sk->snd_buf = 256 * 1024;
1844         clear_bit(SK_TCP_TIMESTAMP, &bnx2i_ep->cm_sk->tcp_flags);
1845
1846         memset(&saddr, 0, sizeof(saddr));
1847         if (dst_addr->sa_family == AF_INET) {
1848                 desti = (struct sockaddr_in *) dst_addr;
1849                 saddr.remote.v4 = *desti;
1850                 saddr.local.v4.sin_family = desti->sin_family;
1851         } else if (dst_addr->sa_family == AF_INET6) {
1852                 desti6 = (struct sockaddr_in6 *) dst_addr;
1853                 saddr.remote.v6 = *desti6;
1854                 saddr.local.v6.sin6_family = desti6->sin6_family;
1855         }
1856
1857         bnx2i_ep->timestamp = jiffies;
1858         bnx2i_ep->state = EP_STATE_CONNECT_START;
1859         if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
1860                 rc = -EINVAL;
1861                 goto conn_failed;
1862         } else
1863                 rc = cnic->cm_connect(bnx2i_ep->cm_sk, &saddr);
1864         if (rc)
1865                 goto release_ep;
1866
1867         bnx2i_ep_active_list_add(hba, bnx2i_ep);
1868
1869         if (bnx2i_map_ep_dbell_regs(bnx2i_ep))
1870                 goto del_active_ep;
1871
1872         mutex_unlock(&hba->net_dev_lock);
1873         return ep;
1874
1875 del_active_ep:
1876         bnx2i_ep_active_list_del(hba, bnx2i_ep);
1877 release_ep:
1878         if (bnx2i_tear_down_conn(hba, bnx2i_ep)) {
1879                 mutex_unlock(&hba->net_dev_lock);
1880                 return ERR_PTR(rc);
1881         }
1882 conn_failed:
1883         bnx2i_free_qp_resc(hba, bnx2i_ep);
1884 qp_resc_err:
1885         bnx2i_free_ep(ep);
1886 check_busy:
1887         mutex_unlock(&hba->net_dev_lock);
1888 nohba:
1889         return ERR_PTR(rc);
1890 }
1891
1892
1893 /**
1894  * bnx2i_ep_poll - polls for TCP connection establishement
1895  * @ep:                 TCP connection (endpoint) handle
1896  * @timeout_ms:         timeout value in milli secs
1897  *
1898  * polls for TCP connect request to complete
1899  */
1900 static int bnx2i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1901 {
1902         struct bnx2i_endpoint *bnx2i_ep;
1903         int rc = 0;
1904
1905         bnx2i_ep = ep->dd_data;
1906         if ((bnx2i_ep->state == EP_STATE_IDLE) ||
1907             (bnx2i_ep->state == EP_STATE_CONNECT_FAILED) ||
1908             (bnx2i_ep->state == EP_STATE_OFLD_FAILED))
1909                 return -1;
1910         if (bnx2i_ep->state == EP_STATE_CONNECT_COMPL)
1911                 return 1;
1912
1913         rc = wait_event_interruptible_timeout(bnx2i_ep->ofld_wait,
1914                                               ((bnx2i_ep->state ==
1915                                                 EP_STATE_OFLD_FAILED) ||
1916                                                (bnx2i_ep->state ==
1917                                                 EP_STATE_CONNECT_FAILED) ||
1918                                                (bnx2i_ep->state ==
1919                                                 EP_STATE_CONNECT_COMPL)),
1920                                               msecs_to_jiffies(timeout_ms));
1921         if (bnx2i_ep->state == EP_STATE_OFLD_FAILED)
1922                 rc = -1;
1923
1924         if (rc > 0)
1925                 return 1;
1926         else if (!rc)
1927                 return 0;       /* timeout */
1928         else
1929                 return rc;
1930 }
1931
1932
1933 /**
1934  * bnx2i_ep_tcp_conn_active - check EP state transition
1935  * @ep:         endpoint pointer
1936  *
1937  * check if underlying TCP connection is active
1938  */
1939 static int bnx2i_ep_tcp_conn_active(struct bnx2i_endpoint *bnx2i_ep)
1940 {
1941         int ret;
1942         int cnic_dev_10g = 0;
1943
1944         if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
1945                 cnic_dev_10g = 1;
1946
1947         switch (bnx2i_ep->state) {
1948         case EP_STATE_CLEANUP_FAILED:
1949         case EP_STATE_OFLD_FAILED:
1950         case EP_STATE_DISCONN_TIMEDOUT:
1951                 ret = 0;
1952                 break;
1953         case EP_STATE_CONNECT_START:
1954         case EP_STATE_CONNECT_FAILED:
1955         case EP_STATE_CONNECT_COMPL:
1956         case EP_STATE_ULP_UPDATE_START:
1957         case EP_STATE_ULP_UPDATE_COMPL:
1958         case EP_STATE_TCP_FIN_RCVD:
1959         case EP_STATE_LOGOUT_SENT:
1960         case EP_STATE_LOGOUT_RESP_RCVD:
1961         case EP_STATE_ULP_UPDATE_FAILED:
1962                 ret = 1;
1963                 break;
1964         case EP_STATE_TCP_RST_RCVD:
1965                 if (cnic_dev_10g)
1966                         ret = 0;
1967                 else
1968                         ret = 1;
1969                 break;
1970         default:
1971                 ret = 0;
1972         }
1973
1974         return ret;
1975 }
1976
1977
1978 /*
1979  * bnx2i_hw_ep_disconnect - executes TCP connection teardown process in the hw
1980  * @ep:         TCP connection (bnx2i endpoint) handle
1981  *
1982  * executes  TCP connection teardown process
1983  */
1984 int bnx2i_hw_ep_disconnect(struct bnx2i_endpoint *bnx2i_ep)
1985 {
1986         struct bnx2i_hba *hba = bnx2i_ep->hba;
1987         struct cnic_dev *cnic;
1988         struct iscsi_session *session = NULL;
1989         struct iscsi_conn *conn = NULL;
1990         int ret = 0;
1991         int close = 0;
1992         int close_ret = 0;
1993
1994         if (!hba)
1995                 return 0;
1996
1997         cnic = hba->cnic;
1998         if (!cnic)
1999                 return 0;
2000
2001         if (bnx2i_ep->state == EP_STATE_IDLE ||
2002             bnx2i_ep->state == EP_STATE_DISCONN_TIMEDOUT)
2003                 return 0;
2004
2005         if (!bnx2i_ep_tcp_conn_active(bnx2i_ep))
2006                 goto destroy_conn;
2007
2008         if (bnx2i_ep->conn) {
2009                 conn = bnx2i_ep->conn->cls_conn->dd_data;
2010                 session = conn->session;
2011         }
2012
2013         init_timer(&bnx2i_ep->ofld_timer);
2014         bnx2i_ep->ofld_timer.expires = hba->conn_teardown_tmo + jiffies;
2015         bnx2i_ep->ofld_timer.function = bnx2i_ep_ofld_timer;
2016         bnx2i_ep->ofld_timer.data = (unsigned long) bnx2i_ep;
2017         add_timer(&bnx2i_ep->ofld_timer);
2018
2019         if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic))
2020                 goto out;
2021
2022         if (session) {
2023                 spin_lock_bh(&session->lock);
2024                 if (bnx2i_ep->state != EP_STATE_TCP_FIN_RCVD) {
2025                         if (session->state == ISCSI_STATE_LOGGING_OUT) {
2026                                 if (bnx2i_ep->state == EP_STATE_LOGOUT_SENT) {
2027                                         /* Logout sent, but no resp */
2028                                         printk(KERN_ALERT "bnx2i (%s): WARNING"
2029                                                 " logout response was not "
2030                                                 "received!\n",
2031                                                 bnx2i_ep->hba->netdev->name);
2032                                 } else if (bnx2i_ep->state ==
2033                                            EP_STATE_LOGOUT_RESP_RCVD)
2034                                         close = 1;
2035                         }
2036                 } else
2037                         close = 1;
2038
2039                 spin_unlock_bh(&session->lock);
2040         }
2041
2042         bnx2i_ep->state = EP_STATE_DISCONN_START;
2043
2044         if (close)
2045                 close_ret = cnic->cm_close(bnx2i_ep->cm_sk);
2046         else
2047                 close_ret = cnic->cm_abort(bnx2i_ep->cm_sk);
2048
2049         if (close_ret)
2050                 printk(KERN_ALERT "bnx2i (%s): close/abort(%d) returned %d\n",
2051                         bnx2i_ep->hba->netdev->name, close, close_ret);
2052         else
2053                 /* wait for option-2 conn teardown */
2054                 wait_event_interruptible(bnx2i_ep->ofld_wait,
2055                                  bnx2i_ep->state != EP_STATE_DISCONN_START);
2056
2057         if (signal_pending(current))
2058                 flush_signals(current);
2059         del_timer_sync(&bnx2i_ep->ofld_timer);
2060
2061 destroy_conn:
2062         bnx2i_ep_active_list_del(hba, bnx2i_ep);
2063         if (bnx2i_tear_down_conn(hba, bnx2i_ep))
2064                 return -EINVAL;
2065 out:
2066         bnx2i_ep->state = EP_STATE_IDLE;
2067         return ret;
2068 }
2069
2070
2071 /**
2072  * bnx2i_ep_disconnect - executes TCP connection teardown process
2073  * @ep:         TCP connection (iscsi endpoint) handle
2074  *
2075  * executes  TCP connection teardown process
2076  */
2077 static void bnx2i_ep_disconnect(struct iscsi_endpoint *ep)
2078 {
2079         struct bnx2i_endpoint *bnx2i_ep;
2080         struct bnx2i_conn *bnx2i_conn = NULL;
2081         struct iscsi_conn *conn = NULL;
2082         struct bnx2i_hba *hba;
2083
2084         bnx2i_ep = ep->dd_data;
2085
2086         /* driver should not attempt connection cleanup until TCP_CONNECT
2087          * completes either successfully or fails. Timeout is 9-secs, so
2088          * wait for it to complete
2089          */
2090         while ((bnx2i_ep->state == EP_STATE_CONNECT_START) &&
2091                 !time_after(jiffies, bnx2i_ep->timestamp + (12 * HZ)))
2092                 msleep(250);
2093
2094         if (bnx2i_ep->conn) {
2095                 bnx2i_conn = bnx2i_ep->conn;
2096                 conn = bnx2i_conn->cls_conn->dd_data;
2097                 iscsi_suspend_queue(conn);
2098         }
2099         hba = bnx2i_ep->hba;
2100
2101         mutex_lock(&hba->net_dev_lock);
2102
2103         if (bnx2i_ep->state == EP_STATE_DISCONN_TIMEDOUT)
2104                 goto out;
2105
2106         if (bnx2i_ep->state == EP_STATE_IDLE)
2107                 goto free_resc;
2108
2109         if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) ||
2110             (bnx2i_ep->hba_age != hba->age)) {
2111                 bnx2i_ep_active_list_del(hba, bnx2i_ep);
2112                 goto free_resc;
2113         }
2114
2115         /* Do all chip cleanup here */
2116         if (bnx2i_hw_ep_disconnect(bnx2i_ep)) {
2117                 mutex_unlock(&hba->net_dev_lock);
2118                 return;
2119         }
2120 free_resc:
2121         bnx2i_free_qp_resc(hba, bnx2i_ep);
2122
2123         if (bnx2i_conn)
2124                 bnx2i_conn->ep = NULL;
2125
2126         bnx2i_free_ep(ep);
2127 out:
2128         mutex_unlock(&hba->net_dev_lock);
2129
2130         wake_up_interruptible(&hba->eh_wait);
2131 }
2132
2133
2134 /**
2135  * bnx2i_nl_set_path - ISCSI_UEVENT_PATH_UPDATE user message handler
2136  * @buf:        pointer to buffer containing iscsi path message
2137  *
2138  */
2139 static int bnx2i_nl_set_path(struct Scsi_Host *shost, struct iscsi_path *params)
2140 {
2141         struct bnx2i_hba *hba = iscsi_host_priv(shost);
2142         char *buf = (char *) params;
2143         u16 len = sizeof(*params);
2144
2145         /* handled by cnic driver */
2146         hba->cnic->iscsi_nl_msg_recv(hba->cnic, ISCSI_UEVENT_PATH_UPDATE, buf,
2147                                      len);
2148
2149         return 0;
2150 }
2151
2152
2153 /*
2154  * 'Scsi_Host_Template' structure and 'iscsi_tranport' structure template
2155  * used while registering with the scsi host and iSCSI transport module.
2156  */
2157 static struct scsi_host_template bnx2i_host_template = {
2158         .module                 = THIS_MODULE,
2159         .name                   = "Broadcom Offload iSCSI Initiator",
2160         .proc_name              = "bnx2i",
2161         .queuecommand           = iscsi_queuecommand,
2162         .eh_abort_handler       = iscsi_eh_abort,
2163         .eh_device_reset_handler = iscsi_eh_device_reset,
2164         .eh_target_reset_handler = iscsi_eh_recover_target,
2165         .change_queue_depth     = iscsi_change_queue_depth,
2166         .can_queue              = 1024,
2167         .max_sectors            = 127,
2168         .cmd_per_lun            = 24,
2169         .this_id                = -1,
2170         .use_clustering         = ENABLE_CLUSTERING,
2171         .sg_tablesize           = ISCSI_MAX_BDS_PER_CMD,
2172         .shost_attrs            = bnx2i_dev_attributes,
2173 };
2174
2175 struct iscsi_transport bnx2i_iscsi_transport = {
2176         .owner                  = THIS_MODULE,
2177         .name                   = "bnx2i",
2178         .caps                   = CAP_RECOVERY_L0 | CAP_HDRDGST |
2179                                   CAP_MULTI_R2T | CAP_DATADGST |
2180                                   CAP_DATA_PATH_OFFLOAD |
2181                                   CAP_TEXT_NEGO,
2182         .param_mask             = ISCSI_MAX_RECV_DLENGTH |
2183                                   ISCSI_MAX_XMIT_DLENGTH |
2184                                   ISCSI_HDRDGST_EN |
2185                                   ISCSI_DATADGST_EN |
2186                                   ISCSI_INITIAL_R2T_EN |
2187                                   ISCSI_MAX_R2T |
2188                                   ISCSI_IMM_DATA_EN |
2189                                   ISCSI_FIRST_BURST |
2190                                   ISCSI_MAX_BURST |
2191                                   ISCSI_PDU_INORDER_EN |
2192                                   ISCSI_DATASEQ_INORDER_EN |
2193                                   ISCSI_ERL |
2194                                   ISCSI_CONN_PORT |
2195                                   ISCSI_CONN_ADDRESS |
2196                                   ISCSI_EXP_STATSN |
2197                                   ISCSI_PERSISTENT_PORT |
2198                                   ISCSI_PERSISTENT_ADDRESS |
2199                                   ISCSI_TARGET_NAME | ISCSI_TPGT |
2200                                   ISCSI_USERNAME | ISCSI_PASSWORD |
2201                                   ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
2202                                   ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
2203                                   ISCSI_LU_RESET_TMO | ISCSI_TGT_RESET_TMO |
2204                                   ISCSI_PING_TMO | ISCSI_RECV_TMO |
2205                                   ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
2206         .host_param_mask        = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
2207                                   ISCSI_HOST_NETDEV_NAME,
2208         .create_session         = bnx2i_session_create,
2209         .destroy_session        = bnx2i_session_destroy,
2210         .create_conn            = bnx2i_conn_create,
2211         .bind_conn              = bnx2i_conn_bind,
2212         .destroy_conn           = bnx2i_conn_destroy,
2213         .set_param              = iscsi_set_param,
2214         .get_conn_param         = iscsi_conn_get_param,
2215         .get_session_param      = iscsi_session_get_param,
2216         .get_host_param         = bnx2i_host_get_param,
2217         .start_conn             = bnx2i_conn_start,
2218         .stop_conn              = iscsi_conn_stop,
2219         .send_pdu               = iscsi_conn_send_pdu,
2220         .xmit_task              = bnx2i_task_xmit,
2221         .get_stats              = bnx2i_conn_get_stats,
2222         /* TCP connect - disconnect - option-2 interface calls */
2223         .get_ep_param           = bnx2i_ep_get_param,
2224         .ep_connect             = bnx2i_ep_connect,
2225         .ep_poll                = bnx2i_ep_poll,
2226         .ep_disconnect          = bnx2i_ep_disconnect,
2227         .set_path               = bnx2i_nl_set_path,
2228         /* Error recovery timeout call */
2229         .session_recovery_timedout = iscsi_session_recovery_timedout,
2230         .cleanup_task           = bnx2i_cleanup_task,
2231 };