megaraid_mbox.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright (c) 2003-2004 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. * FILE : megaraid_mbox.c
  13. * Version : v2.20.5.1 (Nov 16 2006)
  14. *
  15. * Authors:
  16. * Atul Mukker <Atul.Mukker@lsi.com>
  17. * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsi.com>
  18. * Manoj Jose <Manoj.Jose@lsi.com>
  19. * Seokmann Ju
  20. *
  21. * List of supported controllers
  22. *
  23. * OEM Product Name VID DID SSVID SSID
  24. * --- ------------ --- --- ---- ----
  25. * Dell PERC3/QC 101E 1960 1028 0471
  26. * Dell PERC3/DC 101E 1960 1028 0493
  27. * Dell PERC3/SC 101E 1960 1028 0475
  28. * Dell PERC3/Di 1028 1960 1028 0123
  29. * Dell PERC4/SC 1000 1960 1028 0520
  30. * Dell PERC4/DC 1000 1960 1028 0518
  31. * Dell PERC4/QC 1000 0407 1028 0531
  32. * Dell PERC4/Di 1028 000F 1028 014A
  33. * Dell PERC 4e/Si 1028 0013 1028 016c
  34. * Dell PERC 4e/Di 1028 0013 1028 016d
  35. * Dell PERC 4e/Di 1028 0013 1028 016e
  36. * Dell PERC 4e/Di 1028 0013 1028 016f
  37. * Dell PERC 4e/Di 1028 0013 1028 0170
  38. * Dell PERC 4e/DC 1000 0408 1028 0002
  39. * Dell PERC 4e/SC 1000 0408 1028 0001
  40. *
  41. *
  42. * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
  43. * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
  44. * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
  45. * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
  46. * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
  47. * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
  48. * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
  49. * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
  50. * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
  51. * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
  52. * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
  53. * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
  54. *
  55. * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
  56. * INTEL RAID Controller SRCS16 1000 1960 8086 0523
  57. * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
  58. * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
  59. * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
  60. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
  61. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
  62. * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
  63. *
  64. * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
  65. *
  66. * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
  67. *
  68. * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
  69. *
  70. * For history of changes, see Documentation/scsi/ChangeLog.megaraid
  71. */
  72. #include <linux/slab.h>
  73. #include <linux/module.h>
  74. #include "megaraid_mbox.h"
  75. static int megaraid_init(void);
  76. static void megaraid_exit(void);
  77. static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
  78. static void megaraid_detach_one(struct pci_dev *);
  79. static void megaraid_mbox_shutdown(struct pci_dev *);
  80. static int megaraid_io_attach(adapter_t *);
  81. static void megaraid_io_detach(adapter_t *);
  82. static int megaraid_init_mbox(adapter_t *);
  83. static void megaraid_fini_mbox(adapter_t *);
  84. static int megaraid_alloc_cmd_packets(adapter_t *);
  85. static void megaraid_free_cmd_packets(adapter_t *);
  86. static int megaraid_mbox_setup_dma_pools(adapter_t *);
  87. static void megaraid_mbox_teardown_dma_pools(adapter_t *);
  88. static int megaraid_sysfs_alloc_resources(adapter_t *);
  89. static void megaraid_sysfs_free_resources(adapter_t *);
  90. static int megaraid_abort_handler(struct scsi_cmnd *);
  91. static int megaraid_reset_handler(struct scsi_cmnd *);
  92. static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
  93. static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
  94. static int megaraid_busywait_mbox(mraid_device_t *);
  95. static int megaraid_mbox_product_info(adapter_t *);
  96. static int megaraid_mbox_extended_cdb(adapter_t *);
  97. static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
  98. static int megaraid_mbox_support_random_del(adapter_t *);
  99. static int megaraid_mbox_get_max_sg(adapter_t *);
  100. static void megaraid_mbox_enum_raid_scsi(adapter_t *);
  101. static void megaraid_mbox_flush_cache(adapter_t *);
  102. static int megaraid_mbox_fire_sync_cmd(adapter_t *);
  103. static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
  104. static void megaraid_mbox_setup_device_map(adapter_t *);
  105. static int megaraid_queue_command(struct Scsi_Host *, struct scsi_cmnd *);
  106. static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
  107. static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
  108. static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
  109. struct scsi_cmnd *);
  110. static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
  111. struct scsi_cmnd *);
  112. static irqreturn_t megaraid_isr(int, void *);
  113. static void megaraid_mbox_dpc(unsigned long);
  114. static ssize_t megaraid_sysfs_show_app_hndl(struct device *, struct device_attribute *attr, char *);
  115. static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
  116. static int megaraid_cmm_register(adapter_t *);
  117. static int megaraid_cmm_unregister(adapter_t *);
  118. static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
  119. static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
  120. static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
  121. static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
  122. static int wait_till_fw_empty(adapter_t *);
  123. MODULE_AUTHOR("megaraidlinux@lsi.com");
  124. MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
  125. MODULE_LICENSE("GPL");
  126. MODULE_VERSION(MEGARAID_VERSION);
  127. /*
  128. * ### modules parameters for driver ###
  129. */
  130. /*
  131. * Set to enable driver to expose unconfigured disk to kernel
  132. */
  133. static int megaraid_expose_unconf_disks = 0;
  134. module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
  135. MODULE_PARM_DESC(unconf_disks,
  136. "Set to expose unconfigured disks to kernel (default=0)");
  137. /*
  138. * driver wait time if the adapter's mailbox is busy
  139. */
  140. static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  141. module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
  142. MODULE_PARM_DESC(busy_wait,
  143. "Max wait for mailbox in microseconds if busy (default=10)");
  144. /*
  145. * number of sectors per IO command
  146. */
  147. static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
  148. module_param_named(max_sectors, megaraid_max_sectors, int, 0);
  149. MODULE_PARM_DESC(max_sectors,
  150. "Maximum number of sectors per IO command (default=128)");
  151. /*
  152. * number of commands per logical unit
  153. */
  154. static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
  155. module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
  156. MODULE_PARM_DESC(cmd_per_lun,
  157. "Maximum number of commands per logical unit (default=64)");
  158. /*
  159. * Fast driver load option, skip scanning for physical devices during load.
  160. * This would result in non-disk devices being skipped during driver load
  161. * time. These can be later added though, using /proc/scsi/scsi
  162. */
  163. static unsigned int megaraid_fast_load = 0;
  164. module_param_named(fast_load, megaraid_fast_load, int, 0);
  165. MODULE_PARM_DESC(fast_load,
  166. "Faster loading of the driver, skips physical devices! (default=0)");
  167. /*
  168. * mraid_debug level - threshold for amount of information to be displayed by
  169. * the driver. This level can be changed through modules parameters, ioctl or
  170. * sysfs/proc interface. By default, print the announcement messages only.
  171. */
  172. int mraid_debug_level = CL_ANN;
  173. module_param_named(debug_level, mraid_debug_level, int, 0);
  174. MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
  175. /*
  176. * ### global data ###
  177. */
  178. static uint8_t megaraid_mbox_version[8] =
  179. { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
  180. /*
  181. * PCI table for all supported controllers.
  182. */
  183. static struct pci_device_id pci_id_table_g[] = {
  184. {
  185. PCI_VENDOR_ID_DELL,
  186. PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
  187. PCI_VENDOR_ID_DELL,
  188. PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
  189. },
  190. {
  191. PCI_VENDOR_ID_LSI_LOGIC,
  192. PCI_DEVICE_ID_PERC4_SC,
  193. PCI_VENDOR_ID_DELL,
  194. PCI_SUBSYS_ID_PERC4_SC,
  195. },
  196. {
  197. PCI_VENDOR_ID_LSI_LOGIC,
  198. PCI_DEVICE_ID_PERC4_DC,
  199. PCI_VENDOR_ID_DELL,
  200. PCI_SUBSYS_ID_PERC4_DC,
  201. },
  202. {
  203. PCI_VENDOR_ID_LSI_LOGIC,
  204. PCI_DEVICE_ID_VERDE,
  205. PCI_ANY_ID,
  206. PCI_ANY_ID,
  207. },
  208. {
  209. PCI_VENDOR_ID_DELL,
  210. PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
  211. PCI_VENDOR_ID_DELL,
  212. PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
  213. },
  214. {
  215. PCI_VENDOR_ID_DELL,
  216. PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
  217. PCI_VENDOR_ID_DELL,
  218. PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
  219. },
  220. {
  221. PCI_VENDOR_ID_DELL,
  222. PCI_DEVICE_ID_PERC4E_DI_KOBUK,
  223. PCI_VENDOR_ID_DELL,
  224. PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
  225. },
  226. {
  227. PCI_VENDOR_ID_DELL,
  228. PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
  229. PCI_VENDOR_ID_DELL,
  230. PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
  231. },
  232. {
  233. PCI_VENDOR_ID_DELL,
  234. PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
  235. PCI_VENDOR_ID_DELL,
  236. PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
  237. },
  238. {
  239. PCI_VENDOR_ID_DELL,
  240. PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
  241. PCI_VENDOR_ID_DELL,
  242. PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
  243. },
  244. {
  245. PCI_VENDOR_ID_LSI_LOGIC,
  246. PCI_DEVICE_ID_DOBSON,
  247. PCI_ANY_ID,
  248. PCI_ANY_ID,
  249. },
  250. {
  251. PCI_VENDOR_ID_AMI,
  252. PCI_DEVICE_ID_AMI_MEGARAID3,
  253. PCI_ANY_ID,
  254. PCI_ANY_ID,
  255. },
  256. {
  257. PCI_VENDOR_ID_LSI_LOGIC,
  258. PCI_DEVICE_ID_AMI_MEGARAID3,
  259. PCI_ANY_ID,
  260. PCI_ANY_ID,
  261. },
  262. {
  263. PCI_VENDOR_ID_LSI_LOGIC,
  264. PCI_DEVICE_ID_LINDSAY,
  265. PCI_ANY_ID,
  266. PCI_ANY_ID,
  267. },
  268. {0} /* Terminating entry */
  269. };
  270. MODULE_DEVICE_TABLE(pci, pci_id_table_g);
  271. static struct pci_driver megaraid_pci_driver = {
  272. .name = "megaraid",
  273. .id_table = pci_id_table_g,
  274. .probe = megaraid_probe_one,
  275. .remove = __devexit_p(megaraid_detach_one),
  276. .shutdown = megaraid_mbox_shutdown,
  277. };
  278. // definitions for the device attributes for exporting logical drive number
  279. // for a scsi address (Host, Channel, Id, Lun)
  280. DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
  281. NULL);
  282. // Host template initializer for megaraid mbox sysfs device attributes
  283. static struct device_attribute *megaraid_shost_attrs[] = {
  284. &dev_attr_megaraid_mbox_app_hndl,
  285. NULL,
  286. };
  287. DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
  288. // Host template initializer for megaraid mbox sysfs device attributes
  289. static struct device_attribute *megaraid_sdev_attrs[] = {
  290. &dev_attr_megaraid_mbox_ld,
  291. NULL,
  292. };
  293. /**
  294. * megaraid_change_queue_depth - Change the device's queue depth
  295. * @sdev: scsi device struct
  296. * @qdepth: depth to set
  297. * @reason: calling context
  298. *
  299. * Return value:
  300. * actual depth set
  301. */
  302. static int megaraid_change_queue_depth(struct scsi_device *sdev, int qdepth,
  303. int reason)
  304. {
  305. if (reason != SCSI_QDEPTH_DEFAULT)
  306. return -EOPNOTSUPP;
  307. if (qdepth > MBOX_MAX_SCSI_CMDS)
  308. qdepth = MBOX_MAX_SCSI_CMDS;
  309. scsi_adjust_queue_depth(sdev, 0, qdepth);
  310. return sdev->queue_depth;
  311. }
  312. /*
  313. * Scsi host template for megaraid unified driver
  314. */
  315. static struct scsi_host_template megaraid_template_g = {
  316. .module = THIS_MODULE,
  317. .name = "LSI Logic MegaRAID driver",
  318. .proc_name = "megaraid",
  319. .queuecommand = megaraid_queue_command,
  320. .eh_abort_handler = megaraid_abort_handler,
  321. .eh_device_reset_handler = megaraid_reset_handler,
  322. .eh_bus_reset_handler = megaraid_reset_handler,
  323. .eh_host_reset_handler = megaraid_reset_handler,
  324. .change_queue_depth = megaraid_change_queue_depth,
  325. .use_clustering = ENABLE_CLUSTERING,
  326. .sdev_attrs = megaraid_sdev_attrs,
  327. .shost_attrs = megaraid_shost_attrs,
  328. };
  329. /**
  330. * megaraid_init - module load hook
  331. *
  332. * We register ourselves as hotplug enabled module and let PCI subsystem
  333. * discover our adapters.
  334. */
  335. static int __init
  336. megaraid_init(void)
  337. {
  338. int rval;
  339. // Announce the driver version
  340. con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
  341. MEGARAID_EXT_VERSION));
  342. // check validity of module parameters
  343. if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
  344. con_log(CL_ANN, (KERN_WARNING
  345. "megaraid mailbox: max commands per lun reset to %d\n",
  346. MBOX_MAX_SCSI_CMDS));
  347. megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
  348. }
  349. // register as a PCI hot-plug driver module
  350. rval = pci_register_driver(&megaraid_pci_driver);
  351. if (rval < 0) {
  352. con_log(CL_ANN, (KERN_WARNING
  353. "megaraid: could not register hotplug support.\n"));
  354. }
  355. return rval;
  356. }
  357. /**
  358. * megaraid_exit - driver unload entry point
  359. *
  360. * We simply unwrap the megaraid_init routine here.
  361. */
  362. static void __exit
  363. megaraid_exit(void)
  364. {
  365. con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
  366. // unregister as PCI hotplug driver
  367. pci_unregister_driver(&megaraid_pci_driver);
  368. return;
  369. }
  370. /**
  371. * megaraid_probe_one - PCI hotplug entry point
  372. * @pdev : handle to this controller's PCI configuration space
  373. * @id : pci device id of the class of controllers
  374. *
  375. * This routine should be called whenever a new adapter is detected by the
  376. * PCI hotplug susbsystem.
  377. */
  378. static int __devinit
  379. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  380. {
  381. adapter_t *adapter;
  382. // detected a new controller
  383. con_log(CL_ANN, (KERN_INFO
  384. "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  385. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  386. pdev->subsystem_device));
  387. con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
  388. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
  389. if (pci_enable_device(pdev)) {
  390. con_log(CL_ANN, (KERN_WARNING
  391. "megaraid: pci_enable_device failed\n"));
  392. return -ENODEV;
  393. }
  394. // Enable bus-mastering on this controller
  395. pci_set_master(pdev);
  396. // Allocate the per driver initialization structure
  397. adapter = kzalloc(sizeof(adapter_t), GFP_KERNEL);
  398. if (adapter == NULL) {
  399. con_log(CL_ANN, (KERN_WARNING
  400. "megaraid: out of memory, %s %d.\n", __func__, __LINE__));
  401. goto out_probe_one;
  402. }
  403. // set up PCI related soft state and other pre-known parameters
  404. adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
  405. adapter->irq = pdev->irq;
  406. adapter->pdev = pdev;
  407. atomic_set(&adapter->being_detached, 0);
  408. // Setup the default DMA mask. This would be changed later on
  409. // depending on hardware capabilities
  410. if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(32)) != 0) {
  411. con_log(CL_ANN, (KERN_WARNING
  412. "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
  413. goto out_free_adapter;
  414. }
  415. // Initialize the synchronization lock for kernel and LLD
  416. spin_lock_init(&adapter->lock);
  417. // Initialize the command queues: the list of free SCBs and the list
  418. // of pending SCBs.
  419. INIT_LIST_HEAD(&adapter->kscb_pool);
  420. spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
  421. INIT_LIST_HEAD(&adapter->pend_list);
  422. spin_lock_init(PENDING_LIST_LOCK(adapter));
  423. INIT_LIST_HEAD(&adapter->completed_list);
  424. spin_lock_init(COMPLETED_LIST_LOCK(adapter));
  425. // Start the mailbox based controller
  426. if (megaraid_init_mbox(adapter) != 0) {
  427. con_log(CL_ANN, (KERN_WARNING
  428. "megaraid: maibox adapter did not initialize\n"));
  429. goto out_free_adapter;
  430. }
  431. // Register with LSI Common Management Module
  432. if (megaraid_cmm_register(adapter) != 0) {
  433. con_log(CL_ANN, (KERN_WARNING
  434. "megaraid: could not register with management module\n"));
  435. goto out_fini_mbox;
  436. }
  437. // setup adapter handle in PCI soft state
  438. pci_set_drvdata(pdev, adapter);
  439. // attach with scsi mid-layer
  440. if (megaraid_io_attach(adapter) != 0) {
  441. con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
  442. goto out_cmm_unreg;
  443. }
  444. return 0;
  445. out_cmm_unreg:
  446. pci_set_drvdata(pdev, NULL);
  447. megaraid_cmm_unregister(adapter);
  448. out_fini_mbox:
  449. megaraid_fini_mbox(adapter);
  450. out_free_adapter:
  451. kfree(adapter);
  452. out_probe_one:
  453. pci_disable_device(pdev);
  454. return -ENODEV;
  455. }
  456. /**
  457. * megaraid_detach_one - release framework resources and call LLD release routine
  458. * @pdev : handle for our PCI cofiguration space
  459. *
  460. * This routine is called during driver unload. We free all the allocated
  461. * resources and call the corresponding LLD so that it can also release all
  462. * its resources.
  463. *
  464. * This routine is also called from the PCI hotplug system.
  465. */
  466. static void
  467. megaraid_detach_one(struct pci_dev *pdev)
  468. {
  469. adapter_t *adapter;
  470. struct Scsi_Host *host;
  471. // Start a rollback on this adapter
  472. adapter = pci_get_drvdata(pdev);
  473. if (!adapter) {
  474. con_log(CL_ANN, (KERN_CRIT
  475. "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  476. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  477. pdev->subsystem_device));
  478. return;
  479. }
  480. else {
  481. con_log(CL_ANN, (KERN_NOTICE
  482. "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  483. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  484. pdev->subsystem_device));
  485. }
  486. host = adapter->host;
  487. // do not allow any more requests from the management module for this
  488. // adapter.
  489. // FIXME: How do we account for the request which might still be
  490. // pending with us?
  491. atomic_set(&adapter->being_detached, 1);
  492. // detach from the IO sub-system
  493. megaraid_io_detach(adapter);
  494. // reset the device state in the PCI structure. We check this
  495. // condition when we enter here. If the device state is NULL,
  496. // that would mean the device has already been removed
  497. pci_set_drvdata(pdev, NULL);
  498. // Unregister from common management module
  499. //
  500. // FIXME: this must return success or failure for conditions if there
  501. // is a command pending with LLD or not.
  502. megaraid_cmm_unregister(adapter);
  503. // finalize the mailbox based controller and release all resources
  504. megaraid_fini_mbox(adapter);
  505. kfree(adapter);
  506. scsi_host_put(host);
  507. pci_disable_device(pdev);
  508. return;
  509. }
  510. /**
  511. * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
  512. * @pdev : generic driver model device
  513. *
  514. * Shutdown notification, perform flush cache.
  515. */
  516. static void
  517. megaraid_mbox_shutdown(struct pci_dev *pdev)
  518. {
  519. adapter_t *adapter = pci_get_drvdata(pdev);
  520. static int counter;
  521. if (!adapter) {
  522. con_log(CL_ANN, (KERN_WARNING
  523. "megaraid: null device in shutdown\n"));
  524. return;
  525. }
  526. // flush caches now
  527. con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
  528. counter++));
  529. megaraid_mbox_flush_cache(adapter);
  530. con_log(CL_ANN, ("done\n"));
  531. }
  532. /**
  533. * megaraid_io_attach - attach a device with the IO subsystem
  534. * @adapter : controller's soft state
  535. *
  536. * Attach this device with the IO subsystem.
  537. */
  538. static int
  539. megaraid_io_attach(adapter_t *adapter)
  540. {
  541. struct Scsi_Host *host;
  542. // Initialize SCSI Host structure
  543. host = scsi_host_alloc(&megaraid_template_g, 8);
  544. if (!host) {
  545. con_log(CL_ANN, (KERN_WARNING
  546. "megaraid mbox: scsi_register failed\n"));
  547. return -1;
  548. }
  549. SCSIHOST2ADAP(host) = (caddr_t)adapter;
  550. adapter->host = host;
  551. host->irq = adapter->irq;
  552. host->unique_id = adapter->unique_id;
  553. host->can_queue = adapter->max_cmds;
  554. host->this_id = adapter->init_id;
  555. host->sg_tablesize = adapter->sglen;
  556. host->max_sectors = adapter->max_sectors;
  557. host->cmd_per_lun = adapter->cmd_per_lun;
  558. host->max_channel = adapter->max_channel;
  559. host->max_id = adapter->max_target;
  560. host->max_lun = adapter->max_lun;
  561. // notify mid-layer about the new controller
  562. if (scsi_add_host(host, &adapter->pdev->dev)) {
  563. con_log(CL_ANN, (KERN_WARNING
  564. "megaraid mbox: scsi_add_host failed\n"));
  565. scsi_host_put(host);
  566. return -1;
  567. }
  568. scsi_scan_host(host);
  569. return 0;
  570. }
  571. /**
  572. * megaraid_io_detach - detach a device from the IO subsystem
  573. * @adapter : controller's soft state
  574. *
  575. * Detach this device from the IO subsystem.
  576. */
  577. static void
  578. megaraid_io_detach(adapter_t *adapter)
  579. {
  580. struct Scsi_Host *host;
  581. con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
  582. host = adapter->host;
  583. scsi_remove_host(host);
  584. return;
  585. }
  586. /*
  587. * START: Mailbox Low Level Driver
  588. *
  589. * This is section specific to the single mailbox based controllers
  590. */
  591. /**
  592. * megaraid_init_mbox - initialize controller
  593. * @adapter : our soft state
  594. *
  595. * - Allocate 16-byte aligned mailbox memory for firmware handshake
  596. * - Allocate controller's memory resources
  597. * - Find out all initialization data
  598. * - Allocate memory required for all the commands
  599. * - Use internal library of FW routines, build up complete soft state
  600. */
  601. static int __devinit
  602. megaraid_init_mbox(adapter_t *adapter)
  603. {
  604. struct pci_dev *pdev;
  605. mraid_device_t *raid_dev;
  606. int i;
  607. uint32_t magic64;
  608. adapter->ito = MBOX_TIMEOUT;
  609. pdev = adapter->pdev;
  610. /*
  611. * Allocate and initialize the init data structure for mailbox
  612. * controllers
  613. */
  614. raid_dev = kzalloc(sizeof(mraid_device_t), GFP_KERNEL);
  615. if (raid_dev == NULL) return -1;
  616. /*
  617. * Attach the adapter soft state to raid device soft state
  618. */
  619. adapter->raid_device = (caddr_t)raid_dev;
  620. raid_dev->fast_load = megaraid_fast_load;
  621. // our baseport
  622. raid_dev->baseport = pci_resource_start(pdev, 0);
  623. if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
  624. con_log(CL_ANN, (KERN_WARNING
  625. "megaraid: mem region busy\n"));
  626. goto out_free_raid_dev;
  627. }
  628. raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
  629. if (!raid_dev->baseaddr) {
  630. con_log(CL_ANN, (KERN_WARNING
  631. "megaraid: could not map hba memory\n") );
  632. goto out_release_regions;
  633. }
  634. /* initialize the mutual exclusion lock for the mailbox */
  635. spin_lock_init(&raid_dev->mailbox_lock);
  636. /* allocate memory required for commands */
  637. if (megaraid_alloc_cmd_packets(adapter) != 0)
  638. goto out_iounmap;
  639. /*
  640. * Issue SYNC cmd to flush the pending cmds in the adapter
  641. * and initialize its internal state
  642. */
  643. if (megaraid_mbox_fire_sync_cmd(adapter))
  644. con_log(CL_ANN, ("megaraid: sync cmd failed\n"));
  645. /*
  646. * Setup the rest of the soft state using the library of
  647. * FW routines
  648. */
  649. /* request IRQ and register the interrupt service routine */
  650. if (request_irq(adapter->irq, megaraid_isr, IRQF_SHARED, "megaraid",
  651. adapter)) {
  652. con_log(CL_ANN, (KERN_WARNING
  653. "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
  654. goto out_alloc_cmds;
  655. }
  656. // Product info
  657. if (megaraid_mbox_product_info(adapter) != 0)
  658. goto out_free_irq;
  659. // Do we support extended CDBs
  660. adapter->max_cdb_sz = 10;
  661. if (megaraid_mbox_extended_cdb(adapter) == 0) {
  662. adapter->max_cdb_sz = 16;
  663. }
  664. /*
  665. * Do we support cluster environment, if we do, what is the initiator
  666. * id.
  667. * NOTE: In a non-cluster aware firmware environment, the LLD should
  668. * return 7 as initiator id.
  669. */
  670. adapter->ha = 0;
  671. adapter->init_id = -1;
  672. if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
  673. adapter->ha = 1;
  674. }
  675. /*
  676. * Prepare the device ids array to have the mapping between the kernel
  677. * device address and megaraid device address.
  678. * We export the physical devices on their actual addresses. The
  679. * logical drives are exported on a virtual SCSI channel
  680. */
  681. megaraid_mbox_setup_device_map(adapter);
  682. // If the firmware supports random deletion, update the device id map
  683. if (megaraid_mbox_support_random_del(adapter)) {
  684. // Change the logical drives numbers in device_ids array one
  685. // slot in device_ids is reserved for target id, that's why
  686. // "<=" below
  687. for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
  688. adapter->device_ids[adapter->max_channel][i] += 0x80;
  689. }
  690. adapter->device_ids[adapter->max_channel][adapter->init_id] =
  691. 0xFF;
  692. raid_dev->random_del_supported = 1;
  693. }
  694. /*
  695. * find out the maximum number of scatter-gather elements supported by
  696. * this firmware
  697. */
  698. adapter->sglen = megaraid_mbox_get_max_sg(adapter);
  699. // enumerate RAID and SCSI channels so that all devices on SCSI
  700. // channels can later be exported, including disk devices
  701. megaraid_mbox_enum_raid_scsi(adapter);
  702. /*
  703. * Other parameters required by upper layer
  704. *
  705. * maximum number of sectors per IO command
  706. */
  707. adapter->max_sectors = megaraid_max_sectors;
  708. /*
  709. * number of queued commands per LUN.
  710. */
  711. adapter->cmd_per_lun = megaraid_cmd_per_lun;
  712. /*
  713. * Allocate resources required to issue FW calls, when sysfs is
  714. * accessed
  715. */
  716. if (megaraid_sysfs_alloc_resources(adapter) != 0)
  717. goto out_free_irq;
  718. // Set the DMA mask to 64-bit. All supported controllers as capable of
  719. // DMA in this range
  720. pci_read_config_dword(adapter->pdev, PCI_CONF_AMISIG64, &magic64);
  721. if (((magic64 == HBA_SIGNATURE_64_BIT) &&
  722. ((adapter->pdev->subsystem_device !=
  723. PCI_SUBSYS_ID_MEGARAID_SATA_150_6) &&
  724. (adapter->pdev->subsystem_device !=
  725. PCI_SUBSYS_ID_MEGARAID_SATA_150_4))) ||
  726. (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
  727. adapter->pdev->device == PCI_DEVICE_ID_VERDE) ||
  728. (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
  729. adapter->pdev->device == PCI_DEVICE_ID_DOBSON) ||
  730. (adapter->pdev->vendor == PCI_VENDOR_ID_LSI_LOGIC &&
  731. adapter->pdev->device == PCI_DEVICE_ID_LINDSAY) ||
  732. (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
  733. adapter->pdev->device == PCI_DEVICE_ID_PERC4_DI_EVERGLADES) ||
  734. (adapter->pdev->vendor == PCI_VENDOR_ID_DELL &&
  735. adapter->pdev->device == PCI_DEVICE_ID_PERC4E_DI_KOBUK)) {
  736. if (pci_set_dma_mask(adapter->pdev, DMA_BIT_MASK(64))) {
  737. con_log(CL_ANN, (KERN_WARNING
  738. "megaraid: DMA mask for 64-bit failed\n"));
  739. if (pci_set_dma_mask (adapter->pdev, DMA_BIT_MASK(32))) {
  740. con_log(CL_ANN, (KERN_WARNING
  741. "megaraid: 32-bit DMA mask failed\n"));
  742. goto out_free_sysfs_res;
  743. }
  744. }
  745. }
  746. // setup tasklet for DPC
  747. tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
  748. (unsigned long)adapter);
  749. con_log(CL_DLEVEL1, (KERN_INFO
  750. "megaraid mbox hba successfully initialized\n"));
  751. return 0;
  752. out_free_sysfs_res:
  753. megaraid_sysfs_free_resources(adapter);
  754. out_free_irq:
  755. free_irq(adapter->irq, adapter);
  756. out_alloc_cmds:
  757. megaraid_free_cmd_packets(adapter);
  758. out_iounmap:
  759. iounmap(raid_dev->baseaddr);
  760. out_release_regions:
  761. pci_release_regions(pdev);
  762. out_free_raid_dev:
  763. kfree(raid_dev);
  764. return -1;
  765. }
  766. /**
  767. * megaraid_fini_mbox - undo controller initialization
  768. * @adapter : our soft state
  769. */
  770. static void
  771. megaraid_fini_mbox(adapter_t *adapter)
  772. {
  773. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  774. // flush all caches
  775. megaraid_mbox_flush_cache(adapter);
  776. tasklet_kill(&adapter->dpc_h);
  777. megaraid_sysfs_free_resources(adapter);
  778. megaraid_free_cmd_packets(adapter);
  779. free_irq(adapter->irq, adapter);
  780. iounmap(raid_dev->baseaddr);
  781. pci_release_regions(adapter->pdev);
  782. kfree(raid_dev);
  783. return;
  784. }
  785. /**
  786. * megaraid_alloc_cmd_packets - allocate shared mailbox
  787. * @adapter : soft state of the raid controller
  788. *
  789. * Allocate and align the shared mailbox. This maibox is used to issue
  790. * all the commands. For IO based controllers, the mailbox is also regsitered
  791. * with the FW. Allocate memory for all commands as well.
  792. * This is our big allocator.
  793. */
  794. static int
  795. megaraid_alloc_cmd_packets(adapter_t *adapter)
  796. {
  797. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  798. struct pci_dev *pdev;
  799. unsigned long align;
  800. scb_t *scb;
  801. mbox_ccb_t *ccb;
  802. struct mraid_pci_blk *epthru_pci_blk;
  803. struct mraid_pci_blk *sg_pci_blk;
  804. struct mraid_pci_blk *mbox_pci_blk;
  805. int i;
  806. pdev = adapter->pdev;
  807. /*
  808. * Setup the mailbox
  809. * Allocate the common 16-byte aligned memory for the handshake
  810. * mailbox.
  811. */
  812. raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
  813. sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
  814. if (!raid_dev->una_mbox64) {
  815. con_log(CL_ANN, (KERN_WARNING
  816. "megaraid: out of memory, %s %d\n", __func__,
  817. __LINE__));
  818. return -1;
  819. }
  820. memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
  821. /*
  822. * Align the mailbox at 16-byte boundary
  823. */
  824. raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
  825. raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
  826. (~0UL ^ 0xFUL));
  827. raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
  828. align = ((void *)raid_dev->mbox -
  829. ((void *)&raid_dev->una_mbox64->mbox32));
  830. raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
  831. align;
  832. // Allocate memory for commands issued internally
  833. adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
  834. &adapter->ibuf_dma_h);
  835. if (!adapter->ibuf) {
  836. con_log(CL_ANN, (KERN_WARNING
  837. "megaraid: out of memory, %s %d\n", __func__,
  838. __LINE__));
  839. goto out_free_common_mbox;
  840. }
  841. memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
  842. // Allocate memory for our SCSI Command Blocks and their associated
  843. // memory
  844. /*
  845. * Allocate memory for the base list of scb. Later allocate memory for
  846. * CCBs and embedded components of each CCB and point the pointers in
  847. * scb to the allocated components
  848. * NOTE: The code to allocate SCB will be duplicated in all the LLD
  849. * since the calling routine does not yet know the number of available
  850. * commands.
  851. */
  852. adapter->kscb_list = kcalloc(MBOX_MAX_SCSI_CMDS, sizeof(scb_t), GFP_KERNEL);
  853. if (adapter->kscb_list == NULL) {
  854. con_log(CL_ANN, (KERN_WARNING
  855. "megaraid: out of memory, %s %d\n", __func__,
  856. __LINE__));
  857. goto out_free_ibuf;
  858. }
  859. // memory allocation for our command packets
  860. if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
  861. con_log(CL_ANN, (KERN_WARNING
  862. "megaraid: out of memory, %s %d\n", __func__,
  863. __LINE__));
  864. goto out_free_scb_list;
  865. }
  866. // Adjust the scb pointers and link in the free pool
  867. epthru_pci_blk = raid_dev->epthru_pool;
  868. sg_pci_blk = raid_dev->sg_pool;
  869. mbox_pci_blk = raid_dev->mbox_pool;
  870. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  871. scb = adapter->kscb_list + i;
  872. ccb = raid_dev->ccb_list + i;
  873. ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
  874. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  875. ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
  876. ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
  877. // make sure the mailbox is aligned properly
  878. if (ccb->mbox_dma_h & 0x0F) {
  879. con_log(CL_ANN, (KERN_CRIT
  880. "megaraid mbox: not aligned on 16-bytes\n"));
  881. goto out_teardown_dma_pools;
  882. }
  883. ccb->epthru = (mraid_epassthru_t *)
  884. epthru_pci_blk[i].vaddr;
  885. ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
  886. ccb->pthru = (mraid_passthru_t *)ccb->epthru;
  887. ccb->pthru_dma_h = ccb->epthru_dma_h;
  888. ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
  889. ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
  890. ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
  891. scb->ccb = (caddr_t)ccb;
  892. scb->gp = 0;
  893. scb->sno = i; // command index
  894. scb->scp = NULL;
  895. scb->state = SCB_FREE;
  896. scb->dma_direction = PCI_DMA_NONE;
  897. scb->dma_type = MRAID_DMA_NONE;
  898. scb->dev_channel = -1;
  899. scb->dev_target = -1;
  900. // put scb in the free pool
  901. list_add_tail(&scb->list, &adapter->kscb_pool);
  902. }
  903. return 0;
  904. out_teardown_dma_pools:
  905. megaraid_mbox_teardown_dma_pools(adapter);
  906. out_free_scb_list:
  907. kfree(adapter->kscb_list);
  908. out_free_ibuf:
  909. pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
  910. adapter->ibuf_dma_h);
  911. out_free_common_mbox:
  912. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  913. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  914. return -1;
  915. }
  916. /**
  917. * megaraid_free_cmd_packets - free memory
  918. * @adapter : soft state of the raid controller
  919. *
  920. * Release memory resources allocated for commands.
  921. */
  922. static void
  923. megaraid_free_cmd_packets(adapter_t *adapter)
  924. {
  925. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  926. megaraid_mbox_teardown_dma_pools(adapter);
  927. kfree(adapter->kscb_list);
  928. pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
  929. (void *)adapter->ibuf, adapter->ibuf_dma_h);
  930. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  931. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  932. return;
  933. }
  934. /**
  935. * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
  936. * @adapter : HBA soft state
  937. *
  938. * Setup the dma pools for mailbox, passthru and extended passthru structures,
  939. * and scatter-gather lists.
  940. */
  941. static int
  942. megaraid_mbox_setup_dma_pools(adapter_t *adapter)
  943. {
  944. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  945. struct mraid_pci_blk *epthru_pci_blk;
  946. struct mraid_pci_blk *sg_pci_blk;
  947. struct mraid_pci_blk *mbox_pci_blk;
  948. int i;
  949. // Allocate memory for 16-bytes aligned mailboxes
  950. raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
  951. adapter->pdev,
  952. sizeof(mbox64_t) + 16,
  953. 16, 0);
  954. if (raid_dev->mbox_pool_handle == NULL) {
  955. goto fail_setup_dma_pool;
  956. }
  957. mbox_pci_blk = raid_dev->mbox_pool;
  958. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  959. mbox_pci_blk[i].vaddr = pci_pool_alloc(
  960. raid_dev->mbox_pool_handle,
  961. GFP_KERNEL,
  962. &mbox_pci_blk[i].dma_addr);
  963. if (!mbox_pci_blk[i].vaddr) {
  964. goto fail_setup_dma_pool;
  965. }
  966. }
  967. /*
  968. * Allocate memory for each embedded passthru strucuture pointer
  969. * Request for a 128 bytes aligned structure for each passthru command
  970. * structure
  971. * Since passthru and extended passthru commands are exclusive, they
  972. * share common memory pool. Passthru structures piggyback on memory
  973. * allocted to extended passthru since passthru is smaller of the two
  974. */
  975. raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
  976. adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
  977. if (raid_dev->epthru_pool_handle == NULL) {
  978. goto fail_setup_dma_pool;
  979. }
  980. epthru_pci_blk = raid_dev->epthru_pool;
  981. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  982. epthru_pci_blk[i].vaddr = pci_pool_alloc(
  983. raid_dev->epthru_pool_handle,
  984. GFP_KERNEL,
  985. &epthru_pci_blk[i].dma_addr);
  986. if (!epthru_pci_blk[i].vaddr) {
  987. goto fail_setup_dma_pool;
  988. }
  989. }
  990. // Allocate memory for each scatter-gather list. Request for 512 bytes
  991. // alignment for each sg list
  992. raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
  993. adapter->pdev,
  994. sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
  995. 512, 0);
  996. if (raid_dev->sg_pool_handle == NULL) {
  997. goto fail_setup_dma_pool;
  998. }
  999. sg_pci_blk = raid_dev->sg_pool;
  1000. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  1001. sg_pci_blk[i].vaddr = pci_pool_alloc(
  1002. raid_dev->sg_pool_handle,
  1003. GFP_KERNEL,
  1004. &sg_pci_blk[i].dma_addr);
  1005. if (!sg_pci_blk[i].vaddr) {
  1006. goto fail_setup_dma_pool;
  1007. }
  1008. }
  1009. return 0;
  1010. fail_setup_dma_pool:
  1011. megaraid_mbox_teardown_dma_pools(adapter);
  1012. return -1;
  1013. }
  1014. /**
  1015. * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
  1016. * @adapter : HBA soft state
  1017. *
  1018. * Teardown the dma pool for mailbox, passthru and extended passthru
  1019. * structures, and scatter-gather lists.
  1020. */
  1021. static void
  1022. megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
  1023. {
  1024. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1025. struct mraid_pci_blk *epthru_pci_blk;
  1026. struct mraid_pci_blk *sg_pci_blk;
  1027. struct mraid_pci_blk *mbox_pci_blk;
  1028. int i;
  1029. sg_pci_blk = raid_dev->sg_pool;
  1030. for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
  1031. pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
  1032. sg_pci_blk[i].dma_addr);
  1033. }
  1034. if (raid_dev->sg_pool_handle)
  1035. pci_pool_destroy(raid_dev->sg_pool_handle);
  1036. epthru_pci_blk = raid_dev->epthru_pool;
  1037. for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
  1038. pci_pool_free(raid_dev->epthru_pool_handle,
  1039. epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
  1040. }
  1041. if (raid_dev->epthru_pool_handle)
  1042. pci_pool_destroy(raid_dev->epthru_pool_handle);
  1043. mbox_pci_blk = raid_dev->mbox_pool;
  1044. for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
  1045. pci_pool_free(raid_dev->mbox_pool_handle,
  1046. mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
  1047. }
  1048. if (raid_dev->mbox_pool_handle)
  1049. pci_pool_destroy(raid_dev->mbox_pool_handle);
  1050. return;
  1051. }
  1052. /**
  1053. * megaraid_alloc_scb - detach and return a scb from the free list
  1054. * @adapter : controller's soft state
  1055. * @scp : pointer to the scsi command to be executed
  1056. *
  1057. * Return the scb from the head of the free list. %NULL if there are none
  1058. * available.
  1059. */
  1060. static scb_t *
  1061. megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
  1062. {
  1063. struct list_head *head = &adapter->kscb_pool;
  1064. scb_t *scb = NULL;
  1065. unsigned long flags;
  1066. // detach scb from free pool
  1067. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1068. if (list_empty(head)) {
  1069. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1070. return NULL;
  1071. }
  1072. scb = list_entry(head->next, scb_t, list);
  1073. list_del_init(&scb->list);
  1074. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1075. scb->state = SCB_ACTIVE;
  1076. scb->scp = scp;
  1077. scb->dma_type = MRAID_DMA_NONE;
  1078. return scb;
  1079. }
  1080. /**
  1081. * megaraid_dealloc_scb - return the scb to the free pool
  1082. * @adapter : controller's soft state
  1083. * @scb : scb to be freed
  1084. *
  1085. * Return the scb back to the free list of scbs. The caller must 'flush' the
  1086. * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
  1087. * NOTE NOTE: Make sure the scb is not on any list before calling this
  1088. * routine.
  1089. */
  1090. static inline void
  1091. megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
  1092. {
  1093. unsigned long flags;
  1094. // put scb in the free pool
  1095. scb->state = SCB_FREE;
  1096. scb->scp = NULL;
  1097. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1098. list_add(&scb->list, &adapter->kscb_pool);
  1099. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1100. return;
  1101. }
  1102. /**
  1103. * megaraid_mbox_mksgl - make the scatter-gather list
  1104. * @adapter : controller's soft state
  1105. * @scb : scsi control block
  1106. *
  1107. * Prepare the scatter-gather list.
  1108. */
  1109. static int
  1110. megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
  1111. {
  1112. struct scatterlist *sgl;
  1113. mbox_ccb_t *ccb;
  1114. struct scsi_cmnd *scp;
  1115. int sgcnt;
  1116. int i;
  1117. scp = scb->scp;
  1118. ccb = (mbox_ccb_t *)scb->ccb;
  1119. sgcnt = scsi_dma_map(scp);
  1120. BUG_ON(sgcnt < 0 || sgcnt > adapter->sglen);
  1121. // no mapping required if no data to be transferred
  1122. if (!sgcnt)
  1123. return 0;
  1124. scb->dma_type = MRAID_DMA_WSG;
  1125. scsi_for_each_sg(scp, sgl, sgcnt, i) {
  1126. ccb->sgl64[i].address = sg_dma_address(sgl);
  1127. ccb->sgl64[i].length = sg_dma_len(sgl);
  1128. }
  1129. // Return count of SG nodes
  1130. return sgcnt;
  1131. }
  1132. /**
  1133. * mbox_post_cmd - issue a mailbox command
  1134. * @adapter : controller's soft state
  1135. * @scb : command to be issued
  1136. *
  1137. * Post the command to the controller if mailbox is available.
  1138. */
  1139. static int
  1140. mbox_post_cmd(adapter_t *adapter, scb_t *scb)
  1141. {
  1142. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1143. mbox64_t *mbox64;
  1144. mbox_t *mbox;
  1145. mbox_ccb_t *ccb;
  1146. unsigned long flags;
  1147. unsigned int i = 0;
  1148. ccb = (mbox_ccb_t *)scb->ccb;
  1149. mbox = raid_dev->mbox;
  1150. mbox64 = raid_dev->mbox64;
  1151. /*
  1152. * Check for busy mailbox. If it is, return failure - the caller
  1153. * should retry later.
  1154. */
  1155. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1156. if (unlikely(mbox->busy)) {
  1157. do {
  1158. udelay(1);
  1159. i++;
  1160. rmb();
  1161. } while(mbox->busy && (i < max_mbox_busy_wait));
  1162. if (mbox->busy) {
  1163. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1164. return -1;
  1165. }
  1166. }
  1167. // Copy this command's mailbox data into "adapter's" mailbox
  1168. memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
  1169. mbox->cmdid = scb->sno;
  1170. adapter->outstanding_cmds++;
  1171. if (scb->dma_direction == PCI_DMA_TODEVICE)
  1172. pci_dma_sync_sg_for_device(adapter->pdev,
  1173. scsi_sglist(scb->scp),
  1174. scsi_sg_count(scb->scp),
  1175. PCI_DMA_TODEVICE);
  1176. mbox->busy = 1; // Set busy
  1177. mbox->poll = 0;
  1178. mbox->ack = 0;
  1179. wmb();
  1180. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  1181. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1182. return 0;
  1183. }
  1184. /**
  1185. * megaraid_queue_command - generic queue entry point for all LLDs
  1186. * @scp : pointer to the scsi command to be executed
  1187. * @done : callback routine to be called after the cmd has be completed
  1188. *
  1189. * Queue entry point for mailbox based controllers.
  1190. */
  1191. static int
  1192. megaraid_queue_command_lck(struct scsi_cmnd *scp, void (*done)(struct scsi_cmnd *))
  1193. {
  1194. adapter_t *adapter;
  1195. scb_t *scb;
  1196. int if_busy;
  1197. adapter = SCP2ADAPTER(scp);
  1198. scp->scsi_done = done;
  1199. scp->result = 0;
  1200. /*
  1201. * Allocate and build a SCB request
  1202. * if_busy flag will be set if megaraid_mbox_build_cmd() command could
  1203. * not allocate scb. We will return non-zero status in that case.
  1204. * NOTE: scb can be null even though certain commands completed
  1205. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
  1206. * return 0 in that case, and we would do the callback right away.
  1207. */
  1208. if_busy = 0;
  1209. scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
  1210. if (!scb) { // command already completed
  1211. done(scp);
  1212. return 0;
  1213. }
  1214. megaraid_mbox_runpendq(adapter, scb);
  1215. return if_busy;
  1216. }
  1217. static DEF_SCSI_QCMD(megaraid_queue_command)
  1218. /**
  1219. * megaraid_mbox_build_cmd - transform the mid-layer scsi commands
  1220. * @adapter : controller's soft state
  1221. * @scp : mid-layer scsi command pointer
  1222. * @busy : set if request could not be completed because of lack of
  1223. * resources
  1224. *
  1225. * Transform the mid-layer scsi command to megaraid firmware lingua.
  1226. * Convert the command issued by mid-layer to format understood by megaraid
  1227. * firmware. We also complete certain commands without sending them to firmware.
  1228. */
  1229. static scb_t *
  1230. megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
  1231. {
  1232. mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
  1233. int channel;
  1234. int target;
  1235. int islogical;
  1236. mbox_ccb_t *ccb;
  1237. mraid_passthru_t *pthru;
  1238. mbox64_t *mbox64;
  1239. mbox_t *mbox;
  1240. scb_t *scb;
  1241. char skip[] = "skipping";
  1242. char scan[] = "scanning";
  1243. char *ss;
  1244. /*
  1245. * Get the appropriate device map for the device this command is
  1246. * intended for
  1247. */
  1248. MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
  1249. /*
  1250. * Logical drive commands
  1251. */
  1252. if (islogical) {
  1253. switch (scp->cmnd[0]) {
  1254. case TEST_UNIT_READY:
  1255. /*
  1256. * Do we support clustering and is the support enabled
  1257. * If no, return success always
  1258. */
  1259. if (!adapter->ha) {
  1260. scp->result = (DID_OK << 16);
  1261. return NULL;
  1262. }
  1263. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1264. scp->result = (DID_ERROR << 16);
  1265. *busy = 1;
  1266. return NULL;
  1267. }
  1268. scb->dma_direction = scp->sc_data_direction;
  1269. scb->dev_channel = 0xFF;
  1270. scb->dev_target = target;
  1271. ccb = (mbox_ccb_t *)scb->ccb;
  1272. /*
  1273. * The command id will be provided by the command
  1274. * issuance routine
  1275. */
  1276. ccb->raw_mbox[0] = CLUSTER_CMD;
  1277. ccb->raw_mbox[2] = RESERVATION_STATUS;
  1278. ccb->raw_mbox[3] = target;
  1279. return scb;
  1280. case MODE_SENSE:
  1281. {
  1282. struct scatterlist *sgl;
  1283. caddr_t vaddr;
  1284. sgl = scsi_sglist(scp);
  1285. if (sg_page(sgl)) {
  1286. vaddr = (caddr_t) sg_virt(&sgl[0]);
  1287. memset(vaddr, 0, scp->cmnd[4]);
  1288. }
  1289. else {
  1290. con_log(CL_ANN, (KERN_WARNING
  1291. "megaraid mailbox: invalid sg:%d\n",
  1292. __LINE__));
  1293. }
  1294. }
  1295. scp->result = (DID_OK << 16);
  1296. return NULL;
  1297. case INQUIRY:
  1298. /*
  1299. * Display the channel scan for logical drives
  1300. * Do not display scan for a channel if already done.
  1301. */
  1302. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1303. con_log(CL_ANN, (KERN_INFO
  1304. "scsi[%d]: scanning scsi channel %d",
  1305. adapter->host->host_no,
  1306. SCP2CHANNEL(scp)));
  1307. con_log(CL_ANN, (
  1308. " [virtual] for logical drives\n"));
  1309. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1310. }
  1311. if (scp->cmnd[1] & MEGA_SCSI_INQ_EVPD) {
  1312. scp->sense_buffer[0] = 0x70;
  1313. scp->sense_buffer[2] = ILLEGAL_REQUEST;
  1314. scp->sense_buffer[12] = MEGA_INVALID_FIELD_IN_CDB;
  1315. scp->result = CHECK_CONDITION << 1;
  1316. return NULL;
  1317. }
  1318. /* Fall through */
  1319. case READ_CAPACITY:
  1320. /*
  1321. * Do not allow LUN > 0 for logical drives and
  1322. * requests for more than 40 logical drives
  1323. */
  1324. if (SCP2LUN(scp)) {
  1325. scp->result = (DID_BAD_TARGET << 16);
  1326. return NULL;
  1327. }
  1328. if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
  1329. scp->result = (DID_BAD_TARGET << 16);
  1330. return NULL;
  1331. }
  1332. /* Allocate a SCB and initialize passthru */
  1333. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1334. scp->result = (DID_ERROR << 16);
  1335. *busy = 1;
  1336. return NULL;
  1337. }
  1338. ccb = (mbox_ccb_t *)scb->ccb;
  1339. scb->dev_channel = 0xFF;
  1340. scb->dev_target = target;
  1341. pthru = ccb->pthru;
  1342. mbox = ccb->mbox;
  1343. mbox64 = ccb->mbox64;
  1344. pthru->timeout = 0;
  1345. pthru->ars = 1;
  1346. pthru->reqsenselen = 14;
  1347. pthru->islogical = 1;
  1348. pthru->logdrv = target;
  1349. pthru->cdblen = scp->cmd_len;
  1350. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1351. mbox->cmd = MBOXCMD_PASSTHRU64;
  1352. scb->dma_direction = scp->sc_data_direction;
  1353. pthru->dataxferlen = scsi_bufflen(scp);
  1354. pthru->dataxferaddr = ccb->sgl_dma_h;
  1355. pthru->numsge = megaraid_mbox_mksgl(adapter,
  1356. scb);
  1357. mbox->xferaddr = 0xFFFFFFFF;
  1358. mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
  1359. mbox64->xferaddr_hi = 0;
  1360. return scb;
  1361. case READ_6:
  1362. case WRITE_6:
  1363. case READ_10:
  1364. case WRITE_10:
  1365. case READ_12:
  1366. case WRITE_12:
  1367. /*
  1368. * Allocate a SCB and initialize mailbox
  1369. */
  1370. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1371. scp->result = (DID_ERROR << 16);
  1372. *busy = 1;
  1373. return NULL;
  1374. }
  1375. ccb = (mbox_ccb_t *)scb->ccb;
  1376. scb->dev_channel = 0xFF;
  1377. scb->dev_target = target;
  1378. mbox = ccb->mbox;
  1379. mbox64 = ccb->mbox64;
  1380. mbox->logdrv = target;
  1381. /*
  1382. * A little HACK: 2nd bit is zero for all scsi read
  1383. * commands and is set for all scsi write commands
  1384. */
  1385. mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
  1386. MBOXCMD_LREAD64 ;
  1387. /*
  1388. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1389. */
  1390. if (scp->cmd_len == 6) {
  1391. mbox->numsectors = (uint32_t)scp->cmnd[4];
  1392. mbox->lba =
  1393. ((uint32_t)scp->cmnd[1] << 16) |
  1394. ((uint32_t)scp->cmnd[2] << 8) |
  1395. (uint32_t)scp->cmnd[3];
  1396. mbox->lba &= 0x1FFFFF;
  1397. }
  1398. /*
  1399. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1400. */
  1401. else if (scp->cmd_len == 10) {
  1402. mbox->numsectors =
  1403. (uint32_t)scp->cmnd[8] |
  1404. ((uint32_t)scp->cmnd[7] << 8);
  1405. mbox->lba =
  1406. ((uint32_t)scp->cmnd[2] << 24) |
  1407. ((uint32_t)scp->cmnd[3] << 16) |
  1408. ((uint32_t)scp->cmnd[4] << 8) |
  1409. (uint32_t)scp->cmnd[5];
  1410. }
  1411. /*
  1412. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1413. */
  1414. else if (scp->cmd_len == 12) {
  1415. mbox->lba =
  1416. ((uint32_t)scp->cmnd[2] << 24) |
  1417. ((uint32_t)scp->cmnd[3] << 16) |
  1418. ((uint32_t)scp->cmnd[4] << 8) |
  1419. (uint32_t)scp->cmnd[5];
  1420. mbox->numsectors =
  1421. ((uint32_t)scp->cmnd[6] << 24) |
  1422. ((uint32_t)scp->cmnd[7] << 16) |
  1423. ((uint32_t)scp->cmnd[8] << 8) |
  1424. (uint32_t)scp->cmnd[9];
  1425. }
  1426. else {
  1427. con_log(CL_ANN, (KERN_WARNING
  1428. "megaraid: unsupported CDB length\n"));
  1429. megaraid_dealloc_scb(adapter, scb);
  1430. scp->result = (DID_ERROR << 16);
  1431. return NULL;
  1432. }
  1433. scb->dma_direction = scp->sc_data_direction;
  1434. // Calculate Scatter-Gather info
  1435. mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
  1436. mbox->numsge = megaraid_mbox_mksgl(adapter,
  1437. scb);
  1438. mbox->xferaddr = 0xFFFFFFFF;
  1439. mbox64->xferaddr_hi = 0;
  1440. return scb;
  1441. case RESERVE:
  1442. case RELEASE:
  1443. /*
  1444. * Do we support clustering and is the support enabled
  1445. */
  1446. if (!adapter->ha) {
  1447. scp->result = (DID_BAD_TARGET << 16);
  1448. return NULL;
  1449. }
  1450. /*
  1451. * Allocate a SCB and initialize mailbox
  1452. */
  1453. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1454. scp->result = (DID_ERROR << 16);
  1455. *busy = 1;
  1456. return NULL;
  1457. }
  1458. ccb = (mbox_ccb_t *)scb->ccb;
  1459. scb->dev_channel = 0xFF;
  1460. scb->dev_target = target;
  1461. ccb->raw_mbox[0] = CLUSTER_CMD;
  1462. ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
  1463. RESERVE_LD : RELEASE_LD;
  1464. ccb->raw_mbox[3] = target;
  1465. scb->dma_direction = scp->sc_data_direction;
  1466. return scb;
  1467. default:
  1468. scp->result = (DID_BAD_TARGET << 16);
  1469. return NULL;
  1470. }
  1471. }
  1472. else { // Passthru device commands
  1473. // Do not allow access to target id > 15 or LUN > 7
  1474. if (target > 15 || SCP2LUN(scp) > 7) {
  1475. scp->result = (DID_BAD_TARGET << 16);
  1476. return NULL;
  1477. }
  1478. // if fast load option was set and scan for last device is
  1479. // over, reset the fast_load flag so that during a possible
  1480. // next scan, devices can be made available
  1481. if (rdev->fast_load && (target == 15) &&
  1482. (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
  1483. con_log(CL_ANN, (KERN_INFO
  1484. "megaraid[%d]: physical device scan re-enabled\n",
  1485. adapter->host->host_no));
  1486. rdev->fast_load = 0;
  1487. }
  1488. /*
  1489. * Display the channel scan for physical devices
  1490. */
  1491. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1492. ss = rdev->fast_load ? skip : scan;
  1493. con_log(CL_ANN, (KERN_INFO
  1494. "scsi[%d]: %s scsi channel %d [Phy %d]",
  1495. adapter->host->host_no, ss, SCP2CHANNEL(scp),
  1496. channel));
  1497. con_log(CL_ANN, (
  1498. " for non-raid devices\n"));
  1499. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1500. }
  1501. // disable channel sweep if fast load option given
  1502. if (rdev->fast_load) {
  1503. scp->result = (DID_BAD_TARGET << 16);
  1504. return NULL;
  1505. }
  1506. // Allocate a SCB and initialize passthru
  1507. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1508. scp->result = (DID_ERROR << 16);
  1509. *busy = 1;
  1510. return NULL;
  1511. }
  1512. ccb = (mbox_ccb_t *)scb->ccb;
  1513. scb->dev_channel = channel;
  1514. scb->dev_target = target;
  1515. scb->dma_direction = scp->sc_data_direction;
  1516. mbox = ccb->mbox;
  1517. mbox64 = ccb->mbox64;
  1518. // Does this firmware support extended CDBs
  1519. if (adapter->max_cdb_sz == 16) {
  1520. mbox->cmd = MBOXCMD_EXTPTHRU;
  1521. megaraid_mbox_prepare_epthru(adapter, scb, scp);
  1522. mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
  1523. mbox64->xferaddr_hi = 0;
  1524. mbox->xferaddr = 0xFFFFFFFF;
  1525. }
  1526. else {
  1527. mbox->cmd = MBOXCMD_PASSTHRU64;
  1528. megaraid_mbox_prepare_pthru(adapter, scb, scp);
  1529. mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
  1530. mbox64->xferaddr_hi = 0;
  1531. mbox->xferaddr = 0xFFFFFFFF;
  1532. }
  1533. return scb;
  1534. }
  1535. // NOT REACHED
  1536. }
  1537. /**
  1538. * megaraid_mbox_runpendq - execute commands queued in the pending queue
  1539. * @adapter : controller's soft state
  1540. * @scb_q : SCB to be queued in the pending list
  1541. *
  1542. * Scan the pending list for commands which are not yet issued and try to
  1543. * post to the controller. The SCB can be a null pointer, which would indicate
  1544. * no SCB to be queue, just try to execute the ones in the pending list.
  1545. *
  1546. * NOTE: We do not actually traverse the pending list. The SCBs are plucked
  1547. * out from the head of the pending list. If it is successfully issued, the
  1548. * next SCB is at the head now.
  1549. */
  1550. static void
  1551. megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
  1552. {
  1553. scb_t *scb;
  1554. unsigned long flags;
  1555. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1556. if (scb_q) {
  1557. scb_q->state = SCB_PENDQ;
  1558. list_add_tail(&scb_q->list, &adapter->pend_list);
  1559. }
  1560. // if the adapter in not in quiescent mode, post the commands to FW
  1561. if (adapter->quiescent) {
  1562. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1563. return;
  1564. }
  1565. while (!list_empty(&adapter->pend_list)) {
  1566. assert_spin_locked(PENDING_LIST_LOCK(adapter));
  1567. scb = list_entry(adapter->pend_list.next, scb_t, list);
  1568. // remove the scb from the pending list and try to
  1569. // issue. If we are unable to issue it, put back in
  1570. // the pending list and return
  1571. list_del_init(&scb->list);
  1572. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1573. // if mailbox was busy, return SCB back to pending
  1574. // list. Make sure to add at the head, since that's
  1575. // where it would have been removed from
  1576. scb->state = SCB_ISSUED;
  1577. if (mbox_post_cmd(adapter, scb) != 0) {
  1578. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1579. scb->state = SCB_PENDQ;
  1580. list_add(&scb->list, &adapter->pend_list);
  1581. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  1582. flags);
  1583. return;
  1584. }
  1585. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1586. }
  1587. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1588. return;
  1589. }
  1590. /**
  1591. * megaraid_mbox_prepare_pthru - prepare a command for physical devices
  1592. * @adapter : pointer to controller's soft state
  1593. * @scb : scsi control block
  1594. * @scp : scsi command from the mid-layer
  1595. *
  1596. * Prepare a command for the scsi physical devices.
  1597. */
  1598. static void
  1599. megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
  1600. struct scsi_cmnd *scp)
  1601. {
  1602. mbox_ccb_t *ccb;
  1603. mraid_passthru_t *pthru;
  1604. uint8_t channel;
  1605. uint8_t target;
  1606. ccb = (mbox_ccb_t *)scb->ccb;
  1607. pthru = ccb->pthru;
  1608. channel = scb->dev_channel;
  1609. target = scb->dev_target;
  1610. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1611. pthru->timeout = 4;
  1612. pthru->ars = 1;
  1613. pthru->islogical = 0;
  1614. pthru->channel = 0;
  1615. pthru->target = (channel << 4) | target;
  1616. pthru->logdrv = SCP2LUN(scp);
  1617. pthru->reqsenselen = 14;
  1618. pthru->cdblen = scp->cmd_len;
  1619. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1620. if (scsi_bufflen(scp)) {
  1621. pthru->dataxferlen = scsi_bufflen(scp);
  1622. pthru->dataxferaddr = ccb->sgl_dma_h;
  1623. pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1624. }
  1625. else {
  1626. pthru->dataxferaddr = 0;
  1627. pthru->dataxferlen = 0;
  1628. pthru->numsge = 0;
  1629. }
  1630. return;
  1631. }
  1632. /**
  1633. * megaraid_mbox_prepare_epthru - prepare a command for physical devices
  1634. * @adapter : pointer to controller's soft state
  1635. * @scb : scsi control block
  1636. * @scp : scsi command from the mid-layer
  1637. *
  1638. * Prepare a command for the scsi physical devices. This rountine prepares
  1639. * commands for devices which can take extended CDBs (>10 bytes).
  1640. */
  1641. static void
  1642. megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
  1643. struct scsi_cmnd *scp)
  1644. {
  1645. mbox_ccb_t *ccb;
  1646. mraid_epassthru_t *epthru;
  1647. uint8_t channel;
  1648. uint8_t target;
  1649. ccb = (mbox_ccb_t *)scb->ccb;
  1650. epthru = ccb->epthru;
  1651. channel = scb->dev_channel;
  1652. target = scb->dev_target;
  1653. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1654. epthru->timeout = 4;
  1655. epthru->ars = 1;
  1656. epthru->islogical = 0;
  1657. epthru->channel = 0;
  1658. epthru->target = (channel << 4) | target;
  1659. epthru->logdrv = SCP2LUN(scp);
  1660. epthru->reqsenselen = 14;
  1661. epthru->cdblen = scp->cmd_len;
  1662. memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
  1663. if (scsi_bufflen(scp)) {
  1664. epthru->dataxferlen = scsi_bufflen(scp);
  1665. epthru->dataxferaddr = ccb->sgl_dma_h;
  1666. epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1667. }
  1668. else {
  1669. epthru->dataxferaddr = 0;
  1670. epthru->dataxferlen = 0;
  1671. epthru->numsge = 0;
  1672. }
  1673. return;
  1674. }
  1675. /**
  1676. * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
  1677. * @adapter : controller's soft state
  1678. *
  1679. * Interrupt acknowledgement sequence for memory mapped HBAs. Find out the
  1680. * completed command and put them on the completed list for later processing.
  1681. *
  1682. * Returns: 1 if the interrupt is valid, 0 otherwise
  1683. */
  1684. static int
  1685. megaraid_ack_sequence(adapter_t *adapter)
  1686. {
  1687. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1688. mbox_t *mbox;
  1689. scb_t *scb;
  1690. uint8_t nstatus;
  1691. uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
  1692. struct list_head clist;
  1693. int handled;
  1694. uint32_t dword;
  1695. unsigned long flags;
  1696. int i, j;
  1697. mbox = raid_dev->mbox;
  1698. // move the SCBs from the firmware completed array to our local list
  1699. INIT_LIST_HEAD(&clist);
  1700. // loop till F/W has more commands for us to complete
  1701. handled = 0;
  1702. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1703. do {
  1704. /*
  1705. * Check if a valid interrupt is pending. If found, force the
  1706. * interrupt line low.
  1707. */
  1708. dword = RDOUTDOOR(raid_dev);
  1709. if (dword != 0x10001234) break;
  1710. handled = 1;
  1711. WROUTDOOR(raid_dev, 0x10001234);
  1712. nstatus = 0;
  1713. // wait for valid numstatus to post
  1714. for (i = 0; i < 0xFFFFF; i++) {
  1715. if (mbox->numstatus != 0xFF) {
  1716. nstatus = mbox->numstatus;
  1717. break;
  1718. }
  1719. rmb();
  1720. }
  1721. mbox->numstatus = 0xFF;
  1722. adapter->outstanding_cmds -= nstatus;
  1723. for (i = 0; i < nstatus; i++) {
  1724. // wait for valid command index to post
  1725. for (j = 0; j < 0xFFFFF; j++) {
  1726. if (mbox->completed[i] != 0xFF) break;
  1727. rmb();
  1728. }
  1729. completed[i] = mbox->completed[i];
  1730. mbox->completed[i] = 0xFF;
  1731. if (completed[i] == 0xFF) {
  1732. con_log(CL_ANN, (KERN_CRIT
  1733. "megaraid: command posting timed out\n"));
  1734. BUG();
  1735. continue;
  1736. }
  1737. // Get SCB associated with this command id
  1738. if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
  1739. // a cmm command
  1740. scb = adapter->uscb_list + (completed[i] -
  1741. MBOX_MAX_SCSI_CMDS);
  1742. }
  1743. else {
  1744. // an os command
  1745. scb = adapter->kscb_list + completed[i];
  1746. }
  1747. scb->status = mbox->status;
  1748. list_add_tail(&scb->list, &clist);
  1749. }
  1750. // Acknowledge interrupt
  1751. WRINDOOR(raid_dev, 0x02);
  1752. } while(1);
  1753. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1754. // put the completed commands in the completed list. DPC would
  1755. // complete these commands later
  1756. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1757. list_splice(&clist, &adapter->completed_list);
  1758. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1759. // schedule the DPC if there is some work for it
  1760. if (handled)
  1761. tasklet_schedule(&adapter->dpc_h);
  1762. return handled;
  1763. }
  1764. /**
  1765. * megaraid_isr - isr for memory based mailbox based controllers
  1766. * @irq : irq
  1767. * @devp : pointer to our soft state
  1768. *
  1769. * Interrupt service routine for memory-mapped mailbox controllers.
  1770. */
  1771. static irqreturn_t
  1772. megaraid_isr(int irq, void *devp)
  1773. {
  1774. adapter_t *adapter = devp;
  1775. int handled;
  1776. handled = megaraid_ack_sequence(adapter);
  1777. /* Loop through any pending requests */
  1778. if (!adapter->quiescent) {
  1779. megaraid_mbox_runpendq(adapter, NULL);
  1780. }
  1781. return IRQ_RETVAL(handled);
  1782. }
  1783. /**
  1784. * megaraid_mbox_sync_scb - sync kernel buffers
  1785. * @adapter : controller's soft state
  1786. * @scb : pointer to the resource packet
  1787. *
  1788. * DMA sync if required.
  1789. */
  1790. static void
  1791. megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
  1792. {
  1793. mbox_ccb_t *ccb;
  1794. ccb = (mbox_ccb_t *)scb->ccb;
  1795. if (scb->dma_direction == PCI_DMA_FROMDEVICE)
  1796. pci_dma_sync_sg_for_cpu(adapter->pdev,
  1797. scsi_sglist(scb->scp),
  1798. scsi_sg_count(scb->scp),
  1799. PCI_DMA_FROMDEVICE);
  1800. scsi_dma_unmap(scb->scp);
  1801. return;
  1802. }
  1803. /**
  1804. * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
  1805. * @devp : pointer to HBA soft state
  1806. *
  1807. * Pick up the commands from the completed list and send back to the owners.
  1808. * This is a reentrant function and does not assume any locks are held while
  1809. * it is being called.
  1810. */
  1811. static void
  1812. megaraid_mbox_dpc(unsigned long devp)
  1813. {
  1814. adapter_t *adapter = (adapter_t *)devp;
  1815. mraid_device_t *raid_dev;
  1816. struct list_head clist;
  1817. struct scatterlist *sgl;
  1818. scb_t *scb;
  1819. scb_t *tmp;
  1820. struct scsi_cmnd *scp;
  1821. mraid_passthru_t *pthru;
  1822. mraid_epassthru_t *epthru;
  1823. mbox_ccb_t *ccb;
  1824. int islogical;
  1825. int pdev_index;
  1826. int pdev_state;
  1827. mbox_t *mbox;
  1828. unsigned long flags;
  1829. uint8_t c;
  1830. int status;
  1831. uioc_t *kioc;
  1832. if (!adapter) return;
  1833. raid_dev = ADAP2RAIDDEV(adapter);
  1834. // move the SCBs from the completed list to our local list
  1835. INIT_LIST_HEAD(&clist);
  1836. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1837. list_splice_init(&adapter->completed_list, &clist);
  1838. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1839. list_for_each_entry_safe(scb, tmp, &clist, list) {
  1840. status = scb->status;
  1841. scp = scb->scp;
  1842. ccb = (mbox_ccb_t *)scb->ccb;
  1843. pthru = ccb->pthru;
  1844. epthru = ccb->epthru;
  1845. mbox = ccb->mbox;
  1846. // Make sure f/w has completed a valid command
  1847. if (scb->state != SCB_ISSUED) {
  1848. con_log(CL_ANN, (KERN_CRIT
  1849. "megaraid critical err: invalid command %d:%d:%p\n",
  1850. scb->sno, scb->state, scp));
  1851. BUG();
  1852. continue; // Must never happen!
  1853. }
  1854. // check for the management command and complete it right away
  1855. if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
  1856. scb->state = SCB_FREE;
  1857. scb->status = status;
  1858. // remove from local clist
  1859. list_del_init(&scb->list);
  1860. kioc = (uioc_t *)scb->gp;
  1861. kioc->status = 0;
  1862. megaraid_mbox_mm_done(adapter, scb);
  1863. continue;
  1864. }
  1865. // Was an abort issued for this command earlier
  1866. if (scb->state & SCB_ABORT) {
  1867. con_log(CL_ANN, (KERN_NOTICE
  1868. "megaraid: aborted cmd [%x] completed\n",
  1869. scb->sno));
  1870. }
  1871. /*
  1872. * If the inquiry came of a disk drive which is not part of
  1873. * any RAID array, expose it to the kernel. For this to be
  1874. * enabled, user must set the "megaraid_expose_unconf_disks"
  1875. * flag to 1 by specifying it on module parameter list.
  1876. * This would enable data migration off drives from other
  1877. * configurations.
  1878. */
  1879. islogical = MRAID_IS_LOGICAL(adapter, scp);
  1880. if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
  1881. && IS_RAID_CH(raid_dev, scb->dev_channel)) {
  1882. sgl = scsi_sglist(scp);
  1883. if (sg_page(sgl)) {
  1884. c = *(unsigned char *) sg_virt(&sgl[0]);
  1885. } else {
  1886. con_log(CL_ANN, (KERN_WARNING
  1887. "megaraid mailbox: invalid sg:%d\n",
  1888. __LINE__));
  1889. c = 0;
  1890. }
  1891. if ((c & 0x1F ) == TYPE_DISK) {
  1892. pdev_index = (scb->dev_channel * 16) +
  1893. scb->dev_target;
  1894. pdev_state =
  1895. raid_dev->pdrv_state[pdev_index] & 0x0F;
  1896. if (pdev_state == PDRV_ONLINE ||
  1897. pdev_state == PDRV_FAILED ||
  1898. pdev_state == PDRV_RBLD ||
  1899. pdev_state == PDRV_HOTSPARE ||
  1900. megaraid_expose_unconf_disks == 0) {
  1901. status = 0xF0;
  1902. }
  1903. }
  1904. }
  1905. // Convert MegaRAID status to Linux error code
  1906. switch (status) {
  1907. case 0x00:
  1908. scp->result = (DID_OK << 16);
  1909. break;
  1910. case 0x02:
  1911. /* set sense_buffer and result fields */
  1912. if (mbox->cmd == MBOXCMD_PASSTHRU ||
  1913. mbox->cmd == MBOXCMD_PASSTHRU64) {
  1914. memcpy(scp->sense_buffer, pthru->reqsensearea,
  1915. 14);
  1916. scp->result = DRIVER_SENSE << 24 |
  1917. DID_OK << 16 | CHECK_CONDITION << 1;
  1918. }
  1919. else {
  1920. if (mbox->cmd == MBOXCMD_EXTPTHRU) {
  1921. memcpy(scp->sense_buffer,
  1922. epthru->reqsensearea, 14);
  1923. scp->result = DRIVER_SENSE << 24 |
  1924. DID_OK << 16 |
  1925. CHECK_CONDITION << 1;
  1926. } else {
  1927. scp->sense_buffer[0] = 0x70;
  1928. scp->sense_buffer[2] = ABORTED_COMMAND;
  1929. scp->result = CHECK_CONDITION << 1;
  1930. }
  1931. }
  1932. break;
  1933. case 0x08:
  1934. scp->result = DID_BUS_BUSY << 16 | status;
  1935. break;
  1936. default:
  1937. /*
  1938. * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
  1939. * failed
  1940. */
  1941. if (scp->cmnd[0] == TEST_UNIT_READY) {
  1942. scp->result = DID_ERROR << 16 |
  1943. RESERVATION_CONFLICT << 1;
  1944. }
  1945. else
  1946. /*
  1947. * Error code returned is 1 if Reserve or Release
  1948. * failed or the input parameter is invalid
  1949. */
  1950. if (status == 1 && (scp->cmnd[0] == RESERVE ||
  1951. scp->cmnd[0] == RELEASE)) {
  1952. scp->result = DID_ERROR << 16 |
  1953. RESERVATION_CONFLICT << 1;
  1954. }
  1955. else {
  1956. scp->result = DID_BAD_TARGET << 16 | status;
  1957. }
  1958. }
  1959. // print a debug message for all failed commands
  1960. if (status) {
  1961. megaraid_mbox_display_scb(adapter, scb);
  1962. }
  1963. // Free our internal resources and call the mid-layer callback
  1964. // routine
  1965. megaraid_mbox_sync_scb(adapter, scb);
  1966. // remove from local clist
  1967. list_del_init(&scb->list);
  1968. // put back in free list
  1969. megaraid_dealloc_scb(adapter, scb);
  1970. // send the scsi packet back to kernel
  1971. scp->scsi_done(scp);
  1972. }
  1973. return;
  1974. }
  1975. /**
  1976. * megaraid_abort_handler - abort the scsi command
  1977. * @scp : command to be aborted
  1978. *
  1979. * Abort a previous SCSI request. Only commands on the pending list can be
  1980. * aborted. All the commands issued to the F/W must complete.
  1981. **/
  1982. static int
  1983. megaraid_abort_handler(struct scsi_cmnd *scp)
  1984. {
  1985. adapter_t *adapter;
  1986. mraid_device_t *raid_dev;
  1987. scb_t *scb;
  1988. scb_t *tmp;
  1989. int found;
  1990. unsigned long flags;
  1991. int i;
  1992. adapter = SCP2ADAPTER(scp);
  1993. raid_dev = ADAP2RAIDDEV(adapter);
  1994. con_log(CL_ANN, (KERN_WARNING
  1995. "megaraid: aborting cmd=%x <c=%d t=%d l=%d>\n",
  1996. scp->cmnd[0], SCP2CHANNEL(scp),
  1997. SCP2TARGET(scp), SCP2LUN(scp)));
  1998. // If FW has stopped responding, simply return failure
  1999. if (raid_dev->hw_error) {
  2000. con_log(CL_ANN, (KERN_NOTICE
  2001. "megaraid: hw error, not aborting\n"));
  2002. return FAILED;
  2003. }
  2004. // There might a race here, where the command was completed by the
  2005. // firmware and now it is on the completed list. Before we could
  2006. // complete the command to the kernel in dpc, the abort came.
  2007. // Find out if this is the case to avoid the race.
  2008. scb = NULL;
  2009. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  2010. list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
  2011. if (scb->scp == scp) { // Found command
  2012. list_del_init(&scb->list); // from completed list
  2013. con_log(CL_ANN, (KERN_WARNING
  2014. "megaraid: %d[%d:%d], abort from completed list\n",
  2015. scb->sno, scb->dev_channel, scb->dev_target));
  2016. scp->result = (DID_ABORT << 16);
  2017. scp->scsi_done(scp);
  2018. megaraid_dealloc_scb(adapter, scb);
  2019. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
  2020. flags);
  2021. return SUCCESS;
  2022. }
  2023. }
  2024. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  2025. // Find out if this command is still on the pending list. If it is and
  2026. // was never issued, abort and return success. If the command is owned
  2027. // by the firmware, we must wait for it to complete by the FW.
  2028. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2029. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2030. if (scb->scp == scp) { // Found command
  2031. list_del_init(&scb->list); // from pending list
  2032. ASSERT(!(scb->state & SCB_ISSUED));
  2033. con_log(CL_ANN, (KERN_WARNING
  2034. "megaraid abort: [%d:%d], driver owner\n",
  2035. scb->dev_channel, scb->dev_target));
  2036. scp->result = (DID_ABORT << 16);
  2037. scp->scsi_done(scp);
  2038. megaraid_dealloc_scb(adapter, scb);
  2039. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  2040. flags);
  2041. return SUCCESS;
  2042. }
  2043. }
  2044. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2045. // Check do we even own this command, in which case this would be
  2046. // owned by the firmware. The only way to locate the FW scb is to
  2047. // traverse through the list of all SCB, since driver does not
  2048. // maintain these SCBs on any list
  2049. found = 0;
  2050. spin_lock_irq(&adapter->lock);
  2051. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  2052. scb = adapter->kscb_list + i;
  2053. if (scb->scp == scp) {
  2054. found = 1;
  2055. if (!(scb->state & SCB_ISSUED)) {
  2056. con_log(CL_ANN, (KERN_WARNING
  2057. "megaraid abort: %d[%d:%d], invalid state\n",
  2058. scb->sno, scb->dev_channel, scb->dev_target));
  2059. BUG();
  2060. }
  2061. else {
  2062. con_log(CL_ANN, (KERN_WARNING
  2063. "megaraid abort: %d[%d:%d], fw owner\n",
  2064. scb->sno, scb->dev_channel, scb->dev_target));
  2065. }
  2066. }
  2067. }
  2068. spin_unlock_irq(&adapter->lock);
  2069. if (!found) {
  2070. con_log(CL_ANN, (KERN_WARNING "megaraid abort: do now own\n"));
  2071. // FIXME: Should there be a callback for this command?
  2072. return SUCCESS;
  2073. }
  2074. // We cannot actually abort a command owned by firmware, return
  2075. // failure and wait for reset. In host reset handler, we will find out
  2076. // if the HBA is still live
  2077. return FAILED;
  2078. }
  2079. /**
  2080. * megaraid_reset_handler - device reset hadler for mailbox based driver
  2081. * @scp : reference command
  2082. *
  2083. * Reset handler for the mailbox based controller. First try to find out if
  2084. * the FW is still live, in which case the outstanding commands counter mut go
  2085. * down to 0. If that happens, also issue the reservation reset command to
  2086. * relinquish (possible) reservations on the logical drives connected to this
  2087. * host.
  2088. **/
  2089. static int
  2090. megaraid_reset_handler(struct scsi_cmnd *scp)
  2091. {
  2092. adapter_t *adapter;
  2093. scb_t *scb;
  2094. scb_t *tmp;
  2095. mraid_device_t *raid_dev;
  2096. unsigned long flags;
  2097. uint8_t raw_mbox[sizeof(mbox_t)];
  2098. int rval;
  2099. int recovery_window;
  2100. int recovering;
  2101. int i;
  2102. uioc_t *kioc;
  2103. adapter = SCP2ADAPTER(scp);
  2104. raid_dev = ADAP2RAIDDEV(adapter);
  2105. // return failure if adapter is not responding
  2106. if (raid_dev->hw_error) {
  2107. con_log(CL_ANN, (KERN_NOTICE
  2108. "megaraid: hw error, cannot reset\n"));
  2109. return FAILED;
  2110. }
  2111. // Under exceptional conditions, FW can take up to 3 minutes to
  2112. // complete command processing. Wait for additional 2 minutes for the
  2113. // pending commands counter to go down to 0. If it doesn't, let the
  2114. // controller be marked offline
  2115. // Also, reset all the commands currently owned by the driver
  2116. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2117. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2118. list_del_init(&scb->list); // from pending list
  2119. if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
  2120. con_log(CL_ANN, (KERN_WARNING
  2121. "megaraid: IOCTL packet with %d[%d:%d] being reset\n",
  2122. scb->sno, scb->dev_channel, scb->dev_target));
  2123. scb->status = -1;
  2124. kioc = (uioc_t *)scb->gp;
  2125. kioc->status = -EFAULT;
  2126. megaraid_mbox_mm_done(adapter, scb);
  2127. } else {
  2128. if (scb->scp == scp) { // Found command
  2129. con_log(CL_ANN, (KERN_WARNING
  2130. "megaraid: %d[%d:%d], reset from pending list\n",
  2131. scb->sno, scb->dev_channel, scb->dev_target));
  2132. } else {
  2133. con_log(CL_ANN, (KERN_WARNING
  2134. "megaraid: IO packet with %d[%d:%d] being reset\n",
  2135. scb->sno, scb->dev_channel, scb->dev_target));
  2136. }
  2137. scb->scp->result = (DID_RESET << 16);
  2138. scb->scp->scsi_done(scb->scp);
  2139. megaraid_dealloc_scb(adapter, scb);
  2140. }
  2141. }
  2142. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2143. if (adapter->outstanding_cmds) {
  2144. con_log(CL_ANN, (KERN_NOTICE
  2145. "megaraid: %d outstanding commands. Max wait %d sec\n",
  2146. adapter->outstanding_cmds,
  2147. (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT)));
  2148. }
  2149. recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
  2150. recovering = adapter->outstanding_cmds;
  2151. for (i = 0; i < recovery_window; i++) {
  2152. megaraid_ack_sequence(adapter);
  2153. // print a message once every 5 seconds only
  2154. if (!(i % 5)) {
  2155. con_log(CL_ANN, (
  2156. "megaraid mbox: Wait for %d commands to complete:%d\n",
  2157. adapter->outstanding_cmds,
  2158. (MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT) - i));
  2159. }
  2160. // bailout if no recovery happened in reset time
  2161. if (adapter->outstanding_cmds == 0) {
  2162. break;
  2163. }
  2164. msleep(1000);
  2165. }
  2166. spin_lock(&adapter->lock);
  2167. // If still outstanding commands, bail out
  2168. if (adapter->outstanding_cmds) {
  2169. con_log(CL_ANN, (KERN_WARNING
  2170. "megaraid mbox: critical hardware error!\n"));
  2171. raid_dev->hw_error = 1;
  2172. rval = FAILED;
  2173. goto out;
  2174. }
  2175. else {
  2176. con_log(CL_ANN, (KERN_NOTICE
  2177. "megaraid mbox: reset sequence completed successfully\n"));
  2178. }
  2179. // If the controller supports clustering, reset reservations
  2180. if (!adapter->ha) {
  2181. rval = SUCCESS;
  2182. goto out;
  2183. }
  2184. // clear reservations if any
  2185. raw_mbox[0] = CLUSTER_CMD;
  2186. raw_mbox[2] = RESET_RESERVATIONS;
  2187. rval = SUCCESS;
  2188. if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
  2189. con_log(CL_ANN,
  2190. (KERN_INFO "megaraid: reservation reset\n"));
  2191. }
  2192. else {
  2193. rval = FAILED;
  2194. con_log(CL_ANN, (KERN_WARNING
  2195. "megaraid: reservation reset failed\n"));
  2196. }
  2197. out:
  2198. spin_unlock_irq(&adapter->lock);
  2199. return rval;
  2200. }
  2201. /*
  2202. * START: internal commands library
  2203. *
  2204. * This section of the driver has the common routine used by the driver and
  2205. * also has all the FW routines
  2206. */
  2207. /**
  2208. * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
  2209. * @adapter : controller's soft state
  2210. * @raw_mbox : the mailbox
  2211. *
  2212. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2213. * controllers.
  2214. */
  2215. static int
  2216. mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
  2217. {
  2218. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2219. mbox64_t *mbox64;
  2220. mbox_t *mbox;
  2221. uint8_t status;
  2222. int i;
  2223. mbox64 = raid_dev->mbox64;
  2224. mbox = raid_dev->mbox;
  2225. /*
  2226. * Wait until mailbox is free
  2227. */
  2228. if (megaraid_busywait_mbox(raid_dev) != 0)
  2229. goto blocked_mailbox;
  2230. /*
  2231. * Copy mailbox data into host structure
  2232. */
  2233. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
  2234. mbox->cmdid = 0xFE;
  2235. mbox->busy = 1;
  2236. mbox->poll = 0;
  2237. mbox->ack = 0;
  2238. mbox->numstatus = 0xFF;
  2239. mbox->status = 0xFF;
  2240. wmb();
  2241. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2242. // wait for maximum 1 second for status to post. If the status is not
  2243. // available within 1 second, assume FW is initializing and wait
  2244. // for an extended amount of time
  2245. if (mbox->numstatus == 0xFF) { // status not yet available
  2246. udelay(25);
  2247. for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
  2248. rmb();
  2249. msleep(1);
  2250. }
  2251. if (i == 1000) {
  2252. con_log(CL_ANN, (KERN_NOTICE
  2253. "megaraid mailbox: wait for FW to boot "));
  2254. for (i = 0; (mbox->numstatus == 0xFF) &&
  2255. (i < MBOX_RESET_WAIT); i++) {
  2256. rmb();
  2257. con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
  2258. MBOX_RESET_WAIT - i));
  2259. msleep(1000);
  2260. }
  2261. if (i == MBOX_RESET_WAIT) {
  2262. con_log(CL_ANN, (
  2263. "\nmegaraid mailbox: status not available\n"));
  2264. return -1;
  2265. }
  2266. con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
  2267. }
  2268. }
  2269. // wait for maximum 1 second for poll semaphore
  2270. if (mbox->poll != 0x77) {
  2271. udelay(25);
  2272. for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
  2273. rmb();
  2274. msleep(1);
  2275. }
  2276. if (i == 1000) {
  2277. con_log(CL_ANN, (KERN_WARNING
  2278. "megaraid mailbox: could not get poll semaphore\n"));
  2279. return -1;
  2280. }
  2281. }
  2282. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2283. wmb();
  2284. // wait for maximum 1 second for acknowledgement
  2285. if (RDINDOOR(raid_dev) & 0x2) {
  2286. udelay(25);
  2287. for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
  2288. rmb();
  2289. msleep(1);
  2290. }
  2291. if (i == 1000) {
  2292. con_log(CL_ANN, (KERN_WARNING
  2293. "megaraid mailbox: could not acknowledge\n"));
  2294. return -1;
  2295. }
  2296. }
  2297. mbox->poll = 0;
  2298. mbox->ack = 0x77;
  2299. status = mbox->status;
  2300. // invalidate the completed command id array. After command
  2301. // completion, firmware would write the valid id.
  2302. mbox->numstatus = 0xFF;
  2303. mbox->status = 0xFF;
  2304. for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
  2305. mbox->completed[i] = 0xFF;
  2306. }
  2307. return status;
  2308. blocked_mailbox:
  2309. con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
  2310. return -1;
  2311. }
  2312. /**
  2313. * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
  2314. * @adapter : controller's soft state
  2315. * @raw_mbox : the mailbox
  2316. *
  2317. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2318. * controllers. This is a faster version of the synchronous command and
  2319. * therefore can be called in interrupt-context as well.
  2320. */
  2321. static int
  2322. mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
  2323. {
  2324. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2325. mbox_t *mbox;
  2326. long i;
  2327. mbox = raid_dev->mbox;
  2328. // return immediately if the mailbox is busy
  2329. if (mbox->busy) return -1;
  2330. // Copy mailbox data into host structure
  2331. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
  2332. mbox->cmdid = 0xFE;
  2333. mbox->busy = 1;
  2334. mbox->poll = 0;
  2335. mbox->ack = 0;
  2336. mbox->numstatus = 0xFF;
  2337. mbox->status = 0xFF;
  2338. wmb();
  2339. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2340. for (i = 0; i < MBOX_SYNC_WAIT_CNT; i++) {
  2341. if (mbox->numstatus != 0xFF) break;
  2342. rmb();
  2343. udelay(MBOX_SYNC_DELAY_200);
  2344. }
  2345. if (i == MBOX_SYNC_WAIT_CNT) {
  2346. // We may need to re-calibrate the counter
  2347. con_log(CL_ANN, (KERN_CRIT
  2348. "megaraid: fast sync command timed out\n"));
  2349. }
  2350. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2351. wmb();
  2352. return mbox->status;
  2353. }
  2354. /**
  2355. * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
  2356. * @raid_dev : RAID device (HBA) soft state
  2357. *
  2358. * Wait until the controller's mailbox is available to accept more commands.
  2359. * Wait for at most 1 second.
  2360. */
  2361. static int
  2362. megaraid_busywait_mbox(mraid_device_t *raid_dev)
  2363. {
  2364. mbox_t *mbox = raid_dev->mbox;
  2365. int i = 0;
  2366. if (mbox->busy) {
  2367. udelay(25);
  2368. for (i = 0; mbox->busy && i < 1000; i++)
  2369. msleep(1);
  2370. }
  2371. if (i < 1000) return 0;
  2372. else return -1;
  2373. }
  2374. /**
  2375. * megaraid_mbox_product_info - some static information about the controller
  2376. * @adapter : our soft state
  2377. *
  2378. * Issue commands to the controller to grab some parameters required by our
  2379. * caller.
  2380. */
  2381. static int
  2382. megaraid_mbox_product_info(adapter_t *adapter)
  2383. {
  2384. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2385. mbox_t *mbox;
  2386. uint8_t raw_mbox[sizeof(mbox_t)];
  2387. mraid_pinfo_t *pinfo;
  2388. dma_addr_t pinfo_dma_h;
  2389. mraid_inquiry3_t *mraid_inq3;
  2390. int i;
  2391. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2392. mbox = (mbox_t *)raw_mbox;
  2393. /*
  2394. * Issue an ENQUIRY3 command to find out certain adapter parameters,
  2395. * e.g., max channels, max commands etc.
  2396. */
  2397. pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2398. &pinfo_dma_h);
  2399. if (pinfo == NULL) {
  2400. con_log(CL_ANN, (KERN_WARNING
  2401. "megaraid: out of memory, %s %d\n", __func__,
  2402. __LINE__));
  2403. return -1;
  2404. }
  2405. memset(pinfo, 0, sizeof(mraid_pinfo_t));
  2406. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2407. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2408. raw_mbox[0] = FC_NEW_CONFIG;
  2409. raw_mbox[2] = NC_SUBOP_ENQUIRY3;
  2410. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
  2411. // Issue the command
  2412. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2413. con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
  2414. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2415. pinfo, pinfo_dma_h);
  2416. return -1;
  2417. }
  2418. /*
  2419. * Collect information about state of each physical drive
  2420. * attached to the controller. We will expose all the disks
  2421. * which are not part of RAID
  2422. */
  2423. mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
  2424. for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
  2425. raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
  2426. }
  2427. /*
  2428. * Get product info for information like number of channels,
  2429. * maximum commands supported.
  2430. */
  2431. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2432. mbox->xferaddr = (uint32_t)pinfo_dma_h;
  2433. raw_mbox[0] = FC_NEW_CONFIG;
  2434. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
  2435. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2436. con_log(CL_ANN, (KERN_WARNING
  2437. "megaraid: product info failed\n"));
  2438. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2439. pinfo, pinfo_dma_h);
  2440. return -1;
  2441. }
  2442. /*
  2443. * Setup some parameters for host, as required by our caller
  2444. */
  2445. adapter->max_channel = pinfo->nchannels;
  2446. /*
  2447. * we will export all the logical drives on a single channel.
  2448. * Add 1 since inquires do not come for inititor ID
  2449. */
  2450. adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
  2451. adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
  2452. /*
  2453. * These are the maximum outstanding commands for the scsi-layer
  2454. */
  2455. adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
  2456. memset(adapter->fw_version, 0, VERSION_SIZE);
  2457. memset(adapter->bios_version, 0, VERSION_SIZE);
  2458. memcpy(adapter->fw_version, pinfo->fw_version, 4);
  2459. adapter->fw_version[4] = 0;
  2460. memcpy(adapter->bios_version, pinfo->bios_version, 4);
  2461. adapter->bios_version[4] = 0;
  2462. con_log(CL_ANN, (KERN_NOTICE
  2463. "megaraid: fw version:[%s] bios version:[%s]\n",
  2464. adapter->fw_version, adapter->bios_version));
  2465. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
  2466. pinfo_dma_h);
  2467. return 0;
  2468. }
  2469. /**
  2470. * megaraid_mbox_extended_cdb - check for support for extended CDBs
  2471. * @adapter : soft state for the controller
  2472. *
  2473. * This routine check whether the controller in question supports extended
  2474. * ( > 10 bytes ) CDBs.
  2475. */
  2476. static int
  2477. megaraid_mbox_extended_cdb(adapter_t *adapter)
  2478. {
  2479. mbox_t *mbox;
  2480. uint8_t raw_mbox[sizeof(mbox_t)];
  2481. int rval;
  2482. mbox = (mbox_t *)raw_mbox;
  2483. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2484. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2485. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2486. raw_mbox[0] = MAIN_MISC_OPCODE;
  2487. raw_mbox[2] = SUPPORT_EXT_CDB;
  2488. /*
  2489. * Issue the command
  2490. */
  2491. rval = 0;
  2492. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2493. rval = -1;
  2494. }
  2495. return rval;
  2496. }
  2497. /**
  2498. * megaraid_mbox_support_ha - Do we support clustering
  2499. * @adapter : soft state for the controller
  2500. * @init_id : ID of the initiator
  2501. *
  2502. * Determine if the firmware supports clustering and the ID of the initiator.
  2503. */
  2504. static int
  2505. megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
  2506. {
  2507. mbox_t *mbox;
  2508. uint8_t raw_mbox[sizeof(mbox_t)];
  2509. int rval;
  2510. mbox = (mbox_t *)raw_mbox;
  2511. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2512. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2513. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2514. raw_mbox[0] = GET_TARGET_ID;
  2515. // Issue the command
  2516. *init_id = 7;
  2517. rval = -1;
  2518. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2519. *init_id = *(uint8_t *)adapter->ibuf;
  2520. con_log(CL_ANN, (KERN_INFO
  2521. "megaraid: cluster firmware, initiator ID: %d\n",
  2522. *init_id));
  2523. rval = 0;
  2524. }
  2525. return rval;
  2526. }
  2527. /**
  2528. * megaraid_mbox_support_random_del - Do we support random deletion
  2529. * @adapter : soft state for the controller
  2530. *
  2531. * Determine if the firmware supports random deletion.
  2532. * Return: 1 is operation supported, 0 otherwise
  2533. */
  2534. static int
  2535. megaraid_mbox_support_random_del(adapter_t *adapter)
  2536. {
  2537. mbox_t *mbox;
  2538. uint8_t raw_mbox[sizeof(mbox_t)];
  2539. int rval;
  2540. /*
  2541. * Newer firmware on Dell CERC expect a different
  2542. * random deletion handling, so disable it.
  2543. */
  2544. if (adapter->pdev->vendor == PCI_VENDOR_ID_AMI &&
  2545. adapter->pdev->device == PCI_DEVICE_ID_AMI_MEGARAID3 &&
  2546. adapter->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
  2547. adapter->pdev->subsystem_device == PCI_SUBSYS_ID_CERC_ATA100_4CH &&
  2548. (adapter->fw_version[0] > '6' ||
  2549. (adapter->fw_version[0] == '6' &&
  2550. adapter->fw_version[2] > '6') ||
  2551. (adapter->fw_version[0] == '6'
  2552. && adapter->fw_version[2] == '6'
  2553. && adapter->fw_version[3] > '1'))) {
  2554. con_log(CL_DLEVEL1, ("megaraid: disable random deletion\n"));
  2555. return 0;
  2556. }
  2557. mbox = (mbox_t *)raw_mbox;
  2558. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2559. raw_mbox[0] = FC_DEL_LOGDRV;
  2560. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  2561. // Issue the command
  2562. rval = 0;
  2563. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2564. con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
  2565. rval = 1;
  2566. }
  2567. return rval;
  2568. }
  2569. /**
  2570. * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
  2571. * @adapter : soft state for the controller
  2572. *
  2573. * Find out the maximum number of scatter-gather elements supported by the
  2574. * firmware.
  2575. */
  2576. static int
  2577. megaraid_mbox_get_max_sg(adapter_t *adapter)
  2578. {
  2579. mbox_t *mbox;
  2580. uint8_t raw_mbox[sizeof(mbox_t)];
  2581. int nsg;
  2582. mbox = (mbox_t *)raw_mbox;
  2583. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2584. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2585. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2586. raw_mbox[0] = MAIN_MISC_OPCODE;
  2587. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  2588. // Issue the command
  2589. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2590. nsg = *(uint8_t *)adapter->ibuf;
  2591. }
  2592. else {
  2593. nsg = MBOX_DEFAULT_SG_SIZE;
  2594. }
  2595. if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
  2596. return nsg;
  2597. }
  2598. /**
  2599. * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
  2600. * @adapter : soft state for the controller
  2601. *
  2602. * Enumerate the RAID and SCSI channels for ROMB platforms so that channels
  2603. * can be exported as regular SCSI channels.
  2604. */
  2605. static void
  2606. megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
  2607. {
  2608. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2609. mbox_t *mbox;
  2610. uint8_t raw_mbox[sizeof(mbox_t)];
  2611. mbox = (mbox_t *)raw_mbox;
  2612. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2613. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2614. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2615. raw_mbox[0] = CHNL_CLASS;
  2616. raw_mbox[2] = GET_CHNL_CLASS;
  2617. // Issue the command. If the command fails, all channels are RAID
  2618. // channels
  2619. raid_dev->channel_class = 0xFF;
  2620. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2621. raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
  2622. }
  2623. return;
  2624. }
  2625. /**
  2626. * megaraid_mbox_flush_cache - flush adapter and disks cache
  2627. * @adapter : soft state for the controller
  2628. *
  2629. * Flush adapter cache followed by disks cache.
  2630. */
  2631. static void
  2632. megaraid_mbox_flush_cache(adapter_t *adapter)
  2633. {
  2634. mbox_t *mbox;
  2635. uint8_t raw_mbox[sizeof(mbox_t)];
  2636. mbox = (mbox_t *)raw_mbox;
  2637. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2638. raw_mbox[0] = FLUSH_ADAPTER;
  2639. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2640. con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
  2641. }
  2642. raw_mbox[0] = FLUSH_SYSTEM;
  2643. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2644. con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
  2645. }
  2646. return;
  2647. }
  2648. /**
  2649. * megaraid_mbox_fire_sync_cmd - fire the sync cmd
  2650. * @adapter : soft state for the controller
  2651. *
  2652. * Clears the pending cmds in FW and reinits its RAID structs.
  2653. */
  2654. static int
  2655. megaraid_mbox_fire_sync_cmd(adapter_t *adapter)
  2656. {
  2657. mbox_t *mbox;
  2658. uint8_t raw_mbox[sizeof(mbox_t)];
  2659. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2660. mbox64_t *mbox64;
  2661. int status = 0;
  2662. int i;
  2663. uint32_t dword;
  2664. mbox = (mbox_t *)raw_mbox;
  2665. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2666. raw_mbox[0] = 0xFF;
  2667. mbox64 = raid_dev->mbox64;
  2668. mbox = raid_dev->mbox;
  2669. /* Wait until mailbox is free */
  2670. if (megaraid_busywait_mbox(raid_dev) != 0) {
  2671. status = 1;
  2672. goto blocked_mailbox;
  2673. }
  2674. /* Copy mailbox data into host structure */
  2675. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
  2676. mbox->cmdid = 0xFE;
  2677. mbox->busy = 1;
  2678. mbox->poll = 0;
  2679. mbox->ack = 0;
  2680. mbox->numstatus = 0;
  2681. mbox->status = 0;
  2682. wmb();
  2683. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2684. /* Wait for maximum 1 min for status to post.
  2685. * If the Firmware SUPPORTS the ABOVE COMMAND,
  2686. * mbox->cmd will be set to 0
  2687. * else
  2688. * the firmware will reject the command with
  2689. * mbox->numstatus set to 1
  2690. */
  2691. i = 0;
  2692. status = 0;
  2693. while (!mbox->numstatus && mbox->cmd == 0xFF) {
  2694. rmb();
  2695. msleep(1);
  2696. i++;
  2697. if (i > 1000 * 60) {
  2698. status = 1;
  2699. break;
  2700. }
  2701. }
  2702. if (mbox->numstatus == 1)
  2703. status = 1; /*cmd not supported*/
  2704. /* Check for interrupt line */
  2705. dword = RDOUTDOOR(raid_dev);
  2706. WROUTDOOR(raid_dev, dword);
  2707. WRINDOOR(raid_dev,2);
  2708. return status;
  2709. blocked_mailbox:
  2710. con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n"));
  2711. return status;
  2712. }
  2713. /**
  2714. * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
  2715. * @adapter : controller's soft state
  2716. * @scb : SCB to be displayed
  2717. * @level : debug level for console print
  2718. *
  2719. * Diplay information about the given SCB iff the current debug level is
  2720. * verbose.
  2721. */
  2722. static void
  2723. megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
  2724. {
  2725. mbox_ccb_t *ccb;
  2726. struct scsi_cmnd *scp;
  2727. mbox_t *mbox;
  2728. int level;
  2729. int i;
  2730. ccb = (mbox_ccb_t *)scb->ccb;
  2731. scp = scb->scp;
  2732. mbox = ccb->mbox;
  2733. level = CL_DLEVEL3;
  2734. con_log(level, (KERN_NOTICE
  2735. "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
  2736. mbox->cmd, scb->sno));
  2737. con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
  2738. mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
  2739. mbox->numsge));
  2740. if (!scp) return;
  2741. con_log(level, (KERN_NOTICE "scsi cmnd: "));
  2742. for (i = 0; i < scp->cmd_len; i++) {
  2743. con_log(level, ("%#2.02x ", scp->cmnd[i]));
  2744. }
  2745. con_log(level, ("\n"));
  2746. return;
  2747. }
  2748. /**
  2749. * megaraid_mbox_setup_device_map - manage device ids
  2750. * @adapter : Driver's soft state
  2751. *
  2752. * Manange the device ids to have an appropriate mapping between the kernel
  2753. * scsi addresses and megaraid scsi and logical drive addresses. We export
  2754. * scsi devices on their actual addresses, whereas the logical drives are
  2755. * exported on a virtual scsi channel.
  2756. */
  2757. static void
  2758. megaraid_mbox_setup_device_map(adapter_t *adapter)
  2759. {
  2760. uint8_t c;
  2761. uint8_t t;
  2762. /*
  2763. * First fill the values on the logical drive channel
  2764. */
  2765. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2766. adapter->device_ids[adapter->max_channel][t] =
  2767. (t < adapter->init_id) ? t : t - 1;
  2768. adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
  2769. /*
  2770. * Fill the values on the physical devices channels
  2771. */
  2772. for (c = 0; c < adapter->max_channel; c++)
  2773. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2774. adapter->device_ids[c][t] = (c << 8) | t;
  2775. }
  2776. /*
  2777. * END: internal commands library
  2778. */
  2779. /*
  2780. * START: Interface for the common management module
  2781. *
  2782. * This is the module, which interfaces with the common management module to
  2783. * provide support for ioctl and sysfs
  2784. */
  2785. /**
  2786. * megaraid_cmm_register - register with the management module
  2787. * @adapter : HBA soft state
  2788. *
  2789. * Register with the management module, which allows applications to issue
  2790. * ioctl calls to the drivers. This interface is used by the management module
  2791. * to setup sysfs support as well.
  2792. */
  2793. static int
  2794. megaraid_cmm_register(adapter_t *adapter)
  2795. {
  2796. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2797. mraid_mmadp_t adp;
  2798. scb_t *scb;
  2799. mbox_ccb_t *ccb;
  2800. int rval;
  2801. int i;
  2802. // Allocate memory for the base list of scb for management module.
  2803. adapter->uscb_list = kcalloc(MBOX_MAX_USER_CMDS, sizeof(scb_t), GFP_KERNEL);
  2804. if (adapter->uscb_list == NULL) {
  2805. con_log(CL_ANN, (KERN_WARNING
  2806. "megaraid: out of memory, %s %d\n", __func__,
  2807. __LINE__));
  2808. return -1;
  2809. }
  2810. // Initialize the synchronization parameters for resources for
  2811. // commands for management module
  2812. INIT_LIST_HEAD(&adapter->uscb_pool);
  2813. spin_lock_init(USER_FREE_LIST_LOCK(adapter));
  2814. // link all the packets. Note, CCB for commands, coming from the
  2815. // commom management module, mailbox physical address are already
  2816. // setup by it. We just need placeholder for that in our local command
  2817. // control blocks
  2818. for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
  2819. scb = adapter->uscb_list + i;
  2820. ccb = raid_dev->uccb_list + i;
  2821. scb->ccb = (caddr_t)ccb;
  2822. ccb->mbox64 = raid_dev->umbox64 + i;
  2823. ccb->mbox = &ccb->mbox64->mbox32;
  2824. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  2825. scb->gp = 0;
  2826. // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
  2827. // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
  2828. scb->sno = i + MBOX_MAX_SCSI_CMDS;
  2829. scb->scp = NULL;
  2830. scb->state = SCB_FREE;
  2831. scb->dma_direction = PCI_DMA_NONE;
  2832. scb->dma_type = MRAID_DMA_NONE;
  2833. scb->dev_channel = -1;
  2834. scb->dev_target = -1;
  2835. // put scb in the free pool
  2836. list_add_tail(&scb->list, &adapter->uscb_pool);
  2837. }
  2838. adp.unique_id = adapter->unique_id;
  2839. adp.drvr_type = DRVRTYPE_MBOX;
  2840. adp.drvr_data = (unsigned long)adapter;
  2841. adp.pdev = adapter->pdev;
  2842. adp.issue_uioc = megaraid_mbox_mm_handler;
  2843. adp.timeout = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
  2844. adp.max_kioc = MBOX_MAX_USER_CMDS;
  2845. if ((rval = mraid_mm_register_adp(&adp)) != 0) {
  2846. con_log(CL_ANN, (KERN_WARNING
  2847. "megaraid mbox: did not register with CMM\n"));
  2848. kfree(adapter->uscb_list);
  2849. }
  2850. return rval;
  2851. }
  2852. /**
  2853. * megaraid_cmm_unregister - un-register with the management module
  2854. * @adapter : HBA soft state
  2855. *
  2856. * Un-register with the management module.
  2857. * FIXME: mgmt module must return failure for unregister if it has pending
  2858. * commands in LLD.
  2859. */
  2860. static int
  2861. megaraid_cmm_unregister(adapter_t *adapter)
  2862. {
  2863. kfree(adapter->uscb_list);
  2864. mraid_mm_unregister_adp(adapter->unique_id);
  2865. return 0;
  2866. }
  2867. /**
  2868. * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
  2869. * @drvr_data : LLD specific data
  2870. * @kioc : CMM interface packet
  2871. * @action : command action
  2872. *
  2873. * This routine is invoked whenever the Common Management Module (CMM) has a
  2874. * command for us. The 'action' parameter specifies if this is a new command
  2875. * or otherwise.
  2876. */
  2877. static int
  2878. megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
  2879. {
  2880. adapter_t *adapter;
  2881. if (action != IOCTL_ISSUE) {
  2882. con_log(CL_ANN, (KERN_WARNING
  2883. "megaraid: unsupported management action:%#2x\n",
  2884. action));
  2885. return (-ENOTSUPP);
  2886. }
  2887. adapter = (adapter_t *)drvr_data;
  2888. // make sure this adapter is not being detached right now.
  2889. if (atomic_read(&adapter->being_detached)) {
  2890. con_log(CL_ANN, (KERN_WARNING
  2891. "megaraid: reject management request, detaching\n"));
  2892. return (-ENODEV);
  2893. }
  2894. switch (kioc->opcode) {
  2895. case GET_ADAP_INFO:
  2896. kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
  2897. (unsigned long)kioc->buf_vaddr);
  2898. kioc->done(kioc);
  2899. return kioc->status;
  2900. case MBOX_CMD:
  2901. return megaraid_mbox_mm_command(adapter, kioc);
  2902. default:
  2903. kioc->status = (-EINVAL);
  2904. kioc->done(kioc);
  2905. return (-EINVAL);
  2906. }
  2907. return 0; // not reached
  2908. }
  2909. /**
  2910. * megaraid_mbox_mm_command - issues commands routed through CMM
  2911. * @adapter : HBA soft state
  2912. * @kioc : management command packet
  2913. *
  2914. * Issues commands, which are routed through the management module.
  2915. */
  2916. static int
  2917. megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
  2918. {
  2919. struct list_head *head = &adapter->uscb_pool;
  2920. mbox64_t *mbox64;
  2921. uint8_t *raw_mbox;
  2922. scb_t *scb;
  2923. mbox_ccb_t *ccb;
  2924. unsigned long flags;
  2925. // detach one scb from free pool
  2926. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  2927. if (list_empty(head)) { // should never happen because of CMM
  2928. con_log(CL_ANN, (KERN_WARNING
  2929. "megaraid mbox: bug in cmm handler, lost resources\n"));
  2930. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2931. return (-EINVAL);
  2932. }
  2933. scb = list_entry(head->next, scb_t, list);
  2934. list_del_init(&scb->list);
  2935. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2936. scb->state = SCB_ACTIVE;
  2937. scb->dma_type = MRAID_DMA_NONE;
  2938. scb->dma_direction = PCI_DMA_NONE;
  2939. ccb = (mbox_ccb_t *)scb->ccb;
  2940. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  2941. raw_mbox = (uint8_t *)&mbox64->mbox32;
  2942. memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
  2943. scb->gp = (unsigned long)kioc;
  2944. /*
  2945. * If it is a logdrv random delete operation, we have to wait till
  2946. * there are no outstanding cmds at the fw and then issue it directly
  2947. */
  2948. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  2949. if (wait_till_fw_empty(adapter)) {
  2950. con_log(CL_ANN, (KERN_NOTICE
  2951. "megaraid mbox: LD delete, timed out\n"));
  2952. kioc->status = -ETIME;
  2953. scb->status = -1;
  2954. megaraid_mbox_mm_done(adapter, scb);
  2955. return (-ETIME);
  2956. }
  2957. INIT_LIST_HEAD(&scb->list);
  2958. scb->state = SCB_ISSUED;
  2959. if (mbox_post_cmd(adapter, scb) != 0) {
  2960. con_log(CL_ANN, (KERN_NOTICE
  2961. "megaraid mbox: LD delete, mailbox busy\n"));
  2962. kioc->status = -EBUSY;
  2963. scb->status = -1;
  2964. megaraid_mbox_mm_done(adapter, scb);
  2965. return (-EBUSY);
  2966. }
  2967. return 0;
  2968. }
  2969. // put the command on the pending list and execute
  2970. megaraid_mbox_runpendq(adapter, scb);
  2971. return 0;
  2972. }
  2973. static int
  2974. wait_till_fw_empty(adapter_t *adapter)
  2975. {
  2976. unsigned long flags = 0;
  2977. int i;
  2978. /*
  2979. * Set the quiescent flag to stop issuing cmds to FW.
  2980. */
  2981. spin_lock_irqsave(&adapter->lock, flags);
  2982. adapter->quiescent++;
  2983. spin_unlock_irqrestore(&adapter->lock, flags);
  2984. /*
  2985. * Wait till there are no more cmds outstanding at FW. Try for at most
  2986. * 60 seconds
  2987. */
  2988. for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
  2989. con_log(CL_DLEVEL1, (KERN_INFO
  2990. "megaraid: FW has %d pending commands\n",
  2991. adapter->outstanding_cmds));
  2992. msleep(1000);
  2993. }
  2994. return adapter->outstanding_cmds;
  2995. }
  2996. /**
  2997. * megaraid_mbox_mm_done - callback for CMM commands
  2998. * @adapter : HBA soft state
  2999. * @scb : completed command
  3000. *
  3001. * Callback routine for internal commands originated from the management
  3002. * module.
  3003. */
  3004. static void
  3005. megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
  3006. {
  3007. uioc_t *kioc;
  3008. mbox64_t *mbox64;
  3009. uint8_t *raw_mbox;
  3010. unsigned long flags;
  3011. kioc = (uioc_t *)scb->gp;
  3012. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  3013. mbox64->mbox32.status = scb->status;
  3014. raw_mbox = (uint8_t *)&mbox64->mbox32;
  3015. // put scb in the free pool
  3016. scb->state = SCB_FREE;
  3017. scb->scp = NULL;
  3018. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  3019. list_add(&scb->list, &adapter->uscb_pool);
  3020. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  3021. // if a delete logical drive operation succeeded, restart the
  3022. // controller
  3023. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  3024. adapter->quiescent--;
  3025. megaraid_mbox_runpendq(adapter, NULL);
  3026. }
  3027. kioc->done(kioc);
  3028. return;
  3029. }
  3030. /**
  3031. * gather_hbainfo - HBA characteristics for the applications
  3032. * @adapter : HBA soft state
  3033. * @hinfo : pointer to the caller's host info strucuture
  3034. */
  3035. static int
  3036. gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
  3037. {
  3038. uint8_t dmajor;
  3039. dmajor = megaraid_mbox_version[0];
  3040. hinfo->pci_vendor_id = adapter->pdev->vendor;
  3041. hinfo->pci_device_id = adapter->pdev->device;
  3042. hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
  3043. hinfo->subsys_device_id = adapter->pdev->subsystem_device;
  3044. hinfo->pci_bus = adapter->pdev->bus->number;
  3045. hinfo->pci_dev_fn = adapter->pdev->devfn;
  3046. hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
  3047. hinfo->irq = adapter->host->irq;
  3048. hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
  3049. hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
  3050. hinfo->host_no = adapter->host->host_no;
  3051. return 0;
  3052. }
  3053. /*
  3054. * END: Interface for the common management module
  3055. */
  3056. /**
  3057. * megaraid_sysfs_alloc_resources - allocate sysfs related resources
  3058. * @adapter : controller's soft state
  3059. *
  3060. * Allocate packets required to issue FW calls whenever the sysfs attributes
  3061. * are read. These attributes would require up-to-date information from the
  3062. * FW. Also set up resources for mutual exclusion to share these resources and
  3063. * the wait queue.
  3064. *
  3065. * Return 0 on success.
  3066. * Return -ERROR_CODE on failure.
  3067. */
  3068. static int
  3069. megaraid_sysfs_alloc_resources(adapter_t *adapter)
  3070. {
  3071. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3072. int rval = 0;
  3073. raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
  3074. raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
  3075. raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
  3076. PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
  3077. if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
  3078. !raid_dev->sysfs_buffer) {
  3079. con_log(CL_ANN, (KERN_WARNING
  3080. "megaraid: out of memory, %s %d\n", __func__,
  3081. __LINE__));
  3082. rval = -ENOMEM;
  3083. megaraid_sysfs_free_resources(adapter);
  3084. }
  3085. mutex_init(&raid_dev->sysfs_mtx);
  3086. init_waitqueue_head(&raid_dev->sysfs_wait_q);
  3087. return rval;
  3088. }
  3089. /**
  3090. * megaraid_sysfs_free_resources - free sysfs related resources
  3091. * @adapter : controller's soft state
  3092. *
  3093. * Free packets allocated for sysfs FW commands
  3094. */
  3095. static void
  3096. megaraid_sysfs_free_resources(adapter_t *adapter)
  3097. {
  3098. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3099. kfree(raid_dev->sysfs_uioc);
  3100. kfree(raid_dev->sysfs_mbox64);
  3101. if (raid_dev->sysfs_buffer) {
  3102. pci_free_consistent(adapter->pdev, PAGE_SIZE,
  3103. raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
  3104. }
  3105. }
  3106. /**
  3107. * megaraid_sysfs_get_ldmap_done - callback for get ldmap
  3108. * @uioc : completed packet
  3109. *
  3110. * Callback routine called in the ISR/tasklet context for get ldmap call
  3111. */
  3112. static void
  3113. megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
  3114. {
  3115. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3116. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3117. uioc->status = 0;
  3118. wake_up(&raid_dev->sysfs_wait_q);
  3119. }
  3120. /**
  3121. * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
  3122. * @data : timed out packet
  3123. *
  3124. * Timeout routine to recover and return to application, in case the adapter
  3125. * has stopped responding. A timeout of 60 seconds for this command seems like
  3126. * a good value.
  3127. */
  3128. static void
  3129. megaraid_sysfs_get_ldmap_timeout(unsigned long data)
  3130. {
  3131. uioc_t *uioc = (uioc_t *)data;
  3132. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3133. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3134. uioc->status = -ETIME;
  3135. wake_up(&raid_dev->sysfs_wait_q);
  3136. }
  3137. /**
  3138. * megaraid_sysfs_get_ldmap - get update logical drive map
  3139. * @adapter : controller's soft state
  3140. *
  3141. * This routine will be called whenever user reads the logical drive
  3142. * attributes, go get the current logical drive mapping table from the
  3143. * firmware. We use the management API's to issue commands to the controller.
  3144. *
  3145. * NOTE: The commands issuance functionality is not generalized and
  3146. * implemented in context of "get ld map" command only. If required, the
  3147. * command issuance logical can be trivially pulled out and implemented as a
  3148. * standalone library. For now, this should suffice since there is no other
  3149. * user of this interface.
  3150. *
  3151. * Return 0 on success.
  3152. * Return -1 on failure.
  3153. */
  3154. static int
  3155. megaraid_sysfs_get_ldmap(adapter_t *adapter)
  3156. {
  3157. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3158. uioc_t *uioc;
  3159. mbox64_t *mbox64;
  3160. mbox_t *mbox;
  3161. char *raw_mbox;
  3162. struct timer_list sysfs_timer;
  3163. struct timer_list *timerp;
  3164. caddr_t ldmap;
  3165. int rval = 0;
  3166. /*
  3167. * Allow only one read at a time to go through the sysfs attributes
  3168. */
  3169. mutex_lock(&raid_dev->sysfs_mtx);
  3170. uioc = raid_dev->sysfs_uioc;
  3171. mbox64 = raid_dev->sysfs_mbox64;
  3172. ldmap = raid_dev->sysfs_buffer;
  3173. memset(uioc, 0, sizeof(uioc_t));
  3174. memset(mbox64, 0, sizeof(mbox64_t));
  3175. memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
  3176. mbox = &mbox64->mbox32;
  3177. raw_mbox = (char *)mbox;
  3178. uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
  3179. uioc->buf_vaddr = (caddr_t)adapter;
  3180. uioc->status = -ENODATA;
  3181. uioc->done = megaraid_sysfs_get_ldmap_done;
  3182. /*
  3183. * Prepare the mailbox packet to get the current logical drive mapping
  3184. * table
  3185. */
  3186. mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
  3187. raw_mbox[0] = FC_DEL_LOGDRV;
  3188. raw_mbox[2] = OP_GET_LDID_MAP;
  3189. /*
  3190. * Setup a timer to recover from a non-responding controller
  3191. */
  3192. timerp = &sysfs_timer;
  3193. init_timer(timerp);
  3194. timerp->function = megaraid_sysfs_get_ldmap_timeout;
  3195. timerp->data = (unsigned long)uioc;
  3196. timerp->expires = jiffies + 60 * HZ;
  3197. add_timer(timerp);
  3198. /*
  3199. * Send the command to the firmware
  3200. */
  3201. rval = megaraid_mbox_mm_command(adapter, uioc);
  3202. if (rval == 0) { // command successfully issued
  3203. wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
  3204. /*
  3205. * Check if the command timed out
  3206. */
  3207. if (uioc->status == -ETIME) {
  3208. con_log(CL_ANN, (KERN_NOTICE
  3209. "megaraid: sysfs get ld map timed out\n"));
  3210. rval = -ETIME;
  3211. }
  3212. else {
  3213. rval = mbox->status;
  3214. }
  3215. if (rval == 0) {
  3216. memcpy(raid_dev->curr_ldmap, ldmap,
  3217. sizeof(raid_dev->curr_ldmap));
  3218. }
  3219. else {
  3220. con_log(CL_ANN, (KERN_NOTICE
  3221. "megaraid: get ld map failed with %x\n", rval));
  3222. }
  3223. }
  3224. else {
  3225. con_log(CL_ANN, (KERN_NOTICE
  3226. "megaraid: could not issue ldmap command:%x\n", rval));
  3227. }
  3228. del_timer_sync(timerp);
  3229. mutex_unlock(&raid_dev->sysfs_mtx);
  3230. return rval;
  3231. }
  3232. /**
  3233. * megaraid_sysfs_show_app_hndl - display application handle for this adapter
  3234. * @cdev : class device object representation for the host
  3235. * @buf : buffer to send data to
  3236. *
  3237. * Display the handle used by the applications while executing management
  3238. * tasks on the adapter. We invoke a management module API to get the adapter
  3239. * handle, since we do not interface with applications directly.
  3240. */
  3241. static ssize_t
  3242. megaraid_sysfs_show_app_hndl(struct device *dev, struct device_attribute *attr,
  3243. char *buf)
  3244. {
  3245. struct Scsi_Host *shost = class_to_shost(dev);
  3246. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
  3247. uint32_t app_hndl;
  3248. app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
  3249. return snprintf(buf, 8, "%u\n", app_hndl);
  3250. }
  3251. /**
  3252. * megaraid_sysfs_show_ldnum - display the logical drive number for this device
  3253. * @dev : device object representation for the scsi device
  3254. * @attr : device attribute to show
  3255. * @buf : buffer to send data to
  3256. *
  3257. * Display the logical drive number for the device in question, if it a valid
  3258. * logical drive. For physical devices, "-1" is returned.
  3259. *
  3260. * The logical drive number is displayed in following format:
  3261. *
  3262. * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
  3263. *
  3264. * <int> <int> <int> <int>
  3265. */
  3266. static ssize_t
  3267. megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
  3268. {
  3269. struct scsi_device *sdev = to_scsi_device(dev);
  3270. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
  3271. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3272. int scsi_id = -1;
  3273. int logical_drv = -1;
  3274. int ldid_map = -1;
  3275. uint32_t app_hndl = 0;
  3276. int mapped_sdev_id;
  3277. int rval;
  3278. int i;
  3279. if (raid_dev->random_del_supported &&
  3280. MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
  3281. rval = megaraid_sysfs_get_ldmap(adapter);
  3282. if (rval == 0) {
  3283. for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
  3284. mapped_sdev_id = sdev->id;
  3285. if (sdev->id > adapter->init_id) {
  3286. mapped_sdev_id -= 1;
  3287. }
  3288. if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
  3289. scsi_id = sdev->id;
  3290. logical_drv = i;
  3291. ldid_map = raid_dev->curr_ldmap[i];
  3292. app_hndl = mraid_mm_adapter_app_handle(
  3293. adapter->unique_id);
  3294. break;
  3295. }
  3296. }
  3297. }
  3298. else {
  3299. con_log(CL_ANN, (KERN_NOTICE
  3300. "megaraid: sysfs get ld map failed: %x\n",
  3301. rval));
  3302. }
  3303. }
  3304. return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
  3305. ldid_map, app_hndl);
  3306. }
  3307. /*
  3308. * END: Mailbox Low Level Driver
  3309. */
  3310. module_init(megaraid_init);
  3311. module_exit(megaraid_exit);
  3312. /* vim: set ts=8 sw=8 tw=78 ai si: */