sd.c 75 KB

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