ata.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* $OpenBSD: ata.c,v 1.34 2015/03/14 03:38:47 jsg Exp $ */
  2. /* $NetBSD: ata.c,v 1.9 1999/04/15 09:41:09 bouyer Exp $ */
  3. /*
  4. * Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <sys/param.h>
  27. #include <sys/systm.h>
  28. #include <sys/kernel.h>
  29. #include <sys/file.h>
  30. #include <sys/stat.h>
  31. #include <sys/malloc.h>
  32. #include <sys/device.h>
  33. #include <sys/syslog.h>
  34. #include <sys/pool.h>
  35. #include <machine/bus.h>
  36. #include <dev/ata/atareg.h>
  37. #include <dev/ata/atavar.h>
  38. #include <dev/ic/wdcreg.h>
  39. #define DEBUG_FUNCS 0x08
  40. #define DEBUG_PROBE 0x10
  41. #ifdef WDCDEBUG
  42. extern int wdcdebug_mask; /* init'ed in wdc.c */
  43. #define WDCDEBUG_PRINT(args, level) do { \
  44. if ((wdcdebug_mask & (level)) != 0) \
  45. printf args; \
  46. } while (0)
  47. #else
  48. #define WDCDEBUG_PRINT(args, level)
  49. #endif
  50. #define ATAPARAMS_SIZE 512
  51. /* Get the disk's parameters */
  52. int
  53. ata_get_params(struct ata_drive_datas *drvp, u_int8_t flags,
  54. struct ataparams *prms)
  55. {
  56. char *tb;
  57. struct wdc_command wdc_c;
  58. int i;
  59. u_int16_t *p;
  60. int ret;
  61. WDCDEBUG_PRINT(("ata_get_params\n"), DEBUG_FUNCS);
  62. bzero(&wdc_c, sizeof(struct wdc_command));
  63. if (drvp->drive_flags & DRIVE_ATA) {
  64. wdc_c.r_command = WDCC_IDENTIFY;
  65. wdc_c.r_st_bmask = WDCS_DRDY;
  66. wdc_c.r_st_pmask = 0;
  67. wdc_c.timeout = 3000; /* 3s */
  68. } else if (drvp->drive_flags & DRIVE_ATAPI) {
  69. wdc_c.r_command = ATAPI_IDENTIFY_DEVICE;
  70. wdc_c.r_st_bmask = 0;
  71. wdc_c.r_st_pmask = 0;
  72. wdc_c.timeout = 10000; /* 10s */
  73. } else {
  74. WDCDEBUG_PRINT(("ata_get_params: no disks\n"),
  75. DEBUG_FUNCS|DEBUG_PROBE);
  76. return CMD_ERR;
  77. }
  78. tb = dma_alloc(ATAPARAMS_SIZE, PR_NOWAIT | PR_ZERO);
  79. if (tb == NULL)
  80. return CMD_AGAIN;
  81. wdc_c.flags = AT_READ | flags;
  82. wdc_c.data = tb;
  83. wdc_c.bcount = ATAPARAMS_SIZE;
  84. if ((ret = wdc_exec_command(drvp, &wdc_c)) != WDC_COMPLETE) {
  85. WDCDEBUG_PRINT(("%s: wdc_exec_command failed: %d\n",
  86. __func__, ret), DEBUG_PROBE);
  87. dma_free(tb, ATAPARAMS_SIZE);
  88. return CMD_AGAIN;
  89. }
  90. if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
  91. WDCDEBUG_PRINT(("%s: IDENTIFY failed: 0x%x\n", __func__,
  92. wdc_c.flags), DEBUG_PROBE);
  93. dma_free(tb, ATAPARAMS_SIZE);
  94. return CMD_ERR;
  95. } else {
  96. #if BYTE_ORDER == BIG_ENDIAN
  97. /* All the fields in the params structure are 16-bit
  98. integers except for the ID strings which are char
  99. strings. The 16-bit integers are currently in
  100. memory in little-endian, regardless of architecture.
  101. So, they need to be swapped on big-endian architectures
  102. before they are accessed through the ataparams structure.
  103. The swaps below avoid touching the char strings.
  104. */
  105. swap16_multi((u_int16_t *)tb, 10);
  106. swap16_multi((u_int16_t *)tb + 20, 3);
  107. swap16_multi((u_int16_t *)tb + 47, ATAPARAMS_SIZE / 2 - 47);
  108. #endif
  109. /* Read in parameter block. */
  110. bcopy(tb, prms, sizeof(struct ataparams));
  111. /*
  112. * Shuffle string byte order.
  113. * ATAPI Mitsumi and NEC drives don't need this.
  114. */
  115. if ((prms->atap_config & WDC_CFG_ATAPI_MASK) ==
  116. WDC_CFG_ATAPI &&
  117. ((prms->atap_model[0] == 'N' &&
  118. prms->atap_model[1] == 'E') ||
  119. (prms->atap_model[0] == 'F' &&
  120. prms->atap_model[1] == 'X'))) {
  121. dma_free(tb, ATAPARAMS_SIZE);
  122. return CMD_OK;
  123. }
  124. for (i = 0; i < sizeof(prms->atap_model); i += 2) {
  125. p = (u_short *)(prms->atap_model + i);
  126. *p = swap16(*p);
  127. }
  128. for (i = 0; i < sizeof(prms->atap_serial); i += 2) {
  129. p = (u_short *)(prms->atap_serial + i);
  130. *p = swap16(*p);
  131. }
  132. for (i = 0; i < sizeof(prms->atap_revision); i += 2) {
  133. p = (u_short *)(prms->atap_revision + i);
  134. *p = swap16(*p);
  135. }
  136. dma_free(tb, ATAPARAMS_SIZE);
  137. return CMD_OK;
  138. }
  139. }
  140. int
  141. ata_set_mode(struct ata_drive_datas *drvp, u_int8_t mode, u_int8_t flags)
  142. {
  143. struct wdc_command wdc_c;
  144. WDCDEBUG_PRINT(("%s: mode=0x%x, flags=0x%x\n", __func__,
  145. mode, flags), DEBUG_PROBE);
  146. bzero(&wdc_c, sizeof(struct wdc_command));
  147. wdc_c.r_command = SET_FEATURES;
  148. wdc_c.r_st_bmask = 0;
  149. wdc_c.r_st_pmask = 0;
  150. wdc_c.r_precomp = WDSF_SET_MODE;
  151. wdc_c.r_count = mode;
  152. wdc_c.flags = flags;
  153. wdc_c.timeout = 1000; /* 1s */
  154. if (wdc_exec_command(drvp, &wdc_c) != WDC_COMPLETE)
  155. return CMD_AGAIN;
  156. WDCDEBUG_PRINT(("%s: after wdc_exec_command() wdc_c.flags=0x%x\n",
  157. __func__, wdc_c.flags), DEBUG_PROBE);
  158. if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
  159. return CMD_ERR;
  160. }
  161. return CMD_OK;
  162. }
  163. void
  164. ata_dmaerr(struct ata_drive_datas *drvp)
  165. {
  166. /*
  167. * Downgrade decision: if we get NERRS_MAX in NXFER.
  168. * We start with n_dmaerrs set to NERRS_MAX-1 so that the
  169. * first error within the first NXFER ops will immediately trigger
  170. * a downgrade.
  171. * If we got an error and n_xfers is bigger than NXFER reset counters.
  172. */
  173. drvp->n_dmaerrs++;
  174. if (drvp->n_dmaerrs >= NERRS_MAX && drvp->n_xfers <= NXFER) {
  175. wdc_downgrade_mode(drvp);
  176. drvp->n_dmaerrs = NERRS_MAX-1;
  177. drvp->n_xfers = 0;
  178. return;
  179. }
  180. if (drvp->n_xfers > NXFER) {
  181. drvp->n_dmaerrs = 1; /* just got an error */
  182. drvp->n_xfers = 1; /* restart counting from this error */
  183. }
  184. }
  185. void
  186. ata_perror(struct ata_drive_datas *drvp, int errno, char *buf, size_t buf_len)
  187. {
  188. static char *errstr0_3[] = {"address mark not found",
  189. "track 0 not found", "aborted command", "media change requested",
  190. "id not found", "media changed", "uncorrectable data error",
  191. "bad block detected"};
  192. static char *errstr4_5[] = {"",
  193. "no media/write protected", "aborted command",
  194. "media change requested", "id not found", "media changed",
  195. "uncorrectable data error", "interface CRC error"};
  196. char **errstr;
  197. int i;
  198. char *sep = "";
  199. size_t len = 0;
  200. if (drvp->ata_vers >= 4)
  201. errstr = errstr4_5;
  202. else
  203. errstr = errstr0_3;
  204. if (errno == 0) {
  205. snprintf(buf, buf_len, "error not notified");
  206. }
  207. for (i = 0; i < 8; i++) {
  208. if (errno & (1 << i)) {
  209. snprintf(buf + len, buf_len - len, "%s%s", sep,
  210. errstr[i]);
  211. len = strlen(buf);
  212. sep = ", ";
  213. }
  214. }
  215. }