ide-disk.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
  3. * Copyright (C) 1998-2002 Linux ATA Development
  4. * Andre Hedrick <andre@linux-ide.org>
  5. * Copyright (C) 2003 Red Hat
  6. * Copyright (C) 2003-2005, 2007 Bartlomiej Zolnierkiewicz
  7. */
  8. /*
  9. * Mostly written by Mark Lord <mlord@pobox.com>
  10. * and Gadi Oxman <gadio@netvision.net.il>
  11. * and Andre Hedrick <andre@linux-ide.org>
  12. *
  13. * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
  14. */
  15. #include <linux/types.h>
  16. #include <linux/string.h>
  17. #include <linux/kernel.h>
  18. #include <linux/timer.h>
  19. #include <linux/mm.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/major.h>
  22. #include <linux/errno.h>
  23. #include <linux/genhd.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <linux/mutex.h>
  27. #include <linux/leds.h>
  28. #include <linux/ide.h>
  29. #include <asm/byteorder.h>
  30. #include <asm/irq.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/io.h>
  33. #include <asm/div64.h>
  34. #include "ide-disk.h"
  35. static const u8 ide_rw_cmds[] = {
  36. ATA_CMD_READ_MULTI,
  37. ATA_CMD_WRITE_MULTI,
  38. ATA_CMD_READ_MULTI_EXT,
  39. ATA_CMD_WRITE_MULTI_EXT,
  40. ATA_CMD_PIO_READ,
  41. ATA_CMD_PIO_WRITE,
  42. ATA_CMD_PIO_READ_EXT,
  43. ATA_CMD_PIO_WRITE_EXT,
  44. ATA_CMD_READ,
  45. ATA_CMD_WRITE,
  46. ATA_CMD_READ_EXT,
  47. ATA_CMD_WRITE_EXT,
  48. };
  49. static void ide_tf_set_cmd(ide_drive_t *drive, struct ide_cmd *cmd, u8 dma)
  50. {
  51. u8 index, lba48, write;
  52. lba48 = (cmd->tf_flags & IDE_TFLAG_LBA48) ? 2 : 0;
  53. write = (cmd->tf_flags & IDE_TFLAG_WRITE) ? 1 : 0;
  54. if (dma) {
  55. cmd->protocol = ATA_PROT_DMA;
  56. index = 8;
  57. } else {
  58. cmd->protocol = ATA_PROT_PIO;
  59. if (drive->mult_count) {
  60. cmd->tf_flags |= IDE_TFLAG_MULTI_PIO;
  61. index = 0;
  62. } else
  63. index = 4;
  64. }
  65. cmd->tf.command = ide_rw_cmds[index + lba48 + write];
  66. }
  67. /*
  68. * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
  69. * using LBA if supported, or CHS otherwise, to address sectors.
  70. */
  71. static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
  72. sector_t block)
  73. {
  74. ide_hwif_t *hwif = drive->hwif;
  75. u16 nsectors = (u16)rq->nr_sectors;
  76. u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48);
  77. u8 dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
  78. struct ide_cmd cmd;
  79. struct ide_taskfile *tf = &cmd.tf;
  80. ide_startstop_t rc;
  81. if ((hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) && lba48 && dma) {
  82. if (block + rq->nr_sectors > 1ULL << 28)
  83. dma = 0;
  84. else
  85. lba48 = 0;
  86. }
  87. memset(&cmd, 0, sizeof(cmd));
  88. cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  89. if (drive->dev_flags & IDE_DFLAG_LBA) {
  90. if (lba48) {
  91. pr_debug("%s: LBA=0x%012llx\n", drive->name,
  92. (unsigned long long)block);
  93. tf->hob_nsect = (nsectors >> 8) & 0xff;
  94. tf->hob_lbal = (u8)(block >> 24);
  95. if (sizeof(block) != 4) {
  96. tf->hob_lbam = (u8)((u64)block >> 32);
  97. tf->hob_lbah = (u8)((u64)block >> 40);
  98. }
  99. tf->nsect = nsectors & 0xff;
  100. tf->lbal = (u8) block;
  101. tf->lbam = (u8)(block >> 8);
  102. tf->lbah = (u8)(block >> 16);
  103. cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
  104. } else {
  105. tf->nsect = nsectors & 0xff;
  106. tf->lbal = block;
  107. tf->lbam = block >>= 8;
  108. tf->lbah = block >>= 8;
  109. tf->device = (block >> 8) & 0xf;
  110. }
  111. tf->device |= ATA_LBA;
  112. } else {
  113. unsigned int sect, head, cyl, track;
  114. track = (int)block / drive->sect;
  115. sect = (int)block % drive->sect + 1;
  116. head = track % drive->head;
  117. cyl = track / drive->head;
  118. pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
  119. tf->nsect = nsectors & 0xff;
  120. tf->lbal = sect;
  121. tf->lbam = cyl;
  122. tf->lbah = cyl >> 8;
  123. tf->device = head;
  124. }
  125. cmd.tf_flags |= IDE_TFLAG_FS;
  126. if (rq_data_dir(rq))
  127. cmd.tf_flags |= IDE_TFLAG_WRITE;
  128. ide_tf_set_cmd(drive, &cmd, dma);
  129. cmd.rq = rq;
  130. if (dma == 0) {
  131. ide_init_sg_cmd(&cmd, nsectors << 9);
  132. ide_map_sg(drive, &cmd);
  133. }
  134. rc = do_rw_taskfile(drive, &cmd);
  135. if (rc == ide_stopped && dma) {
  136. /* fallback to PIO */
  137. cmd.tf_flags |= IDE_TFLAG_DMA_PIO_FALLBACK;
  138. ide_tf_set_cmd(drive, &cmd, 0);
  139. ide_init_sg_cmd(&cmd, nsectors << 9);
  140. rc = do_rw_taskfile(drive, &cmd);
  141. }
  142. return rc;
  143. }
  144. /*
  145. * 268435455 == 137439 MB or 28bit limit
  146. * 320173056 == 163929 MB or 48bit addressing
  147. * 1073741822 == 549756 MB or 48bit addressing fake drive
  148. */
  149. static ide_startstop_t ide_do_rw_disk(ide_drive_t *drive, struct request *rq,
  150. sector_t block)
  151. {
  152. ide_hwif_t *hwif = drive->hwif;
  153. BUG_ON(drive->dev_flags & IDE_DFLAG_BLOCKED);
  154. if (!blk_fs_request(rq)) {
  155. blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
  156. if (rq->errors == 0)
  157. rq->errors = -EIO;
  158. ide_complete_rq(drive, -EIO, ide_rq_bytes(rq));
  159. return ide_stopped;
  160. }
  161. ledtrig_ide_activity();
  162. pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
  163. drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
  164. (unsigned long long)block, rq->nr_sectors,
  165. (unsigned long)rq->buffer);
  166. if (hwif->rw_disk)
  167. hwif->rw_disk(drive, rq);
  168. return __ide_do_rw_disk(drive, rq, block);
  169. }
  170. /*
  171. * Queries for true maximum capacity of the drive.
  172. * Returns maximum LBA address (> 0) of the drive, 0 if failed.
  173. */
  174. static u64 idedisk_read_native_max_address(ide_drive_t *drive, int lba48)
  175. {
  176. struct ide_cmd cmd;
  177. struct ide_taskfile *tf = &cmd.tf;
  178. u64 addr = 0;
  179. memset(&cmd, 0, sizeof(cmd));
  180. if (lba48)
  181. tf->command = ATA_CMD_READ_NATIVE_MAX_EXT;
  182. else
  183. tf->command = ATA_CMD_READ_NATIVE_MAX;
  184. tf->device = ATA_LBA;
  185. cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  186. if (lba48)
  187. cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
  188. ide_no_data_taskfile(drive, &cmd);
  189. /* if OK, compute maximum address value */
  190. if (!(tf->status & ATA_ERR))
  191. addr = ide_get_lba_addr(tf, lba48) + 1;
  192. return addr;
  193. }
  194. /*
  195. * Sets maximum virtual LBA address of the drive.
  196. * Returns new maximum virtual LBA address (> 0) or 0 on failure.
  197. */
  198. static u64 idedisk_set_max_address(ide_drive_t *drive, u64 addr_req, int lba48)
  199. {
  200. struct ide_cmd cmd;
  201. struct ide_taskfile *tf = &cmd.tf;
  202. u64 addr_set = 0;
  203. addr_req--;
  204. memset(&cmd, 0, sizeof(cmd));
  205. tf->lbal = (addr_req >> 0) & 0xff;
  206. tf->lbam = (addr_req >>= 8) & 0xff;
  207. tf->lbah = (addr_req >>= 8) & 0xff;
  208. if (lba48) {
  209. tf->hob_lbal = (addr_req >>= 8) & 0xff;
  210. tf->hob_lbam = (addr_req >>= 8) & 0xff;
  211. tf->hob_lbah = (addr_req >>= 8) & 0xff;
  212. tf->command = ATA_CMD_SET_MAX_EXT;
  213. } else {
  214. tf->device = (addr_req >>= 8) & 0x0f;
  215. tf->command = ATA_CMD_SET_MAX;
  216. }
  217. tf->device |= ATA_LBA;
  218. cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  219. if (lba48)
  220. cmd.tf_flags |= (IDE_TFLAG_LBA48 | IDE_TFLAG_HOB);
  221. ide_no_data_taskfile(drive, &cmd);
  222. /* if OK, compute maximum address value */
  223. if (!(tf->status & ATA_ERR))
  224. addr_set = ide_get_lba_addr(tf, lba48) + 1;
  225. return addr_set;
  226. }
  227. static unsigned long long sectors_to_MB(unsigned long long n)
  228. {
  229. n <<= 9; /* make it bytes */
  230. do_div(n, 1000000); /* make it MB */
  231. return n;
  232. }
  233. /*
  234. * Some disks report total number of sectors instead of
  235. * maximum sector address. We list them here.
  236. */
  237. static const struct drive_list_entry hpa_list[] = {
  238. { "ST340823A", NULL },
  239. { "ST320413A", NULL },
  240. { "ST310211A", NULL },
  241. { NULL, NULL }
  242. };
  243. static void idedisk_check_hpa(ide_drive_t *drive)
  244. {
  245. unsigned long long capacity, set_max;
  246. int lba48 = ata_id_lba48_enabled(drive->id);
  247. capacity = drive->capacity64;
  248. set_max = idedisk_read_native_max_address(drive, lba48);
  249. if (ide_in_drive_list(drive->id, hpa_list)) {
  250. /*
  251. * Since we are inclusive wrt to firmware revisions do this
  252. * extra check and apply the workaround only when needed.
  253. */
  254. if (set_max == capacity + 1)
  255. set_max--;
  256. }
  257. if (set_max <= capacity)
  258. return;
  259. printk(KERN_INFO "%s: Host Protected Area detected.\n"
  260. "\tcurrent capacity is %llu sectors (%llu MB)\n"
  261. "\tnative capacity is %llu sectors (%llu MB)\n",
  262. drive->name,
  263. capacity, sectors_to_MB(capacity),
  264. set_max, sectors_to_MB(set_max));
  265. set_max = idedisk_set_max_address(drive, set_max, lba48);
  266. if (set_max) {
  267. drive->capacity64 = set_max;
  268. printk(KERN_INFO "%s: Host Protected Area disabled.\n",
  269. drive->name);
  270. }
  271. }
  272. static int ide_disk_get_capacity(ide_drive_t *drive)
  273. {
  274. u16 *id = drive->id;
  275. int lba;
  276. if (ata_id_lba48_enabled(id)) {
  277. /* drive speaks 48-bit LBA */
  278. lba = 1;
  279. drive->capacity64 = ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
  280. } else if (ata_id_has_lba(id) && ata_id_is_lba_capacity_ok(id)) {
  281. /* drive speaks 28-bit LBA */
  282. lba = 1;
  283. drive->capacity64 = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
  284. } else {
  285. /* drive speaks boring old 28-bit CHS */
  286. lba = 0;
  287. drive->capacity64 = drive->cyl * drive->head * drive->sect;
  288. }
  289. if (lba) {
  290. drive->dev_flags |= IDE_DFLAG_LBA;
  291. /*
  292. * If this device supports the Host Protected Area feature set,
  293. * then we may need to change our opinion about its capacity.
  294. */
  295. if (ata_id_hpa_enabled(id))
  296. idedisk_check_hpa(drive);
  297. }
  298. /* limit drive capacity to 137GB if LBA48 cannot be used */
  299. if ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 &&
  300. drive->capacity64 > 1ULL << 28) {
  301. printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
  302. "%llu sectors (%llu MB)\n",
  303. drive->name, (unsigned long long)drive->capacity64,
  304. sectors_to_MB(drive->capacity64));
  305. drive->capacity64 = 1ULL << 28;
  306. }
  307. if ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA) &&
  308. (drive->dev_flags & IDE_DFLAG_LBA48)) {
  309. if (drive->capacity64 > 1ULL << 28) {
  310. printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode"
  311. " will be used for accessing sectors "
  312. "> %u\n", drive->name, 1 << 28);
  313. } else
  314. drive->dev_flags &= ~IDE_DFLAG_LBA48;
  315. }
  316. return 0;
  317. }
  318. static void idedisk_prepare_flush(struct request_queue *q, struct request *rq)
  319. {
  320. ide_drive_t *drive = q->queuedata;
  321. struct ide_cmd *cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
  322. /* FIXME: map struct ide_taskfile on rq->cmd[] */
  323. BUG_ON(cmd == NULL);
  324. memset(cmd, 0, sizeof(*cmd));
  325. if (ata_id_flush_ext_enabled(drive->id) &&
  326. (drive->capacity64 >= (1UL << 28)))
  327. cmd->tf.command = ATA_CMD_FLUSH_EXT;
  328. else
  329. cmd->tf.command = ATA_CMD_FLUSH;
  330. cmd->tf_flags = IDE_TFLAG_OUT_TF | IDE_TFLAG_OUT_DEVICE |
  331. IDE_TFLAG_DYN;
  332. cmd->protocol = ATA_PROT_NODATA;
  333. rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
  334. rq->cmd_flags |= REQ_SOFTBARRIER;
  335. rq->special = cmd;
  336. }
  337. ide_devset_get(multcount, mult_count);
  338. /*
  339. * This is tightly woven into the driver->do_special can not touch.
  340. * DON'T do it again until a total personality rewrite is committed.
  341. */
  342. static int set_multcount(ide_drive_t *drive, int arg)
  343. {
  344. struct request *rq;
  345. int error;
  346. if (arg < 0 || arg > (drive->id[ATA_ID_MAX_MULTSECT] & 0xff))
  347. return -EINVAL;
  348. if (drive->special.b.set_multmode)
  349. return -EBUSY;
  350. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  351. rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
  352. drive->mult_req = arg;
  353. drive->special.b.set_multmode = 1;
  354. error = blk_execute_rq(drive->queue, NULL, rq, 0);
  355. blk_put_request(rq);
  356. return (drive->mult_count == arg) ? 0 : -EIO;
  357. }
  358. ide_devset_get_flag(nowerr, IDE_DFLAG_NOWERR);
  359. static int set_nowerr(ide_drive_t *drive, int arg)
  360. {
  361. if (arg < 0 || arg > 1)
  362. return -EINVAL;
  363. if (arg)
  364. drive->dev_flags |= IDE_DFLAG_NOWERR;
  365. else
  366. drive->dev_flags &= ~IDE_DFLAG_NOWERR;
  367. drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
  368. return 0;
  369. }
  370. static int ide_do_setfeature(ide_drive_t *drive, u8 feature, u8 nsect)
  371. {
  372. struct ide_cmd cmd;
  373. memset(&cmd, 0, sizeof(cmd));
  374. cmd.tf.feature = feature;
  375. cmd.tf.nsect = nsect;
  376. cmd.tf.command = ATA_CMD_SET_FEATURES;
  377. cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  378. return ide_no_data_taskfile(drive, &cmd);
  379. }
  380. static void update_ordered(ide_drive_t *drive)
  381. {
  382. u16 *id = drive->id;
  383. unsigned ordered = QUEUE_ORDERED_NONE;
  384. prepare_flush_fn *prep_fn = NULL;
  385. if (drive->dev_flags & IDE_DFLAG_WCACHE) {
  386. unsigned long long capacity;
  387. int barrier;
  388. /*
  389. * We must avoid issuing commands a drive does not
  390. * understand or we may crash it. We check flush cache
  391. * is supported. We also check we have the LBA48 flush
  392. * cache if the drive capacity is too large. By this
  393. * time we have trimmed the drive capacity if LBA48 is
  394. * not available so we don't need to recheck that.
  395. */
  396. capacity = ide_gd_capacity(drive);
  397. barrier = ata_id_flush_enabled(id) &&
  398. (drive->dev_flags & IDE_DFLAG_NOFLUSH) == 0 &&
  399. ((drive->dev_flags & IDE_DFLAG_LBA48) == 0 ||
  400. capacity <= (1ULL << 28) ||
  401. ata_id_flush_ext_enabled(id));
  402. printk(KERN_INFO "%s: cache flushes %ssupported\n",
  403. drive->name, barrier ? "" : "not ");
  404. if (barrier) {
  405. ordered = QUEUE_ORDERED_DRAIN_FLUSH;
  406. prep_fn = idedisk_prepare_flush;
  407. }
  408. } else
  409. ordered = QUEUE_ORDERED_DRAIN;
  410. blk_queue_ordered(drive->queue, ordered, prep_fn);
  411. }
  412. ide_devset_get_flag(wcache, IDE_DFLAG_WCACHE);
  413. static int set_wcache(ide_drive_t *drive, int arg)
  414. {
  415. int err = 1;
  416. if (arg < 0 || arg > 1)
  417. return -EINVAL;
  418. if (ata_id_flush_enabled(drive->id)) {
  419. err = ide_do_setfeature(drive,
  420. arg ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF, 0);
  421. if (err == 0) {
  422. if (arg)
  423. drive->dev_flags |= IDE_DFLAG_WCACHE;
  424. else
  425. drive->dev_flags &= ~IDE_DFLAG_WCACHE;
  426. }
  427. }
  428. update_ordered(drive);
  429. return err;
  430. }
  431. static int do_idedisk_flushcache(ide_drive_t *drive)
  432. {
  433. struct ide_cmd cmd;
  434. memset(&cmd, 0, sizeof(cmd));
  435. if (ata_id_flush_ext_enabled(drive->id))
  436. cmd.tf.command = ATA_CMD_FLUSH_EXT;
  437. else
  438. cmd.tf.command = ATA_CMD_FLUSH;
  439. cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  440. return ide_no_data_taskfile(drive, &cmd);
  441. }
  442. ide_devset_get(acoustic, acoustic);
  443. static int set_acoustic(ide_drive_t *drive, int arg)
  444. {
  445. if (arg < 0 || arg > 254)
  446. return -EINVAL;
  447. ide_do_setfeature(drive,
  448. arg ? SETFEATURES_AAM_ON : SETFEATURES_AAM_OFF, arg);
  449. drive->acoustic = arg;
  450. return 0;
  451. }
  452. ide_devset_get_flag(addressing, IDE_DFLAG_LBA48);
  453. /*
  454. * drive->addressing:
  455. * 0: 28-bit
  456. * 1: 48-bit
  457. * 2: 48-bit capable doing 28-bit
  458. */
  459. static int set_addressing(ide_drive_t *drive, int arg)
  460. {
  461. if (arg < 0 || arg > 2)
  462. return -EINVAL;
  463. if (arg && ((drive->hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
  464. ata_id_lba48_enabled(drive->id) == 0))
  465. return -EIO;
  466. if (arg == 2)
  467. arg = 0;
  468. if (arg)
  469. drive->dev_flags |= IDE_DFLAG_LBA48;
  470. else
  471. drive->dev_flags &= ~IDE_DFLAG_LBA48;
  472. return 0;
  473. }
  474. ide_ext_devset_rw(acoustic, acoustic);
  475. ide_ext_devset_rw(address, addressing);
  476. ide_ext_devset_rw(multcount, multcount);
  477. ide_ext_devset_rw(wcache, wcache);
  478. ide_ext_devset_rw_sync(nowerr, nowerr);
  479. static int ide_disk_check(ide_drive_t *drive, const char *s)
  480. {
  481. return 1;
  482. }
  483. static void ide_disk_setup(ide_drive_t *drive)
  484. {
  485. struct ide_disk_obj *idkp = drive->driver_data;
  486. struct request_queue *q = drive->queue;
  487. ide_hwif_t *hwif = drive->hwif;
  488. u16 *id = drive->id;
  489. char *m = (char *)&id[ATA_ID_PROD];
  490. unsigned long long capacity;
  491. ide_proc_register_driver(drive, idkp->driver);
  492. if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0)
  493. return;
  494. if (drive->dev_flags & IDE_DFLAG_REMOVABLE) {
  495. /*
  496. * Removable disks (eg. SYQUEST); ignore 'WD' drives
  497. */
  498. if (m[0] != 'W' || m[1] != 'D')
  499. drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
  500. }
  501. (void)set_addressing(drive, 1);
  502. if (drive->dev_flags & IDE_DFLAG_LBA48) {
  503. int max_s = 2048;
  504. if (max_s > hwif->rqsize)
  505. max_s = hwif->rqsize;
  506. blk_queue_max_sectors(q, max_s);
  507. }
  508. printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name,
  509. q->max_sectors / 2);
  510. if (ata_id_is_ssd(id))
  511. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
  512. /* calculate drive capacity, and select LBA if possible */
  513. ide_disk_get_capacity(drive);
  514. /*
  515. * if possible, give fdisk access to more of the drive,
  516. * by correcting bios_cyls:
  517. */
  518. capacity = ide_gd_capacity(drive);
  519. if ((drive->dev_flags & IDE_DFLAG_FORCED_GEOM) == 0) {
  520. if (ata_id_lba48_enabled(drive->id)) {
  521. /* compatibility */
  522. drive->bios_sect = 63;
  523. drive->bios_head = 255;
  524. }
  525. if (drive->bios_sect && drive->bios_head) {
  526. unsigned int cap0 = capacity; /* truncate to 32 bits */
  527. unsigned int cylsz, cyl;
  528. if (cap0 != capacity)
  529. drive->bios_cyl = 65535;
  530. else {
  531. cylsz = drive->bios_sect * drive->bios_head;
  532. cyl = cap0 / cylsz;
  533. if (cyl > 65535)
  534. cyl = 65535;
  535. if (cyl > drive->bios_cyl)
  536. drive->bios_cyl = cyl;
  537. }
  538. }
  539. }
  540. printk(KERN_INFO "%s: %llu sectors (%llu MB)",
  541. drive->name, capacity, sectors_to_MB(capacity));
  542. /* Only print cache size when it was specified */
  543. if (id[ATA_ID_BUF_SIZE])
  544. printk(KERN_CONT " w/%dKiB Cache", id[ATA_ID_BUF_SIZE] / 2);
  545. printk(KERN_CONT ", CHS=%d/%d/%d\n",
  546. drive->bios_cyl, drive->bios_head, drive->bios_sect);
  547. /* write cache enabled? */
  548. if ((id[ATA_ID_CSFO] & 1) || ata_id_wcache_enabled(id))
  549. drive->dev_flags |= IDE_DFLAG_WCACHE;
  550. set_wcache(drive, 1);
  551. if ((drive->dev_flags & IDE_DFLAG_LBA) == 0 &&
  552. (drive->head == 0 || drive->head > 16)) {
  553. printk(KERN_ERR "%s: invalid geometry: %d physical heads?\n",
  554. drive->name, drive->head);
  555. drive->dev_flags &= ~IDE_DFLAG_ATTACH;
  556. } else
  557. drive->dev_flags |= IDE_DFLAG_ATTACH;
  558. }
  559. static void ide_disk_flush(ide_drive_t *drive)
  560. {
  561. if (ata_id_flush_enabled(drive->id) == 0 ||
  562. (drive->dev_flags & IDE_DFLAG_WCACHE) == 0)
  563. return;
  564. if (do_idedisk_flushcache(drive))
  565. printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
  566. }
  567. static int ide_disk_init_media(ide_drive_t *drive, struct gendisk *disk)
  568. {
  569. return 0;
  570. }
  571. static int ide_disk_set_doorlock(ide_drive_t *drive, struct gendisk *disk,
  572. int on)
  573. {
  574. struct ide_cmd cmd;
  575. int ret;
  576. if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
  577. return 0;
  578. memset(&cmd, 0, sizeof(cmd));
  579. cmd.tf.command = on ? ATA_CMD_MEDIA_LOCK : ATA_CMD_MEDIA_UNLOCK;
  580. cmd.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
  581. ret = ide_no_data_taskfile(drive, &cmd);
  582. if (ret)
  583. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  584. return ret;
  585. }
  586. const struct ide_disk_ops ide_ata_disk_ops = {
  587. .check = ide_disk_check,
  588. .get_capacity = ide_disk_get_capacity,
  589. .setup = ide_disk_setup,
  590. .flush = ide_disk_flush,
  591. .init_media = ide_disk_init_media,
  592. .set_doorlock = ide_disk_set_doorlock,
  593. .do_request = ide_do_rw_disk,
  594. .ioctl = ide_disk_ioctl,
  595. };