PageRenderTime 67ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/memstick/core/ms_block.c

http://github.com/torvalds/linux
C | 2362 lines | 1745 code | 482 blank | 135 comment | 311 complexity | 3a44c91cfff22d5cf63a289b6105adc2 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ms_block.c - Sony MemoryStick (legacy) storage support
  4. * Copyright (C) 2013 Maxim Levitsky <maximlevitsky@gmail.com>
  5. *
  6. * Minor portions of the driver were copied from mspro_block.c which is
  7. * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
  8. */
  9. #define DRIVER_NAME "ms_block"
  10. #define pr_fmt(fmt) DRIVER_NAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/blk-mq.h>
  13. #include <linux/memstick.h>
  14. #include <linux/idr.h>
  15. #include <linux/hdreg.h>
  16. #include <linux/delay.h>
  17. #include <linux/slab.h>
  18. #include <linux/random.h>
  19. #include <linux/bitmap.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/mutex.h>
  24. #include "ms_block.h"
  25. static int debug;
  26. static int cache_flush_timeout = 1000;
  27. static bool verify_writes;
  28. /*
  29. * Copies section of 'sg_from' starting from offset 'offset' and with length
  30. * 'len' To another scatterlist of to_nents enties
  31. */
  32. static size_t msb_sg_copy(struct scatterlist *sg_from,
  33. struct scatterlist *sg_to, int to_nents, size_t offset, size_t len)
  34. {
  35. size_t copied = 0;
  36. while (offset > 0) {
  37. if (offset >= sg_from->length) {
  38. if (sg_is_last(sg_from))
  39. return 0;
  40. offset -= sg_from->length;
  41. sg_from = sg_next(sg_from);
  42. continue;
  43. }
  44. copied = min(len, sg_from->length - offset);
  45. sg_set_page(sg_to, sg_page(sg_from),
  46. copied, sg_from->offset + offset);
  47. len -= copied;
  48. offset = 0;
  49. if (sg_is_last(sg_from) || !len)
  50. goto out;
  51. sg_to = sg_next(sg_to);
  52. to_nents--;
  53. sg_from = sg_next(sg_from);
  54. }
  55. while (len > sg_from->length && to_nents--) {
  56. len -= sg_from->length;
  57. copied += sg_from->length;
  58. sg_set_page(sg_to, sg_page(sg_from),
  59. sg_from->length, sg_from->offset);
  60. if (sg_is_last(sg_from) || !len)
  61. goto out;
  62. sg_from = sg_next(sg_from);
  63. sg_to = sg_next(sg_to);
  64. }
  65. if (len && to_nents) {
  66. sg_set_page(sg_to, sg_page(sg_from), len, sg_from->offset);
  67. copied += len;
  68. }
  69. out:
  70. sg_mark_end(sg_to);
  71. return copied;
  72. }
  73. /*
  74. * Compares section of 'sg' starting from offset 'offset' and with length 'len'
  75. * to linear buffer of length 'len' at address 'buffer'
  76. * Returns 0 if equal and -1 otherwice
  77. */
  78. static int msb_sg_compare_to_buffer(struct scatterlist *sg,
  79. size_t offset, u8 *buffer, size_t len)
  80. {
  81. int retval = 0, cmplen;
  82. struct sg_mapping_iter miter;
  83. sg_miter_start(&miter, sg, sg_nents(sg),
  84. SG_MITER_ATOMIC | SG_MITER_FROM_SG);
  85. while (sg_miter_next(&miter) && len > 0) {
  86. if (offset >= miter.length) {
  87. offset -= miter.length;
  88. continue;
  89. }
  90. cmplen = min(miter.length - offset, len);
  91. retval = memcmp(miter.addr + offset, buffer, cmplen) ? -1 : 0;
  92. if (retval)
  93. break;
  94. buffer += cmplen;
  95. len -= cmplen;
  96. offset = 0;
  97. }
  98. if (!retval && len)
  99. retval = -1;
  100. sg_miter_stop(&miter);
  101. return retval;
  102. }
  103. /* Get zone at which block with logical address 'lba' lives
  104. * Flash is broken into zones.
  105. * Each zone consists of 512 eraseblocks, out of which in first
  106. * zone 494 are used and 496 are for all following zones.
  107. * Therefore zone #0 hosts blocks 0-493, zone #1 blocks 494-988, etc...
  108. */
  109. static int msb_get_zone_from_lba(int lba)
  110. {
  111. if (lba < 494)
  112. return 0;
  113. return ((lba - 494) / 496) + 1;
  114. }
  115. /* Get zone of physical block. Trivial */
  116. static int msb_get_zone_from_pba(int pba)
  117. {
  118. return pba / MS_BLOCKS_IN_ZONE;
  119. }
  120. /* Debug test to validate free block counts */
  121. static int msb_validate_used_block_bitmap(struct msb_data *msb)
  122. {
  123. int total_free_blocks = 0;
  124. int i;
  125. if (!debug)
  126. return 0;
  127. for (i = 0; i < msb->zone_count; i++)
  128. total_free_blocks += msb->free_block_count[i];
  129. if (msb->block_count - bitmap_weight(msb->used_blocks_bitmap,
  130. msb->block_count) == total_free_blocks)
  131. return 0;
  132. pr_err("BUG: free block counts don't match the bitmap");
  133. msb->read_only = true;
  134. return -EINVAL;
  135. }
  136. /* Mark physical block as used */
  137. static void msb_mark_block_used(struct msb_data *msb, int pba)
  138. {
  139. int zone = msb_get_zone_from_pba(pba);
  140. if (test_bit(pba, msb->used_blocks_bitmap)) {
  141. pr_err(
  142. "BUG: attempt to mark already used pba %d as used", pba);
  143. msb->read_only = true;
  144. return;
  145. }
  146. if (msb_validate_used_block_bitmap(msb))
  147. return;
  148. /* No races because all IO is single threaded */
  149. __set_bit(pba, msb->used_blocks_bitmap);
  150. msb->free_block_count[zone]--;
  151. }
  152. /* Mark physical block as free */
  153. static void msb_mark_block_unused(struct msb_data *msb, int pba)
  154. {
  155. int zone = msb_get_zone_from_pba(pba);
  156. if (!test_bit(pba, msb->used_blocks_bitmap)) {
  157. pr_err("BUG: attempt to mark already unused pba %d as unused" , pba);
  158. msb->read_only = true;
  159. return;
  160. }
  161. if (msb_validate_used_block_bitmap(msb))
  162. return;
  163. /* No races because all IO is single threaded */
  164. __clear_bit(pba, msb->used_blocks_bitmap);
  165. msb->free_block_count[zone]++;
  166. }
  167. /* Invalidate current register window */
  168. static void msb_invalidate_reg_window(struct msb_data *msb)
  169. {
  170. msb->reg_addr.w_offset = offsetof(struct ms_register, id);
  171. msb->reg_addr.w_length = sizeof(struct ms_id_register);
  172. msb->reg_addr.r_offset = offsetof(struct ms_register, id);
  173. msb->reg_addr.r_length = sizeof(struct ms_id_register);
  174. msb->addr_valid = false;
  175. }
  176. /* Start a state machine */
  177. static int msb_run_state_machine(struct msb_data *msb, int (*state_func)
  178. (struct memstick_dev *card, struct memstick_request **req))
  179. {
  180. struct memstick_dev *card = msb->card;
  181. WARN_ON(msb->state != -1);
  182. msb->int_polling = false;
  183. msb->state = 0;
  184. msb->exit_error = 0;
  185. memset(&card->current_mrq, 0, sizeof(card->current_mrq));
  186. card->next_request = state_func;
  187. memstick_new_req(card->host);
  188. wait_for_completion(&card->mrq_complete);
  189. WARN_ON(msb->state != -1);
  190. return msb->exit_error;
  191. }
  192. /* State machines call that to exit */
  193. static int msb_exit_state_machine(struct msb_data *msb, int error)
  194. {
  195. WARN_ON(msb->state == -1);
  196. msb->state = -1;
  197. msb->exit_error = error;
  198. msb->card->next_request = h_msb_default_bad;
  199. /* Invalidate reg window on errors */
  200. if (error)
  201. msb_invalidate_reg_window(msb);
  202. complete(&msb->card->mrq_complete);
  203. return -ENXIO;
  204. }
  205. /* read INT register */
  206. static int msb_read_int_reg(struct msb_data *msb, long timeout)
  207. {
  208. struct memstick_request *mrq = &msb->card->current_mrq;
  209. WARN_ON(msb->state == -1);
  210. if (!msb->int_polling) {
  211. msb->int_timeout = jiffies +
  212. msecs_to_jiffies(timeout == -1 ? 500 : timeout);
  213. msb->int_polling = true;
  214. } else if (time_after(jiffies, msb->int_timeout)) {
  215. mrq->data[0] = MEMSTICK_INT_CMDNAK;
  216. return 0;
  217. }
  218. if ((msb->caps & MEMSTICK_CAP_AUTO_GET_INT) &&
  219. mrq->need_card_int && !mrq->error) {
  220. mrq->data[0] = mrq->int_reg;
  221. mrq->need_card_int = false;
  222. return 0;
  223. } else {
  224. memstick_init_req(mrq, MS_TPC_GET_INT, NULL, 1);
  225. return 1;
  226. }
  227. }
  228. /* Read a register */
  229. static int msb_read_regs(struct msb_data *msb, int offset, int len)
  230. {
  231. struct memstick_request *req = &msb->card->current_mrq;
  232. if (msb->reg_addr.r_offset != offset ||
  233. msb->reg_addr.r_length != len || !msb->addr_valid) {
  234. msb->reg_addr.r_offset = offset;
  235. msb->reg_addr.r_length = len;
  236. msb->addr_valid = true;
  237. memstick_init_req(req, MS_TPC_SET_RW_REG_ADRS,
  238. &msb->reg_addr, sizeof(msb->reg_addr));
  239. return 0;
  240. }
  241. memstick_init_req(req, MS_TPC_READ_REG, NULL, len);
  242. return 1;
  243. }
  244. /* Write a card register */
  245. static int msb_write_regs(struct msb_data *msb, int offset, int len, void *buf)
  246. {
  247. struct memstick_request *req = &msb->card->current_mrq;
  248. if (msb->reg_addr.w_offset != offset ||
  249. msb->reg_addr.w_length != len || !msb->addr_valid) {
  250. msb->reg_addr.w_offset = offset;
  251. msb->reg_addr.w_length = len;
  252. msb->addr_valid = true;
  253. memstick_init_req(req, MS_TPC_SET_RW_REG_ADRS,
  254. &msb->reg_addr, sizeof(msb->reg_addr));
  255. return 0;
  256. }
  257. memstick_init_req(req, MS_TPC_WRITE_REG, buf, len);
  258. return 1;
  259. }
  260. /* Handler for absence of IO */
  261. static int h_msb_default_bad(struct memstick_dev *card,
  262. struct memstick_request **mrq)
  263. {
  264. return -ENXIO;
  265. }
  266. /*
  267. * This function is a handler for reads of one page from device.
  268. * Writes output to msb->current_sg, takes sector address from msb->reg.param
  269. * Can also be used to read extra data only. Set params accordintly.
  270. */
  271. static int h_msb_read_page(struct memstick_dev *card,
  272. struct memstick_request **out_mrq)
  273. {
  274. struct msb_data *msb = memstick_get_drvdata(card);
  275. struct memstick_request *mrq = *out_mrq = &card->current_mrq;
  276. struct scatterlist sg[2];
  277. u8 command, intreg;
  278. if (mrq->error) {
  279. dbg("read_page, unknown error");
  280. return msb_exit_state_machine(msb, mrq->error);
  281. }
  282. again:
  283. switch (msb->state) {
  284. case MSB_RP_SEND_BLOCK_ADDRESS:
  285. /* msb_write_regs sometimes "fails" because it needs to update
  286. the reg window, and thus it returns request for that.
  287. Then we stay in this state and retry */
  288. if (!msb_write_regs(msb,
  289. offsetof(struct ms_register, param),
  290. sizeof(struct ms_param_register),
  291. (unsigned char *)&msb->regs.param))
  292. return 0;
  293. msb->state = MSB_RP_SEND_READ_COMMAND;
  294. return 0;
  295. case MSB_RP_SEND_READ_COMMAND:
  296. command = MS_CMD_BLOCK_READ;
  297. memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
  298. msb->state = MSB_RP_SEND_INT_REQ;
  299. return 0;
  300. case MSB_RP_SEND_INT_REQ:
  301. msb->state = MSB_RP_RECEIVE_INT_REQ_RESULT;
  302. /* If dont actually need to send the int read request (only in
  303. serial mode), then just fall through */
  304. if (msb_read_int_reg(msb, -1))
  305. return 0;
  306. /* fallthrough */
  307. case MSB_RP_RECEIVE_INT_REQ_RESULT:
  308. intreg = mrq->data[0];
  309. msb->regs.status.interrupt = intreg;
  310. if (intreg & MEMSTICK_INT_CMDNAK)
  311. return msb_exit_state_machine(msb, -EIO);
  312. if (!(intreg & MEMSTICK_INT_CED)) {
  313. msb->state = MSB_RP_SEND_INT_REQ;
  314. goto again;
  315. }
  316. msb->int_polling = false;
  317. msb->state = (intreg & MEMSTICK_INT_ERR) ?
  318. MSB_RP_SEND_READ_STATUS_REG : MSB_RP_SEND_OOB_READ;
  319. goto again;
  320. case MSB_RP_SEND_READ_STATUS_REG:
  321. /* read the status register to understand source of the INT_ERR */
  322. if (!msb_read_regs(msb,
  323. offsetof(struct ms_register, status),
  324. sizeof(struct ms_status_register)))
  325. return 0;
  326. msb->state = MSB_RP_RECEIVE_STATUS_REG;
  327. return 0;
  328. case MSB_RP_RECEIVE_STATUS_REG:
  329. msb->regs.status = *(struct ms_status_register *)mrq->data;
  330. msb->state = MSB_RP_SEND_OOB_READ;
  331. /* fallthrough */
  332. case MSB_RP_SEND_OOB_READ:
  333. if (!msb_read_regs(msb,
  334. offsetof(struct ms_register, extra_data),
  335. sizeof(struct ms_extra_data_register)))
  336. return 0;
  337. msb->state = MSB_RP_RECEIVE_OOB_READ;
  338. return 0;
  339. case MSB_RP_RECEIVE_OOB_READ:
  340. msb->regs.extra_data =
  341. *(struct ms_extra_data_register *) mrq->data;
  342. msb->state = MSB_RP_SEND_READ_DATA;
  343. /* fallthrough */
  344. case MSB_RP_SEND_READ_DATA:
  345. /* Skip that state if we only read the oob */
  346. if (msb->regs.param.cp == MEMSTICK_CP_EXTRA) {
  347. msb->state = MSB_RP_RECEIVE_READ_DATA;
  348. goto again;
  349. }
  350. sg_init_table(sg, ARRAY_SIZE(sg));
  351. msb_sg_copy(msb->current_sg, sg, ARRAY_SIZE(sg),
  352. msb->current_sg_offset,
  353. msb->page_size);
  354. memstick_init_req_sg(mrq, MS_TPC_READ_LONG_DATA, sg);
  355. msb->state = MSB_RP_RECEIVE_READ_DATA;
  356. return 0;
  357. case MSB_RP_RECEIVE_READ_DATA:
  358. if (!(msb->regs.status.interrupt & MEMSTICK_INT_ERR)) {
  359. msb->current_sg_offset += msb->page_size;
  360. return msb_exit_state_machine(msb, 0);
  361. }
  362. if (msb->regs.status.status1 & MEMSTICK_UNCORR_ERROR) {
  363. dbg("read_page: uncorrectable error");
  364. return msb_exit_state_machine(msb, -EBADMSG);
  365. }
  366. if (msb->regs.status.status1 & MEMSTICK_CORR_ERROR) {
  367. dbg("read_page: correctable error");
  368. msb->current_sg_offset += msb->page_size;
  369. return msb_exit_state_machine(msb, -EUCLEAN);
  370. } else {
  371. dbg("read_page: INT error, but no status error bits");
  372. return msb_exit_state_machine(msb, -EIO);
  373. }
  374. }
  375. BUG();
  376. }
  377. /*
  378. * Handler of writes of exactly one block.
  379. * Takes address from msb->regs.param.
  380. * Writes same extra data to blocks, also taken
  381. * from msb->regs.extra
  382. * Returns -EBADMSG if write fails due to uncorrectable error, or -EIO if
  383. * device refuses to take the command or something else
  384. */
  385. static int h_msb_write_block(struct memstick_dev *card,
  386. struct memstick_request **out_mrq)
  387. {
  388. struct msb_data *msb = memstick_get_drvdata(card);
  389. struct memstick_request *mrq = *out_mrq = &card->current_mrq;
  390. struct scatterlist sg[2];
  391. u8 intreg, command;
  392. if (mrq->error)
  393. return msb_exit_state_machine(msb, mrq->error);
  394. again:
  395. switch (msb->state) {
  396. /* HACK: Jmicon handling of TPCs between 8 and
  397. * sizeof(memstick_request.data) is broken due to hardware
  398. * bug in PIO mode that is used for these TPCs
  399. * Therefore split the write
  400. */
  401. case MSB_WB_SEND_WRITE_PARAMS:
  402. if (!msb_write_regs(msb,
  403. offsetof(struct ms_register, param),
  404. sizeof(struct ms_param_register),
  405. &msb->regs.param))
  406. return 0;
  407. msb->state = MSB_WB_SEND_WRITE_OOB;
  408. return 0;
  409. case MSB_WB_SEND_WRITE_OOB:
  410. if (!msb_write_regs(msb,
  411. offsetof(struct ms_register, extra_data),
  412. sizeof(struct ms_extra_data_register),
  413. &msb->regs.extra_data))
  414. return 0;
  415. msb->state = MSB_WB_SEND_WRITE_COMMAND;
  416. return 0;
  417. case MSB_WB_SEND_WRITE_COMMAND:
  418. command = MS_CMD_BLOCK_WRITE;
  419. memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
  420. msb->state = MSB_WB_SEND_INT_REQ;
  421. return 0;
  422. case MSB_WB_SEND_INT_REQ:
  423. msb->state = MSB_WB_RECEIVE_INT_REQ;
  424. if (msb_read_int_reg(msb, -1))
  425. return 0;
  426. /* fallthrough */
  427. case MSB_WB_RECEIVE_INT_REQ:
  428. intreg = mrq->data[0];
  429. msb->regs.status.interrupt = intreg;
  430. /* errors mean out of here, and fast... */
  431. if (intreg & (MEMSTICK_INT_CMDNAK))
  432. return msb_exit_state_machine(msb, -EIO);
  433. if (intreg & MEMSTICK_INT_ERR)
  434. return msb_exit_state_machine(msb, -EBADMSG);
  435. /* for last page we need to poll CED */
  436. if (msb->current_page == msb->pages_in_block) {
  437. if (intreg & MEMSTICK_INT_CED)
  438. return msb_exit_state_machine(msb, 0);
  439. msb->state = MSB_WB_SEND_INT_REQ;
  440. goto again;
  441. }
  442. /* for non-last page we need BREQ before writing next chunk */
  443. if (!(intreg & MEMSTICK_INT_BREQ)) {
  444. msb->state = MSB_WB_SEND_INT_REQ;
  445. goto again;
  446. }
  447. msb->int_polling = false;
  448. msb->state = MSB_WB_SEND_WRITE_DATA;
  449. /* fallthrough */
  450. case MSB_WB_SEND_WRITE_DATA:
  451. sg_init_table(sg, ARRAY_SIZE(sg));
  452. if (msb_sg_copy(msb->current_sg, sg, ARRAY_SIZE(sg),
  453. msb->current_sg_offset,
  454. msb->page_size) < msb->page_size)
  455. return msb_exit_state_machine(msb, -EIO);
  456. memstick_init_req_sg(mrq, MS_TPC_WRITE_LONG_DATA, sg);
  457. mrq->need_card_int = 1;
  458. msb->state = MSB_WB_RECEIVE_WRITE_CONFIRMATION;
  459. return 0;
  460. case MSB_WB_RECEIVE_WRITE_CONFIRMATION:
  461. msb->current_page++;
  462. msb->current_sg_offset += msb->page_size;
  463. msb->state = MSB_WB_SEND_INT_REQ;
  464. goto again;
  465. default:
  466. BUG();
  467. }
  468. return 0;
  469. }
  470. /*
  471. * This function is used to send simple IO requests to device that consist
  472. * of register write + command
  473. */
  474. static int h_msb_send_command(struct memstick_dev *card,
  475. struct memstick_request **out_mrq)
  476. {
  477. struct msb_data *msb = memstick_get_drvdata(card);
  478. struct memstick_request *mrq = *out_mrq = &card->current_mrq;
  479. u8 intreg;
  480. if (mrq->error) {
  481. dbg("send_command: unknown error");
  482. return msb_exit_state_machine(msb, mrq->error);
  483. }
  484. again:
  485. switch (msb->state) {
  486. /* HACK: see h_msb_write_block */
  487. case MSB_SC_SEND_WRITE_PARAMS: /* write param register*/
  488. if (!msb_write_regs(msb,
  489. offsetof(struct ms_register, param),
  490. sizeof(struct ms_param_register),
  491. &msb->regs.param))
  492. return 0;
  493. msb->state = MSB_SC_SEND_WRITE_OOB;
  494. return 0;
  495. case MSB_SC_SEND_WRITE_OOB:
  496. if (!msb->command_need_oob) {
  497. msb->state = MSB_SC_SEND_COMMAND;
  498. goto again;
  499. }
  500. if (!msb_write_regs(msb,
  501. offsetof(struct ms_register, extra_data),
  502. sizeof(struct ms_extra_data_register),
  503. &msb->regs.extra_data))
  504. return 0;
  505. msb->state = MSB_SC_SEND_COMMAND;
  506. return 0;
  507. case MSB_SC_SEND_COMMAND:
  508. memstick_init_req(mrq, MS_TPC_SET_CMD, &msb->command_value, 1);
  509. msb->state = MSB_SC_SEND_INT_REQ;
  510. return 0;
  511. case MSB_SC_SEND_INT_REQ:
  512. msb->state = MSB_SC_RECEIVE_INT_REQ;
  513. if (msb_read_int_reg(msb, -1))
  514. return 0;
  515. /* fallthrough */
  516. case MSB_SC_RECEIVE_INT_REQ:
  517. intreg = mrq->data[0];
  518. if (intreg & MEMSTICK_INT_CMDNAK)
  519. return msb_exit_state_machine(msb, -EIO);
  520. if (intreg & MEMSTICK_INT_ERR)
  521. return msb_exit_state_machine(msb, -EBADMSG);
  522. if (!(intreg & MEMSTICK_INT_CED)) {
  523. msb->state = MSB_SC_SEND_INT_REQ;
  524. goto again;
  525. }
  526. return msb_exit_state_machine(msb, 0);
  527. }
  528. BUG();
  529. }
  530. /* Small handler for card reset */
  531. static int h_msb_reset(struct memstick_dev *card,
  532. struct memstick_request **out_mrq)
  533. {
  534. u8 command = MS_CMD_RESET;
  535. struct msb_data *msb = memstick_get_drvdata(card);
  536. struct memstick_request *mrq = *out_mrq = &card->current_mrq;
  537. if (mrq->error)
  538. return msb_exit_state_machine(msb, mrq->error);
  539. switch (msb->state) {
  540. case MSB_RS_SEND:
  541. memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
  542. mrq->need_card_int = 0;
  543. msb->state = MSB_RS_CONFIRM;
  544. return 0;
  545. case MSB_RS_CONFIRM:
  546. return msb_exit_state_machine(msb, 0);
  547. }
  548. BUG();
  549. }
  550. /* This handler is used to do serial->parallel switch */
  551. static int h_msb_parallel_switch(struct memstick_dev *card,
  552. struct memstick_request **out_mrq)
  553. {
  554. struct msb_data *msb = memstick_get_drvdata(card);
  555. struct memstick_request *mrq = *out_mrq = &card->current_mrq;
  556. struct memstick_host *host = card->host;
  557. if (mrq->error) {
  558. dbg("parallel_switch: error");
  559. msb->regs.param.system &= ~MEMSTICK_SYS_PAM;
  560. return msb_exit_state_machine(msb, mrq->error);
  561. }
  562. switch (msb->state) {
  563. case MSB_PS_SEND_SWITCH_COMMAND:
  564. /* Set the parallel interface on memstick side */
  565. msb->regs.param.system |= MEMSTICK_SYS_PAM;
  566. if (!msb_write_regs(msb,
  567. offsetof(struct ms_register, param),
  568. 1,
  569. (unsigned char *)&msb->regs.param))
  570. return 0;
  571. msb->state = MSB_PS_SWICH_HOST;
  572. return 0;
  573. case MSB_PS_SWICH_HOST:
  574. /* Set parallel interface on our side + send a dummy request
  575. to see if card responds */
  576. host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PAR4);
  577. memstick_init_req(mrq, MS_TPC_GET_INT, NULL, 1);
  578. msb->state = MSB_PS_CONFIRM;
  579. return 0;
  580. case MSB_PS_CONFIRM:
  581. return msb_exit_state_machine(msb, 0);
  582. }
  583. BUG();
  584. }
  585. static int msb_switch_to_parallel(struct msb_data *msb);
  586. /* Reset the card, to guard against hw errors beeing treated as bad blocks */
  587. static int msb_reset(struct msb_data *msb, bool full)
  588. {
  589. bool was_parallel = msb->regs.param.system & MEMSTICK_SYS_PAM;
  590. struct memstick_dev *card = msb->card;
  591. struct memstick_host *host = card->host;
  592. int error;
  593. /* Reset the card */
  594. msb->regs.param.system = MEMSTICK_SYS_BAMD;
  595. if (full) {
  596. error = host->set_param(host,
  597. MEMSTICK_POWER, MEMSTICK_POWER_OFF);
  598. if (error)
  599. goto out_error;
  600. msb_invalidate_reg_window(msb);
  601. error = host->set_param(host,
  602. MEMSTICK_POWER, MEMSTICK_POWER_ON);
  603. if (error)
  604. goto out_error;
  605. error = host->set_param(host,
  606. MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
  607. if (error) {
  608. out_error:
  609. dbg("Failed to reset the host controller");
  610. msb->read_only = true;
  611. return -EFAULT;
  612. }
  613. }
  614. error = msb_run_state_machine(msb, h_msb_reset);
  615. if (error) {
  616. dbg("Failed to reset the card");
  617. msb->read_only = true;
  618. return -ENODEV;
  619. }
  620. /* Set parallel mode */
  621. if (was_parallel)
  622. msb_switch_to_parallel(msb);
  623. return 0;
  624. }
  625. /* Attempts to switch interface to parallel mode */
  626. static int msb_switch_to_parallel(struct msb_data *msb)
  627. {
  628. int error;
  629. error = msb_run_state_machine(msb, h_msb_parallel_switch);
  630. if (error) {
  631. pr_err("Switch to parallel failed");
  632. msb->regs.param.system &= ~MEMSTICK_SYS_PAM;
  633. msb_reset(msb, true);
  634. return -EFAULT;
  635. }
  636. msb->caps |= MEMSTICK_CAP_AUTO_GET_INT;
  637. return 0;
  638. }
  639. /* Changes overwrite flag on a page */
  640. static int msb_set_overwrite_flag(struct msb_data *msb,
  641. u16 pba, u8 page, u8 flag)
  642. {
  643. if (msb->read_only)
  644. return -EROFS;
  645. msb->regs.param.block_address = cpu_to_be16(pba);
  646. msb->regs.param.page_address = page;
  647. msb->regs.param.cp = MEMSTICK_CP_OVERWRITE;
  648. msb->regs.extra_data.overwrite_flag = flag;
  649. msb->command_value = MS_CMD_BLOCK_WRITE;
  650. msb->command_need_oob = true;
  651. dbg_verbose("changing overwrite flag to %02x for sector %d, page %d",
  652. flag, pba, page);
  653. return msb_run_state_machine(msb, h_msb_send_command);
  654. }
  655. static int msb_mark_bad(struct msb_data *msb, int pba)
  656. {
  657. pr_notice("marking pba %d as bad", pba);
  658. msb_reset(msb, true);
  659. return msb_set_overwrite_flag(
  660. msb, pba, 0, 0xFF & ~MEMSTICK_OVERWRITE_BKST);
  661. }
  662. static int msb_mark_page_bad(struct msb_data *msb, int pba, int page)
  663. {
  664. dbg("marking page %d of pba %d as bad", page, pba);
  665. msb_reset(msb, true);
  666. return msb_set_overwrite_flag(msb,
  667. pba, page, ~MEMSTICK_OVERWRITE_PGST0);
  668. }
  669. /* Erases one physical block */
  670. static int msb_erase_block(struct msb_data *msb, u16 pba)
  671. {
  672. int error, try;
  673. if (msb->read_only)
  674. return -EROFS;
  675. dbg_verbose("erasing pba %d", pba);
  676. for (try = 1; try < 3; try++) {
  677. msb->regs.param.block_address = cpu_to_be16(pba);
  678. msb->regs.param.page_address = 0;
  679. msb->regs.param.cp = MEMSTICK_CP_BLOCK;
  680. msb->command_value = MS_CMD_BLOCK_ERASE;
  681. msb->command_need_oob = false;
  682. error = msb_run_state_machine(msb, h_msb_send_command);
  683. if (!error || msb_reset(msb, true))
  684. break;
  685. }
  686. if (error) {
  687. pr_err("erase failed, marking pba %d as bad", pba);
  688. msb_mark_bad(msb, pba);
  689. }
  690. dbg_verbose("erase success, marking pba %d as unused", pba);
  691. msb_mark_block_unused(msb, pba);
  692. __set_bit(pba, msb->erased_blocks_bitmap);
  693. return error;
  694. }
  695. /* Reads one page from device */
  696. static int msb_read_page(struct msb_data *msb,
  697. u16 pba, u8 page, struct ms_extra_data_register *extra,
  698. struct scatterlist *sg, int offset)
  699. {
  700. int try, error;
  701. if (pba == MS_BLOCK_INVALID) {
  702. unsigned long flags;
  703. struct sg_mapping_iter miter;
  704. size_t len = msb->page_size;
  705. dbg_verbose("read unmapped sector. returning 0xFF");
  706. local_irq_save(flags);
  707. sg_miter_start(&miter, sg, sg_nents(sg),
  708. SG_MITER_ATOMIC | SG_MITER_TO_SG);
  709. while (sg_miter_next(&miter) && len > 0) {
  710. int chunklen;
  711. if (offset && offset >= miter.length) {
  712. offset -= miter.length;
  713. continue;
  714. }
  715. chunklen = min(miter.length - offset, len);
  716. memset(miter.addr + offset, 0xFF, chunklen);
  717. len -= chunklen;
  718. offset = 0;
  719. }
  720. sg_miter_stop(&miter);
  721. local_irq_restore(flags);
  722. if (offset)
  723. return -EFAULT;
  724. if (extra)
  725. memset(extra, 0xFF, sizeof(*extra));
  726. return 0;
  727. }
  728. if (pba >= msb->block_count) {
  729. pr_err("BUG: attempt to read beyond the end of the card at pba %d", pba);
  730. return -EINVAL;
  731. }
  732. for (try = 1; try < 3; try++) {
  733. msb->regs.param.block_address = cpu_to_be16(pba);
  734. msb->regs.param.page_address = page;
  735. msb->regs.param.cp = MEMSTICK_CP_PAGE;
  736. msb->current_sg = sg;
  737. msb->current_sg_offset = offset;
  738. error = msb_run_state_machine(msb, h_msb_read_page);
  739. if (error == -EUCLEAN) {
  740. pr_notice("correctable error on pba %d, page %d",
  741. pba, page);
  742. error = 0;
  743. }
  744. if (!error && extra)
  745. *extra = msb->regs.extra_data;
  746. if (!error || msb_reset(msb, true))
  747. break;
  748. }
  749. /* Mark bad pages */
  750. if (error == -EBADMSG) {
  751. pr_err("uncorrectable error on read of pba %d, page %d",
  752. pba, page);
  753. if (msb->regs.extra_data.overwrite_flag &
  754. MEMSTICK_OVERWRITE_PGST0)
  755. msb_mark_page_bad(msb, pba, page);
  756. return -EBADMSG;
  757. }
  758. if (error)
  759. pr_err("read of pba %d, page %d failed with error %d",
  760. pba, page, error);
  761. return error;
  762. }
  763. /* Reads oob of page only */
  764. static int msb_read_oob(struct msb_data *msb, u16 pba, u16 page,
  765. struct ms_extra_data_register *extra)
  766. {
  767. int error;
  768. BUG_ON(!extra);
  769. msb->regs.param.block_address = cpu_to_be16(pba);
  770. msb->regs.param.page_address = page;
  771. msb->regs.param.cp = MEMSTICK_CP_EXTRA;
  772. if (pba > msb->block_count) {
  773. pr_err("BUG: attempt to read beyond the end of card at pba %d", pba);
  774. return -EINVAL;
  775. }
  776. error = msb_run_state_machine(msb, h_msb_read_page);
  777. *extra = msb->regs.extra_data;
  778. if (error == -EUCLEAN) {
  779. pr_notice("correctable error on pba %d, page %d",
  780. pba, page);
  781. return 0;
  782. }
  783. return error;
  784. }
  785. /* Reads a block and compares it with data contained in scatterlist orig_sg */
  786. static int msb_verify_block(struct msb_data *msb, u16 pba,
  787. struct scatterlist *orig_sg, int offset)
  788. {
  789. struct scatterlist sg;
  790. int page = 0, error;
  791. sg_init_one(&sg, msb->block_buffer, msb->block_size);
  792. while (page < msb->pages_in_block) {
  793. error = msb_read_page(msb, pba, page,
  794. NULL, &sg, page * msb->page_size);
  795. if (error)
  796. return error;
  797. page++;
  798. }
  799. if (msb_sg_compare_to_buffer(orig_sg, offset,
  800. msb->block_buffer, msb->block_size))
  801. return -EIO;
  802. return 0;
  803. }
  804. /* Writes exectly one block + oob */
  805. static int msb_write_block(struct msb_data *msb,
  806. u16 pba, u32 lba, struct scatterlist *sg, int offset)
  807. {
  808. int error, current_try = 1;
  809. BUG_ON(sg->length < msb->page_size);
  810. if (msb->read_only)
  811. return -EROFS;
  812. if (pba == MS_BLOCK_INVALID) {
  813. pr_err(
  814. "BUG: write: attempt to write MS_BLOCK_INVALID block");
  815. return -EINVAL;
  816. }
  817. if (pba >= msb->block_count || lba >= msb->logical_block_count) {
  818. pr_err(
  819. "BUG: write: attempt to write beyond the end of device");
  820. return -EINVAL;
  821. }
  822. if (msb_get_zone_from_lba(lba) != msb_get_zone_from_pba(pba)) {
  823. pr_err("BUG: write: lba zone mismatch");
  824. return -EINVAL;
  825. }
  826. if (pba == msb->boot_block_locations[0] ||
  827. pba == msb->boot_block_locations[1]) {
  828. pr_err("BUG: write: attempt to write to boot blocks!");
  829. return -EINVAL;
  830. }
  831. while (1) {
  832. if (msb->read_only)
  833. return -EROFS;
  834. msb->regs.param.cp = MEMSTICK_CP_BLOCK;
  835. msb->regs.param.page_address = 0;
  836. msb->regs.param.block_address = cpu_to_be16(pba);
  837. msb->regs.extra_data.management_flag = 0xFF;
  838. msb->regs.extra_data.overwrite_flag = 0xF8;
  839. msb->regs.extra_data.logical_address = cpu_to_be16(lba);
  840. msb->current_sg = sg;
  841. msb->current_sg_offset = offset;
  842. msb->current_page = 0;
  843. error = msb_run_state_machine(msb, h_msb_write_block);
  844. /* Sector we just wrote to is assumed erased since its pba
  845. was erased. If it wasn't erased, write will succeed
  846. and will just clear the bits that were set in the block
  847. thus test that what we have written,
  848. matches what we expect.
  849. We do trust the blocks that we erased */
  850. if (!error && (verify_writes ||
  851. !test_bit(pba, msb->erased_blocks_bitmap)))
  852. error = msb_verify_block(msb, pba, sg, offset);
  853. if (!error)
  854. break;
  855. if (current_try > 1 || msb_reset(msb, true))
  856. break;
  857. pr_err("write failed, trying to erase the pba %d", pba);
  858. error = msb_erase_block(msb, pba);
  859. if (error)
  860. break;
  861. current_try++;
  862. }
  863. return error;
  864. }
  865. /* Finds a free block for write replacement */
  866. static u16 msb_get_free_block(struct msb_data *msb, int zone)
  867. {
  868. u16 pos;
  869. int pba = zone * MS_BLOCKS_IN_ZONE;
  870. int i;
  871. get_random_bytes(&pos, sizeof(pos));
  872. if (!msb->free_block_count[zone]) {
  873. pr_err("NO free blocks in the zone %d, to use for a write, (media is WORN out) switching to RO mode", zone);
  874. msb->read_only = true;
  875. return MS_BLOCK_INVALID;
  876. }
  877. pos %= msb->free_block_count[zone];
  878. dbg_verbose("have %d choices for a free block, selected randomly: %d",
  879. msb->free_block_count[zone], pos);
  880. pba = find_next_zero_bit(msb->used_blocks_bitmap,
  881. msb->block_count, pba);
  882. for (i = 0; i < pos; ++i)
  883. pba = find_next_zero_bit(msb->used_blocks_bitmap,
  884. msb->block_count, pba + 1);
  885. dbg_verbose("result of the free blocks scan: pba %d", pba);
  886. if (pba == msb->block_count || (msb_get_zone_from_pba(pba)) != zone) {
  887. pr_err("BUG: cant get a free block");
  888. msb->read_only = true;
  889. return MS_BLOCK_INVALID;
  890. }
  891. msb_mark_block_used(msb, pba);
  892. return pba;
  893. }
  894. static int msb_update_block(struct msb_data *msb, u16 lba,
  895. struct scatterlist *sg, int offset)
  896. {
  897. u16 pba, new_pba;
  898. int error, try;
  899. pba = msb->lba_to_pba_table[lba];
  900. dbg_verbose("start of a block update at lba %d, pba %d", lba, pba);
  901. if (pba != MS_BLOCK_INVALID) {
  902. dbg_verbose("setting the update flag on the block");
  903. msb_set_overwrite_flag(msb, pba, 0,
  904. 0xFF & ~MEMSTICK_OVERWRITE_UDST);
  905. }
  906. for (try = 0; try < 3; try++) {
  907. new_pba = msb_get_free_block(msb,
  908. msb_get_zone_from_lba(lba));
  909. if (new_pba == MS_BLOCK_INVALID) {
  910. error = -EIO;
  911. goto out;
  912. }
  913. dbg_verbose("block update: writing updated block to the pba %d",
  914. new_pba);
  915. error = msb_write_block(msb, new_pba, lba, sg, offset);
  916. if (error == -EBADMSG) {
  917. msb_mark_bad(msb, new_pba);
  918. continue;
  919. }
  920. if (error)
  921. goto out;
  922. dbg_verbose("block update: erasing the old block");
  923. msb_erase_block(msb, pba);
  924. msb->lba_to_pba_table[lba] = new_pba;
  925. return 0;
  926. }
  927. out:
  928. if (error) {
  929. pr_err("block update error after %d tries, switching to r/o mode", try);
  930. msb->read_only = true;
  931. }
  932. return error;
  933. }
  934. /* Converts endiannes in the boot block for easy use */
  935. static void msb_fix_boot_page_endianness(struct ms_boot_page *p)
  936. {
  937. p->header.block_id = be16_to_cpu(p->header.block_id);
  938. p->header.format_reserved = be16_to_cpu(p->header.format_reserved);
  939. p->entry.disabled_block.start_addr
  940. = be32_to_cpu(p->entry.disabled_block.start_addr);
  941. p->entry.disabled_block.data_size
  942. = be32_to_cpu(p->entry.disabled_block.data_size);
  943. p->entry.cis_idi.start_addr
  944. = be32_to_cpu(p->entry.cis_idi.start_addr);
  945. p->entry.cis_idi.data_size
  946. = be32_to_cpu(p->entry.cis_idi.data_size);
  947. p->attr.block_size = be16_to_cpu(p->attr.block_size);
  948. p->attr.number_of_blocks = be16_to_cpu(p->attr.number_of_blocks);
  949. p->attr.number_of_effective_blocks
  950. = be16_to_cpu(p->attr.number_of_effective_blocks);
  951. p->attr.page_size = be16_to_cpu(p->attr.page_size);
  952. p->attr.memory_manufacturer_code
  953. = be16_to_cpu(p->attr.memory_manufacturer_code);
  954. p->attr.memory_device_code = be16_to_cpu(p->attr.memory_device_code);
  955. p->attr.implemented_capacity
  956. = be16_to_cpu(p->attr.implemented_capacity);
  957. p->attr.controller_number = be16_to_cpu(p->attr.controller_number);
  958. p->attr.controller_function = be16_to_cpu(p->attr.controller_function);
  959. }
  960. static int msb_read_boot_blocks(struct msb_data *msb)
  961. {
  962. int pba = 0;
  963. struct scatterlist sg;
  964. struct ms_extra_data_register extra;
  965. struct ms_boot_page *page;
  966. msb->boot_block_locations[0] = MS_BLOCK_INVALID;
  967. msb->boot_block_locations[1] = MS_BLOCK_INVALID;
  968. msb->boot_block_count = 0;
  969. dbg_verbose("Start of a scan for the boot blocks");
  970. if (!msb->boot_page) {
  971. page = kmalloc_array(2, sizeof(struct ms_boot_page),
  972. GFP_KERNEL);
  973. if (!page)
  974. return -ENOMEM;
  975. msb->boot_page = page;
  976. } else
  977. page = msb->boot_page;
  978. msb->block_count = MS_BLOCK_MAX_BOOT_ADDR;
  979. for (pba = 0; pba < MS_BLOCK_MAX_BOOT_ADDR; pba++) {
  980. sg_init_one(&sg, page, sizeof(*page));
  981. if (msb_read_page(msb, pba, 0, &extra, &sg, 0)) {
  982. dbg("boot scan: can't read pba %d", pba);
  983. continue;
  984. }
  985. if (extra.management_flag & MEMSTICK_MANAGEMENT_SYSFLG) {
  986. dbg("management flag doesn't indicate boot block %d",
  987. pba);
  988. continue;
  989. }
  990. if (be16_to_cpu(page->header.block_id) != MS_BLOCK_BOOT_ID) {
  991. dbg("the pba at %d doesn' contain boot block ID", pba);
  992. continue;
  993. }
  994. msb_fix_boot_page_endianness(page);
  995. msb->boot_block_locations[msb->boot_block_count] = pba;
  996. page++;
  997. msb->boot_block_count++;
  998. if (msb->boot_block_count == 2)
  999. break;
  1000. }
  1001. if (!msb->boot_block_count) {
  1002. pr_err("media doesn't contain master page, aborting");
  1003. return -EIO;
  1004. }
  1005. dbg_verbose("End of scan for boot blocks");
  1006. return 0;
  1007. }
  1008. static int msb_read_bad_block_table(struct msb_data *msb, int block_nr)
  1009. {
  1010. struct ms_boot_page *boot_block;
  1011. struct scatterlist sg;
  1012. u16 *buffer = NULL;
  1013. int offset = 0;
  1014. int i, error = 0;
  1015. int data_size, data_offset, page, page_offset, size_to_read;
  1016. u16 pba;
  1017. BUG_ON(block_nr > 1);
  1018. boot_block = &msb->boot_page[block_nr];
  1019. pba = msb->boot_block_locations[block_nr];
  1020. if (msb->boot_block_locations[block_nr] == MS_BLOCK_INVALID)
  1021. return -EINVAL;
  1022. data_size = boot_block->entry.disabled_block.data_size;
  1023. data_offset = sizeof(struct ms_boot_page) +
  1024. boot_block->entry.disabled_block.start_addr;
  1025. if (!data_size)
  1026. return 0;
  1027. page = data_offset / msb->page_size;
  1028. page_offset = data_offset % msb->page_size;
  1029. size_to_read =
  1030. DIV_ROUND_UP(data_size + page_offset, msb->page_size) *
  1031. msb->page_size;
  1032. dbg("reading bad block of boot block at pba %d, offset %d len %d",
  1033. pba, data_offset, data_size);
  1034. buffer = kzalloc(size_to_read, GFP_KERNEL);
  1035. if (!buffer)
  1036. return -ENOMEM;
  1037. /* Read the buffer */
  1038. sg_init_one(&sg, buffer, size_to_read);
  1039. while (offset < size_to_read) {
  1040. error = msb_read_page(msb, pba, page, NULL, &sg, offset);
  1041. if (error)
  1042. goto out;
  1043. page++;
  1044. offset += msb->page_size;
  1045. if (page == msb->pages_in_block) {
  1046. pr_err(
  1047. "bad block table extends beyond the boot block");
  1048. break;
  1049. }
  1050. }
  1051. /* Process the bad block table */
  1052. for (i = page_offset; i < data_size / sizeof(u16); i++) {
  1053. u16 bad_block = be16_to_cpu(buffer[i]);
  1054. if (bad_block >= msb->block_count) {
  1055. dbg("bad block table contains invalid block %d",
  1056. bad_block);
  1057. continue;
  1058. }
  1059. if (test_bit(bad_block, msb->used_blocks_bitmap)) {
  1060. dbg("duplicate bad block %d in the table",
  1061. bad_block);
  1062. continue;
  1063. }
  1064. dbg("block %d is marked as factory bad", bad_block);
  1065. msb_mark_block_used(msb, bad_block);
  1066. }
  1067. out:
  1068. kfree(buffer);
  1069. return error;
  1070. }
  1071. static int msb_ftl_initialize(struct msb_data *msb)
  1072. {
  1073. int i;
  1074. if (msb->ftl_initialized)
  1075. return 0;
  1076. msb->zone_count = msb->block_count / MS_BLOCKS_IN_ZONE;
  1077. msb->logical_block_count = msb->zone_count * 496 - 2;
  1078. msb->used_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
  1079. msb->erased_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
  1080. msb->lba_to_pba_table =
  1081. kmalloc_array(msb->logical_block_count, sizeof(u16),
  1082. GFP_KERNEL);
  1083. if (!msb->used_blocks_bitmap || !msb->lba_to_pba_table ||
  1084. !msb->erased_blocks_bitmap) {
  1085. kfree(msb->used_blocks_bitmap);
  1086. kfree(msb->lba_to_pba_table);
  1087. kfree(msb->erased_blocks_bitmap);
  1088. return -ENOMEM;
  1089. }
  1090. for (i = 0; i < msb->zone_count; i++)
  1091. msb->free_block_count[i] = MS_BLOCKS_IN_ZONE;
  1092. memset(msb->lba_to_pba_table, MS_BLOCK_INVALID,
  1093. msb->logical_block_count * sizeof(u16));
  1094. dbg("initial FTL tables created. Zone count = %d, Logical block count = %d",
  1095. msb->zone_count, msb->logical_block_count);
  1096. msb->ftl_initialized = true;
  1097. return 0;
  1098. }
  1099. static int msb_ftl_scan(struct msb_data *msb)
  1100. {
  1101. u16 pba, lba, other_block;
  1102. u8 overwrite_flag, management_flag, other_overwrite_flag;
  1103. int error;
  1104. struct ms_extra_data_register extra;
  1105. u8 *overwrite_flags = kzalloc(msb->block_count, GFP_KERNEL);
  1106. if (!overwrite_flags)
  1107. return -ENOMEM;
  1108. dbg("Start of media scanning");
  1109. for (pba = 0; pba < msb->block_count; pba++) {
  1110. if (pba == msb->boot_block_locations[0] ||
  1111. pba == msb->boot_block_locations[1]) {
  1112. dbg_verbose("pba %05d -> [boot block]", pba);
  1113. msb_mark_block_used(msb, pba);
  1114. continue;
  1115. }
  1116. if (test_bit(pba, msb->used_blocks_bitmap)) {
  1117. dbg_verbose("pba %05d -> [factory bad]", pba);
  1118. continue;
  1119. }
  1120. memset(&extra, 0, sizeof(extra));
  1121. error = msb_read_oob(msb, pba, 0, &extra);
  1122. /* can't trust the page if we can't read the oob */
  1123. if (error == -EBADMSG) {
  1124. pr_notice(
  1125. "oob of pba %d damaged, will try to erase it", pba);
  1126. msb_mark_block_used(msb, pba);
  1127. msb_erase_block(msb, pba);
  1128. continue;
  1129. } else if (error) {
  1130. pr_err("unknown error %d on read of oob of pba %d - aborting",
  1131. error, pba);
  1132. kfree(overwrite_flags);
  1133. return error;
  1134. }
  1135. lba = be16_to_cpu(extra.logical_address);
  1136. management_flag = extra.management_flag;
  1137. overwrite_flag = extra.overwrite_flag;
  1138. overwrite_flags[pba] = overwrite_flag;
  1139. /* Skip bad blocks */
  1140. if (!(overwrite_flag & MEMSTICK_OVERWRITE_BKST)) {
  1141. dbg("pba %05d -> [BAD]", pba);
  1142. msb_mark_block_used(msb, pba);
  1143. continue;
  1144. }
  1145. /* Skip system/drm blocks */
  1146. if ((management_flag & MEMSTICK_MANAGEMENT_FLAG_NORMAL) !=
  1147. MEMSTICK_MANAGEMENT_FLAG_NORMAL) {
  1148. dbg("pba %05d -> [reserved management flag %02x]",
  1149. pba, management_flag);
  1150. msb_mark_block_used(msb, pba);
  1151. continue;
  1152. }
  1153. /* Erase temporary tables */
  1154. if (!(management_flag & MEMSTICK_MANAGEMENT_ATFLG)) {
  1155. dbg("pba %05d -> [temp table] - will erase", pba);
  1156. msb_mark_block_used(msb, pba);
  1157. msb_erase_block(msb, pba);
  1158. continue;
  1159. }
  1160. if (lba == MS_BLOCK_INVALID) {
  1161. dbg_verbose("pba %05d -> [free]", pba);
  1162. continue;
  1163. }
  1164. msb_mark_block_used(msb, pba);
  1165. /* Block has LBA not according to zoning*/
  1166. if (msb_get_zone_from_lba(lba) != msb_get_zone_from_pba(pba)) {
  1167. pr_notice("pba %05d -> [bad lba %05d] - will erase",
  1168. pba, lba);
  1169. msb_erase_block(msb, pba);
  1170. continue;
  1171. }
  1172. /* No collisions - great */
  1173. if (msb->lba_to_pba_table[lba] == MS_BLOCK_INVALID) {
  1174. dbg_verbose("pba %05d -> [lba %05d]", pba, lba);
  1175. msb->lba_to_pba_table[lba] = pba;
  1176. continue;
  1177. }
  1178. other_block = msb->lba_to_pba_table[lba];
  1179. other_overwrite_flag = overwrite_flags[other_block];
  1180. pr_notice("Collision between pba %d and pba %d",
  1181. pba, other_block);
  1182. if (!(overwrite_flag & MEMSTICK_OVERWRITE_UDST)) {
  1183. pr_notice("pba %d is marked as stable, use it", pba);
  1184. msb_erase_block(msb, other_block);
  1185. msb->lba_to_pba_table[lba] = pba;
  1186. continue;
  1187. }
  1188. if (!(other_overwrite_flag & MEMSTICK_OVERWRITE_UDST)) {
  1189. pr_notice("pba %d is marked as stable, use it",
  1190. other_block);
  1191. msb_erase_block(msb, pba);
  1192. continue;
  1193. }
  1194. pr_notice("collision between blocks %d and %d, without stable flag set on both, erasing pba %d",
  1195. pba, other_block, other_block);
  1196. msb_erase_block(msb, other_block);
  1197. msb->lba_to_pba_table[lba] = pba;
  1198. }
  1199. dbg("End of media scanning");
  1200. kfree(overwrite_flags);
  1201. return 0;
  1202. }
  1203. static void msb_cache_flush_timer(struct timer_list *t)
  1204. {
  1205. struct msb_data *msb = from_timer(msb, t, cache_flush_timer);
  1206. msb->need_flush_cache = true;
  1207. queue_work(msb->io_queue, &msb->io_work);
  1208. }
  1209. static void msb_cache_discard(struct msb_data *msb)
  1210. {
  1211. if (msb->cache_block_lba == MS_BLOCK_INVALID)
  1212. return;
  1213. del_timer_sync(&msb->cache_flush_timer);
  1214. dbg_verbose("Discarding the write cache");
  1215. msb->cache_block_lba = MS_BLOCK_INVALID;
  1216. bitmap_zero(&msb->valid_cache_bitmap, msb->pages_in_block);
  1217. }
  1218. static int msb_cache_init(struct msb_data *msb)
  1219. {
  1220. timer_setup(&msb->cache_flush_timer, msb_cache_flush_timer, 0);
  1221. if (!msb->cache)
  1222. msb->cache = kzalloc(msb->block_size, GFP_KERNEL);
  1223. if (!msb->cache)
  1224. return -ENOMEM;
  1225. msb_cache_discard(msb);
  1226. return 0;
  1227. }
  1228. static int msb_cache_flush(struct msb_data *msb)
  1229. {
  1230. struct scatterlist sg;
  1231. struct ms_extra_data_register extra;
  1232. int page, offset, error;
  1233. u16 pba, lba;
  1234. if (msb->read_only)
  1235. return -EROFS;
  1236. if (msb->cache_block_lba == MS_BLOCK_INVALID)
  1237. return 0;
  1238. lba = msb->cache_block_lba;
  1239. pba = msb->lba_to_pba_table[lba];
  1240. dbg_verbose("Flushing the write cache of pba %d (LBA %d)",
  1241. pba, msb->cache_block_lba);
  1242. sg_init_one(&sg, msb->cache , msb->block_size);
  1243. /* Read all missing pages in cache */
  1244. for (page = 0; page < msb->pages_in_block; page++) {
  1245. if (test_bit(page, &msb->valid_cache_bitmap))
  1246. continue;
  1247. offset = page * msb->page_size;
  1248. dbg_verbose("reading non-present sector %d of cache block %d",
  1249. page, lba);
  1250. error = msb_read_page(msb, pba, page, &extra, &sg, offset);
  1251. /* Bad pages are copied with 00 page status */
  1252. if (error == -EBADMSG) {
  1253. pr_err("read error on sector %d, contents probably damaged", page);
  1254. continue;
  1255. }
  1256. if (error)
  1257. return error;
  1258. if ((extra.overwrite_flag & MEMSTICK_OV_PG_NORMAL) !=
  1259. MEMSTICK_OV_PG_NORMAL) {
  1260. dbg("page %d is marked as bad", page);
  1261. continue;
  1262. }
  1263. set_bit(page, &msb->valid_cache_bitmap);
  1264. }
  1265. /* Write the cache now */
  1266. error = msb_update_block(msb, msb->cache_block_lba, &sg, 0);
  1267. pba = msb->lba_to_pba_table[msb->cache_block_lba];
  1268. /* Mark invalid pages */
  1269. if (!error) {
  1270. for (page = 0; page < msb->pages_in_block; page++) {
  1271. if (test_bit(page, &msb->valid_cache_bitmap))
  1272. continue;
  1273. dbg("marking page %d as containing damaged data",
  1274. page);
  1275. msb_set_overwrite_flag(msb,
  1276. pba , page, 0xFF & ~MEMSTICK_OV_PG_NORMAL);
  1277. }
  1278. }
  1279. msb_cache_discard(msb);
  1280. return error;
  1281. }
  1282. static int msb_cache_write(struct msb_data *msb, int lba,
  1283. int page, bool add_to_cache_only, struct scatterlist *sg, int offset)
  1284. {
  1285. int error;
  1286. struct scatterlist sg_tmp[10];
  1287. if (msb->read_only)
  1288. return -EROFS;
  1289. if (msb->cache_block_lba == MS_BLOCK_INVALID ||
  1290. lba != msb->cache_block_lba)
  1291. if (add_to_cache_only)
  1292. return 0;
  1293. /* If we need to write different block */
  1294. if (msb->cache_block_lba != MS_BLOCK_INVALID &&
  1295. lba != msb->cache_block_lba) {
  1296. dbg_verbose("first flush the cache");
  1297. error = msb_cache_flush(msb);
  1298. if (error)
  1299. return error;
  1300. }
  1301. if (msb->cache_block_lba == MS_BLOCK_INVALID) {
  1302. msb->cache_block_lba = lba;
  1303. mod_timer(&msb->cache_flush_timer,
  1304. jiffies + msecs_to_jiffies(cache_flush_timeout));
  1305. }
  1306. dbg_verbose("Write of LBA %d page %d to cache ", lba, page);
  1307. sg_init_table(sg_tmp, ARRAY_SIZE(sg_tmp));
  1308. msb_sg_copy(sg, sg_tmp, ARRAY_SIZE(sg_tmp), offset, msb->page_size);
  1309. sg_copy_to_buffer(sg_tmp, sg_nents(sg_tmp),
  1310. msb->cache + page * msb->page_size, msb->page_size);
  1311. set_bit(page, &msb->valid_cache_bitmap);
  1312. return 0;
  1313. }
  1314. static int msb_cache_read(struct msb_data *msb, int lba,
  1315. int page, struct scatterlist *sg, int offset)
  1316. {
  1317. int pba = msb->lba_to_pba_table[lba];
  1318. struct scatterlist sg_tmp[10];
  1319. int error = 0;
  1320. if (lba == msb->cache_block_lba &&
  1321. test_bit(page, &msb->valid_cache_bitmap)) {
  1322. dbg_verbose("Read of LBA %d (pba %d) sector %d from cache",
  1323. lba, pba, page);
  1324. sg_init_table(sg_tmp, ARRAY_SIZE(sg_tmp));
  1325. msb_sg_copy(sg, sg_tmp, ARRAY_SIZE(sg_tmp),
  1326. offset, msb->page_size);
  1327. sg_copy_from_buffer(sg_tmp, sg_nents(sg_tmp),
  1328. msb->cache + msb->page_size * page,
  1329. msb->page_size);
  1330. } else {
  1331. dbg_verbose("Read of LBA %d (pba %d) sector %d from device",
  1332. lba, pba, page);
  1333. error = msb_read_page(msb, pba, page, NULL, sg, offset);
  1334. if (error)
  1335. return error;
  1336. msb_cache_write(msb, lba, page, true, sg, offset);
  1337. }
  1338. return error;
  1339. }
  1340. /* Emulated geometry table
  1341. * This table content isn't that importaint,
  1342. * One could put here different values, providing that they still
  1343. * cover whole disk.
  1344. * 64 MB entry is what windows reports for my 64M memstick */
  1345. static const struct chs_entry chs_table[] = {
  1346. /* size sectors cylynders heads */
  1347. { 4, 16, 247, 2 },
  1348. { 8, 16, 495, 2 },
  1349. { 16, 16, 495, 4 },
  1350. { 32, 16, 991, 4 },
  1351. { 64, 16, 991, 8 },
  1352. {128, 16, 991, 16 },
  1353. { 0 }
  1354. };
  1355. /* Load information about the card */
  1356. static int msb_init_card(struct memstick_dev *card)
  1357. {
  1358. struct msb_data *msb = memstick_get_drvdata(card);
  1359. struct memstick_host *host = card->host;
  1360. struct ms_boot_page *boot_block;
  1361. int error = 0, i, raw_size_in_megs;
  1362. msb->caps = 0;
  1363. if (card->id.class >= MEMSTICK_CLASS_ROM &&
  1364. card->id.class <= MEMSTICK_CLASS_ROM)
  1365. msb->read_only = true;
  1366. msb->state = -1;
  1367. error = msb_reset(msb, false);
  1368. if (error)
  1369. return error;
  1370. /* Due to a bug in Jmicron driver written by Alex Dubov,
  1371. its serial mode barely works,
  1372. so we switch to parallel mode right away */
  1373. if (host->caps & MEMSTICK_CAP_PAR4)
  1374. msb_switch_to_parallel(msb);
  1375. msb->page_size = sizeof(struct ms_boot_page);
  1376. /* Read the boot page */
  1377. error = msb_read_boot_blocks(msb);
  1378. if (error)
  1379. return -EIO;
  1380. boot_block = &msb->boot_page[0];
  1381. /* Save intersting attributes from boot page */
  1382. msb->block_count = boot_block->attr.number_of_blocks;
  1383. msb->page_size = boot_block->attr.page_size;
  1384. msb->pages_in_block = boot_block->attr.block_size * 2;
  1385. msb->block_size = msb->page_size * msb->pages_in_block;
  1386. if (msb->page_size > PAGE_SIZE) {
  1387. /* this isn't supported by linux at all, anyway*/
  1388. dbg("device page %d size isn't supported", msb->page_size);
  1389. return -EINVAL;
  1390. }
  1391. msb->block_buffer = kzalloc(msb->block_size, GFP_KERNEL);
  1392. if (!msb->block_buffer)
  1393. return -ENOMEM;
  1394. raw_size_in_megs = (msb->block_size * msb->block_count) >> 20;
  1395. for (i = 0; chs_table[i].size; i++) {
  1396. if (chs_table[i].size != raw_size_in_megs)
  1397. continue;
  1398. msb->geometry.cylinders = chs_table[i].cyl;
  1399. msb->geometry.heads = chs_table[i].head;
  1400. msb->geometry.sectors = chs_table[i].sec;
  1401. break;
  1402. }
  1403. if (boot_block->attr.transfer_supporting == 1)
  1404. msb->caps |= MEMSTICK_CAP_PAR4;
  1405. if (boot_block->attr.device_type & 0x03)
  1406. msb->read_only = true;
  1407. dbg("Total block count = %d", msb->block_count);
  1408. dbg("Each block consists of %d pages", msb->pages_in_block);
  1409. dbg("Page size = %d bytes", msb->page_size);
  1410. dbg("Parallel mode supported: %d", !!(msb->caps & MEMSTICK_CAP_PAR4));
  1411. dbg("Read only: %d", msb->read_only);
  1412. #if 0
  1413. /* Now we can switch the interface */
  1414. if (host->caps & msb->caps & MEMSTICK_CAP_PAR4)
  1415. msb_switch_to_parallel(msb);
  1416. #endif
  1417. error = msb_cache_init(msb);
  1418. if (error)
  1419. return error;
  1420. error = msb_ftl_initialize(msb);
  1421. if (error)
  1422. return error;
  1423. /* Read the bad block table */
  1424. error = msb_read_bad_block_table(msb, 0);
  1425. if (error && error != -ENOMEM) {
  1426. dbg("failed to read bad block table from primary boot block, trying from backup");
  1427. error = msb_read_bad_block_table(msb, 1);
  1428. }
  1429. if (error)
  1430. return error;
  1431. /* *drum roll* Scan the media */
  1432. error = msb_ftl_scan(msb);
  1433. if (error) {
  1434. pr_err("Scan of media failed");
  1435. return error;
  1436. }
  1437. return 0;
  1438. }
  1439. static int msb_do_write_request(struct msb_data *msb, int lba,
  1440. int page, struct scatterlist *sg, size_t len, int *sucessfuly_written)
  1441. {
  1442. int error = 0;
  1443. off_t offset = 0;
  1444. *sucessfuly_written = 0;
  1445. while (offset < len) {
  1446. if (page == 0 && len - offset >= msb->block_size) {
  1447. if (msb->cache_block_lba == lba)
  1448. msb_cache_discard(msb);
  1449. dbg_verbose("Writing whole lba %d", lba);
  1450. error = msb_update_block(msb, lba, sg, offset);
  1451. if (error)
  1452. return error;
  1453. offset += msb->block_size;
  1454. *sucessfuly_written += msb->block_size;
  1455. lba++;
  1456. continue;
  1457. }
  1458. error = msb_cache_write(msb, lba, page, false, sg, offset);
  1459. if (error)
  1460. return error;
  1461. offset += msb->page_size;
  1462. *sucessfuly_written += msb->page_size;
  1463. page++;
  1464. if (page == msb->pages_in_block) {
  1465. page = 0;
  1466. lba++;
  1467. }
  1468. }
  1469. return 0;
  1470. }
  1471. static int msb_do_read_request(struct msb_data *msb, int lba,
  1472. int page, struct scatterlist *sg, int len, int *sucessfuly_read)
  1473. {
  1474. int error = 0;
  1475. int offset = 0;
  1476. *sucessfuly_read = 0;
  1477. while (offset < len) {
  1478. error = msb_cache_read(msb, lba, page, sg, offset);
  1479. if (error)
  1480. return error;
  1481. offset += msb->page_size;
  1482. *sucessfuly_read += msb->page_size;
  1483. page++;
  1484. if (page == msb->pages_in_block) {
  1485. page = 0;
  1486. lba++;
  1487. }
  1488. }
  1489. return 0;
  1490. }
  1491. static void msb_io_work(struct work_struct *work)
  1492. {
  1493. struct msb_data *msb = container_of(work, struct msb_data, io_work);
  1494. int page, error, len;
  1495. sector_t lba;
  1496. struct scatterlist *sg = msb->prealloc_sg;
  1497. struct request *req;
  1498. dbg_verbose("IO: work started");
  1499. while (1) {
  1500. spin_lock_irq(&msb->q_lock);
  1501. if (msb->need_flush_cache) {
  1502. msb->need_flush_cache = false;
  1503. spin_unlock_irq(&msb->q_lock);
  1504. msb_cache_flush(msb);
  1505. continue;
  1506. }
  1507. req = msb->req;
  1508. if (!req) {
  1509. dbg_verbose("IO: no more requests exiting");
  1510. spin_unlock_irq(&msb->q_lock);
  1511. return;
  1512. }
  1513. spin_unlock_irq(&msb->q_lock);
  1514. /* process the request */
  1515. dbg_verbose("IO: processing new request");
  1516. blk_rq_map_sg(msb->queue, req, sg);
  1517. lba = blk_rq_pos(req);
  1518. sector_div(lba, msb->page_size / 512);
  1519. page = sector_div(lba, msb->pages_in_block);
  1520. if (rq_data_dir(msb->req) == READ)
  1521. error = msb_do_read_request(msb, lba, page, sg,
  1522. blk_rq_bytes(req), &len);
  1523. else
  1524. error = msb_do_write_request(msb, lba, page, sg,
  1525. blk_rq_bytes(req), &len);
  1526. if (len && !blk_update_request(req, BLK_STS_OK, len)) {
  1527. __blk_mq_end_request(req, BLK_STS_OK);
  1528. spin_lock_irq(&msb->q_lock);
  1529. msb->req = NULL;
  1530. spin_unlock_irq(&msb->q_lock);
  1531. }
  1532. if (error && msb->req) {
  1533. blk_status_t ret = errno_to_blk_status(error);
  1534. dbg_verbose("IO: ending one sector of the request with error");
  1535. blk_mq_end_request(req, ret);
  1536. spin_lock_irq(&msb->q_lock);
  1537. msb->req = NULL;
  1538. spin_unlock_irq(&msb->q_lock);
  1539. }
  1540. if (msb->req)
  1541. dbg_verbose("IO: request still pending");
  1542. }
  1543. }
  1544. static DEFINE_IDR(msb_disk_idr); /*set of used disk numbers */
  1545. static DEFINE_MUTEX(msb_disk_lock); /* protects against races in open/release */
  1546. static int msb_bd_open(struct block_device *bdev, fmode_t mode)
  1547. {
  1548. struct gendisk *disk = bdev->bd_disk;
  1549. struct msb_data *msb = disk->private_data;
  1550. dbg_verbose("block device open");
  1551. mutex_lock(&msb_disk_lock);
  1552. if (msb && msb->card)
  1553. msb->usage_count++;
  1554. mutex_unlock(&msb_disk_lock);
  1555. return 0;
  1556. }
  1557. static void msb_data_clear(struct msb_data *msb)
  1558. {
  1559. kfree(msb->boot_page);
  1560. kfree(msb->used_blocks_bitmap);
  1561. kfree(msb->lba_to_pba_table);
  1562. kfree(msb->cache);
  1563. msb->card = NULL;
  1564. }
  1565. static int msb_disk_release(struct gendisk *disk)
  1566. {
  1567. struct msb_data *msb = disk->private_data;
  1568. dbg_verbose("block device release");
  1569. mutex_lock(&msb_disk_lock);
  1570. if (msb) {
  1571. if (msb->usage_count)
  1572. msb->usage_count--;
  1573. if (!msb->usage_count) {
  1574. disk->private_data = NULL;
  1575. idr_remove(&msb_disk_idr, msb->disk_id);
  1576. put_disk(disk);
  1577. kfree(msb);
  1578. }
  1579. }
  1580. mutex_unlock(&msb_disk_lock);
  1581. return 0;
  1582. }
  1583. static void msb_bd_release(struct gendisk *disk, fmode_t mode)
  1584. {
  1585. msb_disk_release(disk);
  1586. }
  1587. static int msb_bd_getgeo(struct block_device *bdev,
  1588. struct hd_geometry *geo)
  1589. {
  1590. struct msb_data *msb = bdev->bd_disk->private_data;
  1591. *geo = msb->geometry;
  1592. return 0;
  1593. }
  1594. static blk_status_t msb_queue_rq(struct blk_mq_hw_ctx *hctx,
  1595. const struct blk_mq_queue_data *bd)
  1596. {
  1597. struct memstick_dev *card = hctx->queue->queuedata;
  1598. struct msb_data *msb = memstick_get_drvdata(card);
  1599. struct request *req = bd->rq;
  1600. dbg_verbose("Submit request");
  1601. spin_lock_irq(&msb->q_lock);
  1602. if (msb->card_dead) {
  1603. dbg("Refusing requests on removed card");
  1604. WARN_ON(!msb->io_queue_stopped);
  1605. spin_unlock_irq(&msb->q_lock);
  1606. blk_mq_start_request(req);
  1607. return BLK_STS_IOERR;
  1608. }
  1609. if (msb->req) {
  1610. spin_unlock_irq(&msb->q_lock);
  1611. return

Large files files are truncated, but you can click here to view the full file