2 * bnx2i_iscsi.c: Broadcom NetXtreme II iSCSI driver.
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
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.
12 * Written by: Anil Veerabhadrappa (anilgv@broadcom.com)
13 * Maintained by: Eddie Wai (eddie.wai@broadcom.com)
16 #include <linux/slab.h>
17 #include <scsi/scsi_tcq.h>
18 #include <scsi/libiscsi.h>
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;
26 * Global endpoint resource info
28 static DEFINE_SPINLOCK(bnx2i_resc_lock); /* protects global resources */
31 static int bnx2i_adapter_ready(struct bnx2i_hba *hba)
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))
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
50 * identifies & marks various bd info for scsi command's imm data,
51 * unsolicited data and the first solicited data seq.
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)
56 struct iscsi_bd *bd_tbl = cmd->io_tbl.bd_tbl;
61 while (buf_off >= (cur_offset + bd_tbl->buffer_length)) {
62 cur_offset += bd_tbl->buffer_length;
68 *start_bd_off = buf_off - cur_offset;
69 *start_bd_idx = cur_bd_idx;
73 * bnx2i_setup_write_cmd_bd_info - sets up BD various information
74 * @task: transport layer's cmd struct pointer
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.
81 static void bnx2i_setup_write_cmd_bd_info(struct iscsi_task *task)
83 struct bnx2i_cmd *cmd = task->dd_data;
86 u32 buffer_offset = 0;
87 u32 cmd_len = cmd->req.total_data_transfer_length;
89 /* if ImmediateData is turned off & IntialR2T is turned on,
90 * there will be no immediate or unsolicited data, just return.
92 if (!iscsi_task_has_unsol_data(task) && !task->imm_count)
96 buffer_offset += task->imm_count;
97 if (task->imm_count == cmd_len)
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;
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))) {
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].\
126 cmd->req.sd_buffer_offset = start_bd_offset;
127 cmd->req.sd_start_bd_index = start_bd_idx;
134 * bnx2i_map_scsi_sg - maps IO buffer and prepares the BD table
135 * @hba: adapter instance
136 * @cmd: iscsi cmd struct pointer
140 static int bnx2i_map_scsi_sg(struct bnx2i_hba *hba, struct bnx2i_cmd *cmd)
142 struct scsi_cmnd *sc = cmd->scsi_cmd;
143 struct iscsi_bd *bd = cmd->io_tbl.bd_tbl;
144 struct scatterlist *sg;
152 BUG_ON(scsi_sg_count(sc) > ISCSI_MAX_BDS_PER_CMD);
154 sg_count = scsi_dma_map(sc);
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;
164 bd[bd_count].flags = ISCSI_BD_FIRST_IN_BD_CHAIN;
166 byte_count += sg_len;
171 bd[bd_count - 1].flags |= ISCSI_BD_LAST_IN_BD_CHAIN;
173 BUG_ON(byte_count != scsi_bufflen(sc));
178 * bnx2i_iscsi_map_sg_list - maps SG list
179 * @cmd: iscsi cmd struct pointer
181 * creates BD list table for the command
183 static void bnx2i_iscsi_map_sg_list(struct bnx2i_cmd *cmd)
187 bd_count = bnx2i_map_scsi_sg(cmd->conn->hba, cmd);
189 struct iscsi_bd *bd = cmd->io_tbl.bd_tbl;
191 bd[0].buffer_addr_lo = bd[0].buffer_addr_hi = 0;
192 bd[0].buffer_length = bd[0].flags = 0;
194 cmd->io_tbl.bd_valid = bd_count;
199 * bnx2i_iscsi_unmap_sg_list - unmaps SG list
200 * @cmd: iscsi cmd struct pointer
202 * unmap IO buffers and invalidate the BD table
204 void bnx2i_iscsi_unmap_sg_list(struct bnx2i_cmd *cmd)
206 struct scsi_cmnd *sc = cmd->scsi_cmd;
208 if (cmd->io_tbl.bd_valid && sc) {
210 cmd->io_tbl.bd_valid = 0;
214 static void bnx2i_setup_cmd_wqe_template(struct bnx2i_cmd *cmd)
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);
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)
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
235 static int bnx2i_bind_conn_to_iscsi_cid(struct bnx2i_hba *hba,
236 struct bnx2i_conn *bnx2i_conn,
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);
245 hba->cid_que.conn_cid_tbl[iscsi_cid] = bnx2i_conn;
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)
255 struct bnx2i_conn *bnx2i_get_conn_from_id(struct bnx2i_hba *hba,
258 if (!hba->cid_que.conn_cid_tbl) {
259 printk(KERN_ERR "bnx2i: ERROR - missing conn<->cid table\n");
262 } else if (iscsi_cid >= hba->max_active_conns) {
263 printk(KERN_ERR "bnx2i: wrong cid #%d\n", iscsi_cid);
266 return hba->cid_que.conn_cid_tbl[iscsi_cid];
271 * bnx2i_alloc_iscsi_cid - allocates a iscsi_cid from free pool
272 * @hba: pointer to adapter instance
274 static u32 bnx2i_alloc_iscsi_cid(struct bnx2i_hba *hba)
278 if (!hba->cid_que.cid_free_cnt)
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;
286 hba->cid_que.cid_free_cnt--;
287 return hba->cid_que.cid_que[idx];
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
296 static void bnx2i_free_iscsi_cid(struct bnx2i_hba *hba, u16 iscsi_cid)
300 if (iscsi_cid == (u16) -1)
303 hba->cid_que.cid_free_cnt++;
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;
315 * bnx2i_setup_free_cid_que - sets up free iscsi cid queue
316 * @hba: pointer to adapter instance
318 * allocates memory for iscsi cid queue & 'cid - conn ptr' mapping table,
319 * and initialize table attributes
321 static int bnx2i_setup_free_cid_que(struct bnx2i_hba *hba)
326 mem_size = hba->max_active_conns * sizeof(u32);
327 mem_size = (mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
329 hba->cid_que.cid_que_base = kmalloc(mem_size, GFP_KERNEL);
330 if (!hba->cid_que.cid_que_base)
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;
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;
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;
357 * bnx2i_release_free_cid_que - releases 'iscsi_cid' queue resources
358 * @hba: pointer to adapter instance
360 static void bnx2i_release_free_cid_que(struct bnx2i_hba *hba)
362 kfree(hba->cid_que.cid_que_base);
363 hba->cid_que.cid_que_base = NULL;
365 kfree(hba->cid_que.conn_cid_tbl);
366 hba->cid_que.conn_cid_tbl = NULL;
371 * bnx2i_alloc_ep - allocates ep structure from global pool
372 * @hba: pointer to adapter instance
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
378 static struct iscsi_endpoint *bnx2i_alloc_ep(struct bnx2i_hba *hba)
380 struct iscsi_endpoint *ep;
381 struct bnx2i_endpoint *bnx2i_ep;
384 ep = iscsi_create_endpoint(sizeof(*bnx2i_ep));
386 printk(KERN_ERR "bnx2i: Could not allocate ep\n");
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;
396 bnx2i_ep->hba_age = hba->age;
398 ec_div = event_coal_div;
400 bnx2i_ep->ec_shift += 1;
402 hba->ofld_conns_active++;
403 init_waitqueue_head(&bnx2i_ep->ofld_wait);
409 * bnx2i_free_ep - free endpoint
410 * @ep: pointer to iscsi endpoint structure
412 static void bnx2i_free_ep(struct iscsi_endpoint *ep)
414 struct bnx2i_endpoint *bnx2i_ep = ep->dd_data;
417 spin_lock_irqsave(&bnx2i_resc_lock, flags);
418 bnx2i_ep->state = EP_STATE_IDLE;
419 bnx2i_ep->hba->ofld_conns_active--;
421 if (bnx2i_ep->ep_iscsi_cid != (u16) -1)
422 bnx2i_free_iscsi_cid(bnx2i_ep->hba, bnx2i_ep->ep_iscsi_cid);
424 if (bnx2i_ep->conn) {
425 bnx2i_ep->conn->ep = NULL;
426 bnx2i_ep->conn = NULL;
429 bnx2i_ep->hba = NULL;
430 spin_unlock_irqrestore(&bnx2i_resc_lock, flags);
431 iscsi_destroy_endpoint(ep);
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
441 static int bnx2i_alloc_bdt(struct bnx2i_hba *hba, struct iscsi_session *session,
442 struct bnx2i_cmd *cmd)
444 struct io_bdt *io = &cmd->io_tbl;
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);
451 iscsi_session_printk(KERN_ERR, session, "Could not "
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
465 static void bnx2i_destroy_cmd_pool(struct bnx2i_hba *hba,
466 struct iscsi_session *session)
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;
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),
479 cmd->io_tbl.bd_tbl_dma);
486 * bnx2i_setup_cmd_pool - sets up iscsi command pool for the session
487 * @hba: adapter instance pointer
488 * @session: iscsi session pointer
490 static int bnx2i_setup_cmd_pool(struct bnx2i_hba *hba,
491 struct iscsi_session *session)
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;
499 task->hdr = &cmd->hdr;
500 task->hdr_max = sizeof(struct iscsi_hdr);
502 if (bnx2i_alloc_bdt(hba, session, cmd))
509 bnx2i_destroy_cmd_pool(hba, session);
515 * bnx2i_setup_mp_bdt - allocate BD table resources
516 * @hba: pointer to adapter structure
518 * Allocate memory for dummy buffer and associated BD
519 * table to be used by middle path (MP) requests
521 static int bnx2i_setup_mp_bdt(struct bnx2i_hba *hba)
524 struct iscsi_bd *mp_bdt;
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");
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;
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;
559 * bnx2i_free_mp_bdt - releases ITT back to free pool
560 * @hba: pointer to adapter instance
562 * free MP dummy buffer and associated BD table
564 static void bnx2i_free_mp_bdt(struct bnx2i_hba *hba)
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;
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;
580 * bnx2i_drop_session - notifies iscsid of connection error.
581 * @hba: adapter instance pointer
582 * @session: iscsi session pointer
584 * This notifies iscsid that there is a error, so it can initiate
587 * This relies on caller using the iscsi class iterator so the object
588 * is refcounted and does not disapper from under us.
590 void bnx2i_drop_session(struct iscsi_cls_session *cls_session)
592 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
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
600 * EP destroy queue manager
602 static int bnx2i_ep_destroy_list_add(struct bnx2i_hba *hba,
603 struct bnx2i_endpoint *ep)
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);
612 * bnx2i_ep_destroy_list_del - add an entry to EP destroy list
614 * @hba: pointer to adapter instance
615 * @ep: pointer to endpoint (transport indentifier) structure
617 * EP destroy queue manager
619 static int bnx2i_ep_destroy_list_del(struct bnx2i_hba *hba,
620 struct bnx2i_endpoint *ep)
622 write_lock_bh(&hba->ep_rdwr_lock);
623 list_del_init(&ep->link);
624 write_unlock_bh(&hba->ep_rdwr_lock);
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
634 * pending conn offload completion queue manager
636 static int bnx2i_ep_ofld_list_add(struct bnx2i_hba *hba,
637 struct bnx2i_endpoint *ep)
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);
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
650 * pending conn offload completion queue manager
652 static int bnx2i_ep_ofld_list_del(struct bnx2i_hba *hba,
653 struct bnx2i_endpoint *ep)
655 write_lock_bh(&hba->ep_rdwr_lock);
656 list_del_init(&ep->link);
657 write_unlock_bh(&hba->ep_rdwr_lock);
663 * bnx2i_find_ep_in_ofld_list - find iscsi_cid in pending list of endpoints
665 * @hba: pointer to adapter instance
666 * @iscsi_cid: iscsi context ID to find
669 struct bnx2i_endpoint *
670 bnx2i_find_ep_in_ofld_list(struct bnx2i_hba *hba, u32 iscsi_cid)
672 struct list_head *list;
673 struct list_head *tmp;
674 struct bnx2i_endpoint *ep;
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;
680 if (ep->ep_iscsi_cid == iscsi_cid)
684 read_unlock_bh(&hba->ep_rdwr_lock);
687 printk(KERN_ERR "l5 cid %d not found\n", iscsi_cid);
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
697 struct bnx2i_endpoint *
698 bnx2i_find_ep_in_destroy_list(struct bnx2i_hba *hba, u32 iscsi_cid)
700 struct list_head *list;
701 struct list_head *tmp;
702 struct bnx2i_endpoint *ep;
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;
708 if (ep->ep_iscsi_cid == iscsi_cid)
712 read_unlock_bh(&hba->ep_rdwr_lock);
715 printk(KERN_ERR "l5 cid %d not found\n", iscsi_cid);
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
725 * current active conn queue manager
727 static void bnx2i_ep_active_list_add(struct bnx2i_hba *hba,
728 struct bnx2i_endpoint *ep)
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);
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
741 * current active conn queue manager
743 static void bnx2i_ep_active_list_del(struct bnx2i_hba *hba,
744 struct bnx2i_endpoint *ep)
746 write_lock_bh(&hba->ep_rdwr_lock);
747 list_del_init(&ep->link);
748 write_unlock_bh(&hba->ep_rdwr_lock);
753 * bnx2i_setup_host_queue_size - assigns shost->can_queue param
754 * @hba: pointer to adapter instance
755 * @shost: scsi host pointer
757 * Initializes 'can_queue' parameter based on how many outstanding commands
758 * the device can handle. Each device 5708/5709/57710 has different
761 static void bnx2i_setup_host_queue_size(struct bnx2i_hba *hba,
762 struct Scsi_Host *shost)
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;
771 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5708;
776 * bnx2i_alloc_hba - allocate and init adapter instance
777 * @cnic: cnic device pointer
779 * allocate & initialize adapter structure and call other
780 * support routines to do per adapter initialization
782 struct bnx2i_hba *bnx2i_alloc_hba(struct cnic_dev *cnic)
784 struct Scsi_Host *shost;
785 struct bnx2i_hba *hba;
787 shost = iscsi_host_alloc(&bnx2i_host_template, sizeof(*hba), 0);
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;
797 hba = iscsi_host_priv(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);
810 bnx2i_identify_device(hba);
811 bnx2i_setup_host_queue_size(hba, shost);
813 if (test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type)) {
814 hba->regview = ioremap_nocache(hba->netdev->base_addr,
818 } else if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
819 hba->regview = ioremap_nocache(hba->netdev->base_addr, 4096);
824 if (bnx2i_setup_mp_bdt(hba))
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);
832 hba->mtu_supported = BNX2I_MAX_MTU_SUPPORTED;
834 /* different values for 5708/5709/57710 */
835 hba->max_active_conns = ISCSI_MAX_CONNS_PER_HBA;
837 if (bnx2i_setup_free_cid_que(hba))
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;
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;
850 hba->max_sqes = BNX2I_570X_SQ_WQES_DEFAULT;
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;
861 hba->num_ccell = hba->max_sqes / 2;
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;
876 if (iscsi_host_add(shost, &hba->pcidev->dev))
881 bnx2i_release_free_cid_que(hba);
883 bnx2i_free_mp_bdt(hba);
886 iounmap(hba->regview);
890 pci_dev_put(hba->pcidev);
891 scsi_host_put(shost);
896 * bnx2i_free_hba- releases hba structure and resources held by the adapter
897 * @hba: pointer to adapter instance
899 * free adapter structure and call various cleanup routines.
901 void bnx2i_free_hba(struct bnx2i_hba *hba)
903 struct Scsi_Host *shost = hba->shost;
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);
912 iounmap(hba->regview);
915 bnx2i_free_mp_bdt(hba);
916 bnx2i_release_free_cid_que(hba);
917 iscsi_host_free(shost);
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
925 * Login related resources, mostly BDT & payload DMA memory is freed
927 static void bnx2i_conn_free_login_resources(struct bnx2i_hba *hba,
928 struct bnx2i_conn *bnx2i_conn)
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;
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;
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;
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;
962 * bnx2i_conn_alloc_login_resources - alloc DMA resources for login/nop.
963 * @hba: pointer to adapter instance
964 * @bnx2i_conn: iscsi connection pointer
966 * Mgmt task DNA resources are allocated in this routine.
968 static int bnx2i_conn_alloc_login_resources(struct bnx2i_hba *hba,
969 struct bnx2i_conn *bnx2i_conn)
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,
977 if (bnx2i_conn->gen_pdu.req_buf == NULL)
978 goto login_req_buf_failure;
980 bnx2i_conn->gen_pdu.req_buf_size = 0;
981 bnx2i_conn->gen_pdu.req_wr_ptr = bnx2i_conn->gen_pdu.req_buf;
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,
988 if (bnx2i_conn->gen_pdu.resp_buf == NULL)
989 goto login_resp_buf_failure;
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;
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;
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,
1004 if (bnx2i_conn->gen_pdu.resp_bd_tbl == NULL)
1005 goto login_resp_bd_tbl_failure;
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;
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");
1034 * bnx2i_iscsi_prep_generic_pdu_bd - prepares BD table.
1035 * @bnx2i_conn: iscsi connection pointer
1037 * Allocates buffers and BD tables before shipping requests to cnic
1038 * for PDUs prepared by 'iscsid' daemon
1040 static void bnx2i_iscsi_prep_generic_pdu_bd(struct bnx2i_conn *bnx2i_conn)
1042 struct iscsi_bd *bd_tbl;
1044 bd_tbl = (struct iscsi_bd *) bnx2i_conn->gen_pdu.req_bd_tbl;
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;
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;
1066 * bnx2i_iscsi_send_generic_request - called to send mgmt tasks.
1067 * @task: transport layer task pointer
1069 * called to transmit PDUs prepared by the 'iscsid' daemon. iSCSI login,
1070 * Nop-out and Logout requests flow through this path.
1072 static int bnx2i_iscsi_send_generic_request(struct iscsi_task *task)
1074 struct bnx2i_cmd *cmd = task->dd_data;
1075 struct bnx2i_conn *bnx2i_conn = cmd->conn;
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);
1085 case ISCSI_OP_NOOP_OUT:
1086 data_len = bnx2i_conn->gen_pdu.req_buf_size;
1087 buf = bnx2i_conn->gen_pdu.req_buf;
1089 rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
1092 rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
1095 case ISCSI_OP_LOGOUT:
1096 rc = bnx2i_send_iscsi_logout(bnx2i_conn, task);
1098 case ISCSI_OP_SCSI_TMFUNC:
1099 rc = bnx2i_send_iscsi_tmf(bnx2i_conn, task);
1102 rc = bnx2i_send_iscsi_text(bnx2i_conn, task);
1105 iscsi_conn_printk(KERN_ALERT, bnx2i_conn->cls_conn->dd_data,
1106 "send_gen: unsupported op 0x%x\n",
1113 /**********************************************************************
1115 **********************************************************************/
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
1122 static void bnx2i_cpy_scsi_cdb(struct scsi_cmnd *sc, struct bnx2i_cmd *cmd)
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]);
1134 lpcnt = cmd->scsi_cmd->cmd_len / sizeof(dword);
1135 srcp = (u8 *) sc->cmnd;
1136 dstp = (u32 *) cmd->req.cdb;
1138 memcpy(&dword, (const void *) srcp, 4);
1139 *dstp = cpu_to_be32(dword);
1143 if (sc->cmd_len & 0x3) {
1144 dword = (u32) srcp[0] | ((u32) srcp[1] << 8);
1145 *dstp = cpu_to_be32(dword);
1149 static void bnx2i_cleanup_task(struct iscsi_task *task)
1151 struct iscsi_conn *conn = task->conn;
1152 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1153 struct bnx2i_hba *hba = bnx2i_conn->hba;
1156 * mgmt task or cmd was never sent to us to transmit.
1158 if (!task->sc || task->state == ISCSI_TASK_PENDING)
1161 * need to clean-up task context to claim dma buffers
1163 if (task->state == ISCSI_TASK_ABRT_TMF) {
1164 bnx2i_send_cmd_cleanup_req(hba, task->dd_data);
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);
1171 bnx2i_iscsi_unmap_sg_list(task->dd_data);
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
1180 bnx2i_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
1182 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1183 struct bnx2i_cmd *cmd = task->dd_data;
1185 memset(bnx2i_conn->gen_pdu.req_buf, 0, ISCSI_DEF_MAX_RECV_SEG_LEN);
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,
1192 bnx2i_conn->gen_pdu.req_wr_ptr =
1193 bnx2i_conn->gen_pdu.req_buf + task->data_count;
1195 cmd->conn = conn->dd_data;
1196 cmd->scsi_cmd = NULL;
1197 return bnx2i_iscsi_send_generic_request(task);
1201 * bnx2i_task_xmit - transmit iscsi command to chip for further processing
1202 * @task: transport layer command structure pointer
1204 * maps SG buffers and send request to chip/firmware in the form of SQ WQE
1206 static int bnx2i_task_xmit(struct iscsi_task *task)
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_scsi_req *hdr = (struct iscsi_scsi_req *)task->hdr;
1217 if (bnx2i_conn->ep->num_active_cmds + 1 > hba->max_sqes)
1221 * If there is no scsi_cmnd this must be a mgmt task
1224 return bnx2i_mtask_xmit(conn, task);
1226 bnx2i_setup_cmd_wqe_template(cmd);
1227 cmd->req.op_code = ISCSI_OP_SCSI_CMD;
1228 cmd->conn = bnx2i_conn;
1230 cmd->req.total_data_transfer_length = scsi_bufflen(sc);
1231 cmd->req.cmd_sn = be32_to_cpu(hdr->cmdsn);
1233 bnx2i_iscsi_map_sg_list(cmd);
1234 bnx2i_cpy_scsi_cdb(sc, cmd);
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);
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);
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;
1256 bnx2i_send_iscsi_scsicmd(bnx2i_conn, cmd);
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
1266 * Creates a new iSCSI session instance on given device.
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)
1273 struct Scsi_Host *shost;
1274 struct iscsi_cls_session *cls_session;
1275 struct bnx2i_hba *hba;
1276 struct bnx2i_endpoint *bnx2i_ep;
1279 printk(KERN_ERR "bnx2i: missing ep.\n");
1283 bnx2i_ep = ep->dd_data;
1284 shost = bnx2i_ep->hba->shost;
1285 hba = iscsi_host_priv(shost);
1286 if (bnx2i_adapter_ready(hba))
1290 * user can override hw limit as long as it is within
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;
1298 cls_session = iscsi_session_setup(&bnx2i_iscsi_transport, shost,
1299 cmds_max, 0, sizeof(struct bnx2i_cmd),
1300 initial_cmdsn, ISCSI_MAX_TARGET);
1304 if (bnx2i_setup_cmd_pool(hba, cls_session->dd_data))
1305 goto session_teardown;
1309 iscsi_session_teardown(cls_session);
1315 * bnx2i_session_destroy - destroys iscsi session
1316 * @cls_session: pointer to iscsi cls session
1318 * Destroys previously created iSCSI session instance and releases
1319 * all resources held by it
1321 static void bnx2i_session_destroy(struct iscsi_cls_session *cls_session)
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);
1327 bnx2i_destroy_cmd_pool(hba, session);
1328 iscsi_session_teardown(cls_session);
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)
1337 * Creates a new iSCSI connection instance for a given session
1339 static struct iscsi_cls_conn *
1340 bnx2i_conn_create(struct iscsi_cls_session *cls_session, uint32_t cid)
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;
1348 cls_conn = iscsi_conn_setup(cls_session, sizeof(*bnx2i_conn),
1352 conn = cls_conn->dd_data;
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);
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");
1370 iscsi_conn_teardown(cls_conn);
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?
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
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)
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;
1398 ep = iscsi_lookup_endpoint(transport_fd);
1402 * Forcefully terminate all in progress connection recovery at the
1403 * earliest, either in bind(), send_pdu(LOGIN), or conn_start()
1405 if (bnx2i_adapter_ready(hba))
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 */
1414 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1417 if (bnx2i_ep->hba != hba) {
1418 /* Error - TCP connection does not belong to this device
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",
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;
1433 ret_code = bnx2i_bind_conn_to_iscsi_cid(hba, bnx2i_conn,
1434 bnx2i_ep->ep_iscsi_cid);
1436 /* 5706/5708/5709 FW takes RQ as full when initiated, but for 57710
1437 * driver needs to explicitly replenish RQ index during setup.
1439 if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
1440 bnx2i_put_rq_buf(bnx2i_conn, 0);
1442 bnx2i_arm_cq_event_coalescing(bnx2i_conn->ep, CNIC_ARM_CQE);
1448 * bnx2i_conn_destroy - destroy iscsi connection instance & release resources
1449 * @cls_conn: pointer to iscsi cls conn
1451 * Destroy an iSCSI connection instance and release memory resources held by
1454 static void bnx2i_conn_destroy(struct iscsi_cls_conn *cls_conn)
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;
1461 shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
1462 hba = iscsi_host_priv(shost);
1464 bnx2i_conn_free_login_resources(hba, bnx2i_conn);
1465 iscsi_conn_teardown(cls_conn);
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
1475 * returns iSCSI ep parameters
1477 static int bnx2i_ep_get_param(struct iscsi_endpoint *ep,
1478 enum iscsi_param param, char *buf)
1480 struct bnx2i_endpoint *bnx2i_ep = ep->dd_data;
1481 struct bnx2i_hba *hba = bnx2i_ep->hba;
1482 int len = -ENOTCONN;
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);
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);
1508 * bnx2i_host_get_param - returns host (adapter) related parameters
1509 * @shost: scsi host pointer
1510 * @param: parameter type identifier
1511 * @buf: buffer pointer
1513 static int bnx2i_host_get_param(struct Scsi_Host *shost,
1514 enum iscsi_host_param param, char *buf)
1516 struct bnx2i_hba *hba = iscsi_host_priv(shost);
1520 case ISCSI_HOST_PARAM_HWADDRESS:
1521 len = sysfs_format_mac(buf, hba->cnic->mac_addr, 6);
1523 case ISCSI_HOST_PARAM_NETDEV_NAME:
1524 len = sprintf(buf, "%s\n", hba->netdev->name);
1526 case ISCSI_HOST_PARAM_IPADDRESS: {
1527 struct list_head *active_list = &hba->ep_active_list;
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;
1534 bnx2i_ep = list_first_entry(active_list,
1535 struct bnx2i_endpoint,
1537 csk = bnx2i_ep->cm_sk;
1538 if (test_bit(SK_F_IPV6, &csk->flags))
1539 len = sprintf(buf, "%pI6\n", csk->src_ip);
1541 len = sprintf(buf, "%pI4\n", csk->src_ip);
1543 read_unlock_bh(&hba->ep_rdwr_lock);
1547 return iscsi_host_get_param(shost, param, buf);
1553 * bnx2i_conn_start - completes iscsi connection migration to FFP
1554 * @cls_conn: pointer to iscsi cls conn
1556 * last call in FFP migration to handover iscsi conn to the driver
1558 static int bnx2i_conn_start(struct iscsi_cls_conn *cls_conn)
1560 struct iscsi_conn *conn = cls_conn->dd_data;
1561 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1563 bnx2i_conn->ep->state = EP_STATE_ULP_UPDATE_START;
1564 bnx2i_update_iscsi_conn(conn);
1567 * this should normally not sleep for a long time so it should
1568 * not disrupt the caller.
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);
1578 if (signal_pending(current))
1579 flush_signals(current);
1580 del_timer_sync(&bnx2i_conn->ep->ofld_timer);
1582 iscsi_conn_start(cls_conn);
1588 * bnx2i_conn_get_stats - returns iSCSI stats
1589 * @cls_conn: pointer to iscsi cls conn
1590 * @stats: pointer to iscsi statistic struct
1592 static void bnx2i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1593 struct iscsi_stats *stats)
1595 struct iscsi_conn *conn = cls_conn->dd_data;
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;
1616 * bnx2i_check_route - checks if target IP route belongs to one of NX2 devices
1617 * @dst_addr: target IP address
1619 * check if route resolves to BNX2 device
1621 static struct bnx2i_hba *bnx2i_check_route(struct sockaddr *dst_addr)
1623 struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
1624 struct bnx2i_hba *hba;
1625 struct cnic_dev *cnic = NULL;
1627 hba = get_adapter_list_head();
1628 if (hba && hba->cnic)
1629 cnic = hba->cnic->cm_select_dev(desti, CNIC_ULP_ISCSI);
1631 printk(KERN_ALERT "bnx2i: no route,"
1632 "can't connect using cnic\n");
1635 hba = bnx2i_find_hba_for_cnic(cnic);
1639 if (bnx2i_adapter_ready(hba)) {
1640 printk(KERN_ALERT "bnx2i: check route, hba not found\n");
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);
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
1661 * destroys cm_sock structure and on chip iscsi context
1663 static int bnx2i_tear_down_conn(struct bnx2i_hba *hba,
1664 struct bnx2i_endpoint *ep)
1666 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) && ep->cm_sk)
1667 hba->cnic->cm_destroy(ep->cm_sk);
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;
1675 /* Must suspend all rx queue activity for this ep */
1676 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1678 /* CONN_DISCONNECT timeout may or may not be an issue depending
1679 * on what transcribed in TCP layer, different targets behave
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",
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);
1695 bnx2i_ep_destroy_list_add(hba, ep);
1697 /* destroy iSCSI context, wait for it to complete */
1698 if (bnx2i_send_conn_destroy(hba, ep))
1699 ep->state = EP_STATE_CLEANUP_CMPL;
1701 wait_event_interruptible(ep->ofld_wait,
1702 (ep->state != EP_STATE_CLEANUP_START));
1704 if (signal_pending(current))
1705 flush_signals(current);
1706 del_timer_sync(&ep->ofld_timer);
1708 bnx2i_ep_destroy_list_del(hba, ep);
1710 if (ep->state != EP_STATE_CLEANUP_CMPL)
1711 /* should never happen */
1712 printk(KERN_ALERT "bnx2i - conn destroy failed\n");
1719 * bnx2i_ep_connect - establish TCP connection to target portal
1721 * @dst_addr: target IP address
1722 * @non_blocking: blocking or non-blocking call
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
1730 static struct iscsi_endpoint *bnx2i_ep_connect(struct Scsi_Host *shost,
1731 struct sockaddr *dst_addr,
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;
1745 /* driver is given scsi host to work with */
1746 hba = iscsi_host_priv(shost);
1749 * check if the given destination can be reached through
1750 * a iscsi capable NetXtreme2 device
1752 hba = bnx2i_check_route(dst_addr);
1758 mutex_lock(&hba->net_dev_lock);
1760 if (bnx2i_adapter_ready(hba) || !hba->cid_que.cid_free_cnt) {
1765 ep = bnx2i_alloc_ep(hba);
1770 bnx2i_ep = ep->dd_data;
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);
1781 bnx2i_ep->hba_age = hba->age;
1783 rc = bnx2i_alloc_qp_resc(hba, bnx2i_ep);
1785 printk(KERN_ALERT "bnx2i (%s): ep_conn - alloc QP resc error"
1786 "\n", hba->netdev->name);
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);
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);
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);
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);
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);
1818 if (signal_pending(current))
1819 flush_signals(current);
1820 del_timer_sync(&bnx2i_ep->ofld_timer);
1822 bnx2i_ep_ofld_list_del(hba, bnx2i_ep);
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);
1834 rc = cnic->cm_create(cnic, CNIC_ULP_ISCSI, bnx2i_ep->ep_cid,
1835 iscsi_cid, &bnx2i_ep->cm_sk, bnx2i_ep);
1838 /* Need to terminate and cleanup the connection */
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);
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;
1857 bnx2i_ep->timestamp = jiffies;
1858 bnx2i_ep->state = EP_STATE_CONNECT_START;
1859 if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
1863 rc = cnic->cm_connect(bnx2i_ep->cm_sk, &saddr);
1867 bnx2i_ep_active_list_add(hba, bnx2i_ep);
1869 if (bnx2i_map_ep_dbell_regs(bnx2i_ep))
1872 mutex_unlock(&hba->net_dev_lock);
1876 bnx2i_ep_active_list_del(hba, bnx2i_ep);
1878 if (bnx2i_tear_down_conn(hba, bnx2i_ep)) {
1879 mutex_unlock(&hba->net_dev_lock);
1883 bnx2i_free_qp_resc(hba, bnx2i_ep);
1887 mutex_unlock(&hba->net_dev_lock);
1894 * bnx2i_ep_poll - polls for TCP connection establishement
1895 * @ep: TCP connection (endpoint) handle
1896 * @timeout_ms: timeout value in milli secs
1898 * polls for TCP connect request to complete
1900 static int bnx2i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1902 struct bnx2i_endpoint *bnx2i_ep;
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))
1910 if (bnx2i_ep->state == EP_STATE_CONNECT_COMPL)
1913 rc = wait_event_interruptible_timeout(bnx2i_ep->ofld_wait,
1914 ((bnx2i_ep->state ==
1915 EP_STATE_OFLD_FAILED) ||
1917 EP_STATE_CONNECT_FAILED) ||
1919 EP_STATE_CONNECT_COMPL)),
1920 msecs_to_jiffies(timeout_ms));
1921 if (bnx2i_ep->state == EP_STATE_OFLD_FAILED)
1927 return 0; /* timeout */
1934 * bnx2i_ep_tcp_conn_active - check EP state transition
1935 * @ep: endpoint pointer
1937 * check if underlying TCP connection is active
1939 static int bnx2i_ep_tcp_conn_active(struct bnx2i_endpoint *bnx2i_ep)
1942 int cnic_dev_10g = 0;
1944 if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
1947 switch (bnx2i_ep->state) {
1948 case EP_STATE_CLEANUP_FAILED:
1949 case EP_STATE_OFLD_FAILED:
1950 case EP_STATE_DISCONN_TIMEDOUT:
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:
1964 case EP_STATE_TCP_RST_RCVD:
1979 * bnx2i_hw_ep_disconnect - executes TCP connection teardown process in the hw
1980 * @ep: TCP connection (bnx2i endpoint) handle
1982 * executes TCP connection teardown process
1984 int bnx2i_hw_ep_disconnect(struct bnx2i_endpoint *bnx2i_ep)
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;
2001 if (bnx2i_ep->state == EP_STATE_IDLE ||
2002 bnx2i_ep->state == EP_STATE_DISCONN_TIMEDOUT)
2005 if (!bnx2i_ep_tcp_conn_active(bnx2i_ep))
2008 if (bnx2i_ep->conn) {
2009 conn = bnx2i_ep->conn->cls_conn->dd_data;
2010 session = conn->session;
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);
2019 if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic))
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 "
2031 bnx2i_ep->hba->netdev->name);
2032 } else if (bnx2i_ep->state ==
2033 EP_STATE_LOGOUT_RESP_RCVD)
2039 spin_unlock_bh(&session->lock);
2042 bnx2i_ep->state = EP_STATE_DISCONN_START;
2045 close_ret = cnic->cm_close(bnx2i_ep->cm_sk);
2047 close_ret = cnic->cm_abort(bnx2i_ep->cm_sk);
2050 printk(KERN_ALERT "bnx2i (%s): close/abort(%d) returned %d\n",
2051 bnx2i_ep->hba->netdev->name, close, close_ret);
2053 /* wait for option-2 conn teardown */
2054 wait_event_interruptible(bnx2i_ep->ofld_wait,
2055 bnx2i_ep->state != EP_STATE_DISCONN_START);
2057 if (signal_pending(current))
2058 flush_signals(current);
2059 del_timer_sync(&bnx2i_ep->ofld_timer);
2062 bnx2i_ep_active_list_del(hba, bnx2i_ep);
2063 if (bnx2i_tear_down_conn(hba, bnx2i_ep))
2066 bnx2i_ep->state = EP_STATE_IDLE;
2072 * bnx2i_ep_disconnect - executes TCP connection teardown process
2073 * @ep: TCP connection (iscsi endpoint) handle
2075 * executes TCP connection teardown process
2077 static void bnx2i_ep_disconnect(struct iscsi_endpoint *ep)
2079 struct bnx2i_endpoint *bnx2i_ep;
2080 struct bnx2i_conn *bnx2i_conn = NULL;
2081 struct iscsi_conn *conn = NULL;
2082 struct bnx2i_hba *hba;
2084 bnx2i_ep = ep->dd_data;
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
2090 while ((bnx2i_ep->state == EP_STATE_CONNECT_START) &&
2091 !time_after(jiffies, bnx2i_ep->timestamp + (12 * HZ)))
2094 if (bnx2i_ep->conn) {
2095 bnx2i_conn = bnx2i_ep->conn;
2096 conn = bnx2i_conn->cls_conn->dd_data;
2097 iscsi_suspend_queue(conn);
2099 hba = bnx2i_ep->hba;
2101 mutex_lock(&hba->net_dev_lock);
2103 if (bnx2i_ep->state == EP_STATE_DISCONN_TIMEDOUT)
2106 if (bnx2i_ep->state == EP_STATE_IDLE)
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);
2115 /* Do all chip cleanup here */
2116 if (bnx2i_hw_ep_disconnect(bnx2i_ep)) {
2117 mutex_unlock(&hba->net_dev_lock);
2121 bnx2i_free_qp_resc(hba, bnx2i_ep);
2124 bnx2i_conn->ep = NULL;
2128 mutex_unlock(&hba->net_dev_lock);
2130 wake_up_interruptible(&hba->eh_wait);
2135 * bnx2i_nl_set_path - ISCSI_UEVENT_PATH_UPDATE user message handler
2136 * @buf: pointer to buffer containing iscsi path message
2139 static int bnx2i_nl_set_path(struct Scsi_Host *shost, struct iscsi_path *params)
2141 struct bnx2i_hba *hba = iscsi_host_priv(shost);
2142 char *buf = (char *) params;
2143 u16 len = sizeof(*params);
2145 /* handled by cnic driver */
2146 hba->cnic->iscsi_nl_msg_recv(hba->cnic, ISCSI_UEVENT_PATH_UPDATE, buf,
2154 * 'Scsi_Host_Template' structure and 'iscsi_tranport' structure template
2155 * used while registering with the scsi host and iSCSI transport module.
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,
2170 .use_clustering = ENABLE_CLUSTERING,
2171 .sg_tablesize = ISCSI_MAX_BDS_PER_CMD,
2172 .shost_attrs = bnx2i_dev_attributes,
2175 struct iscsi_transport bnx2i_iscsi_transport = {
2176 .owner = THIS_MODULE,
2178 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST |
2179 CAP_MULTI_R2T | CAP_DATADGST |
2180 CAP_DATA_PATH_OFFLOAD |
2182 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2183 ISCSI_MAX_XMIT_DLENGTH |
2186 ISCSI_INITIAL_R2T_EN |
2191 ISCSI_PDU_INORDER_EN |
2192 ISCSI_DATASEQ_INORDER_EN |
2195 ISCSI_CONN_ADDRESS |
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,