sd.c 79 KB

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