]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/enic/enic_res.c
04cfc4e3f0630c186d1630965a43dbe6ac4894dc
[net-next-2.6.git] / drivers / net / enic / enic_res.c
1 /*
2  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * This program is free software; you may redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16  * SOFTWARE.
17  *
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/pci.h>
24 #include <linux/netdevice.h>
25
26 #include "wq_enet_desc.h"
27 #include "rq_enet_desc.h"
28 #include "cq_enet_desc.h"
29 #include "vnic_resource.h"
30 #include "vnic_enet.h"
31 #include "vnic_dev.h"
32 #include "vnic_wq.h"
33 #include "vnic_rq.h"
34 #include "vnic_cq.h"
35 #include "vnic_intr.h"
36 #include "vnic_stats.h"
37 #include "vnic_nic.h"
38 #include "vnic_rss.h"
39 #include "enic_res.h"
40 #include "enic.h"
41
42 int enic_get_vnic_config(struct enic *enic)
43 {
44         struct vnic_enet_config *c = &enic->config;
45         int err;
46
47         err = vnic_dev_mac_addr(enic->vdev, enic->mac_addr);
48         if (err) {
49                 printk(KERN_ERR PFX "Error getting MAC addr, %d\n", err);
50                 return err;
51         }
52
53 #define GET_CONFIG(m) \
54         do { \
55                 err = vnic_dev_spec(enic->vdev, \
56                         offsetof(struct vnic_enet_config, m), \
57                         sizeof(c->m), &c->m); \
58                 if (err) { \
59                         printk(KERN_ERR PFX \
60                                 "Error getting %s, %d\n", #m, err); \
61                         return err; \
62                 } \
63         } while (0)
64
65         GET_CONFIG(flags);
66         GET_CONFIG(wq_desc_count);
67         GET_CONFIG(rq_desc_count);
68         GET_CONFIG(mtu);
69         GET_CONFIG(intr_timer_type);
70         GET_CONFIG(intr_mode);
71         GET_CONFIG(intr_timer_usec);
72
73         c->wq_desc_count =
74                 min_t(u32, ENIC_MAX_WQ_DESCS,
75                 max_t(u32, ENIC_MIN_WQ_DESCS,
76                 c->wq_desc_count));
77         c->wq_desc_count &= 0xffffffe0; /* must be aligned to groups of 32 */
78
79         c->rq_desc_count =
80                 min_t(u32, ENIC_MAX_RQ_DESCS,
81                 max_t(u32, ENIC_MIN_RQ_DESCS,
82                 c->rq_desc_count));
83         c->rq_desc_count &= 0xffffffe0; /* must be aligned to groups of 32 */
84
85         if (c->mtu == 0)
86                 c->mtu = 1500;
87         c->mtu = min_t(u16, ENIC_MAX_MTU,
88                 max_t(u16, ENIC_MIN_MTU,
89                 c->mtu));
90
91         c->intr_timer_usec = min_t(u32,
92                 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
93                 c->intr_timer_usec);
94
95         printk(KERN_INFO PFX "vNIC MAC addr %pM wq/rq %d/%d\n",
96                 enic->mac_addr, c->wq_desc_count, c->rq_desc_count);
97         printk(KERN_INFO PFX "vNIC mtu %d csum tx/rx %d/%d tso/lro %d/%d "
98                 "intr timer %d usec\n",
99                 c->mtu, ENIC_SETTING(enic, TXCSUM),
100                 ENIC_SETTING(enic, RXCSUM), ENIC_SETTING(enic, TSO),
101                 ENIC_SETTING(enic, LRO), c->intr_timer_usec);
102
103         return 0;
104 }
105
106 int enic_add_vlan(struct enic *enic, u16 vlanid)
107 {
108         u64 a0 = vlanid, a1 = 0;
109         int wait = 1000;
110         int err;
111
112         err = vnic_dev_cmd(enic->vdev, CMD_VLAN_ADD, &a0, &a1, wait);
113         if (err)
114                 printk(KERN_ERR PFX "Can't add vlan id, %d\n", err);
115
116         return err;
117 }
118
119 int enic_del_vlan(struct enic *enic, u16 vlanid)
120 {
121         u64 a0 = vlanid, a1 = 0;
122         int wait = 1000;
123         int err;
124
125         err = vnic_dev_cmd(enic->vdev, CMD_VLAN_DEL, &a0, &a1, wait);
126         if (err)
127                 printk(KERN_ERR PFX "Can't delete vlan id, %d\n", err);
128
129         return err;
130 }
131
132 int enic_set_nic_cfg(struct enic *enic, u8 rss_default_cpu, u8 rss_hash_type,
133         u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable, u8 tso_ipid_split_en,
134         u8 ig_vlan_strip_en)
135 {
136         u64 a0, a1;
137         u32 nic_cfg;
138         int wait = 1000;
139
140         vnic_set_nic_cfg(&nic_cfg, rss_default_cpu,
141                 rss_hash_type, rss_hash_bits, rss_base_cpu,
142                 rss_enable, tso_ipid_split_en, ig_vlan_strip_en);
143
144         a0 = nic_cfg;
145         a1 = 0;
146
147         return vnic_dev_cmd(enic->vdev, CMD_NIC_CFG, &a0, &a1, wait);
148 }
149
150 int enic_set_rss_key(struct enic *enic, dma_addr_t key_pa, u64 len)
151 {
152         u64 a0 = (u64)key_pa, a1 = len;
153         int wait = 1000;
154
155         return vnic_dev_cmd(enic->vdev, CMD_RSS_KEY, &a0, &a1, wait);
156 }
157
158 int enic_set_rss_cpu(struct enic *enic, dma_addr_t cpu_pa, u64 len)
159 {
160         u64 a0 = (u64)cpu_pa, a1 = len;
161         int wait = 1000;
162
163         return vnic_dev_cmd(enic->vdev, CMD_RSS_CPU, &a0, &a1, wait);
164 }
165
166 void enic_free_vnic_resources(struct enic *enic)
167 {
168         unsigned int i;
169
170         for (i = 0; i < enic->wq_count; i++)
171                 vnic_wq_free(&enic->wq[i]);
172         for (i = 0; i < enic->rq_count; i++)
173                 vnic_rq_free(&enic->rq[i]);
174         for (i = 0; i < enic->cq_count; i++)
175                 vnic_cq_free(&enic->cq[i]);
176         for (i = 0; i < enic->intr_count; i++)
177                 vnic_intr_free(&enic->intr[i]);
178 }
179
180 void enic_get_res_counts(struct enic *enic)
181 {
182         enic->wq_count = min_t(int,
183                 vnic_dev_get_res_count(enic->vdev, RES_TYPE_WQ),
184                 ENIC_WQ_MAX);
185         enic->rq_count = min_t(int,
186                 vnic_dev_get_res_count(enic->vdev, RES_TYPE_RQ),
187                 ENIC_RQ_MAX);
188         enic->cq_count = min_t(int,
189                 vnic_dev_get_res_count(enic->vdev, RES_TYPE_CQ),
190                 ENIC_CQ_MAX);
191         enic->intr_count = min_t(int,
192                 vnic_dev_get_res_count(enic->vdev, RES_TYPE_INTR_CTRL),
193                 ENIC_INTR_MAX);
194
195         printk(KERN_INFO PFX "vNIC resources avail: "
196                 "wq %d rq %d cq %d intr %d\n",
197                 enic->wq_count, enic->rq_count,
198                 enic->cq_count, enic->intr_count);
199 }
200
201 void enic_init_vnic_resources(struct enic *enic)
202 {
203         enum vnic_dev_intr_mode intr_mode;
204         unsigned int mask_on_assertion;
205         unsigned int interrupt_offset;
206         unsigned int error_interrupt_enable;
207         unsigned int error_interrupt_offset;
208         unsigned int cq_index;
209         unsigned int i;
210
211         intr_mode = vnic_dev_get_intr_mode(enic->vdev);
212
213         /* Init RQ/WQ resources.
214          *
215          * RQ[0 - n-1] point to CQ[0 - n-1]
216          * WQ[0 - m-1] point to CQ[n - n+m-1]
217          *
218          * Error interrupt is not enabled for MSI.
219          */
220
221         switch (intr_mode) {
222         case VNIC_DEV_INTR_MODE_INTX:
223         case VNIC_DEV_INTR_MODE_MSIX:
224                 error_interrupt_enable = 1;
225                 error_interrupt_offset = enic->intr_count - 2;
226                 break;
227         default:
228                 error_interrupt_enable = 0;
229                 error_interrupt_offset = 0;
230                 break;
231         }
232
233         for (i = 0; i < enic->rq_count; i++) {
234                 cq_index = i;
235                 vnic_rq_init(&enic->rq[i],
236                         cq_index,
237                         error_interrupt_enable,
238                         error_interrupt_offset);
239         }
240
241         for (i = 0; i < enic->wq_count; i++) {
242                 cq_index = enic->rq_count + i;
243                 vnic_wq_init(&enic->wq[i],
244                         cq_index,
245                         error_interrupt_enable,
246                         error_interrupt_offset);
247         }
248
249         /* Init CQ resources
250          *
251          * CQ[0 - n+m-1] point to INTR[0] for INTx, MSI
252          * CQ[0 - n+m-1] point to INTR[0 - n+m-1] for MSI-X
253          */
254
255         for (i = 0; i < enic->cq_count; i++) {
256
257                 switch (intr_mode) {
258                 case VNIC_DEV_INTR_MODE_MSIX:
259                         interrupt_offset = i;
260                         break;
261                 default:
262                         interrupt_offset = 0;
263                         break;
264                 }
265
266                 vnic_cq_init(&enic->cq[i],
267                         0 /* flow_control_enable */,
268                         1 /* color_enable */,
269                         0 /* cq_head */,
270                         0 /* cq_tail */,
271                         1 /* cq_tail_color */,
272                         1 /* interrupt_enable */,
273                         1 /* cq_entry_enable */,
274                         0 /* cq_message_enable */,
275                         interrupt_offset,
276                         0 /* cq_message_addr */);
277         }
278
279         /* Init INTR resources
280          *
281          * mask_on_assertion is not used for INTx due to the level-
282          * triggered nature of INTx
283          */
284
285         switch (intr_mode) {
286         case VNIC_DEV_INTR_MODE_MSI:
287         case VNIC_DEV_INTR_MODE_MSIX:
288                 mask_on_assertion = 1;
289                 break;
290         default:
291                 mask_on_assertion = 0;
292                 break;
293         }
294
295         for (i = 0; i < enic->intr_count; i++) {
296                 vnic_intr_init(&enic->intr[i],
297                         INTR_COALESCE_USEC_TO_HW(enic->config.intr_timer_usec),
298                         enic->config.intr_timer_type,
299                         mask_on_assertion);
300         }
301 }
302
303 int enic_alloc_vnic_resources(struct enic *enic)
304 {
305         enum vnic_dev_intr_mode intr_mode;
306         unsigned int i;
307         int err;
308
309         intr_mode = vnic_dev_get_intr_mode(enic->vdev);
310
311         printk(KERN_INFO PFX "vNIC resources used:  "
312                 "wq %d rq %d cq %d intr %d intr mode %s\n",
313                 enic->wq_count, enic->rq_count,
314                 enic->cq_count, enic->intr_count,
315                 intr_mode == VNIC_DEV_INTR_MODE_INTX ? "legacy PCI INTx" :
316                 intr_mode == VNIC_DEV_INTR_MODE_MSI ? "MSI" :
317                 intr_mode == VNIC_DEV_INTR_MODE_MSIX ? "MSI-X" :
318                 "unknown"
319                 );
320
321         /* Allocate queue resources
322          */
323
324         for (i = 0; i < enic->wq_count; i++) {
325                 err = vnic_wq_alloc(enic->vdev, &enic->wq[i], i,
326                         enic->config.wq_desc_count,
327                         sizeof(struct wq_enet_desc));
328                 if (err)
329                         goto err_out_cleanup;
330         }
331
332         for (i = 0; i < enic->rq_count; i++) {
333                 err = vnic_rq_alloc(enic->vdev, &enic->rq[i], i,
334                         enic->config.rq_desc_count,
335                         sizeof(struct rq_enet_desc));
336                 if (err)
337                         goto err_out_cleanup;
338         }
339
340         for (i = 0; i < enic->cq_count; i++) {
341                 if (i < enic->rq_count)
342                         err = vnic_cq_alloc(enic->vdev, &enic->cq[i], i,
343                                 enic->config.rq_desc_count,
344                                 sizeof(struct cq_enet_rq_desc));
345                 else
346                         err = vnic_cq_alloc(enic->vdev, &enic->cq[i], i,
347                                 enic->config.wq_desc_count,
348                                 sizeof(struct cq_enet_wq_desc));
349                 if (err)
350                         goto err_out_cleanup;
351         }
352
353         for (i = 0; i < enic->intr_count; i++) {
354                 err = vnic_intr_alloc(enic->vdev, &enic->intr[i], i);
355                 if (err)
356                         goto err_out_cleanup;
357         }
358
359         /* Hook remaining resource
360          */
361
362         enic->legacy_pba = vnic_dev_get_res(enic->vdev,
363                 RES_TYPE_INTR_PBA_LEGACY, 0);
364         if (!enic->legacy_pba && intr_mode == VNIC_DEV_INTR_MODE_INTX) {
365                 printk(KERN_ERR PFX "Failed to hook legacy pba resource\n");
366                 err = -ENODEV;
367                 goto err_out_cleanup;
368         }
369
370         return 0;
371
372 err_out_cleanup:
373         enic_free_vnic_resources(enic);
374
375         return err;
376 }