pd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /*
  2. pd.c (c) 1997-8 Grant R. Guenther <grant@torque.net>
  3. Under the terms of the GNU General Public License.
  4. This is the high-level driver for parallel port IDE hard
  5. drives based on chips supported by the paride module.
  6. By default, the driver will autoprobe for a single parallel
  7. port IDE drive, but if their individual parameters are
  8. specified, the driver can handle up to 4 drives.
  9. The behaviour of the pd driver can be altered by setting
  10. some parameters from the insmod command line. The following
  11. parameters are adjustable:
  12. drive0 These four arguments can be arrays of
  13. drive1 1-8 integers as follows:
  14. drive2
  15. drive3 <prt>,<pro>,<uni>,<mod>,<geo>,<sby>,<dly>,<slv>
  16. Where,
  17. <prt> is the base of the parallel port address for
  18. the corresponding drive. (required)
  19. <pro> is the protocol number for the adapter that
  20. supports this drive. These numbers are
  21. logged by 'paride' when the protocol modules
  22. are initialised. (0 if not given)
  23. <uni> for those adapters that support chained
  24. devices, this is the unit selector for the
  25. chain of devices on the given port. It should
  26. be zero for devices that don't support chaining.
  27. (0 if not given)
  28. <mod> this can be -1 to choose the best mode, or one
  29. of the mode numbers supported by the adapter.
  30. (-1 if not given)
  31. <geo> this defaults to 0 to indicate that the driver
  32. should use the CHS geometry provided by the drive
  33. itself. If set to 1, the driver will provide
  34. a logical geometry with 64 heads and 32 sectors
  35. per track, to be consistent with most SCSI
  36. drivers. (0 if not given)
  37. <sby> set this to zero to disable the power saving
  38. standby mode, if needed. (1 if not given)
  39. <dly> some parallel ports require the driver to
  40. go more slowly. -1 sets a default value that
  41. should work with the chosen protocol. Otherwise,
  42. set this to a small integer, the larger it is
  43. the slower the port i/o. In some cases, setting
  44. this to zero will speed up the device. (default -1)
  45. <slv> IDE disks can be jumpered to master or slave.
  46. Set this to 0 to choose the master drive, 1 to
  47. choose the slave, -1 (the default) to choose the
  48. first drive found.
  49. major You may use this parameter to overide the
  50. default major number (45) that this driver
  51. will use. Be sure to change the device
  52. name as well.
  53. name This parameter is a character string that
  54. contains the name the kernel will use for this
  55. device (in /proc output, for instance).
  56. (default "pd")
  57. cluster The driver will attempt to aggregate requests
  58. for adjacent blocks into larger multi-block
  59. clusters. The maximum cluster size (in 512
  60. byte sectors) is set with this parameter.
  61. (default 64)
  62. verbose This parameter controls the amount of logging
  63. that the driver will do. Set it to 0 for
  64. normal operation, 1 to see autoprobe progress
  65. messages, or 2 to see additional debugging
  66. output. (default 0)
  67. nice This parameter controls the driver's use of
  68. idle CPU time, at the expense of some speed.
  69. If this driver is built into the kernel, you can use kernel
  70. the following command line parameters, with the same values
  71. as the corresponding module parameters listed above:
  72. pd.drive0
  73. pd.drive1
  74. pd.drive2
  75. pd.drive3
  76. pd.cluster
  77. pd.nice
  78. In addition, you can use the parameter pd.disable to disable
  79. the driver entirely.
  80. */
  81. /* Changes:
  82. 1.01 GRG 1997.01.24 Restored pd_reset()
  83. Added eject ioctl
  84. 1.02 GRG 1998.05.06 SMP spinlock changes,
  85. Added slave support
  86. 1.03 GRG 1998.06.16 Eliminate an Ugh.
  87. 1.04 GRG 1998.08.15 Extra debugging, use HZ in loop timing
  88. 1.05 GRG 1998.09.24 Added jumbo support
  89. */
  90. #define PD_VERSION "1.05"
  91. #define PD_MAJOR 45
  92. #define PD_NAME "pd"
  93. #define PD_UNITS 4
  94. /* Here are things one can override from the insmod command.
  95. Most are autoprobed by paride unless set here. Verbose is off
  96. by default.
  97. */
  98. #include <linux/types.h>
  99. static int verbose = 0;
  100. static int major = PD_MAJOR;
  101. static char *name = PD_NAME;
  102. static int cluster = 64;
  103. static int nice = 0;
  104. static int disable = 0;
  105. static int drive0[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
  106. static int drive1[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
  107. static int drive2[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
  108. static int drive3[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
  109. static int (*drives[4])[8] = {&drive0, &drive1, &drive2, &drive3};
  110. enum {D_PRT, D_PRO, D_UNI, D_MOD, D_GEO, D_SBY, D_DLY, D_SLV};
  111. /* end of parameters */
  112. #include <linux/init.h>
  113. #include <linux/module.h>
  114. #include <linux/gfp.h>
  115. #include <linux/fs.h>
  116. #include <linux/delay.h>
  117. #include <linux/hdreg.h>
  118. #include <linux/cdrom.h> /* for the eject ioctl */
  119. #include <linux/blkdev.h>
  120. #include <linux/blkpg.h>
  121. #include <linux/kernel.h>
  122. #include <linux/mutex.h>
  123. #include <asm/uaccess.h>
  124. #include <linux/workqueue.h>
  125. static DEFINE_MUTEX(pd_mutex);
  126. static DEFINE_SPINLOCK(pd_lock);
  127. module_param(verbose, int, 0);
  128. module_param(major, int, 0);
  129. module_param(name, charp, 0);
  130. module_param(cluster, int, 0);
  131. module_param(nice, int, 0);
  132. module_param_array(drive0, int, NULL, 0);
  133. module_param_array(drive1, int, NULL, 0);
  134. module_param_array(drive2, int, NULL, 0);
  135. module_param_array(drive3, int, NULL, 0);
  136. #include "paride.h"
  137. #define PD_BITS 4
  138. /* numbers for "SCSI" geometry */
  139. #define PD_LOG_HEADS 64
  140. #define PD_LOG_SECTS 32
  141. #define PD_ID_OFF 54
  142. #define PD_ID_LEN 14
  143. #define PD_MAX_RETRIES 5
  144. #define PD_TMO 800 /* interrupt timeout in jiffies */
  145. #define PD_SPIN_DEL 50 /* spin delay in micro-seconds */
  146. #define PD_SPIN (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
  147. #define STAT_ERR 0x00001
  148. #define STAT_INDEX 0x00002
  149. #define STAT_ECC 0x00004
  150. #define STAT_DRQ 0x00008
  151. #define STAT_SEEK 0x00010
  152. #define STAT_WRERR 0x00020
  153. #define STAT_READY 0x00040
  154. #define STAT_BUSY 0x00080
  155. #define ERR_AMNF 0x00100
  156. #define ERR_TK0NF 0x00200
  157. #define ERR_ABRT 0x00400
  158. #define ERR_MCR 0x00800
  159. #define ERR_IDNF 0x01000
  160. #define ERR_MC 0x02000
  161. #define ERR_UNC 0x04000
  162. #define ERR_TMO 0x10000
  163. #define IDE_READ 0x20
  164. #define IDE_WRITE 0x30
  165. #define IDE_READ_VRFY 0x40
  166. #define IDE_INIT_DEV_PARMS 0x91
  167. #define IDE_STANDBY 0x96
  168. #define IDE_ACKCHANGE 0xdb
  169. #define IDE_DOORLOCK 0xde
  170. #define IDE_DOORUNLOCK 0xdf
  171. #define IDE_IDENTIFY 0xec
  172. #define IDE_EJECT 0xed
  173. #define PD_NAMELEN 8
  174. struct pd_unit {
  175. struct pi_adapter pia; /* interface to paride layer */
  176. struct pi_adapter *pi;
  177. int access; /* count of active opens ... */
  178. int capacity; /* Size of this volume in sectors */
  179. int heads; /* physical geometry */
  180. int sectors;
  181. int cylinders;
  182. int can_lba;
  183. int drive; /* master=0 slave=1 */
  184. int changed; /* Have we seen a disk change ? */
  185. int removable; /* removable media device ? */
  186. int standby;
  187. int alt_geom;
  188. char name[PD_NAMELEN]; /* pda, pdb, etc ... */
  189. struct gendisk *gd;
  190. };
  191. static struct pd_unit pd[PD_UNITS];
  192. static char pd_scratch[512]; /* scratch block buffer */
  193. static char *pd_errs[17] = { "ERR", "INDEX", "ECC", "DRQ", "SEEK", "WRERR",
  194. "READY", "BUSY", "AMNF", "TK0NF", "ABRT", "MCR",
  195. "IDNF", "MC", "UNC", "???", "TMO"
  196. };
  197. static void *par_drv; /* reference of parport driver */
  198. static inline int status_reg(struct pd_unit *disk)
  199. {
  200. return pi_read_regr(disk->pi, 1, 6);
  201. }
  202. static inline int read_reg(struct pd_unit *disk, int reg)
  203. {
  204. return pi_read_regr(disk->pi, 0, reg);
  205. }
  206. static inline void write_status(struct pd_unit *disk, int val)
  207. {
  208. pi_write_regr(disk->pi, 1, 6, val);
  209. }
  210. static inline void write_reg(struct pd_unit *disk, int reg, int val)
  211. {
  212. pi_write_regr(disk->pi, 0, reg, val);
  213. }
  214. static inline u8 DRIVE(struct pd_unit *disk)
  215. {
  216. return 0xa0+0x10*disk->drive;
  217. }
  218. /* ide command interface */
  219. static void pd_print_error(struct pd_unit *disk, char *msg, int status)
  220. {
  221. int i;
  222. printk("%s: %s: status = 0x%x =", disk->name, msg, status);
  223. for (i = 0; i < ARRAY_SIZE(pd_errs); i++)
  224. if (status & (1 << i))
  225. printk(" %s", pd_errs[i]);
  226. printk("\n");
  227. }
  228. static void pd_reset(struct pd_unit *disk)
  229. { /* called only for MASTER drive */
  230. write_status(disk, 4);
  231. udelay(50);
  232. write_status(disk, 0);
  233. udelay(250);
  234. }
  235. #define DBMSG(msg) ((verbose>1)?(msg):NULL)
  236. static int pd_wait_for(struct pd_unit *disk, int w, char *msg)
  237. { /* polled wait */
  238. int k, r, e;
  239. k = 0;
  240. while (k < PD_SPIN) {
  241. r = status_reg(disk);
  242. k++;
  243. if (((r & w) == w) && !(r & STAT_BUSY))
  244. break;
  245. udelay(PD_SPIN_DEL);
  246. }
  247. e = (read_reg(disk, 1) << 8) + read_reg(disk, 7);
  248. if (k >= PD_SPIN)
  249. e |= ERR_TMO;
  250. if ((e & (STAT_ERR | ERR_TMO)) && (msg != NULL))
  251. pd_print_error(disk, msg, e);
  252. return e;
  253. }
  254. static void pd_send_command(struct pd_unit *disk, int n, int s, int h, int c0, int c1, int func)
  255. {
  256. write_reg(disk, 6, DRIVE(disk) + h);
  257. write_reg(disk, 1, 0); /* the IDE task file */
  258. write_reg(disk, 2, n);
  259. write_reg(disk, 3, s);
  260. write_reg(disk, 4, c0);
  261. write_reg(disk, 5, c1);
  262. write_reg(disk, 7, func);
  263. udelay(1);
  264. }
  265. static void pd_ide_command(struct pd_unit *disk, int func, int block, int count)
  266. {
  267. int c1, c0, h, s;
  268. if (disk->can_lba) {
  269. s = block & 255;
  270. c0 = (block >>= 8) & 255;
  271. c1 = (block >>= 8) & 255;
  272. h = ((block >>= 8) & 15) + 0x40;
  273. } else {
  274. s = (block % disk->sectors) + 1;
  275. h = (block /= disk->sectors) % disk->heads;
  276. c0 = (block /= disk->heads) % 256;
  277. c1 = (block >>= 8);
  278. }
  279. pd_send_command(disk, count, s, h, c0, c1, func);
  280. }
  281. /* The i/o request engine */
  282. enum action {Fail = 0, Ok = 1, Hold, Wait};
  283. static struct request *pd_req; /* current request */
  284. static enum action (*phase)(void);
  285. static void run_fsm(void);
  286. static void ps_tq_int(struct work_struct *work);
  287. static DECLARE_DELAYED_WORK(fsm_tq, ps_tq_int);
  288. static void schedule_fsm(void)
  289. {
  290. if (!nice)
  291. schedule_delayed_work(&fsm_tq, 0);
  292. else
  293. schedule_delayed_work(&fsm_tq, nice-1);
  294. }
  295. static void ps_tq_int(struct work_struct *work)
  296. {
  297. run_fsm();
  298. }
  299. static enum action do_pd_io_start(void);
  300. static enum action pd_special(void);
  301. static enum action do_pd_read_start(void);
  302. static enum action do_pd_write_start(void);
  303. static enum action do_pd_read_drq(void);
  304. static enum action do_pd_write_done(void);
  305. static struct request_queue *pd_queue;
  306. static int pd_claimed;
  307. static struct pd_unit *pd_current; /* current request's drive */
  308. static PIA *pi_current; /* current request's PIA */
  309. static void run_fsm(void)
  310. {
  311. while (1) {
  312. enum action res;
  313. unsigned long saved_flags;
  314. int stop = 0;
  315. if (!phase) {
  316. pd_current = pd_req->rq_disk->private_data;
  317. pi_current = pd_current->pi;
  318. phase = do_pd_io_start;
  319. }
  320. switch (pd_claimed) {
  321. case 0:
  322. pd_claimed = 1;
  323. if (!pi_schedule_claimed(pi_current, run_fsm))
  324. return;
  325. case 1:
  326. pd_claimed = 2;
  327. pi_current->proto->connect(pi_current);
  328. }
  329. switch(res = phase()) {
  330. case Ok: case Fail:
  331. pi_disconnect(pi_current);
  332. pd_claimed = 0;
  333. phase = NULL;
  334. spin_lock_irqsave(&pd_lock, saved_flags);
  335. if (!__blk_end_request_cur(pd_req,
  336. res == Ok ? 0 : -EIO)) {
  337. pd_req = blk_fetch_request(pd_queue);
  338. if (!pd_req)
  339. stop = 1;
  340. }
  341. spin_unlock_irqrestore(&pd_lock, saved_flags);
  342. if (stop)
  343. return;
  344. case Hold:
  345. schedule_fsm();
  346. return;
  347. case Wait:
  348. pi_disconnect(pi_current);
  349. pd_claimed = 0;
  350. }
  351. }
  352. }
  353. static int pd_retries = 0; /* i/o error retry count */
  354. static int pd_block; /* address of next requested block */
  355. static int pd_count; /* number of blocks still to do */
  356. static int pd_run; /* sectors in current cluster */
  357. static int pd_cmd; /* current command READ/WRITE */
  358. static char *pd_buf; /* buffer for request in progress */
  359. static enum action do_pd_io_start(void)
  360. {
  361. if (pd_req->cmd_type == REQ_TYPE_DRV_PRIV) {
  362. phase = pd_special;
  363. return pd_special();
  364. }
  365. pd_cmd = rq_data_dir(pd_req);
  366. if (pd_cmd == READ || pd_cmd == WRITE) {
  367. pd_block = blk_rq_pos(pd_req);
  368. pd_count = blk_rq_cur_sectors(pd_req);
  369. if (pd_block + pd_count > get_capacity(pd_req->rq_disk))
  370. return Fail;
  371. pd_run = blk_rq_sectors(pd_req);
  372. pd_buf = bio_data(pd_req->bio);
  373. pd_retries = 0;
  374. if (pd_cmd == READ)
  375. return do_pd_read_start();
  376. else
  377. return do_pd_write_start();
  378. }
  379. return Fail;
  380. }
  381. static enum action pd_special(void)
  382. {
  383. enum action (*func)(struct pd_unit *) = pd_req->special;
  384. return func(pd_current);
  385. }
  386. static int pd_next_buf(void)
  387. {
  388. unsigned long saved_flags;
  389. pd_count--;
  390. pd_run--;
  391. pd_buf += 512;
  392. pd_block++;
  393. if (!pd_run)
  394. return 1;
  395. if (pd_count)
  396. return 0;
  397. spin_lock_irqsave(&pd_lock, saved_flags);
  398. __blk_end_request_cur(pd_req, 0);
  399. pd_count = blk_rq_cur_sectors(pd_req);
  400. pd_buf = bio_data(pd_req->bio);
  401. spin_unlock_irqrestore(&pd_lock, saved_flags);
  402. return 0;
  403. }
  404. static unsigned long pd_timeout;
  405. static enum action do_pd_read_start(void)
  406. {
  407. if (pd_wait_for(pd_current, STAT_READY, "do_pd_read") & STAT_ERR) {
  408. if (pd_retries < PD_MAX_RETRIES) {
  409. pd_retries++;
  410. return Wait;
  411. }
  412. return Fail;
  413. }
  414. pd_ide_command(pd_current, IDE_READ, pd_block, pd_run);
  415. phase = do_pd_read_drq;
  416. pd_timeout = jiffies + PD_TMO;
  417. return Hold;
  418. }
  419. static enum action do_pd_write_start(void)
  420. {
  421. if (pd_wait_for(pd_current, STAT_READY, "do_pd_write") & STAT_ERR) {
  422. if (pd_retries < PD_MAX_RETRIES) {
  423. pd_retries++;
  424. return Wait;
  425. }
  426. return Fail;
  427. }
  428. pd_ide_command(pd_current, IDE_WRITE, pd_block, pd_run);
  429. while (1) {
  430. if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_write_drq") & STAT_ERR) {
  431. if (pd_retries < PD_MAX_RETRIES) {
  432. pd_retries++;
  433. return Wait;
  434. }
  435. return Fail;
  436. }
  437. pi_write_block(pd_current->pi, pd_buf, 512);
  438. if (pd_next_buf())
  439. break;
  440. }
  441. phase = do_pd_write_done;
  442. pd_timeout = jiffies + PD_TMO;
  443. return Hold;
  444. }
  445. static inline int pd_ready(void)
  446. {
  447. return !(status_reg(pd_current) & STAT_BUSY);
  448. }
  449. static enum action do_pd_read_drq(void)
  450. {
  451. if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
  452. return Hold;
  453. while (1) {
  454. if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_read_drq") & STAT_ERR) {
  455. if (pd_retries < PD_MAX_RETRIES) {
  456. pd_retries++;
  457. phase = do_pd_read_start;
  458. return Wait;
  459. }
  460. return Fail;
  461. }
  462. pi_read_block(pd_current->pi, pd_buf, 512);
  463. if (pd_next_buf())
  464. break;
  465. }
  466. return Ok;
  467. }
  468. static enum action do_pd_write_done(void)
  469. {
  470. if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
  471. return Hold;
  472. if (pd_wait_for(pd_current, STAT_READY, "do_pd_write_done") & STAT_ERR) {
  473. if (pd_retries < PD_MAX_RETRIES) {
  474. pd_retries++;
  475. phase = do_pd_write_start;
  476. return Wait;
  477. }
  478. return Fail;
  479. }
  480. return Ok;
  481. }
  482. /* special io requests */
  483. /* According to the ATA standard, the default CHS geometry should be
  484. available following a reset. Some Western Digital drives come up
  485. in a mode where only LBA addresses are accepted until the device
  486. parameters are initialised.
  487. */
  488. static void pd_init_dev_parms(struct pd_unit *disk)
  489. {
  490. pd_wait_for(disk, 0, DBMSG("before init_dev_parms"));
  491. pd_send_command(disk, disk->sectors, 0, disk->heads - 1, 0, 0,
  492. IDE_INIT_DEV_PARMS);
  493. udelay(300);
  494. pd_wait_for(disk, 0, "Initialise device parameters");
  495. }
  496. static enum action pd_door_lock(struct pd_unit *disk)
  497. {
  498. if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
  499. pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORLOCK);
  500. pd_wait_for(disk, STAT_READY, "Lock done");
  501. }
  502. return Ok;
  503. }
  504. static enum action pd_door_unlock(struct pd_unit *disk)
  505. {
  506. if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
  507. pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
  508. pd_wait_for(disk, STAT_READY, "Lock done");
  509. }
  510. return Ok;
  511. }
  512. static enum action pd_eject(struct pd_unit *disk)
  513. {
  514. pd_wait_for(disk, 0, DBMSG("before unlock on eject"));
  515. pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
  516. pd_wait_for(disk, 0, DBMSG("after unlock on eject"));
  517. pd_wait_for(disk, 0, DBMSG("before eject"));
  518. pd_send_command(disk, 0, 0, 0, 0, 0, IDE_EJECT);
  519. pd_wait_for(disk, 0, DBMSG("after eject"));
  520. return Ok;
  521. }
  522. static enum action pd_media_check(struct pd_unit *disk)
  523. {
  524. int r = pd_wait_for(disk, STAT_READY, DBMSG("before media_check"));
  525. if (!(r & STAT_ERR)) {
  526. pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
  527. r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after READ_VRFY"));
  528. } else
  529. disk->changed = 1; /* say changed if other error */
  530. if (r & ERR_MC) {
  531. disk->changed = 1;
  532. pd_send_command(disk, 1, 0, 0, 0, 0, IDE_ACKCHANGE);
  533. pd_wait_for(disk, STAT_READY, DBMSG("RDY after ACKCHANGE"));
  534. pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
  535. r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after VRFY"));
  536. }
  537. return Ok;
  538. }
  539. static void pd_standby_off(struct pd_unit *disk)
  540. {
  541. pd_wait_for(disk, 0, DBMSG("before STANDBY"));
  542. pd_send_command(disk, 0, 0, 0, 0, 0, IDE_STANDBY);
  543. pd_wait_for(disk, 0, DBMSG("after STANDBY"));
  544. }
  545. static enum action pd_identify(struct pd_unit *disk)
  546. {
  547. int j;
  548. char id[PD_ID_LEN + 1];
  549. /* WARNING: here there may be dragons. reset() applies to both drives,
  550. but we call it only on probing the MASTER. This should allow most
  551. common configurations to work, but be warned that a reset can clear
  552. settings on the SLAVE drive.
  553. */
  554. if (disk->drive == 0)
  555. pd_reset(disk);
  556. write_reg(disk, 6, DRIVE(disk));
  557. pd_wait_for(disk, 0, DBMSG("before IDENT"));
  558. pd_send_command(disk, 1, 0, 0, 0, 0, IDE_IDENTIFY);
  559. if (pd_wait_for(disk, STAT_DRQ, DBMSG("IDENT DRQ")) & STAT_ERR)
  560. return Fail;
  561. pi_read_block(disk->pi, pd_scratch, 512);
  562. disk->can_lba = pd_scratch[99] & 2;
  563. disk->sectors = le16_to_cpu(*(__le16 *) (pd_scratch + 12));
  564. disk->heads = le16_to_cpu(*(__le16 *) (pd_scratch + 6));
  565. disk->cylinders = le16_to_cpu(*(__le16 *) (pd_scratch + 2));
  566. if (disk->can_lba)
  567. disk->capacity = le32_to_cpu(*(__le32 *) (pd_scratch + 120));
  568. else
  569. disk->capacity = disk->sectors * disk->heads * disk->cylinders;
  570. for (j = 0; j < PD_ID_LEN; j++)
  571. id[j ^ 1] = pd_scratch[j + PD_ID_OFF];
  572. j = PD_ID_LEN - 1;
  573. while ((j >= 0) && (id[j] <= 0x20))
  574. j--;
  575. j++;
  576. id[j] = 0;
  577. disk->removable = pd_scratch[0] & 0x80;
  578. printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
  579. disk->name, id,
  580. disk->drive ? "slave" : "master",
  581. disk->capacity, disk->capacity / 2048,
  582. disk->cylinders, disk->heads, disk->sectors,
  583. disk->removable ? "removable" : "fixed");
  584. if (disk->capacity)
  585. pd_init_dev_parms(disk);
  586. if (!disk->standby)
  587. pd_standby_off(disk);
  588. return Ok;
  589. }
  590. /* end of io request engine */
  591. static void do_pd_request(struct request_queue * q)
  592. {
  593. if (pd_req)
  594. return;
  595. pd_req = blk_fetch_request(q);
  596. if (!pd_req)
  597. return;
  598. schedule_fsm();
  599. }
  600. static int pd_special_command(struct pd_unit *disk,
  601. enum action (*func)(struct pd_unit *disk))
  602. {
  603. struct request *rq;
  604. int err = 0;
  605. rq = blk_get_request(disk->gd->queue, READ, __GFP_RECLAIM);
  606. if (IS_ERR(rq))
  607. return PTR_ERR(rq);
  608. rq->cmd_type = REQ_TYPE_DRV_PRIV;
  609. rq->special = func;
  610. err = blk_execute_rq(disk->gd->queue, disk->gd, rq, 0);
  611. blk_put_request(rq);
  612. return err;
  613. }
  614. /* kernel glue structures */
  615. static int pd_open(struct block_device *bdev, fmode_t mode)
  616. {
  617. struct pd_unit *disk = bdev->bd_disk->private_data;
  618. mutex_lock(&pd_mutex);
  619. disk->access++;
  620. if (disk->removable) {
  621. pd_special_command(disk, pd_media_check);
  622. pd_special_command(disk, pd_door_lock);
  623. }
  624. mutex_unlock(&pd_mutex);
  625. return 0;
  626. }
  627. static int pd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  628. {
  629. struct pd_unit *disk = bdev->bd_disk->private_data;
  630. if (disk->alt_geom) {
  631. geo->heads = PD_LOG_HEADS;
  632. geo->sectors = PD_LOG_SECTS;
  633. geo->cylinders = disk->capacity / (geo->heads * geo->sectors);
  634. } else {
  635. geo->heads = disk->heads;
  636. geo->sectors = disk->sectors;
  637. geo->cylinders = disk->cylinders;
  638. }
  639. return 0;
  640. }
  641. static int pd_ioctl(struct block_device *bdev, fmode_t mode,
  642. unsigned int cmd, unsigned long arg)
  643. {
  644. struct pd_unit *disk = bdev->bd_disk->private_data;
  645. switch (cmd) {
  646. case CDROMEJECT:
  647. mutex_lock(&pd_mutex);
  648. if (disk->access == 1)
  649. pd_special_command(disk, pd_eject);
  650. mutex_unlock(&pd_mutex);
  651. return 0;
  652. default:
  653. return -EINVAL;
  654. }
  655. }
  656. static void pd_release(struct gendisk *p, fmode_t mode)
  657. {
  658. struct pd_unit *disk = p->private_data;
  659. mutex_lock(&pd_mutex);
  660. if (!--disk->access && disk->removable)
  661. pd_special_command(disk, pd_door_unlock);
  662. mutex_unlock(&pd_mutex);
  663. }
  664. static unsigned int pd_check_events(struct gendisk *p, unsigned int clearing)
  665. {
  666. struct pd_unit *disk = p->private_data;
  667. int r;
  668. if (!disk->removable)
  669. return 0;
  670. pd_special_command(disk, pd_media_check);
  671. r = disk->changed;
  672. disk->changed = 0;
  673. return r ? DISK_EVENT_MEDIA_CHANGE : 0;
  674. }
  675. static int pd_revalidate(struct gendisk *p)
  676. {
  677. struct pd_unit *disk = p->private_data;
  678. if (pd_special_command(disk, pd_identify) == 0)
  679. set_capacity(p, disk->capacity);
  680. else
  681. set_capacity(p, 0);
  682. return 0;
  683. }
  684. static const struct block_device_operations pd_fops = {
  685. .owner = THIS_MODULE,
  686. .open = pd_open,
  687. .release = pd_release,
  688. .ioctl = pd_ioctl,
  689. .getgeo = pd_getgeo,
  690. .check_events = pd_check_events,
  691. .revalidate_disk= pd_revalidate
  692. };
  693. /* probing */
  694. static void pd_probe_drive(struct pd_unit *disk)
  695. {
  696. struct gendisk *p = alloc_disk(1 << PD_BITS);
  697. if (!p)
  698. return;
  699. strcpy(p->disk_name, disk->name);
  700. p->fops = &pd_fops;
  701. p->major = major;
  702. p->first_minor = (disk - pd) << PD_BITS;
  703. disk->gd = p;
  704. p->private_data = disk;
  705. p->queue = pd_queue;
  706. if (disk->drive == -1) {
  707. for (disk->drive = 0; disk->drive <= 1; disk->drive++)
  708. if (pd_special_command(disk, pd_identify) == 0)
  709. return;
  710. } else if (pd_special_command(disk, pd_identify) == 0)
  711. return;
  712. disk->gd = NULL;
  713. put_disk(p);
  714. }
  715. static int pd_detect(void)
  716. {
  717. int found = 0, unit, pd_drive_count = 0;
  718. struct pd_unit *disk;
  719. for (unit = 0; unit < PD_UNITS; unit++) {
  720. int *parm = *drives[unit];
  721. struct pd_unit *disk = pd + unit;
  722. disk->pi = &disk->pia;
  723. disk->access = 0;
  724. disk->changed = 1;
  725. disk->capacity = 0;
  726. disk->drive = parm[D_SLV];
  727. snprintf(disk->name, PD_NAMELEN, "%s%c", name, 'a'+unit);
  728. disk->alt_geom = parm[D_GEO];
  729. disk->standby = parm[D_SBY];
  730. if (parm[D_PRT])
  731. pd_drive_count++;
  732. }
  733. par_drv = pi_register_driver(name);
  734. if (!par_drv) {
  735. pr_err("failed to register %s driver\n", name);
  736. return -1;
  737. }
  738. if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
  739. disk = pd;
  740. if (pi_init(disk->pi, 1, -1, -1, -1, -1, -1, pd_scratch,
  741. PI_PD, verbose, disk->name)) {
  742. pd_probe_drive(disk);
  743. if (!disk->gd)
  744. pi_release(disk->pi);
  745. }
  746. } else {
  747. for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
  748. int *parm = *drives[unit];
  749. if (!parm[D_PRT])
  750. continue;
  751. if (pi_init(disk->pi, 0, parm[D_PRT], parm[D_MOD],
  752. parm[D_UNI], parm[D_PRO], parm[D_DLY],
  753. pd_scratch, PI_PD, verbose, disk->name)) {
  754. pd_probe_drive(disk);
  755. if (!disk->gd)
  756. pi_release(disk->pi);
  757. }
  758. }
  759. }
  760. for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
  761. if (disk->gd) {
  762. set_capacity(disk->gd, disk->capacity);
  763. add_disk(disk->gd);
  764. found = 1;
  765. }
  766. }
  767. if (!found) {
  768. printk("%s: no valid drive found\n", name);
  769. pi_unregister_driver(par_drv);
  770. }
  771. return found;
  772. }
  773. static int __init pd_init(void)
  774. {
  775. if (disable)
  776. goto out1;
  777. pd_queue = blk_init_queue(do_pd_request, &pd_lock);
  778. if (!pd_queue)
  779. goto out1;
  780. blk_queue_max_hw_sectors(pd_queue, cluster);
  781. if (register_blkdev(major, name))
  782. goto out2;
  783. printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
  784. name, name, PD_VERSION, major, cluster, nice);
  785. if (!pd_detect())
  786. goto out3;
  787. return 0;
  788. out3:
  789. unregister_blkdev(major, name);
  790. out2:
  791. blk_cleanup_queue(pd_queue);
  792. out1:
  793. return -ENODEV;
  794. }
  795. static void __exit pd_exit(void)
  796. {
  797. struct pd_unit *disk;
  798. int unit;
  799. unregister_blkdev(major, name);
  800. for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
  801. struct gendisk *p = disk->gd;
  802. if (p) {
  803. disk->gd = NULL;
  804. del_gendisk(p);
  805. put_disk(p);
  806. pi_release(disk->pi);
  807. }
  808. }
  809. blk_cleanup_queue(pd_queue);
  810. }
  811. MODULE_LICENSE("GPL");
  812. module_init(pd_init)
  813. module_exit(pd_exit)