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