sd.c 50 KB

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