sd.c 52 KB

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