cciss.c 83 KB

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