sd.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430
  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 <linux/string_helpers.h>
  50. #include <linux/async.h>
  51. #include <asm/uaccess.h>
  52. #include <asm/unaligned.h>
  53. #include <scsi/scsi.h>
  54. #include <scsi/scsi_cmnd.h>
  55. #include <scsi/scsi_dbg.h>
  56. #include <scsi/scsi_device.h>
  57. #include <scsi/scsi_driver.h>
  58. #include <scsi/scsi_eh.h>
  59. #include <scsi/scsi_host.h>
  60. #include <scsi/scsi_ioctl.h>
  61. #include <scsi/scsicam.h>
  62. #include "sd.h"
  63. #include "scsi_logging.h"
  64. MODULE_AUTHOR("Eric Youngdale");
  65. MODULE_DESCRIPTION("SCSI disk (sd) driver");
  66. MODULE_LICENSE("GPL");
  67. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
  68. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
  69. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
  70. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
  71. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
  72. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
  73. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
  74. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
  75. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
  76. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
  77. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
  78. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
  79. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
  80. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
  81. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
  82. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
  83. MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
  84. MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
  85. MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
  86. #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
  87. #define SD_MINORS 16
  88. #else
  89. #define SD_MINORS 0
  90. #endif
  91. static int sd_revalidate_disk(struct gendisk *);
  92. static int sd_probe(struct device *);
  93. static int sd_remove(struct device *);
  94. static void sd_shutdown(struct device *);
  95. static int sd_suspend(struct device *, pm_message_t state);
  96. static int sd_resume(struct device *);
  97. static void sd_rescan(struct device *);
  98. static int sd_done(struct scsi_cmnd *);
  99. static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
  100. static void scsi_disk_release(struct device *cdev);
  101. static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
  102. static void sd_print_result(struct scsi_disk *, int);
  103. static DEFINE_SPINLOCK(sd_index_lock);
  104. static DEFINE_IDA(sd_index_ida);
  105. /* This semaphore is used to mediate the 0->1 reference get in the
  106. * face of object destruction (i.e. we can't allow a get on an
  107. * object after last put) */
  108. static DEFINE_MUTEX(sd_ref_mutex);
  109. struct kmem_cache *sd_cdb_cache;
  110. mempool_t *sd_cdb_pool;
  111. static const char *sd_cache_types[] = {
  112. "write through", "none", "write back",
  113. "write back, no read (daft)"
  114. };
  115. static ssize_t
  116. sd_store_cache_type(struct device *dev, struct device_attribute *attr,
  117. const char *buf, size_t count)
  118. {
  119. int i, ct = -1, rcd, wce, sp;
  120. struct scsi_disk *sdkp = to_scsi_disk(dev);
  121. struct scsi_device *sdp = sdkp->device;
  122. char buffer[64];
  123. char *buffer_data;
  124. struct scsi_mode_data data;
  125. struct scsi_sense_hdr sshdr;
  126. int len;
  127. if (sdp->type != TYPE_DISK)
  128. /* no cache control on RBC devices; theoretically they
  129. * can do it, but there's probably so many exceptions
  130. * it's not worth the risk */
  131. return -EINVAL;
  132. for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
  133. const int len = strlen(sd_cache_types[i]);
  134. if (strncmp(sd_cache_types[i], buf, len) == 0 &&
  135. buf[len] == '\n') {
  136. ct = i;
  137. break;
  138. }
  139. }
  140. if (ct < 0)
  141. return -EINVAL;
  142. rcd = ct & 0x01 ? 1 : 0;
  143. wce = ct & 0x02 ? 1 : 0;
  144. if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
  145. SD_MAX_RETRIES, &data, NULL))
  146. return -EINVAL;
  147. len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
  148. data.block_descriptor_length);
  149. buffer_data = buffer + data.header_length +
  150. data.block_descriptor_length;
  151. buffer_data[2] &= ~0x05;
  152. buffer_data[2] |= wce << 2 | rcd;
  153. sp = buffer_data[0] & 0x80 ? 1 : 0;
  154. if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
  155. SD_MAX_RETRIES, &data, &sshdr)) {
  156. if (scsi_sense_valid(&sshdr))
  157. sd_print_sense_hdr(sdkp, &sshdr);
  158. return -EINVAL;
  159. }
  160. revalidate_disk(sdkp->disk);
  161. return count;
  162. }
  163. static ssize_t
  164. sd_store_manage_start_stop(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. sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
  172. return count;
  173. }
  174. static ssize_t
  175. sd_store_allow_restart(struct device *dev, struct device_attribute *attr,
  176. const char *buf, size_t count)
  177. {
  178. struct scsi_disk *sdkp = to_scsi_disk(dev);
  179. struct scsi_device *sdp = sdkp->device;
  180. if (!capable(CAP_SYS_ADMIN))
  181. return -EACCES;
  182. if (sdp->type != TYPE_DISK)
  183. return -EINVAL;
  184. sdp->allow_restart = simple_strtoul(buf, NULL, 10);
  185. return count;
  186. }
  187. static ssize_t
  188. sd_show_cache_type(struct device *dev, struct device_attribute *attr,
  189. char *buf)
  190. {
  191. struct scsi_disk *sdkp = to_scsi_disk(dev);
  192. int ct = sdkp->RCD + 2*sdkp->WCE;
  193. return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
  194. }
  195. static ssize_t
  196. sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf)
  197. {
  198. struct scsi_disk *sdkp = to_scsi_disk(dev);
  199. return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
  200. }
  201. static ssize_t
  202. sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr,
  203. char *buf)
  204. {
  205. struct scsi_disk *sdkp = to_scsi_disk(dev);
  206. struct scsi_device *sdp = sdkp->device;
  207. return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
  208. }
  209. static ssize_t
  210. sd_show_allow_restart(struct device *dev, struct device_attribute *attr,
  211. char *buf)
  212. {
  213. struct scsi_disk *sdkp = to_scsi_disk(dev);
  214. return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
  215. }
  216. static ssize_t
  217. sd_show_protection_type(struct device *dev, struct device_attribute *attr,
  218. char *buf)
  219. {
  220. struct scsi_disk *sdkp = to_scsi_disk(dev);
  221. return snprintf(buf, 20, "%u\n", sdkp->protection_type);
  222. }
  223. static ssize_t
  224. sd_show_app_tag_own(struct device *dev, struct device_attribute *attr,
  225. char *buf)
  226. {
  227. struct scsi_disk *sdkp = to_scsi_disk(dev);
  228. return snprintf(buf, 20, "%u\n", sdkp->ATO);
  229. }
  230. static struct device_attribute sd_disk_attrs[] = {
  231. __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
  232. sd_store_cache_type),
  233. __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
  234. __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
  235. sd_store_allow_restart),
  236. __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
  237. sd_store_manage_start_stop),
  238. __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
  239. __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
  240. __ATTR_NULL,
  241. };
  242. static struct class sd_disk_class = {
  243. .name = "scsi_disk",
  244. .owner = THIS_MODULE,
  245. .dev_release = scsi_disk_release,
  246. .dev_attrs = sd_disk_attrs,
  247. };
  248. static struct scsi_driver sd_template = {
  249. .owner = THIS_MODULE,
  250. .gendrv = {
  251. .name = "sd",
  252. .probe = sd_probe,
  253. .remove = sd_remove,
  254. .suspend = sd_suspend,
  255. .resume = sd_resume,
  256. .shutdown = sd_shutdown,
  257. },
  258. .rescan = sd_rescan,
  259. .done = sd_done,
  260. };
  261. /*
  262. * Device no to disk mapping:
  263. *
  264. * major disc2 disc p1
  265. * |............|.............|....|....| <- dev_t
  266. * 31 20 19 8 7 4 3 0
  267. *
  268. * Inside a major, we have 16k disks, however mapped non-
  269. * contiguously. The first 16 disks are for major0, the next
  270. * ones with major1, ... Disk 256 is for major0 again, disk 272
  271. * for major1, ...
  272. * As we stay compatible with our numbering scheme, we can reuse
  273. * the well-know SCSI majors 8, 65--71, 136--143.
  274. */
  275. static int sd_major(int major_idx)
  276. {
  277. switch (major_idx) {
  278. case 0:
  279. return SCSI_DISK0_MAJOR;
  280. case 1 ... 7:
  281. return SCSI_DISK1_MAJOR + major_idx - 1;
  282. case 8 ... 15:
  283. return SCSI_DISK8_MAJOR + major_idx - 8;
  284. default:
  285. BUG();
  286. return 0; /* shut up gcc */
  287. }
  288. }
  289. static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
  290. {
  291. struct scsi_disk *sdkp = NULL;
  292. if (disk->private_data) {
  293. sdkp = scsi_disk(disk);
  294. if (scsi_device_get(sdkp->device) == 0)
  295. get_device(&sdkp->dev);
  296. else
  297. sdkp = NULL;
  298. }
  299. return sdkp;
  300. }
  301. static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
  302. {
  303. struct scsi_disk *sdkp;
  304. mutex_lock(&sd_ref_mutex);
  305. sdkp = __scsi_disk_get(disk);
  306. mutex_unlock(&sd_ref_mutex);
  307. return sdkp;
  308. }
  309. static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
  310. {
  311. struct scsi_disk *sdkp;
  312. mutex_lock(&sd_ref_mutex);
  313. sdkp = dev_get_drvdata(dev);
  314. if (sdkp)
  315. sdkp = __scsi_disk_get(sdkp->disk);
  316. mutex_unlock(&sd_ref_mutex);
  317. return sdkp;
  318. }
  319. static void scsi_disk_put(struct scsi_disk *sdkp)
  320. {
  321. struct scsi_device *sdev = sdkp->device;
  322. mutex_lock(&sd_ref_mutex);
  323. put_device(&sdkp->dev);
  324. scsi_device_put(sdev);
  325. mutex_unlock(&sd_ref_mutex);
  326. }
  327. static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
  328. {
  329. unsigned int prot_op = SCSI_PROT_NORMAL;
  330. unsigned int dix = scsi_prot_sg_count(scmd);
  331. if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
  332. if (dif && dix)
  333. prot_op = SCSI_PROT_READ_PASS;
  334. else if (dif && !dix)
  335. prot_op = SCSI_PROT_READ_STRIP;
  336. else if (!dif && dix)
  337. prot_op = SCSI_PROT_READ_INSERT;
  338. } else {
  339. if (dif && dix)
  340. prot_op = SCSI_PROT_WRITE_PASS;
  341. else if (dif && !dix)
  342. prot_op = SCSI_PROT_WRITE_INSERT;
  343. else if (!dif && dix)
  344. prot_op = SCSI_PROT_WRITE_STRIP;
  345. }
  346. scsi_set_prot_op(scmd, prot_op);
  347. scsi_set_prot_type(scmd, dif);
  348. }
  349. /**
  350. * sd_init_command - build a scsi (read or write) command from
  351. * information in the request structure.
  352. * @SCpnt: pointer to mid-level's per scsi command structure that
  353. * contains request and into which the scsi command is written
  354. *
  355. * Returns 1 if successful and 0 if error (or cannot be done now).
  356. **/
  357. static int sd_prep_fn(struct request_queue *q, struct request *rq)
  358. {
  359. struct scsi_cmnd *SCpnt;
  360. struct scsi_device *sdp = q->queuedata;
  361. struct gendisk *disk = rq->rq_disk;
  362. struct scsi_disk *sdkp;
  363. sector_t block = blk_rq_pos(rq);
  364. sector_t threshold;
  365. unsigned int this_count = blk_rq_sectors(rq);
  366. int ret, host_dif;
  367. unsigned char protect;
  368. if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  369. ret = scsi_setup_blk_pc_cmnd(sdp, rq);
  370. goto out;
  371. } else if (rq->cmd_type != REQ_TYPE_FS) {
  372. ret = BLKPREP_KILL;
  373. goto out;
  374. }
  375. ret = scsi_setup_fs_cmnd(sdp, rq);
  376. if (ret != BLKPREP_OK)
  377. goto out;
  378. SCpnt = rq->special;
  379. sdkp = scsi_disk(disk);
  380. /* from here on until we're complete, any goto out
  381. * is used for a killable error condition */
  382. ret = BLKPREP_KILL;
  383. SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
  384. "sd_init_command: block=%llu, "
  385. "count=%d\n",
  386. (unsigned long long)block,
  387. this_count));
  388. if (!sdp || !scsi_device_online(sdp) ||
  389. block + blk_rq_sectors(rq) > get_capacity(disk)) {
  390. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  391. "Finishing %u sectors\n",
  392. blk_rq_sectors(rq)));
  393. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  394. "Retry with 0x%p\n", SCpnt));
  395. goto out;
  396. }
  397. if (sdp->changed) {
  398. /*
  399. * quietly refuse to do anything to a changed disc until
  400. * the changed bit has been reset
  401. */
  402. /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
  403. goto out;
  404. }
  405. /*
  406. * Some SD card readers can't handle multi-sector accesses which touch
  407. * the last one or two hardware sectors. Split accesses as needed.
  408. */
  409. threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
  410. (sdp->sector_size / 512);
  411. if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
  412. if (block < threshold) {
  413. /* Access up to the threshold but not beyond */
  414. this_count = threshold - block;
  415. } else {
  416. /* Access only a single hardware sector */
  417. this_count = sdp->sector_size / 512;
  418. }
  419. }
  420. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
  421. (unsigned long long)block));
  422. /*
  423. * If we have a 1K hardware sectorsize, prevent access to single
  424. * 512 byte sectors. In theory we could handle this - in fact
  425. * the scsi cdrom driver must be able to handle this because
  426. * we typically use 1K blocksizes, and cdroms typically have
  427. * 2K hardware sectorsizes. Of course, things are simpler
  428. * with the cdrom, since it is read-only. For performance
  429. * reasons, the filesystems should be able to handle this
  430. * and not force the scsi disk driver to use bounce buffers
  431. * for this.
  432. */
  433. if (sdp->sector_size == 1024) {
  434. if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
  435. scmd_printk(KERN_ERR, SCpnt,
  436. "Bad block number requested\n");
  437. goto out;
  438. } else {
  439. block = block >> 1;
  440. this_count = this_count >> 1;
  441. }
  442. }
  443. if (sdp->sector_size == 2048) {
  444. if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
  445. scmd_printk(KERN_ERR, SCpnt,
  446. "Bad block number requested\n");
  447. goto out;
  448. } else {
  449. block = block >> 2;
  450. this_count = this_count >> 2;
  451. }
  452. }
  453. if (sdp->sector_size == 4096) {
  454. if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
  455. scmd_printk(KERN_ERR, SCpnt,
  456. "Bad block number requested\n");
  457. goto out;
  458. } else {
  459. block = block >> 3;
  460. this_count = this_count >> 3;
  461. }
  462. }
  463. if (rq_data_dir(rq) == WRITE) {
  464. if (!sdp->writeable) {
  465. goto out;
  466. }
  467. SCpnt->cmnd[0] = WRITE_6;
  468. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  469. if (blk_integrity_rq(rq) &&
  470. sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
  471. goto out;
  472. } else if (rq_data_dir(rq) == READ) {
  473. SCpnt->cmnd[0] = READ_6;
  474. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  475. } else {
  476. scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
  477. goto out;
  478. }
  479. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  480. "%s %d/%u 512 byte blocks.\n",
  481. (rq_data_dir(rq) == WRITE) ?
  482. "writing" : "reading", this_count,
  483. blk_rq_sectors(rq)));
  484. /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
  485. host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
  486. if (host_dif)
  487. protect = 1 << 5;
  488. else
  489. protect = 0;
  490. if (host_dif == SD_DIF_TYPE2_PROTECTION) {
  491. SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
  492. if (unlikely(SCpnt->cmnd == NULL)) {
  493. ret = BLKPREP_DEFER;
  494. goto out;
  495. }
  496. SCpnt->cmd_len = SD_EXT_CDB_SIZE;
  497. memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
  498. SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
  499. SCpnt->cmnd[7] = 0x18;
  500. SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
  501. SCpnt->cmnd[10] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
  502. /* LBA */
  503. SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  504. SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  505. SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  506. SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  507. SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
  508. SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
  509. SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
  510. SCpnt->cmnd[19] = (unsigned char) block & 0xff;
  511. /* Expected Indirect LBA */
  512. SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
  513. SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
  514. SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
  515. SCpnt->cmnd[23] = (unsigned char) block & 0xff;
  516. /* Transfer length */
  517. SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
  518. SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
  519. SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
  520. SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
  521. } else if (block > 0xffffffff) {
  522. SCpnt->cmnd[0] += READ_16 - READ_6;
  523. SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
  524. SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  525. SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  526. SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  527. SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  528. SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
  529. SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
  530. SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
  531. SCpnt->cmnd[9] = (unsigned char) block & 0xff;
  532. SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
  533. SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
  534. SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
  535. SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
  536. SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
  537. } else if ((this_count > 0xff) || (block > 0x1fffff) ||
  538. scsi_device_protection(SCpnt->device) ||
  539. SCpnt->device->use_10_for_rw) {
  540. if (this_count > 0xffff)
  541. this_count = 0xffff;
  542. SCpnt->cmnd[0] += READ_10 - READ_6;
  543. SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
  544. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  545. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  546. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  547. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  548. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  549. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  550. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  551. } else {
  552. if (unlikely(blk_fua_rq(rq))) {
  553. /*
  554. * This happens only if this drive failed
  555. * 10byte rw command with ILLEGAL_REQUEST
  556. * during operation and thus turned off
  557. * use_10_for_rw.
  558. */
  559. scmd_printk(KERN_ERR, SCpnt,
  560. "FUA write on READ/WRITE(6) drive\n");
  561. goto out;
  562. }
  563. SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  564. SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
  565. SCpnt->cmnd[3] = (unsigned char) block & 0xff;
  566. SCpnt->cmnd[4] = (unsigned char) this_count;
  567. SCpnt->cmnd[5] = 0;
  568. }
  569. SCpnt->sdb.length = this_count * sdp->sector_size;
  570. /* If DIF or DIX is enabled, tell HBA how to handle request */
  571. if (host_dif || scsi_prot_sg_count(SCpnt))
  572. sd_prot_op(SCpnt, host_dif);
  573. /*
  574. * We shouldn't disconnect in the middle of a sector, so with a dumb
  575. * host adapter, it's safe to assume that we can at least transfer
  576. * this many bytes between each connect / disconnect.
  577. */
  578. SCpnt->transfersize = sdp->sector_size;
  579. SCpnt->underflow = this_count << 9;
  580. SCpnt->allowed = SD_MAX_RETRIES;
  581. /*
  582. * This indicates that the command is ready from our end to be
  583. * queued.
  584. */
  585. ret = BLKPREP_OK;
  586. out:
  587. return scsi_prep_return(q, rq, ret);
  588. }
  589. /**
  590. * sd_open - open a scsi disk device
  591. * @inode: only i_rdev member may be used
  592. * @filp: only f_mode and f_flags may be used
  593. *
  594. * Returns 0 if successful. Returns a negated errno value in case
  595. * of error.
  596. *
  597. * Note: This can be called from a user context (e.g. fsck(1) )
  598. * or from within the kernel (e.g. as a result of a mount(1) ).
  599. * In the latter case @inode and @filp carry an abridged amount
  600. * of information as noted above.
  601. **/
  602. static int sd_open(struct block_device *bdev, fmode_t mode)
  603. {
  604. struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
  605. struct scsi_device *sdev;
  606. int retval;
  607. if (!sdkp)
  608. return -ENXIO;
  609. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
  610. sdev = sdkp->device;
  611. /*
  612. * If the device is in error recovery, wait until it is done.
  613. * If the device is offline, then disallow any access to it.
  614. */
  615. retval = -ENXIO;
  616. if (!scsi_block_when_processing_errors(sdev))
  617. goto error_out;
  618. if (sdev->removable || sdkp->write_prot)
  619. check_disk_change(bdev);
  620. /*
  621. * If the drive is empty, just let the open fail.
  622. */
  623. retval = -ENOMEDIUM;
  624. if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
  625. goto error_out;
  626. /*
  627. * If the device has the write protect tab set, have the open fail
  628. * if the user expects to be able to write to the thing.
  629. */
  630. retval = -EROFS;
  631. if (sdkp->write_prot && (mode & FMODE_WRITE))
  632. goto error_out;
  633. /*
  634. * It is possible that the disk changing stuff resulted in
  635. * the device being taken offline. If this is the case,
  636. * report this to the user, and don't pretend that the
  637. * open actually succeeded.
  638. */
  639. retval = -ENXIO;
  640. if (!scsi_device_online(sdev))
  641. goto error_out;
  642. if (!sdkp->openers++ && sdev->removable) {
  643. if (scsi_block_when_processing_errors(sdev))
  644. scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  645. }
  646. return 0;
  647. error_out:
  648. scsi_disk_put(sdkp);
  649. return retval;
  650. }
  651. /**
  652. * sd_release - invoked when the (last) close(2) is called on this
  653. * scsi disk.
  654. * @inode: only i_rdev member may be used
  655. * @filp: only f_mode and f_flags may be used
  656. *
  657. * Returns 0.
  658. *
  659. * Note: may block (uninterruptible) if error recovery is underway
  660. * on this disk.
  661. **/
  662. static int sd_release(struct gendisk *disk, fmode_t mode)
  663. {
  664. struct scsi_disk *sdkp = scsi_disk(disk);
  665. struct scsi_device *sdev = sdkp->device;
  666. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
  667. if (!--sdkp->openers && sdev->removable) {
  668. if (scsi_block_when_processing_errors(sdev))
  669. scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  670. }
  671. /*
  672. * XXX and what if there are packets in flight and this close()
  673. * XXX is followed by a "rmmod sd_mod"?
  674. */
  675. scsi_disk_put(sdkp);
  676. return 0;
  677. }
  678. static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  679. {
  680. struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
  681. struct scsi_device *sdp = sdkp->device;
  682. struct Scsi_Host *host = sdp->host;
  683. int diskinfo[4];
  684. /* default to most commonly used values */
  685. diskinfo[0] = 0x40; /* 1 << 6 */
  686. diskinfo[1] = 0x20; /* 1 << 5 */
  687. diskinfo[2] = sdkp->capacity >> 11;
  688. /* override with calculated, extended default, or driver values */
  689. if (host->hostt->bios_param)
  690. host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
  691. else
  692. scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
  693. geo->heads = diskinfo[0];
  694. geo->sectors = diskinfo[1];
  695. geo->cylinders = diskinfo[2];
  696. return 0;
  697. }
  698. /**
  699. * sd_ioctl - process an ioctl
  700. * @inode: only i_rdev/i_bdev members may be used
  701. * @filp: only f_mode and f_flags may be used
  702. * @cmd: ioctl command number
  703. * @arg: this is third argument given to ioctl(2) system call.
  704. * Often contains a pointer.
  705. *
  706. * Returns 0 if successful (some ioctls return postive numbers on
  707. * success as well). Returns a negated errno value in case of error.
  708. *
  709. * Note: most ioctls are forward onto the block subsystem or further
  710. * down in the scsi subsystem.
  711. **/
  712. static int sd_ioctl(struct block_device *bdev, fmode_t mode,
  713. unsigned int cmd, unsigned long arg)
  714. {
  715. struct gendisk *disk = bdev->bd_disk;
  716. struct scsi_device *sdp = scsi_disk(disk)->device;
  717. void __user *p = (void __user *)arg;
  718. int error;
  719. SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
  720. disk->disk_name, cmd));
  721. /*
  722. * If we are in the middle of error recovery, don't let anyone
  723. * else try and use this device. Also, if error recovery fails, it
  724. * may try and take the device offline, in which case all further
  725. * access to the device is prohibited.
  726. */
  727. error = scsi_nonblockable_ioctl(sdp, cmd, p,
  728. (mode & FMODE_NDELAY) != 0);
  729. if (!scsi_block_when_processing_errors(sdp) || !error)
  730. return error;
  731. /*
  732. * Send SCSI addressing ioctls directly to mid level, send other
  733. * ioctls to block level and then onto mid level if they can't be
  734. * resolved.
  735. */
  736. switch (cmd) {
  737. case SCSI_IOCTL_GET_IDLUN:
  738. case SCSI_IOCTL_GET_BUS_NUMBER:
  739. return scsi_ioctl(sdp, cmd, p);
  740. default:
  741. error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p);
  742. if (error != -ENOTTY)
  743. return error;
  744. }
  745. return scsi_ioctl(sdp, cmd, p);
  746. }
  747. static void set_media_not_present(struct scsi_disk *sdkp)
  748. {
  749. sdkp->media_present = 0;
  750. sdkp->capacity = 0;
  751. sdkp->device->changed = 1;
  752. }
  753. /**
  754. * sd_media_changed - check if our medium changed
  755. * @disk: kernel device descriptor
  756. *
  757. * Returns 0 if not applicable or no change; 1 if change
  758. *
  759. * Note: this function is invoked from the block subsystem.
  760. **/
  761. static int sd_media_changed(struct gendisk *disk)
  762. {
  763. struct scsi_disk *sdkp = scsi_disk(disk);
  764. struct scsi_device *sdp = sdkp->device;
  765. struct scsi_sense_hdr *sshdr = NULL;
  766. int retval;
  767. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
  768. if (!sdp->removable)
  769. return 0;
  770. /*
  771. * If the device is offline, don't send any commands - just pretend as
  772. * if the command failed. If the device ever comes back online, we
  773. * can deal with it then. It is only because of unrecoverable errors
  774. * that we would ever take a device offline in the first place.
  775. */
  776. if (!scsi_device_online(sdp)) {
  777. set_media_not_present(sdkp);
  778. retval = 1;
  779. goto out;
  780. }
  781. /*
  782. * Using TEST_UNIT_READY enables differentiation between drive with
  783. * no cartridge loaded - NOT READY, drive with changed cartridge -
  784. * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
  785. *
  786. * Drives that auto spin down. eg iomega jaz 1G, will be started
  787. * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
  788. * sd_revalidate() is called.
  789. */
  790. retval = -ENODEV;
  791. if (scsi_block_when_processing_errors(sdp)) {
  792. sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
  793. retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
  794. sshdr);
  795. }
  796. /*
  797. * Unable to test, unit probably not ready. This usually
  798. * means there is no disc in the drive. Mark as changed,
  799. * and we will figure it out later once the drive is
  800. * available again.
  801. */
  802. if (retval || (scsi_sense_valid(sshdr) &&
  803. /* 0x3a is medium not present */
  804. sshdr->asc == 0x3a)) {
  805. set_media_not_present(sdkp);
  806. retval = 1;
  807. goto out;
  808. }
  809. /*
  810. * For removable scsi disk we have to recognise the presence
  811. * of a disk in the drive. This is kept in the struct scsi_disk
  812. * struct and tested at open ! Daniel Roche (dan@lectra.fr)
  813. */
  814. sdkp->media_present = 1;
  815. retval = sdp->changed;
  816. sdp->changed = 0;
  817. out:
  818. if (retval != sdkp->previous_state)
  819. sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
  820. sdkp->previous_state = retval;
  821. kfree(sshdr);
  822. return retval;
  823. }
  824. static int sd_sync_cache(struct scsi_disk *sdkp)
  825. {
  826. int retries, res;
  827. struct scsi_device *sdp = sdkp->device;
  828. struct scsi_sense_hdr sshdr;
  829. if (!scsi_device_online(sdp))
  830. return -ENODEV;
  831. for (retries = 3; retries > 0; --retries) {
  832. unsigned char cmd[10] = { 0 };
  833. cmd[0] = SYNCHRONIZE_CACHE;
  834. /*
  835. * Leave the rest of the command zero to indicate
  836. * flush everything.
  837. */
  838. res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  839. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  840. if (res == 0)
  841. break;
  842. }
  843. if (res) {
  844. sd_print_result(sdkp, res);
  845. if (driver_byte(res) & DRIVER_SENSE)
  846. sd_print_sense_hdr(sdkp, &sshdr);
  847. }
  848. if (res)
  849. return -EIO;
  850. return 0;
  851. }
  852. static void sd_prepare_flush(struct request_queue *q, struct request *rq)
  853. {
  854. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  855. rq->timeout = SD_TIMEOUT;
  856. rq->cmd[0] = SYNCHRONIZE_CACHE;
  857. rq->cmd_len = 10;
  858. }
  859. static void sd_rescan(struct device *dev)
  860. {
  861. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  862. if (sdkp) {
  863. revalidate_disk(sdkp->disk);
  864. scsi_disk_put(sdkp);
  865. }
  866. }
  867. #ifdef CONFIG_COMPAT
  868. /*
  869. * This gets directly called from VFS. When the ioctl
  870. * is not recognized we go back to the other translation paths.
  871. */
  872. static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
  873. unsigned int cmd, unsigned long arg)
  874. {
  875. struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
  876. /*
  877. * If we are in the middle of error recovery, don't let anyone
  878. * else try and use this device. Also, if error recovery fails, it
  879. * may try and take the device offline, in which case all further
  880. * access to the device is prohibited.
  881. */
  882. if (!scsi_block_when_processing_errors(sdev))
  883. return -ENODEV;
  884. if (sdev->host->hostt->compat_ioctl) {
  885. int ret;
  886. ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
  887. return ret;
  888. }
  889. /*
  890. * Let the static ioctl translation table take care of it.
  891. */
  892. return -ENOIOCTLCMD;
  893. }
  894. #endif
  895. static const struct block_device_operations sd_fops = {
  896. .owner = THIS_MODULE,
  897. .open = sd_open,
  898. .release = sd_release,
  899. .locked_ioctl = sd_ioctl,
  900. .getgeo = sd_getgeo,
  901. #ifdef CONFIG_COMPAT
  902. .compat_ioctl = sd_compat_ioctl,
  903. #endif
  904. .media_changed = sd_media_changed,
  905. .revalidate_disk = sd_revalidate_disk,
  906. };
  907. static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
  908. {
  909. u64 start_lba = blk_rq_pos(scmd->request);
  910. u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
  911. u64 bad_lba;
  912. int info_valid;
  913. if (!blk_fs_request(scmd->request))
  914. return 0;
  915. info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
  916. SCSI_SENSE_BUFFERSIZE,
  917. &bad_lba);
  918. if (!info_valid)
  919. return 0;
  920. if (scsi_bufflen(scmd) <= scmd->device->sector_size)
  921. return 0;
  922. if (scmd->device->sector_size < 512) {
  923. /* only legitimate sector_size here is 256 */
  924. start_lba <<= 1;
  925. end_lba <<= 1;
  926. } else {
  927. /* be careful ... don't want any overflows */
  928. u64 factor = scmd->device->sector_size / 512;
  929. do_div(start_lba, factor);
  930. do_div(end_lba, factor);
  931. }
  932. /* The bad lba was reported incorrectly, we have no idea where
  933. * the error is.
  934. */
  935. if (bad_lba < start_lba || bad_lba >= end_lba)
  936. return 0;
  937. /* This computation should always be done in terms of
  938. * the resolution of the device's medium.
  939. */
  940. return (bad_lba - start_lba) * scmd->device->sector_size;
  941. }
  942. /**
  943. * sd_done - bottom half handler: called when the lower level
  944. * driver has completed (successfully or otherwise) a scsi command.
  945. * @SCpnt: mid-level's per command structure.
  946. *
  947. * Note: potentially run from within an ISR. Must not block.
  948. **/
  949. static int sd_done(struct scsi_cmnd *SCpnt)
  950. {
  951. int result = SCpnt->result;
  952. unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
  953. struct scsi_sense_hdr sshdr;
  954. struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
  955. int sense_valid = 0;
  956. int sense_deferred = 0;
  957. if (result) {
  958. sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
  959. if (sense_valid)
  960. sense_deferred = scsi_sense_is_deferred(&sshdr);
  961. }
  962. #ifdef CONFIG_SCSI_LOGGING
  963. SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
  964. if (sense_valid) {
  965. SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
  966. "sd_done: sb[respc,sk,asc,"
  967. "ascq]=%x,%x,%x,%x\n",
  968. sshdr.response_code,
  969. sshdr.sense_key, sshdr.asc,
  970. sshdr.ascq));
  971. }
  972. #endif
  973. if (driver_byte(result) != DRIVER_SENSE &&
  974. (!sense_valid || sense_deferred))
  975. goto out;
  976. switch (sshdr.sense_key) {
  977. case HARDWARE_ERROR:
  978. case MEDIUM_ERROR:
  979. good_bytes = sd_completed_bytes(SCpnt);
  980. break;
  981. case RECOVERED_ERROR:
  982. good_bytes = scsi_bufflen(SCpnt);
  983. break;
  984. case NO_SENSE:
  985. /* This indicates a false check condition, so ignore it. An
  986. * unknown amount of data was transferred so treat it as an
  987. * error.
  988. */
  989. scsi_print_sense("sd", SCpnt);
  990. SCpnt->result = 0;
  991. memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  992. break;
  993. case ABORTED_COMMAND:
  994. if (sshdr.asc == 0x10) { /* DIF: Disk detected corruption */
  995. scsi_print_result(SCpnt);
  996. scsi_print_sense("sd", SCpnt);
  997. good_bytes = sd_completed_bytes(SCpnt);
  998. }
  999. break;
  1000. case ILLEGAL_REQUEST:
  1001. if (sshdr.asc == 0x10) { /* DIX: HBA detected corruption */
  1002. scsi_print_result(SCpnt);
  1003. scsi_print_sense("sd", SCpnt);
  1004. good_bytes = sd_completed_bytes(SCpnt);
  1005. }
  1006. break;
  1007. default:
  1008. break;
  1009. }
  1010. out:
  1011. if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
  1012. sd_dif_complete(SCpnt, good_bytes);
  1013. if (scsi_host_dif_capable(sdkp->device->host, sdkp->protection_type)
  1014. == SD_DIF_TYPE2_PROTECTION && SCpnt->cmnd != SCpnt->request->cmd)
  1015. mempool_free(SCpnt->cmnd, sd_cdb_pool);
  1016. return good_bytes;
  1017. }
  1018. static int media_not_present(struct scsi_disk *sdkp,
  1019. struct scsi_sense_hdr *sshdr)
  1020. {
  1021. if (!scsi_sense_valid(sshdr))
  1022. return 0;
  1023. /* not invoked for commands that could return deferred errors */
  1024. if (sshdr->sense_key != NOT_READY &&
  1025. sshdr->sense_key != UNIT_ATTENTION)
  1026. return 0;
  1027. if (sshdr->asc != 0x3A) /* medium not present */
  1028. return 0;
  1029. set_media_not_present(sdkp);
  1030. return 1;
  1031. }
  1032. /*
  1033. * spinup disk - called only in sd_revalidate_disk()
  1034. */
  1035. static void
  1036. sd_spinup_disk(struct scsi_disk *sdkp)
  1037. {
  1038. unsigned char cmd[10];
  1039. unsigned long spintime_expire = 0;
  1040. int retries, spintime;
  1041. unsigned int the_result;
  1042. struct scsi_sense_hdr sshdr;
  1043. int sense_valid = 0;
  1044. spintime = 0;
  1045. /* Spin up drives, as required. Only do this at boot time */
  1046. /* Spinup needs to be done for module loads too. */
  1047. do {
  1048. retries = 0;
  1049. do {
  1050. cmd[0] = TEST_UNIT_READY;
  1051. memset((void *) &cmd[1], 0, 9);
  1052. the_result = scsi_execute_req(sdkp->device, cmd,
  1053. DMA_NONE, NULL, 0,
  1054. &sshdr, SD_TIMEOUT,
  1055. SD_MAX_RETRIES, NULL);
  1056. /*
  1057. * If the drive has indicated to us that it
  1058. * doesn't have any media in it, don't bother
  1059. * with any more polling.
  1060. */
  1061. if (media_not_present(sdkp, &sshdr))
  1062. return;
  1063. if (the_result)
  1064. sense_valid = scsi_sense_valid(&sshdr);
  1065. retries++;
  1066. } while (retries < 3 &&
  1067. (!scsi_status_is_good(the_result) ||
  1068. ((driver_byte(the_result) & DRIVER_SENSE) &&
  1069. sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
  1070. if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
  1071. /* no sense, TUR either succeeded or failed
  1072. * with a status error */
  1073. if(!spintime && !scsi_status_is_good(the_result)) {
  1074. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1075. sd_print_result(sdkp, the_result);
  1076. }
  1077. break;
  1078. }
  1079. /*
  1080. * The device does not want the automatic start to be issued.
  1081. */
  1082. if (sdkp->device->no_start_on_add)
  1083. break;
  1084. if (sense_valid && sshdr.sense_key == NOT_READY) {
  1085. if (sshdr.asc == 4 && sshdr.ascq == 3)
  1086. break; /* manual intervention required */
  1087. if (sshdr.asc == 4 && sshdr.ascq == 0xb)
  1088. break; /* standby */
  1089. if (sshdr.asc == 4 && sshdr.ascq == 0xc)
  1090. break; /* unavailable */
  1091. /*
  1092. * Issue command to spin up drive when not ready
  1093. */
  1094. if (!spintime) {
  1095. sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
  1096. cmd[0] = START_STOP;
  1097. cmd[1] = 1; /* Return immediately */
  1098. memset((void *) &cmd[2], 0, 8);
  1099. cmd[4] = 1; /* Start spin cycle */
  1100. if (sdkp->device->start_stop_pwr_cond)
  1101. cmd[4] |= 1 << 4;
  1102. scsi_execute_req(sdkp->device, cmd, DMA_NONE,
  1103. NULL, 0, &sshdr,
  1104. SD_TIMEOUT, SD_MAX_RETRIES,
  1105. NULL);
  1106. spintime_expire = jiffies + 100 * HZ;
  1107. spintime = 1;
  1108. }
  1109. /* Wait 1 second for next try */
  1110. msleep(1000);
  1111. printk(".");
  1112. /*
  1113. * Wait for USB flash devices with slow firmware.
  1114. * Yes, this sense key/ASC combination shouldn't
  1115. * occur here. It's characteristic of these devices.
  1116. */
  1117. } else if (sense_valid &&
  1118. sshdr.sense_key == UNIT_ATTENTION &&
  1119. sshdr.asc == 0x28) {
  1120. if (!spintime) {
  1121. spintime_expire = jiffies + 5 * HZ;
  1122. spintime = 1;
  1123. }
  1124. /* Wait 1 second for next try */
  1125. msleep(1000);
  1126. } else {
  1127. /* we don't understand the sense code, so it's
  1128. * probably pointless to loop */
  1129. if(!spintime) {
  1130. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1131. sd_print_sense_hdr(sdkp, &sshdr);
  1132. }
  1133. break;
  1134. }
  1135. } while (spintime && time_before_eq(jiffies, spintime_expire));
  1136. if (spintime) {
  1137. if (scsi_status_is_good(the_result))
  1138. printk("ready\n");
  1139. else
  1140. printk("not responding...\n");
  1141. }
  1142. }
  1143. /*
  1144. * Determine whether disk supports Data Integrity Field.
  1145. */
  1146. void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1147. {
  1148. struct scsi_device *sdp = sdkp->device;
  1149. u8 type;
  1150. if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
  1151. return;
  1152. type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
  1153. if (type == sdkp->protection_type || !sdkp->first_scan)
  1154. return;
  1155. sdkp->protection_type = type;
  1156. if (type > SD_DIF_TYPE3_PROTECTION) {
  1157. sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \
  1158. "protection type %u. Disabling disk!\n", type);
  1159. sdkp->capacity = 0;
  1160. return;
  1161. }
  1162. if (scsi_host_dif_capable(sdp->host, type))
  1163. sd_printk(KERN_NOTICE, sdkp,
  1164. "Enabling DIF Type %u protection\n", type);
  1165. else
  1166. sd_printk(KERN_NOTICE, sdkp,
  1167. "Disabling DIF Type %u protection\n", type);
  1168. }
  1169. static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1170. struct scsi_sense_hdr *sshdr, int sense_valid,
  1171. int the_result)
  1172. {
  1173. sd_print_result(sdkp, the_result);
  1174. if (driver_byte(the_result) & DRIVER_SENSE)
  1175. sd_print_sense_hdr(sdkp, sshdr);
  1176. else
  1177. sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
  1178. /*
  1179. * Set dirty bit for removable devices if not ready -
  1180. * sometimes drives will not report this properly.
  1181. */
  1182. if (sdp->removable &&
  1183. sense_valid && sshdr->sense_key == NOT_READY)
  1184. sdp->changed = 1;
  1185. /*
  1186. * We used to set media_present to 0 here to indicate no media
  1187. * in the drive, but some drives fail read capacity even with
  1188. * media present, so we can't do that.
  1189. */
  1190. sdkp->capacity = 0; /* unknown mapped to zero - as usual */
  1191. }
  1192. #define RC16_LEN 32
  1193. #if RC16_LEN > SD_BUF_SIZE
  1194. #error RC16_LEN must not be more than SD_BUF_SIZE
  1195. #endif
  1196. static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1197. unsigned char *buffer)
  1198. {
  1199. unsigned char cmd[16];
  1200. struct scsi_sense_hdr sshdr;
  1201. int sense_valid = 0;
  1202. int the_result;
  1203. int retries = 3;
  1204. unsigned int alignment;
  1205. unsigned long long lba;
  1206. unsigned sector_size;
  1207. do {
  1208. memset(cmd, 0, 16);
  1209. cmd[0] = SERVICE_ACTION_IN;
  1210. cmd[1] = SAI_READ_CAPACITY_16;
  1211. cmd[13] = RC16_LEN;
  1212. memset(buffer, 0, RC16_LEN);
  1213. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1214. buffer, RC16_LEN, &sshdr,
  1215. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1216. if (media_not_present(sdkp, &sshdr))
  1217. return -ENODEV;
  1218. if (the_result) {
  1219. sense_valid = scsi_sense_valid(&sshdr);
  1220. if (sense_valid &&
  1221. sshdr.sense_key == ILLEGAL_REQUEST &&
  1222. (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
  1223. sshdr.ascq == 0x00)
  1224. /* Invalid Command Operation Code or
  1225. * Invalid Field in CDB, just retry
  1226. * silently with RC10 */
  1227. return -EINVAL;
  1228. }
  1229. retries--;
  1230. } while (the_result && retries);
  1231. if (the_result) {
  1232. sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
  1233. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1234. return -EINVAL;
  1235. }
  1236. sector_size = get_unaligned_be32(&buffer[8]);
  1237. lba = get_unaligned_be64(&buffer[0]);
  1238. sd_read_protection_type(sdkp, buffer);
  1239. if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
  1240. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1241. "kernel compiled with support for large block "
  1242. "devices.\n");
  1243. sdkp->capacity = 0;
  1244. return -EOVERFLOW;
  1245. }
  1246. /* Logical blocks per physical block exponent */
  1247. sdkp->hw_sector_size = (1 << (buffer[13] & 0xf)) * sector_size;
  1248. /* Lowest aligned logical block */
  1249. alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
  1250. blk_queue_alignment_offset(sdp->request_queue, alignment);
  1251. if (alignment && sdkp->first_scan)
  1252. sd_printk(KERN_NOTICE, sdkp,
  1253. "physical block alignment offset: %u\n", alignment);
  1254. sdkp->capacity = lba + 1;
  1255. return sector_size;
  1256. }
  1257. static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1258. unsigned char *buffer)
  1259. {
  1260. unsigned char cmd[16];
  1261. struct scsi_sense_hdr sshdr;
  1262. int sense_valid = 0;
  1263. int the_result;
  1264. int retries = 3;
  1265. sector_t lba;
  1266. unsigned sector_size;
  1267. do {
  1268. cmd[0] = READ_CAPACITY;
  1269. memset(&cmd[1], 0, 9);
  1270. memset(buffer, 0, 8);
  1271. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1272. buffer, 8, &sshdr,
  1273. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1274. if (media_not_present(sdkp, &sshdr))
  1275. return -ENODEV;
  1276. if (the_result)
  1277. sense_valid = scsi_sense_valid(&sshdr);
  1278. retries--;
  1279. } while (the_result && retries);
  1280. if (the_result) {
  1281. sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
  1282. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1283. return -EINVAL;
  1284. }
  1285. sector_size = get_unaligned_be32(&buffer[4]);
  1286. lba = get_unaligned_be32(&buffer[0]);
  1287. if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
  1288. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1289. "kernel compiled with support for large block "
  1290. "devices.\n");
  1291. sdkp->capacity = 0;
  1292. return -EOVERFLOW;
  1293. }
  1294. sdkp->capacity = lba + 1;
  1295. sdkp->hw_sector_size = sector_size;
  1296. return sector_size;
  1297. }
  1298. static int sd_try_rc16_first(struct scsi_device *sdp)
  1299. {
  1300. if (sdp->scsi_level > SCSI_SPC_2)
  1301. return 1;
  1302. if (scsi_device_protection(sdp))
  1303. return 1;
  1304. return 0;
  1305. }
  1306. /*
  1307. * read disk capacity
  1308. */
  1309. static void
  1310. sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
  1311. {
  1312. int sector_size;
  1313. struct scsi_device *sdp = sdkp->device;
  1314. sector_t old_capacity = sdkp->capacity;
  1315. if (sd_try_rc16_first(sdp)) {
  1316. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1317. if (sector_size == -EOVERFLOW)
  1318. goto got_data;
  1319. if (sector_size == -ENODEV)
  1320. return;
  1321. if (sector_size < 0)
  1322. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1323. if (sector_size < 0)
  1324. return;
  1325. } else {
  1326. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1327. if (sector_size == -EOVERFLOW)
  1328. goto got_data;
  1329. if (sector_size < 0)
  1330. return;
  1331. if ((sizeof(sdkp->capacity) > 4) &&
  1332. (sdkp->capacity > 0xffffffffULL)) {
  1333. int old_sector_size = sector_size;
  1334. sd_printk(KERN_NOTICE, sdkp, "Very big device. "
  1335. "Trying to use READ CAPACITY(16).\n");
  1336. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1337. if (sector_size < 0) {
  1338. sd_printk(KERN_NOTICE, sdkp,
  1339. "Using 0xffffffff as device size\n");
  1340. sdkp->capacity = 1 + (sector_t) 0xffffffff;
  1341. sector_size = old_sector_size;
  1342. goto got_data;
  1343. }
  1344. }
  1345. }
  1346. /* Some devices are known to return the total number of blocks,
  1347. * not the highest block number. Some devices have versions
  1348. * which do this and others which do not. Some devices we might
  1349. * suspect of doing this but we don't know for certain.
  1350. *
  1351. * If we know the reported capacity is wrong, decrement it. If
  1352. * we can only guess, then assume the number of blocks is even
  1353. * (usually true but not always) and err on the side of lowering
  1354. * the capacity.
  1355. */
  1356. if (sdp->fix_capacity ||
  1357. (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
  1358. sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
  1359. "from its reported value: %llu\n",
  1360. (unsigned long long) sdkp->capacity);
  1361. --sdkp->capacity;
  1362. }
  1363. got_data:
  1364. if (sector_size == 0) {
  1365. sector_size = 512;
  1366. sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
  1367. "assuming 512.\n");
  1368. }
  1369. if (sector_size != 512 &&
  1370. sector_size != 1024 &&
  1371. sector_size != 2048 &&
  1372. sector_size != 4096 &&
  1373. sector_size != 256) {
  1374. sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
  1375. sector_size);
  1376. /*
  1377. * The user might want to re-format the drive with
  1378. * a supported sectorsize. Once this happens, it
  1379. * would be relatively trivial to set the thing up.
  1380. * For this reason, we leave the thing in the table.
  1381. */
  1382. sdkp->capacity = 0;
  1383. /*
  1384. * set a bogus sector size so the normal read/write
  1385. * logic in the block layer will eventually refuse any
  1386. * request on this device without tripping over power
  1387. * of two sector size assumptions
  1388. */
  1389. sector_size = 512;
  1390. }
  1391. blk_queue_logical_block_size(sdp->request_queue, sector_size);
  1392. {
  1393. char cap_str_2[10], cap_str_10[10];
  1394. u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
  1395. string_get_size(sz, STRING_UNITS_2, cap_str_2,
  1396. sizeof(cap_str_2));
  1397. string_get_size(sz, STRING_UNITS_10, cap_str_10,
  1398. sizeof(cap_str_10));
  1399. if (sdkp->first_scan || old_capacity != sdkp->capacity) {
  1400. sd_printk(KERN_NOTICE, sdkp,
  1401. "%llu %d-byte logical blocks: (%s/%s)\n",
  1402. (unsigned long long)sdkp->capacity,
  1403. sector_size, cap_str_10, cap_str_2);
  1404. if (sdkp->hw_sector_size != sector_size)
  1405. sd_printk(KERN_NOTICE, sdkp,
  1406. "%u-byte physical blocks\n",
  1407. sdkp->hw_sector_size);
  1408. }
  1409. }
  1410. /* Rescale capacity to 512-byte units */
  1411. if (sector_size == 4096)
  1412. sdkp->capacity <<= 3;
  1413. else if (sector_size == 2048)
  1414. sdkp->capacity <<= 2;
  1415. else if (sector_size == 1024)
  1416. sdkp->capacity <<= 1;
  1417. else if (sector_size == 256)
  1418. sdkp->capacity >>= 1;
  1419. blk_queue_physical_block_size(sdp->request_queue, sdkp->hw_sector_size);
  1420. sdkp->device->sector_size = sector_size;
  1421. }
  1422. /* called with buffer of length 512 */
  1423. static inline int
  1424. sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
  1425. unsigned char *buffer, int len, struct scsi_mode_data *data,
  1426. struct scsi_sense_hdr *sshdr)
  1427. {
  1428. return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
  1429. SD_TIMEOUT, SD_MAX_RETRIES, data,
  1430. sshdr);
  1431. }
  1432. /*
  1433. * read write protect setting, if possible - called only in sd_revalidate_disk()
  1434. * called with buffer of length SD_BUF_SIZE
  1435. */
  1436. static void
  1437. sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
  1438. {
  1439. int res;
  1440. struct scsi_device *sdp = sdkp->device;
  1441. struct scsi_mode_data data;
  1442. int old_wp = sdkp->write_prot;
  1443. set_disk_ro(sdkp->disk, 0);
  1444. if (sdp->skip_ms_page_3f) {
  1445. sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
  1446. return;
  1447. }
  1448. if (sdp->use_192_bytes_for_3f) {
  1449. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
  1450. } else {
  1451. /*
  1452. * First attempt: ask for all pages (0x3F), but only 4 bytes.
  1453. * We have to start carefully: some devices hang if we ask
  1454. * for more than is available.
  1455. */
  1456. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
  1457. /*
  1458. * Second attempt: ask for page 0 When only page 0 is
  1459. * implemented, a request for page 3F may return Sense Key
  1460. * 5: Illegal Request, Sense Code 24: Invalid field in
  1461. * CDB.
  1462. */
  1463. if (!scsi_status_is_good(res))
  1464. res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
  1465. /*
  1466. * Third attempt: ask 255 bytes, as we did earlier.
  1467. */
  1468. if (!scsi_status_is_good(res))
  1469. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
  1470. &data, NULL);
  1471. }
  1472. if (!scsi_status_is_good(res)) {
  1473. sd_printk(KERN_WARNING, sdkp,
  1474. "Test WP failed, assume Write Enabled\n");
  1475. } else {
  1476. sdkp->write_prot = ((data.device_specific & 0x80) != 0);
  1477. set_disk_ro(sdkp->disk, sdkp->write_prot);
  1478. if (sdkp->first_scan || old_wp != sdkp->write_prot) {
  1479. sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
  1480. sdkp->write_prot ? "on" : "off");
  1481. sd_printk(KERN_DEBUG, sdkp,
  1482. "Mode Sense: %02x %02x %02x %02x\n",
  1483. buffer[0], buffer[1], buffer[2], buffer[3]);
  1484. }
  1485. }
  1486. }
  1487. /*
  1488. * sd_read_cache_type - called only from sd_revalidate_disk()
  1489. * called with buffer of length SD_BUF_SIZE
  1490. */
  1491. static void
  1492. sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1493. {
  1494. int len = 0, res;
  1495. struct scsi_device *sdp = sdkp->device;
  1496. int dbd;
  1497. int modepage;
  1498. struct scsi_mode_data data;
  1499. struct scsi_sense_hdr sshdr;
  1500. int old_wce = sdkp->WCE;
  1501. int old_rcd = sdkp->RCD;
  1502. int old_dpofua = sdkp->DPOFUA;
  1503. if (sdp->skip_ms_page_8)
  1504. goto defaults;
  1505. if (sdp->type == TYPE_RBC) {
  1506. modepage = 6;
  1507. dbd = 8;
  1508. } else {
  1509. modepage = 8;
  1510. dbd = 0;
  1511. }
  1512. /* cautiously ask */
  1513. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
  1514. if (!scsi_status_is_good(res))
  1515. goto bad_sense;
  1516. if (!data.header_length) {
  1517. modepage = 6;
  1518. sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
  1519. }
  1520. /* that went OK, now ask for the proper length */
  1521. len = data.length;
  1522. /*
  1523. * We're only interested in the first three bytes, actually.
  1524. * But the data cache page is defined for the first 20.
  1525. */
  1526. if (len < 3)
  1527. goto bad_sense;
  1528. if (len > 20)
  1529. len = 20;
  1530. /* Take headers and block descriptors into account */
  1531. len += data.header_length + data.block_descriptor_length;
  1532. if (len > SD_BUF_SIZE)
  1533. goto bad_sense;
  1534. /* Get the data */
  1535. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
  1536. if (scsi_status_is_good(res)) {
  1537. int offset = data.header_length + data.block_descriptor_length;
  1538. if (offset >= SD_BUF_SIZE - 2) {
  1539. sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
  1540. goto defaults;
  1541. }
  1542. if ((buffer[offset] & 0x3f) != modepage) {
  1543. sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
  1544. goto defaults;
  1545. }
  1546. if (modepage == 8) {
  1547. sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
  1548. sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
  1549. } else {
  1550. sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
  1551. sdkp->RCD = 0;
  1552. }
  1553. sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
  1554. if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
  1555. sd_printk(KERN_NOTICE, sdkp,
  1556. "Uses READ/WRITE(6), disabling FUA\n");
  1557. sdkp->DPOFUA = 0;
  1558. }
  1559. if (sdkp->first_scan || old_wce != sdkp->WCE ||
  1560. old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
  1561. sd_printk(KERN_NOTICE, sdkp,
  1562. "Write cache: %s, read cache: %s, %s\n",
  1563. sdkp->WCE ? "enabled" : "disabled",
  1564. sdkp->RCD ? "disabled" : "enabled",
  1565. sdkp->DPOFUA ? "supports DPO and FUA"
  1566. : "doesn't support DPO or FUA");
  1567. return;
  1568. }
  1569. bad_sense:
  1570. if (scsi_sense_valid(&sshdr) &&
  1571. sshdr.sense_key == ILLEGAL_REQUEST &&
  1572. sshdr.asc == 0x24 && sshdr.ascq == 0x0)
  1573. /* Invalid field in CDB */
  1574. sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
  1575. else
  1576. sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
  1577. defaults:
  1578. sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
  1579. sdkp->WCE = 0;
  1580. sdkp->RCD = 0;
  1581. sdkp->DPOFUA = 0;
  1582. }
  1583. /*
  1584. * The ATO bit indicates whether the DIF application tag is available
  1585. * for use by the operating system.
  1586. */
  1587. void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
  1588. {
  1589. int res, offset;
  1590. struct scsi_device *sdp = sdkp->device;
  1591. struct scsi_mode_data data;
  1592. struct scsi_sense_hdr sshdr;
  1593. if (sdp->type != TYPE_DISK)
  1594. return;
  1595. if (sdkp->protection_type == 0)
  1596. return;
  1597. res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
  1598. SD_MAX_RETRIES, &data, &sshdr);
  1599. if (!scsi_status_is_good(res) || !data.header_length ||
  1600. data.length < 6) {
  1601. sd_printk(KERN_WARNING, sdkp,
  1602. "getting Control mode page failed, assume no ATO\n");
  1603. if (scsi_sense_valid(&sshdr))
  1604. sd_print_sense_hdr(sdkp, &sshdr);
  1605. return;
  1606. }
  1607. offset = data.header_length + data.block_descriptor_length;
  1608. if ((buffer[offset] & 0x3f) != 0x0a) {
  1609. sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
  1610. return;
  1611. }
  1612. if ((buffer[offset + 5] & 0x80) == 0)
  1613. return;
  1614. sdkp->ATO = 1;
  1615. return;
  1616. }
  1617. /**
  1618. * sd_read_block_limits - Query disk device for preferred I/O sizes.
  1619. * @disk: disk to query
  1620. */
  1621. static void sd_read_block_limits(struct scsi_disk *sdkp)
  1622. {
  1623. unsigned int sector_sz = sdkp->device->sector_size;
  1624. char *buffer;
  1625. /* Block Limits VPD */
  1626. buffer = scsi_get_vpd_page(sdkp->device, 0xb0);
  1627. if (buffer == NULL)
  1628. return;
  1629. blk_queue_io_min(sdkp->disk->queue,
  1630. get_unaligned_be16(&buffer[6]) * sector_sz);
  1631. blk_queue_io_opt(sdkp->disk->queue,
  1632. get_unaligned_be32(&buffer[12]) * sector_sz);
  1633. kfree(buffer);
  1634. }
  1635. /**
  1636. * sd_read_block_characteristics - Query block dev. characteristics
  1637. * @disk: disk to query
  1638. */
  1639. static void sd_read_block_characteristics(struct scsi_disk *sdkp)
  1640. {
  1641. char *buffer;
  1642. u16 rot;
  1643. /* Block Device Characteristics VPD */
  1644. buffer = scsi_get_vpd_page(sdkp->device, 0xb1);
  1645. if (buffer == NULL)
  1646. return;
  1647. rot = get_unaligned_be16(&buffer[4]);
  1648. if (rot == 1)
  1649. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
  1650. kfree(buffer);
  1651. }
  1652. static int sd_try_extended_inquiry(struct scsi_device *sdp)
  1653. {
  1654. /*
  1655. * Although VPD inquiries can go to SCSI-2 type devices,
  1656. * some USB ones crash on receiving them, and the pages
  1657. * we currently ask for are for SPC-3 and beyond
  1658. */
  1659. if (sdp->scsi_level > SCSI_SPC_2)
  1660. return 1;
  1661. return 0;
  1662. }
  1663. /**
  1664. * sd_revalidate_disk - called the first time a new disk is seen,
  1665. * performs disk spin up, read_capacity, etc.
  1666. * @disk: struct gendisk we care about
  1667. **/
  1668. static int sd_revalidate_disk(struct gendisk *disk)
  1669. {
  1670. struct scsi_disk *sdkp = scsi_disk(disk);
  1671. struct scsi_device *sdp = sdkp->device;
  1672. unsigned char *buffer;
  1673. unsigned ordered;
  1674. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
  1675. "sd_revalidate_disk\n"));
  1676. /*
  1677. * If the device is offline, don't try and read capacity or any
  1678. * of the other niceties.
  1679. */
  1680. if (!scsi_device_online(sdp))
  1681. goto out;
  1682. buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
  1683. if (!buffer) {
  1684. sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
  1685. "allocation failure.\n");
  1686. goto out;
  1687. }
  1688. sd_spinup_disk(sdkp);
  1689. /*
  1690. * Without media there is no reason to ask; moreover, some devices
  1691. * react badly if we do.
  1692. */
  1693. if (sdkp->media_present) {
  1694. sd_read_capacity(sdkp, buffer);
  1695. if (sd_try_extended_inquiry(sdp)) {
  1696. sd_read_block_limits(sdkp);
  1697. sd_read_block_characteristics(sdkp);
  1698. }
  1699. sd_read_write_protect_flag(sdkp, buffer);
  1700. sd_read_cache_type(sdkp, buffer);
  1701. sd_read_app_tag_own(sdkp, buffer);
  1702. }
  1703. sdkp->first_scan = 0;
  1704. /*
  1705. * We now have all cache related info, determine how we deal
  1706. * with ordered requests. Note that as the current SCSI
  1707. * dispatch function can alter request order, we cannot use
  1708. * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
  1709. */
  1710. if (sdkp->WCE)
  1711. ordered = sdkp->DPOFUA
  1712. ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
  1713. else
  1714. ordered = QUEUE_ORDERED_DRAIN;
  1715. blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
  1716. set_capacity(disk, sdkp->capacity);
  1717. kfree(buffer);
  1718. out:
  1719. return 0;
  1720. }
  1721. /**
  1722. * sd_format_disk_name - format disk name
  1723. * @prefix: name prefix - ie. "sd" for SCSI disks
  1724. * @index: index of the disk to format name for
  1725. * @buf: output buffer
  1726. * @buflen: length of the output buffer
  1727. *
  1728. * SCSI disk names starts at sda. The 26th device is sdz and the
  1729. * 27th is sdaa. The last one for two lettered suffix is sdzz
  1730. * which is followed by sdaaa.
  1731. *
  1732. * This is basically 26 base counting with one extra 'nil' entry
  1733. * at the beggining from the second digit on and can be
  1734. * determined using similar method as 26 base conversion with the
  1735. * index shifted -1 after each digit is computed.
  1736. *
  1737. * CONTEXT:
  1738. * Don't care.
  1739. *
  1740. * RETURNS:
  1741. * 0 on success, -errno on failure.
  1742. */
  1743. static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
  1744. {
  1745. const int base = 'z' - 'a' + 1;
  1746. char *begin = buf + strlen(prefix);
  1747. char *end = buf + buflen;
  1748. char *p;
  1749. int unit;
  1750. p = end - 1;
  1751. *p = '\0';
  1752. unit = base;
  1753. do {
  1754. if (p == begin)
  1755. return -EINVAL;
  1756. *--p = 'a' + (index % unit);
  1757. index = (index / unit) - 1;
  1758. } while (index >= 0);
  1759. memmove(begin, p, end - p);
  1760. memcpy(buf, prefix, strlen(prefix));
  1761. return 0;
  1762. }
  1763. /*
  1764. * The asynchronous part of sd_probe
  1765. */
  1766. static void sd_probe_async(void *data, async_cookie_t cookie)
  1767. {
  1768. struct scsi_disk *sdkp = data;
  1769. struct scsi_device *sdp;
  1770. struct gendisk *gd;
  1771. u32 index;
  1772. struct device *dev;
  1773. sdp = sdkp->device;
  1774. gd = sdkp->disk;
  1775. index = sdkp->index;
  1776. dev = &sdp->sdev_gendev;
  1777. if (index < SD_MAX_DISKS) {
  1778. gd->major = sd_major((index & 0xf0) >> 4);
  1779. gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
  1780. gd->minors = SD_MINORS;
  1781. }
  1782. gd->fops = &sd_fops;
  1783. gd->private_data = &sdkp->driver;
  1784. gd->queue = sdkp->device->request_queue;
  1785. /* defaults, until the device tells us otherwise */
  1786. sdp->sector_size = 512;
  1787. sdkp->capacity = 0;
  1788. sdkp->media_present = 1;
  1789. sdkp->write_prot = 0;
  1790. sdkp->WCE = 0;
  1791. sdkp->RCD = 0;
  1792. sdkp->ATO = 0;
  1793. sdkp->first_scan = 1;
  1794. sd_revalidate_disk(gd);
  1795. blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
  1796. gd->driverfs_dev = &sdp->sdev_gendev;
  1797. gd->flags = GENHD_FL_EXT_DEVT | GENHD_FL_DRIVERFS;
  1798. if (sdp->removable)
  1799. gd->flags |= GENHD_FL_REMOVABLE;
  1800. dev_set_drvdata(dev, sdkp);
  1801. add_disk(gd);
  1802. sd_dif_config_host(sdkp);
  1803. sd_revalidate_disk(gd);
  1804. sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
  1805. sdp->removable ? "removable " : "");
  1806. put_device(&sdkp->dev);
  1807. }
  1808. /**
  1809. * sd_probe - called during driver initialization and whenever a
  1810. * new scsi device is attached to the system. It is called once
  1811. * for each scsi device (not just disks) present.
  1812. * @dev: pointer to device object
  1813. *
  1814. * Returns 0 if successful (or not interested in this scsi device
  1815. * (e.g. scanner)); 1 when there is an error.
  1816. *
  1817. * Note: this function is invoked from the scsi mid-level.
  1818. * This function sets up the mapping between a given
  1819. * <host,channel,id,lun> (found in sdp) and new device name
  1820. * (e.g. /dev/sda). More precisely it is the block device major
  1821. * and minor number that is chosen here.
  1822. *
  1823. * Assume sd_attach is not re-entrant (for time being)
  1824. * Also think about sd_attach() and sd_remove() running coincidentally.
  1825. **/
  1826. static int sd_probe(struct device *dev)
  1827. {
  1828. struct scsi_device *sdp = to_scsi_device(dev);
  1829. struct scsi_disk *sdkp;
  1830. struct gendisk *gd;
  1831. u32 index;
  1832. int error;
  1833. error = -ENODEV;
  1834. if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
  1835. goto out;
  1836. SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
  1837. "sd_attach\n"));
  1838. error = -ENOMEM;
  1839. sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
  1840. if (!sdkp)
  1841. goto out;
  1842. gd = alloc_disk(SD_MINORS);
  1843. if (!gd)
  1844. goto out_free;
  1845. do {
  1846. if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
  1847. goto out_put;
  1848. spin_lock(&sd_index_lock);
  1849. error = ida_get_new(&sd_index_ida, &index);
  1850. spin_unlock(&sd_index_lock);
  1851. } while (error == -EAGAIN);
  1852. if (error)
  1853. goto out_put;
  1854. error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
  1855. if (error)
  1856. goto out_free_index;
  1857. sdkp->device = sdp;
  1858. sdkp->driver = &sd_template;
  1859. sdkp->disk = gd;
  1860. sdkp->index = index;
  1861. sdkp->openers = 0;
  1862. sdkp->previous_state = 1;
  1863. if (!sdp->request_queue->rq_timeout) {
  1864. if (sdp->type != TYPE_MOD)
  1865. blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
  1866. else
  1867. blk_queue_rq_timeout(sdp->request_queue,
  1868. SD_MOD_TIMEOUT);
  1869. }
  1870. device_initialize(&sdkp->dev);
  1871. sdkp->dev.parent = &sdp->sdev_gendev;
  1872. sdkp->dev.class = &sd_disk_class;
  1873. dev_set_name(&sdkp->dev, dev_name(&sdp->sdev_gendev));
  1874. if (device_add(&sdkp->dev))
  1875. goto out_free_index;
  1876. get_device(&sdp->sdev_gendev);
  1877. get_device(&sdkp->dev); /* prevent release before async_schedule */
  1878. async_schedule(sd_probe_async, sdkp);
  1879. return 0;
  1880. out_free_index:
  1881. spin_lock(&sd_index_lock);
  1882. ida_remove(&sd_index_ida, index);
  1883. spin_unlock(&sd_index_lock);
  1884. out_put:
  1885. put_disk(gd);
  1886. out_free:
  1887. kfree(sdkp);
  1888. out:
  1889. return error;
  1890. }
  1891. /**
  1892. * sd_remove - called whenever a scsi disk (previously recognized by
  1893. * sd_probe) is detached from the system. It is called (potentially
  1894. * multiple times) during sd module unload.
  1895. * @sdp: pointer to mid level scsi device object
  1896. *
  1897. * Note: this function is invoked from the scsi mid-level.
  1898. * This function potentially frees up a device name (e.g. /dev/sdc)
  1899. * that could be re-used by a subsequent sd_probe().
  1900. * This function is not called when the built-in sd driver is "exit-ed".
  1901. **/
  1902. static int sd_remove(struct device *dev)
  1903. {
  1904. struct scsi_disk *sdkp;
  1905. async_synchronize_full();
  1906. sdkp = dev_get_drvdata(dev);
  1907. blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
  1908. device_del(&sdkp->dev);
  1909. del_gendisk(sdkp->disk);
  1910. sd_shutdown(dev);
  1911. mutex_lock(&sd_ref_mutex);
  1912. dev_set_drvdata(dev, NULL);
  1913. put_device(&sdkp->dev);
  1914. mutex_unlock(&sd_ref_mutex);
  1915. return 0;
  1916. }
  1917. /**
  1918. * scsi_disk_release - Called to free the scsi_disk structure
  1919. * @dev: pointer to embedded class device
  1920. *
  1921. * sd_ref_mutex must be held entering this routine. Because it is
  1922. * called on last put, you should always use the scsi_disk_get()
  1923. * scsi_disk_put() helpers which manipulate the semaphore directly
  1924. * and never do a direct put_device.
  1925. **/
  1926. static void scsi_disk_release(struct device *dev)
  1927. {
  1928. struct scsi_disk *sdkp = to_scsi_disk(dev);
  1929. struct gendisk *disk = sdkp->disk;
  1930. spin_lock(&sd_index_lock);
  1931. ida_remove(&sd_index_ida, sdkp->index);
  1932. spin_unlock(&sd_index_lock);
  1933. disk->private_data = NULL;
  1934. put_disk(disk);
  1935. put_device(&sdkp->device->sdev_gendev);
  1936. kfree(sdkp);
  1937. }
  1938. static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  1939. {
  1940. unsigned char cmd[6] = { START_STOP }; /* START_VALID */
  1941. struct scsi_sense_hdr sshdr;
  1942. struct scsi_device *sdp = sdkp->device;
  1943. int res;
  1944. if (start)
  1945. cmd[4] |= 1; /* START */
  1946. if (sdp->start_stop_pwr_cond)
  1947. cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
  1948. if (!scsi_device_online(sdp))
  1949. return -ENODEV;
  1950. res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  1951. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1952. if (res) {
  1953. sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
  1954. sd_print_result(sdkp, res);
  1955. if (driver_byte(res) & DRIVER_SENSE)
  1956. sd_print_sense_hdr(sdkp, &sshdr);
  1957. }
  1958. return res;
  1959. }
  1960. /*
  1961. * Send a SYNCHRONIZE CACHE instruction down to the device through
  1962. * the normal SCSI command structure. Wait for the command to
  1963. * complete.
  1964. */
  1965. static void sd_shutdown(struct device *dev)
  1966. {
  1967. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  1968. if (!sdkp)
  1969. return; /* this can happen */
  1970. if (sdkp->WCE) {
  1971. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  1972. sd_sync_cache(sdkp);
  1973. }
  1974. if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
  1975. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  1976. sd_start_stop_device(sdkp, 0);
  1977. }
  1978. scsi_disk_put(sdkp);
  1979. }
  1980. static int sd_suspend(struct device *dev, pm_message_t mesg)
  1981. {
  1982. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  1983. int ret = 0;
  1984. if (!sdkp)
  1985. return 0; /* this can happen */
  1986. if (sdkp->WCE) {
  1987. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  1988. ret = sd_sync_cache(sdkp);
  1989. if (ret)
  1990. goto done;
  1991. }
  1992. if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
  1993. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  1994. ret = sd_start_stop_device(sdkp, 0);
  1995. }
  1996. done:
  1997. scsi_disk_put(sdkp);
  1998. return ret;
  1999. }
  2000. static int sd_resume(struct device *dev)
  2001. {
  2002. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  2003. int ret = 0;
  2004. if (!sdkp->device->manage_start_stop)
  2005. goto done;
  2006. sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
  2007. ret = sd_start_stop_device(sdkp, 1);
  2008. done:
  2009. scsi_disk_put(sdkp);
  2010. return ret;
  2011. }
  2012. /**
  2013. * init_sd - entry point for this driver (both when built in or when
  2014. * a module).
  2015. *
  2016. * Note: this function registers this driver with the scsi mid-level.
  2017. **/
  2018. static int __init init_sd(void)
  2019. {
  2020. int majors = 0, i, err;
  2021. SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
  2022. for (i = 0; i < SD_MAJORS; i++)
  2023. if (register_blkdev(sd_major(i), "sd") == 0)
  2024. majors++;
  2025. if (!majors)
  2026. return -ENODEV;
  2027. err = class_register(&sd_disk_class);
  2028. if (err)
  2029. goto err_out;
  2030. err = scsi_register_driver(&sd_template.gendrv);
  2031. if (err)
  2032. goto err_out_class;
  2033. sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
  2034. 0, 0, NULL);
  2035. if (!sd_cdb_cache) {
  2036. printk(KERN_ERR "sd: can't init extended cdb cache\n");
  2037. goto err_out_class;
  2038. }
  2039. sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
  2040. if (!sd_cdb_pool) {
  2041. printk(KERN_ERR "sd: can't init extended cdb pool\n");
  2042. goto err_out_cache;
  2043. }
  2044. return 0;
  2045. err_out_cache:
  2046. kmem_cache_destroy(sd_cdb_cache);
  2047. err_out_class:
  2048. class_unregister(&sd_disk_class);
  2049. err_out:
  2050. for (i = 0; i < SD_MAJORS; i++)
  2051. unregister_blkdev(sd_major(i), "sd");
  2052. return err;
  2053. }
  2054. /**
  2055. * exit_sd - exit point for this driver (when it is a module).
  2056. *
  2057. * Note: this function unregisters this driver from the scsi mid-level.
  2058. **/
  2059. static void __exit exit_sd(void)
  2060. {
  2061. int i;
  2062. SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
  2063. mempool_destroy(sd_cdb_pool);
  2064. kmem_cache_destroy(sd_cdb_cache);
  2065. scsi_unregister_driver(&sd_template.gendrv);
  2066. class_unregister(&sd_disk_class);
  2067. for (i = 0; i < SD_MAJORS; i++)
  2068. unregister_blkdev(sd_major(i), "sd");
  2069. }
  2070. module_init(init_sd);
  2071. module_exit(exit_sd);
  2072. static void sd_print_sense_hdr(struct scsi_disk *sdkp,
  2073. struct scsi_sense_hdr *sshdr)
  2074. {
  2075. sd_printk(KERN_INFO, sdkp, "");
  2076. scsi_show_sense_hdr(sshdr);
  2077. sd_printk(KERN_INFO, sdkp, "");
  2078. scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
  2079. }
  2080. static void sd_print_result(struct scsi_disk *sdkp, int result)
  2081. {
  2082. sd_printk(KERN_INFO, sdkp, "");
  2083. scsi_show_result(result);
  2084. }