blob: b41deb64aff10558b0477815140daa160f9b5e1b [file] [log] [blame]
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001/* bnx2fc_hwi.c: Broadcom NetXtreme II Linux FCoE offload driver.
2 * This file contains the code that low level functions that interact
3 * with 57712 FCoE firmware.
4 *
5 * Copyright (c) 2008 - 2010 Broadcom Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation.
10 *
11 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
12 */
13
14#include "bnx2fc.h"
15
16DECLARE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
17
18static void bnx2fc_fastpath_notification(struct bnx2fc_hba *hba,
19 struct fcoe_kcqe *new_cqe_kcqe);
20static void bnx2fc_process_ofld_cmpl(struct bnx2fc_hba *hba,
21 struct fcoe_kcqe *ofld_kcqe);
22static void bnx2fc_process_enable_conn_cmpl(struct bnx2fc_hba *hba,
23 struct fcoe_kcqe *ofld_kcqe);
24static void bnx2fc_init_failure(struct bnx2fc_hba *hba, u32 err_code);
25static void bnx2fc_process_conn_destroy_cmpl(struct bnx2fc_hba *hba,
26 struct fcoe_kcqe *conn_destroy);
27
28int bnx2fc_send_stat_req(struct bnx2fc_hba *hba)
29{
30 struct fcoe_kwqe_stat stat_req;
31 struct kwqe *kwqe_arr[2];
32 int num_kwqes = 1;
33 int rc = 0;
34
35 memset(&stat_req, 0x00, sizeof(struct fcoe_kwqe_stat));
36 stat_req.hdr.op_code = FCOE_KWQE_OPCODE_STAT;
37 stat_req.hdr.flags =
38 (FCOE_KWQE_LAYER_CODE << FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
39
40 stat_req.stat_params_addr_lo = (u32) hba->stats_buf_dma;
41 stat_req.stat_params_addr_hi = (u32) ((u64)hba->stats_buf_dma >> 32);
42
43 kwqe_arr[0] = (struct kwqe *) &stat_req;
44
45 if (hba->cnic && hba->cnic->submit_kwqes)
46 rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, num_kwqes);
47
48 return rc;
49}
50
51/**
52 * bnx2fc_send_fw_fcoe_init_msg - initiates initial handshake with FCoE f/w
53 *
54 * @hba: adapter structure pointer
55 *
56 * Send down FCoE firmware init KWQEs which initiates the initial handshake
57 * with the f/w.
58 *
59 */
60int bnx2fc_send_fw_fcoe_init_msg(struct bnx2fc_hba *hba)
61{
62 struct fcoe_kwqe_init1 fcoe_init1;
63 struct fcoe_kwqe_init2 fcoe_init2;
64 struct fcoe_kwqe_init3 fcoe_init3;
65 struct kwqe *kwqe_arr[3];
66 int num_kwqes = 3;
67 int rc = 0;
68
69 if (!hba->cnic) {
70 printk(KERN_ALERT PFX "hba->cnic NULL during fcoe fw init\n");
71 return -ENODEV;
72 }
73
74 /* fill init1 KWQE */
75 memset(&fcoe_init1, 0x00, sizeof(struct fcoe_kwqe_init1));
76 fcoe_init1.hdr.op_code = FCOE_KWQE_OPCODE_INIT1;
77 fcoe_init1.hdr.flags = (FCOE_KWQE_LAYER_CODE <<
78 FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
79
80 fcoe_init1.num_tasks = BNX2FC_MAX_TASKS;
81 fcoe_init1.sq_num_wqes = BNX2FC_SQ_WQES_MAX;
82 fcoe_init1.rq_num_wqes = BNX2FC_RQ_WQES_MAX;
83 fcoe_init1.rq_buffer_log_size = BNX2FC_RQ_BUF_LOG_SZ;
84 fcoe_init1.cq_num_wqes = BNX2FC_CQ_WQES_MAX;
85 fcoe_init1.dummy_buffer_addr_lo = (u32) hba->dummy_buf_dma;
86 fcoe_init1.dummy_buffer_addr_hi = (u32) ((u64)hba->dummy_buf_dma >> 32);
87 fcoe_init1.task_list_pbl_addr_lo = (u32) hba->task_ctx_bd_dma;
88 fcoe_init1.task_list_pbl_addr_hi =
89 (u32) ((u64) hba->task_ctx_bd_dma >> 32);
Bhanu Gollapudi1294bfe2011-03-17 17:13:34 -070090 fcoe_init1.mtu = BNX2FC_MINI_JUMBO_MTU;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080091
92 fcoe_init1.flags = (PAGE_SHIFT <<
93 FCOE_KWQE_INIT1_LOG_PAGE_SIZE_SHIFT);
94
95 fcoe_init1.num_sessions_log = BNX2FC_NUM_MAX_SESS_LOG;
96
97 /* fill init2 KWQE */
98 memset(&fcoe_init2, 0x00, sizeof(struct fcoe_kwqe_init2));
99 fcoe_init2.hdr.op_code = FCOE_KWQE_OPCODE_INIT2;
100 fcoe_init2.hdr.flags = (FCOE_KWQE_LAYER_CODE <<
101 FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
102
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300103 fcoe_init2.hsi_major_version = FCOE_HSI_MAJOR_VERSION;
104 fcoe_init2.hsi_minor_version = FCOE_HSI_MINOR_VERSION;
105
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800106 fcoe_init2.hash_tbl_pbl_addr_lo = (u32) hba->hash_tbl_pbl_dma;
107 fcoe_init2.hash_tbl_pbl_addr_hi = (u32)
108 ((u64) hba->hash_tbl_pbl_dma >> 32);
109
110 fcoe_init2.t2_hash_tbl_addr_lo = (u32) hba->t2_hash_tbl_dma;
111 fcoe_init2.t2_hash_tbl_addr_hi = (u32)
112 ((u64) hba->t2_hash_tbl_dma >> 32);
113
114 fcoe_init2.t2_ptr_hash_tbl_addr_lo = (u32) hba->t2_hash_tbl_ptr_dma;
115 fcoe_init2.t2_ptr_hash_tbl_addr_hi = (u32)
116 ((u64) hba->t2_hash_tbl_ptr_dma >> 32);
117
118 fcoe_init2.free_list_count = BNX2FC_NUM_MAX_SESS;
119
120 /* fill init3 KWQE */
121 memset(&fcoe_init3, 0x00, sizeof(struct fcoe_kwqe_init3));
122 fcoe_init3.hdr.op_code = FCOE_KWQE_OPCODE_INIT3;
123 fcoe_init3.hdr.flags = (FCOE_KWQE_LAYER_CODE <<
124 FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
125 fcoe_init3.error_bit_map_lo = 0xffffffff;
126 fcoe_init3.error_bit_map_hi = 0xffffffff;
127
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300128 fcoe_init3.perf_config = 1;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800129
130 kwqe_arr[0] = (struct kwqe *) &fcoe_init1;
131 kwqe_arr[1] = (struct kwqe *) &fcoe_init2;
132 kwqe_arr[2] = (struct kwqe *) &fcoe_init3;
133
134 if (hba->cnic && hba->cnic->submit_kwqes)
135 rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, num_kwqes);
136
137 return rc;
138}
139int bnx2fc_send_fw_fcoe_destroy_msg(struct bnx2fc_hba *hba)
140{
141 struct fcoe_kwqe_destroy fcoe_destroy;
142 struct kwqe *kwqe_arr[2];
143 int num_kwqes = 1;
144 int rc = -1;
145
146 /* fill destroy KWQE */
147 memset(&fcoe_destroy, 0x00, sizeof(struct fcoe_kwqe_destroy));
148 fcoe_destroy.hdr.op_code = FCOE_KWQE_OPCODE_DESTROY;
149 fcoe_destroy.hdr.flags = (FCOE_KWQE_LAYER_CODE <<
150 FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
151 kwqe_arr[0] = (struct kwqe *) &fcoe_destroy;
152
153 if (hba->cnic && hba->cnic->submit_kwqes)
154 rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, num_kwqes);
155 return rc;
156}
157
158/**
159 * bnx2fc_send_session_ofld_req - initiates FCoE Session offload process
160 *
161 * @port: port structure pointer
162 * @tgt: bnx2fc_rport structure pointer
163 */
164int bnx2fc_send_session_ofld_req(struct fcoe_port *port,
165 struct bnx2fc_rport *tgt)
166{
167 struct fc_lport *lport = port->lport;
168 struct bnx2fc_hba *hba = port->priv;
169 struct kwqe *kwqe_arr[4];
170 struct fcoe_kwqe_conn_offload1 ofld_req1;
171 struct fcoe_kwqe_conn_offload2 ofld_req2;
172 struct fcoe_kwqe_conn_offload3 ofld_req3;
173 struct fcoe_kwqe_conn_offload4 ofld_req4;
174 struct fc_rport_priv *rdata = tgt->rdata;
175 struct fc_rport *rport = tgt->rport;
176 int num_kwqes = 4;
177 u32 port_id;
178 int rc = 0;
179 u16 conn_id;
180
181 /* Initialize offload request 1 structure */
182 memset(&ofld_req1, 0x00, sizeof(struct fcoe_kwqe_conn_offload1));
183
184 ofld_req1.hdr.op_code = FCOE_KWQE_OPCODE_OFFLOAD_CONN1;
185 ofld_req1.hdr.flags =
186 (FCOE_KWQE_LAYER_CODE << FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
187
188
189 conn_id = (u16)tgt->fcoe_conn_id;
190 ofld_req1.fcoe_conn_id = conn_id;
191
192
193 ofld_req1.sq_addr_lo = (u32) tgt->sq_dma;
194 ofld_req1.sq_addr_hi = (u32)((u64) tgt->sq_dma >> 32);
195
196 ofld_req1.rq_pbl_addr_lo = (u32) tgt->rq_pbl_dma;
197 ofld_req1.rq_pbl_addr_hi = (u32)((u64) tgt->rq_pbl_dma >> 32);
198
199 ofld_req1.rq_first_pbe_addr_lo = (u32) tgt->rq_dma;
200 ofld_req1.rq_first_pbe_addr_hi =
201 (u32)((u64) tgt->rq_dma >> 32);
202
203 ofld_req1.rq_prod = 0x8000;
204
205 /* Initialize offload request 2 structure */
206 memset(&ofld_req2, 0x00, sizeof(struct fcoe_kwqe_conn_offload2));
207
208 ofld_req2.hdr.op_code = FCOE_KWQE_OPCODE_OFFLOAD_CONN2;
209 ofld_req2.hdr.flags =
210 (FCOE_KWQE_LAYER_CODE << FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
211
212 ofld_req2.tx_max_fc_pay_len = rdata->maxframe_size;
213
214 ofld_req2.cq_addr_lo = (u32) tgt->cq_dma;
215 ofld_req2.cq_addr_hi = (u32)((u64)tgt->cq_dma >> 32);
216
217 ofld_req2.xferq_addr_lo = (u32) tgt->xferq_dma;
218 ofld_req2.xferq_addr_hi = (u32)((u64)tgt->xferq_dma >> 32);
219
220 ofld_req2.conn_db_addr_lo = (u32)tgt->conn_db_dma;
221 ofld_req2.conn_db_addr_hi = (u32)((u64)tgt->conn_db_dma >> 32);
222
223 /* Initialize offload request 3 structure */
224 memset(&ofld_req3, 0x00, sizeof(struct fcoe_kwqe_conn_offload3));
225
226 ofld_req3.hdr.op_code = FCOE_KWQE_OPCODE_OFFLOAD_CONN3;
227 ofld_req3.hdr.flags =
228 (FCOE_KWQE_LAYER_CODE << FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
229
230 ofld_req3.vlan_tag = hba->vlan_id <<
231 FCOE_KWQE_CONN_OFFLOAD3_VLAN_ID_SHIFT;
232 ofld_req3.vlan_tag |= 3 << FCOE_KWQE_CONN_OFFLOAD3_PRIORITY_SHIFT;
233
234 port_id = fc_host_port_id(lport->host);
235 if (port_id == 0) {
236 BNX2FC_HBA_DBG(lport, "ofld_req: port_id = 0, link down?\n");
237 return -EINVAL;
238 }
239
240 /*
241 * Store s_id of the initiator for further reference. This will
242 * be used during disable/destroy during linkdown processing as
243 * when the lport is reset, the port_id also is reset to 0
244 */
245 tgt->sid = port_id;
246 ofld_req3.s_id[0] = (port_id & 0x000000FF);
247 ofld_req3.s_id[1] = (port_id & 0x0000FF00) >> 8;
248 ofld_req3.s_id[2] = (port_id & 0x00FF0000) >> 16;
249
250 port_id = rport->port_id;
251 ofld_req3.d_id[0] = (port_id & 0x000000FF);
252 ofld_req3.d_id[1] = (port_id & 0x0000FF00) >> 8;
253 ofld_req3.d_id[2] = (port_id & 0x00FF0000) >> 16;
254
255 ofld_req3.tx_total_conc_seqs = rdata->max_seq;
256
257 ofld_req3.tx_max_conc_seqs_c3 = rdata->max_seq;
258 ofld_req3.rx_max_fc_pay_len = lport->mfs;
259
260 ofld_req3.rx_total_conc_seqs = BNX2FC_MAX_SEQS;
261 ofld_req3.rx_max_conc_seqs_c3 = BNX2FC_MAX_SEQS;
262 ofld_req3.rx_open_seqs_exch_c3 = 1;
263
264 ofld_req3.confq_first_pbe_addr_lo = tgt->confq_dma;
265 ofld_req3.confq_first_pbe_addr_hi = (u32)((u64) tgt->confq_dma >> 32);
266
267 /* set mul_n_port_ids supported flag to 0, until it is supported */
268 ofld_req3.flags = 0;
269 /*
270 ofld_req3.flags |= (((lport->send_sp_features & FC_SP_FT_MNA) ? 1:0) <<
271 FCOE_KWQE_CONN_OFFLOAD3_B_MUL_N_PORT_IDS_SHIFT);
272 */
273 /* Info from PLOGI response */
274 ofld_req3.flags |= (((rdata->sp_features & FC_SP_FT_EDTR) ? 1 : 0) <<
275 FCOE_KWQE_CONN_OFFLOAD3_B_E_D_TOV_RES_SHIFT);
276
277 ofld_req3.flags |= (((rdata->sp_features & FC_SP_FT_SEQC) ? 1 : 0) <<
278 FCOE_KWQE_CONN_OFFLOAD3_B_CONT_INCR_SEQ_CNT_SHIFT);
279
280 /* vlan flag */
281 ofld_req3.flags |= (hba->vlan_enabled <<
282 FCOE_KWQE_CONN_OFFLOAD3_B_VLAN_FLAG_SHIFT);
283
284 /* C2_VALID and ACK flags are not set as they are not suppported */
285
286
287 /* Initialize offload request 4 structure */
288 memset(&ofld_req4, 0x00, sizeof(struct fcoe_kwqe_conn_offload4));
289 ofld_req4.hdr.op_code = FCOE_KWQE_OPCODE_OFFLOAD_CONN4;
290 ofld_req4.hdr.flags =
291 (FCOE_KWQE_LAYER_CODE << FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
292
293 ofld_req4.e_d_tov_timer_val = lport->e_d_tov / 20;
294
295
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300296 ofld_req4.src_mac_addr_lo[0] = port->data_src_addr[5];
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800297 /* local mac */
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300298 ofld_req4.src_mac_addr_lo[1] = port->data_src_addr[4];
299 ofld_req4.src_mac_addr_mid[0] = port->data_src_addr[3];
300 ofld_req4.src_mac_addr_mid[1] = port->data_src_addr[2];
301 ofld_req4.src_mac_addr_hi[0] = port->data_src_addr[1];
302 ofld_req4.src_mac_addr_hi[1] = port->data_src_addr[0];
303 ofld_req4.dst_mac_addr_lo[0] = hba->ctlr.dest_addr[5];/* fcf mac */
304 ofld_req4.dst_mac_addr_lo[1] = hba->ctlr.dest_addr[4];
305 ofld_req4.dst_mac_addr_mid[0] = hba->ctlr.dest_addr[3];
306 ofld_req4.dst_mac_addr_mid[1] = hba->ctlr.dest_addr[2];
307 ofld_req4.dst_mac_addr_hi[0] = hba->ctlr.dest_addr[1];
308 ofld_req4.dst_mac_addr_hi[1] = hba->ctlr.dest_addr[0];
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800309
310 ofld_req4.lcq_addr_lo = (u32) tgt->lcq_dma;
311 ofld_req4.lcq_addr_hi = (u32)((u64) tgt->lcq_dma >> 32);
312
313 ofld_req4.confq_pbl_base_addr_lo = (u32) tgt->confq_pbl_dma;
314 ofld_req4.confq_pbl_base_addr_hi =
315 (u32)((u64) tgt->confq_pbl_dma >> 32);
316
317 kwqe_arr[0] = (struct kwqe *) &ofld_req1;
318 kwqe_arr[1] = (struct kwqe *) &ofld_req2;
319 kwqe_arr[2] = (struct kwqe *) &ofld_req3;
320 kwqe_arr[3] = (struct kwqe *) &ofld_req4;
321
322 if (hba->cnic && hba->cnic->submit_kwqes)
323 rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, num_kwqes);
324
325 return rc;
326}
327
328/**
329 * bnx2fc_send_session_enable_req - initiates FCoE Session enablement
330 *
331 * @port: port structure pointer
332 * @tgt: bnx2fc_rport structure pointer
333 */
334static int bnx2fc_send_session_enable_req(struct fcoe_port *port,
335 struct bnx2fc_rport *tgt)
336{
337 struct kwqe *kwqe_arr[2];
338 struct bnx2fc_hba *hba = port->priv;
339 struct fcoe_kwqe_conn_enable_disable enbl_req;
340 struct fc_lport *lport = port->lport;
341 struct fc_rport *rport = tgt->rport;
342 int num_kwqes = 1;
343 int rc = 0;
344 u32 port_id;
345
346 memset(&enbl_req, 0x00,
347 sizeof(struct fcoe_kwqe_conn_enable_disable));
348 enbl_req.hdr.op_code = FCOE_KWQE_OPCODE_ENABLE_CONN;
349 enbl_req.hdr.flags =
350 (FCOE_KWQE_LAYER_CODE << FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
351
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300352 enbl_req.src_mac_addr_lo[0] = port->data_src_addr[5];
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800353 /* local mac */
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300354 enbl_req.src_mac_addr_lo[1] = port->data_src_addr[4];
355 enbl_req.src_mac_addr_mid[0] = port->data_src_addr[3];
356 enbl_req.src_mac_addr_mid[1] = port->data_src_addr[2];
357 enbl_req.src_mac_addr_hi[0] = port->data_src_addr[1];
358 enbl_req.src_mac_addr_hi[1] = port->data_src_addr[0];
359 memcpy(tgt->src_addr, port->data_src_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800360
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300361 enbl_req.dst_mac_addr_lo[0] = hba->ctlr.dest_addr[5];/* fcf mac */
362 enbl_req.dst_mac_addr_lo[1] = hba->ctlr.dest_addr[4];
363 enbl_req.dst_mac_addr_mid[0] = hba->ctlr.dest_addr[3];
364 enbl_req.dst_mac_addr_mid[1] = hba->ctlr.dest_addr[2];
365 enbl_req.dst_mac_addr_hi[0] = hba->ctlr.dest_addr[1];
366 enbl_req.dst_mac_addr_hi[1] = hba->ctlr.dest_addr[0];
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800367
368 port_id = fc_host_port_id(lport->host);
369 if (port_id != tgt->sid) {
370 printk(KERN_ERR PFX "WARN: enable_req port_id = 0x%x,"
371 "sid = 0x%x\n", port_id, tgt->sid);
372 port_id = tgt->sid;
373 }
374 enbl_req.s_id[0] = (port_id & 0x000000FF);
375 enbl_req.s_id[1] = (port_id & 0x0000FF00) >> 8;
376 enbl_req.s_id[2] = (port_id & 0x00FF0000) >> 16;
377
378 port_id = rport->port_id;
379 enbl_req.d_id[0] = (port_id & 0x000000FF);
380 enbl_req.d_id[1] = (port_id & 0x0000FF00) >> 8;
381 enbl_req.d_id[2] = (port_id & 0x00FF0000) >> 16;
382 enbl_req.vlan_tag = hba->vlan_id <<
383 FCOE_KWQE_CONN_ENABLE_DISABLE_VLAN_ID_SHIFT;
384 enbl_req.vlan_tag |= 3 << FCOE_KWQE_CONN_ENABLE_DISABLE_PRIORITY_SHIFT;
385 enbl_req.vlan_flag = hba->vlan_enabled;
386 enbl_req.context_id = tgt->context_id;
387 enbl_req.conn_id = tgt->fcoe_conn_id;
388
389 kwqe_arr[0] = (struct kwqe *) &enbl_req;
390
391 if (hba->cnic && hba->cnic->submit_kwqes)
392 rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, num_kwqes);
393 return rc;
394}
395
396/**
397 * bnx2fc_send_session_disable_req - initiates FCoE Session disable
398 *
399 * @port: port structure pointer
400 * @tgt: bnx2fc_rport structure pointer
401 */
402int bnx2fc_send_session_disable_req(struct fcoe_port *port,
403 struct bnx2fc_rport *tgt)
404{
405 struct bnx2fc_hba *hba = port->priv;
406 struct fcoe_kwqe_conn_enable_disable disable_req;
407 struct kwqe *kwqe_arr[2];
408 struct fc_rport *rport = tgt->rport;
409 int num_kwqes = 1;
410 int rc = 0;
411 u32 port_id;
412
413 memset(&disable_req, 0x00,
414 sizeof(struct fcoe_kwqe_conn_enable_disable));
415 disable_req.hdr.op_code = FCOE_KWQE_OPCODE_DISABLE_CONN;
416 disable_req.hdr.flags =
417 (FCOE_KWQE_LAYER_CODE << FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
418
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300419 disable_req.src_mac_addr_lo[0] = tgt->src_addr[5];
420 disable_req.src_mac_addr_lo[1] = tgt->src_addr[4];
421 disable_req.src_mac_addr_mid[0] = tgt->src_addr[3];
422 disable_req.src_mac_addr_mid[1] = tgt->src_addr[2];
423 disable_req.src_mac_addr_hi[0] = tgt->src_addr[1];
424 disable_req.src_mac_addr_hi[1] = tgt->src_addr[0];
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800425
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300426 disable_req.dst_mac_addr_lo[0] = hba->ctlr.dest_addr[5];/* fcf mac */
427 disable_req.dst_mac_addr_lo[1] = hba->ctlr.dest_addr[4];
428 disable_req.dst_mac_addr_mid[0] = hba->ctlr.dest_addr[3];
429 disable_req.dst_mac_addr_mid[1] = hba->ctlr.dest_addr[2];
430 disable_req.dst_mac_addr_hi[0] = hba->ctlr.dest_addr[1];
431 disable_req.dst_mac_addr_hi[1] = hba->ctlr.dest_addr[0];
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800432
433 port_id = tgt->sid;
434 disable_req.s_id[0] = (port_id & 0x000000FF);
435 disable_req.s_id[1] = (port_id & 0x0000FF00) >> 8;
436 disable_req.s_id[2] = (port_id & 0x00FF0000) >> 16;
437
438
439 port_id = rport->port_id;
440 disable_req.d_id[0] = (port_id & 0x000000FF);
441 disable_req.d_id[1] = (port_id & 0x0000FF00) >> 8;
442 disable_req.d_id[2] = (port_id & 0x00FF0000) >> 16;
443 disable_req.context_id = tgt->context_id;
444 disable_req.conn_id = tgt->fcoe_conn_id;
445 disable_req.vlan_tag = hba->vlan_id <<
446 FCOE_KWQE_CONN_ENABLE_DISABLE_VLAN_ID_SHIFT;
447 disable_req.vlan_tag |=
448 3 << FCOE_KWQE_CONN_ENABLE_DISABLE_PRIORITY_SHIFT;
449 disable_req.vlan_flag = hba->vlan_enabled;
450
451 kwqe_arr[0] = (struct kwqe *) &disable_req;
452
453 if (hba->cnic && hba->cnic->submit_kwqes)
454 rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, num_kwqes);
455
456 return rc;
457}
458
459/**
460 * bnx2fc_send_session_destroy_req - initiates FCoE Session destroy
461 *
462 * @port: port structure pointer
463 * @tgt: bnx2fc_rport structure pointer
464 */
465int bnx2fc_send_session_destroy_req(struct bnx2fc_hba *hba,
466 struct bnx2fc_rport *tgt)
467{
468 struct fcoe_kwqe_conn_destroy destroy_req;
469 struct kwqe *kwqe_arr[2];
470 int num_kwqes = 1;
471 int rc = 0;
472
473 memset(&destroy_req, 0x00, sizeof(struct fcoe_kwqe_conn_destroy));
474 destroy_req.hdr.op_code = FCOE_KWQE_OPCODE_DESTROY_CONN;
475 destroy_req.hdr.flags =
476 (FCOE_KWQE_LAYER_CODE << FCOE_KWQE_HEADER_LAYER_CODE_SHIFT);
477
478 destroy_req.context_id = tgt->context_id;
479 destroy_req.conn_id = tgt->fcoe_conn_id;
480
481 kwqe_arr[0] = (struct kwqe *) &destroy_req;
482
483 if (hba->cnic && hba->cnic->submit_kwqes)
484 rc = hba->cnic->submit_kwqes(hba->cnic, kwqe_arr, num_kwqes);
485
486 return rc;
487}
488
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -0700489static bool is_valid_lport(struct bnx2fc_hba *hba, struct fc_lport *lport)
490{
491 struct bnx2fc_lport *blport;
492
493 spin_lock_bh(&hba->hba_lock);
494 list_for_each_entry(blport, &hba->vports, list) {
495 if (blport->lport == lport) {
496 spin_unlock_bh(&hba->hba_lock);
497 return true;
498 }
499 }
500 spin_unlock_bh(&hba->hba_lock);
501 return false;
502
503}
504
505
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800506static void bnx2fc_unsol_els_work(struct work_struct *work)
507{
508 struct bnx2fc_unsol_els *unsol_els;
509 struct fc_lport *lport;
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -0700510 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800511 struct fc_frame *fp;
512
513 unsol_els = container_of(work, struct bnx2fc_unsol_els, unsol_els_work);
514 lport = unsol_els->lport;
515 fp = unsol_els->fp;
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -0700516 hba = unsol_els->hba;
517 if (is_valid_lport(hba, lport))
518 fc_exch_recv(lport, fp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800519 kfree(unsol_els);
520}
521
522void bnx2fc_process_l2_frame_compl(struct bnx2fc_rport *tgt,
523 unsigned char *buf,
524 u32 frame_len, u16 l2_oxid)
525{
526 struct fcoe_port *port = tgt->port;
527 struct fc_lport *lport = port->lport;
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -0700528 struct bnx2fc_hba *hba = port->priv;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800529 struct bnx2fc_unsol_els *unsol_els;
530 struct fc_frame_header *fh;
531 struct fc_frame *fp;
532 struct sk_buff *skb;
533 u32 payload_len;
534 u32 crc;
535 u8 op;
536
537
538 unsol_els = kzalloc(sizeof(*unsol_els), GFP_ATOMIC);
539 if (!unsol_els) {
540 BNX2FC_TGT_DBG(tgt, "Unable to allocate unsol_work\n");
541 return;
542 }
543
544 BNX2FC_TGT_DBG(tgt, "l2_frame_compl l2_oxid = 0x%x, frame_len = %d\n",
545 l2_oxid, frame_len);
546
547 payload_len = frame_len - sizeof(struct fc_frame_header);
548
549 fp = fc_frame_alloc(lport, payload_len);
550 if (!fp) {
551 printk(KERN_ERR PFX "fc_frame_alloc failure\n");
Julia Lawall5c2dce22011-04-01 16:23:46 +0200552 kfree(unsol_els);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800553 return;
554 }
555
556 fh = (struct fc_frame_header *) fc_frame_header_get(fp);
557 /* Copy FC Frame header and payload into the frame */
558 memcpy(fh, buf, frame_len);
559
560 if (l2_oxid != FC_XID_UNKNOWN)
561 fh->fh_ox_id = htons(l2_oxid);
562
563 skb = fp_skb(fp);
564
565 if ((fh->fh_r_ctl == FC_RCTL_ELS_REQ) ||
566 (fh->fh_r_ctl == FC_RCTL_ELS_REP)) {
567
568 if (fh->fh_type == FC_TYPE_ELS) {
569 op = fc_frame_payload_op(fp);
570 if ((op == ELS_TEST) || (op == ELS_ESTC) ||
571 (op == ELS_FAN) || (op == ELS_CSU)) {
572 /*
573 * No need to reply for these
574 * ELS requests
575 */
576 printk(KERN_ERR PFX "dropping ELS 0x%x\n", op);
577 kfree_skb(skb);
Julia Lawall5c2dce22011-04-01 16:23:46 +0200578 kfree(unsol_els);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800579 return;
580 }
581 }
582 crc = fcoe_fc_crc(fp);
583 fc_frame_init(fp);
584 fr_dev(fp) = lport;
585 fr_sof(fp) = FC_SOF_I3;
586 fr_eof(fp) = FC_EOF_T;
587 fr_crc(fp) = cpu_to_le32(~crc);
588 unsol_els->lport = lport;
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -0700589 unsol_els->hba = hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800590 unsol_els->fp = fp;
591 INIT_WORK(&unsol_els->unsol_els_work, bnx2fc_unsol_els_work);
592 queue_work(bnx2fc_wq, &unsol_els->unsol_els_work);
593 } else {
594 BNX2FC_HBA_DBG(lport, "fh_r_ctl = 0x%x\n", fh->fh_r_ctl);
595 kfree_skb(skb);
Julia Lawall5c2dce22011-04-01 16:23:46 +0200596 kfree(unsol_els);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800597 }
598}
599
600static void bnx2fc_process_unsol_compl(struct bnx2fc_rport *tgt, u16 wqe)
601{
602 u8 num_rq;
603 struct fcoe_err_report_entry *err_entry;
604 unsigned char *rq_data;
605 unsigned char *buf = NULL, *buf1;
606 int i;
607 u16 xid;
608 u32 frame_len, len;
609 struct bnx2fc_cmd *io_req = NULL;
610 struct fcoe_task_ctx_entry *task, *task_page;
611 struct bnx2fc_hba *hba = tgt->port->priv;
612 int task_idx, index;
613 int rc = 0;
614
615
616 BNX2FC_TGT_DBG(tgt, "Entered UNSOL COMPLETION wqe = 0x%x\n", wqe);
617 switch (wqe & FCOE_UNSOLICITED_CQE_SUBTYPE) {
618 case FCOE_UNSOLICITED_FRAME_CQE_TYPE:
619 frame_len = (wqe & FCOE_UNSOLICITED_CQE_PKT_LEN) >>
620 FCOE_UNSOLICITED_CQE_PKT_LEN_SHIFT;
621
622 num_rq = (frame_len + BNX2FC_RQ_BUF_SZ - 1) / BNX2FC_RQ_BUF_SZ;
623
Nithin Sujir68695972011-03-17 17:13:31 -0700624 spin_lock_bh(&tgt->tgt_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800625 rq_data = (unsigned char *)bnx2fc_get_next_rqe(tgt, num_rq);
Nithin Sujir68695972011-03-17 17:13:31 -0700626 spin_unlock_bh(&tgt->tgt_lock);
627
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800628 if (rq_data) {
629 buf = rq_data;
630 } else {
631 buf1 = buf = kmalloc((num_rq * BNX2FC_RQ_BUF_SZ),
632 GFP_ATOMIC);
633
634 if (!buf1) {
635 BNX2FC_TGT_DBG(tgt, "Memory alloc failure\n");
636 break;
637 }
638
639 for (i = 0; i < num_rq; i++) {
Nithin Sujir68695972011-03-17 17:13:31 -0700640 spin_lock_bh(&tgt->tgt_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800641 rq_data = (unsigned char *)
642 bnx2fc_get_next_rqe(tgt, 1);
Nithin Sujir68695972011-03-17 17:13:31 -0700643 spin_unlock_bh(&tgt->tgt_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800644 len = BNX2FC_RQ_BUF_SZ;
645 memcpy(buf1, rq_data, len);
646 buf1 += len;
647 }
648 }
649 bnx2fc_process_l2_frame_compl(tgt, buf, frame_len,
650 FC_XID_UNKNOWN);
651
652 if (buf != rq_data)
653 kfree(buf);
Nithin Sujir68695972011-03-17 17:13:31 -0700654 spin_lock_bh(&tgt->tgt_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800655 bnx2fc_return_rqe(tgt, num_rq);
Nithin Sujir68695972011-03-17 17:13:31 -0700656 spin_unlock_bh(&tgt->tgt_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800657 break;
658
659 case FCOE_ERROR_DETECTION_CQE_TYPE:
660 /*
Nithin Sujir68695972011-03-17 17:13:31 -0700661 * In case of error reporting CQE a single RQ entry
662 * is consumed.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800663 */
664 spin_lock_bh(&tgt->tgt_lock);
665 num_rq = 1;
666 err_entry = (struct fcoe_err_report_entry *)
667 bnx2fc_get_next_rqe(tgt, 1);
668 xid = err_entry->fc_hdr.ox_id;
669 BNX2FC_TGT_DBG(tgt, "Unsol Error Frame OX_ID = 0x%x\n", xid);
670 BNX2FC_TGT_DBG(tgt, "err_warn_bitmap = %08x:%08x\n",
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300671 err_entry->data.err_warn_bitmap_hi,
672 err_entry->data.err_warn_bitmap_lo);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800673 BNX2FC_TGT_DBG(tgt, "buf_offsets - tx = 0x%x, rx = 0x%x\n",
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300674 err_entry->data.tx_buf_off, err_entry->data.rx_buf_off);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800675
676 bnx2fc_return_rqe(tgt, 1);
677
678 if (xid > BNX2FC_MAX_XID) {
679 BNX2FC_TGT_DBG(tgt, "xid(0x%x) out of FW range\n",
680 xid);
681 spin_unlock_bh(&tgt->tgt_lock);
682 break;
683 }
684
685 task_idx = xid / BNX2FC_TASKS_PER_PAGE;
686 index = xid % BNX2FC_TASKS_PER_PAGE;
687 task_page = (struct fcoe_task_ctx_entry *)
688 hba->task_ctx[task_idx];
689 task = &(task_page[index]);
690
691 io_req = (struct bnx2fc_cmd *)hba->cmd_mgr->cmds[xid];
692 if (!io_req) {
693 spin_unlock_bh(&tgt->tgt_lock);
694 break;
695 }
696
697 if (io_req->cmd_type != BNX2FC_SCSI_CMD) {
698 printk(KERN_ERR PFX "err_warn: Not a SCSI cmd\n");
699 spin_unlock_bh(&tgt->tgt_lock);
700 break;
701 }
702
703 if (test_and_clear_bit(BNX2FC_FLAG_IO_CLEANUP,
704 &io_req->req_flags)) {
705 BNX2FC_IO_DBG(io_req, "unsol_err: cleanup in "
706 "progress.. ignore unsol err\n");
707 spin_unlock_bh(&tgt->tgt_lock);
708 break;
709 }
710
711 /*
712 * If ABTS is already in progress, and FW error is
713 * received after that, do not cancel the timeout_work
714 * and let the error recovery continue by explicitly
715 * logging out the target, when the ABTS eventually
716 * times out.
717 */
718 if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
719 &io_req->req_flags)) {
720 /*
721 * Cancel the timeout_work, as we received IO
722 * completion with FW error.
723 */
724 if (cancel_delayed_work(&io_req->timeout_work))
725 kref_put(&io_req->refcount,
726 bnx2fc_cmd_release); /* timer hold */
727
728 rc = bnx2fc_initiate_abts(io_req);
729 if (rc != SUCCESS) {
730 BNX2FC_IO_DBG(io_req, "err_warn: initiate_abts "
731 "failed. issue cleanup\n");
732 rc = bnx2fc_initiate_cleanup(io_req);
733 BUG_ON(rc);
734 }
735 } else
736 printk(KERN_ERR PFX "err_warn: io_req (0x%x) already "
737 "in ABTS processing\n", xid);
738 spin_unlock_bh(&tgt->tgt_lock);
739 break;
740
741 case FCOE_WARNING_DETECTION_CQE_TYPE:
742 /*
743 *In case of warning reporting CQE a single RQ entry
744 * is consumes.
745 */
Nithin Sujir68695972011-03-17 17:13:31 -0700746 spin_lock_bh(&tgt->tgt_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800747 num_rq = 1;
748 err_entry = (struct fcoe_err_report_entry *)
749 bnx2fc_get_next_rqe(tgt, 1);
750 xid = cpu_to_be16(err_entry->fc_hdr.ox_id);
751 BNX2FC_TGT_DBG(tgt, "Unsol Warning Frame OX_ID = 0x%x\n", xid);
752 BNX2FC_TGT_DBG(tgt, "err_warn_bitmap = %08x:%08x",
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300753 err_entry->data.err_warn_bitmap_hi,
754 err_entry->data.err_warn_bitmap_lo);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800755 BNX2FC_TGT_DBG(tgt, "buf_offsets - tx = 0x%x, rx = 0x%x",
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300756 err_entry->data.tx_buf_off, err_entry->data.rx_buf_off);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800757
758 bnx2fc_return_rqe(tgt, 1);
Nithin Sujir68695972011-03-17 17:13:31 -0700759 spin_unlock_bh(&tgt->tgt_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800760 break;
761
762 default:
763 printk(KERN_ERR PFX "Unsol Compl: Invalid CQE Subtype\n");
764 break;
765 }
766}
767
768void bnx2fc_process_cq_compl(struct bnx2fc_rport *tgt, u16 wqe)
769{
770 struct fcoe_task_ctx_entry *task;
771 struct fcoe_task_ctx_entry *task_page;
772 struct fcoe_port *port = tgt->port;
773 struct bnx2fc_hba *hba = port->priv;
774 struct bnx2fc_cmd *io_req;
775 int task_idx, index;
776 u16 xid;
777 u8 cmd_type;
778 u8 rx_state = 0;
779 u8 num_rq;
780
781 spin_lock_bh(&tgt->tgt_lock);
782 xid = wqe & FCOE_PEND_WQ_CQE_TASK_ID;
783 if (xid >= BNX2FC_MAX_TASKS) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700784 printk(KERN_ERR PFX "ERROR:xid out of range\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800785 spin_unlock_bh(&tgt->tgt_lock);
786 return;
787 }
788 task_idx = xid / BNX2FC_TASKS_PER_PAGE;
789 index = xid % BNX2FC_TASKS_PER_PAGE;
790 task_page = (struct fcoe_task_ctx_entry *)hba->task_ctx[task_idx];
791 task = &(task_page[index]);
792
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300793 num_rq = ((task->rxwr_txrd.var_ctx.rx_flags &
794 FCOE_TCE_RX_WR_TX_RD_VAR_NUM_RQ_WQE) >>
795 FCOE_TCE_RX_WR_TX_RD_VAR_NUM_RQ_WQE_SHIFT);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800796
797 io_req = (struct bnx2fc_cmd *)hba->cmd_mgr->cmds[xid];
798
799 if (io_req == NULL) {
800 printk(KERN_ERR PFX "ERROR? cq_compl - io_req is NULL\n");
801 spin_unlock_bh(&tgt->tgt_lock);
802 return;
803 }
804
805 /* Timestamp IO completion time */
806 cmd_type = io_req->cmd_type;
807
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300808 rx_state = ((task->rxwr_txrd.var_ctx.rx_flags &
809 FCOE_TCE_RX_WR_TX_RD_VAR_RX_STATE) >>
810 FCOE_TCE_RX_WR_TX_RD_VAR_RX_STATE_SHIFT);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800811
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300812 /* Process other IO completion types */
813 switch (cmd_type) {
814 case BNX2FC_SCSI_CMD:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800815 if (rx_state == FCOE_TASK_RX_STATE_COMPLETED) {
816 bnx2fc_process_scsi_cmd_compl(io_req, task, num_rq);
817 spin_unlock_bh(&tgt->tgt_lock);
818 return;
819 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800820
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800821 if (rx_state == FCOE_TASK_RX_STATE_ABTS_COMPLETED)
822 bnx2fc_process_abts_compl(io_req, task, num_rq);
823 else if (rx_state ==
824 FCOE_TASK_RX_STATE_EXCHANGE_CLEANUP_COMPLETED)
825 bnx2fc_process_cleanup_compl(io_req, task, num_rq);
826 else
827 printk(KERN_ERR PFX "Invalid rx state - %d\n",
828 rx_state);
829 break;
830
831 case BNX2FC_TASK_MGMT_CMD:
832 BNX2FC_IO_DBG(io_req, "Processing TM complete\n");
833 bnx2fc_process_tm_compl(io_req, task, num_rq);
834 break;
835
836 case BNX2FC_ABTS:
837 /*
838 * ABTS request received by firmware. ABTS response
839 * will be delivered to the task belonging to the IO
840 * that was aborted
841 */
842 BNX2FC_IO_DBG(io_req, "cq_compl- ABTS sent out by fw\n");
843 kref_put(&io_req->refcount, bnx2fc_cmd_release);
844 break;
845
846 case BNX2FC_ELS:
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300847 if (rx_state == FCOE_TASK_RX_STATE_COMPLETED)
848 bnx2fc_process_els_compl(io_req, task, num_rq);
849 else if (rx_state == FCOE_TASK_RX_STATE_ABTS_COMPLETED)
850 bnx2fc_process_abts_compl(io_req, task, num_rq);
851 else if (rx_state ==
852 FCOE_TASK_RX_STATE_EXCHANGE_CLEANUP_COMPLETED)
853 bnx2fc_process_cleanup_compl(io_req, task, num_rq);
854 else
855 printk(KERN_ERR PFX "Invalid rx state = %d\n",
856 rx_state);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800857 break;
858
859 case BNX2FC_CLEANUP:
860 BNX2FC_IO_DBG(io_req, "cq_compl- cleanup resp rcvd\n");
861 kref_put(&io_req->refcount, bnx2fc_cmd_release);
862 break;
863
864 default:
865 printk(KERN_ERR PFX "Invalid cmd_type %d\n", cmd_type);
866 break;
867 }
868 spin_unlock_bh(&tgt->tgt_lock);
869}
870
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300871void bnx2fc_arm_cq(struct bnx2fc_rport *tgt)
872{
873 struct b577xx_fcoe_rx_doorbell *rx_db = &tgt->rx_db;
874 u32 msg;
875
876 wmb();
877 rx_db->doorbell_cq_cons = tgt->cq_cons_idx | (tgt->cq_curr_toggle_bit <<
878 FCOE_CQE_TOGGLE_BIT_SHIFT);
879 msg = *((u32 *)rx_db);
880 writel(cpu_to_le32(msg), tgt->ctx_base);
881 mmiowb();
882
883}
884
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800885struct bnx2fc_work *bnx2fc_alloc_work(struct bnx2fc_rport *tgt, u16 wqe)
886{
887 struct bnx2fc_work *work;
888 work = kzalloc(sizeof(struct bnx2fc_work), GFP_ATOMIC);
889 if (!work)
890 return NULL;
891
892 INIT_LIST_HEAD(&work->list);
893 work->tgt = tgt;
894 work->wqe = wqe;
895 return work;
896}
897
898int bnx2fc_process_new_cqes(struct bnx2fc_rport *tgt)
899{
900 struct fcoe_cqe *cq;
901 u32 cq_cons;
902 struct fcoe_cqe *cqe;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300903 u32 num_free_sqes = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800904 u16 wqe;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800905
906 /*
907 * cq_lock is a low contention lock used to protect
908 * the CQ data structure from being freed up during
909 * the upload operation
910 */
911 spin_lock_bh(&tgt->cq_lock);
912
913 if (!tgt->cq) {
914 printk(KERN_ERR PFX "process_new_cqes: cq is NULL\n");
915 spin_unlock_bh(&tgt->cq_lock);
916 return 0;
917 }
918 cq = tgt->cq;
919 cq_cons = tgt->cq_cons_idx;
920 cqe = &cq[cq_cons];
921
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300922 while (((wqe = cqe->wqe) & FCOE_CQE_TOGGLE_BIT) ==
923 (tgt->cq_curr_toggle_bit <<
924 FCOE_CQE_TOGGLE_BIT_SHIFT)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800925
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300926 /* new entry on the cq */
927 if (wqe & FCOE_CQE_CQE_TYPE) {
928 /* Unsolicited event notification */
929 bnx2fc_process_unsol_compl(tgt, wqe);
930 } else {
931 /* Pending work request completion */
932 struct bnx2fc_work *work = NULL;
933 struct bnx2fc_percpu_s *fps = NULL;
934 unsigned int cpu = wqe % num_possible_cpus();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800935
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300936 fps = &per_cpu(bnx2fc_percpu, cpu);
937 spin_lock_bh(&fps->fp_work_lock);
938 if (unlikely(!fps->iothread))
939 goto unlock;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800940
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300941 work = bnx2fc_alloc_work(tgt, wqe);
942 if (work)
943 list_add_tail(&work->list,
944 &fps->work_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800945unlock:
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300946 spin_unlock_bh(&fps->fp_work_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800947
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300948 /* Pending work request completion */
949 if (fps->iothread && work)
950 wake_up_process(fps->iothread);
951 else
952 bnx2fc_process_cq_compl(tgt, wqe);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800953 }
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300954 cqe++;
955 tgt->cq_cons_idx++;
956 num_free_sqes++;
957
958 if (tgt->cq_cons_idx == BNX2FC_CQ_WQES_MAX) {
959 tgt->cq_cons_idx = 0;
960 cqe = cq;
961 tgt->cq_curr_toggle_bit =
962 1 - tgt->cq_curr_toggle_bit;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800963 }
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300964 }
965 bnx2fc_arm_cq(tgt);
966 atomic_add(num_free_sqes, &tgt->free_sqes);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800967 spin_unlock_bh(&tgt->cq_lock);
968 return 0;
969}
970
971/**
972 * bnx2fc_fastpath_notification - process global event queue (KCQ)
973 *
974 * @hba: adapter structure pointer
975 * @new_cqe_kcqe: pointer to newly DMA'd KCQ entry
976 *
977 * Fast path event notification handler
978 */
979static void bnx2fc_fastpath_notification(struct bnx2fc_hba *hba,
980 struct fcoe_kcqe *new_cqe_kcqe)
981{
982 u32 conn_id = new_cqe_kcqe->fcoe_conn_id;
983 struct bnx2fc_rport *tgt = hba->tgt_ofld_list[conn_id];
984
985 if (!tgt) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700986 printk(KERN_ERR PFX "conn_id 0x%x not valid\n", conn_id);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800987 return;
988 }
989
990 bnx2fc_process_new_cqes(tgt);
991}
992
993/**
994 * bnx2fc_process_ofld_cmpl - process FCoE session offload completion
995 *
996 * @hba: adapter structure pointer
997 * @ofld_kcqe: connection offload kcqe pointer
998 *
999 * handle session offload completion, enable the session if offload is
1000 * successful.
1001 */
1002static void bnx2fc_process_ofld_cmpl(struct bnx2fc_hba *hba,
1003 struct fcoe_kcqe *ofld_kcqe)
1004{
1005 struct bnx2fc_rport *tgt;
1006 struct fcoe_port *port;
1007 u32 conn_id;
1008 u32 context_id;
1009 int rc;
1010
1011 conn_id = ofld_kcqe->fcoe_conn_id;
1012 context_id = ofld_kcqe->fcoe_conn_context_id;
1013 tgt = hba->tgt_ofld_list[conn_id];
1014 if (!tgt) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001015 printk(KERN_ERR PFX "ERROR:ofld_cmpl: No pending ofld req\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001016 return;
1017 }
1018 BNX2FC_TGT_DBG(tgt, "Entered ofld compl - context_id = 0x%x\n",
1019 ofld_kcqe->fcoe_conn_context_id);
1020 port = tgt->port;
1021 if (hba != tgt->port->priv) {
1022 printk(KERN_ALERT PFX "ERROR:ofld_cmpl: HBA mis-match\n");
1023 goto ofld_cmpl_err;
1024 }
1025 /*
1026 * cnic has allocated a context_id for this session; use this
1027 * while enabling the session.
1028 */
1029 tgt->context_id = context_id;
1030 if (ofld_kcqe->completion_status) {
1031 if (ofld_kcqe->completion_status ==
1032 FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE) {
1033 printk(KERN_ERR PFX "unable to allocate FCoE context "
1034 "resources\n");
1035 set_bit(BNX2FC_FLAG_CTX_ALLOC_FAILURE, &tgt->flags);
1036 }
1037 goto ofld_cmpl_err;
1038 } else {
1039
1040 /* now enable the session */
1041 rc = bnx2fc_send_session_enable_req(port, tgt);
1042 if (rc) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001043 printk(KERN_ERR PFX "enable session failed\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001044 goto ofld_cmpl_err;
1045 }
1046 }
1047 return;
1048ofld_cmpl_err:
1049 set_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
1050 wake_up_interruptible(&tgt->ofld_wait);
1051}
1052
1053/**
1054 * bnx2fc_process_enable_conn_cmpl - process FCoE session enable completion
1055 *
1056 * @hba: adapter structure pointer
1057 * @ofld_kcqe: connection offload kcqe pointer
1058 *
1059 * handle session enable completion, mark the rport as ready
1060 */
1061
1062static void bnx2fc_process_enable_conn_cmpl(struct bnx2fc_hba *hba,
1063 struct fcoe_kcqe *ofld_kcqe)
1064{
1065 struct bnx2fc_rport *tgt;
1066 u32 conn_id;
1067 u32 context_id;
1068
1069 context_id = ofld_kcqe->fcoe_conn_context_id;
1070 conn_id = ofld_kcqe->fcoe_conn_id;
1071 tgt = hba->tgt_ofld_list[conn_id];
1072 if (!tgt) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001073 printk(KERN_ERR PFX "ERROR:enbl_cmpl: No pending ofld req\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001074 return;
1075 }
1076
1077 BNX2FC_TGT_DBG(tgt, "Enable compl - context_id = 0x%x\n",
1078 ofld_kcqe->fcoe_conn_context_id);
1079
1080 /*
1081 * context_id should be the same for this target during offload
1082 * and enable
1083 */
1084 if (tgt->context_id != context_id) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001085 printk(KERN_ERR PFX "context id mis-match\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001086 return;
1087 }
1088 if (hba != tgt->port->priv) {
1089 printk(KERN_ALERT PFX "bnx2fc-enbl_cmpl: HBA mis-match\n");
1090 goto enbl_cmpl_err;
1091 }
1092 if (ofld_kcqe->completion_status) {
1093 goto enbl_cmpl_err;
1094 } else {
1095 /* enable successful - rport ready for issuing IOs */
1096 set_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags);
1097 set_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
1098 wake_up_interruptible(&tgt->ofld_wait);
1099 }
1100 return;
1101
1102enbl_cmpl_err:
1103 set_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
1104 wake_up_interruptible(&tgt->ofld_wait);
1105}
1106
1107static void bnx2fc_process_conn_disable_cmpl(struct bnx2fc_hba *hba,
1108 struct fcoe_kcqe *disable_kcqe)
1109{
1110
1111 struct bnx2fc_rport *tgt;
1112 u32 conn_id;
1113
1114 conn_id = disable_kcqe->fcoe_conn_id;
1115 tgt = hba->tgt_ofld_list[conn_id];
1116 if (!tgt) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001117 printk(KERN_ERR PFX "ERROR: disable_cmpl: No disable req\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001118 return;
1119 }
1120
1121 BNX2FC_TGT_DBG(tgt, PFX "disable_cmpl: conn_id %d\n", conn_id);
1122
1123 if (disable_kcqe->completion_status) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001124 printk(KERN_ERR PFX "Disable failed with cmpl status %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001125 disable_kcqe->completion_status);
1126 return;
1127 } else {
1128 /* disable successful */
1129 BNX2FC_TGT_DBG(tgt, "disable successful\n");
1130 clear_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags);
1131 set_bit(BNX2FC_FLAG_DISABLED, &tgt->flags);
1132 set_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
1133 wake_up_interruptible(&tgt->upld_wait);
1134 }
1135}
1136
1137static void bnx2fc_process_conn_destroy_cmpl(struct bnx2fc_hba *hba,
1138 struct fcoe_kcqe *destroy_kcqe)
1139{
1140 struct bnx2fc_rport *tgt;
1141 u32 conn_id;
1142
1143 conn_id = destroy_kcqe->fcoe_conn_id;
1144 tgt = hba->tgt_ofld_list[conn_id];
1145 if (!tgt) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001146 printk(KERN_ERR PFX "destroy_cmpl: No destroy req\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001147 return;
1148 }
1149
1150 BNX2FC_TGT_DBG(tgt, "destroy_cmpl: conn_id %d\n", conn_id);
1151
1152 if (destroy_kcqe->completion_status) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001153 printk(KERN_ERR PFX "Destroy conn failed, cmpl status %d\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001154 destroy_kcqe->completion_status);
1155 return;
1156 } else {
1157 /* destroy successful */
1158 BNX2FC_TGT_DBG(tgt, "upload successful\n");
1159 clear_bit(BNX2FC_FLAG_DISABLED, &tgt->flags);
1160 set_bit(BNX2FC_FLAG_DESTROYED, &tgt->flags);
1161 set_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
1162 wake_up_interruptible(&tgt->upld_wait);
1163 }
1164}
1165
1166static void bnx2fc_init_failure(struct bnx2fc_hba *hba, u32 err_code)
1167{
1168 switch (err_code) {
1169 case FCOE_KCQE_COMPLETION_STATUS_INVALID_OPCODE:
1170 printk(KERN_ERR PFX "init_failure due to invalid opcode\n");
1171 break;
1172
1173 case FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE:
1174 printk(KERN_ERR PFX "init failed due to ctx alloc failure\n");
1175 break;
1176
1177 case FCOE_KCQE_COMPLETION_STATUS_NIC_ERROR:
1178 printk(KERN_ERR PFX "init_failure due to NIC error\n");
1179 break;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001180 case FCOE_KCQE_COMPLETION_STATUS_ERROR:
1181 printk(KERN_ERR PFX "init failure due to compl status err\n");
1182 break;
1183 case FCOE_KCQE_COMPLETION_STATUS_WRONG_HSI_VERSION:
1184 printk(KERN_ERR PFX "init failure due to HSI mismatch\n");
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001185 break;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001186 default:
1187 printk(KERN_ERR PFX "Unknown Error code %d\n", err_code);
1188 }
1189}
1190
1191/**
1192 * bnx2fc_indicae_kcqe - process KCQE
1193 *
1194 * @hba: adapter structure pointer
1195 * @kcqe: kcqe pointer
1196 * @num_cqe: Number of completion queue elements
1197 *
1198 * Generic KCQ event handler
1199 */
1200void bnx2fc_indicate_kcqe(void *context, struct kcqe *kcq[],
1201 u32 num_cqe)
1202{
1203 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
1204 int i = 0;
1205 struct fcoe_kcqe *kcqe = NULL;
1206
1207 while (i < num_cqe) {
1208 kcqe = (struct fcoe_kcqe *) kcq[i++];
1209
1210 switch (kcqe->op_code) {
1211 case FCOE_KCQE_OPCODE_CQ_EVENT_NOTIFICATION:
1212 bnx2fc_fastpath_notification(hba, kcqe);
1213 break;
1214
1215 case FCOE_KCQE_OPCODE_OFFLOAD_CONN:
1216 bnx2fc_process_ofld_cmpl(hba, kcqe);
1217 break;
1218
1219 case FCOE_KCQE_OPCODE_ENABLE_CONN:
1220 bnx2fc_process_enable_conn_cmpl(hba, kcqe);
1221 break;
1222
1223 case FCOE_KCQE_OPCODE_INIT_FUNC:
1224 if (kcqe->completion_status !=
1225 FCOE_KCQE_COMPLETION_STATUS_SUCCESS) {
1226 bnx2fc_init_failure(hba,
1227 kcqe->completion_status);
1228 } else {
1229 set_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1230 bnx2fc_get_link_state(hba);
1231 printk(KERN_INFO PFX "[%.2x]: FCOE_INIT passed\n",
1232 (u8)hba->pcidev->bus->number);
1233 }
1234 break;
1235
1236 case FCOE_KCQE_OPCODE_DESTROY_FUNC:
1237 if (kcqe->completion_status !=
1238 FCOE_KCQE_COMPLETION_STATUS_SUCCESS) {
1239
1240 printk(KERN_ERR PFX "DESTROY failed\n");
1241 } else {
1242 printk(KERN_ERR PFX "DESTROY success\n");
1243 }
1244 hba->flags |= BNX2FC_FLAG_DESTROY_CMPL;
1245 wake_up_interruptible(&hba->destroy_wait);
1246 break;
1247
1248 case FCOE_KCQE_OPCODE_DISABLE_CONN:
1249 bnx2fc_process_conn_disable_cmpl(hba, kcqe);
1250 break;
1251
1252 case FCOE_KCQE_OPCODE_DESTROY_CONN:
1253 bnx2fc_process_conn_destroy_cmpl(hba, kcqe);
1254 break;
1255
1256 case FCOE_KCQE_OPCODE_STAT_FUNC:
1257 if (kcqe->completion_status !=
1258 FCOE_KCQE_COMPLETION_STATUS_SUCCESS)
1259 printk(KERN_ERR PFX "STAT failed\n");
1260 complete(&hba->stat_req_done);
1261 break;
1262
1263 case FCOE_KCQE_OPCODE_FCOE_ERROR:
1264 /* fall thru */
1265 default:
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001266 printk(KERN_ERR PFX "unknown opcode 0x%x\n",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001267 kcqe->op_code);
1268 }
1269 }
1270}
1271
1272void bnx2fc_add_2_sq(struct bnx2fc_rport *tgt, u16 xid)
1273{
1274 struct fcoe_sqe *sqe;
1275
1276 sqe = &tgt->sq[tgt->sq_prod_idx];
1277
1278 /* Fill SQ WQE */
1279 sqe->wqe = xid << FCOE_SQE_TASK_ID_SHIFT;
1280 sqe->wqe |= tgt->sq_curr_toggle_bit << FCOE_SQE_TOGGLE_BIT_SHIFT;
1281
1282 /* Advance SQ Prod Idx */
1283 if (++tgt->sq_prod_idx == BNX2FC_SQ_WQES_MAX) {
1284 tgt->sq_prod_idx = 0;
1285 tgt->sq_curr_toggle_bit = 1 - tgt->sq_curr_toggle_bit;
1286 }
1287}
1288
1289void bnx2fc_ring_doorbell(struct bnx2fc_rport *tgt)
1290{
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001291 struct b577xx_doorbell_set_prod *sq_db = &tgt->sq_db;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001292 u32 msg;
1293
1294 wmb();
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001295 sq_db->prod = tgt->sq_prod_idx |
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001296 (tgt->sq_curr_toggle_bit << 15);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001297 msg = *((u32 *)sq_db);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001298 writel(cpu_to_le32(msg), tgt->ctx_base);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001299 mmiowb();
1300
1301}
1302
1303int bnx2fc_map_doorbell(struct bnx2fc_rport *tgt)
1304{
1305 u32 context_id = tgt->context_id;
1306 struct fcoe_port *port = tgt->port;
1307 u32 reg_off;
1308 resource_size_t reg_base;
1309 struct bnx2fc_hba *hba = port->priv;
1310
1311 reg_base = pci_resource_start(hba->pcidev,
1312 BNX2X_DOORBELL_PCI_BAR);
1313 reg_off = BNX2FC_5771X_DB_PAGE_SIZE *
1314 (context_id & 0x1FFFF) + DPM_TRIGER_TYPE;
1315 tgt->ctx_base = ioremap_nocache(reg_base + reg_off, 4);
1316 if (!tgt->ctx_base)
1317 return -ENOMEM;
1318 return 0;
1319}
1320
1321char *bnx2fc_get_next_rqe(struct bnx2fc_rport *tgt, u8 num_items)
1322{
1323 char *buf = (char *)tgt->rq + (tgt->rq_cons_idx * BNX2FC_RQ_BUF_SZ);
1324
1325 if (tgt->rq_cons_idx + num_items > BNX2FC_RQ_WQES_MAX)
1326 return NULL;
1327
1328 tgt->rq_cons_idx += num_items;
1329
1330 if (tgt->rq_cons_idx >= BNX2FC_RQ_WQES_MAX)
1331 tgt->rq_cons_idx -= BNX2FC_RQ_WQES_MAX;
1332
1333 return buf;
1334}
1335
1336void bnx2fc_return_rqe(struct bnx2fc_rport *tgt, u8 num_items)
1337{
1338 /* return the rq buffer */
1339 u32 next_prod_idx = tgt->rq_prod_idx + num_items;
1340 if ((next_prod_idx & 0x7fff) == BNX2FC_RQ_WQES_MAX) {
1341 /* Wrap around RQ */
1342 next_prod_idx += 0x8000 - BNX2FC_RQ_WQES_MAX;
1343 }
1344 tgt->rq_prod_idx = next_prod_idx;
1345 tgt->conn_db->rq_prod = tgt->rq_prod_idx;
1346}
1347
1348void bnx2fc_init_cleanup_task(struct bnx2fc_cmd *io_req,
1349 struct fcoe_task_ctx_entry *task,
1350 u16 orig_xid)
1351{
1352 u8 task_type = FCOE_TASK_TYPE_EXCHANGE_CLEANUP;
1353 struct bnx2fc_rport *tgt = io_req->tgt;
1354 u32 context_id = tgt->context_id;
1355
1356 memset(task, 0, sizeof(struct fcoe_task_ctx_entry));
1357
1358 /* Tx Write Rx Read */
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001359 /* init flags */
1360 task->txwr_rxrd.const_ctx.init_flags = task_type <<
1361 FCOE_TCE_TX_WR_RX_RD_CONST_TASK_TYPE_SHIFT;
1362 task->txwr_rxrd.const_ctx.init_flags |= FCOE_TASK_CLASS_TYPE_3 <<
1363 FCOE_TCE_TX_WR_RX_RD_CONST_CLASS_TYPE_SHIFT;
1364 task->txwr_rxrd.const_ctx.init_flags |=
1365 FCOE_TASK_DEV_TYPE_DISK <<
1366 FCOE_TCE_TX_WR_RX_RD_CONST_DEV_TYPE_SHIFT;
1367 task->txwr_rxrd.union_ctx.cleanup.ctx.cleaned_task_id = orig_xid;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001368
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001369 /* Tx flags */
1370 task->txwr_rxrd.const_ctx.tx_flags =
1371 FCOE_TASK_TX_STATE_EXCHANGE_CLEANUP <<
1372 FCOE_TCE_TX_WR_RX_RD_CONST_TX_STATE_SHIFT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001373
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001374 /* Rx Read Tx Write */
1375 task->rxwr_txrd.const_ctx.init_flags = context_id <<
1376 FCOE_TCE_RX_WR_TX_RD_CONST_CID_SHIFT;
1377 task->rxwr_txrd.var_ctx.rx_flags |= 1 <<
1378 FCOE_TCE_RX_WR_TX_RD_VAR_EXP_FIRST_FRAME_SHIFT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001379}
1380
1381void bnx2fc_init_mp_task(struct bnx2fc_cmd *io_req,
1382 struct fcoe_task_ctx_entry *task)
1383{
1384 struct bnx2fc_mp_req *mp_req = &(io_req->mp_req);
1385 struct bnx2fc_rport *tgt = io_req->tgt;
1386 struct fc_frame_header *fc_hdr;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001387 struct fcoe_ext_mul_sges_ctx *sgl;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001388 u8 task_type = 0;
1389 u64 *hdr;
1390 u64 temp_hdr[3];
1391 u32 context_id;
1392
1393
1394 /* Obtain task_type */
1395 if ((io_req->cmd_type == BNX2FC_TASK_MGMT_CMD) ||
1396 (io_req->cmd_type == BNX2FC_ELS)) {
1397 task_type = FCOE_TASK_TYPE_MIDPATH;
1398 } else if (io_req->cmd_type == BNX2FC_ABTS) {
1399 task_type = FCOE_TASK_TYPE_ABTS;
1400 }
1401
1402 memset(task, 0, sizeof(struct fcoe_task_ctx_entry));
1403
1404 /* Setup the task from io_req for easy reference */
1405 io_req->task = task;
1406
1407 BNX2FC_IO_DBG(io_req, "Init MP task for cmd_type = %d task_type = %d\n",
1408 io_req->cmd_type, task_type);
1409
1410 /* Tx only */
1411 if ((task_type == FCOE_TASK_TYPE_MIDPATH) ||
1412 (task_type == FCOE_TASK_TYPE_UNSOLICITED)) {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001413 task->txwr_only.sgl_ctx.sgl.mul_sgl.cur_sge_addr.lo =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001414 (u32)mp_req->mp_req_bd_dma;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001415 task->txwr_only.sgl_ctx.sgl.mul_sgl.cur_sge_addr.hi =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001416 (u32)((u64)mp_req->mp_req_bd_dma >> 32);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001417 task->txwr_only.sgl_ctx.sgl.mul_sgl.sgl_size = 1;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001418 }
1419
1420 /* Tx Write Rx Read */
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001421 /* init flags */
1422 task->txwr_rxrd.const_ctx.init_flags = task_type <<
1423 FCOE_TCE_TX_WR_RX_RD_CONST_TASK_TYPE_SHIFT;
1424 task->txwr_rxrd.const_ctx.init_flags |=
1425 FCOE_TASK_DEV_TYPE_DISK <<
1426 FCOE_TCE_TX_WR_RX_RD_CONST_DEV_TYPE_SHIFT;
1427 task->txwr_rxrd.const_ctx.init_flags |= FCOE_TASK_CLASS_TYPE_3 <<
1428 FCOE_TCE_TX_WR_RX_RD_CONST_CLASS_TYPE_SHIFT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001429
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001430 /* tx flags */
1431 task->txwr_rxrd.const_ctx.tx_flags = FCOE_TASK_TX_STATE_INIT <<
1432 FCOE_TCE_TX_WR_RX_RD_CONST_TX_STATE_SHIFT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001433
1434 /* Rx Write Tx Read */
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001435 task->rxwr_txrd.const_ctx.data_2_trns = io_req->data_xfer_len;
1436
1437 /* rx flags */
1438 task->rxwr_txrd.var_ctx.rx_flags |= 1 <<
1439 FCOE_TCE_RX_WR_TX_RD_VAR_EXP_FIRST_FRAME_SHIFT;
1440
1441 context_id = tgt->context_id;
1442 task->rxwr_txrd.const_ctx.init_flags = context_id <<
1443 FCOE_TCE_RX_WR_TX_RD_CONST_CID_SHIFT;
1444
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001445 fc_hdr = &(mp_req->req_fc_hdr);
1446 if (task_type == FCOE_TASK_TYPE_MIDPATH) {
1447 fc_hdr->fh_ox_id = cpu_to_be16(io_req->xid);
1448 fc_hdr->fh_rx_id = htons(0xffff);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001449 task->rxwr_txrd.var_ctx.rx_id = 0xffff;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001450 } else if (task_type == FCOE_TASK_TYPE_UNSOLICITED) {
1451 fc_hdr->fh_rx_id = cpu_to_be16(io_req->xid);
1452 }
1453
1454 /* Fill FC Header into middle path buffer */
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001455 hdr = (u64 *) &task->txwr_rxrd.union_ctx.tx_frame.fc_hdr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001456 memcpy(temp_hdr, fc_hdr, sizeof(temp_hdr));
1457 hdr[0] = cpu_to_be64(temp_hdr[0]);
1458 hdr[1] = cpu_to_be64(temp_hdr[1]);
1459 hdr[2] = cpu_to_be64(temp_hdr[2]);
1460
1461 /* Rx Only */
1462 if (task_type == FCOE_TASK_TYPE_MIDPATH) {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001463 sgl = &task->rxwr_only.union_ctx.read_info.sgl_ctx.sgl;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001464
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001465 sgl->mul_sgl.cur_sge_addr.lo = (u32)mp_req->mp_resp_bd_dma;
1466 sgl->mul_sgl.cur_sge_addr.hi =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001467 (u32)((u64)mp_req->mp_resp_bd_dma >> 32);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001468 sgl->mul_sgl.sgl_size = 1;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001469 }
1470}
1471
1472void bnx2fc_init_task(struct bnx2fc_cmd *io_req,
1473 struct fcoe_task_ctx_entry *task)
1474{
1475 u8 task_type;
1476 struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1477 struct io_bdt *bd_tbl = io_req->bd_tbl;
1478 struct bnx2fc_rport *tgt = io_req->tgt;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001479 struct fcoe_cached_sge_ctx *cached_sge;
1480 struct fcoe_ext_mul_sges_ctx *sgl;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001481 u64 *fcp_cmnd;
1482 u64 tmp_fcp_cmnd[4];
1483 u32 context_id;
1484 int cnt, i;
1485 int bd_count;
1486
1487 memset(task, 0, sizeof(struct fcoe_task_ctx_entry));
1488
1489 /* Setup the task from io_req for easy reference */
1490 io_req->task = task;
1491
1492 if (sc_cmd->sc_data_direction == DMA_TO_DEVICE)
1493 task_type = FCOE_TASK_TYPE_WRITE;
1494 else
1495 task_type = FCOE_TASK_TYPE_READ;
1496
1497 /* Tx only */
1498 if (task_type == FCOE_TASK_TYPE_WRITE) {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001499 task->txwr_only.sgl_ctx.sgl.mul_sgl.cur_sge_addr.lo =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001500 (u32)bd_tbl->bd_tbl_dma;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001501 task->txwr_only.sgl_ctx.sgl.mul_sgl.cur_sge_addr.hi =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001502 (u32)((u64)bd_tbl->bd_tbl_dma >> 32);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001503 task->txwr_only.sgl_ctx.sgl.mul_sgl.sgl_size =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001504 bd_tbl->bd_valid;
1505 }
1506
1507 /*Tx Write Rx Read */
1508 /* Init state to NORMAL */
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001509 task->txwr_rxrd.const_ctx.init_flags = task_type <<
1510 FCOE_TCE_TX_WR_RX_RD_CONST_TASK_TYPE_SHIFT;
1511 task->txwr_rxrd.const_ctx.init_flags |=
1512 FCOE_TASK_DEV_TYPE_DISK <<
1513 FCOE_TCE_TX_WR_RX_RD_CONST_DEV_TYPE_SHIFT;
1514 task->txwr_rxrd.const_ctx.init_flags |= FCOE_TASK_CLASS_TYPE_3 <<
1515 FCOE_TCE_TX_WR_RX_RD_CONST_CLASS_TYPE_SHIFT;
1516 /* tx flags */
1517 task->txwr_rxrd.const_ctx.tx_flags = FCOE_TASK_TX_STATE_NORMAL <<
1518 FCOE_TCE_TX_WR_RX_RD_CONST_TX_STATE_SHIFT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001519
1520 /* Set initial seq counter */
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001521 task->txwr_rxrd.union_ctx.tx_seq.ctx.seq_cnt = 1;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001522
1523 /* Fill FCP_CMND IU */
1524 fcp_cmnd = (u64 *)
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001525 task->txwr_rxrd.union_ctx.fcp_cmd.opaque;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001526 bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)&tmp_fcp_cmnd);
1527
1528 /* swap fcp_cmnd */
1529 cnt = sizeof(struct fcp_cmnd) / sizeof(u64);
1530
1531 for (i = 0; i < cnt; i++) {
1532 *fcp_cmnd = cpu_to_be64(tmp_fcp_cmnd[i]);
1533 fcp_cmnd++;
1534 }
1535
1536 /* Rx Write Tx Read */
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001537 task->rxwr_txrd.const_ctx.data_2_trns = io_req->data_xfer_len;
1538
1539 context_id = tgt->context_id;
1540 task->rxwr_txrd.const_ctx.init_flags = context_id <<
1541 FCOE_TCE_RX_WR_TX_RD_CONST_CID_SHIFT;
1542
1543 /* rx flags */
1544 /* Set state to "waiting for the first packet" */
1545 task->rxwr_txrd.var_ctx.rx_flags |= 1 <<
1546 FCOE_TCE_RX_WR_TX_RD_VAR_EXP_FIRST_FRAME_SHIFT;
1547
1548 task->rxwr_txrd.var_ctx.rx_id = 0xffff;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001549
1550 /* Rx Only */
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001551 cached_sge = &task->rxwr_only.union_ctx.read_info.sgl_ctx.cached_sge;
1552 sgl = &task->rxwr_only.union_ctx.read_info.sgl_ctx.sgl;
1553 bd_count = bd_tbl->bd_valid;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001554 if (task_type == FCOE_TASK_TYPE_READ) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001555 if (bd_count == 1) {
1556
1557 struct fcoe_bd_ctx *fcoe_bd_tbl = bd_tbl->bd_tbl;
1558
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001559 cached_sge->cur_buf_addr.lo = fcoe_bd_tbl->buf_addr_lo;
1560 cached_sge->cur_buf_addr.hi = fcoe_bd_tbl->buf_addr_hi;
1561 cached_sge->cur_buf_rem = fcoe_bd_tbl->buf_len;
1562 task->txwr_rxrd.const_ctx.init_flags |= 1 <<
1563 FCOE_TCE_TX_WR_RX_RD_CONST_CACHED_SGE_SHIFT;
1564 } else if (bd_count == 2) {
1565 struct fcoe_bd_ctx *fcoe_bd_tbl = bd_tbl->bd_tbl;
1566
1567 cached_sge->cur_buf_addr.lo = fcoe_bd_tbl->buf_addr_lo;
1568 cached_sge->cur_buf_addr.hi = fcoe_bd_tbl->buf_addr_hi;
1569 cached_sge->cur_buf_rem = fcoe_bd_tbl->buf_len;
1570
1571 fcoe_bd_tbl++;
1572 cached_sge->second_buf_addr.lo =
1573 fcoe_bd_tbl->buf_addr_lo;
1574 cached_sge->second_buf_addr.hi =
1575 fcoe_bd_tbl->buf_addr_hi;
1576 cached_sge->second_buf_rem = fcoe_bd_tbl->buf_len;
1577 task->txwr_rxrd.const_ctx.init_flags |= 1 <<
1578 FCOE_TCE_TX_WR_RX_RD_CONST_CACHED_SGE_SHIFT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001579 } else {
1580
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001581 sgl->mul_sgl.cur_sge_addr.lo = (u32)bd_tbl->bd_tbl_dma;
1582 sgl->mul_sgl.cur_sge_addr.hi =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001583 (u32)((u64)bd_tbl->bd_tbl_dma >> 32);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001584 sgl->mul_sgl.sgl_size = bd_count;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001585 }
1586 }
1587}
1588
1589/**
1590 * bnx2fc_setup_task_ctx - allocate and map task context
1591 *
1592 * @hba: pointer to adapter structure
1593 *
1594 * allocate memory for task context, and associated BD table to be used
1595 * by firmware
1596 *
1597 */
1598int bnx2fc_setup_task_ctx(struct bnx2fc_hba *hba)
1599{
1600 int rc = 0;
1601 struct regpair *task_ctx_bdt;
1602 dma_addr_t addr;
1603 int i;
1604
1605 /*
1606 * Allocate task context bd table. A page size of bd table
1607 * can map 256 buffers. Each buffer contains 32 task context
1608 * entries. Hence the limit with one page is 8192 task context
1609 * entries.
1610 */
1611 hba->task_ctx_bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
1612 PAGE_SIZE,
1613 &hba->task_ctx_bd_dma,
1614 GFP_KERNEL);
1615 if (!hba->task_ctx_bd_tbl) {
1616 printk(KERN_ERR PFX "unable to allocate task context BDT\n");
1617 rc = -1;
1618 goto out;
1619 }
1620 memset(hba->task_ctx_bd_tbl, 0, PAGE_SIZE);
1621
1622 /*
1623 * Allocate task_ctx which is an array of pointers pointing to
1624 * a page containing 32 task contexts
1625 */
1626 hba->task_ctx = kzalloc((BNX2FC_TASK_CTX_ARR_SZ * sizeof(void *)),
1627 GFP_KERNEL);
1628 if (!hba->task_ctx) {
1629 printk(KERN_ERR PFX "unable to allocate task context array\n");
1630 rc = -1;
1631 goto out1;
1632 }
1633
1634 /*
1635 * Allocate task_ctx_dma which is an array of dma addresses
1636 */
1637 hba->task_ctx_dma = kmalloc((BNX2FC_TASK_CTX_ARR_SZ *
1638 sizeof(dma_addr_t)), GFP_KERNEL);
1639 if (!hba->task_ctx_dma) {
1640 printk(KERN_ERR PFX "unable to alloc context mapping array\n");
1641 rc = -1;
1642 goto out2;
1643 }
1644
1645 task_ctx_bdt = (struct regpair *)hba->task_ctx_bd_tbl;
1646 for (i = 0; i < BNX2FC_TASK_CTX_ARR_SZ; i++) {
1647
1648 hba->task_ctx[i] = dma_alloc_coherent(&hba->pcidev->dev,
1649 PAGE_SIZE,
1650 &hba->task_ctx_dma[i],
1651 GFP_KERNEL);
1652 if (!hba->task_ctx[i]) {
1653 printk(KERN_ERR PFX "unable to alloc task context\n");
1654 rc = -1;
1655 goto out3;
1656 }
1657 memset(hba->task_ctx[i], 0, PAGE_SIZE);
1658 addr = (u64)hba->task_ctx_dma[i];
1659 task_ctx_bdt->hi = cpu_to_le32((u64)addr >> 32);
1660 task_ctx_bdt->lo = cpu_to_le32((u32)addr);
1661 task_ctx_bdt++;
1662 }
1663 return 0;
1664
1665out3:
1666 for (i = 0; i < BNX2FC_TASK_CTX_ARR_SZ; i++) {
1667 if (hba->task_ctx[i]) {
1668
1669 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
1670 hba->task_ctx[i], hba->task_ctx_dma[i]);
1671 hba->task_ctx[i] = NULL;
1672 }
1673 }
1674
1675 kfree(hba->task_ctx_dma);
1676 hba->task_ctx_dma = NULL;
1677out2:
1678 kfree(hba->task_ctx);
1679 hba->task_ctx = NULL;
1680out1:
1681 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
1682 hba->task_ctx_bd_tbl, hba->task_ctx_bd_dma);
1683 hba->task_ctx_bd_tbl = NULL;
1684out:
1685 return rc;
1686}
1687
1688void bnx2fc_free_task_ctx(struct bnx2fc_hba *hba)
1689{
1690 int i;
1691
1692 if (hba->task_ctx_bd_tbl) {
1693 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
1694 hba->task_ctx_bd_tbl,
1695 hba->task_ctx_bd_dma);
1696 hba->task_ctx_bd_tbl = NULL;
1697 }
1698
1699 if (hba->task_ctx) {
1700 for (i = 0; i < BNX2FC_TASK_CTX_ARR_SZ; i++) {
1701 if (hba->task_ctx[i]) {
1702 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
1703 hba->task_ctx[i],
1704 hba->task_ctx_dma[i]);
1705 hba->task_ctx[i] = NULL;
1706 }
1707 }
1708 kfree(hba->task_ctx);
1709 hba->task_ctx = NULL;
1710 }
1711
1712 kfree(hba->task_ctx_dma);
1713 hba->task_ctx_dma = NULL;
1714}
1715
1716static void bnx2fc_free_hash_table(struct bnx2fc_hba *hba)
1717{
1718 int i;
1719 int segment_count;
1720 int hash_table_size;
1721 u32 *pbl;
1722
1723 segment_count = hba->hash_tbl_segment_count;
1724 hash_table_size = BNX2FC_NUM_MAX_SESS * BNX2FC_MAX_ROWS_IN_HASH_TBL *
1725 sizeof(struct fcoe_hash_table_entry);
1726
1727 pbl = hba->hash_tbl_pbl;
1728 for (i = 0; i < segment_count; ++i) {
1729 dma_addr_t dma_address;
1730
1731 dma_address = le32_to_cpu(*pbl);
1732 ++pbl;
1733 dma_address += ((u64)le32_to_cpu(*pbl)) << 32;
1734 ++pbl;
1735 dma_free_coherent(&hba->pcidev->dev,
1736 BNX2FC_HASH_TBL_CHUNK_SIZE,
1737 hba->hash_tbl_segments[i],
1738 dma_address);
1739
1740 }
1741
1742 if (hba->hash_tbl_pbl) {
1743 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
1744 hba->hash_tbl_pbl,
1745 hba->hash_tbl_pbl_dma);
1746 hba->hash_tbl_pbl = NULL;
1747 }
1748}
1749
1750static int bnx2fc_allocate_hash_table(struct bnx2fc_hba *hba)
1751{
1752 int i;
1753 int hash_table_size;
1754 int segment_count;
1755 int segment_array_size;
1756 int dma_segment_array_size;
1757 dma_addr_t *dma_segment_array;
1758 u32 *pbl;
1759
1760 hash_table_size = BNX2FC_NUM_MAX_SESS * BNX2FC_MAX_ROWS_IN_HASH_TBL *
1761 sizeof(struct fcoe_hash_table_entry);
1762
1763 segment_count = hash_table_size + BNX2FC_HASH_TBL_CHUNK_SIZE - 1;
1764 segment_count /= BNX2FC_HASH_TBL_CHUNK_SIZE;
1765 hba->hash_tbl_segment_count = segment_count;
1766
1767 segment_array_size = segment_count * sizeof(*hba->hash_tbl_segments);
1768 hba->hash_tbl_segments = kzalloc(segment_array_size, GFP_KERNEL);
1769 if (!hba->hash_tbl_segments) {
1770 printk(KERN_ERR PFX "hash table pointers alloc failed\n");
1771 return -ENOMEM;
1772 }
1773 dma_segment_array_size = segment_count * sizeof(*dma_segment_array);
1774 dma_segment_array = kzalloc(dma_segment_array_size, GFP_KERNEL);
1775 if (!dma_segment_array) {
1776 printk(KERN_ERR PFX "hash table pointers (dma) alloc failed\n");
1777 return -ENOMEM;
1778 }
1779
1780 for (i = 0; i < segment_count; ++i) {
1781 hba->hash_tbl_segments[i] =
1782 dma_alloc_coherent(&hba->pcidev->dev,
1783 BNX2FC_HASH_TBL_CHUNK_SIZE,
1784 &dma_segment_array[i],
1785 GFP_KERNEL);
1786 if (!hba->hash_tbl_segments[i]) {
1787 printk(KERN_ERR PFX "hash segment alloc failed\n");
1788 while (--i >= 0) {
1789 dma_free_coherent(&hba->pcidev->dev,
1790 BNX2FC_HASH_TBL_CHUNK_SIZE,
1791 hba->hash_tbl_segments[i],
1792 dma_segment_array[i]);
1793 hba->hash_tbl_segments[i] = NULL;
1794 }
1795 kfree(dma_segment_array);
1796 return -ENOMEM;
1797 }
1798 memset(hba->hash_tbl_segments[i], 0,
1799 BNX2FC_HASH_TBL_CHUNK_SIZE);
1800 }
1801
1802 hba->hash_tbl_pbl = dma_alloc_coherent(&hba->pcidev->dev,
1803 PAGE_SIZE,
1804 &hba->hash_tbl_pbl_dma,
1805 GFP_KERNEL);
1806 if (!hba->hash_tbl_pbl) {
1807 printk(KERN_ERR PFX "hash table pbl alloc failed\n");
1808 kfree(dma_segment_array);
1809 return -ENOMEM;
1810 }
1811 memset(hba->hash_tbl_pbl, 0, PAGE_SIZE);
1812
1813 pbl = hba->hash_tbl_pbl;
1814 for (i = 0; i < segment_count; ++i) {
1815 u64 paddr = dma_segment_array[i];
1816 *pbl = cpu_to_le32((u32) paddr);
1817 ++pbl;
1818 *pbl = cpu_to_le32((u32) (paddr >> 32));
1819 ++pbl;
1820 }
1821 pbl = hba->hash_tbl_pbl;
1822 i = 0;
1823 while (*pbl && *(pbl + 1)) {
1824 u32 lo;
1825 u32 hi;
1826 lo = *pbl;
1827 ++pbl;
1828 hi = *pbl;
1829 ++pbl;
1830 ++i;
1831 }
1832 kfree(dma_segment_array);
1833 return 0;
1834}
1835
1836/**
1837 * bnx2fc_setup_fw_resc - Allocate and map hash table and dummy buffer
1838 *
1839 * @hba: Pointer to adapter structure
1840 *
1841 */
1842int bnx2fc_setup_fw_resc(struct bnx2fc_hba *hba)
1843{
1844 u64 addr;
1845 u32 mem_size;
1846 int i;
1847
1848 if (bnx2fc_allocate_hash_table(hba))
1849 return -ENOMEM;
1850
1851 mem_size = BNX2FC_NUM_MAX_SESS * sizeof(struct regpair);
1852 hba->t2_hash_tbl_ptr = dma_alloc_coherent(&hba->pcidev->dev, mem_size,
1853 &hba->t2_hash_tbl_ptr_dma,
1854 GFP_KERNEL);
1855 if (!hba->t2_hash_tbl_ptr) {
1856 printk(KERN_ERR PFX "unable to allocate t2 hash table ptr\n");
1857 bnx2fc_free_fw_resc(hba);
1858 return -ENOMEM;
1859 }
1860 memset(hba->t2_hash_tbl_ptr, 0x00, mem_size);
1861
1862 mem_size = BNX2FC_NUM_MAX_SESS *
1863 sizeof(struct fcoe_t2_hash_table_entry);
1864 hba->t2_hash_tbl = dma_alloc_coherent(&hba->pcidev->dev, mem_size,
1865 &hba->t2_hash_tbl_dma,
1866 GFP_KERNEL);
1867 if (!hba->t2_hash_tbl) {
1868 printk(KERN_ERR PFX "unable to allocate t2 hash table\n");
1869 bnx2fc_free_fw_resc(hba);
1870 return -ENOMEM;
1871 }
1872 memset(hba->t2_hash_tbl, 0x00, mem_size);
1873 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
1874 addr = (unsigned long) hba->t2_hash_tbl_dma +
1875 ((i+1) * sizeof(struct fcoe_t2_hash_table_entry));
1876 hba->t2_hash_tbl[i].next.lo = addr & 0xffffffff;
1877 hba->t2_hash_tbl[i].next.hi = addr >> 32;
1878 }
1879
1880 hba->dummy_buffer = dma_alloc_coherent(&hba->pcidev->dev,
1881 PAGE_SIZE, &hba->dummy_buf_dma,
1882 GFP_KERNEL);
1883 if (!hba->dummy_buffer) {
1884 printk(KERN_ERR PFX "unable to alloc MP Dummy Buffer\n");
1885 bnx2fc_free_fw_resc(hba);
1886 return -ENOMEM;
1887 }
1888
1889 hba->stats_buffer = dma_alloc_coherent(&hba->pcidev->dev,
1890 PAGE_SIZE,
1891 &hba->stats_buf_dma,
1892 GFP_KERNEL);
1893 if (!hba->stats_buffer) {
1894 printk(KERN_ERR PFX "unable to alloc Stats Buffer\n");
1895 bnx2fc_free_fw_resc(hba);
1896 return -ENOMEM;
1897 }
1898 memset(hba->stats_buffer, 0x00, PAGE_SIZE);
1899
1900 return 0;
1901}
1902
1903void bnx2fc_free_fw_resc(struct bnx2fc_hba *hba)
1904{
1905 u32 mem_size;
1906
1907 if (hba->stats_buffer) {
1908 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
1909 hba->stats_buffer, hba->stats_buf_dma);
1910 hba->stats_buffer = NULL;
1911 }
1912
1913 if (hba->dummy_buffer) {
1914 dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
1915 hba->dummy_buffer, hba->dummy_buf_dma);
1916 hba->dummy_buffer = NULL;
1917 }
1918
1919 if (hba->t2_hash_tbl_ptr) {
1920 mem_size = BNX2FC_NUM_MAX_SESS * sizeof(struct regpair);
1921 dma_free_coherent(&hba->pcidev->dev, mem_size,
1922 hba->t2_hash_tbl_ptr,
1923 hba->t2_hash_tbl_ptr_dma);
1924 hba->t2_hash_tbl_ptr = NULL;
1925 }
1926
1927 if (hba->t2_hash_tbl) {
1928 mem_size = BNX2FC_NUM_MAX_SESS *
1929 sizeof(struct fcoe_t2_hash_table_entry);
1930 dma_free_coherent(&hba->pcidev->dev, mem_size,
1931 hba->t2_hash_tbl, hba->t2_hash_tbl_dma);
1932 hba->t2_hash_tbl = NULL;
1933 }
1934 bnx2fc_free_hash_table(hba);
1935}