sd.c 61 KB

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