blob: 236d8894bd82a031e0d216e70a10cf504de9ba95 [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
Bhanu Prakash Gollapudi26bf62a2012-12-21 19:40:30 -080062static void bnx2fc_ofld_wait(struct bnx2fc_rport *tgt)
63{
64 setup_timer(&tgt->ofld_timer, bnx2fc_ofld_timer, (unsigned long)tgt);
65 mod_timer(&tgt->ofld_timer, jiffies + BNX2FC_FW_TIMEOUT);
66
67 wait_event_interruptible(tgt->ofld_wait,
68 (test_bit(
69 BNX2FC_FLAG_OFLD_REQ_CMPL,
70 &tgt->flags)));
71 if (signal_pending(current))
72 flush_signals(current);
73 del_timer_sync(&tgt->ofld_timer);
74}
75
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080076static void bnx2fc_offload_session(struct fcoe_port *port,
77 struct bnx2fc_rport *tgt,
78 struct fc_rport_priv *rdata)
79{
80 struct fc_lport *lport = rdata->local_port;
81 struct fc_rport *rport = rdata->rport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070082 struct bnx2fc_interface *interface = port->priv;
83 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080084 int rval;
85 int i = 0;
86
87 /* Initialize bnx2fc_rport */
88 /* NOTE: tgt is already bzero'd */
89 rval = bnx2fc_init_tgt(tgt, port, rdata);
90 if (rval) {
91 printk(KERN_ERR PFX "Failed to allocate conn id for "
92 "port_id (%6x)\n", rport->port_id);
Bhanu Prakash Gollapudi5fb8fd02011-08-04 17:38:47 -070093 goto tgt_init_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080094 }
95
96 /* Allocate session resources */
97 rval = bnx2fc_alloc_session_resc(hba, tgt);
98 if (rval) {
99 printk(KERN_ERR PFX "Failed to allocate resources\n");
100 goto ofld_err;
101 }
102
103 /*
104 * Initialize FCoE session offload process.
105 * Upon completion of offload process add
106 * rport to list of rports
107 */
108retry_ofld:
109 clear_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
110 rval = bnx2fc_send_session_ofld_req(port, tgt);
111 if (rval) {
112 printk(KERN_ERR PFX "ofld_req failed\n");
113 goto ofld_err;
114 }
115
116 /*
117 * wait for the session is offloaded and enabled. 3 Secs
118 * should be ample time for this process to complete.
119 */
Bhanu Prakash Gollapudi26bf62a2012-12-21 19:40:30 -0800120 bnx2fc_ofld_wait(tgt);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800121
122 if (!(test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags))) {
123 if (test_and_clear_bit(BNX2FC_FLAG_CTX_ALLOC_FAILURE,
124 &tgt->flags)) {
125 BNX2FC_TGT_DBG(tgt, "ctx_alloc_failure, "
126 "retry ofld..%d\n", i++);
127 msleep_interruptible(1000);
128 if (i > 3) {
129 i = 0;
130 goto ofld_err;
131 }
132 goto retry_ofld;
133 }
134 goto ofld_err;
135 }
136 if (bnx2fc_map_doorbell(tgt)) {
137 printk(KERN_ERR PFX "map doorbell failed - no mem\n");
138 /* upload will take care of cleaning up sess resc */
139 lport->tt.rport_logoff(rdata);
Bhanu Prakash Gollapudia96e8e12011-08-30 15:54:53 -0700140 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800141 return;
142
143ofld_err:
144 /* couldn't offload the session. log off from this rport */
145 BNX2FC_TGT_DBG(tgt, "bnx2fc_offload_session - offload error\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800146 /* Free session resources */
147 bnx2fc_free_session_resc(hba, tgt);
Bhanu Prakash Gollapudi5fb8fd02011-08-04 17:38:47 -0700148tgt_init_err:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800149 if (tgt->fcoe_conn_id != -1)
150 bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
Bhanu Prakash Gollapudi5fb8fd02011-08-04 17:38:47 -0700151 lport->tt.rport_logoff(rdata);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800152}
153
154void bnx2fc_flush_active_ios(struct bnx2fc_rport *tgt)
155{
156 struct bnx2fc_cmd *io_req;
Bhanu Prakash Gollapudid71fb3b2012-06-07 02:19:36 -0700157 struct bnx2fc_cmd *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800158 int rc;
159 int i = 0;
160 BNX2FC_TGT_DBG(tgt, "Entered flush_active_ios - %d\n",
161 tgt->num_active_ios.counter);
162
163 spin_lock_bh(&tgt->tgt_lock);
164 tgt->flush_in_prog = 1;
165
Bhanu Prakash Gollapudid71fb3b2012-06-07 02:19:36 -0700166 list_for_each_entry_safe(io_req, tmp, &tgt->active_cmd_queue, link) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800167 i++;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800168 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);
Bhanu Prakash Gollapudi5c17ae22012-06-07 02:19:35 -0700186
187 /* Do not issue cleanup when disable request failed */
188 if (test_bit(BNX2FC_FLAG_DISABLE_FAILED, &tgt->flags))
189 bnx2fc_process_cleanup_compl(io_req, io_req->task, 0);
190 else {
191 rc = bnx2fc_initiate_cleanup(io_req);
192 BUG_ON(rc);
193 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800194 }
195
Bhanu Prakash Gollapudid71fb3b2012-06-07 02:19:36 -0700196 list_for_each_entry_safe(io_req, tmp, &tgt->active_tm_queue, link) {
Bhanu Prakash Gollapudi92886c92012-04-24 15:26:03 -0700197 i++;
Bhanu Prakash Gollapudi92886c92012-04-24 15:26:03 -0700198 list_del_init(&io_req->link);
199 io_req->on_tmf_queue = 0;
200 BNX2FC_IO_DBG(io_req, "tm_queue cleanup\n");
201 if (io_req->wait_for_comp)
202 complete(&io_req->tm_done);
203 }
204
Bhanu Prakash Gollapudid71fb3b2012-06-07 02:19:36 -0700205 list_for_each_entry_safe(io_req, tmp, &tgt->els_queue, link) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800206 i++;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800207 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
Bhanu Prakash Gollapudi5c17ae22012-06-07 02:19:35 -0700221 /* 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 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800228 }
229
Bhanu Prakash Gollapudid71fb3b2012-06-07 02:19:36 -0700230 list_for_each_entry_safe(io_req, tmp, &tgt->io_retire_queue, link) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800231 i++;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800232 list_del_init(&io_req->link);
233
234 BNX2FC_IO_DBG(io_req, "retire_queue flush\n");
235
Bhanu Prakash Gollapudic1bb4f32012-04-24 15:26:02 -0700236 if (cancel_delayed_work(&io_req->timeout_work)) {
237 if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
238 &io_req->req_flags)) {
239 /* Handle eh_abort timeout */
240 BNX2FC_IO_DBG(io_req, "eh_abort for IO "
241 "in retire_q\n");
242 if (io_req->wait_for_comp)
243 complete(&io_req->tm_done);
244 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800245 kref_put(&io_req->refcount, bnx2fc_cmd_release);
Bhanu Prakash Gollapudic1bb4f32012-04-24 15:26:02 -0700246 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800247
248 clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
249 }
250
251 BNX2FC_TGT_DBG(tgt, "IOs flushed = %d\n", i);
252 i = 0;
253 spin_unlock_bh(&tgt->tgt_lock);
254 /* wait for active_ios to go to 0 */
255 while ((tgt->num_active_ios.counter != 0) && (i++ < BNX2FC_WAIT_CNT))
256 msleep(25);
257 if (tgt->num_active_ios.counter != 0)
258 printk(KERN_ERR PFX "CLEANUP on port 0x%x:"
259 " active_ios = %d\n",
260 tgt->rdata->ids.port_id, tgt->num_active_ios.counter);
261 spin_lock_bh(&tgt->tgt_lock);
262 tgt->flush_in_prog = 0;
263 spin_unlock_bh(&tgt->tgt_lock);
264}
265
Bhanu Prakash Gollapudi26bf62a2012-12-21 19:40:30 -0800266static void bnx2fc_upld_wait(struct bnx2fc_rport *tgt)
267{
268 setup_timer(&tgt->upld_timer, bnx2fc_upld_timer, (unsigned long)tgt);
269 mod_timer(&tgt->upld_timer, jiffies + BNX2FC_FW_TIMEOUT);
270 wait_event_interruptible(tgt->upld_wait,
271 (test_bit(
272 BNX2FC_FLAG_UPLD_REQ_COMPL,
273 &tgt->flags)));
274 if (signal_pending(current))
275 flush_signals(current);
276 del_timer_sync(&tgt->upld_timer);
277}
278
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800279static void bnx2fc_upload_session(struct fcoe_port *port,
280 struct bnx2fc_rport *tgt)
281{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700282 struct bnx2fc_interface *interface = port->priv;
283 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800284
285 BNX2FC_TGT_DBG(tgt, "upload_session: active_ios = %d\n",
286 tgt->num_active_ios.counter);
287
288 /*
289 * Called with hba->hba_mutex held.
290 * This is a blocking call
291 */
292 clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
293 bnx2fc_send_session_disable_req(port, tgt);
294
295 /*
296 * wait for upload to complete. 3 Secs
297 * should be sufficient time for this process to complete.
298 */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800299 BNX2FC_TGT_DBG(tgt, "waiting for disable compl\n");
Bhanu Prakash Gollapudi26bf62a2012-12-21 19:40:30 -0800300 bnx2fc_upld_wait(tgt);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800301
302 /*
303 * traverse thru the active_q and tmf_q and cleanup
304 * IOs in these lists
305 */
306 BNX2FC_TGT_DBG(tgt, "flush/upload - disable wait flags = 0x%lx\n",
307 tgt->flags);
308 bnx2fc_flush_active_ios(tgt);
309
310 /* Issue destroy KWQE */
311 if (test_bit(BNX2FC_FLAG_DISABLED, &tgt->flags)) {
312 BNX2FC_TGT_DBG(tgt, "send destroy req\n");
313 clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
314 bnx2fc_send_session_destroy_req(hba, tgt);
315
316 /* wait for destroy to complete */
Bhanu Prakash Gollapudi26bf62a2012-12-21 19:40:30 -0800317 bnx2fc_upld_wait(tgt);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800318
319 if (!(test_bit(BNX2FC_FLAG_DESTROYED, &tgt->flags)))
320 printk(KERN_ERR PFX "ERROR!! destroy timed out\n");
321
322 BNX2FC_TGT_DBG(tgt, "destroy wait complete flags = 0x%lx\n",
323 tgt->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800324
Bhanu Prakash Gollapudi5c17ae22012-06-07 02:19:35 -0700325 } else if (test_bit(BNX2FC_FLAG_DISABLE_FAILED, &tgt->flags)) {
326 printk(KERN_ERR PFX "ERROR!! DISABLE req failed, destroy"
327 " not sent to FW\n");
328 } else {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800329 printk(KERN_ERR PFX "ERROR!! DISABLE req timed out, destroy"
330 " not sent to FW\n");
Bhanu Prakash Gollapudi5c17ae22012-06-07 02:19:35 -0700331 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800332
333 /* Free session resources */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800334 bnx2fc_free_session_resc(hba, tgt);
335 bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800336}
337
338static int bnx2fc_init_tgt(struct bnx2fc_rport *tgt,
339 struct fcoe_port *port,
340 struct fc_rport_priv *rdata)
341{
342
343 struct fc_rport *rport = rdata->rport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700344 struct bnx2fc_interface *interface = port->priv;
345 struct bnx2fc_hba *hba = interface->hba;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300346 struct b577xx_doorbell_set_prod *sq_db = &tgt->sq_db;
347 struct b577xx_fcoe_rx_doorbell *rx_db = &tgt->rx_db;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800348
349 tgt->rport = rport;
350 tgt->rdata = rdata;
351 tgt->port = port;
352
353 if (hba->num_ofld_sess >= BNX2FC_NUM_MAX_SESS) {
354 BNX2FC_TGT_DBG(tgt, "exceeded max sessions. logoff this tgt\n");
355 tgt->fcoe_conn_id = -1;
356 return -1;
357 }
358
359 tgt->fcoe_conn_id = bnx2fc_alloc_conn_id(hba, tgt);
360 if (tgt->fcoe_conn_id == -1)
361 return -1;
362
363 BNX2FC_TGT_DBG(tgt, "init_tgt - conn_id = 0x%x\n", tgt->fcoe_conn_id);
364
365 tgt->max_sqes = BNX2FC_SQ_WQES_MAX;
366 tgt->max_rqes = BNX2FC_RQ_WQES_MAX;
367 tgt->max_cqes = BNX2FC_CQ_WQES_MAX;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300368 atomic_set(&tgt->free_sqes, BNX2FC_SQ_WQES_MAX);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800369
370 /* Initialize the toggle bit */
371 tgt->sq_curr_toggle_bit = 1;
372 tgt->cq_curr_toggle_bit = 1;
373 tgt->sq_prod_idx = 0;
374 tgt->cq_cons_idx = 0;
375 tgt->rq_prod_idx = 0x8000;
376 tgt->rq_cons_idx = 0;
377 atomic_set(&tgt->num_active_ios, 0);
378
Bhanu Prakash Gollapudi50b71862013-01-10 23:59:38 -0800379 if (rdata->flags & FC_RP_FLAGS_RETRY &&
380 rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET &&
381 !(rdata->ids.roles & FC_RPORT_ROLE_FCP_INITIATOR)) {
Bhanu Prakash Gollapudib252f4c2011-07-26 14:51:40 -0700382 tgt->dev_type = TYPE_TAPE;
383 tgt->io_timeout = 0; /* use default ULP timeout */
384 } else {
385 tgt->dev_type = TYPE_DISK;
386 tgt->io_timeout = BNX2FC_IO_TIMEOUT;
387 }
388
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300389 /* initialize sq doorbell */
390 sq_db->header.header = B577XX_DOORBELL_HDR_DB_TYPE;
391 sq_db->header.header |= B577XX_FCOE_CONNECTION_TYPE <<
392 B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT;
393 /* initialize rx doorbell */
394 rx_db->hdr.header = ((0x1 << B577XX_DOORBELL_HDR_RX_SHIFT) |
395 (0x1 << B577XX_DOORBELL_HDR_DB_TYPE_SHIFT) |
396 (B577XX_FCOE_CONNECTION_TYPE <<
397 B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT));
398 rx_db->params = (0x2 << B577XX_FCOE_RX_DOORBELL_NEGATIVE_ARM_SHIFT) |
399 (0x3 << B577XX_FCOE_RX_DOORBELL_OPCODE_SHIFT);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800400
401 spin_lock_init(&tgt->tgt_lock);
402 spin_lock_init(&tgt->cq_lock);
403
404 /* Initialize active_cmd_queue list */
405 INIT_LIST_HEAD(&tgt->active_cmd_queue);
406
407 /* Initialize IO retire queue */
408 INIT_LIST_HEAD(&tgt->io_retire_queue);
409
410 INIT_LIST_HEAD(&tgt->els_queue);
411
412 /* Initialize active_tm_queue list */
413 INIT_LIST_HEAD(&tgt->active_tm_queue);
414
415 init_waitqueue_head(&tgt->ofld_wait);
416 init_waitqueue_head(&tgt->upld_wait);
417
418 return 0;
419}
420
421/**
422 * This event_callback is called after successful completion of libfc
423 * initiated target login. bnx2fc can proceed with initiating the session
424 * establishment.
425 */
426void bnx2fc_rport_event_handler(struct fc_lport *lport,
427 struct fc_rport_priv *rdata,
428 enum fc_rport_event event)
429{
430 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700431 struct bnx2fc_interface *interface = port->priv;
432 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800433 struct fc_rport *rport = rdata->rport;
434 struct fc_rport_libfc_priv *rp;
435 struct bnx2fc_rport *tgt;
436 u32 port_id;
437
438 BNX2FC_HBA_DBG(lport, "rport_event_hdlr: event = %d, port_id = 0x%x\n",
439 event, rdata->ids.port_id);
440 switch (event) {
441 case RPORT_EV_READY:
442 if (!rport) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700443 printk(KERN_ERR PFX "rport is NULL: ERROR!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800444 break;
445 }
446
447 rp = rport->dd_data;
448 if (rport->port_id == FC_FID_DIR_SERV) {
449 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300450 * bnx2fc_rport structure doesn't exist for
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800451 * directory server.
452 * We should not come here, as lport will
453 * take care of fabric login
454 */
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700455 printk(KERN_ERR PFX "%x - rport_event_handler ERROR\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800456 rdata->ids.port_id);
457 break;
458 }
459
460 if (rdata->spp_type != FC_TYPE_FCP) {
461 BNX2FC_HBA_DBG(lport, "not FCP type target."
462 " not offloading\n");
463 break;
464 }
465 if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
466 BNX2FC_HBA_DBG(lport, "not FCP_TARGET"
467 " not offloading\n");
468 break;
469 }
470
471 /*
472 * Offlaod process is protected with hba mutex.
473 * Use the same mutex_lock for upload process too
474 */
475 mutex_lock(&hba->hba_mutex);
476 tgt = (struct bnx2fc_rport *)&rp[1];
477
478 /* This can happen when ADISC finds the same target */
479 if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
480 BNX2FC_TGT_DBG(tgt, "already offloaded\n");
481 mutex_unlock(&hba->hba_mutex);
482 return;
483 }
484
485 /*
486 * Offload the session. This is a blocking call, and will
487 * wait until the session is offloaded.
488 */
489 bnx2fc_offload_session(port, tgt, rdata);
490
491 BNX2FC_TGT_DBG(tgt, "OFFLOAD num_ofld_sess = %d\n",
492 hba->num_ofld_sess);
493
494 if (test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags)) {
495 /*
496 * Session is offloaded and enabled. Map
497 * doorbell register for this target
498 */
499 BNX2FC_TGT_DBG(tgt, "sess offloaded\n");
500 /* This counter is protected with hba mutex */
501 hba->num_ofld_sess++;
502
503 set_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
504 } else {
505 /*
506 * Offload or enable would have failed.
507 * In offload/enable completion path, the
508 * rport would have already been removed
509 */
510 BNX2FC_TGT_DBG(tgt, "Port is being logged off as "
511 "offloaded flag not set\n");
512 }
513 mutex_unlock(&hba->hba_mutex);
514 break;
515 case RPORT_EV_LOGO:
516 case RPORT_EV_FAILED:
517 case RPORT_EV_STOP:
518 port_id = rdata->ids.port_id;
519 if (port_id == FC_FID_DIR_SERV)
520 break;
521
522 if (!rport) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700523 printk(KERN_INFO PFX "%x - rport not created Yet!!\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800524 port_id);
525 break;
526 }
527 rp = rport->dd_data;
528 mutex_lock(&hba->hba_mutex);
529 /*
530 * Perform session upload. Note that rdata->peers is already
531 * removed from disc->rports list before we get this event.
532 */
533 tgt = (struct bnx2fc_rport *)&rp[1];
534
535 if (!(test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags))) {
536 mutex_unlock(&hba->hba_mutex);
537 break;
538 }
539 clear_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
540
541 bnx2fc_upload_session(port, tgt);
542 hba->num_ofld_sess--;
543 BNX2FC_TGT_DBG(tgt, "UPLOAD num_ofld_sess = %d\n",
544 hba->num_ofld_sess);
545 /*
546 * Try to wake up the linkdown wait thread. If num_ofld_sess
547 * is 0, the waiting therad wakes up
548 */
549 if ((hba->wait_for_link_down) &&
550 (hba->num_ofld_sess == 0)) {
551 wake_up_interruptible(&hba->shutdown_wait);
552 }
553 if (test_bit(BNX2FC_FLAG_EXPL_LOGO, &tgt->flags)) {
554 printk(KERN_ERR PFX "Relogin to the tgt\n");
555 mutex_lock(&lport->disc.disc_mutex);
556 lport->tt.rport_login(rdata);
557 mutex_unlock(&lport->disc.disc_mutex);
558 }
559 mutex_unlock(&hba->hba_mutex);
560
561 break;
562
563 case RPORT_EV_NONE:
564 break;
565 }
566}
567
568/**
569 * bnx2fc_tgt_lookup() - Lookup a bnx2fc_rport by port_id
570 *
571 * @port: fcoe_port struct to lookup the target port on
572 * @port_id: The remote port ID to look up
573 */
574struct bnx2fc_rport *bnx2fc_tgt_lookup(struct fcoe_port *port,
575 u32 port_id)
576{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700577 struct bnx2fc_interface *interface = port->priv;
578 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800579 struct bnx2fc_rport *tgt;
580 struct fc_rport_priv *rdata;
581 int i;
582
583 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
584 tgt = hba->tgt_ofld_list[i];
585 if ((tgt) && (tgt->port == port)) {
586 rdata = tgt->rdata;
587 if (rdata->ids.port_id == port_id) {
588 if (rdata->rp_state != RPORT_ST_DELETE) {
589 BNX2FC_TGT_DBG(tgt, "rport "
590 "obtained\n");
591 return tgt;
592 } else {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700593 BNX2FC_TGT_DBG(tgt, "rport 0x%x "
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800594 "is in DELETED state\n",
595 rdata->ids.port_id);
596 return NULL;
597 }
598 }
599 }
600 }
601 return NULL;
602}
603
604
605/**
606 * bnx2fc_alloc_conn_id - allocates FCOE Connection id
607 *
608 * @hba: pointer to adapter structure
609 * @tgt: pointer to bnx2fc_rport structure
610 */
611static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
612 struct bnx2fc_rport *tgt)
613{
614 u32 conn_id, next;
615
616 /* called with hba mutex held */
617
618 /*
619 * tgt_ofld_list access is synchronized using
620 * both hba mutex and hba lock. Atleast hba mutex or
621 * hba lock needs to be held for read access.
622 */
623
624 spin_lock_bh(&hba->hba_lock);
625 next = hba->next_conn_id;
626 conn_id = hba->next_conn_id++;
627 if (hba->next_conn_id == BNX2FC_NUM_MAX_SESS)
628 hba->next_conn_id = 0;
629
630 while (hba->tgt_ofld_list[conn_id] != NULL) {
631 conn_id++;
632 if (conn_id == BNX2FC_NUM_MAX_SESS)
633 conn_id = 0;
634
635 if (conn_id == next) {
636 /* No free conn_ids are available */
637 spin_unlock_bh(&hba->hba_lock);
638 return -1;
639 }
640 }
641 hba->tgt_ofld_list[conn_id] = tgt;
642 tgt->fcoe_conn_id = conn_id;
643 spin_unlock_bh(&hba->hba_lock);
644 return conn_id;
645}
646
647static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id)
648{
649 /* called with hba mutex held */
650 spin_lock_bh(&hba->hba_lock);
651 hba->tgt_ofld_list[conn_id] = NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800652 spin_unlock_bh(&hba->hba_lock);
653}
654
655/**
656 *bnx2fc_alloc_session_resc - Allocate qp resources for the session
657 *
658 */
659static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
660 struct bnx2fc_rport *tgt)
661{
662 dma_addr_t page;
663 int num_pages;
664 u32 *pbl;
665
666 /* Allocate and map SQ */
667 tgt->sq_mem_size = tgt->max_sqes * BNX2FC_SQ_WQE_SIZE;
668 tgt->sq_mem_size = (tgt->sq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
669
670 tgt->sq = dma_alloc_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
671 &tgt->sq_dma, GFP_KERNEL);
672 if (!tgt->sq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700673 printk(KERN_ERR PFX "unable to allocate SQ memory %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800674 tgt->sq_mem_size);
675 goto mem_alloc_failure;
676 }
677 memset(tgt->sq, 0, tgt->sq_mem_size);
678
679 /* Allocate and map CQ */
680 tgt->cq_mem_size = tgt->max_cqes * BNX2FC_CQ_WQE_SIZE;
681 tgt->cq_mem_size = (tgt->cq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
682
683 tgt->cq = dma_alloc_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
684 &tgt->cq_dma, GFP_KERNEL);
685 if (!tgt->cq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700686 printk(KERN_ERR PFX "unable to allocate CQ memory %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800687 tgt->cq_mem_size);
688 goto mem_alloc_failure;
689 }
690 memset(tgt->cq, 0, tgt->cq_mem_size);
691
692 /* Allocate and map RQ and RQ PBL */
693 tgt->rq_mem_size = tgt->max_rqes * BNX2FC_RQ_WQE_SIZE;
694 tgt->rq_mem_size = (tgt->rq_mem_size + (PAGE_SIZE - 1)) & PAGE_MASK;
695
696 tgt->rq = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
697 &tgt->rq_dma, GFP_KERNEL);
698 if (!tgt->rq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700699 printk(KERN_ERR PFX "unable to allocate RQ memory %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800700 tgt->rq_mem_size);
701 goto mem_alloc_failure;
702 }
703 memset(tgt->rq, 0, tgt->rq_mem_size);
704
705 tgt->rq_pbl_size = (tgt->rq_mem_size / PAGE_SIZE) * sizeof(void *);
706 tgt->rq_pbl_size = (tgt->rq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
707
708 tgt->rq_pbl = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
709 &tgt->rq_pbl_dma, GFP_KERNEL);
710 if (!tgt->rq_pbl) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700711 printk(KERN_ERR PFX "unable to allocate RQ PBL %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800712 tgt->rq_pbl_size);
713 goto mem_alloc_failure;
714 }
715
716 memset(tgt->rq_pbl, 0, tgt->rq_pbl_size);
717 num_pages = tgt->rq_mem_size / PAGE_SIZE;
718 page = tgt->rq_dma;
719 pbl = (u32 *)tgt->rq_pbl;
720
721 while (num_pages--) {
722 *pbl = (u32)page;
723 pbl++;
724 *pbl = (u32)((u64)page >> 32);
725 pbl++;
726 page += PAGE_SIZE;
727 }
728
729 /* Allocate and map XFERQ */
730 tgt->xferq_mem_size = tgt->max_sqes * BNX2FC_XFERQ_WQE_SIZE;
731 tgt->xferq_mem_size = (tgt->xferq_mem_size + (PAGE_SIZE - 1)) &
732 PAGE_MASK;
733
734 tgt->xferq = dma_alloc_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
735 &tgt->xferq_dma, GFP_KERNEL);
736 if (!tgt->xferq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700737 printk(KERN_ERR PFX "unable to allocate XFERQ %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800738 tgt->xferq_mem_size);
739 goto mem_alloc_failure;
740 }
741 memset(tgt->xferq, 0, tgt->xferq_mem_size);
742
743 /* Allocate and map CONFQ & CONFQ PBL */
744 tgt->confq_mem_size = tgt->max_sqes * BNX2FC_CONFQ_WQE_SIZE;
745 tgt->confq_mem_size = (tgt->confq_mem_size + (PAGE_SIZE - 1)) &
746 PAGE_MASK;
747
748 tgt->confq = dma_alloc_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
749 &tgt->confq_dma, GFP_KERNEL);
750 if (!tgt->confq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700751 printk(KERN_ERR PFX "unable to allocate CONFQ %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800752 tgt->confq_mem_size);
753 goto mem_alloc_failure;
754 }
755 memset(tgt->confq, 0, tgt->confq_mem_size);
756
757 tgt->confq_pbl_size =
758 (tgt->confq_mem_size / PAGE_SIZE) * sizeof(void *);
759 tgt->confq_pbl_size =
760 (tgt->confq_pbl_size + (PAGE_SIZE - 1)) & PAGE_MASK;
761
762 tgt->confq_pbl = dma_alloc_coherent(&hba->pcidev->dev,
763 tgt->confq_pbl_size,
764 &tgt->confq_pbl_dma, GFP_KERNEL);
765 if (!tgt->confq_pbl) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700766 printk(KERN_ERR PFX "unable to allocate CONFQ PBL %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800767 tgt->confq_pbl_size);
768 goto mem_alloc_failure;
769 }
770
771 memset(tgt->confq_pbl, 0, tgt->confq_pbl_size);
772 num_pages = tgt->confq_mem_size / PAGE_SIZE;
773 page = tgt->confq_dma;
774 pbl = (u32 *)tgt->confq_pbl;
775
776 while (num_pages--) {
777 *pbl = (u32)page;
778 pbl++;
779 *pbl = (u32)((u64)page >> 32);
780 pbl++;
781 page += PAGE_SIZE;
782 }
783
784 /* Allocate and map ConnDB */
785 tgt->conn_db_mem_size = sizeof(struct fcoe_conn_db);
786
787 tgt->conn_db = dma_alloc_coherent(&hba->pcidev->dev,
788 tgt->conn_db_mem_size,
789 &tgt->conn_db_dma, GFP_KERNEL);
790 if (!tgt->conn_db) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700791 printk(KERN_ERR PFX "unable to allocate conn_db %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800792 tgt->conn_db_mem_size);
793 goto mem_alloc_failure;
794 }
795 memset(tgt->conn_db, 0, tgt->conn_db_mem_size);
796
797
798 /* Allocate and map LCQ */
799 tgt->lcq_mem_size = (tgt->max_sqes + 8) * BNX2FC_SQ_WQE_SIZE;
800 tgt->lcq_mem_size = (tgt->lcq_mem_size + (PAGE_SIZE - 1)) &
801 PAGE_MASK;
802
803 tgt->lcq = dma_alloc_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
804 &tgt->lcq_dma, GFP_KERNEL);
805
806 if (!tgt->lcq) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700807 printk(KERN_ERR PFX "unable to allocate lcq %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800808 tgt->lcq_mem_size);
809 goto mem_alloc_failure;
810 }
811 memset(tgt->lcq, 0, tgt->lcq_mem_size);
812
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800813 tgt->conn_db->rq_prod = 0x8000;
814
815 return 0;
816
817mem_alloc_failure:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800818 return -ENOMEM;
819}
820
821/**
822 * bnx2i_free_session_resc - free qp resources for the session
823 *
824 * @hba: adapter structure pointer
825 * @tgt: bnx2fc_rport structure pointer
826 *
827 * Free QP resources - SQ/RQ/CQ/XFERQ memory and PBL
828 */
829static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
830 struct bnx2fc_rport *tgt)
831{
Bhanu Prakash Gollapudib338c782011-08-04 17:38:46 -0700832 void __iomem *ctx_base_ptr;
833
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800834 BNX2FC_TGT_DBG(tgt, "Freeing up session resources\n");
835
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300836 spin_lock_bh(&tgt->cq_lock);
Bhanu Prakash Gollapudib338c782011-08-04 17:38:46 -0700837 ctx_base_ptr = tgt->ctx_base;
838 tgt->ctx_base = NULL;
839
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800840 /* Free LCQ */
841 if (tgt->lcq) {
842 dma_free_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
843 tgt->lcq, tgt->lcq_dma);
844 tgt->lcq = NULL;
845 }
846 /* Free connDB */
847 if (tgt->conn_db) {
848 dma_free_coherent(&hba->pcidev->dev, tgt->conn_db_mem_size,
849 tgt->conn_db, tgt->conn_db_dma);
850 tgt->conn_db = NULL;
851 }
852 /* Free confq and confq pbl */
853 if (tgt->confq_pbl) {
854 dma_free_coherent(&hba->pcidev->dev, tgt->confq_pbl_size,
855 tgt->confq_pbl, tgt->confq_pbl_dma);
856 tgt->confq_pbl = NULL;
857 }
858 if (tgt->confq) {
859 dma_free_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
860 tgt->confq, tgt->confq_dma);
861 tgt->confq = NULL;
862 }
863 /* Free XFERQ */
864 if (tgt->xferq) {
865 dma_free_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
866 tgt->xferq, tgt->xferq_dma);
867 tgt->xferq = NULL;
868 }
869 /* Free RQ PBL and RQ */
870 if (tgt->rq_pbl) {
871 dma_free_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
872 tgt->rq_pbl, tgt->rq_pbl_dma);
873 tgt->rq_pbl = NULL;
874 }
875 if (tgt->rq) {
876 dma_free_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
877 tgt->rq, tgt->rq_dma);
878 tgt->rq = NULL;
879 }
880 /* Free CQ */
881 if (tgt->cq) {
882 dma_free_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
883 tgt->cq, tgt->cq_dma);
884 tgt->cq = NULL;
885 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800886 /* Free SQ */
887 if (tgt->sq) {
888 dma_free_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
889 tgt->sq, tgt->sq_dma);
890 tgt->sq = NULL;
891 }
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300892 spin_unlock_bh(&tgt->cq_lock);
Bhanu Prakash Gollapudib338c782011-08-04 17:38:46 -0700893
894 if (ctx_base_ptr)
895 iounmap(ctx_base_ptr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800896}