scsicam.c 7.8 KB

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