sd.c 56 KB

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