blob: 263c2678ff6295432cfb7aac0541ee3d0039ec93 [file] [log] [blame]
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001/* bnx2fc_tgt.c: Broadcom NetXtreme II Linux FCoE offload driver.
2 * Handles operations such as session offload/upload etc, and manages
3 * session resources such as connection id and qp resources.
4 *
5 * Copyright (c) 2008 - 2010 Broadcom Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation.
10 *
11 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
12 */
13
14#include "bnx2fc.h"
15static void bnx2fc_upld_timer(unsigned long data);
16static void bnx2fc_ofld_timer(unsigned long data);
17static int bnx2fc_init_tgt(struct bnx2fc_rport *tgt,
18 struct fcoe_port *port,
19 struct fc_rport_priv *rdata);
20static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
21 struct bnx2fc_rport *tgt);
22static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
23 struct bnx2fc_rport *tgt);
24static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
25 struct bnx2fc_rport *tgt);
26static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id);
27
28static void bnx2fc_upld_timer(unsigned long data)
29{
30
31 struct bnx2fc_rport *tgt = (struct bnx2fc_rport *)data;
32
33 BNX2FC_TGT_DBG(tgt, "upld_timer - Upload compl not received!!\n");
34 /* fake upload completion */
35 clear_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags);
36 set_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
37 wake_up_interruptible(&tgt->upld_wait);
38}
39
40static void bnx2fc_ofld_timer(unsigned long data)
41{
42
43 struct bnx2fc_rport *tgt = (struct bnx2fc_rport *)data;
44
45 BNX2FC_TGT_DBG(tgt, "entered bnx2fc_ofld_timer\n");
46 /* NOTE: This function should never be called, as
47 * offload should never timeout
48 */
49 /*
50 * If the timer has expired, this session is dead
51 * Clear offloaded flag and logout of this device.
52 * Since OFFLOADED flag is cleared, this case
53 * will be considered as offload error and the
54 * port will be logged off, and conn_id, session
55 * resources are freed up in bnx2fc_offload_session
56 */
57 clear_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags);
58 set_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
59 wake_up_interruptible(&tgt->ofld_wait);
60}
61
62static void bnx2fc_offload_session(struct fcoe_port *port,
63 struct bnx2fc_rport *tgt,
64 struct fc_rport_priv *rdata)
65{
66 struct fc_lport *lport = rdata->local_port;
67 struct fc_rport *rport = rdata->rport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070068 struct bnx2fc_interface *interface = port->priv;
69 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080070 int rval;
71 int i = 0;
72
73 /* Initialize bnx2fc_rport */
74 /* NOTE: tgt is already bzero'd */
75 rval = bnx2fc_init_tgt(tgt, port, rdata);
76 if (rval) {
77 printk(KERN_ERR PFX "Failed to allocate conn id for "
78 "port_id (%6x)\n", rport->port_id);
79 goto ofld_err;
80 }
81
82 /* Allocate session resources */
83 rval = bnx2fc_alloc_session_resc(hba, tgt);
84 if (rval) {
85 printk(KERN_ERR PFX "Failed to allocate resources\n");
86 goto ofld_err;
87 }
88
89 /*
90 * Initialize FCoE session offload process.
91 * Upon completion of offload process add
92 * rport to list of rports
93 */
94retry_ofld:
95 clear_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
96 rval = bnx2fc_send_session_ofld_req(port, tgt);
97 if (rval) {
98 printk(KERN_ERR PFX "ofld_req failed\n");
99 goto ofld_err;
100 }
101
102 /*
103 * wait for the session is offloaded and enabled. 3 Secs
104 * should be ample time for this process to complete.
105 */
106 setup_timer(&tgt->ofld_timer, bnx2fc_ofld_timer, (unsigned long)tgt);
107 mod_timer(&tgt->ofld_timer, jiffies + BNX2FC_FW_TIMEOUT);
108
109 wait_event_interruptible(tgt->ofld_wait,
110 (test_bit(
111 BNX2FC_FLAG_OFLD_REQ_CMPL,
112 &tgt->flags)));
113 if (signal_pending(current))
114 flush_signals(current);
115
116 del_timer_sync(&tgt->ofld_timer);
117
118 if (!(test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags))) {
119 if (test_and_clear_bit(BNX2FC_FLAG_CTX_ALLOC_FAILURE,
120 &tgt->flags)) {
121 BNX2FC_TGT_DBG(tgt, "ctx_alloc_failure, "
122 "retry ofld..%d\n", i++);
123 msleep_interruptible(1000);
124 if (i > 3) {
125 i = 0;
126 goto ofld_err;
127 }
128 goto retry_ofld;
129 }
130 goto ofld_err;
131 }
132 if (bnx2fc_map_doorbell(tgt)) {
133 printk(KERN_ERR PFX "map doorbell failed - no mem\n");
134 /* upload will take care of cleaning up sess resc */
135 lport->tt.rport_logoff(rdata);
136 }
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300137 /* Arm CQ */
138 bnx2fc_arm_cq(tgt);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800139 return;
140
141ofld_err:
142 /* couldn't offload the session. log off from this rport */
143 BNX2FC_TGT_DBG(tgt, "bnx2fc_offload_session - offload error\n");
144 lport->tt.rport_logoff(rdata);
145 /* Free session resources */
146 bnx2fc_free_session_resc(hba, tgt);
147 if (tgt->fcoe_conn_id != -1)
148 bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
149}
150
151void bnx2fc_flush_active_ios(struct bnx2fc_rport *tgt)
152{
153 struct bnx2fc_cmd *io_req;
154 struct list_head *list;
155 struct list_head *tmp;
156 int rc;
157 int i = 0;
158 BNX2FC_TGT_DBG(tgt, "Entered flush_active_ios - %d\n",
159 tgt->num_active_ios.counter);
160
161 spin_lock_bh(&tgt->tgt_lock);
162 tgt->flush_in_prog = 1;
163
164 list_for_each_safe(list, tmp, &tgt->active_cmd_queue) {
165 i++;
166 io_req = (struct bnx2fc_cmd *)list;
167 list_del_init(&io_req->link);
168 io_req->on_active_queue = 0;
169 BNX2FC_IO_DBG(io_req, "cmd_queue cleanup\n");
170
171 if (cancel_delayed_work(&io_req->timeout_work)) {
172 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
173 &io_req->req_flags)) {
174 /* Handle eh_abort timeout */
175 BNX2FC_IO_DBG(io_req, "eh_abort for IO "
176 "cleaned up\n");
177 complete(&io_req->tm_done);
178 }
179 kref_put(&io_req->refcount,
180 bnx2fc_cmd_release); /* drop timer hold */
181 }
182
183 set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags);
184 set_bit(BNX2FC_FLAG_IO_CLEANUP, &io_req->req_flags);
185 rc = bnx2fc_initiate_cleanup(io_req);
186 BUG_ON(rc);
187 }
188
189 list_for_each_safe(list, tmp, &tgt->els_queue) {
190 i++;
191 io_req = (struct bnx2fc_cmd *)list;
192 list_del_init(&io_req->link);
193 io_req->on_active_queue = 0;
194
195 BNX2FC_IO_DBG(io_req, "els_queue cleanup\n");
196
197 if (cancel_delayed_work(&io_req->timeout_work))
198 kref_put(&io_req->refcount,
199 bnx2fc_cmd_release); /* drop timer hold */
200
201 if ((io_req->cb_func) && (io_req->cb_arg)) {
202 io_req->cb_func(io_req->cb_arg);
203 io_req->cb_arg = NULL;
204 }
205
206 rc = bnx2fc_initiate_cleanup(io_req);
207 BUG_ON(rc);
208 }
209
210 list_for_each_safe(list, tmp, &tgt->io_retire_queue) {
211 i++;
212 io_req = (struct bnx2fc_cmd *)list;
213 list_del_init(&io_req->link);
214
215 BNX2FC_IO_DBG(io_req, "retire_queue flush\n");
216
217 if (cancel_delayed_work(&io_req->timeout_work))
218 kref_put(&io_req->refcount, bnx2fc_cmd_release);
219
220 clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
221 }
222
223 BNX2FC_TGT_DBG(tgt, "IOs flushed = %d\n", i);
224 i = 0;
225 spin_unlock_bh(&tgt->tgt_lock);
226 /* wait for active_ios to go to 0 */
227 while ((tgt->num_active_ios.counter != 0) && (i++ < BNX2FC_WAIT_CNT))
228 msleep(25);
229 if (tgt->num_active_ios.counter != 0)
230 printk(KERN_ERR PFX "CLEANUP on port 0x%x:"
231 " active_ios = %d\n",
232 tgt->rdata->ids.port_id, tgt->num_active_ios.counter);
233 spin_lock_bh(&tgt->tgt_lock);
234 tgt->flush_in_prog = 0;
235 spin_unlock_bh(&tgt->tgt_lock);
236}
237
238static void bnx2fc_upload_session(struct fcoe_port *port,
239 struct bnx2fc_rport *tgt)
240{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700241 struct bnx2fc_interface *interface = port->priv;
242 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800243
244 BNX2FC_TGT_DBG(tgt, "upload_session: active_ios = %d\n",
245 tgt->num_active_ios.counter);
246
247 /*
248 * Called with hba->hba_mutex held.
249 * This is a blocking call
250 */
251 clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
252 bnx2fc_send_session_disable_req(port, tgt);
253
254 /*
255 * wait for upload to complete. 3 Secs
256 * should be sufficient time for this process to complete.
257 */
258 setup_timer(&tgt->upld_timer, bnx2fc_upld_timer, (unsigned long)tgt);
259 mod_timer(&tgt->upld_timer, jiffies + BNX2FC_FW_TIMEOUT);
260
261 BNX2FC_TGT_DBG(tgt, "waiting for disable compl\n");
262 wait_event_interruptible(tgt->upld_wait,
263 (test_bit(
264 BNX2FC_FLAG_UPLD_REQ_COMPL,
265 &tgt->flags)));
266
267 if (signal_pending(current))
268 flush_signals(current);
269
270 del_timer_sync(&tgt->upld_timer);
271
272 /*
273 * traverse thru the active_q and tmf_q and cleanup
274 * IOs in these lists
275 */
276 BNX2FC_TGT_DBG(tgt, "flush/upload - disable wait flags = 0x%lx\n",
277 tgt->flags);
278 bnx2fc_flush_active_ios(tgt);
279
280 /* Issue destroy KWQE */
281 if (test_bit(BNX2FC_FLAG_DISABLED, &tgt->flags)) {
282 BNX2FC_TGT_DBG(tgt, "send destroy req\n");
283 clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
284 bnx2fc_send_session_destroy_req(hba, tgt);
285
286 /* wait for destroy to complete */
287 setup_timer(&tgt->upld_timer,
288 bnx2fc_upld_timer, (unsigned long)tgt);
289 mod_timer(&tgt->upld_timer, jiffies + BNX2FC_FW_TIMEOUT);
290
291 wait_event_interruptible(tgt->upld_wait,
292 (test_bit(
293 BNX2FC_FLAG_UPLD_REQ_COMPL,
294 &tgt->flags)));
295
296 if (!(test_bit(BNX2FC_FLAG_DESTROYED, &tgt->flags)))
297 printk(KERN_ERR PFX "ERROR!! destroy timed out\n");
298
299 BNX2FC_TGT_DBG(tgt, "destroy wait complete flags = 0x%lx\n",
300 tgt->flags);
301 if (signal_pending(current))
302 flush_signals(current);
303
304 del_timer_sync(&tgt->upld_timer);
305
306 } else
307 printk(KERN_ERR PFX "ERROR!! DISABLE req timed out, destroy"
308 " not sent to FW\n");
309
310 /* Free session resources */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800311 bnx2fc_free_session_resc(hba, tgt);
312 bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800313}
314
315static int bnx2fc_init_tgt(struct bnx2fc_rport *tgt,
316 struct fcoe_port *port,
317 struct fc_rport_priv *rdata)
318{
319
320 struct fc_rport *rport = rdata->rport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700321 struct bnx2fc_interface *interface = port->priv;
322 struct bnx2fc_hba *hba = interface->hba;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300323 struct b577xx_doorbell_set_prod *sq_db = &tgt->sq_db;
324 struct b577xx_fcoe_rx_doorbell *rx_db = &tgt->rx_db;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800325
326 tgt->rport = rport;
327 tgt->rdata = rdata;
328 tgt->port = port;
329
330 if (hba->num_ofld_sess >= BNX2FC_NUM_MAX_SESS) {
331 BNX2FC_TGT_DBG(tgt, "exceeded max sessions. logoff this tgt\n");
332 tgt->fcoe_conn_id = -1;
333 return -1;
334 }
335
336 tgt->fcoe_conn_id = bnx2fc_alloc_conn_id(hba, tgt);
337 if (tgt->fcoe_conn_id == -1)
338 return -1;
339
340 BNX2FC_TGT_DBG(tgt, "init_tgt - conn_id = 0x%x\n", tgt->fcoe_conn_id);
341
342 tgt->max_sqes = BNX2FC_SQ_WQES_MAX;
343 tgt->max_rqes = BNX2FC_RQ_WQES_MAX;
344 tgt->max_cqes = BNX2FC_CQ_WQES_MAX;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300345 atomic_set(&tgt->free_sqes, BNX2FC_SQ_WQES_MAX);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800346
347 /* Initialize the toggle bit */
348 tgt->sq_curr_toggle_bit = 1;
349 tgt->cq_curr_toggle_bit = 1;
350 tgt->sq_prod_idx = 0;
351 tgt->cq_cons_idx = 0;
352 tgt->rq_prod_idx = 0x8000;
353 tgt->rq_cons_idx = 0;
354 atomic_set(&tgt->num_active_ios, 0);
355
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300356 /* initialize sq doorbell */
357 sq_db->header.header = B577XX_DOORBELL_HDR_DB_TYPE;
358 sq_db->header.header |= B577XX_FCOE_CONNECTION_TYPE <<
359 B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT;
360 /* initialize rx doorbell */
361 rx_db->hdr.header = ((0x1 << B577XX_DOORBELL_HDR_RX_SHIFT) |
362 (0x1 << B577XX_DOORBELL_HDR_DB_TYPE_SHIFT) |
363 (B577XX_FCOE_CONNECTION_TYPE <<
364 B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT));
365 rx_db->params = (0x2 << B577XX_FCOE_RX_DOORBELL_NEGATIVE_ARM_SHIFT) |
366 (0x3 << B577XX_FCOE_RX_DOORBELL_OPCODE_SHIFT);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800367
368 spin_lock_init(&tgt->tgt_lock);
369 spin_lock_init(&tgt->cq_lock);
370
371 /* Initialize active_cmd_queue list */
372 INIT_LIST_HEAD(&tgt->active_cmd_queue);
373
374 /* Initialize IO retire queue */
375 INIT_LIST_HEAD(&tgt->io_retire_queue);
376
377 INIT_LIST_HEAD(&tgt->els_queue);
378
379 /* Initialize active_tm_queue list */
380 INIT_LIST_HEAD(&tgt->active_tm_queue);
381
382 init_waitqueue_head(&tgt->ofld_wait);
383 init_waitqueue_head(&tgt->upld_wait);
384
385 return 0;
386}
387
388/**
389 * This event_callback is called after successful completion of libfc
390 * initiated target login. bnx2fc can proceed with initiating the session
391 * establishment.
392 */
393void bnx2fc_rport_event_handler(struct fc_lport *lport,
394 struct fc_rport_priv *rdata,
395 enum fc_rport_event event)
396{
397 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700398 struct bnx2fc_interface *interface = port->priv;
399 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800400 struct fc_rport *rport = rdata->rport;
401 struct fc_rport_libfc_priv *rp;
402 struct bnx2fc_rport *tgt;
403 u32 port_id;
404
405 BNX2FC_HBA_DBG(lport, "rport_event_hdlr: event = %d, port_id = 0x%x\n",
406 event, rdata->ids.port_id);
407 switch (event) {
408 case RPORT_EV_READY:
409 if (!rport) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700410 printk(KERN_ERR PFX "rport is NULL: ERROR!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800411 break;
412 }
413
414 rp = rport->dd_data;
415 if (rport->port_id == FC_FID_DIR_SERV) {
416 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300417 * bnx2fc_rport structure doesn't exist for
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800418 * directory server.
419 * We should not come here, as lport will
420 * take care of fabric login
421 */
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700422 printk(KERN_ERR PFX "%x - rport_event_handler ERROR\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800423 rdata->ids.port_id);
424 break;
425 }
426
427 if (rdata->spp_type != FC_TYPE_FCP) {
428 BNX2FC_HBA_DBG(lport, "not FCP type target."
429 " not offloading\n");
430 break;
431 }
432 if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
433 BNX2FC_HBA_DBG(lport, "not FCP_TARGET"
434 " not offloading\n");
435 break;
436 }
437
438 /*
439 * Offlaod process is protected with hba mutex.
440 * Use the same mutex_lock for upload process too
441 */
442 mutex_lock(&hba->hba_mutex);
443 tgt = (struct bnx2fc_rport *)&rp[1];
444
445 /* This can happen when ADISC finds the same target */
446 if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
447 BNX2FC_TGT_DBG(tgt, "already offloaded\n");
448 mutex_unlock(&hba->hba_mutex);
449 return;
450 }
451
452 /*
453 * Offload the session. This is a blocking call, and will
454 * wait until the session is offloaded.
455 */
456 bnx2fc_offload_session(port, tgt, rdata);
457
458 BNX2FC_TGT_DBG(tgt, "OFFLOAD num_ofld_sess = %d\n",
459 hba->num_ofld_sess);
460
461 if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
462 /*
463 * Session is offloaded and enabled. Map
464 * doorbell register for this target
465 */
466 BNX2FC_TGT_DBG(tgt, "sess offloaded\n");
467 /* This counter is protected with hba mutex */
468 hba->num_ofld_sess++;
469
470 set_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
471 } else {
472 /*
473 * Offload or enable would have failed.
474 * In offload/enable completion path, the
475 * rport would have already been removed
476 */
477 BNX2FC_TGT_DBG(tgt, "Port is being logged off as "
478 "offloaded flag not set\n");
479 }
480 mutex_unlock(&hba->hba_mutex);
481 break;
482 case RPORT_EV_LOGO:
483 case RPORT_EV_FAILED:
484 case RPORT_EV_STOP:
485 port_id = rdata->ids.port_id;
486 if (port_id == FC_FID_DIR_SERV)
487 break;
488
489 if (!rport) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700490 printk(KERN_INFO PFX "%x - rport not created Yet!!\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800491 port_id);
492 break;
493 }
494 rp = rport->dd_data;
495 mutex_lock(&hba->hba_mutex);
496 /*
497 * Perform session upload. Note that rdata->peers is already
498 * removed from disc->rports list before we get this event.
499 */
500 tgt = (struct bnx2fc_rport *)&rp[1];
501
502 if (!(test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags))) {
503 mutex_unlock(&hba->hba_mutex);
504 break;
505 }
506 clear_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
507
508 bnx2fc_upload_session(port, tgt);
509 hba->num_ofld_sess--;
510 BNX2FC_TGT_DBG(tgt, "UPLOAD num_ofld_sess = %d\n",
511 hba->num_ofld_sess);
512 /*
513 * Try to wake up the linkdown wait thread. If num_ofld_sess
514 * is 0, the waiting therad wakes up
515 */
516 if ((hba->wait_for_link_down) &&
517 (hba->num_ofld_sess == 0)) {
518 wake_up_interruptible(&hba->shutdown_wait);
519 }
520 if (test_bit(BNX2FC_FLAG_EXPL_LOGO, &tgt->flags)) {
521 printk(KERN_ERR PFX "Relogin to the tgt\n");
522 mutex_lock(&lport->disc.disc_mutex);
523 lport->tt.rport_login(rdata);
524 mutex_unlock(&lport->disc.disc_mutex);
525 }
526 mutex_unlock(&hba->hba_mutex);
527
528 break;
529
530 case RPORT_EV_NONE:
531 break;
532 }
533}
534
535/**
536 * bnx2fc_tgt_lookup() - Lookup a bnx2fc_rport by port_id
537 *
538 * @port: fcoe_port struct to lookup the target port on
539 * @port_id: The remote port ID to look up
540 */
541struct bnx2fc_rport *bnx2fc_tgt_lookup(struct fcoe_port *port,
542 u32 port_id)
543{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700544 struct bnx2fc_interface *interface = port->priv;
545 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800546 struct bnx2fc_rport *tgt;
547 struct fc_rport_priv *rdata;
548 int i;
549
550 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
551 tgt = hba->tgt_ofld_list[i];
552 if ((tgt) && (tgt->port == port)) {
553 rdata = tgt->rdata;
554 if (rdata->ids.port_id == port_id) {
555 if (rdata->rp_state != RPORT_ST_DELETE) {
556 BNX2FC_TGT_DBG(tgt, "rport "
557 "obtained\n");
558 return tgt;
559 } else {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700560 BNX2FC_TGT_DBG(tgt, "rport 0x%x "
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800561 "is in DELETED state\n",
562 rdata->ids.port_id);
563 return NULL;
564 }
565 }
566 }
567 }
568 return NULL;
569}
570
571
572/**
573 * bnx2fc_alloc_conn_id - allocates FCOE Connection id
574 *
575 * @hba: pointer to adapter structure
576 * @tgt: pointer to bnx2fc_rport structure
577 */
578static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
579 struct bnx2fc_rport *tgt)
580{
581 u32 conn_id, next;
582
583 /* called with hba mutex held */
584
585 /*
586 * tgt_ofld_list access is synchronized using
587 * both hba mutex and hba lock. Atleast hba mutex or
588 * hba lock needs to be held for read access.
589 */
590
591 spin_lock_bh(&hba->hba_lock);
592 next = hba->next_conn_id;
593 conn_id = hba->next_conn_id++;
594 if (hba->next_conn_id == BNX2FC_NUM_MAX_SESS)
595 hba->next_conn_id = 0;
596
597 while (hba->tgt_ofld_list[conn_id] != NULL) {
598 conn_id++;
599 if (conn_id == BNX2FC_NUM_MAX_SESS)
600 conn_id = 0;
601
602 if (conn_id == next) {
603 /* No free conn_ids are available */
604 spin_unlock_bh(&hba->hba_lock);
605 return -1;
606 }
607 }
608 hba->tgt_ofld_list[conn_id] = tgt;
609 tgt->fcoe_conn_id = conn_id;
610 spin_unlock_bh(&hba->hba_lock);
611 return conn_id;
612}
613
614static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id)
615{
616 /* called with hba mutex held */
617 spin_lock_bh(&hba->hba_lock);
618 hba->tgt_ofld_list[conn_id] = NULL;
619 hba->next_conn_id = conn_id;
620 spin_unlock_bh(&hba->hba_lock);
621}
622
623/**
624 *bnx2fc_alloc_session_resc - Allocate qp resources for the session
625 *
626 */
627static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
628 struct bnx2fc_rport *tgt)
629{
630 dma_addr_t page;
631 int num_pages;
632 u32 *pbl;
633
634 /* Allocate and map SQ */
635 tgt->sq_mem_size = tgt->max_sqes * BNX2FC_SQ_WQE_SIZE;
636 tgt->sq_mem_size = (tgt->sq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
637
638 tgt->sq = dma_alloc_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
639 &tgt->sq_dma, GFP_KERNEL);
640 if (!tgt->sq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700641 printk(KERN_ERR PFX "unable to allocate SQ memory %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800642 tgt->sq_mem_size);
643 goto mem_alloc_failure;
644 }
645 memset(tgt->sq, 0, tgt->sq_mem_size);
646
647 /* Allocate and map CQ */
648 tgt->cq_mem_size = tgt->max_cqes * BNX2FC_CQ_WQE_SIZE;
649 tgt->cq_mem_size = (tgt->cq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
650
651 tgt->cq = dma_alloc_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
652 &tgt->cq_dma, GFP_KERNEL);
653 if (!tgt->cq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700654 printk(KERN_ERR PFX "unable to allocate CQ memory %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800655 tgt->cq_mem_size);
656 goto mem_alloc_failure;
657 }
658 memset(tgt->cq, 0, tgt->cq_mem_size);
659
660 /* Allocate and map RQ and RQ PBL */
661 tgt->rq_mem_size = tgt->max_rqes * BNX2FC_RQ_WQE_SIZE;
662 tgt->rq_mem_size = (tgt->rq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
663
664 tgt->rq = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
665 &tgt->rq_dma, GFP_KERNEL);
666 if (!tgt->rq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700667 printk(KERN_ERR PFX "unable to allocate RQ memory %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800668 tgt->rq_mem_size);
669 goto mem_alloc_failure;
670 }
671 memset(tgt->rq, 0, tgt->rq_mem_size);
672
673 tgt->rq_pbl_size = (tgt->rq_mem_size / PAGE_SIZE) * sizeof(void *);
674 tgt->rq_pbl_size = (tgt->rq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
675
676 tgt->rq_pbl = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
677 &tgt->rq_pbl_dma, GFP_KERNEL);
678 if (!tgt->rq_pbl) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700679 printk(KERN_ERR PFX "unable to allocate RQ PBL %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800680 tgt->rq_pbl_size);
681 goto mem_alloc_failure;
682 }
683
684 memset(tgt->rq_pbl, 0, tgt->rq_pbl_size);
685 num_pages = tgt->rq_mem_size / PAGE_SIZE;
686 page = tgt->rq_dma;
687 pbl = (u32 *)tgt->rq_pbl;
688
689 while (num_pages--) {
690 *pbl = (u32)page;
691 pbl++;
692 *pbl = (u32)((u64)page >> 32);
693 pbl++;
694 page += PAGE_SIZE;
695 }
696
697 /* Allocate and map XFERQ */
698 tgt->xferq_mem_size = tgt->max_sqes * BNX2FC_XFERQ_WQE_SIZE;
699 tgt->xferq_mem_size = (tgt->xferq_mem_size + (PAGE_SIZE - 1)) &
700 PAGE_MASK;
701
702 tgt->xferq = dma_alloc_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
703 &tgt->xferq_dma, GFP_KERNEL);
704 if (!tgt->xferq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700705 printk(KERN_ERR PFX "unable to allocate XFERQ %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800706 tgt->xferq_mem_size);
707 goto mem_alloc_failure;
708 }
709 memset(tgt->xferq, 0, tgt->xferq_mem_size);
710
711 /* Allocate and map CONFQ & CONFQ PBL */
712 tgt->confq_mem_size = tgt->max_sqes * BNX2FC_CONFQ_WQE_SIZE;
713 tgt->confq_mem_size = (tgt->confq_mem_size + (PAGE_SIZE - 1)) &
714 PAGE_MASK;
715
716 tgt->confq = dma_alloc_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
717 &tgt->confq_dma, GFP_KERNEL);
718 if (!tgt->confq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700719 printk(KERN_ERR PFX "unable to allocate CONFQ %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800720 tgt->confq_mem_size);
721 goto mem_alloc_failure;
722 }
723 memset(tgt->confq, 0, tgt->confq_mem_size);
724
725 tgt->confq_pbl_size =
726 (tgt->confq_mem_size / PAGE_SIZE) * sizeof(void *);
727 tgt->confq_pbl_size =
728 (tgt->confq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
729
730 tgt->confq_pbl = dma_alloc_coherent(&hba->pcidev->dev,
731 tgt->confq_pbl_size,
732 &tgt->confq_pbl_dma, GFP_KERNEL);
733 if (!tgt->confq_pbl) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700734 printk(KERN_ERR PFX "unable to allocate CONFQ PBL %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800735 tgt->confq_pbl_size);
736 goto mem_alloc_failure;
737 }
738
739 memset(tgt->confq_pbl, 0, tgt->confq_pbl_size);
740 num_pages = tgt->confq_mem_size / PAGE_SIZE;
741 page = tgt->confq_dma;
742 pbl = (u32 *)tgt->confq_pbl;
743
744 while (num_pages--) {
745 *pbl = (u32)page;
746 pbl++;
747 *pbl = (u32)((u64)page >> 32);
748 pbl++;
749 page += PAGE_SIZE;
750 }
751
752 /* Allocate and map ConnDB */
753 tgt->conn_db_mem_size = sizeof(struct fcoe_conn_db);
754
755 tgt->conn_db = dma_alloc_coherent(&hba->pcidev->dev,
756 tgt->conn_db_mem_size,
757 &tgt->conn_db_dma, GFP_KERNEL);
758 if (!tgt->conn_db) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700759 printk(KERN_ERR PFX "unable to allocate conn_db %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800760 tgt->conn_db_mem_size);
761 goto mem_alloc_failure;
762 }
763 memset(tgt->conn_db, 0, tgt->conn_db_mem_size);
764
765
766 /* Allocate and map LCQ */
767 tgt->lcq_mem_size = (tgt->max_sqes + 8) * BNX2FC_SQ_WQE_SIZE;
768 tgt->lcq_mem_size = (tgt->lcq_mem_size + (PAGE_SIZE - 1)) &
769 PAGE_MASK;
770
771 tgt->lcq = dma_alloc_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
772 &tgt->lcq_dma, GFP_KERNEL);
773
774 if (!tgt->lcq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700775 printk(KERN_ERR PFX "unable to allocate lcq %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800776 tgt->lcq_mem_size);
777 goto mem_alloc_failure;
778 }
779 memset(tgt->lcq, 0, tgt->lcq_mem_size);
780
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800781 tgt->conn_db->rq_prod = 0x8000;
782
783 return 0;
784
785mem_alloc_failure:
786 bnx2fc_free_session_resc(hba, tgt);
787 bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
788 return -ENOMEM;
789}
790
791/**
792 * bnx2i_free_session_resc - free qp resources for the session
793 *
794 * @hba: adapter structure pointer
795 * @tgt: bnx2fc_rport structure pointer
796 *
797 * Free QP resources - SQ/RQ/CQ/XFERQ memory and PBL
798 */
799static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
800 struct bnx2fc_rport *tgt)
801{
802 BNX2FC_TGT_DBG(tgt, "Freeing up session resources\n");
803
804 if (tgt->ctx_base) {
805 iounmap(tgt->ctx_base);
806 tgt->ctx_base = NULL;
807 }
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300808
809 spin_lock_bh(&tgt->cq_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800810 /* Free LCQ */
811 if (tgt->lcq) {
812 dma_free_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
813 tgt->lcq, tgt->lcq_dma);
814 tgt->lcq = NULL;
815 }
816 /* Free connDB */
817 if (tgt->conn_db) {
818 dma_free_coherent(&hba->pcidev->dev, tgt->conn_db_mem_size,
819 tgt->conn_db, tgt->conn_db_dma);
820 tgt->conn_db = NULL;
821 }
822 /* Free confq and confq pbl */
823 if (tgt->confq_pbl) {
824 dma_free_coherent(&hba->pcidev->dev, tgt->confq_pbl_size,
825 tgt->confq_pbl, tgt->confq_pbl_dma);
826 tgt->confq_pbl = NULL;
827 }
828 if (tgt->confq) {
829 dma_free_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
830 tgt->confq, tgt->confq_dma);
831 tgt->confq = NULL;
832 }
833 /* Free XFERQ */
834 if (tgt->xferq) {
835 dma_free_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
836 tgt->xferq, tgt->xferq_dma);
837 tgt->xferq = NULL;
838 }
839 /* Free RQ PBL and RQ */
840 if (tgt->rq_pbl) {
841 dma_free_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
842 tgt->rq_pbl, tgt->rq_pbl_dma);
843 tgt->rq_pbl = NULL;
844 }
845 if (tgt->rq) {
846 dma_free_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
847 tgt->rq, tgt->rq_dma);
848 tgt->rq = NULL;
849 }
850 /* Free CQ */
851 if (tgt->cq) {
852 dma_free_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
853 tgt->cq, tgt->cq_dma);
854 tgt->cq = NULL;
855 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800856 /* Free SQ */
857 if (tgt->sq) {
858 dma_free_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
859 tgt->sq, tgt->sq_dma);
860 tgt->sq = NULL;
861 }
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300862 spin_unlock_bh(&tgt->cq_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800863}