scsicam.c 7.6 KB

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