Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / scsi / bnx2fc / bnx2fc_tgt.c
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.
4  *
5  * Copyright (c) 2008 - 2011 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"
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);
27
28 static 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
40 static 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
62 static 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;
68         struct bnx2fc_interface *interface = port->priv;
69         struct bnx2fc_hba *hba = interface->hba;
70         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 tgt_init_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          */
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;
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         }
137         return;
138
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);
148 }
149
150 void bnx2fc_flush_active_ios(struct bnx2fc_rport *tgt)
151 {
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);
159
160         spin_lock_bh(&tgt->tgt_lock);
161         tgt->flush_in_prog = 1;
162
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");
169
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);
177                         }
178                         kref_put(&io_req->refcount,
179                                  bnx2fc_cmd_release); /* drop timer hold */
180                 }
181
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);
186         }
187
188         list_for_each_safe(list, tmp, &tgt->active_tm_queue) {
189                 i++;
190                 io_req = (struct bnx2fc_cmd *)list;
191                 list_del_init(&io_req->link);
192                 io_req->on_tmf_queue = 0;
193                 BNX2FC_IO_DBG(io_req, "tm_queue cleanup\n");
194                 if (io_req->wait_for_comp)
195                         complete(&io_req->tm_done);
196         }
197
198         list_for_each_safe(list, tmp, &tgt->els_queue) {
199                 i++;
200                 io_req = (struct bnx2fc_cmd *)list;
201                 list_del_init(&io_req->link);
202                 io_req->on_active_queue = 0;
203
204                 BNX2FC_IO_DBG(io_req, "els_queue cleanup\n");
205
206                 if (cancel_delayed_work(&io_req->timeout_work))
207                         kref_put(&io_req->refcount,
208                                  bnx2fc_cmd_release); /* drop timer hold */
209
210                 if ((io_req->cb_func) && (io_req->cb_arg)) {
211                         io_req->cb_func(io_req->cb_arg);
212                         io_req->cb_arg = NULL;
213                 }
214
215                 rc = bnx2fc_initiate_cleanup(io_req);
216                 BUG_ON(rc);
217         }
218
219         list_for_each_safe(list, tmp, &tgt->io_retire_queue) {
220                 i++;
221                 io_req = (struct bnx2fc_cmd *)list;
222                 list_del_init(&io_req->link);
223
224                 BNX2FC_IO_DBG(io_req, "retire_queue flush\n");
225
226                 if (cancel_delayed_work(&io_req->timeout_work)) {
227                         if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
228                                                 &io_req->req_flags)) {
229                                 /* Handle eh_abort timeout */
230                                 BNX2FC_IO_DBG(io_req, "eh_abort for IO "
231                                               "in retire_q\n");
232                                 if (io_req->wait_for_comp)
233                                         complete(&io_req->tm_done);
234                         }
235                         kref_put(&io_req->refcount, bnx2fc_cmd_release);
236                 }
237
238                 clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
239         }
240
241         BNX2FC_TGT_DBG(tgt, "IOs flushed = %d\n", i);
242         i = 0;
243         spin_unlock_bh(&tgt->tgt_lock);
244         /* wait for active_ios to go to 0 */
245         while ((tgt->num_active_ios.counter != 0) && (i++ < BNX2FC_WAIT_CNT))
246                 msleep(25);
247         if (tgt->num_active_ios.counter != 0)
248                 printk(KERN_ERR PFX "CLEANUP on port 0x%x:"
249                                     " active_ios = %d\n",
250                         tgt->rdata->ids.port_id, tgt->num_active_ios.counter);
251         spin_lock_bh(&tgt->tgt_lock);
252         tgt->flush_in_prog = 0;
253         spin_unlock_bh(&tgt->tgt_lock);
254 }
255
256 static void bnx2fc_upload_session(struct fcoe_port *port,
257                                         struct bnx2fc_rport *tgt)
258 {
259         struct bnx2fc_interface *interface = port->priv;
260         struct bnx2fc_hba *hba = interface->hba;
261
262         BNX2FC_TGT_DBG(tgt, "upload_session: active_ios = %d\n",
263                 tgt->num_active_ios.counter);
264
265         /*
266          * Called with hba->hba_mutex held.
267          * This is a blocking call
268          */
269         clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
270         bnx2fc_send_session_disable_req(port, tgt);
271
272         /*
273          * wait for upload to complete. 3 Secs
274          * should be sufficient time for this process to complete.
275          */
276         setup_timer(&tgt->upld_timer, bnx2fc_upld_timer, (unsigned long)tgt);
277         mod_timer(&tgt->upld_timer, jiffies + BNX2FC_FW_TIMEOUT);
278
279         BNX2FC_TGT_DBG(tgt, "waiting for disable compl\n");
280         wait_event_interruptible(tgt->upld_wait,
281                                  (test_bit(
282                                   BNX2FC_FLAG_UPLD_REQ_COMPL,
283                                   &tgt->flags)));
284
285         if (signal_pending(current))
286                 flush_signals(current);
287
288         del_timer_sync(&tgt->upld_timer);
289
290         /*
291          * traverse thru the active_q and tmf_q and cleanup
292          * IOs in these lists
293          */
294         BNX2FC_TGT_DBG(tgt, "flush/upload - disable wait flags = 0x%lx\n",
295                        tgt->flags);
296         bnx2fc_flush_active_ios(tgt);
297
298         /* Issue destroy KWQE */
299         if (test_bit(BNX2FC_FLAG_DISABLED, &tgt->flags)) {
300                 BNX2FC_TGT_DBG(tgt, "send destroy req\n");
301                 clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
302                 bnx2fc_send_session_destroy_req(hba, tgt);
303
304                 /* wait for destroy to complete */
305                 setup_timer(&tgt->upld_timer,
306                             bnx2fc_upld_timer, (unsigned long)tgt);
307                 mod_timer(&tgt->upld_timer, jiffies + BNX2FC_FW_TIMEOUT);
308
309                 wait_event_interruptible(tgt->upld_wait,
310                                          (test_bit(
311                                           BNX2FC_FLAG_UPLD_REQ_COMPL,
312                                           &tgt->flags)));
313
314                 if (!(test_bit(BNX2FC_FLAG_DESTROYED, &tgt->flags)))
315                         printk(KERN_ERR PFX "ERROR!! destroy timed out\n");
316
317                 BNX2FC_TGT_DBG(tgt, "destroy wait complete flags = 0x%lx\n",
318                         tgt->flags);
319                 if (signal_pending(current))
320                         flush_signals(current);
321
322                 del_timer_sync(&tgt->upld_timer);
323
324         } else
325                 printk(KERN_ERR PFX "ERROR!! DISABLE req timed out, destroy"
326                                 " not sent to FW\n");
327
328         /* Free session resources */
329         bnx2fc_free_session_resc(hba, tgt);
330         bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
331 }
332
333 static int bnx2fc_init_tgt(struct bnx2fc_rport *tgt,
334                            struct fcoe_port *port,
335                            struct fc_rport_priv *rdata)
336 {
337
338         struct fc_rport *rport = rdata->rport;
339         struct bnx2fc_interface *interface = port->priv;
340         struct bnx2fc_hba *hba = interface->hba;
341         struct b577xx_doorbell_set_prod *sq_db = &tgt->sq_db;
342         struct b577xx_fcoe_rx_doorbell *rx_db = &tgt->rx_db;
343
344         tgt->rport = rport;
345         tgt->rdata = rdata;
346         tgt->port = port;
347
348         if (hba->num_ofld_sess >= BNX2FC_NUM_MAX_SESS) {
349                 BNX2FC_TGT_DBG(tgt, "exceeded max sessions. logoff this tgt\n");
350                 tgt->fcoe_conn_id = -1;
351                 return -1;
352         }
353
354         tgt->fcoe_conn_id = bnx2fc_alloc_conn_id(hba, tgt);
355         if (tgt->fcoe_conn_id == -1)
356                 return -1;
357
358         BNX2FC_TGT_DBG(tgt, "init_tgt - conn_id = 0x%x\n", tgt->fcoe_conn_id);
359
360         tgt->max_sqes = BNX2FC_SQ_WQES_MAX;
361         tgt->max_rqes = BNX2FC_RQ_WQES_MAX;
362         tgt->max_cqes = BNX2FC_CQ_WQES_MAX;
363         atomic_set(&tgt->free_sqes, BNX2FC_SQ_WQES_MAX);
364
365         /* Initialize the toggle bit */
366         tgt->sq_curr_toggle_bit = 1;
367         tgt->cq_curr_toggle_bit = 1;
368         tgt->sq_prod_idx = 0;
369         tgt->cq_cons_idx = 0;
370         tgt->rq_prod_idx = 0x8000;
371         tgt->rq_cons_idx = 0;
372         atomic_set(&tgt->num_active_ios, 0);
373
374         if (rdata->flags & FC_RP_FLAGS_RETRY) {
375                 tgt->dev_type = TYPE_TAPE;
376                 tgt->io_timeout = 0; /* use default ULP timeout */
377         } else {
378                 tgt->dev_type = TYPE_DISK;
379                 tgt->io_timeout = BNX2FC_IO_TIMEOUT;
380         }
381
382         /* initialize sq doorbell */
383         sq_db->header.header = B577XX_DOORBELL_HDR_DB_TYPE;
384         sq_db->header.header |= B577XX_FCOE_CONNECTION_TYPE <<
385                                         B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT;
386         /* initialize rx doorbell */
387         rx_db->hdr.header = ((0x1 << B577XX_DOORBELL_HDR_RX_SHIFT) |
388                           (0x1 << B577XX_DOORBELL_HDR_DB_TYPE_SHIFT) |
389                           (B577XX_FCOE_CONNECTION_TYPE <<
390                                 B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT));
391         rx_db->params = (0x2 << B577XX_FCOE_RX_DOORBELL_NEGATIVE_ARM_SHIFT) |
392                      (0x3 << B577XX_FCOE_RX_DOORBELL_OPCODE_SHIFT);
393
394         spin_lock_init(&tgt->tgt_lock);
395         spin_lock_init(&tgt->cq_lock);
396
397         /* Initialize active_cmd_queue list */
398         INIT_LIST_HEAD(&tgt->active_cmd_queue);
399
400         /* Initialize IO retire queue */
401         INIT_LIST_HEAD(&tgt->io_retire_queue);
402
403         INIT_LIST_HEAD(&tgt->els_queue);
404
405         /* Initialize active_tm_queue list */
406         INIT_LIST_HEAD(&tgt->active_tm_queue);
407
408         init_waitqueue_head(&tgt->ofld_wait);
409         init_waitqueue_head(&tgt->upld_wait);
410
411         return 0;
412 }
413
414 /**
415  * This event_callback is called after successful completion of libfc
416  * initiated target login. bnx2fc can proceed with initiating the session
417  * establishment.
418  */
419 void bnx2fc_rport_event_handler(struct fc_lport *lport,
420                                 struct fc_rport_priv *rdata,
421                                 enum fc_rport_event event)
422 {
423         struct fcoe_port *port = lport_priv(lport);
424         struct bnx2fc_interface *interface = port->priv;
425         struct bnx2fc_hba *hba = interface->hba;
426         struct fc_rport *rport = rdata->rport;
427         struct fc_rport_libfc_priv *rp;
428         struct bnx2fc_rport *tgt;
429         u32 port_id;
430
431         BNX2FC_HBA_DBG(lport, "rport_event_hdlr: event = %d, port_id = 0x%x\n",
432                 event, rdata->ids.port_id);
433         switch (event) {
434         case RPORT_EV_READY:
435                 if (!rport) {
436                         printk(KERN_ERR PFX "rport is NULL: ERROR!\n");
437                         break;
438                 }
439
440                 rp = rport->dd_data;
441                 if (rport->port_id == FC_FID_DIR_SERV) {
442                         /*
443                          * bnx2fc_rport structure doesn't exist for
444                          * directory server.
445                          * We should not come here, as lport will
446                          * take care of fabric login
447                          */
448                         printk(KERN_ERR PFX "%x - rport_event_handler ERROR\n",
449                                 rdata->ids.port_id);
450                         break;
451                 }
452
453                 if (rdata->spp_type != FC_TYPE_FCP) {
454                         BNX2FC_HBA_DBG(lport, "not FCP type target."
455                                    " not offloading\n");
456                         break;
457                 }
458                 if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
459                         BNX2FC_HBA_DBG(lport, "not FCP_TARGET"
460                                    " not offloading\n");
461                         break;
462                 }
463
464                 /*
465                  * Offlaod process is protected with hba mutex.
466                  * Use the same mutex_lock for upload process too
467                  */
468                 mutex_lock(&hba->hba_mutex);
469                 tgt = (struct bnx2fc_rport *)&rp[1];
470
471                 /* This can happen when ADISC finds the same target */
472                 if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
473                         BNX2FC_TGT_DBG(tgt, "already offloaded\n");
474                         mutex_unlock(&hba->hba_mutex);
475                         return;
476                 }
477
478                 /*
479                  * Offload the session. This is a blocking call, and will
480                  * wait until the session is offloaded.
481                  */
482                 bnx2fc_offload_session(port, tgt, rdata);
483
484                 BNX2FC_TGT_DBG(tgt, "OFFLOAD num_ofld_sess = %d\n",
485                         hba->num_ofld_sess);
486
487                 if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
488                         /*
489                          * Session is offloaded and enabled. Map
490                          * doorbell register for this target
491                          */
492                         BNX2FC_TGT_DBG(tgt, "sess offloaded\n");
493                         /* This counter is protected with hba mutex */
494                         hba->num_ofld_sess++;
495
496                         set_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
497                 } else {
498                         /*
499                          * Offload or enable would have failed.
500                          * In offload/enable completion path, the
501                          * rport would have already been removed
502                          */
503                         BNX2FC_TGT_DBG(tgt, "Port is being logged off as "
504                                    "offloaded flag not set\n");
505                 }
506                 mutex_unlock(&hba->hba_mutex);
507                 break;
508         case RPORT_EV_LOGO:
509         case RPORT_EV_FAILED:
510         case RPORT_EV_STOP:
511                 port_id = rdata->ids.port_id;
512                 if (port_id == FC_FID_DIR_SERV)
513                         break;
514
515                 if (!rport) {
516                         printk(KERN_INFO PFX "%x - rport not created Yet!!\n",
517                                 port_id);
518                         break;
519                 }
520                 rp = rport->dd_data;
521                 mutex_lock(&hba->hba_mutex);
522                 /*
523                  * Perform session upload. Note that rdata->peers is already
524                  * removed from disc->rports list before we get this event.
525                  */
526                 tgt = (struct bnx2fc_rport *)&rp[1];
527
528                 if (!(test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags))) {
529                         mutex_unlock(&hba->hba_mutex);
530                         break;
531                 }
532                 clear_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
533
534                 bnx2fc_upload_session(port, tgt);
535                 hba->num_ofld_sess--;
536                 BNX2FC_TGT_DBG(tgt, "UPLOAD num_ofld_sess = %d\n",
537                         hba->num_ofld_sess);
538                 /*
539                  * Try to wake up the linkdown wait thread. If num_ofld_sess
540                  * is 0, the waiting therad wakes up
541                  */
542                 if ((hba->wait_for_link_down) &&
543                     (hba->num_ofld_sess == 0)) {
544                         wake_up_interruptible(&hba->shutdown_wait);
545                 }
546                 if (test_bit(BNX2FC_FLAG_EXPL_LOGO, &tgt->flags)) {
547                         printk(KERN_ERR PFX "Relogin to the tgt\n");
548                         mutex_lock(&lport->disc.disc_mutex);
549                         lport->tt.rport_login(rdata);
550                         mutex_unlock(&lport->disc.disc_mutex);
551                 }
552                 mutex_unlock(&hba->hba_mutex);
553
554                 break;
555
556         case RPORT_EV_NONE:
557                 break;
558         }
559 }
560
561 /**
562  * bnx2fc_tgt_lookup() - Lookup a bnx2fc_rport by port_id
563  *
564  * @port:  fcoe_port struct to lookup the target port on
565  * @port_id: The remote port ID to look up
566  */
567 struct bnx2fc_rport *bnx2fc_tgt_lookup(struct fcoe_port *port,
568                                              u32 port_id)
569 {
570         struct bnx2fc_interface *interface = port->priv;
571         struct bnx2fc_hba *hba = interface->hba;
572         struct bnx2fc_rport *tgt;
573         struct fc_rport_priv *rdata;
574         int i;
575
576         for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
577                 tgt = hba->tgt_ofld_list[i];
578                 if ((tgt) && (tgt->port == port)) {
579                         rdata = tgt->rdata;
580                         if (rdata->ids.port_id == port_id) {
581                                 if (rdata->rp_state != RPORT_ST_DELETE) {
582                                         BNX2FC_TGT_DBG(tgt, "rport "
583                                                 "obtained\n");
584                                         return tgt;
585                                 } else {
586                                         BNX2FC_TGT_DBG(tgt, "rport 0x%x "
587                                                 "is in DELETED state\n",
588                                                 rdata->ids.port_id);
589                                         return NULL;
590                                 }
591                         }
592                 }
593         }
594         return NULL;
595 }
596
597
598 /**
599  * bnx2fc_alloc_conn_id - allocates FCOE Connection id
600  *
601  * @hba:        pointer to adapter structure
602  * @tgt:        pointer to bnx2fc_rport structure
603  */
604 static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
605                                 struct bnx2fc_rport *tgt)
606 {
607         u32 conn_id, next;
608
609         /* called with hba mutex held */
610
611         /*
612          * tgt_ofld_list access is synchronized using
613          * both hba mutex and hba lock. Atleast hba mutex or
614          * hba lock needs to be held for read access.
615          */
616
617         spin_lock_bh(&hba->hba_lock);
618         next = hba->next_conn_id;
619         conn_id = hba->next_conn_id++;
620         if (hba->next_conn_id == BNX2FC_NUM_MAX_SESS)
621                 hba->next_conn_id = 0;
622
623         while (hba->tgt_ofld_list[conn_id] != NULL) {
624                 conn_id++;
625                 if (conn_id == BNX2FC_NUM_MAX_SESS)
626                         conn_id = 0;
627
628                 if (conn_id == next) {
629                         /* No free conn_ids are available */
630                         spin_unlock_bh(&hba->hba_lock);
631                         return -1;
632                 }
633         }
634         hba->tgt_ofld_list[conn_id] = tgt;
635         tgt->fcoe_conn_id = conn_id;
636         spin_unlock_bh(&hba->hba_lock);
637         return conn_id;
638 }
639
640 static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id)
641 {
642         /* called with hba mutex held */
643         spin_lock_bh(&hba->hba_lock);
644         hba->tgt_ofld_list[conn_id] = NULL;
645         spin_unlock_bh(&hba->hba_lock);
646 }
647
648 /**
649  *bnx2fc_alloc_session_resc - Allocate qp resources for the session
650  *
651  */
652 static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
653                                         struct bnx2fc_rport *tgt)
654 {
655         dma_addr_t page;
656         int num_pages;
657         u32 *pbl;
658
659         /* Allocate and map SQ */
660         tgt->sq_mem_size = tgt->max_sqes * BNX2FC_SQ_WQE_SIZE;
661         tgt->sq_mem_size = (tgt->sq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
662
663         tgt->sq = dma_alloc_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
664                                      &tgt->sq_dma, GFP_KERNEL);
665         if (!tgt->sq) {
666                 printk(KERN_ERR PFX "unable to allocate SQ memory %d\n",
667                         tgt->sq_mem_size);
668                 goto mem_alloc_failure;
669         }
670         memset(tgt->sq, 0, tgt->sq_mem_size);
671
672         /* Allocate and map CQ */
673         tgt->cq_mem_size = tgt->max_cqes * BNX2FC_CQ_WQE_SIZE;
674         tgt->cq_mem_size = (tgt->cq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
675
676         tgt->cq = dma_alloc_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
677                                      &tgt->cq_dma, GFP_KERNEL);
678         if (!tgt->cq) {
679                 printk(KERN_ERR PFX "unable to allocate CQ memory %d\n",
680                         tgt->cq_mem_size);
681                 goto mem_alloc_failure;
682         }
683         memset(tgt->cq, 0, tgt->cq_mem_size);
684
685         /* Allocate and map RQ and RQ PBL */
686         tgt->rq_mem_size = tgt->max_rqes * BNX2FC_RQ_WQE_SIZE;
687         tgt->rq_mem_size = (tgt->rq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
688
689         tgt->rq = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
690                                         &tgt->rq_dma, GFP_KERNEL);
691         if (!tgt->rq) {
692                 printk(KERN_ERR PFX "unable to allocate RQ memory %d\n",
693                         tgt->rq_mem_size);
694                 goto mem_alloc_failure;
695         }
696         memset(tgt->rq, 0, tgt->rq_mem_size);
697
698         tgt->rq_pbl_size = (tgt->rq_mem_size / PAGE_SIZE) * sizeof(void *);
699         tgt->rq_pbl_size = (tgt->rq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
700
701         tgt->rq_pbl = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
702                                          &tgt->rq_pbl_dma, GFP_KERNEL);
703         if (!tgt->rq_pbl) {
704                 printk(KERN_ERR PFX "unable to allocate RQ PBL %d\n",
705                         tgt->rq_pbl_size);
706                 goto mem_alloc_failure;
707         }
708
709         memset(tgt->rq_pbl, 0, tgt->rq_pbl_size);
710         num_pages = tgt->rq_mem_size / PAGE_SIZE;
711         page = tgt->rq_dma;
712         pbl = (u32 *)tgt->rq_pbl;
713
714         while (num_pages--) {
715                 *pbl = (u32)page;
716                 pbl++;
717                 *pbl = (u32)((u64)page >> 32);
718                 pbl++;
719                 page += PAGE_SIZE;
720         }
721
722         /* Allocate and map XFERQ */
723         tgt->xferq_mem_size = tgt->max_sqes * BNX2FC_XFERQ_WQE_SIZE;
724         tgt->xferq_mem_size = (tgt->xferq_mem_size + (PAGE_SIZE - 1)) &
725                                PAGE_MASK;
726
727         tgt->xferq = dma_alloc_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
728                                         &tgt->xferq_dma, GFP_KERNEL);
729         if (!tgt->xferq) {
730                 printk(KERN_ERR PFX "unable to allocate XFERQ %d\n",
731                         tgt->xferq_mem_size);
732                 goto mem_alloc_failure;
733         }
734         memset(tgt->xferq, 0, tgt->xferq_mem_size);
735
736         /* Allocate and map CONFQ & CONFQ PBL */
737         tgt->confq_mem_size = tgt->max_sqes * BNX2FC_CONFQ_WQE_SIZE;
738         tgt->confq_mem_size = (tgt->confq_mem_size + (PAGE_SIZE - 1)) &
739                                PAGE_MASK;
740
741         tgt->confq = dma_alloc_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
742                                         &tgt->confq_dma, GFP_KERNEL);
743         if (!tgt->confq) {
744                 printk(KERN_ERR PFX "unable to allocate CONFQ %d\n",
745                         tgt->confq_mem_size);
746                 goto mem_alloc_failure;
747         }
748         memset(tgt->confq, 0, tgt->confq_mem_size);
749
750         tgt->confq_pbl_size =
751                 (tgt->confq_mem_size / PAGE_SIZE) * sizeof(void *);
752         tgt->confq_pbl_size =
753                 (tgt->confq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
754
755         tgt->confq_pbl = dma_alloc_coherent(&hba->pcidev->dev,
756                                             tgt->confq_pbl_size,
757                                             &tgt->confq_pbl_dma, GFP_KERNEL);
758         if (!tgt->confq_pbl) {
759                 printk(KERN_ERR PFX "unable to allocate CONFQ PBL %d\n",
760                         tgt->confq_pbl_size);
761                 goto mem_alloc_failure;
762         }
763
764         memset(tgt->confq_pbl, 0, tgt->confq_pbl_size);
765         num_pages = tgt->confq_mem_size / PAGE_SIZE;
766         page = tgt->confq_dma;
767         pbl = (u32 *)tgt->confq_pbl;
768
769         while (num_pages--) {
770                 *pbl = (u32)page;
771                 pbl++;
772                 *pbl = (u32)((u64)page >> 32);
773                 pbl++;
774                 page += PAGE_SIZE;
775         }
776
777         /* Allocate and map ConnDB */
778         tgt->conn_db_mem_size = sizeof(struct fcoe_conn_db);
779
780         tgt->conn_db = dma_alloc_coherent(&hba->pcidev->dev,
781                                           tgt->conn_db_mem_size,
782                                           &tgt->conn_db_dma, GFP_KERNEL);
783         if (!tgt->conn_db) {
784                 printk(KERN_ERR PFX "unable to allocate conn_db %d\n",
785                                                 tgt->conn_db_mem_size);
786                 goto mem_alloc_failure;
787         }
788         memset(tgt->conn_db, 0, tgt->conn_db_mem_size);
789
790
791         /* Allocate and map LCQ */
792         tgt->lcq_mem_size = (tgt->max_sqes + 8) * BNX2FC_SQ_WQE_SIZE;
793         tgt->lcq_mem_size = (tgt->lcq_mem_size + (PAGE_SIZE - 1)) &
794                              PAGE_MASK;
795
796         tgt->lcq = dma_alloc_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
797                                       &tgt->lcq_dma, GFP_KERNEL);
798
799         if (!tgt->lcq) {
800                 printk(KERN_ERR PFX "unable to allocate lcq %d\n",
801                        tgt->lcq_mem_size);
802                 goto mem_alloc_failure;
803         }
804         memset(tgt->lcq, 0, tgt->lcq_mem_size);
805
806         tgt->conn_db->rq_prod = 0x8000;
807
808         return 0;
809
810 mem_alloc_failure:
811         return -ENOMEM;
812 }
813
814 /**
815  * bnx2i_free_session_resc - free qp resources for the session
816  *
817  * @hba:        adapter structure pointer
818  * @tgt:        bnx2fc_rport structure pointer
819  *
820  * Free QP resources - SQ/RQ/CQ/XFERQ memory and PBL
821  */
822 static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
823                                                 struct bnx2fc_rport *tgt)
824 {
825         void __iomem *ctx_base_ptr;
826
827         BNX2FC_TGT_DBG(tgt, "Freeing up session resources\n");
828
829         spin_lock_bh(&tgt->cq_lock);
830         ctx_base_ptr = tgt->ctx_base;
831         tgt->ctx_base = NULL;
832
833         /* Free LCQ */
834         if (tgt->lcq) {
835                 dma_free_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
836                                     tgt->lcq, tgt->lcq_dma);
837                 tgt->lcq = NULL;
838         }
839         /* Free connDB */
840         if (tgt->conn_db) {
841                 dma_free_coherent(&hba->pcidev->dev, tgt->conn_db_mem_size,
842                                     tgt->conn_db, tgt->conn_db_dma);
843                 tgt->conn_db = NULL;
844         }
845         /* Free confq  and confq pbl */
846         if (tgt->confq_pbl) {
847                 dma_free_coherent(&hba->pcidev->dev, tgt->confq_pbl_size,
848                                     tgt->confq_pbl, tgt->confq_pbl_dma);
849                 tgt->confq_pbl = NULL;
850         }
851         if (tgt->confq) {
852                 dma_free_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
853                                     tgt->confq, tgt->confq_dma);
854                 tgt->confq = NULL;
855         }
856         /* Free XFERQ */
857         if (tgt->xferq) {
858                 dma_free_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
859                                     tgt->xferq, tgt->xferq_dma);
860                 tgt->xferq = NULL;
861         }
862         /* Free RQ PBL and RQ */
863         if (tgt->rq_pbl) {
864                 dma_free_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
865                                     tgt->rq_pbl, tgt->rq_pbl_dma);
866                 tgt->rq_pbl = NULL;
867         }
868         if (tgt->rq) {
869                 dma_free_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
870                                     tgt->rq, tgt->rq_dma);
871                 tgt->rq = NULL;
872         }
873         /* Free CQ */
874         if (tgt->cq) {
875                 dma_free_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
876                                     tgt->cq, tgt->cq_dma);
877                 tgt->cq = NULL;
878         }
879         /* Free SQ */
880         if (tgt->sq) {
881                 dma_free_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
882                                     tgt->sq, tgt->sq_dma);
883                 tgt->sq = NULL;
884         }
885         spin_unlock_bh(&tgt->cq_lock);
886
887         if (ctx_base_ptr)
888                 iounmap(ctx_base_ptr);
889 }