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