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