isd200.c 46 KB

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