sd.c 50 KB

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