sd.c 87 KB

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