sd.c 57 KB

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