isd200.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. /* Transport & Protocol Driver for In-System Design, Inc. ISD200 ASIC
  2. *
  3. * $Id: isd200.c,v 1.16 2002/04/22 03:39:43 mdharm Exp $
  4. *
  5. * Current development and maintenance:
  6. * (C) 2001-2002 Björn Stenberg (bjorn@haxx.se)
  7. *
  8. * Developed with the assistance of:
  9. * (C) 2002 Alan Stern <stern@rowland.org>
  10. *
  11. * Initial work:
  12. * (C) 2000 In-System Design, Inc. (support@in-system.com)
  13. *
  14. * The ISD200 ASIC does not natively support ATA devices. The chip
  15. * does implement an interface, the ATA Command Block (ATACB) which provides
  16. * a means of passing ATA commands and ATA register accesses to a device.
  17. *
  18. * This program is free software; you can redistribute it and/or modify it
  19. * under the terms of the GNU General Public License as published by the
  20. * Free Software Foundation; either version 2, or (at your option) any
  21. * later version.
  22. *
  23. * This program is distributed in the hope that it will be useful, but
  24. * WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License along
  29. * with this program; if not, write to the Free Software Foundation, Inc.,
  30. * 675 Mass Ave, Cambridge, MA 02139, USA.
  31. *
  32. * History:
  33. *
  34. * 2002-10-19: Removed the specialized transfer routines.
  35. * (Alan Stern <stern@rowland.harvard.edu>)
  36. * 2001-02-24: Removed lots of duplicate code and simplified the structure.
  37. * (bjorn@haxx.se)
  38. * 2002-01-16: Fixed endianness bug so it works on the ppc arch.
  39. * (Luc Saillard <luc@saillard.org>)
  40. * 2002-01-17: All bitfields removed.
  41. * (bjorn@haxx.se)
  42. */
  43. /* Include files */
  44. #include <linux/jiffies.h>
  45. #include <linux/errno.h>
  46. #include <linux/slab.h>
  47. #include <linux/hdreg.h>
  48. #include <linux/ide.h>
  49. #include <linux/scatterlist.h>
  50. #include <scsi/scsi.h>
  51. #include <scsi/scsi_cmnd.h>
  52. #include <scsi/scsi_device.h>
  53. #include "usb.h"
  54. #include "transport.h"
  55. #include "protocol.h"
  56. #include "debug.h"
  57. #include "scsiglue.h"
  58. #include "isd200.h"
  59. /* Timeout defines (in Seconds) */
  60. #define ISD200_ENUM_BSY_TIMEOUT 35
  61. #define ISD200_ENUM_DETECT_TIMEOUT 30
  62. #define ISD200_DEFAULT_TIMEOUT 30
  63. /* device flags */
  64. #define DF_ATA_DEVICE 0x0001
  65. #define DF_MEDIA_STATUS_ENABLED 0x0002
  66. #define DF_REMOVABLE_MEDIA 0x0004
  67. /* capability bit definitions */
  68. #define CAPABILITY_DMA 0x01
  69. #define CAPABILITY_LBA 0x02
  70. /* command_setX bit definitions */
  71. #define COMMANDSET_REMOVABLE 0x02
  72. #define COMMANDSET_MEDIA_STATUS 0x10
  73. /* ATA Vendor Specific defines */
  74. #define ATA_ADDRESS_DEVHEAD_STD 0xa0
  75. #define ATA_ADDRESS_DEVHEAD_LBA_MODE 0x40
  76. #define ATA_ADDRESS_DEVHEAD_SLAVE 0x10
  77. /* Action Select bits */
  78. #define ACTION_SELECT_0 0x01
  79. #define ACTION_SELECT_1 0x02
  80. #define ACTION_SELECT_2 0x04
  81. #define ACTION_SELECT_3 0x08
  82. #define ACTION_SELECT_4 0x10
  83. #define ACTION_SELECT_5 0x20
  84. #define ACTION_SELECT_6 0x40
  85. #define ACTION_SELECT_7 0x80
  86. /* Register Select bits */
  87. #define REG_ALTERNATE_STATUS 0x01
  88. #define REG_DEVICE_CONTROL 0x01
  89. #define REG_ERROR 0x02
  90. #define REG_FEATURES 0x02
  91. #define REG_SECTOR_COUNT 0x04
  92. #define REG_SECTOR_NUMBER 0x08
  93. #define REG_CYLINDER_LOW 0x10
  94. #define REG_CYLINDER_HIGH 0x20
  95. #define REG_DEVICE_HEAD 0x40
  96. #define REG_STATUS 0x80
  97. #define REG_COMMAND 0x80
  98. /* ATA error definitions not in <linux/hdreg.h> */
  99. #define ATA_ERROR_MEDIA_CHANGE 0x20
  100. /* ATA command definitions not in <linux/hdreg.h> */
  101. #define ATA_COMMAND_GET_MEDIA_STATUS 0xDA
  102. #define ATA_COMMAND_MEDIA_EJECT 0xED
  103. /* ATA drive control definitions */
  104. #define ATA_DC_DISABLE_INTERRUPTS 0x02
  105. #define ATA_DC_RESET_CONTROLLER 0x04
  106. #define ATA_DC_REENABLE_CONTROLLER 0x00
  107. /*
  108. * General purpose return codes
  109. */
  110. #define ISD200_ERROR -1
  111. #define ISD200_GOOD 0
  112. /*
  113. * Transport return codes
  114. */
  115. #define ISD200_TRANSPORT_GOOD 0 /* Transport good, command good */
  116. #define ISD200_TRANSPORT_FAILED 1 /* Transport good, command failed */
  117. #define ISD200_TRANSPORT_ERROR 2 /* Transport bad (i.e. device dead) */
  118. /* driver action codes */
  119. #define ACTION_READ_STATUS 0
  120. #define ACTION_RESET 1
  121. #define ACTION_REENABLE 2
  122. #define ACTION_SOFT_RESET 3
  123. #define ACTION_ENUM 4
  124. #define ACTION_IDENTIFY 5
  125. /*
  126. * ata_cdb struct
  127. */
  128. union ata_cdb {
  129. struct {
  130. unsigned char SignatureByte0;
  131. unsigned char SignatureByte1;
  132. unsigned char ActionSelect;
  133. unsigned char RegisterSelect;
  134. unsigned char TransferBlockSize;
  135. unsigned char WriteData3F6;
  136. unsigned char WriteData1F1;
  137. unsigned char WriteData1F2;
  138. unsigned char WriteData1F3;
  139. unsigned char WriteData1F4;
  140. unsigned char WriteData1F5;
  141. unsigned char WriteData1F6;
  142. unsigned char WriteData1F7;
  143. unsigned char Reserved[3];
  144. } generic;
  145. struct {
  146. unsigned char SignatureByte0;
  147. unsigned char SignatureByte1;
  148. unsigned char ActionSelect;
  149. unsigned char RegisterSelect;
  150. unsigned char TransferBlockSize;
  151. unsigned char AlternateStatusByte;
  152. unsigned char ErrorByte;
  153. unsigned char SectorCountByte;
  154. unsigned char SectorNumberByte;
  155. unsigned char CylinderLowByte;
  156. unsigned char CylinderHighByte;
  157. unsigned char DeviceHeadByte;
  158. unsigned char StatusByte;
  159. unsigned char Reserved[3];
  160. } read;
  161. struct {
  162. unsigned char SignatureByte0;
  163. unsigned char SignatureByte1;
  164. unsigned char ActionSelect;
  165. unsigned char RegisterSelect;
  166. unsigned char TransferBlockSize;
  167. unsigned char DeviceControlByte;
  168. unsigned char FeaturesByte;
  169. unsigned char SectorCountByte;
  170. unsigned char SectorNumberByte;
  171. unsigned char CylinderLowByte;
  172. unsigned char CylinderHighByte;
  173. unsigned char DeviceHeadByte;
  174. unsigned char CommandByte;
  175. unsigned char Reserved[3];
  176. } write;
  177. };
  178. /*
  179. * Inquiry data structure. This is the data returned from the target
  180. * after it receives an inquiry.
  181. *
  182. * This structure may be extended by the number of bytes specified
  183. * in the field AdditionalLength. The defined size constant only
  184. * includes fields through ProductRevisionLevel.
  185. */
  186. /*
  187. * DeviceType field
  188. */
  189. #define DIRECT_ACCESS_DEVICE 0x00 /* disks */
  190. #define DEVICE_REMOVABLE 0x80
  191. struct inquiry_data {
  192. unsigned char DeviceType;
  193. unsigned char DeviceTypeModifier;
  194. unsigned char Versions;
  195. unsigned char Format;
  196. unsigned char AdditionalLength;
  197. unsigned char Reserved[2];
  198. unsigned char Capability;
  199. unsigned char VendorId[8];
  200. unsigned char ProductId[16];
  201. unsigned char ProductRevisionLevel[4];
  202. unsigned char VendorSpecific[20];
  203. unsigned char Reserved3[40];
  204. } __attribute__ ((packed));
  205. /*
  206. * INQUIRY data buffer size
  207. */
  208. #define INQUIRYDATABUFFERSIZE 36
  209. /*
  210. * ISD200 CONFIG data struct
  211. */
  212. #define ATACFG_TIMING 0x0f
  213. #define ATACFG_ATAPI_RESET 0x10
  214. #define ATACFG_MASTER 0x20
  215. #define ATACFG_BLOCKSIZE 0xa0
  216. #define ATACFGE_LAST_LUN 0x07
  217. #define ATACFGE_DESC_OVERRIDE 0x08
  218. #define ATACFGE_STATE_SUSPEND 0x10
  219. #define ATACFGE_SKIP_BOOT 0x20
  220. #define ATACFGE_CONF_DESC2 0x40
  221. #define ATACFGE_INIT_STATUS 0x80
  222. #define CFG_CAPABILITY_SRST 0x01
  223. struct isd200_config {
  224. unsigned char EventNotification;
  225. unsigned char ExternalClock;
  226. unsigned char ATAInitTimeout;
  227. unsigned char ATAConfig;
  228. unsigned char ATAMajorCommand;
  229. unsigned char ATAMinorCommand;
  230. unsigned char ATAExtraConfig;
  231. unsigned char Capability;
  232. }__attribute__ ((packed));
  233. /*
  234. * ISD200 driver information struct
  235. */
  236. struct isd200_info {
  237. struct inquiry_data InquiryData;
  238. struct hd_driveid *id;
  239. struct isd200_config ConfigData;
  240. unsigned char *RegsBuf;
  241. unsigned char ATARegs[8];
  242. unsigned char DeviceHead;
  243. unsigned char DeviceFlags;
  244. /* maximum number of LUNs supported */
  245. unsigned char MaxLUNs;
  246. struct scsi_cmnd srb;
  247. struct scatterlist sg;
  248. };
  249. /*
  250. * Read Capacity Data - returned in Big Endian format
  251. */
  252. struct read_capacity_data {
  253. __be32 LogicalBlockAddress;
  254. __be32 BytesPerBlock;
  255. };
  256. /*
  257. * Read Block Limits Data - returned in Big Endian format
  258. * This structure returns the maximum and minimum block
  259. * size for a TAPE device.
  260. */
  261. struct read_block_limits {
  262. unsigned char Reserved;
  263. unsigned char BlockMaximumSize[3];
  264. unsigned char BlockMinimumSize[2];
  265. };
  266. /*
  267. * Sense Data Format
  268. */
  269. #define SENSE_ERRCODE 0x7f
  270. #define SENSE_ERRCODE_VALID 0x80
  271. #define SENSE_FLAG_SENSE_KEY 0x0f
  272. #define SENSE_FLAG_BAD_LENGTH 0x20
  273. #define SENSE_FLAG_END_OF_MEDIA 0x40
  274. #define SENSE_FLAG_FILE_MARK 0x80
  275. struct sense_data {
  276. unsigned char ErrorCode;
  277. unsigned char SegmentNumber;
  278. unsigned char Flags;
  279. unsigned char Information[4];
  280. unsigned char AdditionalSenseLength;
  281. unsigned char CommandSpecificInformation[4];
  282. unsigned char AdditionalSenseCode;
  283. unsigned char AdditionalSenseCodeQualifier;
  284. unsigned char FieldReplaceableUnitCode;
  285. unsigned char SenseKeySpecific[3];
  286. } __attribute__ ((packed));
  287. /*
  288. * Default request sense buffer size
  289. */
  290. #define SENSE_BUFFER_SIZE 18
  291. /***********************************************************************
  292. * Helper routines
  293. ***********************************************************************/
  294. /**************************************************************************
  295. * isd200_build_sense
  296. *
  297. * Builds an artificial sense buffer to report the results of a
  298. * failed command.
  299. *
  300. * RETURNS:
  301. * void
  302. */
  303. static void isd200_build_sense(struct us_data *us, struct scsi_cmnd *srb)
  304. {
  305. struct isd200_info *info = (struct isd200_info *)us->extra;
  306. struct sense_data *buf = (struct sense_data *) &srb->sense_buffer[0];
  307. unsigned char error = info->ATARegs[IDE_ERROR_OFFSET];
  308. if(error & ATA_ERROR_MEDIA_CHANGE) {
  309. buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
  310. buf->AdditionalSenseLength = 0xb;
  311. buf->Flags = UNIT_ATTENTION;
  312. buf->AdditionalSenseCode = 0;
  313. buf->AdditionalSenseCodeQualifier = 0;
  314. } else if(error & MCR_ERR) {
  315. buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
  316. buf->AdditionalSenseLength = 0xb;
  317. buf->Flags = UNIT_ATTENTION;
  318. buf->AdditionalSenseCode = 0;
  319. buf->AdditionalSenseCodeQualifier = 0;
  320. } else if(error & TRK0_ERR) {
  321. buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
  322. buf->AdditionalSenseLength = 0xb;
  323. buf->Flags = NOT_READY;
  324. buf->AdditionalSenseCode = 0;
  325. buf->AdditionalSenseCodeQualifier = 0;
  326. } else if(error & ECC_ERR) {
  327. buf->ErrorCode = 0x70 | SENSE_ERRCODE_VALID;
  328. buf->AdditionalSenseLength = 0xb;
  329. buf->Flags = DATA_PROTECT;
  330. buf->AdditionalSenseCode = 0;
  331. buf->AdditionalSenseCodeQualifier = 0;
  332. } else {
  333. buf->ErrorCode = 0;
  334. buf->AdditionalSenseLength = 0;
  335. buf->Flags = 0;
  336. buf->AdditionalSenseCode = 0;
  337. buf->AdditionalSenseCodeQualifier = 0;
  338. }
  339. }
  340. /***********************************************************************
  341. * Transport routines
  342. ***********************************************************************/
  343. /**************************************************************************
  344. * isd200_set_srb(), isd200_srb_set_bufflen()
  345. *
  346. * Two helpers to facilitate in initialization of scsi_cmnd structure
  347. * Will need to change when struct scsi_cmnd changes
  348. */
  349. static void isd200_set_srb(struct isd200_info *info,
  350. enum dma_data_direction dir, void* buff, unsigned bufflen)
  351. {
  352. struct scsi_cmnd *srb = &info->srb;
  353. if (buff)
  354. sg_init_one(&info->sg, buff, bufflen);
  355. srb->sc_data_direction = dir;
  356. srb->request_buffer = buff ? &info->sg : NULL;
  357. srb->request_bufflen = bufflen;
  358. srb->use_sg = buff ? 1 : 0;
  359. }
  360. static void isd200_srb_set_bufflen(struct scsi_cmnd *srb, unsigned bufflen)
  361. {
  362. srb->request_bufflen = bufflen;
  363. }
  364. /**************************************************************************
  365. * isd200_action
  366. *
  367. * Routine for sending commands to the isd200
  368. *
  369. * RETURNS:
  370. * ISD status code
  371. */
  372. static int isd200_action( struct us_data *us, int action,
  373. void* pointer, int value )
  374. {
  375. union ata_cdb ata;
  376. struct scsi_device srb_dev;
  377. struct isd200_info *info = (struct isd200_info *)us->extra;
  378. struct scsi_cmnd *srb = &info->srb;
  379. int status;
  380. memset(&ata, 0, sizeof(ata));
  381. memset(&srb_dev, 0, sizeof(srb_dev));
  382. srb->device = &srb_dev;
  383. ++srb->serial_number;
  384. ata.generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  385. ata.generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  386. ata.generic.TransferBlockSize = 1;
  387. switch ( action ) {
  388. case ACTION_READ_STATUS:
  389. US_DEBUGP(" isd200_action(READ_STATUS)\n");
  390. ata.generic.ActionSelect = ACTION_SELECT_0|ACTION_SELECT_2;
  391. ata.generic.RegisterSelect =
  392. REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
  393. REG_STATUS | REG_ERROR;
  394. isd200_set_srb(info, DMA_FROM_DEVICE, pointer, value);
  395. break;
  396. case ACTION_ENUM:
  397. US_DEBUGP(" isd200_action(ENUM,0x%02x)\n",value);
  398. ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
  399. ACTION_SELECT_3|ACTION_SELECT_4|
  400. ACTION_SELECT_5;
  401. ata.generic.RegisterSelect = REG_DEVICE_HEAD;
  402. ata.write.DeviceHeadByte = value;
  403. isd200_set_srb(info, DMA_NONE, NULL, 0);
  404. break;
  405. case ACTION_RESET:
  406. US_DEBUGP(" isd200_action(RESET)\n");
  407. ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
  408. ACTION_SELECT_3|ACTION_SELECT_4;
  409. ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
  410. ata.write.DeviceControlByte = ATA_DC_RESET_CONTROLLER;
  411. isd200_set_srb(info, DMA_NONE, NULL, 0);
  412. break;
  413. case ACTION_REENABLE:
  414. US_DEBUGP(" isd200_action(REENABLE)\n");
  415. ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_2|
  416. ACTION_SELECT_3|ACTION_SELECT_4;
  417. ata.generic.RegisterSelect = REG_DEVICE_CONTROL;
  418. ata.write.DeviceControlByte = ATA_DC_REENABLE_CONTROLLER;
  419. isd200_set_srb(info, DMA_NONE, NULL, 0);
  420. break;
  421. case ACTION_SOFT_RESET:
  422. US_DEBUGP(" isd200_action(SOFT_RESET)\n");
  423. ata.generic.ActionSelect = ACTION_SELECT_1|ACTION_SELECT_5;
  424. ata.generic.RegisterSelect = REG_DEVICE_HEAD | REG_COMMAND;
  425. ata.write.DeviceHeadByte = info->DeviceHead;
  426. ata.write.CommandByte = WIN_SRST;
  427. isd200_set_srb(info, DMA_NONE, NULL, 0);
  428. break;
  429. case ACTION_IDENTIFY:
  430. US_DEBUGP(" isd200_action(IDENTIFY)\n");
  431. ata.generic.RegisterSelect = REG_COMMAND;
  432. ata.write.CommandByte = WIN_IDENTIFY;
  433. isd200_set_srb(info, DMA_FROM_DEVICE, info->id,
  434. sizeof(struct hd_driveid));
  435. break;
  436. default:
  437. US_DEBUGP("Error: Undefined action %d\n",action);
  438. return ISD200_ERROR;
  439. }
  440. memcpy(srb->cmnd, &ata, sizeof(ata.generic));
  441. srb->cmd_len = sizeof(ata.generic);
  442. status = usb_stor_Bulk_transport(srb, us);
  443. if (status == USB_STOR_TRANSPORT_GOOD)
  444. status = ISD200_GOOD;
  445. else {
  446. US_DEBUGP(" isd200_action(0x%02x) error: %d\n",action,status);
  447. status = ISD200_ERROR;
  448. /* need to reset device here */
  449. }
  450. return status;
  451. }
  452. /**************************************************************************
  453. * isd200_read_regs
  454. *
  455. * Read ATA Registers
  456. *
  457. * RETURNS:
  458. * ISD status code
  459. */
  460. static int isd200_read_regs( struct us_data *us )
  461. {
  462. struct isd200_info *info = (struct isd200_info *)us->extra;
  463. int retStatus = ISD200_GOOD;
  464. int transferStatus;
  465. US_DEBUGP("Entering isd200_IssueATAReadRegs\n");
  466. transferStatus = isd200_action( us, ACTION_READ_STATUS,
  467. info->RegsBuf, sizeof(info->ATARegs) );
  468. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  469. US_DEBUGP(" Error reading ATA registers\n");
  470. retStatus = ISD200_ERROR;
  471. } else {
  472. memcpy(info->ATARegs, info->RegsBuf, sizeof(info->ATARegs));
  473. US_DEBUGP(" Got ATA Register[IDE_ERROR_OFFSET] = 0x%x\n",
  474. info->ATARegs[IDE_ERROR_OFFSET]);
  475. }
  476. return retStatus;
  477. }
  478. /**************************************************************************
  479. * Invoke the transport and basic error-handling/recovery methods
  480. *
  481. * This is used by the protocol layers to actually send the message to
  482. * the device and receive the response.
  483. */
  484. static void isd200_invoke_transport( struct us_data *us,
  485. struct scsi_cmnd *srb,
  486. union ata_cdb *ataCdb )
  487. {
  488. int need_auto_sense = 0;
  489. int transferStatus;
  490. int result;
  491. /* send the command to the transport layer */
  492. memcpy(srb->cmnd, ataCdb, sizeof(ataCdb->generic));
  493. srb->cmd_len = sizeof(ataCdb->generic);
  494. transferStatus = usb_stor_Bulk_transport(srb, us);
  495. /* if the command gets aborted by the higher layers, we need to
  496. * short-circuit all other processing
  497. */
  498. if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
  499. US_DEBUGP("-- command was aborted\n");
  500. goto Handle_Abort;
  501. }
  502. switch (transferStatus) {
  503. case USB_STOR_TRANSPORT_GOOD:
  504. /* Indicate a good result */
  505. srb->result = SAM_STAT_GOOD;
  506. break;
  507. case USB_STOR_TRANSPORT_NO_SENSE:
  508. US_DEBUGP("-- transport indicates protocol failure\n");
  509. srb->result = SAM_STAT_CHECK_CONDITION;
  510. return;
  511. case USB_STOR_TRANSPORT_FAILED:
  512. US_DEBUGP("-- transport indicates command failure\n");
  513. need_auto_sense = 1;
  514. break;
  515. case USB_STOR_TRANSPORT_ERROR:
  516. US_DEBUGP("-- transport indicates transport error\n");
  517. srb->result = DID_ERROR << 16;
  518. /* Need reset here */
  519. return;
  520. default:
  521. US_DEBUGP("-- transport indicates unknown error\n");
  522. srb->result = DID_ERROR << 16;
  523. /* Need reset here */
  524. return;
  525. }
  526. if ((scsi_get_resid(srb) > 0) &&
  527. !((srb->cmnd[0] == REQUEST_SENSE) ||
  528. (srb->cmnd[0] == INQUIRY) ||
  529. (srb->cmnd[0] == MODE_SENSE) ||
  530. (srb->cmnd[0] == LOG_SENSE) ||
  531. (srb->cmnd[0] == MODE_SENSE_10))) {
  532. US_DEBUGP("-- unexpectedly short transfer\n");
  533. need_auto_sense = 1;
  534. }
  535. if (need_auto_sense) {
  536. result = isd200_read_regs(us);
  537. if (test_bit(US_FLIDX_TIMED_OUT, &us->flags)) {
  538. US_DEBUGP("-- auto-sense aborted\n");
  539. goto Handle_Abort;
  540. }
  541. if (result == ISD200_GOOD) {
  542. isd200_build_sense(us, srb);
  543. srb->result = SAM_STAT_CHECK_CONDITION;
  544. /* If things are really okay, then let's show that */
  545. if ((srb->sense_buffer[2] & 0xf) == 0x0)
  546. srb->result = SAM_STAT_GOOD;
  547. } else {
  548. srb->result = DID_ERROR << 16;
  549. /* Need reset here */
  550. }
  551. }
  552. /* Regardless of auto-sense, if we _know_ we have an error
  553. * condition, show that in the result code
  554. */
  555. if (transferStatus == USB_STOR_TRANSPORT_FAILED)
  556. srb->result = SAM_STAT_CHECK_CONDITION;
  557. return;
  558. /* abort processing: the bulk-only transport requires a reset
  559. * following an abort */
  560. Handle_Abort:
  561. srb->result = DID_ABORT << 16;
  562. /* permit the reset transfer to take place */
  563. clear_bit(US_FLIDX_ABORTING, &us->flags);
  564. /* Need reset here */
  565. }
  566. #ifdef CONFIG_USB_STORAGE_DEBUG
  567. static void isd200_log_config( struct isd200_info* info )
  568. {
  569. US_DEBUGP(" Event Notification: 0x%x\n",
  570. info->ConfigData.EventNotification);
  571. US_DEBUGP(" External Clock: 0x%x\n",
  572. info->ConfigData.ExternalClock);
  573. US_DEBUGP(" ATA Init Timeout: 0x%x\n",
  574. info->ConfigData.ATAInitTimeout);
  575. US_DEBUGP(" ATAPI Command Block Size: 0x%x\n",
  576. (info->ConfigData.ATAConfig & ATACFG_BLOCKSIZE) >> 6);
  577. US_DEBUGP(" Master/Slave Selection: 0x%x\n",
  578. info->ConfigData.ATAConfig & ATACFG_MASTER);
  579. US_DEBUGP(" ATAPI Reset: 0x%x\n",
  580. info->ConfigData.ATAConfig & ATACFG_ATAPI_RESET);
  581. US_DEBUGP(" ATA Timing: 0x%x\n",
  582. info->ConfigData.ATAConfig & ATACFG_TIMING);
  583. US_DEBUGP(" ATA Major Command: 0x%x\n",
  584. info->ConfigData.ATAMajorCommand);
  585. US_DEBUGP(" ATA Minor Command: 0x%x\n",
  586. info->ConfigData.ATAMinorCommand);
  587. US_DEBUGP(" Init Status: 0x%x\n",
  588. info->ConfigData.ATAExtraConfig & ATACFGE_INIT_STATUS);
  589. US_DEBUGP(" Config Descriptor 2: 0x%x\n",
  590. info->ConfigData.ATAExtraConfig & ATACFGE_CONF_DESC2);
  591. US_DEBUGP(" Skip Device Boot: 0x%x\n",
  592. info->ConfigData.ATAExtraConfig & ATACFGE_SKIP_BOOT);
  593. US_DEBUGP(" ATA 3 State Supsend: 0x%x\n",
  594. info->ConfigData.ATAExtraConfig & ATACFGE_STATE_SUSPEND);
  595. US_DEBUGP(" Descriptor Override: 0x%x\n",
  596. info->ConfigData.ATAExtraConfig & ATACFGE_DESC_OVERRIDE);
  597. US_DEBUGP(" Last LUN Identifier: 0x%x\n",
  598. info->ConfigData.ATAExtraConfig & ATACFGE_LAST_LUN);
  599. US_DEBUGP(" SRST Enable: 0x%x\n",
  600. info->ConfigData.ATAExtraConfig & CFG_CAPABILITY_SRST);
  601. }
  602. #endif
  603. /**************************************************************************
  604. * isd200_write_config
  605. *
  606. * Write the ISD200 Configuration data
  607. *
  608. * RETURNS:
  609. * ISD status code
  610. */
  611. static int isd200_write_config( struct us_data *us )
  612. {
  613. struct isd200_info *info = (struct isd200_info *)us->extra;
  614. int retStatus = ISD200_GOOD;
  615. int result;
  616. #ifdef CONFIG_USB_STORAGE_DEBUG
  617. US_DEBUGP("Entering isd200_write_config\n");
  618. US_DEBUGP(" Writing the following ISD200 Config Data:\n");
  619. isd200_log_config(info);
  620. #endif
  621. /* let's send the command via the control pipe */
  622. result = usb_stor_ctrl_transfer(
  623. us,
  624. us->send_ctrl_pipe,
  625. 0x01,
  626. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
  627. 0x0000,
  628. 0x0002,
  629. (void *) &info->ConfigData,
  630. sizeof(info->ConfigData));
  631. if (result >= 0) {
  632. US_DEBUGP(" ISD200 Config Data was written successfully\n");
  633. } else {
  634. US_DEBUGP(" Request to write ISD200 Config Data failed!\n");
  635. retStatus = ISD200_ERROR;
  636. }
  637. US_DEBUGP("Leaving isd200_write_config %08X\n", retStatus);
  638. return retStatus;
  639. }
  640. /**************************************************************************
  641. * isd200_read_config
  642. *
  643. * Reads the ISD200 Configuration data
  644. *
  645. * RETURNS:
  646. * ISD status code
  647. */
  648. static int isd200_read_config( struct us_data *us )
  649. {
  650. struct isd200_info *info = (struct isd200_info *)us->extra;
  651. int retStatus = ISD200_GOOD;
  652. int result;
  653. US_DEBUGP("Entering isd200_read_config\n");
  654. /* read the configuration information from ISD200. Use this to */
  655. /* determine what the special ATA CDB bytes are. */
  656. result = usb_stor_ctrl_transfer(
  657. us,
  658. us->recv_ctrl_pipe,
  659. 0x02,
  660. USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
  661. 0x0000,
  662. 0x0002,
  663. (void *) &info->ConfigData,
  664. sizeof(info->ConfigData));
  665. if (result >= 0) {
  666. US_DEBUGP(" Retrieved the following ISD200 Config Data:\n");
  667. #ifdef CONFIG_USB_STORAGE_DEBUG
  668. isd200_log_config(info);
  669. #endif
  670. } else {
  671. US_DEBUGP(" Request to get ISD200 Config Data failed!\n");
  672. retStatus = ISD200_ERROR;
  673. }
  674. US_DEBUGP("Leaving isd200_read_config %08X\n", retStatus);
  675. return retStatus;
  676. }
  677. /**************************************************************************
  678. * isd200_atapi_soft_reset
  679. *
  680. * Perform an Atapi Soft Reset on the device
  681. *
  682. * RETURNS:
  683. * NT status code
  684. */
  685. static int isd200_atapi_soft_reset( struct us_data *us )
  686. {
  687. int retStatus = ISD200_GOOD;
  688. int transferStatus;
  689. US_DEBUGP("Entering isd200_atapi_soft_reset\n");
  690. transferStatus = isd200_action( us, ACTION_SOFT_RESET, NULL, 0 );
  691. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  692. US_DEBUGP(" Error issuing Atapi Soft Reset\n");
  693. retStatus = ISD200_ERROR;
  694. }
  695. US_DEBUGP("Leaving isd200_atapi_soft_reset %08X\n", retStatus);
  696. return retStatus;
  697. }
  698. /**************************************************************************
  699. * isd200_srst
  700. *
  701. * Perform an SRST on the device
  702. *
  703. * RETURNS:
  704. * ISD status code
  705. */
  706. static int isd200_srst( struct us_data *us )
  707. {
  708. int retStatus = ISD200_GOOD;
  709. int transferStatus;
  710. US_DEBUGP("Entering isd200_SRST\n");
  711. transferStatus = isd200_action( us, ACTION_RESET, NULL, 0 );
  712. /* check to see if this request failed */
  713. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  714. US_DEBUGP(" Error issuing SRST\n");
  715. retStatus = ISD200_ERROR;
  716. } else {
  717. /* delay 10ms to give the drive a chance to see it */
  718. msleep(10);
  719. transferStatus = isd200_action( us, ACTION_REENABLE, NULL, 0 );
  720. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  721. US_DEBUGP(" Error taking drive out of reset\n");
  722. retStatus = ISD200_ERROR;
  723. } else {
  724. /* delay 50ms to give the drive a chance to recover after SRST */
  725. msleep(50);
  726. }
  727. }
  728. US_DEBUGP("Leaving isd200_srst %08X\n", retStatus);
  729. return retStatus;
  730. }
  731. /**************************************************************************
  732. * isd200_try_enum
  733. *
  734. * Helper function for isd200_manual_enum(). Does ENUM and READ_STATUS
  735. * and tries to analyze the status registers
  736. *
  737. * RETURNS:
  738. * ISD status code
  739. */
  740. static int isd200_try_enum(struct us_data *us, unsigned char master_slave,
  741. int detect )
  742. {
  743. int status = ISD200_GOOD;
  744. unsigned long endTime;
  745. struct isd200_info *info = (struct isd200_info *)us->extra;
  746. unsigned char *regs = info->RegsBuf;
  747. int recheckAsMaster = 0;
  748. if ( detect )
  749. endTime = jiffies + ISD200_ENUM_DETECT_TIMEOUT * HZ;
  750. else
  751. endTime = jiffies + ISD200_ENUM_BSY_TIMEOUT * HZ;
  752. /* loop until we detect !BSY or timeout */
  753. while(1) {
  754. #ifdef CONFIG_USB_STORAGE_DEBUG
  755. char* mstr = master_slave == ATA_ADDRESS_DEVHEAD_STD ?
  756. "Master" : "Slave";
  757. #endif
  758. status = isd200_action( us, ACTION_ENUM, NULL, master_slave );
  759. if ( status != ISD200_GOOD )
  760. break;
  761. status = isd200_action( us, ACTION_READ_STATUS,
  762. regs, 8 );
  763. if ( status != ISD200_GOOD )
  764. break;
  765. if (!detect) {
  766. if (regs[IDE_STATUS_OFFSET] & BUSY_STAT ) {
  767. US_DEBUGP(" %s status is still BSY, try again...\n",mstr);
  768. } else {
  769. US_DEBUGP(" %s status !BSY, continue with next operation\n",mstr);
  770. break;
  771. }
  772. }
  773. /* check for BUSY_STAT and */
  774. /* WRERR_STAT (workaround ATA Zip drive) and */
  775. /* ERR_STAT (workaround for Archos CD-ROM) */
  776. else if (regs[IDE_STATUS_OFFSET] &
  777. (BUSY_STAT | WRERR_STAT | ERR_STAT )) {
  778. US_DEBUGP(" Status indicates it is not ready, try again...\n");
  779. }
  780. /* check for DRDY, ATA devices set DRDY after SRST */
  781. else if (regs[IDE_STATUS_OFFSET] & READY_STAT) {
  782. US_DEBUGP(" Identified ATA device\n");
  783. info->DeviceFlags |= DF_ATA_DEVICE;
  784. info->DeviceHead = master_slave;
  785. break;
  786. }
  787. /* check Cylinder High/Low to
  788. determine if it is an ATAPI device
  789. */
  790. else if ((regs[IDE_HCYL_OFFSET] == 0xEB) &&
  791. (regs[IDE_LCYL_OFFSET] == 0x14)) {
  792. /* It seems that the RICOH
  793. MP6200A CD/RW drive will
  794. report itself okay as a
  795. slave when it is really a
  796. master. So this check again
  797. as a master device just to
  798. make sure it doesn't report
  799. itself okay as a master also
  800. */
  801. if ((master_slave & ATA_ADDRESS_DEVHEAD_SLAVE) &&
  802. !recheckAsMaster) {
  803. US_DEBUGP(" Identified ATAPI device as slave. Rechecking again as master\n");
  804. recheckAsMaster = 1;
  805. master_slave = ATA_ADDRESS_DEVHEAD_STD;
  806. } else {
  807. US_DEBUGP(" Identified ATAPI device\n");
  808. info->DeviceHead = master_slave;
  809. status = isd200_atapi_soft_reset(us);
  810. break;
  811. }
  812. } else {
  813. US_DEBUGP(" Not ATA, not ATAPI. Weird.\n");
  814. break;
  815. }
  816. /* check for timeout on this request */
  817. if (time_after_eq(jiffies, endTime)) {
  818. if (!detect)
  819. US_DEBUGP(" BSY check timeout, just continue with next operation...\n");
  820. else
  821. US_DEBUGP(" Device detect timeout!\n");
  822. break;
  823. }
  824. }
  825. return status;
  826. }
  827. /**************************************************************************
  828. * isd200_manual_enum
  829. *
  830. * Determines if the drive attached is an ATA or ATAPI and if it is a
  831. * master or slave.
  832. *
  833. * RETURNS:
  834. * ISD status code
  835. */
  836. static int isd200_manual_enum(struct us_data *us)
  837. {
  838. struct isd200_info *info = (struct isd200_info *)us->extra;
  839. int retStatus = ISD200_GOOD;
  840. US_DEBUGP("Entering isd200_manual_enum\n");
  841. retStatus = isd200_read_config(us);
  842. if (retStatus == ISD200_GOOD) {
  843. int isslave;
  844. /* master or slave? */
  845. retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 0);
  846. if (retStatus == ISD200_GOOD)
  847. retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_SLAVE, 0);
  848. if (retStatus == ISD200_GOOD) {
  849. retStatus = isd200_srst(us);
  850. if (retStatus == ISD200_GOOD)
  851. /* ata or atapi? */
  852. retStatus = isd200_try_enum( us, ATA_ADDRESS_DEVHEAD_STD, 1);
  853. }
  854. isslave = (info->DeviceHead & ATA_ADDRESS_DEVHEAD_SLAVE) ? 1 : 0;
  855. if (!(info->ConfigData.ATAConfig & ATACFG_MASTER)) {
  856. US_DEBUGP(" Setting Master/Slave selection to %d\n", isslave);
  857. info->ConfigData.ATAConfig &= 0x3f;
  858. info->ConfigData.ATAConfig |= (isslave<<6);
  859. retStatus = isd200_write_config(us);
  860. }
  861. }
  862. US_DEBUGP("Leaving isd200_manual_enum %08X\n", retStatus);
  863. return(retStatus);
  864. }
  865. /*
  866. * We are the last non IDE user of the legacy IDE ident structures
  867. * and we thus want to keep a private copy of this function so the
  868. * driver can be used without the obsolete drivers/ide layer
  869. */
  870. static void isd200_fix_driveid (struct hd_driveid *id)
  871. {
  872. #ifndef __LITTLE_ENDIAN
  873. # ifdef __BIG_ENDIAN
  874. int i;
  875. u16 *stringcast;
  876. id->config = __le16_to_cpu(id->config);
  877. id->cyls = __le16_to_cpu(id->cyls);
  878. id->reserved2 = __le16_to_cpu(id->reserved2);
  879. id->heads = __le16_to_cpu(id->heads);
  880. id->track_bytes = __le16_to_cpu(id->track_bytes);
  881. id->sector_bytes = __le16_to_cpu(id->sector_bytes);
  882. id->sectors = __le16_to_cpu(id->sectors);
  883. id->vendor0 = __le16_to_cpu(id->vendor0);
  884. id->vendor1 = __le16_to_cpu(id->vendor1);
  885. id->vendor2 = __le16_to_cpu(id->vendor2);
  886. stringcast = (u16 *)&id->serial_no[0];
  887. for (i = 0; i < (20/2); i++)
  888. stringcast[i] = __le16_to_cpu(stringcast[i]);
  889. id->buf_type = __le16_to_cpu(id->buf_type);
  890. id->buf_size = __le16_to_cpu(id->buf_size);
  891. id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
  892. stringcast = (u16 *)&id->fw_rev[0];
  893. for (i = 0; i < (8/2); i++)
  894. stringcast[i] = __le16_to_cpu(stringcast[i]);
  895. stringcast = (u16 *)&id->model[0];
  896. for (i = 0; i < (40/2); i++)
  897. stringcast[i] = __le16_to_cpu(stringcast[i]);
  898. id->dword_io = __le16_to_cpu(id->dword_io);
  899. id->reserved50 = __le16_to_cpu(id->reserved50);
  900. id->field_valid = __le16_to_cpu(id->field_valid);
  901. id->cur_cyls = __le16_to_cpu(id->cur_cyls);
  902. id->cur_heads = __le16_to_cpu(id->cur_heads);
  903. id->cur_sectors = __le16_to_cpu(id->cur_sectors);
  904. id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
  905. id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
  906. id->lba_capacity = __le32_to_cpu(id->lba_capacity);
  907. id->dma_1word = __le16_to_cpu(id->dma_1word);
  908. id->dma_mword = __le16_to_cpu(id->dma_mword);
  909. id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
  910. id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
  911. id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
  912. id->eide_pio = __le16_to_cpu(id->eide_pio);
  913. id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
  914. for (i = 0; i < 2; ++i)
  915. id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
  916. for (i = 0; i < 4; ++i)
  917. id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
  918. id->queue_depth = __le16_to_cpu(id->queue_depth);
  919. for (i = 0; i < 4; ++i)
  920. id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
  921. id->major_rev_num = __le16_to_cpu(id->major_rev_num);
  922. id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
  923. id->command_set_1 = __le16_to_cpu(id->command_set_1);
  924. id->command_set_2 = __le16_to_cpu(id->command_set_2);
  925. id->cfsse = __le16_to_cpu(id->cfsse);
  926. id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
  927. id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
  928. id->csf_default = __le16_to_cpu(id->csf_default);
  929. id->dma_ultra = __le16_to_cpu(id->dma_ultra);
  930. id->trseuc = __le16_to_cpu(id->trseuc);
  931. id->trsEuc = __le16_to_cpu(id->trsEuc);
  932. id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
  933. id->mprc = __le16_to_cpu(id->mprc);
  934. id->hw_config = __le16_to_cpu(id->hw_config);
  935. id->acoustic = __le16_to_cpu(id->acoustic);
  936. id->msrqs = __le16_to_cpu(id->msrqs);
  937. id->sxfert = __le16_to_cpu(id->sxfert);
  938. id->sal = __le16_to_cpu(id->sal);
  939. id->spg = __le32_to_cpu(id->spg);
  940. id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
  941. for (i = 0; i < 22; i++)
  942. id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
  943. id->last_lun = __le16_to_cpu(id->last_lun);
  944. id->word127 = __le16_to_cpu(id->word127);
  945. id->dlf = __le16_to_cpu(id->dlf);
  946. id->csfo = __le16_to_cpu(id->csfo);
  947. for (i = 0; i < 26; i++)
  948. id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
  949. id->word156 = __le16_to_cpu(id->word156);
  950. for (i = 0; i < 3; i++)
  951. id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
  952. id->cfa_power = __le16_to_cpu(id->cfa_power);
  953. for (i = 0; i < 14; i++)
  954. id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
  955. for (i = 0; i < 31; i++)
  956. id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
  957. for (i = 0; i < 48; i++)
  958. id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
  959. id->integrity_word = __le16_to_cpu(id->integrity_word);
  960. # else
  961. # error "Please fix <asm/byteorder.h>"
  962. # endif
  963. #endif
  964. }
  965. /**************************************************************************
  966. * isd200_get_inquiry_data
  967. *
  968. * Get inquiry data
  969. *
  970. * RETURNS:
  971. * ISD status code
  972. */
  973. static int isd200_get_inquiry_data( struct us_data *us )
  974. {
  975. struct isd200_info *info = (struct isd200_info *)us->extra;
  976. int retStatus = ISD200_GOOD;
  977. struct hd_driveid *id = info->id;
  978. US_DEBUGP("Entering isd200_get_inquiry_data\n");
  979. /* set default to Master */
  980. info->DeviceHead = ATA_ADDRESS_DEVHEAD_STD;
  981. /* attempt to manually enumerate this device */
  982. retStatus = isd200_manual_enum(us);
  983. if (retStatus == ISD200_GOOD) {
  984. int transferStatus;
  985. /* check for an ATA device */
  986. if (info->DeviceFlags & DF_ATA_DEVICE) {
  987. /* this must be an ATA device */
  988. /* perform an ATA Command Identify */
  989. transferStatus = isd200_action( us, ACTION_IDENTIFY,
  990. id,
  991. sizeof(struct hd_driveid) );
  992. if (transferStatus != ISD200_TRANSPORT_GOOD) {
  993. /* Error issuing ATA Command Identify */
  994. US_DEBUGP(" Error issuing ATA Command Identify\n");
  995. retStatus = ISD200_ERROR;
  996. } else {
  997. /* ATA Command Identify successful */
  998. int i;
  999. __be16 *src;
  1000. __u16 *dest;
  1001. isd200_fix_driveid(id);
  1002. US_DEBUGP(" Identify Data Structure:\n");
  1003. US_DEBUGP(" config = 0x%x\n", id->config);
  1004. US_DEBUGP(" cyls = 0x%x\n", id->cyls);
  1005. US_DEBUGP(" heads = 0x%x\n", id->heads);
  1006. US_DEBUGP(" track_bytes = 0x%x\n", id->track_bytes);
  1007. US_DEBUGP(" sector_bytes = 0x%x\n", id->sector_bytes);
  1008. US_DEBUGP(" sectors = 0x%x\n", id->sectors);
  1009. US_DEBUGP(" serial_no[0] = 0x%x\n", id->serial_no[0]);
  1010. US_DEBUGP(" buf_type = 0x%x\n", id->buf_type);
  1011. US_DEBUGP(" buf_size = 0x%x\n", id->buf_size);
  1012. US_DEBUGP(" ecc_bytes = 0x%x\n", id->ecc_bytes);
  1013. US_DEBUGP(" fw_rev[0] = 0x%x\n", id->fw_rev[0]);
  1014. US_DEBUGP(" model[0] = 0x%x\n", id->model[0]);
  1015. US_DEBUGP(" max_multsect = 0x%x\n", id->max_multsect);
  1016. US_DEBUGP(" dword_io = 0x%x\n", id->dword_io);
  1017. US_DEBUGP(" capability = 0x%x\n", id->capability);
  1018. US_DEBUGP(" tPIO = 0x%x\n", id->tPIO);
  1019. US_DEBUGP(" tDMA = 0x%x\n", id->tDMA);
  1020. US_DEBUGP(" field_valid = 0x%x\n", id->field_valid);
  1021. US_DEBUGP(" cur_cyls = 0x%x\n", id->cur_cyls);
  1022. US_DEBUGP(" cur_heads = 0x%x\n", id->cur_heads);
  1023. US_DEBUGP(" cur_sectors = 0x%x\n", id->cur_sectors);
  1024. US_DEBUGP(" cur_capacity = 0x%x\n", (id->cur_capacity1 << 16) + id->cur_capacity0 );
  1025. US_DEBUGP(" multsect = 0x%x\n", id->multsect);
  1026. US_DEBUGP(" lba_capacity = 0x%x\n", id->lba_capacity);
  1027. US_DEBUGP(" command_set_1 = 0x%x\n", id->command_set_1);
  1028. US_DEBUGP(" command_set_2 = 0x%x\n", id->command_set_2);
  1029. memset(&info->InquiryData, 0, sizeof(info->InquiryData));
  1030. /* Standard IDE interface only supports disks */
  1031. info->InquiryData.DeviceType = DIRECT_ACCESS_DEVICE;
  1032. /* The length must be at least 36 (5 + 31) */
  1033. info->InquiryData.AdditionalLength = 0x1F;
  1034. if (id->command_set_1 & COMMANDSET_MEDIA_STATUS) {
  1035. /* set the removable bit */
  1036. info->InquiryData.DeviceTypeModifier = DEVICE_REMOVABLE;
  1037. info->DeviceFlags |= DF_REMOVABLE_MEDIA;
  1038. }
  1039. /* Fill in vendor identification fields */
  1040. src = (__be16*)id->model;
  1041. dest = (__u16*)info->InquiryData.VendorId;
  1042. for (i=0;i<4;i++)
  1043. dest[i] = be16_to_cpu(src[i]);
  1044. src = (__be16*)(id->model+8);
  1045. dest = (__u16*)info->InquiryData.ProductId;
  1046. for (i=0;i<8;i++)
  1047. dest[i] = be16_to_cpu(src[i]);
  1048. src = (__be16*)id->fw_rev;
  1049. dest = (__u16*)info->InquiryData.ProductRevisionLevel;
  1050. for (i=0;i<2;i++)
  1051. dest[i] = be16_to_cpu(src[i]);
  1052. /* determine if it supports Media Status Notification */
  1053. if (id->command_set_2 & COMMANDSET_MEDIA_STATUS) {
  1054. US_DEBUGP(" Device supports Media Status Notification\n");
  1055. /* Indicate that it is enabled, even though it is not
  1056. * This allows the lock/unlock of the media to work
  1057. * correctly.
  1058. */
  1059. info->DeviceFlags |= DF_MEDIA_STATUS_ENABLED;
  1060. }
  1061. else
  1062. info->DeviceFlags &= ~DF_MEDIA_STATUS_ENABLED;
  1063. }
  1064. } else {
  1065. /*
  1066. * this must be an ATAPI device
  1067. * use an ATAPI protocol (Transparent SCSI)
  1068. */
  1069. us->protocol_name = "Transparent SCSI";
  1070. us->proto_handler = usb_stor_transparent_scsi_command;
  1071. US_DEBUGP("Protocol changed to: %s\n", us->protocol_name);
  1072. /* Free driver structure */
  1073. us->extra_destructor(info);
  1074. us->extra = NULL;
  1075. us->extra_destructor = NULL;
  1076. }
  1077. }
  1078. US_DEBUGP("Leaving isd200_get_inquiry_data %08X\n", retStatus);
  1079. return(retStatus);
  1080. }
  1081. /**************************************************************************
  1082. * isd200_scsi_to_ata
  1083. *
  1084. * Translate SCSI commands to ATA commands.
  1085. *
  1086. * RETURNS:
  1087. * 1 if the command needs to be sent to the transport layer
  1088. * 0 otherwise
  1089. */
  1090. static int isd200_scsi_to_ata(struct scsi_cmnd *srb, struct us_data *us,
  1091. union ata_cdb * ataCdb)
  1092. {
  1093. struct isd200_info *info = (struct isd200_info *)us->extra;
  1094. struct hd_driveid *id = info->id;
  1095. int sendToTransport = 1;
  1096. unsigned char sectnum, head;
  1097. unsigned short cylinder;
  1098. unsigned long lba;
  1099. unsigned long blockCount;
  1100. unsigned char senseData[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  1101. memset(ataCdb, 0, sizeof(union ata_cdb));
  1102. /* SCSI Command */
  1103. switch (srb->cmnd[0]) {
  1104. case INQUIRY:
  1105. US_DEBUGP(" ATA OUT - INQUIRY\n");
  1106. /* copy InquiryData */
  1107. usb_stor_set_xfer_buf((unsigned char *) &info->InquiryData,
  1108. sizeof(info->InquiryData), srb);
  1109. srb->result = SAM_STAT_GOOD;
  1110. sendToTransport = 0;
  1111. break;
  1112. case MODE_SENSE:
  1113. US_DEBUGP(" ATA OUT - SCSIOP_MODE_SENSE\n");
  1114. /* Initialize the return buffer */
  1115. usb_stor_set_xfer_buf(senseData, sizeof(senseData), srb);
  1116. if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
  1117. {
  1118. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1119. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1120. ataCdb->generic.TransferBlockSize = 1;
  1121. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1122. ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
  1123. isd200_srb_set_bufflen(srb, 0);
  1124. } else {
  1125. US_DEBUGP(" Media Status not supported, just report okay\n");
  1126. srb->result = SAM_STAT_GOOD;
  1127. sendToTransport = 0;
  1128. }
  1129. break;
  1130. case TEST_UNIT_READY:
  1131. US_DEBUGP(" ATA OUT - SCSIOP_TEST_UNIT_READY\n");
  1132. if (info->DeviceFlags & DF_MEDIA_STATUS_ENABLED)
  1133. {
  1134. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1135. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1136. ataCdb->generic.TransferBlockSize = 1;
  1137. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1138. ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
  1139. isd200_srb_set_bufflen(srb, 0);
  1140. } else {
  1141. US_DEBUGP(" Media Status not supported, just report okay\n");
  1142. srb->result = SAM_STAT_GOOD;
  1143. sendToTransport = 0;
  1144. }
  1145. break;
  1146. case READ_CAPACITY:
  1147. {
  1148. unsigned long capacity;
  1149. struct read_capacity_data readCapacityData;
  1150. US_DEBUGP(" ATA OUT - SCSIOP_READ_CAPACITY\n");
  1151. if (id->capability & CAPABILITY_LBA ) {
  1152. capacity = id->lba_capacity - 1;
  1153. } else {
  1154. capacity = (id->heads *
  1155. id->cyls *
  1156. id->sectors) - 1;
  1157. }
  1158. readCapacityData.LogicalBlockAddress = cpu_to_be32(capacity);
  1159. readCapacityData.BytesPerBlock = cpu_to_be32(0x200);
  1160. usb_stor_set_xfer_buf((unsigned char *) &readCapacityData,
  1161. sizeof(readCapacityData), srb);
  1162. srb->result = SAM_STAT_GOOD;
  1163. sendToTransport = 0;
  1164. }
  1165. break;
  1166. case READ_10:
  1167. US_DEBUGP(" ATA OUT - SCSIOP_READ\n");
  1168. lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
  1169. blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
  1170. if (id->capability & CAPABILITY_LBA) {
  1171. sectnum = (unsigned char)(lba);
  1172. cylinder = (unsigned short)(lba>>8);
  1173. head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
  1174. } else {
  1175. sectnum = (unsigned char)((lba % id->sectors) + 1);
  1176. cylinder = (unsigned short)(lba / (id->sectors *
  1177. id->heads));
  1178. head = (unsigned char)((lba / id->sectors) %
  1179. id->heads);
  1180. }
  1181. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1182. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1183. ataCdb->generic.TransferBlockSize = 1;
  1184. ataCdb->generic.RegisterSelect =
  1185. REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
  1186. REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
  1187. REG_DEVICE_HEAD | REG_COMMAND;
  1188. ataCdb->write.SectorCountByte = (unsigned char)blockCount;
  1189. ataCdb->write.SectorNumberByte = sectnum;
  1190. ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
  1191. ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
  1192. ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
  1193. ataCdb->write.CommandByte = WIN_READ;
  1194. break;
  1195. case WRITE_10:
  1196. US_DEBUGP(" ATA OUT - SCSIOP_WRITE\n");
  1197. lba = be32_to_cpu(*(__be32 *)&srb->cmnd[2]);
  1198. blockCount = (unsigned long)srb->cmnd[7]<<8 | (unsigned long)srb->cmnd[8];
  1199. if (id->capability & CAPABILITY_LBA) {
  1200. sectnum = (unsigned char)(lba);
  1201. cylinder = (unsigned short)(lba>>8);
  1202. head = ATA_ADDRESS_DEVHEAD_LBA_MODE | (unsigned char)(lba>>24 & 0x0F);
  1203. } else {
  1204. sectnum = (unsigned char)((lba % id->sectors) + 1);
  1205. cylinder = (unsigned short)(lba / (id->sectors * id->heads));
  1206. head = (unsigned char)((lba / id->sectors) % id->heads);
  1207. }
  1208. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1209. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1210. ataCdb->generic.TransferBlockSize = 1;
  1211. ataCdb->generic.RegisterSelect =
  1212. REG_SECTOR_COUNT | REG_SECTOR_NUMBER |
  1213. REG_CYLINDER_LOW | REG_CYLINDER_HIGH |
  1214. REG_DEVICE_HEAD | REG_COMMAND;
  1215. ataCdb->write.SectorCountByte = (unsigned char)blockCount;
  1216. ataCdb->write.SectorNumberByte = sectnum;
  1217. ataCdb->write.CylinderHighByte = (unsigned char)(cylinder>>8);
  1218. ataCdb->write.CylinderLowByte = (unsigned char)cylinder;
  1219. ataCdb->write.DeviceHeadByte = (head | ATA_ADDRESS_DEVHEAD_STD);
  1220. ataCdb->write.CommandByte = WIN_WRITE;
  1221. break;
  1222. case ALLOW_MEDIUM_REMOVAL:
  1223. US_DEBUGP(" ATA OUT - SCSIOP_MEDIUM_REMOVAL\n");
  1224. if (info->DeviceFlags & DF_REMOVABLE_MEDIA) {
  1225. US_DEBUGP(" srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
  1226. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1227. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1228. ataCdb->generic.TransferBlockSize = 1;
  1229. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1230. ataCdb->write.CommandByte = (srb->cmnd[4] & 0x1) ?
  1231. WIN_DOORLOCK : WIN_DOORUNLOCK;
  1232. isd200_srb_set_bufflen(srb, 0);
  1233. } else {
  1234. US_DEBUGP(" Not removeable media, just report okay\n");
  1235. srb->result = SAM_STAT_GOOD;
  1236. sendToTransport = 0;
  1237. }
  1238. break;
  1239. case START_STOP:
  1240. US_DEBUGP(" ATA OUT - SCSIOP_START_STOP_UNIT\n");
  1241. US_DEBUGP(" srb->cmnd[4] = 0x%X\n", srb->cmnd[4]);
  1242. if ((srb->cmnd[4] & 0x3) == 0x2) {
  1243. US_DEBUGP(" Media Eject\n");
  1244. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1245. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1246. ataCdb->generic.TransferBlockSize = 0;
  1247. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1248. ataCdb->write.CommandByte = ATA_COMMAND_MEDIA_EJECT;
  1249. } else if ((srb->cmnd[4] & 0x3) == 0x1) {
  1250. US_DEBUGP(" Get Media Status\n");
  1251. ataCdb->generic.SignatureByte0 = info->ConfigData.ATAMajorCommand;
  1252. ataCdb->generic.SignatureByte1 = info->ConfigData.ATAMinorCommand;
  1253. ataCdb->generic.TransferBlockSize = 1;
  1254. ataCdb->generic.RegisterSelect = REG_COMMAND;
  1255. ataCdb->write.CommandByte = ATA_COMMAND_GET_MEDIA_STATUS;
  1256. isd200_srb_set_bufflen(srb, 0);
  1257. } else {
  1258. US_DEBUGP(" Nothing to do, just report okay\n");
  1259. srb->result = SAM_STAT_GOOD;
  1260. sendToTransport = 0;
  1261. }
  1262. break;
  1263. default:
  1264. US_DEBUGP("Unsupported SCSI command - 0x%X\n", srb->cmnd[0]);
  1265. srb->result = DID_ERROR << 16;
  1266. sendToTransport = 0;
  1267. break;
  1268. }
  1269. return(sendToTransport);
  1270. }
  1271. /**************************************************************************
  1272. * isd200_free_info
  1273. *
  1274. * Frees the driver structure.
  1275. */
  1276. static void isd200_free_info_ptrs(void *info_)
  1277. {
  1278. struct isd200_info *info = (struct isd200_info *) info_;
  1279. if (info) {
  1280. kfree(info->id);
  1281. kfree(info->RegsBuf);
  1282. }
  1283. }
  1284. /**************************************************************************
  1285. * isd200_init_info
  1286. *
  1287. * Allocates (if necessary) and initializes the driver structure.
  1288. *
  1289. * RETURNS:
  1290. * ISD status code
  1291. */
  1292. static int isd200_init_info(struct us_data *us)
  1293. {
  1294. int retStatus = ISD200_GOOD;
  1295. struct isd200_info *info;
  1296. info = (struct isd200_info *)
  1297. kzalloc(sizeof(struct isd200_info), GFP_KERNEL);
  1298. if (!info)
  1299. retStatus = ISD200_ERROR;
  1300. else {
  1301. info->id = (struct hd_driveid *)
  1302. kzalloc(sizeof(struct hd_driveid), GFP_KERNEL);
  1303. info->RegsBuf = (unsigned char *)
  1304. kmalloc(sizeof(info->ATARegs), GFP_KERNEL);
  1305. if (!info->id || !info->RegsBuf) {
  1306. isd200_free_info_ptrs(info);
  1307. kfree(info);
  1308. retStatus = ISD200_ERROR;
  1309. }
  1310. }
  1311. if (retStatus == ISD200_GOOD) {
  1312. us->extra = info;
  1313. us->extra_destructor = isd200_free_info_ptrs;
  1314. } else
  1315. US_DEBUGP("ERROR - kmalloc failure\n");
  1316. return retStatus;
  1317. }
  1318. /**************************************************************************
  1319. * Initialization for the ISD200
  1320. */
  1321. int isd200_Initialization(struct us_data *us)
  1322. {
  1323. US_DEBUGP("ISD200 Initialization...\n");
  1324. /* Initialize ISD200 info struct */
  1325. if (isd200_init_info(us) == ISD200_ERROR) {
  1326. US_DEBUGP("ERROR Initializing ISD200 Info struct\n");
  1327. } else {
  1328. /* Get device specific data */
  1329. if (isd200_get_inquiry_data(us) != ISD200_GOOD)
  1330. US_DEBUGP("ISD200 Initialization Failure\n");
  1331. else
  1332. US_DEBUGP("ISD200 Initialization complete\n");
  1333. }
  1334. return 0;
  1335. }
  1336. /**************************************************************************
  1337. * Protocol and Transport for the ISD200 ASIC
  1338. *
  1339. * This protocol and transport are for ATA devices connected to an ISD200
  1340. * ASIC. An ATAPI device that is conected as a slave device will be
  1341. * detected in the driver initialization function and the protocol will
  1342. * be changed to an ATAPI protocol (Transparent SCSI).
  1343. *
  1344. */
  1345. void isd200_ata_command(struct scsi_cmnd *srb, struct us_data *us)
  1346. {
  1347. int sendToTransport = 1, orig_bufflen;
  1348. union ata_cdb ataCdb;
  1349. /* Make sure driver was initialized */
  1350. if (us->extra == NULL)
  1351. US_DEBUGP("ERROR Driver not initialized\n");
  1352. scsi_set_resid(srb, 0);
  1353. /* scsi_bufflen might change in protocol translation to ata */
  1354. orig_bufflen = scsi_bufflen(srb);
  1355. sendToTransport = isd200_scsi_to_ata(srb, us, &ataCdb);
  1356. /* send the command to the transport layer */
  1357. if (sendToTransport)
  1358. isd200_invoke_transport(us, srb, &ataCdb);
  1359. isd200_srb_set_bufflen(srb, orig_bufflen);
  1360. }