sd.c 68 KB

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