sm_smart.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2009-2013 Willem Dijkstra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. #include <sys/param.h>
  31. #include <sys/stat.h>
  32. #include <sys/types.h>
  33. #include <sys/ata.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <strings.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <camlib.h>
  41. #include <cam/cam_ccb.h>
  42. #include "conf.h"
  43. #include "data.h"
  44. #include "error.h"
  45. #include "xmalloc.h"
  46. #include "smart.h"
  47. #include "diskname.h"
  48. #ifdef HAS_IOCATAREQUEST
  49. #ifndef HAS_ATA_SMART_CMD
  50. #define ATA_SMART_CMD 0xb0
  51. #endif
  52. #define MSG_SIMPLE_Q_TAG 0x20
  53. /* per drive storage structure */
  54. struct smart_device {
  55. struct smart_values data;
  56. struct ata_ioc_request cmd;
  57. struct cam_device *cd;
  58. struct ccb_ataio ccb;
  59. char name[MAX_PATH_LEN];
  60. int fd;
  61. int type;
  62. int failed;
  63. };
  64. static struct smart_device *smart_devs = NULL;
  65. static int smart_cur = 0;
  66. void
  67. init_smart(struct stream *st)
  68. {
  69. struct disknamectx c;
  70. int fd;
  71. int i;
  72. char drivename[MAX_PATH_LEN];
  73. struct ata_ioc_request *pa;
  74. struct cam_device *cd;
  75. struct ccb_ataio *pc;
  76. if (sizeof(struct smart_values) != DISK_BLOCK_LEN) {
  77. fatal("smart: internal error: smart values structure is broken");
  78. }
  79. if (st->arg == NULL) {
  80. fatal("smart: need a <disk device|name> argument");
  81. }
  82. initdisknamectx(&c, st->arg, drivename, sizeof(drivename));
  83. fd = -1;
  84. cd = NULL;
  85. while (nextdiskname(&c)) {
  86. if ((cd = cam_open_device(drivename, O_RDWR)) != NULL)
  87. break;
  88. if ((fd = open(drivename, O_RDONLY, O_NONBLOCK)) != -1)
  89. break;
  90. }
  91. if ((fd < 0) && (cd == NULL))
  92. fatal("smart: cannot open '%.200s'", st->arg);
  93. /* look for drive in our global table */
  94. for (i = 0; i < smart_cur; i++) {
  95. if (strncmp(smart_devs[i].name, drivename, MAX_PATH_LEN) == 0) {
  96. st->parg.smart = i;
  97. return;
  98. }
  99. }
  100. /* this is a new drive; allocate the command and data block */
  101. if (smart_cur > SYMON_MAX_DOBJECTS) {
  102. fatal("%s:%d: dynamic object limit (%d) exceeded for smart data",
  103. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  104. }
  105. smart_devs = xrealloc(smart_devs, (smart_cur + 1) * sizeof(struct smart_device));
  106. bzero(&smart_devs[smart_cur], sizeof(struct smart_device));
  107. /* rewire all bufferlocations, as our addresses may have changed */
  108. for (i = 0; i <= smart_cur; i++) {
  109. smart_devs[i].cmd.data = (caddr_t)&smart_devs[i].data;
  110. smart_devs[i].ccb.data_ptr = (u_int8_t *)&smart_devs[i].data;
  111. }
  112. /* store drivename in new block */
  113. snprintf(smart_devs[smart_cur].name, MAX_PATH_LEN, "%s", drivename);
  114. /* store filedescriptor to device */
  115. if (fd > 0) {
  116. smart_devs[smart_cur].fd = fd;
  117. /* populate ata command header */
  118. pa = &smart_devs[smart_cur].cmd;
  119. pa->u.ata.command = ATA_SMART_CMD;
  120. pa->timeout = SMART_TIMEOUT;
  121. pa->u.ata.feature = ATA_SMART_READ_VALUES;
  122. pa->u.ata.lba = SMART_CYLINDER << 8;
  123. pa->flags = ATA_CMD_READ;
  124. pa->count = DISK_BLOCK_LEN;
  125. } else if (cd) {
  126. smart_devs[smart_cur].cd = cd;
  127. /* populate cam control block */
  128. pc = &smart_devs[smart_cur].ccb;
  129. cam_fill_ataio(pc,
  130. 0, /* retries */
  131. NULL, /* completion callback */
  132. CAM_DIR_IN, /* flags */
  133. MSG_SIMPLE_Q_TAG, /* tag_action */
  134. (u_int8_t *)&smart_devs[smart_cur].data,
  135. DISK_BLOCK_LEN,
  136. SMART_TIMEOUT); /* timeout (s) */
  137. /* disable queue freeze on error */
  138. pc->ccb_h.flags |= CAM_DEV_QFRZDIS;
  139. /* populate ata command header */
  140. pc->cmd.flags = CAM_ATAIO_NEEDRESULT;
  141. pc->cmd.command = ATA_SMART_CMD;
  142. pc->cmd.features = ATA_SMART_READ_VALUES;
  143. pc->cmd.lba_low = 0;
  144. pc->cmd.lba_mid = SMART_CYLINDER & 0xff;
  145. pc->cmd.lba_high = (SMART_CYLINDER >> 8) & 0xff;
  146. pc->cmd.lba_low_exp = 0;
  147. pc->cmd.lba_mid_exp = 0;
  148. pc->cmd.lba_high_exp = 0;
  149. pc->cmd.sector_count = 1;
  150. pc->cmd.sector_count_exp = 0;
  151. }
  152. /* store smart dev entry in stream to facilitate quick get */
  153. st->parg.smart = smart_cur;
  154. smart_cur++;
  155. info("started module smart(%.200s)", st->arg);
  156. }
  157. void
  158. gets_smart()
  159. {
  160. int i;
  161. for (i = 0; i < smart_cur; i++) {
  162. if (smart_devs[i].fd > 0) {
  163. if (ioctl(smart_devs[i].fd, IOCATAREQUEST, &smart_devs[i].cmd) || smart_devs[i].cmd.error) {
  164. warning("smart: ioctl for drive '%s' failed: %s",
  165. &smart_devs[i].name, strerror(errno));
  166. smart_devs[i].failed = 1;
  167. continue;
  168. }
  169. /* Some drives do not calculate the smart checksum correctly;
  170. * additional code that identifies these drives would increase our
  171. * footprint and the amount of datajuggling we need to do; we would
  172. * rather ignore the checksums.
  173. */
  174. smart_devs[i].failed = 0;
  175. } else if (smart_devs[i].cd != NULL) {
  176. if ((cam_send_ccb(smart_devs[i].cd, (union ccb *)&smart_devs[i].ccb) < 0) || smart_devs[i].ccb.res.error) {
  177. warning("smart: ccb for drive '%s' failed: %s",
  178. &smart_devs[i].name,
  179. cam_error_string(smart_devs[i].cd, (union ccb *)&smart_devs[i].ccb,
  180. (char *)&smart_devs[i].data, DISK_BLOCK_LEN,
  181. CAM_ESF_ALL, CAM_EPF_ALL));
  182. smart_devs[i].failed = 1;
  183. continue;
  184. }
  185. smart_devs[i].failed = 0;
  186. }
  187. }
  188. return;
  189. }
  190. int
  191. get_smart(char *symon_buf, int maxlen, struct stream *st)
  192. {
  193. struct smart_report sr;
  194. if ((st->parg.smart < smart_cur) &&
  195. (!smart_devs[st->parg.smart].failed))
  196. {
  197. smart_parse(&smart_devs[st->parg.smart].data, &sr);
  198. debug("%x %x %x %x %x %x %x %x %x %x %x %x",
  199. sr.read_error_rate,
  200. sr.reallocated_sectors,
  201. sr.spin_retries,
  202. sr.air_flow_temp,
  203. sr.temperature,
  204. sr.reallocations,
  205. sr.current_pending,
  206. sr.uncorrectables,
  207. sr.soft_read_error_rate,
  208. sr.g_sense_error_rate,
  209. sr.temperature2,
  210. sr.free_fall_protection);
  211. return snpack(symon_buf, maxlen, st->arg, MT_SMART,
  212. sr.read_error_rate,
  213. sr.reallocated_sectors,
  214. sr.spin_retries,
  215. sr.air_flow_temp,
  216. sr.temperature,
  217. sr.reallocations,
  218. sr.current_pending,
  219. sr.uncorrectables,
  220. sr.soft_read_error_rate,
  221. sr.g_sense_error_rate,
  222. sr.temperature2,
  223. sr.free_fall_protection);
  224. }
  225. return 0;
  226. }
  227. #else
  228. void
  229. init_smart(struct stream *st)
  230. {
  231. fatal("smart module not available");
  232. }
  233. void
  234. gets_smart()
  235. {
  236. fatal("smart module not available");
  237. }
  238. int
  239. get_smart(char *symon_buf, int maxlen, struct stream *st)
  240. {
  241. fatal("io module not available");
  242. /* NOT REACHED */
  243. return 0;
  244. }
  245. #endif