sd.c 46 KB

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