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