scsicam.c 7.7 KB

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