ide-disk.c 19 KB

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