blob: 7b4d1d041e2eb72b4187631f62acb33faa3729de [file] [log] [blame]
Michael Chancf4e6362009-06-08 18:14:44 -07001/*
2 * bnx2i_iscsi.c: Broadcom NetXtreme II iSCSI driver.
3 *
Eddie Wai11cec1e2010-11-23 15:29:31 -08004 * Copyright (c) 2006 - 2010 Broadcom Corporation
Michael Chancf4e6362009-06-08 18:14:44 -07005 * 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)
Eddie Wai11cec1e2010-11-23 15:29:31 -080013 * Maintained by: Eddie Wai (eddie.wai@broadcom.com)
Michael Chancf4e6362009-06-08 18:14:44 -070014 */
15
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Michael Chancf4e6362009-06-08 18:14:44 -070017#include <scsi/scsi_tcq.h>
18#include <scsi/libiscsi.h>
19#include "bnx2i.h"
20
21struct scsi_transport_template *bnx2i_scsi_xport_template;
22struct iscsi_transport bnx2i_iscsi_transport;
23static struct scsi_host_template bnx2i_host_template;
24
25/*
26 * Global endpoint resource info
27 */
28static DEFINE_SPINLOCK(bnx2i_resc_lock); /* protects global resources */
29
30
31static 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 */
53static 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 */
81static 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 */
140static 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 */
183static 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 */
204void 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
214static 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 */
235static 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 */
255struct 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 */
274static 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 */
296static 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 */
321static 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 */
360static 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 */
378static struct iscsi_endpoint *bnx2i_alloc_ep(struct bnx2i_hba *hba)
379{
380 struct iscsi_endpoint *ep;
381 struct bnx2i_endpoint *bnx2i_ep;
382
383 ep = iscsi_create_endpoint(sizeof(*bnx2i_ep));
384 if (!ep) {
385 printk(KERN_ERR "bnx2i: Could not allocate ep\n");
386 return NULL;
387 }
388
389 bnx2i_ep = ep->dd_data;
Eddie Wai46012e82010-07-01 15:34:51 -0700390 bnx2i_ep->cls_ep = ep;
Michael Chancf4e6362009-06-08 18:14:44 -0700391 INIT_LIST_HEAD(&bnx2i_ep->link);
392 bnx2i_ep->state = EP_STATE_IDLE;
Anil Veerabhadrappac19dcd02009-07-29 21:50:11 -0700393 bnx2i_ep->ep_iscsi_cid = (u16) -1;
Michael Chancf4e6362009-06-08 18:14:44 -0700394 bnx2i_ep->hba = hba;
395 bnx2i_ep->hba_age = hba->age;
396 hba->ofld_conns_active++;
397 init_waitqueue_head(&bnx2i_ep->ofld_wait);
398 return ep;
399}
400
401
402/**
403 * bnx2i_free_ep - free endpoint
404 * @ep: pointer to iscsi endpoint structure
405 */
406static void bnx2i_free_ep(struct iscsi_endpoint *ep)
407{
408 struct bnx2i_endpoint *bnx2i_ep = ep->dd_data;
409 unsigned long flags;
410
411 spin_lock_irqsave(&bnx2i_resc_lock, flags);
412 bnx2i_ep->state = EP_STATE_IDLE;
413 bnx2i_ep->hba->ofld_conns_active--;
414
Eddie Waia91031a2010-11-23 15:29:30 -0800415 if (bnx2i_ep->ep_iscsi_cid != (u16) -1)
416 bnx2i_free_iscsi_cid(bnx2i_ep->hba, bnx2i_ep->ep_iscsi_cid);
417
Michael Chancf4e6362009-06-08 18:14:44 -0700418 if (bnx2i_ep->conn) {
419 bnx2i_ep->conn->ep = NULL;
420 bnx2i_ep->conn = NULL;
421 }
422
423 bnx2i_ep->hba = NULL;
424 spin_unlock_irqrestore(&bnx2i_resc_lock, flags);
425 iscsi_destroy_endpoint(ep);
426}
427
428
429/**
430 * bnx2i_alloc_bdt - allocates buffer descriptor (BD) table for the command
431 * @hba: adapter instance pointer
432 * @session: iscsi session pointer
433 * @cmd: iscsi command structure
434 */
435static int bnx2i_alloc_bdt(struct bnx2i_hba *hba, struct iscsi_session *session,
436 struct bnx2i_cmd *cmd)
437{
438 struct io_bdt *io = &cmd->io_tbl;
439 struct iscsi_bd *bd;
440
441 io->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
442 ISCSI_MAX_BDS_PER_CMD * sizeof(*bd),
443 &io->bd_tbl_dma, GFP_KERNEL);
444 if (!io->bd_tbl) {
445 iscsi_session_printk(KERN_ERR, session, "Could not "
446 "allocate bdt.\n");
447 return -ENOMEM;
448 }
449 io->bd_valid = 0;
450 return 0;
451}
452
453/**
454 * bnx2i_destroy_cmd_pool - destroys iscsi command pool and release BD table
455 * @hba: adapter instance pointer
456 * @session: iscsi session pointer
457 * @cmd: iscsi command structure
458 */
459static void bnx2i_destroy_cmd_pool(struct bnx2i_hba *hba,
460 struct iscsi_session *session)
461{
462 int i;
463
464 for (i = 0; i < session->cmds_max; i++) {
465 struct iscsi_task *task = session->cmds[i];
466 struct bnx2i_cmd *cmd = task->dd_data;
467
468 if (cmd->io_tbl.bd_tbl)
469 dma_free_coherent(&hba->pcidev->dev,
470 ISCSI_MAX_BDS_PER_CMD *
471 sizeof(struct iscsi_bd),
472 cmd->io_tbl.bd_tbl,
473 cmd->io_tbl.bd_tbl_dma);
474 }
475
476}
477
478
479/**
480 * bnx2i_setup_cmd_pool - sets up iscsi command pool for the session
481 * @hba: adapter instance pointer
482 * @session: iscsi session pointer
483 */
484static int bnx2i_setup_cmd_pool(struct bnx2i_hba *hba,
485 struct iscsi_session *session)
486{
487 int i;
488
489 for (i = 0; i < session->cmds_max; i++) {
490 struct iscsi_task *task = session->cmds[i];
491 struct bnx2i_cmd *cmd = task->dd_data;
492
Michael Chancf4e6362009-06-08 18:14:44 -0700493 task->hdr = &cmd->hdr;
494 task->hdr_max = sizeof(struct iscsi_hdr);
495
496 if (bnx2i_alloc_bdt(hba, session, cmd))
497 goto free_bdts;
498 }
499
500 return 0;
501
502free_bdts:
503 bnx2i_destroy_cmd_pool(hba, session);
504 return -ENOMEM;
505}
506
507
508/**
509 * bnx2i_setup_mp_bdt - allocate BD table resources
510 * @hba: pointer to adapter structure
511 *
512 * Allocate memory for dummy buffer and associated BD
513 * table to be used by middle path (MP) requests
514 */
515static int bnx2i_setup_mp_bdt(struct bnx2i_hba *hba)
516{
517 int rc = 0;
518 struct iscsi_bd *mp_bdt;
519 u64 addr;
520
521 hba->mp_bd_tbl = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
522 &hba->mp_bd_dma, GFP_KERNEL);
523 if (!hba->mp_bd_tbl) {
524 printk(KERN_ERR "unable to allocate Middle Path BDT\n");
525 rc = -1;
526 goto out;
527 }
528
529 hba->dummy_buffer = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
530 &hba->dummy_buf_dma, GFP_KERNEL);
531 if (!hba->dummy_buffer) {
532 printk(KERN_ERR "unable to alloc Middle Path Dummy Buffer\n");
533 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
534 hba->mp_bd_tbl, hba->mp_bd_dma);
535 hba->mp_bd_tbl = NULL;
536 rc = -1;
537 goto out;
538 }
539
540 mp_bdt = (struct iscsi_bd *) hba->mp_bd_tbl;
541 addr = (unsigned long) hba->dummy_buf_dma;
542 mp_bdt->buffer_addr_lo = addr & 0xffffffff;
543 mp_bdt->buffer_addr_hi = addr >> 32;
544 mp_bdt->buffer_length = PAGE_SIZE;
545 mp_bdt->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
546 ISCSI_BD_FIRST_IN_BD_CHAIN;
547out:
548 return rc;
549}
550
551
552/**
553 * bnx2i_free_mp_bdt - releases ITT back to free pool
554 * @hba: pointer to adapter instance
555 *
556 * free MP dummy buffer and associated BD table
557 */
558static void bnx2i_free_mp_bdt(struct bnx2i_hba *hba)
559{
560 if (hba->mp_bd_tbl) {
561 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
562 hba->mp_bd_tbl, hba->mp_bd_dma);
563 hba->mp_bd_tbl = NULL;
564 }
565 if (hba->dummy_buffer) {
566 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
567 hba->dummy_buffer, hba->dummy_buf_dma);
568 hba->dummy_buffer = NULL;
569 }
570 return;
571}
572
573/**
574 * bnx2i_drop_session - notifies iscsid of connection error.
575 * @hba: adapter instance pointer
576 * @session: iscsi session pointer
577 *
578 * This notifies iscsid that there is a error, so it can initiate
579 * recovery.
580 *
581 * This relies on caller using the iscsi class iterator so the object
582 * is refcounted and does not disapper from under us.
583 */
584void bnx2i_drop_session(struct iscsi_cls_session *cls_session)
585{
586 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
587}
588
589/**
590 * bnx2i_ep_destroy_list_add - add an entry to EP destroy list
591 * @hba: pointer to adapter instance
592 * @ep: pointer to endpoint (transport indentifier) structure
593 *
594 * EP destroy queue manager
595 */
596static int bnx2i_ep_destroy_list_add(struct bnx2i_hba *hba,
597 struct bnx2i_endpoint *ep)
598{
599 write_lock_bh(&hba->ep_rdwr_lock);
600 list_add_tail(&ep->link, &hba->ep_destroy_list);
601 write_unlock_bh(&hba->ep_rdwr_lock);
602 return 0;
603}
604
605/**
606 * bnx2i_ep_destroy_list_del - add an entry to EP destroy list
607 *
608 * @hba: pointer to adapter instance
609 * @ep: pointer to endpoint (transport indentifier) structure
610 *
611 * EP destroy queue manager
612 */
613static int bnx2i_ep_destroy_list_del(struct bnx2i_hba *hba,
614 struct bnx2i_endpoint *ep)
615{
616 write_lock_bh(&hba->ep_rdwr_lock);
617 list_del_init(&ep->link);
618 write_unlock_bh(&hba->ep_rdwr_lock);
619
620 return 0;
621}
622
623/**
624 * bnx2i_ep_ofld_list_add - add an entry to ep offload pending list
625 * @hba: pointer to adapter instance
626 * @ep: pointer to endpoint (transport indentifier) structure
627 *
628 * pending conn offload completion queue manager
629 */
630static int bnx2i_ep_ofld_list_add(struct bnx2i_hba *hba,
631 struct bnx2i_endpoint *ep)
632{
633 write_lock_bh(&hba->ep_rdwr_lock);
634 list_add_tail(&ep->link, &hba->ep_ofld_list);
635 write_unlock_bh(&hba->ep_rdwr_lock);
636 return 0;
637}
638
639/**
640 * bnx2i_ep_ofld_list_del - add an entry to ep offload pending list
641 * @hba: pointer to adapter instance
642 * @ep: pointer to endpoint (transport indentifier) structure
643 *
644 * pending conn offload completion queue manager
645 */
646static int bnx2i_ep_ofld_list_del(struct bnx2i_hba *hba,
647 struct bnx2i_endpoint *ep)
648{
649 write_lock_bh(&hba->ep_rdwr_lock);
650 list_del_init(&ep->link);
651 write_unlock_bh(&hba->ep_rdwr_lock);
652 return 0;
653}
654
655
656/**
657 * bnx2i_find_ep_in_ofld_list - find iscsi_cid in pending list of endpoints
658 *
659 * @hba: pointer to adapter instance
660 * @iscsi_cid: iscsi context ID to find
661 *
662 */
663struct bnx2i_endpoint *
664bnx2i_find_ep_in_ofld_list(struct bnx2i_hba *hba, u32 iscsi_cid)
665{
666 struct list_head *list;
667 struct list_head *tmp;
668 struct bnx2i_endpoint *ep;
669
670 read_lock_bh(&hba->ep_rdwr_lock);
671 list_for_each_safe(list, tmp, &hba->ep_ofld_list) {
672 ep = (struct bnx2i_endpoint *)list;
673
674 if (ep->ep_iscsi_cid == iscsi_cid)
675 break;
676 ep = NULL;
677 }
678 read_unlock_bh(&hba->ep_rdwr_lock);
679
680 if (!ep)
681 printk(KERN_ERR "l5 cid %d not found\n", iscsi_cid);
682 return ep;
683}
684
Michael Chancf4e6362009-06-08 18:14:44 -0700685/**
686 * bnx2i_find_ep_in_destroy_list - find iscsi_cid in destroy list
687 * @hba: pointer to adapter instance
688 * @iscsi_cid: iscsi context ID to find
689 *
690 */
691struct bnx2i_endpoint *
692bnx2i_find_ep_in_destroy_list(struct bnx2i_hba *hba, u32 iscsi_cid)
693{
694 struct list_head *list;
695 struct list_head *tmp;
696 struct bnx2i_endpoint *ep;
697
698 read_lock_bh(&hba->ep_rdwr_lock);
699 list_for_each_safe(list, tmp, &hba->ep_destroy_list) {
700 ep = (struct bnx2i_endpoint *)list;
701
702 if (ep->ep_iscsi_cid == iscsi_cid)
703 break;
704 ep = NULL;
705 }
706 read_unlock_bh(&hba->ep_rdwr_lock);
707
708 if (!ep)
709 printk(KERN_ERR "l5 cid %d not found\n", iscsi_cid);
710
711 return ep;
712}
713
Eddie Wai46012e82010-07-01 15:34:51 -0700714/**
715 * bnx2i_ep_active_list_add - add an entry to ep active list
716 * @hba: pointer to adapter instance
717 * @ep: pointer to endpoint (transport indentifier) structure
718 *
719 * current active conn queue manager
720 */
721static void bnx2i_ep_active_list_add(struct bnx2i_hba *hba,
722 struct bnx2i_endpoint *ep)
723{
724 write_lock_bh(&hba->ep_rdwr_lock);
725 list_add_tail(&ep->link, &hba->ep_active_list);
726 write_unlock_bh(&hba->ep_rdwr_lock);
727}
728
729
730/**
731 * bnx2i_ep_active_list_del - deletes an entry to ep active list
732 * @hba: pointer to adapter instance
733 * @ep: pointer to endpoint (transport indentifier) structure
734 *
735 * current active conn queue manager
736 */
737static void bnx2i_ep_active_list_del(struct bnx2i_hba *hba,
738 struct bnx2i_endpoint *ep)
739{
740 write_lock_bh(&hba->ep_rdwr_lock);
741 list_del_init(&ep->link);
742 write_unlock_bh(&hba->ep_rdwr_lock);
743}
744
745
Michael Chancf4e6362009-06-08 18:14:44 -0700746/**
747 * bnx2i_setup_host_queue_size - assigns shost->can_queue param
748 * @hba: pointer to adapter instance
749 * @shost: scsi host pointer
750 *
751 * Initializes 'can_queue' parameter based on how many outstanding commands
752 * the device can handle. Each device 5708/5709/57710 has different
753 * capabilities
754 */
755static void bnx2i_setup_host_queue_size(struct bnx2i_hba *hba,
756 struct Scsi_Host *shost)
757{
758 if (test_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type))
759 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5708;
760 else if (test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type))
761 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5709;
762 else if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type))
763 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_57710;
764 else
765 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5708;
766}
767
768
769/**
770 * bnx2i_alloc_hba - allocate and init adapter instance
771 * @cnic: cnic device pointer
772 *
773 * allocate & initialize adapter structure and call other
774 * support routines to do per adapter initialization
775 */
776struct bnx2i_hba *bnx2i_alloc_hba(struct cnic_dev *cnic)
777{
778 struct Scsi_Host *shost;
779 struct bnx2i_hba *hba;
780
781 shost = iscsi_host_alloc(&bnx2i_host_template, sizeof(*hba), 0);
782 if (!shost)
783 return NULL;
784 shost->dma_boundary = cnic->pcidev->dma_mask;
785 shost->transportt = bnx2i_scsi_xport_template;
786 shost->max_id = ISCSI_MAX_CONNS_PER_HBA;
787 shost->max_channel = 0;
788 shost->max_lun = 512;
789 shost->max_cmd_len = 16;
790
791 hba = iscsi_host_priv(shost);
792 hba->shost = shost;
793 hba->netdev = cnic->netdev;
794 /* Get PCI related information and update hba struct members */
795 hba->pcidev = cnic->pcidev;
796 pci_dev_get(hba->pcidev);
797 hba->pci_did = hba->pcidev->device;
798 hba->pci_vid = hba->pcidev->vendor;
799 hba->pci_sdid = hba->pcidev->subsystem_device;
800 hba->pci_svid = hba->pcidev->subsystem_vendor;
801 hba->pci_func = PCI_FUNC(hba->pcidev->devfn);
802 hba->pci_devno = PCI_SLOT(hba->pcidev->devfn);
Michael Chancf4e6362009-06-08 18:14:44 -0700803
804 bnx2i_identify_device(hba);
805 bnx2i_setup_host_queue_size(hba, shost);
806
807 if (test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type)) {
808 hba->regview = ioremap_nocache(hba->netdev->base_addr,
809 BNX2_MQ_CONFIG2);
810 if (!hba->regview)
811 goto ioreg_map_err;
812 } else if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
813 hba->regview = ioremap_nocache(hba->netdev->base_addr, 4096);
814 if (!hba->regview)
815 goto ioreg_map_err;
816 }
817
818 if (bnx2i_setup_mp_bdt(hba))
819 goto mp_bdt_mem_err;
820
821 INIT_LIST_HEAD(&hba->ep_ofld_list);
Eddie Wai46012e82010-07-01 15:34:51 -0700822 INIT_LIST_HEAD(&hba->ep_active_list);
Michael Chancf4e6362009-06-08 18:14:44 -0700823 INIT_LIST_HEAD(&hba->ep_destroy_list);
824 rwlock_init(&hba->ep_rdwr_lock);
825
826 hba->mtu_supported = BNX2I_MAX_MTU_SUPPORTED;
827
828 /* different values for 5708/5709/57710 */
829 hba->max_active_conns = ISCSI_MAX_CONNS_PER_HBA;
830
831 if (bnx2i_setup_free_cid_que(hba))
832 goto cid_que_err;
833
834 /* SQ/RQ/CQ size can be changed via sysfx interface */
835 if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
836 if (sq_size && sq_size <= BNX2I_5770X_SQ_WQES_MAX)
837 hba->max_sqes = sq_size;
838 else
839 hba->max_sqes = BNX2I_5770X_SQ_WQES_DEFAULT;
840 } else { /* 5706/5708/5709 */
841 if (sq_size && sq_size <= BNX2I_570X_SQ_WQES_MAX)
842 hba->max_sqes = sq_size;
843 else
844 hba->max_sqes = BNX2I_570X_SQ_WQES_DEFAULT;
845 }
846
847 hba->max_rqes = rq_size;
848 hba->max_cqes = hba->max_sqes + rq_size;
849 if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
850 if (hba->max_cqes > BNX2I_5770X_CQ_WQES_MAX)
851 hba->max_cqes = BNX2I_5770X_CQ_WQES_MAX;
852 } else if (hba->max_cqes > BNX2I_570X_CQ_WQES_MAX)
853 hba->max_cqes = BNX2I_570X_CQ_WQES_MAX;
854
855 hba->num_ccell = hba->max_sqes / 2;
856
857 spin_lock_init(&hba->lock);
858 mutex_init(&hba->net_dev_lock);
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -0700859 init_waitqueue_head(&hba->eh_wait);
Eddie Waie37d2c42010-07-01 15:34:53 -0700860 if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
Eddie Wai55e15c92010-07-01 15:34:52 -0700861 hba->hba_shutdown_tmo = 20 * HZ;
Eddie Waie37d2c42010-07-01 15:34:53 -0700862 hba->conn_teardown_tmo = 20 * HZ;
863 hba->conn_ctx_destroy_tmo = 6 * HZ;
864 } else { /* 5706/5708/5709 */
Eddie Wai55e15c92010-07-01 15:34:52 -0700865 hba->hba_shutdown_tmo = 20 * HZ;
Eddie Waie37d2c42010-07-01 15:34:53 -0700866 hba->conn_teardown_tmo = 10 * HZ;
867 hba->conn_ctx_destroy_tmo = 2 * HZ;
868 }
Michael Chancf4e6362009-06-08 18:14:44 -0700869
870 if (iscsi_host_add(shost, &hba->pcidev->dev))
871 goto free_dump_mem;
872 return hba;
873
874free_dump_mem:
875 bnx2i_release_free_cid_que(hba);
876cid_que_err:
877 bnx2i_free_mp_bdt(hba);
878mp_bdt_mem_err:
879 if (hba->regview) {
880 iounmap(hba->regview);
881 hba->regview = NULL;
882 }
883ioreg_map_err:
884 pci_dev_put(hba->pcidev);
885 scsi_host_put(shost);
886 return NULL;
887}
888
889/**
890 * bnx2i_free_hba- releases hba structure and resources held by the adapter
891 * @hba: pointer to adapter instance
892 *
893 * free adapter structure and call various cleanup routines.
894 */
895void bnx2i_free_hba(struct bnx2i_hba *hba)
896{
897 struct Scsi_Host *shost = hba->shost;
898
899 iscsi_host_remove(shost);
900 INIT_LIST_HEAD(&hba->ep_ofld_list);
Eddie Wai46012e82010-07-01 15:34:51 -0700901 INIT_LIST_HEAD(&hba->ep_active_list);
Michael Chancf4e6362009-06-08 18:14:44 -0700902 INIT_LIST_HEAD(&hba->ep_destroy_list);
903 pci_dev_put(hba->pcidev);
904
905 if (hba->regview) {
906 iounmap(hba->regview);
907 hba->regview = NULL;
908 }
909 bnx2i_free_mp_bdt(hba);
910 bnx2i_release_free_cid_que(hba);
911 iscsi_host_free(shost);
912}
913
914/**
915 * bnx2i_conn_free_login_resources - free DMA resources used for login process
916 * @hba: pointer to adapter instance
917 * @bnx2i_conn: iscsi connection pointer
918 *
919 * Login related resources, mostly BDT & payload DMA memory is freed
920 */
921static void bnx2i_conn_free_login_resources(struct bnx2i_hba *hba,
922 struct bnx2i_conn *bnx2i_conn)
923{
924 if (bnx2i_conn->gen_pdu.resp_bd_tbl) {
925 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
926 bnx2i_conn->gen_pdu.resp_bd_tbl,
927 bnx2i_conn->gen_pdu.resp_bd_dma);
928 bnx2i_conn->gen_pdu.resp_bd_tbl = NULL;
929 }
930
931 if (bnx2i_conn->gen_pdu.req_bd_tbl) {
932 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
933 bnx2i_conn->gen_pdu.req_bd_tbl,
934 bnx2i_conn->gen_pdu.req_bd_dma);
935 bnx2i_conn->gen_pdu.req_bd_tbl = NULL;
936 }
937
938 if (bnx2i_conn->gen_pdu.resp_buf) {
939 dma_free_coherent(&hba->pcidev->dev,
940 ISCSI_DEF_MAX_RECV_SEG_LEN,
941 bnx2i_conn->gen_pdu.resp_buf,
942 bnx2i_conn->gen_pdu.resp_dma_addr);
943 bnx2i_conn->gen_pdu.resp_buf = NULL;
944 }
945
946 if (bnx2i_conn->gen_pdu.req_buf) {
947 dma_free_coherent(&hba->pcidev->dev,
948 ISCSI_DEF_MAX_RECV_SEG_LEN,
949 bnx2i_conn->gen_pdu.req_buf,
950 bnx2i_conn->gen_pdu.req_dma_addr);
951 bnx2i_conn->gen_pdu.req_buf = NULL;
952 }
953}
954
955/**
956 * bnx2i_conn_alloc_login_resources - alloc DMA resources for login/nop.
957 * @hba: pointer to adapter instance
958 * @bnx2i_conn: iscsi connection pointer
959 *
960 * Mgmt task DNA resources are allocated in this routine.
961 */
962static int bnx2i_conn_alloc_login_resources(struct bnx2i_hba *hba,
963 struct bnx2i_conn *bnx2i_conn)
964{
965 /* Allocate memory for login request/response buffers */
966 bnx2i_conn->gen_pdu.req_buf =
967 dma_alloc_coherent(&hba->pcidev->dev,
968 ISCSI_DEF_MAX_RECV_SEG_LEN,
969 &bnx2i_conn->gen_pdu.req_dma_addr,
970 GFP_KERNEL);
971 if (bnx2i_conn->gen_pdu.req_buf == NULL)
972 goto login_req_buf_failure;
973
974 bnx2i_conn->gen_pdu.req_buf_size = 0;
975 bnx2i_conn->gen_pdu.req_wr_ptr = bnx2i_conn->gen_pdu.req_buf;
976
977 bnx2i_conn->gen_pdu.resp_buf =
978 dma_alloc_coherent(&hba->pcidev->dev,
979 ISCSI_DEF_MAX_RECV_SEG_LEN,
980 &bnx2i_conn->gen_pdu.resp_dma_addr,
981 GFP_KERNEL);
982 if (bnx2i_conn->gen_pdu.resp_buf == NULL)
983 goto login_resp_buf_failure;
984
985 bnx2i_conn->gen_pdu.resp_buf_size = ISCSI_DEF_MAX_RECV_SEG_LEN;
986 bnx2i_conn->gen_pdu.resp_wr_ptr = bnx2i_conn->gen_pdu.resp_buf;
987
988 bnx2i_conn->gen_pdu.req_bd_tbl =
989 dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
990 &bnx2i_conn->gen_pdu.req_bd_dma, GFP_KERNEL);
991 if (bnx2i_conn->gen_pdu.req_bd_tbl == NULL)
992 goto login_req_bd_tbl_failure;
993
994 bnx2i_conn->gen_pdu.resp_bd_tbl =
995 dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
996 &bnx2i_conn->gen_pdu.resp_bd_dma,
997 GFP_KERNEL);
998 if (bnx2i_conn->gen_pdu.resp_bd_tbl == NULL)
999 goto login_resp_bd_tbl_failure;
1000
1001 return 0;
1002
1003login_resp_bd_tbl_failure:
1004 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
1005 bnx2i_conn->gen_pdu.req_bd_tbl,
1006 bnx2i_conn->gen_pdu.req_bd_dma);
1007 bnx2i_conn->gen_pdu.req_bd_tbl = NULL;
1008
1009login_req_bd_tbl_failure:
1010 dma_free_coherent(&hba->pcidev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
1011 bnx2i_conn->gen_pdu.resp_buf,
1012 bnx2i_conn->gen_pdu.resp_dma_addr);
1013 bnx2i_conn->gen_pdu.resp_buf = NULL;
1014login_resp_buf_failure:
1015 dma_free_coherent(&hba->pcidev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
1016 bnx2i_conn->gen_pdu.req_buf,
1017 bnx2i_conn->gen_pdu.req_dma_addr);
1018 bnx2i_conn->gen_pdu.req_buf = NULL;
1019login_req_buf_failure:
1020 iscsi_conn_printk(KERN_ERR, bnx2i_conn->cls_conn->dd_data,
1021 "login resource alloc failed!!\n");
1022 return -ENOMEM;
1023
1024}
1025
1026
1027/**
1028 * bnx2i_iscsi_prep_generic_pdu_bd - prepares BD table.
1029 * @bnx2i_conn: iscsi connection pointer
1030 *
1031 * Allocates buffers and BD tables before shipping requests to cnic
1032 * for PDUs prepared by 'iscsid' daemon
1033 */
1034static void bnx2i_iscsi_prep_generic_pdu_bd(struct bnx2i_conn *bnx2i_conn)
1035{
1036 struct iscsi_bd *bd_tbl;
1037
1038 bd_tbl = (struct iscsi_bd *) bnx2i_conn->gen_pdu.req_bd_tbl;
1039
1040 bd_tbl->buffer_addr_hi =
1041 (u32) ((u64) bnx2i_conn->gen_pdu.req_dma_addr >> 32);
1042 bd_tbl->buffer_addr_lo = (u32) bnx2i_conn->gen_pdu.req_dma_addr;
1043 bd_tbl->buffer_length = bnx2i_conn->gen_pdu.req_wr_ptr -
1044 bnx2i_conn->gen_pdu.req_buf;
1045 bd_tbl->reserved0 = 0;
1046 bd_tbl->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
1047 ISCSI_BD_FIRST_IN_BD_CHAIN;
1048
1049 bd_tbl = (struct iscsi_bd *) bnx2i_conn->gen_pdu.resp_bd_tbl;
1050 bd_tbl->buffer_addr_hi = (u64) bnx2i_conn->gen_pdu.resp_dma_addr >> 32;
1051 bd_tbl->buffer_addr_lo = (u32) bnx2i_conn->gen_pdu.resp_dma_addr;
1052 bd_tbl->buffer_length = ISCSI_DEF_MAX_RECV_SEG_LEN;
1053 bd_tbl->reserved0 = 0;
1054 bd_tbl->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
1055 ISCSI_BD_FIRST_IN_BD_CHAIN;
1056}
1057
1058
1059/**
1060 * bnx2i_iscsi_send_generic_request - called to send mgmt tasks.
1061 * @task: transport layer task pointer
1062 *
1063 * called to transmit PDUs prepared by the 'iscsid' daemon. iSCSI login,
1064 * Nop-out and Logout requests flow through this path.
1065 */
1066static int bnx2i_iscsi_send_generic_request(struct iscsi_task *task)
1067{
1068 struct bnx2i_cmd *cmd = task->dd_data;
1069 struct bnx2i_conn *bnx2i_conn = cmd->conn;
1070 int rc = 0;
1071 char *buf;
1072 int data_len;
1073
1074 bnx2i_iscsi_prep_generic_pdu_bd(bnx2i_conn);
1075 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
1076 case ISCSI_OP_LOGIN:
1077 bnx2i_send_iscsi_login(bnx2i_conn, task);
1078 break;
1079 case ISCSI_OP_NOOP_OUT:
1080 data_len = bnx2i_conn->gen_pdu.req_buf_size;
1081 buf = bnx2i_conn->gen_pdu.req_buf;
1082 if (data_len)
1083 rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
Michael Chancf4e6362009-06-08 18:14:44 -07001084 buf, data_len, 1);
1085 else
1086 rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
Michael Chancf4e6362009-06-08 18:14:44 -07001087 NULL, 0, 1);
1088 break;
1089 case ISCSI_OP_LOGOUT:
1090 rc = bnx2i_send_iscsi_logout(bnx2i_conn, task);
1091 break;
1092 case ISCSI_OP_SCSI_TMFUNC:
1093 rc = bnx2i_send_iscsi_tmf(bnx2i_conn, task);
1094 break;
Eddie Wai09813ba2011-02-16 15:04:30 -06001095 case ISCSI_OP_TEXT:
1096 rc = bnx2i_send_iscsi_text(bnx2i_conn, task);
1097 break;
Michael Chancf4e6362009-06-08 18:14:44 -07001098 default:
1099 iscsi_conn_printk(KERN_ALERT, bnx2i_conn->cls_conn->dd_data,
1100 "send_gen: unsupported op 0x%x\n",
1101 task->hdr->opcode);
1102 }
1103 return rc;
1104}
1105
1106
1107/**********************************************************************
1108 * SCSI-ML Interface
1109 **********************************************************************/
1110
1111/**
1112 * bnx2i_cpy_scsi_cdb - copies LUN & CDB fields in required format to sq wqe
1113 * @sc: SCSI-ML command pointer
1114 * @cmd: iscsi cmd pointer
1115 */
1116static void bnx2i_cpy_scsi_cdb(struct scsi_cmnd *sc, struct bnx2i_cmd *cmd)
1117{
1118 u32 dword;
1119 int lpcnt;
1120 u8 *srcp;
1121 u32 *dstp;
1122 u32 scsi_lun[2];
1123
1124 int_to_scsilun(sc->device->lun, (struct scsi_lun *) scsi_lun);
1125 cmd->req.lun[0] = be32_to_cpu(scsi_lun[0]);
1126 cmd->req.lun[1] = be32_to_cpu(scsi_lun[1]);
1127
1128 lpcnt = cmd->scsi_cmd->cmd_len / sizeof(dword);
1129 srcp = (u8 *) sc->cmnd;
1130 dstp = (u32 *) cmd->req.cdb;
1131 while (lpcnt--) {
1132 memcpy(&dword, (const void *) srcp, 4);
1133 *dstp = cpu_to_be32(dword);
1134 srcp += 4;
1135 dstp++;
1136 }
1137 if (sc->cmd_len & 0x3) {
1138 dword = (u32) srcp[0] | ((u32) srcp[1] << 8);
1139 *dstp = cpu_to_be32(dword);
1140 }
1141}
1142
1143static void bnx2i_cleanup_task(struct iscsi_task *task)
1144{
1145 struct iscsi_conn *conn = task->conn;
1146 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1147 struct bnx2i_hba *hba = bnx2i_conn->hba;
1148
1149 /*
1150 * mgmt task or cmd was never sent to us to transmit.
1151 */
1152 if (!task->sc || task->state == ISCSI_TASK_PENDING)
1153 return;
1154 /*
1155 * need to clean-up task context to claim dma buffers
1156 */
1157 if (task->state == ISCSI_TASK_ABRT_TMF) {
1158 bnx2i_send_cmd_cleanup_req(hba, task->dd_data);
1159
1160 spin_unlock_bh(&conn->session->lock);
1161 wait_for_completion_timeout(&bnx2i_conn->cmd_cleanup_cmpl,
1162 msecs_to_jiffies(ISCSI_CMD_CLEANUP_TIMEOUT));
1163 spin_lock_bh(&conn->session->lock);
1164 }
1165 bnx2i_iscsi_unmap_sg_list(task->dd_data);
1166}
1167
1168/**
1169 * bnx2i_mtask_xmit - transmit mtask to chip for further processing
1170 * @conn: transport layer conn structure pointer
1171 * @task: transport layer command structure pointer
1172 */
1173static int
1174bnx2i_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
1175{
1176 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1177 struct bnx2i_cmd *cmd = task->dd_data;
1178
1179 memset(bnx2i_conn->gen_pdu.req_buf, 0, ISCSI_DEF_MAX_RECV_SEG_LEN);
1180
1181 bnx2i_setup_cmd_wqe_template(cmd);
1182 bnx2i_conn->gen_pdu.req_buf_size = task->data_count;
1183 if (task->data_count) {
1184 memcpy(bnx2i_conn->gen_pdu.req_buf, task->data,
1185 task->data_count);
1186 bnx2i_conn->gen_pdu.req_wr_ptr =
1187 bnx2i_conn->gen_pdu.req_buf + task->data_count;
1188 }
1189 cmd->conn = conn->dd_data;
1190 cmd->scsi_cmd = NULL;
1191 return bnx2i_iscsi_send_generic_request(task);
1192}
1193
1194/**
1195 * bnx2i_task_xmit - transmit iscsi command to chip for further processing
1196 * @task: transport layer command structure pointer
1197 *
1198 * maps SG buffers and send request to chip/firmware in the form of SQ WQE
1199 */
1200static int bnx2i_task_xmit(struct iscsi_task *task)
1201{
1202 struct iscsi_conn *conn = task->conn;
1203 struct iscsi_session *session = conn->session;
1204 struct Scsi_Host *shost = iscsi_session_to_shost(session->cls_session);
1205 struct bnx2i_hba *hba = iscsi_host_priv(shost);
1206 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1207 struct scsi_cmnd *sc = task->sc;
1208 struct bnx2i_cmd *cmd = task->dd_data;
1209 struct iscsi_cmd *hdr = (struct iscsi_cmd *) task->hdr;
1210
Eddie Wai7287c632011-05-16 11:13:18 -07001211 if (bnx2i_conn->ep->num_active_cmds + 1 > hba->max_sqes)
1212 return -ENOMEM;
1213
Michael Chancf4e6362009-06-08 18:14:44 -07001214 /*
1215 * If there is no scsi_cmnd this must be a mgmt task
1216 */
1217 if (!sc)
1218 return bnx2i_mtask_xmit(conn, task);
1219
1220 bnx2i_setup_cmd_wqe_template(cmd);
1221 cmd->req.op_code = ISCSI_OP_SCSI_CMD;
1222 cmd->conn = bnx2i_conn;
1223 cmd->scsi_cmd = sc;
1224 cmd->req.total_data_transfer_length = scsi_bufflen(sc);
1225 cmd->req.cmd_sn = be32_to_cpu(hdr->cmdsn);
1226
1227 bnx2i_iscsi_map_sg_list(cmd);
1228 bnx2i_cpy_scsi_cdb(sc, cmd);
1229
1230 cmd->req.op_attr = ISCSI_ATTR_SIMPLE;
1231 if (sc->sc_data_direction == DMA_TO_DEVICE) {
1232 cmd->req.op_attr |= ISCSI_CMD_REQUEST_WRITE;
1233 cmd->req.itt = task->itt |
1234 (ISCSI_TASK_TYPE_WRITE << ISCSI_CMD_REQUEST_TYPE_SHIFT);
1235 bnx2i_setup_write_cmd_bd_info(task);
1236 } else {
1237 if (scsi_bufflen(sc))
1238 cmd->req.op_attr |= ISCSI_CMD_REQUEST_READ;
1239 cmd->req.itt = task->itt |
1240 (ISCSI_TASK_TYPE_READ << ISCSI_CMD_REQUEST_TYPE_SHIFT);
1241 }
1242
1243 cmd->req.num_bds = cmd->io_tbl.bd_valid;
1244 if (!cmd->io_tbl.bd_valid) {
1245 cmd->req.bd_list_addr_lo = (u32) hba->mp_bd_dma;
1246 cmd->req.bd_list_addr_hi = (u32) ((u64) hba->mp_bd_dma >> 32);
1247 cmd->req.num_bds = 1;
1248 }
1249
1250 bnx2i_send_iscsi_scsicmd(bnx2i_conn, cmd);
1251 return 0;
1252}
1253
1254/**
1255 * bnx2i_session_create - create a new iscsi session
1256 * @cmds_max: max commands supported
1257 * @qdepth: scsi queue depth to support
1258 * @initial_cmdsn: initial iscsi CMDSN to be used for this session
1259 *
1260 * Creates a new iSCSI session instance on given device.
1261 */
1262static struct iscsi_cls_session *
1263bnx2i_session_create(struct iscsi_endpoint *ep,
1264 uint16_t cmds_max, uint16_t qdepth,
1265 uint32_t initial_cmdsn)
1266{
1267 struct Scsi_Host *shost;
1268 struct iscsi_cls_session *cls_session;
1269 struct bnx2i_hba *hba;
1270 struct bnx2i_endpoint *bnx2i_ep;
1271
1272 if (!ep) {
1273 printk(KERN_ERR "bnx2i: missing ep.\n");
1274 return NULL;
1275 }
1276
1277 bnx2i_ep = ep->dd_data;
1278 shost = bnx2i_ep->hba->shost;
1279 hba = iscsi_host_priv(shost);
1280 if (bnx2i_adapter_ready(hba))
1281 return NULL;
1282
1283 /*
1284 * user can override hw limit as long as it is within
1285 * the min/max.
1286 */
1287 if (cmds_max > hba->max_sqes)
1288 cmds_max = hba->max_sqes;
1289 else if (cmds_max < BNX2I_SQ_WQES_MIN)
1290 cmds_max = BNX2I_SQ_WQES_MIN;
1291
1292 cls_session = iscsi_session_setup(&bnx2i_iscsi_transport, shost,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05301293 cmds_max, 0, sizeof(struct bnx2i_cmd),
Michael Chancf4e6362009-06-08 18:14:44 -07001294 initial_cmdsn, ISCSI_MAX_TARGET);
1295 if (!cls_session)
1296 return NULL;
1297
1298 if (bnx2i_setup_cmd_pool(hba, cls_session->dd_data))
1299 goto session_teardown;
1300 return cls_session;
1301
1302session_teardown:
1303 iscsi_session_teardown(cls_session);
1304 return NULL;
1305}
1306
1307
1308/**
1309 * bnx2i_session_destroy - destroys iscsi session
1310 * @cls_session: pointer to iscsi cls session
1311 *
1312 * Destroys previously created iSCSI session instance and releases
1313 * all resources held by it
1314 */
1315static void bnx2i_session_destroy(struct iscsi_cls_session *cls_session)
1316{
1317 struct iscsi_session *session = cls_session->dd_data;
1318 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1319 struct bnx2i_hba *hba = iscsi_host_priv(shost);
1320
1321 bnx2i_destroy_cmd_pool(hba, session);
1322 iscsi_session_teardown(cls_session);
1323}
1324
1325
1326/**
1327 * bnx2i_conn_create - create iscsi connection instance
1328 * @cls_session: pointer to iscsi cls session
1329 * @cid: iscsi cid as per rfc (not NX2's CID terminology)
1330 *
1331 * Creates a new iSCSI connection instance for a given session
1332 */
1333static struct iscsi_cls_conn *
1334bnx2i_conn_create(struct iscsi_cls_session *cls_session, uint32_t cid)
1335{
1336 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1337 struct bnx2i_hba *hba = iscsi_host_priv(shost);
1338 struct bnx2i_conn *bnx2i_conn;
1339 struct iscsi_cls_conn *cls_conn;
1340 struct iscsi_conn *conn;
1341
1342 cls_conn = iscsi_conn_setup(cls_session, sizeof(*bnx2i_conn),
1343 cid);
1344 if (!cls_conn)
1345 return NULL;
1346 conn = cls_conn->dd_data;
1347
1348 bnx2i_conn = conn->dd_data;
1349 bnx2i_conn->cls_conn = cls_conn;
1350 bnx2i_conn->hba = hba;
1351 /* 'ep' ptr will be assigned in bind() call */
1352 bnx2i_conn->ep = NULL;
1353 init_completion(&bnx2i_conn->cmd_cleanup_cmpl);
1354
1355 if (bnx2i_conn_alloc_login_resources(hba, bnx2i_conn)) {
1356 iscsi_conn_printk(KERN_ALERT, conn,
1357 "conn_new: login resc alloc failed!!\n");
1358 goto free_conn;
1359 }
1360
1361 return cls_conn;
1362
1363free_conn:
1364 iscsi_conn_teardown(cls_conn);
1365 return NULL;
1366}
1367
1368/**
1369 * bnx2i_conn_bind - binds iscsi sess, conn and ep objects together
1370 * @cls_session: pointer to iscsi cls session
1371 * @cls_conn: pointer to iscsi cls conn
1372 * @transport_fd: 64-bit EP handle
1373 * @is_leading: leading connection on this session?
1374 *
1375 * Binds together iSCSI session instance, iSCSI connection instance
1376 * and the TCP connection. This routine returns error code if
1377 * TCP connection does not belong on the device iSCSI sess/conn
1378 * is bound
1379 */
1380static int bnx2i_conn_bind(struct iscsi_cls_session *cls_session,
1381 struct iscsi_cls_conn *cls_conn,
1382 uint64_t transport_fd, int is_leading)
1383{
1384 struct iscsi_conn *conn = cls_conn->dd_data;
1385 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1386 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1387 struct bnx2i_hba *hba = iscsi_host_priv(shost);
1388 struct bnx2i_endpoint *bnx2i_ep;
1389 struct iscsi_endpoint *ep;
1390 int ret_code;
1391
1392 ep = iscsi_lookup_endpoint(transport_fd);
1393 if (!ep)
1394 return -EINVAL;
Eddie Wai842158d2010-11-23 15:29:28 -08001395 /*
1396 * Forcefully terminate all in progress connection recovery at the
1397 * earliest, either in bind(), send_pdu(LOGIN), or conn_start()
1398 */
1399 if (bnx2i_adapter_ready(hba))
1400 return -EIO;
Michael Chancf4e6362009-06-08 18:14:44 -07001401
1402 bnx2i_ep = ep->dd_data;
1403 if ((bnx2i_ep->state == EP_STATE_TCP_FIN_RCVD) ||
1404 (bnx2i_ep->state == EP_STATE_TCP_RST_RCVD))
1405 /* Peer disconnect via' FIN or RST */
1406 return -EINVAL;
1407
1408 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1409 return -EINVAL;
1410
1411 if (bnx2i_ep->hba != hba) {
1412 /* Error - TCP connection does not belong to this device
1413 */
1414 iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
1415 "conn bind, ep=0x%p (%s) does not",
1416 bnx2i_ep, bnx2i_ep->hba->netdev->name);
1417 iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
1418 "belong to hba (%s)\n",
1419 hba->netdev->name);
1420 return -EEXIST;
1421 }
Michael Chancf4e6362009-06-08 18:14:44 -07001422 bnx2i_ep->conn = bnx2i_conn;
1423 bnx2i_conn->ep = bnx2i_ep;
1424 bnx2i_conn->iscsi_conn_cid = bnx2i_ep->ep_iscsi_cid;
1425 bnx2i_conn->fw_cid = bnx2i_ep->ep_cid;
Michael Chancf4e6362009-06-08 18:14:44 -07001426
1427 ret_code = bnx2i_bind_conn_to_iscsi_cid(hba, bnx2i_conn,
1428 bnx2i_ep->ep_iscsi_cid);
1429
1430 /* 5706/5708/5709 FW takes RQ as full when initiated, but for 57710
1431 * driver needs to explicitly replenish RQ index during setup.
1432 */
1433 if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
1434 bnx2i_put_rq_buf(bnx2i_conn, 0);
1435
1436 bnx2i_arm_cq_event_coalescing(bnx2i_conn->ep, CNIC_ARM_CQE);
1437 return ret_code;
1438}
1439
1440
1441/**
1442 * bnx2i_conn_destroy - destroy iscsi connection instance & release resources
1443 * @cls_conn: pointer to iscsi cls conn
1444 *
1445 * Destroy an iSCSI connection instance and release memory resources held by
1446 * this connection
1447 */
1448static void bnx2i_conn_destroy(struct iscsi_cls_conn *cls_conn)
1449{
1450 struct iscsi_conn *conn = cls_conn->dd_data;
1451 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1452 struct Scsi_Host *shost;
1453 struct bnx2i_hba *hba;
1454
1455 shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
1456 hba = iscsi_host_priv(shost);
1457
1458 bnx2i_conn_free_login_resources(hba, bnx2i_conn);
1459 iscsi_conn_teardown(cls_conn);
1460}
1461
1462
1463/**
Mike Christied8585bc2011-02-16 15:04:39 -06001464 * bnx2i_ep_get_param - return iscsi ep parameter to caller
1465 * @ep: pointer to iscsi endpoint
Michael Chancf4e6362009-06-08 18:14:44 -07001466 * @param: parameter type identifier
1467 * @buf: buffer pointer
1468 *
Mike Christied8585bc2011-02-16 15:04:39 -06001469 * returns iSCSI ep parameters
Michael Chancf4e6362009-06-08 18:14:44 -07001470 */
Mike Christied8585bc2011-02-16 15:04:39 -06001471static int bnx2i_ep_get_param(struct iscsi_endpoint *ep,
1472 enum iscsi_param param, char *buf)
Michael Chancf4e6362009-06-08 18:14:44 -07001473{
Mike Christied8585bc2011-02-16 15:04:39 -06001474 struct bnx2i_endpoint *bnx2i_ep = ep->dd_data;
1475 struct bnx2i_hba *hba = bnx2i_ep->hba;
1476 int len = -ENOTCONN;
Michael Chancf4e6362009-06-08 18:14:44 -07001477
Mike Christied8585bc2011-02-16 15:04:39 -06001478 if (!hba)
1479 return -ENOTCONN;
Eddie Wai7a2962c2010-11-23 15:29:26 -08001480
Michael Chancf4e6362009-06-08 18:14:44 -07001481 switch (param) {
1482 case ISCSI_PARAM_CONN_PORT:
Mike Christied8585bc2011-02-16 15:04:39 -06001483 mutex_lock(&hba->net_dev_lock);
1484 if (bnx2i_ep->cm_sk)
1485 len = sprintf(buf, "%hu\n", bnx2i_ep->cm_sk->dst_port);
1486 mutex_unlock(&hba->net_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -07001487 break;
1488 case ISCSI_PARAM_CONN_ADDRESS:
Mike Christied8585bc2011-02-16 15:04:39 -06001489 mutex_lock(&hba->net_dev_lock);
1490 if (bnx2i_ep->cm_sk)
1491 len = sprintf(buf, "%pI4\n", &bnx2i_ep->cm_sk->dst_ip);
1492 mutex_unlock(&hba->net_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -07001493 break;
1494 default:
Mike Christied8585bc2011-02-16 15:04:39 -06001495 return -ENOSYS;
Michael Chancf4e6362009-06-08 18:14:44 -07001496 }
Mike Christied8585bc2011-02-16 15:04:39 -06001497
Michael Chancf4e6362009-06-08 18:14:44 -07001498 return len;
1499}
1500
1501/**
1502 * bnx2i_host_get_param - returns host (adapter) related parameters
1503 * @shost: scsi host pointer
1504 * @param: parameter type identifier
1505 * @buf: buffer pointer
1506 */
1507static int bnx2i_host_get_param(struct Scsi_Host *shost,
1508 enum iscsi_host_param param, char *buf)
1509{
1510 struct bnx2i_hba *hba = iscsi_host_priv(shost);
1511 int len = 0;
1512
1513 switch (param) {
1514 case ISCSI_HOST_PARAM_HWADDRESS:
1515 len = sysfs_format_mac(buf, hba->cnic->mac_addr, 6);
1516 break;
1517 case ISCSI_HOST_PARAM_NETDEV_NAME:
1518 len = sprintf(buf, "%s\n", hba->netdev->name);
1519 break;
Michael Chan625986c2010-07-01 15:34:55 -07001520 case ISCSI_HOST_PARAM_IPADDRESS: {
1521 struct list_head *active_list = &hba->ep_active_list;
1522
1523 read_lock_bh(&hba->ep_rdwr_lock);
1524 if (!list_empty(&hba->ep_active_list)) {
1525 struct bnx2i_endpoint *bnx2i_ep;
1526 struct cnic_sock *csk;
1527
1528 bnx2i_ep = list_first_entry(active_list,
1529 struct bnx2i_endpoint,
1530 link);
1531 csk = bnx2i_ep->cm_sk;
1532 if (test_bit(SK_F_IPV6, &csk->flags))
1533 len = sprintf(buf, "%pI6\n", csk->src_ip);
1534 else
1535 len = sprintf(buf, "%pI4\n", csk->src_ip);
1536 }
1537 read_unlock_bh(&hba->ep_rdwr_lock);
1538 break;
1539 }
Michael Chancf4e6362009-06-08 18:14:44 -07001540 default:
1541 return iscsi_host_get_param(shost, param, buf);
1542 }
1543 return len;
1544}
1545
1546/**
1547 * bnx2i_conn_start - completes iscsi connection migration to FFP
1548 * @cls_conn: pointer to iscsi cls conn
1549 *
1550 * last call in FFP migration to handover iscsi conn to the driver
1551 */
1552static int bnx2i_conn_start(struct iscsi_cls_conn *cls_conn)
1553{
1554 struct iscsi_conn *conn = cls_conn->dd_data;
1555 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1556
1557 bnx2i_conn->ep->state = EP_STATE_ULP_UPDATE_START;
1558 bnx2i_update_iscsi_conn(conn);
1559
1560 /*
1561 * this should normally not sleep for a long time so it should
1562 * not disrupt the caller.
1563 */
1564 bnx2i_conn->ep->ofld_timer.expires = 1 * HZ + jiffies;
1565 bnx2i_conn->ep->ofld_timer.function = bnx2i_ep_ofld_timer;
1566 bnx2i_conn->ep->ofld_timer.data = (unsigned long) bnx2i_conn->ep;
1567 add_timer(&bnx2i_conn->ep->ofld_timer);
1568 /* update iSCSI context for this conn, wait for CNIC to complete */
1569 wait_event_interruptible(bnx2i_conn->ep->ofld_wait,
1570 bnx2i_conn->ep->state != EP_STATE_ULP_UPDATE_START);
1571
1572 if (signal_pending(current))
1573 flush_signals(current);
1574 del_timer_sync(&bnx2i_conn->ep->ofld_timer);
1575
1576 iscsi_conn_start(cls_conn);
1577 return 0;
1578}
1579
1580
1581/**
1582 * bnx2i_conn_get_stats - returns iSCSI stats
1583 * @cls_conn: pointer to iscsi cls conn
1584 * @stats: pointer to iscsi statistic struct
1585 */
1586static void bnx2i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1587 struct iscsi_stats *stats)
1588{
1589 struct iscsi_conn *conn = cls_conn->dd_data;
1590
1591 stats->txdata_octets = conn->txdata_octets;
1592 stats->rxdata_octets = conn->rxdata_octets;
1593 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1594 stats->dataout_pdus = conn->dataout_pdus_cnt;
1595 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1596 stats->datain_pdus = conn->datain_pdus_cnt;
1597 stats->r2t_pdus = conn->r2t_pdus_cnt;
1598 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1599 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1600 stats->custom_length = 3;
1601 strcpy(stats->custom[2].desc, "eh_abort_cnt");
1602 stats->custom[2].value = conn->eh_abort_cnt;
1603 stats->digest_err = 0;
1604 stats->timeout_err = 0;
1605 stats->custom_length = 0;
1606}
1607
1608
1609/**
1610 * bnx2i_check_route - checks if target IP route belongs to one of NX2 devices
1611 * @dst_addr: target IP address
1612 *
1613 * check if route resolves to BNX2 device
1614 */
1615static struct bnx2i_hba *bnx2i_check_route(struct sockaddr *dst_addr)
1616{
1617 struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
1618 struct bnx2i_hba *hba;
1619 struct cnic_dev *cnic = NULL;
1620
Michael Chancf4e6362009-06-08 18:14:44 -07001621 hba = get_adapter_list_head();
1622 if (hba && hba->cnic)
1623 cnic = hba->cnic->cm_select_dev(desti, CNIC_ULP_ISCSI);
1624 if (!cnic) {
1625 printk(KERN_ALERT "bnx2i: no route,"
1626 "can't connect using cnic\n");
1627 goto no_nx2_route;
1628 }
1629 hba = bnx2i_find_hba_for_cnic(cnic);
1630 if (!hba)
1631 goto no_nx2_route;
1632
1633 if (bnx2i_adapter_ready(hba)) {
1634 printk(KERN_ALERT "bnx2i: check route, hba not found\n");
1635 goto no_nx2_route;
1636 }
1637 if (hba->netdev->mtu > hba->mtu_supported) {
1638 printk(KERN_ALERT "bnx2i: %s network i/f mtu is set to %d\n",
1639 hba->netdev->name, hba->netdev->mtu);
1640 printk(KERN_ALERT "bnx2i: iSCSI HBA can support mtu of %d\n",
1641 hba->mtu_supported);
1642 goto no_nx2_route;
1643 }
1644 return hba;
1645no_nx2_route:
1646 return NULL;
1647}
1648
1649
1650/**
1651 * bnx2i_tear_down_conn - tear down iscsi/tcp connection and free resources
1652 * @hba: pointer to adapter instance
1653 * @ep: endpoint (transport indentifier) structure
1654 *
1655 * destroys cm_sock structure and on chip iscsi context
1656 */
1657static int bnx2i_tear_down_conn(struct bnx2i_hba *hba,
1658 struct bnx2i_endpoint *ep)
1659{
Eddie Wai5bf3f392010-11-23 15:29:23 -08001660 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic) && ep->cm_sk)
Michael Chancf4e6362009-06-08 18:14:44 -07001661 hba->cnic->cm_destroy(ep->cm_sk);
1662
Michael Chancf4e6362009-06-08 18:14:44 -07001663 if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type) &&
1664 ep->state == EP_STATE_DISCONN_TIMEDOUT) {
Eddie Wai5bf3f392010-11-23 15:29:23 -08001665 if (ep->conn && ep->conn->cls_conn &&
1666 ep->conn->cls_conn->dd_data) {
1667 struct iscsi_conn *conn = ep->conn->cls_conn->dd_data;
1668
1669 /* Must suspend all rx queue activity for this ep */
1670 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1671 }
1672 /* CONN_DISCONNECT timeout may or may not be an issue depending
1673 * on what transcribed in TCP layer, different targets behave
1674 * differently
1675 */
1676 printk(KERN_ALERT "bnx2i (%s): - WARN - CONN_DISCON timed out, "
1677 "please submit GRC Dump, NW/PCIe trace, "
1678 "driver msgs to developers for analysis\n",
1679 hba->netdev->name);
Michael Chancf4e6362009-06-08 18:14:44 -07001680 }
1681
1682 ep->state = EP_STATE_CLEANUP_START;
1683 init_timer(&ep->ofld_timer);
Eddie Waie37d2c42010-07-01 15:34:53 -07001684 ep->ofld_timer.expires = hba->conn_ctx_destroy_tmo + jiffies;
Michael Chancf4e6362009-06-08 18:14:44 -07001685 ep->ofld_timer.function = bnx2i_ep_ofld_timer;
1686 ep->ofld_timer.data = (unsigned long) ep;
1687 add_timer(&ep->ofld_timer);
1688
1689 bnx2i_ep_destroy_list_add(hba, ep);
1690
1691 /* destroy iSCSI context, wait for it to complete */
Eddie Waibee34872010-11-23 15:29:29 -08001692 if (bnx2i_send_conn_destroy(hba, ep))
1693 ep->state = EP_STATE_CLEANUP_CMPL;
1694
Michael Chancf4e6362009-06-08 18:14:44 -07001695 wait_event_interruptible(ep->ofld_wait,
1696 (ep->state != EP_STATE_CLEANUP_START));
1697
1698 if (signal_pending(current))
1699 flush_signals(current);
1700 del_timer_sync(&ep->ofld_timer);
1701
1702 bnx2i_ep_destroy_list_del(hba, ep);
1703
1704 if (ep->state != EP_STATE_CLEANUP_CMPL)
1705 /* should never happen */
1706 printk(KERN_ALERT "bnx2i - conn destroy failed\n");
1707
1708 return 0;
1709}
1710
1711
1712/**
1713 * bnx2i_ep_connect - establish TCP connection to target portal
1714 * @shost: scsi host
1715 * @dst_addr: target IP address
1716 * @non_blocking: blocking or non-blocking call
1717 *
1718 * this routine initiates the TCP/IP connection by invoking Option-2 i/f
1719 * with l5_core and the CNIC. This is a multi-step process of resolving
1720 * route to target, create a iscsi connection context, handshaking with
1721 * CNIC module to create/initialize the socket struct and finally
1722 * sending down option-2 request to complete TCP 3-way handshake
1723 */
1724static struct iscsi_endpoint *bnx2i_ep_connect(struct Scsi_Host *shost,
1725 struct sockaddr *dst_addr,
1726 int non_blocking)
1727{
1728 u32 iscsi_cid = BNX2I_CID_RESERVED;
1729 struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
1730 struct sockaddr_in6 *desti6;
1731 struct bnx2i_endpoint *bnx2i_ep;
1732 struct bnx2i_hba *hba;
1733 struct cnic_dev *cnic;
1734 struct cnic_sockaddr saddr;
1735 struct iscsi_endpoint *ep;
1736 int rc = 0;
1737
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -07001738 if (shost) {
Michael Chancf4e6362009-06-08 18:14:44 -07001739 /* driver is given scsi host to work with */
1740 hba = iscsi_host_priv(shost);
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -07001741 } else
Michael Chancf4e6362009-06-08 18:14:44 -07001742 /*
1743 * check if the given destination can be reached through
1744 * a iscsi capable NetXtreme2 device
1745 */
1746 hba = bnx2i_check_route(dst_addr);
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -07001747
Eddie Waia91031a2010-11-23 15:29:30 -08001748 if (!hba) {
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -07001749 rc = -EINVAL;
Eddie Wai55e15c92010-07-01 15:34:52 -07001750 goto nohba;
Michael Chancf4e6362009-06-08 18:14:44 -07001751 }
Eddie Wai55e15c92010-07-01 15:34:52 -07001752 mutex_lock(&hba->net_dev_lock);
Eddie Waia91031a2010-11-23 15:29:30 -08001753
1754 if (bnx2i_adapter_ready(hba) || !hba->cid_que.cid_free_cnt) {
1755 rc = -EPERM;
1756 goto check_busy;
1757 }
1758 cnic = hba->cnic;
Michael Chancf4e6362009-06-08 18:14:44 -07001759 ep = bnx2i_alloc_ep(hba);
1760 if (!ep) {
1761 rc = -ENOMEM;
1762 goto check_busy;
1763 }
1764 bnx2i_ep = ep->dd_data;
1765
Michael Chancf4e6362009-06-08 18:14:44 -07001766 bnx2i_ep->num_active_cmds = 0;
1767 iscsi_cid = bnx2i_alloc_iscsi_cid(hba);
1768 if (iscsi_cid == -1) {
Eddie Waia91031a2010-11-23 15:29:30 -08001769 printk(KERN_ALERT "bnx2i (%s): alloc_ep - unable to allocate "
1770 "iscsi cid\n", hba->netdev->name);
Michael Chancf4e6362009-06-08 18:14:44 -07001771 rc = -ENOMEM;
Eddie Waia91031a2010-11-23 15:29:30 -08001772 bnx2i_free_ep(ep);
1773 goto check_busy;
Michael Chancf4e6362009-06-08 18:14:44 -07001774 }
1775 bnx2i_ep->hba_age = hba->age;
1776
1777 rc = bnx2i_alloc_qp_resc(hba, bnx2i_ep);
1778 if (rc != 0) {
Eddie Waia91031a2010-11-23 15:29:30 -08001779 printk(KERN_ALERT "bnx2i (%s): ep_conn - alloc QP resc error"
1780 "\n", hba->netdev->name);
Michael Chancf4e6362009-06-08 18:14:44 -07001781 rc = -ENOMEM;
1782 goto qp_resc_err;
1783 }
1784
1785 bnx2i_ep->ep_iscsi_cid = (u16)iscsi_cid;
1786 bnx2i_ep->state = EP_STATE_OFLD_START;
1787 bnx2i_ep_ofld_list_add(hba, bnx2i_ep);
1788
1789 init_timer(&bnx2i_ep->ofld_timer);
1790 bnx2i_ep->ofld_timer.expires = 2 * HZ + jiffies;
1791 bnx2i_ep->ofld_timer.function = bnx2i_ep_ofld_timer;
1792 bnx2i_ep->ofld_timer.data = (unsigned long) bnx2i_ep;
1793 add_timer(&bnx2i_ep->ofld_timer);
1794
Eddie Waibee34872010-11-23 15:29:29 -08001795 if (bnx2i_send_conn_ofld_req(hba, bnx2i_ep)) {
1796 if (bnx2i_ep->state == EP_STATE_OFLD_FAILED_CID_BUSY) {
1797 printk(KERN_ALERT "bnx2i (%s): iscsi cid %d is busy\n",
1798 hba->netdev->name, bnx2i_ep->ep_iscsi_cid);
1799 rc = -EBUSY;
1800 } else
1801 rc = -ENOSPC;
1802 printk(KERN_ALERT "bnx2i (%s): unable to send conn offld kwqe"
1803 "\n", hba->netdev->name);
1804 bnx2i_ep_ofld_list_del(hba, bnx2i_ep);
1805 goto conn_failed;
1806 }
Michael Chancf4e6362009-06-08 18:14:44 -07001807
1808 /* Wait for CNIC hardware to setup conn context and return 'cid' */
1809 wait_event_interruptible(bnx2i_ep->ofld_wait,
1810 bnx2i_ep->state != EP_STATE_OFLD_START);
1811
1812 if (signal_pending(current))
1813 flush_signals(current);
1814 del_timer_sync(&bnx2i_ep->ofld_timer);
1815
1816 bnx2i_ep_ofld_list_del(hba, bnx2i_ep);
1817
1818 if (bnx2i_ep->state != EP_STATE_OFLD_COMPL) {
Eddie Waia91031a2010-11-23 15:29:30 -08001819 if (bnx2i_ep->state == EP_STATE_OFLD_FAILED_CID_BUSY) {
1820 printk(KERN_ALERT "bnx2i (%s): iscsi cid %d is busy\n",
1821 hba->netdev->name, bnx2i_ep->ep_iscsi_cid);
1822 rc = -EBUSY;
1823 } else
1824 rc = -ENOSPC;
Michael Chancf4e6362009-06-08 18:14:44 -07001825 goto conn_failed;
1826 }
1827
1828 rc = cnic->cm_create(cnic, CNIC_ULP_ISCSI, bnx2i_ep->ep_cid,
1829 iscsi_cid, &bnx2i_ep->cm_sk, bnx2i_ep);
1830 if (rc) {
1831 rc = -EINVAL;
Eddie Waia91031a2010-11-23 15:29:30 -08001832 /* Need to terminate and cleanup the connection */
1833 goto release_ep;
Michael Chancf4e6362009-06-08 18:14:44 -07001834 }
1835
1836 bnx2i_ep->cm_sk->rcv_buf = 256 * 1024;
1837 bnx2i_ep->cm_sk->snd_buf = 256 * 1024;
1838 clear_bit(SK_TCP_TIMESTAMP, &bnx2i_ep->cm_sk->tcp_flags);
1839
1840 memset(&saddr, 0, sizeof(saddr));
1841 if (dst_addr->sa_family == AF_INET) {
1842 desti = (struct sockaddr_in *) dst_addr;
1843 saddr.remote.v4 = *desti;
1844 saddr.local.v4.sin_family = desti->sin_family;
1845 } else if (dst_addr->sa_family == AF_INET6) {
1846 desti6 = (struct sockaddr_in6 *) dst_addr;
1847 saddr.remote.v6 = *desti6;
1848 saddr.local.v6.sin6_family = desti6->sin6_family;
1849 }
1850
1851 bnx2i_ep->timestamp = jiffies;
1852 bnx2i_ep->state = EP_STATE_CONNECT_START;
1853 if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
1854 rc = -EINVAL;
1855 goto conn_failed;
1856 } else
1857 rc = cnic->cm_connect(bnx2i_ep->cm_sk, &saddr);
Michael Chancf4e6362009-06-08 18:14:44 -07001858 if (rc)
1859 goto release_ep;
1860
Eddie Wai46012e82010-07-01 15:34:51 -07001861 bnx2i_ep_active_list_add(hba, bnx2i_ep);
1862
Michael Chancf4e6362009-06-08 18:14:44 -07001863 if (bnx2i_map_ep_dbell_regs(bnx2i_ep))
Eddie Wai46012e82010-07-01 15:34:51 -07001864 goto del_active_ep;
1865
Michael Chancf4e6362009-06-08 18:14:44 -07001866 mutex_unlock(&hba->net_dev_lock);
1867 return ep;
1868
Eddie Wai46012e82010-07-01 15:34:51 -07001869del_active_ep:
1870 bnx2i_ep_active_list_del(hba, bnx2i_ep);
Michael Chancf4e6362009-06-08 18:14:44 -07001871release_ep:
1872 if (bnx2i_tear_down_conn(hba, bnx2i_ep)) {
1873 mutex_unlock(&hba->net_dev_lock);
1874 return ERR_PTR(rc);
1875 }
1876conn_failed:
Michael Chancf4e6362009-06-08 18:14:44 -07001877 bnx2i_free_qp_resc(hba, bnx2i_ep);
1878qp_resc_err:
1879 bnx2i_free_ep(ep);
Michael Chancf4e6362009-06-08 18:14:44 -07001880check_busy:
Eddie Wai55e15c92010-07-01 15:34:52 -07001881 mutex_unlock(&hba->net_dev_lock);
1882nohba:
Michael Chancf4e6362009-06-08 18:14:44 -07001883 return ERR_PTR(rc);
1884}
1885
1886
1887/**
1888 * bnx2i_ep_poll - polls for TCP connection establishement
1889 * @ep: TCP connection (endpoint) handle
1890 * @timeout_ms: timeout value in milli secs
1891 *
1892 * polls for TCP connect request to complete
1893 */
1894static int bnx2i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1895{
1896 struct bnx2i_endpoint *bnx2i_ep;
1897 int rc = 0;
1898
1899 bnx2i_ep = ep->dd_data;
1900 if ((bnx2i_ep->state == EP_STATE_IDLE) ||
1901 (bnx2i_ep->state == EP_STATE_CONNECT_FAILED) ||
1902 (bnx2i_ep->state == EP_STATE_OFLD_FAILED))
1903 return -1;
1904 if (bnx2i_ep->state == EP_STATE_CONNECT_COMPL)
1905 return 1;
1906
1907 rc = wait_event_interruptible_timeout(bnx2i_ep->ofld_wait,
1908 ((bnx2i_ep->state ==
1909 EP_STATE_OFLD_FAILED) ||
1910 (bnx2i_ep->state ==
1911 EP_STATE_CONNECT_FAILED) ||
1912 (bnx2i_ep->state ==
1913 EP_STATE_CONNECT_COMPL)),
1914 msecs_to_jiffies(timeout_ms));
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -07001915 if (bnx2i_ep->state == EP_STATE_OFLD_FAILED)
Michael Chancf4e6362009-06-08 18:14:44 -07001916 rc = -1;
1917
1918 if (rc > 0)
1919 return 1;
1920 else if (!rc)
1921 return 0; /* timeout */
1922 else
1923 return rc;
1924}
1925
1926
1927/**
1928 * bnx2i_ep_tcp_conn_active - check EP state transition
1929 * @ep: endpoint pointer
1930 *
1931 * check if underlying TCP connection is active
1932 */
1933static int bnx2i_ep_tcp_conn_active(struct bnx2i_endpoint *bnx2i_ep)
1934{
1935 int ret;
1936 int cnic_dev_10g = 0;
1937
1938 if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
1939 cnic_dev_10g = 1;
1940
1941 switch (bnx2i_ep->state) {
Michael Chancf4e6362009-06-08 18:14:44 -07001942 case EP_STATE_CLEANUP_FAILED:
1943 case EP_STATE_OFLD_FAILED:
1944 case EP_STATE_DISCONN_TIMEDOUT:
1945 ret = 0;
1946 break;
Eddie Wai252e44802010-11-23 15:29:25 -08001947 case EP_STATE_CONNECT_START:
Eddie Waiec8933b2011-02-16 15:04:25 -06001948 case EP_STATE_CONNECT_FAILED:
Michael Chancf4e6362009-06-08 18:14:44 -07001949 case EP_STATE_CONNECT_COMPL:
1950 case EP_STATE_ULP_UPDATE_START:
1951 case EP_STATE_ULP_UPDATE_COMPL:
1952 case EP_STATE_TCP_FIN_RCVD:
Eddie Wai2eefb202010-07-01 15:34:54 -07001953 case EP_STATE_LOGOUT_SENT:
1954 case EP_STATE_LOGOUT_RESP_RCVD:
Michael Chancf4e6362009-06-08 18:14:44 -07001955 case EP_STATE_ULP_UPDATE_FAILED:
1956 ret = 1;
1957 break;
1958 case EP_STATE_TCP_RST_RCVD:
Michael Chancf4e6362009-06-08 18:14:44 -07001959 if (cnic_dev_10g)
Michael Chancf4e6362009-06-08 18:14:44 -07001960 ret = 0;
Eddie Wai94810e82010-11-23 15:29:24 -08001961 else
1962 ret = 1;
Michael Chancf4e6362009-06-08 18:14:44 -07001963 break;
1964 default:
1965 ret = 0;
1966 }
1967
1968 return ret;
1969}
1970
1971
Eddie Wai6447f282010-07-01 15:34:50 -07001972/*
1973 * bnx2i_hw_ep_disconnect - executes TCP connection teardown process in the hw
1974 * @ep: TCP connection (bnx2i endpoint) handle
1975 *
1976 * executes TCP connection teardown process
1977 */
1978int bnx2i_hw_ep_disconnect(struct bnx2i_endpoint *bnx2i_ep)
1979{
1980 struct bnx2i_hba *hba = bnx2i_ep->hba;
1981 struct cnic_dev *cnic;
1982 struct iscsi_session *session = NULL;
1983 struct iscsi_conn *conn = NULL;
1984 int ret = 0;
Eddie Wai2eefb202010-07-01 15:34:54 -07001985 int close = 0;
1986 int close_ret = 0;
Eddie Wai6447f282010-07-01 15:34:50 -07001987
1988 if (!hba)
1989 return 0;
1990
1991 cnic = hba->cnic;
1992 if (!cnic)
1993 return 0;
1994
Eddie Waia91031a2010-11-23 15:29:30 -08001995 if (bnx2i_ep->state == EP_STATE_IDLE ||
1996 bnx2i_ep->state == EP_STATE_DISCONN_TIMEDOUT)
Eddie Wai250ae982010-08-12 16:44:30 -07001997 return 0;
1998
Eddie Wai6447f282010-07-01 15:34:50 -07001999 if (!bnx2i_ep_tcp_conn_active(bnx2i_ep))
2000 goto destroy_conn;
2001
2002 if (bnx2i_ep->conn) {
2003 conn = bnx2i_ep->conn->cls_conn->dd_data;
2004 session = conn->session;
2005 }
2006
Eddie Wai6447f282010-07-01 15:34:50 -07002007 init_timer(&bnx2i_ep->ofld_timer);
Eddie Waie37d2c42010-07-01 15:34:53 -07002008 bnx2i_ep->ofld_timer.expires = hba->conn_teardown_tmo + jiffies;
Eddie Wai6447f282010-07-01 15:34:50 -07002009 bnx2i_ep->ofld_timer.function = bnx2i_ep_ofld_timer;
2010 bnx2i_ep->ofld_timer.data = (unsigned long) bnx2i_ep;
2011 add_timer(&bnx2i_ep->ofld_timer);
2012
Eddie Wai2eefb202010-07-01 15:34:54 -07002013 if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic))
Eddie Wai6447f282010-07-01 15:34:50 -07002014 goto out;
2015
Eddie Wai2eefb202010-07-01 15:34:54 -07002016 if (session) {
2017 spin_lock_bh(&session->lock);
2018 if (bnx2i_ep->state != EP_STATE_TCP_FIN_RCVD) {
2019 if (session->state == ISCSI_STATE_LOGGING_OUT) {
2020 if (bnx2i_ep->state == EP_STATE_LOGOUT_SENT) {
2021 /* Logout sent, but no resp */
Eddie Waia91031a2010-11-23 15:29:30 -08002022 printk(KERN_ALERT "bnx2i (%s): WARNING"
2023 " logout response was not "
2024 "received!\n",
2025 bnx2i_ep->hba->netdev->name);
Eddie Wai2eefb202010-07-01 15:34:54 -07002026 } else if (bnx2i_ep->state ==
2027 EP_STATE_LOGOUT_RESP_RCVD)
2028 close = 1;
2029 }
2030 } else
2031 close = 1;
2032
2033 spin_unlock_bh(&session->lock);
2034 }
2035
2036 bnx2i_ep->state = EP_STATE_DISCONN_START;
2037
2038 if (close)
2039 close_ret = cnic->cm_close(bnx2i_ep->cm_sk);
2040 else
2041 close_ret = cnic->cm_abort(bnx2i_ep->cm_sk);
2042
2043 if (close_ret)
Eddie Waia91031a2010-11-23 15:29:30 -08002044 printk(KERN_ALERT "bnx2i (%s): close/abort(%d) returned %d\n",
Eddie Wai2c2255e2010-08-12 16:44:29 -07002045 bnx2i_ep->hba->netdev->name, close, close_ret);
2046 else
2047 /* wait for option-2 conn teardown */
2048 wait_event_interruptible(bnx2i_ep->ofld_wait,
Eddie Wai6447f282010-07-01 15:34:50 -07002049 bnx2i_ep->state != EP_STATE_DISCONN_START);
2050
2051 if (signal_pending(current))
2052 flush_signals(current);
2053 del_timer_sync(&bnx2i_ep->ofld_timer);
2054
2055destroy_conn:
Eddie Wai46012e82010-07-01 15:34:51 -07002056 bnx2i_ep_active_list_del(hba, bnx2i_ep);
Eddie Wai6447f282010-07-01 15:34:50 -07002057 if (bnx2i_tear_down_conn(hba, bnx2i_ep))
Eddie Waia91031a2010-11-23 15:29:30 -08002058 return -EINVAL;
Eddie Wai6447f282010-07-01 15:34:50 -07002059out:
2060 bnx2i_ep->state = EP_STATE_IDLE;
2061 return ret;
2062}
2063
2064
Michael Chancf4e6362009-06-08 18:14:44 -07002065/**
2066 * bnx2i_ep_disconnect - executes TCP connection teardown process
Eddie Wai6447f282010-07-01 15:34:50 -07002067 * @ep: TCP connection (iscsi endpoint) handle
Michael Chancf4e6362009-06-08 18:14:44 -07002068 *
2069 * executes TCP connection teardown process
2070 */
2071static void bnx2i_ep_disconnect(struct iscsi_endpoint *ep)
2072{
2073 struct bnx2i_endpoint *bnx2i_ep;
2074 struct bnx2i_conn *bnx2i_conn = NULL;
Eddie Wai6447f282010-07-01 15:34:50 -07002075 struct iscsi_conn *conn = NULL;
Michael Chancf4e6362009-06-08 18:14:44 -07002076 struct bnx2i_hba *hba;
2077
2078 bnx2i_ep = ep->dd_data;
2079
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02002080 /* driver should not attempt connection cleanup until TCP_CONNECT
Michael Chancf4e6362009-06-08 18:14:44 -07002081 * completes either successfully or fails. Timeout is 9-secs, so
2082 * wait for it to complete
2083 */
2084 while ((bnx2i_ep->state == EP_STATE_CONNECT_START) &&
2085 !time_after(jiffies, bnx2i_ep->timestamp + (12 * HZ)))
2086 msleep(250);
2087
2088 if (bnx2i_ep->conn) {
2089 bnx2i_conn = bnx2i_ep->conn;
2090 conn = bnx2i_conn->cls_conn->dd_data;
Mike Christie24246de2009-11-11 16:34:30 -06002091 iscsi_suspend_queue(conn);
Michael Chancf4e6362009-06-08 18:14:44 -07002092 }
Michael Chancf4e6362009-06-08 18:14:44 -07002093 hba = bnx2i_ep->hba;
Michael Chancf4e6362009-06-08 18:14:44 -07002094
2095 mutex_lock(&hba->net_dev_lock);
2096
Eddie Waia91031a2010-11-23 15:29:30 -08002097 if (bnx2i_ep->state == EP_STATE_DISCONN_TIMEDOUT)
2098 goto out;
2099
Eddie Wai6447f282010-07-01 15:34:50 -07002100 if (bnx2i_ep->state == EP_STATE_IDLE)
Michael Chancf4e6362009-06-08 18:14:44 -07002101 goto free_resc;
Eddie Wai6447f282010-07-01 15:34:50 -07002102
Eddie Waia91031a2010-11-23 15:29:30 -08002103 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) ||
2104 (bnx2i_ep->hba_age != hba->age)) {
2105 bnx2i_ep_active_list_del(hba, bnx2i_ep);
Michael Chancf4e6362009-06-08 18:14:44 -07002106 goto free_resc;
Eddie Waia91031a2010-11-23 15:29:30 -08002107 }
Michael Chancf4e6362009-06-08 18:14:44 -07002108
Eddie Wai6447f282010-07-01 15:34:50 -07002109 /* Do all chip cleanup here */
2110 if (bnx2i_hw_ep_disconnect(bnx2i_ep)) {
Michael Chancf4e6362009-06-08 18:14:44 -07002111 mutex_unlock(&hba->net_dev_lock);
2112 return;
2113 }
2114free_resc:
Michael Chancf4e6362009-06-08 18:14:44 -07002115 bnx2i_free_qp_resc(hba, bnx2i_ep);
Eddie Waia91031a2010-11-23 15:29:30 -08002116
Michael Chancf4e6362009-06-08 18:14:44 -07002117 if (bnx2i_conn)
2118 bnx2i_conn->ep = NULL;
2119
2120 bnx2i_free_ep(ep);
Eddie Waia91031a2010-11-23 15:29:30 -08002121out:
Eddie Wai6447f282010-07-01 15:34:50 -07002122 mutex_unlock(&hba->net_dev_lock);
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -07002123
2124 wake_up_interruptible(&hba->eh_wait);
Michael Chancf4e6362009-06-08 18:14:44 -07002125}
2126
2127
2128/**
2129 * bnx2i_nl_set_path - ISCSI_UEVENT_PATH_UPDATE user message handler
2130 * @buf: pointer to buffer containing iscsi path message
2131 *
2132 */
2133static int bnx2i_nl_set_path(struct Scsi_Host *shost, struct iscsi_path *params)
2134{
2135 struct bnx2i_hba *hba = iscsi_host_priv(shost);
2136 char *buf = (char *) params;
2137 u16 len = sizeof(*params);
2138
2139 /* handled by cnic driver */
2140 hba->cnic->iscsi_nl_msg_recv(hba->cnic, ISCSI_UEVENT_PATH_UPDATE, buf,
2141 len);
2142
2143 return 0;
2144}
2145
2146
2147/*
2148 * 'Scsi_Host_Template' structure and 'iscsi_tranport' structure template
2149 * used while registering with the scsi host and iSCSI transport module.
2150 */
2151static struct scsi_host_template bnx2i_host_template = {
2152 .module = THIS_MODULE,
2153 .name = "Broadcom Offload iSCSI Initiator",
2154 .proc_name = "bnx2i",
2155 .queuecommand = iscsi_queuecommand,
2156 .eh_abort_handler = iscsi_eh_abort,
2157 .eh_device_reset_handler = iscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302158 .eh_target_reset_handler = iscsi_eh_recover_target,
Mike Christie9f9127f2010-02-10 16:51:46 -06002159 .change_queue_depth = iscsi_change_queue_depth,
Michael Chancf4e6362009-06-08 18:14:44 -07002160 .can_queue = 1024,
2161 .max_sectors = 127,
2162 .cmd_per_lun = 32,
2163 .this_id = -1,
2164 .use_clustering = ENABLE_CLUSTERING,
2165 .sg_tablesize = ISCSI_MAX_BDS_PER_CMD,
2166 .shost_attrs = bnx2i_dev_attributes,
2167};
2168
2169struct iscsi_transport bnx2i_iscsi_transport = {
2170 .owner = THIS_MODULE,
2171 .name = "bnx2i",
2172 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST |
2173 CAP_MULTI_R2T | CAP_DATADGST |
Eddie Wai09813ba2011-02-16 15:04:30 -06002174 CAP_DATA_PATH_OFFLOAD |
2175 CAP_TEXT_NEGO,
Michael Chancf4e6362009-06-08 18:14:44 -07002176 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2177 ISCSI_MAX_XMIT_DLENGTH |
2178 ISCSI_HDRDGST_EN |
2179 ISCSI_DATADGST_EN |
2180 ISCSI_INITIAL_R2T_EN |
2181 ISCSI_MAX_R2T |
2182 ISCSI_IMM_DATA_EN |
2183 ISCSI_FIRST_BURST |
2184 ISCSI_MAX_BURST |
2185 ISCSI_PDU_INORDER_EN |
2186 ISCSI_DATASEQ_INORDER_EN |
2187 ISCSI_ERL |
2188 ISCSI_CONN_PORT |
2189 ISCSI_CONN_ADDRESS |
2190 ISCSI_EXP_STATSN |
2191 ISCSI_PERSISTENT_PORT |
2192 ISCSI_PERSISTENT_ADDRESS |
2193 ISCSI_TARGET_NAME | ISCSI_TPGT |
2194 ISCSI_USERNAME | ISCSI_PASSWORD |
2195 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
2196 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
Mike Christie3fe5ae82009-11-11 16:34:33 -06002197 ISCSI_LU_RESET_TMO | ISCSI_TGT_RESET_TMO |
Michael Chancf4e6362009-06-08 18:14:44 -07002198 ISCSI_PING_TMO | ISCSI_RECV_TMO |
2199 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
Michael Chan625986c2010-07-01 15:34:55 -07002200 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
2201 ISCSI_HOST_NETDEV_NAME,
Michael Chancf4e6362009-06-08 18:14:44 -07002202 .create_session = bnx2i_session_create,
2203 .destroy_session = bnx2i_session_destroy,
2204 .create_conn = bnx2i_conn_create,
2205 .bind_conn = bnx2i_conn_bind,
2206 .destroy_conn = bnx2i_conn_destroy,
2207 .set_param = iscsi_set_param,
Mike Christied8585bc2011-02-16 15:04:39 -06002208 .get_conn_param = iscsi_conn_get_param,
Michael Chancf4e6362009-06-08 18:14:44 -07002209 .get_session_param = iscsi_session_get_param,
2210 .get_host_param = bnx2i_host_get_param,
2211 .start_conn = bnx2i_conn_start,
2212 .stop_conn = iscsi_conn_stop,
2213 .send_pdu = iscsi_conn_send_pdu,
2214 .xmit_task = bnx2i_task_xmit,
2215 .get_stats = bnx2i_conn_get_stats,
2216 /* TCP connect - disconnect - option-2 interface calls */
Mike Christied8585bc2011-02-16 15:04:39 -06002217 .get_ep_param = bnx2i_ep_get_param,
Michael Chancf4e6362009-06-08 18:14:44 -07002218 .ep_connect = bnx2i_ep_connect,
2219 .ep_poll = bnx2i_ep_poll,
2220 .ep_disconnect = bnx2i_ep_disconnect,
2221 .set_path = bnx2i_nl_set_path,
2222 /* Error recovery timeout call */
2223 .session_recovery_timedout = iscsi_session_recovery_timedout,
2224 .cleanup_task = bnx2i_cleanup_task,
2225};