sd.c 71 KB

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