sd.c 79 KB

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