scsicam.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. u64 capacity64 = capacity; /* Suppress gcc warning */
  54. int ret;
  55. p = scsi_bios_ptable(bdev);
  56. if (!p)
  57. return -1;
  58. /* try to infer mapping from partition table */
  59. ret = scsi_partsize(p, (unsigned long)capacity, (unsigned int *)ip + 2,
  60. (unsigned int *)ip + 0, (unsigned int *)ip + 1);
  61. kfree(p);
  62. if (ret == -1 && capacity64 < (1ULL << 32)) {
  63. /* pick some standard mapping with at most 1024 cylinders,
  64. and at most 62 sectors per track - this works up to
  65. 7905 MB */
  66. ret = setsize((unsigned long)capacity, (unsigned int *)ip + 2,
  67. (unsigned int *)ip + 0, (unsigned int *)ip + 1);
  68. }
  69. /* if something went wrong, then apparently we have to return
  70. a geometry with more than 1024 cylinders */
  71. if (ret || ip[0] > 255 || ip[1] > 63) {
  72. if ((capacity >> 11) > 65534) {
  73. ip[0] = 255;
  74. ip[1] = 63;
  75. } else {
  76. ip[0] = 64;
  77. ip[1] = 32;
  78. }
  79. if (capacity > 65535*63*255)
  80. ip[2] = 65535;
  81. else
  82. ip[2] = (unsigned long)capacity / (ip[0] * ip[1]);
  83. }
  84. return 0;
  85. }
  86. EXPORT_SYMBOL(scsicam_bios_param);
  87. /*
  88. * Function : static int scsi_partsize(unsigned char *buf, unsigned long
  89. * capacity,unsigned int *cyls, unsigned int *hds, unsigned int *secs);
  90. *
  91. * Purpose : to determine the BIOS mapping used to create the partition
  92. * table, storing the results in *cyls, *hds, and *secs
  93. *
  94. * Returns : -1 on failure, 0 on success.
  95. *
  96. */
  97. int scsi_partsize(unsigned char *buf, unsigned long capacity,
  98. unsigned int *cyls, unsigned int *hds, unsigned int *secs)
  99. {
  100. struct partition *p = (struct partition *)buf, *largest = NULL;
  101. int i, largest_cyl;
  102. int cyl, ext_cyl, end_head, end_cyl, end_sector;
  103. unsigned int logical_end, physical_end, ext_physical_end;
  104. if (*(unsigned short *) (buf + 64) == 0xAA55) {
  105. for (largest_cyl = -1, i = 0; i < 4; ++i, ++p) {
  106. if (!p->sys_ind)
  107. continue;
  108. #ifdef DEBUG
  109. printk("scsicam_bios_param : partition %d has system \n",
  110. i);
  111. #endif
  112. cyl = p->cyl + ((p->sector & 0xc0) << 2);
  113. if (cyl > largest_cyl) {
  114. largest_cyl = cyl;
  115. largest = p;
  116. }
  117. }
  118. }
  119. if (largest) {
  120. end_cyl = largest->end_cyl + ((largest->end_sector & 0xc0) << 2);
  121. end_head = largest->end_head;
  122. end_sector = largest->end_sector & 0x3f;
  123. if (end_head + 1 == 0 || end_sector == 0)
  124. return -1;
  125. #ifdef DEBUG
  126. printk("scsicam_bios_param : end at h = %d, c = %d, s = %d\n",
  127. end_head, end_cyl, end_sector);
  128. #endif
  129. physical_end = end_cyl * (end_head + 1) * end_sector +
  130. end_head * end_sector + end_sector;
  131. /* This is the actual _sector_ number at the end */
  132. logical_end = get_unaligned(&largest->start_sect)
  133. + get_unaligned(&largest->nr_sects);
  134. /* This is for >1023 cylinders */
  135. ext_cyl = (logical_end - (end_head * end_sector + end_sector))
  136. / (end_head + 1) / end_sector;
  137. ext_physical_end = ext_cyl * (end_head + 1) * end_sector +
  138. end_head * end_sector + end_sector;
  139. #ifdef DEBUG
  140. printk("scsicam_bios_param : logical_end=%d physical_end=%d ext_physical_end=%d ext_cyl=%d\n"
  141. ,logical_end, physical_end, ext_physical_end, ext_cyl);
  142. #endif
  143. if ((logical_end == physical_end) ||
  144. (end_cyl == 1023 && ext_physical_end == logical_end)) {
  145. *secs = end_sector;
  146. *hds = end_head + 1;
  147. *cyls = capacity / ((end_head + 1) * end_sector);
  148. return 0;
  149. }
  150. #ifdef DEBUG
  151. printk("scsicam_bios_param : logical (%u) != physical (%u)\n",
  152. logical_end, physical_end);
  153. #endif
  154. }
  155. return -1;
  156. }
  157. EXPORT_SYMBOL(scsi_partsize);
  158. /*
  159. * Function : static int setsize(unsigned long capacity,unsigned int *cyls,
  160. * unsigned int *hds, unsigned int *secs);
  161. *
  162. * Purpose : to determine a near-optimal int 0x13 mapping for a
  163. * SCSI disk in terms of lost space of size capacity, storing
  164. * the results in *cyls, *hds, and *secs.
  165. *
  166. * Returns : -1 on failure, 0 on success.
  167. *
  168. * Extracted from
  169. *
  170. * WORKING X3T9.2
  171. * DRAFT 792D
  172. *
  173. *
  174. * Revision 6
  175. * 10-MAR-94
  176. * Information technology -
  177. * SCSI-2 Common access method
  178. * transport and SCSI interface module
  179. *
  180. * ANNEX A :
  181. *
  182. * setsize() converts a read capacity value to int 13h
  183. * head-cylinder-sector requirements. It minimizes the value for
  184. * number of heads and maximizes the number of cylinders. This
  185. * will support rather large disks before the number of heads
  186. * will not fit in 4 bits (or 6 bits). This algorithm also
  187. * minimizes the number of sectors that will be unused at the end
  188. * of the disk while allowing for very large disks to be
  189. * accommodated. This algorithm does not use physical geometry.
  190. */
  191. static int setsize(unsigned long capacity, unsigned int *cyls, unsigned int *hds,
  192. unsigned int *secs)
  193. {
  194. unsigned int rv = 0;
  195. unsigned long heads, sectors, cylinders, temp;
  196. cylinders = 1024L; /* Set number of cylinders to max */
  197. sectors = 62L; /* Maximize sectors per track */
  198. temp = cylinders * sectors; /* Compute divisor for heads */
  199. heads = capacity / temp; /* Compute value for number of heads */
  200. if (capacity % temp) { /* If no remainder, done! */
  201. heads++; /* Else, increment number of heads */
  202. temp = cylinders * heads; /* Compute divisor for sectors */
  203. sectors = capacity / temp; /* Compute value for sectors per
  204. track */
  205. if (capacity % temp) { /* If no remainder, done! */
  206. sectors++; /* Else, increment number of sectors */
  207. temp = heads * sectors; /* Compute divisor for cylinders */
  208. cylinders = capacity / temp; /* Compute number of cylinders */
  209. }
  210. }
  211. if (cylinders == 0)
  212. rv = (unsigned) -1; /* Give error if 0 cylinders */
  213. *cyls = (unsigned int) cylinders; /* Stuff return values */
  214. *secs = (unsigned int) sectors;
  215. *hds = (unsigned int) heads;
  216. return (rv);
  217. }