megaraid.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright © 2002 LSI Logic Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
  13. * - fixes
  14. * - speed-ups (list handling fixes, issued_list, optimizations.)
  15. * - lots of cleanups.
  16. *
  17. * Copyright (c) 2003 Christoph Hellwig <hch@lst.de>
  18. * - new-style, hotplug-aware pci probing and scsi registration
  19. *
  20. * Version : v2.00.3 (Feb 19, 2003) - Atul Mukker <Atul.Mukker@lsil.com>
  21. *
  22. * Description: Linux device driver for LSI Logic MegaRAID controller
  23. *
  24. * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
  25. * 518, 520, 531, 532
  26. *
  27. * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
  28. * and others. Please send updates to the mailing list
  29. * linux-scsi@vger.kernel.org .
  30. *
  31. */
  32. #include <linux/mm.h>
  33. #include <linux/fs.h>
  34. #include <linux/blkdev.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/io.h>
  37. #include <linux/completion.h>
  38. #include <linux/delay.h>
  39. #include <linux/proc_fs.h>
  40. #include <linux/reboot.h>
  41. #include <linux/module.h>
  42. #include <linux/list.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/pci.h>
  45. #include <linux/init.h>
  46. #include <scsi/scsicam.h>
  47. #include "scsi.h"
  48. #include <scsi/scsi_host.h>
  49. #include "megaraid.h"
  50. #define MEGARAID_MODULE_VERSION "2.00.3"
  51. MODULE_AUTHOR ("LSI Logic Corporation");
  52. MODULE_DESCRIPTION ("LSI Logic MegaRAID driver");
  53. MODULE_LICENSE ("GPL");
  54. MODULE_VERSION(MEGARAID_MODULE_VERSION);
  55. static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
  56. module_param(max_cmd_per_lun, uint, 0);
  57. MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
  58. static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
  59. module_param(max_sectors_per_io, ushort, 0);
  60. MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
  61. static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  62. module_param(max_mbox_busy_wait, ushort, 0);
  63. MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
  64. #define RDINDOOR(adapter) readl((adapter)->base + 0x20)
  65. #define RDOUTDOOR(adapter) readl((adapter)->base + 0x2C)
  66. #define WRINDOOR(adapter,value) writel(value, (adapter)->base + 0x20)
  67. #define WROUTDOOR(adapter,value) writel(value, (adapter)->base + 0x2C)
  68. /*
  69. * Global variables
  70. */
  71. static int hba_count;
  72. static adapter_t *hba_soft_state[MAX_CONTROLLERS];
  73. static struct proc_dir_entry *mega_proc_dir_entry;
  74. /* For controller re-ordering */
  75. static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
  76. /*
  77. * The File Operations structure for the serial/ioctl interface of the driver
  78. */
  79. static struct file_operations megadev_fops = {
  80. .owner = THIS_MODULE,
  81. .ioctl = megadev_ioctl,
  82. .open = megadev_open,
  83. };
  84. /*
  85. * Array to structures for storing the information about the controllers. This
  86. * information is sent to the user level applications, when they do an ioctl
  87. * for this information.
  88. */
  89. static struct mcontroller mcontroller[MAX_CONTROLLERS];
  90. /* The current driver version */
  91. static u32 driver_ver = 0x02000000;
  92. /* major number used by the device for character interface */
  93. static int major;
  94. #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
  95. /*
  96. * Debug variable to print some diagnostic messages
  97. */
  98. static int trace_level;
  99. /**
  100. * mega_setup_mailbox()
  101. * @adapter - pointer to our soft state
  102. *
  103. * Allocates a 8 byte aligned memory for the handshake mailbox.
  104. */
  105. static int
  106. mega_setup_mailbox(adapter_t *adapter)
  107. {
  108. unsigned long align;
  109. adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
  110. sizeof(mbox64_t), &adapter->una_mbox64_dma);
  111. if( !adapter->una_mbox64 ) return -1;
  112. adapter->mbox = &adapter->una_mbox64->mbox;
  113. adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
  114. (~0UL ^ 0xFUL));
  115. adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
  116. align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
  117. adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
  118. /*
  119. * Register the mailbox if the controller is an io-mapped controller
  120. */
  121. if( adapter->flag & BOARD_IOMAP ) {
  122. outb_p(adapter->mbox_dma & 0xFF,
  123. adapter->host->io_port + MBOX_PORT0);
  124. outb_p((adapter->mbox_dma >> 8) & 0xFF,
  125. adapter->host->io_port + MBOX_PORT1);
  126. outb_p((adapter->mbox_dma >> 16) & 0xFF,
  127. adapter->host->io_port + MBOX_PORT2);
  128. outb_p((adapter->mbox_dma >> 24) & 0xFF,
  129. adapter->host->io_port + MBOX_PORT3);
  130. outb_p(ENABLE_MBOX_BYTE,
  131. adapter->host->io_port + ENABLE_MBOX_REGION);
  132. irq_ack(adapter);
  133. irq_enable(adapter);
  134. }
  135. return 0;
  136. }
  137. /*
  138. * mega_query_adapter()
  139. * @adapter - pointer to our soft state
  140. *
  141. * Issue the adapter inquiry commands to the controller and find out
  142. * information and parameter about the devices attached
  143. */
  144. static int
  145. mega_query_adapter(adapter_t *adapter)
  146. {
  147. dma_addr_t prod_info_dma_handle;
  148. mega_inquiry3 *inquiry3;
  149. u8 raw_mbox[sizeof(struct mbox_out)];
  150. mbox_t *mbox;
  151. int retval;
  152. /* Initialize adapter inquiry mailbox */
  153. mbox = (mbox_t *)raw_mbox;
  154. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  155. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  156. /*
  157. * Try to issue Inquiry3 command
  158. * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
  159. * update enquiry3 structure
  160. */
  161. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  162. inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
  163. raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
  164. raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
  165. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
  166. /* Issue a blocking command to the card */
  167. if ((retval = issue_scb_block(adapter, raw_mbox))) {
  168. /* the adapter does not support 40ld */
  169. mraid_ext_inquiry *ext_inq;
  170. mraid_inquiry *inq;
  171. dma_addr_t dma_handle;
  172. ext_inq = pci_alloc_consistent(adapter->dev,
  173. sizeof(mraid_ext_inquiry), &dma_handle);
  174. if( ext_inq == NULL ) return -1;
  175. inq = &ext_inq->raid_inq;
  176. mbox->m_out.xferaddr = (u32)dma_handle;
  177. /*issue old 0x04 command to adapter */
  178. mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  179. issue_scb_block(adapter, raw_mbox);
  180. /*
  181. * update Enquiry3 and ProductInfo structures with
  182. * mraid_inquiry structure
  183. */
  184. mega_8_to_40ld(inq, inquiry3,
  185. (mega_product_info *)&adapter->product_info);
  186. pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
  187. ext_inq, dma_handle);
  188. } else { /*adapter supports 40ld */
  189. adapter->flag |= BOARD_40LD;
  190. /*
  191. * get product_info, which is static information and will be
  192. * unchanged
  193. */
  194. prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
  195. &adapter->product_info,
  196. sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  197. mbox->m_out.xferaddr = prod_info_dma_handle;
  198. raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
  199. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
  200. if ((retval = issue_scb_block(adapter, raw_mbox)))
  201. printk(KERN_WARNING
  202. "megaraid: Product_info cmd failed with error: %d\n",
  203. retval);
  204. pci_unmap_single(adapter->dev, prod_info_dma_handle,
  205. sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  206. }
  207. /*
  208. * kernel scans the channels from 0 to <= max_channel
  209. */
  210. adapter->host->max_channel =
  211. adapter->product_info.nchannels + NVIRT_CHAN -1;
  212. adapter->host->max_id = 16; /* max targets per channel */
  213. adapter->host->max_lun = 7; /* Upto 7 luns for non disk devices */
  214. adapter->host->cmd_per_lun = max_cmd_per_lun;
  215. adapter->numldrv = inquiry3->num_ldrv;
  216. adapter->max_cmds = adapter->product_info.max_commands;
  217. if(adapter->max_cmds > MAX_COMMANDS)
  218. adapter->max_cmds = MAX_COMMANDS;
  219. adapter->host->can_queue = adapter->max_cmds - 1;
  220. /*
  221. * Get the maximum number of scatter-gather elements supported by this
  222. * firmware
  223. */
  224. mega_get_max_sgl(adapter);
  225. adapter->host->sg_tablesize = adapter->sglen;
  226. /* use HP firmware and bios version encoding */
  227. if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
  228. sprintf (adapter->fw_version, "%c%d%d.%d%d",
  229. adapter->product_info.fw_version[2],
  230. adapter->product_info.fw_version[1] >> 8,
  231. adapter->product_info.fw_version[1] & 0x0f,
  232. adapter->product_info.fw_version[0] >> 8,
  233. adapter->product_info.fw_version[0] & 0x0f);
  234. sprintf (adapter->bios_version, "%c%d%d.%d%d",
  235. adapter->product_info.bios_version[2],
  236. adapter->product_info.bios_version[1] >> 8,
  237. adapter->product_info.bios_version[1] & 0x0f,
  238. adapter->product_info.bios_version[0] >> 8,
  239. adapter->product_info.bios_version[0] & 0x0f);
  240. } else {
  241. memcpy(adapter->fw_version,
  242. (char *)adapter->product_info.fw_version, 4);
  243. adapter->fw_version[4] = 0;
  244. memcpy(adapter->bios_version,
  245. (char *)adapter->product_info.bios_version, 4);
  246. adapter->bios_version[4] = 0;
  247. }
  248. printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
  249. adapter->fw_version, adapter->bios_version, adapter->numldrv);
  250. /*
  251. * Do we support extended (>10 bytes) cdbs
  252. */
  253. adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
  254. if (adapter->support_ext_cdb)
  255. printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
  256. return 0;
  257. }
  258. /**
  259. * mega_runpendq()
  260. * @adapter - pointer to our soft state
  261. *
  262. * Runs through the list of pending requests.
  263. */
  264. static inline void
  265. mega_runpendq(adapter_t *adapter)
  266. {
  267. if(!list_empty(&adapter->pending_list))
  268. __mega_runpendq(adapter);
  269. }
  270. /*
  271. * megaraid_queue()
  272. * @scmd - Issue this scsi command
  273. * @done - the callback hook into the scsi mid-layer
  274. *
  275. * The command queuing entry point for the mid-layer.
  276. */
  277. static int
  278. megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
  279. {
  280. adapter_t *adapter;
  281. scb_t *scb;
  282. int busy=0;
  283. unsigned long flags;
  284. adapter = (adapter_t *)scmd->device->host->hostdata;
  285. scmd->scsi_done = done;
  286. /*
  287. * Allocate and build a SCB request
  288. * busy flag will be set if mega_build_cmd() command could not
  289. * allocate scb. We will return non-zero status in that case.
  290. * NOTE: scb can be null even though certain commands completed
  291. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
  292. * return 0 in that case.
  293. */
  294. spin_lock_irqsave(&adapter->lock, flags);
  295. scb = mega_build_cmd(adapter, scmd, &busy);
  296. if(scb) {
  297. scb->state |= SCB_PENDQ;
  298. list_add_tail(&scb->list, &adapter->pending_list);
  299. /*
  300. * Check if the HBA is in quiescent state, e.g., during a
  301. * delete logical drive opertion. If it is, don't run
  302. * the pending_list.
  303. */
  304. if(atomic_read(&adapter->quiescent) == 0) {
  305. mega_runpendq(adapter);
  306. }
  307. return 0;
  308. }
  309. spin_unlock_irqrestore(&adapter->lock, flags);
  310. return busy;
  311. }
  312. /**
  313. * mega_allocate_scb()
  314. * @adapter - pointer to our soft state
  315. * @cmd - scsi command from the mid-layer
  316. *
  317. * Allocate a SCB structure. This is the central structure for controller
  318. * commands.
  319. */
  320. static inline scb_t *
  321. mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
  322. {
  323. struct list_head *head = &adapter->free_list;
  324. scb_t *scb;
  325. /* Unlink command from Free List */
  326. if( !list_empty(head) ) {
  327. scb = list_entry(head->next, scb_t, list);
  328. list_del_init(head->next);
  329. scb->state = SCB_ACTIVE;
  330. scb->cmd = cmd;
  331. scb->dma_type = MEGA_DMA_TYPE_NONE;
  332. return scb;
  333. }
  334. return NULL;
  335. }
  336. /**
  337. * mega_get_ldrv_num()
  338. * @adapter - pointer to our soft state
  339. * @cmd - scsi mid layer command
  340. * @channel - channel on the controller
  341. *
  342. * Calculate the logical drive number based on the information in scsi command
  343. * and the channel number.
  344. */
  345. static inline int
  346. mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
  347. {
  348. int tgt;
  349. int ldrv_num;
  350. tgt = cmd->device->id;
  351. if ( tgt > adapter->this_id )
  352. tgt--; /* we do not get inquires for initiator id */
  353. ldrv_num = (channel * 15) + tgt;
  354. /*
  355. * If we have a logical drive with boot enabled, project it first
  356. */
  357. if( adapter->boot_ldrv_enabled ) {
  358. if( ldrv_num == 0 ) {
  359. ldrv_num = adapter->boot_ldrv;
  360. }
  361. else {
  362. if( ldrv_num <= adapter->boot_ldrv ) {
  363. ldrv_num--;
  364. }
  365. }
  366. }
  367. /*
  368. * If "delete logical drive" feature is enabled on this controller.
  369. * Do only if at least one delete logical drive operation was done.
  370. *
  371. * Also, after logical drive deletion, instead of logical drive number,
  372. * the value returned should be 0x80+logical drive id.
  373. *
  374. * These is valid only for IO commands.
  375. */
  376. if (adapter->support_random_del && adapter->read_ldidmap )
  377. switch (cmd->cmnd[0]) {
  378. case READ_6: /* fall through */
  379. case WRITE_6: /* fall through */
  380. case READ_10: /* fall through */
  381. case WRITE_10:
  382. ldrv_num += 0x80;
  383. }
  384. return ldrv_num;
  385. }
  386. /**
  387. * mega_build_cmd()
  388. * @adapter - pointer to our soft state
  389. * @cmd - Prepare using this scsi command
  390. * @busy - busy flag if no resources
  391. *
  392. * Prepares a command and scatter gather list for the controller. This routine
  393. * also finds out if the commands is intended for a logical drive or a
  394. * physical device and prepares the controller command accordingly.
  395. *
  396. * We also re-order the logical drives and physical devices based on their
  397. * boot settings.
  398. */
  399. static scb_t *
  400. mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
  401. {
  402. mega_ext_passthru *epthru;
  403. mega_passthru *pthru;
  404. scb_t *scb;
  405. mbox_t *mbox;
  406. long seg;
  407. char islogical;
  408. int max_ldrv_num;
  409. int channel = 0;
  410. int target = 0;
  411. int ldrv_num = 0; /* logical drive number */
  412. /*
  413. * filter the internal and ioctl commands
  414. */
  415. if((cmd->cmnd[0] == MEGA_INTERNAL_CMD)) {
  416. return cmd->buffer;
  417. }
  418. /*
  419. * We know what channels our logical drives are on - mega_find_card()
  420. */
  421. islogical = adapter->logdrv_chan[cmd->device->channel];
  422. /*
  423. * The theory: If physical drive is chosen for boot, all the physical
  424. * devices are exported before the logical drives, otherwise physical
  425. * devices are pushed after logical drives, in which case - Kernel sees
  426. * the physical devices on virtual channel which is obviously converted
  427. * to actual channel on the HBA.
  428. */
  429. if( adapter->boot_pdrv_enabled ) {
  430. if( islogical ) {
  431. /* logical channel */
  432. channel = cmd->device->channel -
  433. adapter->product_info.nchannels;
  434. }
  435. else {
  436. /* this is physical channel */
  437. channel = cmd->device->channel;
  438. target = cmd->device->id;
  439. /*
  440. * boot from a physical disk, that disk needs to be
  441. * exposed first IF both the channels are SCSI, then
  442. * booting from the second channel is not allowed.
  443. */
  444. if( target == 0 ) {
  445. target = adapter->boot_pdrv_tgt;
  446. }
  447. else if( target == adapter->boot_pdrv_tgt ) {
  448. target = 0;
  449. }
  450. }
  451. }
  452. else {
  453. if( islogical ) {
  454. /* this is the logical channel */
  455. channel = cmd->device->channel;
  456. }
  457. else {
  458. /* physical channel */
  459. channel = cmd->device->channel - NVIRT_CHAN;
  460. target = cmd->device->id;
  461. }
  462. }
  463. if(islogical) {
  464. /* have just LUN 0 for each target on virtual channels */
  465. if (cmd->device->lun) {
  466. cmd->result = (DID_BAD_TARGET << 16);
  467. cmd->scsi_done(cmd);
  468. return NULL;
  469. }
  470. ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
  471. max_ldrv_num = (adapter->flag & BOARD_40LD) ?
  472. MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
  473. /*
  474. * max_ldrv_num increases by 0x80 if some logical drive was
  475. * deleted.
  476. */
  477. if(adapter->read_ldidmap)
  478. max_ldrv_num += 0x80;
  479. if(ldrv_num > max_ldrv_num ) {
  480. cmd->result = (DID_BAD_TARGET << 16);
  481. cmd->scsi_done(cmd);
  482. return NULL;
  483. }
  484. }
  485. else {
  486. if( cmd->device->lun > 7) {
  487. /*
  488. * Do not support lun >7 for physically accessed
  489. * devices
  490. */
  491. cmd->result = (DID_BAD_TARGET << 16);
  492. cmd->scsi_done(cmd);
  493. return NULL;
  494. }
  495. }
  496. /*
  497. *
  498. * Logical drive commands
  499. *
  500. */
  501. if(islogical) {
  502. switch (cmd->cmnd[0]) {
  503. case TEST_UNIT_READY:
  504. #if MEGA_HAVE_CLUSTERING
  505. /*
  506. * Do we support clustering and is the support enabled
  507. * If no, return success always
  508. */
  509. if( !adapter->has_cluster ) {
  510. cmd->result = (DID_OK << 16);
  511. cmd->scsi_done(cmd);
  512. return NULL;
  513. }
  514. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  515. *busy = 1;
  516. return NULL;
  517. }
  518. scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  519. scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
  520. scb->raw_mbox[3] = ldrv_num;
  521. scb->dma_direction = PCI_DMA_NONE;
  522. return scb;
  523. #else
  524. cmd->result = (DID_OK << 16);
  525. cmd->scsi_done(cmd);
  526. return NULL;
  527. #endif
  528. case MODE_SENSE: {
  529. char *buf;
  530. if (cmd->use_sg) {
  531. struct scatterlist *sg;
  532. sg = (struct scatterlist *)cmd->request_buffer;
  533. buf = kmap_atomic(sg->page, KM_IRQ0) +
  534. sg->offset;
  535. } else
  536. buf = cmd->request_buffer;
  537. memset(cmd->request_buffer, 0, cmd->cmnd[4]);
  538. if (cmd->use_sg) {
  539. struct scatterlist *sg;
  540. sg = (struct scatterlist *)cmd->request_buffer;
  541. kunmap_atomic(buf - sg->offset, KM_IRQ0);
  542. }
  543. cmd->result = (DID_OK << 16);
  544. cmd->scsi_done(cmd);
  545. return NULL;
  546. }
  547. case READ_CAPACITY:
  548. case INQUIRY:
  549. if(!(adapter->flag & (1L << cmd->device->channel))) {
  550. printk(KERN_NOTICE
  551. "scsi%d: scanning scsi channel %d ",
  552. adapter->host->host_no,
  553. cmd->device->channel);
  554. printk("for logical drives.\n");
  555. adapter->flag |= (1L << cmd->device->channel);
  556. }
  557. /* Allocate a SCB and initialize passthru */
  558. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  559. *busy = 1;
  560. return NULL;
  561. }
  562. pthru = scb->pthru;
  563. mbox = (mbox_t *)scb->raw_mbox;
  564. memset(mbox, 0, sizeof(scb->raw_mbox));
  565. memset(pthru, 0, sizeof(mega_passthru));
  566. pthru->timeout = 0;
  567. pthru->ars = 1;
  568. pthru->reqsenselen = 14;
  569. pthru->islogical = 1;
  570. pthru->logdrv = ldrv_num;
  571. pthru->cdblen = cmd->cmd_len;
  572. memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  573. if( adapter->has_64bit_addr ) {
  574. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  575. }
  576. else {
  577. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  578. }
  579. scb->dma_direction = PCI_DMA_FROMDEVICE;
  580. pthru->numsgelements = mega_build_sglist(adapter, scb,
  581. &pthru->dataxferaddr, &pthru->dataxferlen);
  582. mbox->m_out.xferaddr = scb->pthru_dma_addr;
  583. return scb;
  584. case READ_6:
  585. case WRITE_6:
  586. case READ_10:
  587. case WRITE_10:
  588. case READ_12:
  589. case WRITE_12:
  590. /* Allocate a SCB and initialize mailbox */
  591. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  592. *busy = 1;
  593. return NULL;
  594. }
  595. mbox = (mbox_t *)scb->raw_mbox;
  596. memset(mbox, 0, sizeof(scb->raw_mbox));
  597. mbox->m_out.logdrv = ldrv_num;
  598. /*
  599. * A little hack: 2nd bit is zero for all scsi read
  600. * commands and is set for all scsi write commands
  601. */
  602. if( adapter->has_64bit_addr ) {
  603. mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  604. MEGA_MBOXCMD_LWRITE64:
  605. MEGA_MBOXCMD_LREAD64 ;
  606. }
  607. else {
  608. mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  609. MEGA_MBOXCMD_LWRITE:
  610. MEGA_MBOXCMD_LREAD ;
  611. }
  612. /*
  613. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  614. */
  615. if( cmd->cmd_len == 6 ) {
  616. mbox->m_out.numsectors = (u32) cmd->cmnd[4];
  617. mbox->m_out.lba =
  618. ((u32)cmd->cmnd[1] << 16) |
  619. ((u32)cmd->cmnd[2] << 8) |
  620. (u32)cmd->cmnd[3];
  621. mbox->m_out.lba &= 0x1FFFFF;
  622. #if MEGA_HAVE_STATS
  623. /*
  624. * Take modulo 0x80, since the logical drive
  625. * number increases by 0x80 when a logical
  626. * drive was deleted
  627. */
  628. if (*cmd->cmnd == READ_6) {
  629. adapter->nreads[ldrv_num%0x80]++;
  630. adapter->nreadblocks[ldrv_num%0x80] +=
  631. mbox->m_out.numsectors;
  632. } else {
  633. adapter->nwrites[ldrv_num%0x80]++;
  634. adapter->nwriteblocks[ldrv_num%0x80] +=
  635. mbox->m_out.numsectors;
  636. }
  637. #endif
  638. }
  639. /*
  640. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  641. */
  642. if( cmd->cmd_len == 10 ) {
  643. mbox->m_out.numsectors =
  644. (u32)cmd->cmnd[8] |
  645. ((u32)cmd->cmnd[7] << 8);
  646. mbox->m_out.lba =
  647. ((u32)cmd->cmnd[2] << 24) |
  648. ((u32)cmd->cmnd[3] << 16) |
  649. ((u32)cmd->cmnd[4] << 8) |
  650. (u32)cmd->cmnd[5];
  651. #if MEGA_HAVE_STATS
  652. if (*cmd->cmnd == READ_10) {
  653. adapter->nreads[ldrv_num%0x80]++;
  654. adapter->nreadblocks[ldrv_num%0x80] +=
  655. mbox->m_out.numsectors;
  656. } else {
  657. adapter->nwrites[ldrv_num%0x80]++;
  658. adapter->nwriteblocks[ldrv_num%0x80] +=
  659. mbox->m_out.numsectors;
  660. }
  661. #endif
  662. }
  663. /*
  664. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  665. */
  666. if( cmd->cmd_len == 12 ) {
  667. mbox->m_out.lba =
  668. ((u32)cmd->cmnd[2] << 24) |
  669. ((u32)cmd->cmnd[3] << 16) |
  670. ((u32)cmd->cmnd[4] << 8) |
  671. (u32)cmd->cmnd[5];
  672. mbox->m_out.numsectors =
  673. ((u32)cmd->cmnd[6] << 24) |
  674. ((u32)cmd->cmnd[7] << 16) |
  675. ((u32)cmd->cmnd[8] << 8) |
  676. (u32)cmd->cmnd[9];
  677. #if MEGA_HAVE_STATS
  678. if (*cmd->cmnd == READ_12) {
  679. adapter->nreads[ldrv_num%0x80]++;
  680. adapter->nreadblocks[ldrv_num%0x80] +=
  681. mbox->m_out.numsectors;
  682. } else {
  683. adapter->nwrites[ldrv_num%0x80]++;
  684. adapter->nwriteblocks[ldrv_num%0x80] +=
  685. mbox->m_out.numsectors;
  686. }
  687. #endif
  688. }
  689. /*
  690. * If it is a read command
  691. */
  692. if( (*cmd->cmnd & 0x0F) == 0x08 ) {
  693. scb->dma_direction = PCI_DMA_FROMDEVICE;
  694. }
  695. else {
  696. scb->dma_direction = PCI_DMA_TODEVICE;
  697. }
  698. /* Calculate Scatter-Gather info */
  699. mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
  700. (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
  701. return scb;
  702. #if MEGA_HAVE_CLUSTERING
  703. case RESERVE: /* Fall through */
  704. case RELEASE:
  705. /*
  706. * Do we support clustering and is the support enabled
  707. */
  708. if( ! adapter->has_cluster ) {
  709. cmd->result = (DID_BAD_TARGET << 16);
  710. cmd->scsi_done(cmd);
  711. return NULL;
  712. }
  713. /* Allocate a SCB and initialize mailbox */
  714. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  715. *busy = 1;
  716. return NULL;
  717. }
  718. scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  719. scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
  720. MEGA_RESERVE_LD : MEGA_RELEASE_LD;
  721. scb->raw_mbox[3] = ldrv_num;
  722. scb->dma_direction = PCI_DMA_NONE;
  723. return scb;
  724. #endif
  725. default:
  726. cmd->result = (DID_BAD_TARGET << 16);
  727. cmd->scsi_done(cmd);
  728. return NULL;
  729. }
  730. }
  731. /*
  732. * Passthru drive commands
  733. */
  734. else {
  735. /* Allocate a SCB and initialize passthru */
  736. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  737. *busy = 1;
  738. return NULL;
  739. }
  740. mbox = (mbox_t *)scb->raw_mbox;
  741. memset(mbox, 0, sizeof(scb->raw_mbox));
  742. if( adapter->support_ext_cdb ) {
  743. epthru = mega_prepare_extpassthru(adapter, scb, cmd,
  744. channel, target);
  745. mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
  746. mbox->m_out.xferaddr = scb->epthru_dma_addr;
  747. }
  748. else {
  749. pthru = mega_prepare_passthru(adapter, scb, cmd,
  750. channel, target);
  751. /* Initialize mailbox */
  752. if( adapter->has_64bit_addr ) {
  753. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  754. }
  755. else {
  756. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  757. }
  758. mbox->m_out.xferaddr = scb->pthru_dma_addr;
  759. }
  760. return scb;
  761. }
  762. return NULL;
  763. }
  764. /**
  765. * mega_prepare_passthru()
  766. * @adapter - pointer to our soft state
  767. * @scb - our scsi control block
  768. * @cmd - scsi command from the mid-layer
  769. * @channel - actual channel on the controller
  770. * @target - actual id on the controller.
  771. *
  772. * prepare a command for the scsi physical devices.
  773. */
  774. static mega_passthru *
  775. mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  776. int channel, int target)
  777. {
  778. mega_passthru *pthru;
  779. pthru = scb->pthru;
  780. memset(pthru, 0, sizeof (mega_passthru));
  781. /* 0=6sec/1=60sec/2=10min/3=3hrs */
  782. pthru->timeout = 2;
  783. pthru->ars = 1;
  784. pthru->reqsenselen = 14;
  785. pthru->islogical = 0;
  786. pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  787. pthru->target = (adapter->flag & BOARD_40LD) ?
  788. (channel << 4) | target : target;
  789. pthru->cdblen = cmd->cmd_len;
  790. pthru->logdrv = cmd->device->lun;
  791. memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  792. /* Not sure about the direction */
  793. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  794. /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
  795. switch (cmd->cmnd[0]) {
  796. case INQUIRY:
  797. case READ_CAPACITY:
  798. if(!(adapter->flag & (1L << cmd->device->channel))) {
  799. printk(KERN_NOTICE
  800. "scsi%d: scanning scsi channel %d [P%d] ",
  801. adapter->host->host_no,
  802. cmd->device->channel, channel);
  803. printk("for physical devices.\n");
  804. adapter->flag |= (1L << cmd->device->channel);
  805. }
  806. /* Fall through */
  807. default:
  808. pthru->numsgelements = mega_build_sglist(adapter, scb,
  809. &pthru->dataxferaddr, &pthru->dataxferlen);
  810. break;
  811. }
  812. return pthru;
  813. }
  814. /**
  815. * mega_prepare_extpassthru()
  816. * @adapter - pointer to our soft state
  817. * @scb - our scsi control block
  818. * @cmd - scsi command from the mid-layer
  819. * @channel - actual channel on the controller
  820. * @target - actual id on the controller.
  821. *
  822. * prepare a command for the scsi physical devices. This rountine prepares
  823. * commands for devices which can take extended CDBs (>10 bytes)
  824. */
  825. static mega_ext_passthru *
  826. mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  827. int channel, int target)
  828. {
  829. mega_ext_passthru *epthru;
  830. epthru = scb->epthru;
  831. memset(epthru, 0, sizeof(mega_ext_passthru));
  832. /* 0=6sec/1=60sec/2=10min/3=3hrs */
  833. epthru->timeout = 2;
  834. epthru->ars = 1;
  835. epthru->reqsenselen = 14;
  836. epthru->islogical = 0;
  837. epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  838. epthru->target = (adapter->flag & BOARD_40LD) ?
  839. (channel << 4) | target : target;
  840. epthru->cdblen = cmd->cmd_len;
  841. epthru->logdrv = cmd->device->lun;
  842. memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
  843. /* Not sure about the direction */
  844. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  845. switch(cmd->cmnd[0]) {
  846. case INQUIRY:
  847. case READ_CAPACITY:
  848. if(!(adapter->flag & (1L << cmd->device->channel))) {
  849. printk(KERN_NOTICE
  850. "scsi%d: scanning scsi channel %d [P%d] ",
  851. adapter->host->host_no,
  852. cmd->device->channel, channel);
  853. printk("for physical devices.\n");
  854. adapter->flag |= (1L << cmd->device->channel);
  855. }
  856. /* Fall through */
  857. default:
  858. epthru->numsgelements = mega_build_sglist(adapter, scb,
  859. &epthru->dataxferaddr, &epthru->dataxferlen);
  860. break;
  861. }
  862. return epthru;
  863. }
  864. static void
  865. __mega_runpendq(adapter_t *adapter)
  866. {
  867. scb_t *scb;
  868. struct list_head *pos, *next;
  869. /* Issue any pending commands to the card */
  870. list_for_each_safe(pos, next, &adapter->pending_list) {
  871. scb = list_entry(pos, scb_t, list);
  872. if( !(scb->state & SCB_ISSUED) ) {
  873. if( issue_scb(adapter, scb) != 0 )
  874. return;
  875. }
  876. }
  877. return;
  878. }
  879. /**
  880. * issue_scb()
  881. * @adapter - pointer to our soft state
  882. * @scb - scsi control block
  883. *
  884. * Post a command to the card if the mailbox is available, otherwise return
  885. * busy. We also take the scb from the pending list if the mailbox is
  886. * available.
  887. */
  888. static int
  889. issue_scb(adapter_t *adapter, scb_t *scb)
  890. {
  891. volatile mbox64_t *mbox64 = adapter->mbox64;
  892. volatile mbox_t *mbox = adapter->mbox;
  893. unsigned int i = 0;
  894. if(unlikely(mbox->m_in.busy)) {
  895. do {
  896. udelay(1);
  897. i++;
  898. } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
  899. if(mbox->m_in.busy) return -1;
  900. }
  901. /* Copy mailbox data into host structure */
  902. memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
  903. sizeof(struct mbox_out));
  904. mbox->m_out.cmdid = scb->idx; /* Set cmdid */
  905. mbox->m_in.busy = 1; /* Set busy */
  906. /*
  907. * Increment the pending queue counter
  908. */
  909. atomic_inc(&adapter->pend_cmds);
  910. switch (mbox->m_out.cmd) {
  911. case MEGA_MBOXCMD_LREAD64:
  912. case MEGA_MBOXCMD_LWRITE64:
  913. case MEGA_MBOXCMD_PASSTHRU64:
  914. case MEGA_MBOXCMD_EXTPTHRU:
  915. mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  916. mbox64->xfer_segment_hi = 0;
  917. mbox->m_out.xferaddr = 0xFFFFFFFF;
  918. break;
  919. default:
  920. mbox64->xfer_segment_lo = 0;
  921. mbox64->xfer_segment_hi = 0;
  922. }
  923. /*
  924. * post the command
  925. */
  926. scb->state |= SCB_ISSUED;
  927. if( likely(adapter->flag & BOARD_MEMMAP) ) {
  928. mbox->m_in.poll = 0;
  929. mbox->m_in.ack = 0;
  930. WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  931. }
  932. else {
  933. irq_enable(adapter);
  934. issue_command(adapter);
  935. }
  936. return 0;
  937. }
  938. /*
  939. * Wait until the controller's mailbox is available
  940. */
  941. static inline int
  942. mega_busywait_mbox (adapter_t *adapter)
  943. {
  944. if (adapter->mbox->m_in.busy)
  945. return __mega_busywait_mbox(adapter);
  946. return 0;
  947. }
  948. /**
  949. * issue_scb_block()
  950. * @adapter - pointer to our soft state
  951. * @raw_mbox - the mailbox
  952. *
  953. * Issue a scb in synchronous and non-interrupt mode
  954. */
  955. static int
  956. issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
  957. {
  958. volatile mbox64_t *mbox64 = adapter->mbox64;
  959. volatile mbox_t *mbox = adapter->mbox;
  960. u8 byte;
  961. /* Wait until mailbox is free */
  962. if(mega_busywait_mbox (adapter))
  963. goto bug_blocked_mailbox;
  964. /* Copy mailbox data into host structure */
  965. memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
  966. mbox->m_out.cmdid = 0xFE;
  967. mbox->m_in.busy = 1;
  968. switch (raw_mbox[0]) {
  969. case MEGA_MBOXCMD_LREAD64:
  970. case MEGA_MBOXCMD_LWRITE64:
  971. case MEGA_MBOXCMD_PASSTHRU64:
  972. case MEGA_MBOXCMD_EXTPTHRU:
  973. mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  974. mbox64->xfer_segment_hi = 0;
  975. mbox->m_out.xferaddr = 0xFFFFFFFF;
  976. break;
  977. default:
  978. mbox64->xfer_segment_lo = 0;
  979. mbox64->xfer_segment_hi = 0;
  980. }
  981. if( likely(adapter->flag & BOARD_MEMMAP) ) {
  982. mbox->m_in.poll = 0;
  983. mbox->m_in.ack = 0;
  984. mbox->m_in.numstatus = 0xFF;
  985. mbox->m_in.status = 0xFF;
  986. WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  987. while((volatile u8)mbox->m_in.numstatus == 0xFF)
  988. cpu_relax();
  989. mbox->m_in.numstatus = 0xFF;
  990. while( (volatile u8)mbox->m_in.poll != 0x77 )
  991. cpu_relax();
  992. mbox->m_in.poll = 0;
  993. mbox->m_in.ack = 0x77;
  994. WRINDOOR(adapter, adapter->mbox_dma | 0x2);
  995. while(RDINDOOR(adapter) & 0x2)
  996. cpu_relax();
  997. }
  998. else {
  999. irq_disable(adapter);
  1000. issue_command(adapter);
  1001. while (!((byte = irq_state(adapter)) & INTR_VALID))
  1002. cpu_relax();
  1003. set_irq_state(adapter, byte);
  1004. irq_enable(adapter);
  1005. irq_ack(adapter);
  1006. }
  1007. return mbox->m_in.status;
  1008. bug_blocked_mailbox:
  1009. printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
  1010. udelay (1000);
  1011. return -1;
  1012. }
  1013. /**
  1014. * megaraid_isr_iomapped()
  1015. * @irq - irq
  1016. * @devp - pointer to our soft state
  1017. * @regs - unused
  1018. *
  1019. * Interrupt service routine for io-mapped controllers.
  1020. * Find out if our device is interrupting. If yes, acknowledge the interrupt
  1021. * and service the completed commands.
  1022. */
  1023. static irqreturn_t
  1024. megaraid_isr_iomapped(int irq, void *devp, struct pt_regs *regs)
  1025. {
  1026. adapter_t *adapter = devp;
  1027. unsigned long flags;
  1028. u8 status;
  1029. u8 nstatus;
  1030. u8 completed[MAX_FIRMWARE_STATUS];
  1031. u8 byte;
  1032. int handled = 0;
  1033. /*
  1034. * loop till F/W has more commands for us to complete.
  1035. */
  1036. spin_lock_irqsave(&adapter->lock, flags);
  1037. do {
  1038. /* Check if a valid interrupt is pending */
  1039. byte = irq_state(adapter);
  1040. if( (byte & VALID_INTR_BYTE) == 0 ) {
  1041. /*
  1042. * No more pending commands
  1043. */
  1044. goto out_unlock;
  1045. }
  1046. set_irq_state(adapter, byte);
  1047. while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  1048. == 0xFF)
  1049. cpu_relax();
  1050. adapter->mbox->m_in.numstatus = 0xFF;
  1051. status = adapter->mbox->m_in.status;
  1052. /*
  1053. * decrement the pending queue counter
  1054. */
  1055. atomic_sub(nstatus, &adapter->pend_cmds);
  1056. memcpy(completed, (void *)adapter->mbox->m_in.completed,
  1057. nstatus);
  1058. /* Acknowledge interrupt */
  1059. irq_ack(adapter);
  1060. mega_cmd_done(adapter, completed, nstatus, status);
  1061. mega_rundoneq(adapter);
  1062. handled = 1;
  1063. /* Loop through any pending requests */
  1064. if(atomic_read(&adapter->quiescent) == 0) {
  1065. mega_runpendq(adapter);
  1066. }
  1067. } while(1);
  1068. out_unlock:
  1069. spin_unlock_irqrestore(&adapter->lock, flags);
  1070. return IRQ_RETVAL(handled);
  1071. }
  1072. /**
  1073. * megaraid_isr_memmapped()
  1074. * @irq - irq
  1075. * @devp - pointer to our soft state
  1076. * @regs - unused
  1077. *
  1078. * Interrupt service routine for memory-mapped controllers.
  1079. * Find out if our device is interrupting. If yes, acknowledge the interrupt
  1080. * and service the completed commands.
  1081. */
  1082. static irqreturn_t
  1083. megaraid_isr_memmapped(int irq, void *devp, struct pt_regs *regs)
  1084. {
  1085. adapter_t *adapter = devp;
  1086. unsigned long flags;
  1087. u8 status;
  1088. u32 dword = 0;
  1089. u8 nstatus;
  1090. u8 completed[MAX_FIRMWARE_STATUS];
  1091. int handled = 0;
  1092. /*
  1093. * loop till F/W has more commands for us to complete.
  1094. */
  1095. spin_lock_irqsave(&adapter->lock, flags);
  1096. do {
  1097. /* Check if a valid interrupt is pending */
  1098. dword = RDOUTDOOR(adapter);
  1099. if(dword != 0x10001234) {
  1100. /*
  1101. * No more pending commands
  1102. */
  1103. goto out_unlock;
  1104. }
  1105. WROUTDOOR(adapter, 0x10001234);
  1106. while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  1107. == 0xFF) {
  1108. cpu_relax();
  1109. }
  1110. adapter->mbox->m_in.numstatus = 0xFF;
  1111. status = adapter->mbox->m_in.status;
  1112. /*
  1113. * decrement the pending queue counter
  1114. */
  1115. atomic_sub(nstatus, &adapter->pend_cmds);
  1116. memcpy(completed, (void *)adapter->mbox->m_in.completed,
  1117. nstatus);
  1118. /* Acknowledge interrupt */
  1119. WRINDOOR(adapter, 0x2);
  1120. handled = 1;
  1121. while( RDINDOOR(adapter) & 0x02 ) cpu_relax();
  1122. mega_cmd_done(adapter, completed, nstatus, status);
  1123. mega_rundoneq(adapter);
  1124. /* Loop through any pending requests */
  1125. if(atomic_read(&adapter->quiescent) == 0) {
  1126. mega_runpendq(adapter);
  1127. }
  1128. } while(1);
  1129. out_unlock:
  1130. spin_unlock_irqrestore(&adapter->lock, flags);
  1131. return IRQ_RETVAL(handled);
  1132. }
  1133. /**
  1134. * mega_cmd_done()
  1135. * @adapter - pointer to our soft state
  1136. * @completed - array of ids of completed commands
  1137. * @nstatus - number of completed commands
  1138. * @status - status of the last command completed
  1139. *
  1140. * Complete the comamnds and call the scsi mid-layer callback hooks.
  1141. */
  1142. static void
  1143. mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
  1144. {
  1145. mega_ext_passthru *epthru = NULL;
  1146. struct scatterlist *sgl;
  1147. Scsi_Cmnd *cmd = NULL;
  1148. mega_passthru *pthru = NULL;
  1149. mbox_t *mbox = NULL;
  1150. u8 c;
  1151. scb_t *scb;
  1152. int islogical;
  1153. int cmdid;
  1154. int i;
  1155. /*
  1156. * for all the commands completed, call the mid-layer callback routine
  1157. * and free the scb.
  1158. */
  1159. for( i = 0; i < nstatus; i++ ) {
  1160. cmdid = completed[i];
  1161. if( cmdid == CMDID_INT_CMDS ) { /* internal command */
  1162. scb = &adapter->int_scb;
  1163. cmd = scb->cmd;
  1164. mbox = (mbox_t *)scb->raw_mbox;
  1165. /*
  1166. * Internal command interface do not fire the extended
  1167. * passthru or 64-bit passthru
  1168. */
  1169. pthru = scb->pthru;
  1170. }
  1171. else {
  1172. scb = &adapter->scb_list[cmdid];
  1173. /*
  1174. * Make sure f/w has completed a valid command
  1175. */
  1176. if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
  1177. printk(KERN_CRIT
  1178. "megaraid: invalid command ");
  1179. printk("Id %d, scb->state:%x, scsi cmd:%p\n",
  1180. cmdid, scb->state, scb->cmd);
  1181. continue;
  1182. }
  1183. /*
  1184. * Was a abort issued for this command
  1185. */
  1186. if( scb->state & SCB_ABORT ) {
  1187. printk(KERN_WARNING
  1188. "megaraid: aborted cmd %lx[%x] complete.\n",
  1189. scb->cmd->serial_number, scb->idx);
  1190. scb->cmd->result = (DID_ABORT << 16);
  1191. list_add_tail(SCSI_LIST(scb->cmd),
  1192. &adapter->completed_list);
  1193. mega_free_scb(adapter, scb);
  1194. continue;
  1195. }
  1196. /*
  1197. * Was a reset issued for this command
  1198. */
  1199. if( scb->state & SCB_RESET ) {
  1200. printk(KERN_WARNING
  1201. "megaraid: reset cmd %lx[%x] complete.\n",
  1202. scb->cmd->serial_number, scb->idx);
  1203. scb->cmd->result = (DID_RESET << 16);
  1204. list_add_tail(SCSI_LIST(scb->cmd),
  1205. &adapter->completed_list);
  1206. mega_free_scb (adapter, scb);
  1207. continue;
  1208. }
  1209. cmd = scb->cmd;
  1210. pthru = scb->pthru;
  1211. epthru = scb->epthru;
  1212. mbox = (mbox_t *)scb->raw_mbox;
  1213. #if MEGA_HAVE_STATS
  1214. {
  1215. int logdrv = mbox->m_out.logdrv;
  1216. islogical = adapter->logdrv_chan[cmd->channel];
  1217. /*
  1218. * Maintain an error counter for the logical drive.
  1219. * Some application like SNMP agent need such
  1220. * statistics
  1221. */
  1222. if( status && islogical && (cmd->cmnd[0] == READ_6 ||
  1223. cmd->cmnd[0] == READ_10 ||
  1224. cmd->cmnd[0] == READ_12)) {
  1225. /*
  1226. * Logical drive number increases by 0x80 when
  1227. * a logical drive is deleted
  1228. */
  1229. adapter->rd_errors[logdrv%0x80]++;
  1230. }
  1231. if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
  1232. cmd->cmnd[0] == WRITE_10 ||
  1233. cmd->cmnd[0] == WRITE_12)) {
  1234. /*
  1235. * Logical drive number increases by 0x80 when
  1236. * a logical drive is deleted
  1237. */
  1238. adapter->wr_errors[logdrv%0x80]++;
  1239. }
  1240. }
  1241. #endif
  1242. }
  1243. /*
  1244. * Do not return the presence of hard disk on the channel so,
  1245. * inquiry sent, and returned data==hard disk or removable
  1246. * hard disk and not logical, request should return failure! -
  1247. * PJ
  1248. */
  1249. islogical = adapter->logdrv_chan[cmd->device->channel];
  1250. if( cmd->cmnd[0] == INQUIRY && !islogical ) {
  1251. if( cmd->use_sg ) {
  1252. sgl = (struct scatterlist *)
  1253. cmd->request_buffer;
  1254. if( sgl->page ) {
  1255. c = *(unsigned char *)
  1256. page_address((&sgl[0])->page) +
  1257. (&sgl[0])->offset;
  1258. }
  1259. else {
  1260. printk(KERN_WARNING
  1261. "megaraid: invalid sg.\n");
  1262. c = 0;
  1263. }
  1264. }
  1265. else {
  1266. c = *(u8 *)cmd->request_buffer;
  1267. }
  1268. if(IS_RAID_CH(adapter, cmd->device->channel) &&
  1269. ((c & 0x1F ) == TYPE_DISK)) {
  1270. status = 0xF0;
  1271. }
  1272. }
  1273. /* clear result; otherwise, success returns corrupt value */
  1274. cmd->result = 0;
  1275. /* Convert MegaRAID status to Linux error code */
  1276. switch (status) {
  1277. case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
  1278. cmd->result |= (DID_OK << 16);
  1279. break;
  1280. case 0x02: /* ERROR_ABORTED, i.e.
  1281. SCSI_STATUS_CHECK_CONDITION */
  1282. /* set sense_buffer and result fields */
  1283. if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
  1284. mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
  1285. memcpy(cmd->sense_buffer, pthru->reqsensearea,
  1286. 14);
  1287. cmd->result = (DRIVER_SENSE << 24) |
  1288. (DID_OK << 16) |
  1289. (CHECK_CONDITION << 1);
  1290. }
  1291. else {
  1292. if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
  1293. memcpy(cmd->sense_buffer,
  1294. epthru->reqsensearea, 14);
  1295. cmd->result = (DRIVER_SENSE << 24) |
  1296. (DID_OK << 16) |
  1297. (CHECK_CONDITION << 1);
  1298. } else {
  1299. cmd->sense_buffer[0] = 0x70;
  1300. cmd->sense_buffer[2] = ABORTED_COMMAND;
  1301. cmd->result |= (CHECK_CONDITION << 1);
  1302. }
  1303. }
  1304. break;
  1305. case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
  1306. SCSI_STATUS_BUSY */
  1307. cmd->result |= (DID_BUS_BUSY << 16) | status;
  1308. break;
  1309. default:
  1310. #if MEGA_HAVE_CLUSTERING
  1311. /*
  1312. * If TEST_UNIT_READY fails, we know
  1313. * MEGA_RESERVATION_STATUS failed
  1314. */
  1315. if( cmd->cmnd[0] == TEST_UNIT_READY ) {
  1316. cmd->result |= (DID_ERROR << 16) |
  1317. (RESERVATION_CONFLICT << 1);
  1318. }
  1319. else
  1320. /*
  1321. * Error code returned is 1 if Reserve or Release
  1322. * failed or the input parameter is invalid
  1323. */
  1324. if( status == 1 &&
  1325. (cmd->cmnd[0] == RESERVE ||
  1326. cmd->cmnd[0] == RELEASE) ) {
  1327. cmd->result |= (DID_ERROR << 16) |
  1328. (RESERVATION_CONFLICT << 1);
  1329. }
  1330. else
  1331. #endif
  1332. cmd->result |= (DID_BAD_TARGET << 16)|status;
  1333. }
  1334. /*
  1335. * Only free SCBs for the commands coming down from the
  1336. * mid-layer, not for which were issued internally
  1337. *
  1338. * For internal command, restore the status returned by the
  1339. * firmware so that user can interpret it.
  1340. */
  1341. if( cmdid == CMDID_INT_CMDS ) { /* internal command */
  1342. cmd->result = status;
  1343. /*
  1344. * Remove the internal command from the pending list
  1345. */
  1346. list_del_init(&scb->list);
  1347. scb->state = SCB_FREE;
  1348. }
  1349. else {
  1350. mega_free_scb(adapter, scb);
  1351. }
  1352. /* Add Scsi_Command to end of completed queue */
  1353. list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
  1354. }
  1355. }
  1356. /*
  1357. * mega_runpendq()
  1358. *
  1359. * Run through the list of completed requests and finish it
  1360. */
  1361. static void
  1362. mega_rundoneq (adapter_t *adapter)
  1363. {
  1364. Scsi_Cmnd *cmd;
  1365. struct list_head *pos;
  1366. list_for_each(pos, &adapter->completed_list) {
  1367. struct scsi_pointer* spos = (struct scsi_pointer *)pos;
  1368. cmd = list_entry(spos, Scsi_Cmnd, SCp);
  1369. cmd->scsi_done(cmd);
  1370. }
  1371. INIT_LIST_HEAD(&adapter->completed_list);
  1372. }
  1373. /*
  1374. * Free a SCB structure
  1375. * Note: We assume the scsi commands associated with this scb is not free yet.
  1376. */
  1377. static void
  1378. mega_free_scb(adapter_t *adapter, scb_t *scb)
  1379. {
  1380. unsigned long length;
  1381. switch( scb->dma_type ) {
  1382. case MEGA_DMA_TYPE_NONE:
  1383. break;
  1384. case MEGA_BULK_DATA:
  1385. if (scb->cmd->use_sg == 0)
  1386. length = scb->cmd->request_bufflen;
  1387. else {
  1388. struct scatterlist *sgl =
  1389. (struct scatterlist *)scb->cmd->request_buffer;
  1390. length = sgl->length;
  1391. }
  1392. pci_unmap_page(adapter->dev, scb->dma_h_bulkdata,
  1393. length, scb->dma_direction);
  1394. break;
  1395. case MEGA_SGLIST:
  1396. pci_unmap_sg(adapter->dev, scb->cmd->request_buffer,
  1397. scb->cmd->use_sg, scb->dma_direction);
  1398. break;
  1399. default:
  1400. break;
  1401. }
  1402. /*
  1403. * Remove from the pending list
  1404. */
  1405. list_del_init(&scb->list);
  1406. /* Link the scb back into free list */
  1407. scb->state = SCB_FREE;
  1408. scb->cmd = NULL;
  1409. list_add(&scb->list, &adapter->free_list);
  1410. }
  1411. static int
  1412. __mega_busywait_mbox (adapter_t *adapter)
  1413. {
  1414. volatile mbox_t *mbox = adapter->mbox;
  1415. long counter;
  1416. for (counter = 0; counter < 10000; counter++) {
  1417. if (!mbox->m_in.busy)
  1418. return 0;
  1419. udelay(100); yield();
  1420. }
  1421. return -1; /* give up after 1 second */
  1422. }
  1423. /*
  1424. * Copies data to SGLIST
  1425. * Note: For 64 bit cards, we need a minimum of one SG element for read/write
  1426. */
  1427. static int
  1428. mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
  1429. {
  1430. struct scatterlist *sgl;
  1431. struct page *page;
  1432. unsigned long offset;
  1433. unsigned int length;
  1434. Scsi_Cmnd *cmd;
  1435. int sgcnt;
  1436. int idx;
  1437. cmd = scb->cmd;
  1438. /* Scatter-gather not used */
  1439. if( cmd->use_sg == 0 || (cmd->use_sg == 1 &&
  1440. !adapter->has_64bit_addr)) {
  1441. if (cmd->use_sg == 0) {
  1442. page = virt_to_page(cmd->request_buffer);
  1443. offset = offset_in_page(cmd->request_buffer);
  1444. length = cmd->request_bufflen;
  1445. } else {
  1446. sgl = (struct scatterlist *)cmd->request_buffer;
  1447. page = sgl->page;
  1448. offset = sgl->offset;
  1449. length = sgl->length;
  1450. }
  1451. scb->dma_h_bulkdata = pci_map_page(adapter->dev,
  1452. page, offset,
  1453. length,
  1454. scb->dma_direction);
  1455. scb->dma_type = MEGA_BULK_DATA;
  1456. /*
  1457. * We need to handle special 64-bit commands that need a
  1458. * minimum of 1 SG
  1459. */
  1460. if( adapter->has_64bit_addr ) {
  1461. scb->sgl64[0].address = scb->dma_h_bulkdata;
  1462. scb->sgl64[0].length = length;
  1463. *buf = (u32)scb->sgl_dma_addr;
  1464. *len = (u32)length;
  1465. return 1;
  1466. }
  1467. else {
  1468. *buf = (u32)scb->dma_h_bulkdata;
  1469. *len = (u32)length;
  1470. }
  1471. return 0;
  1472. }
  1473. sgl = (struct scatterlist *)cmd->request_buffer;
  1474. /*
  1475. * Copy Scatter-Gather list info into controller structure.
  1476. *
  1477. * The number of sg elements returned must not exceed our limit
  1478. */
  1479. sgcnt = pci_map_sg(adapter->dev, sgl, cmd->use_sg,
  1480. scb->dma_direction);
  1481. scb->dma_type = MEGA_SGLIST;
  1482. if( sgcnt > adapter->sglen ) BUG();
  1483. *len = 0;
  1484. for( idx = 0; idx < sgcnt; idx++, sgl++ ) {
  1485. if( adapter->has_64bit_addr ) {
  1486. scb->sgl64[idx].address = sg_dma_address(sgl);
  1487. *len += scb->sgl64[idx].length = sg_dma_len(sgl);
  1488. }
  1489. else {
  1490. scb->sgl[idx].address = sg_dma_address(sgl);
  1491. *len += scb->sgl[idx].length = sg_dma_len(sgl);
  1492. }
  1493. }
  1494. /* Reset pointer and length fields */
  1495. *buf = scb->sgl_dma_addr;
  1496. /* Return count of SG requests */
  1497. return sgcnt;
  1498. }
  1499. /*
  1500. * mega_8_to_40ld()
  1501. *
  1502. * takes all info in AdapterInquiry structure and puts it into ProductInfo and
  1503. * Enquiry3 structures for later use
  1504. */
  1505. static void
  1506. mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
  1507. mega_product_info *product_info)
  1508. {
  1509. int i;
  1510. product_info->max_commands = inquiry->adapter_info.max_commands;
  1511. enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
  1512. product_info->nchannels = inquiry->adapter_info.nchannels;
  1513. for (i = 0; i < 4; i++) {
  1514. product_info->fw_version[i] =
  1515. inquiry->adapter_info.fw_version[i];
  1516. product_info->bios_version[i] =
  1517. inquiry->adapter_info.bios_version[i];
  1518. }
  1519. enquiry3->cache_flush_interval =
  1520. inquiry->adapter_info.cache_flush_interval;
  1521. product_info->dram_size = inquiry->adapter_info.dram_size;
  1522. enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
  1523. for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
  1524. enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
  1525. enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
  1526. enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
  1527. }
  1528. for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
  1529. enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
  1530. }
  1531. static inline void
  1532. mega_free_sgl(adapter_t *adapter)
  1533. {
  1534. scb_t *scb;
  1535. int i;
  1536. for(i = 0; i < adapter->max_cmds; i++) {
  1537. scb = &adapter->scb_list[i];
  1538. if( scb->sgl64 ) {
  1539. pci_free_consistent(adapter->dev,
  1540. sizeof(mega_sgl64) * adapter->sglen,
  1541. scb->sgl64,
  1542. scb->sgl_dma_addr);
  1543. scb->sgl64 = NULL;
  1544. }
  1545. if( scb->pthru ) {
  1546. pci_free_consistent(adapter->dev, sizeof(mega_passthru),
  1547. scb->pthru, scb->pthru_dma_addr);
  1548. scb->pthru = NULL;
  1549. }
  1550. if( scb->epthru ) {
  1551. pci_free_consistent(adapter->dev,
  1552. sizeof(mega_ext_passthru),
  1553. scb->epthru, scb->epthru_dma_addr);
  1554. scb->epthru = NULL;
  1555. }
  1556. }
  1557. }
  1558. /*
  1559. * Get information about the card/driver
  1560. */
  1561. const char *
  1562. megaraid_info(struct Scsi_Host *host)
  1563. {
  1564. static char buffer[512];
  1565. adapter_t *adapter;
  1566. adapter = (adapter_t *)host->hostdata;
  1567. sprintf (buffer,
  1568. "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
  1569. adapter->fw_version, adapter->product_info.max_commands,
  1570. adapter->host->max_id, adapter->host->max_channel,
  1571. adapter->host->max_lun);
  1572. return buffer;
  1573. }
  1574. /*
  1575. * Abort a previous SCSI request. Only commands on the pending list can be
  1576. * aborted. All the commands issued to the F/W must complete.
  1577. */
  1578. static int
  1579. megaraid_abort(Scsi_Cmnd *cmd)
  1580. {
  1581. adapter_t *adapter;
  1582. int rval;
  1583. adapter = (adapter_t *)cmd->device->host->hostdata;
  1584. rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
  1585. /*
  1586. * This is required here to complete any completed requests
  1587. * to be communicated over to the mid layer.
  1588. */
  1589. mega_rundoneq(adapter);
  1590. return rval;
  1591. }
  1592. static int
  1593. megaraid_reset(struct scsi_cmnd *cmd)
  1594. {
  1595. adapter_t *adapter;
  1596. megacmd_t mc;
  1597. int rval;
  1598. adapter = (adapter_t *)cmd->device->host->hostdata;
  1599. #if MEGA_HAVE_CLUSTERING
  1600. mc.cmd = MEGA_CLUSTER_CMD;
  1601. mc.opcode = MEGA_RESET_RESERVATIONS;
  1602. if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
  1603. printk(KERN_WARNING
  1604. "megaraid: reservation reset failed.\n");
  1605. }
  1606. else {
  1607. printk(KERN_INFO "megaraid: reservation reset.\n");
  1608. }
  1609. #endif
  1610. spin_lock_irq(&adapter->lock);
  1611. rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
  1612. /*
  1613. * This is required here to complete any completed requests
  1614. * to be communicated over to the mid layer.
  1615. */
  1616. mega_rundoneq(adapter);
  1617. spin_unlock_irq(&adapter->lock);
  1618. return rval;
  1619. }
  1620. /**
  1621. * megaraid_abort_and_reset()
  1622. * @adapter - megaraid soft state
  1623. * @cmd - scsi command to be aborted or reset
  1624. * @aor - abort or reset flag
  1625. *
  1626. * Try to locate the scsi command in the pending queue. If found and is not
  1627. * issued to the controller, abort/reset it. Otherwise return failure
  1628. */
  1629. static int
  1630. megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
  1631. {
  1632. struct list_head *pos, *next;
  1633. scb_t *scb;
  1634. printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
  1635. (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
  1636. cmd->cmnd[0], cmd->device->channel,
  1637. cmd->device->id, cmd->device->lun);
  1638. if(list_empty(&adapter->pending_list))
  1639. return FALSE;
  1640. list_for_each_safe(pos, next, &adapter->pending_list) {
  1641. scb = list_entry(pos, scb_t, list);
  1642. if (scb->cmd == cmd) { /* Found command */
  1643. scb->state |= aor;
  1644. /*
  1645. * Check if this command has firmare owenership. If
  1646. * yes, we cannot reset this command. Whenever, f/w
  1647. * completes this command, we will return appropriate
  1648. * status from ISR.
  1649. */
  1650. if( scb->state & SCB_ISSUED ) {
  1651. printk(KERN_WARNING
  1652. "megaraid: %s-%lx[%x], fw owner.\n",
  1653. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1654. cmd->serial_number, scb->idx);
  1655. return FALSE;
  1656. }
  1657. else {
  1658. /*
  1659. * Not yet issued! Remove from the pending
  1660. * list
  1661. */
  1662. printk(KERN_WARNING
  1663. "megaraid: %s-%lx[%x], driver owner.\n",
  1664. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1665. cmd->serial_number, scb->idx);
  1666. mega_free_scb(adapter, scb);
  1667. if( aor == SCB_ABORT ) {
  1668. cmd->result = (DID_ABORT << 16);
  1669. }
  1670. else {
  1671. cmd->result = (DID_RESET << 16);
  1672. }
  1673. list_add_tail(SCSI_LIST(cmd),
  1674. &adapter->completed_list);
  1675. return TRUE;
  1676. }
  1677. }
  1678. }
  1679. return FALSE;
  1680. }
  1681. static inline int
  1682. make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
  1683. {
  1684. *pdev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
  1685. if( *pdev == NULL ) return -1;
  1686. memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
  1687. if( pci_set_dma_mask(*pdev, 0xffffffff) != 0 ) {
  1688. kfree(*pdev);
  1689. return -1;
  1690. }
  1691. return 0;
  1692. }
  1693. static inline void
  1694. free_local_pdev(struct pci_dev *pdev)
  1695. {
  1696. kfree(pdev);
  1697. }
  1698. /**
  1699. * mega_allocate_inquiry()
  1700. * @dma_handle - handle returned for dma address
  1701. * @pdev - handle to pci device
  1702. *
  1703. * allocates memory for inquiry structure
  1704. */
  1705. static inline void *
  1706. mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
  1707. {
  1708. return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
  1709. }
  1710. static inline void
  1711. mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
  1712. {
  1713. pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
  1714. }
  1715. #ifdef CONFIG_PROC_FS
  1716. /* Following code handles /proc fs */
  1717. #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
  1718. S_IRUSR | S_IFREG, \
  1719. controller_proc_dir_entry, \
  1720. func, adapter)
  1721. /**
  1722. * mega_create_proc_entry()
  1723. * @index - index in soft state array
  1724. * @parent - parent node for this /proc entry
  1725. *
  1726. * Creates /proc entries for our controllers.
  1727. */
  1728. static void
  1729. mega_create_proc_entry(int index, struct proc_dir_entry *parent)
  1730. {
  1731. struct proc_dir_entry *controller_proc_dir_entry = NULL;
  1732. u8 string[64] = { 0 };
  1733. adapter_t *adapter = hba_soft_state[index];
  1734. sprintf(string, "hba%d", adapter->host->host_no);
  1735. controller_proc_dir_entry =
  1736. adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
  1737. if(!controller_proc_dir_entry) {
  1738. printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
  1739. return;
  1740. }
  1741. adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
  1742. adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
  1743. adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
  1744. #if MEGA_HAVE_ENH_PROC
  1745. adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
  1746. adapter->proc_battery = CREATE_READ_PROC("battery-status",
  1747. proc_battery);
  1748. /*
  1749. * Display each physical drive on its channel
  1750. */
  1751. adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
  1752. proc_pdrv_ch0);
  1753. adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
  1754. proc_pdrv_ch1);
  1755. adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
  1756. proc_pdrv_ch2);
  1757. adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
  1758. proc_pdrv_ch3);
  1759. /*
  1760. * Display a set of up to 10 logical drive through each of following
  1761. * /proc entries
  1762. */
  1763. adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
  1764. proc_rdrv_10);
  1765. adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
  1766. proc_rdrv_20);
  1767. adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
  1768. proc_rdrv_30);
  1769. adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
  1770. proc_rdrv_40);
  1771. #endif
  1772. }
  1773. /**
  1774. * proc_read_config()
  1775. * @page - buffer to write the data in
  1776. * @start - where the actual data has been written in page
  1777. * @offset - same meaning as the read system call
  1778. * @count - same meaning as the read system call
  1779. * @eof - set if no more data needs to be returned
  1780. * @data - pointer to our soft state
  1781. *
  1782. * Display configuration information about the controller.
  1783. */
  1784. static int
  1785. proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
  1786. void *data)
  1787. {
  1788. adapter_t *adapter = (adapter_t *)data;
  1789. int len = 0;
  1790. len += sprintf(page+len, "%s", MEGARAID_VERSION);
  1791. if(adapter->product_info.product_name[0])
  1792. len += sprintf(page+len, "%s\n",
  1793. adapter->product_info.product_name);
  1794. len += sprintf(page+len, "Controller Type: ");
  1795. if( adapter->flag & BOARD_MEMMAP ) {
  1796. len += sprintf(page+len,
  1797. "438/466/467/471/493/518/520/531/532\n");
  1798. }
  1799. else {
  1800. len += sprintf(page+len,
  1801. "418/428/434\n");
  1802. }
  1803. if(adapter->flag & BOARD_40LD) {
  1804. len += sprintf(page+len,
  1805. "Controller Supports 40 Logical Drives\n");
  1806. }
  1807. if(adapter->flag & BOARD_64BIT) {
  1808. len += sprintf(page+len,
  1809. "Controller capable of 64-bit memory addressing\n");
  1810. }
  1811. if( adapter->has_64bit_addr ) {
  1812. len += sprintf(page+len,
  1813. "Controller using 64-bit memory addressing\n");
  1814. }
  1815. else {
  1816. len += sprintf(page+len,
  1817. "Controller is not using 64-bit memory addressing\n");
  1818. }
  1819. len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
  1820. adapter->host->irq);
  1821. len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
  1822. adapter->numldrv, adapter->product_info.nchannels);
  1823. len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
  1824. adapter->fw_version, adapter->bios_version,
  1825. adapter->product_info.dram_size);
  1826. len += sprintf(page+len,
  1827. "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
  1828. adapter->product_info.max_commands, adapter->max_cmds);
  1829. len += sprintf(page+len, "support_ext_cdb = %d\n",
  1830. adapter->support_ext_cdb);
  1831. len += sprintf(page+len, "support_random_del = %d\n",
  1832. adapter->support_random_del);
  1833. len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
  1834. adapter->boot_ldrv_enabled);
  1835. len += sprintf(page+len, "boot_ldrv = %d\n",
  1836. adapter->boot_ldrv);
  1837. len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
  1838. adapter->boot_pdrv_enabled);
  1839. len += sprintf(page+len, "boot_pdrv_ch = %d\n",
  1840. adapter->boot_pdrv_ch);
  1841. len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
  1842. adapter->boot_pdrv_tgt);
  1843. len += sprintf(page+len, "quiescent = %d\n",
  1844. atomic_read(&adapter->quiescent));
  1845. len += sprintf(page+len, "has_cluster = %d\n",
  1846. adapter->has_cluster);
  1847. len += sprintf(page+len, "\nModule Parameters:\n");
  1848. len += sprintf(page+len, "max_cmd_per_lun = %d\n",
  1849. max_cmd_per_lun);
  1850. len += sprintf(page+len, "max_sectors_per_io = %d\n",
  1851. max_sectors_per_io);
  1852. *eof = 1;
  1853. return len;
  1854. }
  1855. /**
  1856. * proc_read_stat()
  1857. * @page - buffer to write the data in
  1858. * @start - where the actual data has been written in page
  1859. * @offset - same meaning as the read system call
  1860. * @count - same meaning as the read system call
  1861. * @eof - set if no more data needs to be returned
  1862. * @data - pointer to our soft state
  1863. *
  1864. * Diaplay statistical information about the I/O activity.
  1865. */
  1866. static int
  1867. proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
  1868. void *data)
  1869. {
  1870. adapter_t *adapter;
  1871. int len;
  1872. int i;
  1873. i = 0; /* avoid compilation warnings */
  1874. len = 0;
  1875. adapter = (adapter_t *)data;
  1876. len = sprintf(page, "Statistical Information for this controller\n");
  1877. len += sprintf(page+len, "pend_cmds = %d\n",
  1878. atomic_read(&adapter->pend_cmds));
  1879. #if MEGA_HAVE_STATS
  1880. for(i = 0; i < adapter->numldrv; i++) {
  1881. len += sprintf(page+len, "Logical Drive %d:\n", i);
  1882. len += sprintf(page+len,
  1883. "\tReads Issued = %lu, Writes Issued = %lu\n",
  1884. adapter->nreads[i], adapter->nwrites[i]);
  1885. len += sprintf(page+len,
  1886. "\tSectors Read = %lu, Sectors Written = %lu\n",
  1887. adapter->nreadblocks[i], adapter->nwriteblocks[i]);
  1888. len += sprintf(page+len,
  1889. "\tRead errors = %lu, Write errors = %lu\n\n",
  1890. adapter->rd_errors[i], adapter->wr_errors[i]);
  1891. }
  1892. #else
  1893. len += sprintf(page+len,
  1894. "IO and error counters not compiled in driver.\n");
  1895. #endif
  1896. *eof = 1;
  1897. return len;
  1898. }
  1899. /**
  1900. * proc_read_mbox()
  1901. * @page - buffer to write the data in
  1902. * @start - where the actual data has been written in page
  1903. * @offset - same meaning as the read system call
  1904. * @count - same meaning as the read system call
  1905. * @eof - set if no more data needs to be returned
  1906. * @data - pointer to our soft state
  1907. *
  1908. * Display mailbox information for the last command issued. This information
  1909. * is good for debugging.
  1910. */
  1911. static int
  1912. proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
  1913. void *data)
  1914. {
  1915. adapter_t *adapter = (adapter_t *)data;
  1916. volatile mbox_t *mbox = adapter->mbox;
  1917. int len = 0;
  1918. len = sprintf(page, "Contents of Mail Box Structure\n");
  1919. len += sprintf(page+len, " Fw Command = 0x%02x\n",
  1920. mbox->m_out.cmd);
  1921. len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
  1922. mbox->m_out.cmdid);
  1923. len += sprintf(page+len, " No of Sectors= %04d\n",
  1924. mbox->m_out.numsectors);
  1925. len += sprintf(page+len, " LBA = 0x%02x\n",
  1926. mbox->m_out.lba);
  1927. len += sprintf(page+len, " DTA = 0x%08x\n",
  1928. mbox->m_out.xferaddr);
  1929. len += sprintf(page+len, " Logical Drive= 0x%02x\n",
  1930. mbox->m_out.logdrv);
  1931. len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
  1932. mbox->m_out.numsgelements);
  1933. len += sprintf(page+len, " Busy = %01x\n",
  1934. mbox->m_in.busy);
  1935. len += sprintf(page+len, " Status = 0x%02x\n",
  1936. mbox->m_in.status);
  1937. *eof = 1;
  1938. return len;
  1939. }
  1940. /**
  1941. * proc_rebuild_rate()
  1942. * @page - buffer to write the data in
  1943. * @start - where the actual data has been written in page
  1944. * @offset - same meaning as the read system call
  1945. * @count - same meaning as the read system call
  1946. * @eof - set if no more data needs to be returned
  1947. * @data - pointer to our soft state
  1948. *
  1949. * Display current rebuild rate
  1950. */
  1951. static int
  1952. proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
  1953. void *data)
  1954. {
  1955. adapter_t *adapter = (adapter_t *)data;
  1956. dma_addr_t dma_handle;
  1957. caddr_t inquiry;
  1958. struct pci_dev *pdev;
  1959. int len = 0;
  1960. if( make_local_pdev(adapter, &pdev) != 0 ) {
  1961. *eof = 1;
  1962. return len;
  1963. }
  1964. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  1965. free_local_pdev(pdev);
  1966. *eof = 1;
  1967. return len;
  1968. }
  1969. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  1970. len = sprintf(page, "Adapter inquiry failed.\n");
  1971. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  1972. mega_free_inquiry(inquiry, dma_handle, pdev);
  1973. free_local_pdev(pdev);
  1974. *eof = 1;
  1975. return len;
  1976. }
  1977. if( adapter->flag & BOARD_40LD ) {
  1978. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1979. ((mega_inquiry3 *)inquiry)->rebuild_rate);
  1980. }
  1981. else {
  1982. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1983. ((mraid_ext_inquiry *)
  1984. inquiry)->raid_inq.adapter_info.rebuild_rate);
  1985. }
  1986. mega_free_inquiry(inquiry, dma_handle, pdev);
  1987. free_local_pdev(pdev);
  1988. *eof = 1;
  1989. return len;
  1990. }
  1991. /**
  1992. * proc_battery()
  1993. * @page - buffer to write the data in
  1994. * @start - where the actual data has been written in page
  1995. * @offset - same meaning as the read system call
  1996. * @count - same meaning as the read system call
  1997. * @eof - set if no more data needs to be returned
  1998. * @data - pointer to our soft state
  1999. *
  2000. * Display information about the battery module on the controller.
  2001. */
  2002. static int
  2003. proc_battery(char *page, char **start, off_t offset, int count, int *eof,
  2004. void *data)
  2005. {
  2006. adapter_t *adapter = (adapter_t *)data;
  2007. dma_addr_t dma_handle;
  2008. caddr_t inquiry;
  2009. struct pci_dev *pdev;
  2010. u8 battery_status = 0;
  2011. char str[256];
  2012. int len = 0;
  2013. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2014. *eof = 1;
  2015. return len;
  2016. }
  2017. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2018. free_local_pdev(pdev);
  2019. *eof = 1;
  2020. return len;
  2021. }
  2022. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2023. len = sprintf(page, "Adapter inquiry failed.\n");
  2024. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2025. mega_free_inquiry(inquiry, dma_handle, pdev);
  2026. free_local_pdev(pdev);
  2027. *eof = 1;
  2028. return len;
  2029. }
  2030. if( adapter->flag & BOARD_40LD ) {
  2031. battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
  2032. }
  2033. else {
  2034. battery_status = ((mraid_ext_inquiry *)inquiry)->
  2035. raid_inq.adapter_info.battery_status;
  2036. }
  2037. /*
  2038. * Decode the battery status
  2039. */
  2040. sprintf(str, "Battery Status:[%d]", battery_status);
  2041. if(battery_status == MEGA_BATT_CHARGE_DONE)
  2042. strcat(str, " Charge Done");
  2043. if(battery_status & MEGA_BATT_MODULE_MISSING)
  2044. strcat(str, " Module Missing");
  2045. if(battery_status & MEGA_BATT_LOW_VOLTAGE)
  2046. strcat(str, " Low Voltage");
  2047. if(battery_status & MEGA_BATT_TEMP_HIGH)
  2048. strcat(str, " Temperature High");
  2049. if(battery_status & MEGA_BATT_PACK_MISSING)
  2050. strcat(str, " Pack Missing");
  2051. if(battery_status & MEGA_BATT_CHARGE_INPROG)
  2052. strcat(str, " Charge In-progress");
  2053. if(battery_status & MEGA_BATT_CHARGE_FAIL)
  2054. strcat(str, " Charge Fail");
  2055. if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
  2056. strcat(str, " Cycles Exceeded");
  2057. len = sprintf(page, "%s\n", str);
  2058. mega_free_inquiry(inquiry, dma_handle, pdev);
  2059. free_local_pdev(pdev);
  2060. *eof = 1;
  2061. return len;
  2062. }
  2063. /**
  2064. * proc_pdrv_ch0()
  2065. * @page - buffer to write the data in
  2066. * @start - where the actual data has been written in page
  2067. * @offset - same meaning as the read system call
  2068. * @count - same meaning as the read system call
  2069. * @eof - set if no more data needs to be returned
  2070. * @data - pointer to our soft state
  2071. *
  2072. * Display information about the physical drives on physical channel 0.
  2073. */
  2074. static int
  2075. proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
  2076. void *data)
  2077. {
  2078. adapter_t *adapter = (adapter_t *)data;
  2079. *eof = 1;
  2080. return (proc_pdrv(adapter, page, 0));
  2081. }
  2082. /**
  2083. * proc_pdrv_ch1()
  2084. * @page - buffer to write the data in
  2085. * @start - where the actual data has been written in page
  2086. * @offset - same meaning as the read system call
  2087. * @count - same meaning as the read system call
  2088. * @eof - set if no more data needs to be returned
  2089. * @data - pointer to our soft state
  2090. *
  2091. * Display information about the physical drives on physical channel 1.
  2092. */
  2093. static int
  2094. proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
  2095. void *data)
  2096. {
  2097. adapter_t *adapter = (adapter_t *)data;
  2098. *eof = 1;
  2099. return (proc_pdrv(adapter, page, 1));
  2100. }
  2101. /**
  2102. * proc_pdrv_ch2()
  2103. * @page - buffer to write the data in
  2104. * @start - where the actual data has been written in page
  2105. * @offset - same meaning as the read system call
  2106. * @count - same meaning as the read system call
  2107. * @eof - set if no more data needs to be returned
  2108. * @data - pointer to our soft state
  2109. *
  2110. * Display information about the physical drives on physical channel 2.
  2111. */
  2112. static int
  2113. proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
  2114. void *data)
  2115. {
  2116. adapter_t *adapter = (adapter_t *)data;
  2117. *eof = 1;
  2118. return (proc_pdrv(adapter, page, 2));
  2119. }
  2120. /**
  2121. * proc_pdrv_ch3()
  2122. * @page - buffer to write the data in
  2123. * @start - where the actual data has been written in page
  2124. * @offset - same meaning as the read system call
  2125. * @count - same meaning as the read system call
  2126. * @eof - set if no more data needs to be returned
  2127. * @data - pointer to our soft state
  2128. *
  2129. * Display information about the physical drives on physical channel 3.
  2130. */
  2131. static int
  2132. proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
  2133. void *data)
  2134. {
  2135. adapter_t *adapter = (adapter_t *)data;
  2136. *eof = 1;
  2137. return (proc_pdrv(adapter, page, 3));
  2138. }
  2139. /**
  2140. * proc_pdrv()
  2141. * @page - buffer to write the data in
  2142. * @adapter - pointer to our soft state
  2143. *
  2144. * Display information about the physical drives.
  2145. */
  2146. static int
  2147. proc_pdrv(adapter_t *adapter, char *page, int channel)
  2148. {
  2149. dma_addr_t dma_handle;
  2150. char *scsi_inq;
  2151. dma_addr_t scsi_inq_dma_handle;
  2152. caddr_t inquiry;
  2153. struct pci_dev *pdev;
  2154. u8 *pdrv_state;
  2155. u8 state;
  2156. int tgt;
  2157. int max_channels;
  2158. int len = 0;
  2159. char str[80];
  2160. int i;
  2161. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2162. return len;
  2163. }
  2164. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2165. goto free_pdev;
  2166. }
  2167. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2168. len = sprintf(page, "Adapter inquiry failed.\n");
  2169. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2170. goto free_inquiry;
  2171. }
  2172. scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
  2173. if( scsi_inq == NULL ) {
  2174. len = sprintf(page, "memory not available for scsi inq.\n");
  2175. goto free_inquiry;
  2176. }
  2177. if( adapter->flag & BOARD_40LD ) {
  2178. pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
  2179. }
  2180. else {
  2181. pdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2182. raid_inq.pdrv_info.pdrv_state;
  2183. }
  2184. max_channels = adapter->product_info.nchannels;
  2185. if( channel >= max_channels ) {
  2186. goto free_pci;
  2187. }
  2188. for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
  2189. i = channel*16 + tgt;
  2190. state = *(pdrv_state + i);
  2191. switch( state & 0x0F ) {
  2192. case PDRV_ONLINE:
  2193. sprintf(str,
  2194. "Channel:%2d Id:%2d State: Online",
  2195. channel, tgt);
  2196. break;
  2197. case PDRV_FAILED:
  2198. sprintf(str,
  2199. "Channel:%2d Id:%2d State: Failed",
  2200. channel, tgt);
  2201. break;
  2202. case PDRV_RBLD:
  2203. sprintf(str,
  2204. "Channel:%2d Id:%2d State: Rebuild",
  2205. channel, tgt);
  2206. break;
  2207. case PDRV_HOTSPARE:
  2208. sprintf(str,
  2209. "Channel:%2d Id:%2d State: Hot spare",
  2210. channel, tgt);
  2211. break;
  2212. default:
  2213. sprintf(str,
  2214. "Channel:%2d Id:%2d State: Un-configured",
  2215. channel, tgt);
  2216. break;
  2217. }
  2218. /*
  2219. * This interface displays inquiries for disk drives
  2220. * only. Inquries for logical drives and non-disk
  2221. * devices are available through /proc/scsi/scsi
  2222. */
  2223. memset(scsi_inq, 0, 256);
  2224. if( mega_internal_dev_inquiry(adapter, channel, tgt,
  2225. scsi_inq_dma_handle) ||
  2226. (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
  2227. continue;
  2228. }
  2229. /*
  2230. * Check for overflow. We print less than 240
  2231. * characters for inquiry
  2232. */
  2233. if( (len + 240) >= PAGE_SIZE ) break;
  2234. len += sprintf(page+len, "%s.\n", str);
  2235. len += mega_print_inquiry(page+len, scsi_inq);
  2236. }
  2237. free_pci:
  2238. pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
  2239. free_inquiry:
  2240. mega_free_inquiry(inquiry, dma_handle, pdev);
  2241. free_pdev:
  2242. free_local_pdev(pdev);
  2243. return len;
  2244. }
  2245. /*
  2246. * Display scsi inquiry
  2247. */
  2248. static int
  2249. mega_print_inquiry(char *page, char *scsi_inq)
  2250. {
  2251. int len = 0;
  2252. int i;
  2253. len = sprintf(page, " Vendor: ");
  2254. for( i = 8; i < 16; i++ ) {
  2255. len += sprintf(page+len, "%c", scsi_inq[i]);
  2256. }
  2257. len += sprintf(page+len, " Model: ");
  2258. for( i = 16; i < 32; i++ ) {
  2259. len += sprintf(page+len, "%c", scsi_inq[i]);
  2260. }
  2261. len += sprintf(page+len, " Rev: ");
  2262. for( i = 32; i < 36; i++ ) {
  2263. len += sprintf(page+len, "%c", scsi_inq[i]);
  2264. }
  2265. len += sprintf(page+len, "\n");
  2266. i = scsi_inq[0] & 0x1f;
  2267. len += sprintf(page+len, " Type: %s ",
  2268. i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
  2269. "Unknown ");
  2270. len += sprintf(page+len,
  2271. " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
  2272. if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
  2273. len += sprintf(page+len, " CCS\n");
  2274. else
  2275. len += sprintf(page+len, "\n");
  2276. return len;
  2277. }
  2278. /**
  2279. * proc_rdrv_10()
  2280. * @page - buffer to write the data in
  2281. * @start - where the actual data has been written in page
  2282. * @offset - same meaning as the read system call
  2283. * @count - same meaning as the read system call
  2284. * @eof - set if no more data needs to be returned
  2285. * @data - pointer to our soft state
  2286. *
  2287. * Display real time information about the logical drives 0 through 9.
  2288. */
  2289. static int
  2290. proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
  2291. void *data)
  2292. {
  2293. adapter_t *adapter = (adapter_t *)data;
  2294. *eof = 1;
  2295. return (proc_rdrv(adapter, page, 0, 9));
  2296. }
  2297. /**
  2298. * proc_rdrv_20()
  2299. * @page - buffer to write the data in
  2300. * @start - where the actual data has been written in page
  2301. * @offset - same meaning as the read system call
  2302. * @count - same meaning as the read system call
  2303. * @eof - set if no more data needs to be returned
  2304. * @data - pointer to our soft state
  2305. *
  2306. * Display real time information about the logical drives 0 through 9.
  2307. */
  2308. static int
  2309. proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
  2310. void *data)
  2311. {
  2312. adapter_t *adapter = (adapter_t *)data;
  2313. *eof = 1;
  2314. return (proc_rdrv(adapter, page, 10, 19));
  2315. }
  2316. /**
  2317. * proc_rdrv_30()
  2318. * @page - buffer to write the data in
  2319. * @start - where the actual data has been written in page
  2320. * @offset - same meaning as the read system call
  2321. * @count - same meaning as the read system call
  2322. * @eof - set if no more data needs to be returned
  2323. * @data - pointer to our soft state
  2324. *
  2325. * Display real time information about the logical drives 0 through 9.
  2326. */
  2327. static int
  2328. proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
  2329. void *data)
  2330. {
  2331. adapter_t *adapter = (adapter_t *)data;
  2332. *eof = 1;
  2333. return (proc_rdrv(adapter, page, 20, 29));
  2334. }
  2335. /**
  2336. * proc_rdrv_40()
  2337. * @page - buffer to write the data in
  2338. * @start - where the actual data has been written in page
  2339. * @offset - same meaning as the read system call
  2340. * @count - same meaning as the read system call
  2341. * @eof - set if no more data needs to be returned
  2342. * @data - pointer to our soft state
  2343. *
  2344. * Display real time information about the logical drives 0 through 9.
  2345. */
  2346. static int
  2347. proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
  2348. void *data)
  2349. {
  2350. adapter_t *adapter = (adapter_t *)data;
  2351. *eof = 1;
  2352. return (proc_rdrv(adapter, page, 30, 39));
  2353. }
  2354. /**
  2355. * proc_rdrv()
  2356. * @page - buffer to write the data in
  2357. * @adapter - pointer to our soft state
  2358. * @start - starting logical drive to display
  2359. * @end - ending logical drive to display
  2360. *
  2361. * We do not print the inquiry information since its already available through
  2362. * /proc/scsi/scsi interface
  2363. */
  2364. static int
  2365. proc_rdrv(adapter_t *adapter, char *page, int start, int end )
  2366. {
  2367. dma_addr_t dma_handle;
  2368. logdrv_param *lparam;
  2369. megacmd_t mc;
  2370. char *disk_array;
  2371. dma_addr_t disk_array_dma_handle;
  2372. caddr_t inquiry;
  2373. struct pci_dev *pdev;
  2374. u8 *rdrv_state;
  2375. int num_ldrv;
  2376. u32 array_sz;
  2377. int len = 0;
  2378. int i;
  2379. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2380. return len;
  2381. }
  2382. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2383. free_local_pdev(pdev);
  2384. return len;
  2385. }
  2386. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2387. len = sprintf(page, "Adapter inquiry failed.\n");
  2388. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2389. mega_free_inquiry(inquiry, dma_handle, pdev);
  2390. free_local_pdev(pdev);
  2391. return len;
  2392. }
  2393. memset(&mc, 0, sizeof(megacmd_t));
  2394. if( adapter->flag & BOARD_40LD ) {
  2395. array_sz = sizeof(disk_array_40ld);
  2396. rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
  2397. num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
  2398. }
  2399. else {
  2400. array_sz = sizeof(disk_array_8ld);
  2401. rdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2402. raid_inq.logdrv_info.ldrv_state;
  2403. num_ldrv = ((mraid_ext_inquiry *)inquiry)->
  2404. raid_inq.logdrv_info.num_ldrv;
  2405. }
  2406. disk_array = pci_alloc_consistent(pdev, array_sz,
  2407. &disk_array_dma_handle);
  2408. if( disk_array == NULL ) {
  2409. len = sprintf(page, "memory not available.\n");
  2410. mega_free_inquiry(inquiry, dma_handle, pdev);
  2411. free_local_pdev(pdev);
  2412. return len;
  2413. }
  2414. mc.xferaddr = (u32)disk_array_dma_handle;
  2415. if( adapter->flag & BOARD_40LD ) {
  2416. mc.cmd = FC_NEW_CONFIG;
  2417. mc.opcode = OP_DCMD_READ_CONFIG;
  2418. if( mega_internal_command(adapter, &mc, NULL) ) {
  2419. len = sprintf(page, "40LD read config failed.\n");
  2420. mega_free_inquiry(inquiry, dma_handle, pdev);
  2421. pci_free_consistent(pdev, array_sz, disk_array,
  2422. disk_array_dma_handle);
  2423. free_local_pdev(pdev);
  2424. return len;
  2425. }
  2426. }
  2427. else {
  2428. mc.cmd = NEW_READ_CONFIG_8LD;
  2429. if( mega_internal_command(adapter, &mc, NULL) ) {
  2430. mc.cmd = READ_CONFIG_8LD;
  2431. if( mega_internal_command(adapter, &mc,
  2432. NULL) ){
  2433. len = sprintf(page,
  2434. "8LD read config failed.\n");
  2435. mega_free_inquiry(inquiry, dma_handle, pdev);
  2436. pci_free_consistent(pdev, array_sz,
  2437. disk_array,
  2438. disk_array_dma_handle);
  2439. free_local_pdev(pdev);
  2440. return len;
  2441. }
  2442. }
  2443. }
  2444. for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
  2445. if( adapter->flag & BOARD_40LD ) {
  2446. lparam =
  2447. &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
  2448. }
  2449. else {
  2450. lparam =
  2451. &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
  2452. }
  2453. /*
  2454. * Check for overflow. We print less than 240 characters for
  2455. * information about each logical drive.
  2456. */
  2457. if( (len + 240) >= PAGE_SIZE ) break;
  2458. len += sprintf(page+len, "Logical drive:%2d:, ", i);
  2459. switch( rdrv_state[i] & 0x0F ) {
  2460. case RDRV_OFFLINE:
  2461. len += sprintf(page+len, "state: offline");
  2462. break;
  2463. case RDRV_DEGRADED:
  2464. len += sprintf(page+len, "state: degraded");
  2465. break;
  2466. case RDRV_OPTIMAL:
  2467. len += sprintf(page+len, "state: optimal");
  2468. break;
  2469. case RDRV_DELETED:
  2470. len += sprintf(page+len, "state: deleted");
  2471. break;
  2472. default:
  2473. len += sprintf(page+len, "state: unknown");
  2474. break;
  2475. }
  2476. /*
  2477. * Check if check consistency or initialization is going on
  2478. * for this logical drive.
  2479. */
  2480. if( (rdrv_state[i] & 0xF0) == 0x20 ) {
  2481. len += sprintf(page+len,
  2482. ", check-consistency in progress");
  2483. }
  2484. else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
  2485. len += sprintf(page+len,
  2486. ", initialization in progress");
  2487. }
  2488. len += sprintf(page+len, "\n");
  2489. len += sprintf(page+len, "Span depth:%3d, ",
  2490. lparam->span_depth);
  2491. len += sprintf(page+len, "RAID level:%3d, ",
  2492. lparam->level);
  2493. len += sprintf(page+len, "Stripe size:%3d, ",
  2494. lparam->stripe_sz ? lparam->stripe_sz/2: 128);
  2495. len += sprintf(page+len, "Row size:%3d\n",
  2496. lparam->row_size);
  2497. len += sprintf(page+len, "Read Policy: ");
  2498. switch(lparam->read_ahead) {
  2499. case NO_READ_AHEAD:
  2500. len += sprintf(page+len, "No read ahead, ");
  2501. break;
  2502. case READ_AHEAD:
  2503. len += sprintf(page+len, "Read ahead, ");
  2504. break;
  2505. case ADAP_READ_AHEAD:
  2506. len += sprintf(page+len, "Adaptive, ");
  2507. break;
  2508. }
  2509. len += sprintf(page+len, "Write Policy: ");
  2510. switch(lparam->write_mode) {
  2511. case WRMODE_WRITE_THRU:
  2512. len += sprintf(page+len, "Write thru, ");
  2513. break;
  2514. case WRMODE_WRITE_BACK:
  2515. len += sprintf(page+len, "Write back, ");
  2516. break;
  2517. }
  2518. len += sprintf(page+len, "Cache Policy: ");
  2519. switch(lparam->direct_io) {
  2520. case CACHED_IO:
  2521. len += sprintf(page+len, "Cached IO\n\n");
  2522. break;
  2523. case DIRECT_IO:
  2524. len += sprintf(page+len, "Direct IO\n\n");
  2525. break;
  2526. }
  2527. }
  2528. mega_free_inquiry(inquiry, dma_handle, pdev);
  2529. pci_free_consistent(pdev, array_sz, disk_array,
  2530. disk_array_dma_handle);
  2531. free_local_pdev(pdev);
  2532. return len;
  2533. }
  2534. #endif
  2535. /**
  2536. * megaraid_biosparam()
  2537. *
  2538. * Return the disk geometry for a particular disk
  2539. */
  2540. static int
  2541. megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  2542. sector_t capacity, int geom[])
  2543. {
  2544. adapter_t *adapter;
  2545. unsigned char *bh;
  2546. int heads;
  2547. int sectors;
  2548. int cylinders;
  2549. int rval;
  2550. /* Get pointer to host config structure */
  2551. adapter = (adapter_t *)sdev->host->hostdata;
  2552. if (IS_RAID_CH(adapter, sdev->channel)) {
  2553. /* Default heads (64) & sectors (32) */
  2554. heads = 64;
  2555. sectors = 32;
  2556. cylinders = (ulong)capacity / (heads * sectors);
  2557. /*
  2558. * Handle extended translation size for logical drives
  2559. * > 1Gb
  2560. */
  2561. if ((ulong)capacity >= 0x200000) {
  2562. heads = 255;
  2563. sectors = 63;
  2564. cylinders = (ulong)capacity / (heads * sectors);
  2565. }
  2566. /* return result */
  2567. geom[0] = heads;
  2568. geom[1] = sectors;
  2569. geom[2] = cylinders;
  2570. }
  2571. else {
  2572. bh = scsi_bios_ptable(bdev);
  2573. if( bh ) {
  2574. rval = scsi_partsize(bh, capacity,
  2575. &geom[2], &geom[0], &geom[1]);
  2576. kfree(bh);
  2577. if( rval != -1 )
  2578. return rval;
  2579. }
  2580. printk(KERN_INFO
  2581. "megaraid: invalid partition on this disk on channel %d\n",
  2582. sdev->channel);
  2583. /* Default heads (64) & sectors (32) */
  2584. heads = 64;
  2585. sectors = 32;
  2586. cylinders = (ulong)capacity / (heads * sectors);
  2587. /* Handle extended translation size for logical drives > 1Gb */
  2588. if ((ulong)capacity >= 0x200000) {
  2589. heads = 255;
  2590. sectors = 63;
  2591. cylinders = (ulong)capacity / (heads * sectors);
  2592. }
  2593. /* return result */
  2594. geom[0] = heads;
  2595. geom[1] = sectors;
  2596. geom[2] = cylinders;
  2597. }
  2598. return 0;
  2599. }
  2600. /**
  2601. * mega_init_scb()
  2602. * @adapter - pointer to our soft state
  2603. *
  2604. * Allocate memory for the various pointers in the scb structures:
  2605. * scatter-gather list pointer, passthru and extended passthru structure
  2606. * pointers.
  2607. */
  2608. static int
  2609. mega_init_scb(adapter_t *adapter)
  2610. {
  2611. scb_t *scb;
  2612. int i;
  2613. for( i = 0; i < adapter->max_cmds; i++ ) {
  2614. scb = &adapter->scb_list[i];
  2615. scb->sgl64 = NULL;
  2616. scb->sgl = NULL;
  2617. scb->pthru = NULL;
  2618. scb->epthru = NULL;
  2619. }
  2620. for( i = 0; i < adapter->max_cmds; i++ ) {
  2621. scb = &adapter->scb_list[i];
  2622. scb->idx = i;
  2623. scb->sgl64 = pci_alloc_consistent(adapter->dev,
  2624. sizeof(mega_sgl64) * adapter->sglen,
  2625. &scb->sgl_dma_addr);
  2626. scb->sgl = (mega_sglist *)scb->sgl64;
  2627. if( !scb->sgl ) {
  2628. printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
  2629. mega_free_sgl(adapter);
  2630. return -1;
  2631. }
  2632. scb->pthru = pci_alloc_consistent(adapter->dev,
  2633. sizeof(mega_passthru),
  2634. &scb->pthru_dma_addr);
  2635. if( !scb->pthru ) {
  2636. printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
  2637. mega_free_sgl(adapter);
  2638. return -1;
  2639. }
  2640. scb->epthru = pci_alloc_consistent(adapter->dev,
  2641. sizeof(mega_ext_passthru),
  2642. &scb->epthru_dma_addr);
  2643. if( !scb->epthru ) {
  2644. printk(KERN_WARNING
  2645. "Can't allocate extended passthru.\n");
  2646. mega_free_sgl(adapter);
  2647. return -1;
  2648. }
  2649. scb->dma_type = MEGA_DMA_TYPE_NONE;
  2650. /*
  2651. * Link to free list
  2652. * lock not required since we are loading the driver, so no
  2653. * commands possible right now.
  2654. */
  2655. scb->state = SCB_FREE;
  2656. scb->cmd = NULL;
  2657. list_add(&scb->list, &adapter->free_list);
  2658. }
  2659. return 0;
  2660. }
  2661. /**
  2662. * megadev_open()
  2663. * @inode - unused
  2664. * @filep - unused
  2665. *
  2666. * Routines for the character/ioctl interface to the driver. Find out if this
  2667. * is a valid open. If yes, increment the module use count so that it cannot
  2668. * be unloaded.
  2669. */
  2670. static int
  2671. megadev_open (struct inode *inode, struct file *filep)
  2672. {
  2673. /*
  2674. * Only allow superuser to access private ioctl interface
  2675. */
  2676. if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
  2677. return 0;
  2678. }
  2679. /**
  2680. * megadev_ioctl()
  2681. * @inode - Our device inode
  2682. * @filep - unused
  2683. * @cmd - ioctl command
  2684. * @arg - user buffer
  2685. *
  2686. * ioctl entry point for our private ioctl interface. We move the data in from
  2687. * the user space, prepare the command (if necessary, convert the old MIMD
  2688. * ioctl to new ioctl command), and issue a synchronous command to the
  2689. * controller.
  2690. */
  2691. static int
  2692. megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
  2693. unsigned long arg)
  2694. {
  2695. adapter_t *adapter;
  2696. nitioctl_t uioc;
  2697. int adapno;
  2698. int rval;
  2699. mega_passthru __user *upthru; /* user address for passthru */
  2700. mega_passthru *pthru; /* copy user passthru here */
  2701. dma_addr_t pthru_dma_hndl;
  2702. void *data = NULL; /* data to be transferred */
  2703. dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
  2704. megacmd_t mc;
  2705. megastat_t __user *ustats;
  2706. int num_ldrv;
  2707. u32 uxferaddr = 0;
  2708. struct pci_dev *pdev;
  2709. ustats = NULL; /* avoid compilation warnings */
  2710. num_ldrv = 0;
  2711. /*
  2712. * Make sure only USCSICMD are issued through this interface.
  2713. * MIMD application would still fire different command.
  2714. */
  2715. if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
  2716. return -EINVAL;
  2717. }
  2718. /*
  2719. * Check and convert a possible MIMD command to NIT command.
  2720. * mega_m_to_n() copies the data from the user space, so we do not
  2721. * have to do it here.
  2722. * NOTE: We will need some user address to copyout the data, therefore
  2723. * the inteface layer will also provide us with the required user
  2724. * addresses.
  2725. */
  2726. memset(&uioc, 0, sizeof(nitioctl_t));
  2727. if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
  2728. return rval;
  2729. switch( uioc.opcode ) {
  2730. case GET_DRIVER_VER:
  2731. if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
  2732. return (-EFAULT);
  2733. break;
  2734. case GET_N_ADAP:
  2735. if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
  2736. return (-EFAULT);
  2737. /*
  2738. * Shucks. MIMD interface returns a positive value for number
  2739. * of adapters. TODO: Change it to return 0 when there is no
  2740. * applicatio using mimd interface.
  2741. */
  2742. return hba_count;
  2743. case GET_ADAP_INFO:
  2744. /*
  2745. * Which adapter
  2746. */
  2747. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2748. return (-ENODEV);
  2749. if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
  2750. sizeof(struct mcontroller)) )
  2751. return (-EFAULT);
  2752. break;
  2753. #if MEGA_HAVE_STATS
  2754. case GET_STATS:
  2755. /*
  2756. * Which adapter
  2757. */
  2758. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2759. return (-ENODEV);
  2760. adapter = hba_soft_state[adapno];
  2761. ustats = uioc.uioc_uaddr;
  2762. if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
  2763. return (-EFAULT);
  2764. /*
  2765. * Check for the validity of the logical drive number
  2766. */
  2767. if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
  2768. if( copy_to_user(ustats->nreads, adapter->nreads,
  2769. num_ldrv*sizeof(u32)) )
  2770. return -EFAULT;
  2771. if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
  2772. num_ldrv*sizeof(u32)) )
  2773. return -EFAULT;
  2774. if( copy_to_user(ustats->nwrites, adapter->nwrites,
  2775. num_ldrv*sizeof(u32)) )
  2776. return -EFAULT;
  2777. if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
  2778. num_ldrv*sizeof(u32)) )
  2779. return -EFAULT;
  2780. if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
  2781. num_ldrv*sizeof(u32)) )
  2782. return -EFAULT;
  2783. if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
  2784. num_ldrv*sizeof(u32)) )
  2785. return -EFAULT;
  2786. return 0;
  2787. #endif
  2788. case MBOX_CMD:
  2789. /*
  2790. * Which adapter
  2791. */
  2792. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2793. return (-ENODEV);
  2794. adapter = hba_soft_state[adapno];
  2795. /*
  2796. * Deletion of logical drive is a special case. The adapter
  2797. * should be quiescent before this command is issued.
  2798. */
  2799. if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
  2800. uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
  2801. /*
  2802. * Do we support this feature
  2803. */
  2804. if( !adapter->support_random_del ) {
  2805. printk(KERN_WARNING "megaraid: logdrv ");
  2806. printk("delete on non-supporting F/W.\n");
  2807. return (-EINVAL);
  2808. }
  2809. rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
  2810. if( rval == 0 ) {
  2811. memset(&mc, 0, sizeof(megacmd_t));
  2812. mc.status = rval;
  2813. rval = mega_n_to_m((void __user *)arg, &mc);
  2814. }
  2815. return rval;
  2816. }
  2817. /*
  2818. * This interface only support the regular passthru commands.
  2819. * Reject extended passthru and 64-bit passthru
  2820. */
  2821. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
  2822. uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
  2823. printk(KERN_WARNING "megaraid: rejected passthru.\n");
  2824. return (-EINVAL);
  2825. }
  2826. /*
  2827. * For all internal commands, the buffer must be allocated in
  2828. * <4GB address range
  2829. */
  2830. if( make_local_pdev(adapter, &pdev) != 0 )
  2831. return -EIO;
  2832. /* Is it a passthru command or a DCMD */
  2833. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
  2834. /* Passthru commands */
  2835. pthru = pci_alloc_consistent(pdev,
  2836. sizeof(mega_passthru),
  2837. &pthru_dma_hndl);
  2838. if( pthru == NULL ) {
  2839. free_local_pdev(pdev);
  2840. return (-ENOMEM);
  2841. }
  2842. /*
  2843. * The user passthru structure
  2844. */
  2845. upthru = (mega_passthru __user *)MBOX(uioc)->xferaddr;
  2846. /*
  2847. * Copy in the user passthru here.
  2848. */
  2849. if( copy_from_user(pthru, upthru,
  2850. sizeof(mega_passthru)) ) {
  2851. pci_free_consistent(pdev,
  2852. sizeof(mega_passthru), pthru,
  2853. pthru_dma_hndl);
  2854. free_local_pdev(pdev);
  2855. return (-EFAULT);
  2856. }
  2857. /*
  2858. * Is there a data transfer
  2859. */
  2860. if( pthru->dataxferlen ) {
  2861. data = pci_alloc_consistent(pdev,
  2862. pthru->dataxferlen,
  2863. &data_dma_hndl);
  2864. if( data == NULL ) {
  2865. pci_free_consistent(pdev,
  2866. sizeof(mega_passthru),
  2867. pthru,
  2868. pthru_dma_hndl);
  2869. free_local_pdev(pdev);
  2870. return (-ENOMEM);
  2871. }
  2872. /*
  2873. * Save the user address and point the kernel
  2874. * address at just allocated memory
  2875. */
  2876. uxferaddr = pthru->dataxferaddr;
  2877. pthru->dataxferaddr = data_dma_hndl;
  2878. }
  2879. /*
  2880. * Is data coming down-stream
  2881. */
  2882. if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
  2883. /*
  2884. * Get the user data
  2885. */
  2886. if( copy_from_user(data, (char __user *)uxferaddr,
  2887. pthru->dataxferlen) ) {
  2888. rval = (-EFAULT);
  2889. goto freemem_and_return;
  2890. }
  2891. }
  2892. memset(&mc, 0, sizeof(megacmd_t));
  2893. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  2894. mc.xferaddr = (u32)pthru_dma_hndl;
  2895. /*
  2896. * Issue the command
  2897. */
  2898. mega_internal_command(adapter, &mc, pthru);
  2899. rval = mega_n_to_m((void __user *)arg, &mc);
  2900. if( rval ) goto freemem_and_return;
  2901. /*
  2902. * Is data going up-stream
  2903. */
  2904. if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
  2905. if( copy_to_user((char __user *)uxferaddr, data,
  2906. pthru->dataxferlen) ) {
  2907. rval = (-EFAULT);
  2908. }
  2909. }
  2910. /*
  2911. * Send the request sense data also, irrespective of
  2912. * whether the user has asked for it or not.
  2913. */
  2914. copy_to_user(upthru->reqsensearea,
  2915. pthru->reqsensearea, 14);
  2916. freemem_and_return:
  2917. if( pthru->dataxferlen ) {
  2918. pci_free_consistent(pdev,
  2919. pthru->dataxferlen, data,
  2920. data_dma_hndl);
  2921. }
  2922. pci_free_consistent(pdev, sizeof(mega_passthru),
  2923. pthru, pthru_dma_hndl);
  2924. free_local_pdev(pdev);
  2925. return rval;
  2926. }
  2927. else {
  2928. /* DCMD commands */
  2929. /*
  2930. * Is there a data transfer
  2931. */
  2932. if( uioc.xferlen ) {
  2933. data = pci_alloc_consistent(pdev,
  2934. uioc.xferlen, &data_dma_hndl);
  2935. if( data == NULL ) {
  2936. free_local_pdev(pdev);
  2937. return (-ENOMEM);
  2938. }
  2939. uxferaddr = MBOX(uioc)->xferaddr;
  2940. }
  2941. /*
  2942. * Is data coming down-stream
  2943. */
  2944. if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
  2945. /*
  2946. * Get the user data
  2947. */
  2948. if( copy_from_user(data, (char __user *)uxferaddr,
  2949. uioc.xferlen) ) {
  2950. pci_free_consistent(pdev,
  2951. uioc.xferlen,
  2952. data, data_dma_hndl);
  2953. free_local_pdev(pdev);
  2954. return (-EFAULT);
  2955. }
  2956. }
  2957. memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
  2958. mc.xferaddr = (u32)data_dma_hndl;
  2959. /*
  2960. * Issue the command
  2961. */
  2962. mega_internal_command(adapter, &mc, NULL);
  2963. rval = mega_n_to_m((void __user *)arg, &mc);
  2964. if( rval ) {
  2965. if( uioc.xferlen ) {
  2966. pci_free_consistent(pdev,
  2967. uioc.xferlen, data,
  2968. data_dma_hndl);
  2969. }
  2970. free_local_pdev(pdev);
  2971. return rval;
  2972. }
  2973. /*
  2974. * Is data going up-stream
  2975. */
  2976. if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
  2977. if( copy_to_user((char __user *)uxferaddr, data,
  2978. uioc.xferlen) ) {
  2979. rval = (-EFAULT);
  2980. }
  2981. }
  2982. if( uioc.xferlen ) {
  2983. pci_free_consistent(pdev,
  2984. uioc.xferlen, data,
  2985. data_dma_hndl);
  2986. }
  2987. free_local_pdev(pdev);
  2988. return rval;
  2989. }
  2990. default:
  2991. return (-EINVAL);
  2992. }
  2993. return 0;
  2994. }
  2995. /**
  2996. * mega_m_to_n()
  2997. * @arg - user address
  2998. * @uioc - new ioctl structure
  2999. *
  3000. * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
  3001. * structure
  3002. *
  3003. * Converts the older mimd ioctl structure to newer NIT structure
  3004. */
  3005. static int
  3006. mega_m_to_n(void __user *arg, nitioctl_t *uioc)
  3007. {
  3008. struct uioctl_t uioc_mimd;
  3009. char signature[8] = {0};
  3010. u8 opcode;
  3011. u8 subopcode;
  3012. /*
  3013. * check is the application conforms to NIT. We do not have to do much
  3014. * in that case.
  3015. * We exploit the fact that the signature is stored in the very
  3016. * begining of the structure.
  3017. */
  3018. if( copy_from_user(signature, arg, 7) )
  3019. return (-EFAULT);
  3020. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  3021. /*
  3022. * NOTE NOTE: The nit ioctl is still under flux because of
  3023. * change of mailbox definition, in HPE. No applications yet
  3024. * use this interface and let's not have applications use this
  3025. * interface till the new specifitions are in place.
  3026. */
  3027. return -EINVAL;
  3028. #if 0
  3029. if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
  3030. return (-EFAULT);
  3031. return 0;
  3032. #endif
  3033. }
  3034. /*
  3035. * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
  3036. *
  3037. * Get the user ioctl structure
  3038. */
  3039. if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
  3040. return (-EFAULT);
  3041. /*
  3042. * Get the opcode and subopcode for the commands
  3043. */
  3044. opcode = uioc_mimd.ui.fcs.opcode;
  3045. subopcode = uioc_mimd.ui.fcs.subopcode;
  3046. switch (opcode) {
  3047. case 0x82:
  3048. switch (subopcode) {
  3049. case MEGAIOC_QDRVRVER: /* Query driver version */
  3050. uioc->opcode = GET_DRIVER_VER;
  3051. uioc->uioc_uaddr = uioc_mimd.data;
  3052. break;
  3053. case MEGAIOC_QNADAP: /* Get # of adapters */
  3054. uioc->opcode = GET_N_ADAP;
  3055. uioc->uioc_uaddr = uioc_mimd.data;
  3056. break;
  3057. case MEGAIOC_QADAPINFO: /* Get adapter information */
  3058. uioc->opcode = GET_ADAP_INFO;
  3059. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3060. uioc->uioc_uaddr = uioc_mimd.data;
  3061. break;
  3062. default:
  3063. return(-EINVAL);
  3064. }
  3065. break;
  3066. case 0x81:
  3067. uioc->opcode = MBOX_CMD;
  3068. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3069. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3070. uioc->xferlen = uioc_mimd.ui.fcs.length;
  3071. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3072. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3073. break;
  3074. case 0x80:
  3075. uioc->opcode = MBOX_CMD;
  3076. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3077. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3078. /*
  3079. * Choose the xferlen bigger of input and output data
  3080. */
  3081. uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
  3082. uioc_mimd.outlen : uioc_mimd.inlen;
  3083. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3084. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3085. break;
  3086. default:
  3087. return (-EINVAL);
  3088. }
  3089. return 0;
  3090. }
  3091. /*
  3092. * mega_n_to_m()
  3093. * @arg - user address
  3094. * @mc - mailbox command
  3095. *
  3096. * Updates the status information to the application, depending on application
  3097. * conforms to older mimd ioctl interface or newer NIT ioctl interface
  3098. */
  3099. static int
  3100. mega_n_to_m(void __user *arg, megacmd_t *mc)
  3101. {
  3102. nitioctl_t __user *uiocp;
  3103. megacmd_t __user *umc;
  3104. mega_passthru __user *upthru;
  3105. struct uioctl_t __user *uioc_mimd;
  3106. char signature[8] = {0};
  3107. /*
  3108. * check is the application conforms to NIT.
  3109. */
  3110. if( copy_from_user(signature, arg, 7) )
  3111. return -EFAULT;
  3112. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  3113. uiocp = arg;
  3114. if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
  3115. return (-EFAULT);
  3116. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3117. umc = MBOX_P(uiocp);
  3118. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3119. return -EFAULT;
  3120. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
  3121. return (-EFAULT);
  3122. }
  3123. }
  3124. else {
  3125. uioc_mimd = arg;
  3126. if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
  3127. return (-EFAULT);
  3128. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3129. umc = (megacmd_t __user *)uioc_mimd->mbox;
  3130. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3131. return (-EFAULT);
  3132. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
  3133. return (-EFAULT);
  3134. }
  3135. }
  3136. return 0;
  3137. }
  3138. /*
  3139. * MEGARAID 'FW' commands.
  3140. */
  3141. /**
  3142. * mega_is_bios_enabled()
  3143. * @adapter - pointer to our soft state
  3144. *
  3145. * issue command to find out if the BIOS is enabled for this controller
  3146. */
  3147. static int
  3148. mega_is_bios_enabled(adapter_t *adapter)
  3149. {
  3150. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3151. mbox_t *mbox;
  3152. int ret;
  3153. mbox = (mbox_t *)raw_mbox;
  3154. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3155. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3156. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3157. raw_mbox[0] = IS_BIOS_ENABLED;
  3158. raw_mbox[2] = GET_BIOS;
  3159. ret = issue_scb_block(adapter, raw_mbox);
  3160. return *(char *)adapter->mega_buffer;
  3161. }
  3162. /**
  3163. * mega_enum_raid_scsi()
  3164. * @adapter - pointer to our soft state
  3165. *
  3166. * Find out what channels are RAID/SCSI. This information is used to
  3167. * differentiate the virtual channels and physical channels and to support
  3168. * ROMB feature and non-disk devices.
  3169. */
  3170. static void
  3171. mega_enum_raid_scsi(adapter_t *adapter)
  3172. {
  3173. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3174. mbox_t *mbox;
  3175. int i;
  3176. mbox = (mbox_t *)raw_mbox;
  3177. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3178. /*
  3179. * issue command to find out what channels are raid/scsi
  3180. */
  3181. raw_mbox[0] = CHNL_CLASS;
  3182. raw_mbox[2] = GET_CHNL_CLASS;
  3183. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3184. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3185. /*
  3186. * Non-ROMB firmware fail this command, so all channels
  3187. * must be shown RAID
  3188. */
  3189. adapter->mega_ch_class = 0xFF;
  3190. if(!issue_scb_block(adapter, raw_mbox)) {
  3191. adapter->mega_ch_class = *((char *)adapter->mega_buffer);
  3192. }
  3193. for( i = 0; i < adapter->product_info.nchannels; i++ ) {
  3194. if( (adapter->mega_ch_class >> i) & 0x01 ) {
  3195. printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
  3196. i);
  3197. }
  3198. else {
  3199. printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
  3200. i);
  3201. }
  3202. }
  3203. return;
  3204. }
  3205. /**
  3206. * mega_get_boot_drv()
  3207. * @adapter - pointer to our soft state
  3208. *
  3209. * Find out which device is the boot device. Note, any logical drive or any
  3210. * phyical device (e.g., a CDROM) can be designated as a boot device.
  3211. */
  3212. static void
  3213. mega_get_boot_drv(adapter_t *adapter)
  3214. {
  3215. struct private_bios_data *prv_bios_data;
  3216. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3217. mbox_t *mbox;
  3218. u16 cksum = 0;
  3219. u8 *cksum_p;
  3220. u8 boot_pdrv;
  3221. int i;
  3222. mbox = (mbox_t *)raw_mbox;
  3223. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3224. raw_mbox[0] = BIOS_PVT_DATA;
  3225. raw_mbox[2] = GET_BIOS_PVT_DATA;
  3226. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3227. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3228. adapter->boot_ldrv_enabled = 0;
  3229. adapter->boot_ldrv = 0;
  3230. adapter->boot_pdrv_enabled = 0;
  3231. adapter->boot_pdrv_ch = 0;
  3232. adapter->boot_pdrv_tgt = 0;
  3233. if(issue_scb_block(adapter, raw_mbox) == 0) {
  3234. prv_bios_data =
  3235. (struct private_bios_data *)adapter->mega_buffer;
  3236. cksum = 0;
  3237. cksum_p = (char *)prv_bios_data;
  3238. for (i = 0; i < 14; i++ ) {
  3239. cksum += (u16)(*cksum_p++);
  3240. }
  3241. if (prv_bios_data->cksum == (u16)(0-cksum) ) {
  3242. /*
  3243. * If MSB is set, a physical drive is set as boot
  3244. * device
  3245. */
  3246. if( prv_bios_data->boot_drv & 0x80 ) {
  3247. adapter->boot_pdrv_enabled = 1;
  3248. boot_pdrv = prv_bios_data->boot_drv & 0x7F;
  3249. adapter->boot_pdrv_ch = boot_pdrv / 16;
  3250. adapter->boot_pdrv_tgt = boot_pdrv % 16;
  3251. }
  3252. else {
  3253. adapter->boot_ldrv_enabled = 1;
  3254. adapter->boot_ldrv = prv_bios_data->boot_drv;
  3255. }
  3256. }
  3257. }
  3258. }
  3259. /**
  3260. * mega_support_random_del()
  3261. * @adapter - pointer to our soft state
  3262. *
  3263. * Find out if this controller supports random deletion and addition of
  3264. * logical drives
  3265. */
  3266. static int
  3267. mega_support_random_del(adapter_t *adapter)
  3268. {
  3269. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3270. mbox_t *mbox;
  3271. int rval;
  3272. mbox = (mbox_t *)raw_mbox;
  3273. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3274. /*
  3275. * issue command
  3276. */
  3277. raw_mbox[0] = FC_DEL_LOGDRV;
  3278. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  3279. rval = issue_scb_block(adapter, raw_mbox);
  3280. return !rval;
  3281. }
  3282. /**
  3283. * mega_support_ext_cdb()
  3284. * @adapter - pointer to our soft state
  3285. *
  3286. * Find out if this firmware support cdblen > 10
  3287. */
  3288. static int
  3289. mega_support_ext_cdb(adapter_t *adapter)
  3290. {
  3291. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3292. mbox_t *mbox;
  3293. int rval;
  3294. mbox = (mbox_t *)raw_mbox;
  3295. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3296. /*
  3297. * issue command to find out if controller supports extended CDBs.
  3298. */
  3299. raw_mbox[0] = 0xA4;
  3300. raw_mbox[2] = 0x16;
  3301. rval = issue_scb_block(adapter, raw_mbox);
  3302. return !rval;
  3303. }
  3304. /**
  3305. * mega_del_logdrv()
  3306. * @adapter - pointer to our soft state
  3307. * @logdrv - logical drive to be deleted
  3308. *
  3309. * Delete the specified logical drive. It is the responsibility of the user
  3310. * app to let the OS know about this operation.
  3311. */
  3312. static int
  3313. mega_del_logdrv(adapter_t *adapter, int logdrv)
  3314. {
  3315. unsigned long flags;
  3316. scb_t *scb;
  3317. int rval;
  3318. /*
  3319. * Stop sending commands to the controller, queue them internally.
  3320. * When deletion is complete, ISR will flush the queue.
  3321. */
  3322. atomic_set(&adapter->quiescent, 1);
  3323. /*
  3324. * Wait till all the issued commands are complete and there are no
  3325. * commands in the pending queue
  3326. */
  3327. while (atomic_read(&adapter->pend_cmds) > 0 ||
  3328. !list_empty(&adapter->pending_list))
  3329. msleep(1000); /* sleep for 1s */
  3330. rval = mega_do_del_logdrv(adapter, logdrv);
  3331. spin_lock_irqsave(&adapter->lock, flags);
  3332. /*
  3333. * If delete operation was successful, add 0x80 to the logical drive
  3334. * ids for commands in the pending queue.
  3335. */
  3336. if (adapter->read_ldidmap) {
  3337. struct list_head *pos;
  3338. list_for_each(pos, &adapter->pending_list) {
  3339. scb = list_entry(pos, scb_t, list);
  3340. if (scb->pthru->logdrv < 0x80 )
  3341. scb->pthru->logdrv += 0x80;
  3342. }
  3343. }
  3344. atomic_set(&adapter->quiescent, 0);
  3345. mega_runpendq(adapter);
  3346. spin_unlock_irqrestore(&adapter->lock, flags);
  3347. return rval;
  3348. }
  3349. static int
  3350. mega_do_del_logdrv(adapter_t *adapter, int logdrv)
  3351. {
  3352. megacmd_t mc;
  3353. int rval;
  3354. memset( &mc, 0, sizeof(megacmd_t));
  3355. mc.cmd = FC_DEL_LOGDRV;
  3356. mc.opcode = OP_DEL_LOGDRV;
  3357. mc.subopcode = logdrv;
  3358. rval = mega_internal_command(adapter, &mc, NULL);
  3359. /* log this event */
  3360. if(rval) {
  3361. printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
  3362. return rval;
  3363. }
  3364. /*
  3365. * After deleting first logical drive, the logical drives must be
  3366. * addressed by adding 0x80 to the logical drive id.
  3367. */
  3368. adapter->read_ldidmap = 1;
  3369. return rval;
  3370. }
  3371. /**
  3372. * mega_get_max_sgl()
  3373. * @adapter - pointer to our soft state
  3374. *
  3375. * Find out the maximum number of scatter-gather elements supported by this
  3376. * version of the firmware
  3377. */
  3378. static void
  3379. mega_get_max_sgl(adapter_t *adapter)
  3380. {
  3381. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3382. mbox_t *mbox;
  3383. mbox = (mbox_t *)raw_mbox;
  3384. memset(mbox, 0, sizeof(raw_mbox));
  3385. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3386. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3387. raw_mbox[0] = MAIN_MISC_OPCODE;
  3388. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  3389. if( issue_scb_block(adapter, raw_mbox) ) {
  3390. /*
  3391. * f/w does not support this command. Choose the default value
  3392. */
  3393. adapter->sglen = MIN_SGLIST;
  3394. }
  3395. else {
  3396. adapter->sglen = *((char *)adapter->mega_buffer);
  3397. /*
  3398. * Make sure this is not more than the resources we are
  3399. * planning to allocate
  3400. */
  3401. if ( adapter->sglen > MAX_SGLIST )
  3402. adapter->sglen = MAX_SGLIST;
  3403. }
  3404. return;
  3405. }
  3406. /**
  3407. * mega_support_cluster()
  3408. * @adapter - pointer to our soft state
  3409. *
  3410. * Find out if this firmware support cluster calls.
  3411. */
  3412. static int
  3413. mega_support_cluster(adapter_t *adapter)
  3414. {
  3415. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3416. mbox_t *mbox;
  3417. mbox = (mbox_t *)raw_mbox;
  3418. memset(mbox, 0, sizeof(raw_mbox));
  3419. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3420. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3421. /*
  3422. * Try to get the initiator id. This command will succeed iff the
  3423. * clustering is available on this HBA.
  3424. */
  3425. raw_mbox[0] = MEGA_GET_TARGET_ID;
  3426. if( issue_scb_block(adapter, raw_mbox) == 0 ) {
  3427. /*
  3428. * Cluster support available. Get the initiator target id.
  3429. * Tell our id to mid-layer too.
  3430. */
  3431. adapter->this_id = *(u32 *)adapter->mega_buffer;
  3432. adapter->host->this_id = adapter->this_id;
  3433. return 1;
  3434. }
  3435. return 0;
  3436. }
  3437. /**
  3438. * mega_adapinq()
  3439. * @adapter - pointer to our soft state
  3440. * @dma_handle - DMA address of the buffer
  3441. *
  3442. * Issue internal comamnds while interrupts are available.
  3443. * We only issue direct mailbox commands from within the driver. ioctl()
  3444. * interface using these routines can issue passthru commands.
  3445. */
  3446. static int
  3447. mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
  3448. {
  3449. megacmd_t mc;
  3450. memset(&mc, 0, sizeof(megacmd_t));
  3451. if( adapter->flag & BOARD_40LD ) {
  3452. mc.cmd = FC_NEW_CONFIG;
  3453. mc.opcode = NC_SUBOP_ENQUIRY3;
  3454. mc.subopcode = ENQ3_GET_SOLICITED_FULL;
  3455. }
  3456. else {
  3457. mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  3458. }
  3459. mc.xferaddr = (u32)dma_handle;
  3460. if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
  3461. return -1;
  3462. }
  3463. return 0;
  3464. }
  3465. /** mega_internal_dev_inquiry()
  3466. * @adapter - pointer to our soft state
  3467. * @ch - channel for this device
  3468. * @tgt - ID of this device
  3469. * @buf_dma_handle - DMA address of the buffer
  3470. *
  3471. * Issue the scsi inquiry for the specified device.
  3472. */
  3473. static int
  3474. mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
  3475. dma_addr_t buf_dma_handle)
  3476. {
  3477. mega_passthru *pthru;
  3478. dma_addr_t pthru_dma_handle;
  3479. megacmd_t mc;
  3480. int rval;
  3481. struct pci_dev *pdev;
  3482. /*
  3483. * For all internal commands, the buffer must be allocated in <4GB
  3484. * address range
  3485. */
  3486. if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
  3487. pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
  3488. &pthru_dma_handle);
  3489. if( pthru == NULL ) {
  3490. free_local_pdev(pdev);
  3491. return -1;
  3492. }
  3493. pthru->timeout = 2;
  3494. pthru->ars = 1;
  3495. pthru->reqsenselen = 14;
  3496. pthru->islogical = 0;
  3497. pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
  3498. pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
  3499. pthru->cdblen = 6;
  3500. pthru->cdb[0] = INQUIRY;
  3501. pthru->cdb[1] = 0;
  3502. pthru->cdb[2] = 0;
  3503. pthru->cdb[3] = 0;
  3504. pthru->cdb[4] = 255;
  3505. pthru->cdb[5] = 0;
  3506. pthru->dataxferaddr = (u32)buf_dma_handle;
  3507. pthru->dataxferlen = 256;
  3508. memset(&mc, 0, sizeof(megacmd_t));
  3509. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  3510. mc.xferaddr = (u32)pthru_dma_handle;
  3511. rval = mega_internal_command(adapter, &mc, pthru);
  3512. pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
  3513. pthru_dma_handle);
  3514. free_local_pdev(pdev);
  3515. return rval;
  3516. }
  3517. /**
  3518. * mega_internal_command()
  3519. * @adapter - pointer to our soft state
  3520. * @mc - the mailbox command
  3521. * @pthru - Passthru structure for DCDB commands
  3522. *
  3523. * Issue the internal commands in interrupt mode.
  3524. * The last argument is the address of the passthru structure if the command
  3525. * to be fired is a passthru command
  3526. *
  3527. * lockscope specifies whether the caller has already acquired the lock. Of
  3528. * course, the caller must know which lock we are talking about.
  3529. *
  3530. * Note: parameter 'pthru' is null for non-passthru commands.
  3531. */
  3532. static int
  3533. mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
  3534. {
  3535. Scsi_Cmnd *scmd;
  3536. struct scsi_device *sdev;
  3537. unsigned long flags = 0;
  3538. scb_t *scb;
  3539. int rval;
  3540. /*
  3541. * The internal commands share one command id and hence are
  3542. * serialized. This is so because we want to reserve maximum number of
  3543. * available command ids for the I/O commands.
  3544. */
  3545. down(&adapter->int_mtx);
  3546. scb = &adapter->int_scb;
  3547. memset(scb, 0, sizeof(scb_t));
  3548. scmd = &adapter->int_scmd;
  3549. memset(scmd, 0, sizeof(Scsi_Cmnd));
  3550. sdev = kmalloc(sizeof(struct scsi_device), GFP_KERNEL);
  3551. memset(sdev, 0, sizeof(struct scsi_device));
  3552. scmd->device = sdev;
  3553. scmd->device->host = adapter->host;
  3554. scmd->buffer = (void *)scb;
  3555. scmd->cmnd[0] = MEGA_INTERNAL_CMD;
  3556. scb->state |= SCB_ACTIVE;
  3557. scb->cmd = scmd;
  3558. memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
  3559. /*
  3560. * Is it a passthru command
  3561. */
  3562. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3563. scb->pthru = pthru;
  3564. }
  3565. scb->idx = CMDID_INT_CMDS;
  3566. megaraid_queue(scmd, mega_internal_done);
  3567. wait_for_completion(&adapter->int_waitq);
  3568. rval = scmd->result;
  3569. mc->status = scmd->result;
  3570. kfree(sdev);
  3571. /*
  3572. * Print a debug message for all failed commands. Applications can use
  3573. * this information.
  3574. */
  3575. if( scmd->result && trace_level ) {
  3576. printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
  3577. mc->cmd, mc->opcode, mc->subopcode, scmd->result);
  3578. }
  3579. up(&adapter->int_mtx);
  3580. return rval;
  3581. }
  3582. /**
  3583. * mega_internal_done()
  3584. * @scmd - internal scsi command
  3585. *
  3586. * Callback routine for internal commands.
  3587. */
  3588. static void
  3589. mega_internal_done(Scsi_Cmnd *scmd)
  3590. {
  3591. adapter_t *adapter;
  3592. adapter = (adapter_t *)scmd->device->host->hostdata;
  3593. complete(&adapter->int_waitq);
  3594. }
  3595. static struct scsi_host_template megaraid_template = {
  3596. .module = THIS_MODULE,
  3597. .name = "MegaRAID",
  3598. .proc_name = "megaraid",
  3599. .info = megaraid_info,
  3600. .queuecommand = megaraid_queue,
  3601. .bios_param = megaraid_biosparam,
  3602. .max_sectors = MAX_SECTORS_PER_IO,
  3603. .can_queue = MAX_COMMANDS,
  3604. .this_id = DEFAULT_INITIATOR_ID,
  3605. .sg_tablesize = MAX_SGLIST,
  3606. .cmd_per_lun = DEF_CMD_PER_LUN,
  3607. .use_clustering = ENABLE_CLUSTERING,
  3608. .eh_abort_handler = megaraid_abort,
  3609. .eh_device_reset_handler = megaraid_reset,
  3610. .eh_bus_reset_handler = megaraid_reset,
  3611. .eh_host_reset_handler = megaraid_reset,
  3612. };
  3613. static int __devinit
  3614. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3615. {
  3616. struct Scsi_Host *host;
  3617. adapter_t *adapter;
  3618. unsigned long mega_baseport, tbase, flag = 0;
  3619. u16 subsysid, subsysvid;
  3620. u8 pci_bus, pci_dev_func;
  3621. int irq, i, j;
  3622. int error = -ENODEV;
  3623. if (pci_enable_device(pdev))
  3624. goto out;
  3625. pci_set_master(pdev);
  3626. pci_bus = pdev->bus->number;
  3627. pci_dev_func = pdev->devfn;
  3628. /*
  3629. * The megaraid3 stuff reports the ID of the Intel part which is not
  3630. * remotely specific to the megaraid
  3631. */
  3632. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  3633. u16 magic;
  3634. /*
  3635. * Don't fall over the Compaq management cards using the same
  3636. * PCI identifier
  3637. */
  3638. if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
  3639. pdev->subsystem_device == 0xC000)
  3640. return -ENODEV;
  3641. /* Now check the magic signature byte */
  3642. pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
  3643. if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
  3644. return -ENODEV;
  3645. /* Ok it is probably a megaraid */
  3646. }
  3647. /*
  3648. * For these vendor and device ids, signature offsets are not
  3649. * valid and 64 bit is implicit
  3650. */
  3651. if (id->driver_data & BOARD_64BIT)
  3652. flag |= BOARD_64BIT;
  3653. else {
  3654. u32 magic64;
  3655. pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
  3656. if (magic64 == HBA_SIGNATURE_64BIT)
  3657. flag |= BOARD_64BIT;
  3658. }
  3659. subsysvid = pdev->subsystem_vendor;
  3660. subsysid = pdev->subsystem_device;
  3661. printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
  3662. id->vendor, id->device, pci_bus);
  3663. printk("slot %d:func %d\n",
  3664. PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
  3665. /* Read the base port and IRQ from PCI */
  3666. mega_baseport = pci_resource_start(pdev, 0);
  3667. irq = pdev->irq;
  3668. tbase = mega_baseport;
  3669. if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
  3670. flag |= BOARD_MEMMAP;
  3671. if (!request_mem_region(mega_baseport, 128, "megaraid")) {
  3672. printk(KERN_WARNING "megaraid: mem region busy!\n");
  3673. goto out_disable_device;
  3674. }
  3675. mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
  3676. if (!mega_baseport) {
  3677. printk(KERN_WARNING
  3678. "megaraid: could not map hba memory\n");
  3679. goto out_release_region;
  3680. }
  3681. } else {
  3682. flag |= BOARD_IOMAP;
  3683. mega_baseport += 0x10;
  3684. if (!request_region(mega_baseport, 16, "megaraid"))
  3685. goto out_disable_device;
  3686. }
  3687. /* Initialize SCSI Host structure */
  3688. host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
  3689. if (!host)
  3690. goto out_iounmap;
  3691. adapter = (adapter_t *)host->hostdata;
  3692. memset(adapter, 0, sizeof(adapter_t));
  3693. printk(KERN_NOTICE
  3694. "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
  3695. host->host_no, mega_baseport, irq);
  3696. adapter->base = mega_baseport;
  3697. INIT_LIST_HEAD(&adapter->free_list);
  3698. INIT_LIST_HEAD(&adapter->pending_list);
  3699. INIT_LIST_HEAD(&adapter->completed_list);
  3700. adapter->flag = flag;
  3701. spin_lock_init(&adapter->lock);
  3702. scsi_assign_lock(host, &adapter->lock);
  3703. host->cmd_per_lun = max_cmd_per_lun;
  3704. host->max_sectors = max_sectors_per_io;
  3705. adapter->dev = pdev;
  3706. adapter->host = host;
  3707. adapter->host->irq = irq;
  3708. if (flag & BOARD_MEMMAP)
  3709. adapter->host->base = tbase;
  3710. else {
  3711. adapter->host->io_port = tbase;
  3712. adapter->host->n_io_port = 16;
  3713. }
  3714. adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
  3715. /*
  3716. * Allocate buffer to issue internal commands.
  3717. */
  3718. adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
  3719. MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
  3720. if (!adapter->mega_buffer) {
  3721. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3722. goto out_host_put;
  3723. }
  3724. adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
  3725. if (!adapter->scb_list) {
  3726. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3727. goto out_free_cmd_buffer;
  3728. }
  3729. if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
  3730. megaraid_isr_memmapped : megaraid_isr_iomapped,
  3731. SA_SHIRQ, "megaraid", adapter)) {
  3732. printk(KERN_WARNING
  3733. "megaraid: Couldn't register IRQ %d!\n", irq);
  3734. goto out_free_scb_list;
  3735. }
  3736. if (mega_setup_mailbox(adapter))
  3737. goto out_free_irq;
  3738. if (mega_query_adapter(adapter))
  3739. goto out_free_mbox;
  3740. /*
  3741. * Have checks for some buggy f/w
  3742. */
  3743. if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
  3744. /*
  3745. * Which firmware
  3746. */
  3747. if (!strcmp(adapter->fw_version, "3.00") ||
  3748. !strcmp(adapter->fw_version, "3.01")) {
  3749. printk( KERN_WARNING
  3750. "megaraid: Your card is a Dell PERC "
  3751. "2/SC RAID controller with "
  3752. "firmware\nmegaraid: 3.00 or 3.01. "
  3753. "This driver is known to have "
  3754. "corruption issues\nmegaraid: with "
  3755. "those firmware versions on this "
  3756. "specific card. In order\nmegaraid: "
  3757. "to protect your data, please upgrade "
  3758. "your firmware to version\nmegaraid: "
  3759. "3.10 or later, available from the "
  3760. "Dell Technical Support web\n"
  3761. "megaraid: site at\nhttp://support."
  3762. "dell.com/us/en/filelib/download/"
  3763. "index.asp?fileid=2940\n"
  3764. );
  3765. }
  3766. }
  3767. /*
  3768. * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
  3769. * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
  3770. * support, since this firmware cannot handle 64 bit
  3771. * addressing
  3772. */
  3773. if ((subsysvid == HP_SUBSYS_VID) &&
  3774. ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
  3775. /*
  3776. * which firmware
  3777. */
  3778. if (!strcmp(adapter->fw_version, "H01.07") ||
  3779. !strcmp(adapter->fw_version, "H01.08") ||
  3780. !strcmp(adapter->fw_version, "H01.09") ) {
  3781. printk(KERN_WARNING
  3782. "megaraid: Firmware H.01.07, "
  3783. "H.01.08, and H.01.09 on 1M/2M "
  3784. "controllers\n"
  3785. "megaraid: do not support 64 bit "
  3786. "addressing.\nmegaraid: DISABLING "
  3787. "64 bit support.\n");
  3788. adapter->flag &= ~BOARD_64BIT;
  3789. }
  3790. }
  3791. if (mega_is_bios_enabled(adapter))
  3792. mega_hbas[hba_count].is_bios_enabled = 1;
  3793. mega_hbas[hba_count].hostdata_addr = adapter;
  3794. /*
  3795. * Find out which channel is raid and which is scsi. This is
  3796. * for ROMB support.
  3797. */
  3798. mega_enum_raid_scsi(adapter);
  3799. /*
  3800. * Find out if a logical drive is set as the boot drive. If
  3801. * there is one, will make that as the first logical drive.
  3802. * ROMB: Do we have to boot from a physical drive. Then all
  3803. * the physical drives would appear before the logical disks.
  3804. * Else, all the physical drives would be exported to the mid
  3805. * layer after logical drives.
  3806. */
  3807. mega_get_boot_drv(adapter);
  3808. if (adapter->boot_pdrv_enabled) {
  3809. j = adapter->product_info.nchannels;
  3810. for( i = 0; i < j; i++ )
  3811. adapter->logdrv_chan[i] = 0;
  3812. for( i = j; i < NVIRT_CHAN + j; i++ )
  3813. adapter->logdrv_chan[i] = 1;
  3814. } else {
  3815. for (i = 0; i < NVIRT_CHAN; i++)
  3816. adapter->logdrv_chan[i] = 1;
  3817. for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
  3818. adapter->logdrv_chan[i] = 0;
  3819. adapter->mega_ch_class <<= NVIRT_CHAN;
  3820. }
  3821. /*
  3822. * Do we support random deletion and addition of logical
  3823. * drives
  3824. */
  3825. adapter->read_ldidmap = 0; /* set it after first logdrv
  3826. delete cmd */
  3827. adapter->support_random_del = mega_support_random_del(adapter);
  3828. /* Initialize SCBs */
  3829. if (mega_init_scb(adapter))
  3830. goto out_free_mbox;
  3831. /*
  3832. * Reset the pending commands counter
  3833. */
  3834. atomic_set(&adapter->pend_cmds, 0);
  3835. /*
  3836. * Reset the adapter quiescent flag
  3837. */
  3838. atomic_set(&adapter->quiescent, 0);
  3839. hba_soft_state[hba_count] = adapter;
  3840. /*
  3841. * Fill in the structure which needs to be passed back to the
  3842. * application when it does an ioctl() for controller related
  3843. * information.
  3844. */
  3845. i = hba_count;
  3846. mcontroller[i].base = mega_baseport;
  3847. mcontroller[i].irq = irq;
  3848. mcontroller[i].numldrv = adapter->numldrv;
  3849. mcontroller[i].pcibus = pci_bus;
  3850. mcontroller[i].pcidev = id->device;
  3851. mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
  3852. mcontroller[i].pciid = -1;
  3853. mcontroller[i].pcivendor = id->vendor;
  3854. mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
  3855. mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
  3856. /* Set the Mode of addressing to 64 bit if we can */
  3857. if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
  3858. pci_set_dma_mask(pdev, 0xffffffffffffffffULL);
  3859. adapter->has_64bit_addr = 1;
  3860. } else {
  3861. pci_set_dma_mask(pdev, 0xffffffff);
  3862. adapter->has_64bit_addr = 0;
  3863. }
  3864. init_MUTEX(&adapter->int_mtx);
  3865. init_completion(&adapter->int_waitq);
  3866. adapter->this_id = DEFAULT_INITIATOR_ID;
  3867. adapter->host->this_id = DEFAULT_INITIATOR_ID;
  3868. #if MEGA_HAVE_CLUSTERING
  3869. /*
  3870. * Is cluster support enabled on this controller
  3871. * Note: In a cluster the HBAs ( the initiators ) will have
  3872. * different target IDs and we cannot assume it to be 7. Call
  3873. * to mega_support_cluster() will get the target ids also if
  3874. * the cluster support is available
  3875. */
  3876. adapter->has_cluster = mega_support_cluster(adapter);
  3877. if (adapter->has_cluster) {
  3878. printk(KERN_NOTICE
  3879. "megaraid: Cluster driver, initiator id:%d\n",
  3880. adapter->this_id);
  3881. }
  3882. #endif
  3883. pci_set_drvdata(pdev, host);
  3884. mega_create_proc_entry(hba_count, mega_proc_dir_entry);
  3885. error = scsi_add_host(host, &pdev->dev);
  3886. if (error)
  3887. goto out_free_mbox;
  3888. scsi_scan_host(host);
  3889. hba_count++;
  3890. return 0;
  3891. out_free_mbox:
  3892. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3893. adapter->una_mbox64, adapter->una_mbox64_dma);
  3894. out_free_irq:
  3895. free_irq(adapter->host->irq, adapter);
  3896. out_free_scb_list:
  3897. kfree(adapter->scb_list);
  3898. out_free_cmd_buffer:
  3899. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3900. adapter->mega_buffer, adapter->buf_dma_handle);
  3901. out_host_put:
  3902. scsi_host_put(host);
  3903. out_iounmap:
  3904. if (flag & BOARD_MEMMAP)
  3905. iounmap((void *)mega_baseport);
  3906. out_release_region:
  3907. if (flag & BOARD_MEMMAP)
  3908. release_mem_region(tbase, 128);
  3909. else
  3910. release_region(mega_baseport, 16);
  3911. out_disable_device:
  3912. pci_disable_device(pdev);
  3913. out:
  3914. return error;
  3915. }
  3916. static void
  3917. __megaraid_shutdown(adapter_t *adapter)
  3918. {
  3919. u_char raw_mbox[sizeof(struct mbox_out)];
  3920. mbox_t *mbox = (mbox_t *)raw_mbox;
  3921. int i;
  3922. /* Flush adapter cache */
  3923. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3924. raw_mbox[0] = FLUSH_ADAPTER;
  3925. free_irq(adapter->host->irq, adapter);
  3926. /* Issue a blocking (interrupts disabled) command to the card */
  3927. issue_scb_block(adapter, raw_mbox);
  3928. /* Flush disks cache */
  3929. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3930. raw_mbox[0] = FLUSH_SYSTEM;
  3931. /* Issue a blocking (interrupts disabled) command to the card */
  3932. issue_scb_block(adapter, raw_mbox);
  3933. if (atomic_read(&adapter->pend_cmds) > 0)
  3934. printk(KERN_WARNING "megaraid: pending commands!!\n");
  3935. /*
  3936. * Have a delibrate delay to make sure all the caches are
  3937. * actually flushed.
  3938. */
  3939. for (i = 0; i <= 10; i++)
  3940. mdelay(1000);
  3941. }
  3942. static void
  3943. megaraid_remove_one(struct pci_dev *pdev)
  3944. {
  3945. struct Scsi_Host *host = pci_get_drvdata(pdev);
  3946. adapter_t *adapter = (adapter_t *)host->hostdata;
  3947. char buf[12] = { 0 };
  3948. scsi_remove_host(host);
  3949. __megaraid_shutdown(adapter);
  3950. /* Free our resources */
  3951. if (adapter->flag & BOARD_MEMMAP) {
  3952. iounmap((void *)adapter->base);
  3953. release_mem_region(adapter->host->base, 128);
  3954. } else
  3955. release_region(adapter->base, 16);
  3956. mega_free_sgl(adapter);
  3957. #ifdef CONFIG_PROC_FS
  3958. if (adapter->controller_proc_dir_entry) {
  3959. remove_proc_entry("stat", adapter->controller_proc_dir_entry);
  3960. remove_proc_entry("config",
  3961. adapter->controller_proc_dir_entry);
  3962. remove_proc_entry("mailbox",
  3963. adapter->controller_proc_dir_entry);
  3964. #if MEGA_HAVE_ENH_PROC
  3965. remove_proc_entry("rebuild-rate",
  3966. adapter->controller_proc_dir_entry);
  3967. remove_proc_entry("battery-status",
  3968. adapter->controller_proc_dir_entry);
  3969. remove_proc_entry("diskdrives-ch0",
  3970. adapter->controller_proc_dir_entry);
  3971. remove_proc_entry("diskdrives-ch1",
  3972. adapter->controller_proc_dir_entry);
  3973. remove_proc_entry("diskdrives-ch2",
  3974. adapter->controller_proc_dir_entry);
  3975. remove_proc_entry("diskdrives-ch3",
  3976. adapter->controller_proc_dir_entry);
  3977. remove_proc_entry("raiddrives-0-9",
  3978. adapter->controller_proc_dir_entry);
  3979. remove_proc_entry("raiddrives-10-19",
  3980. adapter->controller_proc_dir_entry);
  3981. remove_proc_entry("raiddrives-20-29",
  3982. adapter->controller_proc_dir_entry);
  3983. remove_proc_entry("raiddrives-30-39",
  3984. adapter->controller_proc_dir_entry);
  3985. #endif
  3986. sprintf(buf, "hba%d", adapter->host->host_no);
  3987. remove_proc_entry(buf, mega_proc_dir_entry);
  3988. }
  3989. #endif
  3990. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3991. adapter->mega_buffer, adapter->buf_dma_handle);
  3992. kfree(adapter->scb_list);
  3993. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3994. adapter->una_mbox64, adapter->una_mbox64_dma);
  3995. scsi_host_put(host);
  3996. pci_disable_device(pdev);
  3997. hba_count--;
  3998. }
  3999. static void
  4000. megaraid_shutdown(struct pci_dev *pdev)
  4001. {
  4002. struct Scsi_Host *host = pci_get_drvdata(pdev);
  4003. adapter_t *adapter = (adapter_t *)host->hostdata;
  4004. __megaraid_shutdown(adapter);
  4005. }
  4006. static struct pci_device_id megaraid_pci_tbl[] = {
  4007. {PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DISCOVERY,
  4008. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4009. {PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_PERC4_DI,
  4010. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BOARD_64BIT},
  4011. {PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_PERC4_QC_VERDE,
  4012. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BOARD_64BIT},
  4013. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
  4014. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4015. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
  4016. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4017. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID3,
  4018. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4019. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
  4020. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4021. {PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_AMI_MEGARAID3,
  4022. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4023. {0,}
  4024. };
  4025. MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
  4026. static struct pci_driver megaraid_pci_driver = {
  4027. .name = "megaraid",
  4028. .id_table = megaraid_pci_tbl,
  4029. .probe = megaraid_probe_one,
  4030. .remove = __devexit_p(megaraid_remove_one),
  4031. .shutdown = megaraid_shutdown,
  4032. };
  4033. static int __init megaraid_init(void)
  4034. {
  4035. int error;
  4036. if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
  4037. max_cmd_per_lun = MAX_CMD_PER_LUN;
  4038. if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
  4039. max_mbox_busy_wait = MBOX_BUSY_WAIT;
  4040. #ifdef CONFIG_PROC_FS
  4041. mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
  4042. if (!mega_proc_dir_entry) {
  4043. printk(KERN_WARNING
  4044. "megaraid: failed to create megaraid root\n");
  4045. }
  4046. #endif
  4047. error = pci_module_init(&megaraid_pci_driver);
  4048. if (error) {
  4049. #ifdef CONFIG_PROC_FS
  4050. remove_proc_entry("megaraid", &proc_root);
  4051. #endif
  4052. return error;
  4053. }
  4054. /*
  4055. * Register the driver as a character device, for applications
  4056. * to access it for ioctls.
  4057. * First argument (major) to register_chrdev implies a dynamic
  4058. * major number allocation.
  4059. */
  4060. major = register_chrdev(0, "megadev", &megadev_fops);
  4061. if (!major) {
  4062. printk(KERN_WARNING
  4063. "megaraid: failed to register char device\n");
  4064. }
  4065. return 0;
  4066. }
  4067. static void __exit megaraid_exit(void)
  4068. {
  4069. /*
  4070. * Unregister the character device interface to the driver.
  4071. */
  4072. unregister_chrdev(major, "megadev");
  4073. pci_unregister_driver(&megaraid_pci_driver);
  4074. #ifdef CONFIG_PROC_FS
  4075. remove_proc_entry("megaraid", &proc_root);
  4076. #endif
  4077. }
  4078. module_init(megaraid_init);
  4079. module_exit(megaraid_exit);
  4080. /* vi: set ts=8 sw=8 tw=78: */