ide-disk.c 20 KB

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