blob: 40bd67a5c8e932d70af7fff8cba5f641e18653ee [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vivien Didelot720c6342017-03-11 16:12:48 -05002/*
3 * Marvell 88E6xxx Address Translation Unit (ATU) support
4 *
5 * Copyright (c) 2008 Marvell Semiconductor
6 * Copyright (c) 2017 Savoir-faire Linux, Inc.
Vivien Didelot720c6342017-03-11 16:12:48 -05007 */
Vivien Didelot19fb7f62019-08-09 18:47:55 -04008
9#include <linux/bitfield.h>
Andrew Lunn09776442018-01-14 02:32:44 +010010#include <linux/interrupt.h>
11#include <linux/irqdomain.h>
Vivien Didelot720c6342017-03-11 16:12:48 -050012
Vivien Didelot4d5f2ba72017-06-02 17:06:15 -040013#include "chip.h"
Vivien Didelot720c6342017-03-11 16:12:48 -050014#include "global1.h"
15
Vivien Didelot9c13c022017-03-11 16:12:52 -050016/* Offset 0x01: ATU FID Register */
17
18static int mv88e6xxx_g1_atu_fid_write(struct mv88e6xxx_chip *chip, u16 fid)
19{
Vivien Didelot27c0e602017-06-15 12:14:01 -040020 return mv88e6xxx_g1_write(chip, MV88E6352_G1_ATU_FID, fid & 0xfff);
Vivien Didelot9c13c022017-03-11 16:12:52 -050021}
22
Vivien Didelot720c6342017-03-11 16:12:48 -050023/* Offset 0x0A: ATU Control Register */
24
Vivien Didelotc3a7d4a2017-03-11 16:12:51 -050025int mv88e6xxx_g1_atu_set_learn2all(struct mv88e6xxx_chip *chip, bool learn2all)
26{
27 u16 val;
28 int err;
29
Vivien Didelot27c0e602017-06-15 12:14:01 -040030 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_ATU_CTL, &val);
Vivien Didelotc3a7d4a2017-03-11 16:12:51 -050031 if (err)
32 return err;
33
34 if (learn2all)
Vivien Didelot27c0e602017-06-15 12:14:01 -040035 val |= MV88E6XXX_G1_ATU_CTL_LEARN2ALL;
Vivien Didelotc3a7d4a2017-03-11 16:12:51 -050036 else
Vivien Didelot27c0e602017-06-15 12:14:01 -040037 val &= ~MV88E6XXX_G1_ATU_CTL_LEARN2ALL;
Vivien Didelotc3a7d4a2017-03-11 16:12:51 -050038
Vivien Didelot27c0e602017-06-15 12:14:01 -040039 return mv88e6xxx_g1_write(chip, MV88E6XXX_G1_ATU_CTL, val);
Vivien Didelotc3a7d4a2017-03-11 16:12:51 -050040}
41
Vivien Didelot720c6342017-03-11 16:12:48 -050042int mv88e6xxx_g1_atu_set_age_time(struct mv88e6xxx_chip *chip,
43 unsigned int msecs)
44{
45 const unsigned int coeff = chip->info->age_time_coeff;
46 const unsigned int min = 0x01 * coeff;
47 const unsigned int max = 0xff * coeff;
48 u8 age_time;
49 u16 val;
50 int err;
51
52 if (msecs < min || msecs > max)
53 return -ERANGE;
54
55 /* Round to nearest multiple of coeff */
56 age_time = (msecs + coeff / 2) / coeff;
57
Vivien Didelot27c0e602017-06-15 12:14:01 -040058 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_ATU_CTL, &val);
Vivien Didelot720c6342017-03-11 16:12:48 -050059 if (err)
60 return err;
61
62 /* AgeTime is 11:4 bits */
63 val &= ~0xff0;
64 val |= age_time << 4;
65
Vivien Didelot27c0e602017-06-15 12:14:01 -040066 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_ATU_CTL, val);
Vivien Didelotbae76dd2017-03-29 15:38:37 -040067 if (err)
68 return err;
69
70 dev_dbg(chip->dev, "AgeTime set to 0x%02x (%d ms)\n", age_time,
71 age_time * coeff);
72
73 return 0;
Vivien Didelot720c6342017-03-11 16:12:48 -050074}
Vivien Didelot9c13c022017-03-11 16:12:52 -050075
Andrew Lunn23e8b472019-10-25 01:03:52 +020076int mv88e6165_g1_atu_get_hash(struct mv88e6xxx_chip *chip, u8 *hash)
77{
78 int err;
79 u16 val;
80
81 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_ATU_CTL, &val);
82 if (err)
83 return err;
84
85 *hash = val & MV88E6161_G1_ATU_CTL_HASH_MASK;
86
87 return 0;
88}
89
90int mv88e6165_g1_atu_set_hash(struct mv88e6xxx_chip *chip, u8 hash)
91{
92 int err;
93 u16 val;
94
95 if (hash & ~MV88E6161_G1_ATU_CTL_HASH_MASK)
96 return -EINVAL;
97
98 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_ATU_CTL, &val);
99 if (err)
100 return err;
101
102 val &= ~MV88E6161_G1_ATU_CTL_HASH_MASK;
103 val |= hash;
104
105 return mv88e6xxx_g1_write(chip, MV88E6XXX_G1_ATU_CTL, val);
106}
107
Vivien Didelot9c13c022017-03-11 16:12:52 -0500108/* Offset 0x0B: ATU Operation Register */
109
110static int mv88e6xxx_g1_atu_op_wait(struct mv88e6xxx_chip *chip)
111{
Vivien Didelot19fb7f62019-08-09 18:47:55 -0400112 int bit = __bf_shf(MV88E6XXX_G1_ATU_OP_BUSY);
113
114 return mv88e6xxx_g1_wait_bit(chip, MV88E6XXX_G1_ATU_OP, bit, 0);
Vivien Didelot9c13c022017-03-11 16:12:52 -0500115}
116
117static int mv88e6xxx_g1_atu_op(struct mv88e6xxx_chip *chip, u16 fid, u16 op)
118{
119 u16 val;
120 int err;
121
122 /* FID bits are dispatched all around gradually as more are supported */
123 if (mv88e6xxx_num_databases(chip) > 256) {
124 err = mv88e6xxx_g1_atu_fid_write(chip, fid);
125 if (err)
126 return err;
127 } else {
Rasmus Villemoes7b83df02019-06-04 07:34:25 +0000128 if (mv88e6xxx_num_databases(chip) > 64) {
Vivien Didelot9c13c022017-03-11 16:12:52 -0500129 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
Vivien Didelot27c0e602017-06-15 12:14:01 -0400130 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_ATU_CTL,
131 &val);
Vivien Didelot9c13c022017-03-11 16:12:52 -0500132 if (err)
133 return err;
134
135 val = (val & 0x0fff) | ((fid << 8) & 0xf000);
Vivien Didelot27c0e602017-06-15 12:14:01 -0400136 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_ATU_CTL,
137 val);
Vivien Didelot9c13c022017-03-11 16:12:52 -0500138 if (err)
139 return err;
Rasmus Villemoes7b83df02019-06-04 07:34:25 +0000140 } else if (mv88e6xxx_num_databases(chip) > 16) {
141 /* ATU DBNum[5:4] are located in ATU Operation 9:8 */
142 op |= (fid & 0x30) << 4;
Vivien Didelot9c13c022017-03-11 16:12:52 -0500143 }
144
145 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
146 op |= fid & 0xf;
147 }
148
Vivien Didelot27c0e602017-06-15 12:14:01 -0400149 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_ATU_OP,
150 MV88E6XXX_G1_ATU_OP_BUSY | op);
Vivien Didelot9c13c022017-03-11 16:12:52 -0500151 if (err)
152 return err;
153
154 return mv88e6xxx_g1_atu_op_wait(chip);
155}
156
Andrew Lunnc5f299d52019-11-05 01:13:00 +0100157int mv88e6xxx_g1_atu_get_next(struct mv88e6xxx_chip *chip, u16 fid)
158{
159 return mv88e6xxx_g1_atu_op(chip, fid, MV88E6XXX_G1_ATU_OP_GET_NEXT_DB);
160}
161
Vivien Didelot9c13c022017-03-11 16:12:52 -0500162/* Offset 0x0C: ATU Data Register */
163
Vivien Didelotdabc1a92017-03-11 16:12:53 -0500164static int mv88e6xxx_g1_atu_data_read(struct mv88e6xxx_chip *chip,
165 struct mv88e6xxx_atu_entry *entry)
166{
167 u16 val;
168 int err;
169
Vivien Didelot27c0e602017-06-15 12:14:01 -0400170 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_ATU_DATA, &val);
Vivien Didelotdabc1a92017-03-11 16:12:53 -0500171 if (err)
172 return err;
173
174 entry->state = val & 0xf;
Vivien Didelotd8291a92019-09-07 16:00:47 -0400175 if (entry->state) {
Vivien Didelot27c0e602017-06-15 12:14:01 -0400176 entry->trunk = !!(val & MV88E6XXX_G1_ATU_DATA_TRUNK);
Vivien Didelot01bd96c2017-03-11 16:12:57 -0500177 entry->portvec = (val >> 4) & mv88e6xxx_port_mask(chip);
Vivien Didelotdabc1a92017-03-11 16:12:53 -0500178 }
179
180 return 0;
181}
182
Vivien Didelot9c13c022017-03-11 16:12:52 -0500183static int mv88e6xxx_g1_atu_data_write(struct mv88e6xxx_chip *chip,
184 struct mv88e6xxx_atu_entry *entry)
185{
186 u16 data = entry->state & 0xf;
187
Vivien Didelotd8291a92019-09-07 16:00:47 -0400188 if (entry->state) {
Vivien Didelot9c13c022017-03-11 16:12:52 -0500189 if (entry->trunk)
Vivien Didelot27c0e602017-06-15 12:14:01 -0400190 data |= MV88E6XXX_G1_ATU_DATA_TRUNK;
Vivien Didelot9c13c022017-03-11 16:12:52 -0500191
Vivien Didelot01bd96c2017-03-11 16:12:57 -0500192 data |= (entry->portvec & mv88e6xxx_port_mask(chip)) << 4;
Vivien Didelot9c13c022017-03-11 16:12:52 -0500193 }
194
Vivien Didelot27c0e602017-06-15 12:14:01 -0400195 return mv88e6xxx_g1_write(chip, MV88E6XXX_G1_ATU_DATA, data);
Vivien Didelot9c13c022017-03-11 16:12:52 -0500196}
197
198/* Offset 0x0D: ATU MAC Address Register Bytes 0 & 1
199 * Offset 0x0E: ATU MAC Address Register Bytes 2 & 3
200 * Offset 0x0F: ATU MAC Address Register Bytes 4 & 5
201 */
202
Vivien Didelotdabc1a92017-03-11 16:12:53 -0500203static int mv88e6xxx_g1_atu_mac_read(struct mv88e6xxx_chip *chip,
204 struct mv88e6xxx_atu_entry *entry)
205{
206 u16 val;
207 int i, err;
208
209 for (i = 0; i < 3; i++) {
Vivien Didelot27c0e602017-06-15 12:14:01 -0400210 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_ATU_MAC01 + i, &val);
Vivien Didelotdabc1a92017-03-11 16:12:53 -0500211 if (err)
212 return err;
213
214 entry->mac[i * 2] = val >> 8;
215 entry->mac[i * 2 + 1] = val & 0xff;
216 }
217
218 return 0;
219}
220
Vivien Didelot9c13c022017-03-11 16:12:52 -0500221static int mv88e6xxx_g1_atu_mac_write(struct mv88e6xxx_chip *chip,
222 struct mv88e6xxx_atu_entry *entry)
223{
224 u16 val;
225 int i, err;
226
227 for (i = 0; i < 3; i++) {
228 val = (entry->mac[i * 2] << 8) | entry->mac[i * 2 + 1];
Vivien Didelot27c0e602017-06-15 12:14:01 -0400229 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_ATU_MAC01 + i, val);
Vivien Didelot9c13c022017-03-11 16:12:52 -0500230 if (err)
231 return err;
232 }
233
234 return 0;
235}
236
237/* Address Translation Unit operations */
238
Vivien Didelotdabc1a92017-03-11 16:12:53 -0500239int mv88e6xxx_g1_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
240 struct mv88e6xxx_atu_entry *entry)
241{
242 int err;
243
244 err = mv88e6xxx_g1_atu_op_wait(chip);
245 if (err)
246 return err;
247
248 /* Write the MAC address to iterate from only once */
Vivien Didelotd8291a92019-09-07 16:00:47 -0400249 if (!entry->state) {
Vivien Didelotdabc1a92017-03-11 16:12:53 -0500250 err = mv88e6xxx_g1_atu_mac_write(chip, entry);
251 if (err)
252 return err;
253 }
254
Vivien Didelot27c0e602017-06-15 12:14:01 -0400255 err = mv88e6xxx_g1_atu_op(chip, fid, MV88E6XXX_G1_ATU_OP_GET_NEXT_DB);
Vivien Didelotdabc1a92017-03-11 16:12:53 -0500256 if (err)
257 return err;
258
259 err = mv88e6xxx_g1_atu_data_read(chip, entry);
260 if (err)
261 return err;
262
263 return mv88e6xxx_g1_atu_mac_read(chip, entry);
264}
265
Vivien Didelot9c13c022017-03-11 16:12:52 -0500266int mv88e6xxx_g1_atu_loadpurge(struct mv88e6xxx_chip *chip, u16 fid,
267 struct mv88e6xxx_atu_entry *entry)
268{
269 int err;
270
271 err = mv88e6xxx_g1_atu_op_wait(chip);
272 if (err)
273 return err;
274
275 err = mv88e6xxx_g1_atu_mac_write(chip, entry);
276 if (err)
277 return err;
278
279 err = mv88e6xxx_g1_atu_data_write(chip, entry);
280 if (err)
281 return err;
282
Vivien Didelot27c0e602017-06-15 12:14:01 -0400283 return mv88e6xxx_g1_atu_op(chip, fid, MV88E6XXX_G1_ATU_OP_LOAD_DB);
Vivien Didelot9c13c022017-03-11 16:12:52 -0500284}
Vivien Didelotdaefc942017-03-11 16:12:54 -0500285
286static int mv88e6xxx_g1_atu_flushmove(struct mv88e6xxx_chip *chip, u16 fid,
287 struct mv88e6xxx_atu_entry *entry,
288 bool all)
289{
290 u16 op;
291 int err;
292
293 err = mv88e6xxx_g1_atu_op_wait(chip);
294 if (err)
295 return err;
296
297 err = mv88e6xxx_g1_atu_data_write(chip, entry);
298 if (err)
299 return err;
300
301 /* Flush/Move all or non-static entries from all or a given database */
302 if (all && fid)
Vivien Didelot27c0e602017-06-15 12:14:01 -0400303 op = MV88E6XXX_G1_ATU_OP_FLUSH_MOVE_ALL_DB;
Vivien Didelotdaefc942017-03-11 16:12:54 -0500304 else if (fid)
Vivien Didelot27c0e602017-06-15 12:14:01 -0400305 op = MV88E6XXX_G1_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
Vivien Didelotdaefc942017-03-11 16:12:54 -0500306 else if (all)
Vivien Didelot27c0e602017-06-15 12:14:01 -0400307 op = MV88E6XXX_G1_ATU_OP_FLUSH_MOVE_ALL;
Vivien Didelotdaefc942017-03-11 16:12:54 -0500308 else
Vivien Didelot27c0e602017-06-15 12:14:01 -0400309 op = MV88E6XXX_G1_ATU_OP_FLUSH_MOVE_NON_STATIC;
Vivien Didelotdaefc942017-03-11 16:12:54 -0500310
311 return mv88e6xxx_g1_atu_op(chip, fid, op);
312}
313
314int mv88e6xxx_g1_atu_flush(struct mv88e6xxx_chip *chip, u16 fid, bool all)
315{
316 struct mv88e6xxx_atu_entry entry = {
317 .state = 0, /* Null EntryState means Flush */
318 };
319
320 return mv88e6xxx_g1_atu_flushmove(chip, fid, &entry, all);
321}
Vivien Didelote606ca32017-03-11 16:12:55 -0500322
323static int mv88e6xxx_g1_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
324 int from_port, int to_port, bool all)
325{
326 struct mv88e6xxx_atu_entry entry = { 0 };
327 unsigned long mask;
328 int shift;
329
330 if (!chip->info->atu_move_port_mask)
331 return -EOPNOTSUPP;
332
333 mask = chip->info->atu_move_port_mask;
334 shift = bitmap_weight(&mask, 16);
335
Zheng Yongjun59d4c932020-12-09 21:39:38 +0800336 entry.state = 0xf; /* Full EntryState means Move */
Vivien Didelot01bd96c2017-03-11 16:12:57 -0500337 entry.portvec = from_port & mask;
338 entry.portvec |= (to_port & mask) << shift;
Vivien Didelote606ca32017-03-11 16:12:55 -0500339
340 return mv88e6xxx_g1_atu_flushmove(chip, fid, &entry, all);
341}
342
343int mv88e6xxx_g1_atu_remove(struct mv88e6xxx_chip *chip, u16 fid, int port,
344 bool all)
345{
346 int from_port = port;
347 int to_port = chip->info->atu_move_port_mask;
348
349 return mv88e6xxx_g1_atu_move(chip, fid, from_port, to_port, all);
350}
Andrew Lunn09776442018-01-14 02:32:44 +0100351
352static irqreturn_t mv88e6xxx_g1_atu_prob_irq_thread_fn(int irq, void *dev_id)
353{
354 struct mv88e6xxx_chip *chip = dev_id;
355 struct mv88e6xxx_atu_entry entry;
Andrew Lunn75c05a72019-02-06 00:02:58 +0100356 int spid;
Andrew Lunn09776442018-01-14 02:32:44 +0100357 int err;
358 u16 val;
359
Rasmus Villemoesc9acece2019-06-20 13:50:42 +0000360 mv88e6xxx_reg_lock(chip);
Andrew Lunn09776442018-01-14 02:32:44 +0100361
362 err = mv88e6xxx_g1_atu_op(chip, 0,
363 MV88E6XXX_G1_ATU_OP_GET_CLR_VIOLATION);
364 if (err)
365 goto out;
366
367 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_ATU_OP, &val);
368 if (err)
369 goto out;
370
371 err = mv88e6xxx_g1_atu_data_read(chip, &entry);
372 if (err)
373 goto out;
374
375 err = mv88e6xxx_g1_atu_mac_read(chip, &entry);
376 if (err)
377 goto out;
378
Andrew Lunn75c05a72019-02-06 00:02:58 +0100379 spid = entry.state;
380
Andrew Lunn09776442018-01-14 02:32:44 +0100381 if (val & MV88E6XXX_G1_ATU_OP_AGE_OUT_VIOLATION) {
382 dev_err_ratelimited(chip->dev,
383 "ATU age out violation for %pM\n",
384 entry.mac);
385 }
386
387 if (val & MV88E6XXX_G1_ATU_OP_MEMBER_VIOLATION) {
388 dev_err_ratelimited(chip->dev,
Andrew Lunn75c05a72019-02-06 00:02:58 +0100389 "ATU member violation for %pM portvec %x spid %d\n",
390 entry.mac, entry.portvec, spid);
391 chip->ports[spid].atu_member_violation++;
Andrew Lunn09776442018-01-14 02:32:44 +0100392 }
393
Andrew Lunnddca24d2018-09-14 23:46:12 +0200394 if (val & MV88E6XXX_G1_ATU_OP_MISS_VIOLATION) {
Andrew Lunn09776442018-01-14 02:32:44 +0100395 dev_err_ratelimited(chip->dev,
Andrew Lunn75c05a72019-02-06 00:02:58 +0100396 "ATU miss violation for %pM portvec %x spid %d\n",
397 entry.mac, entry.portvec, spid);
398 chip->ports[spid].atu_miss_violation++;
Andrew Lunn65f60e42018-03-28 23:50:28 +0200399 }
Andrew Lunn09776442018-01-14 02:32:44 +0100400
Andrew Lunn65f60e42018-03-28 23:50:28 +0200401 if (val & MV88E6XXX_G1_ATU_OP_FULL_VIOLATION) {
Andrew Lunn09776442018-01-14 02:32:44 +0100402 dev_err_ratelimited(chip->dev,
Andrew Lunn75c05a72019-02-06 00:02:58 +0100403 "ATU full violation for %pM portvec %x spid %d\n",
404 entry.mac, entry.portvec, spid);
405 chip->ports[spid].atu_full_violation++;
Andrew Lunn65f60e42018-03-28 23:50:28 +0200406 }
Rasmus Villemoesc9acece2019-06-20 13:50:42 +0000407 mv88e6xxx_reg_unlock(chip);
Andrew Lunn09776442018-01-14 02:32:44 +0100408
409 return IRQ_HANDLED;
410
411out:
Rasmus Villemoesc9acece2019-06-20 13:50:42 +0000412 mv88e6xxx_reg_unlock(chip);
Andrew Lunn09776442018-01-14 02:32:44 +0100413
414 dev_err(chip->dev, "ATU problem: error %d while handling interrupt\n",
415 err);
416 return IRQ_HANDLED;
417}
418
419int mv88e6xxx_g1_atu_prob_irq_setup(struct mv88e6xxx_chip *chip)
420{
421 int err;
422
423 chip->atu_prob_irq = irq_find_mapping(chip->g1_irq.domain,
424 MV88E6XXX_G1_STS_IRQ_ATU_PROB);
425 if (chip->atu_prob_irq < 0)
Andrew Lunn9b662a3e2018-01-18 17:42:49 +0100426 return chip->atu_prob_irq;
Andrew Lunn09776442018-01-14 02:32:44 +0100427
Andrew Lunn8ddf0b52020-01-06 17:13:52 +0100428 snprintf(chip->atu_prob_irq_name, sizeof(chip->atu_prob_irq_name),
429 "mv88e6xxx-%s-g1-atu-prob", dev_name(chip->dev));
430
Andrew Lunn09776442018-01-14 02:32:44 +0100431 err = request_threaded_irq(chip->atu_prob_irq, NULL,
432 mv88e6xxx_g1_atu_prob_irq_thread_fn,
Andrew Lunn8ddf0b52020-01-06 17:13:52 +0100433 IRQF_ONESHOT, chip->atu_prob_irq_name,
Andrew Lunn09776442018-01-14 02:32:44 +0100434 chip);
435 if (err)
436 irq_dispose_mapping(chip->atu_prob_irq);
437
438 return err;
439}
440
441void mv88e6xxx_g1_atu_prob_irq_free(struct mv88e6xxx_chip *chip)
442{
443 free_irq(chip->atu_prob_irq, chip);
444 irq_dispose_mapping(chip->atu_prob_irq);
445}