sd.c 50 KB

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