ide-disk.c 18 KB

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