sd.c 76 KB

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