blob: fb5fe88de90fddf24d44b7b34b6dc85c8befad0a [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;
Eddie Wai46012e82010-07-01 15:34:51 -0700389 bnx2i_ep->cls_ep = ep;
Michael Chancf4e6362009-06-08 18:14:44 -0700390 INIT_LIST_HEAD(&bnx2i_ep->link);
391 bnx2i_ep->state = EP_STATE_IDLE;
Anil Veerabhadrappac19dcd02009-07-29 21:50:11 -0700392 bnx2i_ep->ep_iscsi_cid = (u16) -1;
Michael Chancf4e6362009-06-08 18:14:44 -0700393 bnx2i_ep->hba = hba;
394 bnx2i_ep->hba_age = hba->age;
395 hba->ofld_conns_active++;
396 init_waitqueue_head(&bnx2i_ep->ofld_wait);
397 return ep;
398}
399
400
401/**
402 * bnx2i_free_ep - free endpoint
403 * @ep: pointer to iscsi endpoint structure
404 */
405static void bnx2i_free_ep(struct iscsi_endpoint *ep)
406{
407 struct bnx2i_endpoint *bnx2i_ep = ep->dd_data;
408 unsigned long flags;
409
410 spin_lock_irqsave(&bnx2i_resc_lock, flags);
411 bnx2i_ep->state = EP_STATE_IDLE;
412 bnx2i_ep->hba->ofld_conns_active--;
413
414 bnx2i_free_iscsi_cid(bnx2i_ep->hba, bnx2i_ep->ep_iscsi_cid);
415 if (bnx2i_ep->conn) {
416 bnx2i_ep->conn->ep = NULL;
417 bnx2i_ep->conn = NULL;
418 }
419
420 bnx2i_ep->hba = NULL;
421 spin_unlock_irqrestore(&bnx2i_resc_lock, flags);
422 iscsi_destroy_endpoint(ep);
423}
424
425
426/**
427 * bnx2i_alloc_bdt - allocates buffer descriptor (BD) table for the command
428 * @hba: adapter instance pointer
429 * @session: iscsi session pointer
430 * @cmd: iscsi command structure
431 */
432static int bnx2i_alloc_bdt(struct bnx2i_hba *hba, struct iscsi_session *session,
433 struct bnx2i_cmd *cmd)
434{
435 struct io_bdt *io = &cmd->io_tbl;
436 struct iscsi_bd *bd;
437
438 io->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
439 ISCSI_MAX_BDS_PER_CMD * sizeof(*bd),
440 &io->bd_tbl_dma, GFP_KERNEL);
441 if (!io->bd_tbl) {
442 iscsi_session_printk(KERN_ERR, session, "Could not "
443 "allocate bdt.\n");
444 return -ENOMEM;
445 }
446 io->bd_valid = 0;
447 return 0;
448}
449
450/**
451 * bnx2i_destroy_cmd_pool - destroys iscsi command pool and release BD table
452 * @hba: adapter instance pointer
453 * @session: iscsi session pointer
454 * @cmd: iscsi command structure
455 */
456static void bnx2i_destroy_cmd_pool(struct bnx2i_hba *hba,
457 struct iscsi_session *session)
458{
459 int i;
460
461 for (i = 0; i < session->cmds_max; i++) {
462 struct iscsi_task *task = session->cmds[i];
463 struct bnx2i_cmd *cmd = task->dd_data;
464
465 if (cmd->io_tbl.bd_tbl)
466 dma_free_coherent(&hba->pcidev->dev,
467 ISCSI_MAX_BDS_PER_CMD *
468 sizeof(struct iscsi_bd),
469 cmd->io_tbl.bd_tbl,
470 cmd->io_tbl.bd_tbl_dma);
471 }
472
473}
474
475
476/**
477 * bnx2i_setup_cmd_pool - sets up iscsi command pool for the session
478 * @hba: adapter instance pointer
479 * @session: iscsi session pointer
480 */
481static int bnx2i_setup_cmd_pool(struct bnx2i_hba *hba,
482 struct iscsi_session *session)
483{
484 int i;
485
486 for (i = 0; i < session->cmds_max; i++) {
487 struct iscsi_task *task = session->cmds[i];
488 struct bnx2i_cmd *cmd = task->dd_data;
489
Michael Chancf4e6362009-06-08 18:14:44 -0700490 task->hdr = &cmd->hdr;
491 task->hdr_max = sizeof(struct iscsi_hdr);
492
493 if (bnx2i_alloc_bdt(hba, session, cmd))
494 goto free_bdts;
495 }
496
497 return 0;
498
499free_bdts:
500 bnx2i_destroy_cmd_pool(hba, session);
501 return -ENOMEM;
502}
503
504
505/**
506 * bnx2i_setup_mp_bdt - allocate BD table resources
507 * @hba: pointer to adapter structure
508 *
509 * Allocate memory for dummy buffer and associated BD
510 * table to be used by middle path (MP) requests
511 */
512static int bnx2i_setup_mp_bdt(struct bnx2i_hba *hba)
513{
514 int rc = 0;
515 struct iscsi_bd *mp_bdt;
516 u64 addr;
517
518 hba->mp_bd_tbl = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
519 &hba->mp_bd_dma, GFP_KERNEL);
520 if (!hba->mp_bd_tbl) {
521 printk(KERN_ERR "unable to allocate Middle Path BDT\n");
522 rc = -1;
523 goto out;
524 }
525
526 hba->dummy_buffer = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
527 &hba->dummy_buf_dma, GFP_KERNEL);
528 if (!hba->dummy_buffer) {
529 printk(KERN_ERR "unable to alloc Middle Path Dummy Buffer\n");
530 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
531 hba->mp_bd_tbl, hba->mp_bd_dma);
532 hba->mp_bd_tbl = NULL;
533 rc = -1;
534 goto out;
535 }
536
537 mp_bdt = (struct iscsi_bd *) hba->mp_bd_tbl;
538 addr = (unsigned long) hba->dummy_buf_dma;
539 mp_bdt->buffer_addr_lo = addr & 0xffffffff;
540 mp_bdt->buffer_addr_hi = addr >> 32;
541 mp_bdt->buffer_length = PAGE_SIZE;
542 mp_bdt->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
543 ISCSI_BD_FIRST_IN_BD_CHAIN;
544out:
545 return rc;
546}
547
548
549/**
550 * bnx2i_free_mp_bdt - releases ITT back to free pool
551 * @hba: pointer to adapter instance
552 *
553 * free MP dummy buffer and associated BD table
554 */
555static void bnx2i_free_mp_bdt(struct bnx2i_hba *hba)
556{
557 if (hba->mp_bd_tbl) {
558 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
559 hba->mp_bd_tbl, hba->mp_bd_dma);
560 hba->mp_bd_tbl = NULL;
561 }
562 if (hba->dummy_buffer) {
563 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
564 hba->dummy_buffer, hba->dummy_buf_dma);
565 hba->dummy_buffer = NULL;
566 }
567 return;
568}
569
570/**
571 * bnx2i_drop_session - notifies iscsid of connection error.
572 * @hba: adapter instance pointer
573 * @session: iscsi session pointer
574 *
575 * This notifies iscsid that there is a error, so it can initiate
576 * recovery.
577 *
578 * This relies on caller using the iscsi class iterator so the object
579 * is refcounted and does not disapper from under us.
580 */
581void bnx2i_drop_session(struct iscsi_cls_session *cls_session)
582{
583 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_CONN_FAILED);
584}
585
586/**
587 * bnx2i_ep_destroy_list_add - add an entry to EP destroy list
588 * @hba: pointer to adapter instance
589 * @ep: pointer to endpoint (transport indentifier) structure
590 *
591 * EP destroy queue manager
592 */
593static int bnx2i_ep_destroy_list_add(struct bnx2i_hba *hba,
594 struct bnx2i_endpoint *ep)
595{
596 write_lock_bh(&hba->ep_rdwr_lock);
597 list_add_tail(&ep->link, &hba->ep_destroy_list);
598 write_unlock_bh(&hba->ep_rdwr_lock);
599 return 0;
600}
601
602/**
603 * bnx2i_ep_destroy_list_del - add an entry to EP destroy list
604 *
605 * @hba: pointer to adapter instance
606 * @ep: pointer to endpoint (transport indentifier) structure
607 *
608 * EP destroy queue manager
609 */
610static int bnx2i_ep_destroy_list_del(struct bnx2i_hba *hba,
611 struct bnx2i_endpoint *ep)
612{
613 write_lock_bh(&hba->ep_rdwr_lock);
614 list_del_init(&ep->link);
615 write_unlock_bh(&hba->ep_rdwr_lock);
616
617 return 0;
618}
619
620/**
621 * bnx2i_ep_ofld_list_add - add an entry to ep offload pending list
622 * @hba: pointer to adapter instance
623 * @ep: pointer to endpoint (transport indentifier) structure
624 *
625 * pending conn offload completion queue manager
626 */
627static int bnx2i_ep_ofld_list_add(struct bnx2i_hba *hba,
628 struct bnx2i_endpoint *ep)
629{
630 write_lock_bh(&hba->ep_rdwr_lock);
631 list_add_tail(&ep->link, &hba->ep_ofld_list);
632 write_unlock_bh(&hba->ep_rdwr_lock);
633 return 0;
634}
635
636/**
637 * bnx2i_ep_ofld_list_del - add an entry to ep offload pending list
638 * @hba: pointer to adapter instance
639 * @ep: pointer to endpoint (transport indentifier) structure
640 *
641 * pending conn offload completion queue manager
642 */
643static int bnx2i_ep_ofld_list_del(struct bnx2i_hba *hba,
644 struct bnx2i_endpoint *ep)
645{
646 write_lock_bh(&hba->ep_rdwr_lock);
647 list_del_init(&ep->link);
648 write_unlock_bh(&hba->ep_rdwr_lock);
649 return 0;
650}
651
652
653/**
654 * bnx2i_find_ep_in_ofld_list - find iscsi_cid in pending list of endpoints
655 *
656 * @hba: pointer to adapter instance
657 * @iscsi_cid: iscsi context ID to find
658 *
659 */
660struct bnx2i_endpoint *
661bnx2i_find_ep_in_ofld_list(struct bnx2i_hba *hba, u32 iscsi_cid)
662{
663 struct list_head *list;
664 struct list_head *tmp;
665 struct bnx2i_endpoint *ep;
666
667 read_lock_bh(&hba->ep_rdwr_lock);
668 list_for_each_safe(list, tmp, &hba->ep_ofld_list) {
669 ep = (struct bnx2i_endpoint *)list;
670
671 if (ep->ep_iscsi_cid == iscsi_cid)
672 break;
673 ep = NULL;
674 }
675 read_unlock_bh(&hba->ep_rdwr_lock);
676
677 if (!ep)
678 printk(KERN_ERR "l5 cid %d not found\n", iscsi_cid);
679 return ep;
680}
681
Michael Chancf4e6362009-06-08 18:14:44 -0700682/**
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
Eddie Wai46012e82010-07-01 15:34:51 -0700711
712/**
713 * bnx2i_ep_active_list_add - add an entry to ep active list
714 * @hba: pointer to adapter instance
715 * @ep: pointer to endpoint (transport indentifier) structure
716 *
717 * current active conn queue manager
718 */
719static void bnx2i_ep_active_list_add(struct bnx2i_hba *hba,
720 struct bnx2i_endpoint *ep)
721{
722 write_lock_bh(&hba->ep_rdwr_lock);
723 list_add_tail(&ep->link, &hba->ep_active_list);
724 write_unlock_bh(&hba->ep_rdwr_lock);
725}
726
727
728/**
729 * bnx2i_ep_active_list_del - deletes an entry to ep active list
730 * @hba: pointer to adapter instance
731 * @ep: pointer to endpoint (transport indentifier) structure
732 *
733 * current active conn queue manager
734 */
735static void bnx2i_ep_active_list_del(struct bnx2i_hba *hba,
736 struct bnx2i_endpoint *ep)
737{
738 write_lock_bh(&hba->ep_rdwr_lock);
739 list_del_init(&ep->link);
740 write_unlock_bh(&hba->ep_rdwr_lock);
741}
742
743
Michael Chancf4e6362009-06-08 18:14:44 -0700744/**
745 * bnx2i_setup_host_queue_size - assigns shost->can_queue param
746 * @hba: pointer to adapter instance
747 * @shost: scsi host pointer
748 *
749 * Initializes 'can_queue' parameter based on how many outstanding commands
750 * the device can handle. Each device 5708/5709/57710 has different
751 * capabilities
752 */
753static void bnx2i_setup_host_queue_size(struct bnx2i_hba *hba,
754 struct Scsi_Host *shost)
755{
756 if (test_bit(BNX2I_NX2_DEV_5708, &hba->cnic_dev_type))
757 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5708;
758 else if (test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type))
759 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5709;
760 else if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type))
761 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_57710;
762 else
763 shost->can_queue = ISCSI_MAX_CMDS_PER_HBA_5708;
764}
765
766
767/**
768 * bnx2i_alloc_hba - allocate and init adapter instance
769 * @cnic: cnic device pointer
770 *
771 * allocate & initialize adapter structure and call other
772 * support routines to do per adapter initialization
773 */
774struct bnx2i_hba *bnx2i_alloc_hba(struct cnic_dev *cnic)
775{
776 struct Scsi_Host *shost;
777 struct bnx2i_hba *hba;
778
779 shost = iscsi_host_alloc(&bnx2i_host_template, sizeof(*hba), 0);
780 if (!shost)
781 return NULL;
782 shost->dma_boundary = cnic->pcidev->dma_mask;
783 shost->transportt = bnx2i_scsi_xport_template;
784 shost->max_id = ISCSI_MAX_CONNS_PER_HBA;
785 shost->max_channel = 0;
786 shost->max_lun = 512;
787 shost->max_cmd_len = 16;
788
789 hba = iscsi_host_priv(shost);
790 hba->shost = shost;
791 hba->netdev = cnic->netdev;
792 /* Get PCI related information and update hba struct members */
793 hba->pcidev = cnic->pcidev;
794 pci_dev_get(hba->pcidev);
795 hba->pci_did = hba->pcidev->device;
796 hba->pci_vid = hba->pcidev->vendor;
797 hba->pci_sdid = hba->pcidev->subsystem_device;
798 hba->pci_svid = hba->pcidev->subsystem_vendor;
799 hba->pci_func = PCI_FUNC(hba->pcidev->devfn);
800 hba->pci_devno = PCI_SLOT(hba->pcidev->devfn);
Michael Chancf4e6362009-06-08 18:14:44 -0700801
802 bnx2i_identify_device(hba);
803 bnx2i_setup_host_queue_size(hba, shost);
804
805 if (test_bit(BNX2I_NX2_DEV_5709, &hba->cnic_dev_type)) {
806 hba->regview = ioremap_nocache(hba->netdev->base_addr,
807 BNX2_MQ_CONFIG2);
808 if (!hba->regview)
809 goto ioreg_map_err;
810 } else if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
811 hba->regview = ioremap_nocache(hba->netdev->base_addr, 4096);
812 if (!hba->regview)
813 goto ioreg_map_err;
814 }
815
816 if (bnx2i_setup_mp_bdt(hba))
817 goto mp_bdt_mem_err;
818
819 INIT_LIST_HEAD(&hba->ep_ofld_list);
Eddie Wai46012e82010-07-01 15:34:51 -0700820 INIT_LIST_HEAD(&hba->ep_active_list);
Michael Chancf4e6362009-06-08 18:14:44 -0700821 INIT_LIST_HEAD(&hba->ep_destroy_list);
822 rwlock_init(&hba->ep_rdwr_lock);
823
824 hba->mtu_supported = BNX2I_MAX_MTU_SUPPORTED;
825
826 /* different values for 5708/5709/57710 */
827 hba->max_active_conns = ISCSI_MAX_CONNS_PER_HBA;
828
829 if (bnx2i_setup_free_cid_que(hba))
830 goto cid_que_err;
831
832 /* SQ/RQ/CQ size can be changed via sysfx interface */
833 if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
834 if (sq_size && sq_size <= BNX2I_5770X_SQ_WQES_MAX)
835 hba->max_sqes = sq_size;
836 else
837 hba->max_sqes = BNX2I_5770X_SQ_WQES_DEFAULT;
838 } else { /* 5706/5708/5709 */
839 if (sq_size && sq_size <= BNX2I_570X_SQ_WQES_MAX)
840 hba->max_sqes = sq_size;
841 else
842 hba->max_sqes = BNX2I_570X_SQ_WQES_DEFAULT;
843 }
844
845 hba->max_rqes = rq_size;
846 hba->max_cqes = hba->max_sqes + rq_size;
847 if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type)) {
848 if (hba->max_cqes > BNX2I_5770X_CQ_WQES_MAX)
849 hba->max_cqes = BNX2I_5770X_CQ_WQES_MAX;
850 } else if (hba->max_cqes > BNX2I_570X_CQ_WQES_MAX)
851 hba->max_cqes = BNX2I_570X_CQ_WQES_MAX;
852
853 hba->num_ccell = hba->max_sqes / 2;
854
855 spin_lock_init(&hba->lock);
856 mutex_init(&hba->net_dev_lock);
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -0700857 init_waitqueue_head(&hba->eh_wait);
858 if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type))
859 hba->hba_shutdown_tmo = 240 * HZ;
860 else /* 5706/5708/5709 */
861 hba->hba_shutdown_tmo = 30 * HZ;
Michael Chancf4e6362009-06-08 18:14:44 -0700862
863 if (iscsi_host_add(shost, &hba->pcidev->dev))
864 goto free_dump_mem;
865 return hba;
866
867free_dump_mem:
868 bnx2i_release_free_cid_que(hba);
869cid_que_err:
870 bnx2i_free_mp_bdt(hba);
871mp_bdt_mem_err:
872 if (hba->regview) {
873 iounmap(hba->regview);
874 hba->regview = NULL;
875 }
876ioreg_map_err:
877 pci_dev_put(hba->pcidev);
878 scsi_host_put(shost);
879 return NULL;
880}
881
882/**
883 * bnx2i_free_hba- releases hba structure and resources held by the adapter
884 * @hba: pointer to adapter instance
885 *
886 * free adapter structure and call various cleanup routines.
887 */
888void bnx2i_free_hba(struct bnx2i_hba *hba)
889{
890 struct Scsi_Host *shost = hba->shost;
891
892 iscsi_host_remove(shost);
893 INIT_LIST_HEAD(&hba->ep_ofld_list);
Eddie Wai46012e82010-07-01 15:34:51 -0700894 INIT_LIST_HEAD(&hba->ep_active_list);
Michael Chancf4e6362009-06-08 18:14:44 -0700895 INIT_LIST_HEAD(&hba->ep_destroy_list);
896 pci_dev_put(hba->pcidev);
897
898 if (hba->regview) {
899 iounmap(hba->regview);
900 hba->regview = NULL;
901 }
902 bnx2i_free_mp_bdt(hba);
903 bnx2i_release_free_cid_que(hba);
904 iscsi_host_free(shost);
905}
906
907/**
908 * bnx2i_conn_free_login_resources - free DMA resources used for login process
909 * @hba: pointer to adapter instance
910 * @bnx2i_conn: iscsi connection pointer
911 *
912 * Login related resources, mostly BDT & payload DMA memory is freed
913 */
914static void bnx2i_conn_free_login_resources(struct bnx2i_hba *hba,
915 struct bnx2i_conn *bnx2i_conn)
916{
917 if (bnx2i_conn->gen_pdu.resp_bd_tbl) {
918 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
919 bnx2i_conn->gen_pdu.resp_bd_tbl,
920 bnx2i_conn->gen_pdu.resp_bd_dma);
921 bnx2i_conn->gen_pdu.resp_bd_tbl = NULL;
922 }
923
924 if (bnx2i_conn->gen_pdu.req_bd_tbl) {
925 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
926 bnx2i_conn->gen_pdu.req_bd_tbl,
927 bnx2i_conn->gen_pdu.req_bd_dma);
928 bnx2i_conn->gen_pdu.req_bd_tbl = NULL;
929 }
930
931 if (bnx2i_conn->gen_pdu.resp_buf) {
932 dma_free_coherent(&hba->pcidev->dev,
933 ISCSI_DEF_MAX_RECV_SEG_LEN,
934 bnx2i_conn->gen_pdu.resp_buf,
935 bnx2i_conn->gen_pdu.resp_dma_addr);
936 bnx2i_conn->gen_pdu.resp_buf = NULL;
937 }
938
939 if (bnx2i_conn->gen_pdu.req_buf) {
940 dma_free_coherent(&hba->pcidev->dev,
941 ISCSI_DEF_MAX_RECV_SEG_LEN,
942 bnx2i_conn->gen_pdu.req_buf,
943 bnx2i_conn->gen_pdu.req_dma_addr);
944 bnx2i_conn->gen_pdu.req_buf = NULL;
945 }
946}
947
948/**
949 * bnx2i_conn_alloc_login_resources - alloc DMA resources for login/nop.
950 * @hba: pointer to adapter instance
951 * @bnx2i_conn: iscsi connection pointer
952 *
953 * Mgmt task DNA resources are allocated in this routine.
954 */
955static int bnx2i_conn_alloc_login_resources(struct bnx2i_hba *hba,
956 struct bnx2i_conn *bnx2i_conn)
957{
958 /* Allocate memory for login request/response buffers */
959 bnx2i_conn->gen_pdu.req_buf =
960 dma_alloc_coherent(&hba->pcidev->dev,
961 ISCSI_DEF_MAX_RECV_SEG_LEN,
962 &bnx2i_conn->gen_pdu.req_dma_addr,
963 GFP_KERNEL);
964 if (bnx2i_conn->gen_pdu.req_buf == NULL)
965 goto login_req_buf_failure;
966
967 bnx2i_conn->gen_pdu.req_buf_size = 0;
968 bnx2i_conn->gen_pdu.req_wr_ptr = bnx2i_conn->gen_pdu.req_buf;
969
970 bnx2i_conn->gen_pdu.resp_buf =
971 dma_alloc_coherent(&hba->pcidev->dev,
972 ISCSI_DEF_MAX_RECV_SEG_LEN,
973 &bnx2i_conn->gen_pdu.resp_dma_addr,
974 GFP_KERNEL);
975 if (bnx2i_conn->gen_pdu.resp_buf == NULL)
976 goto login_resp_buf_failure;
977
978 bnx2i_conn->gen_pdu.resp_buf_size = ISCSI_DEF_MAX_RECV_SEG_LEN;
979 bnx2i_conn->gen_pdu.resp_wr_ptr = bnx2i_conn->gen_pdu.resp_buf;
980
981 bnx2i_conn->gen_pdu.req_bd_tbl =
982 dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
983 &bnx2i_conn->gen_pdu.req_bd_dma, GFP_KERNEL);
984 if (bnx2i_conn->gen_pdu.req_bd_tbl == NULL)
985 goto login_req_bd_tbl_failure;
986
987 bnx2i_conn->gen_pdu.resp_bd_tbl =
988 dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
989 &bnx2i_conn->gen_pdu.resp_bd_dma,
990 GFP_KERNEL);
991 if (bnx2i_conn->gen_pdu.resp_bd_tbl == NULL)
992 goto login_resp_bd_tbl_failure;
993
994 return 0;
995
996login_resp_bd_tbl_failure:
997 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
998 bnx2i_conn->gen_pdu.req_bd_tbl,
999 bnx2i_conn->gen_pdu.req_bd_dma);
1000 bnx2i_conn->gen_pdu.req_bd_tbl = NULL;
1001
1002login_req_bd_tbl_failure:
1003 dma_free_coherent(&hba->pcidev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
1004 bnx2i_conn->gen_pdu.resp_buf,
1005 bnx2i_conn->gen_pdu.resp_dma_addr);
1006 bnx2i_conn->gen_pdu.resp_buf = NULL;
1007login_resp_buf_failure:
1008 dma_free_coherent(&hba->pcidev->dev, ISCSI_DEF_MAX_RECV_SEG_LEN,
1009 bnx2i_conn->gen_pdu.req_buf,
1010 bnx2i_conn->gen_pdu.req_dma_addr);
1011 bnx2i_conn->gen_pdu.req_buf = NULL;
1012login_req_buf_failure:
1013 iscsi_conn_printk(KERN_ERR, bnx2i_conn->cls_conn->dd_data,
1014 "login resource alloc failed!!\n");
1015 return -ENOMEM;
1016
1017}
1018
1019
1020/**
1021 * bnx2i_iscsi_prep_generic_pdu_bd - prepares BD table.
1022 * @bnx2i_conn: iscsi connection pointer
1023 *
1024 * Allocates buffers and BD tables before shipping requests to cnic
1025 * for PDUs prepared by 'iscsid' daemon
1026 */
1027static void bnx2i_iscsi_prep_generic_pdu_bd(struct bnx2i_conn *bnx2i_conn)
1028{
1029 struct iscsi_bd *bd_tbl;
1030
1031 bd_tbl = (struct iscsi_bd *) bnx2i_conn->gen_pdu.req_bd_tbl;
1032
1033 bd_tbl->buffer_addr_hi =
1034 (u32) ((u64) bnx2i_conn->gen_pdu.req_dma_addr >> 32);
1035 bd_tbl->buffer_addr_lo = (u32) bnx2i_conn->gen_pdu.req_dma_addr;
1036 bd_tbl->buffer_length = bnx2i_conn->gen_pdu.req_wr_ptr -
1037 bnx2i_conn->gen_pdu.req_buf;
1038 bd_tbl->reserved0 = 0;
1039 bd_tbl->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
1040 ISCSI_BD_FIRST_IN_BD_CHAIN;
1041
1042 bd_tbl = (struct iscsi_bd *) bnx2i_conn->gen_pdu.resp_bd_tbl;
1043 bd_tbl->buffer_addr_hi = (u64) bnx2i_conn->gen_pdu.resp_dma_addr >> 32;
1044 bd_tbl->buffer_addr_lo = (u32) bnx2i_conn->gen_pdu.resp_dma_addr;
1045 bd_tbl->buffer_length = ISCSI_DEF_MAX_RECV_SEG_LEN;
1046 bd_tbl->reserved0 = 0;
1047 bd_tbl->flags = ISCSI_BD_LAST_IN_BD_CHAIN |
1048 ISCSI_BD_FIRST_IN_BD_CHAIN;
1049}
1050
1051
1052/**
1053 * bnx2i_iscsi_send_generic_request - called to send mgmt tasks.
1054 * @task: transport layer task pointer
1055 *
1056 * called to transmit PDUs prepared by the 'iscsid' daemon. iSCSI login,
1057 * Nop-out and Logout requests flow through this path.
1058 */
1059static int bnx2i_iscsi_send_generic_request(struct iscsi_task *task)
1060{
1061 struct bnx2i_cmd *cmd = task->dd_data;
1062 struct bnx2i_conn *bnx2i_conn = cmd->conn;
1063 int rc = 0;
1064 char *buf;
1065 int data_len;
1066
1067 bnx2i_iscsi_prep_generic_pdu_bd(bnx2i_conn);
1068 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
1069 case ISCSI_OP_LOGIN:
1070 bnx2i_send_iscsi_login(bnx2i_conn, task);
1071 break;
1072 case ISCSI_OP_NOOP_OUT:
1073 data_len = bnx2i_conn->gen_pdu.req_buf_size;
1074 buf = bnx2i_conn->gen_pdu.req_buf;
1075 if (data_len)
1076 rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
1077 RESERVED_ITT,
1078 buf, data_len, 1);
1079 else
1080 rc = bnx2i_send_iscsi_nopout(bnx2i_conn, task,
1081 RESERVED_ITT,
1082 NULL, 0, 1);
1083 break;
1084 case ISCSI_OP_LOGOUT:
1085 rc = bnx2i_send_iscsi_logout(bnx2i_conn, task);
1086 break;
1087 case ISCSI_OP_SCSI_TMFUNC:
1088 rc = bnx2i_send_iscsi_tmf(bnx2i_conn, task);
1089 break;
1090 default:
1091 iscsi_conn_printk(KERN_ALERT, bnx2i_conn->cls_conn->dd_data,
1092 "send_gen: unsupported op 0x%x\n",
1093 task->hdr->opcode);
1094 }
1095 return rc;
1096}
1097
1098
1099/**********************************************************************
1100 * SCSI-ML Interface
1101 **********************************************************************/
1102
1103/**
1104 * bnx2i_cpy_scsi_cdb - copies LUN & CDB fields in required format to sq wqe
1105 * @sc: SCSI-ML command pointer
1106 * @cmd: iscsi cmd pointer
1107 */
1108static void bnx2i_cpy_scsi_cdb(struct scsi_cmnd *sc, struct bnx2i_cmd *cmd)
1109{
1110 u32 dword;
1111 int lpcnt;
1112 u8 *srcp;
1113 u32 *dstp;
1114 u32 scsi_lun[2];
1115
1116 int_to_scsilun(sc->device->lun, (struct scsi_lun *) scsi_lun);
1117 cmd->req.lun[0] = be32_to_cpu(scsi_lun[0]);
1118 cmd->req.lun[1] = be32_to_cpu(scsi_lun[1]);
1119
1120 lpcnt = cmd->scsi_cmd->cmd_len / sizeof(dword);
1121 srcp = (u8 *) sc->cmnd;
1122 dstp = (u32 *) cmd->req.cdb;
1123 while (lpcnt--) {
1124 memcpy(&dword, (const void *) srcp, 4);
1125 *dstp = cpu_to_be32(dword);
1126 srcp += 4;
1127 dstp++;
1128 }
1129 if (sc->cmd_len & 0x3) {
1130 dword = (u32) srcp[0] | ((u32) srcp[1] << 8);
1131 *dstp = cpu_to_be32(dword);
1132 }
1133}
1134
1135static void bnx2i_cleanup_task(struct iscsi_task *task)
1136{
1137 struct iscsi_conn *conn = task->conn;
1138 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1139 struct bnx2i_hba *hba = bnx2i_conn->hba;
1140
1141 /*
1142 * mgmt task or cmd was never sent to us to transmit.
1143 */
1144 if (!task->sc || task->state == ISCSI_TASK_PENDING)
1145 return;
1146 /*
1147 * need to clean-up task context to claim dma buffers
1148 */
1149 if (task->state == ISCSI_TASK_ABRT_TMF) {
1150 bnx2i_send_cmd_cleanup_req(hba, task->dd_data);
1151
1152 spin_unlock_bh(&conn->session->lock);
1153 wait_for_completion_timeout(&bnx2i_conn->cmd_cleanup_cmpl,
1154 msecs_to_jiffies(ISCSI_CMD_CLEANUP_TIMEOUT));
1155 spin_lock_bh(&conn->session->lock);
1156 }
1157 bnx2i_iscsi_unmap_sg_list(task->dd_data);
1158}
1159
1160/**
1161 * bnx2i_mtask_xmit - transmit mtask to chip for further processing
1162 * @conn: transport layer conn structure pointer
1163 * @task: transport layer command structure pointer
1164 */
1165static int
1166bnx2i_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
1167{
1168 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1169 struct bnx2i_cmd *cmd = task->dd_data;
1170
1171 memset(bnx2i_conn->gen_pdu.req_buf, 0, ISCSI_DEF_MAX_RECV_SEG_LEN);
1172
1173 bnx2i_setup_cmd_wqe_template(cmd);
1174 bnx2i_conn->gen_pdu.req_buf_size = task->data_count;
1175 if (task->data_count) {
1176 memcpy(bnx2i_conn->gen_pdu.req_buf, task->data,
1177 task->data_count);
1178 bnx2i_conn->gen_pdu.req_wr_ptr =
1179 bnx2i_conn->gen_pdu.req_buf + task->data_count;
1180 }
1181 cmd->conn = conn->dd_data;
1182 cmd->scsi_cmd = NULL;
1183 return bnx2i_iscsi_send_generic_request(task);
1184}
1185
1186/**
1187 * bnx2i_task_xmit - transmit iscsi command to chip for further processing
1188 * @task: transport layer command structure pointer
1189 *
1190 * maps SG buffers and send request to chip/firmware in the form of SQ WQE
1191 */
1192static int bnx2i_task_xmit(struct iscsi_task *task)
1193{
1194 struct iscsi_conn *conn = task->conn;
1195 struct iscsi_session *session = conn->session;
1196 struct Scsi_Host *shost = iscsi_session_to_shost(session->cls_session);
1197 struct bnx2i_hba *hba = iscsi_host_priv(shost);
1198 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1199 struct scsi_cmnd *sc = task->sc;
1200 struct bnx2i_cmd *cmd = task->dd_data;
1201 struct iscsi_cmd *hdr = (struct iscsi_cmd *) task->hdr;
1202
Michael Chancf4e6362009-06-08 18:14:44 -07001203 /*
1204 * If there is no scsi_cmnd this must be a mgmt task
1205 */
1206 if (!sc)
1207 return bnx2i_mtask_xmit(conn, task);
1208
1209 bnx2i_setup_cmd_wqe_template(cmd);
1210 cmd->req.op_code = ISCSI_OP_SCSI_CMD;
1211 cmd->conn = bnx2i_conn;
1212 cmd->scsi_cmd = sc;
1213 cmd->req.total_data_transfer_length = scsi_bufflen(sc);
1214 cmd->req.cmd_sn = be32_to_cpu(hdr->cmdsn);
1215
1216 bnx2i_iscsi_map_sg_list(cmd);
1217 bnx2i_cpy_scsi_cdb(sc, cmd);
1218
1219 cmd->req.op_attr = ISCSI_ATTR_SIMPLE;
1220 if (sc->sc_data_direction == DMA_TO_DEVICE) {
1221 cmd->req.op_attr |= ISCSI_CMD_REQUEST_WRITE;
1222 cmd->req.itt = task->itt |
1223 (ISCSI_TASK_TYPE_WRITE << ISCSI_CMD_REQUEST_TYPE_SHIFT);
1224 bnx2i_setup_write_cmd_bd_info(task);
1225 } else {
1226 if (scsi_bufflen(sc))
1227 cmd->req.op_attr |= ISCSI_CMD_REQUEST_READ;
1228 cmd->req.itt = task->itt |
1229 (ISCSI_TASK_TYPE_READ << ISCSI_CMD_REQUEST_TYPE_SHIFT);
1230 }
1231
1232 cmd->req.num_bds = cmd->io_tbl.bd_valid;
1233 if (!cmd->io_tbl.bd_valid) {
1234 cmd->req.bd_list_addr_lo = (u32) hba->mp_bd_dma;
1235 cmd->req.bd_list_addr_hi = (u32) ((u64) hba->mp_bd_dma >> 32);
1236 cmd->req.num_bds = 1;
1237 }
1238
1239 bnx2i_send_iscsi_scsicmd(bnx2i_conn, cmd);
1240 return 0;
1241}
1242
1243/**
1244 * bnx2i_session_create - create a new iscsi session
1245 * @cmds_max: max commands supported
1246 * @qdepth: scsi queue depth to support
1247 * @initial_cmdsn: initial iscsi CMDSN to be used for this session
1248 *
1249 * Creates a new iSCSI session instance on given device.
1250 */
1251static struct iscsi_cls_session *
1252bnx2i_session_create(struct iscsi_endpoint *ep,
1253 uint16_t cmds_max, uint16_t qdepth,
1254 uint32_t initial_cmdsn)
1255{
1256 struct Scsi_Host *shost;
1257 struct iscsi_cls_session *cls_session;
1258 struct bnx2i_hba *hba;
1259 struct bnx2i_endpoint *bnx2i_ep;
1260
1261 if (!ep) {
1262 printk(KERN_ERR "bnx2i: missing ep.\n");
1263 return NULL;
1264 }
1265
1266 bnx2i_ep = ep->dd_data;
1267 shost = bnx2i_ep->hba->shost;
1268 hba = iscsi_host_priv(shost);
1269 if (bnx2i_adapter_ready(hba))
1270 return NULL;
1271
1272 /*
1273 * user can override hw limit as long as it is within
1274 * the min/max.
1275 */
1276 if (cmds_max > hba->max_sqes)
1277 cmds_max = hba->max_sqes;
1278 else if (cmds_max < BNX2I_SQ_WQES_MIN)
1279 cmds_max = BNX2I_SQ_WQES_MIN;
1280
1281 cls_session = iscsi_session_setup(&bnx2i_iscsi_transport, shost,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05301282 cmds_max, 0, sizeof(struct bnx2i_cmd),
Michael Chancf4e6362009-06-08 18:14:44 -07001283 initial_cmdsn, ISCSI_MAX_TARGET);
1284 if (!cls_session)
1285 return NULL;
1286
1287 if (bnx2i_setup_cmd_pool(hba, cls_session->dd_data))
1288 goto session_teardown;
1289 return cls_session;
1290
1291session_teardown:
1292 iscsi_session_teardown(cls_session);
1293 return NULL;
1294}
1295
1296
1297/**
1298 * bnx2i_session_destroy - destroys iscsi session
1299 * @cls_session: pointer to iscsi cls session
1300 *
1301 * Destroys previously created iSCSI session instance and releases
1302 * all resources held by it
1303 */
1304static void bnx2i_session_destroy(struct iscsi_cls_session *cls_session)
1305{
1306 struct iscsi_session *session = cls_session->dd_data;
1307 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1308 struct bnx2i_hba *hba = iscsi_host_priv(shost);
1309
1310 bnx2i_destroy_cmd_pool(hba, session);
1311 iscsi_session_teardown(cls_session);
1312}
1313
1314
1315/**
1316 * bnx2i_conn_create - create iscsi connection instance
1317 * @cls_session: pointer to iscsi cls session
1318 * @cid: iscsi cid as per rfc (not NX2's CID terminology)
1319 *
1320 * Creates a new iSCSI connection instance for a given session
1321 */
1322static struct iscsi_cls_conn *
1323bnx2i_conn_create(struct iscsi_cls_session *cls_session, uint32_t cid)
1324{
1325 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1326 struct bnx2i_hba *hba = iscsi_host_priv(shost);
1327 struct bnx2i_conn *bnx2i_conn;
1328 struct iscsi_cls_conn *cls_conn;
1329 struct iscsi_conn *conn;
1330
1331 cls_conn = iscsi_conn_setup(cls_session, sizeof(*bnx2i_conn),
1332 cid);
1333 if (!cls_conn)
1334 return NULL;
1335 conn = cls_conn->dd_data;
1336
1337 bnx2i_conn = conn->dd_data;
1338 bnx2i_conn->cls_conn = cls_conn;
1339 bnx2i_conn->hba = hba;
1340 /* 'ep' ptr will be assigned in bind() call */
1341 bnx2i_conn->ep = NULL;
1342 init_completion(&bnx2i_conn->cmd_cleanup_cmpl);
1343
1344 if (bnx2i_conn_alloc_login_resources(hba, bnx2i_conn)) {
1345 iscsi_conn_printk(KERN_ALERT, conn,
1346 "conn_new: login resc alloc failed!!\n");
1347 goto free_conn;
1348 }
1349
1350 return cls_conn;
1351
1352free_conn:
1353 iscsi_conn_teardown(cls_conn);
1354 return NULL;
1355}
1356
1357/**
1358 * bnx2i_conn_bind - binds iscsi sess, conn and ep objects together
1359 * @cls_session: pointer to iscsi cls session
1360 * @cls_conn: pointer to iscsi cls conn
1361 * @transport_fd: 64-bit EP handle
1362 * @is_leading: leading connection on this session?
1363 *
1364 * Binds together iSCSI session instance, iSCSI connection instance
1365 * and the TCP connection. This routine returns error code if
1366 * TCP connection does not belong on the device iSCSI sess/conn
1367 * is bound
1368 */
1369static int bnx2i_conn_bind(struct iscsi_cls_session *cls_session,
1370 struct iscsi_cls_conn *cls_conn,
1371 uint64_t transport_fd, int is_leading)
1372{
1373 struct iscsi_conn *conn = cls_conn->dd_data;
1374 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1375 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1376 struct bnx2i_hba *hba = iscsi_host_priv(shost);
1377 struct bnx2i_endpoint *bnx2i_ep;
1378 struct iscsi_endpoint *ep;
1379 int ret_code;
1380
1381 ep = iscsi_lookup_endpoint(transport_fd);
1382 if (!ep)
1383 return -EINVAL;
1384
1385 bnx2i_ep = ep->dd_data;
1386 if ((bnx2i_ep->state == EP_STATE_TCP_FIN_RCVD) ||
1387 (bnx2i_ep->state == EP_STATE_TCP_RST_RCVD))
1388 /* Peer disconnect via' FIN or RST */
1389 return -EINVAL;
1390
1391 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1392 return -EINVAL;
1393
1394 if (bnx2i_ep->hba != hba) {
1395 /* Error - TCP connection does not belong to this device
1396 */
1397 iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
1398 "conn bind, ep=0x%p (%s) does not",
1399 bnx2i_ep, bnx2i_ep->hba->netdev->name);
1400 iscsi_conn_printk(KERN_ALERT, cls_conn->dd_data,
1401 "belong to hba (%s)\n",
1402 hba->netdev->name);
1403 return -EEXIST;
1404 }
1405
1406 bnx2i_ep->conn = bnx2i_conn;
1407 bnx2i_conn->ep = bnx2i_ep;
1408 bnx2i_conn->iscsi_conn_cid = bnx2i_ep->ep_iscsi_cid;
1409 bnx2i_conn->fw_cid = bnx2i_ep->ep_cid;
Michael Chancf4e6362009-06-08 18:14:44 -07001410
1411 ret_code = bnx2i_bind_conn_to_iscsi_cid(hba, bnx2i_conn,
1412 bnx2i_ep->ep_iscsi_cid);
1413
1414 /* 5706/5708/5709 FW takes RQ as full when initiated, but for 57710
1415 * driver needs to explicitly replenish RQ index during setup.
1416 */
1417 if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
1418 bnx2i_put_rq_buf(bnx2i_conn, 0);
1419
1420 bnx2i_arm_cq_event_coalescing(bnx2i_conn->ep, CNIC_ARM_CQE);
1421 return ret_code;
1422}
1423
1424
1425/**
1426 * bnx2i_conn_destroy - destroy iscsi connection instance & release resources
1427 * @cls_conn: pointer to iscsi cls conn
1428 *
1429 * Destroy an iSCSI connection instance and release memory resources held by
1430 * this connection
1431 */
1432static void bnx2i_conn_destroy(struct iscsi_cls_conn *cls_conn)
1433{
1434 struct iscsi_conn *conn = cls_conn->dd_data;
1435 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1436 struct Scsi_Host *shost;
1437 struct bnx2i_hba *hba;
1438
1439 shost = iscsi_session_to_shost(iscsi_conn_to_session(cls_conn));
1440 hba = iscsi_host_priv(shost);
1441
1442 bnx2i_conn_free_login_resources(hba, bnx2i_conn);
1443 iscsi_conn_teardown(cls_conn);
1444}
1445
1446
1447/**
1448 * bnx2i_conn_get_param - return iscsi connection parameter to caller
1449 * @cls_conn: pointer to iscsi cls conn
1450 * @param: parameter type identifier
1451 * @buf: buffer pointer
1452 *
1453 * returns iSCSI connection parameters
1454 */
1455static int bnx2i_conn_get_param(struct iscsi_cls_conn *cls_conn,
1456 enum iscsi_param param, char *buf)
1457{
1458 struct iscsi_conn *conn = cls_conn->dd_data;
1459 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1460 int len = 0;
1461
1462 switch (param) {
1463 case ISCSI_PARAM_CONN_PORT:
1464 if (bnx2i_conn->ep)
1465 len = sprintf(buf, "%hu\n",
1466 bnx2i_conn->ep->cm_sk->dst_port);
1467 break;
1468 case ISCSI_PARAM_CONN_ADDRESS:
1469 if (bnx2i_conn->ep)
Joe Perchesd9573e72010-02-10 16:51:43 -06001470 len = sprintf(buf, "%pI4\n",
1471 &bnx2i_conn->ep->cm_sk->dst_ip);
Michael Chancf4e6362009-06-08 18:14:44 -07001472 break;
1473 default:
1474 return iscsi_conn_get_param(cls_conn, param, buf);
1475 }
1476
1477 return len;
1478}
1479
1480/**
1481 * bnx2i_host_get_param - returns host (adapter) related parameters
1482 * @shost: scsi host pointer
1483 * @param: parameter type identifier
1484 * @buf: buffer pointer
1485 */
1486static int bnx2i_host_get_param(struct Scsi_Host *shost,
1487 enum iscsi_host_param param, char *buf)
1488{
1489 struct bnx2i_hba *hba = iscsi_host_priv(shost);
1490 int len = 0;
1491
1492 switch (param) {
1493 case ISCSI_HOST_PARAM_HWADDRESS:
1494 len = sysfs_format_mac(buf, hba->cnic->mac_addr, 6);
1495 break;
1496 case ISCSI_HOST_PARAM_NETDEV_NAME:
1497 len = sprintf(buf, "%s\n", hba->netdev->name);
1498 break;
1499 default:
1500 return iscsi_host_get_param(shost, param, buf);
1501 }
1502 return len;
1503}
1504
1505/**
1506 * bnx2i_conn_start - completes iscsi connection migration to FFP
1507 * @cls_conn: pointer to iscsi cls conn
1508 *
1509 * last call in FFP migration to handover iscsi conn to the driver
1510 */
1511static int bnx2i_conn_start(struct iscsi_cls_conn *cls_conn)
1512{
1513 struct iscsi_conn *conn = cls_conn->dd_data;
1514 struct bnx2i_conn *bnx2i_conn = conn->dd_data;
1515
1516 bnx2i_conn->ep->state = EP_STATE_ULP_UPDATE_START;
1517 bnx2i_update_iscsi_conn(conn);
1518
1519 /*
1520 * this should normally not sleep for a long time so it should
1521 * not disrupt the caller.
1522 */
1523 bnx2i_conn->ep->ofld_timer.expires = 1 * HZ + jiffies;
1524 bnx2i_conn->ep->ofld_timer.function = bnx2i_ep_ofld_timer;
1525 bnx2i_conn->ep->ofld_timer.data = (unsigned long) bnx2i_conn->ep;
1526 add_timer(&bnx2i_conn->ep->ofld_timer);
1527 /* update iSCSI context for this conn, wait for CNIC to complete */
1528 wait_event_interruptible(bnx2i_conn->ep->ofld_wait,
1529 bnx2i_conn->ep->state != EP_STATE_ULP_UPDATE_START);
1530
1531 if (signal_pending(current))
1532 flush_signals(current);
1533 del_timer_sync(&bnx2i_conn->ep->ofld_timer);
1534
1535 iscsi_conn_start(cls_conn);
1536 return 0;
1537}
1538
1539
1540/**
1541 * bnx2i_conn_get_stats - returns iSCSI stats
1542 * @cls_conn: pointer to iscsi cls conn
1543 * @stats: pointer to iscsi statistic struct
1544 */
1545static void bnx2i_conn_get_stats(struct iscsi_cls_conn *cls_conn,
1546 struct iscsi_stats *stats)
1547{
1548 struct iscsi_conn *conn = cls_conn->dd_data;
1549
1550 stats->txdata_octets = conn->txdata_octets;
1551 stats->rxdata_octets = conn->rxdata_octets;
1552 stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
1553 stats->dataout_pdus = conn->dataout_pdus_cnt;
1554 stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
1555 stats->datain_pdus = conn->datain_pdus_cnt;
1556 stats->r2t_pdus = conn->r2t_pdus_cnt;
1557 stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
1558 stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
1559 stats->custom_length = 3;
1560 strcpy(stats->custom[2].desc, "eh_abort_cnt");
1561 stats->custom[2].value = conn->eh_abort_cnt;
1562 stats->digest_err = 0;
1563 stats->timeout_err = 0;
1564 stats->custom_length = 0;
1565}
1566
1567
1568/**
1569 * bnx2i_check_route - checks if target IP route belongs to one of NX2 devices
1570 * @dst_addr: target IP address
1571 *
1572 * check if route resolves to BNX2 device
1573 */
1574static struct bnx2i_hba *bnx2i_check_route(struct sockaddr *dst_addr)
1575{
1576 struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
1577 struct bnx2i_hba *hba;
1578 struct cnic_dev *cnic = NULL;
1579
1580 bnx2i_reg_dev_all();
1581
1582 hba = get_adapter_list_head();
1583 if (hba && hba->cnic)
1584 cnic = hba->cnic->cm_select_dev(desti, CNIC_ULP_ISCSI);
1585 if (!cnic) {
1586 printk(KERN_ALERT "bnx2i: no route,"
1587 "can't connect using cnic\n");
1588 goto no_nx2_route;
1589 }
1590 hba = bnx2i_find_hba_for_cnic(cnic);
1591 if (!hba)
1592 goto no_nx2_route;
1593
1594 if (bnx2i_adapter_ready(hba)) {
1595 printk(KERN_ALERT "bnx2i: check route, hba not found\n");
1596 goto no_nx2_route;
1597 }
1598 if (hba->netdev->mtu > hba->mtu_supported) {
1599 printk(KERN_ALERT "bnx2i: %s network i/f mtu is set to %d\n",
1600 hba->netdev->name, hba->netdev->mtu);
1601 printk(KERN_ALERT "bnx2i: iSCSI HBA can support mtu of %d\n",
1602 hba->mtu_supported);
1603 goto no_nx2_route;
1604 }
1605 return hba;
1606no_nx2_route:
1607 return NULL;
1608}
1609
1610
1611/**
1612 * bnx2i_tear_down_conn - tear down iscsi/tcp connection and free resources
1613 * @hba: pointer to adapter instance
1614 * @ep: endpoint (transport indentifier) structure
1615 *
1616 * destroys cm_sock structure and on chip iscsi context
1617 */
1618static int bnx2i_tear_down_conn(struct bnx2i_hba *hba,
1619 struct bnx2i_endpoint *ep)
1620{
1621 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic))
1622 hba->cnic->cm_destroy(ep->cm_sk);
1623
1624 if (test_bit(ADAPTER_STATE_GOING_DOWN, &ep->hba->adapter_state))
1625 ep->state = EP_STATE_DISCONN_COMPL;
1626
1627 if (test_bit(BNX2I_NX2_DEV_57710, &hba->cnic_dev_type) &&
1628 ep->state == EP_STATE_DISCONN_TIMEDOUT) {
1629 printk(KERN_ALERT "bnx2i - ERROR - please submit GRC Dump,"
1630 " NW/PCIe trace, driver msgs to developers"
1631 " for analysis\n");
1632 return 1;
1633 }
1634
1635 ep->state = EP_STATE_CLEANUP_START;
1636 init_timer(&ep->ofld_timer);
1637 ep->ofld_timer.expires = 10*HZ + jiffies;
1638 ep->ofld_timer.function = bnx2i_ep_ofld_timer;
1639 ep->ofld_timer.data = (unsigned long) ep;
1640 add_timer(&ep->ofld_timer);
1641
1642 bnx2i_ep_destroy_list_add(hba, ep);
1643
1644 /* destroy iSCSI context, wait for it to complete */
1645 bnx2i_send_conn_destroy(hba, ep);
1646 wait_event_interruptible(ep->ofld_wait,
1647 (ep->state != EP_STATE_CLEANUP_START));
1648
1649 if (signal_pending(current))
1650 flush_signals(current);
1651 del_timer_sync(&ep->ofld_timer);
1652
1653 bnx2i_ep_destroy_list_del(hba, ep);
1654
1655 if (ep->state != EP_STATE_CLEANUP_CMPL)
1656 /* should never happen */
1657 printk(KERN_ALERT "bnx2i - conn destroy failed\n");
1658
1659 return 0;
1660}
1661
1662
1663/**
1664 * bnx2i_ep_connect - establish TCP connection to target portal
1665 * @shost: scsi host
1666 * @dst_addr: target IP address
1667 * @non_blocking: blocking or non-blocking call
1668 *
1669 * this routine initiates the TCP/IP connection by invoking Option-2 i/f
1670 * with l5_core and the CNIC. This is a multi-step process of resolving
1671 * route to target, create a iscsi connection context, handshaking with
1672 * CNIC module to create/initialize the socket struct and finally
1673 * sending down option-2 request to complete TCP 3-way handshake
1674 */
1675static struct iscsi_endpoint *bnx2i_ep_connect(struct Scsi_Host *shost,
1676 struct sockaddr *dst_addr,
1677 int non_blocking)
1678{
1679 u32 iscsi_cid = BNX2I_CID_RESERVED;
1680 struct sockaddr_in *desti = (struct sockaddr_in *) dst_addr;
1681 struct sockaddr_in6 *desti6;
1682 struct bnx2i_endpoint *bnx2i_ep;
1683 struct bnx2i_hba *hba;
1684 struct cnic_dev *cnic;
1685 struct cnic_sockaddr saddr;
1686 struct iscsi_endpoint *ep;
1687 int rc = 0;
1688
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -07001689 if (shost) {
Michael Chancf4e6362009-06-08 18:14:44 -07001690 /* driver is given scsi host to work with */
1691 hba = iscsi_host_priv(shost);
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -07001692 /* Register the device with cnic if not already done so */
1693 bnx2i_register_device(hba);
1694 } else
Michael Chancf4e6362009-06-08 18:14:44 -07001695 /*
1696 * check if the given destination can be reached through
1697 * a iscsi capable NetXtreme2 device
1698 */
1699 hba = bnx2i_check_route(dst_addr);
Anil Veerabhadrappafac3cc42009-07-08 18:21:01 -07001700
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -07001701 if (!hba || test_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state)) {
1702 rc = -EINVAL;
Michael Chancf4e6362009-06-08 18:14:44 -07001703 goto check_busy;
1704 }
1705
1706 cnic = hba->cnic;
1707 ep = bnx2i_alloc_ep(hba);
1708 if (!ep) {
1709 rc = -ENOMEM;
1710 goto check_busy;
1711 }
1712 bnx2i_ep = ep->dd_data;
1713
1714 mutex_lock(&hba->net_dev_lock);
1715 if (bnx2i_adapter_ready(hba)) {
1716 rc = -EPERM;
1717 goto net_if_down;
1718 }
1719
Michael Chancf4e6362009-06-08 18:14:44 -07001720 bnx2i_ep->num_active_cmds = 0;
1721 iscsi_cid = bnx2i_alloc_iscsi_cid(hba);
1722 if (iscsi_cid == -1) {
1723 printk(KERN_ALERT "alloc_ep: unable to allocate iscsi cid\n");
1724 rc = -ENOMEM;
1725 goto iscsi_cid_err;
1726 }
1727 bnx2i_ep->hba_age = hba->age;
1728
1729 rc = bnx2i_alloc_qp_resc(hba, bnx2i_ep);
1730 if (rc != 0) {
1731 printk(KERN_ALERT "bnx2i: ep_conn, alloc QP resc error\n");
1732 rc = -ENOMEM;
1733 goto qp_resc_err;
1734 }
1735
1736 bnx2i_ep->ep_iscsi_cid = (u16)iscsi_cid;
1737 bnx2i_ep->state = EP_STATE_OFLD_START;
1738 bnx2i_ep_ofld_list_add(hba, bnx2i_ep);
1739
1740 init_timer(&bnx2i_ep->ofld_timer);
1741 bnx2i_ep->ofld_timer.expires = 2 * HZ + jiffies;
1742 bnx2i_ep->ofld_timer.function = bnx2i_ep_ofld_timer;
1743 bnx2i_ep->ofld_timer.data = (unsigned long) bnx2i_ep;
1744 add_timer(&bnx2i_ep->ofld_timer);
1745
1746 bnx2i_send_conn_ofld_req(hba, bnx2i_ep);
1747
1748 /* Wait for CNIC hardware to setup conn context and return 'cid' */
1749 wait_event_interruptible(bnx2i_ep->ofld_wait,
1750 bnx2i_ep->state != EP_STATE_OFLD_START);
1751
1752 if (signal_pending(current))
1753 flush_signals(current);
1754 del_timer_sync(&bnx2i_ep->ofld_timer);
1755
1756 bnx2i_ep_ofld_list_del(hba, bnx2i_ep);
1757
1758 if (bnx2i_ep->state != EP_STATE_OFLD_COMPL) {
1759 rc = -ENOSPC;
1760 goto conn_failed;
1761 }
1762
1763 rc = cnic->cm_create(cnic, CNIC_ULP_ISCSI, bnx2i_ep->ep_cid,
1764 iscsi_cid, &bnx2i_ep->cm_sk, bnx2i_ep);
1765 if (rc) {
1766 rc = -EINVAL;
1767 goto conn_failed;
1768 }
1769
1770 bnx2i_ep->cm_sk->rcv_buf = 256 * 1024;
1771 bnx2i_ep->cm_sk->snd_buf = 256 * 1024;
1772 clear_bit(SK_TCP_TIMESTAMP, &bnx2i_ep->cm_sk->tcp_flags);
1773
1774 memset(&saddr, 0, sizeof(saddr));
1775 if (dst_addr->sa_family == AF_INET) {
1776 desti = (struct sockaddr_in *) dst_addr;
1777 saddr.remote.v4 = *desti;
1778 saddr.local.v4.sin_family = desti->sin_family;
1779 } else if (dst_addr->sa_family == AF_INET6) {
1780 desti6 = (struct sockaddr_in6 *) dst_addr;
1781 saddr.remote.v6 = *desti6;
1782 saddr.local.v6.sin6_family = desti6->sin6_family;
1783 }
1784
1785 bnx2i_ep->timestamp = jiffies;
1786 bnx2i_ep->state = EP_STATE_CONNECT_START;
1787 if (!test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
1788 rc = -EINVAL;
1789 goto conn_failed;
1790 } else
1791 rc = cnic->cm_connect(bnx2i_ep->cm_sk, &saddr);
Michael Chancf4e6362009-06-08 18:14:44 -07001792 if (rc)
1793 goto release_ep;
1794
Eddie Wai46012e82010-07-01 15:34:51 -07001795 bnx2i_ep_active_list_add(hba, bnx2i_ep);
1796
Michael Chancf4e6362009-06-08 18:14:44 -07001797 if (bnx2i_map_ep_dbell_regs(bnx2i_ep))
Eddie Wai46012e82010-07-01 15:34:51 -07001798 goto del_active_ep;
1799
Michael Chancf4e6362009-06-08 18:14:44 -07001800 mutex_unlock(&hba->net_dev_lock);
1801 return ep;
1802
Eddie Wai46012e82010-07-01 15:34:51 -07001803del_active_ep:
1804 bnx2i_ep_active_list_del(hba, bnx2i_ep);
Michael Chancf4e6362009-06-08 18:14:44 -07001805release_ep:
1806 if (bnx2i_tear_down_conn(hba, bnx2i_ep)) {
1807 mutex_unlock(&hba->net_dev_lock);
1808 return ERR_PTR(rc);
1809 }
1810conn_failed:
1811net_if_down:
1812iscsi_cid_err:
1813 bnx2i_free_qp_resc(hba, bnx2i_ep);
1814qp_resc_err:
1815 bnx2i_free_ep(ep);
1816 mutex_unlock(&hba->net_dev_lock);
1817check_busy:
1818 bnx2i_unreg_dev_all();
1819 return ERR_PTR(rc);
1820}
1821
1822
1823/**
1824 * bnx2i_ep_poll - polls for TCP connection establishement
1825 * @ep: TCP connection (endpoint) handle
1826 * @timeout_ms: timeout value in milli secs
1827 *
1828 * polls for TCP connect request to complete
1829 */
1830static int bnx2i_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
1831{
1832 struct bnx2i_endpoint *bnx2i_ep;
1833 int rc = 0;
1834
1835 bnx2i_ep = ep->dd_data;
1836 if ((bnx2i_ep->state == EP_STATE_IDLE) ||
1837 (bnx2i_ep->state == EP_STATE_CONNECT_FAILED) ||
1838 (bnx2i_ep->state == EP_STATE_OFLD_FAILED))
1839 return -1;
1840 if (bnx2i_ep->state == EP_STATE_CONNECT_COMPL)
1841 return 1;
1842
1843 rc = wait_event_interruptible_timeout(bnx2i_ep->ofld_wait,
1844 ((bnx2i_ep->state ==
1845 EP_STATE_OFLD_FAILED) ||
1846 (bnx2i_ep->state ==
1847 EP_STATE_CONNECT_FAILED) ||
1848 (bnx2i_ep->state ==
1849 EP_STATE_CONNECT_COMPL)),
1850 msecs_to_jiffies(timeout_ms));
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -07001851 if (bnx2i_ep->state == EP_STATE_OFLD_FAILED)
Michael Chancf4e6362009-06-08 18:14:44 -07001852 rc = -1;
1853
1854 if (rc > 0)
1855 return 1;
1856 else if (!rc)
1857 return 0; /* timeout */
1858 else
1859 return rc;
1860}
1861
1862
1863/**
1864 * bnx2i_ep_tcp_conn_active - check EP state transition
1865 * @ep: endpoint pointer
1866 *
1867 * check if underlying TCP connection is active
1868 */
1869static int bnx2i_ep_tcp_conn_active(struct bnx2i_endpoint *bnx2i_ep)
1870{
1871 int ret;
1872 int cnic_dev_10g = 0;
1873
1874 if (test_bit(BNX2I_NX2_DEV_57710, &bnx2i_ep->hba->cnic_dev_type))
1875 cnic_dev_10g = 1;
1876
1877 switch (bnx2i_ep->state) {
1878 case EP_STATE_CONNECT_START:
1879 case EP_STATE_CLEANUP_FAILED:
1880 case EP_STATE_OFLD_FAILED:
1881 case EP_STATE_DISCONN_TIMEDOUT:
1882 ret = 0;
1883 break;
1884 case EP_STATE_CONNECT_COMPL:
1885 case EP_STATE_ULP_UPDATE_START:
1886 case EP_STATE_ULP_UPDATE_COMPL:
1887 case EP_STATE_TCP_FIN_RCVD:
1888 case EP_STATE_ULP_UPDATE_FAILED:
1889 ret = 1;
1890 break;
1891 case EP_STATE_TCP_RST_RCVD:
1892 ret = 0;
1893 break;
1894 case EP_STATE_CONNECT_FAILED:
1895 if (cnic_dev_10g)
1896 ret = 1;
1897 else
1898 ret = 0;
1899 break;
1900 default:
1901 ret = 0;
1902 }
1903
1904 return ret;
1905}
1906
1907
Eddie Wai6447f282010-07-01 15:34:50 -07001908/*
1909 * bnx2i_hw_ep_disconnect - executes TCP connection teardown process in the hw
1910 * @ep: TCP connection (bnx2i endpoint) handle
1911 *
1912 * executes TCP connection teardown process
1913 */
1914int bnx2i_hw_ep_disconnect(struct bnx2i_endpoint *bnx2i_ep)
1915{
1916 struct bnx2i_hba *hba = bnx2i_ep->hba;
1917 struct cnic_dev *cnic;
1918 struct iscsi_session *session = NULL;
1919 struct iscsi_conn *conn = NULL;
1920 int ret = 0;
1921
1922 if (!hba)
1923 return 0;
1924
1925 cnic = hba->cnic;
1926 if (!cnic)
1927 return 0;
1928
1929 if (!bnx2i_ep_tcp_conn_active(bnx2i_ep))
1930 goto destroy_conn;
1931
1932 if (bnx2i_ep->conn) {
1933 conn = bnx2i_ep->conn->cls_conn->dd_data;
1934 session = conn->session;
1935 }
1936
1937 bnx2i_ep->state = EP_STATE_DISCONN_START;
1938
1939 init_timer(&bnx2i_ep->ofld_timer);
1940 bnx2i_ep->ofld_timer.expires = 10*HZ + jiffies;
1941 bnx2i_ep->ofld_timer.function = bnx2i_ep_ofld_timer;
1942 bnx2i_ep->ofld_timer.data = (unsigned long) bnx2i_ep;
1943 add_timer(&bnx2i_ep->ofld_timer);
1944
1945 if (test_bit(BNX2I_CNIC_REGISTERED, &hba->reg_with_cnic)) {
1946 int close = 0;
1947 int close_ret = 0;
1948
1949 if (session) {
1950 spin_lock_bh(&session->lock);
1951 if (session->state == ISCSI_STATE_LOGGING_OUT)
1952 close = 1;
1953 spin_unlock_bh(&session->lock);
1954 }
1955 if (close)
1956 close_ret = cnic->cm_close(bnx2i_ep->cm_sk);
1957 else
1958 close_ret = cnic->cm_abort(bnx2i_ep->cm_sk);
1959 if (close_ret)
1960 bnx2i_ep->state = EP_STATE_DISCONN_COMPL;
1961 } else
1962 goto out;
1963
1964 /* wait for option-2 conn teardown */
1965 wait_event_interruptible(bnx2i_ep->ofld_wait,
1966 bnx2i_ep->state != EP_STATE_DISCONN_START);
1967
1968 if (signal_pending(current))
1969 flush_signals(current);
1970 del_timer_sync(&bnx2i_ep->ofld_timer);
1971
1972destroy_conn:
Eddie Wai46012e82010-07-01 15:34:51 -07001973 bnx2i_ep_active_list_del(hba, bnx2i_ep);
Eddie Wai6447f282010-07-01 15:34:50 -07001974 if (bnx2i_tear_down_conn(hba, bnx2i_ep))
1975 ret = -EINVAL;
1976out:
1977 bnx2i_ep->state = EP_STATE_IDLE;
1978 return ret;
1979}
1980
1981
Michael Chancf4e6362009-06-08 18:14:44 -07001982/**
1983 * bnx2i_ep_disconnect - executes TCP connection teardown process
Eddie Wai6447f282010-07-01 15:34:50 -07001984 * @ep: TCP connection (iscsi endpoint) handle
Michael Chancf4e6362009-06-08 18:14:44 -07001985 *
1986 * executes TCP connection teardown process
1987 */
1988static void bnx2i_ep_disconnect(struct iscsi_endpoint *ep)
1989{
1990 struct bnx2i_endpoint *bnx2i_ep;
1991 struct bnx2i_conn *bnx2i_conn = NULL;
Eddie Wai6447f282010-07-01 15:34:50 -07001992 struct iscsi_conn *conn = NULL;
Michael Chancf4e6362009-06-08 18:14:44 -07001993 struct bnx2i_hba *hba;
1994
1995 bnx2i_ep = ep->dd_data;
1996
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02001997 /* driver should not attempt connection cleanup until TCP_CONNECT
Michael Chancf4e6362009-06-08 18:14:44 -07001998 * completes either successfully or fails. Timeout is 9-secs, so
1999 * wait for it to complete
2000 */
2001 while ((bnx2i_ep->state == EP_STATE_CONNECT_START) &&
2002 !time_after(jiffies, bnx2i_ep->timestamp + (12 * HZ)))
2003 msleep(250);
2004
2005 if (bnx2i_ep->conn) {
2006 bnx2i_conn = bnx2i_ep->conn;
2007 conn = bnx2i_conn->cls_conn->dd_data;
Mike Christie24246de2009-11-11 16:34:30 -06002008 iscsi_suspend_queue(conn);
Michael Chancf4e6362009-06-08 18:14:44 -07002009 }
Michael Chancf4e6362009-06-08 18:14:44 -07002010 hba = bnx2i_ep->hba;
Michael Chancf4e6362009-06-08 18:14:44 -07002011
2012 mutex_lock(&hba->net_dev_lock);
2013
Eddie Wai6447f282010-07-01 15:34:50 -07002014 if (bnx2i_ep->state == EP_STATE_IDLE)
2015 goto return_bnx2i_ep;
2016
Michael Chancf4e6362009-06-08 18:14:44 -07002017 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
2018 goto free_resc;
Eddie Wai6447f282010-07-01 15:34:50 -07002019
Michael Chancf4e6362009-06-08 18:14:44 -07002020 if (bnx2i_ep->hba_age != hba->age)
2021 goto free_resc;
2022
Eddie Wai6447f282010-07-01 15:34:50 -07002023 /* Do all chip cleanup here */
2024 if (bnx2i_hw_ep_disconnect(bnx2i_ep)) {
Michael Chancf4e6362009-06-08 18:14:44 -07002025 mutex_unlock(&hba->net_dev_lock);
2026 return;
2027 }
2028free_resc:
Michael Chancf4e6362009-06-08 18:14:44 -07002029 bnx2i_free_qp_resc(hba, bnx2i_ep);
2030return_bnx2i_ep:
2031 if (bnx2i_conn)
2032 bnx2i_conn->ep = NULL;
2033
2034 bnx2i_free_ep(ep);
Eddie Wai6447f282010-07-01 15:34:50 -07002035 mutex_unlock(&hba->net_dev_lock);
Michael Chancf4e6362009-06-08 18:14:44 -07002036 if (!hba->ofld_conns_active)
2037 bnx2i_unreg_dev_all();
Anil Veerabhadrappa490475a2010-04-08 15:59:15 -07002038
2039 wake_up_interruptible(&hba->eh_wait);
Michael Chancf4e6362009-06-08 18:14:44 -07002040}
2041
2042
2043/**
2044 * bnx2i_nl_set_path - ISCSI_UEVENT_PATH_UPDATE user message handler
2045 * @buf: pointer to buffer containing iscsi path message
2046 *
2047 */
2048static int bnx2i_nl_set_path(struct Scsi_Host *shost, struct iscsi_path *params)
2049{
2050 struct bnx2i_hba *hba = iscsi_host_priv(shost);
2051 char *buf = (char *) params;
2052 u16 len = sizeof(*params);
2053
2054 /* handled by cnic driver */
2055 hba->cnic->iscsi_nl_msg_recv(hba->cnic, ISCSI_UEVENT_PATH_UPDATE, buf,
2056 len);
2057
2058 return 0;
2059}
2060
2061
2062/*
2063 * 'Scsi_Host_Template' structure and 'iscsi_tranport' structure template
2064 * used while registering with the scsi host and iSCSI transport module.
2065 */
2066static struct scsi_host_template bnx2i_host_template = {
2067 .module = THIS_MODULE,
2068 .name = "Broadcom Offload iSCSI Initiator",
2069 .proc_name = "bnx2i",
2070 .queuecommand = iscsi_queuecommand,
2071 .eh_abort_handler = iscsi_eh_abort,
2072 .eh_device_reset_handler = iscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302073 .eh_target_reset_handler = iscsi_eh_recover_target,
Mike Christie9f9127f2010-02-10 16:51:46 -06002074 .change_queue_depth = iscsi_change_queue_depth,
Michael Chancf4e6362009-06-08 18:14:44 -07002075 .can_queue = 1024,
2076 .max_sectors = 127,
2077 .cmd_per_lun = 32,
2078 .this_id = -1,
2079 .use_clustering = ENABLE_CLUSTERING,
2080 .sg_tablesize = ISCSI_MAX_BDS_PER_CMD,
2081 .shost_attrs = bnx2i_dev_attributes,
2082};
2083
2084struct iscsi_transport bnx2i_iscsi_transport = {
2085 .owner = THIS_MODULE,
2086 .name = "bnx2i",
2087 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST |
2088 CAP_MULTI_R2T | CAP_DATADGST |
2089 CAP_DATA_PATH_OFFLOAD,
2090 .param_mask = ISCSI_MAX_RECV_DLENGTH |
2091 ISCSI_MAX_XMIT_DLENGTH |
2092 ISCSI_HDRDGST_EN |
2093 ISCSI_DATADGST_EN |
2094 ISCSI_INITIAL_R2T_EN |
2095 ISCSI_MAX_R2T |
2096 ISCSI_IMM_DATA_EN |
2097 ISCSI_FIRST_BURST |
2098 ISCSI_MAX_BURST |
2099 ISCSI_PDU_INORDER_EN |
2100 ISCSI_DATASEQ_INORDER_EN |
2101 ISCSI_ERL |
2102 ISCSI_CONN_PORT |
2103 ISCSI_CONN_ADDRESS |
2104 ISCSI_EXP_STATSN |
2105 ISCSI_PERSISTENT_PORT |
2106 ISCSI_PERSISTENT_ADDRESS |
2107 ISCSI_TARGET_NAME | ISCSI_TPGT |
2108 ISCSI_USERNAME | ISCSI_PASSWORD |
2109 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
2110 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
Mike Christie3fe5ae82009-11-11 16:34:33 -06002111 ISCSI_LU_RESET_TMO | ISCSI_TGT_RESET_TMO |
Michael Chancf4e6362009-06-08 18:14:44 -07002112 ISCSI_PING_TMO | ISCSI_RECV_TMO |
2113 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
2114 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_NETDEV_NAME,
2115 .create_session = bnx2i_session_create,
2116 .destroy_session = bnx2i_session_destroy,
2117 .create_conn = bnx2i_conn_create,
2118 .bind_conn = bnx2i_conn_bind,
2119 .destroy_conn = bnx2i_conn_destroy,
2120 .set_param = iscsi_set_param,
2121 .get_conn_param = bnx2i_conn_get_param,
2122 .get_session_param = iscsi_session_get_param,
2123 .get_host_param = bnx2i_host_get_param,
2124 .start_conn = bnx2i_conn_start,
2125 .stop_conn = iscsi_conn_stop,
2126 .send_pdu = iscsi_conn_send_pdu,
2127 .xmit_task = bnx2i_task_xmit,
2128 .get_stats = bnx2i_conn_get_stats,
2129 /* TCP connect - disconnect - option-2 interface calls */
2130 .ep_connect = bnx2i_ep_connect,
2131 .ep_poll = bnx2i_ep_poll,
2132 .ep_disconnect = bnx2i_ep_disconnect,
2133 .set_path = bnx2i_nl_set_path,
2134 /* Error recovery timeout call */
2135 .session_recovery_timedout = iscsi_session_recovery_timedout,
2136 .cleanup_task = bnx2i_cleanup_task,
2137};