alauda.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /*
  2. * Driver for Alauda-based card readers
  3. *
  4. * Current development and maintenance by:
  5. * (c) 2005 Daniel Drake <dsd@gentoo.org>
  6. *
  7. * The 'Alauda' is a chip manufacturered by RATOC for OEM use.
  8. *
  9. * Alauda implements a vendor-specific command set to access two media reader
  10. * ports (XD, SmartMedia). This driver converts SCSI commands to the commands
  11. * which are accepted by these devices.
  12. *
  13. * The driver was developed through reverse-engineering, with the help of the
  14. * sddr09 driver which has many similarities, and with some help from the
  15. * (very old) vendor-supplied GPL sma03 driver.
  16. *
  17. * For protocol info, see http://alauda.sourceforge.net
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the
  21. * Free Software Foundation; either version 2, or (at your option) any
  22. * later version.
  23. *
  24. * This program is distributed in the hope that it will be useful, but
  25. * WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program; if not, write to the Free Software Foundation, Inc.,
  31. * 675 Mass Ave, Cambridge, MA 02139, USA.
  32. */
  33. #include <scsi/scsi.h>
  34. #include <scsi/scsi_cmnd.h>
  35. #include <scsi/scsi_device.h>
  36. #include "usb.h"
  37. #include "transport.h"
  38. #include "protocol.h"
  39. #include "debug.h"
  40. #include "alauda.h"
  41. #define short_pack(lsb,msb) ( ((u16)(lsb)) | ( ((u16)(msb))<<8 ) )
  42. #define LSB_of(s) ((s)&0xFF)
  43. #define MSB_of(s) ((s)>>8)
  44. #define MEDIA_PORT(us) us->srb->device->lun
  45. #define MEDIA_INFO(us) ((struct alauda_info *)us->extra)->port[MEDIA_PORT(us)]
  46. #define PBA_LO(pba) ((pba & 0xF) << 5)
  47. #define PBA_HI(pba) (pba >> 3)
  48. #define PBA_ZONE(pba) (pba >> 11)
  49. /*
  50. * Media handling
  51. */
  52. struct alauda_card_info {
  53. unsigned char id; /* id byte */
  54. unsigned char chipshift; /* 1<<cs bytes total capacity */
  55. unsigned char pageshift; /* 1<<ps bytes in a page */
  56. unsigned char blockshift; /* 1<<bs pages per block */
  57. unsigned char zoneshift; /* 1<<zs blocks per zone */
  58. };
  59. static struct alauda_card_info alauda_card_ids[] = {
  60. /* NAND flash */
  61. { 0x6e, 20, 8, 4, 8}, /* 1 MB */
  62. { 0xe8, 20, 8, 4, 8}, /* 1 MB */
  63. { 0xec, 20, 8, 4, 8}, /* 1 MB */
  64. { 0x64, 21, 8, 4, 9}, /* 2 MB */
  65. { 0xea, 21, 8, 4, 9}, /* 2 MB */
  66. { 0x6b, 22, 9, 4, 9}, /* 4 MB */
  67. { 0xe3, 22, 9, 4, 9}, /* 4 MB */
  68. { 0xe5, 22, 9, 4, 9}, /* 4 MB */
  69. { 0xe6, 23, 9, 4, 10}, /* 8 MB */
  70. { 0x73, 24, 9, 5, 10}, /* 16 MB */
  71. { 0x75, 25, 9, 5, 10}, /* 32 MB */
  72. { 0x76, 26, 9, 5, 10}, /* 64 MB */
  73. { 0x79, 27, 9, 5, 10}, /* 128 MB */
  74. { 0x71, 28, 9, 5, 10}, /* 256 MB */
  75. /* MASK ROM */
  76. { 0x5d, 21, 9, 4, 8}, /* 2 MB */
  77. { 0xd5, 22, 9, 4, 9}, /* 4 MB */
  78. { 0xd6, 23, 9, 4, 10}, /* 8 MB */
  79. { 0x57, 24, 9, 4, 11}, /* 16 MB */
  80. { 0x58, 25, 9, 4, 12}, /* 32 MB */
  81. { 0,}
  82. };
  83. static struct alauda_card_info *alauda_card_find_id(unsigned char id) {
  84. int i;
  85. for (i = 0; alauda_card_ids[i].id != 0; i++)
  86. if (alauda_card_ids[i].id == id)
  87. return &(alauda_card_ids[i]);
  88. return NULL;
  89. }
  90. /*
  91. * ECC computation.
  92. */
  93. static unsigned char parity[256];
  94. static unsigned char ecc2[256];
  95. static void nand_init_ecc(void) {
  96. int i, j, a;
  97. parity[0] = 0;
  98. for (i = 1; i < 256; i++)
  99. parity[i] = (parity[i&(i-1)] ^ 1);
  100. for (i = 0; i < 256; i++) {
  101. a = 0;
  102. for (j = 0; j < 8; j++) {
  103. if (i & (1<<j)) {
  104. if ((j & 1) == 0)
  105. a ^= 0x04;
  106. if ((j & 2) == 0)
  107. a ^= 0x10;
  108. if ((j & 4) == 0)
  109. a ^= 0x40;
  110. }
  111. }
  112. ecc2[i] = ~(a ^ (a<<1) ^ (parity[i] ? 0xa8 : 0));
  113. }
  114. }
  115. /* compute 3-byte ecc on 256 bytes */
  116. static void nand_compute_ecc(unsigned char *data, unsigned char *ecc) {
  117. int i, j, a;
  118. unsigned char par, bit, bits[8];
  119. par = 0;
  120. for (j = 0; j < 8; j++)
  121. bits[j] = 0;
  122. /* collect 16 checksum bits */
  123. for (i = 0; i < 256; i++) {
  124. par ^= data[i];
  125. bit = parity[data[i]];
  126. for (j = 0; j < 8; j++)
  127. if ((i & (1<<j)) == 0)
  128. bits[j] ^= bit;
  129. }
  130. /* put 4+4+4 = 12 bits in the ecc */
  131. a = (bits[3] << 6) + (bits[2] << 4) + (bits[1] << 2) + bits[0];
  132. ecc[0] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  133. a = (bits[7] << 6) + (bits[6] << 4) + (bits[5] << 2) + bits[4];
  134. ecc[1] = ~(a ^ (a<<1) ^ (parity[par] ? 0xaa : 0));
  135. ecc[2] = ecc2[par];
  136. }
  137. static int nand_compare_ecc(unsigned char *data, unsigned char *ecc) {
  138. return (data[0] == ecc[0] && data[1] == ecc[1] && data[2] == ecc[2]);
  139. }
  140. static void nand_store_ecc(unsigned char *data, unsigned char *ecc) {
  141. memcpy(data, ecc, 3);
  142. }
  143. /*
  144. * Alauda driver
  145. */
  146. /*
  147. * Forget our PBA <---> LBA mappings for a particular port
  148. */
  149. static void alauda_free_maps (struct alauda_media_info *media_info)
  150. {
  151. unsigned int shift = media_info->zoneshift
  152. + media_info->blockshift + media_info->pageshift;
  153. unsigned int num_zones = media_info->capacity >> shift;
  154. unsigned int i;
  155. if (media_info->lba_to_pba != NULL)
  156. for (i = 0; i < num_zones; i++) {
  157. kfree(media_info->lba_to_pba[i]);
  158. media_info->lba_to_pba[i] = NULL;
  159. }
  160. if (media_info->pba_to_lba != NULL)
  161. for (i = 0; i < num_zones; i++) {
  162. kfree(media_info->pba_to_lba[i]);
  163. media_info->pba_to_lba[i] = NULL;
  164. }
  165. }
  166. /*
  167. * Returns 2 bytes of status data
  168. * The first byte describes media status, and second byte describes door status
  169. */
  170. static int alauda_get_media_status(struct us_data *us, unsigned char *data)
  171. {
  172. int rc;
  173. unsigned char command;
  174. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  175. command = ALAUDA_GET_XD_MEDIA_STATUS;
  176. else
  177. command = ALAUDA_GET_SM_MEDIA_STATUS;
  178. rc = usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
  179. command, 0xc0, 0, 1, data, 2);
  180. US_DEBUGP("alauda_get_media_status: Media status %02X %02X\n",
  181. data[0], data[1]);
  182. return rc;
  183. }
  184. /*
  185. * Clears the "media was changed" bit so that we know when it changes again
  186. * in the future.
  187. */
  188. static int alauda_ack_media(struct us_data *us)
  189. {
  190. unsigned char command;
  191. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  192. command = ALAUDA_ACK_XD_MEDIA_CHANGE;
  193. else
  194. command = ALAUDA_ACK_SM_MEDIA_CHANGE;
  195. return usb_stor_ctrl_transfer(us, us->send_ctrl_pipe,
  196. command, 0x40, 0, 1, NULL, 0);
  197. }
  198. /*
  199. * Retrieves a 4-byte media signature, which indicates manufacturer, capacity,
  200. * and some other details.
  201. */
  202. static int alauda_get_media_signature(struct us_data *us, unsigned char *data)
  203. {
  204. unsigned char command;
  205. if (MEDIA_PORT(us) == ALAUDA_PORT_XD)
  206. command = ALAUDA_GET_XD_MEDIA_SIG;
  207. else
  208. command = ALAUDA_GET_SM_MEDIA_SIG;
  209. return usb_stor_ctrl_transfer(us, us->recv_ctrl_pipe,
  210. command, 0xc0, 0, 0, data, 4);
  211. }
  212. /*
  213. * Resets the media status (but not the whole device?)
  214. */
  215. static int alauda_reset_media(struct us_data *us)
  216. {
  217. unsigned char *command = us->iobuf;
  218. memset(command, 0, 9);
  219. command[0] = ALAUDA_BULK_CMD;
  220. command[1] = ALAUDA_BULK_RESET_MEDIA;
  221. command[8] = MEDIA_PORT(us);
  222. return usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  223. command, 9, NULL);
  224. }
  225. /*
  226. * Examines the media and deduces capacity, etc.
  227. */
  228. static int alauda_init_media(struct us_data *us)
  229. {
  230. unsigned char *data = us->iobuf;
  231. int ready = 0;
  232. struct alauda_card_info *media_info;
  233. unsigned int num_zones;
  234. while (ready == 0) {
  235. msleep(20);
  236. if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
  237. return USB_STOR_TRANSPORT_ERROR;
  238. if (data[0] & 0x10)
  239. ready = 1;
  240. }
  241. US_DEBUGP("alauda_init_media: We are ready for action!\n");
  242. if (alauda_ack_media(us) != USB_STOR_XFER_GOOD)
  243. return USB_STOR_TRANSPORT_ERROR;
  244. msleep(10);
  245. if (alauda_get_media_status(us, data) != USB_STOR_XFER_GOOD)
  246. return USB_STOR_TRANSPORT_ERROR;
  247. if (data[0] != 0x14) {
  248. US_DEBUGP("alauda_init_media: Media not ready after ack\n");
  249. return USB_STOR_TRANSPORT_ERROR;
  250. }
  251. if (alauda_get_media_signature(us, data) != USB_STOR_XFER_GOOD)
  252. return USB_STOR_TRANSPORT_ERROR;
  253. US_DEBUGP("alauda_init_media: Media signature: %02X %02X %02X %02X\n",
  254. data[0], data[1], data[2], data[3]);
  255. media_info = alauda_card_find_id(data[1]);
  256. if (media_info == NULL) {
  257. printk(KERN_WARNING
  258. "alauda_init_media: Unrecognised media signature: "
  259. "%02X %02X %02X %02X\n",
  260. data[0], data[1], data[2], data[3]);
  261. return USB_STOR_TRANSPORT_ERROR;
  262. }
  263. MEDIA_INFO(us).capacity = 1 << media_info->chipshift;
  264. US_DEBUGP("Found media with capacity: %ldMB\n",
  265. MEDIA_INFO(us).capacity >> 20);
  266. MEDIA_INFO(us).pageshift = media_info->pageshift;
  267. MEDIA_INFO(us).blockshift = media_info->blockshift;
  268. MEDIA_INFO(us).zoneshift = media_info->zoneshift;
  269. MEDIA_INFO(us).pagesize = 1 << media_info->pageshift;
  270. MEDIA_INFO(us).blocksize = 1 << media_info->blockshift;
  271. MEDIA_INFO(us).zonesize = 1 << media_info->zoneshift;
  272. MEDIA_INFO(us).uzonesize = ((1 << media_info->zoneshift) / 128) * 125;
  273. MEDIA_INFO(us).blockmask = MEDIA_INFO(us).blocksize - 1;
  274. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  275. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  276. MEDIA_INFO(us).pba_to_lba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
  277. MEDIA_INFO(us).lba_to_pba = kcalloc(num_zones, sizeof(u16*), GFP_NOIO);
  278. if (alauda_reset_media(us) != USB_STOR_XFER_GOOD)
  279. return USB_STOR_TRANSPORT_ERROR;
  280. return USB_STOR_TRANSPORT_GOOD;
  281. }
  282. /*
  283. * Examines the media status and does the right thing when the media has gone,
  284. * appeared, or changed.
  285. */
  286. static int alauda_check_media(struct us_data *us)
  287. {
  288. struct alauda_info *info = (struct alauda_info *) us->extra;
  289. unsigned char status[2];
  290. int rc;
  291. rc = alauda_get_media_status(us, status);
  292. /* Check for no media or door open */
  293. if ((status[0] & 0x80) || ((status[0] & 0x1F) == 0x10)
  294. || ((status[1] & 0x01) == 0)) {
  295. US_DEBUGP("alauda_check_media: No media, or door open\n");
  296. alauda_free_maps(&MEDIA_INFO(us));
  297. info->sense_key = 0x02;
  298. info->sense_asc = 0x3A;
  299. info->sense_ascq = 0x00;
  300. return USB_STOR_TRANSPORT_FAILED;
  301. }
  302. /* Check for media change */
  303. if (status[0] & 0x08) {
  304. US_DEBUGP("alauda_check_media: Media change detected\n");
  305. alauda_free_maps(&MEDIA_INFO(us));
  306. alauda_init_media(us);
  307. info->sense_key = UNIT_ATTENTION;
  308. info->sense_asc = 0x28;
  309. info->sense_ascq = 0x00;
  310. return USB_STOR_TRANSPORT_FAILED;
  311. }
  312. return USB_STOR_TRANSPORT_GOOD;
  313. }
  314. /*
  315. * Checks the status from the 2nd status register
  316. * Returns 3 bytes of status data, only the first is known
  317. */
  318. static int alauda_check_status2(struct us_data *us)
  319. {
  320. int rc;
  321. unsigned char command[] = {
  322. ALAUDA_BULK_CMD, ALAUDA_BULK_GET_STATUS2,
  323. 0, 0, 0, 0, 3, 0, MEDIA_PORT(us)
  324. };
  325. unsigned char data[3];
  326. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  327. command, 9, NULL);
  328. if (rc != USB_STOR_XFER_GOOD)
  329. return rc;
  330. rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  331. data, 3, NULL);
  332. if (rc != USB_STOR_XFER_GOOD)
  333. return rc;
  334. US_DEBUGP("alauda_check_status2: %02X %02X %02X\n", data[0], data[1], data[2]);
  335. if (data[0] & ALAUDA_STATUS_ERROR)
  336. return USB_STOR_XFER_ERROR;
  337. return USB_STOR_XFER_GOOD;
  338. }
  339. /*
  340. * Gets the redundancy data for the first page of a PBA
  341. * Returns 16 bytes.
  342. */
  343. static int alauda_get_redu_data(struct us_data *us, u16 pba, unsigned char *data)
  344. {
  345. int rc;
  346. unsigned char command[] = {
  347. ALAUDA_BULK_CMD, ALAUDA_BULK_GET_REDU_DATA,
  348. PBA_HI(pba), PBA_ZONE(pba), 0, PBA_LO(pba), 0, 0, MEDIA_PORT(us)
  349. };
  350. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  351. command, 9, NULL);
  352. if (rc != USB_STOR_XFER_GOOD)
  353. return rc;
  354. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  355. data, 16, NULL);
  356. }
  357. /*
  358. * Finds the first unused PBA in a zone
  359. * Returns the absolute PBA of an unused PBA, or 0 if none found.
  360. */
  361. static u16 alauda_find_unused_pba(struct alauda_media_info *info,
  362. unsigned int zone)
  363. {
  364. u16 *pba_to_lba = info->pba_to_lba[zone];
  365. unsigned int i;
  366. for (i = 0; i < info->zonesize; i++)
  367. if (pba_to_lba[i] == UNDEF)
  368. return (zone << info->zoneshift) + i;
  369. return 0;
  370. }
  371. /*
  372. * Reads the redundancy data for all PBA's in a zone
  373. * Produces lba <--> pba mappings
  374. */
  375. static int alauda_read_map(struct us_data *us, unsigned int zone)
  376. {
  377. unsigned char *data = us->iobuf;
  378. int result;
  379. int i, j;
  380. unsigned int zonesize = MEDIA_INFO(us).zonesize;
  381. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  382. unsigned int lba_offset, lba_real, blocknum;
  383. unsigned int zone_base_lba = zone * uzonesize;
  384. unsigned int zone_base_pba = zone * zonesize;
  385. u16 *lba_to_pba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
  386. u16 *pba_to_lba = kcalloc(zonesize, sizeof(u16), GFP_NOIO);
  387. if (lba_to_pba == NULL || pba_to_lba == NULL) {
  388. result = USB_STOR_TRANSPORT_ERROR;
  389. goto error;
  390. }
  391. US_DEBUGP("alauda_read_map: Mapping blocks for zone %d\n", zone);
  392. /* 1024 PBA's per zone */
  393. for (i = 0; i < zonesize; i++)
  394. lba_to_pba[i] = pba_to_lba[i] = UNDEF;
  395. for (i = 0; i < zonesize; i++) {
  396. blocknum = zone_base_pba + i;
  397. result = alauda_get_redu_data(us, blocknum, data);
  398. if (result != USB_STOR_XFER_GOOD) {
  399. result = USB_STOR_TRANSPORT_ERROR;
  400. goto error;
  401. }
  402. /* special PBAs have control field 0^16 */
  403. for (j = 0; j < 16; j++)
  404. if (data[j] != 0)
  405. goto nonz;
  406. pba_to_lba[i] = UNUSABLE;
  407. US_DEBUGP("alauda_read_map: PBA %d has no logical mapping\n", blocknum);
  408. continue;
  409. nonz:
  410. /* unwritten PBAs have control field FF^16 */
  411. for (j = 0; j < 16; j++)
  412. if (data[j] != 0xff)
  413. goto nonff;
  414. continue;
  415. nonff:
  416. /* normal PBAs start with six FFs */
  417. if (j < 6) {
  418. US_DEBUGP("alauda_read_map: PBA %d has no logical mapping: "
  419. "reserved area = %02X%02X%02X%02X "
  420. "data status %02X block status %02X\n",
  421. blocknum, data[0], data[1], data[2], data[3],
  422. data[4], data[5]);
  423. pba_to_lba[i] = UNUSABLE;
  424. continue;
  425. }
  426. if ((data[6] >> 4) != 0x01) {
  427. US_DEBUGP("alauda_read_map: PBA %d has invalid address "
  428. "field %02X%02X/%02X%02X\n",
  429. blocknum, data[6], data[7], data[11], data[12]);
  430. pba_to_lba[i] = UNUSABLE;
  431. continue;
  432. }
  433. /* check even parity */
  434. if (parity[data[6] ^ data[7]]) {
  435. printk(KERN_WARNING
  436. "alauda_read_map: Bad parity in LBA for block %d"
  437. " (%02X %02X)\n", i, data[6], data[7]);
  438. pba_to_lba[i] = UNUSABLE;
  439. continue;
  440. }
  441. lba_offset = short_pack(data[7], data[6]);
  442. lba_offset = (lba_offset & 0x07FF) >> 1;
  443. lba_real = lba_offset + zone_base_lba;
  444. /*
  445. * Every 1024 physical blocks ("zone"), the LBA numbers
  446. * go back to zero, but are within a higher block of LBA's.
  447. * Also, there is a maximum of 1000 LBA's per zone.
  448. * In other words, in PBA 1024-2047 you will find LBA 0-999
  449. * which are really LBA 1000-1999. This allows for 24 bad
  450. * or special physical blocks per zone.
  451. */
  452. if (lba_offset >= uzonesize) {
  453. printk(KERN_WARNING
  454. "alauda_read_map: Bad low LBA %d for block %d\n",
  455. lba_real, blocknum);
  456. continue;
  457. }
  458. if (lba_to_pba[lba_offset] != UNDEF) {
  459. printk(KERN_WARNING
  460. "alauda_read_map: "
  461. "LBA %d seen for PBA %d and %d\n",
  462. lba_real, lba_to_pba[lba_offset], blocknum);
  463. continue;
  464. }
  465. pba_to_lba[i] = lba_real;
  466. lba_to_pba[lba_offset] = blocknum;
  467. continue;
  468. }
  469. MEDIA_INFO(us).lba_to_pba[zone] = lba_to_pba;
  470. MEDIA_INFO(us).pba_to_lba[zone] = pba_to_lba;
  471. result = 0;
  472. goto out;
  473. error:
  474. kfree(lba_to_pba);
  475. kfree(pba_to_lba);
  476. out:
  477. return result;
  478. }
  479. /*
  480. * Checks to see whether we have already mapped a certain zone
  481. * If we haven't, the map is generated
  482. */
  483. static void alauda_ensure_map_for_zone(struct us_data *us, unsigned int zone)
  484. {
  485. if (MEDIA_INFO(us).lba_to_pba[zone] == NULL
  486. || MEDIA_INFO(us).pba_to_lba[zone] == NULL)
  487. alauda_read_map(us, zone);
  488. }
  489. /*
  490. * Erases an entire block
  491. */
  492. static int alauda_erase_block(struct us_data *us, u16 pba)
  493. {
  494. int rc;
  495. unsigned char command[] = {
  496. ALAUDA_BULK_CMD, ALAUDA_BULK_ERASE_BLOCK, PBA_HI(pba),
  497. PBA_ZONE(pba), 0, PBA_LO(pba), 0x02, 0, MEDIA_PORT(us)
  498. };
  499. unsigned char buf[2];
  500. US_DEBUGP("alauda_erase_block: Erasing PBA %d\n", pba);
  501. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  502. command, 9, NULL);
  503. if (rc != USB_STOR_XFER_GOOD)
  504. return rc;
  505. rc = usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  506. buf, 2, NULL);
  507. if (rc != USB_STOR_XFER_GOOD)
  508. return rc;
  509. US_DEBUGP("alauda_erase_block: Erase result: %02X %02X\n",
  510. buf[0], buf[1]);
  511. return rc;
  512. }
  513. /*
  514. * Reads data from a certain offset page inside a PBA, including interleaved
  515. * redundancy data. Returns (pagesize+64)*pages bytes in data.
  516. */
  517. static int alauda_read_block_raw(struct us_data *us, u16 pba,
  518. unsigned int page, unsigned int pages, unsigned char *data)
  519. {
  520. int rc;
  521. unsigned char command[] = {
  522. ALAUDA_BULK_CMD, ALAUDA_BULK_READ_BLOCK, PBA_HI(pba),
  523. PBA_ZONE(pba), 0, PBA_LO(pba) + page, pages, 0, MEDIA_PORT(us)
  524. };
  525. US_DEBUGP("alauda_read_block: pba %d page %d count %d\n",
  526. pba, page, pages);
  527. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  528. command, 9, NULL);
  529. if (rc != USB_STOR_XFER_GOOD)
  530. return rc;
  531. return usb_stor_bulk_transfer_buf(us, us->recv_bulk_pipe,
  532. data, (MEDIA_INFO(us).pagesize + 64) * pages, NULL);
  533. }
  534. /*
  535. * Reads data from a certain offset page inside a PBA, excluding redundancy
  536. * data. Returns pagesize*pages bytes in data. Note that data must be big enough
  537. * to hold (pagesize+64)*pages bytes of data, but you can ignore those 'extra'
  538. * trailing bytes outside this function.
  539. */
  540. static int alauda_read_block(struct us_data *us, u16 pba,
  541. unsigned int page, unsigned int pages, unsigned char *data)
  542. {
  543. int i, rc;
  544. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  545. rc = alauda_read_block_raw(us, pba, page, pages, data);
  546. if (rc != USB_STOR_XFER_GOOD)
  547. return rc;
  548. /* Cut out the redundancy data */
  549. for (i = 0; i < pages; i++) {
  550. int dest_offset = i * pagesize;
  551. int src_offset = i * (pagesize + 64);
  552. memmove(data + dest_offset, data + src_offset, pagesize);
  553. }
  554. return rc;
  555. }
  556. /*
  557. * Writes an entire block of data and checks status after write.
  558. * Redundancy data must be already included in data. Data should be
  559. * (pagesize+64)*blocksize bytes in length.
  560. */
  561. static int alauda_write_block(struct us_data *us, u16 pba, unsigned char *data)
  562. {
  563. int rc;
  564. struct alauda_info *info = (struct alauda_info *) us->extra;
  565. unsigned char command[] = {
  566. ALAUDA_BULK_CMD, ALAUDA_BULK_WRITE_BLOCK, PBA_HI(pba),
  567. PBA_ZONE(pba), 0, PBA_LO(pba), 32, 0, MEDIA_PORT(us)
  568. };
  569. US_DEBUGP("alauda_write_block: pba %d\n", pba);
  570. rc = usb_stor_bulk_transfer_buf(us, us->send_bulk_pipe,
  571. command, 9, NULL);
  572. if (rc != USB_STOR_XFER_GOOD)
  573. return rc;
  574. rc = usb_stor_bulk_transfer_buf(us, info->wr_ep, data,
  575. (MEDIA_INFO(us).pagesize + 64) * MEDIA_INFO(us).blocksize,
  576. NULL);
  577. if (rc != USB_STOR_XFER_GOOD)
  578. return rc;
  579. return alauda_check_status2(us);
  580. }
  581. /*
  582. * Write some data to a specific LBA.
  583. */
  584. static int alauda_write_lba(struct us_data *us, u16 lba,
  585. unsigned int page, unsigned int pages,
  586. unsigned char *ptr, unsigned char *blockbuffer)
  587. {
  588. u16 pba, lbap, new_pba;
  589. unsigned char *bptr, *cptr, *xptr;
  590. unsigned char ecc[3];
  591. int i, result;
  592. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  593. unsigned int zonesize = MEDIA_INFO(us).zonesize;
  594. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  595. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  596. unsigned int lba_offset = lba % uzonesize;
  597. unsigned int new_pba_offset;
  598. unsigned int zone = lba / uzonesize;
  599. alauda_ensure_map_for_zone(us, zone);
  600. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  601. if (pba == 1) {
  602. /* Maybe it is impossible to write to PBA 1.
  603. Fake success, but don't do anything. */
  604. printk(KERN_WARNING
  605. "alauda_write_lba: avoid writing to pba 1\n");
  606. return USB_STOR_TRANSPORT_GOOD;
  607. }
  608. new_pba = alauda_find_unused_pba(&MEDIA_INFO(us), zone);
  609. if (!new_pba) {
  610. printk(KERN_WARNING
  611. "alauda_write_lba: Out of unused blocks\n");
  612. return USB_STOR_TRANSPORT_ERROR;
  613. }
  614. /* read old contents */
  615. if (pba != UNDEF) {
  616. result = alauda_read_block_raw(us, pba, 0,
  617. blocksize, blockbuffer);
  618. if (result != USB_STOR_XFER_GOOD)
  619. return result;
  620. } else {
  621. memset(blockbuffer, 0, blocksize * (pagesize + 64));
  622. }
  623. lbap = (lba_offset << 1) | 0x1000;
  624. if (parity[MSB_of(lbap) ^ LSB_of(lbap)])
  625. lbap ^= 1;
  626. /* check old contents and fill lba */
  627. for (i = 0; i < blocksize; i++) {
  628. bptr = blockbuffer + (i * (pagesize + 64));
  629. cptr = bptr + pagesize;
  630. nand_compute_ecc(bptr, ecc);
  631. if (!nand_compare_ecc(cptr+13, ecc)) {
  632. US_DEBUGP("Warning: bad ecc in page %d- of pba %d\n",
  633. i, pba);
  634. nand_store_ecc(cptr+13, ecc);
  635. }
  636. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  637. if (!nand_compare_ecc(cptr+8, ecc)) {
  638. US_DEBUGP("Warning: bad ecc in page %d+ of pba %d\n",
  639. i, pba);
  640. nand_store_ecc(cptr+8, ecc);
  641. }
  642. cptr[6] = cptr[11] = MSB_of(lbap);
  643. cptr[7] = cptr[12] = LSB_of(lbap);
  644. }
  645. /* copy in new stuff and compute ECC */
  646. xptr = ptr;
  647. for (i = page; i < page+pages; i++) {
  648. bptr = blockbuffer + (i * (pagesize + 64));
  649. cptr = bptr + pagesize;
  650. memcpy(bptr, xptr, pagesize);
  651. xptr += pagesize;
  652. nand_compute_ecc(bptr, ecc);
  653. nand_store_ecc(cptr+13, ecc);
  654. nand_compute_ecc(bptr + (pagesize / 2), ecc);
  655. nand_store_ecc(cptr+8, ecc);
  656. }
  657. result = alauda_write_block(us, new_pba, blockbuffer);
  658. if (result != USB_STOR_XFER_GOOD)
  659. return result;
  660. new_pba_offset = new_pba - (zone * zonesize);
  661. MEDIA_INFO(us).pba_to_lba[zone][new_pba_offset] = lba;
  662. MEDIA_INFO(us).lba_to_pba[zone][lba_offset] = new_pba;
  663. US_DEBUGP("alauda_write_lba: Remapped LBA %d to PBA %d\n",
  664. lba, new_pba);
  665. if (pba != UNDEF) {
  666. unsigned int pba_offset = pba - (zone * zonesize);
  667. result = alauda_erase_block(us, pba);
  668. if (result != USB_STOR_XFER_GOOD)
  669. return result;
  670. MEDIA_INFO(us).pba_to_lba[zone][pba_offset] = UNDEF;
  671. }
  672. return USB_STOR_TRANSPORT_GOOD;
  673. }
  674. /*
  675. * Read data from a specific sector address
  676. */
  677. static int alauda_read_data(struct us_data *us, unsigned long address,
  678. unsigned int sectors)
  679. {
  680. unsigned char *buffer;
  681. u16 lba, max_lba;
  682. unsigned int page, len, offset;
  683. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  684. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  685. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  686. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  687. unsigned int uzonesize = MEDIA_INFO(us).uzonesize;
  688. struct scatterlist *sg;
  689. int result;
  690. /*
  691. * Since we only read in one block at a time, we have to create
  692. * a bounce buffer and move the data a piece at a time between the
  693. * bounce buffer and the actual transfer buffer.
  694. * We make this buffer big enough to hold temporary redundancy data,
  695. * which we use when reading the data blocks.
  696. */
  697. len = min(sectors, blocksize) * (pagesize + 64);
  698. buffer = kmalloc(len, GFP_NOIO);
  699. if (buffer == NULL) {
  700. printk(KERN_WARNING "alauda_read_data: Out of memory\n");
  701. return USB_STOR_TRANSPORT_ERROR;
  702. }
  703. /* Figure out the initial LBA and page */
  704. lba = address >> blockshift;
  705. page = (address & MEDIA_INFO(us).blockmask);
  706. max_lba = MEDIA_INFO(us).capacity >> (blockshift + pageshift);
  707. result = USB_STOR_TRANSPORT_GOOD;
  708. offset = 0;
  709. sg = NULL;
  710. while (sectors > 0) {
  711. unsigned int zone = lba / uzonesize; /* integer division */
  712. unsigned int lba_offset = lba - (zone * uzonesize);
  713. unsigned int pages;
  714. u16 pba;
  715. alauda_ensure_map_for_zone(us, zone);
  716. /* Not overflowing capacity? */
  717. if (lba >= max_lba) {
  718. US_DEBUGP("Error: Requested lba %u exceeds "
  719. "maximum %u\n", lba, max_lba);
  720. result = USB_STOR_TRANSPORT_ERROR;
  721. break;
  722. }
  723. /* Find number of pages we can read in this block */
  724. pages = min(sectors, blocksize - page);
  725. len = pages << pageshift;
  726. /* Find where this lba lives on disk */
  727. pba = MEDIA_INFO(us).lba_to_pba[zone][lba_offset];
  728. if (pba == UNDEF) { /* this lba was never written */
  729. US_DEBUGP("Read %d zero pages (LBA %d) page %d\n",
  730. pages, lba, page);
  731. /* This is not really an error. It just means
  732. that the block has never been written.
  733. Instead of returning USB_STOR_TRANSPORT_ERROR
  734. it is better to return all zero data. */
  735. memset(buffer, 0, len);
  736. } else {
  737. US_DEBUGP("Read %d pages, from PBA %d"
  738. " (LBA %d) page %d\n",
  739. pages, pba, lba, page);
  740. result = alauda_read_block(us, pba, page, pages, buffer);
  741. if (result != USB_STOR_TRANSPORT_GOOD)
  742. break;
  743. }
  744. /* Store the data in the transfer buffer */
  745. usb_stor_access_xfer_buf(buffer, len, us->srb,
  746. &sg, &offset, TO_XFER_BUF);
  747. page = 0;
  748. lba++;
  749. sectors -= pages;
  750. }
  751. kfree(buffer);
  752. return result;
  753. }
  754. /*
  755. * Write data to a specific sector address
  756. */
  757. static int alauda_write_data(struct us_data *us, unsigned long address,
  758. unsigned int sectors)
  759. {
  760. unsigned char *buffer, *blockbuffer;
  761. unsigned int page, len, offset;
  762. unsigned int blockshift = MEDIA_INFO(us).blockshift;
  763. unsigned int pageshift = MEDIA_INFO(us).pageshift;
  764. unsigned int blocksize = MEDIA_INFO(us).blocksize;
  765. unsigned int pagesize = MEDIA_INFO(us).pagesize;
  766. struct scatterlist *sg;
  767. u16 lba, max_lba;
  768. int result;
  769. /*
  770. * Since we don't write the user data directly to the device,
  771. * we have to create a bounce buffer and move the data a piece
  772. * at a time between the bounce buffer and the actual transfer buffer.
  773. */
  774. len = min(sectors, blocksize) * pagesize;
  775. buffer = kmalloc(len, GFP_NOIO);
  776. if (buffer == NULL) {
  777. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  778. return USB_STOR_TRANSPORT_ERROR;
  779. }
  780. /*
  781. * We also need a temporary block buffer, where we read in the old data,
  782. * overwrite parts with the new data, and manipulate the redundancy data
  783. */
  784. blockbuffer = kmalloc((pagesize + 64) * blocksize, GFP_NOIO);
  785. if (blockbuffer == NULL) {
  786. printk(KERN_WARNING "alauda_write_data: Out of memory\n");
  787. kfree(buffer);
  788. return USB_STOR_TRANSPORT_ERROR;
  789. }
  790. /* Figure out the initial LBA and page */
  791. lba = address >> blockshift;
  792. page = (address & MEDIA_INFO(us).blockmask);
  793. max_lba = MEDIA_INFO(us).capacity >> (pageshift + blockshift);
  794. result = USB_STOR_TRANSPORT_GOOD;
  795. offset = 0;
  796. sg = NULL;
  797. while (sectors > 0) {
  798. /* Write as many sectors as possible in this block */
  799. unsigned int pages = min(sectors, blocksize - page);
  800. len = pages << pageshift;
  801. /* Not overflowing capacity? */
  802. if (lba >= max_lba) {
  803. US_DEBUGP("alauda_write_data: Requested lba %u exceeds "
  804. "maximum %u\n", lba, max_lba);
  805. result = USB_STOR_TRANSPORT_ERROR;
  806. break;
  807. }
  808. /* Get the data from the transfer buffer */
  809. usb_stor_access_xfer_buf(buffer, len, us->srb,
  810. &sg, &offset, FROM_XFER_BUF);
  811. result = alauda_write_lba(us, lba, page, pages, buffer,
  812. blockbuffer);
  813. if (result != USB_STOR_TRANSPORT_GOOD)
  814. break;
  815. page = 0;
  816. lba++;
  817. sectors -= pages;
  818. }
  819. kfree(buffer);
  820. kfree(blockbuffer);
  821. return result;
  822. }
  823. /*
  824. * Our interface with the rest of the world
  825. */
  826. static void alauda_info_destructor(void *extra)
  827. {
  828. struct alauda_info *info = (struct alauda_info *) extra;
  829. int port;
  830. if (!info)
  831. return;
  832. for (port = 0; port < 2; port++) {
  833. struct alauda_media_info *media_info = &info->port[port];
  834. alauda_free_maps(media_info);
  835. kfree(media_info->lba_to_pba);
  836. kfree(media_info->pba_to_lba);
  837. }
  838. }
  839. /*
  840. * Initialize alauda_info struct and find the data-write endpoint
  841. */
  842. int init_alauda(struct us_data *us)
  843. {
  844. struct alauda_info *info;
  845. struct usb_host_interface *altsetting = us->pusb_intf->cur_altsetting;
  846. nand_init_ecc();
  847. us->extra = kzalloc(sizeof(struct alauda_info), GFP_NOIO);
  848. if (!us->extra) {
  849. US_DEBUGP("init_alauda: Gah! Can't allocate storage for"
  850. "alauda info struct!\n");
  851. return USB_STOR_TRANSPORT_ERROR;
  852. }
  853. info = (struct alauda_info *) us->extra;
  854. us->extra_destructor = alauda_info_destructor;
  855. info->wr_ep = usb_sndbulkpipe(us->pusb_dev,
  856. altsetting->endpoint[0].desc.bEndpointAddress
  857. & USB_ENDPOINT_NUMBER_MASK);
  858. return USB_STOR_TRANSPORT_GOOD;
  859. }
  860. int alauda_transport(struct scsi_cmnd *srb, struct us_data *us)
  861. {
  862. int rc;
  863. struct alauda_info *info = (struct alauda_info *) us->extra;
  864. unsigned char *ptr = us->iobuf;
  865. static unsigned char inquiry_response[36] = {
  866. 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
  867. };
  868. if (srb->cmnd[0] == INQUIRY) {
  869. US_DEBUGP("alauda_transport: INQUIRY. "
  870. "Returning bogus response.\n");
  871. memcpy(ptr, inquiry_response, sizeof(inquiry_response));
  872. fill_inquiry_response(us, ptr, 36);
  873. return USB_STOR_TRANSPORT_GOOD;
  874. }
  875. if (srb->cmnd[0] == TEST_UNIT_READY) {
  876. US_DEBUGP("alauda_transport: TEST_UNIT_READY.\n");
  877. return alauda_check_media(us);
  878. }
  879. if (srb->cmnd[0] == READ_CAPACITY) {
  880. unsigned int num_zones;
  881. unsigned long capacity;
  882. rc = alauda_check_media(us);
  883. if (rc != USB_STOR_TRANSPORT_GOOD)
  884. return rc;
  885. num_zones = MEDIA_INFO(us).capacity >> (MEDIA_INFO(us).zoneshift
  886. + MEDIA_INFO(us).blockshift + MEDIA_INFO(us).pageshift);
  887. capacity = num_zones * MEDIA_INFO(us).uzonesize
  888. * MEDIA_INFO(us).blocksize;
  889. /* Report capacity and page size */
  890. ((__be32 *) ptr)[0] = cpu_to_be32(capacity - 1);
  891. ((__be32 *) ptr)[1] = cpu_to_be32(512);
  892. usb_stor_set_xfer_buf(ptr, 8, srb);
  893. return USB_STOR_TRANSPORT_GOOD;
  894. }
  895. if (srb->cmnd[0] == READ_10) {
  896. unsigned int page, pages;
  897. rc = alauda_check_media(us);
  898. if (rc != USB_STOR_TRANSPORT_GOOD)
  899. return rc;
  900. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  901. page <<= 16;
  902. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  903. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  904. US_DEBUGP("alauda_transport: READ_10: page %d pagect %d\n",
  905. page, pages);
  906. return alauda_read_data(us, page, pages);
  907. }
  908. if (srb->cmnd[0] == WRITE_10) {
  909. unsigned int page, pages;
  910. rc = alauda_check_media(us);
  911. if (rc != USB_STOR_TRANSPORT_GOOD)
  912. return rc;
  913. page = short_pack(srb->cmnd[3], srb->cmnd[2]);
  914. page <<= 16;
  915. page |= short_pack(srb->cmnd[5], srb->cmnd[4]);
  916. pages = short_pack(srb->cmnd[8], srb->cmnd[7]);
  917. US_DEBUGP("alauda_transport: WRITE_10: page %d pagect %d\n",
  918. page, pages);
  919. return alauda_write_data(us, page, pages);
  920. }
  921. if (srb->cmnd[0] == REQUEST_SENSE) {
  922. US_DEBUGP("alauda_transport: REQUEST_SENSE.\n");
  923. memset(ptr, 0, 18);
  924. ptr[0] = 0xF0;
  925. ptr[2] = info->sense_key;
  926. ptr[7] = 11;
  927. ptr[12] = info->sense_asc;
  928. ptr[13] = info->sense_ascq;
  929. usb_stor_set_xfer_buf(ptr, 18, srb);
  930. return USB_STOR_TRANSPORT_GOOD;
  931. }
  932. if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
  933. /* sure. whatever. not like we can stop the user from popping
  934. the media out of the device (no locking doors, etc) */
  935. return USB_STOR_TRANSPORT_GOOD;
  936. }
  937. US_DEBUGP("alauda_transport: Gah! Unknown command: %d (0x%x)\n",
  938. srb->cmnd[0], srb->cmnd[0]);
  939. info->sense_key = 0x05;
  940. info->sense_asc = 0x20;
  941. info->sense_ascq = 0x00;
  942. return USB_STOR_TRANSPORT_FAILED;
  943. }