sd.c 82 KB

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