1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * linux/drivers/mmc/core/sdio_cis.c
4 *
5 * Author: Nicolas Pitre
6 * Created: June 11, 2007
7 * Copyright: MontaVista Software Inc.
8 *
9 * Copyright 2007 Pierre Ossman
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14
15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/sdio.h>
18 #include <linux/mmc/sdio_func.h>
19
20 #include "sdio_cis.h"
21 #include "sdio_ops.h"
22
cistpl_vers_1(struct mmc_card * card,struct sdio_func * func,const unsigned char * buf,unsigned size)23 static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
24 const unsigned char *buf, unsigned size)
25 {
26 u8 major_rev, minor_rev;
27 unsigned i, nr_strings;
28 char **buffer, *string;
29
30 if (size < 2)
31 return 0;
32
33 major_rev = buf[0];
34 minor_rev = buf[1];
35
36 /* Find all null-terminated (including zero length) strings in
37 the TPLLV1_INFO field. Trailing garbage is ignored. */
38 buf += 2;
39 size -= 2;
40
41 nr_strings = 0;
42 for (i = 0; i < size; i++) {
43 if (buf[i] == 0xff)
44 break;
45 if (buf[i] == 0)
46 nr_strings++;
47 }
48 if (nr_strings == 0)
49 return 0;
50
51 size = i;
52
53 buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
54 if (!buffer)
55 return -ENOMEM;
56
57 string = (char*)(buffer + nr_strings);
58
59 for (i = 0; i < nr_strings; i++) {
60 buffer[i] = string;
61 strcpy(string, buf);
62 string += strlen(string) + 1;
63 buf += strlen(buf) + 1;
64 }
65
66 if (func) {
67 func->major_rev = major_rev;
68 func->minor_rev = minor_rev;
69 func->num_info = nr_strings;
70 func->info = (const char**)buffer;
71 } else {
72 card->major_rev = major_rev;
73 card->minor_rev = minor_rev;
74 card->num_info = nr_strings;
75 card->info = (const char**)buffer;
76 }
77
78 return 0;
79 }
80
cistpl_manfid(struct mmc_card * card,struct sdio_func * func,const unsigned char * buf,unsigned size)81 static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
82 const unsigned char *buf, unsigned size)
83 {
84 unsigned int vendor, device;
85
86 /* TPLMID_MANF */
87 vendor = buf[0] | (buf[1] << 8);
88
89 /* TPLMID_CARD */
90 device = buf[2] | (buf[3] << 8);
91
92 if (func) {
93 func->vendor = vendor;
94 func->device = device;
95 } else {
96 card->cis.vendor = vendor;
97 card->cis.device = device;
98 }
99
100 return 0;
101 }
102
103 static const unsigned char speed_val[16] =
104 { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
105 static const unsigned int speed_unit[8] =
106 { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
107
108
109 typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
110 const unsigned char *, unsigned);
111
112 struct cis_tpl {
113 unsigned char code;
114 unsigned char min_size;
115 tpl_parse_t *parse;
116 };
117
cis_tpl_parse(struct mmc_card * card,struct sdio_func * func,const char * tpl_descr,const struct cis_tpl * tpl,int tpl_count,unsigned char code,const unsigned char * buf,unsigned size)118 static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
119 const char *tpl_descr,
120 const struct cis_tpl *tpl, int tpl_count,
121 unsigned char code,
122 const unsigned char *buf, unsigned size)
123 {
124 int i, ret;
125
126 /* look for a matching code in the table */
127 for (i = 0; i < tpl_count; i++, tpl++) {
128 if (tpl->code == code)
129 break;
130 }
131 if (i < tpl_count) {
132 if (size >= tpl->min_size) {
133 if (tpl->parse)
134 ret = tpl->parse(card, func, buf, size);
135 else
136 ret = -EILSEQ; /* known tuple, not parsed */
137 } else {
138 /* invalid tuple */
139 ret = -EINVAL;
140 }
141 if (ret && ret != -EILSEQ && ret != -ENOENT) {
142 pr_err("%s: bad %s tuple 0x%02x (%u bytes)\n",
143 mmc_hostname(card->host), tpl_descr, code, size);
144 }
145 } else {
146 /* unknown tuple */
147 ret = -ENOENT;
148 }
149
150 return ret;
151 }
152
cistpl_funce_common(struct mmc_card * card,struct sdio_func * func,const unsigned char * buf,unsigned size)153 static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
154 const unsigned char *buf, unsigned size)
155 {
156 /* Only valid for the common CIS (function 0) */
157 if (func)
158 return -EINVAL;
159
160 /* TPLFE_FN0_BLK_SIZE */
161 card->cis.blksize = buf[1] | (buf[2] << 8);
162
163 /* TPLFE_MAX_TRAN_SPEED */
164 card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
165 speed_unit[buf[3] & 7];
166
167 return 0;
168 }
169
cistpl_funce_func(struct mmc_card * card,struct sdio_func * func,const unsigned char * buf,unsigned size)170 static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
171 const unsigned char *buf, unsigned size)
172 {
173 unsigned vsn;
174 unsigned min_size;
175
176 /* Only valid for the individual function's CIS (1-7) */
177 if (!func)
178 return -EINVAL;
179
180 /*
181 * This tuple has a different length depending on the SDIO spec
182 * version.
183 */
184 vsn = func->card->cccr.sdio_vsn;
185 min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
186
187 if (size == 28 && vsn == SDIO_SDIO_REV_1_10) {
188 pr_warn("%s: card has broken SDIO 1.1 CIS, forcing SDIO 1.0\n",
189 mmc_hostname(card->host));
190 vsn = SDIO_SDIO_REV_1_00;
191 } else if (size < min_size) {
192 return -EINVAL;
193 }
194
195 /* TPLFE_MAX_BLK_SIZE */
196 func->max_blksize = buf[12] | (buf[13] << 8);
197
198 /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
199 if (vsn > SDIO_SDIO_REV_1_00)
200 func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
201 else
202 func->enable_timeout = jiffies_to_msecs(HZ);
203
204 return 0;
205 }
206
207 /*
208 * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
209 *
210 * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
211 * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
212 * TPLFID_FUNCTION is always hardcoded to 0x0C.
213 */
214 static const struct cis_tpl cis_tpl_funce_list[] = {
215 { 0x00, 4, cistpl_funce_common },
216 { 0x01, 0, cistpl_funce_func },
217 { 0x04, 1+1+6, /* CISTPL_FUNCE_LAN_NODE_ID */ },
218 };
219
cistpl_funce(struct mmc_card * card,struct sdio_func * func,const unsigned char * buf,unsigned size)220 static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
221 const unsigned char *buf, unsigned size)
222 {
223 if (size < 1)
224 return -EINVAL;
225
226 return cis_tpl_parse(card, func, "CISTPL_FUNCE",
227 cis_tpl_funce_list,
228 ARRAY_SIZE(cis_tpl_funce_list),
229 buf[0], buf, size);
230 }
231
232 /* Known TPL_CODEs table for CIS tuples */
233 static const struct cis_tpl cis_tpl_list[] = {
234 { 0x15, 3, cistpl_vers_1 },
235 { 0x20, 4, cistpl_manfid },
236 { 0x21, 2, /* cistpl_funcid */ },
237 { 0x22, 0, cistpl_funce },
238 { 0x91, 2, /* cistpl_sdio_std */ },
239 };
240
sdio_read_cis(struct mmc_card * card,struct sdio_func * func)241 static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
242 {
243 int ret;
244 struct sdio_func_tuple *this, **prev;
245 unsigned i, ptr = 0;
246
247 /*
248 * Note that this works for the common CIS (function number 0) as
249 * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
250 * have the same offset.
251 */
252 for (i = 0; i < 3; i++) {
253 unsigned char x, fn;
254
255 if (func)
256 fn = func->num;
257 else
258 fn = 0;
259
260 ret = mmc_io_rw_direct(card, 0, 0,
261 SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
262 if (ret)
263 return ret;
264 ptr |= x << (i * 8);
265 }
266
267 if (func)
268 prev = &func->tuples;
269 else
270 prev = &card->tuples;
271
272 if (*prev)
273 return -EINVAL;
274
275 do {
276 unsigned char tpl_code, tpl_link;
277
278 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
279 if (ret)
280 break;
281
282 /* 0xff means we're done */
283 if (tpl_code == 0xff)
284 break;
285
286 /* null entries have no link field or data */
287 if (tpl_code == 0x00)
288 continue;
289
290 ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
291 if (ret)
292 break;
293
294 /* a size of 0xff also means we're done */
295 if (tpl_link == 0xff)
296 break;
297
298 this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
299 if (!this)
300 return -ENOMEM;
301
302 for (i = 0; i < tpl_link; i++) {
303 ret = mmc_io_rw_direct(card, 0, 0,
304 ptr + i, 0, &this->data[i]);
305 if (ret)
306 break;
307 }
308 if (ret) {
309 kfree(this);
310 break;
311 }
312
313 /* Try to parse the CIS tuple */
314 ret = cis_tpl_parse(card, func, "CIS",
315 cis_tpl_list, ARRAY_SIZE(cis_tpl_list),
316 tpl_code, this->data, tpl_link);
317 if (ret == -EILSEQ || ret == -ENOENT) {
318 /*
319 * The tuple is unknown or known but not parsed.
320 * Queue the tuple for the function driver.
321 */
322 this->next = NULL;
323 this->code = tpl_code;
324 this->size = tpl_link;
325 *prev = this;
326 prev = &this->next;
327
328 if (ret == -ENOENT) {
329 /* warn about unknown tuples */
330 pr_warn_ratelimited("%s: queuing unknown"
331 " CIS tuple 0x%02x (%u bytes)\n",
332 mmc_hostname(card->host),
333 tpl_code, tpl_link);
334 }
335
336 /* keep on analyzing tuples */
337 ret = 0;
338 } else {
339 /*
340 * We don't need the tuple anymore if it was
341 * successfully parsed by the SDIO core or if it is
342 * not going to be queued for a driver.
343 */
344 kfree(this);
345 }
346
347 ptr += tpl_link;
348 } while (!ret);
349
350 /*
351 * Link in all unknown tuples found in the common CIS so that
352 * drivers don't have to go digging in two places.
353 */
354 if (func)
355 *prev = card->tuples;
356
357 return ret;
358 }
359
sdio_read_common_cis(struct mmc_card * card)360 int sdio_read_common_cis(struct mmc_card *card)
361 {
362 return sdio_read_cis(card, NULL);
363 }
364
sdio_free_common_cis(struct mmc_card * card)365 void sdio_free_common_cis(struct mmc_card *card)
366 {
367 struct sdio_func_tuple *tuple, *victim;
368
369 tuple = card->tuples;
370
371 while (tuple) {
372 victim = tuple;
373 tuple = tuple->next;
374 kfree(victim);
375 }
376
377 card->tuples = NULL;
378 }
379
sdio_read_func_cis(struct sdio_func * func)380 int sdio_read_func_cis(struct sdio_func *func)
381 {
382 int ret;
383
384 ret = sdio_read_cis(func->card, func);
385 if (ret)
386 return ret;
387
388 /*
389 * Since we've linked to tuples in the card structure,
390 * we must make sure we have a reference to it.
391 */
392 get_device(&func->card->dev);
393
394 /*
395 * Vendor/device id is optional for function CIS, so
396 * copy it from the card structure as needed.
397 */
398 if (func->vendor == 0) {
399 func->vendor = func->card->cis.vendor;
400 func->device = func->card->cis.device;
401 }
402
403 return 0;
404 }
405
sdio_free_func_cis(struct sdio_func * func)406 void sdio_free_func_cis(struct sdio_func *func)
407 {
408 struct sdio_func_tuple *tuple, *victim;
409
410 tuple = func->tuples;
411
412 while (tuple && tuple != func->card->tuples) {
413 victim = tuple;
414 tuple = tuple->next;
415 kfree(victim);
416 }
417
418 func->tuples = NULL;
419
420 /*
421 * We have now removed the link to the tuples in the
422 * card structure, so remove the reference.
423 */
424 put_device(&func->card->dev);
425 }
426
427