sd.c 79 KB

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