sd.c 47 KB

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