acsi.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. /*
  2. * acsi.c -- Device driver for Atari ACSI hard disks
  3. *
  4. * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  5. *
  6. * Some parts are based on hd.c by Linus Torvalds
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file COPYING in the main directory of this archive for
  10. * more details.
  11. *
  12. */
  13. /*
  14. * Still to in this file:
  15. * - If a command ends with an error status (!= 0), the following
  16. * REQUEST SENSE commands (4 to fill the ST-DMA FIFO) are done by
  17. * polling the _IRQ signal (not interrupt-driven). This should be
  18. * avoided in future because it takes up a non-neglectible time in
  19. * the interrupt service routine while interrupts are disabled.
  20. * Maybe a timer interrupt will get lost :-(
  21. */
  22. /*
  23. * General notes:
  24. *
  25. * - All ACSI devices (disks, CD-ROMs, ...) use major number 28.
  26. * Minors are organized like it is with SCSI: The upper 4 bits
  27. * identify the device, the lower 4 bits the partition.
  28. * The device numbers (the upper 4 bits) are given in the same
  29. * order as the devices are found on the bus.
  30. * - Up to 8 LUNs are supported for each target (if CONFIG_ACSI_MULTI_LUN
  31. * is defined), but only a total of 16 devices (due to minor
  32. * numbers...). Note that Atari allows only a maximum of 4 targets
  33. * (i.e. controllers, not devices) on the ACSI bus!
  34. * - A optimizing scheme similar to SCSI scatter-gather is implemented.
  35. * - Removable media are supported. After a medium change to device
  36. * is reinitialized (partition check etc.). Also, if the device
  37. * knows the PREVENT/ALLOW MEDIUM REMOVAL command, the door should
  38. * be locked and unlocked when mounting the first or unmounting the
  39. * last filesystem on the device. The code is untested, because I
  40. * don't have a removable hard disk.
  41. *
  42. */
  43. #include <linux/config.h>
  44. #include <linux/module.h>
  45. #include <linux/errno.h>
  46. #include <linux/signal.h>
  47. #include <linux/sched.h>
  48. #include <linux/timer.h>
  49. #include <linux/fs.h>
  50. #include <linux/kernel.h>
  51. #include <linux/genhd.h>
  52. #include <linux/delay.h>
  53. #include <linux/mm.h>
  54. #include <linux/major.h>
  55. #include <linux/slab.h>
  56. #include <linux/interrupt.h>
  57. #include <scsi/scsi.h> /* for SCSI_IOCTL_GET_IDLUN */
  58. #include <scsi/scsi_ioctl.h>
  59. #include <linux/hdreg.h> /* for HDIO_GETGEO */
  60. #include <linux/blkpg.h>
  61. #include <linux/buffer_head.h>
  62. #include <linux/blkdev.h>
  63. #include <asm/setup.h>
  64. #include <asm/pgtable.h>
  65. #include <asm/system.h>
  66. #include <asm/uaccess.h>
  67. #include <asm/atarihw.h>
  68. #include <asm/atariints.h>
  69. #include <asm/atari_acsi.h>
  70. #include <asm/atari_stdma.h>
  71. #include <asm/atari_stram.h>
  72. static void (*do_acsi)(void) = NULL;
  73. static struct request_queue *acsi_queue;
  74. #define QUEUE (acsi_queue)
  75. #define CURRENT elv_next_request(acsi_queue)
  76. #define DEBUG
  77. #undef DEBUG_DETECT
  78. #undef NO_WRITE
  79. #define MAX_ERRORS 8 /* Max read/write errors/sector */
  80. #define MAX_LUN 8 /* Max LUNs per target */
  81. #define MAX_DEV 16
  82. #define ACSI_BUFFER_SIZE (16*1024) /* "normal" ACSI buffer size */
  83. #define ACSI_BUFFER_MINSIZE (2048) /* min. buf size if ext. DMA */
  84. #define ACSI_BUFFER_SIZE_ORDER 2 /* order size for above */
  85. #define ACSI_BUFFER_MINSIZE_ORDER 0 /* order size for above */
  86. #define ACSI_BUFFER_SECTORS (ACSI_BUFFER_SIZE/512)
  87. #define ACSI_BUFFER_ORDER \
  88. (ATARIHW_PRESENT(EXTD_DMA) ? \
  89. ACSI_BUFFER_MINSIZE_ORDER : \
  90. ACSI_BUFFER_SIZE_ORDER)
  91. #define ACSI_TIMEOUT (4*HZ)
  92. /* minimum delay between two commands */
  93. #define COMMAND_DELAY 500
  94. typedef enum {
  95. NONE, HARDDISK, CDROM
  96. } ACSI_TYPE;
  97. struct acsi_info_struct {
  98. ACSI_TYPE type; /* type of device */
  99. unsigned target; /* target number */
  100. unsigned lun; /* LUN in target controller */
  101. unsigned removable : 1; /* Flag for removable media */
  102. unsigned read_only : 1; /* Flag for read only devices */
  103. unsigned old_atari_disk : 1; /* Is an old Atari disk */
  104. unsigned changed : 1; /* Medium has been changed */
  105. unsigned long size; /* #blocks */
  106. int access_count;
  107. } acsi_info[MAX_DEV];
  108. /*
  109. * SENSE KEYS
  110. */
  111. #define NO_SENSE 0x00
  112. #define RECOVERED_ERROR 0x01
  113. #define NOT_READY 0x02
  114. #define MEDIUM_ERROR 0x03
  115. #define HARDWARE_ERROR 0x04
  116. #define ILLEGAL_REQUEST 0x05
  117. #define UNIT_ATTENTION 0x06
  118. #define DATA_PROTECT 0x07
  119. #define BLANK_CHECK 0x08
  120. #define COPY_ABORTED 0x0a
  121. #define ABORTED_COMMAND 0x0b
  122. #define VOLUME_OVERFLOW 0x0d
  123. #define MISCOMPARE 0x0e
  124. /*
  125. * DEVICE TYPES
  126. */
  127. #define TYPE_DISK 0x00
  128. #define TYPE_TAPE 0x01
  129. #define TYPE_WORM 0x04
  130. #define TYPE_ROM 0x05
  131. #define TYPE_MOD 0x07
  132. #define TYPE_NO_LUN 0x7f
  133. /* The data returned by MODE SENSE differ between the old Atari
  134. * hard disks and SCSI disks connected to ACSI. In the following, both
  135. * formats are defined and some macros to operate on them potably.
  136. */
  137. typedef struct {
  138. unsigned long dummy[2];
  139. unsigned long sector_size;
  140. unsigned char format_code;
  141. #define ATARI_SENSE_FORMAT_FIX 1
  142. #define ATARI_SENSE_FORMAT_CHNG 2
  143. unsigned char cylinders_h;
  144. unsigned char cylinders_l;
  145. unsigned char heads;
  146. unsigned char reduced_h;
  147. unsigned char reduced_l;
  148. unsigned char precomp_h;
  149. unsigned char precomp_l;
  150. unsigned char landing_zone;
  151. unsigned char steprate;
  152. unsigned char type;
  153. #define ATARI_SENSE_TYPE_FIXCHNG_MASK 4
  154. #define ATARI_SENSE_TYPE_SOFTHARD_MASK 8
  155. #define ATARI_SENSE_TYPE_FIX 4
  156. #define ATARI_SENSE_TYPE_CHNG 0
  157. #define ATARI_SENSE_TYPE_SOFT 0
  158. #define ATARI_SENSE_TYPE_HARD 8
  159. unsigned char sectors;
  160. } ATARI_SENSE_DATA;
  161. #define ATARI_CAPACITY(sd) \
  162. (((int)((sd).cylinders_h<<8)|(sd).cylinders_l) * \
  163. (sd).heads * (sd).sectors)
  164. typedef struct {
  165. unsigned char dummy1;
  166. unsigned char medium_type;
  167. unsigned char dummy2;
  168. unsigned char descriptor_size;
  169. unsigned long block_count;
  170. unsigned long sector_size;
  171. /* Page 0 data */
  172. unsigned char page_code;
  173. unsigned char page_size;
  174. unsigned char page_flags;
  175. unsigned char qualifier;
  176. } SCSI_SENSE_DATA;
  177. #define SCSI_CAPACITY(sd) ((sd).block_count & 0xffffff)
  178. typedef union {
  179. ATARI_SENSE_DATA atari;
  180. SCSI_SENSE_DATA scsi;
  181. } SENSE_DATA;
  182. #define SENSE_TYPE_UNKNOWN 0
  183. #define SENSE_TYPE_ATARI 1
  184. #define SENSE_TYPE_SCSI 2
  185. #define SENSE_TYPE(sd) \
  186. (((sd).atari.dummy[0] == 8 && \
  187. ((sd).atari.format_code == 1 || \
  188. (sd).atari.format_code == 2)) ? SENSE_TYPE_ATARI : \
  189. ((sd).scsi.dummy1 >= 11) ? SENSE_TYPE_SCSI : \
  190. SENSE_TYPE_UNKNOWN)
  191. #define CAPACITY(sd) \
  192. (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ? \
  193. ATARI_CAPACITY((sd).atari) : \
  194. SCSI_CAPACITY((sd).scsi))
  195. #define SECTOR_SIZE(sd) \
  196. (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ? \
  197. (sd).atari.sector_size : \
  198. (sd).scsi.sector_size & 0xffffff)
  199. /* Default size if capacity cannot be determined (1 GByte) */
  200. #define DEFAULT_SIZE 0x1fffff
  201. #define CARTRCH_STAT(aip,buf) \
  202. (aip->old_atari_disk ? \
  203. (((buf)[0] & 0x7f) == 0x28) : \
  204. ((((buf)[0] & 0x70) == 0x70) ? \
  205. (((buf)[2] & 0x0f) == 0x06) : \
  206. (((buf)[0] & 0x0f) == 0x06))) \
  207. /* These two are also exported to other drivers that work on the ACSI bus and
  208. * need an ST-RAM buffer. */
  209. char *acsi_buffer;
  210. unsigned long phys_acsi_buffer;
  211. static int NDevices;
  212. static int CurrentNReq;
  213. static int CurrentNSect;
  214. static char *CurrentBuffer;
  215. static DEFINE_SPINLOCK(acsi_lock);
  216. #define SET_TIMER() mod_timer(&acsi_timer, jiffies + ACSI_TIMEOUT)
  217. #define CLEAR_TIMER() del_timer(&acsi_timer)
  218. static unsigned long STramMask;
  219. #define STRAM_ADDR(a) (((a) & STramMask) == 0)
  220. /* ACSI commands */
  221. static char tur_cmd[6] = { 0x00, 0, 0, 0, 0, 0 };
  222. static char modesense_cmd[6] = { 0x1a, 0, 0, 0, 24, 0 };
  223. static char modeselect_cmd[6] = { 0x15, 0, 0, 0, 12, 0 };
  224. static char inquiry_cmd[6] = { 0x12, 0, 0, 0,255, 0 };
  225. static char reqsense_cmd[6] = { 0x03, 0, 0, 0, 4, 0 };
  226. static char read_cmd[6] = { 0x08, 0, 0, 0, 0, 0 };
  227. static char write_cmd[6] = { 0x0a, 0, 0, 0, 0, 0 };
  228. static char pa_med_rem_cmd[6] = { 0x1e, 0, 0, 0, 0, 0 };
  229. #define CMDSET_TARG_LUN(cmd,targ,lun) \
  230. do { \
  231. cmd[0] = (cmd[0] & ~0xe0) | (targ)<<5; \
  232. cmd[1] = (cmd[1] & ~0xe0) | (lun)<<5; \
  233. } while(0)
  234. #define CMDSET_BLOCK(cmd,blk) \
  235. do { \
  236. unsigned long __blk = (blk); \
  237. cmd[3] = __blk; __blk >>= 8; \
  238. cmd[2] = __blk; __blk >>= 8; \
  239. cmd[1] = (cmd[1] & 0xe0) | (__blk & 0x1f); \
  240. } while(0)
  241. #define CMDSET_LEN(cmd,len) \
  242. do { \
  243. cmd[4] = (len); \
  244. } while(0)
  245. /* ACSI errors (from REQUEST SENSE); There are two tables, one for the
  246. * old Atari disks and one for SCSI on ACSI disks.
  247. */
  248. struct acsi_error {
  249. unsigned char code;
  250. const char *text;
  251. } atari_acsi_errors[] = {
  252. { 0x00, "No error (??)" },
  253. { 0x01, "No index pulses" },
  254. { 0x02, "Seek not complete" },
  255. { 0x03, "Write fault" },
  256. { 0x04, "Drive not ready" },
  257. { 0x06, "No Track 00 signal" },
  258. { 0x10, "ECC error in ID field" },
  259. { 0x11, "Uncorrectable data error" },
  260. { 0x12, "ID field address mark not found" },
  261. { 0x13, "Data field address mark not found" },
  262. { 0x14, "Record not found" },
  263. { 0x15, "Seek error" },
  264. { 0x18, "Data check in no retry mode" },
  265. { 0x19, "ECC error during verify" },
  266. { 0x1a, "Access to bad block" },
  267. { 0x1c, "Unformatted or bad format" },
  268. { 0x20, "Invalid command" },
  269. { 0x21, "Invalid block address" },
  270. { 0x23, "Volume overflow" },
  271. { 0x24, "Invalid argument" },
  272. { 0x25, "Invalid drive number" },
  273. { 0x26, "Byte zero parity check" },
  274. { 0x28, "Cartride changed" },
  275. { 0x2c, "Error count overflow" },
  276. { 0x30, "Controller selftest failed" }
  277. },
  278. scsi_acsi_errors[] = {
  279. { 0x00, "No error (??)" },
  280. { 0x01, "Recovered error" },
  281. { 0x02, "Drive not ready" },
  282. { 0x03, "Uncorrectable medium error" },
  283. { 0x04, "Hardware error" },
  284. { 0x05, "Illegal request" },
  285. { 0x06, "Unit attention (Reset or cartridge changed)" },
  286. { 0x07, "Data protection" },
  287. { 0x08, "Blank check" },
  288. { 0x0b, "Aborted Command" },
  289. { 0x0d, "Volume overflow" }
  290. };
  291. /***************************** Prototypes *****************************/
  292. static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int
  293. rwflag, int enable);
  294. static int acsi_reqsense( char *buffer, int targ, int lun);
  295. static void acsi_print_error(const unsigned char *errblk, struct acsi_info_struct *aip);
  296. static irqreturn_t acsi_interrupt (int irq, void *data, struct pt_regs *fp);
  297. static void unexpected_acsi_interrupt( void );
  298. static void bad_rw_intr( void );
  299. static void read_intr( void );
  300. static void write_intr( void);
  301. static void acsi_times_out( unsigned long dummy );
  302. static void copy_to_acsibuffer( void );
  303. static void copy_from_acsibuffer( void );
  304. static void do_end_requests( void );
  305. static void do_acsi_request( request_queue_t * );
  306. static void redo_acsi_request( void );
  307. static int acsi_ioctl( struct inode *inode, struct file *file, unsigned int
  308. cmd, unsigned long arg );
  309. static int acsi_open( struct inode * inode, struct file * filp );
  310. static int acsi_release( struct inode * inode, struct file * file );
  311. static void acsi_prevent_removal(struct acsi_info_struct *aip, int flag );
  312. static int acsi_change_blk_size( int target, int lun);
  313. static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd );
  314. static int acsi_revalidate (struct gendisk *disk);
  315. /************************* End of Prototypes **************************/
  316. DEFINE_TIMER(acsi_timer, acsi_times_out, 0, 0);
  317. #ifdef CONFIG_ATARI_SLM
  318. extern int attach_slm( int target, int lun );
  319. extern int slm_init( void );
  320. #endif
  321. /***********************************************************************
  322. *
  323. * ACSI primitives
  324. *
  325. **********************************************************************/
  326. /*
  327. * The following two functions wait for _IRQ to become Low or High,
  328. * resp., with a timeout. The 'timeout' parameter is in jiffies
  329. * (10ms).
  330. * If the functions are called with timer interrupts on (int level <
  331. * 6), the timeout is based on the 'jiffies' variable to provide exact
  332. * timeouts for device probing etc.
  333. * If interrupts are disabled, the number of tries is based on the
  334. * 'loops_per_jiffy' variable. A rough estimation is sufficient here...
  335. */
  336. #define INT_LEVEL \
  337. ({ unsigned __sr; \
  338. __asm__ __volatile__ ( "movew %/sr,%0" : "=dm" (__sr) ); \
  339. (__sr >> 8) & 7; \
  340. })
  341. int acsi_wait_for_IRQ( unsigned timeout )
  342. {
  343. if (INT_LEVEL < 6) {
  344. unsigned long maxjif = jiffies + timeout;
  345. while (time_before(jiffies, maxjif))
  346. if (!(mfp.par_dt_reg & 0x20)) return( 1 );
  347. }
  348. else {
  349. long tries = loops_per_jiffy / 8 * timeout;
  350. while( --tries >= 0 )
  351. if (!(mfp.par_dt_reg & 0x20)) return( 1 );
  352. }
  353. return( 0 ); /* timeout! */
  354. }
  355. int acsi_wait_for_noIRQ( unsigned timeout )
  356. {
  357. if (INT_LEVEL < 6) {
  358. unsigned long maxjif = jiffies + timeout;
  359. while (time_before(jiffies, maxjif))
  360. if (mfp.par_dt_reg & 0x20) return( 1 );
  361. }
  362. else {
  363. long tries = loops_per_jiffy * timeout / 8;
  364. while( tries-- >= 0 )
  365. if (mfp.par_dt_reg & 0x20) return( 1 );
  366. }
  367. return( 0 ); /* timeout! */
  368. }
  369. static struct timeval start_time;
  370. void
  371. acsi_delay_start(void)
  372. {
  373. do_gettimeofday(&start_time);
  374. }
  375. /* wait from acsi_delay_start to now usec (<1E6) usec */
  376. void
  377. acsi_delay_end(long usec)
  378. {
  379. struct timeval end_time;
  380. long deltau,deltas;
  381. do_gettimeofday(&end_time);
  382. deltau=end_time.tv_usec - start_time.tv_usec;
  383. deltas=end_time.tv_sec - start_time.tv_sec;
  384. if (deltas > 1 || deltas < 0)
  385. return;
  386. if (deltas > 0)
  387. deltau += 1000*1000;
  388. if (deltau >= usec)
  389. return;
  390. udelay(usec-deltau);
  391. }
  392. /* acsicmd_dma() sends an ACSI command and sets up the DMA to transfer
  393. * 'blocks' blocks of 512 bytes from/to 'buffer'.
  394. * Because the _IRQ signal is used for handshaking the command bytes,
  395. * the ACSI interrupt has to be disabled in this function. If the end
  396. * of the operation should be signalled by a real interrupt, it has to be
  397. * reenabled afterwards.
  398. */
  399. static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int rwflag, int enable)
  400. { unsigned long flags, paddr;
  401. int i;
  402. #ifdef NO_WRITE
  403. if (rwflag || *cmd == 0x0a) {
  404. printk( "ACSI: Write commands disabled!\n" );
  405. return( 0 );
  406. }
  407. #endif
  408. rwflag = rwflag ? 0x100 : 0;
  409. paddr = virt_to_phys( buffer );
  410. acsi_delay_end(COMMAND_DELAY);
  411. DISABLE_IRQ();
  412. local_irq_save(flags);
  413. /* Low on A1 */
  414. dma_wd.dma_mode_status = 0x88 | rwflag;
  415. MFPDELAY();
  416. /* set DMA address */
  417. dma_wd.dma_lo = (unsigned char)paddr;
  418. paddr >>= 8;
  419. MFPDELAY();
  420. dma_wd.dma_md = (unsigned char)paddr;
  421. paddr >>= 8;
  422. MFPDELAY();
  423. if (ATARIHW_PRESENT(EXTD_DMA))
  424. st_dma_ext_dmahi = (unsigned short)paddr;
  425. else
  426. dma_wd.dma_hi = (unsigned char)paddr;
  427. MFPDELAY();
  428. local_irq_restore(flags);
  429. /* send the command bytes except the last */
  430. for( i = 0; i < 5; ++i ) {
  431. DMA_LONG_WRITE( *cmd++, 0x8a | rwflag );
  432. udelay(20);
  433. if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
  434. }
  435. /* Clear FIFO and switch DMA to correct direction */
  436. dma_wd.dma_mode_status = 0x92 | (rwflag ^ 0x100);
  437. MFPDELAY();
  438. dma_wd.dma_mode_status = 0x92 | rwflag;
  439. MFPDELAY();
  440. /* How many sectors for DMA */
  441. dma_wd.fdc_acces_seccount = blocks;
  442. MFPDELAY();
  443. /* send last command byte */
  444. dma_wd.dma_mode_status = 0x8a | rwflag;
  445. MFPDELAY();
  446. DMA_LONG_WRITE( *cmd++, 0x0a | rwflag );
  447. if (enable)
  448. ENABLE_IRQ();
  449. udelay(80);
  450. return( 1 );
  451. }
  452. /*
  453. * acsicmd_nodma() sends an ACSI command that requires no DMA.
  454. */
  455. int acsicmd_nodma( const char *cmd, int enable)
  456. { int i;
  457. acsi_delay_end(COMMAND_DELAY);
  458. DISABLE_IRQ();
  459. /* send first command byte */
  460. dma_wd.dma_mode_status = 0x88;
  461. MFPDELAY();
  462. DMA_LONG_WRITE( *cmd++, 0x8a );
  463. udelay(20);
  464. if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
  465. /* send the intermediate command bytes */
  466. for( i = 0; i < 4; ++i ) {
  467. DMA_LONG_WRITE( *cmd++, 0x8a );
  468. udelay(20);
  469. if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
  470. }
  471. /* send last command byte */
  472. DMA_LONG_WRITE( *cmd++, 0x0a );
  473. if (enable)
  474. ENABLE_IRQ();
  475. udelay(80);
  476. return( 1 );
  477. /* Note that the ACSI interrupt is still disabled after this
  478. * function. If you want to get the IRQ delivered, enable it manually!
  479. */
  480. }
  481. static int acsi_reqsense( char *buffer, int targ, int lun)
  482. {
  483. CMDSET_TARG_LUN( reqsense_cmd, targ, lun);
  484. if (!acsicmd_dma( reqsense_cmd, buffer, 1, 0, 0 )) return( 0 );
  485. if (!acsi_wait_for_IRQ( 10 )) return( 0 );
  486. acsi_getstatus();
  487. if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
  488. if (!acsi_wait_for_IRQ( 10 )) return( 0 );
  489. acsi_getstatus();
  490. if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
  491. if (!acsi_wait_for_IRQ( 10 )) return( 0 );
  492. acsi_getstatus();
  493. if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
  494. if (!acsi_wait_for_IRQ( 10 )) return( 0 );
  495. acsi_getstatus();
  496. dma_cache_maintenance( virt_to_phys(buffer), 16, 0 );
  497. return( 1 );
  498. }
  499. /*
  500. * ACSI status phase: get the status byte from the bus
  501. *
  502. * I've seen several times that a 0xff status is read, propably due to
  503. * a timing error. In this case, the procedure is repeated after the
  504. * next _IRQ edge.
  505. */
  506. int acsi_getstatus( void )
  507. { int status;
  508. DISABLE_IRQ();
  509. for(;;) {
  510. if (!acsi_wait_for_IRQ( 100 )) {
  511. acsi_delay_start();
  512. return( -1 );
  513. }
  514. dma_wd.dma_mode_status = 0x8a;
  515. MFPDELAY();
  516. status = dma_wd.fdc_acces_seccount;
  517. if (status != 0xff) break;
  518. #ifdef DEBUG
  519. printk("ACSI: skipping 0xff status byte\n" );
  520. #endif
  521. udelay(40);
  522. acsi_wait_for_noIRQ( 20 );
  523. }
  524. dma_wd.dma_mode_status = 0x80;
  525. udelay(40);
  526. acsi_wait_for_noIRQ( 20 );
  527. acsi_delay_start();
  528. return( status & 0x1f ); /* mask of the device# */
  529. }
  530. #if (defined(CONFIG_ATARI_SLM) || defined(CONFIG_ATARI_SLM_MODULE))
  531. /* Receive data in an extended status phase. Needed by SLM printer. */
  532. int acsi_extstatus( char *buffer, int cnt )
  533. { int status;
  534. DISABLE_IRQ();
  535. udelay(80);
  536. while( cnt-- > 0 ) {
  537. if (!acsi_wait_for_IRQ( 40 )) return( 0 );
  538. dma_wd.dma_mode_status = 0x8a;
  539. MFPDELAY();
  540. status = dma_wd.fdc_acces_seccount;
  541. MFPDELAY();
  542. *buffer++ = status & 0xff;
  543. udelay(40);
  544. }
  545. return( 1 );
  546. }
  547. /* Finish an extended status phase */
  548. void acsi_end_extstatus( void )
  549. {
  550. dma_wd.dma_mode_status = 0x80;
  551. udelay(40);
  552. acsi_wait_for_noIRQ( 20 );
  553. acsi_delay_start();
  554. }
  555. /* Send data in an extended command phase */
  556. int acsi_extcmd( unsigned char *buffer, int cnt )
  557. {
  558. while( cnt-- > 0 ) {
  559. DMA_LONG_WRITE( *buffer++, 0x8a );
  560. udelay(20);
  561. if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
  562. }
  563. return( 1 );
  564. }
  565. #endif
  566. static void acsi_print_error(const unsigned char *errblk, struct acsi_info_struct *aip)
  567. { int atari_err, i, errcode;
  568. struct acsi_error *arr;
  569. atari_err = aip->old_atari_disk;
  570. if (atari_err)
  571. errcode = errblk[0] & 0x7f;
  572. else
  573. if ((errblk[0] & 0x70) == 0x70)
  574. errcode = errblk[2] & 0x0f;
  575. else
  576. errcode = errblk[0] & 0x0f;
  577. printk( KERN_ERR "ACSI error 0x%02x", errcode );
  578. if (errblk[0] & 0x80)
  579. printk( " for sector %d",
  580. ((errblk[1] & 0x1f) << 16) |
  581. (errblk[2] << 8) | errblk[0] );
  582. arr = atari_err ? atari_acsi_errors : scsi_acsi_errors;
  583. i = atari_err ? sizeof(atari_acsi_errors)/sizeof(*atari_acsi_errors) :
  584. sizeof(scsi_acsi_errors)/sizeof(*scsi_acsi_errors);
  585. for( --i; i >= 0; --i )
  586. if (arr[i].code == errcode) break;
  587. if (i >= 0)
  588. printk( ": %s\n", arr[i].text );
  589. }
  590. /*******************************************************************
  591. *
  592. * ACSI interrupt routine
  593. * Test, if this is a ACSI interrupt and call the irq handler
  594. * Otherwise ignore this interrupt.
  595. *
  596. *******************************************************************/
  597. static irqreturn_t acsi_interrupt(int irq, void *data, struct pt_regs *fp )
  598. { void (*acsi_irq_handler)(void) = do_acsi;
  599. do_acsi = NULL;
  600. CLEAR_TIMER();
  601. if (!acsi_irq_handler)
  602. acsi_irq_handler = unexpected_acsi_interrupt;
  603. acsi_irq_handler();
  604. return IRQ_HANDLED;
  605. }
  606. /******************************************************************
  607. *
  608. * The Interrupt handlers
  609. *
  610. *******************************************************************/
  611. static void unexpected_acsi_interrupt( void )
  612. {
  613. printk( KERN_WARNING "Unexpected ACSI interrupt\n" );
  614. }
  615. /* This function is called in case of errors. Because we cannot reset
  616. * the ACSI bus or a single device, there is no other choice than
  617. * retrying several times :-(
  618. */
  619. static void bad_rw_intr( void )
  620. {
  621. if (!CURRENT)
  622. return;
  623. if (++CURRENT->errors >= MAX_ERRORS)
  624. end_request(CURRENT, 0);
  625. /* Otherwise just retry */
  626. }
  627. static void read_intr( void )
  628. { int status;
  629. status = acsi_getstatus();
  630. if (status != 0) {
  631. struct gendisk *disk = CURRENT->rq_disk;
  632. struct acsi_info_struct *aip = disk->private_data;
  633. printk(KERN_ERR "%s: ", disk->disk_name);
  634. if (!acsi_reqsense(acsi_buffer, aip->target, aip->lun))
  635. printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
  636. else {
  637. acsi_print_error(acsi_buffer, aip);
  638. if (CARTRCH_STAT(aip, acsi_buffer))
  639. aip->changed = 1;
  640. }
  641. ENABLE_IRQ();
  642. bad_rw_intr();
  643. redo_acsi_request();
  644. return;
  645. }
  646. dma_cache_maintenance( virt_to_phys(CurrentBuffer), CurrentNSect*512, 0 );
  647. if (CurrentBuffer == acsi_buffer)
  648. copy_from_acsibuffer();
  649. do_end_requests();
  650. redo_acsi_request();
  651. }
  652. static void write_intr(void)
  653. { int status;
  654. status = acsi_getstatus();
  655. if (status != 0) {
  656. struct gendisk *disk = CURRENT->rq_disk;
  657. struct acsi_info_struct *aip = disk->private_data;
  658. printk( KERN_ERR "%s: ", disk->disk_name);
  659. if (!acsi_reqsense( acsi_buffer, aip->target, aip->lun))
  660. printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
  661. else {
  662. acsi_print_error(acsi_buffer, aip);
  663. if (CARTRCH_STAT(aip, acsi_buffer))
  664. aip->changed = 1;
  665. }
  666. bad_rw_intr();
  667. redo_acsi_request();
  668. return;
  669. }
  670. do_end_requests();
  671. redo_acsi_request();
  672. }
  673. static void acsi_times_out( unsigned long dummy )
  674. {
  675. DISABLE_IRQ();
  676. if (!do_acsi) return;
  677. do_acsi = NULL;
  678. printk( KERN_ERR "ACSI timeout\n" );
  679. if (!CURRENT)
  680. return;
  681. if (++CURRENT->errors >= MAX_ERRORS) {
  682. #ifdef DEBUG
  683. printk( KERN_ERR "ACSI: too many errors.\n" );
  684. #endif
  685. end_request(CURRENT, 0);
  686. }
  687. redo_acsi_request();
  688. }
  689. /***********************************************************************
  690. *
  691. * Scatter-gather utility functions
  692. *
  693. ***********************************************************************/
  694. static void copy_to_acsibuffer( void )
  695. { int i;
  696. char *src, *dst;
  697. struct buffer_head *bh;
  698. src = CURRENT->buffer;
  699. dst = acsi_buffer;
  700. bh = CURRENT->bh;
  701. if (!bh)
  702. memcpy( dst, src, CurrentNSect*512 );
  703. else
  704. for( i = 0; i < CurrentNReq; ++i ) {
  705. memcpy( dst, src, bh->b_size );
  706. dst += bh->b_size;
  707. if ((bh = bh->b_reqnext))
  708. src = bh->b_data;
  709. }
  710. }
  711. static void copy_from_acsibuffer( void )
  712. { int i;
  713. char *src, *dst;
  714. struct buffer_head *bh;
  715. dst = CURRENT->buffer;
  716. src = acsi_buffer;
  717. bh = CURRENT->bh;
  718. if (!bh)
  719. memcpy( dst, src, CurrentNSect*512 );
  720. else
  721. for( i = 0; i < CurrentNReq; ++i ) {
  722. memcpy( dst, src, bh->b_size );
  723. src += bh->b_size;
  724. if ((bh = bh->b_reqnext))
  725. dst = bh->b_data;
  726. }
  727. }
  728. static void do_end_requests( void )
  729. { int i, n;
  730. if (!CURRENT->bh) {
  731. CURRENT->nr_sectors -= CurrentNSect;
  732. CURRENT->current_nr_sectors -= CurrentNSect;
  733. CURRENT->sector += CurrentNSect;
  734. if (CURRENT->nr_sectors == 0)
  735. end_request(CURRENT, 1);
  736. }
  737. else {
  738. for( i = 0; i < CurrentNReq; ++i ) {
  739. n = CURRENT->bh->b_size >> 9;
  740. CURRENT->nr_sectors -= n;
  741. CURRENT->current_nr_sectors -= n;
  742. CURRENT->sector += n;
  743. end_request(CURRENT, 1);
  744. }
  745. }
  746. }
  747. /***********************************************************************
  748. *
  749. * do_acsi_request and friends
  750. *
  751. ***********************************************************************/
  752. static void do_acsi_request( request_queue_t * q )
  753. {
  754. stdma_lock( acsi_interrupt, NULL );
  755. redo_acsi_request();
  756. }
  757. static void redo_acsi_request( void )
  758. {
  759. unsigned block, target, lun, nsect;
  760. char *buffer;
  761. unsigned long pbuffer;
  762. struct buffer_head *bh;
  763. struct gendisk *disk;
  764. struct acsi_info_struct *aip;
  765. repeat:
  766. CLEAR_TIMER();
  767. if (do_acsi)
  768. return;
  769. if (!CURRENT) {
  770. do_acsi = NULL;
  771. ENABLE_IRQ();
  772. stdma_release();
  773. return;
  774. }
  775. disk = CURRENT->rq_disk;
  776. aip = disk->private_data;
  777. if (CURRENT->bh) {
  778. if (!CURRENT->bh && !buffer_locked(CURRENT->bh))
  779. panic("ACSI: block not locked");
  780. }
  781. block = CURRENT->sector;
  782. if (block+CURRENT->nr_sectors >= get_capacity(disk)) {
  783. #ifdef DEBUG
  784. printk( "%s: attempted access for blocks %d...%ld past end of device at block %ld.\n",
  785. disk->disk_name,
  786. block, block + CURRENT->nr_sectors - 1,
  787. get_capacity(disk));
  788. #endif
  789. end_request(CURRENT, 0);
  790. goto repeat;
  791. }
  792. if (aip->changed) {
  793. printk( KERN_NOTICE "%s: request denied because cartridge has "
  794. "been changed.\n", disk->disk_name);
  795. end_request(CURRENT, 0);
  796. goto repeat;
  797. }
  798. target = aip->target;
  799. lun = aip->lun;
  800. /* Find out how many sectors should be transferred from/to
  801. * consecutive buffers and thus can be done with a single command.
  802. */
  803. buffer = CURRENT->buffer;
  804. pbuffer = virt_to_phys(buffer);
  805. nsect = CURRENT->current_nr_sectors;
  806. CurrentNReq = 1;
  807. if ((bh = CURRENT->bh) && bh != CURRENT->bhtail) {
  808. if (!STRAM_ADDR(pbuffer)) {
  809. /* If transfer is done via the ACSI buffer anyway, we can
  810. * assemble as much bh's as fit in the buffer.
  811. */
  812. while( (bh = bh->b_reqnext) ) {
  813. if (nsect + (bh->b_size>>9) > ACSI_BUFFER_SECTORS) break;
  814. nsect += bh->b_size >> 9;
  815. ++CurrentNReq;
  816. if (bh == CURRENT->bhtail) break;
  817. }
  818. buffer = acsi_buffer;
  819. pbuffer = phys_acsi_buffer;
  820. }
  821. else {
  822. unsigned long pendadr, pnewadr;
  823. pendadr = pbuffer + nsect*512;
  824. while( (bh = bh->b_reqnext) ) {
  825. pnewadr = virt_to_phys(bh->b_data);
  826. if (!STRAM_ADDR(pnewadr) || pendadr != pnewadr) break;
  827. nsect += bh->b_size >> 9;
  828. pendadr = pnewadr + bh->b_size;
  829. ++CurrentNReq;
  830. if (bh == CURRENT->bhtail) break;
  831. }
  832. }
  833. }
  834. else {
  835. if (!STRAM_ADDR(pbuffer)) {
  836. buffer = acsi_buffer;
  837. pbuffer = phys_acsi_buffer;
  838. if (nsect > ACSI_BUFFER_SECTORS)
  839. nsect = ACSI_BUFFER_SECTORS;
  840. }
  841. }
  842. CurrentBuffer = buffer;
  843. CurrentNSect = nsect;
  844. if (rq_data_dir(CURRENT) == WRITE) {
  845. CMDSET_TARG_LUN( write_cmd, target, lun );
  846. CMDSET_BLOCK( write_cmd, block );
  847. CMDSET_LEN( write_cmd, nsect );
  848. if (buffer == acsi_buffer)
  849. copy_to_acsibuffer();
  850. dma_cache_maintenance( pbuffer, nsect*512, 1 );
  851. do_acsi = write_intr;
  852. if (!acsicmd_dma( write_cmd, buffer, nsect, 1, 1)) {
  853. do_acsi = NULL;
  854. printk( KERN_ERR "ACSI (write): Timeout in command block\n" );
  855. bad_rw_intr();
  856. goto repeat;
  857. }
  858. SET_TIMER();
  859. return;
  860. }
  861. if (rq_data_dir(CURRENT) == READ) {
  862. CMDSET_TARG_LUN( read_cmd, target, lun );
  863. CMDSET_BLOCK( read_cmd, block );
  864. CMDSET_LEN( read_cmd, nsect );
  865. do_acsi = read_intr;
  866. if (!acsicmd_dma( read_cmd, buffer, nsect, 0, 1)) {
  867. do_acsi = NULL;
  868. printk( KERN_ERR "ACSI (read): Timeout in command block\n" );
  869. bad_rw_intr();
  870. goto repeat;
  871. }
  872. SET_TIMER();
  873. return;
  874. }
  875. panic("unknown ACSI command");
  876. }
  877. /***********************************************************************
  878. *
  879. * Misc functions: ioctl, open, release, check_change, ...
  880. *
  881. ***********************************************************************/
  882. static int acsi_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  883. {
  884. struct acsi_info_struct *aip = bdev->bd_disk->private_data;
  885. /*
  886. * Just fake some geometry here, it's nonsense anyway
  887. * To make it easy, use Adaptec's usual 64/32 mapping
  888. */
  889. geo->heads = 64;
  890. geo->sectors = 32;
  891. geo->cylinders = aip->size >> 11;
  892. return 0;
  893. }
  894. static int acsi_ioctl( struct inode *inode, struct file *file,
  895. unsigned int cmd, unsigned long arg )
  896. {
  897. struct gendisk *disk = inode->i_bdev->bd_disk;
  898. struct acsi_info_struct *aip = disk->private_data;
  899. switch (cmd) {
  900. case SCSI_IOCTL_GET_IDLUN:
  901. /* SCSI compatible GET_IDLUN call to get target's ID and LUN number */
  902. put_user( aip->target | (aip->lun << 8),
  903. &((Scsi_Idlun *) arg)->dev_id );
  904. put_user( 0, &((Scsi_Idlun *) arg)->host_unique_id );
  905. return 0;
  906. default:
  907. return -EINVAL;
  908. }
  909. }
  910. /*
  911. * Open a device, check for read-only and lock the medium if it is
  912. * removable.
  913. *
  914. * Changes by Martin Rogge, 9th Aug 1995:
  915. * Check whether check_disk_change (and therefore revalidate_acsidisk)
  916. * was successful. They fail when there is no medium in the drive.
  917. *
  918. * The problem of media being changed during an operation can be
  919. * ignored because of the prevent_removal code.
  920. *
  921. * Added check for the validity of the device number.
  922. *
  923. */
  924. static int acsi_open( struct inode * inode, struct file * filp )
  925. {
  926. struct gendisk *disk = inode->i_bdev->bd_disk;
  927. struct acsi_info_struct *aip = disk->private_data;
  928. if (aip->access_count == 0 && aip->removable) {
  929. #if 0
  930. aip->changed = 1; /* safety first */
  931. #endif
  932. check_disk_change( inode->i_bdev );
  933. if (aip->changed) /* revalidate was not successful (no medium) */
  934. return -ENXIO;
  935. acsi_prevent_removal(aip, 1);
  936. }
  937. aip->access_count++;
  938. if (filp && filp->f_mode) {
  939. check_disk_change( inode->i_bdev );
  940. if (filp->f_mode & 2) {
  941. if (aip->read_only) {
  942. acsi_release( inode, filp );
  943. return -EROFS;
  944. }
  945. }
  946. }
  947. return 0;
  948. }
  949. /*
  950. * Releasing a block device means we sync() it, so that it can safely
  951. * be forgotten about...
  952. */
  953. static int acsi_release( struct inode * inode, struct file * file )
  954. {
  955. struct gendisk *disk = inode->i_bdev->bd_disk;
  956. struct acsi_info_struct *aip = disk->private_data;
  957. if (--aip->access_count == 0 && aip->removable)
  958. acsi_prevent_removal(aip, 0);
  959. return( 0 );
  960. }
  961. /*
  962. * Prevent or allow a media change for removable devices.
  963. */
  964. static void acsi_prevent_removal(struct acsi_info_struct *aip, int flag)
  965. {
  966. stdma_lock( NULL, NULL );
  967. CMDSET_TARG_LUN(pa_med_rem_cmd, aip->target, aip->lun);
  968. CMDSET_LEN( pa_med_rem_cmd, flag );
  969. if (acsicmd_nodma(pa_med_rem_cmd, 0) && acsi_wait_for_IRQ(3*HZ))
  970. acsi_getstatus();
  971. /* Do not report errors -- some devices may not know this command. */
  972. ENABLE_IRQ();
  973. stdma_release();
  974. }
  975. static int acsi_media_change(struct gendisk *disk)
  976. {
  977. struct acsi_info_struct *aip = disk->private_data;
  978. if (!aip->removable)
  979. return 0;
  980. if (aip->changed)
  981. /* We can be sure that the medium has been changed -- REQUEST
  982. * SENSE has reported this earlier.
  983. */
  984. return 1;
  985. /* If the flag isn't set, make a test by reading block 0.
  986. * If errors happen, it seems to be better to say "changed"...
  987. */
  988. stdma_lock( NULL, NULL );
  989. CMDSET_TARG_LUN(read_cmd, aip->target, aip->lun);
  990. CMDSET_BLOCK( read_cmd, 0 );
  991. CMDSET_LEN( read_cmd, 1 );
  992. if (acsicmd_dma(read_cmd, acsi_buffer, 1, 0, 0) &&
  993. acsi_wait_for_IRQ(3*HZ)) {
  994. if (acsi_getstatus()) {
  995. if (acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
  996. if (CARTRCH_STAT(aip, acsi_buffer))
  997. aip->changed = 1;
  998. }
  999. else {
  1000. printk( KERN_ERR "%s: REQUEST SENSE failed in test for "
  1001. "medium change; assuming a change\n", disk->disk_name );
  1002. aip->changed = 1;
  1003. }
  1004. }
  1005. }
  1006. else {
  1007. printk( KERN_ERR "%s: Test for medium changed timed out; "
  1008. "assuming a change\n", disk->disk_name);
  1009. aip->changed = 1;
  1010. }
  1011. ENABLE_IRQ();
  1012. stdma_release();
  1013. /* Now, after reading a block, the changed status is surely valid. */
  1014. return aip->changed;
  1015. }
  1016. static int acsi_change_blk_size( int target, int lun)
  1017. { int i;
  1018. for (i=0; i<12; i++)
  1019. acsi_buffer[i] = 0;
  1020. acsi_buffer[3] = 8;
  1021. acsi_buffer[10] = 2;
  1022. CMDSET_TARG_LUN( modeselect_cmd, target, lun);
  1023. if (!acsicmd_dma( modeselect_cmd, acsi_buffer, 1,1,0) ||
  1024. !acsi_wait_for_IRQ( 3*HZ ) ||
  1025. acsi_getstatus() != 0 ) {
  1026. return(0);
  1027. }
  1028. return(1);
  1029. }
  1030. static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd )
  1031. {
  1032. int page;
  1033. CMDSET_TARG_LUN( modesense_cmd, target, lun );
  1034. for (page=0; page<4; page++) {
  1035. modesense_cmd[2] = page;
  1036. if (!acsicmd_dma( modesense_cmd, acsi_buffer, 1, 0, 0 ) ||
  1037. !acsi_wait_for_IRQ( 3*HZ ) ||
  1038. acsi_getstatus())
  1039. continue;
  1040. /* read twice to jump over the second 16-byte border! */
  1041. udelay(300);
  1042. if (acsi_wait_for_noIRQ( 20 ) &&
  1043. acsicmd_nodma( modesense_cmd, 0 ) &&
  1044. acsi_wait_for_IRQ( 3*HZ ) &&
  1045. acsi_getstatus() == 0)
  1046. break;
  1047. }
  1048. if (page == 4) {
  1049. return(0);
  1050. }
  1051. dma_cache_maintenance( phys_acsi_buffer, sizeof(SENSE_DATA), 0 );
  1052. *sd = *(SENSE_DATA *)acsi_buffer;
  1053. /* Validity check, depending on type of data */
  1054. switch( SENSE_TYPE(*sd) ) {
  1055. case SENSE_TYPE_ATARI:
  1056. if (CAPACITY(*sd) == 0)
  1057. goto invalid_sense;
  1058. break;
  1059. case SENSE_TYPE_SCSI:
  1060. if (sd->scsi.descriptor_size != 8)
  1061. goto invalid_sense;
  1062. break;
  1063. case SENSE_TYPE_UNKNOWN:
  1064. printk( KERN_ERR "ACSI target %d, lun %d: Cannot interpret "
  1065. "sense data\n", target, lun );
  1066. invalid_sense:
  1067. #ifdef DEBUG
  1068. { int i;
  1069. printk( "Mode sense data for ACSI target %d, lun %d seem not valid:",
  1070. target, lun );
  1071. for( i = 0; i < sizeof(SENSE_DATA); ++i )
  1072. printk( "%02x ", (unsigned char)acsi_buffer[i] );
  1073. printk( "\n" );
  1074. }
  1075. #endif
  1076. return( 0 );
  1077. }
  1078. return( 1 );
  1079. }
  1080. /*******************************************************************
  1081. *
  1082. * Initialization
  1083. *
  1084. ********************************************************************/
  1085. extern struct block_device_operations acsi_fops;
  1086. static struct gendisk *acsi_gendisk[MAX_DEV];
  1087. #define MAX_SCSI_DEVICE_CODE 10
  1088. static const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
  1089. {
  1090. "Direct-Access ",
  1091. "Sequential-Access",
  1092. "Printer ",
  1093. "Processor ",
  1094. "WORM ",
  1095. "CD-ROM ",
  1096. "Scanner ",
  1097. "Optical Device ",
  1098. "Medium Changer ",
  1099. "Communications "
  1100. };
  1101. static void print_inquiry(unsigned char *data)
  1102. {
  1103. int i;
  1104. printk(KERN_INFO " Vendor: ");
  1105. for (i = 8; i < 16; i++)
  1106. {
  1107. if (data[i] >= 0x20 && i < data[4] + 5)
  1108. printk("%c", data[i]);
  1109. else
  1110. printk(" ");
  1111. }
  1112. printk(" Model: ");
  1113. for (i = 16; i < 32; i++)
  1114. {
  1115. if (data[i] >= 0x20 && i < data[4] + 5)
  1116. printk("%c", data[i]);
  1117. else
  1118. printk(" ");
  1119. }
  1120. printk(" Rev: ");
  1121. for (i = 32; i < 36; i++)
  1122. {
  1123. if (data[i] >= 0x20 && i < data[4] + 5)
  1124. printk("%c", data[i]);
  1125. else
  1126. printk(" ");
  1127. }
  1128. printk("\n");
  1129. i = data[0] & 0x1f;
  1130. printk(KERN_INFO " Type: %s ", (i < MAX_SCSI_DEVICE_CODE
  1131. ? scsi_device_types[i]
  1132. : "Unknown "));
  1133. printk(" ANSI SCSI revision: %02x", data[2] & 0x07);
  1134. if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
  1135. printk(" CCS\n");
  1136. else
  1137. printk("\n");
  1138. }
  1139. /*
  1140. * Changes by Martin Rogge, 9th Aug 1995:
  1141. * acsi_devinit has been taken out of acsi_geninit, because it needs
  1142. * to be called from revalidate_acsidisk. The result of request sense
  1143. * is now checked for DRIVE NOT READY.
  1144. *
  1145. * The structure *aip is only valid when acsi_devinit returns
  1146. * DEV_SUPPORTED.
  1147. *
  1148. */
  1149. #define DEV_NONE 0
  1150. #define DEV_UNKNOWN 1
  1151. #define DEV_SUPPORTED 2
  1152. #define DEV_SLM 3
  1153. static int acsi_devinit(struct acsi_info_struct *aip)
  1154. {
  1155. int status, got_inquiry;
  1156. SENSE_DATA sense;
  1157. unsigned char reqsense, extsense;
  1158. /*****************************************************************/
  1159. /* Do a TEST UNIT READY command to test the presence of a device */
  1160. /*****************************************************************/
  1161. CMDSET_TARG_LUN(tur_cmd, aip->target, aip->lun);
  1162. if (!acsicmd_nodma(tur_cmd, 0)) {
  1163. /* timed out -> no device here */
  1164. #ifdef DEBUG_DETECT
  1165. printk("target %d lun %d: timeout\n", aip->target, aip->lun);
  1166. #endif
  1167. return DEV_NONE;
  1168. }
  1169. /*************************/
  1170. /* Read the ACSI status. */
  1171. /*************************/
  1172. status = acsi_getstatus();
  1173. if (status) {
  1174. if (status == 0x12) {
  1175. /* The SLM printer should be the only device that
  1176. * responds with the error code in the status byte. In
  1177. * correct status bytes, bit 4 is never set.
  1178. */
  1179. printk( KERN_INFO "Detected SLM printer at id %d lun %d\n",
  1180. aip->target, aip->lun);
  1181. return DEV_SLM;
  1182. }
  1183. /* ignore CHECK CONDITION, since some devices send a
  1184. UNIT ATTENTION */
  1185. if ((status & 0x1e) != 0x2) {
  1186. #ifdef DEBUG_DETECT
  1187. printk("target %d lun %d: status %d\n",
  1188. aip->target, aip->lun, status);
  1189. #endif
  1190. return DEV_UNKNOWN;
  1191. }
  1192. }
  1193. /*******************************/
  1194. /* Do a REQUEST SENSE command. */
  1195. /*******************************/
  1196. if (!acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
  1197. printk( KERN_WARNING "acsi_reqsense failed\n");
  1198. acsi_buffer[0] = 0;
  1199. acsi_buffer[2] = UNIT_ATTENTION;
  1200. }
  1201. reqsense = acsi_buffer[0];
  1202. extsense = acsi_buffer[2] & 0xf;
  1203. if (status) {
  1204. if ((reqsense & 0x70) == 0x70) { /* extended sense */
  1205. if (extsense != UNIT_ATTENTION &&
  1206. extsense != NOT_READY) {
  1207. #ifdef DEBUG_DETECT
  1208. printk("target %d lun %d: extended sense %d\n",
  1209. aip->target, aip->lun, extsense);
  1210. #endif
  1211. return DEV_UNKNOWN;
  1212. }
  1213. }
  1214. else {
  1215. if (reqsense & 0x7f) {
  1216. #ifdef DEBUG_DETECT
  1217. printk("target %d lun %d: sense %d\n",
  1218. aip->target, aip->lun, reqsense);
  1219. #endif
  1220. return DEV_UNKNOWN;
  1221. }
  1222. }
  1223. }
  1224. else
  1225. if (reqsense == 0x4) { /* SH204 Bug workaround */
  1226. #ifdef DEBUG_DETECT
  1227. printk("target %d lun %d status=0 sense=4\n",
  1228. aip->target, aip->lun);
  1229. #endif
  1230. return DEV_UNKNOWN;
  1231. }
  1232. /***********************************************************/
  1233. /* Do an INQUIRY command to get more infos on this device. */
  1234. /***********************************************************/
  1235. /* Assume default values */
  1236. aip->removable = 1;
  1237. aip->read_only = 0;
  1238. aip->old_atari_disk = 0;
  1239. aip->changed = (extsense == NOT_READY); /* medium inserted? */
  1240. aip->size = DEFAULT_SIZE;
  1241. got_inquiry = 0;
  1242. /* Fake inquiry result for old atari disks */
  1243. memcpy(acsi_buffer, "\000\000\001\000 Adaptec 40xx"
  1244. " ", 40);
  1245. CMDSET_TARG_LUN(inquiry_cmd, aip->target, aip->lun);
  1246. if (acsicmd_dma(inquiry_cmd, acsi_buffer, 1, 0, 0) &&
  1247. acsi_getstatus() == 0) {
  1248. acsicmd_nodma(inquiry_cmd, 0);
  1249. acsi_getstatus();
  1250. dma_cache_maintenance( phys_acsi_buffer, 256, 0 );
  1251. got_inquiry = 1;
  1252. aip->removable = !!(acsi_buffer[1] & 0x80);
  1253. }
  1254. if (aip->type == NONE) /* only at boot time */
  1255. print_inquiry(acsi_buffer);
  1256. switch(acsi_buffer[0]) {
  1257. case TYPE_DISK:
  1258. aip->type = HARDDISK;
  1259. break;
  1260. case TYPE_ROM:
  1261. aip->type = CDROM;
  1262. aip->read_only = 1;
  1263. break;
  1264. default:
  1265. return DEV_UNKNOWN;
  1266. }
  1267. /****************************/
  1268. /* Do a MODE SENSE command. */
  1269. /****************************/
  1270. if (!acsi_mode_sense(aip->target, aip->lun, &sense)) {
  1271. printk( KERN_WARNING "No mode sense data.\n" );
  1272. return DEV_UNKNOWN;
  1273. }
  1274. if ((SECTOR_SIZE(sense) != 512) &&
  1275. ((aip->type != CDROM) ||
  1276. !acsi_change_blk_size(aip->target, aip->lun) ||
  1277. !acsi_mode_sense(aip->target, aip->lun, &sense) ||
  1278. (SECTOR_SIZE(sense) != 512))) {
  1279. printk( KERN_WARNING "Sector size != 512 not supported.\n" );
  1280. return DEV_UNKNOWN;
  1281. }
  1282. /* There are disks out there that claim to have 0 sectors... */
  1283. if (CAPACITY(sense))
  1284. aip->size = CAPACITY(sense); /* else keep DEFAULT_SIZE */
  1285. if (!got_inquiry && SENSE_TYPE(sense) == SENSE_TYPE_ATARI) {
  1286. /* If INQUIRY failed and the sense data suggest an old
  1287. * Atari disk (SH20x, Megafile), the disk is not removable
  1288. */
  1289. aip->removable = 0;
  1290. aip->old_atari_disk = 1;
  1291. }
  1292. /******************/
  1293. /* We've done it. */
  1294. /******************/
  1295. return DEV_SUPPORTED;
  1296. }
  1297. EXPORT_SYMBOL(acsi_delay_start);
  1298. EXPORT_SYMBOL(acsi_delay_end);
  1299. EXPORT_SYMBOL(acsi_wait_for_IRQ);
  1300. EXPORT_SYMBOL(acsi_wait_for_noIRQ);
  1301. EXPORT_SYMBOL(acsicmd_nodma);
  1302. EXPORT_SYMBOL(acsi_getstatus);
  1303. EXPORT_SYMBOL(acsi_buffer);
  1304. EXPORT_SYMBOL(phys_acsi_buffer);
  1305. #ifdef CONFIG_ATARI_SLM_MODULE
  1306. void acsi_attach_SLMs( int (*attach_func)( int, int ) );
  1307. EXPORT_SYMBOL(acsi_extstatus);
  1308. EXPORT_SYMBOL(acsi_end_extstatus);
  1309. EXPORT_SYMBOL(acsi_extcmd);
  1310. EXPORT_SYMBOL(acsi_attach_SLMs);
  1311. /* to remember IDs of SLM devices, SLM module is loaded later
  1312. * (index is target#, contents is lun#, -1 means "no SLM") */
  1313. int SLM_devices[8];
  1314. #endif
  1315. static struct block_device_operations acsi_fops = {
  1316. .owner = THIS_MODULE,
  1317. .open = acsi_open,
  1318. .release = acsi_release,
  1319. .ioctl = acsi_ioctl,
  1320. .getgeo = acsi_getgeo,
  1321. .media_changed = acsi_media_change,
  1322. .revalidate_disk= acsi_revalidate,
  1323. };
  1324. #ifdef CONFIG_ATARI_SLM_MODULE
  1325. /* call attach_slm() for each device that is a printer; needed for init of SLM
  1326. * driver as a module, since it's not yet present if acsi.c is inited and thus
  1327. * the bus gets scanned. */
  1328. void acsi_attach_SLMs( int (*attach_func)( int, int ) )
  1329. {
  1330. int i, n = 0;
  1331. for( i = 0; i < 8; ++i )
  1332. if (SLM_devices[i] >= 0)
  1333. n += (*attach_func)( i, SLM_devices[i] );
  1334. printk( KERN_INFO "Found %d SLM printer(s) total.\n", n );
  1335. }
  1336. #endif /* CONFIG_ATARI_SLM_MODULE */
  1337. int acsi_init( void )
  1338. {
  1339. int err = 0;
  1340. int i, target, lun;
  1341. struct acsi_info_struct *aip;
  1342. #ifdef CONFIG_ATARI_SLM
  1343. int n_slm = 0;
  1344. #endif
  1345. if (!MACH_IS_ATARI || !ATARIHW_PRESENT(ACSI))
  1346. return 0;
  1347. if (register_blkdev(ACSI_MAJOR, "ad")) {
  1348. err = -EBUSY;
  1349. goto out1;
  1350. }
  1351. if (!(acsi_buffer =
  1352. (char *)atari_stram_alloc(ACSI_BUFFER_SIZE, "acsi"))) {
  1353. err = -ENOMEM;
  1354. printk( KERN_ERR "Unable to get ACSI ST-Ram buffer.\n" );
  1355. goto out2;
  1356. }
  1357. phys_acsi_buffer = virt_to_phys( acsi_buffer );
  1358. STramMask = ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000 : 0xff000000;
  1359. acsi_queue = blk_init_queue(do_acsi_request, &acsi_lock);
  1360. if (!acsi_queue) {
  1361. err = -ENOMEM;
  1362. goto out2a;
  1363. }
  1364. #ifdef CONFIG_ATARI_SLM
  1365. err = slm_init();
  1366. #endif
  1367. if (err)
  1368. goto out3;
  1369. printk( KERN_INFO "Probing ACSI devices:\n" );
  1370. NDevices = 0;
  1371. #ifdef CONFIG_ATARI_SLM_MODULE
  1372. for( i = 0; i < 8; ++i )
  1373. SLM_devices[i] = -1;
  1374. #endif
  1375. stdma_lock(NULL, NULL);
  1376. for (target = 0; target < 8 && NDevices < MAX_DEV; ++target) {
  1377. lun = 0;
  1378. do {
  1379. aip = &acsi_info[NDevices];
  1380. aip->type = NONE;
  1381. aip->target = target;
  1382. aip->lun = lun;
  1383. i = acsi_devinit(aip);
  1384. switch (i) {
  1385. case DEV_SUPPORTED:
  1386. printk( KERN_INFO "Detected ");
  1387. switch (aip->type) {
  1388. case HARDDISK:
  1389. printk("disk");
  1390. break;
  1391. case CDROM:
  1392. printk("cdrom");
  1393. break;
  1394. default:
  1395. }
  1396. printk(" ad%c at id %d lun %d ",
  1397. 'a' + NDevices, target, lun);
  1398. if (aip->removable)
  1399. printk("(removable) ");
  1400. if (aip->read_only)
  1401. printk("(read-only) ");
  1402. if (aip->size == DEFAULT_SIZE)
  1403. printk(" unkown size, using default ");
  1404. printk("%ld MByte\n",
  1405. (aip->size*512+1024*1024/2)/(1024*1024));
  1406. NDevices++;
  1407. break;
  1408. case DEV_SLM:
  1409. #ifdef CONFIG_ATARI_SLM
  1410. n_slm += attach_slm( target, lun );
  1411. break;
  1412. #endif
  1413. #ifdef CONFIG_ATARI_SLM_MODULE
  1414. SLM_devices[target] = lun;
  1415. break;
  1416. #endif
  1417. /* neither of the above: fall through to unknown device */
  1418. case DEV_UNKNOWN:
  1419. printk( KERN_INFO "Detected unsupported device at "
  1420. "id %d lun %d\n", target, lun);
  1421. break;
  1422. }
  1423. }
  1424. #ifdef CONFIG_ACSI_MULTI_LUN
  1425. while (i != DEV_NONE && ++lun < MAX_LUN);
  1426. #else
  1427. while (0);
  1428. #endif
  1429. }
  1430. /* reenable interrupt */
  1431. ENABLE_IRQ();
  1432. stdma_release();
  1433. #ifndef CONFIG_ATARI_SLM
  1434. printk( KERN_INFO "Found %d ACSI device(s) total.\n", NDevices );
  1435. #else
  1436. printk( KERN_INFO "Found %d ACSI device(s) and %d SLM printer(s) total.\n",
  1437. NDevices, n_slm );
  1438. #endif
  1439. err = -ENOMEM;
  1440. for( i = 0; i < NDevices; ++i ) {
  1441. acsi_gendisk[i] = alloc_disk(16);
  1442. if (!acsi_gendisk[i])
  1443. goto out4;
  1444. }
  1445. for( i = 0; i < NDevices; ++i ) {
  1446. struct gendisk *disk = acsi_gendisk[i];
  1447. sprintf(disk->disk_name, "ad%c", 'a'+i);
  1448. aip = &acsi_info[NDevices];
  1449. sprintf(disk->devfs_name, "ad/target%d/lun%d", aip->target, aip->lun);
  1450. disk->major = ACSI_MAJOR;
  1451. disk->first_minor = i << 4;
  1452. if (acsi_info[i].type != HARDDISK) {
  1453. disk->minors = 1;
  1454. strcat(disk->devfs_name, "/disc");
  1455. }
  1456. disk->fops = &acsi_fops;
  1457. disk->private_data = &acsi_info[i];
  1458. set_capacity(disk, acsi_info[i].size);
  1459. disk->queue = acsi_queue;
  1460. add_disk(disk);
  1461. }
  1462. return 0;
  1463. out4:
  1464. while (i--)
  1465. put_disk(acsi_gendisk[i]);
  1466. out3:
  1467. blk_cleanup_queue(acsi_queue);
  1468. out2a:
  1469. atari_stram_free( acsi_buffer );
  1470. out2:
  1471. unregister_blkdev( ACSI_MAJOR, "ad" );
  1472. out1:
  1473. return err;
  1474. }
  1475. #ifdef MODULE
  1476. MODULE_LICENSE("GPL");
  1477. int init_module(void)
  1478. {
  1479. int err;
  1480. if ((err = acsi_init()))
  1481. return( err );
  1482. printk( KERN_INFO "ACSI driver loaded as module.\n");
  1483. return( 0 );
  1484. }
  1485. void cleanup_module(void)
  1486. {
  1487. int i;
  1488. del_timer( &acsi_timer );
  1489. blk_cleanup_queue(acsi_queue);
  1490. atari_stram_free( acsi_buffer );
  1491. if (unregister_blkdev( ACSI_MAJOR, "ad" ) != 0)
  1492. printk( KERN_ERR "acsi: cleanup_module failed\n");
  1493. for (i = 0; i < NDevices; i++) {
  1494. del_gendisk(acsi_gendisk[i]);
  1495. put_disk(acsi_gendisk[i]);
  1496. }
  1497. }
  1498. #endif
  1499. /*
  1500. * This routine is called to flush all partitions and partition tables
  1501. * for a changed scsi disk, and then re-read the new partition table.
  1502. * If we are revalidating a disk because of a media change, then we
  1503. * enter with usage == 0. If we are using an ioctl, we automatically have
  1504. * usage == 1 (we need an open channel to use an ioctl :-), so this
  1505. * is our limit.
  1506. *
  1507. * Changes by Martin Rogge, 9th Aug 1995:
  1508. * got cd-roms to work by calling acsi_devinit. There are only two problems:
  1509. * First, if there is no medium inserted, the status will remain "changed".
  1510. * That is no problem at all, but our design of three-valued logic (medium
  1511. * changed, medium not changed, no medium inserted).
  1512. * Secondly the check could fail completely and the drive could deliver
  1513. * nonsensical data, which could mess up the acsi_info[] structure. In
  1514. * that case we try to make the entry safe.
  1515. *
  1516. */
  1517. static int acsi_revalidate(struct gendisk *disk)
  1518. {
  1519. struct acsi_info_struct *aip = disk->private_data;
  1520. stdma_lock( NULL, NULL );
  1521. if (acsi_devinit(aip) != DEV_SUPPORTED) {
  1522. printk( KERN_ERR "ACSI: revalidate failed for target %d lun %d\n",
  1523. aip->target, aip->lun);
  1524. aip->size = 0;
  1525. aip->read_only = 1;
  1526. aip->removable = 1;
  1527. aip->changed = 1; /* next acsi_open will try again... */
  1528. }
  1529. ENABLE_IRQ();
  1530. stdma_release();
  1531. set_capacity(disk, aip->size);
  1532. return 0;
  1533. }