acsi.c 45 KB

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