sd.c 91 KB

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