sd.c 87 KB

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