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