sd.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835
  1. /*
  2. * sd.c Copyright (C) 1992 Drew Eckhardt
  3. * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
  4. *
  5. * Linux scsi disk driver
  6. * Initial versions: Drew Eckhardt
  7. * Subsequent revisions: Eric Youngdale
  8. * Modification history:
  9. * - Drew Eckhardt <drew@colorado.edu> original
  10. * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
  11. * outstanding request, and other enhancements.
  12. * Support loadable low-level scsi drivers.
  13. * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
  14. * eight major numbers.
  15. * - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
  16. * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
  17. * sd_init and cleanups.
  18. * - Alex Davis <letmein@erols.com> Fix problem where partition info
  19. * not being read in sd_open. Fix problem where removable media
  20. * could be ejected after sd_open.
  21. * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
  22. * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox
  23. * <willy@debian.org>, Kurt Garloff <garloff@suse.de>:
  24. * Support 32k/1M disks.
  25. *
  26. * Logging policy (needs CONFIG_SCSI_LOGGING defined):
  27. * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
  28. * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
  29. * - entering sd_ioctl: SCSI_LOG_IOCTL level 1
  30. * - entering other commands: SCSI_LOG_HLQUEUE level 3
  31. * Note: when the logging level is set by the user, it must be greater
  32. * than the level indicated above to trigger output.
  33. */
  34. #include <linux/config.h>
  35. #include <linux/module.h>
  36. #include <linux/fs.h>
  37. #include <linux/kernel.h>
  38. #include <linux/sched.h>
  39. #include <linux/mm.h>
  40. #include <linux/bio.h>
  41. #include <linux/genhd.h>
  42. #include <linux/hdreg.h>
  43. #include <linux/errno.h>
  44. #include <linux/idr.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/init.h>
  47. #include <linux/blkdev.h>
  48. #include <linux/blkpg.h>
  49. #include <linux/delay.h>
  50. #include <linux/mutex.h>
  51. #include <asm/uaccess.h>
  52. #include <scsi/scsi.h>
  53. #include <scsi/scsi_cmnd.h>
  54. #include <scsi/scsi_dbg.h>
  55. #include <scsi/scsi_device.h>
  56. #include <scsi/scsi_driver.h>
  57. #include <scsi/scsi_eh.h>
  58. #include <scsi/scsi_host.h>
  59. #include <scsi/scsi_ioctl.h>
  60. #include <scsi/scsicam.h>
  61. #include "scsi_logging.h"
  62. /*
  63. * More than enough for everybody ;) The huge number of majors
  64. * is a leftover from 16bit dev_t days, we don't really need that
  65. * much numberspace.
  66. */
  67. #define SD_MAJORS 16
  68. MODULE_AUTHOR("Eric Youngdale");
  69. MODULE_DESCRIPTION("SCSI disk (sd) driver");
  70. MODULE_LICENSE("GPL");
  71. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
  72. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
  73. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
  74. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
  75. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
  76. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
  77. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
  78. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
  79. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
  80. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
  81. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
  82. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
  83. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
  84. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
  85. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
  86. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
  87. /*
  88. * This is limited by the naming scheme enforced in sd_probe,
  89. * add another character to it if you really need more disks.
  90. */
  91. #define SD_MAX_DISKS (((26 * 26) + 26 + 1) * 26)
  92. /*
  93. * Time out in seconds for disks and Magneto-opticals (which are slower).
  94. */
  95. #define SD_TIMEOUT (30 * HZ)
  96. #define SD_MOD_TIMEOUT (75 * HZ)
  97. /*
  98. * Number of allowed retries
  99. */
  100. #define SD_MAX_RETRIES 5
  101. #define SD_PASSTHROUGH_RETRIES 1
  102. /*
  103. * Size of the initial data buffer for mode and read capacity data
  104. */
  105. #define SD_BUF_SIZE 512
  106. struct scsi_disk {
  107. struct scsi_driver *driver; /* always &sd_template */
  108. struct scsi_device *device;
  109. struct class_device cdev;
  110. struct gendisk *disk;
  111. unsigned int openers; /* protected by BKL for now, yuck */
  112. sector_t capacity; /* size in 512-byte sectors */
  113. u32 index;
  114. u8 media_present;
  115. u8 write_prot;
  116. unsigned WCE : 1; /* state of disk WCE bit */
  117. unsigned RCD : 1; /* state of disk RCD bit, unused */
  118. unsigned DPOFUA : 1; /* state of disk DPOFUA bit */
  119. };
  120. #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,cdev)
  121. static DEFINE_IDR(sd_index_idr);
  122. static DEFINE_SPINLOCK(sd_index_lock);
  123. /* This semaphore is used to mediate the 0->1 reference get in the
  124. * face of object destruction (i.e. we can't allow a get on an
  125. * object after last put) */
  126. static DEFINE_MUTEX(sd_ref_mutex);
  127. static int sd_revalidate_disk(struct gendisk *disk);
  128. static void sd_rw_intr(struct scsi_cmnd * SCpnt);
  129. static int sd_probe(struct device *);
  130. static int sd_remove(struct device *);
  131. static void sd_shutdown(struct device *dev);
  132. static void sd_rescan(struct device *);
  133. static int sd_init_command(struct scsi_cmnd *);
  134. static int sd_issue_flush(struct device *, sector_t *);
  135. static void sd_prepare_flush(request_queue_t *, struct request *);
  136. static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
  137. unsigned char *buffer);
  138. static void scsi_disk_release(struct class_device *cdev);
  139. static const char *sd_cache_types[] = {
  140. "write through", "none", "write back",
  141. "write back, no read (daft)"
  142. };
  143. static ssize_t sd_store_cache_type(struct class_device *cdev, const char *buf,
  144. size_t count)
  145. {
  146. int i, ct = -1, rcd, wce, sp;
  147. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  148. struct scsi_device *sdp = sdkp->device;
  149. char buffer[64];
  150. char *buffer_data;
  151. struct scsi_mode_data data;
  152. struct scsi_sense_hdr sshdr;
  153. int len;
  154. if (sdp->type != TYPE_DISK)
  155. /* no cache control on RBC devices; theoretically they
  156. * can do it, but there's probably so many exceptions
  157. * it's not worth the risk */
  158. return -EINVAL;
  159. for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
  160. const int len = strlen(sd_cache_types[i]);
  161. if (strncmp(sd_cache_types[i], buf, len) == 0 &&
  162. buf[len] == '\n') {
  163. ct = i;
  164. break;
  165. }
  166. }
  167. if (ct < 0)
  168. return -EINVAL;
  169. rcd = ct & 0x01 ? 1 : 0;
  170. wce = ct & 0x02 ? 1 : 0;
  171. if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
  172. SD_MAX_RETRIES, &data, NULL))
  173. return -EINVAL;
  174. len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
  175. data.block_descriptor_length);
  176. buffer_data = buffer + data.header_length +
  177. data.block_descriptor_length;
  178. buffer_data[2] &= ~0x05;
  179. buffer_data[2] |= wce << 2 | rcd;
  180. sp = buffer_data[0] & 0x80 ? 1 : 0;
  181. if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
  182. SD_MAX_RETRIES, &data, &sshdr)) {
  183. if (scsi_sense_valid(&sshdr))
  184. scsi_print_sense_hdr(sdkp->disk->disk_name, &sshdr);
  185. return -EINVAL;
  186. }
  187. sd_revalidate_disk(sdkp->disk);
  188. return count;
  189. }
  190. static ssize_t sd_store_allow_restart(struct class_device *cdev, const char *buf,
  191. size_t count)
  192. {
  193. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  194. struct scsi_device *sdp = sdkp->device;
  195. if (!capable(CAP_SYS_ADMIN))
  196. return -EACCES;
  197. if (sdp->type != TYPE_DISK)
  198. return -EINVAL;
  199. sdp->allow_restart = simple_strtoul(buf, NULL, 10);
  200. return count;
  201. }
  202. static ssize_t sd_show_cache_type(struct class_device *cdev, char *buf)
  203. {
  204. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  205. int ct = sdkp->RCD + 2*sdkp->WCE;
  206. return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
  207. }
  208. static ssize_t sd_show_fua(struct class_device *cdev, char *buf)
  209. {
  210. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  211. return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
  212. }
  213. static ssize_t sd_show_allow_restart(struct class_device *cdev, char *buf)
  214. {
  215. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  216. return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
  217. }
  218. static struct class_device_attribute sd_disk_attrs[] = {
  219. __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
  220. sd_store_cache_type),
  221. __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
  222. __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
  223. sd_store_allow_restart),
  224. __ATTR_NULL,
  225. };
  226. static struct class sd_disk_class = {
  227. .name = "scsi_disk",
  228. .owner = THIS_MODULE,
  229. .release = scsi_disk_release,
  230. .class_dev_attrs = sd_disk_attrs,
  231. };
  232. static struct scsi_driver sd_template = {
  233. .owner = THIS_MODULE,
  234. .gendrv = {
  235. .name = "sd",
  236. .probe = sd_probe,
  237. .remove = sd_remove,
  238. .shutdown = sd_shutdown,
  239. },
  240. .rescan = sd_rescan,
  241. .init_command = sd_init_command,
  242. .issue_flush = sd_issue_flush,
  243. };
  244. /*
  245. * Device no to disk mapping:
  246. *
  247. * major disc2 disc p1
  248. * |............|.............|....|....| <- dev_t
  249. * 31 20 19 8 7 4 3 0
  250. *
  251. * Inside a major, we have 16k disks, however mapped non-
  252. * contiguously. The first 16 disks are for major0, the next
  253. * ones with major1, ... Disk 256 is for major0 again, disk 272
  254. * for major1, ...
  255. * As we stay compatible with our numbering scheme, we can reuse
  256. * the well-know SCSI majors 8, 65--71, 136--143.
  257. */
  258. static int sd_major(int major_idx)
  259. {
  260. switch (major_idx) {
  261. case 0:
  262. return SCSI_DISK0_MAJOR;
  263. case 1 ... 7:
  264. return SCSI_DISK1_MAJOR + major_idx - 1;
  265. case 8 ... 15:
  266. return SCSI_DISK8_MAJOR + major_idx - 8;
  267. default:
  268. BUG();
  269. return 0; /* shut up gcc */
  270. }
  271. }
  272. static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
  273. {
  274. return container_of(disk->private_data, struct scsi_disk, driver);
  275. }
  276. static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
  277. {
  278. struct scsi_disk *sdkp = NULL;
  279. if (disk->private_data) {
  280. sdkp = scsi_disk(disk);
  281. if (scsi_device_get(sdkp->device) == 0)
  282. class_device_get(&sdkp->cdev);
  283. else
  284. sdkp = NULL;
  285. }
  286. return sdkp;
  287. }
  288. static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
  289. {
  290. struct scsi_disk *sdkp;
  291. mutex_lock(&sd_ref_mutex);
  292. sdkp = __scsi_disk_get(disk);
  293. mutex_unlock(&sd_ref_mutex);
  294. return sdkp;
  295. }
  296. static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
  297. {
  298. struct scsi_disk *sdkp;
  299. mutex_lock(&sd_ref_mutex);
  300. sdkp = dev_get_drvdata(dev);
  301. if (sdkp)
  302. sdkp = __scsi_disk_get(sdkp->disk);
  303. mutex_unlock(&sd_ref_mutex);
  304. return sdkp;
  305. }
  306. static void scsi_disk_put(struct scsi_disk *sdkp)
  307. {
  308. struct scsi_device *sdev = sdkp->device;
  309. mutex_lock(&sd_ref_mutex);
  310. class_device_put(&sdkp->cdev);
  311. scsi_device_put(sdev);
  312. mutex_unlock(&sd_ref_mutex);
  313. }
  314. /**
  315. * sd_init_command - build a scsi (read or write) command from
  316. * information in the request structure.
  317. * @SCpnt: pointer to mid-level's per scsi command structure that
  318. * contains request and into which the scsi command is written
  319. *
  320. * Returns 1 if successful and 0 if error (or cannot be done now).
  321. **/
  322. static int sd_init_command(struct scsi_cmnd * SCpnt)
  323. {
  324. struct scsi_device *sdp = SCpnt->device;
  325. struct request *rq = SCpnt->request;
  326. struct gendisk *disk = rq->rq_disk;
  327. sector_t block = rq->sector;
  328. unsigned int this_count = SCpnt->request_bufflen >> 9;
  329. unsigned int timeout = sdp->timeout;
  330. SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
  331. "count=%d\n", disk->disk_name,
  332. (unsigned long long)block, this_count));
  333. if (!sdp || !scsi_device_online(sdp) ||
  334. block + rq->nr_sectors > get_capacity(disk)) {
  335. SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
  336. rq->nr_sectors));
  337. SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
  338. return 0;
  339. }
  340. if (sdp->changed) {
  341. /*
  342. * quietly refuse to do anything to a changed disc until
  343. * the changed bit has been reset
  344. */
  345. /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
  346. return 0;
  347. }
  348. SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
  349. disk->disk_name, (unsigned long long)block));
  350. /*
  351. * If we have a 1K hardware sectorsize, prevent access to single
  352. * 512 byte sectors. In theory we could handle this - in fact
  353. * the scsi cdrom driver must be able to handle this because
  354. * we typically use 1K blocksizes, and cdroms typically have
  355. * 2K hardware sectorsizes. Of course, things are simpler
  356. * with the cdrom, since it is read-only. For performance
  357. * reasons, the filesystems should be able to handle this
  358. * and not force the scsi disk driver to use bounce buffers
  359. * for this.
  360. */
  361. if (sdp->sector_size == 1024) {
  362. if ((block & 1) || (rq->nr_sectors & 1)) {
  363. printk(KERN_ERR "sd: Bad block number requested");
  364. return 0;
  365. } else {
  366. block = block >> 1;
  367. this_count = this_count >> 1;
  368. }
  369. }
  370. if (sdp->sector_size == 2048) {
  371. if ((block & 3) || (rq->nr_sectors & 3)) {
  372. printk(KERN_ERR "sd: Bad block number requested");
  373. return 0;
  374. } else {
  375. block = block >> 2;
  376. this_count = this_count >> 2;
  377. }
  378. }
  379. if (sdp->sector_size == 4096) {
  380. if ((block & 7) || (rq->nr_sectors & 7)) {
  381. printk(KERN_ERR "sd: Bad block number requested");
  382. return 0;
  383. } else {
  384. block = block >> 3;
  385. this_count = this_count >> 3;
  386. }
  387. }
  388. if (rq_data_dir(rq) == WRITE) {
  389. if (!sdp->writeable) {
  390. return 0;
  391. }
  392. SCpnt->cmnd[0] = WRITE_6;
  393. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  394. } else if (rq_data_dir(rq) == READ) {
  395. SCpnt->cmnd[0] = READ_6;
  396. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  397. } else {
  398. printk(KERN_ERR "sd: Unknown command %lx\n", rq->flags);
  399. /* overkill panic("Unknown sd command %lx\n", rq->flags); */
  400. return 0;
  401. }
  402. SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
  403. disk->disk_name, (rq_data_dir(rq) == WRITE) ?
  404. "writing" : "reading", this_count, rq->nr_sectors));
  405. SCpnt->cmnd[1] = 0;
  406. if (block > 0xffffffff) {
  407. SCpnt->cmnd[0] += READ_16 - READ_6;
  408. SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
  409. SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  410. SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  411. SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  412. SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  413. SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
  414. SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
  415. SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
  416. SCpnt->cmnd[9] = (unsigned char) block & 0xff;
  417. SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
  418. SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
  419. SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
  420. SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
  421. SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
  422. } else if ((this_count > 0xff) || (block > 0x1fffff) ||
  423. SCpnt->device->use_10_for_rw) {
  424. if (this_count > 0xffff)
  425. this_count = 0xffff;
  426. SCpnt->cmnd[0] += READ_10 - READ_6;
  427. SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
  428. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  429. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  430. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  431. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  432. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  433. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  434. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  435. } else {
  436. if (unlikely(blk_fua_rq(rq))) {
  437. /*
  438. * This happens only if this drive failed
  439. * 10byte rw command with ILLEGAL_REQUEST
  440. * during operation and thus turned off
  441. * use_10_for_rw.
  442. */
  443. printk(KERN_ERR "sd: FUA write on READ/WRITE(6) drive\n");
  444. return 0;
  445. }
  446. SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  447. SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
  448. SCpnt->cmnd[3] = (unsigned char) block & 0xff;
  449. SCpnt->cmnd[4] = (unsigned char) this_count;
  450. SCpnt->cmnd[5] = 0;
  451. }
  452. SCpnt->request_bufflen = SCpnt->bufflen =
  453. this_count * sdp->sector_size;
  454. /*
  455. * We shouldn't disconnect in the middle of a sector, so with a dumb
  456. * host adapter, it's safe to assume that we can at least transfer
  457. * this many bytes between each connect / disconnect.
  458. */
  459. SCpnt->transfersize = sdp->sector_size;
  460. SCpnt->underflow = this_count << 9;
  461. SCpnt->allowed = SD_MAX_RETRIES;
  462. SCpnt->timeout_per_command = timeout;
  463. /*
  464. * This is the completion routine we use. This is matched in terms
  465. * of capability to this function.
  466. */
  467. SCpnt->done = sd_rw_intr;
  468. /*
  469. * This indicates that the command is ready from our end to be
  470. * queued.
  471. */
  472. return 1;
  473. }
  474. /**
  475. * sd_open - open a scsi disk device
  476. * @inode: only i_rdev member may be used
  477. * @filp: only f_mode and f_flags may be used
  478. *
  479. * Returns 0 if successful. Returns a negated errno value in case
  480. * of error.
  481. *
  482. * Note: This can be called from a user context (e.g. fsck(1) )
  483. * or from within the kernel (e.g. as a result of a mount(1) ).
  484. * In the latter case @inode and @filp carry an abridged amount
  485. * of information as noted above.
  486. **/
  487. static int sd_open(struct inode *inode, struct file *filp)
  488. {
  489. struct gendisk *disk = inode->i_bdev->bd_disk;
  490. struct scsi_disk *sdkp;
  491. struct scsi_device *sdev;
  492. int retval;
  493. if (!(sdkp = scsi_disk_get(disk)))
  494. return -ENXIO;
  495. SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name));
  496. sdev = sdkp->device;
  497. /*
  498. * If the device is in error recovery, wait until it is done.
  499. * If the device is offline, then disallow any access to it.
  500. */
  501. retval = -ENXIO;
  502. if (!scsi_block_when_processing_errors(sdev))
  503. goto error_out;
  504. if (sdev->removable || sdkp->write_prot)
  505. check_disk_change(inode->i_bdev);
  506. /*
  507. * If the drive is empty, just let the open fail.
  508. */
  509. retval = -ENOMEDIUM;
  510. if (sdev->removable && !sdkp->media_present &&
  511. !(filp->f_flags & O_NDELAY))
  512. goto error_out;
  513. /*
  514. * If the device has the write protect tab set, have the open fail
  515. * if the user expects to be able to write to the thing.
  516. */
  517. retval = -EROFS;
  518. if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
  519. goto error_out;
  520. /*
  521. * It is possible that the disk changing stuff resulted in
  522. * the device being taken offline. If this is the case,
  523. * report this to the user, and don't pretend that the
  524. * open actually succeeded.
  525. */
  526. retval = -ENXIO;
  527. if (!scsi_device_online(sdev))
  528. goto error_out;
  529. if (!sdkp->openers++ && sdev->removable) {
  530. if (scsi_block_when_processing_errors(sdev))
  531. scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  532. }
  533. return 0;
  534. error_out:
  535. scsi_disk_put(sdkp);
  536. return retval;
  537. }
  538. /**
  539. * sd_release - invoked when the (last) close(2) is called on this
  540. * scsi disk.
  541. * @inode: only i_rdev member may be used
  542. * @filp: only f_mode and f_flags may be used
  543. *
  544. * Returns 0.
  545. *
  546. * Note: may block (uninterruptible) if error recovery is underway
  547. * on this disk.
  548. **/
  549. static int sd_release(struct inode *inode, struct file *filp)
  550. {
  551. struct gendisk *disk = inode->i_bdev->bd_disk;
  552. struct scsi_disk *sdkp = scsi_disk(disk);
  553. struct scsi_device *sdev = sdkp->device;
  554. SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name));
  555. if (!--sdkp->openers && sdev->removable) {
  556. if (scsi_block_when_processing_errors(sdev))
  557. scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  558. }
  559. /*
  560. * XXX and what if there are packets in flight and this close()
  561. * XXX is followed by a "rmmod sd_mod"?
  562. */
  563. scsi_disk_put(sdkp);
  564. return 0;
  565. }
  566. static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  567. {
  568. struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
  569. struct scsi_device *sdp = sdkp->device;
  570. struct Scsi_Host *host = sdp->host;
  571. int diskinfo[4];
  572. /* default to most commonly used values */
  573. diskinfo[0] = 0x40; /* 1 << 6 */
  574. diskinfo[1] = 0x20; /* 1 << 5 */
  575. diskinfo[2] = sdkp->capacity >> 11;
  576. /* override with calculated, extended default, or driver values */
  577. if (host->hostt->bios_param)
  578. host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
  579. else
  580. scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
  581. geo->heads = diskinfo[0];
  582. geo->sectors = diskinfo[1];
  583. geo->cylinders = diskinfo[2];
  584. return 0;
  585. }
  586. /**
  587. * sd_ioctl - process an ioctl
  588. * @inode: only i_rdev/i_bdev members may be used
  589. * @filp: only f_mode and f_flags may be used
  590. * @cmd: ioctl command number
  591. * @arg: this is third argument given to ioctl(2) system call.
  592. * Often contains a pointer.
  593. *
  594. * Returns 0 if successful (some ioctls return postive numbers on
  595. * success as well). Returns a negated errno value in case of error.
  596. *
  597. * Note: most ioctls are forward onto the block subsystem or further
  598. * down in the scsi subsytem.
  599. **/
  600. static int sd_ioctl(struct inode * inode, struct file * filp,
  601. unsigned int cmd, unsigned long arg)
  602. {
  603. struct block_device *bdev = inode->i_bdev;
  604. struct gendisk *disk = bdev->bd_disk;
  605. struct scsi_device *sdp = scsi_disk(disk)->device;
  606. void __user *p = (void __user *)arg;
  607. int error;
  608. SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
  609. disk->disk_name, cmd));
  610. /*
  611. * If we are in the middle of error recovery, don't let anyone
  612. * else try and use this device. Also, if error recovery fails, it
  613. * may try and take the device offline, in which case all further
  614. * access to the device is prohibited.
  615. */
  616. error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
  617. if (!scsi_block_when_processing_errors(sdp) || !error)
  618. return error;
  619. /*
  620. * Send SCSI addressing ioctls directly to mid level, send other
  621. * ioctls to block level and then onto mid level if they can't be
  622. * resolved.
  623. */
  624. switch (cmd) {
  625. case SCSI_IOCTL_GET_IDLUN:
  626. case SCSI_IOCTL_GET_BUS_NUMBER:
  627. return scsi_ioctl(sdp, cmd, p);
  628. default:
  629. error = scsi_cmd_ioctl(filp, disk, cmd, p);
  630. if (error != -ENOTTY)
  631. return error;
  632. }
  633. return scsi_ioctl(sdp, cmd, p);
  634. }
  635. static void set_media_not_present(struct scsi_disk *sdkp)
  636. {
  637. sdkp->media_present = 0;
  638. sdkp->capacity = 0;
  639. sdkp->device->changed = 1;
  640. }
  641. /**
  642. * sd_media_changed - check if our medium changed
  643. * @disk: kernel device descriptor
  644. *
  645. * Returns 0 if not applicable or no change; 1 if change
  646. *
  647. * Note: this function is invoked from the block subsystem.
  648. **/
  649. static int sd_media_changed(struct gendisk *disk)
  650. {
  651. struct scsi_disk *sdkp = scsi_disk(disk);
  652. struct scsi_device *sdp = sdkp->device;
  653. int retval;
  654. SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
  655. disk->disk_name));
  656. if (!sdp->removable)
  657. return 0;
  658. /*
  659. * If the device is offline, don't send any commands - just pretend as
  660. * if the command failed. If the device ever comes back online, we
  661. * can deal with it then. It is only because of unrecoverable errors
  662. * that we would ever take a device offline in the first place.
  663. */
  664. if (!scsi_device_online(sdp))
  665. goto not_present;
  666. /*
  667. * Using TEST_UNIT_READY enables differentiation between drive with
  668. * no cartridge loaded - NOT READY, drive with changed cartridge -
  669. * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
  670. *
  671. * Drives that auto spin down. eg iomega jaz 1G, will be started
  672. * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
  673. * sd_revalidate() is called.
  674. */
  675. retval = -ENODEV;
  676. if (scsi_block_when_processing_errors(sdp))
  677. retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES);
  678. /*
  679. * Unable to test, unit probably not ready. This usually
  680. * means there is no disc in the drive. Mark as changed,
  681. * and we will figure it out later once the drive is
  682. * available again.
  683. */
  684. if (retval)
  685. goto not_present;
  686. /*
  687. * For removable scsi disk we have to recognise the presence
  688. * of a disk in the drive. This is kept in the struct scsi_disk
  689. * struct and tested at open ! Daniel Roche (dan@lectra.fr)
  690. */
  691. sdkp->media_present = 1;
  692. retval = sdp->changed;
  693. sdp->changed = 0;
  694. return retval;
  695. not_present:
  696. set_media_not_present(sdkp);
  697. return 1;
  698. }
  699. static int sd_sync_cache(struct scsi_device *sdp)
  700. {
  701. int retries, res;
  702. struct scsi_sense_hdr sshdr;
  703. if (!scsi_device_online(sdp))
  704. return -ENODEV;
  705. for (retries = 3; retries > 0; --retries) {
  706. unsigned char cmd[10] = { 0 };
  707. cmd[0] = SYNCHRONIZE_CACHE;
  708. /*
  709. * Leave the rest of the command zero to indicate
  710. * flush everything.
  711. */
  712. res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  713. SD_TIMEOUT, SD_MAX_RETRIES);
  714. if (res == 0)
  715. break;
  716. }
  717. if (res) { printk(KERN_WARNING "FAILED\n status = %x, message = %02x, "
  718. "host = %d, driver = %02x\n ",
  719. status_byte(res), msg_byte(res),
  720. host_byte(res), driver_byte(res));
  721. if (driver_byte(res) & DRIVER_SENSE)
  722. scsi_print_sense_hdr("sd", &sshdr);
  723. }
  724. return res;
  725. }
  726. static int sd_issue_flush(struct device *dev, sector_t *error_sector)
  727. {
  728. int ret = 0;
  729. struct scsi_device *sdp = to_scsi_device(dev);
  730. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  731. if (!sdkp)
  732. return -ENODEV;
  733. if (sdkp->WCE)
  734. ret = sd_sync_cache(sdp);
  735. scsi_disk_put(sdkp);
  736. return ret;
  737. }
  738. static void sd_prepare_flush(request_queue_t *q, struct request *rq)
  739. {
  740. memset(rq->cmd, 0, sizeof(rq->cmd));
  741. rq->flags |= REQ_BLOCK_PC;
  742. rq->timeout = SD_TIMEOUT;
  743. rq->cmd[0] = SYNCHRONIZE_CACHE;
  744. rq->cmd_len = 10;
  745. }
  746. static void sd_rescan(struct device *dev)
  747. {
  748. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  749. if (sdkp) {
  750. sd_revalidate_disk(sdkp->disk);
  751. scsi_disk_put(sdkp);
  752. }
  753. }
  754. #ifdef CONFIG_COMPAT
  755. /*
  756. * This gets directly called from VFS. When the ioctl
  757. * is not recognized we go back to the other translation paths.
  758. */
  759. static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  760. {
  761. struct block_device *bdev = file->f_dentry->d_inode->i_bdev;
  762. struct gendisk *disk = bdev->bd_disk;
  763. struct scsi_device *sdev = scsi_disk(disk)->device;
  764. /*
  765. * If we are in the middle of error recovery, don't let anyone
  766. * else try and use this device. Also, if error recovery fails, it
  767. * may try and take the device offline, in which case all further
  768. * access to the device is prohibited.
  769. */
  770. if (!scsi_block_when_processing_errors(sdev))
  771. return -ENODEV;
  772. if (sdev->host->hostt->compat_ioctl) {
  773. int ret;
  774. ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
  775. return ret;
  776. }
  777. /*
  778. * Let the static ioctl translation table take care of it.
  779. */
  780. return -ENOIOCTLCMD;
  781. }
  782. #endif
  783. static struct block_device_operations sd_fops = {
  784. .owner = THIS_MODULE,
  785. .open = sd_open,
  786. .release = sd_release,
  787. .ioctl = sd_ioctl,
  788. .getgeo = sd_getgeo,
  789. #ifdef CONFIG_COMPAT
  790. .compat_ioctl = sd_compat_ioctl,
  791. #endif
  792. .media_changed = sd_media_changed,
  793. .revalidate_disk = sd_revalidate_disk,
  794. };
  795. /**
  796. * sd_rw_intr - bottom half handler: called when the lower level
  797. * driver has completed (successfully or otherwise) a scsi command.
  798. * @SCpnt: mid-level's per command structure.
  799. *
  800. * Note: potentially run from within an ISR. Must not block.
  801. **/
  802. static void sd_rw_intr(struct scsi_cmnd * SCpnt)
  803. {
  804. int result = SCpnt->result;
  805. unsigned int xfer_size = SCpnt->request_bufflen;
  806. unsigned int good_bytes = result ? 0 : xfer_size;
  807. u64 start_lba = SCpnt->request->sector;
  808. u64 bad_lba;
  809. struct scsi_sense_hdr sshdr;
  810. int sense_valid = 0;
  811. int sense_deferred = 0;
  812. int info_valid;
  813. if (result) {
  814. sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
  815. if (sense_valid)
  816. sense_deferred = scsi_sense_is_deferred(&sshdr);
  817. }
  818. #ifdef CONFIG_SCSI_LOGGING
  819. SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n",
  820. SCpnt->request->rq_disk->disk_name, result));
  821. if (sense_valid) {
  822. SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[respc,sk,asc,"
  823. "ascq]=%x,%x,%x,%x\n", sshdr.response_code,
  824. sshdr.sense_key, sshdr.asc, sshdr.ascq));
  825. }
  826. #endif
  827. if (driver_byte(result) != DRIVER_SENSE &&
  828. (!sense_valid || sense_deferred))
  829. goto out;
  830. switch (sshdr.sense_key) {
  831. case HARDWARE_ERROR:
  832. case MEDIUM_ERROR:
  833. if (!blk_fs_request(SCpnt->request))
  834. goto out;
  835. info_valid = scsi_get_sense_info_fld(SCpnt->sense_buffer,
  836. SCSI_SENSE_BUFFERSIZE,
  837. &bad_lba);
  838. if (!info_valid)
  839. goto out;
  840. if (xfer_size <= SCpnt->device->sector_size)
  841. goto out;
  842. switch (SCpnt->device->sector_size) {
  843. case 256:
  844. start_lba <<= 1;
  845. break;
  846. case 512:
  847. break;
  848. case 1024:
  849. start_lba >>= 1;
  850. break;
  851. case 2048:
  852. start_lba >>= 2;
  853. break;
  854. case 4096:
  855. start_lba >>= 3;
  856. break;
  857. default:
  858. /* Print something here with limiting frequency. */
  859. goto out;
  860. break;
  861. }
  862. /* This computation should always be done in terms of
  863. * the resolution of the device's medium.
  864. */
  865. good_bytes = (bad_lba - start_lba)*SCpnt->device->sector_size;
  866. break;
  867. case RECOVERED_ERROR:
  868. case NO_SENSE:
  869. /* Inform the user, but make sure that it's not treated
  870. * as a hard error.
  871. */
  872. scsi_print_sense("sd", SCpnt);
  873. SCpnt->result = 0;
  874. memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  875. good_bytes = xfer_size;
  876. break;
  877. case ILLEGAL_REQUEST:
  878. if (SCpnt->device->use_10_for_rw &&
  879. (SCpnt->cmnd[0] == READ_10 ||
  880. SCpnt->cmnd[0] == WRITE_10))
  881. SCpnt->device->use_10_for_rw = 0;
  882. if (SCpnt->device->use_10_for_ms &&
  883. (SCpnt->cmnd[0] == MODE_SENSE_10 ||
  884. SCpnt->cmnd[0] == MODE_SELECT_10))
  885. SCpnt->device->use_10_for_ms = 0;
  886. break;
  887. default:
  888. break;
  889. }
  890. out:
  891. scsi_io_completion(SCpnt, good_bytes);
  892. }
  893. static int media_not_present(struct scsi_disk *sdkp,
  894. struct scsi_sense_hdr *sshdr)
  895. {
  896. if (!scsi_sense_valid(sshdr))
  897. return 0;
  898. /* not invoked for commands that could return deferred errors */
  899. if (sshdr->sense_key != NOT_READY &&
  900. sshdr->sense_key != UNIT_ATTENTION)
  901. return 0;
  902. if (sshdr->asc != 0x3A) /* medium not present */
  903. return 0;
  904. set_media_not_present(sdkp);
  905. return 1;
  906. }
  907. /*
  908. * spinup disk - called only in sd_revalidate_disk()
  909. */
  910. static void
  911. sd_spinup_disk(struct scsi_disk *sdkp, char *diskname)
  912. {
  913. unsigned char cmd[10];
  914. unsigned long spintime_expire = 0;
  915. int retries, spintime;
  916. unsigned int the_result;
  917. struct scsi_sense_hdr sshdr;
  918. int sense_valid = 0;
  919. spintime = 0;
  920. /* Spin up drives, as required. Only do this at boot time */
  921. /* Spinup needs to be done for module loads too. */
  922. do {
  923. retries = 0;
  924. do {
  925. cmd[0] = TEST_UNIT_READY;
  926. memset((void *) &cmd[1], 0, 9);
  927. the_result = scsi_execute_req(sdkp->device, cmd,
  928. DMA_NONE, NULL, 0,
  929. &sshdr, SD_TIMEOUT,
  930. SD_MAX_RETRIES);
  931. if (the_result)
  932. sense_valid = scsi_sense_valid(&sshdr);
  933. retries++;
  934. } while (retries < 3 &&
  935. (!scsi_status_is_good(the_result) ||
  936. ((driver_byte(the_result) & DRIVER_SENSE) &&
  937. sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
  938. /*
  939. * If the drive has indicated to us that it doesn't have
  940. * any media in it, don't bother with any of the rest of
  941. * this crap.
  942. */
  943. if (media_not_present(sdkp, &sshdr))
  944. return;
  945. if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
  946. /* no sense, TUR either succeeded or failed
  947. * with a status error */
  948. if(!spintime && !scsi_status_is_good(the_result))
  949. printk(KERN_NOTICE "%s: Unit Not Ready, "
  950. "error = 0x%x\n", diskname, the_result);
  951. break;
  952. }
  953. /*
  954. * The device does not want the automatic start to be issued.
  955. */
  956. if (sdkp->device->no_start_on_add) {
  957. break;
  958. }
  959. /*
  960. * If manual intervention is required, or this is an
  961. * absent USB storage device, a spinup is meaningless.
  962. */
  963. if (sense_valid &&
  964. sshdr.sense_key == NOT_READY &&
  965. sshdr.asc == 4 && sshdr.ascq == 3) {
  966. break; /* manual intervention required */
  967. /*
  968. * Issue command to spin up drive when not ready
  969. */
  970. } else if (sense_valid && sshdr.sense_key == NOT_READY) {
  971. if (!spintime) {
  972. printk(KERN_NOTICE "%s: Spinning up disk...",
  973. diskname);
  974. cmd[0] = START_STOP;
  975. cmd[1] = 1; /* Return immediately */
  976. memset((void *) &cmd[2], 0, 8);
  977. cmd[4] = 1; /* Start spin cycle */
  978. scsi_execute_req(sdkp->device, cmd, DMA_NONE,
  979. NULL, 0, &sshdr,
  980. SD_TIMEOUT, SD_MAX_RETRIES);
  981. spintime_expire = jiffies + 100 * HZ;
  982. spintime = 1;
  983. }
  984. /* Wait 1 second for next try */
  985. msleep(1000);
  986. printk(".");
  987. /*
  988. * Wait for USB flash devices with slow firmware.
  989. * Yes, this sense key/ASC combination shouldn't
  990. * occur here. It's characteristic of these devices.
  991. */
  992. } else if (sense_valid &&
  993. sshdr.sense_key == UNIT_ATTENTION &&
  994. sshdr.asc == 0x28) {
  995. if (!spintime) {
  996. spintime_expire = jiffies + 5 * HZ;
  997. spintime = 1;
  998. }
  999. /* Wait 1 second for next try */
  1000. msleep(1000);
  1001. } else {
  1002. /* we don't understand the sense code, so it's
  1003. * probably pointless to loop */
  1004. if(!spintime) {
  1005. printk(KERN_NOTICE "%s: Unit Not Ready, "
  1006. "sense:\n", diskname);
  1007. scsi_print_sense_hdr("", &sshdr);
  1008. }
  1009. break;
  1010. }
  1011. } while (spintime && time_before_eq(jiffies, spintime_expire));
  1012. if (spintime) {
  1013. if (scsi_status_is_good(the_result))
  1014. printk("ready\n");
  1015. else
  1016. printk("not responding...\n");
  1017. }
  1018. }
  1019. /*
  1020. * read disk capacity
  1021. */
  1022. static void
  1023. sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
  1024. unsigned char *buffer)
  1025. {
  1026. unsigned char cmd[16];
  1027. int the_result, retries;
  1028. int sector_size = 0;
  1029. int longrc = 0;
  1030. struct scsi_sense_hdr sshdr;
  1031. int sense_valid = 0;
  1032. struct scsi_device *sdp = sdkp->device;
  1033. repeat:
  1034. retries = 3;
  1035. do {
  1036. if (longrc) {
  1037. memset((void *) cmd, 0, 16);
  1038. cmd[0] = SERVICE_ACTION_IN;
  1039. cmd[1] = SAI_READ_CAPACITY_16;
  1040. cmd[13] = 12;
  1041. memset((void *) buffer, 0, 12);
  1042. } else {
  1043. cmd[0] = READ_CAPACITY;
  1044. memset((void *) &cmd[1], 0, 9);
  1045. memset((void *) buffer, 0, 8);
  1046. }
  1047. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1048. buffer, longrc ? 12 : 8, &sshdr,
  1049. SD_TIMEOUT, SD_MAX_RETRIES);
  1050. if (media_not_present(sdkp, &sshdr))
  1051. return;
  1052. if (the_result)
  1053. sense_valid = scsi_sense_valid(&sshdr);
  1054. retries--;
  1055. } while (the_result && retries);
  1056. if (the_result && !longrc) {
  1057. printk(KERN_NOTICE "%s : READ CAPACITY failed.\n"
  1058. "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
  1059. diskname, diskname,
  1060. status_byte(the_result),
  1061. msg_byte(the_result),
  1062. host_byte(the_result),
  1063. driver_byte(the_result));
  1064. if (driver_byte(the_result) & DRIVER_SENSE)
  1065. scsi_print_sense_hdr("sd", &sshdr);
  1066. else
  1067. printk("%s : sense not available. \n", diskname);
  1068. /* Set dirty bit for removable devices if not ready -
  1069. * sometimes drives will not report this properly. */
  1070. if (sdp->removable &&
  1071. sense_valid && sshdr.sense_key == NOT_READY)
  1072. sdp->changed = 1;
  1073. /* Either no media are present but the drive didn't tell us,
  1074. or they are present but the read capacity command fails */
  1075. /* sdkp->media_present = 0; -- not always correct */
  1076. sdkp->capacity = 0x200000; /* 1 GB - random */
  1077. return;
  1078. } else if (the_result && longrc) {
  1079. /* READ CAPACITY(16) has been failed */
  1080. printk(KERN_NOTICE "%s : READ CAPACITY(16) failed.\n"
  1081. "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
  1082. diskname, diskname,
  1083. status_byte(the_result),
  1084. msg_byte(the_result),
  1085. host_byte(the_result),
  1086. driver_byte(the_result));
  1087. printk(KERN_NOTICE "%s : use 0xffffffff as device size\n",
  1088. diskname);
  1089. sdkp->capacity = 1 + (sector_t) 0xffffffff;
  1090. goto got_data;
  1091. }
  1092. if (!longrc) {
  1093. sector_size = (buffer[4] << 24) |
  1094. (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  1095. if (buffer[0] == 0xff && buffer[1] == 0xff &&
  1096. buffer[2] == 0xff && buffer[3] == 0xff) {
  1097. if(sizeof(sdkp->capacity) > 4) {
  1098. printk(KERN_NOTICE "%s : very big device. try to use"
  1099. " READ CAPACITY(16).\n", diskname);
  1100. longrc = 1;
  1101. goto repeat;
  1102. }
  1103. printk(KERN_ERR "%s: too big for this kernel. Use a "
  1104. "kernel compiled with support for large block "
  1105. "devices.\n", diskname);
  1106. sdkp->capacity = 0;
  1107. goto got_data;
  1108. }
  1109. sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
  1110. (buffer[1] << 16) |
  1111. (buffer[2] << 8) |
  1112. buffer[3]);
  1113. } else {
  1114. sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
  1115. ((u64)buffer[1] << 48) |
  1116. ((u64)buffer[2] << 40) |
  1117. ((u64)buffer[3] << 32) |
  1118. ((sector_t)buffer[4] << 24) |
  1119. ((sector_t)buffer[5] << 16) |
  1120. ((sector_t)buffer[6] << 8) |
  1121. (sector_t)buffer[7]);
  1122. sector_size = (buffer[8] << 24) |
  1123. (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
  1124. }
  1125. /* Some devices return the total number of sectors, not the
  1126. * highest sector number. Make the necessary adjustment. */
  1127. if (sdp->fix_capacity)
  1128. --sdkp->capacity;
  1129. got_data:
  1130. if (sector_size == 0) {
  1131. sector_size = 512;
  1132. printk(KERN_NOTICE "%s : sector size 0 reported, "
  1133. "assuming 512.\n", diskname);
  1134. }
  1135. if (sector_size != 512 &&
  1136. sector_size != 1024 &&
  1137. sector_size != 2048 &&
  1138. sector_size != 4096 &&
  1139. sector_size != 256) {
  1140. printk(KERN_NOTICE "%s : unsupported sector size "
  1141. "%d.\n", diskname, sector_size);
  1142. /*
  1143. * The user might want to re-format the drive with
  1144. * a supported sectorsize. Once this happens, it
  1145. * would be relatively trivial to set the thing up.
  1146. * For this reason, we leave the thing in the table.
  1147. */
  1148. sdkp->capacity = 0;
  1149. /*
  1150. * set a bogus sector size so the normal read/write
  1151. * logic in the block layer will eventually refuse any
  1152. * request on this device without tripping over power
  1153. * of two sector size assumptions
  1154. */
  1155. sector_size = 512;
  1156. }
  1157. {
  1158. /*
  1159. * The msdos fs needs to know the hardware sector size
  1160. * So I have created this table. See ll_rw_blk.c
  1161. * Jacques Gelinas (Jacques@solucorp.qc.ca)
  1162. */
  1163. int hard_sector = sector_size;
  1164. sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
  1165. request_queue_t *queue = sdp->request_queue;
  1166. sector_t mb = sz;
  1167. blk_queue_hardsect_size(queue, hard_sector);
  1168. /* avoid 64-bit division on 32-bit platforms */
  1169. sector_div(sz, 625);
  1170. mb -= sz - 974;
  1171. sector_div(mb, 1950);
  1172. printk(KERN_NOTICE "SCSI device %s: "
  1173. "%llu %d-byte hdwr sectors (%llu MB)\n",
  1174. diskname, (unsigned long long)sdkp->capacity,
  1175. hard_sector, (unsigned long long)mb);
  1176. }
  1177. /* Rescale capacity to 512-byte units */
  1178. if (sector_size == 4096)
  1179. sdkp->capacity <<= 3;
  1180. else if (sector_size == 2048)
  1181. sdkp->capacity <<= 2;
  1182. else if (sector_size == 1024)
  1183. sdkp->capacity <<= 1;
  1184. else if (sector_size == 256)
  1185. sdkp->capacity >>= 1;
  1186. sdkp->device->sector_size = sector_size;
  1187. }
  1188. /* called with buffer of length 512 */
  1189. static inline int
  1190. sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
  1191. unsigned char *buffer, int len, struct scsi_mode_data *data,
  1192. struct scsi_sense_hdr *sshdr)
  1193. {
  1194. return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
  1195. SD_TIMEOUT, SD_MAX_RETRIES, data,
  1196. sshdr);
  1197. }
  1198. /*
  1199. * read write protect setting, if possible - called only in sd_revalidate_disk()
  1200. * called with buffer of length SD_BUF_SIZE
  1201. */
  1202. static void
  1203. sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
  1204. unsigned char *buffer)
  1205. {
  1206. int res;
  1207. struct scsi_device *sdp = sdkp->device;
  1208. struct scsi_mode_data data;
  1209. set_disk_ro(sdkp->disk, 0);
  1210. if (sdp->skip_ms_page_3f) {
  1211. printk(KERN_NOTICE "%s: assuming Write Enabled\n", diskname);
  1212. return;
  1213. }
  1214. if (sdp->use_192_bytes_for_3f) {
  1215. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
  1216. } else {
  1217. /*
  1218. * First attempt: ask for all pages (0x3F), but only 4 bytes.
  1219. * We have to start carefully: some devices hang if we ask
  1220. * for more than is available.
  1221. */
  1222. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
  1223. /*
  1224. * Second attempt: ask for page 0 When only page 0 is
  1225. * implemented, a request for page 3F may return Sense Key
  1226. * 5: Illegal Request, Sense Code 24: Invalid field in
  1227. * CDB.
  1228. */
  1229. if (!scsi_status_is_good(res))
  1230. res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
  1231. /*
  1232. * Third attempt: ask 255 bytes, as we did earlier.
  1233. */
  1234. if (!scsi_status_is_good(res))
  1235. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
  1236. &data, NULL);
  1237. }
  1238. if (!scsi_status_is_good(res)) {
  1239. printk(KERN_WARNING
  1240. "%s: test WP failed, assume Write Enabled\n", diskname);
  1241. } else {
  1242. sdkp->write_prot = ((data.device_specific & 0x80) != 0);
  1243. set_disk_ro(sdkp->disk, sdkp->write_prot);
  1244. printk(KERN_NOTICE "%s: Write Protect is %s\n", diskname,
  1245. sdkp->write_prot ? "on" : "off");
  1246. printk(KERN_DEBUG "%s: Mode Sense: %02x %02x %02x %02x\n",
  1247. diskname, buffer[0], buffer[1], buffer[2], buffer[3]);
  1248. }
  1249. }
  1250. /*
  1251. * sd_read_cache_type - called only from sd_revalidate_disk()
  1252. * called with buffer of length SD_BUF_SIZE
  1253. */
  1254. static void
  1255. sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
  1256. unsigned char *buffer)
  1257. {
  1258. int len = 0, res;
  1259. struct scsi_device *sdp = sdkp->device;
  1260. int dbd;
  1261. int modepage;
  1262. struct scsi_mode_data data;
  1263. struct scsi_sense_hdr sshdr;
  1264. if (sdp->skip_ms_page_8)
  1265. goto defaults;
  1266. if (sdp->type == TYPE_RBC) {
  1267. modepage = 6;
  1268. dbd = 8;
  1269. } else {
  1270. modepage = 8;
  1271. dbd = 0;
  1272. }
  1273. /* cautiously ask */
  1274. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
  1275. if (!scsi_status_is_good(res))
  1276. goto bad_sense;
  1277. if (!data.header_length) {
  1278. modepage = 6;
  1279. printk(KERN_ERR "%s: missing header in MODE_SENSE response\n",
  1280. diskname);
  1281. }
  1282. /* that went OK, now ask for the proper length */
  1283. len = data.length;
  1284. /*
  1285. * We're only interested in the first three bytes, actually.
  1286. * But the data cache page is defined for the first 20.
  1287. */
  1288. if (len < 3)
  1289. goto bad_sense;
  1290. if (len > 20)
  1291. len = 20;
  1292. /* Take headers and block descriptors into account */
  1293. len += data.header_length + data.block_descriptor_length;
  1294. if (len > SD_BUF_SIZE)
  1295. goto bad_sense;
  1296. /* Get the data */
  1297. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
  1298. if (scsi_status_is_good(res)) {
  1299. int ct = 0;
  1300. int offset = data.header_length + data.block_descriptor_length;
  1301. if (offset >= SD_BUF_SIZE - 2) {
  1302. printk(KERN_ERR "%s: malformed MODE SENSE response",
  1303. diskname);
  1304. goto defaults;
  1305. }
  1306. if ((buffer[offset] & 0x3f) != modepage) {
  1307. printk(KERN_ERR "%s: got wrong page\n", diskname);
  1308. goto defaults;
  1309. }
  1310. if (modepage == 8) {
  1311. sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
  1312. sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
  1313. } else {
  1314. sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
  1315. sdkp->RCD = 0;
  1316. }
  1317. sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
  1318. if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
  1319. printk(KERN_NOTICE "SCSI device %s: uses "
  1320. "READ/WRITE(6), disabling FUA\n", diskname);
  1321. sdkp->DPOFUA = 0;
  1322. }
  1323. ct = sdkp->RCD + 2*sdkp->WCE;
  1324. printk(KERN_NOTICE "SCSI device %s: drive cache: %s%s\n",
  1325. diskname, sd_cache_types[ct],
  1326. sdkp->DPOFUA ? " w/ FUA" : "");
  1327. return;
  1328. }
  1329. bad_sense:
  1330. if (scsi_sense_valid(&sshdr) &&
  1331. sshdr.sense_key == ILLEGAL_REQUEST &&
  1332. sshdr.asc == 0x24 && sshdr.ascq == 0x0)
  1333. printk(KERN_NOTICE "%s: cache data unavailable\n",
  1334. diskname); /* Invalid field in CDB */
  1335. else
  1336. printk(KERN_ERR "%s: asking for cache data failed\n",
  1337. diskname);
  1338. defaults:
  1339. printk(KERN_ERR "%s: assuming drive cache: write through\n",
  1340. diskname);
  1341. sdkp->WCE = 0;
  1342. sdkp->RCD = 0;
  1343. sdkp->DPOFUA = 0;
  1344. }
  1345. /**
  1346. * sd_revalidate_disk - called the first time a new disk is seen,
  1347. * performs disk spin up, read_capacity, etc.
  1348. * @disk: struct gendisk we care about
  1349. **/
  1350. static int sd_revalidate_disk(struct gendisk *disk)
  1351. {
  1352. struct scsi_disk *sdkp = scsi_disk(disk);
  1353. struct scsi_device *sdp = sdkp->device;
  1354. unsigned char *buffer;
  1355. unsigned ordered;
  1356. SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name));
  1357. /*
  1358. * If the device is offline, don't try and read capacity or any
  1359. * of the other niceties.
  1360. */
  1361. if (!scsi_device_online(sdp))
  1362. goto out;
  1363. buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL | __GFP_DMA);
  1364. if (!buffer) {
  1365. printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation "
  1366. "failure.\n");
  1367. goto out;
  1368. }
  1369. /* defaults, until the device tells us otherwise */
  1370. sdp->sector_size = 512;
  1371. sdkp->capacity = 0;
  1372. sdkp->media_present = 1;
  1373. sdkp->write_prot = 0;
  1374. sdkp->WCE = 0;
  1375. sdkp->RCD = 0;
  1376. sd_spinup_disk(sdkp, disk->disk_name);
  1377. /*
  1378. * Without media there is no reason to ask; moreover, some devices
  1379. * react badly if we do.
  1380. */
  1381. if (sdkp->media_present) {
  1382. sd_read_capacity(sdkp, disk->disk_name, buffer);
  1383. sd_read_write_protect_flag(sdkp, disk->disk_name, buffer);
  1384. sd_read_cache_type(sdkp, disk->disk_name, buffer);
  1385. }
  1386. /*
  1387. * We now have all cache related info, determine how we deal
  1388. * with ordered requests. Note that as the current SCSI
  1389. * dispatch function can alter request order, we cannot use
  1390. * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
  1391. */
  1392. if (sdkp->WCE)
  1393. ordered = sdkp->DPOFUA
  1394. ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
  1395. else
  1396. ordered = QUEUE_ORDERED_DRAIN;
  1397. blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
  1398. set_capacity(disk, sdkp->capacity);
  1399. kfree(buffer);
  1400. out:
  1401. return 0;
  1402. }
  1403. /**
  1404. * sd_probe - called during driver initialization and whenever a
  1405. * new scsi device is attached to the system. It is called once
  1406. * for each scsi device (not just disks) present.
  1407. * @dev: pointer to device object
  1408. *
  1409. * Returns 0 if successful (or not interested in this scsi device
  1410. * (e.g. scanner)); 1 when there is an error.
  1411. *
  1412. * Note: this function is invoked from the scsi mid-level.
  1413. * This function sets up the mapping between a given
  1414. * <host,channel,id,lun> (found in sdp) and new device name
  1415. * (e.g. /dev/sda). More precisely it is the block device major
  1416. * and minor number that is chosen here.
  1417. *
  1418. * Assume sd_attach is not re-entrant (for time being)
  1419. * Also think about sd_attach() and sd_remove() running coincidentally.
  1420. **/
  1421. static int sd_probe(struct device *dev)
  1422. {
  1423. struct scsi_device *sdp = to_scsi_device(dev);
  1424. struct scsi_disk *sdkp;
  1425. struct gendisk *gd;
  1426. u32 index;
  1427. int error;
  1428. error = -ENODEV;
  1429. if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
  1430. goto out;
  1431. SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
  1432. "sd_attach\n"));
  1433. error = -ENOMEM;
  1434. sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
  1435. if (!sdkp)
  1436. goto out;
  1437. gd = alloc_disk(16);
  1438. if (!gd)
  1439. goto out_free;
  1440. if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))
  1441. goto out_put;
  1442. spin_lock(&sd_index_lock);
  1443. error = idr_get_new(&sd_index_idr, NULL, &index);
  1444. spin_unlock(&sd_index_lock);
  1445. if (index >= SD_MAX_DISKS)
  1446. error = -EBUSY;
  1447. if (error)
  1448. goto out_put;
  1449. class_device_initialize(&sdkp->cdev);
  1450. sdkp->cdev.dev = &sdp->sdev_gendev;
  1451. sdkp->cdev.class = &sd_disk_class;
  1452. strncpy(sdkp->cdev.class_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE);
  1453. if (class_device_add(&sdkp->cdev))
  1454. goto out_put;
  1455. get_device(&sdp->sdev_gendev);
  1456. sdkp->device = sdp;
  1457. sdkp->driver = &sd_template;
  1458. sdkp->disk = gd;
  1459. sdkp->index = index;
  1460. sdkp->openers = 0;
  1461. if (!sdp->timeout) {
  1462. if (sdp->type != TYPE_MOD)
  1463. sdp->timeout = SD_TIMEOUT;
  1464. else
  1465. sdp->timeout = SD_MOD_TIMEOUT;
  1466. }
  1467. gd->major = sd_major((index & 0xf0) >> 4);
  1468. gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
  1469. gd->minors = 16;
  1470. gd->fops = &sd_fops;
  1471. if (index < 26) {
  1472. sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
  1473. } else if (index < (26 + 1) * 26) {
  1474. sprintf(gd->disk_name, "sd%c%c",
  1475. 'a' + index / 26 - 1,'a' + index % 26);
  1476. } else {
  1477. const unsigned int m1 = (index / 26 - 1) / 26 - 1;
  1478. const unsigned int m2 = (index / 26 - 1) % 26;
  1479. const unsigned int m3 = index % 26;
  1480. sprintf(gd->disk_name, "sd%c%c%c",
  1481. 'a' + m1, 'a' + m2, 'a' + m3);
  1482. }
  1483. gd->private_data = &sdkp->driver;
  1484. gd->queue = sdkp->device->request_queue;
  1485. sd_revalidate_disk(gd);
  1486. gd->driverfs_dev = &sdp->sdev_gendev;
  1487. gd->flags = GENHD_FL_DRIVERFS;
  1488. if (sdp->removable)
  1489. gd->flags |= GENHD_FL_REMOVABLE;
  1490. dev_set_drvdata(dev, sdkp);
  1491. add_disk(gd);
  1492. sdev_printk(KERN_NOTICE, sdp, "Attached scsi %sdisk %s\n",
  1493. sdp->removable ? "removable " : "", gd->disk_name);
  1494. return 0;
  1495. out_put:
  1496. put_disk(gd);
  1497. out_free:
  1498. kfree(sdkp);
  1499. out:
  1500. return error;
  1501. }
  1502. /**
  1503. * sd_remove - called whenever a scsi disk (previously recognized by
  1504. * sd_probe) is detached from the system. It is called (potentially
  1505. * multiple times) during sd module unload.
  1506. * @sdp: pointer to mid level scsi device object
  1507. *
  1508. * Note: this function is invoked from the scsi mid-level.
  1509. * This function potentially frees up a device name (e.g. /dev/sdc)
  1510. * that could be re-used by a subsequent sd_probe().
  1511. * This function is not called when the built-in sd driver is "exit-ed".
  1512. **/
  1513. static int sd_remove(struct device *dev)
  1514. {
  1515. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  1516. class_device_del(&sdkp->cdev);
  1517. del_gendisk(sdkp->disk);
  1518. sd_shutdown(dev);
  1519. mutex_lock(&sd_ref_mutex);
  1520. dev_set_drvdata(dev, NULL);
  1521. class_device_put(&sdkp->cdev);
  1522. mutex_unlock(&sd_ref_mutex);
  1523. return 0;
  1524. }
  1525. /**
  1526. * scsi_disk_release - Called to free the scsi_disk structure
  1527. * @cdev: pointer to embedded class device
  1528. *
  1529. * sd_ref_mutex must be held entering this routine. Because it is
  1530. * called on last put, you should always use the scsi_disk_get()
  1531. * scsi_disk_put() helpers which manipulate the semaphore directly
  1532. * and never do a direct class_device_put().
  1533. **/
  1534. static void scsi_disk_release(struct class_device *cdev)
  1535. {
  1536. struct scsi_disk *sdkp = to_scsi_disk(cdev);
  1537. struct gendisk *disk = sdkp->disk;
  1538. spin_lock(&sd_index_lock);
  1539. idr_remove(&sd_index_idr, sdkp->index);
  1540. spin_unlock(&sd_index_lock);
  1541. disk->private_data = NULL;
  1542. put_disk(disk);
  1543. put_device(&sdkp->device->sdev_gendev);
  1544. kfree(sdkp);
  1545. }
  1546. /*
  1547. * Send a SYNCHRONIZE CACHE instruction down to the device through
  1548. * the normal SCSI command structure. Wait for the command to
  1549. * complete.
  1550. */
  1551. static void sd_shutdown(struct device *dev)
  1552. {
  1553. struct scsi_device *sdp = to_scsi_device(dev);
  1554. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  1555. if (!sdkp)
  1556. return; /* this can happen */
  1557. if (sdkp->WCE) {
  1558. printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n",
  1559. sdkp->disk->disk_name);
  1560. sd_sync_cache(sdp);
  1561. }
  1562. scsi_disk_put(sdkp);
  1563. }
  1564. /**
  1565. * init_sd - entry point for this driver (both when built in or when
  1566. * a module).
  1567. *
  1568. * Note: this function registers this driver with the scsi mid-level.
  1569. **/
  1570. static int __init init_sd(void)
  1571. {
  1572. int majors = 0, i;
  1573. SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
  1574. for (i = 0; i < SD_MAJORS; i++)
  1575. if (register_blkdev(sd_major(i), "sd") == 0)
  1576. majors++;
  1577. if (!majors)
  1578. return -ENODEV;
  1579. class_register(&sd_disk_class);
  1580. return scsi_register_driver(&sd_template.gendrv);
  1581. }
  1582. /**
  1583. * exit_sd - exit point for this driver (when it is a module).
  1584. *
  1585. * Note: this function unregisters this driver from the scsi mid-level.
  1586. **/
  1587. static void __exit exit_sd(void)
  1588. {
  1589. int i;
  1590. SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
  1591. scsi_unregister_driver(&sd_template.gendrv);
  1592. for (i = 0; i < SD_MAJORS; i++)
  1593. unregister_blkdev(sd_major(i), "sd");
  1594. class_unregister(&sd_disk_class);
  1595. }
  1596. module_init(init_sd);
  1597. module_exit(exit_sd);