sd.c 47 KB

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