sd.c 86 KB

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