sd.c 51 KB

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