sd.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  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/config.h>
  35. #include <linux/module.h>
  36. #include <linux/fs.h>
  37. #include <linux/kernel.h>
  38. #include <linux/sched.h>
  39. #include <linux/mm.h>
  40. #include <linux/bio.h>
  41. #include <linux/genhd.h>
  42. #include <linux/hdreg.h>
  43. #include <linux/errno.h>
  44. #include <linux/idr.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/init.h>
  47. #include <linux/blkdev.h>
  48. #include <linux/blkpg.h>
  49. #include <linux/kref.h>
  50. #include <linux/delay.h>
  51. #include <linux/mutex.h>
  52. #include <asm/uaccess.h>
  53. #include <scsi/scsi.h>
  54. #include <scsi/scsi_cmnd.h>
  55. #include <scsi/scsi_dbg.h>
  56. #include <scsi/scsi_device.h>
  57. #include <scsi/scsi_driver.h>
  58. #include <scsi/scsi_eh.h>
  59. #include <scsi/scsi_host.h>
  60. #include <scsi/scsi_ioctl.h>
  61. #include <scsi/scsicam.h>
  62. #include "scsi_logging.h"
  63. /*
  64. * More than enough for everybody ;) The huge number of majors
  65. * is a leftover from 16bit dev_t days, we don't really need that
  66. * much numberspace.
  67. */
  68. #define SD_MAJORS 16
  69. /*
  70. * This is limited by the naming scheme enforced in sd_probe,
  71. * add another character to it if you really need more disks.
  72. */
  73. #define SD_MAX_DISKS (((26 * 26) + 26 + 1) * 26)
  74. /*
  75. * Time out in seconds for disks and Magneto-opticals (which are slower).
  76. */
  77. #define SD_TIMEOUT (30 * HZ)
  78. #define SD_MOD_TIMEOUT (75 * HZ)
  79. /*
  80. * Number of allowed retries
  81. */
  82. #define SD_MAX_RETRIES 5
  83. #define SD_PASSTHROUGH_RETRIES 1
  84. static void scsi_disk_release(struct kref *kref);
  85. struct scsi_disk {
  86. struct scsi_driver *driver; /* always &sd_template */
  87. struct scsi_device *device;
  88. struct kref kref;
  89. struct gendisk *disk;
  90. unsigned int openers; /* protected by BKL for now, yuck */
  91. sector_t capacity; /* size in 512-byte sectors */
  92. u32 index;
  93. u8 media_present;
  94. u8 write_prot;
  95. unsigned WCE : 1; /* state of disk WCE bit */
  96. unsigned RCD : 1; /* state of disk RCD bit, unused */
  97. unsigned DPOFUA : 1; /* state of disk DPOFUA bit */
  98. };
  99. static DEFINE_IDR(sd_index_idr);
  100. static DEFINE_SPINLOCK(sd_index_lock);
  101. /* This semaphore is used to mediate the 0->1 reference get in the
  102. * face of object destruction (i.e. we can't allow a get on an
  103. * object after last put) */
  104. static DEFINE_MUTEX(sd_ref_mutex);
  105. static int sd_revalidate_disk(struct gendisk *disk);
  106. static void sd_rw_intr(struct scsi_cmnd * SCpnt);
  107. static int sd_probe(struct device *);
  108. static int sd_remove(struct device *);
  109. static void sd_shutdown(struct device *dev);
  110. static void sd_rescan(struct device *);
  111. static int sd_init_command(struct scsi_cmnd *);
  112. static int sd_issue_flush(struct device *, sector_t *);
  113. static void sd_prepare_flush(request_queue_t *, struct request *);
  114. static void sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
  115. unsigned char *buffer);
  116. static struct scsi_driver sd_template = {
  117. .owner = THIS_MODULE,
  118. .gendrv = {
  119. .name = "sd",
  120. .probe = sd_probe,
  121. .remove = sd_remove,
  122. .shutdown = sd_shutdown,
  123. },
  124. .rescan = sd_rescan,
  125. .init_command = sd_init_command,
  126. .issue_flush = sd_issue_flush,
  127. };
  128. /*
  129. * Device no to disk mapping:
  130. *
  131. * major disc2 disc p1
  132. * |............|.............|....|....| <- dev_t
  133. * 31 20 19 8 7 4 3 0
  134. *
  135. * Inside a major, we have 16k disks, however mapped non-
  136. * contiguously. The first 16 disks are for major0, the next
  137. * ones with major1, ... Disk 256 is for major0 again, disk 272
  138. * for major1, ...
  139. * As we stay compatible with our numbering scheme, we can reuse
  140. * the well-know SCSI majors 8, 65--71, 136--143.
  141. */
  142. static int sd_major(int major_idx)
  143. {
  144. switch (major_idx) {
  145. case 0:
  146. return SCSI_DISK0_MAJOR;
  147. case 1 ... 7:
  148. return SCSI_DISK1_MAJOR + major_idx - 1;
  149. case 8 ... 15:
  150. return SCSI_DISK8_MAJOR + major_idx - 8;
  151. default:
  152. BUG();
  153. return 0; /* shut up gcc */
  154. }
  155. }
  156. #define to_scsi_disk(obj) container_of(obj,struct scsi_disk,kref)
  157. static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
  158. {
  159. return container_of(disk->private_data, struct scsi_disk, driver);
  160. }
  161. static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
  162. {
  163. struct scsi_disk *sdkp = NULL;
  164. if (disk->private_data) {
  165. sdkp = scsi_disk(disk);
  166. if (scsi_device_get(sdkp->device) == 0)
  167. kref_get(&sdkp->kref);
  168. else
  169. sdkp = NULL;
  170. }
  171. return sdkp;
  172. }
  173. static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
  174. {
  175. struct scsi_disk *sdkp;
  176. mutex_lock(&sd_ref_mutex);
  177. sdkp = __scsi_disk_get(disk);
  178. mutex_unlock(&sd_ref_mutex);
  179. return sdkp;
  180. }
  181. static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
  182. {
  183. struct scsi_disk *sdkp;
  184. mutex_lock(&sd_ref_mutex);
  185. sdkp = dev_get_drvdata(dev);
  186. if (sdkp)
  187. sdkp = __scsi_disk_get(sdkp->disk);
  188. mutex_unlock(&sd_ref_mutex);
  189. return sdkp;
  190. }
  191. static void scsi_disk_put(struct scsi_disk *sdkp)
  192. {
  193. struct scsi_device *sdev = sdkp->device;
  194. mutex_lock(&sd_ref_mutex);
  195. kref_put(&sdkp->kref, scsi_disk_release);
  196. scsi_device_put(sdev);
  197. mutex_unlock(&sd_ref_mutex);
  198. }
  199. /**
  200. * sd_init_command - build a scsi (read or write) command from
  201. * information in the request structure.
  202. * @SCpnt: pointer to mid-level's per scsi command structure that
  203. * contains request and into which the scsi command is written
  204. *
  205. * Returns 1 if successful and 0 if error (or cannot be done now).
  206. **/
  207. static int sd_init_command(struct scsi_cmnd * SCpnt)
  208. {
  209. unsigned int this_count, timeout;
  210. struct gendisk *disk;
  211. sector_t block;
  212. struct scsi_device *sdp = SCpnt->device;
  213. struct request *rq = SCpnt->request;
  214. timeout = sdp->timeout;
  215. /*
  216. * SG_IO from block layer already setup, just copy cdb basically
  217. */
  218. if (blk_pc_request(rq)) {
  219. scsi_setup_blk_pc_cmnd(SCpnt);
  220. if (rq->timeout)
  221. timeout = rq->timeout;
  222. goto queue;
  223. }
  224. /*
  225. * we only do REQ_CMD and REQ_BLOCK_PC
  226. */
  227. if (!blk_fs_request(rq))
  228. return 0;
  229. disk = rq->rq_disk;
  230. block = rq->sector;
  231. this_count = SCpnt->request_bufflen >> 9;
  232. SCSI_LOG_HLQUEUE(1, printk("sd_init_command: disk=%s, block=%llu, "
  233. "count=%d\n", disk->disk_name,
  234. (unsigned long long)block, this_count));
  235. if (!sdp || !scsi_device_online(sdp) ||
  236. block + rq->nr_sectors > get_capacity(disk)) {
  237. SCSI_LOG_HLQUEUE(2, printk("Finishing %ld sectors\n",
  238. rq->nr_sectors));
  239. SCSI_LOG_HLQUEUE(2, printk("Retry with 0x%p\n", SCpnt));
  240. return 0;
  241. }
  242. if (sdp->changed) {
  243. /*
  244. * quietly refuse to do anything to a changed disc until
  245. * the changed bit has been reset
  246. */
  247. /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
  248. return 0;
  249. }
  250. SCSI_LOG_HLQUEUE(2, printk("%s : block=%llu\n",
  251. disk->disk_name, (unsigned long long)block));
  252. /*
  253. * If we have a 1K hardware sectorsize, prevent access to single
  254. * 512 byte sectors. In theory we could handle this - in fact
  255. * the scsi cdrom driver must be able to handle this because
  256. * we typically use 1K blocksizes, and cdroms typically have
  257. * 2K hardware sectorsizes. Of course, things are simpler
  258. * with the cdrom, since it is read-only. For performance
  259. * reasons, the filesystems should be able to handle this
  260. * and not force the scsi disk driver to use bounce buffers
  261. * for this.
  262. */
  263. if (sdp->sector_size == 1024) {
  264. if ((block & 1) || (rq->nr_sectors & 1)) {
  265. printk(KERN_ERR "sd: Bad block number requested");
  266. return 0;
  267. } else {
  268. block = block >> 1;
  269. this_count = this_count >> 1;
  270. }
  271. }
  272. if (sdp->sector_size == 2048) {
  273. if ((block & 3) || (rq->nr_sectors & 3)) {
  274. printk(KERN_ERR "sd: Bad block number requested");
  275. return 0;
  276. } else {
  277. block = block >> 2;
  278. this_count = this_count >> 2;
  279. }
  280. }
  281. if (sdp->sector_size == 4096) {
  282. if ((block & 7) || (rq->nr_sectors & 7)) {
  283. printk(KERN_ERR "sd: Bad block number requested");
  284. return 0;
  285. } else {
  286. block = block >> 3;
  287. this_count = this_count >> 3;
  288. }
  289. }
  290. if (rq_data_dir(rq) == WRITE) {
  291. if (!sdp->writeable) {
  292. return 0;
  293. }
  294. SCpnt->cmnd[0] = WRITE_6;
  295. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  296. } else if (rq_data_dir(rq) == READ) {
  297. SCpnt->cmnd[0] = READ_6;
  298. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  299. } else {
  300. printk(KERN_ERR "sd: Unknown command %lx\n", rq->flags);
  301. /* overkill panic("Unknown sd command %lx\n", rq->flags); */
  302. return 0;
  303. }
  304. SCSI_LOG_HLQUEUE(2, printk("%s : %s %d/%ld 512 byte blocks.\n",
  305. disk->disk_name, (rq_data_dir(rq) == WRITE) ?
  306. "writing" : "reading", this_count, rq->nr_sectors));
  307. SCpnt->cmnd[1] = 0;
  308. if (block > 0xffffffff) {
  309. SCpnt->cmnd[0] += READ_16 - READ_6;
  310. SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
  311. SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  312. SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  313. SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  314. SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  315. SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
  316. SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
  317. SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
  318. SCpnt->cmnd[9] = (unsigned char) block & 0xff;
  319. SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
  320. SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
  321. SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
  322. SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
  323. SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
  324. } else if ((this_count > 0xff) || (block > 0x1fffff) ||
  325. SCpnt->device->use_10_for_rw) {
  326. if (this_count > 0xffff)
  327. this_count = 0xffff;
  328. SCpnt->cmnd[0] += READ_10 - READ_6;
  329. SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0;
  330. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  331. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  332. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  333. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  334. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  335. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  336. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  337. } else {
  338. if (unlikely(blk_fua_rq(rq))) {
  339. /*
  340. * This happens only if this drive failed
  341. * 10byte rw command with ILLEGAL_REQUEST
  342. * during operation and thus turned off
  343. * use_10_for_rw.
  344. */
  345. printk(KERN_ERR "sd: FUA write on READ/WRITE(6) drive\n");
  346. return 0;
  347. }
  348. SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  349. SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
  350. SCpnt->cmnd[3] = (unsigned char) block & 0xff;
  351. SCpnt->cmnd[4] = (unsigned char) this_count;
  352. SCpnt->cmnd[5] = 0;
  353. }
  354. SCpnt->request_bufflen = SCpnt->bufflen =
  355. this_count * sdp->sector_size;
  356. /*
  357. * We shouldn't disconnect in the middle of a sector, so with a dumb
  358. * host adapter, it's safe to assume that we can at least transfer
  359. * this many bytes between each connect / disconnect.
  360. */
  361. SCpnt->transfersize = sdp->sector_size;
  362. SCpnt->underflow = this_count << 9;
  363. SCpnt->allowed = SD_MAX_RETRIES;
  364. queue:
  365. SCpnt->timeout_per_command = timeout;
  366. /*
  367. * This is the completion routine we use. This is matched in terms
  368. * of capability to this function.
  369. */
  370. SCpnt->done = sd_rw_intr;
  371. /*
  372. * This indicates that the command is ready from our end to be
  373. * queued.
  374. */
  375. return 1;
  376. }
  377. /**
  378. * sd_open - open a scsi disk device
  379. * @inode: only i_rdev member may be used
  380. * @filp: only f_mode and f_flags may be used
  381. *
  382. * Returns 0 if successful. Returns a negated errno value in case
  383. * of error.
  384. *
  385. * Note: This can be called from a user context (e.g. fsck(1) )
  386. * or from within the kernel (e.g. as a result of a mount(1) ).
  387. * In the latter case @inode and @filp carry an abridged amount
  388. * of information as noted above.
  389. **/
  390. static int sd_open(struct inode *inode, struct file *filp)
  391. {
  392. struct gendisk *disk = inode->i_bdev->bd_disk;
  393. struct scsi_disk *sdkp;
  394. struct scsi_device *sdev;
  395. int retval;
  396. if (!(sdkp = scsi_disk_get(disk)))
  397. return -ENXIO;
  398. SCSI_LOG_HLQUEUE(3, printk("sd_open: disk=%s\n", disk->disk_name));
  399. sdev = sdkp->device;
  400. /*
  401. * If the device is in error recovery, wait until it is done.
  402. * If the device is offline, then disallow any access to it.
  403. */
  404. retval = -ENXIO;
  405. if (!scsi_block_when_processing_errors(sdev))
  406. goto error_out;
  407. if (sdev->removable || sdkp->write_prot)
  408. check_disk_change(inode->i_bdev);
  409. /*
  410. * If the drive is empty, just let the open fail.
  411. */
  412. retval = -ENOMEDIUM;
  413. if (sdev->removable && !sdkp->media_present &&
  414. !(filp->f_flags & O_NDELAY))
  415. goto error_out;
  416. /*
  417. * If the device has the write protect tab set, have the open fail
  418. * if the user expects to be able to write to the thing.
  419. */
  420. retval = -EROFS;
  421. if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
  422. goto error_out;
  423. /*
  424. * It is possible that the disk changing stuff resulted in
  425. * the device being taken offline. If this is the case,
  426. * report this to the user, and don't pretend that the
  427. * open actually succeeded.
  428. */
  429. retval = -ENXIO;
  430. if (!scsi_device_online(sdev))
  431. goto error_out;
  432. if (!sdkp->openers++ && sdev->removable) {
  433. if (scsi_block_when_processing_errors(sdev))
  434. scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  435. }
  436. return 0;
  437. error_out:
  438. scsi_disk_put(sdkp);
  439. return retval;
  440. }
  441. /**
  442. * sd_release - invoked when the (last) close(2) is called on this
  443. * scsi disk.
  444. * @inode: only i_rdev member may be used
  445. * @filp: only f_mode and f_flags may be used
  446. *
  447. * Returns 0.
  448. *
  449. * Note: may block (uninterruptible) if error recovery is underway
  450. * on this disk.
  451. **/
  452. static int sd_release(struct inode *inode, struct file *filp)
  453. {
  454. struct gendisk *disk = inode->i_bdev->bd_disk;
  455. struct scsi_disk *sdkp = scsi_disk(disk);
  456. struct scsi_device *sdev = sdkp->device;
  457. SCSI_LOG_HLQUEUE(3, printk("sd_release: disk=%s\n", disk->disk_name));
  458. if (!--sdkp->openers && sdev->removable) {
  459. if (scsi_block_when_processing_errors(sdev))
  460. scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  461. }
  462. /*
  463. * XXX and what if there are packets in flight and this close()
  464. * XXX is followed by a "rmmod sd_mod"?
  465. */
  466. scsi_disk_put(sdkp);
  467. return 0;
  468. }
  469. static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  470. {
  471. struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
  472. struct scsi_device *sdp = sdkp->device;
  473. struct Scsi_Host *host = sdp->host;
  474. int diskinfo[4];
  475. /* default to most commonly used values */
  476. diskinfo[0] = 0x40; /* 1 << 6 */
  477. diskinfo[1] = 0x20; /* 1 << 5 */
  478. diskinfo[2] = sdkp->capacity >> 11;
  479. /* override with calculated, extended default, or driver values */
  480. if (host->hostt->bios_param)
  481. host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
  482. else
  483. scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
  484. geo->heads = diskinfo[0];
  485. geo->sectors = diskinfo[1];
  486. geo->cylinders = diskinfo[2];
  487. return 0;
  488. }
  489. /**
  490. * sd_ioctl - process an ioctl
  491. * @inode: only i_rdev/i_bdev members may be used
  492. * @filp: only f_mode and f_flags may be used
  493. * @cmd: ioctl command number
  494. * @arg: this is third argument given to ioctl(2) system call.
  495. * Often contains a pointer.
  496. *
  497. * Returns 0 if successful (some ioctls return postive numbers on
  498. * success as well). Returns a negated errno value in case of error.
  499. *
  500. * Note: most ioctls are forward onto the block subsystem or further
  501. * down in the scsi subsytem.
  502. **/
  503. static int sd_ioctl(struct inode * inode, struct file * filp,
  504. unsigned int cmd, unsigned long arg)
  505. {
  506. struct block_device *bdev = inode->i_bdev;
  507. struct gendisk *disk = bdev->bd_disk;
  508. struct scsi_device *sdp = scsi_disk(disk)->device;
  509. void __user *p = (void __user *)arg;
  510. int error;
  511. SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
  512. disk->disk_name, cmd));
  513. /*
  514. * If we are in the middle of error recovery, don't let anyone
  515. * else try and use this device. Also, if error recovery fails, it
  516. * may try and take the device offline, in which case all further
  517. * access to the device is prohibited.
  518. */
  519. error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
  520. if (!scsi_block_when_processing_errors(sdp) || !error)
  521. return error;
  522. /*
  523. * Send SCSI addressing ioctls directly to mid level, send other
  524. * ioctls to block level and then onto mid level if they can't be
  525. * resolved.
  526. */
  527. switch (cmd) {
  528. case SCSI_IOCTL_GET_IDLUN:
  529. case SCSI_IOCTL_GET_BUS_NUMBER:
  530. return scsi_ioctl(sdp, cmd, p);
  531. default:
  532. error = scsi_cmd_ioctl(filp, disk, cmd, p);
  533. if (error != -ENOTTY)
  534. return error;
  535. }
  536. return scsi_ioctl(sdp, cmd, p);
  537. }
  538. static void set_media_not_present(struct scsi_disk *sdkp)
  539. {
  540. sdkp->media_present = 0;
  541. sdkp->capacity = 0;
  542. sdkp->device->changed = 1;
  543. }
  544. /**
  545. * sd_media_changed - check if our medium changed
  546. * @disk: kernel device descriptor
  547. *
  548. * Returns 0 if not applicable or no change; 1 if change
  549. *
  550. * Note: this function is invoked from the block subsystem.
  551. **/
  552. static int sd_media_changed(struct gendisk *disk)
  553. {
  554. struct scsi_disk *sdkp = scsi_disk(disk);
  555. struct scsi_device *sdp = sdkp->device;
  556. int retval;
  557. SCSI_LOG_HLQUEUE(3, printk("sd_media_changed: disk=%s\n",
  558. disk->disk_name));
  559. if (!sdp->removable)
  560. return 0;
  561. /*
  562. * If the device is offline, don't send any commands - just pretend as
  563. * if the command failed. If the device ever comes back online, we
  564. * can deal with it then. It is only because of unrecoverable errors
  565. * that we would ever take a device offline in the first place.
  566. */
  567. if (!scsi_device_online(sdp))
  568. goto not_present;
  569. /*
  570. * Using TEST_UNIT_READY enables differentiation between drive with
  571. * no cartridge loaded - NOT READY, drive with changed cartridge -
  572. * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
  573. *
  574. * Drives that auto spin down. eg iomega jaz 1G, will be started
  575. * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
  576. * sd_revalidate() is called.
  577. */
  578. retval = -ENODEV;
  579. if (scsi_block_when_processing_errors(sdp))
  580. retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES);
  581. /*
  582. * Unable to test, unit probably not ready. This usually
  583. * means there is no disc in the drive. Mark as changed,
  584. * and we will figure it out later once the drive is
  585. * available again.
  586. */
  587. if (retval)
  588. goto not_present;
  589. /*
  590. * For removable scsi disk we have to recognise the presence
  591. * of a disk in the drive. This is kept in the struct scsi_disk
  592. * struct and tested at open ! Daniel Roche (dan@lectra.fr)
  593. */
  594. sdkp->media_present = 1;
  595. retval = sdp->changed;
  596. sdp->changed = 0;
  597. return retval;
  598. not_present:
  599. set_media_not_present(sdkp);
  600. return 1;
  601. }
  602. static int sd_sync_cache(struct scsi_device *sdp)
  603. {
  604. int retries, res;
  605. struct scsi_sense_hdr sshdr;
  606. if (!scsi_device_online(sdp))
  607. return -ENODEV;
  608. for (retries = 3; retries > 0; --retries) {
  609. unsigned char cmd[10] = { 0 };
  610. cmd[0] = SYNCHRONIZE_CACHE;
  611. /*
  612. * Leave the rest of the command zero to indicate
  613. * flush everything.
  614. */
  615. res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  616. SD_TIMEOUT, SD_MAX_RETRIES);
  617. if (res == 0)
  618. break;
  619. }
  620. if (res) { printk(KERN_WARNING "FAILED\n status = %x, message = %02x, "
  621. "host = %d, driver = %02x\n ",
  622. status_byte(res), msg_byte(res),
  623. host_byte(res), driver_byte(res));
  624. if (driver_byte(res) & DRIVER_SENSE)
  625. scsi_print_sense_hdr("sd", &sshdr);
  626. }
  627. return res;
  628. }
  629. static int sd_issue_flush(struct device *dev, sector_t *error_sector)
  630. {
  631. int ret = 0;
  632. struct scsi_device *sdp = to_scsi_device(dev);
  633. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  634. if (!sdkp)
  635. return -ENODEV;
  636. if (sdkp->WCE)
  637. ret = sd_sync_cache(sdp);
  638. scsi_disk_put(sdkp);
  639. return ret;
  640. }
  641. static void sd_prepare_flush(request_queue_t *q, struct request *rq)
  642. {
  643. memset(rq->cmd, 0, sizeof(rq->cmd));
  644. rq->flags |= REQ_BLOCK_PC;
  645. rq->timeout = SD_TIMEOUT;
  646. rq->cmd[0] = SYNCHRONIZE_CACHE;
  647. rq->cmd_len = 10;
  648. }
  649. static void sd_rescan(struct device *dev)
  650. {
  651. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  652. if (sdkp) {
  653. sd_revalidate_disk(sdkp->disk);
  654. scsi_disk_put(sdkp);
  655. }
  656. }
  657. #ifdef CONFIG_COMPAT
  658. /*
  659. * This gets directly called from VFS. When the ioctl
  660. * is not recognized we go back to the other translation paths.
  661. */
  662. static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  663. {
  664. struct block_device *bdev = file->f_dentry->d_inode->i_bdev;
  665. struct gendisk *disk = bdev->bd_disk;
  666. struct scsi_device *sdev = scsi_disk(disk)->device;
  667. /*
  668. * If we are in the middle of error recovery, don't let anyone
  669. * else try and use this device. Also, if error recovery fails, it
  670. * may try and take the device offline, in which case all further
  671. * access to the device is prohibited.
  672. */
  673. if (!scsi_block_when_processing_errors(sdev))
  674. return -ENODEV;
  675. if (sdev->host->hostt->compat_ioctl) {
  676. int ret;
  677. ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
  678. return ret;
  679. }
  680. /*
  681. * Let the static ioctl translation table take care of it.
  682. */
  683. return -ENOIOCTLCMD;
  684. }
  685. #endif
  686. static struct block_device_operations sd_fops = {
  687. .owner = THIS_MODULE,
  688. .open = sd_open,
  689. .release = sd_release,
  690. .ioctl = sd_ioctl,
  691. .getgeo = sd_getgeo,
  692. #ifdef CONFIG_COMPAT
  693. .compat_ioctl = sd_compat_ioctl,
  694. #endif
  695. .media_changed = sd_media_changed,
  696. .revalidate_disk = sd_revalidate_disk,
  697. };
  698. /**
  699. * sd_rw_intr - bottom half handler: called when the lower level
  700. * driver has completed (successfully or otherwise) a scsi command.
  701. * @SCpnt: mid-level's per command structure.
  702. *
  703. * Note: potentially run from within an ISR. Must not block.
  704. **/
  705. static void sd_rw_intr(struct scsi_cmnd * SCpnt)
  706. {
  707. int result = SCpnt->result;
  708. int this_count = SCpnt->bufflen;
  709. int good_bytes = (result == 0 ? this_count : 0);
  710. sector_t block_sectors = 1;
  711. u64 first_err_block;
  712. sector_t error_sector;
  713. struct scsi_sense_hdr sshdr;
  714. int sense_valid = 0;
  715. int sense_deferred = 0;
  716. int info_valid;
  717. if (result) {
  718. sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
  719. if (sense_valid)
  720. sense_deferred = scsi_sense_is_deferred(&sshdr);
  721. }
  722. #ifdef CONFIG_SCSI_LOGGING
  723. SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: %s: res=0x%x\n",
  724. SCpnt->request->rq_disk->disk_name, result));
  725. if (sense_valid) {
  726. SCSI_LOG_HLCOMPLETE(1, printk("sd_rw_intr: sb[respc,sk,asc,"
  727. "ascq]=%x,%x,%x,%x\n", sshdr.response_code,
  728. sshdr.sense_key, sshdr.asc, sshdr.ascq));
  729. }
  730. #endif
  731. /*
  732. Handle MEDIUM ERRORs that indicate partial success. Since this is a
  733. relatively rare error condition, no care is taken to avoid
  734. unnecessary additional work such as memcpy's that could be avoided.
  735. */
  736. /*
  737. * If SG_IO from block layer then set good_bytes to stop retries;
  738. * else if errors, check them, and if necessary prepare for
  739. * (partial) retries.
  740. */
  741. if (blk_pc_request(SCpnt->request))
  742. good_bytes = this_count;
  743. else if (driver_byte(result) != 0 &&
  744. sense_valid && !sense_deferred) {
  745. switch (sshdr.sense_key) {
  746. case MEDIUM_ERROR:
  747. if (!blk_fs_request(SCpnt->request))
  748. break;
  749. info_valid = scsi_get_sense_info_fld(
  750. SCpnt->sense_buffer, SCSI_SENSE_BUFFERSIZE,
  751. &first_err_block);
  752. /*
  753. * May want to warn and skip if following cast results
  754. * in actual truncation (if sector_t < 64 bits)
  755. */
  756. error_sector = (sector_t)first_err_block;
  757. if (SCpnt->request->bio != NULL)
  758. block_sectors = bio_sectors(SCpnt->request->bio);
  759. switch (SCpnt->device->sector_size) {
  760. case 1024:
  761. error_sector <<= 1;
  762. if (block_sectors < 2)
  763. block_sectors = 2;
  764. break;
  765. case 2048:
  766. error_sector <<= 2;
  767. if (block_sectors < 4)
  768. block_sectors = 4;
  769. break;
  770. case 4096:
  771. error_sector <<=3;
  772. if (block_sectors < 8)
  773. block_sectors = 8;
  774. break;
  775. case 256:
  776. error_sector >>= 1;
  777. break;
  778. default:
  779. break;
  780. }
  781. error_sector &= ~(block_sectors - 1);
  782. good_bytes = (error_sector - SCpnt->request->sector) << 9;
  783. if (good_bytes < 0 || good_bytes >= this_count)
  784. good_bytes = 0;
  785. break;
  786. case RECOVERED_ERROR: /* an error occurred, but it recovered */
  787. case NO_SENSE: /* LLDD got sense data */
  788. /*
  789. * Inform the user, but make sure that it's not treated
  790. * as a hard error.
  791. */
  792. scsi_print_sense("sd", SCpnt);
  793. SCpnt->result = 0;
  794. memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  795. good_bytes = this_count;
  796. break;
  797. case ILLEGAL_REQUEST:
  798. if (SCpnt->device->use_10_for_rw &&
  799. (SCpnt->cmnd[0] == READ_10 ||
  800. SCpnt->cmnd[0] == WRITE_10))
  801. SCpnt->device->use_10_for_rw = 0;
  802. if (SCpnt->device->use_10_for_ms &&
  803. (SCpnt->cmnd[0] == MODE_SENSE_10 ||
  804. SCpnt->cmnd[0] == MODE_SELECT_10))
  805. SCpnt->device->use_10_for_ms = 0;
  806. break;
  807. default:
  808. break;
  809. }
  810. }
  811. /*
  812. * This calls the generic completion function, now that we know
  813. * how many actual sectors finished, and how many sectors we need
  814. * to say have failed.
  815. */
  816. scsi_io_completion(SCpnt, good_bytes, block_sectors << 9);
  817. }
  818. static int media_not_present(struct scsi_disk *sdkp,
  819. struct scsi_sense_hdr *sshdr)
  820. {
  821. if (!scsi_sense_valid(sshdr))
  822. return 0;
  823. /* not invoked for commands that could return deferred errors */
  824. if (sshdr->sense_key != NOT_READY &&
  825. sshdr->sense_key != UNIT_ATTENTION)
  826. return 0;
  827. if (sshdr->asc != 0x3A) /* medium not present */
  828. return 0;
  829. set_media_not_present(sdkp);
  830. return 1;
  831. }
  832. /*
  833. * spinup disk - called only in sd_revalidate_disk()
  834. */
  835. static void
  836. sd_spinup_disk(struct scsi_disk *sdkp, char *diskname)
  837. {
  838. unsigned char cmd[10];
  839. unsigned long spintime_expire = 0;
  840. int retries, spintime;
  841. unsigned int the_result;
  842. struct scsi_sense_hdr sshdr;
  843. int sense_valid = 0;
  844. spintime = 0;
  845. /* Spin up drives, as required. Only do this at boot time */
  846. /* Spinup needs to be done for module loads too. */
  847. do {
  848. retries = 0;
  849. do {
  850. cmd[0] = TEST_UNIT_READY;
  851. memset((void *) &cmd[1], 0, 9);
  852. the_result = scsi_execute_req(sdkp->device, cmd,
  853. DMA_NONE, NULL, 0,
  854. &sshdr, SD_TIMEOUT,
  855. SD_MAX_RETRIES);
  856. if (the_result)
  857. sense_valid = scsi_sense_valid(&sshdr);
  858. retries++;
  859. } while (retries < 3 &&
  860. (!scsi_status_is_good(the_result) ||
  861. ((driver_byte(the_result) & DRIVER_SENSE) &&
  862. sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
  863. /*
  864. * If the drive has indicated to us that it doesn't have
  865. * any media in it, don't bother with any of the rest of
  866. * this crap.
  867. */
  868. if (media_not_present(sdkp, &sshdr))
  869. return;
  870. if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
  871. /* no sense, TUR either succeeded or failed
  872. * with a status error */
  873. if(!spintime && !scsi_status_is_good(the_result))
  874. printk(KERN_NOTICE "%s: Unit Not Ready, "
  875. "error = 0x%x\n", diskname, the_result);
  876. break;
  877. }
  878. /*
  879. * The device does not want the automatic start to be issued.
  880. */
  881. if (sdkp->device->no_start_on_add) {
  882. break;
  883. }
  884. /*
  885. * If manual intervention is required, or this is an
  886. * absent USB storage device, a spinup is meaningless.
  887. */
  888. if (sense_valid &&
  889. sshdr.sense_key == NOT_READY &&
  890. sshdr.asc == 4 && sshdr.ascq == 3) {
  891. break; /* manual intervention required */
  892. /*
  893. * Issue command to spin up drive when not ready
  894. */
  895. } else if (sense_valid && sshdr.sense_key == NOT_READY) {
  896. if (!spintime) {
  897. printk(KERN_NOTICE "%s: Spinning up disk...",
  898. diskname);
  899. cmd[0] = START_STOP;
  900. cmd[1] = 1; /* Return immediately */
  901. memset((void *) &cmd[2], 0, 8);
  902. cmd[4] = 1; /* Start spin cycle */
  903. scsi_execute_req(sdkp->device, cmd, DMA_NONE,
  904. NULL, 0, &sshdr,
  905. SD_TIMEOUT, SD_MAX_RETRIES);
  906. spintime_expire = jiffies + 100 * HZ;
  907. spintime = 1;
  908. }
  909. /* Wait 1 second for next try */
  910. msleep(1000);
  911. printk(".");
  912. /*
  913. * Wait for USB flash devices with slow firmware.
  914. * Yes, this sense key/ASC combination shouldn't
  915. * occur here. It's characteristic of these devices.
  916. */
  917. } else if (sense_valid &&
  918. sshdr.sense_key == UNIT_ATTENTION &&
  919. sshdr.asc == 0x28) {
  920. if (!spintime) {
  921. spintime_expire = jiffies + 5 * HZ;
  922. spintime = 1;
  923. }
  924. /* Wait 1 second for next try */
  925. msleep(1000);
  926. } else {
  927. /* we don't understand the sense code, so it's
  928. * probably pointless to loop */
  929. if(!spintime) {
  930. printk(KERN_NOTICE "%s: Unit Not Ready, "
  931. "sense:\n", diskname);
  932. scsi_print_sense_hdr("", &sshdr);
  933. }
  934. break;
  935. }
  936. } while (spintime && time_before_eq(jiffies, spintime_expire));
  937. if (spintime) {
  938. if (scsi_status_is_good(the_result))
  939. printk("ready\n");
  940. else
  941. printk("not responding...\n");
  942. }
  943. }
  944. /*
  945. * read disk capacity
  946. */
  947. static void
  948. sd_read_capacity(struct scsi_disk *sdkp, char *diskname,
  949. unsigned char *buffer)
  950. {
  951. unsigned char cmd[16];
  952. int the_result, retries;
  953. int sector_size = 0;
  954. int longrc = 0;
  955. struct scsi_sense_hdr sshdr;
  956. int sense_valid = 0;
  957. struct scsi_device *sdp = sdkp->device;
  958. repeat:
  959. retries = 3;
  960. do {
  961. if (longrc) {
  962. memset((void *) cmd, 0, 16);
  963. cmd[0] = SERVICE_ACTION_IN;
  964. cmd[1] = SAI_READ_CAPACITY_16;
  965. cmd[13] = 12;
  966. memset((void *) buffer, 0, 12);
  967. } else {
  968. cmd[0] = READ_CAPACITY;
  969. memset((void *) &cmd[1], 0, 9);
  970. memset((void *) buffer, 0, 8);
  971. }
  972. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  973. buffer, longrc ? 12 : 8, &sshdr,
  974. SD_TIMEOUT, SD_MAX_RETRIES);
  975. if (media_not_present(sdkp, &sshdr))
  976. return;
  977. if (the_result)
  978. sense_valid = scsi_sense_valid(&sshdr);
  979. retries--;
  980. } while (the_result && retries);
  981. if (the_result && !longrc) {
  982. printk(KERN_NOTICE "%s : READ CAPACITY failed.\n"
  983. "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
  984. diskname, diskname,
  985. status_byte(the_result),
  986. msg_byte(the_result),
  987. host_byte(the_result),
  988. driver_byte(the_result));
  989. if (driver_byte(the_result) & DRIVER_SENSE)
  990. scsi_print_sense_hdr("sd", &sshdr);
  991. else
  992. printk("%s : sense not available. \n", diskname);
  993. /* Set dirty bit for removable devices if not ready -
  994. * sometimes drives will not report this properly. */
  995. if (sdp->removable &&
  996. sense_valid && sshdr.sense_key == NOT_READY)
  997. sdp->changed = 1;
  998. /* Either no media are present but the drive didn't tell us,
  999. or they are present but the read capacity command fails */
  1000. /* sdkp->media_present = 0; -- not always correct */
  1001. sdkp->capacity = 0x200000; /* 1 GB - random */
  1002. return;
  1003. } else if (the_result && longrc) {
  1004. /* READ CAPACITY(16) has been failed */
  1005. printk(KERN_NOTICE "%s : READ CAPACITY(16) failed.\n"
  1006. "%s : status=%x, message=%02x, host=%d, driver=%02x \n",
  1007. diskname, diskname,
  1008. status_byte(the_result),
  1009. msg_byte(the_result),
  1010. host_byte(the_result),
  1011. driver_byte(the_result));
  1012. printk(KERN_NOTICE "%s : use 0xffffffff as device size\n",
  1013. diskname);
  1014. sdkp->capacity = 1 + (sector_t) 0xffffffff;
  1015. goto got_data;
  1016. }
  1017. if (!longrc) {
  1018. sector_size = (buffer[4] << 24) |
  1019. (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];
  1020. if (buffer[0] == 0xff && buffer[1] == 0xff &&
  1021. buffer[2] == 0xff && buffer[3] == 0xff) {
  1022. if(sizeof(sdkp->capacity) > 4) {
  1023. printk(KERN_NOTICE "%s : very big device. try to use"
  1024. " READ CAPACITY(16).\n", diskname);
  1025. longrc = 1;
  1026. goto repeat;
  1027. }
  1028. printk(KERN_ERR "%s: too big for this kernel. Use a "
  1029. "kernel compiled with support for large block "
  1030. "devices.\n", diskname);
  1031. sdkp->capacity = 0;
  1032. goto got_data;
  1033. }
  1034. sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |
  1035. (buffer[1] << 16) |
  1036. (buffer[2] << 8) |
  1037. buffer[3]);
  1038. } else {
  1039. sdkp->capacity = 1 + (((u64)buffer[0] << 56) |
  1040. ((u64)buffer[1] << 48) |
  1041. ((u64)buffer[2] << 40) |
  1042. ((u64)buffer[3] << 32) |
  1043. ((sector_t)buffer[4] << 24) |
  1044. ((sector_t)buffer[5] << 16) |
  1045. ((sector_t)buffer[6] << 8) |
  1046. (sector_t)buffer[7]);
  1047. sector_size = (buffer[8] << 24) |
  1048. (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];
  1049. }
  1050. /* Some devices return the total number of sectors, not the
  1051. * highest sector number. Make the necessary adjustment. */
  1052. if (sdp->fix_capacity)
  1053. --sdkp->capacity;
  1054. got_data:
  1055. if (sector_size == 0) {
  1056. sector_size = 512;
  1057. printk(KERN_NOTICE "%s : sector size 0 reported, "
  1058. "assuming 512.\n", diskname);
  1059. }
  1060. if (sector_size != 512 &&
  1061. sector_size != 1024 &&
  1062. sector_size != 2048 &&
  1063. sector_size != 4096 &&
  1064. sector_size != 256) {
  1065. printk(KERN_NOTICE "%s : unsupported sector size "
  1066. "%d.\n", diskname, sector_size);
  1067. /*
  1068. * The user might want to re-format the drive with
  1069. * a supported sectorsize. Once this happens, it
  1070. * would be relatively trivial to set the thing up.
  1071. * For this reason, we leave the thing in the table.
  1072. */
  1073. sdkp->capacity = 0;
  1074. /*
  1075. * set a bogus sector size so the normal read/write
  1076. * logic in the block layer will eventually refuse any
  1077. * request on this device without tripping over power
  1078. * of two sector size assumptions
  1079. */
  1080. sector_size = 512;
  1081. }
  1082. {
  1083. /*
  1084. * The msdos fs needs to know the hardware sector size
  1085. * So I have created this table. See ll_rw_blk.c
  1086. * Jacques Gelinas (Jacques@solucorp.qc.ca)
  1087. */
  1088. int hard_sector = sector_size;
  1089. sector_t sz = (sdkp->capacity/2) * (hard_sector/256);
  1090. request_queue_t *queue = sdp->request_queue;
  1091. sector_t mb = sz;
  1092. blk_queue_hardsect_size(queue, hard_sector);
  1093. /* avoid 64-bit division on 32-bit platforms */
  1094. sector_div(sz, 625);
  1095. mb -= sz - 974;
  1096. sector_div(mb, 1950);
  1097. printk(KERN_NOTICE "SCSI device %s: "
  1098. "%llu %d-byte hdwr sectors (%llu MB)\n",
  1099. diskname, (unsigned long long)sdkp->capacity,
  1100. hard_sector, (unsigned long long)mb);
  1101. }
  1102. /* Rescale capacity to 512-byte units */
  1103. if (sector_size == 4096)
  1104. sdkp->capacity <<= 3;
  1105. else if (sector_size == 2048)
  1106. sdkp->capacity <<= 2;
  1107. else if (sector_size == 1024)
  1108. sdkp->capacity <<= 1;
  1109. else if (sector_size == 256)
  1110. sdkp->capacity >>= 1;
  1111. sdkp->device->sector_size = sector_size;
  1112. }
  1113. /* called with buffer of length 512 */
  1114. static inline int
  1115. sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
  1116. unsigned char *buffer, int len, struct scsi_mode_data *data,
  1117. struct scsi_sense_hdr *sshdr)
  1118. {
  1119. return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
  1120. SD_TIMEOUT, SD_MAX_RETRIES, data,
  1121. sshdr);
  1122. }
  1123. /*
  1124. * read write protect setting, if possible - called only in sd_revalidate_disk()
  1125. * called with buffer of length 512
  1126. */
  1127. static void
  1128. sd_read_write_protect_flag(struct scsi_disk *sdkp, char *diskname,
  1129. unsigned char *buffer)
  1130. {
  1131. int res;
  1132. struct scsi_device *sdp = sdkp->device;
  1133. struct scsi_mode_data data;
  1134. set_disk_ro(sdkp->disk, 0);
  1135. if (sdp->skip_ms_page_3f) {
  1136. printk(KERN_NOTICE "%s: assuming Write Enabled\n", diskname);
  1137. return;
  1138. }
  1139. if (sdp->use_192_bytes_for_3f) {
  1140. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
  1141. } else {
  1142. /*
  1143. * First attempt: ask for all pages (0x3F), but only 4 bytes.
  1144. * We have to start carefully: some devices hang if we ask
  1145. * for more than is available.
  1146. */
  1147. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
  1148. /*
  1149. * Second attempt: ask for page 0 When only page 0 is
  1150. * implemented, a request for page 3F may return Sense Key
  1151. * 5: Illegal Request, Sense Code 24: Invalid field in
  1152. * CDB.
  1153. */
  1154. if (!scsi_status_is_good(res))
  1155. res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
  1156. /*
  1157. * Third attempt: ask 255 bytes, as we did earlier.
  1158. */
  1159. if (!scsi_status_is_good(res))
  1160. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
  1161. &data, NULL);
  1162. }
  1163. if (!scsi_status_is_good(res)) {
  1164. printk(KERN_WARNING
  1165. "%s: test WP failed, assume Write Enabled\n", diskname);
  1166. } else {
  1167. sdkp->write_prot = ((data.device_specific & 0x80) != 0);
  1168. set_disk_ro(sdkp->disk, sdkp->write_prot);
  1169. printk(KERN_NOTICE "%s: Write Protect is %s\n", diskname,
  1170. sdkp->write_prot ? "on" : "off");
  1171. printk(KERN_DEBUG "%s: Mode Sense: %02x %02x %02x %02x\n",
  1172. diskname, buffer[0], buffer[1], buffer[2], buffer[3]);
  1173. }
  1174. }
  1175. /*
  1176. * sd_read_cache_type - called only from sd_revalidate_disk()
  1177. * called with buffer of length 512
  1178. */
  1179. static void
  1180. sd_read_cache_type(struct scsi_disk *sdkp, char *diskname,
  1181. unsigned char *buffer)
  1182. {
  1183. int len = 0, res;
  1184. struct scsi_device *sdp = sdkp->device;
  1185. int dbd;
  1186. int modepage;
  1187. struct scsi_mode_data data;
  1188. struct scsi_sense_hdr sshdr;
  1189. if (sdp->skip_ms_page_8)
  1190. goto defaults;
  1191. if (sdp->type == TYPE_RBC) {
  1192. modepage = 6;
  1193. dbd = 8;
  1194. } else {
  1195. modepage = 8;
  1196. dbd = 0;
  1197. }
  1198. /* cautiously ask */
  1199. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
  1200. if (!scsi_status_is_good(res))
  1201. goto bad_sense;
  1202. /* that went OK, now ask for the proper length */
  1203. len = data.length;
  1204. /*
  1205. * We're only interested in the first three bytes, actually.
  1206. * But the data cache page is defined for the first 20.
  1207. */
  1208. if (len < 3)
  1209. goto bad_sense;
  1210. if (len > 20)
  1211. len = 20;
  1212. /* Take headers and block descriptors into account */
  1213. len += data.header_length + data.block_descriptor_length;
  1214. /* Get the data */
  1215. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
  1216. if (scsi_status_is_good(res)) {
  1217. const char *types[] = {
  1218. "write through", "none", "write back",
  1219. "write back, no read (daft)"
  1220. };
  1221. int ct = 0;
  1222. int offset = data.header_length + data.block_descriptor_length;
  1223. if ((buffer[offset] & 0x3f) != modepage) {
  1224. printk(KERN_ERR "%s: got wrong page\n", diskname);
  1225. goto defaults;
  1226. }
  1227. if (modepage == 8) {
  1228. sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
  1229. sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
  1230. } else {
  1231. sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
  1232. sdkp->RCD = 0;
  1233. }
  1234. sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
  1235. if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
  1236. printk(KERN_NOTICE "SCSI device %s: uses "
  1237. "READ/WRITE(6), disabling FUA\n", diskname);
  1238. sdkp->DPOFUA = 0;
  1239. }
  1240. ct = sdkp->RCD + 2*sdkp->WCE;
  1241. printk(KERN_NOTICE "SCSI device %s: drive cache: %s%s\n",
  1242. diskname, types[ct],
  1243. sdkp->DPOFUA ? " w/ FUA" : "");
  1244. return;
  1245. }
  1246. bad_sense:
  1247. if (scsi_sense_valid(&sshdr) &&
  1248. sshdr.sense_key == ILLEGAL_REQUEST &&
  1249. sshdr.asc == 0x24 && sshdr.ascq == 0x0)
  1250. printk(KERN_NOTICE "%s: cache data unavailable\n",
  1251. diskname); /* Invalid field in CDB */
  1252. else
  1253. printk(KERN_ERR "%s: asking for cache data failed\n",
  1254. diskname);
  1255. defaults:
  1256. printk(KERN_ERR "%s: assuming drive cache: write through\n",
  1257. diskname);
  1258. sdkp->WCE = 0;
  1259. sdkp->RCD = 0;
  1260. }
  1261. /**
  1262. * sd_revalidate_disk - called the first time a new disk is seen,
  1263. * performs disk spin up, read_capacity, etc.
  1264. * @disk: struct gendisk we care about
  1265. **/
  1266. static int sd_revalidate_disk(struct gendisk *disk)
  1267. {
  1268. struct scsi_disk *sdkp = scsi_disk(disk);
  1269. struct scsi_device *sdp = sdkp->device;
  1270. unsigned char *buffer;
  1271. unsigned ordered;
  1272. SCSI_LOG_HLQUEUE(3, printk("sd_revalidate_disk: disk=%s\n", disk->disk_name));
  1273. /*
  1274. * If the device is offline, don't try and read capacity or any
  1275. * of the other niceties.
  1276. */
  1277. if (!scsi_device_online(sdp))
  1278. goto out;
  1279. buffer = kmalloc(512, GFP_KERNEL | __GFP_DMA);
  1280. if (!buffer) {
  1281. printk(KERN_WARNING "(sd_revalidate_disk:) Memory allocation "
  1282. "failure.\n");
  1283. goto out;
  1284. }
  1285. /* defaults, until the device tells us otherwise */
  1286. sdp->sector_size = 512;
  1287. sdkp->capacity = 0;
  1288. sdkp->media_present = 1;
  1289. sdkp->write_prot = 0;
  1290. sdkp->WCE = 0;
  1291. sdkp->RCD = 0;
  1292. sd_spinup_disk(sdkp, disk->disk_name);
  1293. /*
  1294. * Without media there is no reason to ask; moreover, some devices
  1295. * react badly if we do.
  1296. */
  1297. if (sdkp->media_present) {
  1298. sd_read_capacity(sdkp, disk->disk_name, buffer);
  1299. sd_read_write_protect_flag(sdkp, disk->disk_name, buffer);
  1300. sd_read_cache_type(sdkp, disk->disk_name, buffer);
  1301. }
  1302. /*
  1303. * We now have all cache related info, determine how we deal
  1304. * with ordered requests. Note that as the current SCSI
  1305. * dispatch function can alter request order, we cannot use
  1306. * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
  1307. */
  1308. if (sdkp->WCE)
  1309. ordered = sdkp->DPOFUA
  1310. ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
  1311. else
  1312. ordered = QUEUE_ORDERED_DRAIN;
  1313. blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
  1314. set_capacity(disk, sdkp->capacity);
  1315. kfree(buffer);
  1316. out:
  1317. return 0;
  1318. }
  1319. /**
  1320. * sd_probe - called during driver initialization and whenever a
  1321. * new scsi device is attached to the system. It is called once
  1322. * for each scsi device (not just disks) present.
  1323. * @dev: pointer to device object
  1324. *
  1325. * Returns 0 if successful (or not interested in this scsi device
  1326. * (e.g. scanner)); 1 when there is an error.
  1327. *
  1328. * Note: this function is invoked from the scsi mid-level.
  1329. * This function sets up the mapping between a given
  1330. * <host,channel,id,lun> (found in sdp) and new device name
  1331. * (e.g. /dev/sda). More precisely it is the block device major
  1332. * and minor number that is chosen here.
  1333. *
  1334. * Assume sd_attach is not re-entrant (for time being)
  1335. * Also think about sd_attach() and sd_remove() running coincidentally.
  1336. **/
  1337. static int sd_probe(struct device *dev)
  1338. {
  1339. struct scsi_device *sdp = to_scsi_device(dev);
  1340. struct scsi_disk *sdkp;
  1341. struct gendisk *gd;
  1342. u32 index;
  1343. int error;
  1344. error = -ENODEV;
  1345. if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
  1346. goto out;
  1347. SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
  1348. "sd_attach\n"));
  1349. error = -ENOMEM;
  1350. sdkp = kmalloc(sizeof(*sdkp), GFP_KERNEL);
  1351. if (!sdkp)
  1352. goto out;
  1353. memset (sdkp, 0, sizeof(*sdkp));
  1354. kref_init(&sdkp->kref);
  1355. gd = alloc_disk(16);
  1356. if (!gd)
  1357. goto out_free;
  1358. if (!idr_pre_get(&sd_index_idr, GFP_KERNEL))
  1359. goto out_put;
  1360. spin_lock(&sd_index_lock);
  1361. error = idr_get_new(&sd_index_idr, NULL, &index);
  1362. spin_unlock(&sd_index_lock);
  1363. if (index >= SD_MAX_DISKS)
  1364. error = -EBUSY;
  1365. if (error)
  1366. goto out_put;
  1367. get_device(&sdp->sdev_gendev);
  1368. sdkp->device = sdp;
  1369. sdkp->driver = &sd_template;
  1370. sdkp->disk = gd;
  1371. sdkp->index = index;
  1372. sdkp->openers = 0;
  1373. if (!sdp->timeout) {
  1374. if (sdp->type != TYPE_MOD)
  1375. sdp->timeout = SD_TIMEOUT;
  1376. else
  1377. sdp->timeout = SD_MOD_TIMEOUT;
  1378. }
  1379. gd->major = sd_major((index & 0xf0) >> 4);
  1380. gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
  1381. gd->minors = 16;
  1382. gd->fops = &sd_fops;
  1383. if (index < 26) {
  1384. sprintf(gd->disk_name, "sd%c", 'a' + index % 26);
  1385. } else if (index < (26 + 1) * 26) {
  1386. sprintf(gd->disk_name, "sd%c%c",
  1387. 'a' + index / 26 - 1,'a' + index % 26);
  1388. } else {
  1389. const unsigned int m1 = (index / 26 - 1) / 26 - 1;
  1390. const unsigned int m2 = (index / 26 - 1) % 26;
  1391. const unsigned int m3 = index % 26;
  1392. sprintf(gd->disk_name, "sd%c%c%c",
  1393. 'a' + m1, 'a' + m2, 'a' + m3);
  1394. }
  1395. strcpy(gd->devfs_name, sdp->devfs_name);
  1396. gd->private_data = &sdkp->driver;
  1397. gd->queue = sdkp->device->request_queue;
  1398. sd_revalidate_disk(gd);
  1399. gd->driverfs_dev = &sdp->sdev_gendev;
  1400. gd->flags = GENHD_FL_DRIVERFS;
  1401. if (sdp->removable)
  1402. gd->flags |= GENHD_FL_REMOVABLE;
  1403. dev_set_drvdata(dev, sdkp);
  1404. add_disk(gd);
  1405. sdev_printk(KERN_NOTICE, sdp, "Attached scsi %sdisk %s\n",
  1406. sdp->removable ? "removable " : "", gd->disk_name);
  1407. return 0;
  1408. out_put:
  1409. put_disk(gd);
  1410. out_free:
  1411. kfree(sdkp);
  1412. out:
  1413. return error;
  1414. }
  1415. /**
  1416. * sd_remove - called whenever a scsi disk (previously recognized by
  1417. * sd_probe) is detached from the system. It is called (potentially
  1418. * multiple times) during sd module unload.
  1419. * @sdp: pointer to mid level scsi device object
  1420. *
  1421. * Note: this function is invoked from the scsi mid-level.
  1422. * This function potentially frees up a device name (e.g. /dev/sdc)
  1423. * that could be re-used by a subsequent sd_probe().
  1424. * This function is not called when the built-in sd driver is "exit-ed".
  1425. **/
  1426. static int sd_remove(struct device *dev)
  1427. {
  1428. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  1429. del_gendisk(sdkp->disk);
  1430. sd_shutdown(dev);
  1431. mutex_lock(&sd_ref_mutex);
  1432. dev_set_drvdata(dev, NULL);
  1433. kref_put(&sdkp->kref, scsi_disk_release);
  1434. mutex_unlock(&sd_ref_mutex);
  1435. return 0;
  1436. }
  1437. /**
  1438. * scsi_disk_release - Called to free the scsi_disk structure
  1439. * @kref: pointer to embedded kref
  1440. *
  1441. * sd_ref_mutex must be held entering this routine. Because it is
  1442. * called on last put, you should always use the scsi_disk_get()
  1443. * scsi_disk_put() helpers which manipulate the semaphore directly
  1444. * and never do a direct kref_put().
  1445. **/
  1446. static void scsi_disk_release(struct kref *kref)
  1447. {
  1448. struct scsi_disk *sdkp = to_scsi_disk(kref);
  1449. struct gendisk *disk = sdkp->disk;
  1450. spin_lock(&sd_index_lock);
  1451. idr_remove(&sd_index_idr, sdkp->index);
  1452. spin_unlock(&sd_index_lock);
  1453. disk->private_data = NULL;
  1454. put_disk(disk);
  1455. put_device(&sdkp->device->sdev_gendev);
  1456. kfree(sdkp);
  1457. }
  1458. /*
  1459. * Send a SYNCHRONIZE CACHE instruction down to the device through
  1460. * the normal SCSI command structure. Wait for the command to
  1461. * complete.
  1462. */
  1463. static void sd_shutdown(struct device *dev)
  1464. {
  1465. struct scsi_device *sdp = to_scsi_device(dev);
  1466. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  1467. if (!sdkp)
  1468. return; /* this can happen */
  1469. if (sdkp->WCE) {
  1470. printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n",
  1471. sdkp->disk->disk_name);
  1472. sd_sync_cache(sdp);
  1473. }
  1474. scsi_disk_put(sdkp);
  1475. }
  1476. /**
  1477. * init_sd - entry point for this driver (both when built in or when
  1478. * a module).
  1479. *
  1480. * Note: this function registers this driver with the scsi mid-level.
  1481. **/
  1482. static int __init init_sd(void)
  1483. {
  1484. int majors = 0, i;
  1485. SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
  1486. for (i = 0; i < SD_MAJORS; i++)
  1487. if (register_blkdev(sd_major(i), "sd") == 0)
  1488. majors++;
  1489. if (!majors)
  1490. return -ENODEV;
  1491. return scsi_register_driver(&sd_template.gendrv);
  1492. }
  1493. /**
  1494. * exit_sd - exit point for this driver (when it is a module).
  1495. *
  1496. * Note: this function unregisters this driver from the scsi mid-level.
  1497. **/
  1498. static void __exit exit_sd(void)
  1499. {
  1500. int i;
  1501. SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
  1502. scsi_unregister_driver(&sd_template.gendrv);
  1503. for (i = 0; i < SD_MAJORS; i++)
  1504. unregister_blkdev(sd_major(i), "sd");
  1505. }
  1506. MODULE_LICENSE("GPL");
  1507. MODULE_AUTHOR("Eric Youngdale");
  1508. MODULE_DESCRIPTION("SCSI disk (sd) driver");
  1509. module_init(init_sd);
  1510. module_exit(exit_sd);