sd.c 56 KB

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