1 /* bnx2fc_tgt.c: QLogic 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.
5 * Copyright (c) 2008 - 2013 Broadcom Corporation
6 * Copyright (c) 2014, QLogic Corporation
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.
12 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
16 static void bnx2fc_upld_timer(unsigned long data
);
17 static void bnx2fc_ofld_timer(unsigned long data
);
18 static int bnx2fc_init_tgt(struct bnx2fc_rport
*tgt
,
19 struct fcoe_port
*port
,
20 struct fc_rport_priv
*rdata
);
21 static u32
bnx2fc_alloc_conn_id(struct bnx2fc_hba
*hba
,
22 struct bnx2fc_rport
*tgt
);
23 static int bnx2fc_alloc_session_resc(struct bnx2fc_hba
*hba
,
24 struct bnx2fc_rport
*tgt
);
25 static void bnx2fc_free_session_resc(struct bnx2fc_hba
*hba
,
26 struct bnx2fc_rport
*tgt
);
27 static void bnx2fc_free_conn_id(struct bnx2fc_hba
*hba
, u32 conn_id
);
29 static void bnx2fc_upld_timer(unsigned long data
)
32 struct bnx2fc_rport
*tgt
= (struct bnx2fc_rport
*)data
;
34 BNX2FC_TGT_DBG(tgt
, "upld_timer - Upload compl not received!!\n");
35 /* fake upload completion */
36 clear_bit(BNX2FC_FLAG_OFFLOADED
, &tgt
->flags
);
37 clear_bit(BNX2FC_FLAG_ENABLED
, &tgt
->flags
);
38 set_bit(BNX2FC_FLAG_UPLD_REQ_COMPL
, &tgt
->flags
);
39 wake_up_interruptible(&tgt
->upld_wait
);
42 static void bnx2fc_ofld_timer(unsigned long data
)
45 struct bnx2fc_rport
*tgt
= (struct bnx2fc_rport
*)data
;
47 BNX2FC_TGT_DBG(tgt
, "entered bnx2fc_ofld_timer\n");
48 /* NOTE: This function should never be called, as
49 * offload should never timeout
52 * If the timer has expired, this session is dead
53 * Clear offloaded flag and logout of this device.
54 * Since OFFLOADED flag is cleared, this case
55 * will be considered as offload error and the
56 * port will be logged off, and conn_id, session
57 * resources are freed up in bnx2fc_offload_session
59 clear_bit(BNX2FC_FLAG_OFFLOADED
, &tgt
->flags
);
60 clear_bit(BNX2FC_FLAG_ENABLED
, &tgt
->flags
);
61 set_bit(BNX2FC_FLAG_OFLD_REQ_CMPL
, &tgt
->flags
);
62 wake_up_interruptible(&tgt
->ofld_wait
);
65 static void bnx2fc_ofld_wait(struct bnx2fc_rport
*tgt
)
67 setup_timer(&tgt
->ofld_timer
, bnx2fc_ofld_timer
, (unsigned long)tgt
);
68 mod_timer(&tgt
->ofld_timer
, jiffies
+ BNX2FC_FW_TIMEOUT
);
70 wait_event_interruptible(tgt
->ofld_wait
,
72 BNX2FC_FLAG_OFLD_REQ_CMPL
,
74 if (signal_pending(current
))
75 flush_signals(current
);
76 del_timer_sync(&tgt
->ofld_timer
);
79 static void bnx2fc_offload_session(struct fcoe_port
*port
,
80 struct bnx2fc_rport
*tgt
,
81 struct fc_rport_priv
*rdata
)
83 struct fc_lport
*lport
= rdata
->local_port
;
84 struct fc_rport
*rport
= rdata
->rport
;
85 struct bnx2fc_interface
*interface
= port
->priv
;
86 struct bnx2fc_hba
*hba
= interface
->hba
;
90 /* Initialize bnx2fc_rport */
91 /* NOTE: tgt is already bzero'd */
92 rval
= bnx2fc_init_tgt(tgt
, port
, rdata
);
94 printk(KERN_ERR PFX
"Failed to allocate conn id for "
95 "port_id (%6x)\n", rport
->port_id
);
99 /* Allocate session resources */
100 rval
= bnx2fc_alloc_session_resc(hba
, tgt
);
102 printk(KERN_ERR PFX
"Failed to allocate resources\n");
107 * Initialize FCoE session offload process.
108 * Upon completion of offload process add
109 * rport to list of rports
112 clear_bit(BNX2FC_FLAG_OFLD_REQ_CMPL
, &tgt
->flags
);
113 rval
= bnx2fc_send_session_ofld_req(port
, tgt
);
115 printk(KERN_ERR PFX
"ofld_req failed\n");
120 * wait for the session is offloaded and enabled. 3 Secs
121 * should be ample time for this process to complete.
123 bnx2fc_ofld_wait(tgt
);
125 if (!(test_bit(BNX2FC_FLAG_OFFLOADED
, &tgt
->flags
))) {
126 if (test_and_clear_bit(BNX2FC_FLAG_CTX_ALLOC_FAILURE
,
128 BNX2FC_TGT_DBG(tgt
, "ctx_alloc_failure, "
129 "retry ofld..%d\n", i
++);
130 msleep_interruptible(1000);
139 if (bnx2fc_map_doorbell(tgt
)) {
140 printk(KERN_ERR PFX
"map doorbell failed - no mem\n");
143 clear_bit(BNX2FC_FLAG_OFLD_REQ_CMPL
, &tgt
->flags
);
144 rval
= bnx2fc_send_session_enable_req(port
, tgt
);
146 pr_err(PFX
"enable session failed\n");
149 bnx2fc_ofld_wait(tgt
);
150 if (!(test_bit(BNX2FC_FLAG_ENABLED
, &tgt
->flags
)))
155 /* couldn't offload the session. log off from this rport */
156 BNX2FC_TGT_DBG(tgt
, "bnx2fc_offload_session - offload error\n");
157 clear_bit(BNX2FC_FLAG_OFFLOADED
, &tgt
->flags
);
158 /* Free session resources */
159 bnx2fc_free_session_resc(hba
, tgt
);
161 if (tgt
->fcoe_conn_id
!= -1)
162 bnx2fc_free_conn_id(hba
, tgt
->fcoe_conn_id
);
163 lport
->tt
.rport_logoff(rdata
);
166 void bnx2fc_flush_active_ios(struct bnx2fc_rport
*tgt
)
168 struct bnx2fc_cmd
*io_req
;
169 struct bnx2fc_cmd
*tmp
;
172 BNX2FC_TGT_DBG(tgt
, "Entered flush_active_ios - %d\n",
173 tgt
->num_active_ios
.counter
);
175 spin_lock_bh(&tgt
->tgt_lock
);
176 tgt
->flush_in_prog
= 1;
178 list_for_each_entry_safe(io_req
, tmp
, &tgt
->active_cmd_queue
, link
) {
180 list_del_init(&io_req
->link
);
181 io_req
->on_active_queue
= 0;
182 BNX2FC_IO_DBG(io_req
, "cmd_queue cleanup\n");
184 if (cancel_delayed_work(&io_req
->timeout_work
)) {
185 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT
,
186 &io_req
->req_flags
)) {
187 /* Handle eh_abort timeout */
188 BNX2FC_IO_DBG(io_req
, "eh_abort for IO "
190 complete(&io_req
->tm_done
);
192 kref_put(&io_req
->refcount
,
193 bnx2fc_cmd_release
); /* drop timer hold */
196 set_bit(BNX2FC_FLAG_IO_COMPL
, &io_req
->req_flags
);
197 set_bit(BNX2FC_FLAG_IO_CLEANUP
, &io_req
->req_flags
);
199 /* Do not issue cleanup when disable request failed */
200 if (test_bit(BNX2FC_FLAG_DISABLE_FAILED
, &tgt
->flags
))
201 bnx2fc_process_cleanup_compl(io_req
, io_req
->task
, 0);
203 rc
= bnx2fc_initiate_cleanup(io_req
);
208 list_for_each_entry_safe(io_req
, tmp
, &tgt
->active_tm_queue
, link
) {
210 list_del_init(&io_req
->link
);
211 io_req
->on_tmf_queue
= 0;
212 BNX2FC_IO_DBG(io_req
, "tm_queue cleanup\n");
213 if (io_req
->wait_for_comp
)
214 complete(&io_req
->tm_done
);
217 list_for_each_entry_safe(io_req
, tmp
, &tgt
->els_queue
, link
) {
219 list_del_init(&io_req
->link
);
220 io_req
->on_active_queue
= 0;
222 BNX2FC_IO_DBG(io_req
, "els_queue cleanup\n");
224 if (cancel_delayed_work(&io_req
->timeout_work
))
225 kref_put(&io_req
->refcount
,
226 bnx2fc_cmd_release
); /* drop timer hold */
228 if ((io_req
->cb_func
) && (io_req
->cb_arg
)) {
229 io_req
->cb_func(io_req
->cb_arg
);
230 io_req
->cb_arg
= NULL
;
233 /* Do not issue cleanup when disable request failed */
234 if (test_bit(BNX2FC_FLAG_DISABLE_FAILED
, &tgt
->flags
))
235 bnx2fc_process_cleanup_compl(io_req
, io_req
->task
, 0);
237 rc
= bnx2fc_initiate_cleanup(io_req
);
242 list_for_each_entry_safe(io_req
, tmp
, &tgt
->io_retire_queue
, link
) {
244 list_del_init(&io_req
->link
);
246 BNX2FC_IO_DBG(io_req
, "retire_queue flush\n");
248 if (cancel_delayed_work(&io_req
->timeout_work
)) {
249 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT
,
250 &io_req
->req_flags
)) {
251 /* Handle eh_abort timeout */
252 BNX2FC_IO_DBG(io_req
, "eh_abort for IO "
254 if (io_req
->wait_for_comp
)
255 complete(&io_req
->tm_done
);
257 kref_put(&io_req
->refcount
, bnx2fc_cmd_release
);
260 clear_bit(BNX2FC_FLAG_ISSUE_RRQ
, &io_req
->req_flags
);
263 BNX2FC_TGT_DBG(tgt
, "IOs flushed = %d\n", i
);
265 spin_unlock_bh(&tgt
->tgt_lock
);
266 /* wait for active_ios to go to 0 */
267 while ((tgt
->num_active_ios
.counter
!= 0) && (i
++ < BNX2FC_WAIT_CNT
))
269 if (tgt
->num_active_ios
.counter
!= 0)
270 printk(KERN_ERR PFX
"CLEANUP on port 0x%x:"
271 " active_ios = %d\n",
272 tgt
->rdata
->ids
.port_id
, tgt
->num_active_ios
.counter
);
273 spin_lock_bh(&tgt
->tgt_lock
);
274 tgt
->flush_in_prog
= 0;
275 spin_unlock_bh(&tgt
->tgt_lock
);
278 static void bnx2fc_upld_wait(struct bnx2fc_rport
*tgt
)
280 setup_timer(&tgt
->upld_timer
, bnx2fc_upld_timer
, (unsigned long)tgt
);
281 mod_timer(&tgt
->upld_timer
, jiffies
+ BNX2FC_FW_TIMEOUT
);
282 wait_event_interruptible(tgt
->upld_wait
,
284 BNX2FC_FLAG_UPLD_REQ_COMPL
,
286 if (signal_pending(current
))
287 flush_signals(current
);
288 del_timer_sync(&tgt
->upld_timer
);
291 static void bnx2fc_upload_session(struct fcoe_port
*port
,
292 struct bnx2fc_rport
*tgt
)
294 struct bnx2fc_interface
*interface
= port
->priv
;
295 struct bnx2fc_hba
*hba
= interface
->hba
;
297 BNX2FC_TGT_DBG(tgt
, "upload_session: active_ios = %d\n",
298 tgt
->num_active_ios
.counter
);
301 * Called with hba->hba_mutex held.
302 * This is a blocking call
304 clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL
, &tgt
->flags
);
305 bnx2fc_send_session_disable_req(port
, tgt
);
308 * wait for upload to complete. 3 Secs
309 * should be sufficient time for this process to complete.
311 BNX2FC_TGT_DBG(tgt
, "waiting for disable compl\n");
312 bnx2fc_upld_wait(tgt
);
315 * traverse thru the active_q and tmf_q and cleanup
318 BNX2FC_TGT_DBG(tgt
, "flush/upload - disable wait flags = 0x%lx\n",
320 bnx2fc_flush_active_ios(tgt
);
322 /* Issue destroy KWQE */
323 if (test_bit(BNX2FC_FLAG_DISABLED
, &tgt
->flags
)) {
324 BNX2FC_TGT_DBG(tgt
, "send destroy req\n");
325 clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL
, &tgt
->flags
);
326 bnx2fc_send_session_destroy_req(hba
, tgt
);
328 /* wait for destroy to complete */
329 bnx2fc_upld_wait(tgt
);
331 if (!(test_bit(BNX2FC_FLAG_DESTROYED
, &tgt
->flags
)))
332 printk(KERN_ERR PFX
"ERROR!! destroy timed out\n");
334 BNX2FC_TGT_DBG(tgt
, "destroy wait complete flags = 0x%lx\n",
337 } else if (test_bit(BNX2FC_FLAG_DISABLE_FAILED
, &tgt
->flags
)) {
338 printk(KERN_ERR PFX
"ERROR!! DISABLE req failed, destroy"
339 " not sent to FW\n");
341 printk(KERN_ERR PFX
"ERROR!! DISABLE req timed out, destroy"
342 " not sent to FW\n");
345 /* Free session resources */
346 bnx2fc_free_session_resc(hba
, tgt
);
347 bnx2fc_free_conn_id(hba
, tgt
->fcoe_conn_id
);
350 static int bnx2fc_init_tgt(struct bnx2fc_rport
*tgt
,
351 struct fcoe_port
*port
,
352 struct fc_rport_priv
*rdata
)
355 struct fc_rport
*rport
= rdata
->rport
;
356 struct bnx2fc_interface
*interface
= port
->priv
;
357 struct bnx2fc_hba
*hba
= interface
->hba
;
358 struct b577xx_doorbell_set_prod
*sq_db
= &tgt
->sq_db
;
359 struct b577xx_fcoe_rx_doorbell
*rx_db
= &tgt
->rx_db
;
365 if (hba
->num_ofld_sess
>= BNX2FC_NUM_MAX_SESS
) {
366 BNX2FC_TGT_DBG(tgt
, "exceeded max sessions. logoff this tgt\n");
367 tgt
->fcoe_conn_id
= -1;
371 tgt
->fcoe_conn_id
= bnx2fc_alloc_conn_id(hba
, tgt
);
372 if (tgt
->fcoe_conn_id
== -1)
375 BNX2FC_TGT_DBG(tgt
, "init_tgt - conn_id = 0x%x\n", tgt
->fcoe_conn_id
);
377 tgt
->max_sqes
= BNX2FC_SQ_WQES_MAX
;
378 tgt
->max_rqes
= BNX2FC_RQ_WQES_MAX
;
379 tgt
->max_cqes
= BNX2FC_CQ_WQES_MAX
;
380 atomic_set(&tgt
->free_sqes
, BNX2FC_SQ_WQES_MAX
);
382 /* Initialize the toggle bit */
383 tgt
->sq_curr_toggle_bit
= 1;
384 tgt
->cq_curr_toggle_bit
= 1;
385 tgt
->sq_prod_idx
= 0;
386 tgt
->cq_cons_idx
= 0;
387 tgt
->rq_prod_idx
= 0x8000;
388 tgt
->rq_cons_idx
= 0;
389 atomic_set(&tgt
->num_active_ios
, 0);
390 tgt
->retry_delay_timestamp
= 0;
392 if (rdata
->flags
& FC_RP_FLAGS_RETRY
&&
393 rdata
->ids
.roles
& FC_RPORT_ROLE_FCP_TARGET
&&
394 !(rdata
->ids
.roles
& FC_RPORT_ROLE_FCP_INITIATOR
)) {
395 tgt
->dev_type
= TYPE_TAPE
;
396 tgt
->io_timeout
= 0; /* use default ULP timeout */
398 tgt
->dev_type
= TYPE_DISK
;
399 tgt
->io_timeout
= BNX2FC_IO_TIMEOUT
;
402 /* initialize sq doorbell */
403 sq_db
->header
.header
= B577XX_DOORBELL_HDR_DB_TYPE
;
404 sq_db
->header
.header
|= B577XX_FCOE_CONNECTION_TYPE
<<
405 B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT
;
406 /* initialize rx doorbell */
407 rx_db
->hdr
.header
= ((0x1 << B577XX_DOORBELL_HDR_RX_SHIFT
) |
408 (0x1 << B577XX_DOORBELL_HDR_DB_TYPE_SHIFT
) |
409 (B577XX_FCOE_CONNECTION_TYPE
<<
410 B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT
));
411 rx_db
->params
= (0x2 << B577XX_FCOE_RX_DOORBELL_NEGATIVE_ARM_SHIFT
) |
412 (0x3 << B577XX_FCOE_RX_DOORBELL_OPCODE_SHIFT
);
414 spin_lock_init(&tgt
->tgt_lock
);
415 spin_lock_init(&tgt
->cq_lock
);
417 /* Initialize active_cmd_queue list */
418 INIT_LIST_HEAD(&tgt
->active_cmd_queue
);
420 /* Initialize IO retire queue */
421 INIT_LIST_HEAD(&tgt
->io_retire_queue
);
423 INIT_LIST_HEAD(&tgt
->els_queue
);
425 /* Initialize active_tm_queue list */
426 INIT_LIST_HEAD(&tgt
->active_tm_queue
);
428 init_waitqueue_head(&tgt
->ofld_wait
);
429 init_waitqueue_head(&tgt
->upld_wait
);
435 * This event_callback is called after successful completion of libfc
436 * initiated target login. bnx2fc can proceed with initiating the session
439 void bnx2fc_rport_event_handler(struct fc_lport
*lport
,
440 struct fc_rport_priv
*rdata
,
441 enum fc_rport_event event
)
443 struct fcoe_port
*port
= lport_priv(lport
);
444 struct bnx2fc_interface
*interface
= port
->priv
;
445 struct bnx2fc_hba
*hba
= interface
->hba
;
446 struct fc_rport
*rport
= rdata
->rport
;
447 struct fc_rport_libfc_priv
*rp
;
448 struct bnx2fc_rport
*tgt
;
451 BNX2FC_HBA_DBG(lport
, "rport_event_hdlr: event = %d, port_id = 0x%x\n",
452 event
, rdata
->ids
.port_id
);
456 printk(KERN_ERR PFX
"rport is NULL: ERROR!\n");
461 if (rport
->port_id
== FC_FID_DIR_SERV
) {
463 * bnx2fc_rport structure doesn't exist for
465 * We should not come here, as lport will
466 * take care of fabric login
468 printk(KERN_ERR PFX
"%x - rport_event_handler ERROR\n",
473 if (rdata
->spp_type
!= FC_TYPE_FCP
) {
474 BNX2FC_HBA_DBG(lport
, "not FCP type target."
475 " not offloading\n");
478 if (!(rdata
->ids
.roles
& FC_RPORT_ROLE_FCP_TARGET
)) {
479 BNX2FC_HBA_DBG(lport
, "not FCP_TARGET"
480 " not offloading\n");
485 * Offlaod process is protected with hba mutex.
486 * Use the same mutex_lock for upload process too
488 mutex_lock(&hba
->hba_mutex
);
489 tgt
= (struct bnx2fc_rport
*)&rp
[1];
491 /* This can happen when ADISC finds the same target */
492 if (test_bit(BNX2FC_FLAG_ENABLED
, &tgt
->flags
)) {
493 BNX2FC_TGT_DBG(tgt
, "already offloaded\n");
494 mutex_unlock(&hba
->hba_mutex
);
499 * Offload the session. This is a blocking call, and will
500 * wait until the session is offloaded.
502 bnx2fc_offload_session(port
, tgt
, rdata
);
504 BNX2FC_TGT_DBG(tgt
, "OFFLOAD num_ofld_sess = %d\n",
507 if (test_bit(BNX2FC_FLAG_ENABLED
, &tgt
->flags
)) {
508 /* Session is offloaded and enabled. */
509 BNX2FC_TGT_DBG(tgt
, "sess offloaded\n");
510 /* This counter is protected with hba mutex */
511 hba
->num_ofld_sess
++;
513 set_bit(BNX2FC_FLAG_SESSION_READY
, &tgt
->flags
);
516 * Offload or enable would have failed.
517 * In offload/enable completion path, the
518 * rport would have already been removed
520 BNX2FC_TGT_DBG(tgt
, "Port is being logged off as "
521 "offloaded flag not set\n");
523 mutex_unlock(&hba
->hba_mutex
);
526 case RPORT_EV_FAILED
:
528 port_id
= rdata
->ids
.port_id
;
529 if (port_id
== FC_FID_DIR_SERV
)
533 printk(KERN_INFO PFX
"%x - rport not created Yet!!\n",
538 mutex_lock(&hba
->hba_mutex
);
540 * Perform session upload. Note that rdata->peers is already
541 * removed from disc->rports list before we get this event.
543 tgt
= (struct bnx2fc_rport
*)&rp
[1];
545 if (!(test_bit(BNX2FC_FLAG_ENABLED
, &tgt
->flags
))) {
546 mutex_unlock(&hba
->hba_mutex
);
549 clear_bit(BNX2FC_FLAG_SESSION_READY
, &tgt
->flags
);
551 bnx2fc_upload_session(port
, tgt
);
552 hba
->num_ofld_sess
--;
553 BNX2FC_TGT_DBG(tgt
, "UPLOAD num_ofld_sess = %d\n",
556 * Try to wake up the linkdown wait thread. If num_ofld_sess
557 * is 0, the waiting therad wakes up
559 if ((hba
->wait_for_link_down
) &&
560 (hba
->num_ofld_sess
== 0)) {
561 wake_up_interruptible(&hba
->shutdown_wait
);
563 if (test_bit(BNX2FC_FLAG_EXPL_LOGO
, &tgt
->flags
)) {
564 printk(KERN_ERR PFX
"Relogin to the tgt\n");
565 mutex_lock(&lport
->disc
.disc_mutex
);
566 lport
->tt
.rport_login(rdata
);
567 mutex_unlock(&lport
->disc
.disc_mutex
);
569 mutex_unlock(&hba
->hba_mutex
);
579 * bnx2fc_tgt_lookup() - Lookup a bnx2fc_rport by port_id
581 * @port: fcoe_port struct to lookup the target port on
582 * @port_id: The remote port ID to look up
584 struct bnx2fc_rport
*bnx2fc_tgt_lookup(struct fcoe_port
*port
,
587 struct bnx2fc_interface
*interface
= port
->priv
;
588 struct bnx2fc_hba
*hba
= interface
->hba
;
589 struct bnx2fc_rport
*tgt
;
590 struct fc_rport_priv
*rdata
;
593 for (i
= 0; i
< BNX2FC_NUM_MAX_SESS
; i
++) {
594 tgt
= hba
->tgt_ofld_list
[i
];
595 if ((tgt
) && (tgt
->port
== port
)) {
597 if (rdata
->ids
.port_id
== port_id
) {
598 if (rdata
->rp_state
!= RPORT_ST_DELETE
) {
599 BNX2FC_TGT_DBG(tgt
, "rport "
603 BNX2FC_TGT_DBG(tgt
, "rport 0x%x "
604 "is in DELETED state\n",
616 * bnx2fc_alloc_conn_id - allocates FCOE Connection id
618 * @hba: pointer to adapter structure
619 * @tgt: pointer to bnx2fc_rport structure
621 static u32
bnx2fc_alloc_conn_id(struct bnx2fc_hba
*hba
,
622 struct bnx2fc_rport
*tgt
)
626 /* called with hba mutex held */
629 * tgt_ofld_list access is synchronized using
630 * both hba mutex and hba lock. Atleast hba mutex or
631 * hba lock needs to be held for read access.
634 spin_lock_bh(&hba
->hba_lock
);
635 next
= hba
->next_conn_id
;
636 conn_id
= hba
->next_conn_id
++;
637 if (hba
->next_conn_id
== BNX2FC_NUM_MAX_SESS
)
638 hba
->next_conn_id
= 0;
640 while (hba
->tgt_ofld_list
[conn_id
] != NULL
) {
642 if (conn_id
== BNX2FC_NUM_MAX_SESS
)
645 if (conn_id
== next
) {
646 /* No free conn_ids are available */
647 spin_unlock_bh(&hba
->hba_lock
);
651 hba
->tgt_ofld_list
[conn_id
] = tgt
;
652 tgt
->fcoe_conn_id
= conn_id
;
653 spin_unlock_bh(&hba
->hba_lock
);
657 static void bnx2fc_free_conn_id(struct bnx2fc_hba
*hba
, u32 conn_id
)
659 /* called with hba mutex held */
660 spin_lock_bh(&hba
->hba_lock
);
661 hba
->tgt_ofld_list
[conn_id
] = NULL
;
662 spin_unlock_bh(&hba
->hba_lock
);
666 *bnx2fc_alloc_session_resc - Allocate qp resources for the session
669 static int bnx2fc_alloc_session_resc(struct bnx2fc_hba
*hba
,
670 struct bnx2fc_rport
*tgt
)
676 /* Allocate and map SQ */
677 tgt
->sq_mem_size
= tgt
->max_sqes
* BNX2FC_SQ_WQE_SIZE
;
678 tgt
->sq_mem_size
= (tgt
->sq_mem_size
+ (CNIC_PAGE_SIZE
- 1)) &
681 tgt
->sq
= dma_alloc_coherent(&hba
->pcidev
->dev
, tgt
->sq_mem_size
,
682 &tgt
->sq_dma
, GFP_KERNEL
);
684 printk(KERN_ERR PFX
"unable to allocate SQ memory %d\n",
686 goto mem_alloc_failure
;
688 memset(tgt
->sq
, 0, tgt
->sq_mem_size
);
690 /* Allocate and map CQ */
691 tgt
->cq_mem_size
= tgt
->max_cqes
* BNX2FC_CQ_WQE_SIZE
;
692 tgt
->cq_mem_size
= (tgt
->cq_mem_size
+ (CNIC_PAGE_SIZE
- 1)) &
695 tgt
->cq
= dma_alloc_coherent(&hba
->pcidev
->dev
, tgt
->cq_mem_size
,
696 &tgt
->cq_dma
, GFP_KERNEL
);
698 printk(KERN_ERR PFX
"unable to allocate CQ memory %d\n",
700 goto mem_alloc_failure
;
702 memset(tgt
->cq
, 0, tgt
->cq_mem_size
);
704 /* Allocate and map RQ and RQ PBL */
705 tgt
->rq_mem_size
= tgt
->max_rqes
* BNX2FC_RQ_WQE_SIZE
;
706 tgt
->rq_mem_size
= (tgt
->rq_mem_size
+ (CNIC_PAGE_SIZE
- 1)) &
709 tgt
->rq
= dma_alloc_coherent(&hba
->pcidev
->dev
, tgt
->rq_mem_size
,
710 &tgt
->rq_dma
, GFP_KERNEL
);
712 printk(KERN_ERR PFX
"unable to allocate RQ memory %d\n",
714 goto mem_alloc_failure
;
716 memset(tgt
->rq
, 0, tgt
->rq_mem_size
);
718 tgt
->rq_pbl_size
= (tgt
->rq_mem_size
/ CNIC_PAGE_SIZE
) * sizeof(void *);
719 tgt
->rq_pbl_size
= (tgt
->rq_pbl_size
+ (CNIC_PAGE_SIZE
- 1)) &
722 tgt
->rq_pbl
= dma_alloc_coherent(&hba
->pcidev
->dev
, tgt
->rq_pbl_size
,
723 &tgt
->rq_pbl_dma
, GFP_KERNEL
);
725 printk(KERN_ERR PFX
"unable to allocate RQ PBL %d\n",
727 goto mem_alloc_failure
;
730 memset(tgt
->rq_pbl
, 0, tgt
->rq_pbl_size
);
731 num_pages
= tgt
->rq_mem_size
/ CNIC_PAGE_SIZE
;
733 pbl
= (u32
*)tgt
->rq_pbl
;
735 while (num_pages
--) {
738 *pbl
= (u32
)((u64
)page
>> 32);
740 page
+= CNIC_PAGE_SIZE
;
743 /* Allocate and map XFERQ */
744 tgt
->xferq_mem_size
= tgt
->max_sqes
* BNX2FC_XFERQ_WQE_SIZE
;
745 tgt
->xferq_mem_size
= (tgt
->xferq_mem_size
+ (CNIC_PAGE_SIZE
- 1)) &
748 tgt
->xferq
= dma_alloc_coherent(&hba
->pcidev
->dev
, tgt
->xferq_mem_size
,
749 &tgt
->xferq_dma
, GFP_KERNEL
);
751 printk(KERN_ERR PFX
"unable to allocate XFERQ %d\n",
752 tgt
->xferq_mem_size
);
753 goto mem_alloc_failure
;
755 memset(tgt
->xferq
, 0, tgt
->xferq_mem_size
);
757 /* Allocate and map CONFQ & CONFQ PBL */
758 tgt
->confq_mem_size
= tgt
->max_sqes
* BNX2FC_CONFQ_WQE_SIZE
;
759 tgt
->confq_mem_size
= (tgt
->confq_mem_size
+ (CNIC_PAGE_SIZE
- 1)) &
762 tgt
->confq
= dma_alloc_coherent(&hba
->pcidev
->dev
, tgt
->confq_mem_size
,
763 &tgt
->confq_dma
, GFP_KERNEL
);
765 printk(KERN_ERR PFX
"unable to allocate CONFQ %d\n",
766 tgt
->confq_mem_size
);
767 goto mem_alloc_failure
;
769 memset(tgt
->confq
, 0, tgt
->confq_mem_size
);
771 tgt
->confq_pbl_size
=
772 (tgt
->confq_mem_size
/ CNIC_PAGE_SIZE
) * sizeof(void *);
773 tgt
->confq_pbl_size
=
774 (tgt
->confq_pbl_size
+ (CNIC_PAGE_SIZE
- 1)) & CNIC_PAGE_MASK
;
776 tgt
->confq_pbl
= dma_alloc_coherent(&hba
->pcidev
->dev
,
778 &tgt
->confq_pbl_dma
, GFP_KERNEL
);
779 if (!tgt
->confq_pbl
) {
780 printk(KERN_ERR PFX
"unable to allocate CONFQ PBL %d\n",
781 tgt
->confq_pbl_size
);
782 goto mem_alloc_failure
;
785 memset(tgt
->confq_pbl
, 0, tgt
->confq_pbl_size
);
786 num_pages
= tgt
->confq_mem_size
/ CNIC_PAGE_SIZE
;
787 page
= tgt
->confq_dma
;
788 pbl
= (u32
*)tgt
->confq_pbl
;
790 while (num_pages
--) {
793 *pbl
= (u32
)((u64
)page
>> 32);
795 page
+= CNIC_PAGE_SIZE
;
798 /* Allocate and map ConnDB */
799 tgt
->conn_db_mem_size
= sizeof(struct fcoe_conn_db
);
801 tgt
->conn_db
= dma_alloc_coherent(&hba
->pcidev
->dev
,
802 tgt
->conn_db_mem_size
,
803 &tgt
->conn_db_dma
, GFP_KERNEL
);
805 printk(KERN_ERR PFX
"unable to allocate conn_db %d\n",
806 tgt
->conn_db_mem_size
);
807 goto mem_alloc_failure
;
809 memset(tgt
->conn_db
, 0, tgt
->conn_db_mem_size
);
812 /* Allocate and map LCQ */
813 tgt
->lcq_mem_size
= (tgt
->max_sqes
+ 8) * BNX2FC_SQ_WQE_SIZE
;
814 tgt
->lcq_mem_size
= (tgt
->lcq_mem_size
+ (CNIC_PAGE_SIZE
- 1)) &
817 tgt
->lcq
= dma_alloc_coherent(&hba
->pcidev
->dev
, tgt
->lcq_mem_size
,
818 &tgt
->lcq_dma
, GFP_KERNEL
);
821 printk(KERN_ERR PFX
"unable to allocate lcq %d\n",
823 goto mem_alloc_failure
;
825 memset(tgt
->lcq
, 0, tgt
->lcq_mem_size
);
827 tgt
->conn_db
->rq_prod
= 0x8000;
836 * bnx2i_free_session_resc - free qp resources for the session
838 * @hba: adapter structure pointer
839 * @tgt: bnx2fc_rport structure pointer
841 * Free QP resources - SQ/RQ/CQ/XFERQ memory and PBL
843 static void bnx2fc_free_session_resc(struct bnx2fc_hba
*hba
,
844 struct bnx2fc_rport
*tgt
)
846 void __iomem
*ctx_base_ptr
;
848 BNX2FC_TGT_DBG(tgt
, "Freeing up session resources\n");
850 spin_lock_bh(&tgt
->cq_lock
);
851 ctx_base_ptr
= tgt
->ctx_base
;
852 tgt
->ctx_base
= NULL
;
856 dma_free_coherent(&hba
->pcidev
->dev
, tgt
->lcq_mem_size
,
857 tgt
->lcq
, tgt
->lcq_dma
);
862 dma_free_coherent(&hba
->pcidev
->dev
, tgt
->conn_db_mem_size
,
863 tgt
->conn_db
, tgt
->conn_db_dma
);
866 /* Free confq and confq pbl */
867 if (tgt
->confq_pbl
) {
868 dma_free_coherent(&hba
->pcidev
->dev
, tgt
->confq_pbl_size
,
869 tgt
->confq_pbl
, tgt
->confq_pbl_dma
);
870 tgt
->confq_pbl
= NULL
;
873 dma_free_coherent(&hba
->pcidev
->dev
, tgt
->confq_mem_size
,
874 tgt
->confq
, tgt
->confq_dma
);
879 dma_free_coherent(&hba
->pcidev
->dev
, tgt
->xferq_mem_size
,
880 tgt
->xferq
, tgt
->xferq_dma
);
883 /* Free RQ PBL and RQ */
885 dma_free_coherent(&hba
->pcidev
->dev
, tgt
->rq_pbl_size
,
886 tgt
->rq_pbl
, tgt
->rq_pbl_dma
);
890 dma_free_coherent(&hba
->pcidev
->dev
, tgt
->rq_mem_size
,
891 tgt
->rq
, tgt
->rq_dma
);
896 dma_free_coherent(&hba
->pcidev
->dev
, tgt
->cq_mem_size
,
897 tgt
->cq
, tgt
->cq_dma
);
902 dma_free_coherent(&hba
->pcidev
->dev
, tgt
->sq_mem_size
,
903 tgt
->sq
, tgt
->sq_dma
);
906 spin_unlock_bh(&tgt
->cq_lock
);
909 iounmap(ctx_base_ptr
);