hpsa.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880
  1. /*
  2. * Disk Array driver for HP Smart Array SAS controllers
  3. * Copyright 2000, 2009 Hewlett-Packard Development Company, L.P.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  12. * NON INFRINGEMENT. See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Questions/Comments/Bugfixes to iss_storagedev@hp.com
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/types.h>
  24. #include <linux/pci.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/delay.h>
  28. #include <linux/fs.h>
  29. #include <linux/timer.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/init.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/compat.h>
  35. #include <linux/blktrace_api.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/io.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/completion.h>
  40. #include <linux/moduleparam.h>
  41. #include <scsi/scsi.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_device.h>
  44. #include <scsi/scsi_host.h>
  45. #include <linux/cciss_ioctl.h>
  46. #include <linux/string.h>
  47. #include <linux/bitmap.h>
  48. #include <asm/atomic.h>
  49. #include <linux/kthread.h>
  50. #include "hpsa_cmd.h"
  51. #include "hpsa.h"
  52. /* HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.' */
  53. #define HPSA_DRIVER_VERSION "2.0.1-3"
  54. #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
  55. /* How long to wait (in milliseconds) for board to go into simple mode */
  56. #define MAX_CONFIG_WAIT 30000
  57. #define MAX_IOCTL_CONFIG_WAIT 1000
  58. /*define how many times we will try a command because of bus resets */
  59. #define MAX_CMD_RETRIES 3
  60. /* Embedded module documentation macros - see modules.h */
  61. MODULE_AUTHOR("Hewlett-Packard Company");
  62. MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
  63. HPSA_DRIVER_VERSION);
  64. MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
  65. MODULE_VERSION(HPSA_DRIVER_VERSION);
  66. MODULE_LICENSE("GPL");
  67. static int hpsa_allow_any;
  68. module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
  69. MODULE_PARM_DESC(hpsa_allow_any,
  70. "Allow hpsa driver to access unknown HP Smart Array hardware");
  71. /* define the PCI info for the cards we can control */
  72. static const struct pci_device_id hpsa_pci_device_id[] = {
  73. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
  74. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
  75. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
  76. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
  77. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
  78. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324a},
  79. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324b},
  80. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
  81. #define PCI_DEVICE_ID_HP_CISSF 0x333f
  82. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x333F},
  83. {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  84. PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
  85. {0,}
  86. };
  87. MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
  88. /* board_id = Subsystem Device ID & Vendor ID
  89. * product = Marketing Name for the board
  90. * access = Address of the struct of function pointers
  91. */
  92. static struct board_type products[] = {
  93. {0x3241103C, "Smart Array P212", &SA5_access},
  94. {0x3243103C, "Smart Array P410", &SA5_access},
  95. {0x3245103C, "Smart Array P410i", &SA5_access},
  96. {0x3247103C, "Smart Array P411", &SA5_access},
  97. {0x3249103C, "Smart Array P812", &SA5_access},
  98. {0x324a103C, "Smart Array P712m", &SA5_access},
  99. {0x324b103C, "Smart Array P711m", &SA5_access},
  100. {0x3233103C, "StorageWorks P1210m", &SA5_access},
  101. {0x333F103C, "StorageWorks P1210m", &SA5_access},
  102. {0xFFFF103C, "Unknown Smart Array", &SA5_access},
  103. };
  104. static int number_of_controllers;
  105. static irqreturn_t do_hpsa_intr(int irq, void *dev_id);
  106. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg);
  107. static void start_io(struct ctlr_info *h);
  108. #ifdef CONFIG_COMPAT
  109. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg);
  110. #endif
  111. static void cmd_free(struct ctlr_info *h, struct CommandList *c);
  112. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c);
  113. static struct CommandList *cmd_alloc(struct ctlr_info *h);
  114. static struct CommandList *cmd_special_alloc(struct ctlr_info *h);
  115. static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  116. void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
  117. int cmd_type);
  118. static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
  119. void (*done)(struct scsi_cmnd *));
  120. static void hpsa_scan_start(struct Scsi_Host *);
  121. static int hpsa_scan_finished(struct Scsi_Host *sh,
  122. unsigned long elapsed_time);
  123. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
  124. static int hpsa_slave_alloc(struct scsi_device *sdev);
  125. static void hpsa_slave_destroy(struct scsi_device *sdev);
  126. static ssize_t raid_level_show(struct device *dev,
  127. struct device_attribute *attr, char *buf);
  128. static ssize_t lunid_show(struct device *dev,
  129. struct device_attribute *attr, char *buf);
  130. static ssize_t unique_id_show(struct device *dev,
  131. struct device_attribute *attr, char *buf);
  132. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno);
  133. static ssize_t host_store_rescan(struct device *dev,
  134. struct device_attribute *attr, const char *buf, size_t count);
  135. static int check_for_unit_attention(struct ctlr_info *h,
  136. struct CommandList *c);
  137. static void check_ioctl_unit_attention(struct ctlr_info *h,
  138. struct CommandList *c);
  139. /* performant mode helper functions */
  140. static void calc_bucket_map(int *bucket, int num_buckets,
  141. int nsgs, int *bucket_map);
  142. static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
  143. static inline u32 next_command(struct ctlr_info *h);
  144. static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
  145. static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
  146. static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
  147. static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
  148. static struct device_attribute *hpsa_sdev_attrs[] = {
  149. &dev_attr_raid_level,
  150. &dev_attr_lunid,
  151. &dev_attr_unique_id,
  152. NULL,
  153. };
  154. static struct device_attribute *hpsa_shost_attrs[] = {
  155. &dev_attr_rescan,
  156. NULL,
  157. };
  158. static struct scsi_host_template hpsa_driver_template = {
  159. .module = THIS_MODULE,
  160. .name = "hpsa",
  161. .proc_name = "hpsa",
  162. .queuecommand = hpsa_scsi_queue_command,
  163. .scan_start = hpsa_scan_start,
  164. .scan_finished = hpsa_scan_finished,
  165. .this_id = -1,
  166. .sg_tablesize = MAXSGENTRIES,
  167. .use_clustering = ENABLE_CLUSTERING,
  168. .eh_device_reset_handler = hpsa_eh_device_reset_handler,
  169. .ioctl = hpsa_ioctl,
  170. .slave_alloc = hpsa_slave_alloc,
  171. .slave_destroy = hpsa_slave_destroy,
  172. #ifdef CONFIG_COMPAT
  173. .compat_ioctl = hpsa_compat_ioctl,
  174. #endif
  175. .sdev_attrs = hpsa_sdev_attrs,
  176. .shost_attrs = hpsa_shost_attrs,
  177. };
  178. static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
  179. {
  180. unsigned long *priv = shost_priv(sdev->host);
  181. return (struct ctlr_info *) *priv;
  182. }
  183. static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
  184. {
  185. unsigned long *priv = shost_priv(sh);
  186. return (struct ctlr_info *) *priv;
  187. }
  188. static struct task_struct *hpsa_scan_thread;
  189. static DEFINE_MUTEX(hpsa_scan_mutex);
  190. static LIST_HEAD(hpsa_scan_q);
  191. static int hpsa_scan_func(void *data);
  192. /**
  193. * add_to_scan_list() - add controller to rescan queue
  194. * @h: Pointer to the controller.
  195. *
  196. * Adds the controller to the rescan queue if not already on the queue.
  197. *
  198. * returns 1 if added to the queue, 0 if skipped (could be on the
  199. * queue already, or the controller could be initializing or shutting
  200. * down).
  201. **/
  202. static int add_to_scan_list(struct ctlr_info *h)
  203. {
  204. struct ctlr_info *test_h;
  205. int found = 0;
  206. int ret = 0;
  207. if (h->busy_initializing)
  208. return 0;
  209. /*
  210. * If we don't get the lock, it means the driver is unloading
  211. * and there's no point in scheduling a new scan.
  212. */
  213. if (!mutex_trylock(&h->busy_shutting_down))
  214. return 0;
  215. mutex_lock(&hpsa_scan_mutex);
  216. list_for_each_entry(test_h, &hpsa_scan_q, scan_list) {
  217. if (test_h == h) {
  218. found = 1;
  219. break;
  220. }
  221. }
  222. if (!found && !h->busy_scanning) {
  223. INIT_COMPLETION(h->scan_wait);
  224. list_add_tail(&h->scan_list, &hpsa_scan_q);
  225. ret = 1;
  226. }
  227. mutex_unlock(&hpsa_scan_mutex);
  228. mutex_unlock(&h->busy_shutting_down);
  229. return ret;
  230. }
  231. /**
  232. * remove_from_scan_list() - remove controller from rescan queue
  233. * @h: Pointer to the controller.
  234. *
  235. * Removes the controller from the rescan queue if present. Blocks if
  236. * the controller is currently conducting a rescan. The controller
  237. * can be in one of three states:
  238. * 1. Doesn't need a scan
  239. * 2. On the scan list, but not scanning yet (we remove it)
  240. * 3. Busy scanning (and not on the list). In this case we want to wait for
  241. * the scan to complete to make sure the scanning thread for this
  242. * controller is completely idle.
  243. **/
  244. static void remove_from_scan_list(struct ctlr_info *h)
  245. {
  246. struct ctlr_info *test_h, *tmp_h;
  247. mutex_lock(&hpsa_scan_mutex);
  248. list_for_each_entry_safe(test_h, tmp_h, &hpsa_scan_q, scan_list) {
  249. if (test_h == h) { /* state 2. */
  250. list_del(&h->scan_list);
  251. complete_all(&h->scan_wait);
  252. mutex_unlock(&hpsa_scan_mutex);
  253. return;
  254. }
  255. }
  256. if (h->busy_scanning) { /* state 3. */
  257. mutex_unlock(&hpsa_scan_mutex);
  258. wait_for_completion(&h->scan_wait);
  259. } else { /* state 1, nothing to do. */
  260. mutex_unlock(&hpsa_scan_mutex);
  261. }
  262. }
  263. /* hpsa_scan_func() - kernel thread used to rescan controllers
  264. * @data: Ignored.
  265. *
  266. * A kernel thread used scan for drive topology changes on
  267. * controllers. The thread processes only one controller at a time
  268. * using a queue. Controllers are added to the queue using
  269. * add_to_scan_list() and removed from the queue either after done
  270. * processing or using remove_from_scan_list().
  271. *
  272. * returns 0.
  273. **/
  274. static int hpsa_scan_func(__attribute__((unused)) void *data)
  275. {
  276. struct ctlr_info *h;
  277. int host_no;
  278. while (1) {
  279. set_current_state(TASK_INTERRUPTIBLE);
  280. schedule();
  281. if (kthread_should_stop())
  282. break;
  283. while (1) {
  284. mutex_lock(&hpsa_scan_mutex);
  285. if (list_empty(&hpsa_scan_q)) {
  286. mutex_unlock(&hpsa_scan_mutex);
  287. break;
  288. }
  289. h = list_entry(hpsa_scan_q.next, struct ctlr_info,
  290. scan_list);
  291. list_del(&h->scan_list);
  292. h->busy_scanning = 1;
  293. mutex_unlock(&hpsa_scan_mutex);
  294. host_no = h->scsi_host ? h->scsi_host->host_no : -1;
  295. hpsa_scan_start(h->scsi_host);
  296. complete_all(&h->scan_wait);
  297. mutex_lock(&hpsa_scan_mutex);
  298. h->busy_scanning = 0;
  299. mutex_unlock(&hpsa_scan_mutex);
  300. }
  301. }
  302. return 0;
  303. }
  304. static int check_for_unit_attention(struct ctlr_info *h,
  305. struct CommandList *c)
  306. {
  307. if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
  308. return 0;
  309. switch (c->err_info->SenseInfo[12]) {
  310. case STATE_CHANGED:
  311. dev_warn(&h->pdev->dev, "hpsa%d: a state change "
  312. "detected, command retried\n", h->ctlr);
  313. break;
  314. case LUN_FAILED:
  315. dev_warn(&h->pdev->dev, "hpsa%d: LUN failure "
  316. "detected, action required\n", h->ctlr);
  317. break;
  318. case REPORT_LUNS_CHANGED:
  319. dev_warn(&h->pdev->dev, "hpsa%d: report LUN data "
  320. "changed\n", h->ctlr);
  321. /*
  322. * Here, we could call add_to_scan_list and wake up the scan thread,
  323. * except that it's quite likely that we will get more than one
  324. * REPORT_LUNS_CHANGED condition in quick succession, which means
  325. * that those which occur after the first one will likely happen
  326. * *during* the hpsa_scan_thread's rescan. And the rescan code is not
  327. * robust enough to restart in the middle, undoing what it has already
  328. * done, and it's not clear that it's even possible to do this, since
  329. * part of what it does is notify the SCSI mid layer, which starts
  330. * doing it's own i/o to read partition tables and so on, and the
  331. * driver doesn't have visibility to know what might need undoing.
  332. * In any event, if possible, it is horribly complicated to get right
  333. * so we just don't do it for now.
  334. *
  335. * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
  336. */
  337. break;
  338. case POWER_OR_RESET:
  339. dev_warn(&h->pdev->dev, "hpsa%d: a power on "
  340. "or device reset detected\n", h->ctlr);
  341. break;
  342. case UNIT_ATTENTION_CLEARED:
  343. dev_warn(&h->pdev->dev, "hpsa%d: unit attention "
  344. "cleared by another initiator\n", h->ctlr);
  345. break;
  346. default:
  347. dev_warn(&h->pdev->dev, "hpsa%d: unknown "
  348. "unit attention detected\n", h->ctlr);
  349. break;
  350. }
  351. return 1;
  352. }
  353. static ssize_t host_store_rescan(struct device *dev,
  354. struct device_attribute *attr,
  355. const char *buf, size_t count)
  356. {
  357. struct ctlr_info *h;
  358. struct Scsi_Host *shost = class_to_shost(dev);
  359. h = shost_to_hba(shost);
  360. if (add_to_scan_list(h)) {
  361. wake_up_process(hpsa_scan_thread);
  362. wait_for_completion_interruptible(&h->scan_wait);
  363. }
  364. return count;
  365. }
  366. /* Enqueuing and dequeuing functions for cmdlists. */
  367. static inline void addQ(struct hlist_head *list, struct CommandList *c)
  368. {
  369. hlist_add_head(&c->list, list);
  370. }
  371. static inline u32 next_command(struct ctlr_info *h)
  372. {
  373. u32 a;
  374. if (unlikely(h->transMethod != CFGTBL_Trans_Performant))
  375. return h->access.command_completed(h);
  376. if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
  377. a = *(h->reply_pool_head); /* Next cmd in ring buffer */
  378. (h->reply_pool_head)++;
  379. h->commands_outstanding--;
  380. } else {
  381. a = FIFO_EMPTY;
  382. }
  383. /* Check for wraparound */
  384. if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
  385. h->reply_pool_head = h->reply_pool;
  386. h->reply_pool_wraparound ^= 1;
  387. }
  388. return a;
  389. }
  390. /* set_performant_mode: Modify the tag for cciss performant
  391. * set bit 0 for pull model, bits 3-1 for block fetch
  392. * register number
  393. */
  394. static void set_performant_mode(struct ctlr_info *h, struct CommandList *c)
  395. {
  396. if (likely(h->transMethod == CFGTBL_Trans_Performant))
  397. c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
  398. }
  399. static void enqueue_cmd_and_start_io(struct ctlr_info *h,
  400. struct CommandList *c)
  401. {
  402. unsigned long flags;
  403. set_performant_mode(h, c);
  404. spin_lock_irqsave(&h->lock, flags);
  405. addQ(&h->reqQ, c);
  406. h->Qdepth++;
  407. start_io(h);
  408. spin_unlock_irqrestore(&h->lock, flags);
  409. }
  410. static inline void removeQ(struct CommandList *c)
  411. {
  412. if (WARN_ON(hlist_unhashed(&c->list)))
  413. return;
  414. hlist_del_init(&c->list);
  415. }
  416. static inline int is_hba_lunid(unsigned char scsi3addr[])
  417. {
  418. return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
  419. }
  420. static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
  421. {
  422. return (scsi3addr[3] & 0xC0) == 0x40;
  423. }
  424. static inline int is_scsi_rev_5(struct ctlr_info *h)
  425. {
  426. if (!h->hba_inquiry_data)
  427. return 0;
  428. if ((h->hba_inquiry_data[2] & 0x07) == 5)
  429. return 1;
  430. return 0;
  431. }
  432. static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
  433. "UNKNOWN"
  434. };
  435. #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 1)
  436. static ssize_t raid_level_show(struct device *dev,
  437. struct device_attribute *attr, char *buf)
  438. {
  439. ssize_t l = 0;
  440. unsigned char rlevel;
  441. struct ctlr_info *h;
  442. struct scsi_device *sdev;
  443. struct hpsa_scsi_dev_t *hdev;
  444. unsigned long flags;
  445. sdev = to_scsi_device(dev);
  446. h = sdev_to_hba(sdev);
  447. spin_lock_irqsave(&h->lock, flags);
  448. hdev = sdev->hostdata;
  449. if (!hdev) {
  450. spin_unlock_irqrestore(&h->lock, flags);
  451. return -ENODEV;
  452. }
  453. /* Is this even a logical drive? */
  454. if (!is_logical_dev_addr_mode(hdev->scsi3addr)) {
  455. spin_unlock_irqrestore(&h->lock, flags);
  456. l = snprintf(buf, PAGE_SIZE, "N/A\n");
  457. return l;
  458. }
  459. rlevel = hdev->raid_level;
  460. spin_unlock_irqrestore(&h->lock, flags);
  461. if (rlevel > RAID_UNKNOWN)
  462. rlevel = RAID_UNKNOWN;
  463. l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
  464. return l;
  465. }
  466. static ssize_t lunid_show(struct device *dev,
  467. struct device_attribute *attr, char *buf)
  468. {
  469. struct ctlr_info *h;
  470. struct scsi_device *sdev;
  471. struct hpsa_scsi_dev_t *hdev;
  472. unsigned long flags;
  473. unsigned char lunid[8];
  474. sdev = to_scsi_device(dev);
  475. h = sdev_to_hba(sdev);
  476. spin_lock_irqsave(&h->lock, flags);
  477. hdev = sdev->hostdata;
  478. if (!hdev) {
  479. spin_unlock_irqrestore(&h->lock, flags);
  480. return -ENODEV;
  481. }
  482. memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
  483. spin_unlock_irqrestore(&h->lock, flags);
  484. return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  485. lunid[0], lunid[1], lunid[2], lunid[3],
  486. lunid[4], lunid[5], lunid[6], lunid[7]);
  487. }
  488. static ssize_t unique_id_show(struct device *dev,
  489. struct device_attribute *attr, char *buf)
  490. {
  491. struct ctlr_info *h;
  492. struct scsi_device *sdev;
  493. struct hpsa_scsi_dev_t *hdev;
  494. unsigned long flags;
  495. unsigned char sn[16];
  496. sdev = to_scsi_device(dev);
  497. h = sdev_to_hba(sdev);
  498. spin_lock_irqsave(&h->lock, flags);
  499. hdev = sdev->hostdata;
  500. if (!hdev) {
  501. spin_unlock_irqrestore(&h->lock, flags);
  502. return -ENODEV;
  503. }
  504. memcpy(sn, hdev->device_id, sizeof(sn));
  505. spin_unlock_irqrestore(&h->lock, flags);
  506. return snprintf(buf, 16 * 2 + 2,
  507. "%02X%02X%02X%02X%02X%02X%02X%02X"
  508. "%02X%02X%02X%02X%02X%02X%02X%02X\n",
  509. sn[0], sn[1], sn[2], sn[3],
  510. sn[4], sn[5], sn[6], sn[7],
  511. sn[8], sn[9], sn[10], sn[11],
  512. sn[12], sn[13], sn[14], sn[15]);
  513. }
  514. static int hpsa_find_target_lun(struct ctlr_info *h,
  515. unsigned char scsi3addr[], int bus, int *target, int *lun)
  516. {
  517. /* finds an unused bus, target, lun for a new physical device
  518. * assumes h->devlock is held
  519. */
  520. int i, found = 0;
  521. DECLARE_BITMAP(lun_taken, HPSA_MAX_SCSI_DEVS_PER_HBA);
  522. memset(&lun_taken[0], 0, HPSA_MAX_SCSI_DEVS_PER_HBA >> 3);
  523. for (i = 0; i < h->ndevices; i++) {
  524. if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
  525. set_bit(h->dev[i]->target, lun_taken);
  526. }
  527. for (i = 0; i < HPSA_MAX_SCSI_DEVS_PER_HBA; i++) {
  528. if (!test_bit(i, lun_taken)) {
  529. /* *bus = 1; */
  530. *target = i;
  531. *lun = 0;
  532. found = 1;
  533. break;
  534. }
  535. }
  536. return !found;
  537. }
  538. /* Add an entry into h->dev[] array. */
  539. static int hpsa_scsi_add_entry(struct ctlr_info *h, int hostno,
  540. struct hpsa_scsi_dev_t *device,
  541. struct hpsa_scsi_dev_t *added[], int *nadded)
  542. {
  543. /* assumes h->devlock is held */
  544. int n = h->ndevices;
  545. int i;
  546. unsigned char addr1[8], addr2[8];
  547. struct hpsa_scsi_dev_t *sd;
  548. if (n >= HPSA_MAX_SCSI_DEVS_PER_HBA) {
  549. dev_err(&h->pdev->dev, "too many devices, some will be "
  550. "inaccessible.\n");
  551. return -1;
  552. }
  553. /* physical devices do not have lun or target assigned until now. */
  554. if (device->lun != -1)
  555. /* Logical device, lun is already assigned. */
  556. goto lun_assigned;
  557. /* If this device a non-zero lun of a multi-lun device
  558. * byte 4 of the 8-byte LUN addr will contain the logical
  559. * unit no, zero otherise.
  560. */
  561. if (device->scsi3addr[4] == 0) {
  562. /* This is not a non-zero lun of a multi-lun device */
  563. if (hpsa_find_target_lun(h, device->scsi3addr,
  564. device->bus, &device->target, &device->lun) != 0)
  565. return -1;
  566. goto lun_assigned;
  567. }
  568. /* This is a non-zero lun of a multi-lun device.
  569. * Search through our list and find the device which
  570. * has the same 8 byte LUN address, excepting byte 4.
  571. * Assign the same bus and target for this new LUN.
  572. * Use the logical unit number from the firmware.
  573. */
  574. memcpy(addr1, device->scsi3addr, 8);
  575. addr1[4] = 0;
  576. for (i = 0; i < n; i++) {
  577. sd = h->dev[i];
  578. memcpy(addr2, sd->scsi3addr, 8);
  579. addr2[4] = 0;
  580. /* differ only in byte 4? */
  581. if (memcmp(addr1, addr2, 8) == 0) {
  582. device->bus = sd->bus;
  583. device->target = sd->target;
  584. device->lun = device->scsi3addr[4];
  585. break;
  586. }
  587. }
  588. if (device->lun == -1) {
  589. dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
  590. " suspect firmware bug or unsupported hardware "
  591. "configuration.\n");
  592. return -1;
  593. }
  594. lun_assigned:
  595. h->dev[n] = device;
  596. h->ndevices++;
  597. added[*nadded] = device;
  598. (*nadded)++;
  599. /* initially, (before registering with scsi layer) we don't
  600. * know our hostno and we don't want to print anything first
  601. * time anyway (the scsi layer's inquiries will show that info)
  602. */
  603. /* if (hostno != -1) */
  604. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d added.\n",
  605. scsi_device_type(device->devtype), hostno,
  606. device->bus, device->target, device->lun);
  607. return 0;
  608. }
  609. /* Replace an entry from h->dev[] array. */
  610. static void hpsa_scsi_replace_entry(struct ctlr_info *h, int hostno,
  611. int entry, struct hpsa_scsi_dev_t *new_entry,
  612. struct hpsa_scsi_dev_t *added[], int *nadded,
  613. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  614. {
  615. /* assumes h->devlock is held */
  616. BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
  617. removed[*nremoved] = h->dev[entry];
  618. (*nremoved)++;
  619. h->dev[entry] = new_entry;
  620. added[*nadded] = new_entry;
  621. (*nadded)++;
  622. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d changed.\n",
  623. scsi_device_type(new_entry->devtype), hostno, new_entry->bus,
  624. new_entry->target, new_entry->lun);
  625. }
  626. /* Remove an entry from h->dev[] array. */
  627. static void hpsa_scsi_remove_entry(struct ctlr_info *h, int hostno, int entry,
  628. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  629. {
  630. /* assumes h->devlock is held */
  631. int i;
  632. struct hpsa_scsi_dev_t *sd;
  633. BUG_ON(entry < 0 || entry >= HPSA_MAX_SCSI_DEVS_PER_HBA);
  634. sd = h->dev[entry];
  635. removed[*nremoved] = h->dev[entry];
  636. (*nremoved)++;
  637. for (i = entry; i < h->ndevices-1; i++)
  638. h->dev[i] = h->dev[i+1];
  639. h->ndevices--;
  640. dev_info(&h->pdev->dev, "%s device c%db%dt%dl%d removed.\n",
  641. scsi_device_type(sd->devtype), hostno, sd->bus, sd->target,
  642. sd->lun);
  643. }
  644. #define SCSI3ADDR_EQ(a, b) ( \
  645. (a)[7] == (b)[7] && \
  646. (a)[6] == (b)[6] && \
  647. (a)[5] == (b)[5] && \
  648. (a)[4] == (b)[4] && \
  649. (a)[3] == (b)[3] && \
  650. (a)[2] == (b)[2] && \
  651. (a)[1] == (b)[1] && \
  652. (a)[0] == (b)[0])
  653. static void fixup_botched_add(struct ctlr_info *h,
  654. struct hpsa_scsi_dev_t *added)
  655. {
  656. /* called when scsi_add_device fails in order to re-adjust
  657. * h->dev[] to match the mid layer's view.
  658. */
  659. unsigned long flags;
  660. int i, j;
  661. spin_lock_irqsave(&h->lock, flags);
  662. for (i = 0; i < h->ndevices; i++) {
  663. if (h->dev[i] == added) {
  664. for (j = i; j < h->ndevices-1; j++)
  665. h->dev[j] = h->dev[j+1];
  666. h->ndevices--;
  667. break;
  668. }
  669. }
  670. spin_unlock_irqrestore(&h->lock, flags);
  671. kfree(added);
  672. }
  673. static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
  674. struct hpsa_scsi_dev_t *dev2)
  675. {
  676. if ((is_logical_dev_addr_mode(dev1->scsi3addr) ||
  677. (dev1->lun != -1 && dev2->lun != -1)) &&
  678. dev1->devtype != 0x0C)
  679. return (memcmp(dev1, dev2, sizeof(*dev1)) == 0);
  680. /* we compare everything except lun and target as these
  681. * are not yet assigned. Compare parts likely
  682. * to differ first
  683. */
  684. if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
  685. sizeof(dev1->scsi3addr)) != 0)
  686. return 0;
  687. if (memcmp(dev1->device_id, dev2->device_id,
  688. sizeof(dev1->device_id)) != 0)
  689. return 0;
  690. if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
  691. return 0;
  692. if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
  693. return 0;
  694. if (memcmp(dev1->revision, dev2->revision, sizeof(dev1->revision)) != 0)
  695. return 0;
  696. if (dev1->devtype != dev2->devtype)
  697. return 0;
  698. if (dev1->raid_level != dev2->raid_level)
  699. return 0;
  700. if (dev1->bus != dev2->bus)
  701. return 0;
  702. return 1;
  703. }
  704. /* Find needle in haystack. If exact match found, return DEVICE_SAME,
  705. * and return needle location in *index. If scsi3addr matches, but not
  706. * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
  707. * location in *index. If needle not found, return DEVICE_NOT_FOUND.
  708. */
  709. static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
  710. struct hpsa_scsi_dev_t *haystack[], int haystack_size,
  711. int *index)
  712. {
  713. int i;
  714. #define DEVICE_NOT_FOUND 0
  715. #define DEVICE_CHANGED 1
  716. #define DEVICE_SAME 2
  717. for (i = 0; i < haystack_size; i++) {
  718. if (haystack[i] == NULL) /* previously removed. */
  719. continue;
  720. if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
  721. *index = i;
  722. if (device_is_the_same(needle, haystack[i]))
  723. return DEVICE_SAME;
  724. else
  725. return DEVICE_CHANGED;
  726. }
  727. }
  728. *index = -1;
  729. return DEVICE_NOT_FOUND;
  730. }
  731. static void adjust_hpsa_scsi_table(struct ctlr_info *h, int hostno,
  732. struct hpsa_scsi_dev_t *sd[], int nsds)
  733. {
  734. /* sd contains scsi3 addresses and devtypes, and inquiry
  735. * data. This function takes what's in sd to be the current
  736. * reality and updates h->dev[] to reflect that reality.
  737. */
  738. int i, entry, device_change, changes = 0;
  739. struct hpsa_scsi_dev_t *csd;
  740. unsigned long flags;
  741. struct hpsa_scsi_dev_t **added, **removed;
  742. int nadded, nremoved;
  743. struct Scsi_Host *sh = NULL;
  744. added = kzalloc(sizeof(*added) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  745. GFP_KERNEL);
  746. removed = kzalloc(sizeof(*removed) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  747. GFP_KERNEL);
  748. if (!added || !removed) {
  749. dev_warn(&h->pdev->dev, "out of memory in "
  750. "adjust_hpsa_scsi_table\n");
  751. goto free_and_out;
  752. }
  753. spin_lock_irqsave(&h->devlock, flags);
  754. /* find any devices in h->dev[] that are not in
  755. * sd[] and remove them from h->dev[], and for any
  756. * devices which have changed, remove the old device
  757. * info and add the new device info.
  758. */
  759. i = 0;
  760. nremoved = 0;
  761. nadded = 0;
  762. while (i < h->ndevices) {
  763. csd = h->dev[i];
  764. device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
  765. if (device_change == DEVICE_NOT_FOUND) {
  766. changes++;
  767. hpsa_scsi_remove_entry(h, hostno, i,
  768. removed, &nremoved);
  769. continue; /* remove ^^^, hence i not incremented */
  770. } else if (device_change == DEVICE_CHANGED) {
  771. changes++;
  772. hpsa_scsi_replace_entry(h, hostno, i, sd[entry],
  773. added, &nadded, removed, &nremoved);
  774. /* Set it to NULL to prevent it from being freed
  775. * at the bottom of hpsa_update_scsi_devices()
  776. */
  777. sd[entry] = NULL;
  778. }
  779. i++;
  780. }
  781. /* Now, make sure every device listed in sd[] is also
  782. * listed in h->dev[], adding them if they aren't found
  783. */
  784. for (i = 0; i < nsds; i++) {
  785. if (!sd[i]) /* if already added above. */
  786. continue;
  787. device_change = hpsa_scsi_find_entry(sd[i], h->dev,
  788. h->ndevices, &entry);
  789. if (device_change == DEVICE_NOT_FOUND) {
  790. changes++;
  791. if (hpsa_scsi_add_entry(h, hostno, sd[i],
  792. added, &nadded) != 0)
  793. break;
  794. sd[i] = NULL; /* prevent from being freed later. */
  795. } else if (device_change == DEVICE_CHANGED) {
  796. /* should never happen... */
  797. changes++;
  798. dev_warn(&h->pdev->dev,
  799. "device unexpectedly changed.\n");
  800. /* but if it does happen, we just ignore that device */
  801. }
  802. }
  803. spin_unlock_irqrestore(&h->devlock, flags);
  804. /* Don't notify scsi mid layer of any changes the first time through
  805. * (or if there are no changes) scsi_scan_host will do it later the
  806. * first time through.
  807. */
  808. if (hostno == -1 || !changes)
  809. goto free_and_out;
  810. sh = h->scsi_host;
  811. /* Notify scsi mid layer of any removed devices */
  812. for (i = 0; i < nremoved; i++) {
  813. struct scsi_device *sdev =
  814. scsi_device_lookup(sh, removed[i]->bus,
  815. removed[i]->target, removed[i]->lun);
  816. if (sdev != NULL) {
  817. scsi_remove_device(sdev);
  818. scsi_device_put(sdev);
  819. } else {
  820. /* We don't expect to get here.
  821. * future cmds to this device will get selection
  822. * timeout as if the device was gone.
  823. */
  824. dev_warn(&h->pdev->dev, "didn't find c%db%dt%dl%d "
  825. " for removal.", hostno, removed[i]->bus,
  826. removed[i]->target, removed[i]->lun);
  827. }
  828. kfree(removed[i]);
  829. removed[i] = NULL;
  830. }
  831. /* Notify scsi mid layer of any added devices */
  832. for (i = 0; i < nadded; i++) {
  833. if (scsi_add_device(sh, added[i]->bus,
  834. added[i]->target, added[i]->lun) == 0)
  835. continue;
  836. dev_warn(&h->pdev->dev, "scsi_add_device c%db%dt%dl%d failed, "
  837. "device not added.\n", hostno, added[i]->bus,
  838. added[i]->target, added[i]->lun);
  839. /* now we have to remove it from h->dev,
  840. * since it didn't get added to scsi mid layer
  841. */
  842. fixup_botched_add(h, added[i]);
  843. }
  844. free_and_out:
  845. kfree(added);
  846. kfree(removed);
  847. }
  848. /*
  849. * Lookup bus/target/lun and retrun corresponding struct hpsa_scsi_dev_t *
  850. * Assume's h->devlock is held.
  851. */
  852. static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
  853. int bus, int target, int lun)
  854. {
  855. int i;
  856. struct hpsa_scsi_dev_t *sd;
  857. for (i = 0; i < h->ndevices; i++) {
  858. sd = h->dev[i];
  859. if (sd->bus == bus && sd->target == target && sd->lun == lun)
  860. return sd;
  861. }
  862. return NULL;
  863. }
  864. /* link sdev->hostdata to our per-device structure. */
  865. static int hpsa_slave_alloc(struct scsi_device *sdev)
  866. {
  867. struct hpsa_scsi_dev_t *sd;
  868. unsigned long flags;
  869. struct ctlr_info *h;
  870. h = sdev_to_hba(sdev);
  871. spin_lock_irqsave(&h->devlock, flags);
  872. sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
  873. sdev_id(sdev), sdev->lun);
  874. if (sd != NULL)
  875. sdev->hostdata = sd;
  876. spin_unlock_irqrestore(&h->devlock, flags);
  877. return 0;
  878. }
  879. static void hpsa_slave_destroy(struct scsi_device *sdev)
  880. {
  881. /* nothing to do. */
  882. }
  883. static void hpsa_scsi_setup(struct ctlr_info *h)
  884. {
  885. h->ndevices = 0;
  886. h->scsi_host = NULL;
  887. spin_lock_init(&h->devlock);
  888. }
  889. static void complete_scsi_command(struct CommandList *cp,
  890. int timeout, u32 tag)
  891. {
  892. struct scsi_cmnd *cmd;
  893. struct ctlr_info *h;
  894. struct ErrorInfo *ei;
  895. unsigned char sense_key;
  896. unsigned char asc; /* additional sense code */
  897. unsigned char ascq; /* additional sense code qualifier */
  898. ei = cp->err_info;
  899. cmd = (struct scsi_cmnd *) cp->scsi_cmd;
  900. h = cp->h;
  901. scsi_dma_unmap(cmd); /* undo the DMA mappings */
  902. cmd->result = (DID_OK << 16); /* host byte */
  903. cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
  904. cmd->result |= (ei->ScsiStatus << 1);
  905. /* copy the sense data whether we need to or not. */
  906. memcpy(cmd->sense_buffer, ei->SenseInfo,
  907. ei->SenseLen > SCSI_SENSE_BUFFERSIZE ?
  908. SCSI_SENSE_BUFFERSIZE :
  909. ei->SenseLen);
  910. scsi_set_resid(cmd, ei->ResidualCnt);
  911. if (ei->CommandStatus == 0) {
  912. cmd->scsi_done(cmd);
  913. cmd_free(h, cp);
  914. return;
  915. }
  916. /* an error has occurred */
  917. switch (ei->CommandStatus) {
  918. case CMD_TARGET_STATUS:
  919. if (ei->ScsiStatus) {
  920. /* Get sense key */
  921. sense_key = 0xf & ei->SenseInfo[2];
  922. /* Get additional sense code */
  923. asc = ei->SenseInfo[12];
  924. /* Get addition sense code qualifier */
  925. ascq = ei->SenseInfo[13];
  926. }
  927. if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
  928. if (check_for_unit_attention(h, cp)) {
  929. cmd->result = DID_SOFT_ERROR << 16;
  930. break;
  931. }
  932. if (sense_key == ILLEGAL_REQUEST) {
  933. /*
  934. * SCSI REPORT_LUNS is commonly unsupported on
  935. * Smart Array. Suppress noisy complaint.
  936. */
  937. if (cp->Request.CDB[0] == REPORT_LUNS)
  938. break;
  939. /* If ASC/ASCQ indicate Logical Unit
  940. * Not Supported condition,
  941. */
  942. if ((asc == 0x25) && (ascq == 0x0)) {
  943. dev_warn(&h->pdev->dev, "cp %p "
  944. "has check condition\n", cp);
  945. break;
  946. }
  947. }
  948. if (sense_key == NOT_READY) {
  949. /* If Sense is Not Ready, Logical Unit
  950. * Not ready, Manual Intervention
  951. * required
  952. */
  953. if ((asc == 0x04) && (ascq == 0x03)) {
  954. dev_warn(&h->pdev->dev, "cp %p "
  955. "has check condition: unit "
  956. "not ready, manual "
  957. "intervention required\n", cp);
  958. break;
  959. }
  960. }
  961. if (sense_key == ABORTED_COMMAND) {
  962. /* Aborted command is retryable */
  963. dev_warn(&h->pdev->dev, "cp %p "
  964. "has check condition: aborted command: "
  965. "ASC: 0x%x, ASCQ: 0x%x\n",
  966. cp, asc, ascq);
  967. cmd->result = DID_SOFT_ERROR << 16;
  968. break;
  969. }
  970. /* Must be some other type of check condition */
  971. dev_warn(&h->pdev->dev, "cp %p has check condition: "
  972. "unknown type: "
  973. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  974. "Returning result: 0x%x, "
  975. "cmd=[%02x %02x %02x %02x %02x "
  976. "%02x %02x %02x %02x %02x %02x "
  977. "%02x %02x %02x %02x %02x]\n",
  978. cp, sense_key, asc, ascq,
  979. cmd->result,
  980. cmd->cmnd[0], cmd->cmnd[1],
  981. cmd->cmnd[2], cmd->cmnd[3],
  982. cmd->cmnd[4], cmd->cmnd[5],
  983. cmd->cmnd[6], cmd->cmnd[7],
  984. cmd->cmnd[8], cmd->cmnd[9],
  985. cmd->cmnd[10], cmd->cmnd[11],
  986. cmd->cmnd[12], cmd->cmnd[13],
  987. cmd->cmnd[14], cmd->cmnd[15]);
  988. break;
  989. }
  990. /* Problem was not a check condition
  991. * Pass it up to the upper layers...
  992. */
  993. if (ei->ScsiStatus) {
  994. dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
  995. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  996. "Returning result: 0x%x\n",
  997. cp, ei->ScsiStatus,
  998. sense_key, asc, ascq,
  999. cmd->result);
  1000. } else { /* scsi status is zero??? How??? */
  1001. dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
  1002. "Returning no connection.\n", cp),
  1003. /* Ordinarily, this case should never happen,
  1004. * but there is a bug in some released firmware
  1005. * revisions that allows it to happen if, for
  1006. * example, a 4100 backplane loses power and
  1007. * the tape drive is in it. We assume that
  1008. * it's a fatal error of some kind because we
  1009. * can't show that it wasn't. We will make it
  1010. * look like selection timeout since that is
  1011. * the most common reason for this to occur,
  1012. * and it's severe enough.
  1013. */
  1014. cmd->result = DID_NO_CONNECT << 16;
  1015. }
  1016. break;
  1017. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  1018. break;
  1019. case CMD_DATA_OVERRUN:
  1020. dev_warn(&h->pdev->dev, "cp %p has"
  1021. " completed with data overrun "
  1022. "reported\n", cp);
  1023. break;
  1024. case CMD_INVALID: {
  1025. /* print_bytes(cp, sizeof(*cp), 1, 0);
  1026. print_cmd(cp); */
  1027. /* We get CMD_INVALID if you address a non-existent device
  1028. * instead of a selection timeout (no response). You will
  1029. * see this if you yank out a drive, then try to access it.
  1030. * This is kind of a shame because it means that any other
  1031. * CMD_INVALID (e.g. driver bug) will get interpreted as a
  1032. * missing target. */
  1033. cmd->result = DID_NO_CONNECT << 16;
  1034. }
  1035. break;
  1036. case CMD_PROTOCOL_ERR:
  1037. dev_warn(&h->pdev->dev, "cp %p has "
  1038. "protocol error \n", cp);
  1039. break;
  1040. case CMD_HARDWARE_ERR:
  1041. cmd->result = DID_ERROR << 16;
  1042. dev_warn(&h->pdev->dev, "cp %p had hardware error\n", cp);
  1043. break;
  1044. case CMD_CONNECTION_LOST:
  1045. cmd->result = DID_ERROR << 16;
  1046. dev_warn(&h->pdev->dev, "cp %p had connection lost\n", cp);
  1047. break;
  1048. case CMD_ABORTED:
  1049. cmd->result = DID_ABORT << 16;
  1050. dev_warn(&h->pdev->dev, "cp %p was aborted with status 0x%x\n",
  1051. cp, ei->ScsiStatus);
  1052. break;
  1053. case CMD_ABORT_FAILED:
  1054. cmd->result = DID_ERROR << 16;
  1055. dev_warn(&h->pdev->dev, "cp %p reports abort failed\n", cp);
  1056. break;
  1057. case CMD_UNSOLICITED_ABORT:
  1058. cmd->result = DID_RESET << 16;
  1059. dev_warn(&h->pdev->dev, "cp %p aborted do to an unsolicited "
  1060. "abort\n", cp);
  1061. break;
  1062. case CMD_TIMEOUT:
  1063. cmd->result = DID_TIME_OUT << 16;
  1064. dev_warn(&h->pdev->dev, "cp %p timedout\n", cp);
  1065. break;
  1066. default:
  1067. cmd->result = DID_ERROR << 16;
  1068. dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
  1069. cp, ei->CommandStatus);
  1070. }
  1071. cmd->scsi_done(cmd);
  1072. cmd_free(h, cp);
  1073. }
  1074. static int hpsa_scsi_detect(struct ctlr_info *h)
  1075. {
  1076. struct Scsi_Host *sh;
  1077. int error;
  1078. sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
  1079. if (sh == NULL)
  1080. goto fail;
  1081. sh->io_port = 0;
  1082. sh->n_io_port = 0;
  1083. sh->this_id = -1;
  1084. sh->max_channel = 3;
  1085. sh->max_cmd_len = MAX_COMMAND_SIZE;
  1086. sh->max_lun = HPSA_MAX_LUN;
  1087. sh->max_id = HPSA_MAX_LUN;
  1088. sh->can_queue = h->nr_cmds;
  1089. sh->cmd_per_lun = h->nr_cmds;
  1090. h->scsi_host = sh;
  1091. sh->hostdata[0] = (unsigned long) h;
  1092. sh->irq = h->intr[PERF_MODE_INT];
  1093. sh->unique_id = sh->irq;
  1094. error = scsi_add_host(sh, &h->pdev->dev);
  1095. if (error)
  1096. goto fail_host_put;
  1097. scsi_scan_host(sh);
  1098. return 0;
  1099. fail_host_put:
  1100. dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_add_host"
  1101. " failed for controller %d\n", h->ctlr);
  1102. scsi_host_put(sh);
  1103. return error;
  1104. fail:
  1105. dev_err(&h->pdev->dev, "hpsa_scsi_detect: scsi_host_alloc"
  1106. " failed for controller %d\n", h->ctlr);
  1107. return -ENOMEM;
  1108. }
  1109. static void hpsa_pci_unmap(struct pci_dev *pdev,
  1110. struct CommandList *c, int sg_used, int data_direction)
  1111. {
  1112. int i;
  1113. union u64bit addr64;
  1114. for (i = 0; i < sg_used; i++) {
  1115. addr64.val32.lower = c->SG[i].Addr.lower;
  1116. addr64.val32.upper = c->SG[i].Addr.upper;
  1117. pci_unmap_single(pdev, (dma_addr_t) addr64.val, c->SG[i].Len,
  1118. data_direction);
  1119. }
  1120. }
  1121. static void hpsa_map_one(struct pci_dev *pdev,
  1122. struct CommandList *cp,
  1123. unsigned char *buf,
  1124. size_t buflen,
  1125. int data_direction)
  1126. {
  1127. u64 addr64;
  1128. if (buflen == 0 || data_direction == PCI_DMA_NONE) {
  1129. cp->Header.SGList = 0;
  1130. cp->Header.SGTotal = 0;
  1131. return;
  1132. }
  1133. addr64 = (u64) pci_map_single(pdev, buf, buflen, data_direction);
  1134. cp->SG[0].Addr.lower =
  1135. (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
  1136. cp->SG[0].Addr.upper =
  1137. (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
  1138. cp->SG[0].Len = buflen;
  1139. cp->Header.SGList = (u8) 1; /* no. SGs contig in this cmd */
  1140. cp->Header.SGTotal = (u16) 1; /* total sgs in this cmd list */
  1141. }
  1142. static inline void hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
  1143. struct CommandList *c)
  1144. {
  1145. DECLARE_COMPLETION_ONSTACK(wait);
  1146. c->waiting = &wait;
  1147. enqueue_cmd_and_start_io(h, c);
  1148. wait_for_completion(&wait);
  1149. }
  1150. static void hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
  1151. struct CommandList *c, int data_direction)
  1152. {
  1153. int retry_count = 0;
  1154. do {
  1155. memset(c->err_info, 0, sizeof(c->err_info));
  1156. hpsa_scsi_do_simple_cmd_core(h, c);
  1157. retry_count++;
  1158. } while (check_for_unit_attention(h, c) && retry_count <= 3);
  1159. hpsa_pci_unmap(h->pdev, c, 1, data_direction);
  1160. }
  1161. static void hpsa_scsi_interpret_error(struct CommandList *cp)
  1162. {
  1163. struct ErrorInfo *ei;
  1164. struct device *d = &cp->h->pdev->dev;
  1165. ei = cp->err_info;
  1166. switch (ei->CommandStatus) {
  1167. case CMD_TARGET_STATUS:
  1168. dev_warn(d, "cmd %p has completed with errors\n", cp);
  1169. dev_warn(d, "cmd %p has SCSI Status = %x\n", cp,
  1170. ei->ScsiStatus);
  1171. if (ei->ScsiStatus == 0)
  1172. dev_warn(d, "SCSI status is abnormally zero. "
  1173. "(probably indicates selection timeout "
  1174. "reported incorrectly due to a known "
  1175. "firmware bug, circa July, 2001.)\n");
  1176. break;
  1177. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  1178. dev_info(d, "UNDERRUN\n");
  1179. break;
  1180. case CMD_DATA_OVERRUN:
  1181. dev_warn(d, "cp %p has completed with data overrun\n", cp);
  1182. break;
  1183. case CMD_INVALID: {
  1184. /* controller unfortunately reports SCSI passthru's
  1185. * to non-existent targets as invalid commands.
  1186. */
  1187. dev_warn(d, "cp %p is reported invalid (probably means "
  1188. "target device no longer present)\n", cp);
  1189. /* print_bytes((unsigned char *) cp, sizeof(*cp), 1, 0);
  1190. print_cmd(cp); */
  1191. }
  1192. break;
  1193. case CMD_PROTOCOL_ERR:
  1194. dev_warn(d, "cp %p has protocol error \n", cp);
  1195. break;
  1196. case CMD_HARDWARE_ERR:
  1197. /* cmd->result = DID_ERROR << 16; */
  1198. dev_warn(d, "cp %p had hardware error\n", cp);
  1199. break;
  1200. case CMD_CONNECTION_LOST:
  1201. dev_warn(d, "cp %p had connection lost\n", cp);
  1202. break;
  1203. case CMD_ABORTED:
  1204. dev_warn(d, "cp %p was aborted\n", cp);
  1205. break;
  1206. case CMD_ABORT_FAILED:
  1207. dev_warn(d, "cp %p reports abort failed\n", cp);
  1208. break;
  1209. case CMD_UNSOLICITED_ABORT:
  1210. dev_warn(d, "cp %p aborted due to an unsolicited abort\n", cp);
  1211. break;
  1212. case CMD_TIMEOUT:
  1213. dev_warn(d, "cp %p timed out\n", cp);
  1214. break;
  1215. default:
  1216. dev_warn(d, "cp %p returned unknown status %x\n", cp,
  1217. ei->CommandStatus);
  1218. }
  1219. }
  1220. static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
  1221. unsigned char page, unsigned char *buf,
  1222. unsigned char bufsize)
  1223. {
  1224. int rc = IO_OK;
  1225. struct CommandList *c;
  1226. struct ErrorInfo *ei;
  1227. c = cmd_special_alloc(h);
  1228. if (c == NULL) { /* trouble... */
  1229. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1230. return -ENOMEM;
  1231. }
  1232. fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize, page, scsi3addr, TYPE_CMD);
  1233. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1234. ei = c->err_info;
  1235. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1236. hpsa_scsi_interpret_error(c);
  1237. rc = -1;
  1238. }
  1239. cmd_special_free(h, c);
  1240. return rc;
  1241. }
  1242. static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr)
  1243. {
  1244. int rc = IO_OK;
  1245. struct CommandList *c;
  1246. struct ErrorInfo *ei;
  1247. c = cmd_special_alloc(h);
  1248. if (c == NULL) { /* trouble... */
  1249. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1250. return -1;
  1251. }
  1252. fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0, scsi3addr, TYPE_MSG);
  1253. hpsa_scsi_do_simple_cmd_core(h, c);
  1254. /* no unmap needed here because no data xfer. */
  1255. ei = c->err_info;
  1256. if (ei->CommandStatus != 0) {
  1257. hpsa_scsi_interpret_error(c);
  1258. rc = -1;
  1259. }
  1260. cmd_special_free(h, c);
  1261. return rc;
  1262. }
  1263. static void hpsa_get_raid_level(struct ctlr_info *h,
  1264. unsigned char *scsi3addr, unsigned char *raid_level)
  1265. {
  1266. int rc;
  1267. unsigned char *buf;
  1268. *raid_level = RAID_UNKNOWN;
  1269. buf = kzalloc(64, GFP_KERNEL);
  1270. if (!buf)
  1271. return;
  1272. rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0xC1, buf, 64);
  1273. if (rc == 0)
  1274. *raid_level = buf[8];
  1275. if (*raid_level > RAID_UNKNOWN)
  1276. *raid_level = RAID_UNKNOWN;
  1277. kfree(buf);
  1278. return;
  1279. }
  1280. /* Get the device id from inquiry page 0x83 */
  1281. static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
  1282. unsigned char *device_id, int buflen)
  1283. {
  1284. int rc;
  1285. unsigned char *buf;
  1286. if (buflen > 16)
  1287. buflen = 16;
  1288. buf = kzalloc(64, GFP_KERNEL);
  1289. if (!buf)
  1290. return -1;
  1291. rc = hpsa_scsi_do_inquiry(h, scsi3addr, 0x83, buf, 64);
  1292. if (rc == 0)
  1293. memcpy(device_id, &buf[8], buflen);
  1294. kfree(buf);
  1295. return rc != 0;
  1296. }
  1297. static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
  1298. struct ReportLUNdata *buf, int bufsize,
  1299. int extended_response)
  1300. {
  1301. int rc = IO_OK;
  1302. struct CommandList *c;
  1303. unsigned char scsi3addr[8];
  1304. struct ErrorInfo *ei;
  1305. c = cmd_special_alloc(h);
  1306. if (c == NULL) { /* trouble... */
  1307. dev_err(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  1308. return -1;
  1309. }
  1310. /* address the controller */
  1311. memset(scsi3addr, 0, sizeof(scsi3addr));
  1312. fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
  1313. buf, bufsize, 0, scsi3addr, TYPE_CMD);
  1314. if (extended_response)
  1315. c->Request.CDB[1] = extended_response;
  1316. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE);
  1317. ei = c->err_info;
  1318. if (ei->CommandStatus != 0 &&
  1319. ei->CommandStatus != CMD_DATA_UNDERRUN) {
  1320. hpsa_scsi_interpret_error(c);
  1321. rc = -1;
  1322. }
  1323. cmd_special_free(h, c);
  1324. return rc;
  1325. }
  1326. static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
  1327. struct ReportLUNdata *buf,
  1328. int bufsize, int extended_response)
  1329. {
  1330. return hpsa_scsi_do_report_luns(h, 0, buf, bufsize, extended_response);
  1331. }
  1332. static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
  1333. struct ReportLUNdata *buf, int bufsize)
  1334. {
  1335. return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
  1336. }
  1337. static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
  1338. int bus, int target, int lun)
  1339. {
  1340. device->bus = bus;
  1341. device->target = target;
  1342. device->lun = lun;
  1343. }
  1344. static int hpsa_update_device_info(struct ctlr_info *h,
  1345. unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device)
  1346. {
  1347. #define OBDR_TAPE_INQ_SIZE 49
  1348. unsigned char *inq_buff;
  1349. inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  1350. if (!inq_buff)
  1351. goto bail_out;
  1352. /* Do an inquiry to the device to see what it is. */
  1353. if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
  1354. (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
  1355. /* Inquiry failed (msg printed already) */
  1356. dev_err(&h->pdev->dev,
  1357. "hpsa_update_device_info: inquiry failed\n");
  1358. goto bail_out;
  1359. }
  1360. /* As a side effect, record the firmware version number
  1361. * if we happen to be talking to the RAID controller.
  1362. */
  1363. if (is_hba_lunid(scsi3addr))
  1364. memcpy(h->firm_ver, &inq_buff[32], 4);
  1365. this_device->devtype = (inq_buff[0] & 0x1f);
  1366. memcpy(this_device->scsi3addr, scsi3addr, 8);
  1367. memcpy(this_device->vendor, &inq_buff[8],
  1368. sizeof(this_device->vendor));
  1369. memcpy(this_device->model, &inq_buff[16],
  1370. sizeof(this_device->model));
  1371. memcpy(this_device->revision, &inq_buff[32],
  1372. sizeof(this_device->revision));
  1373. memset(this_device->device_id, 0,
  1374. sizeof(this_device->device_id));
  1375. hpsa_get_device_id(h, scsi3addr, this_device->device_id,
  1376. sizeof(this_device->device_id));
  1377. if (this_device->devtype == TYPE_DISK &&
  1378. is_logical_dev_addr_mode(scsi3addr))
  1379. hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
  1380. else
  1381. this_device->raid_level = RAID_UNKNOWN;
  1382. kfree(inq_buff);
  1383. return 0;
  1384. bail_out:
  1385. kfree(inq_buff);
  1386. return 1;
  1387. }
  1388. static unsigned char *msa2xxx_model[] = {
  1389. "MSA2012",
  1390. "MSA2024",
  1391. "MSA2312",
  1392. "MSA2324",
  1393. NULL,
  1394. };
  1395. static int is_msa2xxx(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
  1396. {
  1397. int i;
  1398. for (i = 0; msa2xxx_model[i]; i++)
  1399. if (strncmp(device->model, msa2xxx_model[i],
  1400. strlen(msa2xxx_model[i])) == 0)
  1401. return 1;
  1402. return 0;
  1403. }
  1404. /* Helper function to assign bus, target, lun mapping of devices.
  1405. * Puts non-msa2xxx logical volumes on bus 0, msa2xxx logical
  1406. * volumes on bus 1, physical devices on bus 2. and the hba on bus 3.
  1407. * Logical drive target and lun are assigned at this time, but
  1408. * physical device lun and target assignment are deferred (assigned
  1409. * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
  1410. */
  1411. static void figure_bus_target_lun(struct ctlr_info *h,
  1412. u8 *lunaddrbytes, int *bus, int *target, int *lun,
  1413. struct hpsa_scsi_dev_t *device)
  1414. {
  1415. u32 lunid;
  1416. if (is_logical_dev_addr_mode(lunaddrbytes)) {
  1417. /* logical device */
  1418. if (unlikely(is_scsi_rev_5(h))) {
  1419. /* p1210m, logical drives lun assignments
  1420. * match SCSI REPORT LUNS data.
  1421. */
  1422. lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
  1423. *bus = 0;
  1424. *target = 0;
  1425. *lun = (lunid & 0x3fff) + 1;
  1426. } else {
  1427. /* not p1210m... */
  1428. lunid = le32_to_cpu(*((__le32 *) lunaddrbytes));
  1429. if (is_msa2xxx(h, device)) {
  1430. /* msa2xxx way, put logicals on bus 1
  1431. * and match target/lun numbers box
  1432. * reports.
  1433. */
  1434. *bus = 1;
  1435. *target = (lunid >> 16) & 0x3fff;
  1436. *lun = lunid & 0x00ff;
  1437. } else {
  1438. /* Traditional smart array way. */
  1439. *bus = 0;
  1440. *lun = 0;
  1441. *target = lunid & 0x3fff;
  1442. }
  1443. }
  1444. } else {
  1445. /* physical device */
  1446. if (is_hba_lunid(lunaddrbytes))
  1447. if (unlikely(is_scsi_rev_5(h))) {
  1448. *bus = 0; /* put p1210m ctlr at 0,0,0 */
  1449. *target = 0;
  1450. *lun = 0;
  1451. return;
  1452. } else
  1453. *bus = 3; /* traditional smartarray */
  1454. else
  1455. *bus = 2; /* physical disk */
  1456. *target = -1;
  1457. *lun = -1; /* we will fill these in later. */
  1458. }
  1459. }
  1460. /*
  1461. * If there is no lun 0 on a target, linux won't find any devices.
  1462. * For the MSA2xxx boxes, we have to manually detect the enclosure
  1463. * which is at lun zero, as CCISS_REPORT_PHYSICAL_LUNS doesn't report
  1464. * it for some reason. *tmpdevice is the target we're adding,
  1465. * this_device is a pointer into the current element of currentsd[]
  1466. * that we're building up in update_scsi_devices(), below.
  1467. * lunzerobits is a bitmap that tracks which targets already have a
  1468. * lun 0 assigned.
  1469. * Returns 1 if an enclosure was added, 0 if not.
  1470. */
  1471. static int add_msa2xxx_enclosure_device(struct ctlr_info *h,
  1472. struct hpsa_scsi_dev_t *tmpdevice,
  1473. struct hpsa_scsi_dev_t *this_device, u8 *lunaddrbytes,
  1474. int bus, int target, int lun, unsigned long lunzerobits[],
  1475. int *nmsa2xxx_enclosures)
  1476. {
  1477. unsigned char scsi3addr[8];
  1478. if (test_bit(target, lunzerobits))
  1479. return 0; /* There is already a lun 0 on this target. */
  1480. if (!is_logical_dev_addr_mode(lunaddrbytes))
  1481. return 0; /* It's the logical targets that may lack lun 0. */
  1482. if (!is_msa2xxx(h, tmpdevice))
  1483. return 0; /* It's only the MSA2xxx that have this problem. */
  1484. if (lun == 0) /* if lun is 0, then obviously we have a lun 0. */
  1485. return 0;
  1486. if (is_hba_lunid(scsi3addr))
  1487. return 0; /* Don't add the RAID controller here. */
  1488. if (is_scsi_rev_5(h))
  1489. return 0; /* p1210m doesn't need to do this. */
  1490. #define MAX_MSA2XXX_ENCLOSURES 32
  1491. if (*nmsa2xxx_enclosures >= MAX_MSA2XXX_ENCLOSURES) {
  1492. dev_warn(&h->pdev->dev, "Maximum number of MSA2XXX "
  1493. "enclosures exceeded. Check your hardware "
  1494. "configuration.");
  1495. return 0;
  1496. }
  1497. memset(scsi3addr, 0, 8);
  1498. scsi3addr[3] = target;
  1499. if (hpsa_update_device_info(h, scsi3addr, this_device))
  1500. return 0;
  1501. (*nmsa2xxx_enclosures)++;
  1502. hpsa_set_bus_target_lun(this_device, bus, target, 0);
  1503. set_bit(target, lunzerobits);
  1504. return 1;
  1505. }
  1506. /*
  1507. * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
  1508. * logdev. The number of luns in physdev and logdev are returned in
  1509. * *nphysicals and *nlogicals, respectively.
  1510. * Returns 0 on success, -1 otherwise.
  1511. */
  1512. static int hpsa_gather_lun_info(struct ctlr_info *h,
  1513. int reportlunsize,
  1514. struct ReportLUNdata *physdev, u32 *nphysicals,
  1515. struct ReportLUNdata *logdev, u32 *nlogicals)
  1516. {
  1517. if (hpsa_scsi_do_report_phys_luns(h, physdev, reportlunsize, 0)) {
  1518. dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
  1519. return -1;
  1520. }
  1521. *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 8;
  1522. if (*nphysicals > HPSA_MAX_PHYS_LUN) {
  1523. dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded."
  1524. " %d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  1525. *nphysicals - HPSA_MAX_PHYS_LUN);
  1526. *nphysicals = HPSA_MAX_PHYS_LUN;
  1527. }
  1528. if (hpsa_scsi_do_report_log_luns(h, logdev, reportlunsize)) {
  1529. dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
  1530. return -1;
  1531. }
  1532. *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
  1533. /* Reject Logicals in excess of our max capability. */
  1534. if (*nlogicals > HPSA_MAX_LUN) {
  1535. dev_warn(&h->pdev->dev,
  1536. "maximum logical LUNs (%d) exceeded. "
  1537. "%d LUNs ignored.\n", HPSA_MAX_LUN,
  1538. *nlogicals - HPSA_MAX_LUN);
  1539. *nlogicals = HPSA_MAX_LUN;
  1540. }
  1541. if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
  1542. dev_warn(&h->pdev->dev,
  1543. "maximum logical + physical LUNs (%d) exceeded. "
  1544. "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  1545. *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
  1546. *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
  1547. }
  1548. return 0;
  1549. }
  1550. u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position, int i,
  1551. int nphysicals, int nlogicals, struct ReportLUNdata *physdev_list,
  1552. struct ReportLUNdata *logdev_list)
  1553. {
  1554. /* Helper function, figure out where the LUN ID info is coming from
  1555. * given index i, lists of physical and logical devices, where in
  1556. * the list the raid controller is supposed to appear (first or last)
  1557. */
  1558. int logicals_start = nphysicals + (raid_ctlr_position == 0);
  1559. int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
  1560. if (i == raid_ctlr_position)
  1561. return RAID_CTLR_LUNID;
  1562. if (i < logicals_start)
  1563. return &physdev_list->LUN[i - (raid_ctlr_position == 0)][0];
  1564. if (i < last_device)
  1565. return &logdev_list->LUN[i - nphysicals -
  1566. (raid_ctlr_position == 0)][0];
  1567. BUG();
  1568. return NULL;
  1569. }
  1570. static void hpsa_update_scsi_devices(struct ctlr_info *h, int hostno)
  1571. {
  1572. /* the idea here is we could get notified
  1573. * that some devices have changed, so we do a report
  1574. * physical luns and report logical luns cmd, and adjust
  1575. * our list of devices accordingly.
  1576. *
  1577. * The scsi3addr's of devices won't change so long as the
  1578. * adapter is not reset. That means we can rescan and
  1579. * tell which devices we already know about, vs. new
  1580. * devices, vs. disappearing devices.
  1581. */
  1582. struct ReportLUNdata *physdev_list = NULL;
  1583. struct ReportLUNdata *logdev_list = NULL;
  1584. unsigned char *inq_buff = NULL;
  1585. u32 nphysicals = 0;
  1586. u32 nlogicals = 0;
  1587. u32 ndev_allocated = 0;
  1588. struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
  1589. int ncurrent = 0;
  1590. int reportlunsize = sizeof(*physdev_list) + HPSA_MAX_PHYS_LUN * 8;
  1591. int i, nmsa2xxx_enclosures, ndevs_to_allocate;
  1592. int bus, target, lun;
  1593. int raid_ctlr_position;
  1594. DECLARE_BITMAP(lunzerobits, HPSA_MAX_TARGETS_PER_CTLR);
  1595. currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_SCSI_DEVS_PER_HBA,
  1596. GFP_KERNEL);
  1597. physdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  1598. logdev_list = kzalloc(reportlunsize, GFP_KERNEL);
  1599. inq_buff = kmalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  1600. tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
  1601. if (!currentsd || !physdev_list || !logdev_list ||
  1602. !inq_buff || !tmpdevice) {
  1603. dev_err(&h->pdev->dev, "out of memory\n");
  1604. goto out;
  1605. }
  1606. memset(lunzerobits, 0, sizeof(lunzerobits));
  1607. if (hpsa_gather_lun_info(h, reportlunsize, physdev_list, &nphysicals,
  1608. logdev_list, &nlogicals))
  1609. goto out;
  1610. /* We might see up to 32 MSA2xxx enclosures, actually 8 of them
  1611. * but each of them 4 times through different paths. The plus 1
  1612. * is for the RAID controller.
  1613. */
  1614. ndevs_to_allocate = nphysicals + nlogicals + MAX_MSA2XXX_ENCLOSURES + 1;
  1615. /* Allocate the per device structures */
  1616. for (i = 0; i < ndevs_to_allocate; i++) {
  1617. currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
  1618. if (!currentsd[i]) {
  1619. dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
  1620. __FILE__, __LINE__);
  1621. goto out;
  1622. }
  1623. ndev_allocated++;
  1624. }
  1625. if (unlikely(is_scsi_rev_5(h)))
  1626. raid_ctlr_position = 0;
  1627. else
  1628. raid_ctlr_position = nphysicals + nlogicals;
  1629. /* adjust our table of devices */
  1630. nmsa2xxx_enclosures = 0;
  1631. for (i = 0; i < nphysicals + nlogicals + 1; i++) {
  1632. u8 *lunaddrbytes;
  1633. /* Figure out where the LUN ID info is coming from */
  1634. lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
  1635. i, nphysicals, nlogicals, physdev_list, logdev_list);
  1636. /* skip masked physical devices. */
  1637. if (lunaddrbytes[3] & 0xC0 &&
  1638. i < nphysicals + (raid_ctlr_position == 0))
  1639. continue;
  1640. /* Get device type, vendor, model, device id */
  1641. if (hpsa_update_device_info(h, lunaddrbytes, tmpdevice))
  1642. continue; /* skip it if we can't talk to it. */
  1643. figure_bus_target_lun(h, lunaddrbytes, &bus, &target, &lun,
  1644. tmpdevice);
  1645. this_device = currentsd[ncurrent];
  1646. /*
  1647. * For the msa2xxx boxes, we have to insert a LUN 0 which
  1648. * doesn't show up in CCISS_REPORT_PHYSICAL data, but there
  1649. * is nonetheless an enclosure device there. We have to
  1650. * present that otherwise linux won't find anything if
  1651. * there is no lun 0.
  1652. */
  1653. if (add_msa2xxx_enclosure_device(h, tmpdevice, this_device,
  1654. lunaddrbytes, bus, target, lun, lunzerobits,
  1655. &nmsa2xxx_enclosures)) {
  1656. ncurrent++;
  1657. this_device = currentsd[ncurrent];
  1658. }
  1659. *this_device = *tmpdevice;
  1660. hpsa_set_bus_target_lun(this_device, bus, target, lun);
  1661. switch (this_device->devtype) {
  1662. case TYPE_ROM: {
  1663. /* We don't *really* support actual CD-ROM devices,
  1664. * just "One Button Disaster Recovery" tape drive
  1665. * which temporarily pretends to be a CD-ROM drive.
  1666. * So we check that the device is really an OBDR tape
  1667. * device by checking for "$DR-10" in bytes 43-48 of
  1668. * the inquiry data.
  1669. */
  1670. char obdr_sig[7];
  1671. #define OBDR_TAPE_SIG "$DR-10"
  1672. strncpy(obdr_sig, &inq_buff[43], 6);
  1673. obdr_sig[6] = '\0';
  1674. if (strncmp(obdr_sig, OBDR_TAPE_SIG, 6) != 0)
  1675. /* Not OBDR device, ignore it. */
  1676. break;
  1677. }
  1678. ncurrent++;
  1679. break;
  1680. case TYPE_DISK:
  1681. if (i < nphysicals)
  1682. break;
  1683. ncurrent++;
  1684. break;
  1685. case TYPE_TAPE:
  1686. case TYPE_MEDIUM_CHANGER:
  1687. ncurrent++;
  1688. break;
  1689. case TYPE_RAID:
  1690. /* Only present the Smartarray HBA as a RAID controller.
  1691. * If it's a RAID controller other than the HBA itself
  1692. * (an external RAID controller, MSA500 or similar)
  1693. * don't present it.
  1694. */
  1695. if (!is_hba_lunid(lunaddrbytes))
  1696. break;
  1697. ncurrent++;
  1698. break;
  1699. default:
  1700. break;
  1701. }
  1702. if (ncurrent >= HPSA_MAX_SCSI_DEVS_PER_HBA)
  1703. break;
  1704. }
  1705. adjust_hpsa_scsi_table(h, hostno, currentsd, ncurrent);
  1706. out:
  1707. kfree(tmpdevice);
  1708. for (i = 0; i < ndev_allocated; i++)
  1709. kfree(currentsd[i]);
  1710. kfree(currentsd);
  1711. kfree(inq_buff);
  1712. kfree(physdev_list);
  1713. kfree(logdev_list);
  1714. }
  1715. /* hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
  1716. * dma mapping and fills in the scatter gather entries of the
  1717. * hpsa command, cp.
  1718. */
  1719. static int hpsa_scatter_gather(struct pci_dev *pdev,
  1720. struct CommandList *cp,
  1721. struct scsi_cmnd *cmd)
  1722. {
  1723. unsigned int len;
  1724. struct scatterlist *sg;
  1725. u64 addr64;
  1726. int use_sg, i;
  1727. BUG_ON(scsi_sg_count(cmd) > MAXSGENTRIES);
  1728. use_sg = scsi_dma_map(cmd);
  1729. if (use_sg < 0)
  1730. return use_sg;
  1731. if (!use_sg)
  1732. goto sglist_finished;
  1733. scsi_for_each_sg(cmd, sg, use_sg, i) {
  1734. addr64 = (u64) sg_dma_address(sg);
  1735. len = sg_dma_len(sg);
  1736. cp->SG[i].Addr.lower =
  1737. (u32) (addr64 & (u64) 0x00000000FFFFFFFF);
  1738. cp->SG[i].Addr.upper =
  1739. (u32) ((addr64 >> 32) & (u64) 0x00000000FFFFFFFF);
  1740. cp->SG[i].Len = len;
  1741. cp->SG[i].Ext = 0; /* we are not chaining */
  1742. }
  1743. sglist_finished:
  1744. cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
  1745. cp->Header.SGTotal = (u16) use_sg; /* total sgs in this cmd list */
  1746. return 0;
  1747. }
  1748. static int hpsa_scsi_queue_command(struct scsi_cmnd *cmd,
  1749. void (*done)(struct scsi_cmnd *))
  1750. {
  1751. struct ctlr_info *h;
  1752. struct hpsa_scsi_dev_t *dev;
  1753. unsigned char scsi3addr[8];
  1754. struct CommandList *c;
  1755. unsigned long flags;
  1756. /* Get the ptr to our adapter structure out of cmd->host. */
  1757. h = sdev_to_hba(cmd->device);
  1758. dev = cmd->device->hostdata;
  1759. if (!dev) {
  1760. cmd->result = DID_NO_CONNECT << 16;
  1761. done(cmd);
  1762. return 0;
  1763. }
  1764. memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
  1765. /* Need a lock as this is being allocated from the pool */
  1766. spin_lock_irqsave(&h->lock, flags);
  1767. c = cmd_alloc(h);
  1768. spin_unlock_irqrestore(&h->lock, flags);
  1769. if (c == NULL) { /* trouble... */
  1770. dev_err(&h->pdev->dev, "cmd_alloc returned NULL!\n");
  1771. return SCSI_MLQUEUE_HOST_BUSY;
  1772. }
  1773. /* Fill in the command list header */
  1774. cmd->scsi_done = done; /* save this for use by completion code */
  1775. /* save c in case we have to abort it */
  1776. cmd->host_scribble = (unsigned char *) c;
  1777. c->cmd_type = CMD_SCSI;
  1778. c->scsi_cmd = cmd;
  1779. c->Header.ReplyQueue = 0; /* unused in simple mode */
  1780. memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
  1781. c->Header.Tag.lower = (c->cmdindex << DIRECT_LOOKUP_SHIFT);
  1782. c->Header.Tag.lower |= DIRECT_LOOKUP_BIT;
  1783. /* Fill in the request block... */
  1784. c->Request.Timeout = 0;
  1785. memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
  1786. BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
  1787. c->Request.CDBLen = cmd->cmd_len;
  1788. memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
  1789. c->Request.Type.Type = TYPE_CMD;
  1790. c->Request.Type.Attribute = ATTR_SIMPLE;
  1791. switch (cmd->sc_data_direction) {
  1792. case DMA_TO_DEVICE:
  1793. c->Request.Type.Direction = XFER_WRITE;
  1794. break;
  1795. case DMA_FROM_DEVICE:
  1796. c->Request.Type.Direction = XFER_READ;
  1797. break;
  1798. case DMA_NONE:
  1799. c->Request.Type.Direction = XFER_NONE;
  1800. break;
  1801. case DMA_BIDIRECTIONAL:
  1802. /* This can happen if a buggy application does a scsi passthru
  1803. * and sets both inlen and outlen to non-zero. ( see
  1804. * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
  1805. */
  1806. c->Request.Type.Direction = XFER_RSVD;
  1807. /* This is technically wrong, and hpsa controllers should
  1808. * reject it with CMD_INVALID, which is the most correct
  1809. * response, but non-fibre backends appear to let it
  1810. * slide by, and give the same results as if this field
  1811. * were set correctly. Either way is acceptable for
  1812. * our purposes here.
  1813. */
  1814. break;
  1815. default:
  1816. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  1817. cmd->sc_data_direction);
  1818. BUG();
  1819. break;
  1820. }
  1821. if (hpsa_scatter_gather(h->pdev, c, cmd) < 0) { /* Fill SG list */
  1822. cmd_free(h, c);
  1823. return SCSI_MLQUEUE_HOST_BUSY;
  1824. }
  1825. enqueue_cmd_and_start_io(h, c);
  1826. /* the cmd'll come back via intr handler in complete_scsi_command() */
  1827. return 0;
  1828. }
  1829. static void hpsa_scan_start(struct Scsi_Host *sh)
  1830. {
  1831. struct ctlr_info *h = shost_to_hba(sh);
  1832. unsigned long flags;
  1833. /* wait until any scan already in progress is finished. */
  1834. while (1) {
  1835. spin_lock_irqsave(&h->scan_lock, flags);
  1836. if (h->scan_finished)
  1837. break;
  1838. spin_unlock_irqrestore(&h->scan_lock, flags);
  1839. wait_event(h->scan_wait_queue, h->scan_finished);
  1840. /* Note: We don't need to worry about a race between this
  1841. * thread and driver unload because the midlayer will
  1842. * have incremented the reference count, so unload won't
  1843. * happen if we're in here.
  1844. */
  1845. }
  1846. h->scan_finished = 0; /* mark scan as in progress */
  1847. spin_unlock_irqrestore(&h->scan_lock, flags);
  1848. hpsa_update_scsi_devices(h, h->scsi_host->host_no);
  1849. spin_lock_irqsave(&h->scan_lock, flags);
  1850. h->scan_finished = 1; /* mark scan as finished. */
  1851. wake_up_all(&h->scan_wait_queue);
  1852. spin_unlock_irqrestore(&h->scan_lock, flags);
  1853. }
  1854. static int hpsa_scan_finished(struct Scsi_Host *sh,
  1855. unsigned long elapsed_time)
  1856. {
  1857. struct ctlr_info *h = shost_to_hba(sh);
  1858. unsigned long flags;
  1859. int finished;
  1860. spin_lock_irqsave(&h->scan_lock, flags);
  1861. finished = h->scan_finished;
  1862. spin_unlock_irqrestore(&h->scan_lock, flags);
  1863. return finished;
  1864. }
  1865. static void hpsa_unregister_scsi(struct ctlr_info *h)
  1866. {
  1867. /* we are being forcibly unloaded, and may not refuse. */
  1868. scsi_remove_host(h->scsi_host);
  1869. scsi_host_put(h->scsi_host);
  1870. h->scsi_host = NULL;
  1871. }
  1872. static int hpsa_register_scsi(struct ctlr_info *h)
  1873. {
  1874. int rc;
  1875. rc = hpsa_scsi_detect(h);
  1876. if (rc != 0)
  1877. dev_err(&h->pdev->dev, "hpsa_register_scsi: failed"
  1878. " hpsa_scsi_detect(), rc is %d\n", rc);
  1879. return rc;
  1880. }
  1881. static int wait_for_device_to_become_ready(struct ctlr_info *h,
  1882. unsigned char lunaddr[])
  1883. {
  1884. int rc = 0;
  1885. int count = 0;
  1886. int waittime = 1; /* seconds */
  1887. struct CommandList *c;
  1888. c = cmd_special_alloc(h);
  1889. if (!c) {
  1890. dev_warn(&h->pdev->dev, "out of memory in "
  1891. "wait_for_device_to_become_ready.\n");
  1892. return IO_ERROR;
  1893. }
  1894. /* Send test unit ready until device ready, or give up. */
  1895. while (count < HPSA_TUR_RETRY_LIMIT) {
  1896. /* Wait for a bit. do this first, because if we send
  1897. * the TUR right away, the reset will just abort it.
  1898. */
  1899. msleep(1000 * waittime);
  1900. count++;
  1901. /* Increase wait time with each try, up to a point. */
  1902. if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
  1903. waittime = waittime * 2;
  1904. /* Send the Test Unit Ready */
  1905. fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, lunaddr, TYPE_CMD);
  1906. hpsa_scsi_do_simple_cmd_core(h, c);
  1907. /* no unmap needed here because no data xfer. */
  1908. if (c->err_info->CommandStatus == CMD_SUCCESS)
  1909. break;
  1910. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  1911. c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
  1912. (c->err_info->SenseInfo[2] == NO_SENSE ||
  1913. c->err_info->SenseInfo[2] == UNIT_ATTENTION))
  1914. break;
  1915. dev_warn(&h->pdev->dev, "waiting %d secs "
  1916. "for device to become ready.\n", waittime);
  1917. rc = 1; /* device not ready. */
  1918. }
  1919. if (rc)
  1920. dev_warn(&h->pdev->dev, "giving up on device.\n");
  1921. else
  1922. dev_warn(&h->pdev->dev, "device is ready.\n");
  1923. cmd_special_free(h, c);
  1924. return rc;
  1925. }
  1926. /* Need at least one of these error handlers to keep ../scsi/hosts.c from
  1927. * complaining. Doing a host- or bus-reset can't do anything good here.
  1928. */
  1929. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
  1930. {
  1931. int rc;
  1932. struct ctlr_info *h;
  1933. struct hpsa_scsi_dev_t *dev;
  1934. /* find the controller to which the command to be aborted was sent */
  1935. h = sdev_to_hba(scsicmd->device);
  1936. if (h == NULL) /* paranoia */
  1937. return FAILED;
  1938. dev = scsicmd->device->hostdata;
  1939. if (!dev) {
  1940. dev_err(&h->pdev->dev, "hpsa_eh_device_reset_handler: "
  1941. "device lookup failed.\n");
  1942. return FAILED;
  1943. }
  1944. dev_warn(&h->pdev->dev, "resetting device %d:%d:%d:%d\n",
  1945. h->scsi_host->host_no, dev->bus, dev->target, dev->lun);
  1946. /* send a reset to the SCSI LUN which the command was sent to */
  1947. rc = hpsa_send_reset(h, dev->scsi3addr);
  1948. if (rc == 0 && wait_for_device_to_become_ready(h, dev->scsi3addr) == 0)
  1949. return SUCCESS;
  1950. dev_warn(&h->pdev->dev, "resetting device failed.\n");
  1951. return FAILED;
  1952. }
  1953. /*
  1954. * For operations that cannot sleep, a command block is allocated at init,
  1955. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  1956. * which ones are free or in use. Lock must be held when calling this.
  1957. * cmd_free() is the complement.
  1958. */
  1959. static struct CommandList *cmd_alloc(struct ctlr_info *h)
  1960. {
  1961. struct CommandList *c;
  1962. int i;
  1963. union u64bit temp64;
  1964. dma_addr_t cmd_dma_handle, err_dma_handle;
  1965. do {
  1966. i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
  1967. if (i == h->nr_cmds)
  1968. return NULL;
  1969. } while (test_and_set_bit
  1970. (i & (BITS_PER_LONG - 1),
  1971. h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
  1972. c = h->cmd_pool + i;
  1973. memset(c, 0, sizeof(*c));
  1974. cmd_dma_handle = h->cmd_pool_dhandle
  1975. + i * sizeof(*c);
  1976. c->err_info = h->errinfo_pool + i;
  1977. memset(c->err_info, 0, sizeof(*c->err_info));
  1978. err_dma_handle = h->errinfo_pool_dhandle
  1979. + i * sizeof(*c->err_info);
  1980. h->nr_allocs++;
  1981. c->cmdindex = i;
  1982. INIT_HLIST_NODE(&c->list);
  1983. c->busaddr = (u32) cmd_dma_handle;
  1984. temp64.val = (u64) err_dma_handle;
  1985. c->ErrDesc.Addr.lower = temp64.val32.lower;
  1986. c->ErrDesc.Addr.upper = temp64.val32.upper;
  1987. c->ErrDesc.Len = sizeof(*c->err_info);
  1988. c->h = h;
  1989. return c;
  1990. }
  1991. /* For operations that can wait for kmalloc to possibly sleep,
  1992. * this routine can be called. Lock need not be held to call
  1993. * cmd_special_alloc. cmd_special_free() is the complement.
  1994. */
  1995. static struct CommandList *cmd_special_alloc(struct ctlr_info *h)
  1996. {
  1997. struct CommandList *c;
  1998. union u64bit temp64;
  1999. dma_addr_t cmd_dma_handle, err_dma_handle;
  2000. c = pci_alloc_consistent(h->pdev, sizeof(*c), &cmd_dma_handle);
  2001. if (c == NULL)
  2002. return NULL;
  2003. memset(c, 0, sizeof(*c));
  2004. c->cmdindex = -1;
  2005. c->err_info = pci_alloc_consistent(h->pdev, sizeof(*c->err_info),
  2006. &err_dma_handle);
  2007. if (c->err_info == NULL) {
  2008. pci_free_consistent(h->pdev,
  2009. sizeof(*c), c, cmd_dma_handle);
  2010. return NULL;
  2011. }
  2012. memset(c->err_info, 0, sizeof(*c->err_info));
  2013. INIT_HLIST_NODE(&c->list);
  2014. c->busaddr = (u32) cmd_dma_handle;
  2015. temp64.val = (u64) err_dma_handle;
  2016. c->ErrDesc.Addr.lower = temp64.val32.lower;
  2017. c->ErrDesc.Addr.upper = temp64.val32.upper;
  2018. c->ErrDesc.Len = sizeof(*c->err_info);
  2019. c->h = h;
  2020. return c;
  2021. }
  2022. static void cmd_free(struct ctlr_info *h, struct CommandList *c)
  2023. {
  2024. int i;
  2025. i = c - h->cmd_pool;
  2026. clear_bit(i & (BITS_PER_LONG - 1),
  2027. h->cmd_pool_bits + (i / BITS_PER_LONG));
  2028. h->nr_frees++;
  2029. }
  2030. static void cmd_special_free(struct ctlr_info *h, struct CommandList *c)
  2031. {
  2032. union u64bit temp64;
  2033. temp64.val32.lower = c->ErrDesc.Addr.lower;
  2034. temp64.val32.upper = c->ErrDesc.Addr.upper;
  2035. pci_free_consistent(h->pdev, sizeof(*c->err_info),
  2036. c->err_info, (dma_addr_t) temp64.val);
  2037. pci_free_consistent(h->pdev, sizeof(*c),
  2038. c, (dma_addr_t) c->busaddr);
  2039. }
  2040. #ifdef CONFIG_COMPAT
  2041. static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd, void *arg)
  2042. {
  2043. IOCTL32_Command_struct __user *arg32 =
  2044. (IOCTL32_Command_struct __user *) arg;
  2045. IOCTL_Command_struct arg64;
  2046. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  2047. int err;
  2048. u32 cp;
  2049. err = 0;
  2050. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  2051. sizeof(arg64.LUN_info));
  2052. err |= copy_from_user(&arg64.Request, &arg32->Request,
  2053. sizeof(arg64.Request));
  2054. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  2055. sizeof(arg64.error_info));
  2056. err |= get_user(arg64.buf_size, &arg32->buf_size);
  2057. err |= get_user(cp, &arg32->buf);
  2058. arg64.buf = compat_ptr(cp);
  2059. err |= copy_to_user(p, &arg64, sizeof(arg64));
  2060. if (err)
  2061. return -EFAULT;
  2062. err = hpsa_ioctl(dev, CCISS_PASSTHRU, (void *)p);
  2063. if (err)
  2064. return err;
  2065. err |= copy_in_user(&arg32->error_info, &p->error_info,
  2066. sizeof(arg32->error_info));
  2067. if (err)
  2068. return -EFAULT;
  2069. return err;
  2070. }
  2071. static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
  2072. int cmd, void *arg)
  2073. {
  2074. BIG_IOCTL32_Command_struct __user *arg32 =
  2075. (BIG_IOCTL32_Command_struct __user *) arg;
  2076. BIG_IOCTL_Command_struct arg64;
  2077. BIG_IOCTL_Command_struct __user *p =
  2078. compat_alloc_user_space(sizeof(arg64));
  2079. int err;
  2080. u32 cp;
  2081. err = 0;
  2082. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  2083. sizeof(arg64.LUN_info));
  2084. err |= copy_from_user(&arg64.Request, &arg32->Request,
  2085. sizeof(arg64.Request));
  2086. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  2087. sizeof(arg64.error_info));
  2088. err |= get_user(arg64.buf_size, &arg32->buf_size);
  2089. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  2090. err |= get_user(cp, &arg32->buf);
  2091. arg64.buf = compat_ptr(cp);
  2092. err |= copy_to_user(p, &arg64, sizeof(arg64));
  2093. if (err)
  2094. return -EFAULT;
  2095. err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, (void *)p);
  2096. if (err)
  2097. return err;
  2098. err |= copy_in_user(&arg32->error_info, &p->error_info,
  2099. sizeof(arg32->error_info));
  2100. if (err)
  2101. return -EFAULT;
  2102. return err;
  2103. }
  2104. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void *arg)
  2105. {
  2106. switch (cmd) {
  2107. case CCISS_GETPCIINFO:
  2108. case CCISS_GETINTINFO:
  2109. case CCISS_SETINTINFO:
  2110. case CCISS_GETNODENAME:
  2111. case CCISS_SETNODENAME:
  2112. case CCISS_GETHEARTBEAT:
  2113. case CCISS_GETBUSTYPES:
  2114. case CCISS_GETFIRMVER:
  2115. case CCISS_GETDRIVVER:
  2116. case CCISS_REVALIDVOLS:
  2117. case CCISS_DEREGDISK:
  2118. case CCISS_REGNEWDISK:
  2119. case CCISS_REGNEWD:
  2120. case CCISS_RESCANDISK:
  2121. case CCISS_GETLUNINFO:
  2122. return hpsa_ioctl(dev, cmd, arg);
  2123. case CCISS_PASSTHRU32:
  2124. return hpsa_ioctl32_passthru(dev, cmd, arg);
  2125. case CCISS_BIG_PASSTHRU32:
  2126. return hpsa_ioctl32_big_passthru(dev, cmd, arg);
  2127. default:
  2128. return -ENOIOCTLCMD;
  2129. }
  2130. }
  2131. #endif
  2132. static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
  2133. {
  2134. struct hpsa_pci_info pciinfo;
  2135. if (!argp)
  2136. return -EINVAL;
  2137. pciinfo.domain = pci_domain_nr(h->pdev->bus);
  2138. pciinfo.bus = h->pdev->bus->number;
  2139. pciinfo.dev_fn = h->pdev->devfn;
  2140. pciinfo.board_id = h->board_id;
  2141. if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
  2142. return -EFAULT;
  2143. return 0;
  2144. }
  2145. static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
  2146. {
  2147. DriverVer_type DriverVer;
  2148. unsigned char vmaj, vmin, vsubmin;
  2149. int rc;
  2150. rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
  2151. &vmaj, &vmin, &vsubmin);
  2152. if (rc != 3) {
  2153. dev_info(&h->pdev->dev, "driver version string '%s' "
  2154. "unrecognized.", HPSA_DRIVER_VERSION);
  2155. vmaj = 0;
  2156. vmin = 0;
  2157. vsubmin = 0;
  2158. }
  2159. DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
  2160. if (!argp)
  2161. return -EINVAL;
  2162. if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
  2163. return -EFAULT;
  2164. return 0;
  2165. }
  2166. static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  2167. {
  2168. IOCTL_Command_struct iocommand;
  2169. struct CommandList *c;
  2170. char *buff = NULL;
  2171. union u64bit temp64;
  2172. if (!argp)
  2173. return -EINVAL;
  2174. if (!capable(CAP_SYS_RAWIO))
  2175. return -EPERM;
  2176. if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
  2177. return -EFAULT;
  2178. if ((iocommand.buf_size < 1) &&
  2179. (iocommand.Request.Type.Direction != XFER_NONE)) {
  2180. return -EINVAL;
  2181. }
  2182. if (iocommand.buf_size > 0) {
  2183. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  2184. if (buff == NULL)
  2185. return -EFAULT;
  2186. }
  2187. if (iocommand.Request.Type.Direction == XFER_WRITE) {
  2188. /* Copy the data into the buffer we created */
  2189. if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
  2190. kfree(buff);
  2191. return -EFAULT;
  2192. }
  2193. } else
  2194. memset(buff, 0, iocommand.buf_size);
  2195. c = cmd_special_alloc(h);
  2196. if (c == NULL) {
  2197. kfree(buff);
  2198. return -ENOMEM;
  2199. }
  2200. /* Fill in the command type */
  2201. c->cmd_type = CMD_IOCTL_PEND;
  2202. /* Fill in Command Header */
  2203. c->Header.ReplyQueue = 0; /* unused in simple mode */
  2204. if (iocommand.buf_size > 0) { /* buffer to fill */
  2205. c->Header.SGList = 1;
  2206. c->Header.SGTotal = 1;
  2207. } else { /* no buffers to fill */
  2208. c->Header.SGList = 0;
  2209. c->Header.SGTotal = 0;
  2210. }
  2211. memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
  2212. /* use the kernel address the cmd block for tag */
  2213. c->Header.Tag.lower = c->busaddr;
  2214. /* Fill in Request block */
  2215. memcpy(&c->Request, &iocommand.Request,
  2216. sizeof(c->Request));
  2217. /* Fill in the scatter gather information */
  2218. if (iocommand.buf_size > 0) {
  2219. temp64.val = pci_map_single(h->pdev, buff,
  2220. iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
  2221. c->SG[0].Addr.lower = temp64.val32.lower;
  2222. c->SG[0].Addr.upper = temp64.val32.upper;
  2223. c->SG[0].Len = iocommand.buf_size;
  2224. c->SG[0].Ext = 0; /* we are not chaining*/
  2225. }
  2226. hpsa_scsi_do_simple_cmd_core(h, c);
  2227. hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
  2228. check_ioctl_unit_attention(h, c);
  2229. /* Copy the error information out */
  2230. memcpy(&iocommand.error_info, c->err_info,
  2231. sizeof(iocommand.error_info));
  2232. if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
  2233. kfree(buff);
  2234. cmd_special_free(h, c);
  2235. return -EFAULT;
  2236. }
  2237. if (iocommand.Request.Type.Direction == XFER_READ) {
  2238. /* Copy the data out of the buffer we created */
  2239. if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
  2240. kfree(buff);
  2241. cmd_special_free(h, c);
  2242. return -EFAULT;
  2243. }
  2244. }
  2245. kfree(buff);
  2246. cmd_special_free(h, c);
  2247. return 0;
  2248. }
  2249. static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  2250. {
  2251. BIG_IOCTL_Command_struct *ioc;
  2252. struct CommandList *c;
  2253. unsigned char **buff = NULL;
  2254. int *buff_size = NULL;
  2255. union u64bit temp64;
  2256. BYTE sg_used = 0;
  2257. int status = 0;
  2258. int i;
  2259. u32 left;
  2260. u32 sz;
  2261. BYTE __user *data_ptr;
  2262. if (!argp)
  2263. return -EINVAL;
  2264. if (!capable(CAP_SYS_RAWIO))
  2265. return -EPERM;
  2266. ioc = (BIG_IOCTL_Command_struct *)
  2267. kmalloc(sizeof(*ioc), GFP_KERNEL);
  2268. if (!ioc) {
  2269. status = -ENOMEM;
  2270. goto cleanup1;
  2271. }
  2272. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  2273. status = -EFAULT;
  2274. goto cleanup1;
  2275. }
  2276. if ((ioc->buf_size < 1) &&
  2277. (ioc->Request.Type.Direction != XFER_NONE)) {
  2278. status = -EINVAL;
  2279. goto cleanup1;
  2280. }
  2281. /* Check kmalloc limits using all SGs */
  2282. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  2283. status = -EINVAL;
  2284. goto cleanup1;
  2285. }
  2286. if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
  2287. status = -EINVAL;
  2288. goto cleanup1;
  2289. }
  2290. buff = kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
  2291. if (!buff) {
  2292. status = -ENOMEM;
  2293. goto cleanup1;
  2294. }
  2295. buff_size = kmalloc(MAXSGENTRIES * sizeof(int), GFP_KERNEL);
  2296. if (!buff_size) {
  2297. status = -ENOMEM;
  2298. goto cleanup1;
  2299. }
  2300. left = ioc->buf_size;
  2301. data_ptr = ioc->buf;
  2302. while (left) {
  2303. sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
  2304. buff_size[sg_used] = sz;
  2305. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  2306. if (buff[sg_used] == NULL) {
  2307. status = -ENOMEM;
  2308. goto cleanup1;
  2309. }
  2310. if (ioc->Request.Type.Direction == XFER_WRITE) {
  2311. if (copy_from_user(buff[sg_used], data_ptr, sz)) {
  2312. status = -ENOMEM;
  2313. goto cleanup1;
  2314. }
  2315. } else
  2316. memset(buff[sg_used], 0, sz);
  2317. left -= sz;
  2318. data_ptr += sz;
  2319. sg_used++;
  2320. }
  2321. c = cmd_special_alloc(h);
  2322. if (c == NULL) {
  2323. status = -ENOMEM;
  2324. goto cleanup1;
  2325. }
  2326. c->cmd_type = CMD_IOCTL_PEND;
  2327. c->Header.ReplyQueue = 0;
  2328. if (ioc->buf_size > 0) {
  2329. c->Header.SGList = sg_used;
  2330. c->Header.SGTotal = sg_used;
  2331. } else {
  2332. c->Header.SGList = 0;
  2333. c->Header.SGTotal = 0;
  2334. }
  2335. memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
  2336. c->Header.Tag.lower = c->busaddr;
  2337. memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
  2338. if (ioc->buf_size > 0) {
  2339. int i;
  2340. for (i = 0; i < sg_used; i++) {
  2341. temp64.val = pci_map_single(h->pdev, buff[i],
  2342. buff_size[i], PCI_DMA_BIDIRECTIONAL);
  2343. c->SG[i].Addr.lower = temp64.val32.lower;
  2344. c->SG[i].Addr.upper = temp64.val32.upper;
  2345. c->SG[i].Len = buff_size[i];
  2346. /* we are not chaining */
  2347. c->SG[i].Ext = 0;
  2348. }
  2349. }
  2350. hpsa_scsi_do_simple_cmd_core(h, c);
  2351. hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
  2352. check_ioctl_unit_attention(h, c);
  2353. /* Copy the error information out */
  2354. memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
  2355. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  2356. cmd_special_free(h, c);
  2357. status = -EFAULT;
  2358. goto cleanup1;
  2359. }
  2360. if (ioc->Request.Type.Direction == XFER_READ) {
  2361. /* Copy the data out of the buffer we created */
  2362. BYTE __user *ptr = ioc->buf;
  2363. for (i = 0; i < sg_used; i++) {
  2364. if (copy_to_user(ptr, buff[i], buff_size[i])) {
  2365. cmd_special_free(h, c);
  2366. status = -EFAULT;
  2367. goto cleanup1;
  2368. }
  2369. ptr += buff_size[i];
  2370. }
  2371. }
  2372. cmd_special_free(h, c);
  2373. status = 0;
  2374. cleanup1:
  2375. if (buff) {
  2376. for (i = 0; i < sg_used; i++)
  2377. kfree(buff[i]);
  2378. kfree(buff);
  2379. }
  2380. kfree(buff_size);
  2381. kfree(ioc);
  2382. return status;
  2383. }
  2384. static void check_ioctl_unit_attention(struct ctlr_info *h,
  2385. struct CommandList *c)
  2386. {
  2387. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  2388. c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
  2389. (void) check_for_unit_attention(h, c);
  2390. }
  2391. /*
  2392. * ioctl
  2393. */
  2394. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void *arg)
  2395. {
  2396. struct ctlr_info *h;
  2397. void __user *argp = (void __user *)arg;
  2398. h = sdev_to_hba(dev);
  2399. switch (cmd) {
  2400. case CCISS_DEREGDISK:
  2401. case CCISS_REGNEWDISK:
  2402. case CCISS_REGNEWD:
  2403. hpsa_scan_start(h->scsi_host);
  2404. return 0;
  2405. case CCISS_GETPCIINFO:
  2406. return hpsa_getpciinfo_ioctl(h, argp);
  2407. case CCISS_GETDRIVVER:
  2408. return hpsa_getdrivver_ioctl(h, argp);
  2409. case CCISS_PASSTHRU:
  2410. return hpsa_passthru_ioctl(h, argp);
  2411. case CCISS_BIG_PASSTHRU:
  2412. return hpsa_big_passthru_ioctl(h, argp);
  2413. default:
  2414. return -ENOTTY;
  2415. }
  2416. }
  2417. static void fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  2418. void *buff, size_t size, u8 page_code, unsigned char *scsi3addr,
  2419. int cmd_type)
  2420. {
  2421. int pci_dir = XFER_NONE;
  2422. c->cmd_type = CMD_IOCTL_PEND;
  2423. c->Header.ReplyQueue = 0;
  2424. if (buff != NULL && size > 0) {
  2425. c->Header.SGList = 1;
  2426. c->Header.SGTotal = 1;
  2427. } else {
  2428. c->Header.SGList = 0;
  2429. c->Header.SGTotal = 0;
  2430. }
  2431. c->Header.Tag.lower = c->busaddr;
  2432. memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
  2433. c->Request.Type.Type = cmd_type;
  2434. if (cmd_type == TYPE_CMD) {
  2435. switch (cmd) {
  2436. case HPSA_INQUIRY:
  2437. /* are we trying to read a vital product page */
  2438. if (page_code != 0) {
  2439. c->Request.CDB[1] = 0x01;
  2440. c->Request.CDB[2] = page_code;
  2441. }
  2442. c->Request.CDBLen = 6;
  2443. c->Request.Type.Attribute = ATTR_SIMPLE;
  2444. c->Request.Type.Direction = XFER_READ;
  2445. c->Request.Timeout = 0;
  2446. c->Request.CDB[0] = HPSA_INQUIRY;
  2447. c->Request.CDB[4] = size & 0xFF;
  2448. break;
  2449. case HPSA_REPORT_LOG:
  2450. case HPSA_REPORT_PHYS:
  2451. /* Talking to controller so It's a physical command
  2452. mode = 00 target = 0. Nothing to write.
  2453. */
  2454. c->Request.CDBLen = 12;
  2455. c->Request.Type.Attribute = ATTR_SIMPLE;
  2456. c->Request.Type.Direction = XFER_READ;
  2457. c->Request.Timeout = 0;
  2458. c->Request.CDB[0] = cmd;
  2459. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  2460. c->Request.CDB[7] = (size >> 16) & 0xFF;
  2461. c->Request.CDB[8] = (size >> 8) & 0xFF;
  2462. c->Request.CDB[9] = size & 0xFF;
  2463. break;
  2464. case HPSA_READ_CAPACITY:
  2465. c->Request.CDBLen = 10;
  2466. c->Request.Type.Attribute = ATTR_SIMPLE;
  2467. c->Request.Type.Direction = XFER_READ;
  2468. c->Request.Timeout = 0;
  2469. c->Request.CDB[0] = cmd;
  2470. break;
  2471. case HPSA_CACHE_FLUSH:
  2472. c->Request.CDBLen = 12;
  2473. c->Request.Type.Attribute = ATTR_SIMPLE;
  2474. c->Request.Type.Direction = XFER_WRITE;
  2475. c->Request.Timeout = 0;
  2476. c->Request.CDB[0] = BMIC_WRITE;
  2477. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  2478. break;
  2479. case TEST_UNIT_READY:
  2480. c->Request.CDBLen = 6;
  2481. c->Request.Type.Attribute = ATTR_SIMPLE;
  2482. c->Request.Type.Direction = XFER_NONE;
  2483. c->Request.Timeout = 0;
  2484. break;
  2485. default:
  2486. dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
  2487. BUG();
  2488. return;
  2489. }
  2490. } else if (cmd_type == TYPE_MSG) {
  2491. switch (cmd) {
  2492. case HPSA_DEVICE_RESET_MSG:
  2493. c->Request.CDBLen = 16;
  2494. c->Request.Type.Type = 1; /* It is a MSG not a CMD */
  2495. c->Request.Type.Attribute = ATTR_SIMPLE;
  2496. c->Request.Type.Direction = XFER_NONE;
  2497. c->Request.Timeout = 0; /* Don't time out */
  2498. c->Request.CDB[0] = 0x01; /* RESET_MSG is 0x01 */
  2499. c->Request.CDB[1] = 0x03; /* Reset target above */
  2500. /* If bytes 4-7 are zero, it means reset the */
  2501. /* LunID device */
  2502. c->Request.CDB[4] = 0x00;
  2503. c->Request.CDB[5] = 0x00;
  2504. c->Request.CDB[6] = 0x00;
  2505. c->Request.CDB[7] = 0x00;
  2506. break;
  2507. default:
  2508. dev_warn(&h->pdev->dev, "unknown message type %d\n",
  2509. cmd);
  2510. BUG();
  2511. }
  2512. } else {
  2513. dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
  2514. BUG();
  2515. }
  2516. switch (c->Request.Type.Direction) {
  2517. case XFER_READ:
  2518. pci_dir = PCI_DMA_FROMDEVICE;
  2519. break;
  2520. case XFER_WRITE:
  2521. pci_dir = PCI_DMA_TODEVICE;
  2522. break;
  2523. case XFER_NONE:
  2524. pci_dir = PCI_DMA_NONE;
  2525. break;
  2526. default:
  2527. pci_dir = PCI_DMA_BIDIRECTIONAL;
  2528. }
  2529. hpsa_map_one(h->pdev, c, buff, size, pci_dir);
  2530. return;
  2531. }
  2532. /*
  2533. * Map (physical) PCI mem into (virtual) kernel space
  2534. */
  2535. static void __iomem *remap_pci_mem(ulong base, ulong size)
  2536. {
  2537. ulong page_base = ((ulong) base) & PAGE_MASK;
  2538. ulong page_offs = ((ulong) base) - page_base;
  2539. void __iomem *page_remapped = ioremap(page_base, page_offs + size);
  2540. return page_remapped ? (page_remapped + page_offs) : NULL;
  2541. }
  2542. /* Takes cmds off the submission queue and sends them to the hardware,
  2543. * then puts them on the queue of cmds waiting for completion.
  2544. */
  2545. static void start_io(struct ctlr_info *h)
  2546. {
  2547. struct CommandList *c;
  2548. while (!hlist_empty(&h->reqQ)) {
  2549. c = hlist_entry(h->reqQ.first, struct CommandList, list);
  2550. /* can't do anything if fifo is full */
  2551. if ((h->access.fifo_full(h))) {
  2552. dev_warn(&h->pdev->dev, "fifo full\n");
  2553. break;
  2554. }
  2555. /* Get the first entry from the Request Q */
  2556. removeQ(c);
  2557. h->Qdepth--;
  2558. /* Tell the controller execute command */
  2559. h->access.submit_command(h, c);
  2560. /* Put job onto the completed Q */
  2561. addQ(&h->cmpQ, c);
  2562. }
  2563. }
  2564. static inline unsigned long get_next_completion(struct ctlr_info *h)
  2565. {
  2566. return h->access.command_completed(h);
  2567. }
  2568. static inline bool interrupt_pending(struct ctlr_info *h)
  2569. {
  2570. return h->access.intr_pending(h);
  2571. }
  2572. static inline long interrupt_not_for_us(struct ctlr_info *h)
  2573. {
  2574. return !(h->msi_vector || h->msix_vector) &&
  2575. ((h->access.intr_pending(h) == 0) ||
  2576. (h->interrupts_enabled == 0));
  2577. }
  2578. static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
  2579. u32 raw_tag)
  2580. {
  2581. if (unlikely(tag_index >= h->nr_cmds)) {
  2582. dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
  2583. return 1;
  2584. }
  2585. return 0;
  2586. }
  2587. static inline void finish_cmd(struct CommandList *c, u32 raw_tag)
  2588. {
  2589. removeQ(c);
  2590. if (likely(c->cmd_type == CMD_SCSI))
  2591. complete_scsi_command(c, 0, raw_tag);
  2592. else if (c->cmd_type == CMD_IOCTL_PEND)
  2593. complete(c->waiting);
  2594. }
  2595. static inline u32 hpsa_tag_contains_index(u32 tag)
  2596. {
  2597. #define DIRECT_LOOKUP_BIT 0x10
  2598. return tag & DIRECT_LOOKUP_BIT;
  2599. }
  2600. static inline u32 hpsa_tag_to_index(u32 tag)
  2601. {
  2602. #define DIRECT_LOOKUP_SHIFT 5
  2603. return tag >> DIRECT_LOOKUP_SHIFT;
  2604. }
  2605. static inline u32 hpsa_tag_discard_error_bits(u32 tag)
  2606. {
  2607. #define HPSA_ERROR_BITS 0x03
  2608. return tag & ~HPSA_ERROR_BITS;
  2609. }
  2610. /* process completion of an indexed ("direct lookup") command */
  2611. static inline u32 process_indexed_cmd(struct ctlr_info *h,
  2612. u32 raw_tag)
  2613. {
  2614. u32 tag_index;
  2615. struct CommandList *c;
  2616. tag_index = hpsa_tag_to_index(raw_tag);
  2617. if (bad_tag(h, tag_index, raw_tag))
  2618. return next_command(h);
  2619. c = h->cmd_pool + tag_index;
  2620. finish_cmd(c, raw_tag);
  2621. return next_command(h);
  2622. }
  2623. /* process completion of a non-indexed command */
  2624. static inline u32 process_nonindexed_cmd(struct ctlr_info *h,
  2625. u32 raw_tag)
  2626. {
  2627. u32 tag;
  2628. struct CommandList *c = NULL;
  2629. struct hlist_node *tmp;
  2630. tag = hpsa_tag_discard_error_bits(raw_tag);
  2631. hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
  2632. if ((c->busaddr & 0xFFFFFFE0) == (tag & 0xFFFFFFE0)) {
  2633. finish_cmd(c, raw_tag);
  2634. return next_command(h);
  2635. }
  2636. }
  2637. bad_tag(h, h->nr_cmds + 1, raw_tag);
  2638. return next_command(h);
  2639. }
  2640. static irqreturn_t do_hpsa_intr(int irq, void *dev_id)
  2641. {
  2642. struct ctlr_info *h = dev_id;
  2643. unsigned long flags;
  2644. u32 raw_tag;
  2645. if (interrupt_not_for_us(h))
  2646. return IRQ_NONE;
  2647. spin_lock_irqsave(&h->lock, flags);
  2648. raw_tag = get_next_completion(h);
  2649. while (raw_tag != FIFO_EMPTY) {
  2650. if (hpsa_tag_contains_index(raw_tag))
  2651. raw_tag = process_indexed_cmd(h, raw_tag);
  2652. else
  2653. raw_tag = process_nonindexed_cmd(h, raw_tag);
  2654. }
  2655. spin_unlock_irqrestore(&h->lock, flags);
  2656. return IRQ_HANDLED;
  2657. }
  2658. /* Send a message CDB to the firmwart. */
  2659. static __devinit int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
  2660. unsigned char type)
  2661. {
  2662. struct Command {
  2663. struct CommandListHeader CommandHeader;
  2664. struct RequestBlock Request;
  2665. struct ErrDescriptor ErrorDescriptor;
  2666. };
  2667. struct Command *cmd;
  2668. static const size_t cmd_sz = sizeof(*cmd) +
  2669. sizeof(cmd->ErrorDescriptor);
  2670. dma_addr_t paddr64;
  2671. uint32_t paddr32, tag;
  2672. void __iomem *vaddr;
  2673. int i, err;
  2674. vaddr = pci_ioremap_bar(pdev, 0);
  2675. if (vaddr == NULL)
  2676. return -ENOMEM;
  2677. /* The Inbound Post Queue only accepts 32-bit physical addresses for the
  2678. * CCISS commands, so they must be allocated from the lower 4GiB of
  2679. * memory.
  2680. */
  2681. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  2682. if (err) {
  2683. iounmap(vaddr);
  2684. return -ENOMEM;
  2685. }
  2686. cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
  2687. if (cmd == NULL) {
  2688. iounmap(vaddr);
  2689. return -ENOMEM;
  2690. }
  2691. /* This must fit, because of the 32-bit consistent DMA mask. Also,
  2692. * although there's no guarantee, we assume that the address is at
  2693. * least 4-byte aligned (most likely, it's page-aligned).
  2694. */
  2695. paddr32 = paddr64;
  2696. cmd->CommandHeader.ReplyQueue = 0;
  2697. cmd->CommandHeader.SGList = 0;
  2698. cmd->CommandHeader.SGTotal = 0;
  2699. cmd->CommandHeader.Tag.lower = paddr32;
  2700. cmd->CommandHeader.Tag.upper = 0;
  2701. memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
  2702. cmd->Request.CDBLen = 16;
  2703. cmd->Request.Type.Type = TYPE_MSG;
  2704. cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
  2705. cmd->Request.Type.Direction = XFER_NONE;
  2706. cmd->Request.Timeout = 0; /* Don't time out */
  2707. cmd->Request.CDB[0] = opcode;
  2708. cmd->Request.CDB[1] = type;
  2709. memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
  2710. cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(*cmd);
  2711. cmd->ErrorDescriptor.Addr.upper = 0;
  2712. cmd->ErrorDescriptor.Len = sizeof(struct ErrorInfo);
  2713. writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
  2714. for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
  2715. tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
  2716. if (hpsa_tag_discard_error_bits(tag) == paddr32)
  2717. break;
  2718. msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
  2719. }
  2720. iounmap(vaddr);
  2721. /* we leak the DMA buffer here ... no choice since the controller could
  2722. * still complete the command.
  2723. */
  2724. if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
  2725. dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
  2726. opcode, type);
  2727. return -ETIMEDOUT;
  2728. }
  2729. pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
  2730. if (tag & HPSA_ERROR_BIT) {
  2731. dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
  2732. opcode, type);
  2733. return -EIO;
  2734. }
  2735. dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
  2736. opcode, type);
  2737. return 0;
  2738. }
  2739. #define hpsa_soft_reset_controller(p) hpsa_message(p, 1, 0)
  2740. #define hpsa_noop(p) hpsa_message(p, 3, 0)
  2741. static __devinit int hpsa_reset_msi(struct pci_dev *pdev)
  2742. {
  2743. /* the #defines are stolen from drivers/pci/msi.h. */
  2744. #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
  2745. #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
  2746. int pos;
  2747. u16 control = 0;
  2748. pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
  2749. if (pos) {
  2750. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  2751. if (control & PCI_MSI_FLAGS_ENABLE) {
  2752. dev_info(&pdev->dev, "resetting MSI\n");
  2753. pci_write_config_word(pdev, msi_control_reg(pos),
  2754. control & ~PCI_MSI_FLAGS_ENABLE);
  2755. }
  2756. }
  2757. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  2758. if (pos) {
  2759. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  2760. if (control & PCI_MSIX_FLAGS_ENABLE) {
  2761. dev_info(&pdev->dev, "resetting MSI-X\n");
  2762. pci_write_config_word(pdev, msi_control_reg(pos),
  2763. control & ~PCI_MSIX_FLAGS_ENABLE);
  2764. }
  2765. }
  2766. return 0;
  2767. }
  2768. /* This does a hard reset of the controller using PCI power management
  2769. * states.
  2770. */
  2771. static __devinit int hpsa_hard_reset_controller(struct pci_dev *pdev)
  2772. {
  2773. u16 pmcsr, saved_config_space[32];
  2774. int i, pos;
  2775. dev_info(&pdev->dev, "using PCI PM to reset controller\n");
  2776. /* This is very nearly the same thing as
  2777. *
  2778. * pci_save_state(pci_dev);
  2779. * pci_set_power_state(pci_dev, PCI_D3hot);
  2780. * pci_set_power_state(pci_dev, PCI_D0);
  2781. * pci_restore_state(pci_dev);
  2782. *
  2783. * but we can't use these nice canned kernel routines on
  2784. * kexec, because they also check the MSI/MSI-X state in PCI
  2785. * configuration space and do the wrong thing when it is
  2786. * set/cleared. Also, the pci_save/restore_state functions
  2787. * violate the ordering requirements for restoring the
  2788. * configuration space from the CCISS document (see the
  2789. * comment below). So we roll our own ....
  2790. */
  2791. for (i = 0; i < 32; i++)
  2792. pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
  2793. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  2794. if (pos == 0) {
  2795. dev_err(&pdev->dev,
  2796. "hpsa_reset_controller: PCI PM not supported\n");
  2797. return -ENODEV;
  2798. }
  2799. /* Quoting from the Open CISS Specification: "The Power
  2800. * Management Control/Status Register (CSR) controls the power
  2801. * state of the device. The normal operating state is D0,
  2802. * CSR=00h. The software off state is D3, CSR=03h. To reset
  2803. * the controller, place the interface device in D3 then to
  2804. * D0, this causes a secondary PCI reset which will reset the
  2805. * controller."
  2806. */
  2807. /* enter the D3hot power management state */
  2808. pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
  2809. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2810. pmcsr |= PCI_D3hot;
  2811. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  2812. msleep(500);
  2813. /* enter the D0 power management state */
  2814. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  2815. pmcsr |= PCI_D0;
  2816. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  2817. msleep(500);
  2818. /* Restore the PCI configuration space. The Open CISS
  2819. * Specification says, "Restore the PCI Configuration
  2820. * Registers, offsets 00h through 60h. It is important to
  2821. * restore the command register, 16-bits at offset 04h,
  2822. * last. Do not restore the configuration status register,
  2823. * 16-bits at offset 06h." Note that the offset is 2*i.
  2824. */
  2825. for (i = 0; i < 32; i++) {
  2826. if (i == 2 || i == 3)
  2827. continue;
  2828. pci_write_config_word(pdev, 2*i, saved_config_space[i]);
  2829. }
  2830. wmb();
  2831. pci_write_config_word(pdev, 4, saved_config_space[2]);
  2832. return 0;
  2833. }
  2834. /*
  2835. * We cannot read the structure directly, for portability we must use
  2836. * the io functions.
  2837. * This is for debug only.
  2838. */
  2839. #ifdef HPSA_DEBUG
  2840. static void print_cfg_table(struct device *dev, struct CfgTable *tb)
  2841. {
  2842. int i;
  2843. char temp_name[17];
  2844. dev_info(dev, "Controller Configuration information\n");
  2845. dev_info(dev, "------------------------------------\n");
  2846. for (i = 0; i < 4; i++)
  2847. temp_name[i] = readb(&(tb->Signature[i]));
  2848. temp_name[4] = '\0';
  2849. dev_info(dev, " Signature = %s\n", temp_name);
  2850. dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
  2851. dev_info(dev, " Transport methods supported = 0x%x\n",
  2852. readl(&(tb->TransportSupport)));
  2853. dev_info(dev, " Transport methods active = 0x%x\n",
  2854. readl(&(tb->TransportActive)));
  2855. dev_info(dev, " Requested transport Method = 0x%x\n",
  2856. readl(&(tb->HostWrite.TransportRequest)));
  2857. dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
  2858. readl(&(tb->HostWrite.CoalIntDelay)));
  2859. dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
  2860. readl(&(tb->HostWrite.CoalIntCount)));
  2861. dev_info(dev, " Max outstanding commands = 0x%d\n",
  2862. readl(&(tb->CmdsOutMax)));
  2863. dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
  2864. for (i = 0; i < 16; i++)
  2865. temp_name[i] = readb(&(tb->ServerName[i]));
  2866. temp_name[16] = '\0';
  2867. dev_info(dev, " Server Name = %s\n", temp_name);
  2868. dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
  2869. readl(&(tb->HeartBeat)));
  2870. }
  2871. #endif /* HPSA_DEBUG */
  2872. static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
  2873. {
  2874. int i, offset, mem_type, bar_type;
  2875. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  2876. return 0;
  2877. offset = 0;
  2878. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  2879. bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
  2880. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  2881. offset += 4;
  2882. else {
  2883. mem_type = pci_resource_flags(pdev, i) &
  2884. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  2885. switch (mem_type) {
  2886. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  2887. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  2888. offset += 4; /* 32 bit */
  2889. break;
  2890. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  2891. offset += 8;
  2892. break;
  2893. default: /* reserved in PCI 2.2 */
  2894. dev_warn(&pdev->dev,
  2895. "base address is invalid\n");
  2896. return -1;
  2897. break;
  2898. }
  2899. }
  2900. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  2901. return i + 1;
  2902. }
  2903. return -1;
  2904. }
  2905. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  2906. * controllers that are capable. If not, we use IO-APIC mode.
  2907. */
  2908. static void __devinit hpsa_interrupt_mode(struct ctlr_info *h,
  2909. struct pci_dev *pdev, u32 board_id)
  2910. {
  2911. #ifdef CONFIG_PCI_MSI
  2912. int err;
  2913. struct msix_entry hpsa_msix_entries[4] = { {0, 0}, {0, 1},
  2914. {0, 2}, {0, 3}
  2915. };
  2916. /* Some boards advertise MSI but don't really support it */
  2917. if ((board_id == 0x40700E11) ||
  2918. (board_id == 0x40800E11) ||
  2919. (board_id == 0x40820E11) || (board_id == 0x40830E11))
  2920. goto default_int_mode;
  2921. if (pci_find_capability(pdev, PCI_CAP_ID_MSIX)) {
  2922. dev_info(&pdev->dev, "MSIX\n");
  2923. err = pci_enable_msix(pdev, hpsa_msix_entries, 4);
  2924. if (!err) {
  2925. h->intr[0] = hpsa_msix_entries[0].vector;
  2926. h->intr[1] = hpsa_msix_entries[1].vector;
  2927. h->intr[2] = hpsa_msix_entries[2].vector;
  2928. h->intr[3] = hpsa_msix_entries[3].vector;
  2929. h->msix_vector = 1;
  2930. return;
  2931. }
  2932. if (err > 0) {
  2933. dev_warn(&pdev->dev, "only %d MSI-X vectors "
  2934. "available\n", err);
  2935. goto default_int_mode;
  2936. } else {
  2937. dev_warn(&pdev->dev, "MSI-X init failed %d\n",
  2938. err);
  2939. goto default_int_mode;
  2940. }
  2941. }
  2942. if (pci_find_capability(pdev, PCI_CAP_ID_MSI)) {
  2943. dev_info(&pdev->dev, "MSI\n");
  2944. if (!pci_enable_msi(pdev))
  2945. h->msi_vector = 1;
  2946. else
  2947. dev_warn(&pdev->dev, "MSI init failed\n");
  2948. }
  2949. default_int_mode:
  2950. #endif /* CONFIG_PCI_MSI */
  2951. /* if we get here we're going to use the default interrupt mode */
  2952. h->intr[PERF_MODE_INT] = pdev->irq;
  2953. }
  2954. static int hpsa_pci_init(struct ctlr_info *h, struct pci_dev *pdev)
  2955. {
  2956. ushort subsystem_vendor_id, subsystem_device_id, command;
  2957. u32 board_id, scratchpad = 0;
  2958. u64 cfg_offset;
  2959. u32 cfg_base_addr;
  2960. u64 cfg_base_addr_index;
  2961. u32 trans_offset;
  2962. int i, prod_index, err;
  2963. subsystem_vendor_id = pdev->subsystem_vendor;
  2964. subsystem_device_id = pdev->subsystem_device;
  2965. board_id = (((u32) (subsystem_device_id << 16) & 0xffff0000) |
  2966. subsystem_vendor_id);
  2967. for (i = 0; i < ARRAY_SIZE(products); i++)
  2968. if (board_id == products[i].board_id)
  2969. break;
  2970. prod_index = i;
  2971. if (prod_index == ARRAY_SIZE(products)) {
  2972. prod_index--;
  2973. if (subsystem_vendor_id != PCI_VENDOR_ID_HP ||
  2974. !hpsa_allow_any) {
  2975. dev_warn(&pdev->dev, "unrecognized board ID:"
  2976. " 0x%08lx, ignoring.\n",
  2977. (unsigned long) board_id);
  2978. return -ENODEV;
  2979. }
  2980. }
  2981. /* check to see if controller has been disabled
  2982. * BEFORE trying to enable it
  2983. */
  2984. (void)pci_read_config_word(pdev, PCI_COMMAND, &command);
  2985. if (!(command & 0x02)) {
  2986. dev_warn(&pdev->dev, "controller appears to be disabled\n");
  2987. return -ENODEV;
  2988. }
  2989. err = pci_enable_device(pdev);
  2990. if (err) {
  2991. dev_warn(&pdev->dev, "unable to enable PCI device\n");
  2992. return err;
  2993. }
  2994. err = pci_request_regions(pdev, "hpsa");
  2995. if (err) {
  2996. dev_err(&pdev->dev, "cannot obtain PCI resources, aborting\n");
  2997. return err;
  2998. }
  2999. /* If the kernel supports MSI/MSI-X we will try to enable that,
  3000. * else we use the IO-APIC interrupt assigned to us by system ROM.
  3001. */
  3002. hpsa_interrupt_mode(h, pdev, board_id);
  3003. /* find the memory BAR */
  3004. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  3005. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM)
  3006. break;
  3007. }
  3008. if (i == DEVICE_COUNT_RESOURCE) {
  3009. dev_warn(&pdev->dev, "no memory BAR found\n");
  3010. err = -ENODEV;
  3011. goto err_out_free_res;
  3012. }
  3013. h->paddr = pci_resource_start(pdev, i); /* addressing mode bits
  3014. * already removed
  3015. */
  3016. h->vaddr = remap_pci_mem(h->paddr, 0x250);
  3017. /* Wait for the board to become ready. */
  3018. for (i = 0; i < HPSA_BOARD_READY_ITERATIONS; i++) {
  3019. scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
  3020. if (scratchpad == HPSA_FIRMWARE_READY)
  3021. break;
  3022. msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
  3023. }
  3024. if (scratchpad != HPSA_FIRMWARE_READY) {
  3025. dev_warn(&pdev->dev, "board not ready, timed out.\n");
  3026. err = -ENODEV;
  3027. goto err_out_free_res;
  3028. }
  3029. /* get the address index number */
  3030. cfg_base_addr = readl(h->vaddr + SA5_CTCFG_OFFSET);
  3031. cfg_base_addr &= (u32) 0x0000ffff;
  3032. cfg_base_addr_index = find_PCI_BAR_index(pdev, cfg_base_addr);
  3033. if (cfg_base_addr_index == -1) {
  3034. dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
  3035. err = -ENODEV;
  3036. goto err_out_free_res;
  3037. }
  3038. cfg_offset = readl(h->vaddr + SA5_CTMEM_OFFSET);
  3039. h->cfgtable = remap_pci_mem(pci_resource_start(pdev,
  3040. cfg_base_addr_index) + cfg_offset,
  3041. sizeof(h->cfgtable));
  3042. /* Find performant mode table. */
  3043. trans_offset = readl(&(h->cfgtable->TransMethodOffset));
  3044. h->transtable = remap_pci_mem(pci_resource_start(pdev,
  3045. cfg_base_addr_index)+cfg_offset+trans_offset,
  3046. sizeof(*h->transtable));
  3047. h->board_id = board_id;
  3048. h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
  3049. h->product_name = products[prod_index].product_name;
  3050. h->access = *(products[prod_index].access);
  3051. /* Allow room for some ioctls */
  3052. h->nr_cmds = h->max_commands - 4;
  3053. if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
  3054. (readb(&h->cfgtable->Signature[1]) != 'I') ||
  3055. (readb(&h->cfgtable->Signature[2]) != 'S') ||
  3056. (readb(&h->cfgtable->Signature[3]) != 'S')) {
  3057. dev_warn(&pdev->dev, "not a valid CISS config table\n");
  3058. err = -ENODEV;
  3059. goto err_out_free_res;
  3060. }
  3061. #ifdef CONFIG_X86
  3062. {
  3063. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  3064. u32 prefetch;
  3065. prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
  3066. prefetch |= 0x100;
  3067. writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
  3068. }
  3069. #endif
  3070. /* Disabling DMA prefetch for the P600
  3071. * An ASIC bug may result in a prefetch beyond
  3072. * physical memory.
  3073. */
  3074. if (board_id == 0x3225103C) {
  3075. u32 dma_prefetch;
  3076. dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
  3077. dma_prefetch |= 0x8000;
  3078. writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
  3079. }
  3080. h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
  3081. /* Update the field, and then ring the doorbell */
  3082. writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
  3083. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  3084. /* under certain very rare conditions, this can take awhile.
  3085. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  3086. * as we enter this code.)
  3087. */
  3088. for (i = 0; i < MAX_CONFIG_WAIT; i++) {
  3089. if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  3090. break;
  3091. /* delay and try again */
  3092. msleep(10);
  3093. }
  3094. #ifdef HPSA_DEBUG
  3095. print_cfg_table(&pdev->dev, h->cfgtable);
  3096. #endif /* HPSA_DEBUG */
  3097. if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple)) {
  3098. dev_warn(&pdev->dev, "unable to get board into simple mode\n");
  3099. err = -ENODEV;
  3100. goto err_out_free_res;
  3101. }
  3102. return 0;
  3103. err_out_free_res:
  3104. /*
  3105. * Deliberately omit pci_disable_device(): it does something nasty to
  3106. * Smart Array controllers that pci_enable_device does not undo
  3107. */
  3108. pci_release_regions(pdev);
  3109. return err;
  3110. }
  3111. static void __devinit hpsa_hba_inquiry(struct ctlr_info *h)
  3112. {
  3113. int rc;
  3114. #define HBA_INQUIRY_BYTE_COUNT 64
  3115. h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
  3116. if (!h->hba_inquiry_data)
  3117. return;
  3118. rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
  3119. h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
  3120. if (rc != 0) {
  3121. kfree(h->hba_inquiry_data);
  3122. h->hba_inquiry_data = NULL;
  3123. }
  3124. }
  3125. static int __devinit hpsa_init_one(struct pci_dev *pdev,
  3126. const struct pci_device_id *ent)
  3127. {
  3128. int i, rc;
  3129. int dac;
  3130. struct ctlr_info *h;
  3131. if (number_of_controllers == 0)
  3132. printk(KERN_INFO DRIVER_NAME "\n");
  3133. if (reset_devices) {
  3134. /* Reset the controller with a PCI power-cycle */
  3135. if (hpsa_hard_reset_controller(pdev) || hpsa_reset_msi(pdev))
  3136. return -ENODEV;
  3137. /* Some devices (notably the HP Smart Array 5i Controller)
  3138. need a little pause here */
  3139. msleep(HPSA_POST_RESET_PAUSE_MSECS);
  3140. /* Now try to get the controller to respond to a no-op */
  3141. for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
  3142. if (hpsa_noop(pdev) == 0)
  3143. break;
  3144. else
  3145. dev_warn(&pdev->dev, "no-op failed%s\n",
  3146. (i < 11 ? "; re-trying" : ""));
  3147. }
  3148. }
  3149. /* Command structures must be aligned on a 32-byte boundary because
  3150. * the 5 lower bits of the address are used by the hardware. and by
  3151. * the driver. See comments in hpsa.h for more info.
  3152. */
  3153. #define COMMANDLIST_ALIGNMENT 32
  3154. BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
  3155. h = kzalloc(sizeof(*h), GFP_KERNEL);
  3156. if (!h)
  3157. return -ENOMEM;
  3158. h->busy_initializing = 1;
  3159. INIT_HLIST_HEAD(&h->cmpQ);
  3160. INIT_HLIST_HEAD(&h->reqQ);
  3161. mutex_init(&h->busy_shutting_down);
  3162. init_completion(&h->scan_wait);
  3163. rc = hpsa_pci_init(h, pdev);
  3164. if (rc != 0)
  3165. goto clean1;
  3166. sprintf(h->devname, "hpsa%d", number_of_controllers);
  3167. h->ctlr = number_of_controllers;
  3168. number_of_controllers++;
  3169. h->pdev = pdev;
  3170. /* configure PCI DMA stuff */
  3171. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  3172. if (rc == 0) {
  3173. dac = 1;
  3174. } else {
  3175. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3176. if (rc == 0) {
  3177. dac = 0;
  3178. } else {
  3179. dev_err(&pdev->dev, "no suitable DMA available\n");
  3180. goto clean1;
  3181. }
  3182. }
  3183. /* make sure the board interrupts are off */
  3184. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  3185. rc = request_irq(h->intr[PERF_MODE_INT], do_hpsa_intr,
  3186. IRQF_DISABLED, h->devname, h);
  3187. if (rc) {
  3188. dev_err(&pdev->dev, "unable to get irq %d for %s\n",
  3189. h->intr[PERF_MODE_INT], h->devname);
  3190. goto clean2;
  3191. }
  3192. dev_info(&pdev->dev, "%s: <0x%x> at IRQ %d%s using DAC\n",
  3193. h->devname, pdev->device,
  3194. h->intr[PERF_MODE_INT], dac ? "" : " not");
  3195. h->cmd_pool_bits =
  3196. kmalloc(((h->nr_cmds + BITS_PER_LONG -
  3197. 1) / BITS_PER_LONG) * sizeof(unsigned long), GFP_KERNEL);
  3198. h->cmd_pool = pci_alloc_consistent(h->pdev,
  3199. h->nr_cmds * sizeof(*h->cmd_pool),
  3200. &(h->cmd_pool_dhandle));
  3201. h->errinfo_pool = pci_alloc_consistent(h->pdev,
  3202. h->nr_cmds * sizeof(*h->errinfo_pool),
  3203. &(h->errinfo_pool_dhandle));
  3204. if ((h->cmd_pool_bits == NULL)
  3205. || (h->cmd_pool == NULL)
  3206. || (h->errinfo_pool == NULL)) {
  3207. dev_err(&pdev->dev, "out of memory");
  3208. rc = -ENOMEM;
  3209. goto clean4;
  3210. }
  3211. spin_lock_init(&h->lock);
  3212. spin_lock_init(&h->scan_lock);
  3213. init_waitqueue_head(&h->scan_wait_queue);
  3214. h->scan_finished = 1; /* no scan currently in progress */
  3215. pci_set_drvdata(pdev, h);
  3216. memset(h->cmd_pool_bits, 0,
  3217. ((h->nr_cmds + BITS_PER_LONG -
  3218. 1) / BITS_PER_LONG) * sizeof(unsigned long));
  3219. hpsa_scsi_setup(h);
  3220. /* Turn the interrupts on so we can service requests */
  3221. h->access.set_intr_mask(h, HPSA_INTR_ON);
  3222. hpsa_put_ctlr_into_performant_mode(h);
  3223. hpsa_hba_inquiry(h);
  3224. hpsa_register_scsi(h); /* hook ourselves into SCSI subsystem */
  3225. h->busy_initializing = 0;
  3226. return 1;
  3227. clean4:
  3228. kfree(h->cmd_pool_bits);
  3229. if (h->cmd_pool)
  3230. pci_free_consistent(h->pdev,
  3231. h->nr_cmds * sizeof(struct CommandList),
  3232. h->cmd_pool, h->cmd_pool_dhandle);
  3233. if (h->errinfo_pool)
  3234. pci_free_consistent(h->pdev,
  3235. h->nr_cmds * sizeof(struct ErrorInfo),
  3236. h->errinfo_pool,
  3237. h->errinfo_pool_dhandle);
  3238. free_irq(h->intr[PERF_MODE_INT], h);
  3239. clean2:
  3240. clean1:
  3241. h->busy_initializing = 0;
  3242. kfree(h);
  3243. return rc;
  3244. }
  3245. static void hpsa_flush_cache(struct ctlr_info *h)
  3246. {
  3247. char *flush_buf;
  3248. struct CommandList *c;
  3249. flush_buf = kzalloc(4, GFP_KERNEL);
  3250. if (!flush_buf)
  3251. return;
  3252. c = cmd_special_alloc(h);
  3253. if (!c) {
  3254. dev_warn(&h->pdev->dev, "cmd_special_alloc returned NULL!\n");
  3255. goto out_of_memory;
  3256. }
  3257. fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
  3258. RAID_CTLR_LUNID, TYPE_CMD);
  3259. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_TODEVICE);
  3260. if (c->err_info->CommandStatus != 0)
  3261. dev_warn(&h->pdev->dev,
  3262. "error flushing cache on controller\n");
  3263. cmd_special_free(h, c);
  3264. out_of_memory:
  3265. kfree(flush_buf);
  3266. }
  3267. static void hpsa_shutdown(struct pci_dev *pdev)
  3268. {
  3269. struct ctlr_info *h;
  3270. h = pci_get_drvdata(pdev);
  3271. /* Turn board interrupts off and send the flush cache command
  3272. * sendcmd will turn off interrupt, and send the flush...
  3273. * To write all data in the battery backed cache to disks
  3274. */
  3275. hpsa_flush_cache(h);
  3276. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  3277. free_irq(h->intr[PERF_MODE_INT], h);
  3278. #ifdef CONFIG_PCI_MSI
  3279. if (h->msix_vector)
  3280. pci_disable_msix(h->pdev);
  3281. else if (h->msi_vector)
  3282. pci_disable_msi(h->pdev);
  3283. #endif /* CONFIG_PCI_MSI */
  3284. }
  3285. static void __devexit hpsa_remove_one(struct pci_dev *pdev)
  3286. {
  3287. struct ctlr_info *h;
  3288. if (pci_get_drvdata(pdev) == NULL) {
  3289. dev_err(&pdev->dev, "unable to remove device \n");
  3290. return;
  3291. }
  3292. h = pci_get_drvdata(pdev);
  3293. mutex_lock(&h->busy_shutting_down);
  3294. remove_from_scan_list(h);
  3295. hpsa_unregister_scsi(h); /* unhook from SCSI subsystem */
  3296. hpsa_shutdown(pdev);
  3297. iounmap(h->vaddr);
  3298. pci_free_consistent(h->pdev,
  3299. h->nr_cmds * sizeof(struct CommandList),
  3300. h->cmd_pool, h->cmd_pool_dhandle);
  3301. pci_free_consistent(h->pdev,
  3302. h->nr_cmds * sizeof(struct ErrorInfo),
  3303. h->errinfo_pool, h->errinfo_pool_dhandle);
  3304. pci_free_consistent(h->pdev, h->reply_pool_size,
  3305. h->reply_pool, h->reply_pool_dhandle);
  3306. kfree(h->cmd_pool_bits);
  3307. kfree(h->blockFetchTable);
  3308. kfree(h->hba_inquiry_data);
  3309. /*
  3310. * Deliberately omit pci_disable_device(): it does something nasty to
  3311. * Smart Array controllers that pci_enable_device does not undo
  3312. */
  3313. pci_release_regions(pdev);
  3314. pci_set_drvdata(pdev, NULL);
  3315. mutex_unlock(&h->busy_shutting_down);
  3316. kfree(h);
  3317. }
  3318. static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
  3319. __attribute__((unused)) pm_message_t state)
  3320. {
  3321. return -ENOSYS;
  3322. }
  3323. static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
  3324. {
  3325. return -ENOSYS;
  3326. }
  3327. static struct pci_driver hpsa_pci_driver = {
  3328. .name = "hpsa",
  3329. .probe = hpsa_init_one,
  3330. .remove = __devexit_p(hpsa_remove_one),
  3331. .id_table = hpsa_pci_device_id, /* id_table */
  3332. .shutdown = hpsa_shutdown,
  3333. .suspend = hpsa_suspend,
  3334. .resume = hpsa_resume,
  3335. };
  3336. /* Fill in bucket_map[], given nsgs (the max number of
  3337. * scatter gather elements supported) and bucket[],
  3338. * which is an array of 8 integers. The bucket[] array
  3339. * contains 8 different DMA transfer sizes (in 16
  3340. * byte increments) which the controller uses to fetch
  3341. * commands. This function fills in bucket_map[], which
  3342. * maps a given number of scatter gather elements to one of
  3343. * the 8 DMA transfer sizes. The point of it is to allow the
  3344. * controller to only do as much DMA as needed to fetch the
  3345. * command, with the DMA transfer size encoded in the lower
  3346. * bits of the command address.
  3347. */
  3348. static void calc_bucket_map(int bucket[], int num_buckets,
  3349. int nsgs, int *bucket_map)
  3350. {
  3351. int i, j, b, size;
  3352. /* even a command with 0 SGs requires 4 blocks */
  3353. #define MINIMUM_TRANSFER_BLOCKS 4
  3354. #define NUM_BUCKETS 8
  3355. /* Note, bucket_map must have nsgs+1 entries. */
  3356. for (i = 0; i <= nsgs; i++) {
  3357. /* Compute size of a command with i SG entries */
  3358. size = i + MINIMUM_TRANSFER_BLOCKS;
  3359. b = num_buckets; /* Assume the biggest bucket */
  3360. /* Find the bucket that is just big enough */
  3361. for (j = 0; j < 8; j++) {
  3362. if (bucket[j] >= size) {
  3363. b = j;
  3364. break;
  3365. }
  3366. }
  3367. /* for a command with i SG entries, use bucket b. */
  3368. bucket_map[i] = b;
  3369. }
  3370. }
  3371. static void hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
  3372. {
  3373. u32 trans_support;
  3374. u64 trans_offset;
  3375. /* 5 = 1 s/g entry or 4k
  3376. * 6 = 2 s/g entry or 8k
  3377. * 8 = 4 s/g entry or 16k
  3378. * 10 = 6 s/g entry or 24k
  3379. */
  3380. int bft[8] = {5, 6, 8, 10, 12, 20, 28, 35}; /* for scatter/gathers */
  3381. int i = 0;
  3382. int l = 0;
  3383. unsigned long register_value;
  3384. trans_support = readl(&(h->cfgtable->TransportSupport));
  3385. if (!(trans_support & PERFORMANT_MODE))
  3386. return;
  3387. h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
  3388. h->max_sg_entries = 32;
  3389. /* Performant mode ring buffer and supporting data structures */
  3390. h->reply_pool_size = h->max_commands * sizeof(u64);
  3391. h->reply_pool = pci_alloc_consistent(h->pdev, h->reply_pool_size,
  3392. &(h->reply_pool_dhandle));
  3393. /* Need a block fetch table for performant mode */
  3394. h->blockFetchTable = kmalloc(((h->max_sg_entries+1) *
  3395. sizeof(u32)), GFP_KERNEL);
  3396. if ((h->reply_pool == NULL)
  3397. || (h->blockFetchTable == NULL))
  3398. goto clean_up;
  3399. h->reply_pool_wraparound = 1; /* spec: init to 1 */
  3400. /* Controller spec: zero out this buffer. */
  3401. memset(h->reply_pool, 0, h->reply_pool_size);
  3402. h->reply_pool_head = h->reply_pool;
  3403. trans_offset = readl(&(h->cfgtable->TransMethodOffset));
  3404. bft[7] = h->max_sg_entries + 4;
  3405. calc_bucket_map(bft, ARRAY_SIZE(bft), 32, h->blockFetchTable);
  3406. for (i = 0; i < 8; i++)
  3407. writel(bft[i], &h->transtable->BlockFetch[i]);
  3408. /* size of controller ring buffer */
  3409. writel(h->max_commands, &h->transtable->RepQSize);
  3410. writel(1, &h->transtable->RepQCount);
  3411. writel(0, &h->transtable->RepQCtrAddrLow32);
  3412. writel(0, &h->transtable->RepQCtrAddrHigh32);
  3413. writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
  3414. writel(0, &h->transtable->RepQAddr0High32);
  3415. writel(CFGTBL_Trans_Performant,
  3416. &(h->cfgtable->HostWrite.TransportRequest));
  3417. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  3418. /* under certain very rare conditions, this can take awhile.
  3419. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  3420. * as we enter this code.) */
  3421. for (l = 0; l < MAX_CONFIG_WAIT; l++) {
  3422. register_value = readl(h->vaddr + SA5_DOORBELL);
  3423. if (!(register_value & CFGTBL_ChangeReq))
  3424. break;
  3425. /* delay and try again */
  3426. set_current_state(TASK_INTERRUPTIBLE);
  3427. schedule_timeout(10);
  3428. }
  3429. register_value = readl(&(h->cfgtable->TransportActive));
  3430. if (!(register_value & CFGTBL_Trans_Performant)) {
  3431. dev_warn(&h->pdev->dev, "unable to get board into"
  3432. " performant mode\n");
  3433. return;
  3434. }
  3435. /* Change the access methods to the performant access methods */
  3436. h->access = SA5_performant_access;
  3437. h->transMethod = CFGTBL_Trans_Performant;
  3438. return;
  3439. clean_up:
  3440. if (h->reply_pool)
  3441. pci_free_consistent(h->pdev, h->reply_pool_size,
  3442. h->reply_pool, h->reply_pool_dhandle);
  3443. kfree(h->blockFetchTable);
  3444. }
  3445. /*
  3446. * This is it. Register the PCI driver information for the cards we control
  3447. * the OS will call our registered routines when it finds one of our cards.
  3448. */
  3449. static int __init hpsa_init(void)
  3450. {
  3451. int err;
  3452. /* Start the scan thread */
  3453. hpsa_scan_thread = kthread_run(hpsa_scan_func, NULL, "hpsa_scan");
  3454. if (IS_ERR(hpsa_scan_thread)) {
  3455. err = PTR_ERR(hpsa_scan_thread);
  3456. return -ENODEV;
  3457. }
  3458. err = pci_register_driver(&hpsa_pci_driver);
  3459. if (err)
  3460. kthread_stop(hpsa_scan_thread);
  3461. return err;
  3462. }
  3463. static void __exit hpsa_cleanup(void)
  3464. {
  3465. pci_unregister_driver(&hpsa_pci_driver);
  3466. kthread_stop(hpsa_scan_thread);
  3467. }
  3468. module_init(hpsa_init);
  3469. module_exit(hpsa_cleanup);