ms_block.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342
  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. */
  289. if (!msb_write_regs(msb,
  290. offsetof(struct ms_register, param),
  291. sizeof(struct ms_param_register),
  292. (unsigned char *)&msb->regs.param))
  293. return 0;
  294. msb->state = MSB_RP_SEND_READ_COMMAND;
  295. return 0;
  296. case MSB_RP_SEND_READ_COMMAND:
  297. command = MS_CMD_BLOCK_READ;
  298. memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
  299. msb->state = MSB_RP_SEND_INT_REQ;
  300. return 0;
  301. case MSB_RP_SEND_INT_REQ:
  302. msb->state = MSB_RP_RECEIVE_INT_REQ_RESULT;
  303. /* If dont actually need to send the int read request (only in
  304. * serial mode), then just fall through
  305. */
  306. if (msb_read_int_reg(msb, -1))
  307. return 0;
  308. fallthrough;
  309. case MSB_RP_RECEIVE_INT_REQ_RESULT:
  310. intreg = mrq->data[0];
  311. msb->regs.status.interrupt = intreg;
  312. if (intreg & MEMSTICK_INT_CMDNAK)
  313. return msb_exit_state_machine(msb, -EIO);
  314. if (!(intreg & MEMSTICK_INT_CED)) {
  315. msb->state = MSB_RP_SEND_INT_REQ;
  316. goto again;
  317. }
  318. msb->int_polling = false;
  319. msb->state = (intreg & MEMSTICK_INT_ERR) ?
  320. MSB_RP_SEND_READ_STATUS_REG : MSB_RP_SEND_OOB_READ;
  321. goto again;
  322. case MSB_RP_SEND_READ_STATUS_REG:
  323. /* read the status register to understand source of the INT_ERR */
  324. if (!msb_read_regs(msb,
  325. offsetof(struct ms_register, status),
  326. sizeof(struct ms_status_register)))
  327. return 0;
  328. msb->state = MSB_RP_RECEIVE_STATUS_REG;
  329. return 0;
  330. case MSB_RP_RECEIVE_STATUS_REG:
  331. msb->regs.status = *(struct ms_status_register *)mrq->data;
  332. msb->state = MSB_RP_SEND_OOB_READ;
  333. fallthrough;
  334. case MSB_RP_SEND_OOB_READ:
  335. if (!msb_read_regs(msb,
  336. offsetof(struct ms_register, extra_data),
  337. sizeof(struct ms_extra_data_register)))
  338. return 0;
  339. msb->state = MSB_RP_RECEIVE_OOB_READ;
  340. return 0;
  341. case MSB_RP_RECEIVE_OOB_READ:
  342. msb->regs.extra_data =
  343. *(struct ms_extra_data_register *) mrq->data;
  344. msb->state = MSB_RP_SEND_READ_DATA;
  345. fallthrough;
  346. case MSB_RP_SEND_READ_DATA:
  347. /* Skip that state if we only read the oob */
  348. if (msb->regs.param.cp == MEMSTICK_CP_EXTRA) {
  349. msb->state = MSB_RP_RECEIVE_READ_DATA;
  350. goto again;
  351. }
  352. sg_init_table(sg, ARRAY_SIZE(sg));
  353. msb_sg_copy(msb->current_sg, sg, ARRAY_SIZE(sg),
  354. msb->current_sg_offset,
  355. msb->page_size);
  356. memstick_init_req_sg(mrq, MS_TPC_READ_LONG_DATA, sg);
  357. msb->state = MSB_RP_RECEIVE_READ_DATA;
  358. return 0;
  359. case MSB_RP_RECEIVE_READ_DATA:
  360. if (!(msb->regs.status.interrupt & MEMSTICK_INT_ERR)) {
  361. msb->current_sg_offset += msb->page_size;
  362. return msb_exit_state_machine(msb, 0);
  363. }
  364. if (msb->regs.status.status1 & MEMSTICK_UNCORR_ERROR) {
  365. dbg("read_page: uncorrectable error");
  366. return msb_exit_state_machine(msb, -EBADMSG);
  367. }
  368. if (msb->regs.status.status1 & MEMSTICK_CORR_ERROR) {
  369. dbg("read_page: correctable error");
  370. msb->current_sg_offset += msb->page_size;
  371. return msb_exit_state_machine(msb, -EUCLEAN);
  372. } else {
  373. dbg("read_page: INT error, but no status error bits");
  374. return msb_exit_state_machine(msb, -EIO);
  375. }
  376. }
  377. BUG();
  378. }
  379. /*
  380. * Handler of writes of exactly one block.
  381. * Takes address from msb->regs.param.
  382. * Writes same extra data to blocks, also taken
  383. * from msb->regs.extra
  384. * Returns -EBADMSG if write fails due to uncorrectable error, or -EIO if
  385. * device refuses to take the command or something else
  386. */
  387. static int h_msb_write_block(struct memstick_dev *card,
  388. struct memstick_request **out_mrq)
  389. {
  390. struct msb_data *msb = memstick_get_drvdata(card);
  391. struct memstick_request *mrq = *out_mrq = &card->current_mrq;
  392. struct scatterlist sg[2];
  393. u8 intreg, command;
  394. if (mrq->error)
  395. return msb_exit_state_machine(msb, mrq->error);
  396. again:
  397. switch (msb->state) {
  398. /* HACK: Jmicon handling of TPCs between 8 and
  399. * sizeof(memstick_request.data) is broken due to hardware
  400. * bug in PIO mode that is used for these TPCs
  401. * Therefore split the write
  402. */
  403. case MSB_WB_SEND_WRITE_PARAMS:
  404. if (!msb_write_regs(msb,
  405. offsetof(struct ms_register, param),
  406. sizeof(struct ms_param_register),
  407. &msb->regs.param))
  408. return 0;
  409. msb->state = MSB_WB_SEND_WRITE_OOB;
  410. return 0;
  411. case MSB_WB_SEND_WRITE_OOB:
  412. if (!msb_write_regs(msb,
  413. offsetof(struct ms_register, extra_data),
  414. sizeof(struct ms_extra_data_register),
  415. &msb->regs.extra_data))
  416. return 0;
  417. msb->state = MSB_WB_SEND_WRITE_COMMAND;
  418. return 0;
  419. case MSB_WB_SEND_WRITE_COMMAND:
  420. command = MS_CMD_BLOCK_WRITE;
  421. memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
  422. msb->state = MSB_WB_SEND_INT_REQ;
  423. return 0;
  424. case MSB_WB_SEND_INT_REQ:
  425. msb->state = MSB_WB_RECEIVE_INT_REQ;
  426. if (msb_read_int_reg(msb, -1))
  427. return 0;
  428. fallthrough;
  429. case MSB_WB_RECEIVE_INT_REQ:
  430. intreg = mrq->data[0];
  431. msb->regs.status.interrupt = intreg;
  432. /* errors mean out of here, and fast... */
  433. if (intreg & (MEMSTICK_INT_CMDNAK))
  434. return msb_exit_state_machine(msb, -EIO);
  435. if (intreg & MEMSTICK_INT_ERR)
  436. return msb_exit_state_machine(msb, -EBADMSG);
  437. /* for last page we need to poll CED */
  438. if (msb->current_page == msb->pages_in_block) {
  439. if (intreg & MEMSTICK_INT_CED)
  440. return msb_exit_state_machine(msb, 0);
  441. msb->state = MSB_WB_SEND_INT_REQ;
  442. goto again;
  443. }
  444. /* for non-last page we need BREQ before writing next chunk */
  445. if (!(intreg & MEMSTICK_INT_BREQ)) {
  446. msb->state = MSB_WB_SEND_INT_REQ;
  447. goto again;
  448. }
  449. msb->int_polling = false;
  450. msb->state = MSB_WB_SEND_WRITE_DATA;
  451. fallthrough;
  452. case MSB_WB_SEND_WRITE_DATA:
  453. sg_init_table(sg, ARRAY_SIZE(sg));
  454. if (msb_sg_copy(msb->current_sg, sg, ARRAY_SIZE(sg),
  455. msb->current_sg_offset,
  456. msb->page_size) < msb->page_size)
  457. return msb_exit_state_machine(msb, -EIO);
  458. memstick_init_req_sg(mrq, MS_TPC_WRITE_LONG_DATA, sg);
  459. mrq->need_card_int = 1;
  460. msb->state = MSB_WB_RECEIVE_WRITE_CONFIRMATION;
  461. return 0;
  462. case MSB_WB_RECEIVE_WRITE_CONFIRMATION:
  463. msb->current_page++;
  464. msb->current_sg_offset += msb->page_size;
  465. msb->state = MSB_WB_SEND_INT_REQ;
  466. goto again;
  467. default:
  468. BUG();
  469. }
  470. return 0;
  471. }
  472. /*
  473. * This function is used to send simple IO requests to device that consist
  474. * of register write + command
  475. */
  476. static int h_msb_send_command(struct memstick_dev *card,
  477. struct memstick_request **out_mrq)
  478. {
  479. struct msb_data *msb = memstick_get_drvdata(card);
  480. struct memstick_request *mrq = *out_mrq = &card->current_mrq;
  481. u8 intreg;
  482. if (mrq->error) {
  483. dbg("send_command: unknown error");
  484. return msb_exit_state_machine(msb, mrq->error);
  485. }
  486. again:
  487. switch (msb->state) {
  488. /* HACK: see h_msb_write_block */
  489. case MSB_SC_SEND_WRITE_PARAMS: /* write param register*/
  490. if (!msb_write_regs(msb,
  491. offsetof(struct ms_register, param),
  492. sizeof(struct ms_param_register),
  493. &msb->regs.param))
  494. return 0;
  495. msb->state = MSB_SC_SEND_WRITE_OOB;
  496. return 0;
  497. case MSB_SC_SEND_WRITE_OOB:
  498. if (!msb->command_need_oob) {
  499. msb->state = MSB_SC_SEND_COMMAND;
  500. goto again;
  501. }
  502. if (!msb_write_regs(msb,
  503. offsetof(struct ms_register, extra_data),
  504. sizeof(struct ms_extra_data_register),
  505. &msb->regs.extra_data))
  506. return 0;
  507. msb->state = MSB_SC_SEND_COMMAND;
  508. return 0;
  509. case MSB_SC_SEND_COMMAND:
  510. memstick_init_req(mrq, MS_TPC_SET_CMD, &msb->command_value, 1);
  511. msb->state = MSB_SC_SEND_INT_REQ;
  512. return 0;
  513. case MSB_SC_SEND_INT_REQ:
  514. msb->state = MSB_SC_RECEIVE_INT_REQ;
  515. if (msb_read_int_reg(msb, -1))
  516. return 0;
  517. fallthrough;
  518. case MSB_SC_RECEIVE_INT_REQ:
  519. intreg = mrq->data[0];
  520. if (intreg & MEMSTICK_INT_CMDNAK)
  521. return msb_exit_state_machine(msb, -EIO);
  522. if (intreg & MEMSTICK_INT_ERR)
  523. return msb_exit_state_machine(msb, -EBADMSG);
  524. if (!(intreg & MEMSTICK_INT_CED)) {
  525. msb->state = MSB_SC_SEND_INT_REQ;
  526. goto again;
  527. }
  528. return msb_exit_state_machine(msb, 0);
  529. }
  530. BUG();
  531. }
  532. /* Small handler for card reset */
  533. static int h_msb_reset(struct memstick_dev *card,
  534. struct memstick_request **out_mrq)
  535. {
  536. u8 command = MS_CMD_RESET;
  537. struct msb_data *msb = memstick_get_drvdata(card);
  538. struct memstick_request *mrq = *out_mrq = &card->current_mrq;
  539. if (mrq->error)
  540. return msb_exit_state_machine(msb, mrq->error);
  541. switch (msb->state) {
  542. case MSB_RS_SEND:
  543. memstick_init_req(mrq, MS_TPC_SET_CMD, &command, 1);
  544. mrq->need_card_int = 0;
  545. msb->state = MSB_RS_CONFIRM;
  546. return 0;
  547. case MSB_RS_CONFIRM:
  548. return msb_exit_state_machine(msb, 0);
  549. }
  550. BUG();
  551. }
  552. /* This handler is used to do serial->parallel switch */
  553. static int h_msb_parallel_switch(struct memstick_dev *card,
  554. struct memstick_request **out_mrq)
  555. {
  556. struct msb_data *msb = memstick_get_drvdata(card);
  557. struct memstick_request *mrq = *out_mrq = &card->current_mrq;
  558. struct memstick_host *host = card->host;
  559. if (mrq->error) {
  560. dbg("parallel_switch: error");
  561. msb->regs.param.system &= ~MEMSTICK_SYS_PAM;
  562. return msb_exit_state_machine(msb, mrq->error);
  563. }
  564. switch (msb->state) {
  565. case MSB_PS_SEND_SWITCH_COMMAND:
  566. /* Set the parallel interface on memstick side */
  567. msb->regs.param.system |= MEMSTICK_SYS_PAM;
  568. if (!msb_write_regs(msb,
  569. offsetof(struct ms_register, param),
  570. 1,
  571. (unsigned char *)&msb->regs.param))
  572. return 0;
  573. msb->state = MSB_PS_SWICH_HOST;
  574. return 0;
  575. case MSB_PS_SWICH_HOST:
  576. /* Set parallel interface on our side + send a dummy request
  577. * to see if card responds
  578. */
  579. host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_PAR4);
  580. memstick_init_req(mrq, MS_TPC_GET_INT, NULL, 1);
  581. msb->state = MSB_PS_CONFIRM;
  582. return 0;
  583. case MSB_PS_CONFIRM:
  584. return msb_exit_state_machine(msb, 0);
  585. }
  586. BUG();
  587. }
  588. static int msb_switch_to_parallel(struct msb_data *msb);
  589. /* Reset the card, to guard against hw errors beeing treated as bad blocks */
  590. static int msb_reset(struct msb_data *msb, bool full)
  591. {
  592. bool was_parallel = msb->regs.param.system & MEMSTICK_SYS_PAM;
  593. struct memstick_dev *card = msb->card;
  594. struct memstick_host *host = card->host;
  595. int error;
  596. /* Reset the card */
  597. msb->regs.param.system = MEMSTICK_SYS_BAMD;
  598. if (full) {
  599. error = host->set_param(host,
  600. MEMSTICK_POWER, MEMSTICK_POWER_OFF);
  601. if (error)
  602. goto out_error;
  603. msb_invalidate_reg_window(msb);
  604. error = host->set_param(host,
  605. MEMSTICK_POWER, MEMSTICK_POWER_ON);
  606. if (error)
  607. goto out_error;
  608. error = host->set_param(host,
  609. MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
  610. if (error) {
  611. out_error:
  612. dbg("Failed to reset the host controller");
  613. msb->read_only = true;
  614. return -EFAULT;
  615. }
  616. }
  617. error = msb_run_state_machine(msb, h_msb_reset);
  618. if (error) {
  619. dbg("Failed to reset the card");
  620. msb->read_only = true;
  621. return -ENODEV;
  622. }
  623. /* Set parallel mode */
  624. if (was_parallel)
  625. msb_switch_to_parallel(msb);
  626. return 0;
  627. }
  628. /* Attempts to switch interface to parallel mode */
  629. static int msb_switch_to_parallel(struct msb_data *msb)
  630. {
  631. int error;
  632. error = msb_run_state_machine(msb, h_msb_parallel_switch);
  633. if (error) {
  634. pr_err("Switch to parallel failed");
  635. msb->regs.param.system &= ~MEMSTICK_SYS_PAM;
  636. msb_reset(msb, true);
  637. return -EFAULT;
  638. }
  639. msb->caps |= MEMSTICK_CAP_AUTO_GET_INT;
  640. return 0;
  641. }
  642. /* Changes overwrite flag on a page */
  643. static int msb_set_overwrite_flag(struct msb_data *msb,
  644. u16 pba, u8 page, u8 flag)
  645. {
  646. if (msb->read_only)
  647. return -EROFS;
  648. msb->regs.param.block_address = cpu_to_be16(pba);
  649. msb->regs.param.page_address = page;
  650. msb->regs.param.cp = MEMSTICK_CP_OVERWRITE;
  651. msb->regs.extra_data.overwrite_flag = flag;
  652. msb->command_value = MS_CMD_BLOCK_WRITE;
  653. msb->command_need_oob = true;
  654. dbg_verbose("changing overwrite flag to %02x for sector %d, page %d",
  655. flag, pba, page);
  656. return msb_run_state_machine(msb, h_msb_send_command);
  657. }
  658. static int msb_mark_bad(struct msb_data *msb, int pba)
  659. {
  660. pr_notice("marking pba %d as bad", pba);
  661. msb_reset(msb, true);
  662. return msb_set_overwrite_flag(
  663. msb, pba, 0, 0xFF & ~MEMSTICK_OVERWRITE_BKST);
  664. }
  665. static int msb_mark_page_bad(struct msb_data *msb, int pba, int page)
  666. {
  667. dbg("marking page %d of pba %d as bad", page, pba);
  668. msb_reset(msb, true);
  669. return msb_set_overwrite_flag(msb,
  670. pba, page, ~MEMSTICK_OVERWRITE_PGST0);
  671. }
  672. /* Erases one physical block */
  673. static int msb_erase_block(struct msb_data *msb, u16 pba)
  674. {
  675. int error, try;
  676. if (msb->read_only)
  677. return -EROFS;
  678. dbg_verbose("erasing pba %d", pba);
  679. for (try = 1; try < 3; try++) {
  680. msb->regs.param.block_address = cpu_to_be16(pba);
  681. msb->regs.param.page_address = 0;
  682. msb->regs.param.cp = MEMSTICK_CP_BLOCK;
  683. msb->command_value = MS_CMD_BLOCK_ERASE;
  684. msb->command_need_oob = false;
  685. error = msb_run_state_machine(msb, h_msb_send_command);
  686. if (!error || msb_reset(msb, true))
  687. break;
  688. }
  689. if (error) {
  690. pr_err("erase failed, marking pba %d as bad", pba);
  691. msb_mark_bad(msb, pba);
  692. }
  693. dbg_verbose("erase success, marking pba %d as unused", pba);
  694. msb_mark_block_unused(msb, pba);
  695. __set_bit(pba, msb->erased_blocks_bitmap);
  696. return error;
  697. }
  698. /* Reads one page from device */
  699. static int msb_read_page(struct msb_data *msb,
  700. u16 pba, u8 page, struct ms_extra_data_register *extra,
  701. struct scatterlist *sg, int offset)
  702. {
  703. int try, error;
  704. if (pba == MS_BLOCK_INVALID) {
  705. unsigned long flags;
  706. struct sg_mapping_iter miter;
  707. size_t len = msb->page_size;
  708. dbg_verbose("read unmapped sector. returning 0xFF");
  709. local_irq_save(flags);
  710. sg_miter_start(&miter, sg, sg_nents(sg),
  711. SG_MITER_ATOMIC | SG_MITER_TO_SG);
  712. while (sg_miter_next(&miter) && len > 0) {
  713. int chunklen;
  714. if (offset && offset >= miter.length) {
  715. offset -= miter.length;
  716. continue;
  717. }
  718. chunklen = min(miter.length - offset, len);
  719. memset(miter.addr + offset, 0xFF, chunklen);
  720. len -= chunklen;
  721. offset = 0;
  722. }
  723. sg_miter_stop(&miter);
  724. local_irq_restore(flags);
  725. if (offset)
  726. return -EFAULT;
  727. if (extra)
  728. memset(extra, 0xFF, sizeof(*extra));
  729. return 0;
  730. }
  731. if (pba >= msb->block_count) {
  732. pr_err("BUG: attempt to read beyond the end of the card at pba %d", pba);
  733. return -EINVAL;
  734. }
  735. for (try = 1; try < 3; try++) {
  736. msb->regs.param.block_address = cpu_to_be16(pba);
  737. msb->regs.param.page_address = page;
  738. msb->regs.param.cp = MEMSTICK_CP_PAGE;
  739. msb->current_sg = sg;
  740. msb->current_sg_offset = offset;
  741. error = msb_run_state_machine(msb, h_msb_read_page);
  742. if (error == -EUCLEAN) {
  743. pr_notice("correctable error on pba %d, page %d",
  744. pba, page);
  745. error = 0;
  746. }
  747. if (!error && extra)
  748. *extra = msb->regs.extra_data;
  749. if (!error || msb_reset(msb, true))
  750. break;
  751. }
  752. /* Mark bad pages */
  753. if (error == -EBADMSG) {
  754. pr_err("uncorrectable error on read of pba %d, page %d",
  755. pba, page);
  756. if (msb->regs.extra_data.overwrite_flag &
  757. MEMSTICK_OVERWRITE_PGST0)
  758. msb_mark_page_bad(msb, pba, page);
  759. return -EBADMSG;
  760. }
  761. if (error)
  762. pr_err("read of pba %d, page %d failed with error %d",
  763. pba, page, error);
  764. return error;
  765. }
  766. /* Reads oob of page only */
  767. static int msb_read_oob(struct msb_data *msb, u16 pba, u16 page,
  768. struct ms_extra_data_register *extra)
  769. {
  770. int error;
  771. BUG_ON(!extra);
  772. msb->regs.param.block_address = cpu_to_be16(pba);
  773. msb->regs.param.page_address = page;
  774. msb->regs.param.cp = MEMSTICK_CP_EXTRA;
  775. if (pba > msb->block_count) {
  776. pr_err("BUG: attempt to read beyond the end of card at pba %d", pba);
  777. return -EINVAL;
  778. }
  779. error = msb_run_state_machine(msb, h_msb_read_page);
  780. *extra = msb->regs.extra_data;
  781. if (error == -EUCLEAN) {
  782. pr_notice("correctable error on pba %d, page %d",
  783. pba, page);
  784. return 0;
  785. }
  786. return error;
  787. }
  788. /* Reads a block and compares it with data contained in scatterlist orig_sg */
  789. static int msb_verify_block(struct msb_data *msb, u16 pba,
  790. struct scatterlist *orig_sg, int offset)
  791. {
  792. struct scatterlist sg;
  793. int page = 0, error;
  794. sg_init_one(&sg, msb->block_buffer, msb->block_size);
  795. while (page < msb->pages_in_block) {
  796. error = msb_read_page(msb, pba, page,
  797. NULL, &sg, page * msb->page_size);
  798. if (error)
  799. return error;
  800. page++;
  801. }
  802. if (msb_sg_compare_to_buffer(orig_sg, offset,
  803. msb->block_buffer, msb->block_size))
  804. return -EIO;
  805. return 0;
  806. }
  807. /* Writes exactly one block + oob */
  808. static int msb_write_block(struct msb_data *msb,
  809. u16 pba, u32 lba, struct scatterlist *sg, int offset)
  810. {
  811. int error, current_try = 1;
  812. BUG_ON(sg->length < msb->page_size);
  813. if (msb->read_only)
  814. return -EROFS;
  815. if (pba == MS_BLOCK_INVALID) {
  816. pr_err(
  817. "BUG: write: attempt to write MS_BLOCK_INVALID block");
  818. return -EINVAL;
  819. }
  820. if (pba >= msb->block_count || lba >= msb->logical_block_count) {
  821. pr_err(
  822. "BUG: write: attempt to write beyond the end of device");
  823. return -EINVAL;
  824. }
  825. if (msb_get_zone_from_lba(lba) != msb_get_zone_from_pba(pba)) {
  826. pr_err("BUG: write: lba zone mismatch");
  827. return -EINVAL;
  828. }
  829. if (pba == msb->boot_block_locations[0] ||
  830. pba == msb->boot_block_locations[1]) {
  831. pr_err("BUG: write: attempt to write to boot blocks!");
  832. return -EINVAL;
  833. }
  834. while (1) {
  835. if (msb->read_only)
  836. return -EROFS;
  837. msb->regs.param.cp = MEMSTICK_CP_BLOCK;
  838. msb->regs.param.page_address = 0;
  839. msb->regs.param.block_address = cpu_to_be16(pba);
  840. msb->regs.extra_data.management_flag = 0xFF;
  841. msb->regs.extra_data.overwrite_flag = 0xF8;
  842. msb->regs.extra_data.logical_address = cpu_to_be16(lba);
  843. msb->current_sg = sg;
  844. msb->current_sg_offset = offset;
  845. msb->current_page = 0;
  846. error = msb_run_state_machine(msb, h_msb_write_block);
  847. /* Sector we just wrote to is assumed erased since its pba
  848. * was erased. If it wasn't erased, write will succeed
  849. * and will just clear the bits that were set in the block
  850. * thus test that what we have written,
  851. * matches what we expect.
  852. * We do trust the blocks that we erased
  853. */
  854. if (!error && (verify_writes ||
  855. !test_bit(pba, msb->erased_blocks_bitmap)))
  856. error = msb_verify_block(msb, pba, sg, offset);
  857. if (!error)
  858. break;
  859. if (current_try > 1 || msb_reset(msb, true))
  860. break;
  861. pr_err("write failed, trying to erase the pba %d", pba);
  862. error = msb_erase_block(msb, pba);
  863. if (error)
  864. break;
  865. current_try++;
  866. }
  867. return error;
  868. }
  869. /* Finds a free block for write replacement */
  870. static u16 msb_get_free_block(struct msb_data *msb, int zone)
  871. {
  872. u16 pos;
  873. int pba = zone * MS_BLOCKS_IN_ZONE;
  874. int i;
  875. get_random_bytes(&pos, sizeof(pos));
  876. if (!msb->free_block_count[zone]) {
  877. pr_err("NO free blocks in the zone %d, to use for a write, (media is WORN out) switching to RO mode", zone);
  878. msb->read_only = true;
  879. return MS_BLOCK_INVALID;
  880. }
  881. pos %= msb->free_block_count[zone];
  882. dbg_verbose("have %d choices for a free block, selected randomly: %d",
  883. msb->free_block_count[zone], pos);
  884. pba = find_next_zero_bit(msb->used_blocks_bitmap,
  885. msb->block_count, pba);
  886. for (i = 0; i < pos; ++i)
  887. pba = find_next_zero_bit(msb->used_blocks_bitmap,
  888. msb->block_count, pba + 1);
  889. dbg_verbose("result of the free blocks scan: pba %d", pba);
  890. if (pba == msb->block_count || (msb_get_zone_from_pba(pba)) != zone) {
  891. pr_err("BUG: can't get a free block");
  892. msb->read_only = true;
  893. return MS_BLOCK_INVALID;
  894. }
  895. msb_mark_block_used(msb, pba);
  896. return pba;
  897. }
  898. static int msb_update_block(struct msb_data *msb, u16 lba,
  899. struct scatterlist *sg, int offset)
  900. {
  901. u16 pba, new_pba;
  902. int error, try;
  903. pba = msb->lba_to_pba_table[lba];
  904. dbg_verbose("start of a block update at lba %d, pba %d", lba, pba);
  905. if (pba != MS_BLOCK_INVALID) {
  906. dbg_verbose("setting the update flag on the block");
  907. msb_set_overwrite_flag(msb, pba, 0,
  908. 0xFF & ~MEMSTICK_OVERWRITE_UDST);
  909. }
  910. for (try = 0; try < 3; try++) {
  911. new_pba = msb_get_free_block(msb,
  912. msb_get_zone_from_lba(lba));
  913. if (new_pba == MS_BLOCK_INVALID) {
  914. error = -EIO;
  915. goto out;
  916. }
  917. dbg_verbose("block update: writing updated block to the pba %d",
  918. new_pba);
  919. error = msb_write_block(msb, new_pba, lba, sg, offset);
  920. if (error == -EBADMSG) {
  921. msb_mark_bad(msb, new_pba);
  922. continue;
  923. }
  924. if (error)
  925. goto out;
  926. dbg_verbose("block update: erasing the old block");
  927. msb_erase_block(msb, pba);
  928. msb->lba_to_pba_table[lba] = new_pba;
  929. return 0;
  930. }
  931. out:
  932. if (error) {
  933. pr_err("block update error after %d tries, switching to r/o mode", try);
  934. msb->read_only = true;
  935. }
  936. return error;
  937. }
  938. /* Converts endiannes in the boot block for easy use */
  939. static void msb_fix_boot_page_endianness(struct ms_boot_page *p)
  940. {
  941. p->header.block_id = be16_to_cpu(p->header.block_id);
  942. p->header.format_reserved = be16_to_cpu(p->header.format_reserved);
  943. p->entry.disabled_block.start_addr
  944. = be32_to_cpu(p->entry.disabled_block.start_addr);
  945. p->entry.disabled_block.data_size
  946. = be32_to_cpu(p->entry.disabled_block.data_size);
  947. p->entry.cis_idi.start_addr
  948. = be32_to_cpu(p->entry.cis_idi.start_addr);
  949. p->entry.cis_idi.data_size
  950. = be32_to_cpu(p->entry.cis_idi.data_size);
  951. p->attr.block_size = be16_to_cpu(p->attr.block_size);
  952. p->attr.number_of_blocks = be16_to_cpu(p->attr.number_of_blocks);
  953. p->attr.number_of_effective_blocks
  954. = be16_to_cpu(p->attr.number_of_effective_blocks);
  955. p->attr.page_size = be16_to_cpu(p->attr.page_size);
  956. p->attr.memory_manufacturer_code
  957. = be16_to_cpu(p->attr.memory_manufacturer_code);
  958. p->attr.memory_device_code = be16_to_cpu(p->attr.memory_device_code);
  959. p->attr.implemented_capacity
  960. = be16_to_cpu(p->attr.implemented_capacity);
  961. p->attr.controller_number = be16_to_cpu(p->attr.controller_number);
  962. p->attr.controller_function = be16_to_cpu(p->attr.controller_function);
  963. }
  964. static int msb_read_boot_blocks(struct msb_data *msb)
  965. {
  966. int pba = 0;
  967. struct scatterlist sg;
  968. struct ms_extra_data_register extra;
  969. struct ms_boot_page *page;
  970. msb->boot_block_locations[0] = MS_BLOCK_INVALID;
  971. msb->boot_block_locations[1] = MS_BLOCK_INVALID;
  972. msb->boot_block_count = 0;
  973. dbg_verbose("Start of a scan for the boot blocks");
  974. if (!msb->boot_page) {
  975. page = kmalloc_objs(struct ms_boot_page, 2);
  976. if (!page)
  977. return -ENOMEM;
  978. msb->boot_page = page;
  979. } else
  980. page = msb->boot_page;
  981. msb->block_count = MS_BLOCK_MAX_BOOT_ADDR;
  982. for (pba = 0; pba < MS_BLOCK_MAX_BOOT_ADDR; pba++) {
  983. sg_init_one(&sg, page, sizeof(*page));
  984. if (msb_read_page(msb, pba, 0, &extra, &sg, 0)) {
  985. dbg("boot scan: can't read pba %d", pba);
  986. continue;
  987. }
  988. if (extra.management_flag & MEMSTICK_MANAGEMENT_SYSFLG) {
  989. dbg("management flag doesn't indicate boot block %d",
  990. pba);
  991. continue;
  992. }
  993. if (be16_to_cpu(page->header.block_id) != MS_BLOCK_BOOT_ID) {
  994. dbg("the pba at %d doesn't contain boot block ID", pba);
  995. continue;
  996. }
  997. msb_fix_boot_page_endianness(page);
  998. msb->boot_block_locations[msb->boot_block_count] = pba;
  999. page++;
  1000. msb->boot_block_count++;
  1001. if (msb->boot_block_count == 2)
  1002. break;
  1003. }
  1004. if (!msb->boot_block_count) {
  1005. pr_err("media doesn't contain master page, aborting");
  1006. return -EIO;
  1007. }
  1008. dbg_verbose("End of scan for boot blocks");
  1009. return 0;
  1010. }
  1011. static int msb_read_bad_block_table(struct msb_data *msb, int block_nr)
  1012. {
  1013. struct ms_boot_page *boot_block;
  1014. struct scatterlist sg;
  1015. u16 *buffer = NULL;
  1016. int offset = 0;
  1017. int i, error = 0;
  1018. int data_size, data_offset, page, page_offset, size_to_read;
  1019. u16 pba;
  1020. BUG_ON(block_nr > 1);
  1021. boot_block = &msb->boot_page[block_nr];
  1022. pba = msb->boot_block_locations[block_nr];
  1023. if (msb->boot_block_locations[block_nr] == MS_BLOCK_INVALID)
  1024. return -EINVAL;
  1025. data_size = boot_block->entry.disabled_block.data_size;
  1026. data_offset = sizeof(struct ms_boot_page) +
  1027. boot_block->entry.disabled_block.start_addr;
  1028. if (!data_size)
  1029. return 0;
  1030. page = data_offset / msb->page_size;
  1031. page_offset = data_offset % msb->page_size;
  1032. size_to_read =
  1033. DIV_ROUND_UP(data_size + page_offset, msb->page_size) *
  1034. msb->page_size;
  1035. dbg("reading bad block of boot block at pba %d, offset %d len %d",
  1036. pba, data_offset, data_size);
  1037. buffer = kzalloc(size_to_read, GFP_KERNEL);
  1038. if (!buffer)
  1039. return -ENOMEM;
  1040. /* Read the buffer */
  1041. sg_init_one(&sg, buffer, size_to_read);
  1042. while (offset < size_to_read) {
  1043. error = msb_read_page(msb, pba, page, NULL, &sg, offset);
  1044. if (error)
  1045. goto out;
  1046. page++;
  1047. offset += msb->page_size;
  1048. if (page == msb->pages_in_block) {
  1049. pr_err(
  1050. "bad block table extends beyond the boot block");
  1051. break;
  1052. }
  1053. }
  1054. /* Process the bad block table */
  1055. for (i = page_offset; i < data_size / sizeof(u16); i++) {
  1056. u16 bad_block = be16_to_cpu(buffer[i]);
  1057. if (bad_block >= msb->block_count) {
  1058. dbg("bad block table contains invalid block %d",
  1059. bad_block);
  1060. continue;
  1061. }
  1062. if (test_bit(bad_block, msb->used_blocks_bitmap)) {
  1063. dbg("duplicate bad block %d in the table",
  1064. bad_block);
  1065. continue;
  1066. }
  1067. dbg("block %d is marked as factory bad", bad_block);
  1068. msb_mark_block_used(msb, bad_block);
  1069. }
  1070. out:
  1071. kfree(buffer);
  1072. return error;
  1073. }
  1074. static int msb_ftl_initialize(struct msb_data *msb)
  1075. {
  1076. int i;
  1077. if (msb->ftl_initialized)
  1078. return 0;
  1079. msb->zone_count = msb->block_count / MS_BLOCKS_IN_ZONE;
  1080. msb->logical_block_count = msb->zone_count * 496 - 2;
  1081. msb->used_blocks_bitmap = bitmap_zalloc(msb->block_count, GFP_KERNEL);
  1082. msb->erased_blocks_bitmap = bitmap_zalloc(msb->block_count, GFP_KERNEL);
  1083. msb->lba_to_pba_table =
  1084. kmalloc_array(msb->logical_block_count, sizeof(u16),
  1085. GFP_KERNEL);
  1086. if (!msb->used_blocks_bitmap || !msb->lba_to_pba_table ||
  1087. !msb->erased_blocks_bitmap) {
  1088. bitmap_free(msb->used_blocks_bitmap);
  1089. bitmap_free(msb->erased_blocks_bitmap);
  1090. kfree(msb->lba_to_pba_table);
  1091. return -ENOMEM;
  1092. }
  1093. for (i = 0; i < msb->zone_count; i++)
  1094. msb->free_block_count[i] = MS_BLOCKS_IN_ZONE;
  1095. memset(msb->lba_to_pba_table, MS_BLOCK_INVALID,
  1096. msb->logical_block_count * sizeof(u16));
  1097. dbg("initial FTL tables created. Zone count = %d, Logical block count = %d",
  1098. msb->zone_count, msb->logical_block_count);
  1099. msb->ftl_initialized = true;
  1100. return 0;
  1101. }
  1102. static int msb_ftl_scan(struct msb_data *msb)
  1103. {
  1104. u16 pba, lba, other_block;
  1105. u8 overwrite_flag, management_flag, other_overwrite_flag;
  1106. int error;
  1107. struct ms_extra_data_register extra;
  1108. u8 *overwrite_flags = kzalloc(msb->block_count, GFP_KERNEL);
  1109. if (!overwrite_flags)
  1110. return -ENOMEM;
  1111. dbg("Start of media scanning");
  1112. for (pba = 0; pba < msb->block_count; pba++) {
  1113. if (pba == msb->boot_block_locations[0] ||
  1114. pba == msb->boot_block_locations[1]) {
  1115. dbg_verbose("pba %05d -> [boot block]", pba);
  1116. msb_mark_block_used(msb, pba);
  1117. continue;
  1118. }
  1119. if (test_bit(pba, msb->used_blocks_bitmap)) {
  1120. dbg_verbose("pba %05d -> [factory bad]", pba);
  1121. continue;
  1122. }
  1123. memset(&extra, 0, sizeof(extra));
  1124. error = msb_read_oob(msb, pba, 0, &extra);
  1125. /* can't trust the page if we can't read the oob */
  1126. if (error == -EBADMSG) {
  1127. pr_notice(
  1128. "oob of pba %d damaged, will try to erase it", pba);
  1129. msb_mark_block_used(msb, pba);
  1130. msb_erase_block(msb, pba);
  1131. continue;
  1132. } else if (error) {
  1133. pr_err("unknown error %d on read of oob of pba %d - aborting",
  1134. error, pba);
  1135. kfree(overwrite_flags);
  1136. return error;
  1137. }
  1138. lba = be16_to_cpu(extra.logical_address);
  1139. management_flag = extra.management_flag;
  1140. overwrite_flag = extra.overwrite_flag;
  1141. overwrite_flags[pba] = overwrite_flag;
  1142. /* Skip bad blocks */
  1143. if (!(overwrite_flag & MEMSTICK_OVERWRITE_BKST)) {
  1144. dbg("pba %05d -> [BAD]", pba);
  1145. msb_mark_block_used(msb, pba);
  1146. continue;
  1147. }
  1148. /* Skip system/drm blocks */
  1149. if ((management_flag & MEMSTICK_MANAGEMENT_FLAG_NORMAL) !=
  1150. MEMSTICK_MANAGEMENT_FLAG_NORMAL) {
  1151. dbg("pba %05d -> [reserved management flag %02x]",
  1152. pba, management_flag);
  1153. msb_mark_block_used(msb, pba);
  1154. continue;
  1155. }
  1156. /* Erase temporary tables */
  1157. if (!(management_flag & MEMSTICK_MANAGEMENT_ATFLG)) {
  1158. dbg("pba %05d -> [temp table] - will erase", pba);
  1159. msb_mark_block_used(msb, pba);
  1160. msb_erase_block(msb, pba);
  1161. continue;
  1162. }
  1163. if (lba == MS_BLOCK_INVALID) {
  1164. dbg_verbose("pba %05d -> [free]", pba);
  1165. continue;
  1166. }
  1167. msb_mark_block_used(msb, pba);
  1168. /* Block has LBA not according to zoning*/
  1169. if (msb_get_zone_from_lba(lba) != msb_get_zone_from_pba(pba)) {
  1170. pr_notice("pba %05d -> [bad lba %05d] - will erase",
  1171. pba, lba);
  1172. msb_erase_block(msb, pba);
  1173. continue;
  1174. }
  1175. /* No collisions - great */
  1176. if (msb->lba_to_pba_table[lba] == MS_BLOCK_INVALID) {
  1177. dbg_verbose("pba %05d -> [lba %05d]", pba, lba);
  1178. msb->lba_to_pba_table[lba] = pba;
  1179. continue;
  1180. }
  1181. other_block = msb->lba_to_pba_table[lba];
  1182. other_overwrite_flag = overwrite_flags[other_block];
  1183. pr_notice("Collision between pba %d and pba %d",
  1184. pba, other_block);
  1185. if (!(overwrite_flag & MEMSTICK_OVERWRITE_UDST)) {
  1186. pr_notice("pba %d is marked as stable, use it", pba);
  1187. msb_erase_block(msb, other_block);
  1188. msb->lba_to_pba_table[lba] = pba;
  1189. continue;
  1190. }
  1191. if (!(other_overwrite_flag & MEMSTICK_OVERWRITE_UDST)) {
  1192. pr_notice("pba %d is marked as stable, use it",
  1193. other_block);
  1194. msb_erase_block(msb, pba);
  1195. continue;
  1196. }
  1197. pr_notice("collision between blocks %d and %d, without stable flag set on both, erasing pba %d",
  1198. pba, other_block, other_block);
  1199. msb_erase_block(msb, other_block);
  1200. msb->lba_to_pba_table[lba] = pba;
  1201. }
  1202. dbg("End of media scanning");
  1203. kfree(overwrite_flags);
  1204. return 0;
  1205. }
  1206. static void msb_cache_flush_timer(struct timer_list *t)
  1207. {
  1208. struct msb_data *msb = timer_container_of(msb, t, cache_flush_timer);
  1209. msb->need_flush_cache = true;
  1210. queue_work(msb->io_queue, &msb->io_work);
  1211. }
  1212. static void msb_cache_discard(struct msb_data *msb)
  1213. {
  1214. if (msb->cache_block_lba == MS_BLOCK_INVALID)
  1215. return;
  1216. timer_delete_sync(&msb->cache_flush_timer);
  1217. dbg_verbose("Discarding the write cache");
  1218. msb->cache_block_lba = MS_BLOCK_INVALID;
  1219. bitmap_zero(&msb->valid_cache_bitmap, msb->pages_in_block);
  1220. }
  1221. static int msb_cache_init(struct msb_data *msb)
  1222. {
  1223. timer_setup(&msb->cache_flush_timer, msb_cache_flush_timer, 0);
  1224. if (!msb->cache)
  1225. msb->cache = kzalloc(msb->block_size, GFP_KERNEL);
  1226. if (!msb->cache)
  1227. return -ENOMEM;
  1228. msb_cache_discard(msb);
  1229. return 0;
  1230. }
  1231. static int msb_cache_flush(struct msb_data *msb)
  1232. {
  1233. struct scatterlist sg;
  1234. struct ms_extra_data_register extra;
  1235. int page, offset, error;
  1236. u16 pba, lba;
  1237. if (msb->read_only)
  1238. return -EROFS;
  1239. if (msb->cache_block_lba == MS_BLOCK_INVALID)
  1240. return 0;
  1241. lba = msb->cache_block_lba;
  1242. pba = msb->lba_to_pba_table[lba];
  1243. dbg_verbose("Flushing the write cache of pba %d (LBA %d)",
  1244. pba, msb->cache_block_lba);
  1245. sg_init_one(&sg, msb->cache , msb->block_size);
  1246. /* Read all missing pages in cache */
  1247. for (page = 0; page < msb->pages_in_block; page++) {
  1248. if (test_bit(page, &msb->valid_cache_bitmap))
  1249. continue;
  1250. offset = page * msb->page_size;
  1251. dbg_verbose("reading non-present sector %d of cache block %d",
  1252. page, lba);
  1253. error = msb_read_page(msb, pba, page, &extra, &sg, offset);
  1254. /* Bad pages are copied with 00 page status */
  1255. if (error == -EBADMSG) {
  1256. pr_err("read error on sector %d, contents probably damaged", page);
  1257. continue;
  1258. }
  1259. if (error)
  1260. return error;
  1261. if ((extra.overwrite_flag & MEMSTICK_OV_PG_NORMAL) !=
  1262. MEMSTICK_OV_PG_NORMAL) {
  1263. dbg("page %d is marked as bad", page);
  1264. continue;
  1265. }
  1266. set_bit(page, &msb->valid_cache_bitmap);
  1267. }
  1268. /* Write the cache now */
  1269. error = msb_update_block(msb, msb->cache_block_lba, &sg, 0);
  1270. pba = msb->lba_to_pba_table[msb->cache_block_lba];
  1271. /* Mark invalid pages */
  1272. if (!error) {
  1273. for (page = 0; page < msb->pages_in_block; page++) {
  1274. if (test_bit(page, &msb->valid_cache_bitmap))
  1275. continue;
  1276. dbg("marking page %d as containing damaged data",
  1277. page);
  1278. msb_set_overwrite_flag(msb,
  1279. pba , page, 0xFF & ~MEMSTICK_OV_PG_NORMAL);
  1280. }
  1281. }
  1282. msb_cache_discard(msb);
  1283. return error;
  1284. }
  1285. static int msb_cache_write(struct msb_data *msb, int lba,
  1286. int page, bool add_to_cache_only, struct scatterlist *sg, int offset)
  1287. {
  1288. int error;
  1289. struct scatterlist sg_tmp[10];
  1290. if (msb->read_only)
  1291. return -EROFS;
  1292. if (msb->cache_block_lba == MS_BLOCK_INVALID ||
  1293. lba != msb->cache_block_lba)
  1294. if (add_to_cache_only)
  1295. return 0;
  1296. /* If we need to write different block */
  1297. if (msb->cache_block_lba != MS_BLOCK_INVALID &&
  1298. lba != msb->cache_block_lba) {
  1299. dbg_verbose("first flush the cache");
  1300. error = msb_cache_flush(msb);
  1301. if (error)
  1302. return error;
  1303. }
  1304. if (msb->cache_block_lba == MS_BLOCK_INVALID) {
  1305. msb->cache_block_lba = lba;
  1306. mod_timer(&msb->cache_flush_timer,
  1307. jiffies + msecs_to_jiffies(cache_flush_timeout));
  1308. }
  1309. dbg_verbose("Write of LBA %d page %d to cache ", lba, page);
  1310. sg_init_table(sg_tmp, ARRAY_SIZE(sg_tmp));
  1311. msb_sg_copy(sg, sg_tmp, ARRAY_SIZE(sg_tmp), offset, msb->page_size);
  1312. sg_copy_to_buffer(sg_tmp, sg_nents(sg_tmp),
  1313. msb->cache + page * msb->page_size, msb->page_size);
  1314. set_bit(page, &msb->valid_cache_bitmap);
  1315. return 0;
  1316. }
  1317. static int msb_cache_read(struct msb_data *msb, int lba,
  1318. int page, struct scatterlist *sg, int offset)
  1319. {
  1320. int pba = msb->lba_to_pba_table[lba];
  1321. struct scatterlist sg_tmp[10];
  1322. int error = 0;
  1323. if (lba == msb->cache_block_lba &&
  1324. test_bit(page, &msb->valid_cache_bitmap)) {
  1325. dbg_verbose("Read of LBA %d (pba %d) sector %d from cache",
  1326. lba, pba, page);
  1327. sg_init_table(sg_tmp, ARRAY_SIZE(sg_tmp));
  1328. msb_sg_copy(sg, sg_tmp, ARRAY_SIZE(sg_tmp),
  1329. offset, msb->page_size);
  1330. sg_copy_from_buffer(sg_tmp, sg_nents(sg_tmp),
  1331. msb->cache + msb->page_size * page,
  1332. msb->page_size);
  1333. } else {
  1334. dbg_verbose("Read of LBA %d (pba %d) sector %d from device",
  1335. lba, pba, page);
  1336. error = msb_read_page(msb, pba, page, NULL, sg, offset);
  1337. if (error)
  1338. return error;
  1339. msb_cache_write(msb, lba, page, true, sg, offset);
  1340. }
  1341. return error;
  1342. }
  1343. /* Emulated geometry table
  1344. * This table content isn't that importaint,
  1345. * One could put here different values, providing that they still
  1346. * cover whole disk.
  1347. * 64 MB entry is what windows reports for my 64M memstick
  1348. */
  1349. static const struct chs_entry chs_table[] = {
  1350. /* size sectors cylinders heads */
  1351. { 4, 16, 247, 2 },
  1352. { 8, 16, 495, 2 },
  1353. { 16, 16, 495, 4 },
  1354. { 32, 16, 991, 4 },
  1355. { 64, 16, 991, 8 },
  1356. {128, 16, 991, 16 },
  1357. { 0 }
  1358. };
  1359. /* Load information about the card */
  1360. static int msb_init_card(struct memstick_dev *card)
  1361. {
  1362. struct msb_data *msb = memstick_get_drvdata(card);
  1363. struct memstick_host *host = card->host;
  1364. struct ms_boot_page *boot_block;
  1365. int error = 0, i, raw_size_in_megs;
  1366. msb->caps = 0;
  1367. if (card->id.class >= MEMSTICK_CLASS_ROM &&
  1368. card->id.class <= MEMSTICK_CLASS_ROM)
  1369. msb->read_only = true;
  1370. msb->state = -1;
  1371. error = msb_reset(msb, false);
  1372. if (error)
  1373. return error;
  1374. /* Due to a bug in Jmicron driver written by Alex Dubov,
  1375. * its serial mode barely works,
  1376. * so we switch to parallel mode right away
  1377. */
  1378. if (host->caps & MEMSTICK_CAP_PAR4)
  1379. msb_switch_to_parallel(msb);
  1380. msb->page_size = sizeof(struct ms_boot_page);
  1381. /* Read the boot page */
  1382. error = msb_read_boot_blocks(msb);
  1383. if (error)
  1384. return -EIO;
  1385. boot_block = &msb->boot_page[0];
  1386. /* Save interesting attributes from boot page */
  1387. msb->block_count = boot_block->attr.number_of_blocks;
  1388. msb->page_size = boot_block->attr.page_size;
  1389. msb->pages_in_block = boot_block->attr.block_size * 2;
  1390. msb->block_size = msb->page_size * msb->pages_in_block;
  1391. if ((size_t)msb->page_size > PAGE_SIZE) {
  1392. /* this isn't supported by linux at all, anyway*/
  1393. dbg("device page %d size isn't supported", msb->page_size);
  1394. return -EINVAL;
  1395. }
  1396. msb->block_buffer = kzalloc(msb->block_size, GFP_KERNEL);
  1397. if (!msb->block_buffer)
  1398. return -ENOMEM;
  1399. raw_size_in_megs = (msb->block_size * msb->block_count) >> 20;
  1400. for (i = 0; chs_table[i].size; i++) {
  1401. if (chs_table[i].size != raw_size_in_megs)
  1402. continue;
  1403. msb->geometry.cylinders = chs_table[i].cyl;
  1404. msb->geometry.heads = chs_table[i].head;
  1405. msb->geometry.sectors = chs_table[i].sec;
  1406. break;
  1407. }
  1408. if (boot_block->attr.transfer_supporting == 1)
  1409. msb->caps |= MEMSTICK_CAP_PAR4;
  1410. if (boot_block->attr.device_type & 0x03)
  1411. msb->read_only = true;
  1412. dbg("Total block count = %d", msb->block_count);
  1413. dbg("Each block consists of %d pages", msb->pages_in_block);
  1414. dbg("Page size = %d bytes", msb->page_size);
  1415. dbg("Parallel mode supported: %d", !!(msb->caps & MEMSTICK_CAP_PAR4));
  1416. dbg("Read only: %d", msb->read_only);
  1417. #if 0
  1418. /* Now we can switch the interface */
  1419. if (host->caps & msb->caps & MEMSTICK_CAP_PAR4)
  1420. msb_switch_to_parallel(msb);
  1421. #endif
  1422. error = msb_cache_init(msb);
  1423. if (error)
  1424. return error;
  1425. error = msb_ftl_initialize(msb);
  1426. if (error)
  1427. return error;
  1428. /* Read the bad block table */
  1429. error = msb_read_bad_block_table(msb, 0);
  1430. if (error && error != -ENOMEM) {
  1431. dbg("failed to read bad block table from primary boot block, trying from backup");
  1432. error = msb_read_bad_block_table(msb, 1);
  1433. }
  1434. if (error)
  1435. return error;
  1436. /* *drum roll* Scan the media */
  1437. error = msb_ftl_scan(msb);
  1438. if (error) {
  1439. pr_err("Scan of media failed");
  1440. return error;
  1441. }
  1442. return 0;
  1443. }
  1444. static int msb_do_write_request(struct msb_data *msb, int lba,
  1445. int page, struct scatterlist *sg, size_t len, int *sucessfuly_written)
  1446. {
  1447. int error = 0;
  1448. off_t offset = 0;
  1449. *sucessfuly_written = 0;
  1450. while (offset < len) {
  1451. if (page == 0 && len - offset >= msb->block_size) {
  1452. if (msb->cache_block_lba == lba)
  1453. msb_cache_discard(msb);
  1454. dbg_verbose("Writing whole lba %d", lba);
  1455. error = msb_update_block(msb, lba, sg, offset);
  1456. if (error)
  1457. return error;
  1458. offset += msb->block_size;
  1459. *sucessfuly_written += msb->block_size;
  1460. lba++;
  1461. continue;
  1462. }
  1463. error = msb_cache_write(msb, lba, page, false, sg, offset);
  1464. if (error)
  1465. return error;
  1466. offset += msb->page_size;
  1467. *sucessfuly_written += msb->page_size;
  1468. page++;
  1469. if (page == msb->pages_in_block) {
  1470. page = 0;
  1471. lba++;
  1472. }
  1473. }
  1474. return 0;
  1475. }
  1476. static int msb_do_read_request(struct msb_data *msb, int lba,
  1477. int page, struct scatterlist *sg, int len, int *sucessfuly_read)
  1478. {
  1479. int error = 0;
  1480. int offset = 0;
  1481. *sucessfuly_read = 0;
  1482. while (offset < len) {
  1483. error = msb_cache_read(msb, lba, page, sg, offset);
  1484. if (error)
  1485. return error;
  1486. offset += msb->page_size;
  1487. *sucessfuly_read += msb->page_size;
  1488. page++;
  1489. if (page == msb->pages_in_block) {
  1490. page = 0;
  1491. lba++;
  1492. }
  1493. }
  1494. return 0;
  1495. }
  1496. static void msb_io_work(struct work_struct *work)
  1497. {
  1498. struct msb_data *msb = container_of(work, struct msb_data, io_work);
  1499. int page, error, len;
  1500. sector_t lba;
  1501. struct scatterlist *sg = msb->prealloc_sg;
  1502. struct request *req;
  1503. dbg_verbose("IO: work started");
  1504. while (1) {
  1505. spin_lock_irq(&msb->q_lock);
  1506. if (msb->need_flush_cache) {
  1507. msb->need_flush_cache = false;
  1508. spin_unlock_irq(&msb->q_lock);
  1509. msb_cache_flush(msb);
  1510. continue;
  1511. }
  1512. req = msb->req;
  1513. if (!req) {
  1514. dbg_verbose("IO: no more requests exiting");
  1515. spin_unlock_irq(&msb->q_lock);
  1516. return;
  1517. }
  1518. spin_unlock_irq(&msb->q_lock);
  1519. /* process the request */
  1520. dbg_verbose("IO: processing new request");
  1521. blk_rq_map_sg(req, sg);
  1522. lba = blk_rq_pos(req);
  1523. sector_div(lba, msb->page_size / 512);
  1524. page = sector_div(lba, msb->pages_in_block);
  1525. if (rq_data_dir(msb->req) == READ)
  1526. error = msb_do_read_request(msb, lba, page, sg,
  1527. blk_rq_bytes(req), &len);
  1528. else
  1529. error = msb_do_write_request(msb, lba, page, sg,
  1530. blk_rq_bytes(req), &len);
  1531. if (len && !blk_update_request(req, BLK_STS_OK, len)) {
  1532. __blk_mq_end_request(req, BLK_STS_OK);
  1533. spin_lock_irq(&msb->q_lock);
  1534. msb->req = NULL;
  1535. spin_unlock_irq(&msb->q_lock);
  1536. }
  1537. if (error && msb->req) {
  1538. blk_status_t ret = errno_to_blk_status(error);
  1539. dbg_verbose("IO: ending one sector of the request with error");
  1540. blk_mq_end_request(req, ret);
  1541. spin_lock_irq(&msb->q_lock);
  1542. msb->req = NULL;
  1543. spin_unlock_irq(&msb->q_lock);
  1544. }
  1545. if (msb->req)
  1546. dbg_verbose("IO: request still pending");
  1547. }
  1548. }
  1549. static DEFINE_IDR(msb_disk_idr); /*set of used disk numbers */
  1550. static DEFINE_MUTEX(msb_disk_lock); /* protects against races in open/release */
  1551. static void msb_data_clear(struct msb_data *msb)
  1552. {
  1553. kfree(msb->boot_page);
  1554. bitmap_free(msb->used_blocks_bitmap);
  1555. bitmap_free(msb->erased_blocks_bitmap);
  1556. kfree(msb->lba_to_pba_table);
  1557. kfree(msb->cache);
  1558. msb->card = NULL;
  1559. }
  1560. static int msb_bd_getgeo(struct gendisk *disk,
  1561. struct hd_geometry *geo)
  1562. {
  1563. struct msb_data *msb = disk->private_data;
  1564. *geo = msb->geometry;
  1565. return 0;
  1566. }
  1567. static void msb_bd_free_disk(struct gendisk *disk)
  1568. {
  1569. struct msb_data *msb = disk->private_data;
  1570. mutex_lock(&msb_disk_lock);
  1571. idr_remove(&msb_disk_idr, msb->disk_id);
  1572. mutex_unlock(&msb_disk_lock);
  1573. kfree(msb);
  1574. }
  1575. static blk_status_t msb_queue_rq(struct blk_mq_hw_ctx *hctx,
  1576. const struct blk_mq_queue_data *bd)
  1577. {
  1578. struct memstick_dev *card = hctx->queue->queuedata;
  1579. struct msb_data *msb = memstick_get_drvdata(card);
  1580. struct request *req = bd->rq;
  1581. dbg_verbose("Submit request");
  1582. spin_lock_irq(&msb->q_lock);
  1583. if (msb->card_dead) {
  1584. dbg("Refusing requests on removed card");
  1585. WARN_ON(!msb->io_queue_stopped);
  1586. spin_unlock_irq(&msb->q_lock);
  1587. blk_mq_start_request(req);
  1588. return BLK_STS_IOERR;
  1589. }
  1590. if (msb->req) {
  1591. spin_unlock_irq(&msb->q_lock);
  1592. return BLK_STS_DEV_RESOURCE;
  1593. }
  1594. blk_mq_start_request(req);
  1595. msb->req = req;
  1596. if (!msb->io_queue_stopped)
  1597. queue_work(msb->io_queue, &msb->io_work);
  1598. spin_unlock_irq(&msb->q_lock);
  1599. return BLK_STS_OK;
  1600. }
  1601. static int msb_check_card(struct memstick_dev *card)
  1602. {
  1603. struct msb_data *msb = memstick_get_drvdata(card);
  1604. return (msb->card_dead == 0);
  1605. }
  1606. static void msb_stop(struct memstick_dev *card)
  1607. {
  1608. struct msb_data *msb = memstick_get_drvdata(card);
  1609. unsigned long flags;
  1610. dbg("Stopping all msblock IO");
  1611. blk_mq_stop_hw_queues(msb->queue);
  1612. spin_lock_irqsave(&msb->q_lock, flags);
  1613. msb->io_queue_stopped = true;
  1614. spin_unlock_irqrestore(&msb->q_lock, flags);
  1615. timer_delete_sync(&msb->cache_flush_timer);
  1616. flush_workqueue(msb->io_queue);
  1617. spin_lock_irqsave(&msb->q_lock, flags);
  1618. if (msb->req) {
  1619. blk_mq_requeue_request(msb->req, false);
  1620. msb->req = NULL;
  1621. }
  1622. spin_unlock_irqrestore(&msb->q_lock, flags);
  1623. }
  1624. static void msb_start(struct memstick_dev *card)
  1625. {
  1626. struct msb_data *msb = memstick_get_drvdata(card);
  1627. unsigned long flags;
  1628. dbg("Resuming IO from msblock");
  1629. msb_invalidate_reg_window(msb);
  1630. spin_lock_irqsave(&msb->q_lock, flags);
  1631. if (!msb->io_queue_stopped || msb->card_dead) {
  1632. spin_unlock_irqrestore(&msb->q_lock, flags);
  1633. return;
  1634. }
  1635. spin_unlock_irqrestore(&msb->q_lock, flags);
  1636. /* Kick cache flush anyway, its harmless */
  1637. msb->need_flush_cache = true;
  1638. msb->io_queue_stopped = false;
  1639. blk_mq_start_hw_queues(msb->queue);
  1640. queue_work(msb->io_queue, &msb->io_work);
  1641. }
  1642. static const struct block_device_operations msb_bdops = {
  1643. .owner = THIS_MODULE,
  1644. .getgeo = msb_bd_getgeo,
  1645. .free_disk = msb_bd_free_disk,
  1646. };
  1647. static const struct blk_mq_ops msb_mq_ops = {
  1648. .queue_rq = msb_queue_rq,
  1649. };
  1650. /* Registers the block device */
  1651. static int msb_init_disk(struct memstick_dev *card)
  1652. {
  1653. struct msb_data *msb = memstick_get_drvdata(card);
  1654. struct queue_limits lim = {
  1655. .logical_block_size = msb->page_size,
  1656. .max_hw_sectors = MS_BLOCK_MAX_PAGES,
  1657. .max_segments = MS_BLOCK_MAX_SEGS,
  1658. .max_segment_size = MS_BLOCK_MAX_PAGES * msb->page_size,
  1659. };
  1660. int rc;
  1661. unsigned long capacity;
  1662. mutex_lock(&msb_disk_lock);
  1663. msb->disk_id = idr_alloc(&msb_disk_idr, card, 0, 256, GFP_KERNEL);
  1664. mutex_unlock(&msb_disk_lock);
  1665. if (msb->disk_id < 0)
  1666. return msb->disk_id;
  1667. rc = blk_mq_alloc_sq_tag_set(&msb->tag_set, &msb_mq_ops, 2, 0);
  1668. if (rc)
  1669. goto out_release_id;
  1670. msb->disk = blk_mq_alloc_disk(&msb->tag_set, &lim, card);
  1671. if (IS_ERR(msb->disk)) {
  1672. rc = PTR_ERR(msb->disk);
  1673. goto out_free_tag_set;
  1674. }
  1675. msb->queue = msb->disk->queue;
  1676. sprintf(msb->disk->disk_name, "msblk%d", msb->disk_id);
  1677. msb->disk->fops = &msb_bdops;
  1678. msb->disk->private_data = msb;
  1679. capacity = msb->pages_in_block * msb->logical_block_count;
  1680. capacity *= (msb->page_size / 512);
  1681. set_capacity(msb->disk, capacity);
  1682. dbg("Set total disk size to %lu sectors", capacity);
  1683. msb->io_queue = alloc_ordered_workqueue("ms_block", WQ_MEM_RECLAIM);
  1684. if (!msb->io_queue) {
  1685. rc = -ENOMEM;
  1686. goto out_cleanup_disk;
  1687. }
  1688. INIT_WORK(&msb->io_work, msb_io_work);
  1689. sg_init_table(msb->prealloc_sg, MS_BLOCK_MAX_SEGS+1);
  1690. if (msb->read_only)
  1691. set_disk_ro(msb->disk, 1);
  1692. msb_start(card);
  1693. rc = device_add_disk(&card->dev, msb->disk, NULL);
  1694. if (rc)
  1695. goto out_destroy_workqueue;
  1696. dbg("Disk added");
  1697. return 0;
  1698. out_destroy_workqueue:
  1699. destroy_workqueue(msb->io_queue);
  1700. out_cleanup_disk:
  1701. put_disk(msb->disk);
  1702. out_free_tag_set:
  1703. blk_mq_free_tag_set(&msb->tag_set);
  1704. out_release_id:
  1705. mutex_lock(&msb_disk_lock);
  1706. idr_remove(&msb_disk_idr, msb->disk_id);
  1707. mutex_unlock(&msb_disk_lock);
  1708. return rc;
  1709. }
  1710. static int msb_probe(struct memstick_dev *card)
  1711. {
  1712. struct msb_data *msb;
  1713. int rc = 0;
  1714. msb = kzalloc_obj(struct msb_data);
  1715. if (!msb)
  1716. return -ENOMEM;
  1717. memstick_set_drvdata(card, msb);
  1718. msb->card = card;
  1719. spin_lock_init(&msb->q_lock);
  1720. rc = msb_init_card(card);
  1721. if (rc)
  1722. goto out_free;
  1723. rc = msb_init_disk(card);
  1724. if (!rc) {
  1725. card->check = msb_check_card;
  1726. card->stop = msb_stop;
  1727. card->start = msb_start;
  1728. return 0;
  1729. }
  1730. out_free:
  1731. memstick_set_drvdata(card, NULL);
  1732. msb_data_clear(msb);
  1733. kfree(msb);
  1734. return rc;
  1735. }
  1736. static void msb_remove(struct memstick_dev *card)
  1737. {
  1738. struct msb_data *msb = memstick_get_drvdata(card);
  1739. unsigned long flags;
  1740. if (!msb->io_queue_stopped)
  1741. msb_stop(card);
  1742. dbg("Removing the disk device");
  1743. /* Take care of unhandled + new requests from now on */
  1744. spin_lock_irqsave(&msb->q_lock, flags);
  1745. msb->card_dead = true;
  1746. spin_unlock_irqrestore(&msb->q_lock, flags);
  1747. blk_mq_start_hw_queues(msb->queue);
  1748. /* Remove the disk */
  1749. del_gendisk(msb->disk);
  1750. blk_mq_free_tag_set(&msb->tag_set);
  1751. msb->queue = NULL;
  1752. mutex_lock(&msb_disk_lock);
  1753. msb_data_clear(msb);
  1754. mutex_unlock(&msb_disk_lock);
  1755. put_disk(msb->disk);
  1756. memstick_set_drvdata(card, NULL);
  1757. }
  1758. #ifdef CONFIG_PM
  1759. static int msb_suspend(struct memstick_dev *card, pm_message_t state)
  1760. {
  1761. msb_stop(card);
  1762. return 0;
  1763. }
  1764. static int msb_resume(struct memstick_dev *card)
  1765. {
  1766. struct msb_data *msb = memstick_get_drvdata(card);
  1767. struct msb_data *new_msb = NULL;
  1768. bool card_dead = true;
  1769. #ifndef CONFIG_MEMSTICK_UNSAFE_RESUME
  1770. msb->card_dead = true;
  1771. return 0;
  1772. #endif
  1773. mutex_lock(&card->host->lock);
  1774. new_msb = kzalloc_obj(struct msb_data);
  1775. if (!new_msb)
  1776. goto out;
  1777. new_msb->card = card;
  1778. memstick_set_drvdata(card, new_msb);
  1779. spin_lock_init(&new_msb->q_lock);
  1780. sg_init_table(msb->prealloc_sg, MS_BLOCK_MAX_SEGS+1);
  1781. if (msb_init_card(card))
  1782. goto out;
  1783. if (msb->block_size != new_msb->block_size)
  1784. goto out;
  1785. if (memcmp(msb->boot_page, new_msb->boot_page,
  1786. sizeof(struct ms_boot_page)))
  1787. goto out;
  1788. if (msb->logical_block_count != new_msb->logical_block_count ||
  1789. memcmp(msb->lba_to_pba_table, new_msb->lba_to_pba_table,
  1790. msb->logical_block_count))
  1791. goto out;
  1792. if (msb->block_count != new_msb->block_count ||
  1793. !bitmap_equal(msb->used_blocks_bitmap, new_msb->used_blocks_bitmap,
  1794. msb->block_count))
  1795. goto out;
  1796. card_dead = false;
  1797. out:
  1798. if (card_dead)
  1799. dbg("Card was removed/replaced during suspend");
  1800. msb->card_dead = card_dead;
  1801. memstick_set_drvdata(card, msb);
  1802. if (new_msb) {
  1803. msb_data_clear(new_msb);
  1804. kfree(new_msb);
  1805. }
  1806. msb_start(card);
  1807. mutex_unlock(&card->host->lock);
  1808. return 0;
  1809. }
  1810. #else
  1811. #define msb_suspend NULL
  1812. #define msb_resume NULL
  1813. #endif /* CONFIG_PM */
  1814. static const struct memstick_device_id msb_id_tbl[] = {
  1815. {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
  1816. MEMSTICK_CLASS_FLASH},
  1817. {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
  1818. MEMSTICK_CLASS_ROM},
  1819. {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
  1820. MEMSTICK_CLASS_RO},
  1821. {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_LEGACY, MEMSTICK_CATEGORY_STORAGE,
  1822. MEMSTICK_CLASS_WP},
  1823. {MEMSTICK_MATCH_ALL, MEMSTICK_TYPE_DUO, MEMSTICK_CATEGORY_STORAGE_DUO,
  1824. MEMSTICK_CLASS_DUO},
  1825. {}
  1826. };
  1827. MODULE_DEVICE_TABLE(memstick, msb_id_tbl);
  1828. static struct memstick_driver msb_driver = {
  1829. .driver = {
  1830. .name = DRIVER_NAME,
  1831. .owner = THIS_MODULE
  1832. },
  1833. .id_table = msb_id_tbl,
  1834. .probe = msb_probe,
  1835. .remove = msb_remove,
  1836. .suspend = msb_suspend,
  1837. .resume = msb_resume
  1838. };
  1839. static int __init msb_init(void)
  1840. {
  1841. int rc = memstick_register_driver(&msb_driver);
  1842. if (rc)
  1843. pr_err("failed to register memstick driver (error %d)\n", rc);
  1844. return rc;
  1845. }
  1846. static void __exit msb_exit(void)
  1847. {
  1848. memstick_unregister_driver(&msb_driver);
  1849. idr_destroy(&msb_disk_idr);
  1850. }
  1851. module_init(msb_init);
  1852. module_exit(msb_exit);
  1853. module_param(cache_flush_timeout, int, S_IRUGO);
  1854. MODULE_PARM_DESC(cache_flush_timeout,
  1855. "Cache flush timeout in msec (1000 default)");
  1856. module_param(debug, int, S_IRUGO | S_IWUSR);
  1857. MODULE_PARM_DESC(debug, "Debug level (0-2)");
  1858. module_param(verify_writes, bool, S_IRUGO);
  1859. MODULE_PARM_DESC(verify_writes, "Read back and check all data that is written");
  1860. MODULE_LICENSE("GPL");
  1861. MODULE_AUTHOR("Maxim Levitsky");
  1862. MODULE_DESCRIPTION("Sony MemoryStick block device driver");