sd.c 76 KB

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