sd.c 79 KB

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