sd.c 56 KB

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