cciss.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193
  1. /*
  2. * Disk Array driver for HP SA 5xxx and 6xxx Controllers
  3. * Copyright 2000, 2005 Hewlett-Packard Development Company, L.P.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  13. * NON INFRINGEMENT. See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * Questions/Comments/Bugfixes to iss_storagedev@hp.com
  20. *
  21. */
  22. #include <linux/config.h> /* CONFIG_PROC_FS */
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/types.h>
  26. #include <linux/pci.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. #include <linux/major.h>
  31. #include <linux/fs.h>
  32. #include <linux/bio.h>
  33. #include <linux/blkpg.h>
  34. #include <linux/timer.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/init.h>
  37. #include <linux/hdreg.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/compat.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/io.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/blkdev.h>
  44. #include <linux/genhd.h>
  45. #include <linux/completion.h>
  46. #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
  47. #define DRIVER_NAME "HP CISS Driver (v 2.6.8)"
  48. #define DRIVER_VERSION CCISS_DRIVER_VERSION(2,6,8)
  49. /* Embedded module documentation macros - see modules.h */
  50. MODULE_AUTHOR("Hewlett-Packard Company");
  51. MODULE_DESCRIPTION("Driver for HP Controller SA5xxx SA6xxx version 2.6.8");
  52. MODULE_SUPPORTED_DEVICE("HP SA5i SA5i+ SA532 SA5300 SA5312 SA641 SA642 SA6400"
  53. " SA6i P600 P800 P400 P400i E200 E200i");
  54. MODULE_LICENSE("GPL");
  55. #include "cciss_cmd.h"
  56. #include "cciss.h"
  57. #include <linux/cciss_ioctl.h>
  58. /* define the PCI info for the cards we can control */
  59. static const struct pci_device_id cciss_pci_device_id[] = {
  60. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS,
  61. 0x0E11, 0x4070, 0, 0, 0},
  62. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
  63. 0x0E11, 0x4080, 0, 0, 0},
  64. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
  65. 0x0E11, 0x4082, 0, 0, 0},
  66. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB,
  67. 0x0E11, 0x4083, 0, 0, 0},
  68. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
  69. 0x0E11, 0x409A, 0, 0, 0},
  70. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
  71. 0x0E11, 0x409B, 0, 0, 0},
  72. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
  73. 0x0E11, 0x409C, 0, 0, 0},
  74. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
  75. 0x0E11, 0x409D, 0, 0, 0},
  76. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC,
  77. 0x0E11, 0x4091, 0, 0, 0},
  78. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA,
  79. 0x103C, 0x3225, 0, 0, 0},
  80. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC,
  81. 0x103c, 0x3223, 0, 0, 0},
  82. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC,
  83. 0x103c, 0x3234, 0, 0, 0},
  84. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC,
  85. 0x103c, 0x3235, 0, 0, 0},
  86. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
  87. 0x103c, 0x3211, 0, 0, 0},
  88. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
  89. 0x103c, 0x3212, 0, 0, 0},
  90. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
  91. 0x103c, 0x3213, 0, 0, 0},
  92. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
  93. 0x103c, 0x3214, 0, 0, 0},
  94. { PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD,
  95. 0x103c, 0x3215, 0, 0, 0},
  96. {0,}
  97. };
  98. MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
  99. #define NR_PRODUCTS (sizeof(products)/sizeof(struct board_type))
  100. /* board_id = Subsystem Device ID & Vendor ID
  101. * product = Marketing Name for the board
  102. * access = Address of the struct of function pointers
  103. */
  104. static struct board_type products[] = {
  105. { 0x40700E11, "Smart Array 5300", &SA5_access },
  106. { 0x40800E11, "Smart Array 5i", &SA5B_access},
  107. { 0x40820E11, "Smart Array 532", &SA5B_access},
  108. { 0x40830E11, "Smart Array 5312", &SA5B_access},
  109. { 0x409A0E11, "Smart Array 641", &SA5_access},
  110. { 0x409B0E11, "Smart Array 642", &SA5_access},
  111. { 0x409C0E11, "Smart Array 6400", &SA5_access},
  112. { 0x409D0E11, "Smart Array 6400 EM", &SA5_access},
  113. { 0x40910E11, "Smart Array 6i", &SA5_access},
  114. { 0x3225103C, "Smart Array P600", &SA5_access},
  115. { 0x3223103C, "Smart Array P800", &SA5_access},
  116. { 0x3234103C, "Smart Array P400", &SA5_access},
  117. { 0x3235103C, "Smart Array P400i", &SA5_access},
  118. { 0x3211103C, "Smart Array E200i", &SA5_access},
  119. { 0x3212103C, "Smart Array E200", &SA5_access},
  120. { 0x3213103C, "Smart Array E200i", &SA5_access},
  121. { 0x3214103C, "Smart Array E200i", &SA5_access},
  122. { 0x3215103C, "Smart Array E200i", &SA5_access},
  123. };
  124. /* How long to wait (in millesconds) for board to go into simple mode */
  125. #define MAX_CONFIG_WAIT 30000
  126. #define MAX_IOCTL_CONFIG_WAIT 1000
  127. /*define how many times we will try a command because of bus resets */
  128. #define MAX_CMD_RETRIES 3
  129. #define READ_AHEAD 1024
  130. #define NR_CMDS 384 /* #commands that can be outstanding */
  131. #define MAX_CTLR 32
  132. /* Originally cciss driver only supports 8 major numbers */
  133. #define MAX_CTLR_ORIG 8
  134. static ctlr_info_t *hba[MAX_CTLR];
  135. static void do_cciss_request(request_queue_t *q);
  136. static int cciss_open(struct inode *inode, struct file *filep);
  137. static int cciss_release(struct inode *inode, struct file *filep);
  138. static int cciss_ioctl(struct inode *inode, struct file *filep,
  139. unsigned int cmd, unsigned long arg);
  140. static int revalidate_allvol(ctlr_info_t *host);
  141. static int cciss_revalidate(struct gendisk *disk);
  142. static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk);
  143. static int deregister_disk(struct gendisk *disk, drive_info_struct *drv, int clear_all);
  144. static void cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
  145. int withirq, unsigned int *total_size, unsigned int *block_size);
  146. static void cciss_geometry_inquiry(int ctlr, int logvol,
  147. int withirq, unsigned int total_size,
  148. unsigned int block_size, InquiryData_struct *inq_buff,
  149. drive_info_struct *drv);
  150. static void cciss_getgeometry(int cntl_num);
  151. static void start_io( ctlr_info_t *h);
  152. static int sendcmd( __u8 cmd, int ctlr, void *buff, size_t size,
  153. unsigned int use_unit_num, unsigned int log_unit, __u8 page_code,
  154. unsigned char *scsi3addr, int cmd_type);
  155. static int sendcmd_withirq(__u8 cmd, int ctlr, void *buff, size_t size,
  156. unsigned int use_unit_num, unsigned int log_unit, __u8 page_code,
  157. int cmd_type);
  158. static void fail_all_cmds(unsigned long ctlr);
  159. #ifdef CONFIG_PROC_FS
  160. static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
  161. int length, int *eof, void *data);
  162. static void cciss_procinit(int i);
  163. #else
  164. static void cciss_procinit(int i) {}
  165. #endif /* CONFIG_PROC_FS */
  166. #ifdef CONFIG_COMPAT
  167. static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg);
  168. #endif
  169. static struct block_device_operations cciss_fops = {
  170. .owner = THIS_MODULE,
  171. .open = cciss_open,
  172. .release = cciss_release,
  173. .ioctl = cciss_ioctl,
  174. #ifdef CONFIG_COMPAT
  175. .compat_ioctl = cciss_compat_ioctl,
  176. #endif
  177. .revalidate_disk= cciss_revalidate,
  178. };
  179. /*
  180. * Enqueuing and dequeuing functions for cmdlists.
  181. */
  182. static inline void addQ(CommandList_struct **Qptr, CommandList_struct *c)
  183. {
  184. if (*Qptr == NULL) {
  185. *Qptr = c;
  186. c->next = c->prev = c;
  187. } else {
  188. c->prev = (*Qptr)->prev;
  189. c->next = (*Qptr);
  190. (*Qptr)->prev->next = c;
  191. (*Qptr)->prev = c;
  192. }
  193. }
  194. static inline CommandList_struct *removeQ(CommandList_struct **Qptr,
  195. CommandList_struct *c)
  196. {
  197. if (c && c->next != c) {
  198. if (*Qptr == c) *Qptr = c->next;
  199. c->prev->next = c->next;
  200. c->next->prev = c->prev;
  201. } else {
  202. *Qptr = NULL;
  203. }
  204. return c;
  205. }
  206. #include "cciss_scsi.c" /* For SCSI tape support */
  207. #ifdef CONFIG_PROC_FS
  208. /*
  209. * Report information about this controller.
  210. */
  211. #define ENG_GIG 1000000000
  212. #define ENG_GIG_FACTOR (ENG_GIG/512)
  213. #define RAID_UNKNOWN 6
  214. static const char *raid_label[] = {"0","4","1(1+0)","5","5+1","ADG",
  215. "UNKNOWN"};
  216. static struct proc_dir_entry *proc_cciss;
  217. static int cciss_proc_get_info(char *buffer, char **start, off_t offset,
  218. int length, int *eof, void *data)
  219. {
  220. off_t pos = 0;
  221. off_t len = 0;
  222. int size, i, ctlr;
  223. ctlr_info_t *h = (ctlr_info_t*)data;
  224. drive_info_struct *drv;
  225. unsigned long flags;
  226. sector_t vol_sz, vol_sz_frac;
  227. ctlr = h->ctlr;
  228. /* prevent displaying bogus info during configuration
  229. * or deconfiguration of a logical volume
  230. */
  231. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  232. if (h->busy_configuring) {
  233. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  234. return -EBUSY;
  235. }
  236. h->busy_configuring = 1;
  237. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  238. size = sprintf(buffer, "%s: HP %s Controller\n"
  239. "Board ID: 0x%08lx\n"
  240. "Firmware Version: %c%c%c%c\n"
  241. "IRQ: %d\n"
  242. "Logical drives: %d\n"
  243. "Current Q depth: %d\n"
  244. "Current # commands on controller: %d\n"
  245. "Max Q depth since init: %d\n"
  246. "Max # commands on controller since init: %d\n"
  247. "Max SG entries since init: %d\n\n",
  248. h->devname,
  249. h->product_name,
  250. (unsigned long)h->board_id,
  251. h->firm_ver[0], h->firm_ver[1], h->firm_ver[2], h->firm_ver[3],
  252. (unsigned int)h->intr,
  253. h->num_luns,
  254. h->Qdepth, h->commands_outstanding,
  255. h->maxQsinceinit, h->max_outstanding, h->maxSG);
  256. pos += size; len += size;
  257. cciss_proc_tape_report(ctlr, buffer, &pos, &len);
  258. for(i=0; i<=h->highest_lun; i++) {
  259. drv = &h->drv[i];
  260. if (drv->heads == 0)
  261. continue;
  262. vol_sz = drv->nr_blocks;
  263. vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
  264. vol_sz_frac *= 100;
  265. sector_div(vol_sz_frac, ENG_GIG_FACTOR);
  266. if (drv->raid_level > 5)
  267. drv->raid_level = RAID_UNKNOWN;
  268. size = sprintf(buffer+len, "cciss/c%dd%d:"
  269. "\t%4u.%02uGB\tRAID %s\n",
  270. ctlr, i, (int)vol_sz, (int)vol_sz_frac,
  271. raid_label[drv->raid_level]);
  272. pos += size; len += size;
  273. }
  274. *eof = 1;
  275. *start = buffer+offset;
  276. len -= offset;
  277. if (len>length)
  278. len = length;
  279. h->busy_configuring = 0;
  280. return len;
  281. }
  282. static int
  283. cciss_proc_write(struct file *file, const char __user *buffer,
  284. unsigned long count, void *data)
  285. {
  286. unsigned char cmd[80];
  287. int len;
  288. #ifdef CONFIG_CISS_SCSI_TAPE
  289. ctlr_info_t *h = (ctlr_info_t *) data;
  290. int rc;
  291. #endif
  292. if (count > sizeof(cmd)-1) return -EINVAL;
  293. if (copy_from_user(cmd, buffer, count)) return -EFAULT;
  294. cmd[count] = '\0';
  295. len = strlen(cmd); // above 3 lines ensure safety
  296. if (len && cmd[len-1] == '\n')
  297. cmd[--len] = '\0';
  298. # ifdef CONFIG_CISS_SCSI_TAPE
  299. if (strcmp("engage scsi", cmd)==0) {
  300. rc = cciss_engage_scsi(h->ctlr);
  301. if (rc != 0) return -rc;
  302. return count;
  303. }
  304. /* might be nice to have "disengage" too, but it's not
  305. safely possible. (only 1 module use count, lock issues.) */
  306. # endif
  307. return -EINVAL;
  308. }
  309. /*
  310. * Get us a file in /proc/cciss that says something about each controller.
  311. * Create /proc/cciss if it doesn't exist yet.
  312. */
  313. static void __devinit cciss_procinit(int i)
  314. {
  315. struct proc_dir_entry *pde;
  316. if (proc_cciss == NULL) {
  317. proc_cciss = proc_mkdir("cciss", proc_root_driver);
  318. if (!proc_cciss)
  319. return;
  320. }
  321. pde = create_proc_read_entry(hba[i]->devname,
  322. S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH,
  323. proc_cciss, cciss_proc_get_info, hba[i]);
  324. pde->write_proc = cciss_proc_write;
  325. }
  326. #endif /* CONFIG_PROC_FS */
  327. /*
  328. * For operations that cannot sleep, a command block is allocated at init,
  329. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  330. * which ones are free or in use. For operations that can wait for kmalloc
  331. * to possible sleep, this routine can be called with get_from_pool set to 0.
  332. * cmd_free() MUST be called with a got_from_pool set to 0 if cmd_alloc was.
  333. */
  334. static CommandList_struct * cmd_alloc(ctlr_info_t *h, int get_from_pool)
  335. {
  336. CommandList_struct *c;
  337. int i;
  338. u64bit temp64;
  339. dma_addr_t cmd_dma_handle, err_dma_handle;
  340. if (!get_from_pool)
  341. {
  342. c = (CommandList_struct *) pci_alloc_consistent(
  343. h->pdev, sizeof(CommandList_struct), &cmd_dma_handle);
  344. if(c==NULL)
  345. return NULL;
  346. memset(c, 0, sizeof(CommandList_struct));
  347. c->cmdindex = -1;
  348. c->err_info = (ErrorInfo_struct *)pci_alloc_consistent(
  349. h->pdev, sizeof(ErrorInfo_struct),
  350. &err_dma_handle);
  351. if (c->err_info == NULL)
  352. {
  353. pci_free_consistent(h->pdev,
  354. sizeof(CommandList_struct), c, cmd_dma_handle);
  355. return NULL;
  356. }
  357. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  358. } else /* get it out of the controllers pool */
  359. {
  360. do {
  361. i = find_first_zero_bit(h->cmd_pool_bits, NR_CMDS);
  362. if (i == NR_CMDS)
  363. return NULL;
  364. } while(test_and_set_bit(i & (BITS_PER_LONG - 1), h->cmd_pool_bits+(i/BITS_PER_LONG)) != 0);
  365. #ifdef CCISS_DEBUG
  366. printk(KERN_DEBUG "cciss: using command buffer %d\n", i);
  367. #endif
  368. c = h->cmd_pool + i;
  369. memset(c, 0, sizeof(CommandList_struct));
  370. cmd_dma_handle = h->cmd_pool_dhandle
  371. + i*sizeof(CommandList_struct);
  372. c->err_info = h->errinfo_pool + i;
  373. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  374. err_dma_handle = h->errinfo_pool_dhandle
  375. + i*sizeof(ErrorInfo_struct);
  376. h->nr_allocs++;
  377. c->cmdindex = i;
  378. }
  379. c->busaddr = (__u32) cmd_dma_handle;
  380. temp64.val = (__u64) err_dma_handle;
  381. c->ErrDesc.Addr.lower = temp64.val32.lower;
  382. c->ErrDesc.Addr.upper = temp64.val32.upper;
  383. c->ErrDesc.Len = sizeof(ErrorInfo_struct);
  384. c->ctlr = h->ctlr;
  385. return c;
  386. }
  387. /*
  388. * Frees a command block that was previously allocated with cmd_alloc().
  389. */
  390. static void cmd_free(ctlr_info_t *h, CommandList_struct *c, int got_from_pool)
  391. {
  392. int i;
  393. u64bit temp64;
  394. if( !got_from_pool)
  395. {
  396. temp64.val32.lower = c->ErrDesc.Addr.lower;
  397. temp64.val32.upper = c->ErrDesc.Addr.upper;
  398. pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
  399. c->err_info, (dma_addr_t) temp64.val);
  400. pci_free_consistent(h->pdev, sizeof(CommandList_struct),
  401. c, (dma_addr_t) c->busaddr);
  402. } else
  403. {
  404. i = c - h->cmd_pool;
  405. clear_bit(i&(BITS_PER_LONG-1), h->cmd_pool_bits+(i/BITS_PER_LONG));
  406. h->nr_frees++;
  407. }
  408. }
  409. static inline ctlr_info_t *get_host(struct gendisk *disk)
  410. {
  411. return disk->queue->queuedata;
  412. }
  413. static inline drive_info_struct *get_drv(struct gendisk *disk)
  414. {
  415. return disk->private_data;
  416. }
  417. /*
  418. * Open. Make sure the device is really there.
  419. */
  420. static int cciss_open(struct inode *inode, struct file *filep)
  421. {
  422. ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
  423. drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
  424. #ifdef CCISS_DEBUG
  425. printk(KERN_DEBUG "cciss_open %s\n", inode->i_bdev->bd_disk->disk_name);
  426. #endif /* CCISS_DEBUG */
  427. if (host->busy_initializing)
  428. return -EBUSY;
  429. if (host->busy_initializing || drv->busy_configuring)
  430. return -EBUSY;
  431. /*
  432. * Root is allowed to open raw volume zero even if it's not configured
  433. * so array config can still work. Root is also allowed to open any
  434. * volume that has a LUN ID, so it can issue IOCTL to reread the
  435. * disk information. I don't think I really like this
  436. * but I'm already using way to many device nodes to claim another one
  437. * for "raw controller".
  438. */
  439. if (drv->nr_blocks == 0) {
  440. if (iminor(inode) != 0) { /* not node 0? */
  441. /* if not node 0 make sure it is a partition = 0 */
  442. if (iminor(inode) & 0x0f) {
  443. return -ENXIO;
  444. /* if it is, make sure we have a LUN ID */
  445. } else if (drv->LunID == 0) {
  446. return -ENXIO;
  447. }
  448. }
  449. if (!capable(CAP_SYS_ADMIN))
  450. return -EPERM;
  451. }
  452. drv->usage_count++;
  453. host->usage_count++;
  454. return 0;
  455. }
  456. /*
  457. * Close. Sync first.
  458. */
  459. static int cciss_release(struct inode *inode, struct file *filep)
  460. {
  461. ctlr_info_t *host = get_host(inode->i_bdev->bd_disk);
  462. drive_info_struct *drv = get_drv(inode->i_bdev->bd_disk);
  463. #ifdef CCISS_DEBUG
  464. printk(KERN_DEBUG "cciss_release %s\n", inode->i_bdev->bd_disk->disk_name);
  465. #endif /* CCISS_DEBUG */
  466. drv->usage_count--;
  467. host->usage_count--;
  468. return 0;
  469. }
  470. #ifdef CONFIG_COMPAT
  471. static int do_ioctl(struct file *f, unsigned cmd, unsigned long arg)
  472. {
  473. int ret;
  474. lock_kernel();
  475. ret = cciss_ioctl(f->f_dentry->d_inode, f, cmd, arg);
  476. unlock_kernel();
  477. return ret;
  478. }
  479. static int cciss_ioctl32_passthru(struct file *f, unsigned cmd, unsigned long arg);
  480. static int cciss_ioctl32_big_passthru(struct file *f, unsigned cmd, unsigned long arg);
  481. static long cciss_compat_ioctl(struct file *f, unsigned cmd, unsigned long arg)
  482. {
  483. switch (cmd) {
  484. case CCISS_GETPCIINFO:
  485. case CCISS_GETINTINFO:
  486. case CCISS_SETINTINFO:
  487. case CCISS_GETNODENAME:
  488. case CCISS_SETNODENAME:
  489. case CCISS_GETHEARTBEAT:
  490. case CCISS_GETBUSTYPES:
  491. case CCISS_GETFIRMVER:
  492. case CCISS_GETDRIVVER:
  493. case CCISS_REVALIDVOLS:
  494. case CCISS_DEREGDISK:
  495. case CCISS_REGNEWDISK:
  496. case CCISS_REGNEWD:
  497. case CCISS_RESCANDISK:
  498. case CCISS_GETLUNINFO:
  499. return do_ioctl(f, cmd, arg);
  500. case CCISS_PASSTHRU32:
  501. return cciss_ioctl32_passthru(f, cmd, arg);
  502. case CCISS_BIG_PASSTHRU32:
  503. return cciss_ioctl32_big_passthru(f, cmd, arg);
  504. default:
  505. return -ENOIOCTLCMD;
  506. }
  507. }
  508. static int cciss_ioctl32_passthru(struct file *f, unsigned cmd, unsigned long arg)
  509. {
  510. IOCTL32_Command_struct __user *arg32 =
  511. (IOCTL32_Command_struct __user *) arg;
  512. IOCTL_Command_struct arg64;
  513. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  514. int err;
  515. u32 cp;
  516. err = 0;
  517. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
  518. err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
  519. err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
  520. err |= get_user(arg64.buf_size, &arg32->buf_size);
  521. err |= get_user(cp, &arg32->buf);
  522. arg64.buf = compat_ptr(cp);
  523. err |= copy_to_user(p, &arg64, sizeof(arg64));
  524. if (err)
  525. return -EFAULT;
  526. err = do_ioctl(f, CCISS_PASSTHRU, (unsigned long) p);
  527. if (err)
  528. return err;
  529. err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(arg32->error_info));
  530. if (err)
  531. return -EFAULT;
  532. return err;
  533. }
  534. static int cciss_ioctl32_big_passthru(struct file *file, unsigned cmd, unsigned long arg)
  535. {
  536. BIG_IOCTL32_Command_struct __user *arg32 =
  537. (BIG_IOCTL32_Command_struct __user *) arg;
  538. BIG_IOCTL_Command_struct arg64;
  539. BIG_IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  540. int err;
  541. u32 cp;
  542. err = 0;
  543. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info, sizeof(arg64.LUN_info));
  544. err |= copy_from_user(&arg64.Request, &arg32->Request, sizeof(arg64.Request));
  545. err |= copy_from_user(&arg64.error_info, &arg32->error_info, sizeof(arg64.error_info));
  546. err |= get_user(arg64.buf_size, &arg32->buf_size);
  547. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  548. err |= get_user(cp, &arg32->buf);
  549. arg64.buf = compat_ptr(cp);
  550. err |= copy_to_user(p, &arg64, sizeof(arg64));
  551. if (err)
  552. return -EFAULT;
  553. err = do_ioctl(file, CCISS_BIG_PASSTHRU, (unsigned long) p);
  554. if (err)
  555. return err;
  556. err |= copy_in_user(&arg32->error_info, &p->error_info, sizeof(arg32->error_info));
  557. if (err)
  558. return -EFAULT;
  559. return err;
  560. }
  561. #endif
  562. /*
  563. * ioctl
  564. */
  565. static int cciss_ioctl(struct inode *inode, struct file *filep,
  566. unsigned int cmd, unsigned long arg)
  567. {
  568. struct block_device *bdev = inode->i_bdev;
  569. struct gendisk *disk = bdev->bd_disk;
  570. ctlr_info_t *host = get_host(disk);
  571. drive_info_struct *drv = get_drv(disk);
  572. int ctlr = host->ctlr;
  573. void __user *argp = (void __user *)arg;
  574. #ifdef CCISS_DEBUG
  575. printk(KERN_DEBUG "cciss_ioctl: Called with cmd=%x %lx\n", cmd, arg);
  576. #endif /* CCISS_DEBUG */
  577. switch(cmd) {
  578. case HDIO_GETGEO:
  579. {
  580. struct hd_geometry driver_geo;
  581. if (drv->cylinders) {
  582. driver_geo.heads = drv->heads;
  583. driver_geo.sectors = drv->sectors;
  584. driver_geo.cylinders = drv->cylinders;
  585. } else
  586. return -ENXIO;
  587. driver_geo.start= get_start_sect(inode->i_bdev);
  588. if (copy_to_user(argp, &driver_geo, sizeof(struct hd_geometry)))
  589. return -EFAULT;
  590. return(0);
  591. }
  592. case CCISS_GETPCIINFO:
  593. {
  594. cciss_pci_info_struct pciinfo;
  595. if (!arg) return -EINVAL;
  596. pciinfo.domain = pci_domain_nr(host->pdev->bus);
  597. pciinfo.bus = host->pdev->bus->number;
  598. pciinfo.dev_fn = host->pdev->devfn;
  599. pciinfo.board_id = host->board_id;
  600. if (copy_to_user(argp, &pciinfo, sizeof( cciss_pci_info_struct )))
  601. return -EFAULT;
  602. return(0);
  603. }
  604. case CCISS_GETINTINFO:
  605. {
  606. cciss_coalint_struct intinfo;
  607. if (!arg) return -EINVAL;
  608. intinfo.delay = readl(&host->cfgtable->HostWrite.CoalIntDelay);
  609. intinfo.count = readl(&host->cfgtable->HostWrite.CoalIntCount);
  610. if (copy_to_user(argp, &intinfo, sizeof( cciss_coalint_struct )))
  611. return -EFAULT;
  612. return(0);
  613. }
  614. case CCISS_SETINTINFO:
  615. {
  616. cciss_coalint_struct intinfo;
  617. unsigned long flags;
  618. int i;
  619. if (!arg) return -EINVAL;
  620. if (!capable(CAP_SYS_ADMIN)) return -EPERM;
  621. if (copy_from_user(&intinfo, argp, sizeof( cciss_coalint_struct)))
  622. return -EFAULT;
  623. if ( (intinfo.delay == 0 ) && (intinfo.count == 0))
  624. {
  625. // printk("cciss_ioctl: delay and count cannot be 0\n");
  626. return( -EINVAL);
  627. }
  628. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  629. /* Update the field, and then ring the doorbell */
  630. writel( intinfo.delay,
  631. &(host->cfgtable->HostWrite.CoalIntDelay));
  632. writel( intinfo.count,
  633. &(host->cfgtable->HostWrite.CoalIntCount));
  634. writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
  635. for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
  636. if (!(readl(host->vaddr + SA5_DOORBELL)
  637. & CFGTBL_ChangeReq))
  638. break;
  639. /* delay and try again */
  640. udelay(1000);
  641. }
  642. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  643. if (i >= MAX_IOCTL_CONFIG_WAIT)
  644. return -EAGAIN;
  645. return(0);
  646. }
  647. case CCISS_GETNODENAME:
  648. {
  649. NodeName_type NodeName;
  650. int i;
  651. if (!arg) return -EINVAL;
  652. for(i=0;i<16;i++)
  653. NodeName[i] = readb(&host->cfgtable->ServerName[i]);
  654. if (copy_to_user(argp, NodeName, sizeof( NodeName_type)))
  655. return -EFAULT;
  656. return(0);
  657. }
  658. case CCISS_SETNODENAME:
  659. {
  660. NodeName_type NodeName;
  661. unsigned long flags;
  662. int i;
  663. if (!arg) return -EINVAL;
  664. if (!capable(CAP_SYS_ADMIN)) return -EPERM;
  665. if (copy_from_user(NodeName, argp, sizeof( NodeName_type)))
  666. return -EFAULT;
  667. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  668. /* Update the field, and then ring the doorbell */
  669. for(i=0;i<16;i++)
  670. writeb( NodeName[i], &host->cfgtable->ServerName[i]);
  671. writel( CFGTBL_ChangeReq, host->vaddr + SA5_DOORBELL);
  672. for(i=0;i<MAX_IOCTL_CONFIG_WAIT;i++) {
  673. if (!(readl(host->vaddr + SA5_DOORBELL)
  674. & CFGTBL_ChangeReq))
  675. break;
  676. /* delay and try again */
  677. udelay(1000);
  678. }
  679. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  680. if (i >= MAX_IOCTL_CONFIG_WAIT)
  681. return -EAGAIN;
  682. return(0);
  683. }
  684. case CCISS_GETHEARTBEAT:
  685. {
  686. Heartbeat_type heartbeat;
  687. if (!arg) return -EINVAL;
  688. heartbeat = readl(&host->cfgtable->HeartBeat);
  689. if (copy_to_user(argp, &heartbeat, sizeof( Heartbeat_type)))
  690. return -EFAULT;
  691. return(0);
  692. }
  693. case CCISS_GETBUSTYPES:
  694. {
  695. BusTypes_type BusTypes;
  696. if (!arg) return -EINVAL;
  697. BusTypes = readl(&host->cfgtable->BusTypes);
  698. if (copy_to_user(argp, &BusTypes, sizeof( BusTypes_type) ))
  699. return -EFAULT;
  700. return(0);
  701. }
  702. case CCISS_GETFIRMVER:
  703. {
  704. FirmwareVer_type firmware;
  705. if (!arg) return -EINVAL;
  706. memcpy(firmware, host->firm_ver, 4);
  707. if (copy_to_user(argp, firmware, sizeof( FirmwareVer_type)))
  708. return -EFAULT;
  709. return(0);
  710. }
  711. case CCISS_GETDRIVVER:
  712. {
  713. DriverVer_type DriverVer = DRIVER_VERSION;
  714. if (!arg) return -EINVAL;
  715. if (copy_to_user(argp, &DriverVer, sizeof( DriverVer_type) ))
  716. return -EFAULT;
  717. return(0);
  718. }
  719. case CCISS_REVALIDVOLS:
  720. if (bdev != bdev->bd_contains || drv != host->drv)
  721. return -ENXIO;
  722. return revalidate_allvol(host);
  723. case CCISS_GETLUNINFO: {
  724. LogvolInfo_struct luninfo;
  725. luninfo.LunID = drv->LunID;
  726. luninfo.num_opens = drv->usage_count;
  727. luninfo.num_parts = 0;
  728. if (copy_to_user(argp, &luninfo,
  729. sizeof(LogvolInfo_struct)))
  730. return -EFAULT;
  731. return(0);
  732. }
  733. case CCISS_DEREGDISK:
  734. return rebuild_lun_table(host, disk);
  735. case CCISS_REGNEWD:
  736. return rebuild_lun_table(host, NULL);
  737. case CCISS_PASSTHRU:
  738. {
  739. IOCTL_Command_struct iocommand;
  740. CommandList_struct *c;
  741. char *buff = NULL;
  742. u64bit temp64;
  743. unsigned long flags;
  744. DECLARE_COMPLETION(wait);
  745. if (!arg) return -EINVAL;
  746. if (!capable(CAP_SYS_RAWIO)) return -EPERM;
  747. if (copy_from_user(&iocommand, argp, sizeof( IOCTL_Command_struct) ))
  748. return -EFAULT;
  749. if((iocommand.buf_size < 1) &&
  750. (iocommand.Request.Type.Direction != XFER_NONE))
  751. {
  752. return -EINVAL;
  753. }
  754. #if 0 /* 'buf_size' member is 16-bits, and always smaller than kmalloc limit */
  755. /* Check kmalloc limits */
  756. if(iocommand.buf_size > 128000)
  757. return -EINVAL;
  758. #endif
  759. if(iocommand.buf_size > 0)
  760. {
  761. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  762. if( buff == NULL)
  763. return -EFAULT;
  764. }
  765. if (iocommand.Request.Type.Direction == XFER_WRITE)
  766. {
  767. /* Copy the data into the buffer we created */
  768. if (copy_from_user(buff, iocommand.buf, iocommand.buf_size))
  769. {
  770. kfree(buff);
  771. return -EFAULT;
  772. }
  773. } else {
  774. memset(buff, 0, iocommand.buf_size);
  775. }
  776. if ((c = cmd_alloc(host , 0)) == NULL)
  777. {
  778. kfree(buff);
  779. return -ENOMEM;
  780. }
  781. // Fill in the command type
  782. c->cmd_type = CMD_IOCTL_PEND;
  783. // Fill in Command Header
  784. c->Header.ReplyQueue = 0; // unused in simple mode
  785. if( iocommand.buf_size > 0) // buffer to fill
  786. {
  787. c->Header.SGList = 1;
  788. c->Header.SGTotal= 1;
  789. } else // no buffers to fill
  790. {
  791. c->Header.SGList = 0;
  792. c->Header.SGTotal= 0;
  793. }
  794. c->Header.LUN = iocommand.LUN_info;
  795. c->Header.Tag.lower = c->busaddr; // use the kernel address the cmd block for tag
  796. // Fill in Request block
  797. c->Request = iocommand.Request;
  798. // Fill in the scatter gather information
  799. if (iocommand.buf_size > 0 )
  800. {
  801. temp64.val = pci_map_single( host->pdev, buff,
  802. iocommand.buf_size,
  803. PCI_DMA_BIDIRECTIONAL);
  804. c->SG[0].Addr.lower = temp64.val32.lower;
  805. c->SG[0].Addr.upper = temp64.val32.upper;
  806. c->SG[0].Len = iocommand.buf_size;
  807. c->SG[0].Ext = 0; // we are not chaining
  808. }
  809. c->waiting = &wait;
  810. /* Put the request on the tail of the request queue */
  811. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  812. addQ(&host->reqQ, c);
  813. host->Qdepth++;
  814. start_io(host);
  815. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  816. wait_for_completion(&wait);
  817. /* unlock the buffers from DMA */
  818. temp64.val32.lower = c->SG[0].Addr.lower;
  819. temp64.val32.upper = c->SG[0].Addr.upper;
  820. pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
  821. iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
  822. /* Copy the error information out */
  823. iocommand.error_info = *(c->err_info);
  824. if ( copy_to_user(argp, &iocommand, sizeof( IOCTL_Command_struct) ) )
  825. {
  826. kfree(buff);
  827. cmd_free(host, c, 0);
  828. return( -EFAULT);
  829. }
  830. if (iocommand.Request.Type.Direction == XFER_READ)
  831. {
  832. /* Copy the data out of the buffer we created */
  833. if (copy_to_user(iocommand.buf, buff, iocommand.buf_size))
  834. {
  835. kfree(buff);
  836. cmd_free(host, c, 0);
  837. return -EFAULT;
  838. }
  839. }
  840. kfree(buff);
  841. cmd_free(host, c, 0);
  842. return(0);
  843. }
  844. case CCISS_BIG_PASSTHRU: {
  845. BIG_IOCTL_Command_struct *ioc;
  846. CommandList_struct *c;
  847. unsigned char **buff = NULL;
  848. int *buff_size = NULL;
  849. u64bit temp64;
  850. unsigned long flags;
  851. BYTE sg_used = 0;
  852. int status = 0;
  853. int i;
  854. DECLARE_COMPLETION(wait);
  855. __u32 left;
  856. __u32 sz;
  857. BYTE __user *data_ptr;
  858. if (!arg)
  859. return -EINVAL;
  860. if (!capable(CAP_SYS_RAWIO))
  861. return -EPERM;
  862. ioc = (BIG_IOCTL_Command_struct *)
  863. kmalloc(sizeof(*ioc), GFP_KERNEL);
  864. if (!ioc) {
  865. status = -ENOMEM;
  866. goto cleanup1;
  867. }
  868. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  869. status = -EFAULT;
  870. goto cleanup1;
  871. }
  872. if ((ioc->buf_size < 1) &&
  873. (ioc->Request.Type.Direction != XFER_NONE)) {
  874. status = -EINVAL;
  875. goto cleanup1;
  876. }
  877. /* Check kmalloc limits using all SGs */
  878. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  879. status = -EINVAL;
  880. goto cleanup1;
  881. }
  882. if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
  883. status = -EINVAL;
  884. goto cleanup1;
  885. }
  886. buff = (unsigned char **) kmalloc(MAXSGENTRIES *
  887. sizeof(char *), GFP_KERNEL);
  888. if (!buff) {
  889. status = -ENOMEM;
  890. goto cleanup1;
  891. }
  892. memset(buff, 0, MAXSGENTRIES);
  893. buff_size = (int *) kmalloc(MAXSGENTRIES * sizeof(int),
  894. GFP_KERNEL);
  895. if (!buff_size) {
  896. status = -ENOMEM;
  897. goto cleanup1;
  898. }
  899. left = ioc->buf_size;
  900. data_ptr = ioc->buf;
  901. while (left) {
  902. sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
  903. buff_size[sg_used] = sz;
  904. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  905. if (buff[sg_used] == NULL) {
  906. status = -ENOMEM;
  907. goto cleanup1;
  908. }
  909. if (ioc->Request.Type.Direction == XFER_WRITE &&
  910. copy_from_user(buff[sg_used], data_ptr, sz)) {
  911. status = -ENOMEM;
  912. goto cleanup1;
  913. } else {
  914. memset(buff[sg_used], 0, sz);
  915. }
  916. left -= sz;
  917. data_ptr += sz;
  918. sg_used++;
  919. }
  920. if ((c = cmd_alloc(host , 0)) == NULL) {
  921. status = -ENOMEM;
  922. goto cleanup1;
  923. }
  924. c->cmd_type = CMD_IOCTL_PEND;
  925. c->Header.ReplyQueue = 0;
  926. if( ioc->buf_size > 0) {
  927. c->Header.SGList = sg_used;
  928. c->Header.SGTotal= sg_used;
  929. } else {
  930. c->Header.SGList = 0;
  931. c->Header.SGTotal= 0;
  932. }
  933. c->Header.LUN = ioc->LUN_info;
  934. c->Header.Tag.lower = c->busaddr;
  935. c->Request = ioc->Request;
  936. if (ioc->buf_size > 0 ) {
  937. int i;
  938. for(i=0; i<sg_used; i++) {
  939. temp64.val = pci_map_single( host->pdev, buff[i],
  940. buff_size[i],
  941. PCI_DMA_BIDIRECTIONAL);
  942. c->SG[i].Addr.lower = temp64.val32.lower;
  943. c->SG[i].Addr.upper = temp64.val32.upper;
  944. c->SG[i].Len = buff_size[i];
  945. c->SG[i].Ext = 0; /* we are not chaining */
  946. }
  947. }
  948. c->waiting = &wait;
  949. /* Put the request on the tail of the request queue */
  950. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  951. addQ(&host->reqQ, c);
  952. host->Qdepth++;
  953. start_io(host);
  954. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  955. wait_for_completion(&wait);
  956. /* unlock the buffers from DMA */
  957. for(i=0; i<sg_used; i++) {
  958. temp64.val32.lower = c->SG[i].Addr.lower;
  959. temp64.val32.upper = c->SG[i].Addr.upper;
  960. pci_unmap_single( host->pdev, (dma_addr_t) temp64.val,
  961. buff_size[i], PCI_DMA_BIDIRECTIONAL);
  962. }
  963. /* Copy the error information out */
  964. ioc->error_info = *(c->err_info);
  965. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  966. cmd_free(host, c, 0);
  967. status = -EFAULT;
  968. goto cleanup1;
  969. }
  970. if (ioc->Request.Type.Direction == XFER_READ) {
  971. /* Copy the data out of the buffer we created */
  972. BYTE __user *ptr = ioc->buf;
  973. for(i=0; i< sg_used; i++) {
  974. if (copy_to_user(ptr, buff[i], buff_size[i])) {
  975. cmd_free(host, c, 0);
  976. status = -EFAULT;
  977. goto cleanup1;
  978. }
  979. ptr += buff_size[i];
  980. }
  981. }
  982. cmd_free(host, c, 0);
  983. status = 0;
  984. cleanup1:
  985. if (buff) {
  986. for(i=0; i<sg_used; i++)
  987. if(buff[i] != NULL)
  988. kfree(buff[i]);
  989. kfree(buff);
  990. }
  991. if (buff_size)
  992. kfree(buff_size);
  993. if (ioc)
  994. kfree(ioc);
  995. return(status);
  996. }
  997. default:
  998. return -ENOTTY;
  999. }
  1000. }
  1001. /*
  1002. * revalidate_allvol is for online array config utilities. After a
  1003. * utility reconfigures the drives in the array, it can use this function
  1004. * (through an ioctl) to make the driver zap any previous disk structs for
  1005. * that controller and get new ones.
  1006. *
  1007. * Right now I'm using the getgeometry() function to do this, but this
  1008. * function should probably be finer grained and allow you to revalidate one
  1009. * particualar logical volume (instead of all of them on a particular
  1010. * controller).
  1011. */
  1012. static int revalidate_allvol(ctlr_info_t *host)
  1013. {
  1014. int ctlr = host->ctlr, i;
  1015. unsigned long flags;
  1016. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1017. if (host->usage_count > 1) {
  1018. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1019. printk(KERN_WARNING "cciss: Device busy for volume"
  1020. " revalidation (usage=%d)\n", host->usage_count);
  1021. return -EBUSY;
  1022. }
  1023. host->usage_count++;
  1024. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1025. for(i=0; i< NWD; i++) {
  1026. struct gendisk *disk = host->gendisk[i];
  1027. if (disk->flags & GENHD_FL_UP)
  1028. del_gendisk(disk);
  1029. }
  1030. /*
  1031. * Set the partition and block size structures for all volumes
  1032. * on this controller to zero. We will reread all of this data
  1033. */
  1034. memset(host->drv, 0, sizeof(drive_info_struct)
  1035. * CISS_MAX_LUN);
  1036. /*
  1037. * Tell the array controller not to give us any interrupts while
  1038. * we check the new geometry. Then turn interrupts back on when
  1039. * we're done.
  1040. */
  1041. host->access.set_intr_mask(host, CCISS_INTR_OFF);
  1042. cciss_getgeometry(ctlr);
  1043. host->access.set_intr_mask(host, CCISS_INTR_ON);
  1044. /* Loop through each real device */
  1045. for (i = 0; i < NWD; i++) {
  1046. struct gendisk *disk = host->gendisk[i];
  1047. drive_info_struct *drv = &(host->drv[i]);
  1048. /* we must register the controller even if no disks exist */
  1049. /* this is for the online array utilities */
  1050. if (!drv->heads && i)
  1051. continue;
  1052. blk_queue_hardsect_size(drv->queue, drv->block_size);
  1053. set_capacity(disk, drv->nr_blocks);
  1054. add_disk(disk);
  1055. }
  1056. host->usage_count--;
  1057. return 0;
  1058. }
  1059. /* This function will check the usage_count of the drive to be updated/added.
  1060. * If the usage_count is zero then the drive information will be updated and
  1061. * the disk will be re-registered with the kernel. If not then it will be
  1062. * left alone for the next reboot. The exception to this is disk 0 which
  1063. * will always be left registered with the kernel since it is also the
  1064. * controller node. Any changes to disk 0 will show up on the next
  1065. * reboot.
  1066. */
  1067. static void cciss_update_drive_info(int ctlr, int drv_index)
  1068. {
  1069. ctlr_info_t *h = hba[ctlr];
  1070. struct gendisk *disk;
  1071. ReadCapdata_struct *size_buff = NULL;
  1072. InquiryData_struct *inq_buff = NULL;
  1073. unsigned int block_size;
  1074. unsigned int total_size;
  1075. unsigned long flags = 0;
  1076. int ret = 0;
  1077. /* if the disk already exists then deregister it before proceeding*/
  1078. if (h->drv[drv_index].raid_level != -1){
  1079. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  1080. h->drv[drv_index].busy_configuring = 1;
  1081. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1082. ret = deregister_disk(h->gendisk[drv_index],
  1083. &h->drv[drv_index], 0);
  1084. h->drv[drv_index].busy_configuring = 0;
  1085. }
  1086. /* If the disk is in use return */
  1087. if (ret)
  1088. return;
  1089. /* Get information about the disk and modify the driver sturcture */
  1090. size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
  1091. if (size_buff == NULL)
  1092. goto mem_msg;
  1093. inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
  1094. if (inq_buff == NULL)
  1095. goto mem_msg;
  1096. cciss_read_capacity(ctlr, drv_index, size_buff, 1,
  1097. &total_size, &block_size);
  1098. cciss_geometry_inquiry(ctlr, drv_index, 1, total_size, block_size,
  1099. inq_buff, &h->drv[drv_index]);
  1100. ++h->num_luns;
  1101. disk = h->gendisk[drv_index];
  1102. set_capacity(disk, h->drv[drv_index].nr_blocks);
  1103. /* if it's the controller it's already added */
  1104. if (drv_index){
  1105. disk->queue = blk_init_queue(do_cciss_request, &h->lock);
  1106. /* Set up queue information */
  1107. disk->queue->backing_dev_info.ra_pages = READ_AHEAD;
  1108. blk_queue_bounce_limit(disk->queue, hba[ctlr]->pdev->dma_mask);
  1109. /* This is a hardware imposed limit. */
  1110. blk_queue_max_hw_segments(disk->queue, MAXSGENTRIES);
  1111. /* This is a limit in the driver and could be eliminated. */
  1112. blk_queue_max_phys_segments(disk->queue, MAXSGENTRIES);
  1113. blk_queue_max_sectors(disk->queue, 512);
  1114. disk->queue->queuedata = hba[ctlr];
  1115. blk_queue_hardsect_size(disk->queue,
  1116. hba[ctlr]->drv[drv_index].block_size);
  1117. h->drv[drv_index].queue = disk->queue;
  1118. add_disk(disk);
  1119. }
  1120. freeret:
  1121. kfree(size_buff);
  1122. kfree(inq_buff);
  1123. return;
  1124. mem_msg:
  1125. printk(KERN_ERR "cciss: out of memory\n");
  1126. goto freeret;
  1127. }
  1128. /* This function will find the first index of the controllers drive array
  1129. * that has a -1 for the raid_level and will return that index. This is
  1130. * where new drives will be added. If the index to be returned is greater
  1131. * than the highest_lun index for the controller then highest_lun is set
  1132. * to this new index. If there are no available indexes then -1 is returned.
  1133. */
  1134. static int cciss_find_free_drive_index(int ctlr)
  1135. {
  1136. int i;
  1137. for (i=0; i < CISS_MAX_LUN; i++){
  1138. if (hba[ctlr]->drv[i].raid_level == -1){
  1139. if (i > hba[ctlr]->highest_lun)
  1140. hba[ctlr]->highest_lun = i;
  1141. return i;
  1142. }
  1143. }
  1144. return -1;
  1145. }
  1146. /* This function will add and remove logical drives from the Logical
  1147. * drive array of the controller and maintain persistancy of ordering
  1148. * so that mount points are preserved until the next reboot. This allows
  1149. * for the removal of logical drives in the middle of the drive array
  1150. * without a re-ordering of those drives.
  1151. * INPUT
  1152. * h = The controller to perform the operations on
  1153. * del_disk = The disk to remove if specified. If the value given
  1154. * is NULL then no disk is removed.
  1155. */
  1156. static int rebuild_lun_table(ctlr_info_t *h, struct gendisk *del_disk)
  1157. {
  1158. int ctlr = h->ctlr;
  1159. int num_luns;
  1160. ReportLunData_struct *ld_buff = NULL;
  1161. drive_info_struct *drv = NULL;
  1162. int return_code;
  1163. int listlength = 0;
  1164. int i;
  1165. int drv_found;
  1166. int drv_index = 0;
  1167. __u32 lunid = 0;
  1168. unsigned long flags;
  1169. /* Set busy_configuring flag for this operation */
  1170. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  1171. if (h->num_luns >= CISS_MAX_LUN){
  1172. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1173. return -EINVAL;
  1174. }
  1175. if (h->busy_configuring){
  1176. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1177. return -EBUSY;
  1178. }
  1179. h->busy_configuring = 1;
  1180. /* if del_disk is NULL then we are being called to add a new disk
  1181. * and update the logical drive table. If it is not NULL then
  1182. * we will check if the disk is in use or not.
  1183. */
  1184. if (del_disk != NULL){
  1185. drv = get_drv(del_disk);
  1186. drv->busy_configuring = 1;
  1187. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1188. return_code = deregister_disk(del_disk, drv, 1);
  1189. drv->busy_configuring = 0;
  1190. h->busy_configuring = 0;
  1191. return return_code;
  1192. } else {
  1193. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  1194. if (!capable(CAP_SYS_RAWIO))
  1195. return -EPERM;
  1196. ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
  1197. if (ld_buff == NULL)
  1198. goto mem_msg;
  1199. return_code = sendcmd_withirq(CISS_REPORT_LOG, ctlr, ld_buff,
  1200. sizeof(ReportLunData_struct), 0, 0, 0,
  1201. TYPE_CMD);
  1202. if (return_code == IO_OK){
  1203. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
  1204. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
  1205. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
  1206. listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
  1207. } else{ /* reading number of logical volumes failed */
  1208. printk(KERN_WARNING "cciss: report logical volume"
  1209. " command failed\n");
  1210. listlength = 0;
  1211. goto freeret;
  1212. }
  1213. num_luns = listlength / 8; /* 8 bytes per entry */
  1214. if (num_luns > CISS_MAX_LUN){
  1215. num_luns = CISS_MAX_LUN;
  1216. printk(KERN_WARNING "cciss: more luns configured"
  1217. " on controller than can be handled by"
  1218. " this driver.\n");
  1219. }
  1220. /* Compare controller drive array to drivers drive array.
  1221. * Check for updates in the drive information and any new drives
  1222. * on the controller.
  1223. */
  1224. for (i=0; i < num_luns; i++){
  1225. int j;
  1226. drv_found = 0;
  1227. lunid = (0xff &
  1228. (unsigned int)(ld_buff->LUN[i][3])) << 24;
  1229. lunid |= (0xff &
  1230. (unsigned int)(ld_buff->LUN[i][2])) << 16;
  1231. lunid |= (0xff &
  1232. (unsigned int)(ld_buff->LUN[i][1])) << 8;
  1233. lunid |= 0xff &
  1234. (unsigned int)(ld_buff->LUN[i][0]);
  1235. /* Find if the LUN is already in the drive array
  1236. * of the controller. If so then update its info
  1237. * if not is use. If it does not exist then find
  1238. * the first free index and add it.
  1239. */
  1240. for (j=0; j <= h->highest_lun; j++){
  1241. if (h->drv[j].LunID == lunid){
  1242. drv_index = j;
  1243. drv_found = 1;
  1244. }
  1245. }
  1246. /* check if the drive was found already in the array */
  1247. if (!drv_found){
  1248. drv_index = cciss_find_free_drive_index(ctlr);
  1249. if (drv_index == -1)
  1250. goto freeret;
  1251. }
  1252. h->drv[drv_index].LunID = lunid;
  1253. cciss_update_drive_info(ctlr, drv_index);
  1254. } /* end for */
  1255. } /* end else */
  1256. freeret:
  1257. kfree(ld_buff);
  1258. h->busy_configuring = 0;
  1259. /* We return -1 here to tell the ACU that we have registered/updated
  1260. * all of the drives that we can and to keep it from calling us
  1261. * additional times.
  1262. */
  1263. return -1;
  1264. mem_msg:
  1265. printk(KERN_ERR "cciss: out of memory\n");
  1266. goto freeret;
  1267. }
  1268. /* This function will deregister the disk and it's queue from the
  1269. * kernel. It must be called with the controller lock held and the
  1270. * drv structures busy_configuring flag set. It's parameters are:
  1271. *
  1272. * disk = This is the disk to be deregistered
  1273. * drv = This is the drive_info_struct associated with the disk to be
  1274. * deregistered. It contains information about the disk used
  1275. * by the driver.
  1276. * clear_all = This flag determines whether or not the disk information
  1277. * is going to be completely cleared out and the highest_lun
  1278. * reset. Sometimes we want to clear out information about
  1279. * the disk in preperation for re-adding it. In this case
  1280. * the highest_lun should be left unchanged and the LunID
  1281. * should not be cleared.
  1282. */
  1283. static int deregister_disk(struct gendisk *disk, drive_info_struct *drv,
  1284. int clear_all)
  1285. {
  1286. ctlr_info_t *h = get_host(disk);
  1287. if (!capable(CAP_SYS_RAWIO))
  1288. return -EPERM;
  1289. /* make sure logical volume is NOT is use */
  1290. if(clear_all || (h->gendisk[0] == disk)) {
  1291. if (drv->usage_count > 1)
  1292. return -EBUSY;
  1293. }
  1294. else
  1295. if( drv->usage_count > 0 )
  1296. return -EBUSY;
  1297. /* invalidate the devices and deregister the disk. If it is disk
  1298. * zero do not deregister it but just zero out it's values. This
  1299. * allows us to delete disk zero but keep the controller registered.
  1300. */
  1301. if (h->gendisk[0] != disk){
  1302. if (disk->flags & GENHD_FL_UP){
  1303. blk_cleanup_queue(disk->queue);
  1304. del_gendisk(disk);
  1305. drv->queue = NULL;
  1306. }
  1307. }
  1308. --h->num_luns;
  1309. /* zero out the disk size info */
  1310. drv->nr_blocks = 0;
  1311. drv->block_size = 0;
  1312. drv->heads = 0;
  1313. drv->sectors = 0;
  1314. drv->cylinders = 0;
  1315. drv->raid_level = -1; /* This can be used as a flag variable to
  1316. * indicate that this element of the drive
  1317. * array is free.
  1318. */
  1319. if (clear_all){
  1320. /* check to see if it was the last disk */
  1321. if (drv == h->drv + h->highest_lun) {
  1322. /* if so, find the new hightest lun */
  1323. int i, newhighest =-1;
  1324. for(i=0; i<h->highest_lun; i++) {
  1325. /* if the disk has size > 0, it is available */
  1326. if (h->drv[i].heads)
  1327. newhighest = i;
  1328. }
  1329. h->highest_lun = newhighest;
  1330. }
  1331. drv->LunID = 0;
  1332. }
  1333. return(0);
  1334. }
  1335. static int fill_cmd(CommandList_struct *c, __u8 cmd, int ctlr, void *buff,
  1336. size_t size,
  1337. unsigned int use_unit_num, /* 0: address the controller,
  1338. 1: address logical volume log_unit,
  1339. 2: periph device address is scsi3addr */
  1340. unsigned int log_unit, __u8 page_code, unsigned char *scsi3addr,
  1341. int cmd_type)
  1342. {
  1343. ctlr_info_t *h= hba[ctlr];
  1344. u64bit buff_dma_handle;
  1345. int status = IO_OK;
  1346. c->cmd_type = CMD_IOCTL_PEND;
  1347. c->Header.ReplyQueue = 0;
  1348. if( buff != NULL) {
  1349. c->Header.SGList = 1;
  1350. c->Header.SGTotal= 1;
  1351. } else {
  1352. c->Header.SGList = 0;
  1353. c->Header.SGTotal= 0;
  1354. }
  1355. c->Header.Tag.lower = c->busaddr;
  1356. c->Request.Type.Type = cmd_type;
  1357. if (cmd_type == TYPE_CMD) {
  1358. switch(cmd) {
  1359. case CISS_INQUIRY:
  1360. /* If the logical unit number is 0 then, this is going
  1361. to controller so It's a physical command
  1362. mode = 0 target = 0. So we have nothing to write.
  1363. otherwise, if use_unit_num == 1,
  1364. mode = 1(volume set addressing) target = LUNID
  1365. otherwise, if use_unit_num == 2,
  1366. mode = 0(periph dev addr) target = scsi3addr */
  1367. if (use_unit_num == 1) {
  1368. c->Header.LUN.LogDev.VolId=
  1369. h->drv[log_unit].LunID;
  1370. c->Header.LUN.LogDev.Mode = 1;
  1371. } else if (use_unit_num == 2) {
  1372. memcpy(c->Header.LUN.LunAddrBytes,scsi3addr,8);
  1373. c->Header.LUN.LogDev.Mode = 0;
  1374. }
  1375. /* are we trying to read a vital product page */
  1376. if(page_code != 0) {
  1377. c->Request.CDB[1] = 0x01;
  1378. c->Request.CDB[2] = page_code;
  1379. }
  1380. c->Request.CDBLen = 6;
  1381. c->Request.Type.Attribute = ATTR_SIMPLE;
  1382. c->Request.Type.Direction = XFER_READ;
  1383. c->Request.Timeout = 0;
  1384. c->Request.CDB[0] = CISS_INQUIRY;
  1385. c->Request.CDB[4] = size & 0xFF;
  1386. break;
  1387. case CISS_REPORT_LOG:
  1388. case CISS_REPORT_PHYS:
  1389. /* Talking to controller so It's a physical command
  1390. mode = 00 target = 0. Nothing to write.
  1391. */
  1392. c->Request.CDBLen = 12;
  1393. c->Request.Type.Attribute = ATTR_SIMPLE;
  1394. c->Request.Type.Direction = XFER_READ;
  1395. c->Request.Timeout = 0;
  1396. c->Request.CDB[0] = cmd;
  1397. c->Request.CDB[6] = (size >> 24) & 0xFF; //MSB
  1398. c->Request.CDB[7] = (size >> 16) & 0xFF;
  1399. c->Request.CDB[8] = (size >> 8) & 0xFF;
  1400. c->Request.CDB[9] = size & 0xFF;
  1401. break;
  1402. case CCISS_READ_CAPACITY:
  1403. c->Header.LUN.LogDev.VolId = h->drv[log_unit].LunID;
  1404. c->Header.LUN.LogDev.Mode = 1;
  1405. c->Request.CDBLen = 10;
  1406. c->Request.Type.Attribute = ATTR_SIMPLE;
  1407. c->Request.Type.Direction = XFER_READ;
  1408. c->Request.Timeout = 0;
  1409. c->Request.CDB[0] = cmd;
  1410. break;
  1411. case CCISS_CACHE_FLUSH:
  1412. c->Request.CDBLen = 12;
  1413. c->Request.Type.Attribute = ATTR_SIMPLE;
  1414. c->Request.Type.Direction = XFER_WRITE;
  1415. c->Request.Timeout = 0;
  1416. c->Request.CDB[0] = BMIC_WRITE;
  1417. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  1418. break;
  1419. default:
  1420. printk(KERN_WARNING
  1421. "cciss%d: Unknown Command 0x%c\n", ctlr, cmd);
  1422. return(IO_ERROR);
  1423. }
  1424. } else if (cmd_type == TYPE_MSG) {
  1425. switch (cmd) {
  1426. case 3: /* No-Op message */
  1427. c->Request.CDBLen = 1;
  1428. c->Request.Type.Attribute = ATTR_SIMPLE;
  1429. c->Request.Type.Direction = XFER_WRITE;
  1430. c->Request.Timeout = 0;
  1431. c->Request.CDB[0] = cmd;
  1432. break;
  1433. default:
  1434. printk(KERN_WARNING
  1435. "cciss%d: unknown message type %d\n",
  1436. ctlr, cmd);
  1437. return IO_ERROR;
  1438. }
  1439. } else {
  1440. printk(KERN_WARNING
  1441. "cciss%d: unknown command type %d\n", ctlr, cmd_type);
  1442. return IO_ERROR;
  1443. }
  1444. /* Fill in the scatter gather information */
  1445. if (size > 0) {
  1446. buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
  1447. buff, size, PCI_DMA_BIDIRECTIONAL);
  1448. c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
  1449. c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
  1450. c->SG[0].Len = size;
  1451. c->SG[0].Ext = 0; /* we are not chaining */
  1452. }
  1453. return status;
  1454. }
  1455. static int sendcmd_withirq(__u8 cmd,
  1456. int ctlr,
  1457. void *buff,
  1458. size_t size,
  1459. unsigned int use_unit_num,
  1460. unsigned int log_unit,
  1461. __u8 page_code,
  1462. int cmd_type)
  1463. {
  1464. ctlr_info_t *h = hba[ctlr];
  1465. CommandList_struct *c;
  1466. u64bit buff_dma_handle;
  1467. unsigned long flags;
  1468. int return_status;
  1469. DECLARE_COMPLETION(wait);
  1470. if ((c = cmd_alloc(h , 0)) == NULL)
  1471. return -ENOMEM;
  1472. return_status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
  1473. log_unit, page_code, NULL, cmd_type);
  1474. if (return_status != IO_OK) {
  1475. cmd_free(h, c, 0);
  1476. return return_status;
  1477. }
  1478. resend_cmd2:
  1479. c->waiting = &wait;
  1480. /* Put the request on the tail of the queue and send it */
  1481. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  1482. addQ(&h->reqQ, c);
  1483. h->Qdepth++;
  1484. start_io(h);
  1485. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  1486. wait_for_completion(&wait);
  1487. if(c->err_info->CommandStatus != 0)
  1488. { /* an error has occurred */
  1489. switch(c->err_info->CommandStatus)
  1490. {
  1491. case CMD_TARGET_STATUS:
  1492. printk(KERN_WARNING "cciss: cmd %p has "
  1493. " completed with errors\n", c);
  1494. if( c->err_info->ScsiStatus)
  1495. {
  1496. printk(KERN_WARNING "cciss: cmd %p "
  1497. "has SCSI Status = %x\n",
  1498. c,
  1499. c->err_info->ScsiStatus);
  1500. }
  1501. break;
  1502. case CMD_DATA_UNDERRUN:
  1503. case CMD_DATA_OVERRUN:
  1504. /* expected for inquire and report lun commands */
  1505. break;
  1506. case CMD_INVALID:
  1507. printk(KERN_WARNING "cciss: Cmd %p is "
  1508. "reported invalid\n", c);
  1509. return_status = IO_ERROR;
  1510. break;
  1511. case CMD_PROTOCOL_ERR:
  1512. printk(KERN_WARNING "cciss: cmd %p has "
  1513. "protocol error \n", c);
  1514. return_status = IO_ERROR;
  1515. break;
  1516. case CMD_HARDWARE_ERR:
  1517. printk(KERN_WARNING "cciss: cmd %p had "
  1518. " hardware error\n", c);
  1519. return_status = IO_ERROR;
  1520. break;
  1521. case CMD_CONNECTION_LOST:
  1522. printk(KERN_WARNING "cciss: cmd %p had "
  1523. "connection lost\n", c);
  1524. return_status = IO_ERROR;
  1525. break;
  1526. case CMD_ABORTED:
  1527. printk(KERN_WARNING "cciss: cmd %p was "
  1528. "aborted\n", c);
  1529. return_status = IO_ERROR;
  1530. break;
  1531. case CMD_ABORT_FAILED:
  1532. printk(KERN_WARNING "cciss: cmd %p reports "
  1533. "abort failed\n", c);
  1534. return_status = IO_ERROR;
  1535. break;
  1536. case CMD_UNSOLICITED_ABORT:
  1537. printk(KERN_WARNING
  1538. "cciss%d: unsolicited abort %p\n",
  1539. ctlr, c);
  1540. if (c->retry_count < MAX_CMD_RETRIES) {
  1541. printk(KERN_WARNING
  1542. "cciss%d: retrying %p\n",
  1543. ctlr, c);
  1544. c->retry_count++;
  1545. /* erase the old error information */
  1546. memset(c->err_info, 0,
  1547. sizeof(ErrorInfo_struct));
  1548. return_status = IO_OK;
  1549. INIT_COMPLETION(wait);
  1550. goto resend_cmd2;
  1551. }
  1552. return_status = IO_ERROR;
  1553. break;
  1554. default:
  1555. printk(KERN_WARNING "cciss: cmd %p returned "
  1556. "unknown status %x\n", c,
  1557. c->err_info->CommandStatus);
  1558. return_status = IO_ERROR;
  1559. }
  1560. }
  1561. /* unlock the buffers from DMA */
  1562. buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
  1563. buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
  1564. pci_unmap_single( h->pdev, (dma_addr_t) buff_dma_handle.val,
  1565. c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
  1566. cmd_free(h, c, 0);
  1567. return(return_status);
  1568. }
  1569. static void cciss_geometry_inquiry(int ctlr, int logvol,
  1570. int withirq, unsigned int total_size,
  1571. unsigned int block_size, InquiryData_struct *inq_buff,
  1572. drive_info_struct *drv)
  1573. {
  1574. int return_code;
  1575. memset(inq_buff, 0, sizeof(InquiryData_struct));
  1576. if (withirq)
  1577. return_code = sendcmd_withirq(CISS_INQUIRY, ctlr,
  1578. inq_buff, sizeof(*inq_buff), 1, logvol ,0xC1, TYPE_CMD);
  1579. else
  1580. return_code = sendcmd(CISS_INQUIRY, ctlr, inq_buff,
  1581. sizeof(*inq_buff), 1, logvol ,0xC1, NULL, TYPE_CMD);
  1582. if (return_code == IO_OK) {
  1583. if(inq_buff->data_byte[8] == 0xFF) {
  1584. printk(KERN_WARNING
  1585. "cciss: reading geometry failed, volume "
  1586. "does not support reading geometry\n");
  1587. drv->block_size = block_size;
  1588. drv->nr_blocks = total_size;
  1589. drv->heads = 255;
  1590. drv->sectors = 32; // Sectors per track
  1591. drv->cylinders = total_size / 255 / 32;
  1592. } else {
  1593. unsigned int t;
  1594. drv->block_size = block_size;
  1595. drv->nr_blocks = total_size;
  1596. drv->heads = inq_buff->data_byte[6];
  1597. drv->sectors = inq_buff->data_byte[7];
  1598. drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
  1599. drv->cylinders += inq_buff->data_byte[5];
  1600. drv->raid_level = inq_buff->data_byte[8];
  1601. t = drv->heads * drv->sectors;
  1602. if (t > 1) {
  1603. drv->cylinders = total_size/t;
  1604. }
  1605. }
  1606. } else { /* Get geometry failed */
  1607. printk(KERN_WARNING "cciss: reading geometry failed\n");
  1608. }
  1609. printk(KERN_INFO " heads= %d, sectors= %d, cylinders= %d\n\n",
  1610. drv->heads, drv->sectors, drv->cylinders);
  1611. }
  1612. static void
  1613. cciss_read_capacity(int ctlr, int logvol, ReadCapdata_struct *buf,
  1614. int withirq, unsigned int *total_size, unsigned int *block_size)
  1615. {
  1616. int return_code;
  1617. memset(buf, 0, sizeof(*buf));
  1618. if (withirq)
  1619. return_code = sendcmd_withirq(CCISS_READ_CAPACITY,
  1620. ctlr, buf, sizeof(*buf), 1, logvol, 0, TYPE_CMD);
  1621. else
  1622. return_code = sendcmd(CCISS_READ_CAPACITY,
  1623. ctlr, buf, sizeof(*buf), 1, logvol, 0, NULL, TYPE_CMD);
  1624. if (return_code == IO_OK) {
  1625. *total_size = be32_to_cpu(*((__be32 *) &buf->total_size[0]))+1;
  1626. *block_size = be32_to_cpu(*((__be32 *) &buf->block_size[0]));
  1627. } else { /* read capacity command failed */
  1628. printk(KERN_WARNING "cciss: read capacity failed\n");
  1629. *total_size = 0;
  1630. *block_size = BLOCK_SIZE;
  1631. }
  1632. printk(KERN_INFO " blocks= %u block_size= %d\n",
  1633. *total_size, *block_size);
  1634. return;
  1635. }
  1636. static int cciss_revalidate(struct gendisk *disk)
  1637. {
  1638. ctlr_info_t *h = get_host(disk);
  1639. drive_info_struct *drv = get_drv(disk);
  1640. int logvol;
  1641. int FOUND=0;
  1642. unsigned int block_size;
  1643. unsigned int total_size;
  1644. ReadCapdata_struct *size_buff = NULL;
  1645. InquiryData_struct *inq_buff = NULL;
  1646. for(logvol=0; logvol < CISS_MAX_LUN; logvol++)
  1647. {
  1648. if(h->drv[logvol].LunID == drv->LunID) {
  1649. FOUND=1;
  1650. break;
  1651. }
  1652. }
  1653. if (!FOUND) return 1;
  1654. size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
  1655. if (size_buff == NULL)
  1656. {
  1657. printk(KERN_WARNING "cciss: out of memory\n");
  1658. return 1;
  1659. }
  1660. inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
  1661. if (inq_buff == NULL)
  1662. {
  1663. printk(KERN_WARNING "cciss: out of memory\n");
  1664. kfree(size_buff);
  1665. return 1;
  1666. }
  1667. cciss_read_capacity(h->ctlr, logvol, size_buff, 1, &total_size, &block_size);
  1668. cciss_geometry_inquiry(h->ctlr, logvol, 1, total_size, block_size, inq_buff, drv);
  1669. blk_queue_hardsect_size(drv->queue, drv->block_size);
  1670. set_capacity(disk, drv->nr_blocks);
  1671. kfree(size_buff);
  1672. kfree(inq_buff);
  1673. return 0;
  1674. }
  1675. /*
  1676. * Wait polling for a command to complete.
  1677. * The memory mapped FIFO is polled for the completion.
  1678. * Used only at init time, interrupts from the HBA are disabled.
  1679. */
  1680. static unsigned long pollcomplete(int ctlr)
  1681. {
  1682. unsigned long done;
  1683. int i;
  1684. /* Wait (up to 20 seconds) for a command to complete */
  1685. for (i = 20 * HZ; i > 0; i--) {
  1686. done = hba[ctlr]->access.command_completed(hba[ctlr]);
  1687. if (done == FIFO_EMPTY)
  1688. schedule_timeout_uninterruptible(1);
  1689. else
  1690. return (done);
  1691. }
  1692. /* Invalid address to tell caller we ran out of time */
  1693. return 1;
  1694. }
  1695. /*
  1696. * Send a command to the controller, and wait for it to complete.
  1697. * Only used at init time.
  1698. */
  1699. static int sendcmd(
  1700. __u8 cmd,
  1701. int ctlr,
  1702. void *buff,
  1703. size_t size,
  1704. unsigned int use_unit_num, /* 0: address the controller,
  1705. 1: address logical volume log_unit,
  1706. 2: periph device address is scsi3addr */
  1707. unsigned int log_unit,
  1708. __u8 page_code,
  1709. unsigned char *scsi3addr,
  1710. int cmd_type)
  1711. {
  1712. CommandList_struct *c;
  1713. int i;
  1714. unsigned long complete;
  1715. ctlr_info_t *info_p= hba[ctlr];
  1716. u64bit buff_dma_handle;
  1717. int status;
  1718. if ((c = cmd_alloc(info_p, 1)) == NULL) {
  1719. printk(KERN_WARNING "cciss: unable to get memory");
  1720. return(IO_ERROR);
  1721. }
  1722. status = fill_cmd(c, cmd, ctlr, buff, size, use_unit_num,
  1723. log_unit, page_code, scsi3addr, cmd_type);
  1724. if (status != IO_OK) {
  1725. cmd_free(info_p, c, 1);
  1726. return status;
  1727. }
  1728. resend_cmd1:
  1729. /*
  1730. * Disable interrupt
  1731. */
  1732. #ifdef CCISS_DEBUG
  1733. printk(KERN_DEBUG "cciss: turning intr off\n");
  1734. #endif /* CCISS_DEBUG */
  1735. info_p->access.set_intr_mask(info_p, CCISS_INTR_OFF);
  1736. /* Make sure there is room in the command FIFO */
  1737. /* Actually it should be completely empty at this time. */
  1738. for (i = 200000; i > 0; i--)
  1739. {
  1740. /* if fifo isn't full go */
  1741. if (!(info_p->access.fifo_full(info_p)))
  1742. {
  1743. break;
  1744. }
  1745. udelay(10);
  1746. printk(KERN_WARNING "cciss cciss%d: SendCmd FIFO full,"
  1747. " waiting!\n", ctlr);
  1748. }
  1749. /*
  1750. * Send the cmd
  1751. */
  1752. info_p->access.submit_command(info_p, c);
  1753. complete = pollcomplete(ctlr);
  1754. #ifdef CCISS_DEBUG
  1755. printk(KERN_DEBUG "cciss: command completed\n");
  1756. #endif /* CCISS_DEBUG */
  1757. if (complete != 1) {
  1758. if ( (complete & CISS_ERROR_BIT)
  1759. && (complete & ~CISS_ERROR_BIT) == c->busaddr)
  1760. {
  1761. /* if data overrun or underun on Report command
  1762. ignore it
  1763. */
  1764. if (((c->Request.CDB[0] == CISS_REPORT_LOG) ||
  1765. (c->Request.CDB[0] == CISS_REPORT_PHYS) ||
  1766. (c->Request.CDB[0] == CISS_INQUIRY)) &&
  1767. ((c->err_info->CommandStatus ==
  1768. CMD_DATA_OVERRUN) ||
  1769. (c->err_info->CommandStatus ==
  1770. CMD_DATA_UNDERRUN)
  1771. ))
  1772. {
  1773. complete = c->busaddr;
  1774. } else {
  1775. if (c->err_info->CommandStatus ==
  1776. CMD_UNSOLICITED_ABORT) {
  1777. printk(KERN_WARNING "cciss%d: "
  1778. "unsolicited abort %p\n",
  1779. ctlr, c);
  1780. if (c->retry_count < MAX_CMD_RETRIES) {
  1781. printk(KERN_WARNING
  1782. "cciss%d: retrying %p\n",
  1783. ctlr, c);
  1784. c->retry_count++;
  1785. /* erase the old error */
  1786. /* information */
  1787. memset(c->err_info, 0,
  1788. sizeof(ErrorInfo_struct));
  1789. goto resend_cmd1;
  1790. } else {
  1791. printk(KERN_WARNING
  1792. "cciss%d: retried %p too "
  1793. "many times\n", ctlr, c);
  1794. status = IO_ERROR;
  1795. goto cleanup1;
  1796. }
  1797. }
  1798. printk(KERN_WARNING "ciss ciss%d: sendcmd"
  1799. " Error %x \n", ctlr,
  1800. c->err_info->CommandStatus);
  1801. printk(KERN_WARNING "ciss ciss%d: sendcmd"
  1802. " offensive info\n"
  1803. " size %x\n num %x value %x\n", ctlr,
  1804. c->err_info->MoreErrInfo.Invalid_Cmd.offense_size,
  1805. c->err_info->MoreErrInfo.Invalid_Cmd.offense_num,
  1806. c->err_info->MoreErrInfo.Invalid_Cmd.offense_value);
  1807. status = IO_ERROR;
  1808. goto cleanup1;
  1809. }
  1810. }
  1811. if (complete != c->busaddr) {
  1812. printk( KERN_WARNING "cciss cciss%d: SendCmd "
  1813. "Invalid command list address returned! (%lx)\n",
  1814. ctlr, complete);
  1815. status = IO_ERROR;
  1816. goto cleanup1;
  1817. }
  1818. } else {
  1819. printk( KERN_WARNING
  1820. "cciss cciss%d: SendCmd Timeout out, "
  1821. "No command list address returned!\n",
  1822. ctlr);
  1823. status = IO_ERROR;
  1824. }
  1825. cleanup1:
  1826. /* unlock the data buffer from DMA */
  1827. buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
  1828. buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
  1829. pci_unmap_single(info_p->pdev, (dma_addr_t) buff_dma_handle.val,
  1830. c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
  1831. cmd_free(info_p, c, 1);
  1832. return (status);
  1833. }
  1834. /*
  1835. * Map (physical) PCI mem into (virtual) kernel space
  1836. */
  1837. static void __iomem *remap_pci_mem(ulong base, ulong size)
  1838. {
  1839. ulong page_base = ((ulong) base) & PAGE_MASK;
  1840. ulong page_offs = ((ulong) base) - page_base;
  1841. void __iomem *page_remapped = ioremap(page_base, page_offs+size);
  1842. return page_remapped ? (page_remapped + page_offs) : NULL;
  1843. }
  1844. /*
  1845. * Takes jobs of the Q and sends them to the hardware, then puts it on
  1846. * the Q to wait for completion.
  1847. */
  1848. static void start_io( ctlr_info_t *h)
  1849. {
  1850. CommandList_struct *c;
  1851. while(( c = h->reqQ) != NULL )
  1852. {
  1853. /* can't do anything if fifo is full */
  1854. if ((h->access.fifo_full(h))) {
  1855. printk(KERN_WARNING "cciss: fifo full\n");
  1856. break;
  1857. }
  1858. /* Get the frist entry from the Request Q */
  1859. removeQ(&(h->reqQ), c);
  1860. h->Qdepth--;
  1861. /* Tell the controller execute command */
  1862. h->access.submit_command(h, c);
  1863. /* Put job onto the completed Q */
  1864. addQ (&(h->cmpQ), c);
  1865. }
  1866. }
  1867. static inline void complete_buffers(struct bio *bio, int status)
  1868. {
  1869. while (bio) {
  1870. struct bio *xbh = bio->bi_next;
  1871. int nr_sectors = bio_sectors(bio);
  1872. bio->bi_next = NULL;
  1873. blk_finished_io(len);
  1874. bio_endio(bio, nr_sectors << 9, status ? 0 : -EIO);
  1875. bio = xbh;
  1876. }
  1877. }
  1878. /* Assumes that CCISS_LOCK(h->ctlr) is held. */
  1879. /* Zeros out the error record and then resends the command back */
  1880. /* to the controller */
  1881. static inline void resend_cciss_cmd( ctlr_info_t *h, CommandList_struct *c)
  1882. {
  1883. /* erase the old error information */
  1884. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  1885. /* add it to software queue and then send it to the controller */
  1886. addQ(&(h->reqQ),c);
  1887. h->Qdepth++;
  1888. if(h->Qdepth > h->maxQsinceinit)
  1889. h->maxQsinceinit = h->Qdepth;
  1890. start_io(h);
  1891. }
  1892. /* checks the status of the job and calls complete buffers to mark all
  1893. * buffers for the completed job.
  1894. */
  1895. static inline void complete_command( ctlr_info_t *h, CommandList_struct *cmd,
  1896. int timeout)
  1897. {
  1898. int status = 1;
  1899. int i;
  1900. int retry_cmd = 0;
  1901. u64bit temp64;
  1902. if (timeout)
  1903. status = 0;
  1904. if(cmd->err_info->CommandStatus != 0)
  1905. { /* an error has occurred */
  1906. switch(cmd->err_info->CommandStatus)
  1907. {
  1908. unsigned char sense_key;
  1909. case CMD_TARGET_STATUS:
  1910. status = 0;
  1911. if( cmd->err_info->ScsiStatus == 0x02)
  1912. {
  1913. printk(KERN_WARNING "cciss: cmd %p "
  1914. "has CHECK CONDITION "
  1915. " byte 2 = 0x%x\n", cmd,
  1916. cmd->err_info->SenseInfo[2]
  1917. );
  1918. /* check the sense key */
  1919. sense_key = 0xf &
  1920. cmd->err_info->SenseInfo[2];
  1921. /* no status or recovered error */
  1922. if((sense_key == 0x0) ||
  1923. (sense_key == 0x1))
  1924. {
  1925. status = 1;
  1926. }
  1927. } else
  1928. {
  1929. printk(KERN_WARNING "cciss: cmd %p "
  1930. "has SCSI Status 0x%x\n",
  1931. cmd, cmd->err_info->ScsiStatus);
  1932. }
  1933. break;
  1934. case CMD_DATA_UNDERRUN:
  1935. printk(KERN_WARNING "cciss: cmd %p has"
  1936. " completed with data underrun "
  1937. "reported\n", cmd);
  1938. break;
  1939. case CMD_DATA_OVERRUN:
  1940. printk(KERN_WARNING "cciss: cmd %p has"
  1941. " completed with data overrun "
  1942. "reported\n", cmd);
  1943. break;
  1944. case CMD_INVALID:
  1945. printk(KERN_WARNING "cciss: cmd %p is "
  1946. "reported invalid\n", cmd);
  1947. status = 0;
  1948. break;
  1949. case CMD_PROTOCOL_ERR:
  1950. printk(KERN_WARNING "cciss: cmd %p has "
  1951. "protocol error \n", cmd);
  1952. status = 0;
  1953. break;
  1954. case CMD_HARDWARE_ERR:
  1955. printk(KERN_WARNING "cciss: cmd %p had "
  1956. " hardware error\n", cmd);
  1957. status = 0;
  1958. break;
  1959. case CMD_CONNECTION_LOST:
  1960. printk(KERN_WARNING "cciss: cmd %p had "
  1961. "connection lost\n", cmd);
  1962. status=0;
  1963. break;
  1964. case CMD_ABORTED:
  1965. printk(KERN_WARNING "cciss: cmd %p was "
  1966. "aborted\n", cmd);
  1967. status=0;
  1968. break;
  1969. case CMD_ABORT_FAILED:
  1970. printk(KERN_WARNING "cciss: cmd %p reports "
  1971. "abort failed\n", cmd);
  1972. status=0;
  1973. break;
  1974. case CMD_UNSOLICITED_ABORT:
  1975. printk(KERN_WARNING "cciss%d: unsolicited "
  1976. "abort %p\n", h->ctlr, cmd);
  1977. if (cmd->retry_count < MAX_CMD_RETRIES) {
  1978. retry_cmd=1;
  1979. printk(KERN_WARNING
  1980. "cciss%d: retrying %p\n",
  1981. h->ctlr, cmd);
  1982. cmd->retry_count++;
  1983. } else
  1984. printk(KERN_WARNING
  1985. "cciss%d: %p retried too "
  1986. "many times\n", h->ctlr, cmd);
  1987. status=0;
  1988. break;
  1989. case CMD_TIMEOUT:
  1990. printk(KERN_WARNING "cciss: cmd %p timedout\n",
  1991. cmd);
  1992. status=0;
  1993. break;
  1994. default:
  1995. printk(KERN_WARNING "cciss: cmd %p returned "
  1996. "unknown status %x\n", cmd,
  1997. cmd->err_info->CommandStatus);
  1998. status=0;
  1999. }
  2000. }
  2001. /* We need to return this command */
  2002. if(retry_cmd) {
  2003. resend_cciss_cmd(h,cmd);
  2004. return;
  2005. }
  2006. /* command did not need to be retried */
  2007. /* unmap the DMA mapping for all the scatter gather elements */
  2008. for(i=0; i<cmd->Header.SGList; i++) {
  2009. temp64.val32.lower = cmd->SG[i].Addr.lower;
  2010. temp64.val32.upper = cmd->SG[i].Addr.upper;
  2011. pci_unmap_page(hba[cmd->ctlr]->pdev,
  2012. temp64.val, cmd->SG[i].Len,
  2013. (cmd->Request.Type.Direction == XFER_READ) ?
  2014. PCI_DMA_FROMDEVICE : PCI_DMA_TODEVICE);
  2015. }
  2016. complete_buffers(cmd->rq->bio, status);
  2017. #ifdef CCISS_DEBUG
  2018. printk("Done with %p\n", cmd->rq);
  2019. #endif /* CCISS_DEBUG */
  2020. end_that_request_last(cmd->rq);
  2021. cmd_free(h,cmd,1);
  2022. }
  2023. /*
  2024. * Get a request and submit it to the controller.
  2025. */
  2026. static void do_cciss_request(request_queue_t *q)
  2027. {
  2028. ctlr_info_t *h= q->queuedata;
  2029. CommandList_struct *c;
  2030. int start_blk, seg;
  2031. struct request *creq;
  2032. u64bit temp64;
  2033. struct scatterlist tmp_sg[MAXSGENTRIES];
  2034. drive_info_struct *drv;
  2035. int i, dir;
  2036. /* We call start_io here in case there is a command waiting on the
  2037. * queue that has not been sent.
  2038. */
  2039. if (blk_queue_plugged(q))
  2040. goto startio;
  2041. queue:
  2042. creq = elv_next_request(q);
  2043. if (!creq)
  2044. goto startio;
  2045. if (creq->nr_phys_segments > MAXSGENTRIES)
  2046. BUG();
  2047. if (( c = cmd_alloc(h, 1)) == NULL)
  2048. goto full;
  2049. blkdev_dequeue_request(creq);
  2050. spin_unlock_irq(q->queue_lock);
  2051. c->cmd_type = CMD_RWREQ;
  2052. c->rq = creq;
  2053. /* fill in the request */
  2054. drv = creq->rq_disk->private_data;
  2055. c->Header.ReplyQueue = 0; // unused in simple mode
  2056. /* got command from pool, so use the command block index instead */
  2057. /* for direct lookups. */
  2058. /* The first 2 bits are reserved for controller error reporting. */
  2059. c->Header.Tag.lower = (c->cmdindex << 3);
  2060. c->Header.Tag.lower |= 0x04; /* flag for direct lookup. */
  2061. c->Header.LUN.LogDev.VolId= drv->LunID;
  2062. c->Header.LUN.LogDev.Mode = 1;
  2063. c->Request.CDBLen = 10; // 12 byte commands not in FW yet;
  2064. c->Request.Type.Type = TYPE_CMD; // It is a command.
  2065. c->Request.Type.Attribute = ATTR_SIMPLE;
  2066. c->Request.Type.Direction =
  2067. (rq_data_dir(creq) == READ) ? XFER_READ: XFER_WRITE;
  2068. c->Request.Timeout = 0; // Don't time out
  2069. c->Request.CDB[0] = (rq_data_dir(creq) == READ) ? CCISS_READ : CCISS_WRITE;
  2070. start_blk = creq->sector;
  2071. #ifdef CCISS_DEBUG
  2072. printk(KERN_DEBUG "ciss: sector =%d nr_sectors=%d\n",(int) creq->sector,
  2073. (int) creq->nr_sectors);
  2074. #endif /* CCISS_DEBUG */
  2075. seg = blk_rq_map_sg(q, creq, tmp_sg);
  2076. /* get the DMA records for the setup */
  2077. if (c->Request.Type.Direction == XFER_READ)
  2078. dir = PCI_DMA_FROMDEVICE;
  2079. else
  2080. dir = PCI_DMA_TODEVICE;
  2081. for (i=0; i<seg; i++)
  2082. {
  2083. c->SG[i].Len = tmp_sg[i].length;
  2084. temp64.val = (__u64) pci_map_page(h->pdev, tmp_sg[i].page,
  2085. tmp_sg[i].offset, tmp_sg[i].length,
  2086. dir);
  2087. c->SG[i].Addr.lower = temp64.val32.lower;
  2088. c->SG[i].Addr.upper = temp64.val32.upper;
  2089. c->SG[i].Ext = 0; // we are not chaining
  2090. }
  2091. /* track how many SG entries we are using */
  2092. if( seg > h->maxSG)
  2093. h->maxSG = seg;
  2094. #ifdef CCISS_DEBUG
  2095. printk(KERN_DEBUG "cciss: Submitting %d sectors in %d segments\n", creq->nr_sectors, seg);
  2096. #endif /* CCISS_DEBUG */
  2097. c->Header.SGList = c->Header.SGTotal = seg;
  2098. c->Request.CDB[1]= 0;
  2099. c->Request.CDB[2]= (start_blk >> 24) & 0xff; //MSB
  2100. c->Request.CDB[3]= (start_blk >> 16) & 0xff;
  2101. c->Request.CDB[4]= (start_blk >> 8) & 0xff;
  2102. c->Request.CDB[5]= start_blk & 0xff;
  2103. c->Request.CDB[6]= 0; // (sect >> 24) & 0xff; MSB
  2104. c->Request.CDB[7]= (creq->nr_sectors >> 8) & 0xff;
  2105. c->Request.CDB[8]= creq->nr_sectors & 0xff;
  2106. c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
  2107. spin_lock_irq(q->queue_lock);
  2108. addQ(&(h->reqQ),c);
  2109. h->Qdepth++;
  2110. if(h->Qdepth > h->maxQsinceinit)
  2111. h->maxQsinceinit = h->Qdepth;
  2112. goto queue;
  2113. full:
  2114. blk_stop_queue(q);
  2115. startio:
  2116. /* We will already have the driver lock here so not need
  2117. * to lock it.
  2118. */
  2119. start_io(h);
  2120. }
  2121. static irqreturn_t do_cciss_intr(int irq, void *dev_id, struct pt_regs *regs)
  2122. {
  2123. ctlr_info_t *h = dev_id;
  2124. CommandList_struct *c;
  2125. unsigned long flags;
  2126. __u32 a, a1, a2;
  2127. int j;
  2128. int start_queue = h->next_to_run;
  2129. /* Is this interrupt for us? */
  2130. if (( h->access.intr_pending(h) == 0) || (h->interrupts_enabled == 0))
  2131. return IRQ_NONE;
  2132. /*
  2133. * If there are completed commands in the completion queue,
  2134. * we had better do something about it.
  2135. */
  2136. spin_lock_irqsave(CCISS_LOCK(h->ctlr), flags);
  2137. while( h->access.intr_pending(h))
  2138. {
  2139. while((a = h->access.command_completed(h)) != FIFO_EMPTY)
  2140. {
  2141. a1 = a;
  2142. if ((a & 0x04)) {
  2143. a2 = (a >> 3);
  2144. if (a2 >= NR_CMDS) {
  2145. printk(KERN_WARNING "cciss: controller cciss%d failed, stopping.\n", h->ctlr);
  2146. fail_all_cmds(h->ctlr);
  2147. return IRQ_HANDLED;
  2148. }
  2149. c = h->cmd_pool + a2;
  2150. a = c->busaddr;
  2151. } else {
  2152. a &= ~3;
  2153. if ((c = h->cmpQ) == NULL) {
  2154. printk(KERN_WARNING "cciss: Completion of %08x ignored\n", a1);
  2155. continue;
  2156. }
  2157. while(c->busaddr != a) {
  2158. c = c->next;
  2159. if (c == h->cmpQ)
  2160. break;
  2161. }
  2162. }
  2163. /*
  2164. * If we've found the command, take it off the
  2165. * completion Q and free it
  2166. */
  2167. if (c->busaddr == a) {
  2168. removeQ(&h->cmpQ, c);
  2169. if (c->cmd_type == CMD_RWREQ) {
  2170. complete_command(h, c, 0);
  2171. } else if (c->cmd_type == CMD_IOCTL_PEND) {
  2172. complete(c->waiting);
  2173. }
  2174. # ifdef CONFIG_CISS_SCSI_TAPE
  2175. else if (c->cmd_type == CMD_SCSI)
  2176. complete_scsi_command(c, 0, a1);
  2177. # endif
  2178. continue;
  2179. }
  2180. }
  2181. }
  2182. /* check to see if we have maxed out the number of commands that can
  2183. * be placed on the queue. If so then exit. We do this check here
  2184. * in case the interrupt we serviced was from an ioctl and did not
  2185. * free any new commands.
  2186. */
  2187. if ((find_first_zero_bit(h->cmd_pool_bits, NR_CMDS)) == NR_CMDS)
  2188. goto cleanup;
  2189. /* We have room on the queue for more commands. Now we need to queue
  2190. * them up. We will also keep track of the next queue to run so
  2191. * that every queue gets a chance to be started first.
  2192. */
  2193. for (j=0; j < h->highest_lun + 1; j++){
  2194. int curr_queue = (start_queue + j) % (h->highest_lun + 1);
  2195. /* make sure the disk has been added and the drive is real
  2196. * because this can be called from the middle of init_one.
  2197. */
  2198. if(!(h->drv[curr_queue].queue) ||
  2199. !(h->drv[curr_queue].heads))
  2200. continue;
  2201. blk_start_queue(h->gendisk[curr_queue]->queue);
  2202. /* check to see if we have maxed out the number of commands
  2203. * that can be placed on the queue.
  2204. */
  2205. if ((find_first_zero_bit(h->cmd_pool_bits, NR_CMDS)) == NR_CMDS)
  2206. {
  2207. if (curr_queue == start_queue){
  2208. h->next_to_run = (start_queue + 1) % (h->highest_lun + 1);
  2209. goto cleanup;
  2210. } else {
  2211. h->next_to_run = curr_queue;
  2212. goto cleanup;
  2213. }
  2214. } else {
  2215. curr_queue = (curr_queue + 1) % (h->highest_lun + 1);
  2216. }
  2217. }
  2218. cleanup:
  2219. spin_unlock_irqrestore(CCISS_LOCK(h->ctlr), flags);
  2220. return IRQ_HANDLED;
  2221. }
  2222. /*
  2223. * We cannot read the structure directly, for portablity we must use
  2224. * the io functions.
  2225. * This is for debug only.
  2226. */
  2227. #ifdef CCISS_DEBUG
  2228. static void print_cfg_table( CfgTable_struct *tb)
  2229. {
  2230. int i;
  2231. char temp_name[17];
  2232. printk("Controller Configuration information\n");
  2233. printk("------------------------------------\n");
  2234. for(i=0;i<4;i++)
  2235. temp_name[i] = readb(&(tb->Signature[i]));
  2236. temp_name[4]='\0';
  2237. printk(" Signature = %s\n", temp_name);
  2238. printk(" Spec Number = %d\n", readl(&(tb->SpecValence)));
  2239. printk(" Transport methods supported = 0x%x\n",
  2240. readl(&(tb-> TransportSupport)));
  2241. printk(" Transport methods active = 0x%x\n",
  2242. readl(&(tb->TransportActive)));
  2243. printk(" Requested transport Method = 0x%x\n",
  2244. readl(&(tb->HostWrite.TransportRequest)));
  2245. printk(" Coalese Interrupt Delay = 0x%x\n",
  2246. readl(&(tb->HostWrite.CoalIntDelay)));
  2247. printk(" Coalese Interrupt Count = 0x%x\n",
  2248. readl(&(tb->HostWrite.CoalIntCount)));
  2249. printk(" Max outstanding commands = 0x%d\n",
  2250. readl(&(tb->CmdsOutMax)));
  2251. printk(" Bus Types = 0x%x\n", readl(&(tb-> BusTypes)));
  2252. for(i=0;i<16;i++)
  2253. temp_name[i] = readb(&(tb->ServerName[i]));
  2254. temp_name[16] = '\0';
  2255. printk(" Server Name = %s\n", temp_name);
  2256. printk(" Heartbeat Counter = 0x%x\n\n\n",
  2257. readl(&(tb->HeartBeat)));
  2258. }
  2259. #endif /* CCISS_DEBUG */
  2260. static void release_io_mem(ctlr_info_t *c)
  2261. {
  2262. /* if IO mem was not protected do nothing */
  2263. if( c->io_mem_addr == 0)
  2264. return;
  2265. release_region(c->io_mem_addr, c->io_mem_length);
  2266. c->io_mem_addr = 0;
  2267. c->io_mem_length = 0;
  2268. }
  2269. static int find_PCI_BAR_index(struct pci_dev *pdev,
  2270. unsigned long pci_bar_addr)
  2271. {
  2272. int i, offset, mem_type, bar_type;
  2273. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  2274. return 0;
  2275. offset = 0;
  2276. for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
  2277. bar_type = pci_resource_flags(pdev, i) &
  2278. PCI_BASE_ADDRESS_SPACE;
  2279. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  2280. offset += 4;
  2281. else {
  2282. mem_type = pci_resource_flags(pdev, i) &
  2283. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  2284. switch (mem_type) {
  2285. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  2286. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  2287. offset += 4; /* 32 bit */
  2288. break;
  2289. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  2290. offset += 8;
  2291. break;
  2292. default: /* reserved in PCI 2.2 */
  2293. printk(KERN_WARNING "Base address is invalid\n");
  2294. return -1;
  2295. break;
  2296. }
  2297. }
  2298. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  2299. return i+1;
  2300. }
  2301. return -1;
  2302. }
  2303. static int cciss_pci_init(ctlr_info_t *c, struct pci_dev *pdev)
  2304. {
  2305. ushort subsystem_vendor_id, subsystem_device_id, command;
  2306. __u32 board_id, scratchpad = 0;
  2307. __u64 cfg_offset;
  2308. __u32 cfg_base_addr;
  2309. __u64 cfg_base_addr_index;
  2310. int i;
  2311. /* check to see if controller has been disabled */
  2312. /* BEFORE trying to enable it */
  2313. (void) pci_read_config_word(pdev, PCI_COMMAND,&command);
  2314. if(!(command & 0x02))
  2315. {
  2316. printk(KERN_WARNING "cciss: controller appears to be disabled\n");
  2317. return(-1);
  2318. }
  2319. if (pci_enable_device(pdev))
  2320. {
  2321. printk(KERN_ERR "cciss: Unable to Enable PCI device\n");
  2322. return( -1);
  2323. }
  2324. subsystem_vendor_id = pdev->subsystem_vendor;
  2325. subsystem_device_id = pdev->subsystem_device;
  2326. board_id = (((__u32) (subsystem_device_id << 16) & 0xffff0000) |
  2327. subsystem_vendor_id);
  2328. /* search for our IO range so we can protect it */
  2329. for(i=0; i<DEVICE_COUNT_RESOURCE; i++)
  2330. {
  2331. /* is this an IO range */
  2332. if( pci_resource_flags(pdev, i) & 0x01 ) {
  2333. c->io_mem_addr = pci_resource_start(pdev, i);
  2334. c->io_mem_length = pci_resource_end(pdev, i) -
  2335. pci_resource_start(pdev, i) +1;
  2336. #ifdef CCISS_DEBUG
  2337. printk("IO value found base_addr[%d] %lx %lx\n", i,
  2338. c->io_mem_addr, c->io_mem_length);
  2339. #endif /* CCISS_DEBUG */
  2340. /* register the IO range */
  2341. if(!request_region( c->io_mem_addr,
  2342. c->io_mem_length, "cciss"))
  2343. {
  2344. printk(KERN_WARNING "cciss I/O memory range already in use addr=%lx length=%ld\n",
  2345. c->io_mem_addr, c->io_mem_length);
  2346. c->io_mem_addr= 0;
  2347. c->io_mem_length = 0;
  2348. }
  2349. break;
  2350. }
  2351. }
  2352. #ifdef CCISS_DEBUG
  2353. printk("command = %x\n", command);
  2354. printk("irq = %x\n", pdev->irq);
  2355. printk("board_id = %x\n", board_id);
  2356. #endif /* CCISS_DEBUG */
  2357. c->intr = pdev->irq;
  2358. /*
  2359. * Memory base addr is first addr , the second points to the config
  2360. * table
  2361. */
  2362. c->paddr = pci_resource_start(pdev, 0); /* addressing mode bits already removed */
  2363. #ifdef CCISS_DEBUG
  2364. printk("address 0 = %x\n", c->paddr);
  2365. #endif /* CCISS_DEBUG */
  2366. c->vaddr = remap_pci_mem(c->paddr, 200);
  2367. /* Wait for the board to become ready. (PCI hotplug needs this.)
  2368. * We poll for up to 120 secs, once per 100ms. */
  2369. for (i=0; i < 1200; i++) {
  2370. scratchpad = readl(c->vaddr + SA5_SCRATCHPAD_OFFSET);
  2371. if (scratchpad == CCISS_FIRMWARE_READY)
  2372. break;
  2373. set_current_state(TASK_INTERRUPTIBLE);
  2374. schedule_timeout(HZ / 10); /* wait 100ms */
  2375. }
  2376. if (scratchpad != CCISS_FIRMWARE_READY) {
  2377. printk(KERN_WARNING "cciss: Board not ready. Timed out.\n");
  2378. return -1;
  2379. }
  2380. /* get the address index number */
  2381. cfg_base_addr = readl(c->vaddr + SA5_CTCFG_OFFSET);
  2382. cfg_base_addr &= (__u32) 0x0000ffff;
  2383. #ifdef CCISS_DEBUG
  2384. printk("cfg base address = %x\n", cfg_base_addr);
  2385. #endif /* CCISS_DEBUG */
  2386. cfg_base_addr_index =
  2387. find_PCI_BAR_index(pdev, cfg_base_addr);
  2388. #ifdef CCISS_DEBUG
  2389. printk("cfg base address index = %x\n", cfg_base_addr_index);
  2390. #endif /* CCISS_DEBUG */
  2391. if (cfg_base_addr_index == -1) {
  2392. printk(KERN_WARNING "cciss: Cannot find cfg_base_addr_index\n");
  2393. release_io_mem(c);
  2394. return -1;
  2395. }
  2396. cfg_offset = readl(c->vaddr + SA5_CTMEM_OFFSET);
  2397. #ifdef CCISS_DEBUG
  2398. printk("cfg offset = %x\n", cfg_offset);
  2399. #endif /* CCISS_DEBUG */
  2400. c->cfgtable = remap_pci_mem(pci_resource_start(pdev,
  2401. cfg_base_addr_index) + cfg_offset,
  2402. sizeof(CfgTable_struct));
  2403. c->board_id = board_id;
  2404. #ifdef CCISS_DEBUG
  2405. print_cfg_table(c->cfgtable);
  2406. #endif /* CCISS_DEBUG */
  2407. for(i=0; i<NR_PRODUCTS; i++) {
  2408. if (board_id == products[i].board_id) {
  2409. c->product_name = products[i].product_name;
  2410. c->access = *(products[i].access);
  2411. break;
  2412. }
  2413. }
  2414. if (i == NR_PRODUCTS) {
  2415. printk(KERN_WARNING "cciss: Sorry, I don't know how"
  2416. " to access the Smart Array controller %08lx\n",
  2417. (unsigned long)board_id);
  2418. return -1;
  2419. }
  2420. if ( (readb(&c->cfgtable->Signature[0]) != 'C') ||
  2421. (readb(&c->cfgtable->Signature[1]) != 'I') ||
  2422. (readb(&c->cfgtable->Signature[2]) != 'S') ||
  2423. (readb(&c->cfgtable->Signature[3]) != 'S') )
  2424. {
  2425. printk("Does not appear to be a valid CISS config table\n");
  2426. return -1;
  2427. }
  2428. #ifdef CONFIG_X86
  2429. {
  2430. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  2431. __u32 prefetch;
  2432. prefetch = readl(&(c->cfgtable->SCSI_Prefetch));
  2433. prefetch |= 0x100;
  2434. writel(prefetch, &(c->cfgtable->SCSI_Prefetch));
  2435. }
  2436. #endif
  2437. #ifdef CCISS_DEBUG
  2438. printk("Trying to put board into Simple mode\n");
  2439. #endif /* CCISS_DEBUG */
  2440. c->max_commands = readl(&(c->cfgtable->CmdsOutMax));
  2441. /* Update the field, and then ring the doorbell */
  2442. writel( CFGTBL_Trans_Simple,
  2443. &(c->cfgtable->HostWrite.TransportRequest));
  2444. writel( CFGTBL_ChangeReq, c->vaddr + SA5_DOORBELL);
  2445. /* under certain very rare conditions, this can take awhile.
  2446. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  2447. * as we enter this code.) */
  2448. for(i=0;i<MAX_CONFIG_WAIT;i++) {
  2449. if (!(readl(c->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  2450. break;
  2451. /* delay and try again */
  2452. set_current_state(TASK_INTERRUPTIBLE);
  2453. schedule_timeout(10);
  2454. }
  2455. #ifdef CCISS_DEBUG
  2456. printk(KERN_DEBUG "I counter got to %d %x\n", i, readl(c->vaddr + SA5_DOORBELL));
  2457. #endif /* CCISS_DEBUG */
  2458. #ifdef CCISS_DEBUG
  2459. print_cfg_table(c->cfgtable);
  2460. #endif /* CCISS_DEBUG */
  2461. if (!(readl(&(c->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
  2462. {
  2463. printk(KERN_WARNING "cciss: unable to get board into"
  2464. " simple mode\n");
  2465. return -1;
  2466. }
  2467. return 0;
  2468. }
  2469. /*
  2470. * Gets information about the local volumes attached to the controller.
  2471. */
  2472. static void cciss_getgeometry(int cntl_num)
  2473. {
  2474. ReportLunData_struct *ld_buff;
  2475. ReadCapdata_struct *size_buff;
  2476. InquiryData_struct *inq_buff;
  2477. int return_code;
  2478. int i;
  2479. int listlength = 0;
  2480. __u32 lunid = 0;
  2481. int block_size;
  2482. int total_size;
  2483. ld_buff = kmalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
  2484. if (ld_buff == NULL)
  2485. {
  2486. printk(KERN_ERR "cciss: out of memory\n");
  2487. return;
  2488. }
  2489. memset(ld_buff, 0, sizeof(ReportLunData_struct));
  2490. size_buff = kmalloc(sizeof( ReadCapdata_struct), GFP_KERNEL);
  2491. if (size_buff == NULL)
  2492. {
  2493. printk(KERN_ERR "cciss: out of memory\n");
  2494. kfree(ld_buff);
  2495. return;
  2496. }
  2497. inq_buff = kmalloc(sizeof( InquiryData_struct), GFP_KERNEL);
  2498. if (inq_buff == NULL)
  2499. {
  2500. printk(KERN_ERR "cciss: out of memory\n");
  2501. kfree(ld_buff);
  2502. kfree(size_buff);
  2503. return;
  2504. }
  2505. /* Get the firmware version */
  2506. return_code = sendcmd(CISS_INQUIRY, cntl_num, inq_buff,
  2507. sizeof(InquiryData_struct), 0, 0 ,0, NULL, TYPE_CMD);
  2508. if (return_code == IO_OK)
  2509. {
  2510. hba[cntl_num]->firm_ver[0] = inq_buff->data_byte[32];
  2511. hba[cntl_num]->firm_ver[1] = inq_buff->data_byte[33];
  2512. hba[cntl_num]->firm_ver[2] = inq_buff->data_byte[34];
  2513. hba[cntl_num]->firm_ver[3] = inq_buff->data_byte[35];
  2514. } else /* send command failed */
  2515. {
  2516. printk(KERN_WARNING "cciss: unable to determine firmware"
  2517. " version of controller\n");
  2518. }
  2519. /* Get the number of logical volumes */
  2520. return_code = sendcmd(CISS_REPORT_LOG, cntl_num, ld_buff,
  2521. sizeof(ReportLunData_struct), 0, 0, 0, NULL, TYPE_CMD);
  2522. if( return_code == IO_OK)
  2523. {
  2524. #ifdef CCISS_DEBUG
  2525. printk("LUN Data\n--------------------------\n");
  2526. #endif /* CCISS_DEBUG */
  2527. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[0])) << 24;
  2528. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[1])) << 16;
  2529. listlength |= (0xff & (unsigned int)(ld_buff->LUNListLength[2])) << 8;
  2530. listlength |= 0xff & (unsigned int)(ld_buff->LUNListLength[3]);
  2531. } else /* reading number of logical volumes failed */
  2532. {
  2533. printk(KERN_WARNING "cciss: report logical volume"
  2534. " command failed\n");
  2535. listlength = 0;
  2536. }
  2537. hba[cntl_num]->num_luns = listlength / 8; // 8 bytes pre entry
  2538. if (hba[cntl_num]->num_luns > CISS_MAX_LUN)
  2539. {
  2540. printk(KERN_ERR "ciss: only %d number of logical volumes supported\n",
  2541. CISS_MAX_LUN);
  2542. hba[cntl_num]->num_luns = CISS_MAX_LUN;
  2543. }
  2544. #ifdef CCISS_DEBUG
  2545. printk(KERN_DEBUG "Length = %x %x %x %x = %d\n", ld_buff->LUNListLength[0],
  2546. ld_buff->LUNListLength[1], ld_buff->LUNListLength[2],
  2547. ld_buff->LUNListLength[3], hba[cntl_num]->num_luns);
  2548. #endif /* CCISS_DEBUG */
  2549. hba[cntl_num]->highest_lun = hba[cntl_num]->num_luns-1;
  2550. // for(i=0; i< hba[cntl_num]->num_luns; i++)
  2551. for(i=0; i < CISS_MAX_LUN; i++)
  2552. {
  2553. if (i < hba[cntl_num]->num_luns){
  2554. lunid = (0xff & (unsigned int)(ld_buff->LUN[i][3]))
  2555. << 24;
  2556. lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][2]))
  2557. << 16;
  2558. lunid |= (0xff & (unsigned int)(ld_buff->LUN[i][1]))
  2559. << 8;
  2560. lunid |= 0xff & (unsigned int)(ld_buff->LUN[i][0]);
  2561. hba[cntl_num]->drv[i].LunID = lunid;
  2562. #ifdef CCISS_DEBUG
  2563. printk(KERN_DEBUG "LUN[%d]: %x %x %x %x = %x\n", i,
  2564. ld_buff->LUN[i][0], ld_buff->LUN[i][1],
  2565. ld_buff->LUN[i][2], ld_buff->LUN[i][3],
  2566. hba[cntl_num]->drv[i].LunID);
  2567. #endif /* CCISS_DEBUG */
  2568. cciss_read_capacity(cntl_num, i, size_buff, 0,
  2569. &total_size, &block_size);
  2570. cciss_geometry_inquiry(cntl_num, i, 0, total_size,
  2571. block_size, inq_buff, &hba[cntl_num]->drv[i]);
  2572. } else {
  2573. /* initialize raid_level to indicate a free space */
  2574. hba[cntl_num]->drv[i].raid_level = -1;
  2575. }
  2576. }
  2577. kfree(ld_buff);
  2578. kfree(size_buff);
  2579. kfree(inq_buff);
  2580. }
  2581. /* Function to find the first free pointer into our hba[] array */
  2582. /* Returns -1 if no free entries are left. */
  2583. static int alloc_cciss_hba(void)
  2584. {
  2585. struct gendisk *disk[NWD];
  2586. int i, n;
  2587. for (n = 0; n < NWD; n++) {
  2588. disk[n] = alloc_disk(1 << NWD_SHIFT);
  2589. if (!disk[n])
  2590. goto out;
  2591. }
  2592. for(i=0; i< MAX_CTLR; i++) {
  2593. if (!hba[i]) {
  2594. ctlr_info_t *p;
  2595. p = kmalloc(sizeof(ctlr_info_t), GFP_KERNEL);
  2596. if (!p)
  2597. goto Enomem;
  2598. memset(p, 0, sizeof(ctlr_info_t));
  2599. for (n = 0; n < NWD; n++)
  2600. p->gendisk[n] = disk[n];
  2601. hba[i] = p;
  2602. return i;
  2603. }
  2604. }
  2605. printk(KERN_WARNING "cciss: This driver supports a maximum"
  2606. " of %d controllers.\n", MAX_CTLR);
  2607. goto out;
  2608. Enomem:
  2609. printk(KERN_ERR "cciss: out of memory.\n");
  2610. out:
  2611. while (n--)
  2612. put_disk(disk[n]);
  2613. return -1;
  2614. }
  2615. static void free_hba(int i)
  2616. {
  2617. ctlr_info_t *p = hba[i];
  2618. int n;
  2619. hba[i] = NULL;
  2620. for (n = 0; n < NWD; n++)
  2621. put_disk(p->gendisk[n]);
  2622. kfree(p);
  2623. }
  2624. /*
  2625. * This is it. Find all the controllers and register them. I really hate
  2626. * stealing all these major device numbers.
  2627. * returns the number of block devices registered.
  2628. */
  2629. static int __devinit cciss_init_one(struct pci_dev *pdev,
  2630. const struct pci_device_id *ent)
  2631. {
  2632. request_queue_t *q;
  2633. int i;
  2634. int j;
  2635. int rc;
  2636. printk(KERN_DEBUG "cciss: Device 0x%x has been found at"
  2637. " bus %d dev %d func %d\n",
  2638. pdev->device, pdev->bus->number, PCI_SLOT(pdev->devfn),
  2639. PCI_FUNC(pdev->devfn));
  2640. i = alloc_cciss_hba();
  2641. if(i < 0)
  2642. return (-1);
  2643. hba[i]->busy_initializing = 1;
  2644. if (cciss_pci_init(hba[i], pdev) != 0)
  2645. goto clean1;
  2646. sprintf(hba[i]->devname, "cciss%d", i);
  2647. hba[i]->ctlr = i;
  2648. hba[i]->pdev = pdev;
  2649. /* configure PCI DMA stuff */
  2650. if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK))
  2651. printk("cciss: using DAC cycles\n");
  2652. else if (!pci_set_dma_mask(pdev, DMA_32BIT_MASK))
  2653. printk("cciss: not using DAC cycles\n");
  2654. else {
  2655. printk("cciss: no suitable DMA available\n");
  2656. goto clean1;
  2657. }
  2658. /*
  2659. * register with the major number, or get a dynamic major number
  2660. * by passing 0 as argument. This is done for greater than
  2661. * 8 controller support.
  2662. */
  2663. if (i < MAX_CTLR_ORIG)
  2664. hba[i]->major = MAJOR_NR + i;
  2665. rc = register_blkdev(hba[i]->major, hba[i]->devname);
  2666. if(rc == -EBUSY || rc == -EINVAL) {
  2667. printk(KERN_ERR
  2668. "cciss: Unable to get major number %d for %s "
  2669. "on hba %d\n", hba[i]->major, hba[i]->devname, i);
  2670. goto clean1;
  2671. }
  2672. else {
  2673. if (i >= MAX_CTLR_ORIG)
  2674. hba[i]->major = rc;
  2675. }
  2676. /* make sure the board interrupts are off */
  2677. hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_OFF);
  2678. if( request_irq(hba[i]->intr, do_cciss_intr,
  2679. SA_INTERRUPT | SA_SHIRQ | SA_SAMPLE_RANDOM,
  2680. hba[i]->devname, hba[i])) {
  2681. printk(KERN_ERR "cciss: Unable to get irq %d for %s\n",
  2682. hba[i]->intr, hba[i]->devname);
  2683. goto clean2;
  2684. }
  2685. hba[i]->cmd_pool_bits = kmalloc(((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long), GFP_KERNEL);
  2686. hba[i]->cmd_pool = (CommandList_struct *)pci_alloc_consistent(
  2687. hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
  2688. &(hba[i]->cmd_pool_dhandle));
  2689. hba[i]->errinfo_pool = (ErrorInfo_struct *)pci_alloc_consistent(
  2690. hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
  2691. &(hba[i]->errinfo_pool_dhandle));
  2692. if((hba[i]->cmd_pool_bits == NULL)
  2693. || (hba[i]->cmd_pool == NULL)
  2694. || (hba[i]->errinfo_pool == NULL)) {
  2695. printk( KERN_ERR "cciss: out of memory");
  2696. goto clean4;
  2697. }
  2698. spin_lock_init(&hba[i]->lock);
  2699. /* Initialize the pdev driver private data.
  2700. have it point to hba[i]. */
  2701. pci_set_drvdata(pdev, hba[i]);
  2702. /* command and error info recs zeroed out before
  2703. they are used */
  2704. memset(hba[i]->cmd_pool_bits, 0, ((NR_CMDS+BITS_PER_LONG-1)/BITS_PER_LONG)*sizeof(unsigned long));
  2705. #ifdef CCISS_DEBUG
  2706. printk(KERN_DEBUG "Scanning for drives on controller cciss%d\n",i);
  2707. #endif /* CCISS_DEBUG */
  2708. cciss_getgeometry(i);
  2709. cciss_scsi_setup(i);
  2710. /* Turn the interrupts on so we can service requests */
  2711. hba[i]->access.set_intr_mask(hba[i], CCISS_INTR_ON);
  2712. cciss_procinit(i);
  2713. for(j=0; j < NWD; j++) { /* mfm */
  2714. drive_info_struct *drv = &(hba[i]->drv[j]);
  2715. struct gendisk *disk = hba[i]->gendisk[j];
  2716. q = blk_init_queue(do_cciss_request, &hba[i]->lock);
  2717. if (!q) {
  2718. printk(KERN_ERR
  2719. "cciss: unable to allocate queue for disk %d\n",
  2720. j);
  2721. break;
  2722. }
  2723. drv->queue = q;
  2724. q->backing_dev_info.ra_pages = READ_AHEAD;
  2725. blk_queue_bounce_limit(q, hba[i]->pdev->dma_mask);
  2726. /* This is a hardware imposed limit. */
  2727. blk_queue_max_hw_segments(q, MAXSGENTRIES);
  2728. /* This is a limit in the driver and could be eliminated. */
  2729. blk_queue_max_phys_segments(q, MAXSGENTRIES);
  2730. blk_queue_max_sectors(q, 512);
  2731. q->queuedata = hba[i];
  2732. sprintf(disk->disk_name, "cciss/c%dd%d", i, j);
  2733. sprintf(disk->devfs_name, "cciss/host%d/target%d", i, j);
  2734. disk->major = hba[i]->major;
  2735. disk->first_minor = j << NWD_SHIFT;
  2736. disk->fops = &cciss_fops;
  2737. disk->queue = q;
  2738. disk->private_data = drv;
  2739. /* we must register the controller even if no disks exist */
  2740. /* this is for the online array utilities */
  2741. if(!drv->heads && j)
  2742. continue;
  2743. blk_queue_hardsect_size(q, drv->block_size);
  2744. set_capacity(disk, drv->nr_blocks);
  2745. add_disk(disk);
  2746. }
  2747. hba[i]->busy_initializing = 0;
  2748. return(1);
  2749. clean4:
  2750. if(hba[i]->cmd_pool_bits)
  2751. kfree(hba[i]->cmd_pool_bits);
  2752. if(hba[i]->cmd_pool)
  2753. pci_free_consistent(hba[i]->pdev,
  2754. NR_CMDS * sizeof(CommandList_struct),
  2755. hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
  2756. if(hba[i]->errinfo_pool)
  2757. pci_free_consistent(hba[i]->pdev,
  2758. NR_CMDS * sizeof( ErrorInfo_struct),
  2759. hba[i]->errinfo_pool,
  2760. hba[i]->errinfo_pool_dhandle);
  2761. free_irq(hba[i]->intr, hba[i]);
  2762. clean2:
  2763. unregister_blkdev(hba[i]->major, hba[i]->devname);
  2764. clean1:
  2765. release_io_mem(hba[i]);
  2766. free_hba(i);
  2767. hba[i]->busy_initializing = 0;
  2768. return(-1);
  2769. }
  2770. static void __devexit cciss_remove_one (struct pci_dev *pdev)
  2771. {
  2772. ctlr_info_t *tmp_ptr;
  2773. int i, j;
  2774. char flush_buf[4];
  2775. int return_code;
  2776. if (pci_get_drvdata(pdev) == NULL)
  2777. {
  2778. printk( KERN_ERR "cciss: Unable to remove device \n");
  2779. return;
  2780. }
  2781. tmp_ptr = pci_get_drvdata(pdev);
  2782. i = tmp_ptr->ctlr;
  2783. if (hba[i] == NULL)
  2784. {
  2785. printk(KERN_ERR "cciss: device appears to "
  2786. "already be removed \n");
  2787. return;
  2788. }
  2789. /* Turn board interrupts off and send the flush cache command */
  2790. /* sendcmd will turn off interrupt, and send the flush...
  2791. * To write all data in the battery backed cache to disks */
  2792. memset(flush_buf, 0, 4);
  2793. return_code = sendcmd(CCISS_CACHE_FLUSH, i, flush_buf, 4, 0, 0, 0, NULL,
  2794. TYPE_CMD);
  2795. if(return_code != IO_OK)
  2796. {
  2797. printk(KERN_WARNING "Error Flushing cache on controller %d\n",
  2798. i);
  2799. }
  2800. free_irq(hba[i]->intr, hba[i]);
  2801. pci_set_drvdata(pdev, NULL);
  2802. iounmap(hba[i]->vaddr);
  2803. cciss_unregister_scsi(i); /* unhook from SCSI subsystem */
  2804. unregister_blkdev(hba[i]->major, hba[i]->devname);
  2805. remove_proc_entry(hba[i]->devname, proc_cciss);
  2806. /* remove it from the disk list */
  2807. for (j = 0; j < NWD; j++) {
  2808. struct gendisk *disk = hba[i]->gendisk[j];
  2809. if (disk->flags & GENHD_FL_UP) {
  2810. del_gendisk(disk);
  2811. blk_cleanup_queue(disk->queue);
  2812. }
  2813. }
  2814. pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof(CommandList_struct),
  2815. hba[i]->cmd_pool, hba[i]->cmd_pool_dhandle);
  2816. pci_free_consistent(hba[i]->pdev, NR_CMDS * sizeof( ErrorInfo_struct),
  2817. hba[i]->errinfo_pool, hba[i]->errinfo_pool_dhandle);
  2818. kfree(hba[i]->cmd_pool_bits);
  2819. release_io_mem(hba[i]);
  2820. free_hba(i);
  2821. }
  2822. static struct pci_driver cciss_pci_driver = {
  2823. .name = "cciss",
  2824. .probe = cciss_init_one,
  2825. .remove = __devexit_p(cciss_remove_one),
  2826. .id_table = cciss_pci_device_id, /* id_table */
  2827. };
  2828. /*
  2829. * This is it. Register the PCI driver information for the cards we control
  2830. * the OS will call our registered routines when it finds one of our cards.
  2831. */
  2832. static int __init cciss_init(void)
  2833. {
  2834. printk(KERN_INFO DRIVER_NAME "\n");
  2835. /* Register for our PCI devices */
  2836. return pci_module_init(&cciss_pci_driver);
  2837. }
  2838. static void __exit cciss_cleanup(void)
  2839. {
  2840. int i;
  2841. pci_unregister_driver(&cciss_pci_driver);
  2842. /* double check that all controller entrys have been removed */
  2843. for (i=0; i< MAX_CTLR; i++)
  2844. {
  2845. if (hba[i] != NULL)
  2846. {
  2847. printk(KERN_WARNING "cciss: had to remove"
  2848. " controller %d\n", i);
  2849. cciss_remove_one(hba[i]->pdev);
  2850. }
  2851. }
  2852. remove_proc_entry("cciss", proc_root_driver);
  2853. }
  2854. static void fail_all_cmds(unsigned long ctlr)
  2855. {
  2856. /* If we get here, the board is apparently dead. */
  2857. ctlr_info_t *h = hba[ctlr];
  2858. CommandList_struct *c;
  2859. unsigned long flags;
  2860. printk(KERN_WARNING "cciss%d: controller not responding.\n", h->ctlr);
  2861. h->alive = 0; /* the controller apparently died... */
  2862. spin_lock_irqsave(CCISS_LOCK(ctlr), flags);
  2863. pci_disable_device(h->pdev); /* Make sure it is really dead. */
  2864. /* move everything off the request queue onto the completed queue */
  2865. while( (c = h->reqQ) != NULL ) {
  2866. removeQ(&(h->reqQ), c);
  2867. h->Qdepth--;
  2868. addQ (&(h->cmpQ), c);
  2869. }
  2870. /* Now, fail everything on the completed queue with a HW error */
  2871. while( (c = h->cmpQ) != NULL ) {
  2872. removeQ(&h->cmpQ, c);
  2873. c->err_info->CommandStatus = CMD_HARDWARE_ERR;
  2874. if (c->cmd_type == CMD_RWREQ) {
  2875. complete_command(h, c, 0);
  2876. } else if (c->cmd_type == CMD_IOCTL_PEND)
  2877. complete(c->waiting);
  2878. #ifdef CONFIG_CISS_SCSI_TAPE
  2879. else if (c->cmd_type == CMD_SCSI)
  2880. complete_scsi_command(c, 0, 0);
  2881. #endif
  2882. }
  2883. spin_unlock_irqrestore(CCISS_LOCK(ctlr), flags);
  2884. return;
  2885. }
  2886. module_init(cciss_init);
  2887. module_exit(cciss_cleanup);