sd.c 69 KB

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