sd.c 47 KB

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