sd.c 68 KB

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