sd.c 51 KB

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