isd200.c 45 KB

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