sd.c 85 KB

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