sd.c 56 KB

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