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