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