sd.c 69 KB

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