sd.c 50 KB

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