sd.c 54 KB

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