Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / scsi / bnx2fc / bnx2fc_tgt.c
blobc1800b5312708a914cf424319dd7376fbb008186
1 /* 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.
5 * Copyright (c) 2008 - 2011 Broadcom Corporation
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.
11 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
14 #include "bnx2fc.h"
15 static void bnx2fc_upld_timer(unsigned long data);
16 static void bnx2fc_ofld_timer(unsigned long data);
17 static int bnx2fc_init_tgt(struct bnx2fc_rport *tgt,
18 struct fcoe_port *port,
19 struct fc_rport_priv *rdata);
20 static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
21 struct bnx2fc_rport *tgt);
22 static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
23 struct bnx2fc_rport *tgt);
24 static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
25 struct bnx2fc_rport *tgt);
26 static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id);
28 static void bnx2fc_upld_timer(unsigned long data)
31 struct bnx2fc_rport *tgt = (struct bnx2fc_rport *)data;
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);
40 static void bnx2fc_ofld_timer(unsigned long data)
43 struct bnx2fc_rport *tgt = (struct bnx2fc_rport *)data;
45 BNX2FC_TGT_DBG(tgt, "entered bnx2fc_ofld_timer\n");
46 /* NOTE: This function should never be called, as
47 * offload should never timeout
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
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);
62 static void bnx2fc_offload_session(struct fcoe_port *port,
63 struct bnx2fc_rport *tgt,
64 struct fc_rport_priv *rdata)
66 struct fc_lport *lport = rdata->local_port;
67 struct fc_rport *rport = rdata->rport;
68 struct bnx2fc_interface *interface = port->priv;
69 struct bnx2fc_hba *hba = interface->hba;
70 int rval;
71 int i = 0;
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 tgt_init_err;
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;
90 * Initialize FCoE session offload process.
91 * Upon completion of offload process add
92 * rport to list of rports
94 retry_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;
103 * wait for the session is offloaded and enabled. 3 Secs
104 * should be ample time for this process to complete.
106 setup_timer(&tgt->ofld_timer, bnx2fc_ofld_timer, (unsigned long)tgt);
107 mod_timer(&tgt->ofld_timer, jiffies + BNX2FC_FW_TIMEOUT);
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);
116 del_timer_sync(&tgt->ofld_timer);
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;
128 goto retry_ofld;
130 goto ofld_err;
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);
137 return;
139 ofld_err:
140 /* couldn't offload the session. log off from this rport */
141 BNX2FC_TGT_DBG(tgt, "bnx2fc_offload_session - offload error\n");
142 /* Free session resources */
143 bnx2fc_free_session_resc(hba, tgt);
144 tgt_init_err:
145 if (tgt->fcoe_conn_id != -1)
146 bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
147 lport->tt.rport_logoff(rdata);
150 void bnx2fc_flush_active_ios(struct bnx2fc_rport *tgt)
152 struct bnx2fc_cmd *io_req;
153 struct list_head *list;
154 struct list_head *tmp;
155 int rc;
156 int i = 0;
157 BNX2FC_TGT_DBG(tgt, "Entered flush_active_ios - %d\n",
158 tgt->num_active_ios.counter);
160 spin_lock_bh(&tgt->tgt_lock);
161 tgt->flush_in_prog = 1;
163 list_for_each_safe(list, tmp, &tgt->active_cmd_queue) {
164 i++;
165 io_req = (struct bnx2fc_cmd *)list;
166 list_del_init(&io_req->link);
167 io_req->on_active_queue = 0;
168 BNX2FC_IO_DBG(io_req, "cmd_queue cleanup\n");
170 if (cancel_delayed_work(&io_req->timeout_work)) {
171 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
172 &io_req->req_flags)) {
173 /* Handle eh_abort timeout */
174 BNX2FC_IO_DBG(io_req, "eh_abort for IO "
175 "cleaned up\n");
176 complete(&io_req->tm_done);
178 kref_put(&io_req->refcount,
179 bnx2fc_cmd_release); /* drop timer hold */
182 set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags);
183 set_bit(BNX2FC_FLAG_IO_CLEANUP, &io_req->req_flags);
184 rc = bnx2fc_initiate_cleanup(io_req);
185 BUG_ON(rc);
188 list_for_each_safe(list, tmp, &tgt->els_queue) {
189 i++;
190 io_req = (struct bnx2fc_cmd *)list;
191 list_del_init(&io_req->link);
192 io_req->on_active_queue = 0;
194 BNX2FC_IO_DBG(io_req, "els_queue cleanup\n");
196 if (cancel_delayed_work(&io_req->timeout_work))
197 kref_put(&io_req->refcount,
198 bnx2fc_cmd_release); /* drop timer hold */
200 if ((io_req->cb_func) && (io_req->cb_arg)) {
201 io_req->cb_func(io_req->cb_arg);
202 io_req->cb_arg = NULL;
205 rc = bnx2fc_initiate_cleanup(io_req);
206 BUG_ON(rc);
209 list_for_each_safe(list, tmp, &tgt->io_retire_queue) {
210 i++;
211 io_req = (struct bnx2fc_cmd *)list;
212 list_del_init(&io_req->link);
214 BNX2FC_IO_DBG(io_req, "retire_queue flush\n");
216 if (cancel_delayed_work(&io_req->timeout_work))
217 kref_put(&io_req->refcount, bnx2fc_cmd_release);
219 clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
222 BNX2FC_TGT_DBG(tgt, "IOs flushed = %d\n", i);
223 i = 0;
224 spin_unlock_bh(&tgt->tgt_lock);
225 /* wait for active_ios to go to 0 */
226 while ((tgt->num_active_ios.counter != 0) && (i++ < BNX2FC_WAIT_CNT))
227 msleep(25);
228 if (tgt->num_active_ios.counter != 0)
229 printk(KERN_ERR PFX "CLEANUP on port 0x%x:"
230 " active_ios = %d\n",
231 tgt->rdata->ids.port_id, tgt->num_active_ios.counter);
232 spin_lock_bh(&tgt->tgt_lock);
233 tgt->flush_in_prog = 0;
234 spin_unlock_bh(&tgt->tgt_lock);
237 static void bnx2fc_upload_session(struct fcoe_port *port,
238 struct bnx2fc_rport *tgt)
240 struct bnx2fc_interface *interface = port->priv;
241 struct bnx2fc_hba *hba = interface->hba;
243 BNX2FC_TGT_DBG(tgt, "upload_session: active_ios = %d\n",
244 tgt->num_active_ios.counter);
247 * Called with hba->hba_mutex held.
248 * This is a blocking call
250 clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
251 bnx2fc_send_session_disable_req(port, tgt);
254 * wait for upload to complete. 3 Secs
255 * should be sufficient time for this process to complete.
257 setup_timer(&tgt->upld_timer, bnx2fc_upld_timer, (unsigned long)tgt);
258 mod_timer(&tgt->upld_timer, jiffies + BNX2FC_FW_TIMEOUT);
260 BNX2FC_TGT_DBG(tgt, "waiting for disable compl\n");
261 wait_event_interruptible(tgt->upld_wait,
262 (test_bit(
263 BNX2FC_FLAG_UPLD_REQ_COMPL,
264 &tgt->flags)));
266 if (signal_pending(current))
267 flush_signals(current);
269 del_timer_sync(&tgt->upld_timer);
272 * traverse thru the active_q and tmf_q and cleanup
273 * IOs in these lists
275 BNX2FC_TGT_DBG(tgt, "flush/upload - disable wait flags = 0x%lx\n",
276 tgt->flags);
277 bnx2fc_flush_active_ios(tgt);
279 /* Issue destroy KWQE */
280 if (test_bit(BNX2FC_FLAG_DISABLED, &tgt->flags)) {
281 BNX2FC_TGT_DBG(tgt, "send destroy req\n");
282 clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
283 bnx2fc_send_session_destroy_req(hba, tgt);
285 /* wait for destroy to complete */
286 setup_timer(&tgt->upld_timer,
287 bnx2fc_upld_timer, (unsigned long)tgt);
288 mod_timer(&tgt->upld_timer, jiffies + BNX2FC_FW_TIMEOUT);
290 wait_event_interruptible(tgt->upld_wait,
291 (test_bit(
292 BNX2FC_FLAG_UPLD_REQ_COMPL,
293 &tgt->flags)));
295 if (!(test_bit(BNX2FC_FLAG_DESTROYED, &tgt->flags)))
296 printk(KERN_ERR PFX "ERROR!! destroy timed out\n");
298 BNX2FC_TGT_DBG(tgt, "destroy wait complete flags = 0x%lx\n",
299 tgt->flags);
300 if (signal_pending(current))
301 flush_signals(current);
303 del_timer_sync(&tgt->upld_timer);
305 } else
306 printk(KERN_ERR PFX "ERROR!! DISABLE req timed out, destroy"
307 " not sent to FW\n");
309 /* Free session resources */
310 bnx2fc_free_session_resc(hba, tgt);
311 bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
314 static int bnx2fc_init_tgt(struct bnx2fc_rport *tgt,
315 struct fcoe_port *port,
316 struct fc_rport_priv *rdata)
319 struct fc_rport *rport = rdata->rport;
320 struct bnx2fc_interface *interface = port->priv;
321 struct bnx2fc_hba *hba = interface->hba;
322 struct b577xx_doorbell_set_prod *sq_db = &tgt->sq_db;
323 struct b577xx_fcoe_rx_doorbell *rx_db = &tgt->rx_db;
325 tgt->rport = rport;
326 tgt->rdata = rdata;
327 tgt->port = port;
329 if (hba->num_ofld_sess >= BNX2FC_NUM_MAX_SESS) {
330 BNX2FC_TGT_DBG(tgt, "exceeded max sessions. logoff this tgt\n");
331 tgt->fcoe_conn_id = -1;
332 return -1;
335 tgt->fcoe_conn_id = bnx2fc_alloc_conn_id(hba, tgt);
336 if (tgt->fcoe_conn_id == -1)
337 return -1;
339 BNX2FC_TGT_DBG(tgt, "init_tgt - conn_id = 0x%x\n", tgt->fcoe_conn_id);
341 tgt->max_sqes = BNX2FC_SQ_WQES_MAX;
342 tgt->max_rqes = BNX2FC_RQ_WQES_MAX;
343 tgt->max_cqes = BNX2FC_CQ_WQES_MAX;
344 atomic_set(&tgt->free_sqes, BNX2FC_SQ_WQES_MAX);
346 /* Initialize the toggle bit */
347 tgt->sq_curr_toggle_bit = 1;
348 tgt->cq_curr_toggle_bit = 1;
349 tgt->sq_prod_idx = 0;
350 tgt->cq_cons_idx = 0;
351 tgt->rq_prod_idx = 0x8000;
352 tgt->rq_cons_idx = 0;
353 atomic_set(&tgt->num_active_ios, 0);
355 if (rdata->flags & FC_RP_FLAGS_RETRY) {
356 tgt->dev_type = TYPE_TAPE;
357 tgt->io_timeout = 0; /* use default ULP timeout */
358 } else {
359 tgt->dev_type = TYPE_DISK;
360 tgt->io_timeout = BNX2FC_IO_TIMEOUT;
363 /* initialize sq doorbell */
364 sq_db->header.header = B577XX_DOORBELL_HDR_DB_TYPE;
365 sq_db->header.header |= B577XX_FCOE_CONNECTION_TYPE <<
366 B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT;
367 /* initialize rx doorbell */
368 rx_db->hdr.header = ((0x1 << B577XX_DOORBELL_HDR_RX_SHIFT) |
369 (0x1 << B577XX_DOORBELL_HDR_DB_TYPE_SHIFT) |
370 (B577XX_FCOE_CONNECTION_TYPE <<
371 B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT));
372 rx_db->params = (0x2 << B577XX_FCOE_RX_DOORBELL_NEGATIVE_ARM_SHIFT) |
373 (0x3 << B577XX_FCOE_RX_DOORBELL_OPCODE_SHIFT);
375 spin_lock_init(&tgt->tgt_lock);
376 spin_lock_init(&tgt->cq_lock);
378 /* Initialize active_cmd_queue list */
379 INIT_LIST_HEAD(&tgt->active_cmd_queue);
381 /* Initialize IO retire queue */
382 INIT_LIST_HEAD(&tgt->io_retire_queue);
384 INIT_LIST_HEAD(&tgt->els_queue);
386 /* Initialize active_tm_queue list */
387 INIT_LIST_HEAD(&tgt->active_tm_queue);
389 init_waitqueue_head(&tgt->ofld_wait);
390 init_waitqueue_head(&tgt->upld_wait);
392 return 0;
396 * This event_callback is called after successful completion of libfc
397 * initiated target login. bnx2fc can proceed with initiating the session
398 * establishment.
400 void bnx2fc_rport_event_handler(struct fc_lport *lport,
401 struct fc_rport_priv *rdata,
402 enum fc_rport_event event)
404 struct fcoe_port *port = lport_priv(lport);
405 struct bnx2fc_interface *interface = port->priv;
406 struct bnx2fc_hba *hba = interface->hba;
407 struct fc_rport *rport = rdata->rport;
408 struct fc_rport_libfc_priv *rp;
409 struct bnx2fc_rport *tgt;
410 u32 port_id;
412 BNX2FC_HBA_DBG(lport, "rport_event_hdlr: event = %d, port_id = 0x%x\n",
413 event, rdata->ids.port_id);
414 switch (event) {
415 case RPORT_EV_READY:
416 if (!rport) {
417 printk(KERN_ERR PFX "rport is NULL: ERROR!\n");
418 break;
421 rp = rport->dd_data;
422 if (rport->port_id == FC_FID_DIR_SERV) {
424 * bnx2fc_rport structure doesn't exist for
425 * directory server.
426 * We should not come here, as lport will
427 * take care of fabric login
429 printk(KERN_ERR PFX "%x - rport_event_handler ERROR\n",
430 rdata->ids.port_id);
431 break;
434 if (rdata->spp_type != FC_TYPE_FCP) {
435 BNX2FC_HBA_DBG(lport, "not FCP type target."
436 " not offloading\n");
437 break;
439 if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
440 BNX2FC_HBA_DBG(lport, "not FCP_TARGET"
441 " not offloading\n");
442 break;
446 * Offlaod process is protected with hba mutex.
447 * Use the same mutex_lock for upload process too
449 mutex_lock(&hba->hba_mutex);
450 tgt = (struct bnx2fc_rport *)&rp[1];
452 /* This can happen when ADISC finds the same target */
453 if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
454 BNX2FC_TGT_DBG(tgt, "already offloaded\n");
455 mutex_unlock(&hba->hba_mutex);
456 return;
460 * Offload the session. This is a blocking call, and will
461 * wait until the session is offloaded.
463 bnx2fc_offload_session(port, tgt, rdata);
465 BNX2FC_TGT_DBG(tgt, "OFFLOAD num_ofld_sess = %d\n",
466 hba->num_ofld_sess);
468 if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
470 * Session is offloaded and enabled. Map
471 * doorbell register for this target
473 BNX2FC_TGT_DBG(tgt, "sess offloaded\n");
474 /* This counter is protected with hba mutex */
475 hba->num_ofld_sess++;
477 set_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
478 } else {
480 * Offload or enable would have failed.
481 * In offload/enable completion path, the
482 * rport would have already been removed
484 BNX2FC_TGT_DBG(tgt, "Port is being logged off as "
485 "offloaded flag not set\n");
487 mutex_unlock(&hba->hba_mutex);
488 break;
489 case RPORT_EV_LOGO:
490 case RPORT_EV_FAILED:
491 case RPORT_EV_STOP:
492 port_id = rdata->ids.port_id;
493 if (port_id == FC_FID_DIR_SERV)
494 break;
496 if (!rport) {
497 printk(KERN_INFO PFX "%x - rport not created Yet!!\n",
498 port_id);
499 break;
501 rp = rport->dd_data;
502 mutex_lock(&hba->hba_mutex);
504 * Perform session upload. Note that rdata->peers is already
505 * removed from disc->rports list before we get this event.
507 tgt = (struct bnx2fc_rport *)&rp[1];
509 if (!(test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags))) {
510 mutex_unlock(&hba->hba_mutex);
511 break;
513 clear_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
515 bnx2fc_upload_session(port, tgt);
516 hba->num_ofld_sess--;
517 BNX2FC_TGT_DBG(tgt, "UPLOAD num_ofld_sess = %d\n",
518 hba->num_ofld_sess);
520 * Try to wake up the linkdown wait thread. If num_ofld_sess
521 * is 0, the waiting therad wakes up
523 if ((hba->wait_for_link_down) &&
524 (hba->num_ofld_sess == 0)) {
525 wake_up_interruptible(&hba->shutdown_wait);
527 if (test_bit(BNX2FC_FLAG_EXPL_LOGO, &tgt->flags)) {
528 printk(KERN_ERR PFX "Relogin to the tgt\n");
529 mutex_lock(&lport->disc.disc_mutex);
530 lport->tt.rport_login(rdata);
531 mutex_unlock(&lport->disc.disc_mutex);
533 mutex_unlock(&hba->hba_mutex);
535 break;
537 case RPORT_EV_NONE:
538 break;
543 * bnx2fc_tgt_lookup() - Lookup a bnx2fc_rport by port_id
545 * @port: fcoe_port struct to lookup the target port on
546 * @port_id: The remote port ID to look up
548 struct bnx2fc_rport *bnx2fc_tgt_lookup(struct fcoe_port *port,
549 u32 port_id)
551 struct bnx2fc_interface *interface = port->priv;
552 struct bnx2fc_hba *hba = interface->hba;
553 struct bnx2fc_rport *tgt;
554 struct fc_rport_priv *rdata;
555 int i;
557 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
558 tgt = hba->tgt_ofld_list[i];
559 if ((tgt) && (tgt->port == port)) {
560 rdata = tgt->rdata;
561 if (rdata->ids.port_id == port_id) {
562 if (rdata->rp_state != RPORT_ST_DELETE) {
563 BNX2FC_TGT_DBG(tgt, "rport "
564 "obtained\n");
565 return tgt;
566 } else {
567 BNX2FC_TGT_DBG(tgt, "rport 0x%x "
568 "is in DELETED state\n",
569 rdata->ids.port_id);
570 return NULL;
575 return NULL;
580 * bnx2fc_alloc_conn_id - allocates FCOE Connection id
582 * @hba: pointer to adapter structure
583 * @tgt: pointer to bnx2fc_rport structure
585 static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
586 struct bnx2fc_rport *tgt)
588 u32 conn_id, next;
590 /* called with hba mutex held */
593 * tgt_ofld_list access is synchronized using
594 * both hba mutex and hba lock. Atleast hba mutex or
595 * hba lock needs to be held for read access.
598 spin_lock_bh(&hba->hba_lock);
599 next = hba->next_conn_id;
600 conn_id = hba->next_conn_id++;
601 if (hba->next_conn_id == BNX2FC_NUM_MAX_SESS)
602 hba->next_conn_id = 0;
604 while (hba->tgt_ofld_list[conn_id] != NULL) {
605 conn_id++;
606 if (conn_id == BNX2FC_NUM_MAX_SESS)
607 conn_id = 0;
609 if (conn_id == next) {
610 /* No free conn_ids are available */
611 spin_unlock_bh(&hba->hba_lock);
612 return -1;
615 hba->tgt_ofld_list[conn_id] = tgt;
616 tgt->fcoe_conn_id = conn_id;
617 spin_unlock_bh(&hba->hba_lock);
618 return conn_id;
621 static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id)
623 /* called with hba mutex held */
624 spin_lock_bh(&hba->hba_lock);
625 hba->tgt_ofld_list[conn_id] = NULL;
626 spin_unlock_bh(&hba->hba_lock);
630 *bnx2fc_alloc_session_resc - Allocate qp resources for the session
633 static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
634 struct bnx2fc_rport *tgt)
636 dma_addr_t page;
637 int num_pages;
638 u32 *pbl;
640 /* Allocate and map SQ */
641 tgt->sq_mem_size = tgt->max_sqes * BNX2FC_SQ_WQE_SIZE;
642 tgt->sq_mem_size = (tgt->sq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
644 tgt->sq = dma_alloc_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
645 &tgt->sq_dma, GFP_KERNEL);
646 if (!tgt->sq) {
647 printk(KERN_ERR PFX "unable to allocate SQ memory %d\n",
648 tgt->sq_mem_size);
649 goto mem_alloc_failure;
651 memset(tgt->sq, 0, tgt->sq_mem_size);
653 /* Allocate and map CQ */
654 tgt->cq_mem_size = tgt->max_cqes * BNX2FC_CQ_WQE_SIZE;
655 tgt->cq_mem_size = (tgt->cq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
657 tgt->cq = dma_alloc_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
658 &tgt->cq_dma, GFP_KERNEL);
659 if (!tgt->cq) {
660 printk(KERN_ERR PFX "unable to allocate CQ memory %d\n",
661 tgt->cq_mem_size);
662 goto mem_alloc_failure;
664 memset(tgt->cq, 0, tgt->cq_mem_size);
666 /* Allocate and map RQ and RQ PBL */
667 tgt->rq_mem_size = tgt->max_rqes * BNX2FC_RQ_WQE_SIZE;
668 tgt->rq_mem_size = (tgt->rq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
670 tgt->rq = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
671 &tgt->rq_dma, GFP_KERNEL);
672 if (!tgt->rq) {
673 printk(KERN_ERR PFX "unable to allocate RQ memory %d\n",
674 tgt->rq_mem_size);
675 goto mem_alloc_failure;
677 memset(tgt->rq, 0, tgt->rq_mem_size);
679 tgt->rq_pbl_size = (tgt->rq_mem_size / PAGE_SIZE) * sizeof(void *);
680 tgt->rq_pbl_size = (tgt->rq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
682 tgt->rq_pbl = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
683 &tgt->rq_pbl_dma, GFP_KERNEL);
684 if (!tgt->rq_pbl) {
685 printk(KERN_ERR PFX "unable to allocate RQ PBL %d\n",
686 tgt->rq_pbl_size);
687 goto mem_alloc_failure;
690 memset(tgt->rq_pbl, 0, tgt->rq_pbl_size);
691 num_pages = tgt->rq_mem_size / PAGE_SIZE;
692 page = tgt->rq_dma;
693 pbl = (u32 *)tgt->rq_pbl;
695 while (num_pages--) {
696 *pbl = (u32)page;
697 pbl++;
698 *pbl = (u32)((u64)page >> 32);
699 pbl++;
700 page += PAGE_SIZE;
703 /* Allocate and map XFERQ */
704 tgt->xferq_mem_size = tgt->max_sqes * BNX2FC_XFERQ_WQE_SIZE;
705 tgt->xferq_mem_size = (tgt->xferq_mem_size + (PAGE_SIZE - 1)) &
706 PAGE_MASK;
708 tgt->xferq = dma_alloc_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
709 &tgt->xferq_dma, GFP_KERNEL);
710 if (!tgt->xferq) {
711 printk(KERN_ERR PFX "unable to allocate XFERQ %d\n",
712 tgt->xferq_mem_size);
713 goto mem_alloc_failure;
715 memset(tgt->xferq, 0, tgt->xferq_mem_size);
717 /* Allocate and map CONFQ & CONFQ PBL */
718 tgt->confq_mem_size = tgt->max_sqes * BNX2FC_CONFQ_WQE_SIZE;
719 tgt->confq_mem_size = (tgt->confq_mem_size + (PAGE_SIZE - 1)) &
720 PAGE_MASK;
722 tgt->confq = dma_alloc_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
723 &tgt->confq_dma, GFP_KERNEL);
724 if (!tgt->confq) {
725 printk(KERN_ERR PFX "unable to allocate CONFQ %d\n",
726 tgt->confq_mem_size);
727 goto mem_alloc_failure;
729 memset(tgt->confq, 0, tgt->confq_mem_size);
731 tgt->confq_pbl_size =
732 (tgt->confq_mem_size / PAGE_SIZE) * sizeof(void *);
733 tgt->confq_pbl_size =
734 (tgt->confq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
736 tgt->confq_pbl = dma_alloc_coherent(&hba->pcidev->dev,
737 tgt->confq_pbl_size,
738 &tgt->confq_pbl_dma, GFP_KERNEL);
739 if (!tgt->confq_pbl) {
740 printk(KERN_ERR PFX "unable to allocate CONFQ PBL %d\n",
741 tgt->confq_pbl_size);
742 goto mem_alloc_failure;
745 memset(tgt->confq_pbl, 0, tgt->confq_pbl_size);
746 num_pages = tgt->confq_mem_size / PAGE_SIZE;
747 page = tgt->confq_dma;
748 pbl = (u32 *)tgt->confq_pbl;
750 while (num_pages--) {
751 *pbl = (u32)page;
752 pbl++;
753 *pbl = (u32)((u64)page >> 32);
754 pbl++;
755 page += PAGE_SIZE;
758 /* Allocate and map ConnDB */
759 tgt->conn_db_mem_size = sizeof(struct fcoe_conn_db);
761 tgt->conn_db = dma_alloc_coherent(&hba->pcidev->dev,
762 tgt->conn_db_mem_size,
763 &tgt->conn_db_dma, GFP_KERNEL);
764 if (!tgt->conn_db) {
765 printk(KERN_ERR PFX "unable to allocate conn_db %d\n",
766 tgt->conn_db_mem_size);
767 goto mem_alloc_failure;
769 memset(tgt->conn_db, 0, tgt->conn_db_mem_size);
772 /* Allocate and map LCQ */
773 tgt->lcq_mem_size = (tgt->max_sqes + 8) * BNX2FC_SQ_WQE_SIZE;
774 tgt->lcq_mem_size = (tgt->lcq_mem_size + (PAGE_SIZE - 1)) &
775 PAGE_MASK;
777 tgt->lcq = dma_alloc_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
778 &tgt->lcq_dma, GFP_KERNEL);
780 if (!tgt->lcq) {
781 printk(KERN_ERR PFX "unable to allocate lcq %d\n",
782 tgt->lcq_mem_size);
783 goto mem_alloc_failure;
785 memset(tgt->lcq, 0, tgt->lcq_mem_size);
787 tgt->conn_db->rq_prod = 0x8000;
789 return 0;
791 mem_alloc_failure:
792 return -ENOMEM;
796 * bnx2i_free_session_resc - free qp resources for the session
798 * @hba: adapter structure pointer
799 * @tgt: bnx2fc_rport structure pointer
801 * Free QP resources - SQ/RQ/CQ/XFERQ memory and PBL
803 static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
804 struct bnx2fc_rport *tgt)
806 void __iomem *ctx_base_ptr;
808 BNX2FC_TGT_DBG(tgt, "Freeing up session resources\n");
810 spin_lock_bh(&tgt->cq_lock);
811 ctx_base_ptr = tgt->ctx_base;
812 tgt->ctx_base = NULL;
814 /* Free LCQ */
815 if (tgt->lcq) {
816 dma_free_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
817 tgt->lcq, tgt->lcq_dma);
818 tgt->lcq = NULL;
820 /* Free connDB */
821 if (tgt->conn_db) {
822 dma_free_coherent(&hba->pcidev->dev, tgt->conn_db_mem_size,
823 tgt->conn_db, tgt->conn_db_dma);
824 tgt->conn_db = NULL;
826 /* Free confq and confq pbl */
827 if (tgt->confq_pbl) {
828 dma_free_coherent(&hba->pcidev->dev, tgt->confq_pbl_size,
829 tgt->confq_pbl, tgt->confq_pbl_dma);
830 tgt->confq_pbl = NULL;
832 if (tgt->confq) {
833 dma_free_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
834 tgt->confq, tgt->confq_dma);
835 tgt->confq = NULL;
837 /* Free XFERQ */
838 if (tgt->xferq) {
839 dma_free_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
840 tgt->xferq, tgt->xferq_dma);
841 tgt->xferq = NULL;
843 /* Free RQ PBL and RQ */
844 if (tgt->rq_pbl) {
845 dma_free_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
846 tgt->rq_pbl, tgt->rq_pbl_dma);
847 tgt->rq_pbl = NULL;
849 if (tgt->rq) {
850 dma_free_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
851 tgt->rq, tgt->rq_dma);
852 tgt->rq = NULL;
854 /* Free CQ */
855 if (tgt->cq) {
856 dma_free_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
857 tgt->cq, tgt->cq_dma);
858 tgt->cq = NULL;
860 /* Free SQ */
861 if (tgt->sq) {
862 dma_free_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
863 tgt->sq, tgt->sq_dma);
864 tgt->sq = NULL;
866 spin_unlock_bh(&tgt->cq_lock);
868 if (ctx_base_ptr)
869 iounmap(ctx_base_ptr);