sd.c 71 KB

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