acsi.c 45 KB

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