sd.c 49 KB

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