cciss.c 90 KB

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