Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * MTD device concatenation layer |
| 3 | * |
| 4 | * (C) 2002 Robert Kaiser <rkaiser@sysgo.de> |
| 5 | * |
| 6 | * NAND support by Christian Gan <cgan@iders.ca> |
| 7 | * |
| 8 | * This code is GPL |
| 9 | * |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 10 | * $Id: mtdconcat.c,v 1.11 2005/11/07 11:14:20 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Thomas Gleixner | 15fdc52 | 2005-11-07 00:14:42 +0100 | [diff] [blame] | 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
Thomas Gleixner | 15fdc52 | 2005-11-07 00:14:42 +0100 | [diff] [blame] | 16 | #include <linux/sched.h> |
| 17 | #include <linux/types.h> |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/mtd/mtd.h> |
| 20 | #include <linux/mtd/concat.h> |
| 21 | |
Andrew Morton | 6c8b44a | 2006-05-20 10:17:21 +0100 | [diff] [blame] | 22 | #include <asm/div64.h> |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | /* |
| 25 | * Our storage structure: |
| 26 | * Subdev points to an array of pointers to struct mtd_info objects |
| 27 | * which is allocated along with this structure |
| 28 | * |
| 29 | */ |
| 30 | struct mtd_concat { |
| 31 | struct mtd_info mtd; |
| 32 | int num_subdev; |
| 33 | struct mtd_info **subdev; |
| 34 | }; |
| 35 | |
| 36 | /* |
| 37 | * how to calculate the size required for the above structure, |
| 38 | * including the pointer array subdev points to: |
| 39 | */ |
| 40 | #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \ |
| 41 | ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *))) |
| 42 | |
| 43 | /* |
| 44 | * Given a pointer to the MTD object in the mtd_concat structure, |
| 45 | * we can retrieve the pointer to that structure with this macro. |
| 46 | */ |
| 47 | #define CONCAT(x) ((struct mtd_concat *)(x)) |
| 48 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 49 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * MTD methods which look up the relevant subdevice, translate the |
| 51 | * effective address and pass through to the subdevice. |
| 52 | */ |
| 53 | |
| 54 | static int |
| 55 | concat_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 56 | size_t * retlen, u_char * buf) |
| 57 | { |
| 58 | struct mtd_concat *concat = CONCAT(mtd); |
| 59 | int err = -EINVAL; |
| 60 | int i; |
| 61 | |
| 62 | *retlen = 0; |
| 63 | |
| 64 | for (i = 0; i < concat->num_subdev; i++) { |
| 65 | struct mtd_info *subdev = concat->subdev[i]; |
| 66 | size_t size, retsize; |
| 67 | |
| 68 | if (from >= subdev->size) { |
| 69 | /* Not destined for this subdev */ |
| 70 | size = 0; |
| 71 | from -= subdev->size; |
| 72 | continue; |
| 73 | } |
| 74 | if (from + len > subdev->size) |
| 75 | /* First part goes into this subdev */ |
| 76 | size = subdev->size - from; |
| 77 | else |
| 78 | /* Entire transaction goes into this subdev */ |
| 79 | size = len; |
| 80 | |
| 81 | err = subdev->read(subdev, from, size, &retsize, buf); |
| 82 | |
| 83 | if (err) |
| 84 | break; |
| 85 | |
| 86 | *retlen += retsize; |
| 87 | len -= size; |
| 88 | if (len == 0) |
| 89 | break; |
| 90 | |
| 91 | err = -EINVAL; |
| 92 | buf += size; |
| 93 | from = 0; |
| 94 | } |
| 95 | return err; |
| 96 | } |
| 97 | |
| 98 | static int |
| 99 | concat_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 100 | size_t * retlen, const u_char * buf) |
| 101 | { |
| 102 | struct mtd_concat *concat = CONCAT(mtd); |
| 103 | int err = -EINVAL; |
| 104 | int i; |
| 105 | |
| 106 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 107 | return -EROFS; |
| 108 | |
| 109 | *retlen = 0; |
| 110 | |
| 111 | for (i = 0; i < concat->num_subdev; i++) { |
| 112 | struct mtd_info *subdev = concat->subdev[i]; |
| 113 | size_t size, retsize; |
| 114 | |
| 115 | if (to >= subdev->size) { |
| 116 | size = 0; |
| 117 | to -= subdev->size; |
| 118 | continue; |
| 119 | } |
| 120 | if (to + len > subdev->size) |
| 121 | size = subdev->size - to; |
| 122 | else |
| 123 | size = len; |
| 124 | |
| 125 | if (!(subdev->flags & MTD_WRITEABLE)) |
| 126 | err = -EROFS; |
| 127 | else |
| 128 | err = subdev->write(subdev, to, size, &retsize, buf); |
| 129 | |
| 130 | if (err) |
| 131 | break; |
| 132 | |
| 133 | *retlen += retsize; |
| 134 | len -= size; |
| 135 | if (len == 0) |
| 136 | break; |
| 137 | |
| 138 | err = -EINVAL; |
| 139 | buf += size; |
| 140 | to = 0; |
| 141 | } |
| 142 | return err; |
| 143 | } |
| 144 | |
| 145 | static int |
| 146 | concat_read_ecc(struct mtd_info *mtd, loff_t from, size_t len, |
| 147 | size_t * retlen, u_char * buf, u_char * eccbuf, |
| 148 | struct nand_oobinfo *oobsel) |
| 149 | { |
| 150 | struct mtd_concat *concat = CONCAT(mtd); |
| 151 | int err = -EINVAL; |
| 152 | int i; |
| 153 | |
| 154 | *retlen = 0; |
| 155 | |
| 156 | for (i = 0; i < concat->num_subdev; i++) { |
| 157 | struct mtd_info *subdev = concat->subdev[i]; |
| 158 | size_t size, retsize; |
| 159 | |
| 160 | if (from >= subdev->size) { |
| 161 | /* Not destined for this subdev */ |
| 162 | size = 0; |
| 163 | from -= subdev->size; |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | if (from + len > subdev->size) |
| 168 | /* First part goes into this subdev */ |
| 169 | size = subdev->size - from; |
| 170 | else |
| 171 | /* Entire transaction goes into this subdev */ |
| 172 | size = len; |
| 173 | |
| 174 | if (subdev->read_ecc) |
| 175 | err = subdev->read_ecc(subdev, from, size, |
| 176 | &retsize, buf, eccbuf, oobsel); |
| 177 | else |
| 178 | err = -EINVAL; |
| 179 | |
| 180 | if (err) |
| 181 | break; |
| 182 | |
| 183 | *retlen += retsize; |
| 184 | len -= size; |
| 185 | if (len == 0) |
| 186 | break; |
| 187 | |
| 188 | err = -EINVAL; |
| 189 | buf += size; |
| 190 | if (eccbuf) { |
| 191 | eccbuf += subdev->oobsize; |
| 192 | /* in nand.c at least, eccbufs are |
| 193 | tagged with 2 (int)eccstatus'; we |
| 194 | must account for these */ |
| 195 | eccbuf += 2 * (sizeof (int)); |
| 196 | } |
| 197 | from = 0; |
| 198 | } |
| 199 | return err; |
| 200 | } |
| 201 | |
| 202 | static int |
| 203 | concat_write_ecc(struct mtd_info *mtd, loff_t to, size_t len, |
| 204 | size_t * retlen, const u_char * buf, u_char * eccbuf, |
| 205 | struct nand_oobinfo *oobsel) |
| 206 | { |
| 207 | struct mtd_concat *concat = CONCAT(mtd); |
| 208 | int err = -EINVAL; |
| 209 | int i; |
| 210 | |
| 211 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 212 | return -EROFS; |
| 213 | |
| 214 | *retlen = 0; |
| 215 | |
| 216 | for (i = 0; i < concat->num_subdev; i++) { |
| 217 | struct mtd_info *subdev = concat->subdev[i]; |
| 218 | size_t size, retsize; |
| 219 | |
| 220 | if (to >= subdev->size) { |
| 221 | size = 0; |
| 222 | to -= subdev->size; |
| 223 | continue; |
| 224 | } |
| 225 | if (to + len > subdev->size) |
| 226 | size = subdev->size - to; |
| 227 | else |
| 228 | size = len; |
| 229 | |
| 230 | if (!(subdev->flags & MTD_WRITEABLE)) |
| 231 | err = -EROFS; |
| 232 | else if (subdev->write_ecc) |
| 233 | err = subdev->write_ecc(subdev, to, size, |
| 234 | &retsize, buf, eccbuf, oobsel); |
| 235 | else |
| 236 | err = -EINVAL; |
| 237 | |
| 238 | if (err) |
| 239 | break; |
| 240 | |
| 241 | *retlen += retsize; |
| 242 | len -= size; |
| 243 | if (len == 0) |
| 244 | break; |
| 245 | |
| 246 | err = -EINVAL; |
| 247 | buf += size; |
| 248 | if (eccbuf) |
| 249 | eccbuf += subdev->oobsize; |
| 250 | to = 0; |
| 251 | } |
| 252 | return err; |
| 253 | } |
| 254 | |
| 255 | static int |
Alexander Belyakov | e8d3293 | 2006-05-17 19:11:16 +0400 | [diff] [blame] | 256 | concat_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs, |
| 257 | unsigned long count, loff_t to, size_t * retlen, |
| 258 | u_char *eccbuf, struct nand_oobinfo *oobsel) |
| 259 | { |
| 260 | struct mtd_concat *concat = CONCAT(mtd); |
| 261 | struct kvec *vecs_copy; |
| 262 | unsigned long entry_low, entry_high; |
| 263 | size_t total_len = 0; |
| 264 | int i; |
| 265 | int err = -EINVAL; |
| 266 | |
| 267 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 268 | return -EROFS; |
| 269 | |
| 270 | *retlen = 0; |
| 271 | |
| 272 | /* Calculate total length of data */ |
| 273 | for (i = 0; i < count; i++) |
| 274 | total_len += vecs[i].iov_len; |
| 275 | |
| 276 | /* Do not allow write past end of device */ |
| 277 | if ((to + total_len) > mtd->size) |
| 278 | return -EINVAL; |
| 279 | |
| 280 | /* Check alignment */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame^] | 281 | if (mtd->writesize > 1) { |
Andrew Morton | 6c8b44a | 2006-05-20 10:17:21 +0100 | [diff] [blame] | 282 | loff_t __to = to; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame^] | 283 | if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize)) |
Alexander Belyakov | e8d3293 | 2006-05-17 19:11:16 +0400 | [diff] [blame] | 284 | return -EINVAL; |
Andrew Morton | 6c8b44a | 2006-05-20 10:17:21 +0100 | [diff] [blame] | 285 | } |
Alexander Belyakov | e8d3293 | 2006-05-17 19:11:16 +0400 | [diff] [blame] | 286 | |
| 287 | /* make a copy of vecs */ |
| 288 | vecs_copy = kmalloc(sizeof(struct kvec) * count, GFP_KERNEL); |
| 289 | if (!vecs_copy) |
| 290 | return -ENOMEM; |
| 291 | memcpy(vecs_copy, vecs, sizeof(struct kvec) * count); |
| 292 | |
| 293 | entry_low = 0; |
| 294 | for (i = 0; i < concat->num_subdev; i++) { |
| 295 | struct mtd_info *subdev = concat->subdev[i]; |
| 296 | size_t size, wsize, retsize, old_iov_len; |
| 297 | |
| 298 | if (to >= subdev->size) { |
| 299 | to -= subdev->size; |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | size = min(total_len, (size_t)(subdev->size - to)); |
| 304 | wsize = size; /* store for future use */ |
| 305 | |
| 306 | entry_high = entry_low; |
| 307 | while (entry_high < count) { |
| 308 | if (size <= vecs_copy[entry_high].iov_len) |
| 309 | break; |
| 310 | size -= vecs_copy[entry_high++].iov_len; |
| 311 | } |
| 312 | |
| 313 | old_iov_len = vecs_copy[entry_high].iov_len; |
| 314 | vecs_copy[entry_high].iov_len = size; |
| 315 | |
| 316 | if (!(subdev->flags & MTD_WRITEABLE)) |
| 317 | err = -EROFS; |
| 318 | else if (eccbuf) |
| 319 | err = subdev->writev_ecc(subdev, &vecs_copy[entry_low], |
| 320 | entry_high - entry_low + 1, to, &retsize, |
| 321 | eccbuf, oobsel); |
| 322 | else |
| 323 | err = subdev->writev(subdev, &vecs_copy[entry_low], |
| 324 | entry_high - entry_low + 1, to, &retsize); |
| 325 | |
| 326 | vecs_copy[entry_high].iov_len = old_iov_len - size; |
| 327 | vecs_copy[entry_high].iov_base += size; |
| 328 | |
| 329 | entry_low = entry_high; |
| 330 | |
| 331 | if (err) |
| 332 | break; |
| 333 | |
| 334 | *retlen += retsize; |
| 335 | total_len -= wsize; |
| 336 | if (concat->mtd.type == MTD_NANDFLASH && eccbuf) |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame^] | 337 | eccbuf += mtd->oobavail * (wsize / mtd->writesize); |
Alexander Belyakov | e8d3293 | 2006-05-17 19:11:16 +0400 | [diff] [blame] | 338 | |
| 339 | if (total_len == 0) |
| 340 | break; |
| 341 | |
| 342 | err = -EINVAL; |
| 343 | to = 0; |
| 344 | } |
| 345 | |
| 346 | kfree(vecs_copy); |
| 347 | return err; |
| 348 | } |
| 349 | |
| 350 | static int |
| 351 | concat_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 352 | unsigned long count, loff_t to, size_t * retlen) |
| 353 | { |
| 354 | return concat_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL); |
| 355 | } |
| 356 | |
| 357 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | concat_read_oob(struct mtd_info *mtd, loff_t from, size_t len, |
| 359 | size_t * retlen, u_char * buf) |
| 360 | { |
| 361 | struct mtd_concat *concat = CONCAT(mtd); |
| 362 | int err = -EINVAL; |
| 363 | int i; |
| 364 | |
| 365 | *retlen = 0; |
| 366 | |
| 367 | for (i = 0; i < concat->num_subdev; i++) { |
| 368 | struct mtd_info *subdev = concat->subdev[i]; |
| 369 | size_t size, retsize; |
| 370 | |
| 371 | if (from >= subdev->size) { |
| 372 | /* Not destined for this subdev */ |
| 373 | size = 0; |
| 374 | from -= subdev->size; |
| 375 | continue; |
| 376 | } |
| 377 | if (from + len > subdev->size) |
| 378 | /* First part goes into this subdev */ |
| 379 | size = subdev->size - from; |
| 380 | else |
| 381 | /* Entire transaction goes into this subdev */ |
| 382 | size = len; |
| 383 | |
| 384 | if (subdev->read_oob) |
| 385 | err = subdev->read_oob(subdev, from, size, |
| 386 | &retsize, buf); |
| 387 | else |
| 388 | err = -EINVAL; |
| 389 | |
| 390 | if (err) |
| 391 | break; |
| 392 | |
| 393 | *retlen += retsize; |
| 394 | len -= size; |
| 395 | if (len == 0) |
| 396 | break; |
| 397 | |
| 398 | err = -EINVAL; |
| 399 | buf += size; |
| 400 | from = 0; |
| 401 | } |
| 402 | return err; |
| 403 | } |
| 404 | |
| 405 | static int |
| 406 | concat_write_oob(struct mtd_info *mtd, loff_t to, size_t len, |
| 407 | size_t * retlen, const u_char * buf) |
| 408 | { |
| 409 | struct mtd_concat *concat = CONCAT(mtd); |
| 410 | int err = -EINVAL; |
| 411 | int i; |
| 412 | |
| 413 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 414 | return -EROFS; |
| 415 | |
| 416 | *retlen = 0; |
| 417 | |
| 418 | for (i = 0; i < concat->num_subdev; i++) { |
| 419 | struct mtd_info *subdev = concat->subdev[i]; |
| 420 | size_t size, retsize; |
| 421 | |
| 422 | if (to >= subdev->size) { |
| 423 | size = 0; |
| 424 | to -= subdev->size; |
| 425 | continue; |
| 426 | } |
| 427 | if (to + len > subdev->size) |
| 428 | size = subdev->size - to; |
| 429 | else |
| 430 | size = len; |
| 431 | |
| 432 | if (!(subdev->flags & MTD_WRITEABLE)) |
| 433 | err = -EROFS; |
| 434 | else if (subdev->write_oob) |
| 435 | err = subdev->write_oob(subdev, to, size, &retsize, |
| 436 | buf); |
| 437 | else |
| 438 | err = -EINVAL; |
| 439 | |
| 440 | if (err) |
| 441 | break; |
| 442 | |
| 443 | *retlen += retsize; |
| 444 | len -= size; |
| 445 | if (len == 0) |
| 446 | break; |
| 447 | |
| 448 | err = -EINVAL; |
| 449 | buf += size; |
| 450 | to = 0; |
| 451 | } |
| 452 | return err; |
| 453 | } |
| 454 | |
| 455 | static void concat_erase_callback(struct erase_info *instr) |
| 456 | { |
| 457 | wake_up((wait_queue_head_t *) instr->priv); |
| 458 | } |
| 459 | |
| 460 | static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase) |
| 461 | { |
| 462 | int err; |
| 463 | wait_queue_head_t waitq; |
| 464 | DECLARE_WAITQUEUE(wait, current); |
| 465 | |
| 466 | /* |
| 467 | * This code was stol^H^H^H^Hinspired by mtdchar.c |
| 468 | */ |
| 469 | init_waitqueue_head(&waitq); |
| 470 | |
| 471 | erase->mtd = mtd; |
| 472 | erase->callback = concat_erase_callback; |
| 473 | erase->priv = (unsigned long) &waitq; |
| 474 | |
| 475 | /* |
| 476 | * FIXME: Allow INTERRUPTIBLE. Which means |
| 477 | * not having the wait_queue head on the stack. |
| 478 | */ |
| 479 | err = mtd->erase(mtd, erase); |
| 480 | if (!err) { |
| 481 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 482 | add_wait_queue(&waitq, &wait); |
| 483 | if (erase->state != MTD_ERASE_DONE |
| 484 | && erase->state != MTD_ERASE_FAILED) |
| 485 | schedule(); |
| 486 | remove_wait_queue(&waitq, &wait); |
| 487 | set_current_state(TASK_RUNNING); |
| 488 | |
| 489 | err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0; |
| 490 | } |
| 491 | return err; |
| 492 | } |
| 493 | |
| 494 | static int concat_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 495 | { |
| 496 | struct mtd_concat *concat = CONCAT(mtd); |
| 497 | struct mtd_info *subdev; |
| 498 | int i, err; |
| 499 | u_int32_t length, offset = 0; |
| 500 | struct erase_info *erase; |
| 501 | |
| 502 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 503 | return -EROFS; |
| 504 | |
| 505 | if (instr->addr > concat->mtd.size) |
| 506 | return -EINVAL; |
| 507 | |
| 508 | if (instr->len + instr->addr > concat->mtd.size) |
| 509 | return -EINVAL; |
| 510 | |
| 511 | /* |
| 512 | * Check for proper erase block alignment of the to-be-erased area. |
| 513 | * It is easier to do this based on the super device's erase |
| 514 | * region info rather than looking at each particular sub-device |
| 515 | * in turn. |
| 516 | */ |
| 517 | if (!concat->mtd.numeraseregions) { |
| 518 | /* the easy case: device has uniform erase block size */ |
| 519 | if (instr->addr & (concat->mtd.erasesize - 1)) |
| 520 | return -EINVAL; |
| 521 | if (instr->len & (concat->mtd.erasesize - 1)) |
| 522 | return -EINVAL; |
| 523 | } else { |
| 524 | /* device has variable erase size */ |
| 525 | struct mtd_erase_region_info *erase_regions = |
| 526 | concat->mtd.eraseregions; |
| 527 | |
| 528 | /* |
| 529 | * Find the erase region where the to-be-erased area begins: |
| 530 | */ |
| 531 | for (i = 0; i < concat->mtd.numeraseregions && |
| 532 | instr->addr >= erase_regions[i].offset; i++) ; |
| 533 | --i; |
| 534 | |
| 535 | /* |
| 536 | * Now erase_regions[i] is the region in which the |
| 537 | * to-be-erased area begins. Verify that the starting |
| 538 | * offset is aligned to this region's erase size: |
| 539 | */ |
| 540 | if (instr->addr & (erase_regions[i].erasesize - 1)) |
| 541 | return -EINVAL; |
| 542 | |
| 543 | /* |
| 544 | * now find the erase region where the to-be-erased area ends: |
| 545 | */ |
| 546 | for (; i < concat->mtd.numeraseregions && |
| 547 | (instr->addr + instr->len) >= erase_regions[i].offset; |
| 548 | ++i) ; |
| 549 | --i; |
| 550 | /* |
| 551 | * check if the ending offset is aligned to this region's erase size |
| 552 | */ |
| 553 | if ((instr->addr + instr->len) & (erase_regions[i].erasesize - |
| 554 | 1)) |
| 555 | return -EINVAL; |
| 556 | } |
| 557 | |
| 558 | instr->fail_addr = 0xffffffff; |
| 559 | |
| 560 | /* make a local copy of instr to avoid modifying the caller's struct */ |
| 561 | erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL); |
| 562 | |
| 563 | if (!erase) |
| 564 | return -ENOMEM; |
| 565 | |
| 566 | *erase = *instr; |
| 567 | length = instr->len; |
| 568 | |
| 569 | /* |
| 570 | * find the subdevice where the to-be-erased area begins, adjust |
| 571 | * starting offset to be relative to the subdevice start |
| 572 | */ |
| 573 | for (i = 0; i < concat->num_subdev; i++) { |
| 574 | subdev = concat->subdev[i]; |
| 575 | if (subdev->size <= erase->addr) { |
| 576 | erase->addr -= subdev->size; |
| 577 | offset += subdev->size; |
| 578 | } else { |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | /* must never happen since size limit has been verified above */ |
Eric Sesterhenn | 373ebfb | 2006-03-26 18:15:12 +0200 | [diff] [blame] | 584 | BUG_ON(i >= concat->num_subdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | /* now do the erase: */ |
| 587 | err = 0; |
| 588 | for (; length > 0; i++) { |
| 589 | /* loop for all subdevices affected by this request */ |
| 590 | subdev = concat->subdev[i]; /* get current subdevice */ |
| 591 | |
| 592 | /* limit length to subdevice's size: */ |
| 593 | if (erase->addr + length > subdev->size) |
| 594 | erase->len = subdev->size - erase->addr; |
| 595 | else |
| 596 | erase->len = length; |
| 597 | |
| 598 | if (!(subdev->flags & MTD_WRITEABLE)) { |
| 599 | err = -EROFS; |
| 600 | break; |
| 601 | } |
| 602 | length -= erase->len; |
| 603 | if ((err = concat_dev_erase(subdev, erase))) { |
| 604 | /* sanity check: should never happen since |
| 605 | * block alignment has been checked above */ |
Eric Sesterhenn | 373ebfb | 2006-03-26 18:15:12 +0200 | [diff] [blame] | 606 | BUG_ON(err == -EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | if (erase->fail_addr != 0xffffffff) |
| 608 | instr->fail_addr = erase->fail_addr + offset; |
| 609 | break; |
| 610 | } |
| 611 | /* |
| 612 | * erase->addr specifies the offset of the area to be |
| 613 | * erased *within the current subdevice*. It can be |
| 614 | * non-zero only the first time through this loop, i.e. |
| 615 | * for the first subdevice where blocks need to be erased. |
| 616 | * All the following erases must begin at the start of the |
| 617 | * current subdevice, i.e. at offset zero. |
| 618 | */ |
| 619 | erase->addr = 0; |
| 620 | offset += subdev->size; |
| 621 | } |
| 622 | instr->state = erase->state; |
| 623 | kfree(erase); |
| 624 | if (err) |
| 625 | return err; |
| 626 | |
| 627 | if (instr->callback) |
| 628 | instr->callback(instr); |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 633 | { |
| 634 | struct mtd_concat *concat = CONCAT(mtd); |
| 635 | int i, err = -EINVAL; |
| 636 | |
| 637 | if ((len + ofs) > mtd->size) |
| 638 | return -EINVAL; |
| 639 | |
| 640 | for (i = 0; i < concat->num_subdev; i++) { |
| 641 | struct mtd_info *subdev = concat->subdev[i]; |
| 642 | size_t size; |
| 643 | |
| 644 | if (ofs >= subdev->size) { |
| 645 | size = 0; |
| 646 | ofs -= subdev->size; |
| 647 | continue; |
| 648 | } |
| 649 | if (ofs + len > subdev->size) |
| 650 | size = subdev->size - ofs; |
| 651 | else |
| 652 | size = len; |
| 653 | |
| 654 | err = subdev->lock(subdev, ofs, size); |
| 655 | |
| 656 | if (err) |
| 657 | break; |
| 658 | |
| 659 | len -= size; |
| 660 | if (len == 0) |
| 661 | break; |
| 662 | |
| 663 | err = -EINVAL; |
| 664 | ofs = 0; |
| 665 | } |
| 666 | |
| 667 | return err; |
| 668 | } |
| 669 | |
| 670 | static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 671 | { |
| 672 | struct mtd_concat *concat = CONCAT(mtd); |
| 673 | int i, err = 0; |
| 674 | |
| 675 | if ((len + ofs) > mtd->size) |
| 676 | return -EINVAL; |
| 677 | |
| 678 | for (i = 0; i < concat->num_subdev; i++) { |
| 679 | struct mtd_info *subdev = concat->subdev[i]; |
| 680 | size_t size; |
| 681 | |
| 682 | if (ofs >= subdev->size) { |
| 683 | size = 0; |
| 684 | ofs -= subdev->size; |
| 685 | continue; |
| 686 | } |
| 687 | if (ofs + len > subdev->size) |
| 688 | size = subdev->size - ofs; |
| 689 | else |
| 690 | size = len; |
| 691 | |
| 692 | err = subdev->unlock(subdev, ofs, size); |
| 693 | |
| 694 | if (err) |
| 695 | break; |
| 696 | |
| 697 | len -= size; |
| 698 | if (len == 0) |
| 699 | break; |
| 700 | |
| 701 | err = -EINVAL; |
| 702 | ofs = 0; |
| 703 | } |
| 704 | |
| 705 | return err; |
| 706 | } |
| 707 | |
| 708 | static void concat_sync(struct mtd_info *mtd) |
| 709 | { |
| 710 | struct mtd_concat *concat = CONCAT(mtd); |
| 711 | int i; |
| 712 | |
| 713 | for (i = 0; i < concat->num_subdev; i++) { |
| 714 | struct mtd_info *subdev = concat->subdev[i]; |
| 715 | subdev->sync(subdev); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | static int concat_suspend(struct mtd_info *mtd) |
| 720 | { |
| 721 | struct mtd_concat *concat = CONCAT(mtd); |
| 722 | int i, rc = 0; |
| 723 | |
| 724 | for (i = 0; i < concat->num_subdev; i++) { |
| 725 | struct mtd_info *subdev = concat->subdev[i]; |
| 726 | if ((rc = subdev->suspend(subdev)) < 0) |
| 727 | return rc; |
| 728 | } |
| 729 | return rc; |
| 730 | } |
| 731 | |
| 732 | static void concat_resume(struct mtd_info *mtd) |
| 733 | { |
| 734 | struct mtd_concat *concat = CONCAT(mtd); |
| 735 | int i; |
| 736 | |
| 737 | for (i = 0; i < concat->num_subdev; i++) { |
| 738 | struct mtd_info *subdev = concat->subdev[i]; |
| 739 | subdev->resume(subdev); |
| 740 | } |
| 741 | } |
| 742 | |
Alexander Belyakov | e8d3293 | 2006-05-17 19:11:16 +0400 | [diff] [blame] | 743 | static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs) |
| 744 | { |
| 745 | struct mtd_concat *concat = CONCAT(mtd); |
| 746 | int i, res = 0; |
| 747 | |
| 748 | if (!concat->subdev[0]->block_isbad) |
| 749 | return res; |
| 750 | |
| 751 | if (ofs > mtd->size) |
| 752 | return -EINVAL; |
| 753 | |
| 754 | for (i = 0; i < concat->num_subdev; i++) { |
| 755 | struct mtd_info *subdev = concat->subdev[i]; |
| 756 | |
| 757 | if (ofs >= subdev->size) { |
| 758 | ofs -= subdev->size; |
| 759 | continue; |
| 760 | } |
| 761 | |
| 762 | res = subdev->block_isbad(subdev, ofs); |
| 763 | break; |
| 764 | } |
| 765 | |
| 766 | return res; |
| 767 | } |
| 768 | |
| 769 | static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 770 | { |
| 771 | struct mtd_concat *concat = CONCAT(mtd); |
| 772 | int i, err = -EINVAL; |
| 773 | |
| 774 | if (!concat->subdev[0]->block_markbad) |
| 775 | return 0; |
| 776 | |
| 777 | if (ofs > mtd->size) |
| 778 | return -EINVAL; |
| 779 | |
| 780 | for (i = 0; i < concat->num_subdev; i++) { |
| 781 | struct mtd_info *subdev = concat->subdev[i]; |
| 782 | |
| 783 | if (ofs >= subdev->size) { |
| 784 | ofs -= subdev->size; |
| 785 | continue; |
| 786 | } |
| 787 | |
| 788 | err = subdev->block_markbad(subdev, ofs); |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | return err; |
| 793 | } |
| 794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | /* |
| 796 | * This function constructs a virtual MTD device by concatenating |
| 797 | * num_devs MTD devices. A pointer to the new device object is |
| 798 | * stored to *new_dev upon success. This function does _not_ |
| 799 | * register any devices: this is the caller's responsibility. |
| 800 | */ |
| 801 | struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */ |
| 802 | int num_devs, /* number of subdevices */ |
| 803 | char *name) |
| 804 | { /* name for the new device */ |
| 805 | int i; |
| 806 | size_t size; |
| 807 | struct mtd_concat *concat; |
| 808 | u_int32_t max_erasesize, curr_erasesize; |
| 809 | int num_erase_region; |
| 810 | |
| 811 | printk(KERN_NOTICE "Concatenating MTD devices:\n"); |
| 812 | for (i = 0; i < num_devs; i++) |
| 813 | printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name); |
| 814 | printk(KERN_NOTICE "into device \"%s\"\n", name); |
| 815 | |
| 816 | /* allocate the device structure */ |
| 817 | size = SIZEOF_STRUCT_MTD_CONCAT(num_devs); |
| 818 | concat = kmalloc(size, GFP_KERNEL); |
| 819 | if (!concat) { |
| 820 | printk |
| 821 | ("memory allocation error while creating concatenated device \"%s\"\n", |
| 822 | name); |
| 823 | return NULL; |
| 824 | } |
| 825 | memset(concat, 0, size); |
| 826 | concat->subdev = (struct mtd_info **) (concat + 1); |
| 827 | |
| 828 | /* |
| 829 | * Set up the new "super" device's MTD object structure, check for |
| 830 | * incompatibilites between the subdevices. |
| 831 | */ |
| 832 | concat->mtd.type = subdev[0]->type; |
| 833 | concat->mtd.flags = subdev[0]->flags; |
| 834 | concat->mtd.size = subdev[0]->size; |
| 835 | concat->mtd.erasesize = subdev[0]->erasesize; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame^] | 836 | concat->mtd.writesize = subdev[0]->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | concat->mtd.oobsize = subdev[0]->oobsize; |
| 838 | concat->mtd.ecctype = subdev[0]->ecctype; |
| 839 | concat->mtd.eccsize = subdev[0]->eccsize; |
| 840 | if (subdev[0]->read_ecc) |
| 841 | concat->mtd.read_ecc = concat_read_ecc; |
| 842 | if (subdev[0]->write_ecc) |
| 843 | concat->mtd.write_ecc = concat_write_ecc; |
Alexander Belyakov | e8d3293 | 2006-05-17 19:11:16 +0400 | [diff] [blame] | 844 | if (subdev[0]->writev) |
| 845 | concat->mtd.writev = concat_writev; |
| 846 | if (subdev[0]->writev_ecc) |
| 847 | concat->mtd.writev_ecc = concat_writev_ecc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | if (subdev[0]->read_oob) |
| 849 | concat->mtd.read_oob = concat_read_oob; |
| 850 | if (subdev[0]->write_oob) |
| 851 | concat->mtd.write_oob = concat_write_oob; |
Alexander Belyakov | e8d3293 | 2006-05-17 19:11:16 +0400 | [diff] [blame] | 852 | if (subdev[0]->block_isbad) |
| 853 | concat->mtd.block_isbad = concat_block_isbad; |
| 854 | if (subdev[0]->block_markbad) |
| 855 | concat->mtd.block_markbad = concat_block_markbad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
| 857 | concat->subdev[0] = subdev[0]; |
| 858 | |
| 859 | for (i = 1; i < num_devs; i++) { |
| 860 | if (concat->mtd.type != subdev[i]->type) { |
| 861 | kfree(concat); |
| 862 | printk("Incompatible device type on \"%s\"\n", |
| 863 | subdev[i]->name); |
| 864 | return NULL; |
| 865 | } |
| 866 | if (concat->mtd.flags != subdev[i]->flags) { |
| 867 | /* |
| 868 | * Expect all flags except MTD_WRITEABLE to be |
| 869 | * equal on all subdevices. |
| 870 | */ |
| 871 | if ((concat->mtd.flags ^ subdev[i]-> |
| 872 | flags) & ~MTD_WRITEABLE) { |
| 873 | kfree(concat); |
| 874 | printk("Incompatible device flags on \"%s\"\n", |
| 875 | subdev[i]->name); |
| 876 | return NULL; |
| 877 | } else |
| 878 | /* if writeable attribute differs, |
| 879 | make super device writeable */ |
| 880 | concat->mtd.flags |= |
| 881 | subdev[i]->flags & MTD_WRITEABLE; |
| 882 | } |
| 883 | concat->mtd.size += subdev[i]->size; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame^] | 884 | if (concat->mtd.writesize != subdev[i]->writesize || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | concat->mtd.oobsize != subdev[i]->oobsize || |
| 886 | concat->mtd.ecctype != subdev[i]->ecctype || |
| 887 | concat->mtd.eccsize != subdev[i]->eccsize || |
| 888 | !concat->mtd.read_ecc != !subdev[i]->read_ecc || |
| 889 | !concat->mtd.write_ecc != !subdev[i]->write_ecc || |
| 890 | !concat->mtd.read_oob != !subdev[i]->read_oob || |
| 891 | !concat->mtd.write_oob != !subdev[i]->write_oob) { |
| 892 | kfree(concat); |
| 893 | printk("Incompatible OOB or ECC data on \"%s\"\n", |
| 894 | subdev[i]->name); |
| 895 | return NULL; |
| 896 | } |
| 897 | concat->subdev[i] = subdev[i]; |
| 898 | |
| 899 | } |
| 900 | |
Alexander Belyakov | e8d3293 | 2006-05-17 19:11:16 +0400 | [diff] [blame] | 901 | if(concat->mtd.type == MTD_NANDFLASH) |
| 902 | memcpy(&concat->mtd.oobinfo, &subdev[0]->oobinfo, |
| 903 | sizeof(struct nand_oobinfo)); |
| 904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | concat->num_subdev = num_devs; |
| 906 | concat->mtd.name = name; |
| 907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | concat->mtd.erase = concat_erase; |
| 909 | concat->mtd.read = concat_read; |
| 910 | concat->mtd.write = concat_write; |
| 911 | concat->mtd.sync = concat_sync; |
| 912 | concat->mtd.lock = concat_lock; |
| 913 | concat->mtd.unlock = concat_unlock; |
| 914 | concat->mtd.suspend = concat_suspend; |
| 915 | concat->mtd.resume = concat_resume; |
| 916 | |
| 917 | /* |
| 918 | * Combine the erase block size info of the subdevices: |
| 919 | * |
| 920 | * first, walk the map of the new device and see how |
| 921 | * many changes in erase size we have |
| 922 | */ |
| 923 | max_erasesize = curr_erasesize = subdev[0]->erasesize; |
| 924 | num_erase_region = 1; |
| 925 | for (i = 0; i < num_devs; i++) { |
| 926 | if (subdev[i]->numeraseregions == 0) { |
| 927 | /* current subdevice has uniform erase size */ |
| 928 | if (subdev[i]->erasesize != curr_erasesize) { |
| 929 | /* if it differs from the last subdevice's erase size, count it */ |
| 930 | ++num_erase_region; |
| 931 | curr_erasesize = subdev[i]->erasesize; |
| 932 | if (curr_erasesize > max_erasesize) |
| 933 | max_erasesize = curr_erasesize; |
| 934 | } |
| 935 | } else { |
| 936 | /* current subdevice has variable erase size */ |
| 937 | int j; |
| 938 | for (j = 0; j < subdev[i]->numeraseregions; j++) { |
| 939 | |
| 940 | /* walk the list of erase regions, count any changes */ |
| 941 | if (subdev[i]->eraseregions[j].erasesize != |
| 942 | curr_erasesize) { |
| 943 | ++num_erase_region; |
| 944 | curr_erasesize = |
| 945 | subdev[i]->eraseregions[j]. |
| 946 | erasesize; |
| 947 | if (curr_erasesize > max_erasesize) |
| 948 | max_erasesize = curr_erasesize; |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | if (num_erase_region == 1) { |
| 955 | /* |
| 956 | * All subdevices have the same uniform erase size. |
| 957 | * This is easy: |
| 958 | */ |
| 959 | concat->mtd.erasesize = curr_erasesize; |
| 960 | concat->mtd.numeraseregions = 0; |
| 961 | } else { |
| 962 | /* |
| 963 | * erase block size varies across the subdevices: allocate |
| 964 | * space to store the data describing the variable erase regions |
| 965 | */ |
| 966 | struct mtd_erase_region_info *erase_region_p; |
| 967 | u_int32_t begin, position; |
| 968 | |
| 969 | concat->mtd.erasesize = max_erasesize; |
| 970 | concat->mtd.numeraseregions = num_erase_region; |
| 971 | concat->mtd.eraseregions = erase_region_p = |
| 972 | kmalloc(num_erase_region * |
| 973 | sizeof (struct mtd_erase_region_info), GFP_KERNEL); |
| 974 | if (!erase_region_p) { |
| 975 | kfree(concat); |
| 976 | printk |
| 977 | ("memory allocation error while creating erase region list" |
| 978 | " for device \"%s\"\n", name); |
| 979 | return NULL; |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * walk the map of the new device once more and fill in |
| 984 | * in erase region info: |
| 985 | */ |
| 986 | curr_erasesize = subdev[0]->erasesize; |
| 987 | begin = position = 0; |
| 988 | for (i = 0; i < num_devs; i++) { |
| 989 | if (subdev[i]->numeraseregions == 0) { |
| 990 | /* current subdevice has uniform erase size */ |
| 991 | if (subdev[i]->erasesize != curr_erasesize) { |
| 992 | /* |
| 993 | * fill in an mtd_erase_region_info structure for the area |
| 994 | * we have walked so far: |
| 995 | */ |
| 996 | erase_region_p->offset = begin; |
| 997 | erase_region_p->erasesize = |
| 998 | curr_erasesize; |
| 999 | erase_region_p->numblocks = |
| 1000 | (position - begin) / curr_erasesize; |
| 1001 | begin = position; |
| 1002 | |
| 1003 | curr_erasesize = subdev[i]->erasesize; |
| 1004 | ++erase_region_p; |
| 1005 | } |
| 1006 | position += subdev[i]->size; |
| 1007 | } else { |
| 1008 | /* current subdevice has variable erase size */ |
| 1009 | int j; |
| 1010 | for (j = 0; j < subdev[i]->numeraseregions; j++) { |
| 1011 | /* walk the list of erase regions, count any changes */ |
| 1012 | if (subdev[i]->eraseregions[j]. |
| 1013 | erasesize != curr_erasesize) { |
| 1014 | erase_region_p->offset = begin; |
| 1015 | erase_region_p->erasesize = |
| 1016 | curr_erasesize; |
| 1017 | erase_region_p->numblocks = |
| 1018 | (position - |
| 1019 | begin) / curr_erasesize; |
| 1020 | begin = position; |
| 1021 | |
| 1022 | curr_erasesize = |
| 1023 | subdev[i]->eraseregions[j]. |
| 1024 | erasesize; |
| 1025 | ++erase_region_p; |
| 1026 | } |
| 1027 | position += |
| 1028 | subdev[i]->eraseregions[j]. |
| 1029 | numblocks * curr_erasesize; |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | /* Now write the final entry */ |
| 1034 | erase_region_p->offset = begin; |
| 1035 | erase_region_p->erasesize = curr_erasesize; |
| 1036 | erase_region_p->numblocks = (position - begin) / curr_erasesize; |
| 1037 | } |
| 1038 | |
| 1039 | return &concat->mtd; |
| 1040 | } |
| 1041 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 1042 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | * This function destroys an MTD object obtained from concat_mtd_devs() |
| 1044 | */ |
| 1045 | |
| 1046 | void mtd_concat_destroy(struct mtd_info *mtd) |
| 1047 | { |
| 1048 | struct mtd_concat *concat = CONCAT(mtd); |
| 1049 | if (concat->mtd.numeraseregions) |
| 1050 | kfree(concat->mtd.eraseregions); |
| 1051 | kfree(concat); |
| 1052 | } |
| 1053 | |
| 1054 | EXPORT_SYMBOL(mtd_concat_create); |
| 1055 | EXPORT_SYMBOL(mtd_concat_destroy); |
| 1056 | |
| 1057 | MODULE_LICENSE("GPL"); |
| 1058 | MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>"); |
| 1059 | MODULE_DESCRIPTION("Generic support for concatenating of MTD devices"); |