scsicam.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * scsicam.c - SCSI CAM support functions, use for HDIO_GETGEO, etc.
  3. *
  4. * Copyright 1993, 1994 Drew Eckhardt
  5. * Visionary Computing
  6. * (Unix and Linux consulting and custom programming)
  7. * drew@Colorado.EDU
  8. * +1 (303) 786-7975
  9. *
  10. * For more information, please consult the SCSI-CAM draft.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/genhd.h>
  15. #include <linux/kernel.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/buffer_head.h>
  18. #include <asm/unaligned.h>
  19. #include <scsi/scsicam.h>
  20. static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
  21. unsigned int *secs);
  22. unsigned char *scsi_bios_ptable(struct block_device *dev)
  23. {
  24. unsigned char *res = kmalloc(66, GFP_KERNEL);
  25. if (res) {
  26. struct block_device *bdev = dev->bd_contains;
  27. Sector sect;
  28. void *data = read_dev_sector(bdev, 0, &sect);
  29. if (data) {
  30. memcpy(res, data + 0x1be, 66);
  31. put_dev_sector(sect);
  32. } else {
  33. kfree(res);
  34. res = NULL;
  35. }
  36. }
  37. return res;
  38. }
  39. EXPORT_SYMBOL(scsi_bios_ptable);
  40. /*
  41. * Function : int scsicam_bios_param (struct block_device *bdev, ector_t capacity, int *ip)
  42. *
  43. * Purpose : to determine the BIOS mapping used for a drive in a
  44. * SCSI-CAM system, storing the results in ip as required
  45. * by the HDIO_GETGEO ioctl().
  46. *
  47. * Returns : -1 on failure, 0 on success.
  48. *
  49. */
  50. int scsicam_bios_param(struct block_device *bdev, sector_t capacity, int *ip)
  51. {
  52. unsigned char *p;
  53. int ret;
  54. p = scsi_bios_ptable(bdev);
  55. if (!p)
  56. return -1;
  57. /* try to infer mapping from partition table */
  58. ret = scsi_partsize(p, (unsigned long)capacity, (unsigned int *)ip + 2,
  59. (unsigned int *)ip + 0, (unsigned int *)ip + 1);
  60. kfree(p);
  61. if (ret == -1) {
  62. /* pick some standard mapping with at most 1024 cylinders,
  63. and at most 62 sectors per track - this works up to
  64. 7905 MB */
  65. ret = setsize((unsigned long)capacity, (unsigned int *)ip + 2,
  66. (unsigned int *)ip + 0, (unsigned int *)ip + 1);
  67. }
  68. /* if something went wrong, then apparently we have to return
  69. a geometry with more than 1024 cylinders */
  70. if (ret || ip[0] > 255 || ip[1] > 63) {
  71. if ((capacity >> 11) > 65534) {
  72. ip[0] = 255;
  73. ip[1] = 63;
  74. } else {
  75. ip[0] = 64;
  76. ip[1] = 32;
  77. }
  78. if (capacity > 65535*63*255)
  79. ip[2] = 65535;
  80. else
  81. ip[2] = (unsigned long)capacity / (ip[0] * ip[1]);
  82. }
  83. return 0;
  84. }
  85. EXPORT_SYMBOL(scsicam_bios_param);
  86. /*
  87. * Function : static int scsi_partsize(unsigned char *buf, unsigned long
  88. * capacity,unsigned int *cyls, unsigned int *hds, unsigned int *secs);
  89. *
  90. * Purpose : to determine the BIOS mapping used to create the partition
  91. * table, storing the results in *cyls, *hds, and *secs
  92. *
  93. * Returns : -1 on failure, 0 on success.
  94. *
  95. */
  96. int scsi_partsize(unsigned char *buf, unsigned long capacity,
  97. unsigned int *cyls, unsigned int *hds, unsigned int *secs)
  98. {
  99. struct partition *p = (struct partition *)buf, *largest = NULL;
  100. int i, largest_cyl;
  101. int cyl, ext_cyl, end_head, end_cyl, end_sector;
  102. unsigned int logical_end, physical_end, ext_physical_end;
  103. if (*(unsigned short *) (buf + 64) == 0xAA55) {
  104. for (largest_cyl = -1, i = 0; i < 4; ++i, ++p) {
  105. if (!p->sys_ind)
  106. continue;
  107. #ifdef DEBUG
  108. printk("scsicam_bios_param : partition %d has system \n",
  109. i);
  110. #endif
  111. cyl = p->cyl + ((p->sector & 0xc0) << 2);
  112. if (cyl > largest_cyl) {
  113. largest_cyl = cyl;
  114. largest = p;
  115. }
  116. }
  117. }
  118. if (largest) {
  119. end_cyl = largest->end_cyl + ((largest->end_sector & 0xc0) << 2);
  120. end_head = largest->end_head;
  121. end_sector = largest->end_sector & 0x3f;
  122. if (end_head + 1 == 0 || end_sector == 0)
  123. return -1;
  124. #ifdef DEBUG
  125. printk("scsicam_bios_param : end at h = %d, c = %d, s = %d\n",
  126. end_head, end_cyl, end_sector);
  127. #endif
  128. physical_end = end_cyl * (end_head + 1) * end_sector +
  129. end_head * end_sector + end_sector;
  130. /* This is the actual _sector_ number at the end */
  131. logical_end = get_unaligned(&largest->start_sect)
  132. + get_unaligned(&largest->nr_sects);
  133. /* This is for >1023 cylinders */
  134. ext_cyl = (logical_end - (end_head * end_sector + end_sector))
  135. / (end_head + 1) / end_sector;
  136. ext_physical_end = ext_cyl * (end_head + 1) * end_sector +
  137. end_head * end_sector + end_sector;
  138. #ifdef DEBUG
  139. printk("scsicam_bios_param : logical_end=%d physical_end=%d ext_physical_end=%d ext_cyl=%d\n"
  140. ,logical_end, physical_end, ext_physical_end, ext_cyl);
  141. #endif
  142. if ((logical_end == physical_end) ||
  143. (end_cyl == 1023 && ext_physical_end == logical_end)) {
  144. *secs = end_sector;
  145. *hds = end_head + 1;
  146. *cyls = capacity / ((end_head + 1) * end_sector);
  147. return 0;
  148. }
  149. #ifdef DEBUG
  150. printk("scsicam_bios_param : logical (%u) != physical (%u)\n",
  151. logical_end, physical_end);
  152. #endif
  153. }
  154. return -1;
  155. }
  156. EXPORT_SYMBOL(scsi_partsize);
  157. /*
  158. * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
  159. * unsigned int *hds, unsigned int *secs);
  160. *
  161. * Purpose : to determine a near-optimal int 0x13 mapping for a
  162. * SCSI disk in terms of lost space of size capacity, storing
  163. * the results in *cyls, *hds, and *secs.
  164. *
  165. * Returns : -1 on failure, 0 on success.
  166. *
  167. * Extracted from
  168. *
  169. * WORKING X3T9.2
  170. * DRAFT 792D
  171. *
  172. *
  173. * Revision 6
  174. * 10-MAR-94
  175. * Information technology -
  176. * SCSI-2 Common access method
  177. * transport and SCSI interface module
  178. *
  179. * ANNEX A :
  180. *
  181. * setsize() converts a read capacity value to int 13h
  182. * head-cylinder-sector requirements. It minimizes the value for
  183. * number of heads and maximizes the number of cylinders. This
  184. * will support rather large disks before the number of heads
  185. * will not fit in 4 bits (or 6 bits). This algorithm also
  186. * minimizes the number of sectors that will be unused at the end
  187. * of the disk while allowing for very large disks to be
  188. * accommodated. This algorithm does not use physical geometry.
  189. */
  190. static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
  191. unsigned int *secs)
  192. {
  193. unsigned int rv = 0;
  194. unsigned long heads, sectors, cylinders, temp;
  195. cylinders = 1024L; /* Set number of cylinders to max */
  196. sectors = 62L; /* Maximize sectors per track */
  197. temp = cylinders * sectors; /* Compute divisor for heads */
  198. heads = capacity / temp; /* Compute value for number of heads */
  199. if (capacity % temp) { /* If no remainder, done! */
  200. heads++; /* Else, increment number of heads */
  201. temp = cylinders * heads; /* Compute divisor for sectors */
  202. sectors = capacity / temp; /* Compute value for sectors per
  203. track */
  204. if (capacity % temp) { /* If no remainder, done! */
  205. sectors++; /* Else, increment number of sectors */
  206. temp = heads * sectors; /* Compute divisor for cylinders */
  207. cylinders = capacity / temp; /* Compute number of cylinders */
  208. }
  209. }
  210. if (cylinders == 0)
  211. rv = (unsigned) -1; /* Give error if 0 cylinders */
  212. *cyls = (unsigned int) cylinders; /* Stuff return values */
  213. *secs = (unsigned int) sectors;
  214. *hds = (unsigned int) heads;
  215. return (rv);
  216. }