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