sd.c 52 KB

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