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