sd.c 47 KB

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