sd.c 52 KB

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